Skip to content

Commit 3e1de99

Browse files
committed
Rework DeleteRepeatDialogFragment tests
1 parent 2324306 commit 3e1de99

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

collect_app/src/test/java/org/odk/collect/android/formentry/repeats/DeleteRepeatDialogFragmentTest.java

+22-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import static junit.framework.TestCase.assertTrue;
44
import static org.hamcrest.MatcherAssert.assertThat;
55
import static org.hamcrest.Matchers.equalTo;
6-
import static org.hamcrest.Matchers.instanceOf;
6+
import static org.hamcrest.Matchers.notNullValue;
77
import static org.junit.Assert.assertFalse;
88
import static org.mockito.Mockito.RETURNS_MOCKS;
99
import static org.mockito.Mockito.mock;
@@ -12,12 +12,14 @@
1212
import static org.robolectric.Shadows.shadowOf;
1313

1414
import android.content.DialogInterface;
15+
import android.os.Bundle;
1516
import android.widget.TextView;
1617

1718
import androidx.annotation.NonNull;
1819
import androidx.appcompat.app.AlertDialog;
1920
import androidx.fragment.app.FragmentActivity;
2021
import androidx.fragment.app.FragmentManager;
22+
import androidx.fragment.app.FragmentResultListener;
2123
import androidx.lifecycle.ViewModel;
2224
import androidx.lifecycle.ViewModelProvider;
2325
import androidx.lifecycle.viewmodel.CreationExtras;
@@ -61,12 +63,6 @@ public <T extends ViewModel> T create(@NonNull Class<T> modelClass, @NonNull Cre
6163
});
6264
}
6365

64-
@Test
65-
public void fragmentActivityShouldImplementDeleteRepeatDialogCallback() {
66-
launchDialog();
67-
assertThat(dialogFragment.getActivity(), instanceOf(DeleteRepeatDialogFragment.DeleteRepeatDialogCallback.class));
68-
}
69-
7066
@Test
7167
public void dialogIsNotCancellable() {
7268
launchDialog();
@@ -106,14 +102,23 @@ public void clickingRemoveGroup_shouldDismissTheDialog() {
106102
}
107103

108104
@Test
109-
public void clickingRemoveGroup_callsDeleteGroup() {
105+
public void clickingRemoveGroup_setsResult() {
110106
AlertDialog dialog = launchDialog();
111-
assertThat(activity.deleteGroupCalled, equalTo(false));
107+
108+
FragmentResultCapturer fragmentResultCapturer = new FragmentResultCapturer();
109+
activity.getSupportFragmentManager().setFragmentResultListener(DeleteRepeatDialogFragment.REQUEST_DELETE_REPEAT, activity, fragmentResultCapturer);
112110

113111
dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
112+
RobolectricHelpers.runLooper();
113+
assertThat(fragmentResultCapturer.result, notNullValue());
114+
}
114115

116+
@Test
117+
public void clickingRemoveGroup_callsDeleteRepeat() {
118+
AlertDialog dialog = launchDialog();
119+
120+
dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
115121
RobolectricHelpers.runLooper();
116-
assertThat(activity.deleteGroupCalled, equalTo(true));
117122
verify(formController).deleteRepeat();
118123
}
119124

@@ -123,16 +128,17 @@ private AlertDialog launchDialog() {
123128
return (AlertDialog) ShadowDialog.getLatestDialog();
124129
}
125130

126-
public static class TestActivity extends FragmentActivity implements DeleteRepeatDialogFragment.DeleteRepeatDialogCallback {
131+
public static class TestActivity extends FragmentActivity {
127132

128-
private boolean deleteGroupCalled;
133+
}
129134

130-
TestActivity() {
131-
}
135+
private static class FragmentResultCapturer implements FragmentResultListener {
136+
137+
public Bundle result;
132138

133139
@Override
134-
public void deleteGroup() {
135-
deleteGroupCalled = true;
140+
public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle result) {
141+
this.result = result;
136142
}
137143
}
138144
}

0 commit comments

Comments
 (0)