1
1
package pub .devrel .easypermissions ;
2
2
3
+ import android .content .DialogInterface ;
3
4
import android .content .Intent ;
5
+ import android .support .v7 .app .AlertDialog ;
6
+ import android .widget .Button ;
4
7
5
8
import org .junit .After ;
6
9
import org .junit .Before ;
7
10
import org .junit .Test ;
8
11
import org .junit .runner .RunWith ;
9
12
import org .mockito .ArgumentCaptor ;
10
13
import org .mockito .Captor ;
14
+ import org .mockito .Mock ;
11
15
import org .mockito .Mockito ;
12
16
import org .mockito .MockitoAnnotations ;
13
17
import org .robolectric .Robolectric ;
27
31
import pub .devrel .easypermissions .testhelper .TestSupportFragment ;
28
32
29
33
import static com .google .common .truth .Truth .assertThat ;
34
+ import static org .mockito .ArgumentMatchers .any ;
35
+ import static org .mockito .ArgumentMatchers .anyInt ;
30
36
import static org .mockito .Mockito .times ;
31
37
import static org .mockito .Mockito .verify ;
32
38
import static org .robolectric .Shadows .shadowOf ;
@@ -47,6 +53,10 @@ public class AppSettingsDialogTest {
47
53
private ActivityController <TestActivity > activityController ;
48
54
private FragmentController <TestFragment > fragmentController ;
49
55
private SupportFragmentController <TestSupportFragment > supportFragmentController ;
56
+ @ Mock
57
+ private DialogInterface .OnClickListener positiveListener ;
58
+ @ Mock
59
+ private DialogInterface .OnClickListener negativeListener ;
50
60
@ Captor
51
61
private ArgumentCaptor <Integer > integerCaptor ;
52
62
@ Captor
@@ -67,12 +77,12 @@ public void tearDown() {
67
77
// ------ From Activity ------
68
78
69
79
@ Test
70
- public void shouldStartExpectedSettingsDialog_whenBuildingFromActivity () {
80
+ public void shouldShowExpectedSettingsDialog_whenBuildingFromActivity () {
71
81
new AppSettingsDialog .Builder (spyActivity )
72
- .setTitle (TITLE )
73
- .setRationale (RATIONALE )
74
- .setPositiveButton (POSITIVE )
75
- .setNegativeButton (NEGATIVE )
82
+ .setTitle (android . R . string . dialog_alert_title )
83
+ .setRationale (android . R . string . unknownName )
84
+ .setPositiveButton (android . R . string . ok )
85
+ .setNegativeButton (android . R . string . cancel )
76
86
.setThemeResId (R .style .Theme_AppCompat )
77
87
.build ()
78
88
.show ();
@@ -88,17 +98,51 @@ public void shouldStartExpectedSettingsDialog_whenBuildingFromActivity() {
88
98
assertThat (shadowIntent .getIntentClass ()).isEqualTo (AppSettingsDialogHolderActivity .class );
89
99
}
90
100
91
- // ------ From Fragment ------
101
+ @ Test
102
+ public void shouldPositiveListener_whenClickingPositiveButtonFromActivity () {
103
+ AlertDialog alertDialog = new AppSettingsDialog .Builder (spyActivity )
104
+ .setTitle (TITLE )
105
+ .setRationale (RATIONALE )
106
+ .setPositiveButton (POSITIVE )
107
+ .setNegativeButton (NEGATIVE )
108
+ .setThemeResId (R .style .Theme_AppCompat )
109
+ .build ()
110
+ .showDialog (positiveListener , negativeListener );
111
+ Button positive = alertDialog .getButton (AlertDialog .BUTTON_POSITIVE );
112
+ positive .performClick ();
113
+
114
+ verify (positiveListener , times (1 ))
115
+ .onClick (any (DialogInterface .class ), anyInt ());
116
+ }
92
117
93
118
@ Test
94
- public void shouldStartExpectedSettingsDialog_whenBuildingFromFragment () {
95
- new AppSettingsDialog .Builder (spyFragment )
119
+ public void shouldNegativeListener_whenClickingPositiveButtonFromActivity () {
120
+ AlertDialog alertDialog = new AppSettingsDialog .Builder (spyActivity )
96
121
.setTitle (TITLE )
97
122
.setRationale (RATIONALE )
98
123
.setPositiveButton (POSITIVE )
99
124
.setNegativeButton (NEGATIVE )
100
125
.setThemeResId (R .style .Theme_AppCompat )
101
126
.build ()
127
+ .showDialog (positiveListener , negativeListener );
128
+ Button positive = alertDialog .getButton (AlertDialog .BUTTON_NEGATIVE );
129
+ positive .performClick ();
130
+
131
+ verify (negativeListener , times (1 ))
132
+ .onClick (any (DialogInterface .class ), anyInt ());
133
+ }
134
+
135
+ // ------ From Fragment ------
136
+
137
+ @ Test
138
+ public void shouldShowExpectedSettingsDialog_whenBuildingFromFragment () {
139
+ new AppSettingsDialog .Builder (spyFragment )
140
+ .setTitle (android .R .string .dialog_alert_title )
141
+ .setRationale (android .R .string .unknownName )
142
+ .setPositiveButton (android .R .string .ok )
143
+ .setNegativeButton (android .R .string .cancel )
144
+ .setThemeResId (R .style .Theme_AppCompat )
145
+ .build ()
102
146
.show ();
103
147
104
148
verify (spyFragment , times (1 ))
@@ -112,10 +156,44 @@ public void shouldStartExpectedSettingsDialog_whenBuildingFromFragment() {
112
156
assertThat (shadowIntent .getIntentClass ()).isEqualTo (AppSettingsDialogHolderActivity .class );
113
157
}
114
158
159
+ @ Test
160
+ public void shouldPositiveListener_whenClickingPositiveButtonFromFragment () {
161
+ AlertDialog alertDialog = new AppSettingsDialog .Builder (spyFragment )
162
+ .setTitle (TITLE )
163
+ .setRationale (RATIONALE )
164
+ .setPositiveButton (POSITIVE )
165
+ .setNegativeButton (NEGATIVE )
166
+ .setThemeResId (R .style .Theme_AppCompat )
167
+ .build ()
168
+ .showDialog (positiveListener , negativeListener );
169
+ Button positive = alertDialog .getButton (AlertDialog .BUTTON_POSITIVE );
170
+ positive .performClick ();
171
+
172
+ verify (positiveListener , times (1 ))
173
+ .onClick (any (DialogInterface .class ), anyInt ());
174
+ }
175
+
176
+ @ Test
177
+ public void shouldNegativeListener_whenClickingPositiveButtonFromFragment () {
178
+ AlertDialog alertDialog = new AppSettingsDialog .Builder (spyFragment )
179
+ .setTitle (TITLE )
180
+ .setRationale (RATIONALE )
181
+ .setPositiveButton (POSITIVE )
182
+ .setNegativeButton (NEGATIVE )
183
+ .setThemeResId (R .style .Theme_AppCompat )
184
+ .build ()
185
+ .showDialog (positiveListener , negativeListener );
186
+ Button positive = alertDialog .getButton (AlertDialog .BUTTON_NEGATIVE );
187
+ positive .performClick ();
188
+
189
+ verify (negativeListener , times (1 ))
190
+ .onClick (any (DialogInterface .class ), anyInt ());
191
+ }
192
+
115
193
// ------ From Support Fragment ------
116
194
117
195
@ Test
118
- public void shouldStartExpectedSettingsDialog_whenBuildingFromSupportFragment () {
196
+ public void shouldShowExpectedSettingsDialog_whenBuildingFromSupportFragment () {
119
197
new AppSettingsDialog .Builder (spySupportFragment )
120
198
.setTitle (android .R .string .dialog_alert_title )
121
199
.setRationale (android .R .string .unknownName )
@@ -136,6 +214,40 @@ public void shouldStartExpectedSettingsDialog_whenBuildingFromSupportFragment()
136
214
assertThat (shadowIntent .getIntentClass ()).isEqualTo (AppSettingsDialogHolderActivity .class );
137
215
}
138
216
217
+ @ Test
218
+ public void shouldPositiveListener_whenClickingPositiveButtonFromSupportFragment () {
219
+ AlertDialog alertDialog = new AppSettingsDialog .Builder (spySupportFragment )
220
+ .setTitle (TITLE )
221
+ .setRationale (RATIONALE )
222
+ .setPositiveButton (POSITIVE )
223
+ .setNegativeButton (NEGATIVE )
224
+ .setThemeResId (R .style .Theme_AppCompat )
225
+ .build ()
226
+ .showDialog (positiveListener , negativeListener );
227
+ Button positive = alertDialog .getButton (AlertDialog .BUTTON_POSITIVE );
228
+ positive .performClick ();
229
+
230
+ verify (positiveListener , times (1 ))
231
+ .onClick (any (DialogInterface .class ), anyInt ());
232
+ }
233
+
234
+ @ Test
235
+ public void shouldNegativeListener_whenClickingPositiveButtonFromSupportFragment () {
236
+ AlertDialog alertDialog = new AppSettingsDialog .Builder (spySupportFragment )
237
+ .setTitle (TITLE )
238
+ .setRationale (RATIONALE )
239
+ .setPositiveButton (POSITIVE )
240
+ .setNegativeButton (NEGATIVE )
241
+ .setThemeResId (R .style .Theme_AppCompat )
242
+ .build ()
243
+ .showDialog (positiveListener , negativeListener );
244
+ Button positive = alertDialog .getButton (AlertDialog .BUTTON_NEGATIVE );
245
+ positive .performClick ();
246
+
247
+ verify (negativeListener , times (1 ))
248
+ .onClick (any (DialogInterface .class ), anyInt ());
249
+ }
250
+
139
251
private void setUpActivityAndFragment () {
140
252
activityController = Robolectric .buildActivity (TestActivity .class )
141
253
.create ().start ().resume ();
0 commit comments