android.support.test.runner.lifecycle.Stage Java Examples

The following examples show how to use android.support.test.runner.lifecycle.Stage. 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: ActivityUtils.java    From material-activity-chooser with Apache License 2.0 6 votes vote down vote up
/**
 * Gets an instance of the currently active (displayed) activity.
 * @param activityTestRule test rule
 * @param <T> activity class
 * @return activity instance
 */
public static  <T extends Activity> T getCurrentActivity(@NonNull ActivityTestRule activityTestRule) {
    getInstrumentation().waitForIdleSync();
    final Activity[] activity = new Activity[1];
    try {
        activityTestRule.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                java.util.Collection<Activity> activites = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);
                activity[0] = Iterables.getOnlyElement(activites);
            }});
    } catch (Throwable throwable) {
        throwable.printStackTrace();
    }
    //noinspection unchecked
    return (T) activity[0];
}
 
Example #2
Source File: ActivityFinisher.java    From material-activity-chooser with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    List<Activity> activities = new ArrayList<>();

    for (Stage s : EnumSet.range(Stage.CREATED, Stage.STOPPED)) {
        activities.addAll(mLifecycleMonitor.getActivitiesInStage(s));
    }

    Log.i(TAG, "Activities that are still in CREATED to STOPPED: " + activities.size());

    for (Activity activity : activities) {
        if (!activity.isFinishing()) {
            try {
                Log.i(TAG, "Finishing mActivityRule: " + activity);
                activity.finish();
            } catch (RuntimeException e) {
                Log.e(TAG, "Failed to finish mActivityRule.", e);
            }
        }
    }
}
 
Example #3
Source File: ItClickerActivity.java    From SmoothClicker with MIT License 6 votes vote down vote up
/**
 * Retrieves the on going activity
 * @return Activity - The current activity
 */
private static Activity getActivityInstance(){
    getInstrumentation().runOnMainSync(new Runnable() {
        public void run() {
            Collection resumedActivities = ActivityLifecycleMonitorRegistry.getInstance()
                    .getActivitiesInStage(Stage.RESUMED);
            if (resumedActivities.iterator().hasNext()) {
                mResumedActivity = (Activity) resumedActivities.iterator().next();
            }
            if (resumedActivities.size() <= 0 ){
                mResumedActivity = null;
            }
        }
    });
    return mResumedActivity;
}
 
Example #4
Source File: ItIntroScreensActivity.java    From SmoothClicker with MIT License 6 votes vote down vote up
/**
 * Retrieves the on going activity
 * @return Activity - The current activity
 */
private static Activity getActivityInstance(){
    getInstrumentation().runOnMainSync(new Runnable() {
        public void run() {
            Collection resumedActivities = ActivityLifecycleMonitorRegistry.getInstance()
                    .getActivitiesInStage(Stage.RESUMED);
            if (resumedActivities.iterator().hasNext()) {
                mResumedActivity = (Activity) resumedActivities.iterator().next();
            }
            if (resumedActivities.size() <= 0 ){
                mResumedActivity = null;
            }
        }
    });
    return mResumedActivity;
}
 
Example #5
Source File: ItSettingsActivity.java    From SmoothClicker with MIT License 5 votes vote down vote up
/**
 * Retrieves the on going activity
 * @return Activity - The current activity
 */
private static Activity getActivityInstance(){
    getInstrumentation().runOnMainSync(new Runnable() {
        public void run() {
            Collection resumedActivities = ActivityLifecycleMonitorRegistry.getInstance()
                    .getActivitiesInStage(Stage.RESUMED);
            if (resumedActivities.iterator().hasNext()) {
                mResumedActivity = (Activity) resumedActivities.iterator().next();
            }
        }
    });
    return mResumedActivity;
}
 
Example #6
Source File: ActivityFinisher.java    From CleanGUITestArchitecture with MIT License 5 votes vote down vote up
@Override
public void run() {
    final List<Activity> activities = new ArrayList<>();

    for (final Stage stage : EnumSet.range(Stage.CREATED, Stage.STOPPED)) {
        activities.addAll(activityLifecycleMonitor.getActivitiesInStage(stage));
    }

    for (final Activity activity : activities) {
        if (!activity.isFinishing()) {
            activity.finish();
        }
    }
}
 
Example #7
Source File: ItServiceClicker.java    From SmoothClicker with MIT License 5 votes vote down vote up
/**
 * Retrieves the on going activity
 * @return Activity - The current activity
 */
private static Activity getActivityInstance(){
    getInstrumentation().runOnMainSync(new Runnable() {
        public void run() {
            Collection resumedActivities = ActivityLifecycleMonitorRegistry.getInstance()
                    .getActivitiesInStage(Stage.RESUMED);
            if (resumedActivities.iterator().hasNext()) {
                mResumedActivity = (Activity) resumedActivities.iterator().next();
            }
        }
    });
    return mResumedActivity;
}
 
Example #8
Source File: ItNotificationsManager.java    From SmoothClicker with MIT License 5 votes vote down vote up
/**
 * Retrieves the on going activity
 * @return Activity - The current activity
 */
