Skip to content

Commit ffa9b74

Browse files
author
Henrique Faria
committed
Add tests for AppSettingsDialogTest listeners
1 parent 29143fe commit ffa9b74

File tree

1 file changed

+121
-9
lines changed

1 file changed

+121
-9
lines changed

easypermissions/src/test/java/pub/devrel/easypermissions/AppSettingsDialogTest.java

Lines changed: 121 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package pub.devrel.easypermissions;
22

3+
import android.content.DialogInterface;
34
import android.content.Intent;
5+
import android.support.v7.app.AlertDialog;
6+
import android.widget.Button;
47

58
import org.junit.After;
69
import org.junit.Before;
710
import org.junit.Test;
811
import org.junit.runner.RunWith;
912
import org.mockito.ArgumentCaptor;
1013
import org.mockito.Captor;
14+
import org.mockito.Mock;
1115
import org.mockito.Mockito;
1216
import org.mockito.MockitoAnnotations;
1317
import org.robolectric.Robolectric;
@@ -27,6 +31,8 @@
2731
import pub.devrel.easypermissions.testhelper.TestSupportFragment;
2832

2933
import static com.google.common.truth.Truth.assertThat;
34+
import static org.mockito.ArgumentMatchers.any;
35+
import static org.mockito.ArgumentMatchers.anyInt;
3036
import static org.mockito.Mockito.times;
3137
import static org.mockito.Mockito.verify;
3238
import static org.robolectric.Shadows.shadowOf;
@@ -47,6 +53,10 @@ public class AppSettingsDialogTest {
4753
private ActivityController<TestActivity> activityController;
4854
private FragmentController<TestFragment> fragmentController;
4955
private SupportFragmentController<TestSupportFragment> supportFragmentController;
56+
@Mock
57+
private DialogInterface.OnClickListener positiveListener;
58+
@Mock
59+
private DialogInterface.OnClickListener negativeListener;
5060
@Captor
5161
private ArgumentCaptor<Integer> integerCaptor;
5262
@Captor
@@ -67,12 +77,12 @@ public void tearDown() {
6777
// ------ From Activity ------
6878

6979
@Test
70-
public void shouldStartExpectedSettingsDialog_whenBuildingFromActivity() {
80+
public void shouldShowExpectedSettingsDialog_whenBuildingFromActivity() {
7181
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)
7686
.setThemeResId(R.style.Theme_AppCompat)
7787
.build()
7888
.show();
@@ -88,17 +98,51 @@ public void shouldStartExpectedSettingsDialog_whenBuildingFromActivity() {
8898
assertThat(shadowIntent.getIntentClass()).isEqualTo(AppSettingsDialogHolderActivity.class);
8999
}
90100

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+
}
92117

93118
@Test
94-
public void shouldStartExpectedSettingsDialog_whenBuildingFromFragment() {
95-
new AppSettingsDialog.Builder(spyFragment)
119+
public void shouldNegativeListener_whenClickingPositiveButtonFromActivity() {
120+
AlertDialog alertDialog = new AppSettingsDialog.Builder(spyActivity)
96121
.setTitle(TITLE)
97122
.setRationale(RATIONALE)
98123
.setPositiveButton(POSITIVE)
99124
.setNegativeButton(NEGATIVE)
100125
.setThemeResId(R.style.Theme_AppCompat)
101126
.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()
102146
.show();
103147

104148
verify(spyFragment, times(1))
@@ -112,10 +156,44 @@ public void shouldStartExpectedSettingsDialog_whenBuildingFromFragment() {
112156
assertThat(shadowIntent.getIntentClass()).isEqualTo(AppSettingsDialogHolderActivity.class);
113157
}
114158

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+
115193
// ------ From Support Fragment ------
116194

117195
@Test
118-
public void shouldStartExpectedSettingsDialog_whenBuildingFromSupportFragment() {
196+
public void shouldShowExpectedSettingsDialog_whenBuildingFromSupportFragment() {
119197
new AppSettingsDialog.Builder(spySupportFragment)
120198
.setTitle(android.R.string.dialog_alert_title)
121199
.setRationale(android.R.string.unknownName)
@@ -136,6 +214,40 @@ public void shouldStartExpectedSettingsDialog_whenBuildingFromSupportFragment()
136214
assertThat(shadowIntent.getIntentClass()).isEqualTo(AppSettingsDialogHolderActivity.class);
137215
}
138216

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+
139251
private void setUpActivityAndFragment() {
140252
activityController = Robolectric.buildActivity(TestActivity.class)
141253
.create().start().resume();

0 commit comments

Comments
 (0)