1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.systemui.shared.notifications.data.repository
18 
19 import android.provider.Settings
20 import android.provider.Settings.Secure.ZEN_DURATION_FOREVER
21 import androidx.test.ext.junit.runners.AndroidJUnit4
22 import androidx.test.filters.SmallTest
23 import com.android.systemui.SysuiTestCase
24 import com.android.systemui.coroutines.collectLastValue
25 import com.android.systemui.shared.settings.data.repository.FakeSecureSettingsRepository
26 import com.android.systemui.shared.settings.data.repository.FakeSystemSettingsRepository
27 import com.google.common.truth.Truth.assertThat
28 import kotlinx.coroutines.test.StandardTestDispatcher
29 import kotlinx.coroutines.test.TestScope
30 import kotlinx.coroutines.test.runTest
31 import org.junit.Before
32 import org.junit.Test
33 import org.junit.runner.RunWith
34 
35 @SmallTest
36 @RunWith(AndroidJUnit4::class)
37 class NotificationSettingsRepositoryTest : SysuiTestCase() {
38 
39     private lateinit var underTest: NotificationSettingsRepository
40 
41     private lateinit var testScope: TestScope
42     private lateinit var secureSettingsRepository: FakeSecureSettingsRepository
43     private lateinit var systemSettingsRepository: FakeSystemSettingsRepository
44 
45     @Before
setUpnull46     fun setUp() {
47         val testDispatcher = StandardTestDispatcher()
48         testScope = TestScope(testDispatcher)
49         secureSettingsRepository = FakeSecureSettingsRepository()
50         systemSettingsRepository = FakeSystemSettingsRepository()
51 
52         underTest =
53             NotificationSettingsRepository(
54                 backgroundScope = testScope.backgroundScope,
55                 backgroundDispatcher = testDispatcher,
56                 secureSettingsRepository = secureSettingsRepository,
57                 systemSettingsRepository = systemSettingsRepository,
58             )
59     }
60 
61     @Test
getIsShowNotificationsOnLockscreenEnablednull62     fun getIsShowNotificationsOnLockscreenEnabled() =
63         testScope.runTest {
64             val showNotifs by collectLastValue(underTest.isShowNotificationsOnLockScreenEnabled())
65 
66             secureSettingsRepository.setInt(
67                 name = Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
68                 value = 1,
69             )
70             assertThat(showNotifs).isEqualTo(true)
71 
72             secureSettingsRepository.setInt(
73                 name = Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
74                 value = 0,
75             )
76             assertThat(showNotifs).isEqualTo(false)
77         }
78 
79     @Test
setIsShowNotificationsOnLockscreenEnablednull80     fun setIsShowNotificationsOnLockscreenEnabled() =
81         testScope.runTest {
82             val showNotifs by collectLastValue(underTest.isShowNotificationsOnLockScreenEnabled())
83 
84             underTest.setShowNotificationsOnLockscreenEnabled(true)
85             assertThat(showNotifs).isEqualTo(true)
86 
87             underTest.setShowNotificationsOnLockscreenEnabled(false)
88             assertThat(showNotifs).isEqualTo(false)
89         }
90 
91     @Test
getIsNotificationHistoryEnablednull92     fun getIsNotificationHistoryEnabled() =
93         testScope.runTest {
94             val historyEnabled by collectLastValue(underTest.isNotificationHistoryEnabled)
95 
96             secureSettingsRepository.setInt(
97                 name = Settings.Secure.NOTIFICATION_HISTORY_ENABLED,
98                 value = 1,
99             )
100             assertThat(historyEnabled).isEqualTo(true)
101 
102             secureSettingsRepository.setInt(
103                 name = Settings.Secure.NOTIFICATION_HISTORY_ENABLED,
104                 value = 0,
105             )
106             assertThat(historyEnabled).isEqualTo(false)
107         }
108 
109     @Test
testGetIsCooldownEnablednull110     fun testGetIsCooldownEnabled() =
111         testScope.runTest {
112             val cooldownEnabled by collectLastValue(underTest.isCooldownEnabled)
113 
114             systemSettingsRepository.setInt(
115                 name = Settings.System.NOTIFICATION_COOLDOWN_ENABLED,
116                 value = 1,
117             )
118             assertThat(cooldownEnabled).isEqualTo(true)
119 
120             systemSettingsRepository.setInt(
121                 name = Settings.System.NOTIFICATION_COOLDOWN_ENABLED,
122                 value = 0,
123             )
124             assertThat(cooldownEnabled).isEqualTo(false)
125         }
126 
127     @Test
zenDurationnull128     fun zenDuration() =
129         testScope.runTest {
130             val zenDuration by collectLastValue(underTest.zenDuration)
131 
132             secureSettingsRepository.setInt(
133                 name = Settings.Secure.ZEN_DURATION,
134                 value = 60,
135             )
136             assertThat(zenDuration).isEqualTo(60)
137 
138             secureSettingsRepository.setInt(
139                 name = Settings.Secure.ZEN_DURATION,
140                 value = ZEN_DURATION_FOREVER,
141             )
142             assertThat(zenDuration).isEqualTo(ZEN_DURATION_FOREVER)
143         }
144 }
145