android.support.test.rule.ActivityTestRule Java Examples

The following examples show how to use android.support.test.rule.ActivityTestRule. 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: SnippetListActivityTests.java    From android-java-snippets-sample with MIT License 7 votes vote down vote up
public static void Disconnect(ActivityTestRule<SnippetListActivity> snippetListActivityTestRule) {
    SnippetListActivity snippetListActivity = snippetListActivityTestRule.launchActivity(null);

    openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext());

    // Espresso can't find menu items by id. We'll use the text property.
    onView(withText(R.string.disconnect_menu_item))
            .perform(click());

    intended(allOf(
            hasComponent(hasShortClassName(".SignInActivity")),
            toPackage("com.microsoft.graph.snippets")
    ));

    snippetListActivity.finish();
}
 
Example #2
Source File: SnippetListActivityTests.java    From android-java-snippets-sample with MIT License 6 votes vote down vote up
public static List<Integer> getSnippetsIndexes(ActivityTestRule<SnippetListActivity> snippetListActivityRule) {
    SnippetListActivity snippetListActivity = snippetListActivityRule.launchActivity(null);

    ListAdapter listAdapter = getListAdapter(snippetListActivity);
    int numItems = listAdapter.getCount();

    List<Integer> snippetIndexes = new ArrayList<>();

    // Get the index of items in the adapter that
    // are actual snippets and not categories, which don't have a Url
    for (int i = 0; i < numItems; i++) {
        if(((AbstractSnippet)listAdapter.getItem(i)).getUrl() != null) {
            snippetIndexes.add(i);
        }
    }

    snippetListActivity.finish();

    return snippetIndexes;
}
 
Example #3
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 #4
Source File: TestUtils.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
static <T extends Activity> T recreateActivity(final T activity, ActivityTestRule rule)
        throws Throwable {
    ActivityMonitor monitor = new ActivityMonitor(
            activity.getClass().getCanonicalName(), null, false);
    Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    instrumentation.addMonitor(monitor);
    rule.runOnUiThread(activity::recreate);
    T result;

    // this guarantee that we will reinstall monitor between notifications about onDestroy
    // and onCreate
    //noinspection SynchronizationOnLocalVariableOrMethodParameter
    synchronized (monitor) {
        do {
            // the documetation 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 destoyed" or
            // "Block until an Activity is resumed"...

            // this call will release synchronization monitor's monitor
            result = (T) monitor.waitForActivityWithTimeout(TIMEOUT_MS);
            if (result == null) {
                throw new RuntimeException("Timeout. Failed to recreate an activity");
            }
        } while (result == activity);
    }
    return result;
}
 
Example #5
Source File: InstrumentationTestUtils.java    From medic-gateway with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void recreateActivityFor(final ActivityTestRule testRule) {
	getInstrumentation().runOnMainSync(new Runnable() {
		public void run() {
			testRule.getActivity().recreate();
		}
	});
}
 
Example #6
Source File: LifecycleListenerTest.java    From unity-ads-android with Apache License 2.0 5 votes vote down vote up
protected Activity waitForActivityStart (final ActivityTestRule rule) {
	final ConditionVariable cv = new ConditionVariable();
	WebViewApp.setCurrentApp(new WebViewApp() {
		private boolean allowEvents = false;

		@Override
		public boolean sendEvent(Enum eventCategory, Enum eventId, Object... params) {
			if ("CREATED".equals(eventId.name())) {
				allowEvents = true;
			}

			if (allowEvents && params[0].equals("com.unity3d.ads.test.legacy.LifecycleListenerTestActivity")) {
				DeviceLog.debug(eventId.name() + " " + params[0]);

				EVENTS.add(eventId);
				EVENT_PARAMS.add(params);
				EVENT_COUNT++;
			}

			return true;
		}
	});

	new Thread(new Runnable() {
		@Override
		public void run() {
			rule.launchActivity(new Intent());
			cv.open();
		}
	}).start();

	boolean success = cv.block(30000);
	return rule.getActivity();
}
 
Example #7
Source File: RepositoryBehaviorTest.java    From DebugRank with Apache License 2.0 5 votes vote down vote up
@Override
protected void init(ActivityTestRule activityTestRule)
{
    super.init(activityTestRule);

    userRepo = (MemoryUserRepository) app.getUserRepository();
    dataRepo = (MemoryDataRepository) app.getDataRepository();
}
 
