Reputation: 1664
I am looking at tests on Android activities. I would like to test the activity lifecycle and particularly the end of my activity to start another activity (I am testing the end of the first activity, not the start of the second). I am using ActivityInstrumentationTestCase2 and Android Espresso with JUnit 4 tests.
I have tried to assert that once I trigger the end of an activity, by pressing a button on the screen, the activity is paused or not visible but I ran out of luck so far. Is there any good practice to test transitions between activities?
Upvotes: 0
Views: 198
Reputation: 1795
You should take a look at https://developer.android.com/training/activity-testing/activity-functional-testing.html#activitymonitor.
Basically you'll need to add and register an ActivityMonitor
in your instrumented functional test, which will be set up to listen for your second Activity
. Once the trigger happens (ie a button being clicked in your test case) you will just use waitForActivityWithTimeout(int)
to get the instance of the second Activity
.
If the call times out, or the result is null, then you know your second Activity
hasn't been started. You can also assert on the Activity
instances whether they're finishing/destroyed/etc depending on what you are expecting to happen with them.
Upvotes: 1