1 /*
2  * Copyright (C) 2023 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.privatespace;
18 
19 import static android.provider.Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS;
20 import static android.provider.Settings.Secure.PRIVATE_SPACE_AUTO_LOCK;
21 
22 import static com.android.settings.privatespace.PrivateSpaceMaintainer.HIDE_PRIVATE_SPACE_ENTRY_POINT_DISABLED_VAL;
23 import static com.android.settings.privatespace.PrivateSpaceMaintainer.HIDE_PRIVATE_SPACE_ENTRY_POINT_ENABLED_VAL;
24 import static com.android.settings.privatespace.PrivateSpaceMaintainer.HIDE_PRIVATE_SPACE_SENSITIVE_NOTIFICATIONS_DISABLED_VAL;
25 import static com.android.settings.privatespace.PrivateSpaceMaintainer.PRIVATE_SPACE_CREATE_AUTO_LOCK_VAL;
26 
27 import static com.google.common.truth.Truth.assertThat;
28 
29 import static org.junit.Assume.assumeFalse;
30 import static org.junit.Assume.assumeTrue;
31 import static org.mockito.Mockito.spy;
32 import static org.mockito.Mockito.when;
33 
34 import android.app.ActivityManager;
35 import android.app.IActivityManager;
36 import android.content.ComponentName;
37 import android.content.ContentResolver;
38 import android.content.Context;
39 import android.content.pm.PackageManager;
40 import android.os.Flags;
41 import android.os.RemoteException;
42 import android.os.UserManager;
43 import android.platform.test.flag.junit.SetFlagsRule;
44 import android.provider.Settings;
45 
46 import androidx.test.core.app.ApplicationProvider;
47 import androidx.test.ext.junit.runners.AndroidJUnit4;
48 
49 import com.android.settings.privatespace.PrivateSpaceMaintainer.ErrorDeletingPrivateSpace;
50 
51 import org.junit.After;
52 import org.junit.Assert;
53 import org.junit.Before;
54 import org.junit.Rule;
55 import org.junit.Test;
56 import org.junit.runner.RunWith;
57 import org.mockito.MockitoAnnotations;
58 
59 @RunWith(AndroidJUnit4.class)
60 public class PrivateSpaceMaintainerTest {
61     private static final String TAG = "PSMaintainerTest";
62     private Context mContext;
63     private ContentResolver mContentResolver;
64     @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
65 
66     /** Required setup before a test. */
67     @Before
setUp()68     public void setUp() {
69         MockitoAnnotations.initMocks(this);
70         mContext = ApplicationProvider.getApplicationContext();
71         mContentResolver = mContext.getContentResolver();
72     }
73 
74     @After
tearDown()75     public void tearDown() {
76         PrivateSpaceMaintainer privateSpaceMaintainer =
77                 PrivateSpaceMaintainer.getInstance(mContext);
78         privateSpaceMaintainer.deletePrivateSpace();
79     }
80 
81     /** Tests that {@link PrivateSpaceMaintainer#deletePrivateSpace()} deletes PS when PS exists. */
82     @Test
deletePrivateSpace_psExists_deletesPS()83     public void deletePrivateSpace_psExists_deletesPS() {
84         mSetFlagsRule.enableFlags(
85                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
86         assumeTrue(mContext.getSystemService(UserManager.class).canAddPrivateProfile());
87         PrivateSpaceMaintainer privateSpaceMaintainer =
88                 PrivateSpaceMaintainer.getInstance(mContext);
89         privateSpaceMaintainer.createPrivateSpace();
90         ErrorDeletingPrivateSpace errorDeletingPrivateSpace =
91                 privateSpaceMaintainer.deletePrivateSpace();
92         assertThat(errorDeletingPrivateSpace)
93                 .isEqualTo(ErrorDeletingPrivateSpace.DELETE_PS_ERROR_NONE);
94         assertThat(privateSpaceMaintainer.doesPrivateSpaceExist()).isFalse();
95     }
96 
97     /**
98      * Tests that {@link PrivateSpaceMaintainer#deletePrivateSpace()} returns error when PS does not
99      * exist.
100      */
101     @Test
deletePrivateSpace_psDoesNotExist_returnsNoPSError()102     public void deletePrivateSpace_psDoesNotExist_returnsNoPSError() {
103         mSetFlagsRule.enableFlags(
104                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
105         PrivateSpaceMaintainer privateSpaceMaintainer =
106                 PrivateSpaceMaintainer.getInstance(mContext);
107         privateSpaceMaintainer.deletePrivateSpace();
108         ErrorDeletingPrivateSpace errorDeletingPrivateSpace =
109                 privateSpaceMaintainer.deletePrivateSpace();
110         assertThat(errorDeletingPrivateSpace)
111                 .isEqualTo(ErrorDeletingPrivateSpace.DELETE_PS_ERROR_NO_PRIVATE_SPACE);
112         assertThat(privateSpaceMaintainer.doesPrivateSpaceExist()).isFalse();
113     }
114 
115     /** Tests that {@link PrivateSpaceMaintainer#createPrivateSpace()} when PS exists creates PS. */
116     @Test
createPrivateSpace_psDoesNotExist_createsPS()117     public void createPrivateSpace_psDoesNotExist_createsPS() {
118         mSetFlagsRule.enableFlags(
119                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
120         assumeTrue(mContext.getSystemService(UserManager.class).canAddPrivateProfile());
121         PrivateSpaceMaintainer privateSpaceMaintainer =
122                 PrivateSpaceMaintainer.getInstance(mContext);
123         privateSpaceMaintainer.deletePrivateSpace();
124         assertThat(privateSpaceMaintainer.createPrivateSpace()).isTrue();
125         assertThat(privateSpaceMaintainer.doesPrivateSpaceExist()).isTrue();
126     }
127 
128     /**
129      * Tests that {@link PrivateSpaceMaintainer#createPrivateSpace()} when PS exists still returns
130      * true.
131      */
132     @Test
createPrivateSpace_psExists_returnsFalse()133     public void createPrivateSpace_psExists_returnsFalse() {
134         mSetFlagsRule.enableFlags(
135                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
136         assumeTrue(mContext.getSystemService(UserManager.class).canAddPrivateProfile());
137         PrivateSpaceMaintainer privateSpaceMaintainer =
138                 PrivateSpaceMaintainer.getInstance(mContext);
139         privateSpaceMaintainer.deletePrivateSpace();
140         assertThat(privateSpaceMaintainer.createPrivateSpace()).isTrue();
141         assertThat(privateSpaceMaintainer.doesPrivateSpaceExist()).isTrue();
142         assertThat(privateSpaceMaintainer.createPrivateSpace()).isTrue();
143     }
144 
145     /**
146      * Tests that {@link PrivateSpaceMaintainer#createPrivateSpace()} when no PS exists resets hide
147      * PS Settings.
148      */
149     @Test
createPrivateSpace_psDoesNotExist_resetsHidePSSettings()150     public void createPrivateSpace_psDoesNotExist_resetsHidePSSettings() {
151         mSetFlagsRule.enableFlags(
152                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
153         assumeTrue(mContext.getSystemService(UserManager.class).canAddPrivateProfile());
154         PrivateSpaceMaintainer privateSpaceMaintainer =
155                 PrivateSpaceMaintainer.getInstance(mContext);
156         Settings.Secure.putInt(
157                 mContentResolver,
158                 Settings.Secure.HIDE_PRIVATESPACE_ENTRY_POINT,
159                 HIDE_PRIVATE_SPACE_ENTRY_POINT_ENABLED_VAL);
160 
161         privateSpaceMaintainer.deletePrivateSpace();
162         privateSpaceMaintainer.createPrivateSpace();
163         assertThat(privateSpaceMaintainer.getHidePrivateSpaceEntryPointSetting())
164                 .isEqualTo(HIDE_PRIVATE_SPACE_ENTRY_POINT_DISABLED_VAL);
165     }
166 
167     /**
168      * Tests that {@link PrivateSpaceMaintainer#createPrivateSpace()} sets the PS sensitive
169      * notifications to hidden by default.
170      */
171     @Test
createPrivateSpace_psDoesNotExist_setsDefaultPsSensitiveNotificationsValue()172     public void createPrivateSpace_psDoesNotExist_setsDefaultPsSensitiveNotificationsValue() {
173         mSetFlagsRule.enableFlags(
174                 Flags.FLAG_ALLOW_PRIVATE_PROFILE,
175                 android.multiuser.Flags.FLAG_ENABLE_PS_SENSITIVE_NOTIFICATIONS_TOGGLE,
176                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
177         assumeTrue(mContext.getSystemService(UserManager.class).canAddPrivateProfile());
178         PrivateSpaceMaintainer privateSpaceMaintainer =
179                 PrivateSpaceMaintainer.getInstance(mContext);
180         privateSpaceMaintainer.deletePrivateSpace();
181         privateSpaceMaintainer.createPrivateSpace();
182         assertThat(privateSpaceMaintainer.doesPrivateSpaceExist()).isTrue();
183         assertThat(getPsSensitiveNotificationsValue(privateSpaceMaintainer))
184                 .isEqualTo(HIDE_PRIVATE_SPACE_SENSITIVE_NOTIFICATIONS_DISABLED_VAL);
185     }
186 
187     /**
188      * Tests that {@link PrivateSpaceMaintainer#createPrivateSpace()} when PS exist does not reset
189      * hide PS Settings.
190      */
191     @Test
createPrivateSpace_psExists_doesNotResetHidePSSettings()192     public void createPrivateSpace_psExists_doesNotResetHidePSSettings() {
193         mSetFlagsRule.enableFlags(
194                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
195         assumeTrue(mContext.getSystemService(UserManager.class).canAddPrivateProfile());
196         PrivateSpaceMaintainer privateSpaceMaintainer =
197                 PrivateSpaceMaintainer.getInstance(mContext);
198         privateSpaceMaintainer.createPrivateSpace();
199         Settings.Secure.putInt(
200                 mContentResolver,
201                 Settings.Secure.HIDE_PRIVATESPACE_ENTRY_POINT,
202                 HIDE_PRIVATE_SPACE_ENTRY_POINT_ENABLED_VAL);
203 
204         privateSpaceMaintainer.createPrivateSpace();
205         assertThat(privateSpaceMaintainer.getHidePrivateSpaceEntryPointSetting())
206                 .isEqualTo(HIDE_PRIVATE_SPACE_ENTRY_POINT_ENABLED_VAL);
207     }
208 
209     @Test
createPrivateSpace_psDoesNotExist_registersTheBroadcastReceiver()210     public void createPrivateSpace_psDoesNotExist_registersTheBroadcastReceiver() {
211         mSetFlagsRule.enableFlags(Flags.FLAG_ALLOW_PRIVATE_PROFILE,
212                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
213         PrivateSpaceMaintainer privateSpaceMaintainer =
214                 PrivateSpaceMaintainer.getInstance(mContext);
215         privateSpaceMaintainer.deletePrivateSpace();
216         privateSpaceMaintainer.createPrivateSpace();
217         // test that no exception is thrown, which would indicate that the receiver was registered.
218         mContext.unregisterReceiver(privateSpaceMaintainer.getProfileBroadcastReceiver());
219         privateSpaceMaintainer.resetBroadcastReceiver();
220     }
221 
222     @Test
deletePrivateSpace_psExists_unregistersTheBroadcastReceiver()223     public void deletePrivateSpace_psExists_unregistersTheBroadcastReceiver() {
224         mSetFlagsRule.enableFlags(Flags.FLAG_ALLOW_PRIVATE_PROFILE,
225                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
226         PrivateSpaceMaintainer privateSpaceMaintainer =
227                 PrivateSpaceMaintainer.getInstance(mContext);
228         privateSpaceMaintainer.createPrivateSpace();
229         privateSpaceMaintainer.deletePrivateSpace();
230         assertThat(privateSpaceMaintainer.getProfileBroadcastReceiver()).isNull();
231     }
232 
233     /**
234      * Tests that {@link PrivateSpaceMaintainer#lockPrivateSpace()} when PS exists and is running
235      * locks the private profile.
236      */
237     @Test
lockPrivateSpace_psExistsAndPrivateProfileRunning_locksCreatedPrivateSpace()238     public void lockPrivateSpace_psExistsAndPrivateProfileRunning_locksCreatedPrivateSpace() {
239         mSetFlagsRule.enableFlags(
240                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
241         assumeTrue(mContext.getSystemService(UserManager.class).canAddPrivateProfile());
242         PrivateSpaceMaintainer privateSpaceMaintainer =
243                 PrivateSpaceMaintainer.getInstance(mContext);
244         privateSpaceMaintainer.createPrivateSpace();
245         assertThat(privateSpaceMaintainer.doesPrivateSpaceExist()).isTrue();
246         assertThat(privateSpaceMaintainer.isPrivateProfileRunning()).isTrue();
247         assertThat(privateSpaceMaintainer.isPrivateSpaceLocked()).isFalse();
248         assertThat(privateSpaceMaintainer.lockPrivateSpace()).isTrue();
249         assertThat(privateSpaceMaintainer.isPrivateSpaceLocked()).isTrue();
250     }
251 
252     /**
253      * Tests that {@link PrivateSpaceMaintainer#lockPrivateSpace()} when PS exist and private
254      * profile not running returns false.
255      */
256     @Test
lockPrivateSpace_psExistsAndPrivateProfileNotRunning_returnsFalse()257     public void lockPrivateSpace_psExistsAndPrivateProfileNotRunning_returnsFalse() {
258         mSetFlagsRule.enableFlags(
259                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
260         assumeTrue(mContext.getSystemService(UserManager.class).canAddPrivateProfile());
261         PrivateSpaceMaintainer privateSpaceMaintainer =
262                 PrivateSpaceMaintainer.getInstance(mContext);
263         privateSpaceMaintainer.createPrivateSpace();
264         assertThat(privateSpaceMaintainer.doesPrivateSpaceExist()).isTrue();
265         assertThat(privateSpaceMaintainer.isPrivateProfileRunning()).isTrue();
266         IActivityManager am = ActivityManager.getService();
267         try {
268             am.stopProfile(privateSpaceMaintainer.getPrivateProfileHandle().getIdentifier());
269         } catch (RemoteException e) {
270             Assert.fail("Stop profile failed with exception " + e.getMessage());
271         }
272         assertThat(privateSpaceMaintainer.isPrivateProfileRunning()).isFalse();
273         assertThat(privateSpaceMaintainer.lockPrivateSpace()).isFalse();
274     }
275 
276     /**
277      * Tests that {@link PrivateSpaceMaintainer#lockPrivateSpace()} when no PS exists returns false.
278      */
279     @Test
lockPrivateSpace_psDoesNotExist_returnsFalse()280     public void lockPrivateSpace_psDoesNotExist_returnsFalse() {
281         mSetFlagsRule.enableFlags(
282                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
283         PrivateSpaceMaintainer privateSpaceMaintainer =
284                 PrivateSpaceMaintainer.getInstance(mContext);
285         assertThat(privateSpaceMaintainer.doesPrivateSpaceExist()).isFalse();
286         assertThat(privateSpaceMaintainer.lockPrivateSpace()).isFalse();
287     }
288 
289     /**
290      * Tests that {@link PrivateSpaceMaintainer#createPrivateSpace()} when no PS exists sets
291      * USER_SETUP_COMPLETE setting.
292      */
293     @Test
createPrivateSpace_psDoesNotExist_setsUserSetupComplete()294     public void createPrivateSpace_psDoesNotExist_setsUserSetupComplete() {
295         mSetFlagsRule.enableFlags(
296                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
297         assumeTrue(mContext.getSystemService(UserManager.class).canAddPrivateProfile());
298         PrivateSpaceMaintainer privateSpaceMaintainer =
299                 PrivateSpaceMaintainer.getInstance(mContext);
300         privateSpaceMaintainer.createPrivateSpace();
301         assertThat(getSecureUserSetupComplete()).isEqualTo(1);
302     }
303 
304     /**
305      * Tests that {@link PrivateSpaceMaintainer#createPrivateSpace()} when PS exists does not change
306      * USER_SETUP_COMPLETE setting.
307      */
308     @Test
createPrivateSpace_pSExists_doesNotChangeUserSetupSetting()309     public void createPrivateSpace_pSExists_doesNotChangeUserSetupSetting() {
310         mSetFlagsRule.enableFlags(
311                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
312         assumeTrue(mContext.getSystemService(UserManager.class).canAddPrivateProfile());
313         PrivateSpaceMaintainer privateSpaceMaintainer =
314                 PrivateSpaceMaintainer.getInstance(mContext);
315         privateSpaceMaintainer.createPrivateSpace();
316         assertThat(getSecureUserSetupComplete()).isEqualTo(1);
317         privateSpaceMaintainer.createPrivateSpace();
318         assertThat(getSecureUserSetupComplete()).isEqualTo(1);
319     }
320 
321     /**
322      * Tests that {@link PrivateSpaceMaintainer#createPrivateSpace()} when no PS exists resets PS
323      * auto lock Settings.
324      */
325     @Test
createPrivateSpace_psDoesNotExist_resetsPSAutoLockSettings()326     public void createPrivateSpace_psDoesNotExist_resetsPSAutoLockSettings() {
327         mSetFlagsRule.enableFlags(
328                 Flags.FLAG_ALLOW_PRIVATE_PROFILE,
329                 android.multiuser.Flags.FLAG_SUPPORT_AUTOLOCK_FOR_PRIVATE_SPACE,
330                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
331         assumeTrue(mContext.getSystemService(UserManager.class).canAddPrivateProfile());
332         final int autoLockOption = 2;
333         PrivateSpaceMaintainer privateSpaceMaintainer =
334                 PrivateSpaceMaintainer.getInstance(mContext);
335         Settings.Secure.putInt(
336                 mContentResolver, Settings.Secure.PRIVATE_SPACE_AUTO_LOCK, autoLockOption);
337 
338         privateSpaceMaintainer.deletePrivateSpace();
339         privateSpaceMaintainer.createPrivateSpace();
340         assertThat(privateSpaceMaintainer.getPrivateSpaceAutoLockSetting())
341                 .isEqualTo(PRIVATE_SPACE_CREATE_AUTO_LOCK_VAL);
342         assertThat(Settings.Secure.getInt(mContentResolver, PRIVATE_SPACE_AUTO_LOCK, -1))
343                 .isEqualTo(PRIVATE_SPACE_CREATE_AUTO_LOCK_VAL);
344     }
345 
346     /**
347      * Tests that {@link PrivateSpaceMaintainer#createPrivateSpace()} when PS exist does not reset
348      * PS auto lock setting.
349      */
350     @Test
createPrivateSpace_psExists_doesNotResetPSAutoLockSettings()351     public void createPrivateSpace_psExists_doesNotResetPSAutoLockSettings() {
352         mSetFlagsRule.enableFlags(
353                 Flags.FLAG_ALLOW_PRIVATE_PROFILE,
354                 android.multiuser.Flags.FLAG_SUPPORT_AUTOLOCK_FOR_PRIVATE_SPACE,
355                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
356         assumeTrue(mContext.getSystemService(UserManager.class).canAddPrivateProfile());
357         final int privateSpaceAutLockValue = 1;
358         PrivateSpaceMaintainer privateSpaceMaintainer =
359                 PrivateSpaceMaintainer.getInstance(mContext);
360         privateSpaceMaintainer.createPrivateSpace();
361         Settings.Secure.putInt(
362                 mContentResolver,
363                 Settings.Secure.PRIVATE_SPACE_AUTO_LOCK,
364                 privateSpaceAutLockValue);
365 
366         privateSpaceMaintainer.createPrivateSpace();
367         assertThat(privateSpaceMaintainer.getPrivateSpaceAutoLockSetting())
368                 .isEqualTo(privateSpaceAutLockValue);
369     }
370 
371     @Test
isPrivateSpaceEntryPointEnabled_psExistCanAddProfileTrue_returnsTrue()372     public void isPrivateSpaceEntryPointEnabled_psExistCanAddProfileTrue_returnsTrue() {
373         mSetFlagsRule.enableFlags(
374                 Flags.FLAG_ALLOW_PRIVATE_PROFILE,
375                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
376         assumeTrue(mContext.getSystemService(UserManager.class).canAddPrivateProfile());
377         PrivateSpaceMaintainer privateSpaceMaintainer =
378                 PrivateSpaceMaintainer.getInstance(mContext);
379         privateSpaceMaintainer.createPrivateSpace();
380         assertThat(privateSpaceMaintainer.doesPrivateSpaceExist()).isTrue();
381 
382         assertThat(privateSpaceMaintainer.isPrivateSpaceEntryPointEnabled()).isTrue();
383     }
384 
385     @Test
isPrivateSpaceEntryPointEnabled_psNotExistsCanAddProfileTrue_returnsTrue()386     public void isPrivateSpaceEntryPointEnabled_psNotExistsCanAddProfileTrue_returnsTrue() {
387         mSetFlagsRule.enableFlags(
388                 Flags.FLAG_ALLOW_PRIVATE_PROFILE,
389                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
390         assumeTrue(mContext.getSystemService(UserManager.class).canAddPrivateProfile());
391         PrivateSpaceMaintainer privateSpaceMaintainer =
392                 PrivateSpaceMaintainer.getInstance(mContext);
393         privateSpaceMaintainer.deletePrivateSpace();
394         assertThat(privateSpaceMaintainer.doesPrivateSpaceExist()).isFalse();
395 
396         assertThat(privateSpaceMaintainer.isPrivateSpaceEntryPointEnabled()).isTrue();
397     }
398 
399     @Test
isPrivateSpaceEntryPointEnabled_psExistsCanAddProfileFalse_returnsTrue()400     public void isPrivateSpaceEntryPointEnabled_psExistsCanAddProfileFalse_returnsTrue() {
401         mSetFlagsRule.enableFlags(
402                 Flags.FLAG_ALLOW_PRIVATE_PROFILE,
403                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
404         assumeFalse(mContext.getSystemService(UserManager.class).canAddPrivateProfile());
405         PrivateSpaceMaintainer privateSpaceMaintainer =
406                 spy(PrivateSpaceMaintainer.getInstance(mContext));
407         when(privateSpaceMaintainer.doesPrivateSpaceExist()).thenReturn(true);
408 
409         assertThat(privateSpaceMaintainer.isPrivateSpaceEntryPointEnabled()).isTrue();
410     }
411 
412     @Test
isPrivateSpaceEntryPointEnabled_psNotExistsCanAddProfileFalse_returnsFalse()413     public void isPrivateSpaceEntryPointEnabled_psNotExistsCanAddProfileFalse_returnsFalse() {
414         mSetFlagsRule.enableFlags(
415                 Flags.FLAG_ALLOW_PRIVATE_PROFILE,
416                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
417         assumeFalse(mContext.getSystemService(UserManager.class).canAddPrivateProfile());
418         PrivateSpaceMaintainer privateSpaceMaintainer =
419                 PrivateSpaceMaintainer.getInstance(mContext);
420         privateSpaceMaintainer.deletePrivateSpace();
421         assertThat(privateSpaceMaintainer.doesPrivateSpaceExist()).isFalse();
422 
423         assertThat(privateSpaceMaintainer.isPrivateSpaceEntryPointEnabled()).isFalse();
424     }
425 
426     @Test
createPrivateSpace_psDoesNotExist_setsSkipFirstUseHints()427     public void createPrivateSpace_psDoesNotExist_setsSkipFirstUseHints() {
428         mSetFlagsRule.enableFlags(
429                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
430         assumeTrue(mContext.getSystemService(UserManager.class).canAddPrivateProfile());
431         PrivateSpaceMaintainer privateSpaceMaintainer =
432                 PrivateSpaceMaintainer.getInstance(mContext);
433         privateSpaceMaintainer.createPrivateSpace();
434         assertThat(getSecureSkipFirstUseHints()).isEqualTo(1);
435     }
436 
437     @Test
createPrivateSpace_psDoesNotExist_setsPrivateSpaceSettingsComponentDisabled()438     public void createPrivateSpace_psDoesNotExist_setsPrivateSpaceSettingsComponentDisabled() {
439         mSetFlagsRule.enableFlags(
440                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
441         assumeTrue(mContext.getSystemService(UserManager.class).canAddPrivateProfile());
442         PrivateSpaceMaintainer privateSpaceMaintainer =
443                 PrivateSpaceMaintainer.getInstance(mContext);
444         privateSpaceMaintainer.createPrivateSpace();
445         assertThat(privateSpaceMaintainer.getPrivateProfileHandle()).isNotNull();
446         Context privateSpaceUserContext = mContext.createContextAsUser(
447                 privateSpaceMaintainer.getPrivateProfileHandle(),
448                 /* flags */ 0);
449 
450         // Assert that private space settings launcher app icon is disabled
451         ComponentName settingsComponentName = new ComponentName(privateSpaceUserContext,
452                 com.android.settings.Settings.class);
453         int settingsComponentEnabledSetting = privateSpaceUserContext.getPackageManager()
454                 .getComponentEnabledSetting(settingsComponentName);
455         assertThat(settingsComponentEnabledSetting)
456                 .isEqualTo(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
457 
458         // Assert that private space settings create shortcut activity is disabled
459         ComponentName shortcutPickerComponentName = new ComponentName(privateSpaceUserContext,
460                 com.android.settings.Settings.CreateShortcutActivity.class);
461         int settingsShortcutPickerEnabledSetting = privateSpaceUserContext.getPackageManager()
462                 .getComponentEnabledSetting(shortcutPickerComponentName);
463         assertThat(settingsShortcutPickerEnabledSetting)
464                 .isEqualTo(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
465     }
466 
467     @Test
createPrivateSpace_pSExists_doesNotChangeSkipFirstUseHints()468     public void createPrivateSpace_pSExists_doesNotChangeSkipFirstUseHints() {
469         mSetFlagsRule.enableFlags(
470                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
471         assumeTrue(mContext.getSystemService(UserManager.class).canAddPrivateProfile());
472         PrivateSpaceMaintainer privateSpaceMaintainer =
473                 PrivateSpaceMaintainer.getInstance(mContext);
474         privateSpaceMaintainer.createPrivateSpace();
475         assertThat(getSecureSkipFirstUseHints()).isEqualTo(1);
476         privateSpaceMaintainer.createPrivateSpace();
477         assertThat(getSecureSkipFirstUseHints()).isEqualTo(1);
478     }
479 
getSecureUserSetupComplete()480     private int getSecureUserSetupComplete() {
481         PrivateSpaceMaintainer privateSpaceMaintainer =
482                 PrivateSpaceMaintainer.getInstance(mContext);
483         return Settings.Secure.getIntForUser(
484                 mContentResolver,
485                 Settings.Secure.USER_SETUP_COMPLETE,
486                 0,
487                 privateSpaceMaintainer.getPrivateProfileHandle().getIdentifier());
488     }
489 
getSecureSkipFirstUseHints()490     private int getSecureSkipFirstUseHints() {
491         PrivateSpaceMaintainer privateSpaceMaintainer =
492                 PrivateSpaceMaintainer.getInstance(mContext);
493         return Settings.Secure.getIntForUser(
494                 mContentResolver,
495                 Settings.Secure.SKIP_FIRST_USE_HINTS,
496                 0,
497                 privateSpaceMaintainer.getPrivateProfileHandle().getIdentifier());
498     }
499 
getPsSensitiveNotificationsValue(PrivateSpaceMaintainer privateSpaceMaintainer)500     private int getPsSensitiveNotificationsValue(PrivateSpaceMaintainer privateSpaceMaintainer) {
501         return Settings.Secure.getIntForUser(mContentResolver,
502                 LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS,
503                 /* enabled */ 1,
504                 privateSpaceMaintainer.getPrivateProfileHandle().getIdentifier());
505     }
506 }
507