Example #8
Source File: TestUtils.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
static void waitTillState(final LifecycleOwner owner, ActivityTestRule<?> activityRule,
        Lifecycle.State state)
        throws Throwable {
    final CountDownLatch latch = new CountDownLatch(1);
    activityRule.runOnUiThread(() -> {
        Lifecycle.State currentState = owner.getLifecycle().getCurrentState();
        if (currentState == state) {
            latch.countDown();
        } else {
            owner.getLifecycle().addObserver(new LifecycleObserver() {
                @OnLifecycleEvent(Lifecycle.Event.ON_ANY)
                public void onStateChanged(LifecycleOwner provider) {
                    if (provider.getLifecycle().getCurrentState() == state) {
                        latch.countDown();
                        provider.getLifecycle().removeObserver(this);
                    }
                }
            });
        }
    });
    boolean latchResult = latch.await(1, TimeUnit.MINUTES);
    assertThat("expected " + state + " never happened. Current state:"
                    + owner.getLifecycle().getCurrentState(), latchResult, is(true));

    // wait for another loop to ensure all observers are called
    activityRule.runOnUiThread(() -> {
        // do nothing
    });
}
 
Example #9
Source File: CameraViewTest.java    From cameraview with Apache License 2.0 4 votes vote down vote up
public CameraViewTest() {
    rule = new ActivityTestRule<>(CameraViewActivity.class);
}
 
Example #10
Source File: PageObject.java    From photoviewer with Apache License 2.0 4 votes vote down vote up
public PageObject(Class<T> activityClass) {
  mActivityRule = new ActivityTestRule<>(activityClass, false, false);

  injectFieldsFromApp();
  setUpIdlingResources();
}
 
Example #11
Source File: SuntimesActivityTestBase.java    From SuntimesWidget with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Rotate to given orientation.
 * @param orientation ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE | ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
 */
public void rotateDevice(ActivityTestRule activityRule, int orientation )
{
    activityRule.getActivity().setRequestedOrientation(orientation);
}
 
Example #12
Source File: SuntimesActivityTestBase.java    From SuntimesWidget with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Rotate the device to landscape and back.
 */
