Java Code Examples for android.app.Instrumentation#ActivityMonitor

The following examples show how to use android.app.Instrumentation#ActivityMonitor . 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: ProcessOwnerTest.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@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 2
Source File: TestUtil.java    From android-browser-helper with Apache License 2.0 6 votes vote down vote up
/**
 * 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 3
Source File: MainActivityTest.java    From SimpleAlertDialog-for-Android with Apache License 2.0 6 votes vote down vote up
public void testLaunchAboutActivity() {
    Instrumentation.ActivityMonitor monitorAbout = new Instrumentation.ActivityMonitor(AboutActivity.class.getCanonicalName(), null, false);
    getInstrumentation().addMonitor(monitorAbout);

    mainActivity = launchActivity("com.simplealertdialog.sample.demos", MainActivity.class, null);
    getInstrumentation().waitForIdleSync();

    mainActivity.openOptionsMenu();
    getInstrumentation().waitForIdleSync();

    getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
    getInstrumentation().waitForIdleSync();

    getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_ENTER);
    getInstrumentation().waitForIdleSync();

    AboutActivity aboutActivity = (AboutActivity) getInstrumentation().waitForMonitorWithTimeout(monitorAbout, 3000);
    assertNotNull(aboutActivity);
    assertTrue(getInstrumentation().checkMonitorHit(monitorAbout, 1));

    aboutActivity.finish();
}
 
Example 4
Source File: DebugOverlayInstrumentedTest.java    From DebugOverlay-Android with Apache License 2.0 6 votes vote down vote up
void waitForOverlay(long millis) {
    if (!testSystemLayer()) {
        Instrumentation.ActivityMonitor monitor = new Instrumentation.ActivityMonitor(MainActivity.class.getCanonicalName(),
                null, false);
        getInstrumentation().addMonitor(monitor);
        getActivityRule().launchActivity(new Intent(getApplication(), MainActivity.class));
        try {
            monitor.waitForActivityWithTimeout(5000);
        } finally {
            getInstrumentation().removeMonitor(monitor);
        }
    }
    try {
        Thread.currentThread().sleep(millis);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
 
Example 5
Source File: TestUtil.java    From custom-tabs-client with Apache License 2.0 6 votes vote down vote up
/**
 * 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: ActivityUtilsTest.java    From caffeine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void testLaunchActivity2() {
    assertNotNull(getActivity());

    Instrumentation.ActivityMonitor activityMonitor = getInstrumentation().addMonitor(ActivityTwo.class.getName(), null, false);
    ActivityUtils.launchActivity(getActivity(), ActivityTwo.class, true);
    Activity activityTwo = activityMonitor.waitForActivity();
    SystemClock.sleep(100);

    assertTrue(ActivityTwo.activityStarted);
    activityTwo.finish();
}
 
Example 7
Source File: ActivityUtilsTest.java    From caffeine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void testLaunchActivity() {
    assertNotNull(getActivity());

    Instrumentation.ActivityMonitor activityMonitor = getInstrumentation().addMonitor(ActivityTwo.class.getName(), null, false);
    ActivityUtils.launchActivity(getActivity(), ActivityTwo.class, false);
    Activity activityTwo = activityMonitor.waitForActivity();
    SystemClock.sleep(100);

    assertTrue(ActivityTwo.activityStarted);
    activityTwo.finish();
}
 
Example 8
Source File: BaseRxLoaderActivityTest.java    From rxloader with Apache License 2.0 5 votes vote down vote up
protected T recreateActivity() {
    Instrumentation.ActivityMonitor activityMonitor = new Instrumentation.ActivityMonitor(getActivity().getClass().getName(), null, false);
    getInstrumentation().addMonitor(activityMonitor);
    getInstrumentation().runOnMainSync(new Runnable() {
        @Override
        public void run() {
            getActivity().recreate();
        }
    });
    activityMonitor.waitForActivity();
    getInstrumentation().waitForIdleSync();
    return (T) activityMonitor.getLastActivity();
}
 
Example 9
Source File: WelcomeActivityAndroidTest.java    From welcome-android with Apache License 2.0 5 votes vote down vote up
@Override
public void initActivity() {
    super.initActivity();
    welcomeMonitor = new Instrumentation.ActivityMonitor(DefaultWelcomeActivity.class.getName(), null, false);
    instrumentation.addMonitor(welcomeMonitor);
    new WelcomeHelper(activity, DefaultWelcomeActivity.class).forceShow();
    welcomeActivity = (WelcomeActivity) instrumentation.waitForMonitor(welcomeMonitor);
}
 
Example 10
Source File: ActivityTest.java    From welcome-android with Apache License 2.0 5 votes vote down vote up
@Before
public void initActivity() {
    instrumentation = InstrumentationRegistry.getInstrumentation();
    Instrumentation.ActivityMonitor monitor = new Instrumentation.ActivityMonitor(TestActivity.class.getName(), null, false);
    instrumentation.addMonitor(monitor);

    Intent intent = new Intent(instrumentation.getTargetContext(), TestActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    instrumentation.startActivitySync(intent);

    activity = instrumentation.waitForMonitor(monitor);
    assertNotNull(activity);
}
 
Example 11
Source File: DebugOverlayInstrumentedTest.java    From DebugOverlay-Android with Apache License 2.0 5 votes vote down vote up
@Test
public void startSecondActivity() {
    getInstrumentation().runOnMainSync(new Runnable() {
        @Override
        public void run() {
            debugOverlay = new DebugOverlay.Builder(getApplication())
                    .allowSystemLayer(testSystemLayer())
                    .build();
            debugOverlay.install();
        }
    });

    waitForOverlay();

    Instrumentation.ActivityMonitor monitor = new Instrumentation.ActivityMonitor(ScrollingActivity.class.getCanonicalName(),
            null, false);
    getInstrumentation().addMonitor(monitor);
    try {
        onView(withId(com.ms_square.debugoverlay.sample.R.id.fab)).perform(click());
        Activity nextActivity = monitor.waitForActivityWithTimeout(5000);
        assertThat(nextActivity, Matchers.is(Matchers.notNullValue()));

        takeActivityScreenShot(nextActivity);

        nextActivity.finish();
    } finally {
        getInstrumentation().removeMonitor(monitor);
    }
}
 
Example 12
Source File: ViewModelTest.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
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 13
Source File: ApiDemoActivityTest.java    From gdk-apidemo-sample with Apache License 2.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception {
    super.setUp();
    mCardsActivityMonitor = new Instrumentation.ActivityMonitor(
        CardsActivity.class.getName(), null, false);
    mDetectorActivityMonitor = new Instrumentation.ActivityMonitor(
        SelectGestureDemoActivity.class.getName(), null, false);
    mThemingActivityMonitor = new Instrumentation.ActivityMonitor(
        ThemingActivity.class.getName(), null, false);
    getInstrumentation().addMonitor(mCardsActivityMonitor);
    getInstrumentation().addMonitor(mDetectorActivityMonitor);
    getInstrumentation().addMonitor(mThemingActivityMonitor);
}
 
Example 14
Source File: ProcessOwnerTest.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@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 15
Source File: ProcessOwnerTest.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@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 16
Source File: ChatActivityTest.java    From Android with MIT License 4 votes vote down vote up
@Before
public void startTest() {
    Instrumentation.ActivityMonitor am = new Instrumentation.ActivityMonitor("connect.ui.activity.chat.ChatActivity", null, false);
    getInstrumentation().addMonitor(am);
    getInstrumentation().waitForMonitor(am);
}
 
Example 17
Source File: SingleSetTest.java    From Android with MIT License 4 votes vote down vote up
@Before
public void startTest() {
    Instrumentation.ActivityMonitor am = new Instrumentation.ActivityMonitor("connect.ui.activity.chat.set.SingleSetActivity", null, false);
    getInstrumentation().addMonitor(am);
    getInstrumentation().waitForMonitor(am);
}
 
Example 18
Source File: RoomListTest.java    From Android with MIT License 4 votes vote down vote up
@Before
public void startTest() {
    Instrumentation.ActivityMonitor am = new Instrumentation.ActivityMonitor("connect.ui.activity.home.HomeActivity", null, false);
    getInstrumentation().addMonitor(am);
    getInstrumentation().waitForMonitor(am);
}
 
Example 19
Source File: GroupSetTest.java    From Android with MIT License 4 votes vote down vote up
@Before
public void startTest() {
    Instrumentation.ActivityMonitor am = new Instrumentation.ActivityMonitor("connect.ui.activity.chat.set.GroupSetActivity", null, false);
    getInstrumentation().addMonitor(am);
    getInstrumentation().waitForMonitor(am);
}
 
Example 20
Source File: InstrumentationActivityMonitorAssert.java    From assertj-android with Apache License 2.0 4 votes vote down vote up
public InstrumentationActivityMonitorAssert(Instrumentation.ActivityMonitor actual) {
  super(actual, InstrumentationActivityMonitorAssert.class);
}