3
3
import static junit .framework .TestCase .assertTrue ;
4
4
import static org .hamcrest .MatcherAssert .assertThat ;
5
5
import static org .hamcrest .Matchers .equalTo ;
6
- import static org .hamcrest .Matchers .instanceOf ;
6
+ import static org .hamcrest .Matchers .notNullValue ;
7
7
import static org .junit .Assert .assertFalse ;
8
8
import static org .mockito .Mockito .RETURNS_MOCKS ;
9
9
import static org .mockito .Mockito .mock ;
12
12
import static org .robolectric .Shadows .shadowOf ;
13
13
14
14
import android .content .DialogInterface ;
15
+ import android .os .Bundle ;
15
16
import android .widget .TextView ;
16
17
17
18
import androidx .annotation .NonNull ;
18
19
import androidx .appcompat .app .AlertDialog ;
19
20
import androidx .fragment .app .FragmentActivity ;
20
21
import androidx .fragment .app .FragmentManager ;
22
+ import androidx .fragment .app .FragmentResultListener ;
21
23
import androidx .lifecycle .ViewModel ;
22
24
import androidx .lifecycle .ViewModelProvider ;
23
25
import androidx .lifecycle .viewmodel .CreationExtras ;
@@ -61,12 +63,6 @@ public <T extends ViewModel> T create(@NonNull Class<T> modelClass, @NonNull Cre
61
63
});
62
64
}
63
65
64
- @ Test
65
- public void fragmentActivityShouldImplementDeleteRepeatDialogCallback () {
66
- launchDialog ();
67
- assertThat (dialogFragment .getActivity (), instanceOf (DeleteRepeatDialogFragment .DeleteRepeatDialogCallback .class ));
68
- }
69
-
70
66
@ Test
71
67
public void dialogIsNotCancellable () {
72
68
launchDialog ();
@@ -106,14 +102,23 @@ public void clickingRemoveGroup_shouldDismissTheDialog() {
106
102
}
107
103
108
104
@ Test
109
- public void clickingRemoveGroup_callsDeleteGroup () {
105
+ public void clickingRemoveGroup_setsResult () {
110
106
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 );
112
110
113
111
dialog .getButton (DialogInterface .BUTTON_POSITIVE ).performClick ();
112
+ RobolectricHelpers .runLooper ();
113
+ assertThat (fragmentResultCapturer .result , notNullValue ());
114
+ }
114
115
116
+ @ Test
117
+ public void clickingRemoveGroup_callsDeleteRepeat () {
118
+ AlertDialog dialog = launchDialog ();
119
+
120
+ dialog .getButton (DialogInterface .BUTTON_POSITIVE ).performClick ();
115
121
RobolectricHelpers .runLooper ();
116
- assertThat (activity .deleteGroupCalled , equalTo (true ));
117
122
verify (formController ).deleteRepeat ();
118
123
}
119
124
@@ -123,16 +128,17 @@ private AlertDialog launchDialog() {
123
128
return (AlertDialog ) ShadowDialog .getLatestDialog ();
124
129
}
125
130
126
- public static class TestActivity extends FragmentActivity implements DeleteRepeatDialogFragment . DeleteRepeatDialogCallback {
131
+ public static class TestActivity extends FragmentActivity {
127
132
128
- private boolean deleteGroupCalled ;
133
+ }
129
134
130
- TestActivity () {
131
- }
135
+ private static class FragmentResultCapturer implements FragmentResultListener {
136
+
137
+ public Bundle result ;
132
138
133
139
@ Override
134
- public void deleteGroup ( ) {
135
- deleteGroupCalled = true ;
140
+ public void onFragmentResult ( @ NonNull String requestKey , @ NonNull Bundle result ) {
141
+ this . result = result ;
136
142
}
137
143
}
138
144
}
0 commit comments