Issue
I must be missing something obvious here:
I'm running an instrumentation test case on a device, also with the help of Robotium. At some point my app under test causes the default e-mail compose activity to fire.
What I'd like to know is: how do I close that activity so to show back again my app activities? That is needed at least for one reason: after current test case completes, the email activity remains in foreground, and this prevents the next test case from running properly.
I tried to send the required back keys to the email activity - with this.sendKeys(KeyEvent.KEYCODE_BACK)
or solo.goBack()
- but with no luck.
If I run solo.getCurrentActivity()
or this.getActivity()
I get a reference to the topmost activity within my package, but no way to get a hold of the actual foreground activity. Also, the mentioned sendKeys/goBack
go straight to my activity, not the email one.
Same problem arises when my app launches the Play Store main activity.
I realise that the email activity is not part of my package under test, and this might be part of the problem.
So, is there a way to close the email ativity?
Edit
For now I'm sticking to @Joe Malin first suggestion:
In my ViewModel (or Controller), have a private static boolean _productionMode = true;
and have some helper method (better if in some inner helper class) used only during testing:
public void dontLaunchExternalApps()
{
_productionMode = false;
}
Then in some ViewModel regular method:
if (_productionMode)
{
launchSomeExternalApp();
}
else
{
// Do Nothing Or Set Some Internal Flag
}
Solution
There's no good way to do it. The Android instrumentation framework is really meant to unit test an Android component. Once control passes out of the component, some other framework has to take over.
Perhaps you can start the initial activity with extended data that tells the Activity to send a stub email Intent instead of the real one. The real problem is not closing the the email Activity, it's mocking it so that you get back from it what you expect to get back. Unfortunately, mocking is not supported in Android components. The way around that is to delegate most operations to POJO subclasses and then mock them.
Answered By - Joe Malin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.