Java Code Examples for android.app.Instrumentation#addMonitor()
The following examples show how to use
android.app.Instrumentation#addMonitor() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestUtil.java From android-browser-helper with Apache License 2.0 | 6 votes |
/** * Waits until {@link TestBrowser} is launched and resumed, and returns it. * * @param launchRunnable Runnable that should start the activity. */ public static TestBrowser getBrowserActivityWhenLaunched(Runnable launchRunnable) { Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor(TestBrowser.class.getName(), null, false); launchRunnable.run(); TestBrowser activity = (TestBrowser) instrumentation.waitForMonitorWithTimeout(monitor, 3000); assertNotNull("TestBrowser wasn't launched", activity); // ActivityMonitor is triggered in onCreate and in onResume, which can lead to races when // launching several activity instances. So wait for onResume before returning. boolean resumed = activity.waitForResume(3000); assertTrue("TestBrowser didn't reach onResume", resumed); return activity; }
Example 2
Source File: ProcessOwnerTest.java From android_9.0.0_r45 with Apache License 2.0 | 6 votes |
@Test public void testNavigation() throws Throwable { FragmentActivity firstActivity = setupObserverOnResume(); Instrumentation.ActivityMonitor monitor = new Instrumentation.ActivityMonitor( NavigationTestActivitySecond.class.getCanonicalName(), null, false); Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); instrumentation.addMonitor(monitor); Intent intent = new Intent(firstActivity, NavigationTestActivitySecond.class); firstActivity.finish(); firstActivity.startActivity(intent); FragmentActivity secondActivity = (FragmentActivity) monitor.waitForActivity(); assertThat("Failed to navigate", secondActivity, notNullValue()); checkProcessObserverSilent(secondActivity); }
Example 3
Source File: PartiallyCoveredActivityTest.java From android_9.0.0_r45 with Apache License 2.0 | 6 votes |
private FragmentActivity launchDialog() throws Throwable { Instrumentation.ActivityMonitor monitor = new Instrumentation.ActivityMonitor( NavigationDialogActivity.class.getCanonicalName(), null, false); Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); instrumentation.addMonitor(monitor); FragmentActivity activity = activityRule.getActivity(); Intent intent = new Intent(activity, NavigationDialogActivity.class); // disabling animations helps with less flaky API 16 tests intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); activity.startActivity(intent); FragmentActivity fragmentActivity = (FragmentActivity) monitor.waitForActivity(); TestUtils.waitTillResumed(fragmentActivity, activityRule); return fragmentActivity; }
Example 4
Source File: LiveDataOnSaveInstanceStateTest.java From android_9.0.0_r45 with Apache License 2.0 | 6 votes |
private FragmentActivity launchDialog() throws Throwable { Instrumentation.ActivityMonitor monitor = new Instrumentation.ActivityMonitor( NavigationDialogActivity.class.getCanonicalName(), null, false); Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); instrumentation.addMonitor(monitor); FragmentActivity activity = mActivityTestRule.getActivity(); // helps with less flaky API 16 tests Intent intent = new Intent(activity, NavigationDialogActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); activity.startActivity(intent); FragmentActivity fragmentActivity = (FragmentActivity) monitor.waitForActivity(); TestUtils.waitTillResumed(fragmentActivity, mActivityTestRule); return fragmentActivity; }
Example 5
Source File: TestUtil.java From custom-tabs-client with Apache License 2.0 | 6 votes |
/** * Waits until {@link TestBrowser} is launched and resumed, and returns it. * * @param launchRunnable Runnable that should start the activity. */ public static TestBrowser getBrowserActivityWhenLaunched(Runnable launchRunnable) { Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor(TestBrowser.class.getName(), null, false); launchRunnable.run(); TestBrowser activity = (TestBrowser) instrumentation.waitForMonitorWithTimeout(monitor, 3000); assertNotNull("TestBrowser wasn't launched", activity); // ActivityMonitor is triggered in onCreate and in onResume, which can lead to races when // launching several activity instances. So wait for onResume before returning. boolean resumed = activity.waitForResume(3000); assertTrue("TestBrowser didn't reach onResume", resumed); return activity; }
Example 6
Source File: ActivityListener.java From droidtestrec with Apache License 2.0 | 6 votes |
public ActivityListener(Instrumentation instr, Activity activity, long uniqueId) { this.instr = instr; IntentFilter filter = null; this.monitor = instr.addMonitor(filter, null, false); this.activity = activity; this.uniqueId = uniqueId; eventWriter = new EventWriter(uniqueId, this); activityProcessor = new ActivityProcessor(uniqueId, instr, eventWriter); activityProcessor.processActivity(activity); try { fResumed = Activity.class.getDeclaredField("mResumed"); fResumed.setAccessible(true); fStopped = Activity.class.getDeclaredField("mStopped"); fStopped.setAccessible(true); } catch (NoSuchFieldException e) { Log.e(ActivityProcessor.ANDRIOD_TEST_RECORDER, "NoSuchFieldException", e); } }
Example 7
Source File: ProcessOwnerTest.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
@Test public void testNavigationToNonSupport() throws Throwable { FragmentActivity firstActivity = setupObserverOnResume(); Instrumentation.ActivityMonitor monitor = new Instrumentation.ActivityMonitor( NonSupportActivity.class.getCanonicalName(), null, false); Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); instrumentation.addMonitor(monitor); Intent intent = new Intent(firstActivity, NonSupportActivity.class); firstActivity.finish(); firstActivity.startActivity(intent); NonSupportActivity secondActivity = (NonSupportActivity) monitor.waitForActivity(); assertThat("Failed to navigate", secondActivity, notNullValue()); checkProcessObserverSilent(secondActivity); }
Example 8
Source File: ProcessOwnerTest.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
@Test public void testPressHomeButton() throws Throwable { setupObserverOnResume(); Instrumentation.ActivityMonitor monitor = new Instrumentation.ActivityMonitor( NavigationDialogActivity.class.getCanonicalName(), null, false); Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); instrumentation.addMonitor(monitor); NavigationTestActivityFirst activity = activityTestRule.getActivity(); activity.startActivity(new Intent(activity, NavigationDialogActivity.class)); FragmentActivity dialogActivity = (FragmentActivity) monitor.waitForActivity(); checkProcessObserverSilent(dialogActivity); List<Event> events = Collections.synchronizedList(new ArrayList<>()); LifecycleObserver collectingObserver = new LifecycleObserver() { @OnLifecycleEvent(Event.ON_ANY) public void onStateChanged(@SuppressWarnings("unused") LifecycleOwner provider, Event event) { events.add(event); } }; addProcessObserver(collectingObserver); events.clear(); assertThat(activity.moveTaskToBack(true), is(true)); Thread.sleep(ProcessLifecycleOwner.TIMEOUT_MS * 2); assertThat(events.toArray(), is(new Event[]{ON_PAUSE, ON_STOP})); events.clear(); Context context = InstrumentationRegistry.getContext(); context.startActivity(new Intent(activity, NavigationDialogActivity.class) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); waitTillResumed(dialogActivity, activityTestRule); assertThat(events.toArray(), is(new Event[]{ON_START, ON_RESUME})); removeProcessObserver(collectingObserver); dialogActivity.finish(); }
Example 9
Source File: TestUtils.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") static <T extends Activity> T recreateActivity(final T activity, ActivityTestRule rule) throws Throwable { ActivityMonitor monitor = new ActivityMonitor( activity.getClass().getCanonicalName(), null, false); Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); instrumentation.addMonitor(monitor); rule.runOnUiThread(activity::recreate); T result; // this guarantee that we will reinstall monitor between notifications about onDestroy // and onCreate //noinspection SynchronizationOnLocalVariableOrMethodParameter synchronized (monitor) { do { // the documetation says "Block until an Activity is created // that matches this monitor." This statement is true, but there are some other // true statements like: "Block until an Activity is destoyed" or // "Block until an Activity is resumed"... // this call will release synchronization monitor's monitor result = (T) monitor.waitForActivityWithTimeout(TIMEOUT_MS); if (result == null) { throw new RuntimeException("Timeout. Failed to recreate an activity"); } } while (result == activity); } return result; }
Example 10
Source File: ViewModelTest.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
private ViewModelActivity recreateActivity() throws Throwable { Instrumentation.ActivityMonitor monitor = new Instrumentation.ActivityMonitor( ViewModelActivity.class.getCanonicalName(), null, false); Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); instrumentation.addMonitor(monitor); final ViewModelActivity previous = mActivityRule.getActivity(); mActivityRule.runOnUiThread(new Runnable() { @Override public void run() { previous.recreate(); } }); ViewModelActivity result; // this guarantee that we will reinstall monitor between notifications about onDestroy // and onCreate //noinspection SynchronizationOnLocalVariableOrMethodParameter synchronized (monitor) { do { // the documentation says "Block until an Activity is created // that matches this monitor." This statement is true, but there are some other // true statements like: "Block until an Activity is destroyed" or // "Block until an Activity is resumed"... // this call will release synchronization monitor's monitor result = (ViewModelActivity) monitor.waitForActivityWithTimeout(4000); if (result == null) { throw new RuntimeException("Timeout. Failed to recreate an activity"); } } while (result == previous); } return result; }
Example 11
Source File: SampleTest.java From androidtestdebug with MIT License | 5 votes |
public void test点击链接() { final Instrumentation inst = getInstrumentation(); IntentFilter intentFilter = new IntentFilter(Intent.ACTION_VIEW); intentFilter.addDataScheme("http"); intentFilter.addCategory(Intent.CATEGORY_BROWSABLE); View link = this.getActivity().findViewById(R.id.link); ActivityMonitor monitor = inst.addMonitor( intentFilter, null, false); assertEquals(0, monitor.getHits()); TouchUtils.clickView(this, link); monitor.waitForActivityWithTimeout(5000); assertEquals(1, monitor.getHits()); inst.removeMonitor(monitor); }