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

The following examples show how to use android.support.test.runner.lifecycle.ActivityLifecycleMonitorRegistry. 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: simple-stack   Source File: TestUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Gets an Activity in the RESUMED stage.
 * <p>
 * This method should never be called from the Main thread. In certain situations there might
 * be more than one Activities in RESUMED stage, but only one is returned.
 * See {@link ActivityLifecycleMonitor}.
 */
public static Activity getCurrentActivity()
        throws IllegalStateException {
    // The array is just to wrap the Activity and be able to access it from the Runnable.
    final Activity[] resumedActivity = new Activity[1];

    getInstrumentation().runOnMainSync(new Runnable() {
        public void run() {
            Collection resumedActivities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(RESUMED);
            if(resumedActivities.iterator().hasNext()) {
                resumedActivity[0] = (Activity) resumedActivities.iterator().next();
            } else {
                throw new IllegalStateException("No Activity in stage RESUMED");
            }
        }
    });
    return resumedActivity[0];
}
 
Example 2
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 3
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 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
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 6
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 7
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 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
Source Project: mangosta-android   Source File: BaseInstrumentedTest.java    License: Apache License 2.0 5 votes vote down vote up
protected Activity getCurrentActivity() {
    InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
        public void run() {
            Collection<Activity> resumedActivities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(RESUMED);
            if (resumedActivities.iterator().hasNext()) {
                mCurrentActivity = resumedActivities.iterator().next();
            }
        }
    });
    return mCurrentActivity;
}
 
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: 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 12
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 13
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 14
Source Project: permissive   Source File: PermissiveTestRule.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void beforeActivityLaunched() {
  if (alreadyInitialized) return;
  grantAllPermissions();
  PermissiveTesting permissiveTesting = PermissiveTesting.init();
  for(String fakePerm : fakePermissions) {
    permissiveTesting.grantFakePermission(fakePerm);
  }
  ActivityLifecycleMonitorRegistry.getInstance().addLifecycleCallback(activityLifecycleCallback);
  super.beforeActivityLaunched();
}
 
Example 15
Source Project: permissive   Source File: PermissiveTestRule.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void afterActivityFinished() {
  super.afterActivityFinished();
  InstrumentationRegistry.getInstrumentation().waitForIdleSync();
  ActivityLifecycleMonitorRegistry.getInstance().removeLifecycleCallback(activityLifecycleCallback);
  PermissiveTesting.release();
  alreadyInitialized = false;
}
 
Example 16
public WaitActivityIsResumedIdlingResource(String activityToWaitClassName) {
    instance = ActivityLifecycleMonitorRegistry.getInstance();
    this.activityToWaitClassName = activityToWaitClassName;
}
 
Example 17
ActivityFinisher() {
    this.mLifecycleMonitor = ActivityLifecycleMonitorRegistry.getInstance();
}
 
Example 18
Source Project: CleanGUITestArchitecture   Source File: ActivityFinisher.java    License: MIT License 4 votes vote down vote up
private ActivityFinisher() {
    this.activityLifecycleMonitor = ActivityLifecycleMonitorRegistry.getInstance();
}