@Test public void whenShown_thenButtonsTextsCorrect() { String positive = "126kas"; String neutral = "12asi"; String negative = "98io8"; DialogFields fields = DialogFields.builder(activity) .positiveText(positive) .neutralText(neutral) .negativeText(negative) .build(); LambdaDialogs.delegate(activity) .parameter(fields) .method(new AlertDialogFactory<>()) .show(); AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog(); Assertions.assertThat(dialog.getButton(AlertDialog.BUTTON_POSITIVE)).hasText(positive); Assertions.assertThat(dialog.getButton(AlertDialog.BUTTON_NEUTRAL)).hasText(neutral); Assertions.assertThat(dialog.getButton(AlertDialog.BUTTON_NEGATIVE)).hasText(negative); }
@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 buildingAMagic8Ball() throws Exception { Activity app = Robolectric.setupActivity(EightBall.class); LinearLayout layout = Helper.fetchAppLayout(app); android.widget.TextView text = (TextView) layout.getChildAt(0); android.widget.EditText editText = (EditText) layout.getChildAt(1); android.widget.Button button = (android.widget.Button) layout.getChildAt(2); assertEquals(app.getTitle(), "Magic 8-Ball"); assertEquals("Ask the 8-Ball a question:", text.getText()); assertEquals("", editText.getText().toString()); assertEquals("Shake", button.getText()); button.performClick(); ShadowAlertDialog popup = (shadowOf((AlertDialog) ShadowDialog.getLatestDialog())); assertTrue(Arrays.asList("Yes!", "No!", "Maybe?").contains(popup.getTitle())); }
@Test public void testCreateDirectoryDialog() { final String directoryName = "mydir"; final DirectoryChooserFragment fragment = DirectoryChooserFragment.newInstance( directoryName, null); 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 = Robolectric.shadowOf(dialog); assertEquals(shadowAlertDialog.getTitle(), "Create folder"); assertEquals(shadowAlertDialog.getMessage(), "Create new folder with name \"mydir\"?"); }
@Test public void testThatCheckboxIsCheckedWhenOrderingCommoditiesWithZeroStock() throws Exception { Category antiMalarials = categoryService.all().get(0); Commodity firstCommodity = commodityService.all().get(0); Commodity spyFirstCommodity = spy(firstCommodity); when(spyFirstCommodity.isOutOfStock()).thenReturn(true); ArrayList<BaseCommodityViewModel> currentlySelectedCommodities = new ArrayList<>(); currentlySelectedCommodities.add(new BaseCommodityViewModel(spyFirstCommodity)); itemSelectFragment = ItemSelectFragment.newInstance(getDispenseActivity(), antiMalarials, currentlySelectedCommodities, ALLOW_CLICK_WHEN_OUT_OF_STOCK, getViewModelConverter(), "Activity"); startFragment(itemSelectFragment); Dialog dialog = ShadowDialog.getLatestDialog(); GridView commoditiesLayout = (GridView) dialog.findViewById(R.id.gridViewCommodities); BaseCommodityViewModel loadedCommodity = (BaseCommodityViewModel) commoditiesLayout.getAdapter().getItem(0); assertTrue(loadedCommodity.isSelected()); }
@Test public void testCategoryButtonClickChangesTheEmptyViewText() throws Exception { Dialog dialog = ShadowDialog.getLatestDialog(); TextView textViewEmptyText = (TextView) dialog.findViewById(R.id.editTextEmptyText); LinearLayout categoriesLayout = (LinearLayout) dialog.findViewById(R.id.itemSelectOverlayCategories); Button secondCategoryButton = (Button) categoriesLayout.getChildAt(1); secondCategoryButton.performClick(); GridView commoditiesLayout = (GridView) dialog.findViewById(R.id.gridViewCommodities); assertThat(commoditiesLayout, not(nullValue())); assertThat(commoditiesLayout.getAdapter().getCount(), is(1)); assertThat(secondCategoryButton.isSelected(), is(true)); ANDROID.assertThat(textViewEmptyText).hasText("No Commodities Available"); }
@Test public void shouldConfirmOrderAndOrderItemsOnSubmit() { orderActivity = getOrderActivity(); Commodity commodity = mock(Commodity.class); when(commodity.getName()).thenReturn("Commodity 1"); when(commodity.getId()).thenReturn("id"); when(commodity.getStockOnHand()).thenReturn(10); when(commodity.getMaximumThreshold()).thenReturn(30); OrderCommodityViewModel viewModel = spy(new OrderCommodityViewModel(commodity, 10)); viewModel.setOrderPeriodEndDate(new Date()); viewModel.setOrderPeriodStartDate(new Date()); viewModel.setExpectedOrderQuantity(10); when(viewModel.getStockOnHand()).thenReturn(5); EventBus.getDefault().post(new CommodityToggledEvent(viewModel)); Button buttonSubmitOrder = orderActivity.buttonSubmitOrder; assertThat(buttonSubmitOrder.getVisibility(), is(View.VISIBLE)); buttonSubmitOrder.performClick(); Dialog dialog = ShadowDialog.getLatestDialog(); assertThat(dialog, is(notNullValue())); }
@Test public void shouldShowErrorMessageWhenUnexpectedOrderQuantityIsGiven() throws Exception { orderActivity = getOrderActivity(); OrderCommodityViewModel viewModel = new OrderCommodityViewModel(new Commodity("id", "Panado"), 20); viewModel.setOrderPeriodEndDate(new Date()); viewModel.setOrderPeriodStartDate(new Date()); OrderCommodityViewModel spyViewModel = spy(viewModel); when(spyViewModel.getExpectedOrderQuantity()).thenReturn(10); EventBus.getDefault().post(new CommodityToggledEvent(spyViewModel)); orderActivity.spinnerOrderType.setSelection(0); orderActivity.getSubmitButton().performClick(); assertThat(ShadowToast.getTextOfLatestToast(), is(String.format(application.getString(R.string.unexpected_order_quantity_error), spyViewModel.getName(), spyViewModel.getExpectedOrderQuantity())) ); assertThat(ShadowDialog.getLatestDialog(), is(nullValue())); }
@Test public void shouldShowErrorMessageWhenEmergencyReasonIsNull() throws Exception { orderActivity = getOrderActivity(); OrderCommodityViewModel viewModel = new OrderCommodityViewModel(new Commodity("id", "Panado"), 20); viewModel.setOrderPeriodEndDate(new Date()); viewModel.setOrderPeriodStartDate(new Date()); OrderCommodityViewModel spyViewModel = spy(viewModel); when(spyViewModel.getExpectedOrderQuantity()).thenReturn(10); EventBus.getDefault().post(new CommodityToggledEvent(spyViewModel)); orderActivity.spinnerOrderType.setSelection(1); orderActivity.getSubmitButton().performClick(); assertThat(ShadowToast.getTextOfLatestToast(), is(String.format(application.getString(R.string.unexpected_order_quantity_error), spyViewModel.getName(), spyViewModel.getExpectedOrderQuantity())) ); assertThat(ShadowDialog.getLatestDialog(), is(nullValue())); }
@Test public void whenCanceled_thenCancelMethodCalled() { DelegateDialog dialog = LambdaDialogs.delegate(activity) .method(TestActivity::createDialog) .cancelMethod(mockActivityMethod) .cancelable(true) .build(); assertThat(dialog.isCancelable()).isTrue(); dialog.show(activity.getSupportFragmentManager(), "tag"); ShadowDialog.getLatestDialog().cancel(); verify(mockActivityMethod).call(eq(activity)); }
@Test public void whenDismissed_thenDismissMethodCalled() { LambdaDialogs.delegate(activity) .method(TestActivity::createDialog) .dismissMethod(mockActivityMethod) .show("Tag"); ShadowDialog.getLatestDialog().dismiss(); verify(mockActivityMethod).call(eq(activity)); }
@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); }
@Test public void testCreateDirectoryDialogDisallowFolderNameModification() { final String directoryName = "mydir"; final DirectoryChooserFragment fragment = DirectoryChooserFragment.newInstance( DirectoryChooserConfig.builder() .newDirectoryName(directoryName) .initialDirectory("") .allowReadOnlyDirectory(false) .allowNewDirectoryNameModification(false) .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).isGone(); }
@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 testAboutClick() { Preference about = fragment.getPreferenceScreen().findPreference("about"); ShadowPreference shadowPreference = Shadows.shadowOf(about); shadowPreference.click(); AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog(); Button b = dialog.getButton(AlertDialog.BUTTON_POSITIVE); b.performClick(); assertFalse(dialog.isShowing()); }
@Test public void testOnClickImportDatabaseButton() { System.out.println("--onClickImportDatabaseButton--"); Button importDataBaseButton = (Button) databaseSettingActivity.findViewById(R.id.upload_database_button); assertTrue(importDataBaseButton.performClick()); AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog(); assertEquals(2, dialog.getListView().getAdapter().getCount()); }
@Test public void shouldCloseDialogWhenBackButtonIsPressed() { Dialog dialog = ShadowDialog.getLatestDialog(); assertTrue(dialog.isShowing()); Button closeButton = (Button) dialog.findViewById(R.id.button_losses_goBack); closeButton.performClick(); assertFalse(dialog.isShowing()); }
@Test public void shouldSaveLossesGivenConfirmButtonIsClicked() throws Exception { Dialog dialog = ShadowDialog.getLatestDialog(); assertTrue(dialog.isShowing()); Button confirmButton = (Button) dialog.findViewById(R.id.button_losses_confirm); confirmButton.performClick(); verify(lossServiceMock).saveLoss(Matchers.<Loss>anyObject()); assertThat(ShadowToast.getTextOfLatestToast(), is(application.getResources().getString(R.string.loss_successful))); assertFalse(dialog.isShowing()); }
@Test public void testConfirmButtonExists() throws Exception { Dialog dialog = ShadowDialog.getLatestDialog(); Button buttonClose = (Button) dialog.findViewById(R.id.buttonDispenseConfirm); assertThat(buttonClose, not(nullValue())); assertThat(buttonClose.getText().toString(), is(Robolectric.application.getString(R.string.confirm))); }
@Test public void testListViewExists() throws Exception { Dialog dialog = ShadowDialog.getLatestDialog(); ListView listView = (ListView) dialog.findViewById(R.id.listViewConfirmItems); assertThat(listView, not(nullValue())); assertThat(listView.getAdapter(), not(nullValue())); }
@Test public void testGoBackButtonExists() throws Exception { Dialog dialog = ShadowDialog.getLatestDialog(); Button buttonGoBack = (Button) dialog.findViewById(R.id.buttonDispenseGoBack); assertThat(buttonGoBack, not(nullValue())); }
@Test public void testConfirmButtonLogic() throws Exception { Dialog dialog = ShadowDialog.getLatestDialog(); Button buttonClose = (Button) dialog.findViewById(R.id.buttonDispenseConfirm); assertThat(dispenseConfirmationFragment.dispensing, not(nullValue())); buttonClose.callOnClick(); verify(mockDispensingService).addDispensing(Matchers.<Dispensing>anyObject()); assertFalse(dialog.isShowing()); }
@Test public void testGoBackButtonLogic() throws Exception { Dialog dialog = ShadowDialog.getLatestDialog(); Button buttonGoBack = (Button) dialog.findViewById(R.id.buttonDispenseGoBack); assertThat(dispenseConfirmationFragment.dispensing, not(nullValue())); buttonGoBack.callOnClick(); verify(mockDispensingService, never()).addDispensing(Matchers.<Dispensing>anyObject()); assertFalse(dialog.isShowing()); }
@Test public void testShouldRenderAllCategoryButtons() throws Exception { Dialog dialog = ShadowDialog.getLatestDialog(); assertTrue(dialog.isShowing()); LinearLayout categoriesLayout = (LinearLayout) dialog.findViewById(R.id.itemSelectOverlayCategories); assertThat(categoriesLayout, not(nullValue())); assertThat(categoriesLayout.getChildCount(), is(7)); for (int i = 0; i < categoriesLayout.getChildCount(); i++) { Button button = (Button) categoriesLayout.getChildAt(i); Category currentCategory = categoryService.all().get(i); assertThat(button.getText().toString(), equalTo(currentCategory.getName())); } }
@Test public void testCategoryButtonClickChangesCommoditiesShowing() throws Exception { Dialog dialog = ShadowDialog.getLatestDialog(); LinearLayout categoriesLayout = (LinearLayout) dialog.findViewById(R.id.itemSelectOverlayCategories); Button secondCategoryButton = (Button) categoriesLayout.getChildAt(1); secondCategoryButton.performClick(); GridView commoditiesLayout = (GridView) dialog.findViewById(R.id.gridViewCommodities); assertThat(commoditiesLayout, not(nullValue())); assertThat(commoditiesLayout.getAdapter().getCount(), is(1)); assertThat(secondCategoryButton.isSelected(), is(true)); }
@Test public void testCloseButtonClosesTheDialog() throws Exception { Dialog dialog = ShadowDialog.getLatestDialog(); Button buttonClose = (Button) dialog.findViewById(R.id.buttonClose); assertTrue(itemSelectFragment.isVisible()); buttonClose.callOnClick(); assertFalse(itemSelectFragment.isVisible()); }
@Test public void shouldPreCheckCommoditiesInSelectedCommoditiesListPassedByDispenseActivity() { Category antiMalarials = categoryService.all().get(0); ArrayList<BaseCommodityViewModel> commodities = new ArrayList<>(); Commodity firstCommodity = commodityService.all().get(0); commodities.add(new BaseCommodityViewModel(firstCommodity)); itemSelectFragment = ItemSelectFragment.newInstance(getDispenseActivity(), antiMalarials, commodities, DISALLOW_CLICK_WHEN_OUT_OF_STOCK, getViewModelConverter(), "Activity"); startFragment(itemSelectFragment); Dialog dialog = ShadowDialog.getLatestDialog(); GridView commoditiesLayout = (GridView) dialog.findViewById(R.id.gridViewCommodities); BaseCommodityViewModel loadedCommodity = (BaseCommodityViewModel) commoditiesLayout.getAdapter().getItem(0); assertTrue(loadedCommodity.isSelected()); }
@Test public void testThatShouldNotShowSomeOfTheCommoditiesIfTheStrategyAllowsHiding() throws Exception { Category vaccines = categoryService.all().get(6); int commodityCount = vaccines.getCommodities().size(); itemSelectFragment = ItemSelectFragment.newInstance(getDispenseActivity(), vaccines, new ArrayList<BaseCommodityViewModel>(), CommodityDisplayStrategy.ALLOW_ONLY_LGA_COMMODITIES, getViewModelConverter(), "Activity"); startFragment(itemSelectFragment); Dialog dialog = ShadowDialog.getLatestDialog(); GridView commoditiesLayout = (GridView) dialog.findViewById(R.id.gridViewCommodities); int expectedCount = commodityCount - 1; assertThat(commoditiesLayout.getAdapter().getCount(), is(expectedCount)); }
@Test public void testConfirmButtonLogic() throws Exception { Dialog dialog = ShadowDialog.getLatestDialog(); assertTrue(dialog.isShowing()); Button buttonClose = (Button) dialog.findViewById(R.id.buttonOrderConfirm); buttonClose.performClick(); verify(mockOrderingService).saveOrder(Matchers.<Order>anyObject()); assertFalse(dialog.isShowing()); }
@Test public void testGoBackButtonLogic() throws Exception { Dialog dialog = ShadowDialog.getLatestDialog(); assertTrue(dialog.isShowing()); Button buttonGoBack = (Button) dialog.findViewById(R.id.buttonOrderGoBack); buttonGoBack.performClick(); verify(mockOrderingService, never()).saveOrder(Matchers.<Order>anyObject()); assertFalse(dialog.isShowing()); }
@Test public void shouldCloseFragmentWhenBackButtonIsClicked() { Dialog dialog = ShadowDialog.getLatestDialog(); assertTrue(dialog.isShowing()); Button closeButton = (Button) dialog.findViewById(R.id.button_receive_go_back); closeButton.performClick(); assertFalse(dialog.isShowing()); }
@Test public void shouldSaveReceiveWhenConfirmButtonIsClicked() throws Exception{ Dialog dialog = ShadowDialog.getLatestDialog(); Button confirmButton = (Button) dialog.findViewById(R.id.button_receive_confirm); confirmButton.performClick(); verify(mockReceiveService).saveReceive(Matchers.<Receive>anyObject()); assertThat(ShadowToast.getTextOfLatestToast(), is(application.getResources().getString(R.string.receive_successful))); assertFalse(dialog.isShowing()); }
@Test public void shouldShowConfirmButtonIfDispensingIsValid() throws Exception { DispenseActivity dispenseActivity = getDispenseActivity(); GridView mockGridView = mock(GridView.class); View mockListItemView = mock(View.class); SelectedCommoditiesAdapter mockSelectedCommoditiesAdapter = mock(SelectedCommoditiesAdapter.class); EditText mockEditText = new EditText(application); mockEditText.setText("12"); when(mockListItemView.findViewById(R.id.editTextQuantity)).thenReturn(mockEditText); when(mockGridView.getAdapter()).thenReturn(mockSelectedCommoditiesAdapter); when(mockSelectedCommoditiesAdapter.getItem(anyInt())).thenReturn(new BaseCommodityViewModel(new Commodity("food"))); when(mockSelectedCommoditiesAdapter.getCount()).thenReturn(1); when(mockSelectedCommoditiesAdapter.getView(anyInt(), org.mockito.Matchers.any(View.class), org.mockito.Matchers.any(ViewGroup.class))).thenReturn(mockListItemView); when(mockGridView.getChildAt(anyInt())).thenReturn(mockListItemView); when(mockGridView.getChildCount()).thenReturn(1); dispenseActivity.gridViewSelectedCommodities = mockGridView; dispenseActivity.findViewById(R.id.buttonSubmitDispense).performClick(); Dialog dialog = ShadowDialog.getLatestDialog(); assertThat(dialog, is(notNullValue())); }
@Test public void shouldShowInvalidFieldsToastGivenSubmitWithEmptyFields() { orderActivity = getOrderActivity(); OrderCommodityViewModel commodityViewModel1 = new OrderCommodityViewModel(new Commodity("id", "Commodity 1"), 10); EventBus.getDefault().post(new CommodityToggledEvent(commodityViewModel1)); orderActivity.buttonSubmitOrder.performClick(); assertThat(ShadowToast.getTextOfLatestToast(), is(application.getResources().getString(R.string.fillInAllOrderItemValues))); assertThat(ShadowDialog.getLatestDialog(), is(nullValue())); }
@Test public void shouldOpenConfirmDialogIfLossesAreValid() throws Exception { Commodity mockCommodity = buildMockCommodity(); when(mockCommodity.getStockOnHand()).thenReturn(10); LossesCommodityViewModel lossesCommodityViewModel = new LossesCommodityViewModel(mockCommodity); lossesCommodityViewModel.setLoss(MISSING, 1); EventBus.getDefault().post(new CommodityToggledEvent(lossesCommodityViewModel)); Button submitButton = lossesActivity.getSubmitButton(); submitButton.performClick(); assertThat(submitButton.getVisibility(), is(View.VISIBLE)); Dialog latestDialog = ShadowDialog.getLatestDialog(); assertThat(latestDialog, is(notNullValue())); }
@Test public void shouldNotOpenConfirmDialogIfLossesAreInvalid() { LossesCommodityViewModel lossesCommodityViewModel = new LossesCommodityViewModel(buildMockCommodity()); EventBus.getDefault().post(new CommodityToggledEvent(lossesCommodityViewModel)); Button submitButton = lossesActivity.getSubmitButton(); submitButton.performClick(); assertThat(ShadowToast.getTextOfLatestToast(), is(application.getResources().getString(R.string.fillInSomeLosses))); assertThat(ShadowDialog.getLatestDialog(), is(nullValue())); }
@Test public void clickingOnFloatingActionButton_shouldStartDialog() { // ARRANGE FloatingActionButton addFriendFloatingActionButton = (FloatingActionButton) mDeskActivity.findViewById(R.id.deskfloatingactionbutton); // ACT addFriendFloatingActionButton.performClick(); InputAddFriendDialog inputAddFriendDialog = (InputAddFriendDialog) ShadowDialog.getLatestDialog(); // ASSERT Assert.assertTrue(inputAddFriendDialog.isShowing()); }