@Test public void startActivities_withBundle_shouldStartAllActivities() { final Intent view = new Intent(Intent.ACTION_VIEW).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); final Intent pick = new Intent(Intent.ACTION_PICK).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); contextWrapper.startActivities(new Intent[] {view, pick}, new Bundle()); assertThat(ShadowApplication.getInstance().getNextStartedActivity()).isEqualTo(pick); assertThat(ShadowApplication.getInstance().getNextStartedActivity()).isEqualTo(view); }
@Test public void startActivities_shouldStartAllActivities() { final Intent view = new Intent(Intent.ACTION_VIEW).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); final Intent pick = new Intent(Intent.ACTION_PICK).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); contextWrapper.startActivities(new Intent[] {view, pick}); assertThat(ShadowApplication.getInstance().getNextStartedActivity()).isEqualTo(pick); assertThat(ShadowApplication.getInstance().getNextStartedActivity()).isEqualTo(view); }
@Test public void startIntentSender_activityIntent() throws IntentSender.SendIntentException { PendingIntent intent = PendingIntent.getActivity( context, 0, new Intent() .setClassName(context, "ActivityIntent") .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), PendingIntent.FLAG_UPDATE_CURRENT); context.startIntentSender(intent.getIntentSender(), null, 0, 0, 0); assertThat(shadowOf(context).getNextStartedActivity().getComponent().getClassName()) .isEqualTo("ActivityIntent"); }
@Test public void startActivity_whenActivityCheckingEnabled_findsResolveInfo() throws Exception { shadowOf(context).checkActivities(true); context.startActivity( new Intent() .setClassName(context, TestActivity.class.getName()) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); assertThat(shadowOf(context).getNextStartedActivity()).isNotNull(); }
@Test public void getActivities_withBundle_shouldCreateIntentForBroadcast() throws Exception { Intent[] intents = {new Intent(Intent.ACTION_VIEW), new Intent(Intent.ACTION_PICK)}; PendingIntent pendingIntent = PendingIntent.getActivities(context, 99, intents, 100, Bundle.EMPTY); ShadowPendingIntent shadow = shadowOf(pendingIntent); assertThat(shadow.getSavedIntents()).isEqualTo(intents); pendingIntent.send(); ShadowApplication application = shadowOf((Application) ApplicationProvider.getApplicationContext()); assertThat(application.getNextStartedActivity()).isEqualTo(intents[1]); assertThat(application.getNextStartedActivity()).isEqualTo(intents[0]); }
@Test public void getActivities_shouldCreateIntentForBroadcast() throws Exception { Intent[] intents = {new Intent(Intent.ACTION_VIEW), new Intent(Intent.ACTION_PICK)}; PendingIntent pendingIntent = PendingIntent.getActivities(context, 99, intents, 100); ShadowPendingIntent shadow = shadowOf(pendingIntent); assertThat(shadow.getSavedIntents()).isEqualTo(intents); pendingIntent.send(); ShadowApplication application = shadowOf((Application) ApplicationProvider.getApplicationContext()); assertThat(application.getNextStartedActivity()).isEqualTo(intents[1]); assertThat(application.getNextStartedActivity()).isEqualTo(intents[0]); }
@Test public void testSetTextWithLinksOpenChromeCustomTabs() { TestApplication.addResolver(new Intent(Intent.ACTION_VIEW, Uri.parse("http://example.com"))); TextView textView = new TextView(new ContextThemeWrapper(context, R.style.AppTheme)); AppUtils.setTextWithLinks(textView, AppUtils.fromHtml("<a href=\"http://example.com\">http://example.com</a>")); MotionEvent event = mock(MotionEvent.class); when(event.getAction()).thenReturn(MotionEvent.ACTION_DOWN); when(event.getX()).thenReturn(0f); when(event.getY()).thenReturn(0f); assertTrue(shadowOf(textView).getOnTouchListener().onTouch(textView, event)); when(event.getAction()).thenReturn(MotionEvent.ACTION_UP); when(event.getX()).thenReturn(0f); when(event.getY()).thenReturn(0f); assertTrue(shadowOf(textView).getOnTouchListener().onTouch(textView, event)); assertNotNull(ShadowApplication.getInstance().getNextStartedActivity()); }
@Test public void testSetTextWithLinks() { TestApplication.addResolver(new Intent(Intent.ACTION_VIEW, Uri.parse("http://example.com"))); Preferences.set(context, R.string.pref_custom_tab, false); TextView textView = new TextView(context); AppUtils.setTextWithLinks(textView, AppUtils.fromHtml("<a href=\"http://example.com\">http://example.com</a>")); MotionEvent event = mock(MotionEvent.class); when(event.getAction()).thenReturn(MotionEvent.ACTION_DOWN); when(event.getX()).thenReturn(0f); when(event.getY()).thenReturn(0f); assertTrue(shadowOf(textView).getOnTouchListener().onTouch(textView, event)); when(event.getAction()).thenReturn(MotionEvent.ACTION_UP); when(event.getX()).thenReturn(0f); when(event.getY()).thenReturn(0f); assertTrue(shadowOf(textView).getOnTouchListener().onTouch(textView, event)); assertNotNull(ShadowApplication.getInstance().getNextStartedActivity()); }
@Test public void startActivity_whenActivityCheckingEnabled_doesntFindResolveInfo() throws Exception { shadowOf(context).checkActivities(true); String action = "com.does.not.exist.android.app.v2.mobile"; try { context.startActivity(new Intent(action).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); fail("Expected startActivity to throw ActivityNotFoundException!"); } catch (ActivityNotFoundException e) { assertThat(e.getMessage()).contains(action); assertThat(shadowOf(context).getNextStartedActivity()).isNull(); } }
@Test public void shouldShowExpectedSettingsDialog_whenBuildingFromActivity() { new AppSettingsDialog.Builder(spyActivity) .setTitle(android.R.string.dialog_alert_title) .setRationale(android.R.string.unknownName) .setPositiveButton(android.R.string.ok) .setNegativeButton(android.R.string.cancel) .setThemeResId(R.style.Theme_AppCompat) .build() .show(); verify(spyActivity, times(1)) .startActivityForResult(intentCaptor.capture(), integerCaptor.capture()); assertThat(integerCaptor.getValue()).isEqualTo(DEFAULT_SETTINGS_REQ_CODE); assertThat(Objects.requireNonNull(intentCaptor.getValue().getComponent()).getClassName()) .isEqualTo(AppSettingsDialogHolderActivity.class.getName()); Intent startedIntent = shadowApp.getNextStartedActivity(); ShadowIntent shadowIntent = shadowOf(startedIntent); assertThat(shadowIntent.getIntentClass()).isEqualTo(AppSettingsDialogHolderActivity.class); }
@Test public void shouldShowExpectedSettingsDialog_whenBuildingFromSupportFragment() { new AppSettingsDialog.Builder(spyFragment) .setTitle(android.R.string.dialog_alert_title) .setRationale(android.R.string.unknownName) .setPositiveButton(android.R.string.ok) .setNegativeButton(android.R.string.cancel) .setThemeResId(R.style.Theme_AppCompat) .build() .show(); verify(spyFragment, times(1)) .startActivityForResult(intentCaptor.capture(), integerCaptor.capture()); assertThat(integerCaptor.getValue()).isEqualTo(DEFAULT_SETTINGS_REQ_CODE); assertThat(Objects.requireNonNull(intentCaptor.getValue().getComponent()).getClassName()) .isEqualTo(AppSettingsDialogHolderActivity.class.getName()); Intent startedIntent = shadowApp.getNextStartedActivity(); ShadowIntent shadowIntent = shadowOf(startedIntent); assertThat(shadowIntent.getIntentClass()).isEqualTo(AppSettingsDialogHolderActivity.class); }
@Test public void clickWithIntent() { Activity a = Robolectric.buildActivity(Activity.class).get(); RoboMenu menu = new RoboMenu(a); menu.add(0, 10, 0, org.robolectric.R.string.ok); RoboMenuItem item = (RoboMenuItem) menu.findItem(10); Assert.assertNull(item.getIntent()); Intent intent = new Intent(a, Activity.class); item.setIntent(intent); item.click(); Assert.assertNotNull(item); Intent startedIntent = ShadowApplication.getInstance().getNextStartedActivity(); assertNotNull(startedIntent); }
@Test public void testComponentOnClick() { final Class activityClassToLaunch = PlaygroundActivity.class; final DemoListItemComponent.Builder builder = DemoListItemComponent.create(mComponentsRule.getContext()) .model(new DemoListActivity.DemoListDataModel("My Component", activityClassToLaunch)) .currentIndices(null); // For this test, we mount the view and dispatch the event through the regular // Android event mechanism. final LithoView lithoView = ComponentTestHelper.mountComponent(builder); lithoView.performClick(); final Intent nextIntent = shadowOf(mComponentsRule.getContext().getAndroidContext()) .getShadowApplication() .getNextStartedActivity(); assertThat(nextIntent.getComponent().getClassName()).isSameAs(activityClassToLaunch.getName()); }
@Test public void testComponentOnSyntheticEventClick() { final Class activityClassToLaunch = PlaygroundActivity.class; final Component component = DemoListItemComponent.create(mComponentsRule.getContext()) .model(new DemoListActivity.DemoListDataModel("My Component", activityClassToLaunch)) .currentIndices(null) .build(); // Here, we make use of Litho's internal event infrastructure and manually dispatch the event. final ComponentContext componentContext = withComponentScope(mComponentsRule.getContext(), component); component .getEventDispatcher() .dispatchOnEvent(DemoListItemComponent.onClick(componentContext), new ClickEvent()); final Intent nextIntent = shadowOf(mComponentsRule.getContext().getAndroidContext()) .getShadowApplication() .getNextStartedActivity(); assertThat(nextIntent.getComponent().getClassName()).isSameAs(activityClassToLaunch.getName()); } }
@Test public void testKonami() { gestureListener.onFling(null, null, 0f, 1f); // down, invalid, should ignore gestureListener.onFling(null, null, 0f, -1f); // up gestureListener.onFling(null, null, 0f, 1f); // down, invalid, should reset gestureListener.onFling(null, null, 0f, -1f); // up gestureListener.onFling(null, null, 0f, -1f); // up gestureListener.onFling(null, null, 0f, 1f); // down gestureListener.onFling(null, null, 0f, 1f); // down gestureListener.onFling(null, null, -1f, 0f); // left gestureListener.onFling(null, null, 1f, 0f); // right gestureListener.onFling(null, null, -1f, 0f); // left gestureListener.onFling(null, null, 1f, 0f); // right gestureListener.onDoubleTap(null); Dialog dialog = ShadowDialog.getLatestDialog(); assertNotNull(dialog); shadowOf(dialog).clickOn(android.R.id.button1); // BUTTON_POSITIVE assertThat(shadowOf(RuntimeEnvironment.application).getNextStartedActivity()) .hasAction(Intent.ACTION_VIEW); } }
@Test public void should_navigate_to_main_activity() throws Exception { Robolectric.setupActivity(WelcomeActivity.class); ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); ShadowApplication instance = ShadowApplication.getInstance(); Intent nextStartedActivity = instance.getNextStartedActivity(); assertNotNull(nextStartedActivity); String className = nextStartedActivity.getComponent().getClassName(); assertThat(className, is(LoginActivity.class.getName())); }
/** * Non-Android accessor that delegates to the application to consume and return the next {@code Intent} on the * started activities stack. * * @return the next started {@code Intent} for an activity */ public Intent getNextStartedActivity() { return ShadowApplication.getInstance().getNextStartedActivity(); }
/** * Non-Android accessor that delegates to the application to consume and return the next {@code Intent} on the * started activities stack. * * @return the next started {@code Intent} for an activity */ public Intent getNextStartedActivity() { return ShadowApplication.getInstance().getNextStartedActivity(); }
/** * Delegates to the application to consume and return the next {@code Intent} on the * started activities stack. * * @return the next started {@code Intent} for an activity */ public Intent getNextStartedActivity() { return ShadowApplication.getInstance().getNextStartedActivity(); }
@Test @Config(maxSdk = LOLLIPOP_MR1) public void DoneButtonShouldStartMapActivityOnVersionWithoutBackgroundRestriction() { // Shows welcome assertEquals(View.VISIBLE, welcomeActivity.findViewById(R.id.btn_next).getVisibility()); assertEquals(View.GONE, welcomeActivity.findViewById(R.id.done).getVisibility()); assertTrue(welcomeActivity.findViewById(R.id.btn_next).isEnabled()); clickOn(welcomeActivity.findViewById(R.id.btn_next)); // Shows Done assertEquals(View.GONE, welcomeActivity.findViewById(R.id.btn_next).getVisibility()); assertEquals(View.VISIBLE, welcomeActivity.findViewById(R.id.done).getVisibility()); assertFalse(welcomeActivity.findViewById(R.id.btn_next).isEnabled()); assertTrue(welcomeActivity.findViewById(R.id.done).isEnabled()); clickOn(welcomeActivity.findViewById(R.id.done)); Intent expectedIntent = new Intent(welcomeActivity, MapActivity.class); Intent actualIntent = ShadowApplication.getInstance().getNextStartedActivity(); assertEquals(expectedIntent.getComponent(), actualIntent.getComponent()); } }