Search in sources :

Example 1 with NoActivityResumedException

use of com.google.android.apps.common.testing.ui.espresso.NoActivityResumedException in project double-espresso by JakeWharton.

the class RootViewPicker method waitForAtLeastOneActivityToBeResumed.

@SuppressWarnings("unused")
private void waitForAtLeastOneActivityToBeResumed() {
    Collection<Activity> resumedActivities = activityLifecycleMonitor.getActivitiesInStage(Stage.RESUMED);
    if (resumedActivities.isEmpty()) {
        uiController.loopMainThreadUntilIdle();
        resumedActivities = activityLifecycleMonitor.getActivitiesInStage(Stage.RESUMED);
    }
    if (resumedActivities.isEmpty()) {
        List<Activity> activities = Lists.newArrayList();
        for (Stage s : EnumSet.range(Stage.PRE_ON_CREATE, Stage.RESTARTED)) {
            activities.addAll(activityLifecycleMonitor.getActivitiesInStage(s));
        }
        if (activities.isEmpty()) {
            throw new RuntimeException("No activities found. Did you forget to launch the activity " + "by calling getActivity() or startActivitySync or similar?");
        }
        // well at least there are some activities in the pipeline - lets see if they resume.
        long[] waitTimes = { 10, 50, 100, 500, TimeUnit.SECONDS.toMillis(2), TimeUnit.SECONDS.toMillis(30) };
        for (int waitIdx = 0; waitIdx < waitTimes.length; waitIdx++) {
            Log.w(TAG, "No activity currently resumed - waiting: " + waitTimes[waitIdx] + "ms for one to appear.");
            uiController.loopMainThreadForAtLeast(waitTimes[waitIdx]);
            resumedActivities = activityLifecycleMonitor.getActivitiesInStage(Stage.RESUMED);
            if (!resumedActivities.isEmpty()) {
                // one of the pending activities has resumed
                return;
            }
        }
        throw new NoActivityResumedException("No activities in stage RESUMED. Did you forget to " + "launch the activity. (test.getActivity() or similar)?");
    }
}
Also used : NoActivityResumedException(com.google.android.apps.common.testing.ui.espresso.NoActivityResumedException) Activity(android.app.Activity) Stage(com.google.android.apps.common.testing.testrunner.Stage)

Example 2 with NoActivityResumedException

use of com.google.android.apps.common.testing.ui.espresso.NoActivityResumedException in project double-espresso by JakeWharton.

the class KeyEventActionIntegrationTest method testClickOnBackFromFragment.

@SuppressWarnings("unchecked")
// uses native fragments.
@SdkSuppress(versions = { 7, 8, 10 }, bugId = -1)
@FlakyTest
public void testClickOnBackFromFragment() {
    Intent fragmentStack = new Intent().setClassName(getInstrumentation().getTargetContext(), "com.google.android.apps.common.testing.ui.testapp.FragmentStack");
    fragmentStack.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    getInstrumentation().startActivitySync(fragmentStack);
    onView(allOf(withParent(withId(R.id.simple_fragment)), isAssignableFrom(TextView.class))).check(matches(withText(containsString("#1"))));
    try {
        pressBack();
        fail("Should have thrown NoActivityResumedException");
    } catch (NoActivityResumedException expected) {
    }
    getInstrumentation().startActivitySync(fragmentStack);
    onView(withId(R.id.new_fragment)).perform(click()).perform(click()).perform(click());
    onView(allOf(withParent(withId(R.id.simple_fragment)), isAssignableFrom(TextView.class))).check(matches(withText(containsString("#4"))));
    pressBack();
    onView(allOf(withParent(withId(R.id.simple_fragment)), isAssignableFrom(TextView.class))).check(matches(withText(containsString("#3"))));
    pressBack();
    onView(allOf(withParent(withId(R.id.simple_fragment)), isAssignableFrom(TextView.class))).check(matches(withText(containsString("#2"))));
    pressBack();
    onView(allOf(withParent(withId(R.id.simple_fragment)), isAssignableFrom(TextView.class))).check(matches(withText(containsString("#1"))));
    try {
        pressBack();
        fail("Should have thrown NoActivityResumedException");
    } catch (NoActivityResumedException expected) {
    }
}
Also used : NoActivityResumedException(com.google.android.apps.common.testing.ui.espresso.NoActivityResumedException) Intent(android.content.Intent) TextView(android.widget.TextView) FlakyTest(android.test.FlakyTest) SdkSuppress(com.google.android.apps.common.testing.testrunner.annotations.SdkSuppress)

Example 3 with NoActivityResumedException

use of com.google.android.apps.common.testing.ui.espresso.NoActivityResumedException in project double-espresso by JakeWharton.

the class RootMatchers method getResumedActivityTokens.

private static List<IBinder> getResumedActivityTokens() {
    ActivityLifecycleMonitor activityLifecycleMonitor = ActivityLifecycleMonitorRegistry.getInstance();
    Collection<Activity> resumedActivities = activityLifecycleMonitor.getActivitiesInStage(Stage.RESUMED);
    if (resumedActivities.isEmpty()) {
        throw new NoActivityResumedException("At least one activity should be in RESUMED stage.");
    }
    List<IBinder> tokens = Lists.newArrayList();
    for (Activity activity : resumedActivities) {
        tokens.add(activity.getWindow().getDecorView().getApplicationWindowToken());
    }
    return tokens;
}
Also used : IBinder(android.os.IBinder) NoActivityResumedException(com.google.android.apps.common.testing.ui.espresso.NoActivityResumedException) Activity(android.app.Activity) ActivityLifecycleMonitor(com.google.android.apps.common.testing.testrunner.ActivityLifecycleMonitor)

Aggregations

NoActivityResumedException (com.google.android.apps.common.testing.ui.espresso.NoActivityResumedException)3 Activity (android.app.Activity)2 Intent (android.content.Intent)1 IBinder (android.os.IBinder)1 FlakyTest (android.test.FlakyTest)1 TextView (android.widget.TextView)1 ActivityLifecycleMonitor (com.google.android.apps.common.testing.testrunner.ActivityLifecycleMonitor)1 Stage (com.google.android.apps.common.testing.testrunner.Stage)1 SdkSuppress (com.google.android.apps.common.testing.testrunner.annotations.SdkSuppress)1