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.junit.Assert.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyInt;
28 import static org.mockito.ArgumentMatchers.anyString;
29 import static org.mockito.ArgumentMatchers.eq;
30 import static org.mockito.Mockito.doNothing;
31 import static org.mockito.Mockito.doReturn;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.never;
34 import static org.mockito.Mockito.spy;
35 import static org.mockito.Mockito.verify;
36 import static org.mockito.Mockito.verifyNoMoreInteractions;
37 import static org.robolectric.Shadows.shadowOf;
38 
39 import android.app.admin.DevicePolicyManager;
40 import android.app.settings.SettingsEnums;
41 import android.content.ComponentName;
42 import android.content.Context;
43 import android.content.Intent;
44 import android.content.pm.UserInfo;
45 import android.os.Bundle;
46 import android.os.UserHandle;
47 import android.os.UserManager;
48 import android.telephony.TelephonyManager;
49 
50 import androidx.fragment.app.FragmentActivity;
51 import androidx.preference.Preference;
52 import androidx.preference.PreferenceScreen;
53 import androidx.preference.SwitchPreference;
54 
55 import com.android.settings.R;
56 import com.android.settings.SettingsActivity;
57 import com.android.settings.SubSettings;
58 import com.android.settings.testutils.shadow.ShadowDevicePolicyManager;
59 import com.android.settings.testutils.shadow.ShadowUserManager;
60 import com.android.settingslib.RestrictedLockUtils;
61 import com.android.settingslib.RestrictedPreference;
62 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
63 
64 import org.junit.After;
65 import org.junit.Before;
66 import org.junit.Ignore;
67 import org.junit.Test;
68 import org.junit.runner.RunWith;
69 import org.mockito.Mock;
70 import org.mockito.MockitoAnnotations;
71 import org.robolectric.RobolectricTestRunner;
72 import org.robolectric.RuntimeEnvironment;
73 import org.robolectric.android.controller.ActivityController;
74 import org.robolectric.annotation.Config;
75 import org.robolectric.shadow.api.Shadow;
76 import org.robolectric.shadows.ShadowIntent;
77 import org.robolectric.util.ReflectionHelpers;
78 
79 import java.util.ArrayList;
80 import java.util.List;
81 
82 @RunWith(RobolectricTestRunner.class)
83 @Config(shadows = {
84         ShadowUserManager.class,
85         com.android.settings.testutils.shadow.ShadowFragment.class,
86         ShadowDevicePolicyManager.class
87 })
88 public class UserDetailsSettingsTest {
89 
90     private static final String KEY_GRANT_ADMIN = "user_grant_admin";
91     private static final String KEY_SWITCH_USER = "switch_user";
92     private static final String KEY_ENABLE_TELEPHONY = "enable_calling";
93     private static final String KEY_REMOVE_USER = "remove_user";
94     private static final String KEY_APP_AND_CONTENT_ACCESS = "app_and_content_access";
95     private static final String KEY_APP_COPYING = "app_copying";
96 
97     private static final int DIALOG_CONFIRM_REMOVE = 1;
98     private static final int DIALOG_CONFIRM_RESET_GUEST = 4;
99 
100     @Mock
101     private TelephonyManager mTelephonyManager;
102 
103     private ShadowUserManager mUserManager;
104 
105     @Mock
106     private RestrictedPreference mSwitchUserPref;
107     @Mock
108     private SwitchPreference mPhonePref;
109     @Mock
110     private SwitchPreference mGrantAdminPref;
111     @Mock
112     private Preference mRemoveUserPref;
113     @Mock
114     private Preference mAppAndContentAccessPref;
115     @Mock
116     private Preference mAppCopyingPref;
117 
118     private FragmentActivity mActivity;
119     private Context mContext;
120     private UserCapabilities mUserCapabilities;
121     @Mock
122     private MetricsFeatureProvider mMetricsFeatureProvider;
123     private UserDetailsSettings mFragment;
124     private Bundle mArguments;
125     private UserInfo mUserInfo;
126 
127     @Before
setUp()128     public void setUp() {
129         MockitoAnnotations.initMocks(this);
130 
131         mActivity = spy(ActivityController.of(new FragmentActivity()).get());
132         mContext = spy(RuntimeEnvironment.application);
133         mUserCapabilities = UserCapabilities.create(mContext);
134         mUserCapabilities.mUserSwitcherEnabled = true;
135         mFragment = spy(new UserDetailsSettings());
136         mArguments = new Bundle();
137 
138         UserManager userManager = (UserManager) mContext.getSystemService(
139                 Context.USER_SERVICE);
140         mUserManager = Shadow.extract(userManager);
141 
142         doReturn(mTelephonyManager).when(mActivity).getSystemService(Context.TELEPHONY_SERVICE);
143 
144         ReflectionHelpers.setField(mFragment, "mUserManager", userManager);
145         ReflectionHelpers.setField(mFragment, "mUserCaps", mUserCapabilities);
146         ReflectionHelpers.setField(mFragment, "mMetricsFeatureProvider", mMetricsFeatureProvider);
147         doReturn(mActivity).when(mFragment).getActivity();
148         doReturn(mActivity).when(mFragment).getContext();
149 
150         doReturn(mock(PreferenceScreen.class)).when(mFragment).getPreferenceScreen();
151 
152         doReturn(mSwitchUserPref).when(mFragment).findPreference(KEY_SWITCH_USER);
153         doReturn(mGrantAdminPref).when(mFragment).findPreference(KEY_GRANT_ADMIN);
154         doReturn(mPhonePref).when(mFragment).findPreference(KEY_ENABLE_TELEPHONY);
155         doReturn(mRemoveUserPref).when(mFragment).findPreference(KEY_REMOVE_USER);
156         doReturn(mAppAndContentAccessPref)
157                 .when(mFragment).findPreference(KEY_APP_AND_CONTENT_ACCESS);
158         doReturn(mAppCopyingPref).when(mFragment).findPreference(KEY_APP_COPYING);
159     }
160 
161     @After
tearDown()162     public void tearDown() {
163         ShadowUserManager.reset();
164     }
165 
166     @Test(expected = IllegalStateException.class)
initialize_nullArguments_shouldThrowException()167     public void initialize_nullArguments_shouldThrowException() {
168         mFragment.initialize(mActivity, null);
169     }
170 
171     @Test(expected = IllegalStateException.class)
initialize_emptyArguments_shouldThrowException()172     public void initialize_emptyArguments_shouldThrowException() {
173         mFragment.initialize(mActivity, new Bundle());
174     }
175 
176     @Test
initialize_userSelected_shouldSetupSwitchPref()177     public void initialize_userSelected_shouldSetupSwitchPref() {
178         setupSelectedUser();
179         doReturn("Switch to " + mUserInfo.name)
180                 .when(mActivity).getString(anyInt(), anyString());
181 
182         mFragment.initialize(mActivity, mArguments);
183 
184         verify(mActivity).getString(com.android.settingslib.R.string.user_switch_to_user,
185                 mUserInfo.name);
186         verify(mSwitchUserPref).setTitle("Switch to " + mUserInfo.name);
187         verify(mSwitchUserPref).setOnPreferenceClickListener(mFragment);
188         verify(mFragment, never()).removePreference(KEY_SWITCH_USER);
189     }
190 
191     @Test
initialize_guestSelected_shouldSetupSwitchPref()192     public void initialize_guestSelected_shouldSetupSwitchPref() {
193         setupSelectedGuest();
194         doReturn("Switch to " + mUserInfo.name)
195                 .when(mActivity).getString(anyInt(), anyString());
196 
197         mFragment.initialize(mActivity, mArguments);
198 
199         verify(mActivity).getString(com.android.settingslib.R.string.user_switch_to_user,
200                 mUserInfo.name);
201         verify(mSwitchUserPref).setTitle("Switch to " + mUserInfo.name);
202         verify(mSwitchUserPref).setOnPreferenceClickListener(mFragment);
203         verify(mFragment, never()).removePreference(KEY_SWITCH_USER);
204     }
205 
206     @Test
initialize_userSelected_shouldNotShowAppAndContentPref()207     public void initialize_userSelected_shouldNotShowAppAndContentPref() {
208         setupSelectedUser();
209 
210         mFragment.initialize(mActivity, mArguments);
211 
212         verify(mFragment).removePreference(KEY_APP_AND_CONTENT_ACCESS);
213     }
214 
215     @Test
initialize_guestSelected_shouldNotShowAppAndContentPref()216     public void initialize_guestSelected_shouldNotShowAppAndContentPref() {
217         setupSelectedGuest();
218 
219         mFragment.initialize(mActivity, mArguments);
220 
221         verify(mFragment).removePreference(KEY_APP_AND_CONTENT_ACCESS);
222     }
223 
224     @Test
225     @Ignore("b/313530297")
onResume_canSwitch_shouldEnableSwitchPref()226     public void onResume_canSwitch_shouldEnableSwitchPref() {
227         mUserManager.setSwitchabilityStatus(SWITCHABILITY_STATUS_OK);
228         mFragment.mSwitchUserPref = mSwitchUserPref;
229         mFragment.onAttach(mContext);
230 
231         mFragment.onResume();
232 
233         verify(mSwitchUserPref).setEnabled(true);
234     }
235 
236     @Test
237     @Ignore("b/313530297")
onResume_userInCall_shouldDisableSwitchPref()238     public void onResume_userInCall_shouldDisableSwitchPref() {
239         mUserManager.setSwitchabilityStatus(SWITCHABILITY_STATUS_USER_IN_CALL);
240         mFragment.mSwitchUserPref = mSwitchUserPref;
241         mFragment.onAttach(mContext);
242 
243         mFragment.onResume();
244 
245         verify(mSwitchUserPref).setEnabled(false);
246     }
247 
248     @Test
249     @Ignore("b/313530297")
onResume_switchDisallowed_shouldDisableSwitchPref()250     public void onResume_switchDisallowed_shouldDisableSwitchPref() {
251         mUserManager.setSwitchabilityStatus(SWITCHABILITY_STATUS_USER_SWITCH_DISALLOWED);
252         mFragment.mSwitchUserPref = mSwitchUserPref;
253         mFragment.onAttach(mContext);
254 
255         mFragment.onResume();
256 
257         verify(mSwitchUserPref).setEnabled(false);
258     }
259 
260     @Test
261     @Ignore("b/313530297")
onResume_systemUserLocked_shouldDisableSwitchPref()262     public void onResume_systemUserLocked_shouldDisableSwitchPref() {
263         mUserManager.setSwitchabilityStatus(UserManager.SWITCHABILITY_STATUS_SYSTEM_USER_LOCKED);
264         mFragment.mSwitchUserPref = mSwitchUserPref;
265         mFragment.onAttach(mContext);
266 
267         mFragment.onResume();
268 
269         verify(mSwitchUserPref).setEnabled(false);
270     }
271 
272     @Ignore("b/313530297")
273     @Test
initialize_adminWithTelephony_shouldShowPhonePreference()274     public void initialize_adminWithTelephony_shouldShowPhonePreference() {
275         setupSelectedUser();
276         doReturn(true).when(mTelephonyManager).isVoiceCapable();
277         mUserManager.setIsAdminUser(true);
278 
279         mFragment.initialize(mActivity, mArguments);
280 
281         verify(mFragment, never()).removePreference(KEY_ENABLE_TELEPHONY);
282         verify(mPhonePref).setOnPreferenceChangeListener(mFragment);
283     }
284 
285     @Test
initialize_adminNoTelephony_shouldNotShowPhonePreference()286     public void initialize_adminNoTelephony_shouldNotShowPhonePreference() {
287         setupSelectedUser();
288         doReturn(false).when(mTelephonyManager).isVoiceCapable();
289         mUserManager.setIsAdminUser(true);
290         doReturn(null).when(mActivity).getSystemService(Context.TELEPHONY_SERVICE);
291 
292         mFragment.initialize(mActivity, mArguments);
293 
294         verify(mFragment).removePreference(KEY_ENABLE_TELEPHONY);
295     }
296 
297     @Test
initialize_nonAdminWithTelephony_shouldNotShowPhonePreference()298     public void initialize_nonAdminWithTelephony_shouldNotShowPhonePreference() {
299         setupSelectedUser();
300         doReturn(true).when(mTelephonyManager).isVoiceCapable();
301         mUserManager.setIsAdminUser(false);
302 
303         mFragment.initialize(mActivity, mArguments);
304 
305         verify(mFragment).removePreference(KEY_ENABLE_TELEPHONY);
306     }
307 
308     @Test
initialize_nonAdmin_shouldNotShowAppAndContentPref()309     public void initialize_nonAdmin_shouldNotShowAppAndContentPref() {
310         setupSelectedUser();
311         mUserManager.setIsAdminUser(false);
312 
313         mFragment.initialize(mActivity, mArguments);
314 
315         verify(mFragment).removePreference(KEY_APP_AND_CONTENT_ACCESS);
316     }
317 
318     @Ignore("b/313530297")
319     @Test
initialize_adminSelectsSecondaryUser_shouldShowRemovePreference()320     public void initialize_adminSelectsSecondaryUser_shouldShowRemovePreference() {
321         setupSelectedUser();
322         mUserManager.setIsAdminUser(true);
323 
324         mFragment.initialize(mActivity, mArguments);
325 
326         verify(mRemoveUserPref).setOnPreferenceClickListener(mFragment);
327         verify(mRemoveUserPref).setTitle(R.string.user_remove_user);
328         verify(mFragment, never()).removePreference(KEY_REMOVE_USER);
329     }
330 
331     @Ignore("b/313530297")
332     @Test
initialize_adminSelectsNewRestrictedUser_shouldOpenAppContentScreen()333     public void initialize_adminSelectsNewRestrictedUser_shouldOpenAppContentScreen() {
334         setupSelectedRestrictedUser();
335         mUserManager.setIsAdminUser(true);
336         mArguments.putBoolean(AppRestrictionsFragment.EXTRA_NEW_USER, true);
337 
338         mFragment.initialize(mActivity, mArguments);
339 
340         Intent startedIntent = shadowOf(mActivity).getNextStartedActivity();
341         ShadowIntent shadowIntent = shadowOf(startedIntent);
342         assertThat(shadowIntent.getIntentClass()).isEqualTo(SubSettings.class);
343         assertThat(startedIntent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT))
344                 .isEqualTo(AppRestrictionsFragment.class.getName());
345         Bundle arguments = startedIntent.getBundleExtra(
346                 SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS);
347         assertThat(arguments).isNotNull();
348         assertThat(arguments.getInt(AppRestrictionsFragment.EXTRA_USER_ID, 0))
349                 .isEqualTo(mUserInfo.id);
350         assertThat(arguments.getBoolean(AppRestrictionsFragment.EXTRA_NEW_USER, false))
351                 .isEqualTo(true);
352     }
353 
354     @Ignore("b/313530297")
355     @Test
initialize_adminSelectsRestrictedUser_shouldSetupPreferences()356     public void initialize_adminSelectsRestrictedUser_shouldSetupPreferences() {
357         setupSelectedRestrictedUser();
358         mUserManager.setIsAdminUser(true);
359         doReturn(true).when(mTelephonyManager).isVoiceCapable();
360 
361         mFragment.initialize(mActivity, mArguments);
362 
363         verify(mFragment, never()).removePreference(KEY_REMOVE_USER);
364         verify(mFragment, never()).removePreference(KEY_SWITCH_USER);
365         verify(mFragment, never()).removePreference(KEY_APP_AND_CONTENT_ACCESS);
366         verify(mFragment).removePreference(KEY_ENABLE_TELEPHONY);
367         verify(mSwitchUserPref).setTitle("Switch to " + mUserInfo.name);
368         verify(mAppAndContentAccessPref).setOnPreferenceClickListener(mFragment);
369         verify(mSwitchUserPref).setOnPreferenceClickListener(mFragment);
370         verify(mRemoveUserPref).setOnPreferenceClickListener(mFragment);
371     }
372 
373     @Test
initialize_adminSelectsExistingRestrictedUser_shouldNotStartAppAndContentAccess()374     public void initialize_adminSelectsExistingRestrictedUser_shouldNotStartAppAndContentAccess() {
375         setupSelectedRestrictedUser();
376         mUserManager.setIsAdminUser(true);
377         mArguments.putBoolean(AppRestrictionsFragment.EXTRA_NEW_USER, false);
378 
379         mFragment.initialize(mActivity, mArguments);
380 
381         verify(mActivity, never()).startActivity(any(Intent.class));
382     }
383 
384     @Ignore("b/313530297")
385     @Test
initialize_adminSelectsGuest_shouldShowRemovePreference()386     public void initialize_adminSelectsGuest_shouldShowRemovePreference() {
387         setupSelectedGuest();
388         mUserManager.setIsAdminUser(true);
389 
390         mFragment.initialize(mActivity, mArguments);
391 
392         verify(mRemoveUserPref).setOnPreferenceClickListener(mFragment);
393         verify(mRemoveUserPref).setTitle(com.android.settingslib.R.string.guest_exit_guest);
394         verify(mFragment, never()).removePreference(KEY_REMOVE_USER);
395     }
396 
397     @Test
initialize_nonAdmin_shouldNotShowRemovePreference()398     public void initialize_nonAdmin_shouldNotShowRemovePreference() {
399         setupSelectedUser();
400         mUserManager.setIsAdminUser(false);
401 
402         mFragment.initialize(mActivity, mArguments);
403 
404         verify(mFragment).removePreference(KEY_REMOVE_USER);
405     }
406 
407     @Test
initialize_onMainUser_shouldNotShowRemovePreference()408     public void initialize_onMainUser_shouldNotShowRemovePreference() {
409         setupSelectedMainUser();
410         mUserManager.setIsAdminUser(true);
411 
412         mFragment.initialize(mActivity, mArguments);
413 
414         verify(mFragment).removePreference(KEY_REMOVE_USER);
415     }
416 
417     @Test
initialize_disallowRemoveUserRestriction_shouldNotShowRemovePreference()418     public void initialize_disallowRemoveUserRestriction_shouldNotShowRemovePreference() {
419         setupSelectedUser();
420         mUserManager.setIsAdminUser(true);
421         mUserManager.addBaseUserRestriction(UserManager.DISALLOW_REMOVE_USER);
422 
423         mFragment.initialize(mActivity, mArguments);
424 
425         verify(mFragment).removePreference(KEY_REMOVE_USER);
426     }
427 
428     @Ignore("b/313530297")
429     @Test
initialize_userHasCallRestriction_shouldSetPhoneSwitchUnChecked()430     public void initialize_userHasCallRestriction_shouldSetPhoneSwitchUnChecked() {
431         setupSelectedUser();
432         mUserManager.setIsAdminUser(true);
433         mUserManager.setUserRestriction(mUserInfo.getUserHandle(),
434                 UserManager.DISALLOW_OUTGOING_CALLS, true);
435 
436         mFragment.initialize(mActivity, mArguments);
437 
438         verify(mPhonePref).setChecked(false);
439     }
440 
441     @Ignore("b/313530297")
442     @Test
initialize_noCallRestriction_shouldSetPhoneSwitchChecked()443     public void initialize_noCallRestriction_shouldSetPhoneSwitchChecked() {
444         setupSelectedUser();
445         mUserManager.setIsAdminUser(true);
446 
447         mFragment.initialize(mActivity, mArguments);
448 
449         verify(mPhonePref).setChecked(true);
450     }
451 
452     @Test
initialize_switchUserDisallowed_shouldSetAdminDisabledOnSwitchPreference()453     public void initialize_switchUserDisallowed_shouldSetAdminDisabledOnSwitchPreference() {
454         setupSelectedUser();
455         mUserCapabilities.mDisallowSwitchUser = true;
456         DevicePolicyManager devicePolicyManager = mock(DevicePolicyManager.class);
457         doReturn(devicePolicyManager).when(mActivity)
458                 .getSystemService(Context.DEVICE_POLICY_SERVICE);
459         doReturn(mock(ComponentName.class)).when(devicePolicyManager)
460                 .getDeviceOwnerComponentOnAnyUser();
461 
462         mFragment.initialize(mActivity, mArguments);
463 
464         verify(mSwitchUserPref).setDisabledByAdmin(any(RestrictedLockUtils.EnforcedAdmin.class));
465     }
466 
467     @Test
initialize_switchUserAllowed_shouldSetSwitchPreferenceEnabled()468     public void initialize_switchUserAllowed_shouldSetSwitchPreferenceEnabled() {
469         setupSelectedUser();
470         mUserCapabilities.mDisallowSwitchUser = false;
471 
472         mFragment.initialize(mActivity, mArguments);
473 
474         verify(mSwitchUserPref).setDisabledByAdmin(null);
475         verify(mSwitchUserPref).setSelectable(true);
476         verify(mSwitchUserPref).setOnPreferenceClickListener(mFragment);
477     }
478 
479     @Test
onPreferenceClick_switchClicked_canSwitch_shouldSwitch()480     public void onPreferenceClick_switchClicked_canSwitch_shouldSwitch() {
481         setupSelectedUser();
482         mUserManager.setSwitchabilityStatus(SWITCHABILITY_STATUS_OK);
483         mFragment.mSwitchUserPref = mSwitchUserPref;
484         mFragment.mRemoveUserPref = mRemoveUserPref;
485         mFragment.mAppAndContentAccessPref = mAppAndContentAccessPref;
486         mFragment.mUserInfo = mUserInfo;
487 
488         mFragment.onPreferenceClick(mSwitchUserPref);
489 
490         verify(mFragment).switchUser();
491         verify(mMetricsFeatureProvider).action(any(),
492                 eq(SettingsEnums.ACTION_SWITCH_TO_USER));
493     }
494 
495     @Test
onPreferenceClick_switchToRestrictedClicked_canSwitch_shouldSwitch()496     public void onPreferenceClick_switchToRestrictedClicked_canSwitch_shouldSwitch() {
497         setupSelectedRestrictedUser();
498         mUserManager.setSwitchabilityStatus(SWITCHABILITY_STATUS_OK);
499         mFragment.mSwitchUserPref = mSwitchUserPref;
500         mFragment.mRemoveUserPref = mRemoveUserPref;
501         mFragment.mAppAndContentAccessPref = mAppAndContentAccessPref;
502         mFragment.mUserInfo = mUserInfo;
503 
504         mFragment.onPreferenceClick(mSwitchUserPref);
505 
506         verify(mFragment).switchUser();
507         verify(mMetricsFeatureProvider).action(any(),
508                 eq(SettingsEnums.ACTION_SWITCH_TO_RESTRICTED_USER));
509     }
510 
511     @Test
onPreferenceClick_switchToGuestClicked_canSwitch_shouldSwitch()512     public void onPreferenceClick_switchToGuestClicked_canSwitch_shouldSwitch() {
513         setupSelectedGuest();
514         mUserManager.setSwitchabilityStatus(SWITCHABILITY_STATUS_OK);
515         mFragment.mSwitchUserPref = mSwitchUserPref;
516         mFragment.mRemoveUserPref = mRemoveUserPref;
517         mFragment.mAppAndContentAccessPref = mAppAndContentAccessPref;
518         mFragment.mUserInfo = mUserInfo;
519 
520         mFragment.onPreferenceClick(mSwitchUserPref);
521 
522         verify(mFragment).switchUser();
523         verify(mMetricsFeatureProvider).action(any(), eq(SettingsEnums.ACTION_SWITCH_TO_GUEST));
524     }
525 
526     @Test
onPreferenceClick_switchClicked_canNotSwitch_doNothing()527     public void onPreferenceClick_switchClicked_canNotSwitch_doNothing() {
528         setupSelectedUser();
529         mUserManager.setSwitchabilityStatus(SWITCHABILITY_STATUS_USER_SWITCH_DISALLOWED);
530         mFragment.mSwitchUserPref = mSwitchUserPref;
531         mFragment.mRemoveUserPref = mRemoveUserPref;
532         mFragment.mAppAndContentAccessPref = mAppAndContentAccessPref;
533         mFragment.mUserInfo = mUserInfo;
534 
535         mFragment.onPreferenceClick(mSwitchUserPref);
536 
537         verify(mFragment, never()).switchUser();
538     }
539 
540     @Ignore("b/313530297")
541     @Test
onPreferenceClick_removeGuestClicked_canDelete_shouldShowDialog()542     public void onPreferenceClick_removeGuestClicked_canDelete_shouldShowDialog() {
543         setupSelectedGuest();
544         mFragment.mUserInfo = mUserInfo;
545         mUserManager.setIsAdminUser(true);
546         mFragment.mSwitchUserPref = mSwitchUserPref;
547         mFragment.mRemoveUserPref = mRemoveUserPref;
548         mFragment.mAppAndContentAccessPref = mAppAndContentAccessPref;
549         doNothing().when(mFragment).showDialog(anyInt());
550 
551         mFragment.onPreferenceClick(mRemoveUserPref);
552 
553         verify(mMetricsFeatureProvider).action(any(), eq(SettingsEnums.ACTION_REMOVE_GUEST_USER));
554         verify(mFragment).canDeleteUser();
555         verify(mFragment).showDialog(DIALOG_CONFIRM_RESET_GUEST);
556     }
557 
558     @Ignore("b/313530297")
559     @Test
onPreferenceClick_removeRestrictedClicked_canDelete_shouldShowDialog()560     public void onPreferenceClick_removeRestrictedClicked_canDelete_shouldShowDialog() {
561         setupSelectedRestrictedUser();
562         mFragment.mUserInfo = mUserInfo;
563         mUserManager.setIsAdminUser(true);
564         mFragment.mSwitchUserPref = mSwitchUserPref;
565         mFragment.mRemoveUserPref = mRemoveUserPref;
566         mFragment.mAppAndContentAccessPref = mAppAndContentAccessPref;
567         doNothing().when(mFragment).showDialog(anyInt());
568 
569         mFragment.onPreferenceClick(mRemoveUserPref);
570 
571         verify(mMetricsFeatureProvider)
572                 .action(any(), eq(SettingsEnums.ACTION_REMOVE_RESTRICTED_USER));
573         verify(mFragment).canDeleteUser();
574         verify(mFragment).showDialog(DIALOG_CONFIRM_REMOVE);
575     }
576 
577     @Ignore("b/313530297")
578     @Test
onPreferenceClick_removeClicked_canDelete_shouldShowDialog()579     public void onPreferenceClick_removeClicked_canDelete_shouldShowDialog() {
580         setupSelectedUser();
581         mFragment.mUserInfo = mUserInfo;
582         mUserManager.setIsAdminUser(true);
583         mFragment.mSwitchUserPref = mSwitchUserPref;
584         mFragment.mRemoveUserPref = mRemoveUserPref;
585         mFragment.mAppAndContentAccessPref = mAppAndContentAccessPref;
586         doNothing().when(mFragment).showDialog(anyInt());
587 
588         mFragment.onPreferenceClick(mRemoveUserPref);
589 
590         verify(mMetricsFeatureProvider).action(any(), eq(SettingsEnums.ACTION_REMOVE_USER));
591         verify(mFragment).canDeleteUser();
592         verify(mFragment).showDialog(DIALOG_CONFIRM_REMOVE);
593     }
594 
595     @Test
onPreferenceClick_removeClicked_canNotDelete_doNothing()596     public void onPreferenceClick_removeClicked_canNotDelete_doNothing() {
597         setupSelectedUser();
598         mFragment.mUserInfo = mUserInfo;
599         mUserManager.setIsAdminUser(false);
600         mFragment.mSwitchUserPref = mSwitchUserPref;
601         mFragment.mRemoveUserPref = mRemoveUserPref;
602         mFragment.mAppAndContentAccessPref = mAppAndContentAccessPref;
603         doNothing().when(mFragment).showDialog(anyInt());
604 
605         mFragment.onPreferenceClick(mRemoveUserPref);
606 
607         verify(mFragment).canDeleteUser();
608         verify(mFragment, never()).showDialog(DIALOG_CONFIRM_REMOVE);
609     }
610 
611     @Test
onPreferenceClick_selectRestrictedUser_appAndContentAccessClicked_startActivity()612     public void onPreferenceClick_selectRestrictedUser_appAndContentAccessClicked_startActivity() {
613         setupSelectedRestrictedUser();
614         mFragment.mUserInfo = mUserInfo;
615         mUserManager.setIsAdminUser(true);
616         mFragment.mSwitchUserPref = mSwitchUserPref;
617         mFragment.mRemoveUserPref = mRemoveUserPref;
618         mFragment.mAppAndContentAccessPref = mAppAndContentAccessPref;
619 
620         mFragment.onPreferenceClick(mAppAndContentAccessPref);
621 
622         Intent startedIntent = shadowOf(mActivity).getNextStartedActivity();
623         ShadowIntent shadowIntent = shadowOf(startedIntent);
624         assertThat(shadowIntent.getIntentClass()).isEqualTo(SubSettings.class);
625         assertThat(startedIntent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT))
626                 .isEqualTo(AppRestrictionsFragment.class.getName());
627         Bundle arguments = startedIntent.getBundleExtra(
628                 SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS);
629         assertThat(arguments.getInt(AppRestrictionsFragment.EXTRA_USER_ID, 0))
630                 .isEqualTo(mUserInfo.id);
631         assertThat(arguments.getBoolean(AppRestrictionsFragment.EXTRA_NEW_USER, true))
632                 .isEqualTo(false);
633     }
634 
635     @Test
onPreferenceClick_unknownPreferenceClicked_doNothing()636     public void onPreferenceClick_unknownPreferenceClicked_doNothing() {
637         setupSelectedUser();
638         mFragment.mUserInfo = mUserInfo;
639         mFragment.mSwitchUserPref = mSwitchUserPref;
640         mFragment.mRemoveUserPref = mRemoveUserPref;
641         mFragment.mAppAndContentAccessPref = mAppAndContentAccessPref;
642 
643         mFragment.onPreferenceClick(mock(UserPreference.class));
644 
645         verify(mFragment).onPreferenceClick(any());
646         verifyNoMoreInteractions(mFragment);
647     }
648 
649     @Test
canDeleteUser_nonAdminUser_shouldReturnFalse()650     public void canDeleteUser_nonAdminUser_shouldReturnFalse() {
651         mUserManager.setIsAdminUser(false);
652 
653         boolean result = mFragment.canDeleteUser();
654 
655         assertThat(result).isFalse();
656     }
657 
658     @Test
canDeleteUser_onMainUser_shouldReturnFalse()659     public void canDeleteUser_onMainUser_shouldReturnFalse() {
660         setupSelectedMainUser();
661         mUserManager.setIsAdminUser(true);
662         mFragment.mUserInfo = mUserInfo;
663 
664         boolean result = mFragment.canDeleteUser();
665 
666         assertThat(result).isFalse();
667     }
668 
669     @Ignore("b/313530297")
670     @Test
canDeleteUser_adminSelectsUser_noRestrictions_shouldReturnTrue()671     public void canDeleteUser_adminSelectsUser_noRestrictions_shouldReturnTrue() {
672         setupSelectedUser();
673         mUserManager.setIsAdminUser(true);
674         mFragment.mUserInfo = mUserInfo;
675 
676         boolean result = mFragment.canDeleteUser();
677 
678         assertThat(result).isTrue();
679     }
680 
681     @Test
canDeleteUser_adminSelectsUser_hasRemoveRestriction_shouldReturnFalse()682     public void canDeleteUser_adminSelectsUser_hasRemoveRestriction_shouldReturnFalse() {
683         setupSelectedUser();
684         mUserManager.setIsAdminUser(true);
685         mFragment.mUserInfo = mUserInfo;
686         ComponentName componentName = new ComponentName("test", "test");
687         ShadowDevicePolicyManager.getShadow().setDeviceOwnerComponentOnAnyUser(componentName);
688         ShadowDevicePolicyManager.getShadow().setDeviceOwnerUserId(UserHandle.myUserId());
689         List<UserManager.EnforcingUser> enforcingUsers = new ArrayList<>();
690         enforcingUsers.add(new UserManager.EnforcingUser(UserHandle.myUserId(),
691                 UserManager.RESTRICTION_SOURCE_DEVICE_OWNER));
692         mUserManager.setUserRestrictionSources(
693                 UserManager.DISALLOW_REMOVE_USER,
694                 UserHandle.of(UserHandle.myUserId()),
695                 enforcingUsers
696         );
697 
698         boolean result = mFragment.canDeleteUser();
699 
700         assertThat(result).isFalse();
701     }
702 
703     @Ignore("b/313530297")
704     @Test
initialize_userSelected_shouldShowGrantAdminPref_MultipleAdminEnabled()705     public void initialize_userSelected_shouldShowGrantAdminPref_MultipleAdminEnabled() {
706         setupSelectedUser();
707         ShadowUserManager.setIsMultipleAdminEnabled(true);
708         mFragment.initialize(mActivity, mArguments);
709         assertTrue(UserManager.isMultipleAdminEnabled());
710         verify(mFragment, never()).removePreference(KEY_GRANT_ADMIN);
711     }
712 
713     @Ignore("b/313530297")
714     @Test
initialize_userSelected_shouldNotShowGrantAdminPref()715     public void initialize_userSelected_shouldNotShowGrantAdminPref() {
716         setupSelectedUser();
717         mFragment.initialize(mActivity, mArguments);
718         verify(mFragment).removePreference(KEY_GRANT_ADMIN);
719     }
720 
721     @Ignore("b/313530297")
722     @Test
initialize_restrictUserSelected_shouldNotShowGrantAdminPref_MultipleAdminEnabled()723     public void initialize_restrictUserSelected_shouldNotShowGrantAdminPref_MultipleAdminEnabled() {
724         setupSelectedUser();
725         ShadowUserManager.setIsMultipleAdminEnabled(true);
726         mUserManager.setUserRestriction(mUserInfo.getUserHandle(),
727                 UserManager.DISALLOW_GRANT_ADMIN, true);
728         mFragment.initialize(mActivity, mArguments);
729         verify(mFragment).removePreference(KEY_GRANT_ADMIN);
730     }
731 
732     @Ignore("b/313530297")
733     @Test
initialize_mainUserSelected_shouldShowGrantAdminPref_MultipleAdminEnabled()734     public void initialize_mainUserSelected_shouldShowGrantAdminPref_MultipleAdminEnabled() {
735         setupSelectedMainUser();
736         ShadowUserManager.setIsMultipleAdminEnabled(true);
737         mFragment.initialize(mActivity, mArguments);
738         verify(mFragment).removePreference(KEY_GRANT_ADMIN);
739     }
740 
741     @Ignore("b/313530297")
742     @Test
initialize_guestSelected_shouldNotShowGrantAdminPref_MultipleAdminEnabled()743     public void initialize_guestSelected_shouldNotShowGrantAdminPref_MultipleAdminEnabled() {
744         setupSelectedGuest();
745         ShadowUserManager.setIsMultipleAdminEnabled(true);
746         mFragment.initialize(mActivity, mArguments);
747         verify(mFragment).removePreference(KEY_GRANT_ADMIN);
748     }
749 
750     @Test
onPreferenceChange_grantAdminClicked_isNotAdmin_shouldLogGrantAdmin()751     public void onPreferenceChange_grantAdminClicked_isNotAdmin_shouldLogGrantAdmin() {
752         setupSelectedUser();
753         mFragment.mUserInfo = mUserInfo;
754         mFragment.mGrantAdminPref = mGrantAdminPref;
755         doNothing().when(mFragment).showDialog(anyInt());
756 
757         mFragment.onPreferenceChange(mGrantAdminPref, true);
758 
759         verify(mMetricsFeatureProvider).action(any(),
760                 eq(SettingsEnums.ACTION_GRANT_ADMIN_FROM_SETTINGS));
761     }
762 
763     @Test
onPreferenceChange_grantAdminClicked_isAdmin_shouldLogRevokeAdmin()764     public void onPreferenceChange_grantAdminClicked_isAdmin_shouldLogRevokeAdmin() {
765         setupSelectedAdminUser();
766         mFragment.mUserInfo = mUserInfo;
767         mFragment.mGrantAdminPref = mGrantAdminPref;
768         doNothing().when(mFragment).showDialog(anyInt());
769 
770         mFragment.onPreferenceChange(mGrantAdminPref, false);
771 
772         verify(mMetricsFeatureProvider).action(any(),
773                 eq(SettingsEnums.ACTION_REVOKE_ADMIN_FROM_SETTINGS));
774     }
775 
setupSelectedUser()776     private void setupSelectedUser() {
777         mArguments.putInt("user_id", 1);
778         mUserInfo = new UserInfo(1, "Tom", null,
779                 UserInfo.FLAG_FULL | UserInfo.FLAG_INITIALIZED,
780                 UserManager.USER_TYPE_FULL_SECONDARY);
781 
782         mUserManager.addProfile(mUserInfo);
783     }
784 
setupSelectedMainUser()785     private void setupSelectedMainUser() {
786         mArguments.putInt("user_id", 11);
787         mUserInfo = new UserInfo(11, "Jerry", null,
788                 UserInfo.FLAG_FULL | UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_MAIN,
789                 UserManager.USER_TYPE_FULL_SECONDARY);
790 
791         mUserManager.addProfile(mUserInfo);
792     }
793 
setupSelectedAdminUser()794     private void setupSelectedAdminUser() {
795         mArguments.putInt("user_id", 12);
796         mUserInfo = new UserInfo(12, "Andy", null,
797                 UserInfo.FLAG_FULL | UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_ADMIN,
798                 UserManager.USER_TYPE_FULL_SECONDARY);
799 
800         mUserManager.addProfile(mUserInfo);
801     }
802 
setupSelectedGuest()803     private void setupSelectedGuest() {
804         mArguments.putInt("user_id", 23);
805         mUserInfo = new UserInfo(23, "Guest", null,
806                 UserInfo.FLAG_FULL | UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_GUEST,
807                 UserManager.USER_TYPE_FULL_GUEST);
808 
809         mUserManager.addProfile(mUserInfo);
810     }
811 
setupSelectedRestrictedUser()812     private void setupSelectedRestrictedUser() {
813         mArguments.putInt("user_id", 21);
814         mUserInfo = new UserInfo(21, "Bob", null,
815                 UserInfo.FLAG_FULL | UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_RESTRICTED,
816                 UserManager.USER_TYPE_FULL_RESTRICTED);
817 
818         mUserManager.addProfile(mUserInfo);
819     }
820 }
821