22
33import android .Manifest ;
44
5+ import org .junit .After ;
56import org .junit .Before ;
67import org .junit .Test ;
78import org .junit .runner .RunWith ;
1213import org .robolectric .Robolectric ;
1314import org .robolectric .RobolectricTestRunner ;
1415import org .robolectric .RuntimeEnvironment ;
16+ import org .robolectric .android .controller .ActivityController ;
17+ import org .robolectric .android .controller .FragmentController ;
1518import org .robolectric .annotation .Config ;
16- import org .robolectric .shadows .ShadowApplication ;
1719import org .robolectric .shadows .support .v4 .SupportFragmentController ;
1820
1921import java .util .ArrayList ;
3335@ Config (sdk = 19 )
3436public class EasyPermissionsLowApiTest {
3537
36- private static final int REQUEST_CODE = 5 ;
3738 private static final String RATIONALE = "RATIONALE" ;
3839 private static final String [] ALL_PERMS = new String []{
3940 Manifest .permission .READ_SMS , Manifest .permission .ACCESS_FINE_LOCATION };
4041
4142 private TestActivity spyActivity ;
4243 private TestFragment spyFragment ;
4344 private TestSupportFragment spySupportFragment ;
45+ private ActivityController <TestActivity > activityController ;
46+ private FragmentController <TestFragment > fragmentController ;
47+ private SupportFragmentController <TestSupportFragment > supportFragmentController ;
4448 @ Captor
4549 private ArgumentCaptor <Integer > integerCaptor ;
4650 @ Captor
@@ -52,52 +56,71 @@ public void setUp() {
5256 setUpActivityAndFragment ();
5357 }
5458
59+ @ After
60+ public void tearDown () {
61+ tearDownActivityAndFragment ();
62+ }
63+
64+ // ------ General tests ------
65+
5566 @ Test
5667 public void shouldHavePermission_whenHasPermissionsBeforeMarshmallow () {
5768 assertThat (EasyPermissions .hasPermissions (RuntimeEnvironment .application ,
5869 Manifest .permission .ACCESS_COARSE_LOCATION )).isTrue ();
5970 }
6071
72+ // ------ From Activity ------
73+
6174 @ Test
6275 public void shouldCallbackOnPermissionGranted_whenRequestFromActivity () {
63- EasyPermissions .requestPermissions (spyActivity , RATIONALE , REQUEST_CODE , ALL_PERMS );
76+ EasyPermissions .requestPermissions (spyActivity , RATIONALE , TestActivity . REQUEST_CODE , ALL_PERMS );
6477
6578 verify (spyActivity , times (1 ))
6679 .onPermissionsGranted (integerCaptor .capture (), listCaptor .capture ());
67- assertThat (integerCaptor .getValue ()).isEqualTo (REQUEST_CODE );
80+ assertThat (integerCaptor .getValue ()).isEqualTo (TestActivity . REQUEST_CODE );
6881 assertThat (listCaptor .getValue ()).containsAllIn (ALL_PERMS );
6982 }
7083
84+ // ------ From Fragment ------
85+
7186 @ Test
7287 public void shouldCallbackOnPermissionGranted_whenRequestFromFragment () {
73- EasyPermissions .requestPermissions (spyFragment , RATIONALE , REQUEST_CODE , ALL_PERMS );
88+ EasyPermissions .requestPermissions (spyFragment , RATIONALE , TestFragment . REQUEST_CODE , ALL_PERMS );
7489
7590 verify (spyFragment , times (1 ))
7691 .onPermissionsGranted (integerCaptor .capture (), listCaptor .capture ());
77- assertThat (integerCaptor .getValue ()).isEqualTo (REQUEST_CODE );
92+ assertThat (integerCaptor .getValue ()).isEqualTo (TestFragment . REQUEST_CODE );
7893 assertThat (listCaptor .getValue ()).containsAllIn (ALL_PERMS );
7994 }
8095
96+ // ------ From Support Fragment ------
97+
8198 @ Test
8299 public void shouldCallbackOnPermissionGranted_whenRequestFromSupportedFragment () {
83- EasyPermissions .requestPermissions (spySupportFragment , RATIONALE , REQUEST_CODE , ALL_PERMS );
100+ EasyPermissions .requestPermissions (spySupportFragment , RATIONALE , TestSupportFragment . REQUEST_CODE , ALL_PERMS );
84101
85102 verify (spySupportFragment , times (1 ))
86103 .onPermissionsGranted (integerCaptor .capture (), listCaptor .capture ());
87- assertThat (integerCaptor .getValue ()).isEqualTo (REQUEST_CODE );
104+ assertThat (integerCaptor .getValue ()).isEqualTo (TestSupportFragment . REQUEST_CODE );
88105 assertThat (listCaptor .getValue ()).containsAllIn (ALL_PERMS );
89106 }
90107
91108 private void setUpActivityAndFragment () {
92- TestActivity activity = Robolectric .buildActivity (TestActivity .class )
93- .create ().start ().resume ().get ();
94- TestFragment fragment = Robolectric .buildFragment (TestFragment .class )
95- .create ().start ().resume ().get ();
96- TestSupportFragment supportFragment = SupportFragmentController .of (new TestSupportFragment ())
97- .create ().start ().resume ().get ();
98-
99- spyActivity = Mockito .spy (activity );
100- spyFragment = Mockito .spy (fragment );
101- spySupportFragment = Mockito .spy (supportFragment );
109+ activityController = Robolectric .buildActivity (TestActivity .class )
110+ .create ().start ().resume ();
111+ fragmentController = Robolectric .buildFragment (TestFragment .class )
112+ .create ().start ().resume ();
113+ supportFragmentController = SupportFragmentController .of (new TestSupportFragment ())
114+ .create ().start ().resume ();
115+
116+ spyActivity = Mockito .spy (activityController .get ());
117+ spyFragment = Mockito .spy (fragmentController .get ());
118+ spySupportFragment = Mockito .spy (supportFragmentController .get ());
119+ }
120+
121+ private void tearDownActivityAndFragment () {
122+ activityController .pause ().stop ().destroy ();
123+ fragmentController .pause ().stop ().destroy ();
124+ supportFragmentController .pause ().stop ().destroy ();
102125 }
103126}
0 commit comments