private static Activity getActivityInstance(){
    getInstrumentation().runOnMainSync(new Runnable() {
        public void run() {
            Collection resumedActivities = ActivityLifecycleMonitorRegistry.getInstance()
                    .getActivitiesInStage(Stage.RESUMED);
            if ( resumedActivities.iterator().hasNext() ){
                mResumedActivity = (Activity) resumedActivities.iterator().next();
            }
        }
    });
    return mResumedActivity;
}
 
Example #9
Source File: PicassoIdlingResource.java    From android-espresso-revealed with Apache License 2.0 5 votes vote down vote up
@Override
public void onActivityLifecycleChanged(Activity activity, Stage stage) {
    switch (stage) {
        case RESUMED:
            mPicassoWeakReference = new WeakReference<>(Picasso.with(activity));
            break;
        case PAUSED:
            // Clean up reference
            mPicassoWeakReference = null;
            break;
        default: // NOP
    }
}
 
Example #10
Source File: WaitActivityIsResumedIdlingResource.java    From openshop.io-android with MIT License 5 votes vote down vote up
private boolean isActivityLaunched() {
    Collection<Activity> activitiesInStage = instance.getActivitiesInStage(Stage.RESUMED);
    for (Activity activity : activitiesInStage) {
        if (activity.getClass().getName().equals(activityToWaitClassName)) {
            return true;
        }
    }
    return false;
}
 
Example #11
Source File: EspCloseAllActivitiesFunction.java    From espresso-macchiato with MIT License 5 votes vote down vote up
private static boolean closeActivities(Instrumentation instrumentation) throws Exception {
    final AtomicReference<Boolean> activityClosed = new AtomicReference<>();
    InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
        public void run() {
            /* Activities in android v8 never get destroyed, only stay in Stage.STOPPED ,*/
            final Set<Activity> activities = getActivitiesInStages(Stage.RESUMED, Stage.STARTED, Stage.PAUSED, Stage.CREATED);
            activities.removeAll(getActivitiesInStages(Stage.DESTROYED));

            if (activities.size() > 0) {
                for (Activity activity : activities) {
                    if (activity.isFinishing()) {
                        Log.i("espressotools", "activity in finishing state " + activity);
                    } else {
                        Log.i("espressotools", "activity not finished " + activity);
                        activity.finish();
                    }
                }
                activityClosed.set(true);
            } else {
                activityClosed.set(false);
            }
        }
    });

    if (activityClosed.get()) {
        instrumentation.waitForIdleSync();
    }
    return activityClosed.get();
}
 
Example #12
Source File: EspCloseAllActivitiesFunction.java    From espresso-macchiato with MIT License 5 votes vote down vote up
public static Set<Activity> getActivitiesInStages(Stage... stages) {
    final Set<Activity> activities = Sets.newHashSet();
    final ActivityLifecycleMonitor instance = ActivityLifecycleMonitorRegistry.getInstance();
    for (Stage stage : stages) {
        activities.addAll(instance.getActivitiesInStage(stage));
    }
    return activities;
}
 
Example #13
Source File: Test1.java    From mobile-android-samples with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static Activity getCurrentActivity() {

        ActivityLifecycleMonitor registry = ActivityLifecycleMonitorRegistry.getInstance();
        Collection<Activity> activities = registry.getActivitiesInStage(Stage.RESUMED);

        if (activities.size() > 0) {
            return (Activity) activities.toArray()[0];
        }
        return null;
    }
 
Example #14
Source File: MapActivityTest.java    From mobile-android-samples with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static Activity getCurrentActivity() {

        ActivityLifecycleMonitor registry = ActivityLifecycleMonitorRegistry.getInstance();
        Collection<Activity> activities = registry.getActivitiesInStage(Stage.RESUMED);

        if (activities.size() > 0) {
            return (Activity) activities.toArray()[0];
        }
        return null;
    }
 
Example #15
Source File: CurrentActivityFetcher.java    From px-android with MIT License 5 votes vote down vote up
@Override
public void run() {
    final Collection<Activity> resumedActivity = ActivityLifecycleMonitorRegistry.getInstance()
        .getActivitiesInStage(Stage.RESUMED);

    if (resumedActivity.iterator().hasNext()) {
        currentActivity = resumedActivity.iterator().next();
    }
}
 
Example #16
Source File: ActivityIdlingResource.java    From px-android with MIT License 5 votes vote down vote up
private Activity getActivityInstance() {
    final Activity[] mActivity = new Activity[1];
    final Collection<Activity> resumedActivities =
        ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);
    Iterator<Activity> iterator = resumedActivities.iterator();
    if (iterator.hasNext()) {
        mActivity[0] = iterator.next();
    }
    return mActivity[0];
}
 
Example #17
Source File: PermissiveTestRule.java    From permissive with Apache License 2.0 4 votes vote down vote up
@Override
public void onActivityLifecycleChanged(Activity activity, Stage stage) {
  if (stage == Stage.PRE_ON_CREATE) {
    PermissiveContext.attachTo(activity);
  }
}