@Test public void getLatestAlertDialog_shouldReturnARealAlertDialog() throws Exception { assertThat(ShadowAlertDialog.getLatestAlertDialog()).isNull(); AlertDialog dialog = new AlertDialog.Builder(application).show(); assertThat(ShadowAlertDialog.getLatestAlertDialog()).isSameAs(dialog); }
@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 testBuilder() throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(application); builder.setTitle("title").setMessage("message"); builder.setCancelable(true); AlertDialog alert = builder.create(); alert.show(); assertThat(alert.isShowing()).isTrue(); ShadowAlertDialog shadowAlertDialog = shadowOf(alert); assertEquals("title", shadowAlertDialog.getTitle()); assertThat(shadowAlertDialog.getMessage()).isEqualTo("message"); assertThat(shadowAlertDialog.isCancelable()).isTrue(); assertThat(shadowOf(ShadowAlertDialog.getLatestAlertDialog())).isSameAs(shadowAlertDialog); assertThat(ShadowAlertDialog.getLatestAlertDialog()).isSameAs(alert); }
@Test public void testGuidelines() { shadowOf(activity).clickMenuItem(R.id.menu_guidelines); assertNotNull(ShadowAlertDialog.getLatestAlertDialog()); }
@Test public void testShareComment() { AppUtils.share(context, mock(PopupMenu.class), new View(context), new TestHnItem(1)); assertNull(ShadowAlertDialog.getLatestAlertDialog()); AppUtils.share(context, mock(PopupMenu.class), new View(context), new TestHnItem(1) { @Override public String getUrl() { return String.format(HackerNewsClient.WEB_ITEM_PATH, "1"); } }); assertNull(ShadowAlertDialog.getLatestAlertDialog()); }
@Test public void testBuilderWithItemArrayViaResourceId() throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(new ContextWrapper(context)); builder.setTitle("title"); builder.setItems(R.array.alertDialogTestItems, new TestDialogOnClickListener()); AlertDialog alert = builder.create(); alert.show(); assertThat(alert.isShowing()).isTrue(); ShadowAlertDialog shadowAlertDialog = shadowOf(alert); assertThat(shadowAlertDialog.getTitle().toString()).isEqualTo("title"); assertThat(shadowAlertDialog.getItems().length).isEqualTo(2); assertThat(shadowAlertDialog.getItems()[0]).isEqualTo("Aloha"); assertThat(shadowOf(ShadowAlertDialog.getLatestAlertDialog())).isSameAs(shadowAlertDialog); assertThat(ShadowAlertDialog.getLatestAlertDialog()).isSameAs(alert); }
@Test public void show_setsLatestDialog() { Dialog dialog = new Dialog(context); assertNull(ShadowDialog.getLatestDialog()); dialog.show(); assertSame(dialog, ShadowDialog.getLatestDialog()); assertNull(ShadowAlertDialog.getLatestAlertDialog()); }
@Test public void testOptionsMenuClear() { assertTrue(shadowOf(activity).getOptionsMenu().findItem(R.id.menu_clear).isVisible()); shadowOf(activity).clickMenuItem(R.id.menu_clear); AlertDialog dialog = ShadowAlertDialog.getLatestAlertDialog(); dialog.getButton(DialogInterface.BUTTON_NEGATIVE).performClick(); assertEquals(2, adapter.getItemCount()); shadowOf(activity).clickMenuItem(R.id.menu_clear); dialog = ShadowAlertDialog.getLatestAlertDialog(); dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick(); verify(favoriteManager).clear(any(Context.class), any()); when(favoriteManager.getSize()).thenReturn(0); observerCaptor.getValue().onChanged(); assertEquals(0, adapter.getItemCount()); }
@Test public void testReset() { PreferenceManager.getDefaultSharedPreferences(activity) .edit() .putBoolean(activity.getString(R.string.pref_color_code), false) .apply(); assertFalse(Preferences.colorCodeEnabled(activity)); assertNotNull(shadowOf(activity).getOptionsMenu().findItem(R.id.menu_reset)); shadowOf(activity).clickMenuItem(R.id.menu_reset); AlertDialog alertDialog = ShadowAlertDialog.getLatestAlertDialog(); assertNotNull(alertDialog); alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick(); assertTrue(Preferences.colorCodeEnabled(activity)); }
@Test public void testClearRecentSearches() { ShadowSearchRecentSuggestions.historyClearCount = 0; assertNotNull(shadowOf(activity).getOptionsMenu().findItem(R.id.menu_clear_recent)); shadowOf(activity).clickMenuItem(R.id.menu_clear_recent); AlertDialog alertDialog = ShadowAlertDialog.getLatestAlertDialog(); assertNotNull(alertDialog); alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick(); assertEquals(1, ShadowSearchRecentSuggestions.historyClearCount); }
@Test public void testSubmitDelayedFailed() { ((EditText) activity.findViewById(R.id.edittext_title)).setText("title"); ((EditText) activity.findViewById(R.id.edittext_content)).setText("http://example.com"); shadowOf(activity).clickMenuItem(R.id.menu_send); ShadowAlertDialog.getLatestAlertDialog().getButton(DialogInterface.BUTTON_POSITIVE) .performClick(); verify(userServices).submit(any(Context.class), eq("title"), eq("http://example.com"), eq(true), submitCallback.capture()); shadowOf(activity).clickMenuItem(android.R.id.home); ShadowAlertDialog.getLatestAlertDialog().getButton(DialogInterface.BUTTON_POSITIVE) .performClick(); submitCallback.getValue().onDone(false); assertNull(shadowOf(activity).getNextStartedActivity()); }
@Test public void testSubmitFailed() { ((EditText) activity.findViewById(R.id.edittext_title)).setText("title"); ((EditText) activity.findViewById(R.id.edittext_content)).setText("http://example.com"); shadowOf(activity).clickMenuItem(R.id.menu_send); ShadowAlertDialog.getLatestAlertDialog().getButton(DialogInterface.BUTTON_POSITIVE) .performClick(); verify(userServices).submit(any(Context.class), eq("title"), eq("http://example.com"), eq(true), submitCallback.capture()); submitCallback.getValue().onDone(false); assertThat(shadowOf(activity).getNextStartedActivity()) .hasComponent(activity, LoginActivity.class); }
@Test public void testExitSaveDraft() { ((EditText) activity.findViewById(R.id.edittext_body)).setText("Reply"); shadowOf(activity).clickMenuItem(android.R.id.home); AlertDialog alertDialog = ShadowAlertDialog.getLatestAlertDialog(); assertNotNull(alertDialog); alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick(); assertThat(activity).isFinishing(); assertThat(Preferences.getDraft(activity, "1")).contains("Reply"); }
@Test public void testGuidelines() { shadowOf(activity).clickMenuItem(R.id.menu_guidelines); assertNotNull(ShadowAlertDialog.getLatestAlertDialog()); }
@Test public void testExitDiscardDraft() { ((EditText) activity.findViewById(R.id.edittext_body)).setText("Reply"); shadowOf(activity).clickMenuItem(android.R.id.home); AlertDialog alertDialog = ShadowAlertDialog.getLatestAlertDialog(); assertNotNull(alertDialog); alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).performClick(); assertThat(activity).isFinishing(); assertThat(Preferences.getDraft(activity, "1")).isNullOrEmpty(); }
@Test public void testLoginShowChooser() { TestApplication.applicationGraph.inject(this); shadowOf(ShadowAccountManager.get(context)) .addAccount(new Account("username", BuildConfig.APPLICATION_ID)); AppUtils.showLogin(context, alertDialogBuilder); assertNotNull(ShadowAlertDialog.getLatestAlertDialog()); }
@Test public void testSubmitUrl() { ((EditText) activity.findViewById(R.id.edittext_title)).setText("title"); ((EditText) activity.findViewById(R.id.edittext_content)).setText("http://example.com"); shadowOf(activity).clickMenuItem(R.id.menu_send); AlertDialog alertDialog = ShadowAlertDialog.getLatestAlertDialog(); assertNotNull(alertDialog); assertEquals(activity.getString(R.string.confirm_submit_url), shadowOf(alertDialog).getMessage()); }
@Test public void testSubmitText() { ((EditText) activity.findViewById(R.id.edittext_title)).setText("title"); ((EditText) activity.findViewById(R.id.edittext_content)).setText("content"); shadowOf(activity).clickMenuItem(R.id.menu_send); AlertDialog alertDialog = ShadowAlertDialog.getLatestAlertDialog(); assertNotNull(alertDialog); assertEquals(activity.getString(R.string.confirm_submit_question), shadowOf(alertDialog).getMessage()); }
@Test public void testOnBackPressed() { shadowOf(activity).clickMenuItem(android.R.id.home); AlertDialog alertDialog = ShadowAlertDialog.getLatestAlertDialog(); assertNotNull(alertDialog); alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick(); assertThat(activity).isFinishing(); }
@Config(sdk = 21) @Test public void testRemoveAccount() { AccountManager.get(activity).addAccountExplicitly(new Account("existing", BuildConfig.APPLICATION_ID), "password", null); Preferences.setUsername(activity, "existing"); drawerAccount.performClick(); AlertDialog alertDialog = ShadowAlertDialog.getLatestAlertDialog(); assertNotNull(alertDialog); assertThat(alertDialog.getListView().getAdapter()).hasCount(1); alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL).performClick(); assertThat(alertDialog).isNotShowing(); assertThat(AccountManager.get(activity).getAccounts()).isEmpty(); }