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.retail.data.repository
18 
19 import android.provider.Settings
20 import androidx.test.ext.junit.runners.AndroidJUnit4
21 import androidx.test.filters.SmallTest
22 import com.android.systemui.SysuiTestCase
23 import com.android.systemui.coroutines.collectLastValue
24 import com.android.systemui.retail.data.repository.impl.RetailModeSettingsRepository
25 import com.android.systemui.util.settings.FakeGlobalSettings
26 import com.google.common.truth.Truth.assertThat
27 import kotlinx.coroutines.ExperimentalCoroutinesApi
28 import kotlinx.coroutines.test.StandardTestDispatcher
29 import kotlinx.coroutines.test.TestScope
30 import kotlinx.coroutines.test.runCurrent
31 import kotlinx.coroutines.test.runTest
32 import org.junit.Test
33 import org.junit.runner.RunWith
34 
35 @OptIn(ExperimentalCoroutinesApi::class)
36 @SmallTest
37 @RunWith(AndroidJUnit4::class)
38 class RetailModeSettingsRepositoryTest : SysuiTestCase() {
39 
40     private val globalSettings = FakeGlobalSettings()
41 
42     private val testDispatcher = StandardTestDispatcher()
43     private val testScope = TestScope(testDispatcher)
44 
45     private val underTest =
46         RetailModeSettingsRepository(
47             globalSettings,
48             backgroundDispatcher = testDispatcher,
49             scope = testScope.backgroundScope,
50         )
51 
52     @Test
retailMode_defaultFalsenull53     fun retailMode_defaultFalse() =
54         testScope.runTest {
55             val value by collectLastValue(underTest.retailMode)
56             runCurrent()
57 
58             assertThat(value).isFalse()
59             assertThat(underTest.inRetailMode).isFalse()
60         }
61 
62     @Test
retailMode_falsenull63     fun retailMode_false() =
64         testScope.runTest {
65             val value by collectLastValue(underTest.retailMode)
66             runCurrent()
67 
68             globalSettings.putInt(SETTING, 0)
69 
70             assertThat(value).isFalse()
71             assertThat(underTest.inRetailMode).isFalse()
72         }
73 
74     @Test
retailMode_truenull75     fun retailMode_true() =
76         testScope.runTest {
77             val value by collectLastValue(underTest.retailMode)
78             runCurrent()
79 
80             globalSettings.putInt(SETTING, 1)
81 
82             assertThat(value).isTrue()
83             assertThat(underTest.inRetailMode).isTrue()
84         }
85 
86     companion object {
87         private const val SETTING = Settings.Global.DEVICE_DEMO_MODE
88     }
89 }
90