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 
18 package com.android.customization.picker.notifications.ui.viewmodel
19 
20 import androidx.test.filters.SmallTest
21 import com.android.customization.module.logging.TestThemesUserEventLogger
22 import com.android.customization.module.logging.ThemesUserEventLogger
23 import com.android.systemui.shared.notifications.data.repository.NotificationSettingsRepository
24 import com.android.systemui.shared.notifications.domain.interactor.NotificationSettingsInteractor
25 import com.android.systemui.shared.settings.data.repository.FakeSecureSettingsRepository
26 import com.android.systemui.shared.settings.data.repository.FakeSystemSettingsRepository
27 import com.android.wallpaper.testing.collectLastValue
28 import com.google.common.truth.Truth.assertThat
29 import kotlinx.coroutines.Dispatchers
30 import kotlinx.coroutines.ExperimentalCoroutinesApi
31 import kotlinx.coroutines.test.TestScope
32 import kotlinx.coroutines.test.UnconfinedTestDispatcher
33 import kotlinx.coroutines.test.resetMain
34 import kotlinx.coroutines.test.runTest
35 import kotlinx.coroutines.test.setMain
36 import org.junit.After
37 import org.junit.Before
38 import org.junit.Test
39 import org.junit.runner.RunWith
40 import org.robolectric.RobolectricTestRunner
41 
42 @OptIn(ExperimentalCoroutinesApi::class)
43 @SmallTest
44 @RunWith(RobolectricTestRunner::class)
45 class NotificationSectionViewModelTest {
46 
47     private val logger: ThemesUserEventLogger = TestThemesUserEventLogger()
48 
49     private lateinit var underTest: NotificationSectionViewModel
50 
51     private lateinit var testScope: TestScope
52     private lateinit var interactor: NotificationSettingsInteractor
53 
54     @Before
setUpnull55     fun setUp() {
56         val testDispatcher = UnconfinedTestDispatcher()
57         Dispatchers.setMain(testDispatcher)
58         testScope = TestScope(testDispatcher)
59         interactor =
60             NotificationSettingsInteractor(
61                 repository =
62                     NotificationSettingsRepository(
63                         backgroundScope = testScope.backgroundScope,
64                         backgroundDispatcher = testDispatcher,
65                         secureSettingsRepository = FakeSecureSettingsRepository(),
66                         systemSettingsRepository = FakeSystemSettingsRepository(),
67                     ),
68             )
69 
70         underTest =
71             NotificationSectionViewModel(
72                 interactor = interactor,
73                 logger = logger,
74             )
75     }
76 
77     @After
tearDownnull78     fun tearDown() {
79         Dispatchers.resetMain()
80     }
81 
82     @Test
toggles back and forthnull83     fun `toggles back and forth`() =
84         testScope.runTest {
85             val isSwitchOn = collectLastValue(underTest.isSwitchOn())
86 
87             val initialIsSwitchOn = isSwitchOn()
88 
89             underTest.onClicked()
90             assertThat(isSwitchOn()).isNotEqualTo(initialIsSwitchOn)
91 
92             underTest.onClicked()
93             assertThat(isSwitchOn()).isEqualTo(initialIsSwitchOn)
94         }
95 }
96