0

I writing test cases for Android apps using Robotium. One of my test cases opens a file (e.g., an image) which causes the opening of another activity from another app to display the image.

This far, everythings fine. However, afterwards, I want to get back to my activity under test, as I want to test further functionality.

How can I acchieve this?

Neither

solo.sendKey(android.view.KeyEvent.KEYCODE_BACK);

nor

solo.goBack();

nor

solo.getCurrentActivity().onBackPressed()

works because the activity displaying the image belongs to another app and thus, to another process.

Anything else I could try?

Claas Wilke
  • 1,951
  • 1
  • 18
  • 29
  • 1
    No, Its not possible, Unless you have INJECT_EVENT permisssion on other apps and the app is signed with your keystore else it not possible. – user370305 Jun 22 '12 at 11:15

4 Answers4

4

You are doing acceptance tests yes, this is to see that the UI of your system works.

You don't need to test that the image is opened ( as this is outside of your app scope ).

however it would be recommended to have separate unit tests that assert that you call the correct intent (or whatever code snippet) to open this image.

That way you have the code covered by a test and non of the problems of testing other peoples code.

Blundell
  • 75,855
  • 30
  • 208
  • 233
  • 1
    Actually what I do is that I use unit tests as a workload for performance measurements. Thus, I exactly want to open the image for some reason. I just want to know, whether or not it is possible to get back to my activity under test using robotium. – Claas Wilke Jun 22 '12 at 11:00
1

I suggest you to restart the target activity to reach the initial state :

..... mSolo.finishOpenedActivities(); setActivity(null); mSolo = new Solo(mInstrumentation, getActivity()); ....

xena
  • 31
  • 3
1

For me, this does the job:

mSolo.goBackToActivity("MainActivity");

In my case I open a URL in a browser, and then I get back to the activity under test. The only drawback (so far) is that the other application remains in the backstack, but probably there is a fix for that too.

I assume this method has been added after this question has been asked.

dragi
  • 3,385
  • 5
  • 22
  • 40
0

If you are able to execute adb commands and you know which application was started by this intent, you can do:

adb shell am force-stop <package-of-started-app>

Then you get back to the previous activity.

Note: This does not work if there are multiple possible apps for this intent and the app selection dialog is shown. In this case you have to set the default app for this intent manually.

sascha
  • 1,951
  • 1
  • 11
  • 3