23
23
import static org .mockito .Mockito .mock ;
24
24
import static org .mockito .Mockito .never ;
25
25
import static org .mockito .Mockito .spy ;
26
+ import static org .mockito .Mockito .times ;
26
27
import static org .mockito .Mockito .verify ;
27
28
import static org .mockito .Mockito .when ;
28
29
29
30
import android .content .Context ;
30
31
import android .os .Bundle ;
32
+ import android .provider .Settings ;
31
33
import android .view .View ;
32
34
import android .widget .FrameLayout ;
33
35
41
43
import com .android .settings .testutils .shadow .ShadowFragment ;
42
44
import com .android .settings .widget .WorkOnlyCategory ;
43
45
46
+ import org .junit .After ;
44
47
import org .junit .Before ;
45
48
import org .junit .Test ;
46
49
import org .junit .runner .RunWith ;
@@ -64,21 +67,34 @@ public class SettingsPreferenceFragmentTest {
64
67
private PreferenceScreen mPreferenceScreen ;
65
68
private Context mContext ;
66
69
private TestFragment mFragment ;
70
+ private TestFragment2 mFragment2 ;
67
71
private View mEmptyView ;
72
+ private int mInitDeviceProvisionedValue ;
68
73
69
74
@ Before
70
75
public void setUp () {
71
76
MockitoAnnotations .initMocks (this );
72
77
FakeFeatureFactory .setupForTest ();
73
78
mContext = RuntimeEnvironment .application ;
74
79
mFragment = spy (new TestFragment ());
80
+ mFragment2 = spy (new TestFragment2 ());
75
81
doReturn (mActivity ).when (mFragment ).getActivity ();
76
82
when (mFragment .getContext ()).thenReturn (mContext );
83
+ when (mFragment2 .getContext ()).thenReturn (mContext );
77
84
78
85
mEmptyView = new View (mContext );
79
86
ReflectionHelpers .setField (mFragment , "mEmptyView" , mEmptyView );
80
87
81
88
doReturn (ITEM_COUNT ).when (mPreferenceScreen ).getPreferenceCount ();
89
+
90
+ mInitDeviceProvisionedValue = Settings .Global .getInt (mContext .getContentResolver (),
91
+ Settings .Global .DEVICE_PROVISIONED , 0 );
92
+ }
93
+
94
+ @ After
95
+ public void tearDown () {
96
+ Settings .Global .putInt (mContext .getContentResolver (),
97
+ Settings .Global .DEVICE_PROVISIONED , mInitDeviceProvisionedValue );
82
98
}
83
99
84
100
@ Test
@@ -210,8 +226,66 @@ public void hidePinnedHeader_shouldBeInvisible() {
210
226
assertThat (mFragment .mPinnedHeaderFrameLayout .getVisibility ()).isEqualTo (View .INVISIBLE );
211
227
}
212
228
229
+ @ Test
230
+ public void onAttach_shouldNotSkipForSUWAndDeviceIsProvisioned_notCallFinish () {
231
+ Settings .Global .putInt (mContext .getContentResolver (),
232
+ Settings .Global .DEVICE_PROVISIONED , 1 );
233
+
234
+ mFragment .onAttach (mContext );
235
+
236
+ verify (mFragment , never ()).finish ();
237
+ }
238
+
239
+ @ Test
240
+ public void onAttach_shouldNotSkipForSUWAndDeviceIsNotProvisioned_notCallFinish () {
241
+ Settings .Global .putInt (mContext .getContentResolver (),
242
+ Settings .Global .DEVICE_PROVISIONED , 0 );
243
+
244
+ mFragment .onAttach (mContext );
245
+
246
+ verify (mFragment , never ()).finish ();
247
+ }
248
+
249
+ @ Test
250
+ public void onAttach_shouldSkipForSUWAndDeviceIsDeviceProvisioned_notCallFinish () {
251
+ Settings .Global .putInt (mContext .getContentResolver (),
252
+ Settings .Global .DEVICE_PROVISIONED , 1 );
253
+
254
+ mFragment2 .onAttach (mContext );
255
+
256
+ verify (mFragment2 , never ()).finish ();
257
+ }
258
+
259
+ @ Test
260
+ public void onAttach_shouldSkipForSUWAndDeviceProvisioned_notCallFinish () {
261
+ Settings .Global .putInt (mContext .getContentResolver (),
262
+ Settings .Global .DEVICE_PROVISIONED , 0 );
263
+
264
+ mFragment2 .onAttach (mContext );
265
+
266
+ verify (mFragment2 , times (1 )).finish ();
267
+ }
268
+
213
269
public static class TestFragment extends SettingsPreferenceFragment {
214
270
271
+ @ Override
272
+ protected boolean shouldSkipForInitialSUW () {
273
+ return false ;
274
+ }
275
+
276
+ @ Override
277
+ public int getMetricsCategory () {
278
+ return 0 ;
279
+ }
280
+ }
281
+
282
+ public static class TestFragment2 extends SettingsPreferenceFragment {
283
+
284
+ @ Override
285
+ protected boolean shouldSkipForInitialSUW () {
286
+ return true ;
287
+ }
288
+
215
289
@ Override
216
290
public int getMetricsCategory () {
217
291
return 0 ;
0 commit comments