|
| 1 | +package pub.devrel.easypermissions; |
| 2 | + |
| 3 | +import android.content.DialogInterface; |
| 4 | +import android.content.Intent; |
| 5 | +import android.support.v7.app.AlertDialog; |
| 6 | +import android.widget.Button; |
| 7 | + |
| 8 | +import org.junit.After; |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Test; |
| 11 | +import org.junit.runner.RunWith; |
| 12 | +import org.mockito.ArgumentCaptor; |
| 13 | +import org.mockito.Captor; |
| 14 | +import org.mockito.Mock; |
| 15 | +import org.mockito.Mockito; |
| 16 | +import org.mockito.MockitoAnnotations; |
| 17 | +import org.robolectric.Robolectric; |
| 18 | +import org.robolectric.RobolectricTestRunner; |
| 19 | +import org.robolectric.RuntimeEnvironment; |
| 20 | +import org.robolectric.android.controller.ActivityController; |
| 21 | +import org.robolectric.android.controller.FragmentController; |
| 22 | +import org.robolectric.annotation.Config; |
| 23 | +import org.robolectric.shadows.ShadowApplication; |
| 24 | +import org.robolectric.shadows.ShadowIntent; |
| 25 | +import org.robolectric.shadows.support.v4.SupportFragmentController; |
| 26 | + |
| 27 | +import java.util.Objects; |
| 28 | + |
| 29 | +import pub.devrel.easypermissions.testhelper.TestActivity; |
| 30 | +import pub.devrel.easypermissions.testhelper.TestFragment; |
| 31 | +import pub.devrel.easypermissions.testhelper.TestSupportFragment; |
| 32 | + |
| 33 | +import static com.google.common.truth.Truth.assertThat; |
| 34 | +import static org.mockito.ArgumentMatchers.any; |
| 35 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 36 | +import static org.mockito.Mockito.times; |
| 37 | +import static org.mockito.Mockito.verify; |
| 38 | +import static org.robolectric.Shadows.shadowOf; |
| 39 | +import static pub.devrel.easypermissions.AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE; |
| 40 | + |
| 41 | +@RunWith(RobolectricTestRunner.class) |
| 42 | +@Config(sdk = 23) |
| 43 | +public class AppSettingsDialogTest { |
| 44 | + |
| 45 | + private static final String TITLE = "TITLE"; |
| 46 | + private static final String RATIONALE = "RATIONALE"; |
| 47 | + private static final String NEGATIVE = "NEGATIVE"; |
| 48 | + private static final String POSITIVE = "POSITIVE"; |
| 49 | + private ShadowApplication shadowApp; |
| 50 | + private TestActivity spyActivity; |
| 51 | + private TestFragment spyFragment; |
| 52 | + private TestSupportFragment spySupportFragment; |
| 53 | + private ActivityController<TestActivity> activityController; |
| 54 | + private FragmentController<TestFragment> fragmentController; |
| 55 | + private SupportFragmentController<TestSupportFragment> supportFragmentController; |
| 56 | + @Mock |
| 57 | + private DialogInterface.OnClickListener positiveListener; |
| 58 | + @Mock |
| 59 | + private DialogInterface.OnClickListener negativeListener; |
| 60 | + @Captor |
| 61 | + private ArgumentCaptor<Integer> integerCaptor; |
| 62 | + @Captor |
| 63 | + private ArgumentCaptor<Intent> intentCaptor; |
| 64 | + |
| 65 | + @Before |
| 66 | + public void setUp() { |
| 67 | + MockitoAnnotations.initMocks(this); |
| 68 | + shadowApp = shadowOf(RuntimeEnvironment.application); |
| 69 | + setUpActivityAndFragment(); |
| 70 | + } |
| 71 | + |
| 72 | + @After |
| 73 | + public void tearDown() { |
| 74 | + tearDownActivityAndFragment(); |
| 75 | + } |
| 76 | + |
| 77 | + // ------ From Activity ------ |
| 78 | + |
| 79 | + @Test |
| 80 | + public void shouldShowExpectedSettingsDialog_whenBuildingFromActivity() { |
| 81 | + new AppSettingsDialog.Builder(spyActivity) |
| 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) |
| 86 | + .setThemeResId(R.style.Theme_AppCompat) |
| 87 | + .build() |
| 88 | + .show(); |
| 89 | + |
| 90 | + verify(spyActivity, times(1)) |
| 91 | + .startActivityForResult(intentCaptor.capture(), integerCaptor.capture()); |
| 92 | + assertThat(integerCaptor.getValue()).isEqualTo(DEFAULT_SETTINGS_REQ_CODE); |
| 93 | + assertThat(Objects.requireNonNull(intentCaptor.getValue().getComponent()).getClassName()) |
| 94 | + .isEqualTo(AppSettingsDialogHolderActivity.class.getName()); |
| 95 | + |
| 96 | + Intent startedIntent = shadowApp.getNextStartedActivity(); |
| 97 | + ShadowIntent shadowIntent = shadowOf(startedIntent); |
| 98 | + assertThat(shadowIntent.getIntentClass()).isEqualTo(AppSettingsDialogHolderActivity.class); |
| 99 | + } |
| 100 | + |
| 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 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void shouldNegativeListener_whenClickingPositiveButtonFromActivity() { |
| 120 | + AlertDialog alertDialog = new AppSettingsDialog.Builder(spyActivity) |
| 121 | + .setTitle(TITLE) |
| 122 | + .setRationale(RATIONALE) |
| 123 | + .setPositiveButton(POSITIVE) |
| 124 | + .setNegativeButton(NEGATIVE) |
| 125 | + .setThemeResId(R.style.Theme_AppCompat) |
| 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() |
| 146 | + .show(); |
| 147 | + |
| 148 | + verify(spyFragment, times(1)) |
| 149 | + .startActivityForResult(intentCaptor.capture(), integerCaptor.capture()); |
| 150 | + assertThat(integerCaptor.getValue()).isEqualTo(DEFAULT_SETTINGS_REQ_CODE); |
| 151 | + assertThat(Objects.requireNonNull(intentCaptor.getValue().getComponent()).getClassName()) |
| 152 | + .isEqualTo(AppSettingsDialogHolderActivity.class.getName()); |
| 153 | + |
| 154 | + Intent startedIntent = shadowApp.getNextStartedActivity(); |
| 155 | + ShadowIntent shadowIntent = shadowOf(startedIntent); |
| 156 | + assertThat(shadowIntent.getIntentClass()).isEqualTo(AppSettingsDialogHolderActivity.class); |
| 157 | + } |
| 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 | + |
| 193 | + // ------ From Support Fragment ------ |
| 194 | + |
| 195 | + @Test |
| 196 | + public void shouldShowExpectedSettingsDialog_whenBuildingFromSupportFragment() { |
| 197 | + new AppSettingsDialog.Builder(spySupportFragment) |
| 198 | + .setTitle(android.R.string.dialog_alert_title) |
| 199 | + .setRationale(android.R.string.unknownName) |
| 200 | + .setPositiveButton(android.R.string.ok) |
| 201 | + .setNegativeButton(android.R.string.cancel) |
| 202 | + .setThemeResId(R.style.Theme_AppCompat) |
| 203 | + .build() |
| 204 | + .show(); |
| 205 | + |
| 206 | + verify(spySupportFragment, times(1)) |
| 207 | + .startActivityForResult(intentCaptor.capture(), integerCaptor.capture()); |
| 208 | + assertThat(integerCaptor.getValue()).isEqualTo(DEFAULT_SETTINGS_REQ_CODE); |
| 209 | + assertThat(Objects.requireNonNull(intentCaptor.getValue().getComponent()).getClassName()) |
| 210 | + .isEqualTo(AppSettingsDialogHolderActivity.class.getName()); |
| 211 | + |
| 212 | + Intent startedIntent = shadowApp.getNextStartedActivity(); |
| 213 | + ShadowIntent shadowIntent = shadowOf(startedIntent); |
| 214 | + assertThat(shadowIntent.getIntentClass()).isEqualTo(AppSettingsDialogHolderActivity.class); |
| 215 | + } |
| 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 | + |
| 251 | + private void setUpActivityAndFragment() { |
| 252 | + activityController = Robolectric.buildActivity(TestActivity.class) |
| 253 | + .create().start().resume(); |
| 254 | + fragmentController = Robolectric.buildFragment(TestFragment.class) |
| 255 | + .create().start().resume(); |
| 256 | + supportFragmentController = SupportFragmentController.of(new TestSupportFragment()) |
| 257 | + .create().start().resume(); |
| 258 | + |
| 259 | + spyActivity = Mockito.spy(activityController.get()); |
| 260 | + spyFragment = Mockito.spy(fragmentController.get()); |
| 261 | + spySupportFragment = Mockito.spy(supportFragmentController.get()); |
| 262 | + } |
| 263 | + |
| 264 | + private void tearDownActivityAndFragment() { |
| 265 | + activityController.pause().stop().destroy(); |
| 266 | + fragmentController.pause().stop().destroy(); |
| 267 | + supportFragmentController.pause().stop().destroy(); |
| 268 | + } |
| 269 | +} |
0 commit comments