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 18 package com.android.customization.model.notifications.domain.interactor 19 20 import android.provider.Settings 21 import androidx.test.filters.SmallTest 22 import com.android.customization.picker.notifications.domain.interactor.NotificationsSnapshotRestorer 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.FakeSnapshotStore 28 import com.android.wallpaper.testing.collectLastValue 29 import com.google.common.truth.Truth.assertThat 30 import kotlinx.coroutines.Dispatchers 31 import kotlinx.coroutines.ExperimentalCoroutinesApi 32 import kotlinx.coroutines.test.StandardTestDispatcher 33 import kotlinx.coroutines.test.TestScope 34 import kotlinx.coroutines.test.runTest 35 import kotlinx.coroutines.test.setMain 36 import org.junit.Before 37 import org.junit.Test 38 import org.junit.runner.RunWith 39 import org.robolectric.RobolectricTestRunner 40 41 @OptIn(ExperimentalCoroutinesApi::class) 42 @SmallTest 43 @RunWith(RobolectricTestRunner::class) 44 class NotificationsSnapshotRestorerTest { 45 46 private lateinit var underTest: NotificationsSnapshotRestorer 47 private lateinit var fakeSecureSettingsRepository: FakeSecureSettingsRepository 48 private lateinit var fakeSystemSettingsRepository: FakeSystemSettingsRepository 49 private lateinit var interactor: NotificationSettingsInteractor 50 51 private lateinit var testScope: TestScope 52 53 @Before setUpnull54 fun setUp() { 55 val testDispatcher = StandardTestDispatcher() 56 Dispatchers.setMain(testDispatcher) 57 testScope = TestScope(testDispatcher) 58 fakeSecureSettingsRepository = FakeSecureSettingsRepository() 59 fakeSystemSettingsRepository = FakeSystemSettingsRepository() 60 interactor = 61 NotificationSettingsInteractor( 62 repository = 63 NotificationSettingsRepository( 64 backgroundScope = testScope.backgroundScope, 65 backgroundDispatcher = testDispatcher, 66 secureSettingsRepository = fakeSecureSettingsRepository, 67 systemSettingsRepository = fakeSystemSettingsRepository, 68 ), 69 ) 70 underTest = 71 NotificationsSnapshotRestorer( 72 interactor = interactor, 73 backgroundScope = testScope.backgroundScope 74 ) 75 } 76 77 @Test setUpAndRestore_Activenull78 fun setUpAndRestore_Active() = 79 testScope.runTest { 80 fakeSecureSettingsRepository.setInt(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1) 81 val showNotifs = collectLastValue(interactor.isShowNotificationsOnLockScreenEnabled()) 82 83 val store = FakeSnapshotStore() 84 store.store(underTest.setUpSnapshotRestorer(store = store)) 85 val initialSnapshot = store.retrieve() 86 underTest.restoreToSnapshot(snapshot = initialSnapshot) 87 88 assertThat(showNotifs()).isTrue() 89 } 90 91 @Test setUpAndRestore_Inactivenull92 fun setUpAndRestore_Inactive() = 93 testScope.runTest { 94 fakeSecureSettingsRepository.setInt(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0) 95 val showNotifs = collectLastValue(interactor.isShowNotificationsOnLockScreenEnabled()) 96 97 val store = FakeSnapshotStore() 98 store.store(underTest.setUpSnapshotRestorer(store = store)) 99 val initialSnapshot = store.retrieve() 100 underTest.restoreToSnapshot(snapshot = initialSnapshot) 101 102 assertThat(showNotifs()).isFalse() 103 } 104 105 @Test <lambda>null106 fun setUp_deactivate_restoreToActive() = runTest { 107 fakeSecureSettingsRepository.setInt(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1) 108 val showNotifs = collectLastValue(interactor.isShowNotificationsOnLockScreenEnabled()) 109 val store = FakeSnapshotStore() 110 store.store(underTest.setUpSnapshotRestorer(store = store)) 111 val initialSnapshot = store.retrieve() 112 113 fakeSecureSettingsRepository.setInt(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0) 114 underTest.restoreToSnapshot(snapshot = initialSnapshot) 115 116 assertThat(showNotifs()).isTrue() 117 } 118 119 @Test <lambda>null120 fun setUp_activate_restoreToInactive() = runTest { 121 fakeSecureSettingsRepository.setInt(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0) 122 val showNotifs = collectLastValue(interactor.isShowNotificationsOnLockScreenEnabled()) 123 val store = FakeSnapshotStore() 124 store.store(underTest.setUpSnapshotRestorer(store = store)) 125 val initialSnapshot = store.retrieve() 126 127 fakeSecureSettingsRepository.setInt(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1) 128 underTest.restoreToSnapshot(snapshot = initialSnapshot) 129 130 assertThat(showNotifs()).isFalse() 131 } 132 } 133