public void rotateDevice(ActivityTestRule activityRule)
{
    rotateDevice(activityRule, ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    rotateDevice(activityRule, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
 
Example #13
Source File: PostMessageTest.java    From custom-tabs-client with Apache License 2.0 4 votes vote down vote up
public PostMessageTest() {
    mActivityTestRule = new ActivityTestRule<TestActivity>(TestActivity.class);
    mServiceRule = new ServiceTestRule();
}
 
Example #14
Source File: ActivityFullLifecycleTest.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
public ActivityFullLifecycleTest(Class<? extends Activity> activityClass) {
    //noinspection unchecked
    activityTestRule = new ActivityTestRule(activityClass);
}
 
Example #15
Source File: SnippetListActivityTests.java    From android-java-snippets-sample with MIT License 4 votes vote down vote up
private int getActualSnippetsCount(ActivityTestRule<SnippetListActivity> snippetListActivityRule) {
    List<Integer> actualSnippetsIndexes = getSnippetsIndexes(snippetListActivityRule);
    return actualSnippetsIndexes.size();
}
 
Example #16
Source File: TestUtils.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
static void waitTillCreated(final LifecycleOwner owner, ActivityTestRule<?> activityRule)
        throws Throwable {
    waitTillState(owner, activityRule, CREATED);
}
 
Example #17
Source File: SignInActivityTests.java    From android-java-snippets-sample with MIT License 4 votes vote down vote up
public static void AzureADSignIn(String username, String password, ActivityTestRule<SignInActivity> signInActivityTestRule) throws InterruptedException {
    SignInActivity signInActivity = signInActivityTestRule.launchActivity(null);

    onView(withId(R.id.o365_signin)).perform(click());

    try {
        onWebView()
                .withElement(findElement(Locator.ID, USER_ID_TEXT_ELEMENT))
                .perform(clearElement())
                // Enter text into the input element
                .perform(DriverAtoms.webKeys(username))
                // Set focus on the username input text
                // The form validates the username when this field loses focus
                .perform(webClick())
                .withElement(findElement(Locator.ID, PASSWORD_TEXT_ELEMENT))
                // Now we force focus on this element to make
                // the username element to lose focus and validate
                .perform(webClick())
                .perform(clearElement())
                // Enter text into the input element
                .perform(DriverAtoms.webKeys(password));

        Thread.sleep(2000, 0);

        onWebView()
                .withElement(findElement(Locator.ID, SIGN_IN_BUTTON_ELEMENT))
                .perform(webClick());
    } catch (NoMatchingViewException ex) {
        // If user is already logged in, the flow will go directly to SnippetListActivity
    } finally {
        Thread.sleep(2000, 0);
    }

    // Finally, verify that SnippetListActivity is on top
    intended(allOf(
            hasComponent(hasShortClassName(".SnippetListActivity")),
            toPackage("com.microsoft.graph.snippets")
    ));

    signInActivity.finish();
}
 
Example #18
Source File: PlanTemplateRule.java    From friendly-plans with GNU General Public License v3.0 4 votes vote down vote up
public PlanTemplateRule(DaoSessionResource daoSessionResource, ActivityTestRule activityRule) {
    this.daoSessionResource = daoSessionResource;
    this.activityRule = activityRule;
}
 
Example #19
Source File: TaskTemplateRule.java    From friendly-plans with GNU General Public License v3.0 4 votes vote down vote up
public TaskTemplateRule(DaoSessionResource daoSessionResource, ActivityTestRule activityRule) {
    this.daoSessionResource = daoSessionResource;
    this.activityRule = activityRule;
}
 
Example #20
Source File: AssetTestRule.java    From friendly-plans with GNU General Public License v3.0 4 votes vote down vote up
public AssetTestRule(DaoSessionResource daoSessionResource, ActivityTestRule activityRule) {
    this.daoSessionResource = daoSessionResource;
    this.activityRule = activityRule;
    testFiles = new ArrayList<>();
    isTestAssetSet = false;
}
 
Example #21
Source File: TestActivityTest.java    From Awesome-WanAndroid with Apache License 2.0 4 votes vote down vote up
@Test
public void ActivityTestRule() {
    TestActivity mTestActivity = mActivityTestRule.getActivity();
}
 
Example #22
Source File: AbstractRobotiumTestCase.java    From AndroidStarterAlt with Apache License 2.0 4 votes vote down vote up
protected AbstractRobotiumTestCase(final ActivityTestRule<TypeActivity> poActivityTestRule) {
    mActivityTestRule = poActivityTestRule;
}
 
Example #23
Source File: TestActivityRepoList.java    From AndroidStarterAlt with Apache License 2.0 4 votes vote down vote up
public TestActivityRepoList() {
    super(new ActivityTestRule<>(ActivityMain.class, true, false));
}
 
Example #24
Source File: TestUtils.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
static void waitTillStarted(final LifecycleOwner owner, ActivityTestRule<?> activityRule)
        throws Throwable {
    waitTillState(owner, activityRule, STARTED);
}
 
Example #25
Source File: TestUtils.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
static void waitTillResumed(final LifecycleOwner owner, ActivityTestRule<?> activityRule)
        throws Throwable {
    waitTillState(owner, activityRule, RESUMED);
}
 
Example #26
Source File: TestUtils.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
static void waitTillDestroyed(final LifecycleOwner owner, ActivityTestRule<?> activityRule)
        throws Throwable {
    waitTillState(owner, activityRule, DESTROYED);
}
 
Example #27
Source File: AbstractRobotiumTestCase.java    From AndroidStarter with Apache License 2.0 4 votes vote down vote up
protected AbstractRobotiumTestCase(final ActivityTestRule<TypeActivity> poActivityTestRule) {
    mActivityTestRule = poActivityTestRule;
}
 
Example #28
Source File: TestActivityRepoList.java    From AndroidStarter with Apache License 2.0 4 votes vote down vote up
public TestActivityRepoList() {
    super(new ActivityTestRule<>(ActivityRepoList.class, true, false));
}
 
Example #29
Source File: BaseBehaviorTest.java    From DebugRank with Apache License 2.0 4 votes vote down vote up
protected void init(ActivityTestRule activityTestRule)
{
    app = (TestMyApp) activityTestRule.getActivity().getApplication();
}
 
Example #30
Source File: AppLayerInstrumentedTest.java    From DebugOverlay-Android with Apache License 2.0 4 votes vote down vote up
@Override
ActivityTestRule getActivityRule() {
    return activityRule;
}