Java Code Examples for androidx.test.core.app.ActivityScenario#launch()

The following examples show how to use androidx.test.core.app.ActivityScenario#launch() . 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: WayTodayStartStopTest.java    From itag with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void activity_shouldStartWayToday5min() {
    try (ActivityScenario<MainActivity> ignored = ActivityScenario.launch(MainActivity.class)) {
        Application application = ApplicationProvider.getApplicationContext();
        SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(application);

        assertThat(LocationsTracker.isUpdating, is(false));

        ViewInteraction vi = onView(withId(R.id.btn_waytoday));
        vi.perform(click());
        onView(withText(R.string.freq_min_5))
                .perform(click());
        ArgumentCaptor<LocationsTracker.TrackingState> state =
                ArgumentCaptor.forClass(LocationsTracker.TrackingState.class);

        verify(mockTrackingStateListener, timeout(5000).atLeast(1)).onStateChange(state.capture());
        List<LocationsTracker.TrackingState> states =
                state.getAllValues();
        assertThat(states.size(), greaterThan(0));
        assertThat(states.get(0).isUpdating, is(true));
        assertThat(LocationsTracker.isUpdating, is(true));
        assertThat(p.getInt("freq", -1), equalTo(5 * 60 * 1000));
        assertThat(p.getBoolean("wt", false), is(true));
    }
}
 
Example 2
Source File: WayTodayStartStopTest.java    From itag with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void activity_shouldStartWayToday1h() {
    try (ActivityScenario<MainActivity> ignored = ActivityScenario.launch(MainActivity.class)) {
        Application application = ApplicationProvider.getApplicationContext();
        SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(application);

        assertThat(LocationsTracker.isUpdating, is(false));

        ViewInteraction vi = onView(withId(R.id.btn_waytoday));
        vi.perform(click());
        onView(withText(R.string.freq_hour_1))
                .perform(click());
        ArgumentCaptor<LocationsTracker.TrackingState> state =
                ArgumentCaptor.forClass(LocationsTracker.TrackingState.class);

        verify(mockTrackingStateListener, timeout(5000).atLeast(1)).onStateChange(state.capture());
        List<LocationsTracker.TrackingState> states =
                state.getAllValues();
        assertThat(states.size(), greaterThan(0));
        assertThat(states.get(0).isUpdating, is(true));
        assertThat(LocationsTracker.isUpdating, is(true));
        assertThat(p.getInt("freq", -1), equalTo(60 * 60 * 1000));
        assertThat(p.getBoolean("wt", false), is(true));

    }
}
 
Example 3
Source File: UiControllerImplIntegrationTest.java    From android-test with Apache License 2.0 6 votes vote down vote up
@Test
public void injectKeyEvent() throws InterruptedException {
  try (ActivityScenario<SendActivity> activityScenario =
      ActivityScenario.launch(SendActivity.class)) {
    activityScenario.onActivity(
        activity -> {
          try {
            KeyCharacterMap keyCharacterMap = UiControllerImpl.getKeyCharacterMap();
            KeyEvent[] events = keyCharacterMap.getEvents("a".toCharArray());
            injectEventWorked.set(uiController.injectKeyEvent(events[0]));
            latch.countDown();
          } catch (InjectEventSecurityException e) {
            injectEventThrewSecurityException.set(true);
          }
        });

    assertFalse("injectEvent threw a SecurityException", injectEventThrewSecurityException.get());
    assertTrue("Timed out!", latch.await(10, TimeUnit.SECONDS));
    assertTrue(injectEventWorked.get());
  }
}
 
Example 4
Source File: SwipeOpenItemTouchHelperTest.java    From SwipeOpenItemTouchHelper with Apache License 2.0 6 votes vote down vote up
@Test
public void stateSavingTest() {
  try (ActivityScenario<SwipeOpenItemTouchHelperTestActivity> scenario = ActivityScenario.launch(
      SwipeOpenItemTouchHelperTestActivity.class)) {

    scenario.onActivity(activity -> activity.helper.setCloseOnAction(false));

    // open positions 1 and 2
    onView(withId(R.id.test_recycler)).perform(actionOnItemAtPosition(1, swipeRight()));
    onView(withId(R.id.test_recycler)).perform(actionOnItemAtPosition(2, swipeLeft()));

    // recreate the activity
    scenario.recreate();

    // both positions should still be open
    onView(withText("Test 1")).check(matches(checkTranslationX(true)));
    onView(withText("Test 2")).check(matches(checkTranslationX(false)));
  }
}
 
Example 5
Source File: ChannelDetailActivityTest.java    From zapp with MIT License 5 votes vote down vote up
@Test
public void testRecreation() {
	Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
	Intent intent = ChannelDetailActivity.getStartIntent(context, "das_erste");

	ActivityScenario scenario = ActivityScenario.launch(intent);
	scenario.recreate();

	scenario.onActivity(activity -> activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE));
	scenario.recreate();
}
 
