Android ActivityMonitor failure calls GetActivity () never returns

I have a test case that uses Instrumentation.ActivityMonitor to check if an intent has been sent. This works great when the test succeeds. If the statement fails, the following test case hangs on a call to getActivity () in setUp ().

Should I call someone to clean?

It seems to revolve around the launch of the Activity, but the ActivityMonitor will not catch it. That is, IntentFilter did not start. The test failed, but the new activity never rejects and seems to interfere with the next getActivity () call.

This problem is similar to another question , but this solution there (calling super.tearDown ()) did not fix my problem.

public class SimpleActivityTest extends ActivityInstrumentationTestCase2<SimpleActivity> { private SimpleActivity activity; @Override protected void setUp() throws Exception { super.setUp(); this.getInstrumentation().setInTouchMode(false); Intent intent = new Intent(); intent.putExtra("DATA_ITEM_1", 1); intent.putExtra("DATA_ITEM_2", 2); this.setActivityIntent(intent); this.activity = getActivity(); // this call hangs on second test } public void testOtherActivityCalled() { IntentFilter ifilter = new IntentFilter(Intent.ACTION_VIEW); ifilter.addDataScheme("http"); ifilter.addDataAuthority("some.domain.com", null); ifilter.addDataPath("foobar", PatternMatcher.PATTERN_PREFIX); ActivityMonitor activityMonitor = getInstrumentation().addMonitor( ifilter, null, false); activity.runOnUiThread(new Runnable() { @Override public void run() { // launch other activity somehow } }); getInstrumentation().waitForIdleSync(); Activity otherActivity = activityMonitor.waitForActivityWithTimeout(2000); assertNotNull(otherActivity); otherActivity.finish(); } public void testSomethingElse() { // This code will never run because getActivity() in setUp() will // never return } } 
+6
source share
2 answers

I suspect that the Runnable you created never comes out. Since it runs in a user interface thread, it never allows the Activity lifecycle to do what it needs. Is there any special reason why you are doing this from a user interface thread?

+1
source

I had similar problems, so I started using the Robotium library. I close all the actions in the tearDown () method:

 protected void setUp() throws Exception { super.setUp(); mSolo = new Solo(getInstrumentation(), getActivity()); } protected void tearDown() { mSolo.finishOpenedActivities(); mSolo.finishInactiveActivities(); } 
0
source

Source: https://habr.com/ru/post/907534/


All Articles