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

The following examples show how to use android.support.test.runner.lifecycle.Stage. These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: SmoothClicker   Source File: ItIntroScreensActivity.java    License: 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 2
Source Project: SmoothClicker   Source File: ItClickerActivity.java    License: 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 3
@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 4
Source Project: material-activity-chooser   Source File: ActivityUtils.java    License: 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 5
@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 6
Source Project: px-android   Source File: ActivityIdlingResource.java    License: 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 7
Source Project: px-android   Source File: CurrentActivityFetcher.java    License: 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 8
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 9
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 10
Source Project: espresso-macchiato   Source File: EspCloseAllActivitiesFunction.java    License: 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 11
Source Project: espresso-macchiato   Source File: EspCloseAllActivitiesFunction.java    License: 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
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 13
Source Project: SmoothClicker   Source File: ItSettingsActivity.java    License: 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 14
Source Project: SmoothClicker   Source File: ItNotificationsManager.java    License: 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 15
Source Project: SmoothClicker   Source File: ItServiceClicker.java    License: 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 16
Source Project: CleanGUITestArchitecture   Source File: ActivityFinisher.java    License: 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 17
Source Project: permissive   Source File: PermissiveTestRule.java    License: 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);
  }
}