|
| 1 | +package pub.devrel.easypermissions; |
| 2 | + |
| 3 | +import android.content.Intent; |
| 4 | + |
| 5 | +import org.junit.After; |
| 6 | +import org.junit.Before; |
| 7 | +import org.junit.Test; |
| 8 | +import org.junit.runner.RunWith; |
| 9 | +import org.mockito.ArgumentCaptor; |
| 10 | +import org.mockito.Captor; |
| 11 | +import org.mockito.Mockito; |
| 12 | +import org.mockito.MockitoAnnotations; |
| 13 | +import org.robolectric.Robolectric; |
| 14 | +import org.robolectric.RobolectricTestRunner; |
| 15 | +import org.robolectric.RuntimeEnvironment; |
| 16 | +import org.robolectric.android.controller.ActivityController; |
| 17 | +import org.robolectric.android.controller.FragmentController; |
| 18 | +import org.robolectric.annotation.Config; |
| 19 | +import org.robolectric.shadows.ShadowApplication; |
| 20 | +import org.robolectric.shadows.ShadowIntent; |
| 21 | +import org.robolectric.shadows.support.v4.SupportFragmentController; |
| 22 | + |
| 23 | +import java.util.Objects; |
| 24 | + |
| 25 | +import pub.devrel.easypermissions.testhelper.TestActivity; |
| 26 | +import pub.devrel.easypermissions.testhelper.TestFragment; |
| 27 | +import pub.devrel.easypermissions.testhelper.TestSupportFragment; |
| 28 | + |
| 29 | +import static com.google.common.truth.Truth.assertThat; |
| 30 | +import static org.mockito.Mockito.times; |
| 31 | +import static org.mockito.Mockito.verify; |
| 32 | +import static org.robolectric.Shadows.shadowOf; |
| 33 | +import static pub.devrel.easypermissions.AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE; |
| 34 | + |
| 35 | +@RunWith(RobolectricTestRunner.class) |
| 36 | +@Config(sdk = 23) |
| 37 | +public class AppSettingsDialogTest { |
| 38 | + |
| 39 | + private static final String TITLE = "TITLE"; |
| 40 | + private static final String RATIONALE = "RATIONALE"; |
| 41 | + private static final String NEGATIVE = "NEGATIVE"; |
| 42 | + private static final String POSITIVE = "POSITIVE"; |
| 43 | + private ShadowApplication shadowApp; |
| 44 | + private TestActivity spyActivity; |
| 45 | + private TestFragment spyFragment; |
| 46 | + private TestSupportFragment spySupportFragment; |
| 47 | + private ActivityController<TestActivity> activityController; |
| 48 | + private FragmentController<TestFragment> fragmentController; |
| 49 | + private SupportFragmentController<TestSupportFragment> supportFragmentController; |
| 50 | + @Captor |
| 51 | + private ArgumentCaptor<Integer> integerCaptor; |
| 52 | + @Captor |
| 53 | + private ArgumentCaptor<Intent> intentCaptor; |
| 54 | + |
| 55 | + @Before |
| 56 | + public void setUp() { |
| 57 | + MockitoAnnotations.initMocks(this); |
| 58 | + shadowApp = shadowOf(RuntimeEnvironment.application); |
| 59 | + setUpActivityAndFragment(); |
| 60 | + } |
| 61 | + |
| 62 | + @After |
| 63 | + public void tearDown() { |
| 64 | + tearDownActivityAndFragment(); |
| 65 | + } |
| 66 | + |
| 67 | + // ------ From Activity ------ |
| 68 | + |
| 69 | + @Test |
| 70 | + public void shouldStartExpectedSettingsDialog_whenBuildingFromActivity() { |
| 71 | + new AppSettingsDialog.Builder(spyActivity) |
| 72 | + .setTitle(TITLE) |
| 73 | + .setRationale(RATIONALE) |
| 74 | + .setPositiveButton(POSITIVE) |
| 75 | + .setNegativeButton(NEGATIVE) |
| 76 | + .setThemeResId(R.style.Theme_AppCompat) |
| 77 | + .build() |
| 78 | + .show(); |
| 79 | + |
| 80 | + verify(spyActivity, times(1)) |
| 81 | + .startActivityForResult(intentCaptor.capture(), integerCaptor.capture()); |
| 82 | + assertThat(integerCaptor.getValue()).isEqualTo(DEFAULT_SETTINGS_REQ_CODE); |
| 83 | + assertThat(Objects.requireNonNull(intentCaptor.getValue().getComponent()).getClassName()) |
| 84 | + .isEqualTo(AppSettingsDialogHolderActivity.class.getName()); |
| 85 | + |
| 86 | + Intent startedIntent = shadowApp.getNextStartedActivity(); |
| 87 | + ShadowIntent shadowIntent = shadowOf(startedIntent); |
| 88 | + assertThat(shadowIntent.getIntentClass()).isEqualTo(AppSettingsDialogHolderActivity.class); |
| 89 | + } |
| 90 | + |
| 91 | + // ------ From Fragment ------ |
| 92 | + |
| 93 | + @Test |
| 94 | + public void shouldStartExpectedSettingsDialog_whenBuildingFromFragment() { |
| 95 | + new AppSettingsDialog.Builder(spyFragment) |
| 96 | + .setTitle(TITLE) |
| 97 | + .setRationale(RATIONALE) |
| 98 | + .setPositiveButton(POSITIVE) |
| 99 | + .setNegativeButton(NEGATIVE) |
| 100 | + .setThemeResId(R.style.Theme_AppCompat) |
| 101 | + .build() |
| 102 | + .show(); |
| 103 | + |
| 104 | + verify(spyFragment, times(1)) |
| 105 | + .startActivityForResult(intentCaptor.capture(), integerCaptor.capture()); |
| 106 | + assertThat(integerCaptor.getValue()).isEqualTo(DEFAULT_SETTINGS_REQ_CODE); |
| 107 | + assertThat(Objects.requireNonNull(intentCaptor.getValue().getComponent()).getClassName()) |
| 108 | + .isEqualTo(AppSettingsDialogHolderActivity.class.getName()); |
| 109 | + |
| 110 | + Intent startedIntent = shadowApp.getNextStartedActivity(); |
| 111 | + ShadowIntent shadowIntent = shadowOf(startedIntent); |
| 112 | + assertThat(shadowIntent.getIntentClass()).isEqualTo(AppSettingsDialogHolderActivity.class); |
| 113 | + } |
| 114 | + |
| 115 | + // ------ From Support Fragment ------ |
| 116 | + |
| 117 | + @Test |
| 118 | + public void shouldStartExpectedSettingsDialog_whenBuildingFromSupportFragment() { |
| 119 | + new AppSettingsDialog.Builder(spySupportFragment) |
| 120 | + .setTitle(android.R.string.dialog_alert_title) |
| 121 | + .setRationale(android.R.string.unknownName) |
| 122 | + .setPositiveButton(android.R.string.ok) |
| 123 | + .setNegativeButton(android.R.string.cancel) |
| 124 | + .setThemeResId(R.style.Theme_AppCompat) |
| 125 | + .build() |
| 126 | + .show(); |
| 127 | + |
| 128 | + verify(spySupportFragment, times(1)) |
| 129 | + .startActivityForResult(intentCaptor.capture(), integerCaptor.capture()); |
| 130 | + assertThat(integerCaptor.getValue()).isEqualTo(DEFAULT_SETTINGS_REQ_CODE); |
| 131 | + assertThat(Objects.requireNonNull(intentCaptor.getValue().getComponent()).getClassName()) |
| 132 | + .isEqualTo(AppSettingsDialogHolderActivity.class.getName()); |
| 133 | + |
| 134 | + Intent startedIntent = shadowApp.getNextStartedActivity(); |
| 135 | + ShadowIntent shadowIntent = shadowOf(startedIntent); |
| 136 | + assertThat(shadowIntent.getIntentClass()).isEqualTo(AppSettingsDialogHolderActivity.class); |
| 137 | + } |
| 138 | + |
| 139 | + private void setUpActivityAndFragment() { |
| 140 | + activityController = Robolectric.buildActivity(TestActivity.class) |
| 141 | + .create().start().resume(); |
| 142 | + fragmentController = Robolectric.buildFragment(TestFragment.class) |
| 143 | + .create().start().resume(); |
| 144 | + supportFragmentController = SupportFragmentController.of(new TestSupportFragment()) |
| 145 | + .create().start().resume(); |
| 146 | + |
| 147 | + spyActivity = Mockito.spy(activityController.get()); |
| 148 | + spyFragment = Mockito.spy(fragmentController.get()); |
| 149 | + spySupportFragment = Mockito.spy(supportFragmentController.get()); |
| 150 | + } |
| 151 | + |
| 152 | + private void tearDownActivityAndFragment() { |
| 153 | + activityController.pause().stop().destroy(); |
| 154 | + fragmentController.pause().stop().destroy(); |
| 155 | + supportFragmentController.pause().stop().destroy(); |
| 156 | + } |
| 157 | +} |
0 commit comments