1 /*
2  * Copyright (C) 2024 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.intentresolver
18 
19 import com.android.intentresolver.annotation.JavaInterop
20 import com.android.intentresolver.data.repository.FakeUserRepository
21 import com.android.intentresolver.domain.interactor.UserInteractor
22 import com.android.intentresolver.shared.model.Profile
23 import com.android.intentresolver.shared.model.User
24 import com.google.common.truth.Truth.assertThat
25 import kotlinx.coroutines.Dispatchers
26 import kotlinx.coroutines.ExperimentalCoroutinesApi
27 import kotlinx.coroutines.test.runCurrent
28 import kotlinx.coroutines.test.runTest
29 import org.junit.Test
30 
31 @OptIn(ExperimentalCoroutinesApi::class, JavaInterop::class)
32 class ProfileAvailabilityTest {
33     private val personalUser = User(0, User.Role.PERSONAL)
34     private val workUser = User(10, User.Role.WORK)
35 
36     private val personalProfile = Profile(Profile.Type.PERSONAL, personalUser)
37     private val workProfile = Profile(Profile.Type.WORK, workUser)
38 
39     private val repository = FakeUserRepository(listOf(personalUser, workUser))
40     private val interactor = UserInteractor(repository, launchedAs = personalUser.handle)
41 
42     @Test
<lambda>null43     fun testProfileAvailable() = runTest {
44         val availability = ProfileAvailability(interactor, this, Dispatchers.IO)
45 
46         assertThat(availability.isAvailable(personalProfile)).isTrue()
47         assertThat(availability.isAvailable(workProfile)).isTrue()
48 
49         availability.requestQuietModeState(workProfile, true)
50         runCurrent()
51 
52         assertThat(availability.isAvailable(workProfile)).isFalse()
53 
54         availability.requestQuietModeState(workProfile, false)
55         runCurrent()
56 
57         assertThat(availability.isAvailable(workProfile)).isTrue()
58     }
59 
60     @Test
<lambda>null61     fun waitingToEnableProfile() = runTest {
62         val availability = ProfileAvailability(interactor, this, Dispatchers.IO)
63 
64         availability.requestQuietModeState(workProfile, true)
65         assertThat(availability.waitingToEnableProfile).isFalse()
66         runCurrent()
67 
68         availability.requestQuietModeState(workProfile, false)
69         assertThat(availability.waitingToEnableProfile).isTrue()
70         runCurrent()
71 
72         assertThat(availability.waitingToEnableProfile).isFalse()
73     }
74 }
75