2
2
3
3
import android .Manifest ;
4
4
5
+ import org .junit .After ;
5
6
import org .junit .Before ;
6
7
import org .junit .Test ;
7
8
import org .junit .runner .RunWith ;
12
13
import org .robolectric .Robolectric ;
13
14
import org .robolectric .RobolectricTestRunner ;
14
15
import org .robolectric .RuntimeEnvironment ;
16
+ import org .robolectric .android .controller .ActivityController ;
17
+ import org .robolectric .android .controller .FragmentController ;
15
18
import org .robolectric .annotation .Config ;
16
- import org .robolectric .shadows .ShadowApplication ;
17
19
import org .robolectric .shadows .support .v4 .SupportFragmentController ;
18
20
19
21
import java .util .ArrayList ;
33
35
@ Config (sdk = 19 )
34
36
public class EasyPermissionsLowApiTest {
35
37
36
- private static final int REQUEST_CODE = 5 ;
37
38
private static final String RATIONALE = "RATIONALE" ;
38
39
private static final String [] ALL_PERMS = new String []{
39
40
Manifest .permission .READ_SMS , Manifest .permission .ACCESS_FINE_LOCATION };
40
41
41
42
private TestActivity spyActivity ;
42
43
private TestFragment spyFragment ;
43
44
private TestSupportFragment spySupportFragment ;
45
+ private ActivityController <TestActivity > activityController ;
46
+ private FragmentController <TestFragment > fragmentController ;
47
+ private SupportFragmentController <TestSupportFragment > supportFragmentController ;
44
48
@ Captor
45
49
private ArgumentCaptor <Integer > integerCaptor ;
46
50
@ Captor
@@ -52,52 +56,71 @@ public void setUp() {
52
56
setUpActivityAndFragment ();
53
57
}
54
58
59
+ @ After
60
+ public void tearDown () {
61
+ tearDownActivityAndFragment ();
62
+ }
63
+
64
+ // ------ General tests ------
65
+
55
66
@ Test
56
67
public void shouldHavePermission_whenHasPermissionsBeforeMarshmallow () {
57
68
assertThat (EasyPermissions .hasPermissions (RuntimeEnvironment .application ,
58
69
Manifest .permission .ACCESS_COARSE_LOCATION )).isTrue ();
59
70
}
60
71
72
+ // ------ From Activity ------
73
+
61
74
@ Test
62
75
public void shouldCallbackOnPermissionGranted_whenRequestFromActivity () {
63
- EasyPermissions .requestPermissions (spyActivity , RATIONALE , REQUEST_CODE , ALL_PERMS );
76
+ EasyPermissions .requestPermissions (spyActivity , RATIONALE , TestActivity . REQUEST_CODE , ALL_PERMS );
64
77
65
78
verify (spyActivity , times (1 ))
66
79
.onPermissionsGranted (integerCaptor .capture (), listCaptor .capture ());
67
- assertThat (integerCaptor .getValue ()).isEqualTo (REQUEST_CODE );
80
+ assertThat (integerCaptor .getValue ()).isEqualTo (TestActivity . REQUEST_CODE );
68
81
assertThat (listCaptor .getValue ()).containsAllIn (ALL_PERMS );
69
82
}
70
83
84
+ // ------ From Fragment ------
85
+
71
86
@ Test
72
87
public void shouldCallbackOnPermissionGranted_whenRequestFromFragment () {
73
- EasyPermissions .requestPermissions (spyFragment , RATIONALE , REQUEST_CODE , ALL_PERMS );
88
+ EasyPermissions .requestPermissions (spyFragment , RATIONALE , TestFragment . REQUEST_CODE , ALL_PERMS );
74
89
75
90
verify (spyFragment , times (1 ))
76
91
.onPermissionsGranted (integerCaptor .capture (), listCaptor .capture ());
77
- assertThat (integerCaptor .getValue ()).isEqualTo (REQUEST_CODE );
92
+ assertThat (integerCaptor .getValue ()).isEqualTo (TestFragment . REQUEST_CODE );
78
93
assertThat (listCaptor .getValue ()).containsAllIn (ALL_PERMS );
79
94
}
80
95
96
+ // ------ From Support Fragment ------
97
+
81
98
@ Test
82
99
public void shouldCallbackOnPermissionGranted_whenRequestFromSupportedFragment () {
83
- EasyPermissions .requestPermissions (spySupportFragment , RATIONALE , REQUEST_CODE , ALL_PERMS );
100
+ EasyPermissions .requestPermissions (spySupportFragment , RATIONALE , TestSupportFragment . REQUEST_CODE , ALL_PERMS );
84
101
85
102
verify (spySupportFragment , times (1 ))
86
103
.onPermissionsGranted (integerCaptor .capture (), listCaptor .capture ());
87
- assertThat (integerCaptor .getValue ()).isEqualTo (REQUEST_CODE );
104
+ assertThat (integerCaptor .getValue ()).isEqualTo (TestSupportFragment . REQUEST_CODE );
88
105
assertThat (listCaptor .getValue ()).containsAllIn (ALL_PERMS );
89
106
}
90
107
91
108
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 ();
102
125
}
103
126
}
0 commit comments