1 /*
2  * Copyright (C) 2022 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 android.safetycenter.functional.multiusers
18 
19 import android.Manifest.permission.INTERACT_ACROSS_USERS
20 import android.Manifest.permission.INTERACT_ACROSS_USERS_FULL
21 import android.app.PendingIntent
22 import android.content.Context
23 import android.os.UserHandle
24 import android.platform.test.annotations.RequiresFlagsDisabled
25 import android.platform.test.annotations.RequiresFlagsEnabled
26 import android.platform.test.flag.junit.DeviceFlagsValueProvider
27 import android.safetycenter.SafetyCenterData
28 import android.safetycenter.SafetyCenterEntry
29 import android.safetycenter.SafetyCenterEntry.ENTRY_SEVERITY_LEVEL_CRITICAL_WARNING
30 import android.safetycenter.SafetyCenterEntry.ENTRY_SEVERITY_LEVEL_UNKNOWN
31 import android.safetycenter.SafetyCenterEntry.ENTRY_SEVERITY_LEVEL_UNSPECIFIED
32 import android.safetycenter.SafetyCenterEntry.SEVERITY_UNSPECIFIED_ICON_TYPE_NO_RECOMMENDATION
33 import android.safetycenter.SafetyCenterEntry.SEVERITY_UNSPECIFIED_ICON_TYPE_PRIVACY
34 import android.safetycenter.SafetyCenterEntryGroup
35 import android.safetycenter.SafetyCenterEntryOrGroup
36 import android.safetycenter.SafetyCenterIssue
37 import android.safetycenter.SafetyCenterManager
38 import android.safetycenter.SafetyCenterStaticEntry
39 import android.safetycenter.SafetyCenterStaticEntryGroup
40 import android.safetycenter.SafetyEvent
41 import android.safetycenter.SafetySourceData
42 import androidx.test.core.app.ApplicationProvider
43 import androidx.test.filters.LargeTest
44 import com.android.bedstead.harrier.BedsteadJUnit4
45 import com.android.bedstead.harrier.DeviceState
46 import com.android.bedstead.harrier.annotations.EnsureHasAdditionalUser
47 import com.android.bedstead.harrier.annotations.EnsureHasCloneProfile
48 import com.android.bedstead.enterprise.annotations.EnsureHasNoWorkProfile
49 import com.android.bedstead.harrier.annotations.EnsureHasPrivateProfile
50 import com.android.bedstead.harrier.annotations.EnsureHasNoPrivateProfile
51 import com.android.bedstead.enterprise.annotations.EnsureHasWorkProfile
52 import com.android.bedstead.enterprise.annotations.EnsureHasDeviceOwner
53 import com.android.bedstead.enterprise.annotations.EnsureHasNoDeviceOwner
54 import com.android.bedstead.nene.TestApis
55 import com.android.bedstead.nene.types.OptionalBoolean.TRUE
56 import com.android.compatibility.common.util.DisableAnimationRule
57 import com.android.compatibility.common.util.FreezeRotationRule
58 import com.android.safetycenter.resources.SafetyCenterResourcesApk
59 import com.android.safetycenter.testing.Coroutines.TIMEOUT_SHORT
60 import com.android.safetycenter.testing.NotificationCharacteristics
61 import com.android.safetycenter.testing.SafetyCenterActivityLauncher.launchSafetyCenterActivity
62 import com.android.safetycenter.testing.SafetyCenterApisWithShellPermissions.getSafetyCenterDataWithPermission
63 import com.android.safetycenter.testing.SafetyCenterApisWithShellPermissions.getSafetySourceDataWithPermission
64 import com.android.safetycenter.testing.SafetyCenterApisWithShellPermissions.setSafetySourceDataWithPermission
65 import com.android.safetycenter.testing.SafetyCenterFlags
66 import com.android.safetycenter.testing.SafetyCenterTestConfigs
67 import com.android.safetycenter.testing.SafetyCenterTestConfigs.Companion.DYNAMIC_BAREBONE_ID
68 import com.android.safetycenter.testing.SafetyCenterTestConfigs.Companion.DYNAMIC_DISABLED_ID
69 import com.android.safetycenter.testing.SafetyCenterTestConfigs.Companion.DYNAMIC_GROUP_ID
70 import com.android.safetycenter.testing.SafetyCenterTestConfigs.Companion.DYNAMIC_HIDDEN_ID
71 import com.android.safetycenter.testing.SafetyCenterTestConfigs.Companion.DYNAMIC_IN_STATELESS_ID
72 import com.android.safetycenter.testing.SafetyCenterTestConfigs.Companion.ISSUE_ONLY_ALL_OPTIONAL_ID
73 import com.android.safetycenter.testing.SafetyCenterTestConfigs.Companion.ISSUE_ONLY_ALL_PROFILE_SOURCE_ID
74 import com.android.safetycenter.testing.SafetyCenterTestConfigs.Companion.ISSUE_ONLY_BAREBONE_ID
75 import com.android.safetycenter.testing.SafetyCenterTestConfigs.Companion.ISSUE_ONLY_GROUP_ID
76 import com.android.safetycenter.testing.SafetyCenterTestConfigs.Companion.ISSUE_ONLY_IN_STATELESS_ID
77 import com.android.safetycenter.testing.SafetyCenterTestConfigs.Companion.MIXED_STATELESS_GROUP_ID
78 import com.android.safetycenter.testing.SafetyCenterTestConfigs.Companion.SINGLE_SOURCE_ALL_PROFILE_ID
79 import com.android.safetycenter.testing.SafetyCenterTestConfigs.Companion.SINGLE_SOURCE_GROUP_ID
80 import com.android.safetycenter.testing.SafetyCenterTestConfigs.Companion.SINGLE_SOURCE_ID
81 import com.android.safetycenter.testing.SafetyCenterTestConfigs.Companion.STATIC_ALL_OPTIONAL_ID
82 import com.android.safetycenter.testing.SafetyCenterTestConfigs.Companion.STATIC_BAREBONE_ID
83 import com.android.safetycenter.testing.SafetyCenterTestConfigs.Companion.STATIC_GROUP_ID
84 import com.android.safetycenter.testing.SafetyCenterTestData
85 import com.android.safetycenter.testing.SafetyCenterTestData.Companion.withoutExtras
86 import com.android.safetycenter.testing.SafetyCenterTestHelper
87 import com.android.safetycenter.testing.SafetyCenterTestRule
88 import com.android.safetycenter.testing.SafetySourceTestData
89 import com.android.safetycenter.testing.SafetySourceTestData.Companion.EVENT_SOURCE_STATE_CHANGED
90 import com.android.safetycenter.testing.SafetySourceTestData.Companion.ISSUE_TYPE_ID
91 import com.android.safetycenter.testing.ShellPermissions.callWithShellPermissionIdentity
92 import com.android.safetycenter.testing.SupportsSafetyCenterRule
93 import com.android.safetycenter.testing.TestNotificationListener
94 import com.android.safetycenter.testing.UiTestHelper.waitAllTextDisplayed
95 import com.android.safetycenter.testing.UiTestHelper.waitAllTextNotDisplayed
96 import com.google.common.base.Preconditions.checkState
97 import com.google.common.truth.Truth.assertThat
98 import kotlin.test.assertFailsWith
99 import org.junit.After
100 import org.junit.Before
101 import org.junit.ClassRule
102 import org.junit.Rule
103 import org.junit.Test
104 import org.junit.runner.RunWith
105 
106 /**
107  * Functional tests for our APIs and UI on a device with multiple users. e.g. with a managed or
108  * additional user(s).
109  */
110 @LargeTest
111 @RunWith(BedsteadJUnit4::class)
112 class SafetyCenterMultiUsersTest {
113 
114     companion object {
115         @JvmField @ClassRule @Rule val deviceState: DeviceState = DeviceState()
116     }
117 
118     @Rule @JvmField val mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule()
119 
120     private val context: Context = ApplicationProvider.getApplicationContext()
121     private val safetyCenterResourcesApk = SafetyCenterResourcesApk.forTests(context)
122     private val safetyCenterTestHelper = SafetyCenterTestHelper(context)
123     private val safetySourceTestData = SafetySourceTestData(context)
124     private val safetyCenterTestData = SafetyCenterTestData(context)
125     private val safetyCenterTestConfigs = SafetyCenterTestConfigs(context)
126     private val safetyCenterManager = context.getSystemService(SafetyCenterManager::class.java)!!
127 
128     private var inQuietMode = false
129 
130     private val primaryProfileOnlyIssues: List<SafetyCenterIssue>
131         get() =
132             listOf(
133                 safetyCenterTestData.safetyCenterIssueCritical(
134                     DYNAMIC_BAREBONE_ID,
135                     groupId = DYNAMIC_GROUP_ID
136                 ),
137                 safetyCenterTestData.safetyCenterIssueCritical(
138                     ISSUE_ONLY_BAREBONE_ID,
139                     attributionTitle = null,
140                     groupId = ISSUE_ONLY_GROUP_ID
141                 ),
142                 safetyCenterTestData.safetyCenterIssueRecommendation(
143                     DYNAMIC_DISABLED_ID,
144                     groupId = DYNAMIC_GROUP_ID
145                 ),
146                 safetyCenterTestData.safetyCenterIssueRecommendation(
147                     ISSUE_ONLY_ALL_OPTIONAL_ID,
148                     attributionTitle = null,
149                     groupId = ISSUE_ONLY_GROUP_ID
150                 ),
151                 safetyCenterTestData.safetyCenterIssueInformation(
152                     DYNAMIC_IN_STATELESS_ID,
153                     groupId = MIXED_STATELESS_GROUP_ID
154                 ),
155                 safetyCenterTestData.safetyCenterIssueInformation(
156                     ISSUE_ONLY_IN_STATELESS_ID,
157                     groupId = MIXED_STATELESS_GROUP_ID
158                 )
159             )
160 
161     private val dynamicBareboneDefault: SafetyCenterEntry
162         get() = safetyCenterTestData.safetyCenterEntryDefault(DYNAMIC_BAREBONE_ID)
163 
164     private val dynamicBareboneUpdated: SafetyCenterEntry
165         get() = safetyCenterTestData.safetyCenterEntryCritical(DYNAMIC_BAREBONE_ID)
166 
167     private val dynamicDisabledDefault: SafetyCenterEntry
168         get() =
169             safetyCenterTestData
170                 .safetyCenterEntryDefaultBuilder(DYNAMIC_DISABLED_ID)
171                 .setPendingIntent(null)
172                 .setEnabled(false)
173                 .build()
174 
175     private val dynamicDisabledUpdated: SafetyCenterEntry
176         get() = safetyCenterTestData.safetyCenterEntryRecommendation(DYNAMIC_DISABLED_ID)
177 
178     private val dynamicDisabledForWorkDefaultBuilder: SafetyCenterEntry.Builder
179         get() =
180             safetyCenterTestData
181                 .safetyCenterEntryDefaultBuilder(
182                     DYNAMIC_DISABLED_ID,
183                     userId = deviceState.workProfile().id(),
184                     title = "Paste"
185                 )
186                 .setPendingIntent(null)
187                 .setEnabled(false)
188 
189     private val dynamicDisabledForWorkDefault: SafetyCenterEntry
190         get() = dynamicDisabledForWorkDefaultBuilder.build()
191 
192     private val dynamicDisabledForWorkPausedUpdated: SafetyCenterEntry
193         get() =
194             safetyCenterTestData
195                 .safetyCenterEntryDefaultBuilder(
196                     DYNAMIC_DISABLED_ID,
197                     deviceState.workProfile().id(),
198                     title = "Ok title for Work",
199                     pendingIntent = null
200                 )
201                 .setSummary(
202                     safetyCenterResourcesApk.getStringByName("work_profile_paused"),
203                 )
204                 .setSeverityLevel(ENTRY_SEVERITY_LEVEL_UNSPECIFIED)
205                 .setEnabled(false)
206                 .build()
207 
208     private val dynamicDisabledForWorkUpdated: SafetyCenterEntry
209         get() = safetyCenterEntryOkForWork(DYNAMIC_DISABLED_ID, deviceState.workProfile().id())
210 
211     private val dynamicHiddenUpdated: SafetyCenterEntry
212         get() =
213             safetyCenterTestData.safetyCenterEntryUnspecified(
214                 DYNAMIC_HIDDEN_ID,
215                 pendingIntent = null
216             )
217 
218     private val dynamicHiddenForWorkUpdated: SafetyCenterEntry
219         get() = safetyCenterEntryOkForWork(DYNAMIC_HIDDEN_ID, deviceState.workProfile().id())
220 
221     private val dynamicHiddenForWorkPausedUpdated
222         get() =
223             safetyCenterTestData
224                 .safetyCenterEntryDefaultBuilder(
225                     DYNAMIC_HIDDEN_ID,
226                     deviceState.workProfile().id(),
227                     title = "Ok title for Work",
228                     pendingIntent = null
229                 )
230                 .setSummary(
231                     safetyCenterResourcesApk.getStringByName("work_profile_paused"),
232                 )
233                 .setSeverityLevel(ENTRY_SEVERITY_LEVEL_UNSPECIFIED)
234                 .setEnabled(false)
235                 .build()
236 
237     private val dynamicDisabledForPrivateUpdated: SafetyCenterEntry
238         get() =
239             safetyCenterEntryOkForPrivate(DYNAMIC_DISABLED_ID, deviceState.privateProfile().id())
240 
241     private val dynamicHiddenForPrivateUpdated: SafetyCenterEntry
242         get() = safetyCenterEntryOkForPrivate(DYNAMIC_HIDDEN_ID, deviceState.privateProfile().id())
243 
244     private val staticGroupBuilder =
245         SafetyCenterEntryGroup.Builder(STATIC_GROUP_ID, "OK")
246             .setSeverityLevel(ENTRY_SEVERITY_LEVEL_UNSPECIFIED)
247             .setSeverityUnspecifiedIconType(SEVERITY_UNSPECIFIED_ICON_TYPE_PRIVACY)
248             .setSummary("OK")
249 
250     private val staticBarebone =
251         safetyCenterTestData
252             .safetyCenterEntryDefaultStaticBuilder(STATIC_BAREBONE_ID)
253             .setSummary(null)
254             .build()
255 
256     private val staticAllOptional =
257         safetyCenterTestData.safetyCenterEntryDefaultStaticBuilder(STATIC_ALL_OPTIONAL_ID).build()
258 
259     private val staticAllOptionalForWorkBuilder
260         get() =
261             safetyCenterTestData
262                 .safetyCenterEntryDefaultStaticBuilder(
263                     STATIC_ALL_OPTIONAL_ID,
264                     userId = deviceState.workProfile().id(),
265                     title = "Paste"
266                 )
267                 .setPendingIntent(
268                     createTestActivityRedirectPendingIntentForUser(
269                         deviceState.workProfile().userHandle(),
270                         explicit = false
271                     )
272                 )
273 
274     private val staticAllOptionalForWork
275         get() = staticAllOptionalForWorkBuilder.build()
276 
277     private val staticAllOptionalForWorkPaused
278         get() =
279             staticAllOptionalForWorkBuilder
280                 .setSummary(safetyCenterResourcesApk.getStringByName("work_profile_paused"))
281                 .setEnabled(false)
282                 .build()
283 
284     private val staticAllOptionalForPrivateBuilder
285         get() =
286             safetyCenterTestData
287                 .safetyCenterEntryDefaultStaticBuilder(
288                     STATIC_ALL_OPTIONAL_ID,
289                     userId = deviceState.privateProfile().id(),
290                     title = "Unknown"
291                 )
292                 .setPendingIntent(
293                     createTestActivityRedirectPendingIntentForUser(
294                         deviceState.privateProfile().userHandle(),
295                         explicit = false
296                     )
297                 )
298 
299     private val staticAllOptionalForPrivate
300         get() = staticAllOptionalForPrivateBuilder.build()
301 
createStaticEntrynull302     private fun createStaticEntry(explicit: Boolean = true): SafetyCenterStaticEntry =
303         SafetyCenterStaticEntry.Builder("OK")
304             .setSummary("OK")
305             .setPendingIntent(
306                 safetySourceTestData.createTestActivityRedirectPendingIntent(explicit)
307             )
308             .build()
309 
310     private val staticEntryUpdated: SafetyCenterStaticEntry
311         get() =
312             SafetyCenterStaticEntry.Builder("Unspecified title")
313                 .setSummary("Unspecified summary")
314                 .setPendingIntent(safetySourceTestData.createTestActivityRedirectPendingIntent())
315                 .build()
316 
317     private fun staticEntryForWorkBuilder(title: CharSequence = "Paste", explicit: Boolean = true) =
318         SafetyCenterStaticEntry.Builder(title)
319             .setSummary("OK")
320             .setPendingIntent(
321                 createTestActivityRedirectPendingIntentForUser(
322                     deviceState.workProfile().userHandle(),
323                     explicit
324                 )
325             )
326 
327     private fun staticEntryForPrivateBuilder(
328         title: CharSequence = "Unknown",
329         explicit: Boolean = true
330     ) =
331         SafetyCenterStaticEntry.Builder(title)
332             .setSummary("OK")
333             .setPendingIntent(
334                 createTestActivityRedirectPendingIntentForUser(
335                     deviceState.privateProfile().userHandle(),
336                     explicit
337                 )
338             )
339 
340     private fun createStaticEntryForWork(explicit: Boolean = true): SafetyCenterStaticEntry =
341         staticEntryForWorkBuilder(explicit = explicit).build()
342 
343     private fun createStaticEntryForPrivate(explicit: Boolean = true): SafetyCenterStaticEntry =
344         staticEntryForPrivateBuilder(explicit = explicit).build()
345 
346     private fun createStaticEntryForWorkPaused(): SafetyCenterStaticEntry =
347         staticEntryForWorkBuilder(explicit = false)
348             .setSummary(safetyCenterResourcesApk.getStringByName("work_profile_paused"))
349             .build()
350 
351     private val staticEntryForWorkPausedUpdated: SafetyCenterStaticEntry
352         get() =
353             staticEntryForWorkBuilder(title = "Unspecified title for Work")
354                 .setSummary(safetyCenterResourcesApk.getStringByName("work_profile_paused"))
355                 .build()
356 
357     private val staticEntryForWorkUpdated: SafetyCenterStaticEntry
358         get() =
359             SafetyCenterStaticEntry.Builder("Unspecified title for Work")
360                 .setSummary("Unspecified summary")
361                 .setPendingIntent(safetySourceTestData.createTestActivityRedirectPendingIntent())
362                 .build()
363 
364     private val staticEntryForPrivateUpdated: SafetyCenterStaticEntry
365         get() =
366             SafetyCenterStaticEntry.Builder("Unspecified title for Private")
367                 .setSummary("Unspecified summary")
368                 .setPendingIntent(safetySourceTestData.createTestActivityRedirectPendingIntent())
369                 .build()
370 
371     private val safetyCenterDataForAdditionalUser
372         get() =
373             SafetyCenterData(
374                 safetyCenterTestData.safetyCenterStatusUnknown,
375                 emptyList(),
376                 listOf(
377                     SafetyCenterEntryOrGroup(
378                         safetyCenterTestData.safetyCenterEntryDefault(
379                             SINGLE_SOURCE_ALL_PROFILE_ID,
380                             deviceState.additionalUser().id(),
381                             pendingIntent =
382                                 createTestActivityRedirectPendingIntentForUser(
383                                     deviceState.additionalUser().userHandle()
384                                 )
385                         )
386                     )
387                 ),
388                 emptyList()
389             )
390 
391     @get:Rule(order = 1) val supportsSafetyCenterRule = SupportsSafetyCenterRule(context)
392     @get:Rule(order = 2)
393     val safetyCenterTestRule =
394         SafetyCenterTestRule(safetyCenterTestHelper, withNotifications = true)
395     @get:Rule(order = 3) val disableAnimationRule = DisableAnimationRule()
396     @get:Rule(order = 4) val freezeRotationRule = FreezeRotationRule()
397 
398     @Before
399     fun setRefreshTimeoutsBeforeTest() {
400         SafetyCenterFlags.setAllRefreshTimeoutsTo(TIMEOUT_SHORT)
401     }
402 
403     @After
resetQuietModeAfterTestnull404     fun resetQuietModeAfterTest() {
405         setQuietMode(false)
406     }
407 
408     @Test
409     @EnsureHasWorkProfile
getSafetyCenterData_withProfileOwner_hasWorkPolicyInfonull410     fun getSafetyCenterData_withProfileOwner_hasWorkPolicyInfo() {
411         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.workPolicyInfoConfig)
412 
413         findWorkPolicyInfo()
414     }
415 
416     @Test
417     @EnsureHasDeviceOwner
getSafetyCenterData_withDeviceOwner_hasWorkPolicyInfonull418     fun getSafetyCenterData_withDeviceOwner_hasWorkPolicyInfo() {
419         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.workPolicyInfoConfig)
420 
421         findWorkPolicyInfo()
422     }
423 
424     @Test
425     @EnsureHasWorkProfile
launchActivity_withQuietModeEnabled_hasWorkPolicyInfonull426     fun launchActivity_withQuietModeEnabled_hasWorkPolicyInfo() {
427         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.workPolicyInfoConfig)
428 
429         setQuietMode(true)
430 
431         findWorkPolicyInfo()
432     }
433 
434     @Test
435     @EnsureHasNoWorkProfile
436     @EnsureHasNoDeviceOwner
launchActivity_withoutWorkProfileOrDeviceOwner_doesntHaveWorkPolicyInfonull437     fun launchActivity_withoutWorkProfileOrDeviceOwner_doesntHaveWorkPolicyInfo() {
438         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.workPolicyInfoConfig)
439 
440         context.launchSafetyCenterActivity { waitAllTextNotDisplayed("Your work policy info") }
441     }
442 
443     @Test
444     @EnsureHasWorkProfile(installInstrumentedApp = TRUE)
getSafetySourceData_withQuietModeEnabled_dataIsNotClearednull445     fun getSafetySourceData_withQuietModeEnabled_dataIsNotCleared() {
446         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.singleSourceAllProfileConfig)
447         val dataForWork = safetySourceTestData.informationWithIssueForWork
448         val managedSafetyCenterManager =
449             getSafetyCenterManagerForUser(deviceState.workProfile().userHandle())
450         managedSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
451             SINGLE_SOURCE_ALL_PROFILE_ID,
452             dataForWork
453         )
454 
455         setQuietMode(true)
456         val apiSafetySourceDataForWork =
457             managedSafetyCenterManager.getSafetySourceDataWithInteractAcrossUsersPermission(
458                 SINGLE_SOURCE_ALL_PROFILE_ID
459             )
460 
461         assertThat(apiSafetySourceDataForWork).isEqualTo(dataForWork)
462     }
463 
464     @Test
465     @EnsureHasAdditionalUser(installInstrumentedApp = TRUE)
getSafetySourceData_afterAdditionalUserRemoved_returnsNullnull466     fun getSafetySourceData_afterAdditionalUserRemoved_returnsNull() {
467         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.singleSourceAllProfileConfig)
468         val additionalUserSafetyCenterManager =
469             getSafetyCenterManagerForUser(deviceState.additionalUser().userHandle())
470         val dataForAdditionalUser = safetySourceTestData.information
471         additionalUserSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
472             SINGLE_SOURCE_ALL_PROFILE_ID,
473             dataForAdditionalUser
474         )
475         checkState(
476             additionalUserSafetyCenterManager.getSafetySourceDataWithInteractAcrossUsersPermission(
477                 SINGLE_SOURCE_ALL_PROFILE_ID
478             ) == dataForAdditionalUser
479         )
480 
481         deviceState.additionalUser().remove()
482 
483         assertThat(
484                 additionalUserSafetyCenterManager
485                     .getSafetySourceDataWithInteractAcrossUsersPermission(
486                         SINGLE_SOURCE_ALL_PROFILE_ID
487                     )
488             )
489             .isNull()
490     }
491 
492     @Test
493     @EnsureHasWorkProfile(installInstrumentedApp = TRUE)
getSafetySourceData_withoutInteractAcrossUserPermission_shouldThrowErrornull494     fun getSafetySourceData_withoutInteractAcrossUserPermission_shouldThrowError() {
495         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.singleSourceAllProfileConfig)
496 
497         val managedSafetyCenterManager =
498             getSafetyCenterManagerForUser(deviceState.workProfile().userHandle())
499         assertFailsWith(SecurityException::class) {
500             managedSafetyCenterManager.getSafetySourceData(SINGLE_SOURCE_ALL_PROFILE_ID)
501         }
502     }
503 
504     @Test
505     @EnsureHasWorkProfile(installInstrumentedApp = TRUE)
getSafetySourceData_withoutAppInstalledForWorkProfile_shouldReturnNullnull506     fun getSafetySourceData_withoutAppInstalledForWorkProfile_shouldReturnNull() {
507         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.singleSourceAllProfileConfig)
508         val managedSafetyCenterManager =
509             getSafetyCenterManagerForUser(deviceState.workProfile().userHandle())
510         val dataForWork = safetySourceTestData.informationWithIssueForWork
511         managedSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
512             SINGLE_SOURCE_ALL_PROFILE_ID,
513             dataForWork
514         )
515         TestApis.packages().find(context.packageName).uninstall(deviceState.workProfile())
516 
517         val safetySourceData =
518             managedSafetyCenterManager.getSafetySourceDataWithInteractAcrossUsersPermission(
519                 SINGLE_SOURCE_ALL_PROFILE_ID
520             )
521 
522         assertThat(safetySourceData).isNull()
523     }
524 
525     @Test
526     @EnsureHasWorkProfile(installInstrumentedApp = TRUE)
getSafetySourceData_withRemovedProfile_shouldReturnNullnull527     fun getSafetySourceData_withRemovedProfile_shouldReturnNull() {
528         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.singleSourceAllProfileConfig)
529         val managedSafetyCenterManager =
530             getSafetyCenterManagerForUser(deviceState.workProfile().userHandle())
531         val dataForWork = safetySourceTestData.informationWithIssueForWork
532         managedSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
533             SINGLE_SOURCE_ALL_PROFILE_ID,
534             dataForWork
535         )
536         deviceState.workProfile().remove()
537 
538         val safetySourceData =
539             managedSafetyCenterManager.getSafetySourceDataWithInteractAcrossUsersPermission(
540                 SINGLE_SOURCE_ALL_PROFILE_ID
541             )
542 
543         assertThat(safetySourceData).isNull()
544     }
545 
546     @Test
547     @EnsureHasWorkProfile(installInstrumentedApp = TRUE)
getSafetySourceData_withProfileInQuietMode_shouldReturnDatanull548     fun getSafetySourceData_withProfileInQuietMode_shouldReturnData() {
549         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.singleSourceAllProfileConfig)
550         val managedSafetyCenterManager =
551             getSafetyCenterManagerForUser(deviceState.workProfile().userHandle())
552         val dataForWork = safetySourceTestData.informationWithIssueForWork
553         managedSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
554             SINGLE_SOURCE_ALL_PROFILE_ID,
555             dataForWork
556         )
557         setQuietMode(true)
558 
559         val safetySourceData =
560             managedSafetyCenterManager.getSafetySourceDataWithInteractAcrossUsersPermission(
561                 SINGLE_SOURCE_ALL_PROFILE_ID
562             )
563 
564         assertThat(safetySourceData).isEqualTo(dataForWork)
565     }
566 
567     @Test
568     @EnsureHasNoWorkProfile
569     @EnsureHasNoPrivateProfile
getSafetyCenterData_withComplexConfigWithoutWorkProfile_returnsPrimaryDataFromConfignull570     fun getSafetyCenterData_withComplexConfigWithoutWorkProfile_returnsPrimaryDataFromConfig() {
571         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.complexAllProfileConfig)
572 
573         val apiSafetyCenterData = safetyCenterManager.getSafetyCenterDataWithPermission()
574 
575         val safetyCenterDataFromComplexConfig =
576             SafetyCenterData(
577                 safetyCenterTestData.safetyCenterStatusUnknown,
578                 emptyList(),
579                 listOf(
580                     SafetyCenterEntryOrGroup(
581                         SafetyCenterEntryGroup.Builder(DYNAMIC_GROUP_ID, "OK")
582                             .setSeverityLevel(ENTRY_SEVERITY_LEVEL_UNKNOWN)
583                             .setSummary(
584                                 safetyCenterResourcesApk.getStringByName("group_unknown_summary")
585                             )
586                             .setEntries(listOf(dynamicBareboneDefault, dynamicDisabledDefault))
587                             .setSeverityUnspecifiedIconType(
588                                 SEVERITY_UNSPECIFIED_ICON_TYPE_NO_RECOMMENDATION
589                             )
590                             .build()
591                     ),
592                     SafetyCenterEntryOrGroup(
593                         staticGroupBuilder
594                             .setEntries(listOf(staticBarebone, staticAllOptional))
595                             .build()
596                     )
597                 ),
598                 listOf(
599                     SafetyCenterStaticEntryGroup(
600                         "OK",
601                         listOf(createStaticEntry(), createStaticEntry(explicit = false))
602                     )
603                 )
604             )
605         assertThat(apiSafetyCenterData.withoutExtras()).isEqualTo(safetyCenterDataFromComplexConfig)
606     }
607 
608     @Test
609     @EnsureHasWorkProfile(installInstrumentedApp = TRUE)
610     @EnsureHasNoPrivateProfile
getSafetyCenterData_withComplexConfigWithoutDataProvided_returnsDataFromConfignull611     fun getSafetyCenterData_withComplexConfigWithoutDataProvided_returnsDataFromConfig() {
612         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.complexAllProfileConfig)
613 
614         val apiSafetyCenterData = safetyCenterManager.getSafetyCenterDataWithPermission()
615 
616         val safetyCenterDataFromComplexConfig =
617             SafetyCenterData(
618                 safetyCenterTestData.safetyCenterStatusUnknown,
619                 emptyList(),
620                 listOf(
621                     SafetyCenterEntryOrGroup(
622                         SafetyCenterEntryGroup.Builder(DYNAMIC_GROUP_ID, "OK")
623                             .setSeverityLevel(ENTRY_SEVERITY_LEVEL_UNKNOWN)
624                             .setSummary(
625                                 safetyCenterResourcesApk.getStringByName("group_unknown_summary")
626                             )
627                             .setEntries(
628                                 listOf(
629                                     dynamicBareboneDefault,
630                                     dynamicDisabledDefault,
631                                     dynamicDisabledForWorkDefault
632                                 )
633                             )
634                             .setSeverityUnspecifiedIconType(
635                                 SEVERITY_UNSPECIFIED_ICON_TYPE_NO_RECOMMENDATION
636                             )
637                             .build()
638                     ),
639                     SafetyCenterEntryOrGroup(
640                         staticGroupBuilder
641                             .setEntries(
642                                 listOf(staticBarebone, staticAllOptional, staticAllOptionalForWork)
643                             )
644                             .build()
645                     )
646                 ),
647                 listOf(
648                     SafetyCenterStaticEntryGroup(
649                         "OK",
650                         listOf(
651                             createStaticEntry(),
652                             createStaticEntryForWork(),
653                             createStaticEntry(explicit = false),
654                             createStaticEntryForWork(explicit = false)
655                         )
656                     )
657                 )
658             )
659         assertThat(apiSafetyCenterData.withoutExtras()).isEqualTo(safetyCenterDataFromComplexConfig)
660     }
661 
662     @Test
663     @EnsureHasWorkProfile(installInstrumentedApp = TRUE)
664     @EnsureHasNoPrivateProfile
getSafetyCenterData_withComplexConfigWithPrimaryDataProvided_returnsPrimaryDataProvidednull665     fun getSafetyCenterData_withComplexConfigWithPrimaryDataProvided_returnsPrimaryDataProvided() {
666         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.complexAllProfileConfig)
667         updatePrimaryProfileSources()
668 
669         val apiSafetyCenterData = safetyCenterManager.getSafetyCenterDataWithPermission()
670 
671         val safetyCenterDataFromComplexConfig =
672             SafetyCenterData(
673                 safetyCenterTestData.safetyCenterStatusCritical(6),
674                 primaryProfileOnlyIssues,
675                 listOf(
676                     SafetyCenterEntryOrGroup(
677                         SafetyCenterEntryGroup.Builder(DYNAMIC_GROUP_ID, "OK")
678                             .setSeverityLevel(ENTRY_SEVERITY_LEVEL_CRITICAL_WARNING)
679                             .setSummary("Critical summary")
680                             .setEntries(
681                                 listOf(
682                                     dynamicBareboneUpdated,
683                                     dynamicDisabledUpdated,
684                                     dynamicDisabledForWorkDefault,
685                                     dynamicHiddenUpdated
686                                 )
687                             )
688                             .setSeverityUnspecifiedIconType(
689                                 SEVERITY_UNSPECIFIED_ICON_TYPE_NO_RECOMMENDATION
690                             )
691                             .build()
692                     ),
693                     SafetyCenterEntryOrGroup(
694                         staticGroupBuilder
695                             .setEntries(
696                                 listOf(staticBarebone, staticAllOptional, staticAllOptionalForWork)
697                             )
698                             .build()
699                     )
700                 ),
701                 listOf(
702                     SafetyCenterStaticEntryGroup(
703                         "OK",
704                         listOf(
705                             staticEntryUpdated,
706                             createStaticEntryForWork(),
707                             createStaticEntry(explicit = false),
708                             createStaticEntryForWork(explicit = false)
709                         )
710                     )
711                 )
712             )
713         assertThat(apiSafetyCenterData.withoutExtras()).isEqualTo(safetyCenterDataFromComplexConfig)
714     }
715 
716     @Test
717     @EnsureHasWorkProfile(installInstrumentedApp = TRUE)
718     @EnsureHasNoPrivateProfile
getSafetyCenterData_withComplexConfigWithExtraWorkOnlyWithAllDataProvided_returnsAllDataProvidednull719     fun getSafetyCenterData_withComplexConfigWithExtraWorkOnlyWithAllDataProvided_returnsAllDataProvided() {
720         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.complexAllProfileConfig)
721         updatePrimaryProfileSources()
722         updateWorkProfileSources()
723 
724         val apiSafetyCenterData = safetyCenterManager.getSafetyCenterDataWithPermission()
725 
726         val managedUserId = deviceState.workProfile().id()
727         val safetyCenterDataFromComplexConfig =
728             SafetyCenterData(
729                 safetyCenterTestData.safetyCenterStatusCritical(11),
730                 listOf(
731                     safetyCenterTestData.safetyCenterIssueCritical(
732                         DYNAMIC_BAREBONE_ID,
733                         groupId = DYNAMIC_GROUP_ID
734                     ),
735                     safetyCenterTestData.safetyCenterIssueCritical(
736                         ISSUE_ONLY_BAREBONE_ID,
737                         attributionTitle = null,
738                         groupId = ISSUE_ONLY_GROUP_ID
739                     ),
740                     safetyCenterTestData.safetyCenterIssueRecommendation(
741                         DYNAMIC_DISABLED_ID,
742                         groupId = DYNAMIC_GROUP_ID
743                     ),
744                     safetyCenterTestData.safetyCenterIssueRecommendation(
745                         ISSUE_ONLY_ALL_OPTIONAL_ID,
746                         attributionTitle = null,
747                         groupId = ISSUE_ONLY_GROUP_ID
748                     ),
749                     safetyCenterTestData.safetyCenterIssueInformation(
750                         DYNAMIC_IN_STATELESS_ID,
751                         groupId = MIXED_STATELESS_GROUP_ID
752                     ),
753                     safetyCenterTestData.safetyCenterIssueInformation(
754                         ISSUE_ONLY_IN_STATELESS_ID,
755                         groupId = MIXED_STATELESS_GROUP_ID
756                     ),
757                     safetyCenterTestData.safetyCenterIssueInformation(
758                         DYNAMIC_DISABLED_ID,
759                         managedUserId,
760                         groupId = DYNAMIC_GROUP_ID
761                     ),
762                     safetyCenterTestData.safetyCenterIssueInformation(
763                         DYNAMIC_HIDDEN_ID,
764                         managedUserId,
765                         groupId = DYNAMIC_GROUP_ID
766                     ),
767                     safetyCenterTestData.safetyCenterIssueInformation(
768                         ISSUE_ONLY_ALL_OPTIONAL_ID,
769                         managedUserId,
770                         attributionTitle = null,
771                         groupId = ISSUE_ONLY_GROUP_ID
772                     ),
773                     safetyCenterTestData.safetyCenterIssueInformation(
774                         DYNAMIC_IN_STATELESS_ID,
775                         managedUserId,
776                         groupId = MIXED_STATELESS_GROUP_ID
777                     ),
778                     safetyCenterTestData.safetyCenterIssueInformation(
779                         ISSUE_ONLY_IN_STATELESS_ID,
780                         managedUserId,
781                         groupId = MIXED_STATELESS_GROUP_ID
782                     )
783                 ),
784                 listOf(
785                     SafetyCenterEntryOrGroup(
786                         SafetyCenterEntryGroup.Builder(DYNAMIC_GROUP_ID, "OK")
787                             .setSeverityLevel(ENTRY_SEVERITY_LEVEL_CRITICAL_WARNING)
788                             .setSummary("Critical summary")
789                             .setEntries(
790                                 listOf(
791                                     dynamicBareboneUpdated,
792                                     dynamicDisabledUpdated,
793                                     dynamicDisabledForWorkUpdated,
794                                     dynamicHiddenUpdated,
795                                     dynamicHiddenForWorkUpdated
796                                 )
797                             )
798                             .setSeverityUnspecifiedIconType(
799                                 SEVERITY_UNSPECIFIED_ICON_TYPE_NO_RECOMMENDATION
800                             )
801                             .build()
802                     ),
803                     SafetyCenterEntryOrGroup(
804                         staticGroupBuilder
805                             .setEntries(
806                                 listOf(staticBarebone, staticAllOptional, staticAllOptionalForWork)
807                             )
808                             .build()
809                     )
810                 ),
811                 listOf(
812                     SafetyCenterStaticEntryGroup(
813                         "OK",
814                         listOf(
815                             staticEntryUpdated,
816                             staticEntryForWorkUpdated,
817                             createStaticEntry(explicit = false),
818                             createStaticEntryForWork(explicit = false)
819                         )
820                     )
821                 )
822             )
823         assertThat(apiSafetyCenterData.withoutExtras()).isEqualTo(safetyCenterDataFromComplexConfig)
824     }
825 
826     @Test
827     @RequiresFlagsDisabled(com.android.permission.flags.Flags.FLAG_PRIVATE_PROFILE_SUPPORTED)
828     @EnsureHasWorkProfile(installInstrumentedApp = TRUE)
829     @EnsureHasPrivateProfile(installInstrumentedApp = TRUE)
getSafetyCenterData_withComplexConfigWithPrivateProfileDisallowedWithAllDataProvided_returnsAllDataProvidednull830     fun getSafetyCenterData_withComplexConfigWithPrivateProfileDisallowedWithAllDataProvided_returnsAllDataProvided() {
831         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.complexAllProfileConfig)
832         updatePrimaryProfileSources()
833         updateWorkProfileSources()
834         updatePrivateProfileSources()
835 
836         val apiSafetyCenterData = safetyCenterManager.getSafetyCenterDataWithPermission()
837 
838         val managedUserId = deviceState.workProfile().id()
839         val safetyCenterDataFromComplexConfig =
840             SafetyCenterData(
841                 safetyCenterTestData.safetyCenterStatusCritical(11),
842                 listOf(
843                     safetyCenterTestData.safetyCenterIssueCritical(
844                         DYNAMIC_BAREBONE_ID,
845                         groupId = DYNAMIC_GROUP_ID
846                     ),
847                     safetyCenterTestData.safetyCenterIssueCritical(
848                         ISSUE_ONLY_BAREBONE_ID,
849                         attributionTitle = null,
850                         groupId = ISSUE_ONLY_GROUP_ID
851                     ),
852                     safetyCenterTestData.safetyCenterIssueRecommendation(
853                         DYNAMIC_DISABLED_ID,
854                         groupId = DYNAMIC_GROUP_ID
855                     ),
856                     safetyCenterTestData.safetyCenterIssueRecommendation(
857                         ISSUE_ONLY_ALL_OPTIONAL_ID,
858                         attributionTitle = null,
859                         groupId = ISSUE_ONLY_GROUP_ID
860                     ),
861                     safetyCenterTestData.safetyCenterIssueInformation(
862                         DYNAMIC_IN_STATELESS_ID,
863                         groupId = MIXED_STATELESS_GROUP_ID
864                     ),
865                     safetyCenterTestData.safetyCenterIssueInformation(
866                         ISSUE_ONLY_IN_STATELESS_ID,
867                         groupId = MIXED_STATELESS_GROUP_ID
868                     ),
869                     safetyCenterTestData.safetyCenterIssueInformation(
870                         DYNAMIC_DISABLED_ID,
871                         managedUserId,
872                         groupId = DYNAMIC_GROUP_ID
873                     ),
874                     safetyCenterTestData.safetyCenterIssueInformation(
875                         DYNAMIC_HIDDEN_ID,
876                         managedUserId,
877                         groupId = DYNAMIC_GROUP_ID
878                     ),
879                     safetyCenterTestData.safetyCenterIssueInformation(
880                         ISSUE_ONLY_ALL_OPTIONAL_ID,
881                         managedUserId,
882                         attributionTitle = null,
883                         groupId = ISSUE_ONLY_GROUP_ID
884                     ),
885                     safetyCenterTestData.safetyCenterIssueInformation(
886                         DYNAMIC_IN_STATELESS_ID,
887                         managedUserId,
888                         groupId = MIXED_STATELESS_GROUP_ID
889                     ),
890                     safetyCenterTestData.safetyCenterIssueInformation(
891                         ISSUE_ONLY_IN_STATELESS_ID,
892                         managedUserId,
893                         groupId = MIXED_STATELESS_GROUP_ID
894                     )
895                 ),
896                 listOf(
897                     SafetyCenterEntryOrGroup(
898                         SafetyCenterEntryGroup.Builder(DYNAMIC_GROUP_ID, "OK")
899                             .setSeverityLevel(ENTRY_SEVERITY_LEVEL_CRITICAL_WARNING)
900                             .setSummary("Critical summary")
901                             .setEntries(
902                                 listOf(
903                                     dynamicBareboneUpdated,
904                                     dynamicDisabledUpdated,
905                                     dynamicDisabledForWorkUpdated,
906                                     dynamicHiddenUpdated,
907                                     dynamicHiddenForWorkUpdated
908                                 )
909                             )
910                             .setSeverityUnspecifiedIconType(
911                                 SEVERITY_UNSPECIFIED_ICON_TYPE_NO_RECOMMENDATION
912                             )
913                             .build()
914                     ),
915                     SafetyCenterEntryOrGroup(
916                         staticGroupBuilder
917                             .setEntries(
918                                 listOf(staticBarebone, staticAllOptional, staticAllOptionalForWork)
919                             )
920                             .build()
921                     )
922                 ),
923                 listOf(
924                     SafetyCenterStaticEntryGroup(
925                         "OK",
926                         listOf(
927                             staticEntryUpdated,
928                             staticEntryForWorkUpdated,
929                             createStaticEntry(explicit = false),
930                             createStaticEntryForWork(explicit = false)
931                         )
932                     )
933                 )
934             )
935         assertThat(apiSafetyCenterData.withoutExtras()).isEqualTo(safetyCenterDataFromComplexConfig)
936     }
937 
938     // TODO(b/286539356) add the os feature flag requirement when available.
939     @Test
940     @RequiresFlagsEnabled(com.android.permission.flags.Flags.FLAG_PRIVATE_PROFILE_SUPPORTED)
941     @EnsureHasWorkProfile(installInstrumentedApp = TRUE)
942     @EnsureHasPrivateProfile(installInstrumentedApp = TRUE)
getSafetyCenterData_withComplexConfigWithAllDataProvided_returnsAllDataProvidednull943     fun getSafetyCenterData_withComplexConfigWithAllDataProvided_returnsAllDataProvided() {
944         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.complexAllProfileConfig)
945         updatePrimaryProfileSources()
946         updateWorkProfileSources()
947         updatePrivateProfileSources()
948 
949         val apiSafetyCenterData = safetyCenterManager.getSafetyCenterDataWithPermission()
950 
951         val managedUserId = deviceState.workProfile().id()
952         val privateProfileId = deviceState.privateProfile().id()
953         val safetyCenterDataFromComplexConfig =
954             SafetyCenterData(
955                 safetyCenterTestData.safetyCenterStatusCritical(11),
956                 listOf(
957                     safetyCenterTestData.safetyCenterIssueCritical(
958                         DYNAMIC_BAREBONE_ID,
959                         groupId = DYNAMIC_GROUP_ID
960                     ),
961                     safetyCenterTestData.safetyCenterIssueCritical(
962                         ISSUE_ONLY_BAREBONE_ID,
963                         attributionTitle = null,
964                         groupId = ISSUE_ONLY_GROUP_ID
965                     ),
966                     safetyCenterTestData.safetyCenterIssueRecommendation(
967                         DYNAMIC_DISABLED_ID,
968                         groupId = DYNAMIC_GROUP_ID
969                     ),
970                     safetyCenterTestData.safetyCenterIssueRecommendation(
971                         ISSUE_ONLY_ALL_OPTIONAL_ID,
972                         attributionTitle = null,
973                         groupId = ISSUE_ONLY_GROUP_ID
974                     ),
975                     safetyCenterTestData.safetyCenterIssueInformation(
976                         DYNAMIC_IN_STATELESS_ID,
977                         groupId = MIXED_STATELESS_GROUP_ID
978                     ),
979                     safetyCenterTestData.safetyCenterIssueInformation(
980                         ISSUE_ONLY_IN_STATELESS_ID,
981                         groupId = MIXED_STATELESS_GROUP_ID
982                     ),
983                     safetyCenterTestData.safetyCenterIssueInformation(
984                         DYNAMIC_DISABLED_ID,
985                         managedUserId,
986                         groupId = DYNAMIC_GROUP_ID
987                     ),
988                     safetyCenterTestData.safetyCenterIssueInformation(
989                         DYNAMIC_HIDDEN_ID,
990                         managedUserId,
991                         groupId = DYNAMIC_GROUP_ID
992                     ),
993                     safetyCenterTestData.safetyCenterIssueInformation(
994                         ISSUE_ONLY_ALL_OPTIONAL_ID,
995                         managedUserId,
996                         attributionTitle = null,
997                         groupId = ISSUE_ONLY_GROUP_ID
998                     ),
999                     safetyCenterTestData.safetyCenterIssueInformation(
1000                         DYNAMIC_IN_STATELESS_ID,
1001                         managedUserId,
1002                         groupId = MIXED_STATELESS_GROUP_ID
1003                     ),
1004                     safetyCenterTestData.safetyCenterIssueInformation(
1005                         ISSUE_ONLY_IN_STATELESS_ID,
1006                         managedUserId,
1007                         groupId = MIXED_STATELESS_GROUP_ID
1008                     ),
1009                     safetyCenterTestData.safetyCenterIssueInformation(
1010                         DYNAMIC_DISABLED_ID,
1011                         privateProfileId,
1012                         groupId = DYNAMIC_GROUP_ID
1013                     ),
1014                     safetyCenterTestData.safetyCenterIssueInformation(
1015                         DYNAMIC_HIDDEN_ID,
1016                         privateProfileId,
1017                         groupId = DYNAMIC_GROUP_ID
1018                     ),
1019                     safetyCenterTestData.safetyCenterIssueInformation(
1020                         ISSUE_ONLY_ALL_OPTIONAL_ID,
1021                         privateProfileId,
1022                         attributionTitle = null,
1023                         groupId = ISSUE_ONLY_GROUP_ID
1024                     ),
1025                     safetyCenterTestData.safetyCenterIssueInformation(
1026                         DYNAMIC_IN_STATELESS_ID,
1027                         privateProfileId,
1028                         groupId = MIXED_STATELESS_GROUP_ID
1029                     ),
1030                     safetyCenterTestData.safetyCenterIssueInformation(
1031                         ISSUE_ONLY_IN_STATELESS_ID,
1032                         privateProfileId,
1033                         groupId = MIXED_STATELESS_GROUP_ID
1034                     )
1035                 ),
1036                 listOf(
1037                     SafetyCenterEntryOrGroup(
1038                         SafetyCenterEntryGroup.Builder(DYNAMIC_GROUP_ID, "OK")
1039                             .setSeverityLevel(ENTRY_SEVERITY_LEVEL_CRITICAL_WARNING)
1040                             .setSummary("Critical summary")
1041                             .setEntries(
1042                                 listOf(
1043                                     dynamicBareboneUpdated,
1044                                     dynamicDisabledUpdated,
1045                                     dynamicDisabledForWorkUpdated,
1046                                     dynamicDisabledForPrivateUpdated,
1047                                     dynamicHiddenUpdated,
1048                                     dynamicHiddenForWorkUpdated,
1049                                     dynamicHiddenForPrivateUpdated
1050                                 )
1051                             )
1052                             .setSeverityUnspecifiedIconType(
1053                                 SEVERITY_UNSPECIFIED_ICON_TYPE_NO_RECOMMENDATION
1054                             )
1055                             .build()
1056                     ),
1057                     SafetyCenterEntryOrGroup(
1058                         staticGroupBuilder
1059                             .setEntries(
1060                                 listOf(
1061                                     staticBarebone,
1062                                     staticAllOptional,
1063                                     staticAllOptionalForWork,
1064                                     staticAllOptionalForPrivate
1065                                 )
1066                             )
1067                             .build()
1068                     )
1069                 ),
1070                 listOf(
1071                     SafetyCenterStaticEntryGroup(
1072                         "OK",
1073                         listOf(
1074                             staticEntryUpdated,
1075                             staticEntryForWorkUpdated,
1076                             staticEntryForPrivateUpdated,
1077                             createStaticEntry(explicit = false),
1078                             createStaticEntryForWork(explicit = false),
1079                             createStaticEntryForPrivate(explicit = false)
1080                         )
1081                     )
1082                 )
1083             )
1084         assertThat(apiSafetyCenterData.withoutExtras()).isEqualTo(safetyCenterDataFromComplexConfig)
1085     }
1086 
1087     @Test
1088     @EnsureHasWorkProfile(installInstrumentedApp = TRUE)
1089     @EnsureHasNoPrivateProfile
getSafetyCenterData_withQuietMode_shouldHaveWorkProfilePausedSummaryAndNoWorkIssuesnull1090     fun getSafetyCenterData_withQuietMode_shouldHaveWorkProfilePausedSummaryAndNoWorkIssues() {
1091         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.complexAllProfileConfig)
1092         updatePrimaryProfileSources()
1093         updateWorkProfileSources()
1094         setQuietMode(true)
1095 
1096         val apiSafetyCenterData = safetyCenterManager.getSafetyCenterDataWithPermission()
1097 
1098         val safetyCenterDataFromComplexConfig =
1099             SafetyCenterData(
1100                 safetyCenterTestData.safetyCenterStatusCritical(6),
1101                 primaryProfileOnlyIssues,
1102                 listOf(
1103                     SafetyCenterEntryOrGroup(
1104                         SafetyCenterEntryGroup.Builder(DYNAMIC_GROUP_ID, "OK")
1105                             .setSeverityLevel(ENTRY_SEVERITY_LEVEL_CRITICAL_WARNING)
1106                             .setSummary("Critical summary")
1107                             .setEntries(
1108                                 listOf(
1109                                     dynamicBareboneUpdated,
1110                                     dynamicDisabledUpdated,
1111                                     dynamicDisabledForWorkPausedUpdated,
1112                                     dynamicHiddenUpdated,
1113                                     dynamicHiddenForWorkPausedUpdated,
1114                                 )
1115                             )
1116                             .setSeverityUnspecifiedIconType(
1117                                 SEVERITY_UNSPECIFIED_ICON_TYPE_NO_RECOMMENDATION
1118                             )
1119                             .build()
1120                     ),
1121                     SafetyCenterEntryOrGroup(
1122                         staticGroupBuilder
1123                             .setEntries(
1124                                 listOf(
1125                                     staticBarebone,
1126                                     staticAllOptional,
1127                                     staticAllOptionalForWorkPaused
1128                                 )
1129                             )
1130                             .build()
1131                     )
1132                 ),
1133                 listOf(
1134                     SafetyCenterStaticEntryGroup(
1135                         "OK",
1136                         listOf(
1137                             staticEntryUpdated,
1138                             staticEntryForWorkPausedUpdated,
1139                             createStaticEntry(explicit = false),
1140                             createStaticEntryForWorkPaused()
1141                         )
1142                     )
1143                 )
1144             )
1145         assertThat(apiSafetyCenterData.withoutExtras()).isEqualTo(safetyCenterDataFromComplexConfig)
1146     }
1147 
1148     @Test
1149     @EnsureHasAdditionalUser(installInstrumentedApp = TRUE)
1150     @EnsureHasWorkProfile(installInstrumentedApp = TRUE)
getSafetyCenterData_withDataForDifferentUserProfileGroup_shouldBeUnaffectednull1151     fun getSafetyCenterData_withDataForDifferentUserProfileGroup_shouldBeUnaffected() {
1152         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.singleSourceAllProfileConfig)
1153         val dataForPrimaryUser = safetySourceTestData.information
1154         safetyCenterTestHelper.setData(SINGLE_SOURCE_ALL_PROFILE_ID, dataForPrimaryUser)
1155         val dataForPrimaryUserWorkProfile = safetySourceTestData.informationWithIssueForWork
1156         val managedSafetyCenterManager =
1157             getSafetyCenterManagerForUser(deviceState.workProfile().userHandle())
1158         managedSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
1159             SINGLE_SOURCE_ALL_PROFILE_ID,
1160             dataForPrimaryUserWorkProfile
1161         )
1162 
1163         val additionalUserSafetyCenterManager =
1164             getSafetyCenterManagerForUser(deviceState.additionalUser().userHandle())
1165         val apiSafetyCenterDataForAdditionalUser =
1166             additionalUserSafetyCenterManager.getSafetyCenterDataWithInteractAcrossUsersPermission()
1167 
1168         assertThat(apiSafetyCenterDataForAdditionalUser)
1169             .isEqualTo(safetyCenterDataForAdditionalUser)
1170     }
1171 
1172     @Test
1173     @EnsureHasWorkProfile(installInstrumentedApp = TRUE)
1174     @EnsureHasNoPrivateProfile
getSafetyCenterData_afterManagedProfileRemoved_returnsDefaultDatanull1175     fun getSafetyCenterData_afterManagedProfileRemoved_returnsDefaultData() {
1176         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.singleSourceAllProfileConfig)
1177         val managedSafetyCenterManager =
1178             getSafetyCenterManagerForUser(deviceState.workProfile().userHandle())
1179         val safetyCenterDataWithWorkProfile =
1180             SafetyCenterData(
1181                 safetyCenterTestData.safetyCenterStatusUnknown,
1182                 emptyList(),
1183                 listOf(
1184                     SafetyCenterEntryOrGroup(
1185                         SafetyCenterEntryGroup.Builder(SINGLE_SOURCE_GROUP_ID, "OK")
1186                             .setSeverityLevel(ENTRY_SEVERITY_LEVEL_UNKNOWN)
1187                             .setSummary(
1188                                 safetyCenterResourcesApk.getStringByName("group_unknown_summary")
1189                             )
1190                             .setEntries(
1191                                 listOf(
1192                                     safetyCenterTestData.safetyCenterEntryDefault(
1193                                         SINGLE_SOURCE_ALL_PROFILE_ID
1194                                     ),
1195                                     safetyCenterTestData.safetyCenterEntryDefault(
1196                                         SINGLE_SOURCE_ALL_PROFILE_ID,
1197                                         deviceState.workProfile().id(),
1198                                         title = "Paste",
1199                                         pendingIntent =
1200                                             createTestActivityRedirectPendingIntentForUser(
1201                                                 deviceState.workProfile().userHandle()
1202                                             )
1203                                     )
1204                                 )
1205                             )
1206                             .setSeverityUnspecifiedIconType(
1207                                 SEVERITY_UNSPECIFIED_ICON_TYPE_NO_RECOMMENDATION
1208                             )
1209                             .build()
1210                     )
1211                 ),
1212                 emptyList()
1213             )
1214         checkState(
1215             safetyCenterManager.getSafetyCenterDataWithPermission() ==
1216                 safetyCenterDataWithWorkProfile
1217         )
1218         checkState(
1219             managedSafetyCenterManager.getSafetyCenterDataWithInteractAcrossUsersPermission() ==
1220                 safetyCenterDataWithWorkProfile
1221         )
1222 
1223         deviceState.workProfile().remove()
1224 
1225         val safetyCenterDataForPrimaryUser =
1226             SafetyCenterData(
1227                 safetyCenterTestData.safetyCenterStatusUnknown,
1228                 emptyList(),
1229                 listOf(
1230                     SafetyCenterEntryOrGroup(
1231                         safetyCenterTestData.safetyCenterEntryDefault(SINGLE_SOURCE_ALL_PROFILE_ID)
1232                     )
1233                 ),
1234                 emptyList()
1235             )
1236         assertThat(safetyCenterManager.getSafetyCenterDataWithPermission())
1237             .isEqualTo(safetyCenterDataForPrimaryUser)
1238         assertThat(
1239                 managedSafetyCenterManager.getSafetyCenterDataWithInteractAcrossUsersPermission()
1240             )
1241             .isEqualTo(SafetyCenterTestData.DEFAULT)
1242     }
1243 
1244     @Test
1245     @RequiresFlagsEnabled(com.android.permission.flags.Flags.FLAG_PRIVATE_PROFILE_SUPPORTED)
1246     @EnsureHasPrivateProfile(installInstrumentedApp = TRUE)
getSafetyCenterData_afterPrivateProfileRemoved_returnsDefaultDatanull1247     fun getSafetyCenterData_afterPrivateProfileRemoved_returnsDefaultData() {
1248         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.singleSourceAllProfileConfig)
1249         val privateSafetyCenterManager =
1250             getSafetyCenterManagerForUser(deviceState.privateProfile().userHandle())
1251         val safetyCenterDataWithPrivateProfile =
1252             SafetyCenterData(
1253                 safetyCenterTestData.safetyCenterStatusUnknown,
1254                 emptyList(),
1255                 listOf(
1256                     SafetyCenterEntryOrGroup(
1257                         SafetyCenterEntryGroup.Builder(SINGLE_SOURCE_GROUP_ID, "OK")
1258                             .setSeverityLevel(ENTRY_SEVERITY_LEVEL_UNKNOWN)
1259                             .setSummary(
1260                                 safetyCenterResourcesApk.getStringByName("group_unknown_summary")
1261                             )
1262                             .setEntries(
1263                                 listOf(
1264                                     safetyCenterTestData.safetyCenterEntryDefault(
1265                                         SINGLE_SOURCE_ALL_PROFILE_ID
1266                                     ),
1267                                     safetyCenterTestData.safetyCenterEntryDefault(
1268                                         SINGLE_SOURCE_ALL_PROFILE_ID,
1269                                         deviceState.privateProfile().id(),
1270                                         title = "Unknown",
1271                                         pendingIntent =
1272                                             createTestActivityRedirectPendingIntentForUser(
1273                                                 deviceState.privateProfile().userHandle()
1274                                             )
1275                                     )
1276                                 )
1277                             )
1278                             .setSeverityUnspecifiedIconType(
1279                                 SEVERITY_UNSPECIFIED_ICON_TYPE_NO_RECOMMENDATION
1280                             )
1281                             .build()
1282                     )
1283                 ),
1284                 emptyList()
1285             )
1286 
1287         assertThat(safetyCenterManager.getSafetyCenterDataWithPermission())
1288             .isEqualTo(safetyCenterDataWithPrivateProfile)
1289         assertThat(
1290                 privateSafetyCenterManager.getSafetyCenterDataWithInteractAcrossUsersPermission()
1291             )
1292             .isEqualTo(safetyCenterDataWithPrivateProfile)
1293 
1294         deviceState.privateProfile().remove()
1295 
1296         val safetyCenterDataForPrimaryUser =
1297             SafetyCenterData(
1298                 safetyCenterTestData.safetyCenterStatusUnknown,
1299                 emptyList(),
1300                 listOf(
1301                     SafetyCenterEntryOrGroup(
1302                         safetyCenterTestData.safetyCenterEntryDefault(SINGLE_SOURCE_ALL_PROFILE_ID)
1303                     )
1304                 ),
1305                 emptyList()
1306             )
1307         assertThat(safetyCenterManager.getSafetyCenterDataWithPermission())
1308             .isEqualTo(safetyCenterDataForPrimaryUser)
1309         assertThat(
1310                 privateSafetyCenterManager.getSafetyCenterDataWithInteractAcrossUsersPermission()
1311             )
1312             .isEqualTo(SafetyCenterTestData.DEFAULT)
1313     }
1314 
1315     @Test
1316     @EnsureHasAdditionalUser(installInstrumentedApp = TRUE)
getSafetyCenterData_afterAdditionalUserRemoved_returnsDefaultDatanull1317     fun getSafetyCenterData_afterAdditionalUserRemoved_returnsDefaultData() {
1318         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.singleSourceAllProfileConfig)
1319         val additionalUserSafetyCenterManager =
1320             getSafetyCenterManagerForUser(deviceState.additionalUser().userHandle())
1321         checkState(
1322             additionalUserSafetyCenterManager
1323                 .getSafetyCenterDataWithInteractAcrossUsersPermission() ==
1324                 safetyCenterDataForAdditionalUser
1325         )
1326 
1327         deviceState.additionalUser().remove()
1328 
1329         assertThat(
1330                 additionalUserSafetyCenterManager
1331                     .getSafetyCenterDataWithInteractAcrossUsersPermission()
1332             )
1333             .isEqualTo(SafetyCenterTestData.DEFAULT)
1334     }
1335 
1336     @Test
1337     @EnsureHasWorkProfile(installInstrumentedApp = TRUE)
setSafetySourceData_primaryProfileIssueOnlySource_shouldNotBeAbleToSetDataToWorkProfilenull1338     fun setSafetySourceData_primaryProfileIssueOnlySource_shouldNotBeAbleToSetDataToWorkProfile() {
1339         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.issueOnlySourceConfig)
1340         val managedSafetyCenterManager =
1341             getSafetyCenterManagerForUser(deviceState.workProfile().userHandle())
1342         val dataForWork =
1343             SafetySourceTestData.issuesOnly(safetySourceTestData.criticalResolvingGeneralIssue)
1344 
1345         assertFailsWith(IllegalArgumentException::class) {
1346             managedSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
1347                 ISSUE_ONLY_ALL_OPTIONAL_ID,
1348                 dataForWork
1349             )
1350         }
1351     }
1352 
1353     @Test
1354     @EnsureHasWorkProfile(installInstrumentedApp = TRUE)
setSafetySourceData_withoutInteractAcrossUserPermission_shouldThrowErrornull1355     fun setSafetySourceData_withoutInteractAcrossUserPermission_shouldThrowError() {
1356         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.singleSourceAllProfileConfig)
1357         val dataForWork = safetySourceTestData.informationWithIssueForWork
1358         val managedSafetyCenterManager =
1359             getSafetyCenterManagerForUser(deviceState.workProfile().userHandle())
1360 
1361         assertFailsWith(SecurityException::class) {
1362             managedSafetyCenterManager.setSafetySourceData(
1363                 SINGLE_SOURCE_ALL_PROFILE_ID,
1364                 dataForWork,
1365                 EVENT_SOURCE_STATE_CHANGED
1366             )
1367         }
1368     }
1369 
1370     @Test
1371     @EnsureHasWorkProfile(installInstrumentedApp = TRUE)
setSafetySourceData_withoutAppInstalledForWorkProfile_shouldNoOpnull1372     fun setSafetySourceData_withoutAppInstalledForWorkProfile_shouldNoOp() {
1373         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.singleSourceAllProfileConfig)
1374         val dataForWork = safetySourceTestData.informationWithIssueForWork
1375         val managedSafetyCenterManager =
1376             getSafetyCenterManagerForUser(deviceState.workProfile().userHandle())
1377         TestApis.packages().find(context.packageName).uninstall(deviceState.workProfile())
1378 
1379         managedSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
1380             SINGLE_SOURCE_ALL_PROFILE_ID,
1381             dataForWork
1382         )
1383 
1384         val safetySourceData =
1385             managedSafetyCenterManager.getSafetySourceDataWithInteractAcrossUsersPermission(
1386                 SINGLE_SOURCE_ALL_PROFILE_ID
1387             )
1388         assertThat(safetySourceData).isNull()
1389     }
1390 
1391     @Test
1392     @EnsureHasWorkProfile(installInstrumentedApp = TRUE)
setSafetySourceData_withRemovedProfile_shouldNoOpnull1393     fun setSafetySourceData_withRemovedProfile_shouldNoOp() {
1394         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.singleSourceAllProfileConfig)
1395         val dataForWork = safetySourceTestData.informationWithIssueForWork
1396         val managedSafetyCenterManager =
1397             getSafetyCenterManagerForUser(deviceState.workProfile().userHandle())
1398         deviceState.workProfile().remove()
1399 
1400         managedSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
1401             SINGLE_SOURCE_ALL_PROFILE_ID,
1402             dataForWork
1403         )
1404 
1405         val safetySourceData =
1406             managedSafetyCenterManager.getSafetySourceDataWithInteractAcrossUsersPermission(
1407                 SINGLE_SOURCE_ALL_PROFILE_ID
1408             )
1409         assertThat(safetySourceData).isNull()
1410     }
1411 
1412     @Test
1413     @EnsureHasCloneProfile(installInstrumentedApp = TRUE)
setSafetySourceData_withUnsupportedProfile_shouldNoOpnull1414     fun setSafetySourceData_withUnsupportedProfile_shouldNoOp() {
1415         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.singleSourceAllProfileConfig)
1416         val dataForClone = safetySourceTestData.informationWithIssueForWork
1417         val clonedSafetyCenterManager =
1418             getSafetyCenterManagerForUser(deviceState.cloneProfile().userHandle())
1419 
1420         clonedSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
1421             SINGLE_SOURCE_ALL_PROFILE_ID,
1422             dataForClone
1423         )
1424 
1425         val safetySourceData =
1426             clonedSafetyCenterManager.getSafetySourceDataWithInteractAcrossUsersPermission(
1427                 SINGLE_SOURCE_ALL_PROFILE_ID
1428             )
1429         assertThat(safetySourceData).isNull()
1430     }
1431 
1432     @Test
1433     @EnsureHasWorkProfile(installInstrumentedApp = TRUE)
setSafetySourceData_withProfileInQuietMode_shouldSetDatanull1434     fun setSafetySourceData_withProfileInQuietMode_shouldSetData() {
1435         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.singleSourceAllProfileConfig)
1436         val dataForWork = safetySourceTestData.informationWithIssueForWork
1437         val managedSafetyCenterManager =
1438             getSafetyCenterManagerForUser(deviceState.workProfile().userHandle())
1439         setQuietMode(true)
1440 
1441         managedSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
1442             SINGLE_SOURCE_ALL_PROFILE_ID,
1443             dataForWork
1444         )
1445 
1446         val safetySourceData =
1447             managedSafetyCenterManager.getSafetySourceDataWithInteractAcrossUsersPermission(
1448                 SINGLE_SOURCE_ALL_PROFILE_ID
1449             )
1450         assertThat(safetySourceData).isEqualTo(dataForWork)
1451     }
1452 
1453     @Test
1454     @EnsureHasWorkProfile(installInstrumentedApp = TRUE)
setSafetySourceData_issuesOnlySourceWithWorkProfile_shouldBeAbleToSetDatanull1455     fun setSafetySourceData_issuesOnlySourceWithWorkProfile_shouldBeAbleToSetData() {
1456         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.issueOnlySourceAllProfileConfig)
1457 
1458         val dataForPrimaryUser =
1459             SafetySourceTestData.issuesOnly(safetySourceTestData.recommendationGeneralIssue)
1460         safetyCenterTestHelper.setData(ISSUE_ONLY_ALL_PROFILE_SOURCE_ID, dataForPrimaryUser)
1461         val managedSafetyCenterManager =
1462             getSafetyCenterManagerForUser(deviceState.workProfile().userHandle())
1463         val dataForWorkProfile =
1464             SafetySourceTestData.issuesOnly(safetySourceTestData.criticalResolvingGeneralIssue)
1465         managedSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
1466             ISSUE_ONLY_ALL_PROFILE_SOURCE_ID,
1467             dataForWorkProfile
1468         )
1469 
1470         val apiSafetySourceData =
1471             safetyCenterManager.getSafetySourceDataWithPermission(ISSUE_ONLY_ALL_PROFILE_SOURCE_ID)
1472         val apiSafetySourceDataForWork =
1473             managedSafetyCenterManager.getSafetySourceDataWithInteractAcrossUsersPermission(
1474                 ISSUE_ONLY_ALL_PROFILE_SOURCE_ID
1475             )
1476         assertThat(apiSafetySourceData).isEqualTo(dataForPrimaryUser)
1477         assertThat(apiSafetySourceDataForWork).isEqualTo(dataForWorkProfile)
1478     }
1479 
1480     @Test
1481     @EnsureHasWorkProfile(installInstrumentedApp = TRUE)
setSafetySourceData_primaryProfileSource_shouldNotBeAbleToSetDataToWorkProfilenull1482     fun setSafetySourceData_primaryProfileSource_shouldNotBeAbleToSetDataToWorkProfile() {
1483         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.singleSourceConfig)
1484 
1485         val managedSafetyCenterManager =
1486             getSafetyCenterManagerForUser(deviceState.workProfile().userHandle())
1487         val dataForWork = safetySourceTestData.informationWithIssueForWork
1488         assertFailsWith(IllegalArgumentException::class) {
1489             managedSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
1490                 SINGLE_SOURCE_ID,
1491                 dataForWork
1492             )
1493         }
1494     }
1495 
1496     @Test
1497     @EnsureHasWorkProfile(installInstrumentedApp = TRUE)
setSafetySourceData_sourceWithWorkProfile_shouldBeAbleToSetDatanull1498     fun setSafetySourceData_sourceWithWorkProfile_shouldBeAbleToSetData() {
1499         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.singleSourceAllProfileConfig)
1500 
1501         val dataForPrimaryUser = safetySourceTestData.information
1502         safetyCenterTestHelper.setData(SINGLE_SOURCE_ALL_PROFILE_ID, dataForPrimaryUser)
1503         val dataForWork = safetySourceTestData.informationWithIssueForWork
1504         val managedSafetyCenterManager =
1505             getSafetyCenterManagerForUser(deviceState.workProfile().userHandle())
1506         managedSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
1507             SINGLE_SOURCE_ALL_PROFILE_ID,
1508             dataForWork
1509         )
1510 
1511         val apiSafetySourceData =
1512             safetyCenterManager.getSafetySourceDataWithPermission(SINGLE_SOURCE_ALL_PROFILE_ID)
1513         val apiSafetySourceDataForWork =
1514             managedSafetyCenterManager.getSafetySourceDataWithInteractAcrossUsersPermission(
1515                 SINGLE_SOURCE_ALL_PROFILE_ID
1516             )
1517         assertThat(apiSafetySourceData).isEqualTo(dataForPrimaryUser)
1518         assertThat(apiSafetySourceDataForWork).isEqualTo(dataForWork)
1519     }
1520 
1521     @Test
1522     @EnsureHasWorkProfile(installInstrumentedApp = TRUE)
setSafetySourceData_notificationsAllowed_workProfile_sendsNotificationnull1523     fun setSafetySourceData_notificationsAllowed_workProfile_sendsNotification() {
1524         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.singleSourceAllProfileConfig)
1525         SafetyCenterFlags.notificationsEnabled = true
1526         SafetyCenterFlags.notificationsAllowedSources = setOf(SINGLE_SOURCE_ALL_PROFILE_ID)
1527         SafetyCenterFlags.immediateNotificationBehaviorIssues =
1528             setOf("$SINGLE_SOURCE_ALL_PROFILE_ID/$ISSUE_TYPE_ID")
1529         val dataForWork = safetySourceTestData.informationWithIssueForWork
1530         val managedSafetyCenterManager =
1531             getSafetyCenterManagerForUser(deviceState.workProfile().userHandle())
1532 
1533         managedSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
1534             SINGLE_SOURCE_ALL_PROFILE_ID,
1535             dataForWork
1536         )
1537 
1538         TestNotificationListener.waitForNotificationsMatching(
1539             NotificationCharacteristics(
1540                 title = "Information issue title",
1541                 text = "Information issue summary",
1542                 actions = listOf("Review")
1543             )
1544         )
1545     }
1546 
1547     @Test
1548     @EnsureHasAdditionalUser(installInstrumentedApp = TRUE)
setSafetySourceData_forStoppedUser_shouldSetDatanull1549     fun setSafetySourceData_forStoppedUser_shouldSetData() {
1550         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.singleSourceConfig)
1551         deviceState.additionalUser().stop()
1552 
1553         val dataForPrimaryUser = safetySourceTestData.unspecified
1554         val additionalUserSafetyCenterManager =
1555             getSafetyCenterManagerForUser(deviceState.additionalUser().userHandle())
1556         additionalUserSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
1557             SINGLE_SOURCE_ID,
1558             dataForPrimaryUser
1559         )
1560 
1561         val apiSafetySourceData =
1562             additionalUserSafetyCenterManager.getSafetySourceDataWithInteractAcrossUsersPermission(
1563                 SINGLE_SOURCE_ID
1564             )
1565         assertThat(apiSafetySourceData).isEqualTo(dataForPrimaryUser)
1566     }
1567 
1568     @Test
1569     @EnsureHasAdditionalUser(installInstrumentedApp = TRUE)
setSafetySourceData_forBothPrimaryAdditionalUser_shouldSetDatanull1570     fun setSafetySourceData_forBothPrimaryAdditionalUser_shouldSetData() {
1571         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.singleSourceConfig)
1572 
1573         val dataForPrimaryUser = safetySourceTestData.information
1574         safetyCenterTestHelper.setData(SINGLE_SOURCE_ID, dataForPrimaryUser)
1575         val dataForAdditionalUser = safetySourceTestData.unspecified
1576         val additionalUserSafetyCenterManager =
1577             getSafetyCenterManagerForUser(deviceState.additionalUser().userHandle())
1578         additionalUserSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
1579             SINGLE_SOURCE_ID,
1580             dataForAdditionalUser
1581         )
1582 
1583         val apiSafetySourceDataForPrimaryUser =
1584             safetyCenterManager.getSafetySourceDataWithPermission(SINGLE_SOURCE_ID)
1585         val apiSafetySourceDataForAdditionalUser =
1586             additionalUserSafetyCenterManager.getSafetySourceDataWithInteractAcrossUsersPermission(
1587                 SINGLE_SOURCE_ID
1588             )
1589         assertThat(apiSafetySourceDataForPrimaryUser).isEqualTo(dataForPrimaryUser)
1590         assertThat(apiSafetySourceDataForAdditionalUser).isEqualTo(dataForAdditionalUser)
1591     }
1592 
1593     @Test
1594     @EnsureHasAdditionalUser(installInstrumentedApp = TRUE)
setSafetySourceData_forAdditionalUser_shouldNotAffectDataForPrimaryUsernull1595     fun setSafetySourceData_forAdditionalUser_shouldNotAffectDataForPrimaryUser() {
1596         safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.singleSourceConfig)
1597 
1598         val dataForAdditionalUser = safetySourceTestData.unspecified
1599         val additionalUserSafetyCenterManager =
1600             getSafetyCenterManagerForUser(deviceState.additionalUser().userHandle())
1601         additionalUserSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
1602             SINGLE_SOURCE_ID,
1603             dataForAdditionalUser
1604         )
1605 
1606         val apiSafetySourceDataForPrimaryUser =
1607             safetyCenterManager.getSafetySourceDataWithPermission(SINGLE_SOURCE_ID)
1608         assertThat(apiSafetySourceDataForPrimaryUser).isEqualTo(null)
1609     }
1610 
findWorkPolicyInfonull1611     private fun findWorkPolicyInfo() {
1612         context.launchSafetyCenterActivity {
1613             waitAllTextDisplayed("Your work policy info", "Settings managed by your IT admin")
1614         }
1615     }
1616 
getSafetyCenterManagerForUsernull1617     private fun getSafetyCenterManagerForUser(userHandle: UserHandle): SafetyCenterManager {
1618         val contextForUser = getContextForUser(userHandle)
1619         return contextForUser.getSystemService(SafetyCenterManager::class.java)!!
1620     }
1621 
getContextForUsernull1622     private fun getContextForUser(userHandle: UserHandle): Context {
1623         return callWithShellPermissionIdentity(INTERACT_ACROSS_USERS_FULL) {
1624             context.createContextAsUser(userHandle, 0)
1625         }
1626     }
1627 
createTestActivityRedirectPendingIntentForUsernull1628     private fun createTestActivityRedirectPendingIntentForUser(
1629         user: UserHandle,
1630         explicit: Boolean = true
1631     ): PendingIntent {
1632         return callWithShellPermissionIdentity(INTERACT_ACROSS_USERS) {
1633             SafetySourceTestData.createRedirectPendingIntent(
1634                 getContextForUser(user),
1635                 SafetySourceTestData.createTestActivityIntent(getContextForUser(user), explicit)
1636             )
1637         }
1638     }
1639 
SafetyCenterManagernull1640     private fun SafetyCenterManager.getSafetySourceDataWithInteractAcrossUsersPermission(
1641         id: String
1642     ): SafetySourceData? =
1643         callWithShellPermissionIdentity(INTERACT_ACROSS_USERS_FULL) {
1644             getSafetySourceDataWithPermission(id)
1645         }
1646 
SafetyCenterManagernull1647     private fun SafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
1648         id: String,
1649         dataToSet: SafetySourceData,
1650         safetyEvent: SafetyEvent = EVENT_SOURCE_STATE_CHANGED
1651     ) =
1652         callWithShellPermissionIdentity(INTERACT_ACROSS_USERS_FULL) {
1653             setSafetySourceDataWithPermission(id, dataToSet, safetyEvent)
1654         }
1655 
SafetyCenterManagernull1656     private fun SafetyCenterManager.getSafetyCenterDataWithInteractAcrossUsersPermission():
1657         SafetyCenterData =
1658         callWithShellPermissionIdentity(INTERACT_ACROSS_USERS_FULL) {
1659             getSafetyCenterDataWithPermission()
1660         }
1661 
setQuietModenull1662     private fun setQuietMode(enableQuietMode: Boolean) {
1663         if (inQuietMode == enableQuietMode) {
1664             return
1665         }
1666         if (enableQuietMode) {
1667             deviceState.workProfile().setQuietMode(true)
1668         } else {
1669             // This is needed to ensure the refresh broadcast doesn't leak onto other tests.
1670             disableQuietModeAndWaitForRefreshToComplete()
1671         }
1672         inQuietMode = enableQuietMode
1673     }
1674 
disableQuietModeAndWaitForRefreshToCompletenull1675     private fun disableQuietModeAndWaitForRefreshToComplete() {
1676         val listener = safetyCenterTestHelper.addListener()
1677         deviceState.workProfile().setQuietMode(false)
1678         listener.waitForSafetyCenterRefresh()
1679     }
1680 
safetyCenterEntryOkForWorknull1681     private fun safetyCenterEntryOkForWork(sourceId: String, managedUserId: Int) =
1682         safetyCenterTestData
1683             .safetyCenterEntryOkBuilder(sourceId, managedUserId, title = "Ok title for Work")
1684             .build()
1685 
1686     private fun safetyCenterEntryOkForPrivate(sourceId: String, managedUserId: Int) =
1687         safetyCenterTestData
1688             .safetyCenterEntryOkBuilder(sourceId, managedUserId, title = "Ok title for Private")
1689             .build()
1690 
1691     private fun updatePrimaryProfileSources() {
1692         safetyCenterTestHelper.setData(
1693             DYNAMIC_BAREBONE_ID,
1694             safetySourceTestData.criticalWithResolvingGeneralIssue
1695         )
1696         safetyCenterTestHelper.setData(
1697             DYNAMIC_DISABLED_ID,
1698             safetySourceTestData.recommendationWithGeneralIssue
1699         )
1700         safetyCenterTestHelper.setData(DYNAMIC_HIDDEN_ID, safetySourceTestData.unspecified)
1701         safetyCenterTestHelper.setData(
1702             ISSUE_ONLY_BAREBONE_ID,
1703             SafetySourceTestData.issuesOnly(safetySourceTestData.criticalResolvingGeneralIssue)
1704         )
1705         safetyCenterTestHelper.setData(
1706             ISSUE_ONLY_ALL_OPTIONAL_ID,
1707             SafetySourceTestData.issuesOnly(safetySourceTestData.recommendationGeneralIssue)
1708         )
1709         safetyCenterTestHelper.setData(
1710             DYNAMIC_IN_STATELESS_ID,
1711             safetySourceTestData.unspecifiedWithIssue
1712         )
1713         safetyCenterTestHelper.setData(
1714             ISSUE_ONLY_IN_STATELESS_ID,
1715             SafetySourceTestData.issuesOnly(safetySourceTestData.informationIssue)
1716         )
1717     }
1718 
updateWorkProfileSourcesnull1719     private fun updateWorkProfileSources() {
1720         val managedSafetyCenterManager =
1721             getSafetyCenterManagerForUser(deviceState.workProfile().userHandle())
1722         managedSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
1723             DYNAMIC_DISABLED_ID,
1724             safetySourceTestData.informationWithIssueForWork
1725         )
1726         managedSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
1727             DYNAMIC_HIDDEN_ID,
1728             safetySourceTestData.informationWithIssueForWork
1729         )
1730         managedSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
1731             ISSUE_ONLY_ALL_OPTIONAL_ID,
1732             SafetySourceTestData.issuesOnly(safetySourceTestData.informationIssue)
1733         )
1734         managedSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
1735             DYNAMIC_IN_STATELESS_ID,
1736             safetySourceTestData.unspecifiedWithIssueForWork
1737         )
1738         managedSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
1739             ISSUE_ONLY_IN_STATELESS_ID,
1740             SafetySourceTestData.issuesOnly(safetySourceTestData.informationIssue)
1741         )
1742     }
1743 
updatePrivateProfileSourcesnull1744     private fun updatePrivateProfileSources() {
1745         val privateSafetyCenterManager =
1746             getSafetyCenterManagerForUser(deviceState.privateProfile().userHandle())
1747         privateSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
1748             DYNAMIC_DISABLED_ID,
1749             safetySourceTestData.informationWithIssueForPrivate
1750         )
1751         privateSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
1752             DYNAMIC_HIDDEN_ID,
1753             safetySourceTestData.informationWithIssueForPrivate
1754         )
1755         privateSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
1756             ISSUE_ONLY_ALL_OPTIONAL_ID,
1757             SafetySourceTestData.issuesOnly(safetySourceTestData.informationIssue)
1758         )
1759         privateSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
1760             DYNAMIC_IN_STATELESS_ID,
1761             safetySourceTestData.unspecifiedWithIssueForPrivate
1762         )
1763         privateSafetyCenterManager.setSafetySourceDataWithInteractAcrossUsersPermission(
1764             ISSUE_ONLY_IN_STATELESS_ID,
1765             SafetySourceTestData.issuesOnly(safetySourceTestData.informationIssue)
1766         )
1767     }
1768 }
1769