@Test public void getLatestDialog_shouldReturnARealDialog() throws Exception { assertThat(ShadowDialog.getLatestDialog()).isNull(); Dialog dialog = new Dialog(context); dialog.show(); assertThat(ShadowDialog.getLatestDialog()).isSameAs(dialog); }
@Test public void show_setsLatestDialog() { Dialog dialog = new Dialog(context); assertNull(ShadowDialog.getLatestDialog()); dialog.show(); assertSame(dialog, ShadowDialog.getLatestDialog()); assertNull(ShadowAlertDialog.getLatestAlertDialog()); }
protected String getDialogText() { MaterialDialog dialog = (MaterialDialog)ShadowDialog.getLatestDialog(); return dialog == null || dialog.getContentView() == null ? null : dialog.getContentView().getText().toString(); }
@Test public void show_setsLatestAlertDialogAndLatestDialog() { AlertDialog alertDialog = new AlertDialog.Builder(context).create(); assertNull(ShadowDialog.getLatestDialog()); assertNull(ShadowAlertDialog.getLatestAlertDialog()); alertDialog.show(); assertEquals(alertDialog, ShadowDialog.getLatestDialog()); assertEquals(alertDialog, ShadowAlertDialog.getLatestAlertDialog()); }
@Test public void showDialog_shouldReuseDialogs() { final DialogCreatingActivity activity = Robolectric.setupActivity(DialogCreatingActivity.class); activity.showDialog(1); Dialog firstDialog = ShadowDialog.getLatestDialog(); activity.showDialog(1); Dialog secondDialog = ShadowDialog.getLatestDialog(); assertSame("dialogs should be the same instance", firstDialog, secondDialog); }
@Test public void shouldNotExplodeWhenNestingAlerts() throws Exception { final Activity activity = Robolectric.buildActivity(Activity.class).create().get(); final AlertDialog nestedDialog = new AlertDialog.Builder(activity) .setTitle("Dialog 2") .setMessage("Another dialog") .setPositiveButton("OK", null) .create(); final AlertDialog dialog = new AlertDialog.Builder(activity) .setTitle("Dialog 1") .setMessage("A dialog") .setPositiveButton("Button 1", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { nestedDialog.show(); } }).create(); dialog.show(); assertThat(ShadowDialog.getLatestDialog()).isEqualTo(dialog); dialog.getButton(Dialog.BUTTON_POSITIVE).performClick(); assertThat(ShadowDialog.getLatestDialog()).isEqualTo(nestedDialog); }
protected void clickDialogButton(DialogAction button) { MaterialDialog dialog = (MaterialDialog)ShadowDialog.getLatestDialog(); dialog.getActionButton(button).performClick(); }
@Test public void shouldPopulateListOfRecentDialogsInCorrectOrder() throws Exception { new NestingTestDialog().show(); assertEquals(TestDialog.class, ShadowDialog.getLatestDialog().getClass()); }
@Test public void shouldCallOnCreateDialogFromShowDialog() { ActivityWithOnCreateDialog activity = Robolectric.setupActivity(ActivityWithOnCreateDialog.class); activity.showDialog(123); assertTrue(activity.onCreateDialogWasCalled); assertThat(ShadowDialog.getLatestDialog()).isNotNull(); }
@Test public void show_shouldShowDialogThatWasReturnedFromOnCreateDialog_whenOnCreateDialogReturnsADialog() throws Exception { Dialog dialogFromOnCreateDialog = new Dialog(activity); dialogFragment.returnThisDialogFromOnCreateDialog(dialogFromOnCreateDialog); dialogFragment.show(fragmentManager, "this is a tag"); Dialog dialog = ShadowDialog.getLatestDialog(); assertSame(dialogFromOnCreateDialog, dialog); assertSame(dialogFromOnCreateDialog, dialogFragment.getDialog()); assertSame(dialogFragment, fragmentManager.findFragmentByTag("this is a tag")); }
@Test public void removeDialog_shouldCreateDialogAgain() { final DialogCreatingActivity activity = Robolectric.setupActivity(DialogCreatingActivity.class); activity.showDialog(1); Dialog firstDialog = ShadowDialog.getLatestDialog(); activity.removeDialog(1); assertNull(shadowOf(activity).getDialogById(1)); activity.showDialog(1); Dialog secondDialog = ShadowDialog.getLatestDialog(); assertNotSame("dialogs should not be the same instance", firstDialog, secondDialog); }
@Test public void removeUsingTransaction_shouldDismissTheDialog() throws Exception { dialogFragment.show(fragmentManager, null); FragmentTransaction t = fragmentManager.beginTransaction(); t.remove(dialogFragment); t.commit(); Dialog dialog = ShadowDialog.getLatestDialog(); assertFalse(dialog.isShowing()); assertTrue(shadowOf(dialog).hasBeenDismissed()); }
@Test public void showDialog_shouldShowDialog() throws Exception { final DialogCreatingActivity activity = Robolectric.setupActivity(DialogCreatingActivity.class); activity.showDialog(1); Dialog dialog = ShadowDialog.getLatestDialog(); assertTrue(dialog.isShowing()); }
@Test public void testHelp() { ((PreferencesActivity.SettingsFragment) activity.getSupportFragmentManager() .findFragmentByTag(PreferencesActivity.SettingsFragment.class.getName())) .getPreferenceScreen() .findPreference(activity.getString(R.string.pref_volume_help)) .performClick(); Dialog dialog = ShadowDialog.getLatestDialog(); assertNotNull(dialog); assertThat((TextView) dialog.findViewById(R.id.alertTitle)) .hasText(R.string.pref_volume_title); }
@Test public void testLazyLoadHelp() { ((PreferencesActivity.SettingsFragment) activity.getSupportFragmentManager() .findFragmentByTag(PreferencesActivity.SettingsFragment.class.getName())) .getPreferenceScreen() .findPreference(activity.getString(R.string.pref_lazy_load_help)) .performClick(); Dialog dialog = ShadowDialog.getLatestDialog(); assertNotNull(dialog); assertThat((TextView) dialog.findViewById(R.id.alertTitle)) .hasText(R.string.pref_lazy_load_title); }
@Test public void show_shouldShowDialogThatWasAutomaticallyCreated_whenOnCreateDialogReturnsNull() throws Exception { dialogFragment.show(fragmentManager, "this is a tag"); Dialog dialog = ShadowDialog.getLatestDialog(); assertNotNull(dialog); assertSame(dialog, dialogFragment.getDialog()); assertNotNull(dialog.findViewById(R.id.title)); assertSame(dialogFragment, fragmentManager.findFragmentByTag("this is a tag")); }
@Test public void dismissDialog_shouldDismissPreviouslyShownDialog() throws Exception { final DialogCreatingActivity activity = Robolectric.setupActivity(DialogCreatingActivity.class); activity.showDialog(1); activity.dismissDialog(1); Dialog dialog = ShadowDialog.getLatestDialog(); assertFalse(dialog.isShowing()); }
assertTrue(fragment.hasOptionsMenu()); fragment.onOptionsItemSelected(new RoboMenuItem(R.id.menu_font_options)); assertNotNull(ShadowDialog.getLatestDialog()); PreferenceManager.getDefaultSharedPreferences(activity) .edit()
@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 testCreateDirectoryDialogAllowFolderNameModification() { final String directoryName = "mydir"; final DirectoryChooserFragment fragment = DirectoryChooserFragment.newInstance( DirectoryChooserConfig.builder() .newDirectoryName(directoryName) .initialDirectory("") .allowReadOnlyDirectory(false) .allowNewDirectoryNameModification(true) .build()); startFragment(fragment, DirectoryChooserActivityMock.class); fragment.onOptionsItemSelected(new TestMenuItem() { @Override public int getItemId() { return R.id.new_folder_item; } }); final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog(); final ShadowAlertDialog shadowAlertDialog = Shadows.shadowOf(dialog); assertThat(shadowAlertDialog.getTitle()).isEqualTo("Create folder"); assertThat(ShadowDialog.getShownDialogs()).contains(dialog); final TextView msgView = (TextView) dialog.findViewById(R.id.msgText); assertThat(msgView).hasText("Create new folder with name \"mydir\"?"); final EditText editText = (EditText) dialog.findViewById(R.id.editText); assertThat(editText).isVisible(); assertThat(editText).hasTextString(directoryName); }