Skip to content

Commit

Permalink
make dialogIsNotCancellable() test green
Browse files Browse the repository at this point in the history
  • Loading branch information
SaumiaSinghal committed Jun 16, 2020
1 parent 494c433 commit b9910e4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ public boolean canAddRepeat() {
}
}

public String getLastRepeatedGroupName() {
return formController.getLastRepeatedGroupName();
}

public int getLastRepeatedGroupRepeatCount() {
return formController.getLastRepeatedGroupRepeatCount();
}

public static class Factory implements ViewModelProvider.Factory {

private final Analytics analytics;
Expand All @@ -138,4 +146,4 @@ public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
return (T) new FormEntryViewModel(analytics);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,24 @@
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
import androidx.lifecycle.ViewModelProviders;

import org.odk.collect.android.R;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.javarosawrapper.FormController;
import org.odk.collect.android.formentry.FormEntryViewModel;

import static android.content.DialogInterface.BUTTON_NEGATIVE;
import static android.content.DialogInterface.BUTTON_POSITIVE;

public class DeleteRepeatDialogFragment extends DialogFragment {

protected FormController formController = Collect.getInstance().getFormController();
private FormEntryViewModel viewModel;
protected DeleteRepeatDialogCallback callback;

@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);

viewModel = ViewModelProviders.of(requireActivity()).get(FormEntryViewModel.class);
if (context instanceof DeleteRepeatDialogCallback) {
callback = (DeleteRepeatDialogCallback) context;
}
Expand All @@ -36,8 +37,8 @@ public void onAttach(@NonNull Context context) {
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
super.onCreateDialog(savedInstanceState);

String name = formController.getLastRepeatedGroupName();
int repeatcount = formController.getLastRepeatedGroupRepeatCount();
String name = viewModel.getLastRepeatedGroupName();
int repeatcount = viewModel.getLastRepeatedGroupRepeatCount();
if (repeatcount != -1) {
name += " (" + (repeatcount + 1) + ")";
}
Expand All @@ -58,6 +59,7 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
alertDialog.cancel();
dismiss();
};
setCancelable(false);
alertDialog.setCancelable(false);
alertDialog.setButton(BUTTON_POSITIVE, getActivity().getString(R.string.discard_group), quitListener);
alertDialog.setButton(BUTTON_NEGATIVE, getActivity().getString(R.string.delete_repeat_no), quitListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,16 @@ public void cancelRepeatPrompt_whenThereIsAnErrorSteppingToNextScreen_setsErrorW
viewModel.cancelRepeatPrompt();
assertThat(viewModel.getError().getValue(), equalTo("OH NO"));
}

@Test
public void getLastRepeatedGroupName_returnsCorrectGroupName() {
when(formController.getLastRepeatedGroupName()).thenReturn("blah");
assertThat(viewModel.getLastRepeatedGroupName(), equalTo("blah"));
}

@Test
public void getLastRepeatedGroupRepeatCount_returnsCorrectGroupRepeatCount() {
when(formController.getLastRepeatedGroupRepeatCount()).thenReturn(1);
assertThat(viewModel.getLastRepeatedGroupRepeatCount(), equalTo(1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.odk.collect.android.R;
import org.odk.collect.android.javarosawrapper.FormController;
import org.odk.collect.android.formentry.FormEntryViewModel;
import org.odk.collect.android.support.RobolectricHelpers;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.android.controller.ActivityController;
import org.robolectric.shadows.ShadowDialog;

import static junit.framework.TestCase.assertTrue;
Expand All @@ -26,38 +25,36 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.odk.collect.android.support.RobolectricHelpers.mockViewModelProvider;
import static org.robolectric.Shadows.shadowOf;

@RunWith(RobolectricTestRunner.class)
public class DeleteRepeatDialogFragmentTest {

private ActivityController<FragmentActivity> activity;
private FragmentManager fragmentManager;
private DeleteRepeatDialogFragment dialogFragment;
private FormEntryViewModel formEntryViewModel;

@Before
public void setup() {
activity = RobolectricHelpers.buildThemedActivity(FragmentActivity.class);
activity.setup();
fragmentManager = activity.get().getSupportFragmentManager();
FragmentActivity activity = RobolectricHelpers.createThemedActivity(FragmentActivity.class);
fragmentManager = activity.getSupportFragmentManager();
dialogFragment = new DeleteRepeatDialogFragment();

dialogFragment.formController = mock(FormController.class);
dialogFragment.callback = mock(DeleteRepeatDialogFragment.DeleteRepeatDialogCallback.class);
formEntryViewModel = mockViewModelProvider(activity, FormEntryViewModel.class).get(FormEntryViewModel.class);
}

@Test
public void dialogIsNotCancellable() {
dialogFragment.show(fragmentManager, "TAG");
AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
activity.get().finish();
assertThat(dialog.isShowing(), equalTo(true));
assertThat(shadowOf(dialogFragment.getDialog()).isCancelable(), equalTo(false));
}

@Test
public void shouldShowCorrectMessage() {
when(dialogFragment.formController.getLastRepeatedGroupName()).thenReturn("blah");
when(dialogFragment.formController.getLastRepeatedGroupRepeatCount()).thenReturn(0);
when(formEntryViewModel.getLastRepeatedGroupName()).thenReturn("blah");
when(formEntryViewModel.getLastRepeatedGroupRepeatCount()).thenReturn(0);
dialogFragment.show(fragmentManager, "TAG");
AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
String message = ((TextView) dialog.findViewById(android.R.id.message)).getText().toString();
Expand Down

0 comments on commit b9910e4

Please sign in to comment.