1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.users;
18 
19 import static android.os.UserManager.SWITCHABILITY_STATUS_OK;
20 import static android.os.UserManager.SWITCHABILITY_STATUS_USER_IN_CALL;
21 import static android.os.UserManager.SWITCHABILITY_STATUS_USER_SWITCH_DISALLOWED;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.anyInt;
27 import static org.mockito.ArgumentMatchers.anyString;
28 import static org.mockito.Mockito.doNothing;
29 import static org.mockito.Mockito.doReturn;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.never;
32 import static org.mockito.Mockito.spy;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.verifyNoMoreInteractions;
35 import static org.robolectric.Shadows.shadowOf;
36 
37 import android.app.admin.DevicePolicyManager;
38 import android.content.ComponentName;
39 import android.content.Context;
40 import android.content.Intent;
41 import android.content.pm.UserInfo;
42 import android.os.Bundle;
43 import android.os.UserHandle;
44 import android.os.UserManager;
45 import android.telephony.TelephonyManager;
46 
47 import androidx.fragment.app.FragmentActivity;
48 import androidx.preference.Preference;
49 import androidx.preference.PreferenceScreen;
50 import androidx.preference.SwitchPreference;
51 
52 import com.android.settings.R;
53 import com.android.settings.SettingsActivity;
54 import com.android.settings.SubSettings;
55 import com.android.settings.testutils.shadow.ShadowDevicePolicyManager;
56 import com.android.settings.testutils.shadow.ShadowUserManager;
57 import com.android.settingslib.RestrictedLockUtils;
58 import com.android.settingslib.RestrictedPreference;
59 
60 import org.junit.After;
61 import org.junit.Before;
62 import org.junit.Test;
63 import org.junit.runner.RunWith;
64 import org.mockito.Mock;
65 import org.mockito.MockitoAnnotations;
66 import org.robolectric.RobolectricTestRunner;
67 import org.robolectric.RuntimeEnvironment;
68 import org.robolectric.android.controller.ActivityController;
69 import org.robolectric.annotation.Config;
70 import org.robolectric.shadow.api.Shadow;
71 import org.robolectric.shadows.ShadowIntent;
72 import org.robolectric.util.ReflectionHelpers;
73 
74 import java.util.ArrayList;
75 import java.util.List;
76 
77 @RunWith(RobolectricTestRunner.class)
78 @Config(shadows = {
79         ShadowUserManager.class,
80         ShadowDevicePolicyManager.class
81 })
82 public class UserDetailsSettingsTest {
83 
84     private static final String KEY_SWITCH_USER = "switch_user";
85     private static final String KEY_ENABLE_TELEPHONY = "enable_calling";
86     private static final String KEY_REMOVE_USER = "remove_user";
87     private static final String KEY_APP_AND_CONTENT_ACCESS = "app_and_content_access";
88 
89     private static final int DIALOG_CONFIRM_REMOVE = 1;
90 
91     @Mock
92     private TelephonyManager mTelephonyManager;
93 
94     private ShadowUserManager mUserManager;
95 
96     @Mock
97     private RestrictedPreference mSwitchUserPref;
98     @Mock
99     private SwitchPreference mPhonePref;
100     @Mock
101     private Preference mRemoveUserPref;
102     @Mock
103     private Preference mAppAndContentAccessPref;
104 
105     private FragmentActivity mActivity;
106     private Context mContext;
107     private UserCapabilities mUserCapabilities;
108     private UserDetailsSettings mFragment;
109     private Bundle mArguments;
110     private UserInfo mUserInfo;
111 
112     @Before
setUp()113     public void setUp() {
114         MockitoAnnotations.initMocks(this);
115 
116         mActivity = spy(ActivityController.of(new FragmentActivity()).get());
117         mContext = spy(RuntimeEnvironment.application);
118         mUserCapabilities = UserCapabilities.create(mContext);
119         mUserCapabilities.mUserSwitcherEnabled = true;
120         mFragment = spy(new UserDetailsSettings());
121         mArguments = new Bundle();
122 
123         UserManager userManager = (UserManager) mContext.getSystemService(
124                 Context.USER_SERVICE);
125         mUserManager = Shadow.extract(userManager);
126 
127         doReturn(mTelephonyManager).when(mActivity).getSystemService(Context.TELEPHONY_SERVICE);
128 
129         ReflectionHelpers.setField(mFragment, "mUserManager", userManager);
130         ReflectionHelpers.setField(mFragment, "mUserCaps", mUserCapabilities);
131         doReturn(mActivity).when(mFragment).getActivity();
132         doReturn(mActivity).when(mFragment).getContext();
133 
134         doReturn(mock(PreferenceScreen.class)).when(mFragment).getPreferenceScreen();
135 
136         doReturn(mSwitchUserPref).when(mFragment).findPreference(KEY_SWITCH_USER);
137         doReturn(mPhonePref).when(mFragment).findPreference(KEY_ENABLE_TELEPHONY);
138         doReturn(mRemoveUserPref).when(mFragment).findPreference(KEY_REMOVE_USER);
139         doReturn(mAppAndContentAccessPref)
140                 .when(mFragment).findPreference(KEY_APP_AND_CONTENT_ACCESS);
141     }
142 
143     @After
tearDown()144     public void tearDown() {
145         ShadowUserManager.reset();
146     }
147 
148     @Test(expected = IllegalStateException.class)
initialize_nullArguments_shouldThrowException()149     public void initialize_nullArguments_shouldThrowException() {
150         mFragment.initialize(mActivity, null);
151     }
152 
153     @Test(expected = IllegalStateException.class)
initialize_emptyArguments_shouldThrowException()154     public void initialize_emptyArguments_shouldThrowException() {
155         mFragment.initialize(mActivity, new Bundle());
156     }
157 
158     @Test
initialize_userSelected_shouldSetupSwitchPref()159     public void initialize_userSelected_shouldSetupSwitchPref() {
160         setupSelectedUser();
161         doReturn("Switch to " + mUserInfo.name)
162                 .when(mActivity).getString(anyInt(), anyString());
163 
164         mFragment.initialize(mActivity, mArguments);
165 
166         verify(mActivity).getString(com.android.settingslib.R.string.user_switch_to_user,
167                 mUserInfo.name);
168         verify(mSwitchUserPref).setTitle("Switch to " + mUserInfo.name);
169         verify(mSwitchUserPref).setOnPreferenceClickListener(mFragment);
170         verify(mFragment, never()).removePreference(KEY_SWITCH_USER);
171     }
172 
173     @Test
initialize_guestSelected_shouldSetupSwitchPref()174     public void initialize_guestSelected_shouldSetupSwitchPref() {
175         setupSelectedGuest();
176         doReturn("Switch to " + mUserInfo.name)
177                 .when(mActivity).getString(anyInt(), anyString());
178 
179         mFragment.initialize(mActivity, mArguments);
180 
181         verify(mActivity).getString(com.android.settingslib.R.string.user_switch_to_user,
182                 mUserInfo.name);
183         verify(mSwitchUserPref).setTitle("Switch to " + mUserInfo.name);
184         verify(mSwitchUserPref).setOnPreferenceClickListener(mFragment);
185         verify(mFragment, never()).removePreference(KEY_SWITCH_USER);
186     }
187 
188     @Test
initialize_userSelected_shouldNotShowAppAndContentPref()189     public void initialize_userSelected_shouldNotShowAppAndContentPref() {
190         setupSelectedUser();
191 
192         mFragment.initialize(mActivity, mArguments);
193 
194         verify(mFragment).removePreference(KEY_APP_AND_CONTENT_ACCESS);
195     }
196 
197     @Test
initialize_guestSelected_shouldNotShowAppAndContentPref()198     public void initialize_guestSelected_shouldNotShowAppAndContentPref() {
199         setupSelectedGuest();
200 
201         mFragment.initialize(mActivity, mArguments);
202 
203         verify(mFragment).removePreference(KEY_APP_AND_CONTENT_ACCESS);
204     }
205 
206     @Test
onResume_canSwitch_shouldEnableSwitchPref()207     public void onResume_canSwitch_shouldEnableSwitchPref() {
208         mUserManager.setSwitchabilityStatus(SWITCHABILITY_STATUS_OK);
209         mFragment.mSwitchUserPref = mSwitchUserPref;
210         mFragment.onAttach(mContext);
211 
212         mFragment.onResume();
213 
214         verify(mSwitchUserPref).setEnabled(true);
215     }
216 
217     @Test
onResume_userInCall_shouldDisableSwitchPref()218     public void onResume_userInCall_shouldDisableSwitchPref() {
219         mUserManager.setSwitchabilityStatus(SWITCHABILITY_STATUS_USER_IN_CALL);
220         mFragment.mSwitchUserPref = mSwitchUserPref;
221         mFragment.onAttach(mContext);
222 
223         mFragment.onResume();
224 
225         verify(mSwitchUserPref).setEnabled(false);
226     }
227 
228     @Test
onResume_switchDisallowed_shouldDisableSwitchPref()229     public void onResume_switchDisallowed_shouldDisableSwitchPref() {
230         mUserManager.setSwitchabilityStatus(SWITCHABILITY_STATUS_USER_SWITCH_DISALLOWED);
231         mFragment.mSwitchUserPref = mSwitchUserPref;
232         mFragment.onAttach(mContext);
233 
234         mFragment.onResume();
235 
236         verify(mSwitchUserPref).setEnabled(false);
237     }
238 
239     @Test
onResume_systemUserLocked_shouldDisableSwitchPref()240     public void onResume_systemUserLocked_shouldDisableSwitchPref() {
241         mUserManager.setSwitchabilityStatus(UserManager.SWITCHABILITY_STATUS_SYSTEM_USER_LOCKED);
242         mFragment.mSwitchUserPref = mSwitchUserPref;
243         mFragment.onAttach(mContext);
244 
245         mFragment.onResume();
246 
247         verify(mSwitchUserPref).setEnabled(false);
248     }
249 
250     @Test
initialize_adminWithTelephony_shouldShowPhonePreference()251     public void initialize_adminWithTelephony_shouldShowPhonePreference() {
252         setupSelectedUser();
253         doReturn(true).when(mTelephonyManager).isVoiceCapable();
254         mUserManager.setIsAdminUser(true);
255 
256         mFragment.initialize(mActivity, mArguments);
257 
258         verify(mFragment, never()).removePreference(KEY_ENABLE_TELEPHONY);
259         verify(mPhonePref).setOnPreferenceChangeListener(mFragment);
260     }
261 
262     @Test
initialize_adminNoTelephony_shouldNotShowPhonePreference()263     public void initialize_adminNoTelephony_shouldNotShowPhonePreference() {
264         setupSelectedUser();
265         doReturn(false).when(mTelephonyManager).isVoiceCapable();
266         mUserManager.setIsAdminUser(true);
267         doReturn(null).when(mActivity).getSystemService(Context.TELEPHONY_SERVICE);
268 
269         mFragment.initialize(mActivity, mArguments);
270 
271         verify(mFragment).removePreference(KEY_ENABLE_TELEPHONY);
272     }
273 
274     @Test
initialize_nonAdminWithTelephony_shouldNotShowPhonePreference()275     public void initialize_nonAdminWithTelephony_shouldNotShowPhonePreference() {
276         setupSelectedUser();
277         doReturn(true).when(mTelephonyManager).isVoiceCapable();
278         mUserManager.setIsAdminUser(false);
279 
280         mFragment.initialize(mActivity, mArguments);
281 
282         verify(mFragment).removePreference(KEY_ENABLE_TELEPHONY);
283     }
284 
285     @Test
initialize_nonAdmin_shouldNotShowAppAndContentPref()286     public void initialize_nonAdmin_shouldNotShowAppAndContentPref() {
287         setupSelectedUser();
288         mUserManager.setIsAdminUser(false);
289 
290         mFragment.initialize(mActivity, mArguments);
291 
292         verify(mFragment).removePreference(KEY_APP_AND_CONTENT_ACCESS);
293     }
294 
295     @Test
initialize_adminSelectsSecondaryUser_shouldShowRemovePreference()296     public void initialize_adminSelectsSecondaryUser_shouldShowRemovePreference() {
297         setupSelectedUser();
298         mUserManager.setIsAdminUser(true);
299 
300         mFragment.initialize(mActivity, mArguments);
301 
302         verify(mRemoveUserPref).setOnPreferenceClickListener(mFragment);
303         verify(mRemoveUserPref).setTitle(R.string.user_remove_user);
304         verify(mFragment, never()).removePreference(KEY_REMOVE_USER);
305     }
306 
307     @Test
initialize_adminSelectsNewRestrictedUser_shouldOpenAppContentScreen()308     public void initialize_adminSelectsNewRestrictedUser_shouldOpenAppContentScreen() {
309         setupSelectedRestrictedUser();
310         mUserManager.setIsAdminUser(true);
311         mArguments.putBoolean(AppRestrictionsFragment.EXTRA_NEW_USER, true);
312 
313         mFragment.initialize(mActivity, mArguments);
314 
315         Intent startedIntent = shadowOf(mActivity).getNextStartedActivity();
316         ShadowIntent shadowIntent = shadowOf(startedIntent);
317         assertThat(shadowIntent.getIntentClass()).isEqualTo(SubSettings.class);
318         assertThat(startedIntent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT))
319                 .isEqualTo(AppRestrictionsFragment.class.getName());
320         Bundle arguments = startedIntent.getBundleExtra(
321                 SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS);
322         assertThat(arguments).isNotNull();
323         assertThat(arguments.getInt(AppRestrictionsFragment.EXTRA_USER_ID, 0))
324                 .isEqualTo(mUserInfo.id);
325         assertThat(arguments.getBoolean(AppRestrictionsFragment.EXTRA_NEW_USER, false))
326                 .isEqualTo(true);
327     }
328 
329     @Test
initialize_adminSelectsRestrictedUser_shouldSetupPreferences()330     public void initialize_adminSelectsRestrictedUser_shouldSetupPreferences() {
331         setupSelectedRestrictedUser();
332         mUserManager.setIsAdminUser(true);
333         doReturn(true).when(mTelephonyManager).isVoiceCapable();
334 
335         mFragment.initialize(mActivity, mArguments);
336 
337         verify(mFragment, never()).removePreference(KEY_REMOVE_USER);
338         verify(mFragment, never()).removePreference(KEY_SWITCH_USER);
339         verify(mFragment, never()).removePreference(KEY_APP_AND_CONTENT_ACCESS);
340         verify(mFragment).removePreference(KEY_ENABLE_TELEPHONY);
341         verify(mSwitchUserPref).setTitle("Switch to " + mUserInfo.name);
342         verify(mAppAndContentAccessPref).setOnPreferenceClickListener(mFragment);
343         verify(mSwitchUserPref).setOnPreferenceClickListener(mFragment);
344         verify(mRemoveUserPref).setOnPreferenceClickListener(mFragment);
345     }
346 
347     @Test
initialize_adminSelectsExistingRestrictedUser_shouldNotStartAppAndContentAccess()348     public void initialize_adminSelectsExistingRestrictedUser_shouldNotStartAppAndContentAccess() {
349         setupSelectedRestrictedUser();
350         mUserManager.setIsAdminUser(true);
351         mArguments.putBoolean(AppRestrictionsFragment.EXTRA_NEW_USER, false);
352 
353         mFragment.initialize(mActivity, mArguments);
354 
355         verify(mActivity, never()).startActivity(any(Intent.class));
356     }
357 
358     @Test
initialize_adminSelectsGuest_shouldShowRemovePreference()359     public void initialize_adminSelectsGuest_shouldShowRemovePreference() {
360         setupSelectedGuest();
361         mUserManager.setIsAdminUser(true);
362 
363         mFragment.initialize(mActivity, mArguments);
364 
365         verify(mRemoveUserPref).setOnPreferenceClickListener(mFragment);
366         verify(mRemoveUserPref).setTitle(R.string.user_exit_guest_title);
367         verify(mFragment, never()).removePreference(KEY_REMOVE_USER);
368     }
369 
370     @Test
initialize_nonAdmin_shouldNotShowRemovePreference()371     public void initialize_nonAdmin_shouldNotShowRemovePreference() {
372         setupSelectedUser();
373         mUserManager.setIsAdminUser(false);
374 
375         mFragment.initialize(mActivity, mArguments);
376 
377         verify(mFragment).removePreference(KEY_REMOVE_USER);
378     }
379 
380     @Test
initialize_disallowRemoveUserRestriction_shouldNotShowRemovePreference()381     public void initialize_disallowRemoveUserRestriction_shouldNotShowRemovePreference() {
382         setupSelectedUser();
383         mUserManager.setIsAdminUser(true);
384         mUserManager.addBaseUserRestriction(UserManager.DISALLOW_REMOVE_USER);
385 
386         mFragment.initialize(mActivity, mArguments);
387 
388         verify(mFragment).removePreference(KEY_REMOVE_USER);
389     }
390 
391     @Test
initialize_userHasCallRestriction_shouldSetPhoneSwitchUnChecked()392     public void initialize_userHasCallRestriction_shouldSetPhoneSwitchUnChecked() {
393         setupSelectedUser();
394         mUserManager.setIsAdminUser(true);
395         mUserManager.setUserRestriction(mUserInfo.getUserHandle(),
396                 UserManager.DISALLOW_OUTGOING_CALLS, true);
397 
398         mFragment.initialize(mActivity, mArguments);
399 
400         verify(mPhonePref).setChecked(false);
401     }
402 
403     @Test
initialize_noCallRestriction_shouldSetPhoneSwitchChecked()404     public void initialize_noCallRestriction_shouldSetPhoneSwitchChecked() {
405         setupSelectedUser();
406         mUserManager.setIsAdminUser(true);
407 
408         mFragment.initialize(mActivity, mArguments);
409 
410         verify(mPhonePref).setChecked(true);
411     }
412 
413     @Test
initialize_guestSelected_noCallRestriction_shouldSetPhonePreference()414     public void initialize_guestSelected_noCallRestriction_shouldSetPhonePreference() {
415         setupSelectedGuest();
416         mUserManager.setIsAdminUser(true);
417 
418         mFragment.initialize(mActivity, mArguments);
419 
420         verify(mPhonePref).setTitle(R.string.user_enable_calling);
421         verify(mPhonePref).setChecked(true);
422     }
423 
424     @Test
initialize_guestSelected_callRestriction_shouldSetPhonePreference()425     public void initialize_guestSelected_callRestriction_shouldSetPhonePreference() {
426         setupSelectedGuest();
427         mUserManager.setIsAdminUser(true);
428         mUserManager.addGuestUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS);
429 
430         mFragment.initialize(mActivity, mArguments);
431 
432         verify(mPhonePref).setTitle(R.string.user_enable_calling);
433         verify(mPhonePref).setChecked(false);
434     }
435 
436     @Test
initialize_switchUserDisallowed_shouldSetAdminDisabledOnSwitchPreference()437     public void initialize_switchUserDisallowed_shouldSetAdminDisabledOnSwitchPreference() {
438         setupSelectedUser();
439         mUserCapabilities.mDisallowSwitchUser = true;
440         DevicePolicyManager devicePolicyManager = mock(DevicePolicyManager.class);
441         doReturn(devicePolicyManager).when(mActivity)
442                 .getSystemService(Context.DEVICE_POLICY_SERVICE);
443         doReturn(mock(ComponentName.class)).when(devicePolicyManager)
444                 .getDeviceOwnerComponentOnAnyUser();
445 
446         mFragment.initialize(mActivity, mArguments);
447 
448         verify(mSwitchUserPref).setDisabledByAdmin(any(RestrictedLockUtils.EnforcedAdmin.class));
449     }
450 
451     @Test
initialize_switchUserAllowed_shouldSetSwitchPreferenceEnabled()452     public void initialize_switchUserAllowed_shouldSetSwitchPreferenceEnabled() {
453         setupSelectedUser();
454         mUserCapabilities.mDisallowSwitchUser = false;
455 
456         mFragment.initialize(mActivity, mArguments);
457 
458         verify(mSwitchUserPref).setDisabledByAdmin(null);
459         verify(mSwitchUserPref).setSelectable(true);
460         verify(mSwitchUserPref).setOnPreferenceClickListener(mFragment);
461     }
462 
463     @Test
onPreferenceClick_switchClicked_canSwitch_shouldSwitch()464     public void onPreferenceClick_switchClicked_canSwitch_shouldSwitch() {
465         setupSelectedUser();
466         mUserManager.setSwitchabilityStatus(SWITCHABILITY_STATUS_OK);
467         mFragment.mSwitchUserPref = mSwitchUserPref;
468         mFragment.mRemoveUserPref = mRemoveUserPref;
469         mFragment.mAppAndContentAccessPref = mAppAndContentAccessPref;
470         mFragment.mUserInfo = mUserInfo;
471 
472         mFragment.onPreferenceClick(mSwitchUserPref);
473 
474         verify(mFragment).switchUser();
475     }
476 
477     @Test
onPreferenceClick_switchClicked_canNotSwitch_doNothing()478     public void onPreferenceClick_switchClicked_canNotSwitch_doNothing() {
479         setupSelectedUser();
480         mUserManager.setSwitchabilityStatus(SWITCHABILITY_STATUS_USER_SWITCH_DISALLOWED);
481         mFragment.mSwitchUserPref = mSwitchUserPref;
482         mFragment.mRemoveUserPref = mRemoveUserPref;
483         mFragment.mAppAndContentAccessPref = mAppAndContentAccessPref;
484         mFragment.mUserInfo = mUserInfo;
485 
486         mFragment.onPreferenceClick(mSwitchUserPref);
487 
488         verify(mFragment, never()).switchUser();
489     }
490 
491     @Test
onPreferenceClick_removeClicked_canDelete_shouldShowDialog()492     public void onPreferenceClick_removeClicked_canDelete_shouldShowDialog() {
493         setupSelectedUser();
494         mFragment.mUserInfo = mUserInfo;
495         mUserManager.setIsAdminUser(true);
496         mFragment.mSwitchUserPref = mSwitchUserPref;
497         mFragment.mRemoveUserPref = mRemoveUserPref;
498         mFragment.mAppAndContentAccessPref = mAppAndContentAccessPref;
499         doNothing().when(mFragment).showDialog(anyInt());
500 
501         mFragment.onPreferenceClick(mRemoveUserPref);
502 
503         verify(mFragment).canDeleteUser();
504         verify(mFragment).showDialog(DIALOG_CONFIRM_REMOVE);
505     }
506 
507     @Test
onPreferenceClick_removeClicked_canNotDelete_doNothing()508     public void onPreferenceClick_removeClicked_canNotDelete_doNothing() {
509         setupSelectedUser();
510         mFragment.mUserInfo = mUserInfo;
511         mUserManager.setIsAdminUser(false);
512         mFragment.mSwitchUserPref = mSwitchUserPref;
513         mFragment.mRemoveUserPref = mRemoveUserPref;
514         mFragment.mAppAndContentAccessPref = mAppAndContentAccessPref;
515         doNothing().when(mFragment).showDialog(anyInt());
516 
517         mFragment.onPreferenceClick(mRemoveUserPref);
518 
519         verify(mFragment).canDeleteUser();
520         verify(mFragment, never()).showDialog(DIALOG_CONFIRM_REMOVE);
521     }
522 
523     @Test
onPreferenceClick_selectRestrictedUser_appAndContentAccessClicked_startActivity()524     public void onPreferenceClick_selectRestrictedUser_appAndContentAccessClicked_startActivity() {
525         setupSelectedRestrictedUser();
526         mFragment.mUserInfo = mUserInfo;
527         mUserManager.setIsAdminUser(true);
528         mFragment.mSwitchUserPref = mSwitchUserPref;
529         mFragment.mRemoveUserPref = mRemoveUserPref;
530         mFragment.mAppAndContentAccessPref = mAppAndContentAccessPref;
531 
532         mFragment.onPreferenceClick(mAppAndContentAccessPref);
533 
534         Intent startedIntent = shadowOf(mActivity).getNextStartedActivity();
535         ShadowIntent shadowIntent = shadowOf(startedIntent);
536         assertThat(shadowIntent.getIntentClass()).isEqualTo(SubSettings.class);
537         assertThat(startedIntent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT))
538                 .isEqualTo(AppRestrictionsFragment.class.getName());
539         Bundle arguments = startedIntent.getBundleExtra(
540                 SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS);
541         assertThat(arguments.getInt(AppRestrictionsFragment.EXTRA_USER_ID, 0))
542                 .isEqualTo(mUserInfo.id);
543         assertThat(arguments.getBoolean(AppRestrictionsFragment.EXTRA_NEW_USER, true))
544                 .isEqualTo(false);
545     }
546 
547     @Test
onPreferenceClick_unknownPreferenceClicked_doNothing()548     public void onPreferenceClick_unknownPreferenceClicked_doNothing() {
549         setupSelectedUser();
550         mFragment.mUserInfo = mUserInfo;
551         mFragment.mSwitchUserPref = mSwitchUserPref;
552         mFragment.mRemoveUserPref = mRemoveUserPref;
553         mFragment.mAppAndContentAccessPref = mAppAndContentAccessPref;
554 
555         mFragment.onPreferenceClick(mock(UserPreference.class));
556 
557         verify(mFragment).onPreferenceClick(any());
558         verifyNoMoreInteractions(mFragment);
559     }
560 
561     @Test
canDeleteUser_nonAdminUser_shouldReturnFalse()562     public void canDeleteUser_nonAdminUser_shouldReturnFalse() {
563         mUserManager.setIsAdminUser(false);
564 
565         boolean result = mFragment.canDeleteUser();
566 
567         assertThat(result).isFalse();
568     }
569 
570     @Test
canDeleteUser_adminSelectsUser_noRestrictions_shouldReturnTrue()571     public void canDeleteUser_adminSelectsUser_noRestrictions_shouldReturnTrue() {
572         setupSelectedUser();
573         mUserManager.setIsAdminUser(true);
574 
575         boolean result = mFragment.canDeleteUser();
576 
577         assertThat(result).isTrue();
578     }
579 
580     @Test
canDeleteUser_adminSelectsUser_hasRemoveRestriction_shouldReturnFalse()581     public void canDeleteUser_adminSelectsUser_hasRemoveRestriction_shouldReturnFalse() {
582         setupSelectedUser();
583         mUserManager.setIsAdminUser(true);
584         ComponentName componentName = new ComponentName("test", "test");
585         ShadowDevicePolicyManager.getShadow().setDeviceOwnerComponentOnAnyUser(componentName);
586         ShadowDevicePolicyManager.getShadow().setDeviceOwnerUserId(UserHandle.myUserId());
587         List<UserManager.EnforcingUser> enforcingUsers = new ArrayList<>();
588         enforcingUsers.add(new UserManager.EnforcingUser(UserHandle.myUserId(),
589                 UserManager.RESTRICTION_SOURCE_DEVICE_OWNER));
590         mUserManager.setUserRestrictionSources(
591                 UserManager.DISALLOW_REMOVE_USER,
592                 UserHandle.of(UserHandle.myUserId()),
593                 enforcingUsers
594         );
595 
596         boolean result = mFragment.canDeleteUser();
597 
598         assertThat(result).isFalse();
599     }
600 
setupSelectedUser()601     private void setupSelectedUser() {
602         mArguments.putInt("user_id", 1);
603         mUserInfo = new UserInfo(1, "Tom", null,
604                 UserInfo.FLAG_FULL | UserInfo.FLAG_INITIALIZED,
605                 UserManager.USER_TYPE_FULL_SECONDARY);
606 
607         mUserManager.addProfile(mUserInfo);
608     }
609 
setupSelectedGuest()610     private void setupSelectedGuest() {
611         mArguments.putInt("user_id", 23);
612         mUserInfo = new UserInfo(23, "Guest", null,
613                 UserInfo.FLAG_FULL | UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_GUEST,
614                 UserManager.USER_TYPE_FULL_GUEST);
615 
616         mUserManager.addProfile(mUserInfo);
617     }
618 
setupSelectedRestrictedUser()619     private void setupSelectedRestrictedUser() {
620         mArguments.putInt("user_id", 21);
621         mUserInfo = new UserInfo(21, "Bob", null,
622                 UserInfo.FLAG_FULL | UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_RESTRICTED,
623                 UserManager.USER_TYPE_FULL_RESTRICTED);
624 
625         mUserManager.addProfile(mUserInfo);
626     }
627 }
628