Example 6
Source File: UiControllerImplIntegrationTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Test
public void injectMotionEvent() throws InterruptedException {
  try (ActivityScenario<SendActivity> activityScenario =
      ActivityScenario.launch(SendActivity.class)) {
    final int[] coords = CoordinatesUtil.getCoordinatesInMiddleOfSendButton(activityScenario);

    getInstrumentation()
        .runOnMainSync(
            new Runnable() {
              @Override
              public void run() {
                long downTime = SystemClock.uptimeMillis();
                try {
                  MotionEvent event =
                      MotionEvent.obtain(
                          downTime,
                          SystemClock.uptimeMillis(),
                          MotionEvent.ACTION_DOWN,
                          coords[0],
                          coords[1],
                          0);

                  injectEventWorked.set(uiController.injectMotionEvent(event));
                  event.recycle();
                  latch.countDown();
                } catch (InjectEventSecurityException e) {
                  injectEventThrewSecurityException.set(true);
                }
              }
            });

    assertFalse(
        "SecurityException exception was thrown.", injectEventThrewSecurityException.get());
    assertTrue("Timed out!", latch.await(10, TimeUnit.SECONDS));
    assertTrue(injectEventWorked.get());
  }
}
 
Example 7
Source File: KeyEventActionIntegrationTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/**
 * Test only passes if run in isolation. Unless Gradle supports a single instrumentation per test
 * this test is ignored"
 */
@Suppress
@Test
public void clickBackOnRootAction() {
  ActivityScenario.launch(MainActivity.class);
  expectedException.expect(NoActivityResumedException.class);
  pressBack();
}
 
Example 8
Source File: WayTodayChangeTrackIDTest.java    From itag with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void activity_shouldHaveWayTodayMenu() {

    try (ActivityScenario<MainActivity> ignored = ActivityScenario.launch(MainActivity.class)) {

        ViewInteraction vi = onView(withId(R.id.btn_waytoday));
        vi.perform(click());
        onView(withText(R.string.off))
                .perform(click());
    }
}
 
Example 9
Source File: AccessibilityChecksTest.java    From testing-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void accessibilityChecks() {
    AccessibilityChecks.enable();
    try (ActivityScenario scenario = ActivityScenario.launch(MainActivity.class)) {
        onView(withId(R.id.openBrowserButton)).perform(click());
    }
}
 
Example 10
Source File: KeyEventActionIntegrationTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Test
public void clickBackOnNonRootActionNoLatte() {
  ActivityScenario.launch(MainActivity.class);
  onData(allOf(instanceOf(Map.class), hasValue("LargeViewActivity"))).perform(click());
  onView(isRoot()).perform(ViewActions.pressBack());

  // Make sure we are back.
  onData(allOf(instanceOf(Map.class), hasValue("LargeViewActivity")))
      .check(matches(isDisplayed()));
}
 
Example 11
Source File: TestStorageTest.java    From android-test with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
  ActivityScenario.launch(DummyActivity.class);
}
 
Example 12
Source File: ToolbarTest.java    From android-test with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
  // Espresso will not launch our activity for us, we must launch it via ActivityScenario.launch.
  ActivityScenario.launch(ToolbarActivity.class);
}
 
Example 13
Source File: LayoutTest.java    From android-test with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
  ActivityScenario.launch(LayoutIssuesActivity.class);
}
 
Example 14
Source File: ActionBarTest.java    From android-test with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
  // Espresso will not launch our activity for us, we must launch it via ActivityScenario.launch.
  ActivityScenario.launch(ActionBarTestActivity.class);
}
 
Example 15
Source File: PickerActionsTest.java    From android-test with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
  ActivityScenario.launch(PickersActivity.class);
}
 
Example 16
Source File: ActivityController.java    From easypermissions with Apache License 2.0 4 votes vote down vote up
public ActivityController(Class<T> clazz) {
    scenario = ActivityScenario.launch(clazz);
}
 
Example 17
Source File: DrawerActionsTest.java    From android-test with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
  ActivityScenario.launch(DrawerActivity.class);
}
 
Example 18
Source File: ScrollToTest.java    From android-test with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
  // Espresso will not launch our activity for us, we must launch it via ActivityScenario.launch.
  ActivityScenario.launch(ScrollActivity.class);
}
 
Example 19
Source File: ActivityScenarioRule.java    From android-test with Apache License 2.0 2 votes vote down vote up
/**
 * @see #ActivityScenarioRule(Class)
 * @param activityOptions an activity options bundle to be passed along with the intent to start
 *     activity.
 */
public ActivityScenarioRule(Class<A> activityClass, @Nullable Bundle activityOptions) {
  scenarioSupplier = () -> ActivityScenario.launch(checkNotNull(activityClass), activityOptions);
}
 
Example 20
Source File: ActivityScenarioRule.java    From android-test with Apache License 2.0 2 votes vote down vote up
/**
 * Constructs ActivityScenarioRule with a given intent.
 *
 * @param startActivityIntent an intent to start an activity
 */
public ActivityScenarioRule(Intent startActivityIntent) {
  scenarioSupplier = () -> ActivityScenario.launch(checkNotNull(startActivityIntent));
}