Loading src/com/android/customization/picker/notifications/domain/interactor/NotificationsSnapshotRestorer.kt +12 −9 Original line number Diff line number Diff line Loading @@ -40,16 +40,19 @@ class NotificationsSnapshotRestorer( store: SnapshotStore, ): RestorableSnapshot { snapshotStore = store // The initial snapshot should be returned and stored before storing additional snapshots. return snapshot( NotificationSnapshotModel(interactor.isShowNotificationsOnLockScreenEnabled().value) ) .also { backgroundScope.launch { interactor.isShowNotificationsOnLockScreenEnabled.collect { interactor.isShowNotificationsOnLockScreenEnabled().collect { storeSnapshot( NotificationSnapshotModel(isShowNotificationsOnLockScreenEnabled = it) ) } } return snapshot( NotificationSnapshotModel(interactor.isShowNotificationsOnLockScreenEnabled.value) ) } } override suspend fun restoreToSnapshot(snapshot: RestorableSnapshot) { Loading src/com/android/customization/picker/notifications/ui/binder/NotificationSectionBinder.kt +1 −1 Original line number Diff line number Diff line Loading @@ -44,7 +44,7 @@ object NotificationSectionBinder { lifecycleOwner.lifecycleScope.launch { lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { launch { viewModel.isSwitchOn.collect { switch.isChecked = it } } launch { viewModel.isSwitchOn().collect { switch.isChecked = it } } } } } Loading src/com/android/customization/picker/notifications/ui/viewmodel/NotificationSectionViewModel.kt +2 −2 Original line number Diff line number Diff line Loading @@ -35,14 +35,14 @@ constructor( ) : ViewModel() { /** Whether the switch should be on. */ val isSwitchOn: Flow<Boolean> = interactor.isShowNotificationsOnLockScreenEnabled suspend fun isSwitchOn(): Flow<Boolean> = interactor.isShowNotificationsOnLockScreenEnabled() /** Notifies that the section has been clicked. */ fun onClicked() { viewModelScope.launch { interactor.toggleShowNotificationsOnLockscreenEnabled() logger.logLockScreenNotificationApplied( interactor.isShowNotificationsOnLockScreenEnabled.value interactor.isShowNotificationsOnLockScreenEnabled().value ) } } Loading tests/robotests/src/com/android/customization/model/notifications/domain/interactor/NotificationsSnapshotRestorerTest.kt 0 → 100644 +128 −0 Original line number Diff line number Diff line /* * Copyright (C) 2024 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package com.android.customization.model.notifications.domain.interactor import android.provider.Settings import androidx.test.filters.SmallTest import com.android.customization.picker.notifications.domain.interactor.NotificationsSnapshotRestorer import com.android.systemui.shared.notifications.data.repository.NotificationSettingsRepository import com.android.systemui.shared.notifications.domain.interactor.NotificationSettingsInteractor import com.android.systemui.shared.settings.data.repository.FakeSecureSettingsRepository import com.android.wallpaper.testing.FakeSnapshotStore import com.android.wallpaper.testing.collectLastValue import com.google.common.truth.Truth.assertThat import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.setMain import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner @OptIn(ExperimentalCoroutinesApi::class) @SmallTest @RunWith(RobolectricTestRunner::class) class NotificationsSnapshotRestorerTest { private lateinit var underTest: NotificationsSnapshotRestorer private lateinit var fakeSecureSettingsRepository: FakeSecureSettingsRepository private lateinit var interactor: NotificationSettingsInteractor private lateinit var testScope: TestScope @Before fun setUp() { val testDispatcher = StandardTestDispatcher() Dispatchers.setMain(testDispatcher) testScope = TestScope(testDispatcher) fakeSecureSettingsRepository = FakeSecureSettingsRepository() interactor = NotificationSettingsInteractor( repository = NotificationSettingsRepository( scope = testScope.backgroundScope, backgroundDispatcher = testDispatcher, secureSettingsRepository = fakeSecureSettingsRepository, ), ) underTest = NotificationsSnapshotRestorer( interactor = interactor, backgroundScope = testScope.backgroundScope ) } @Test fun setUpAndRestore_Active() = testScope.runTest { fakeSecureSettingsRepository.setInt(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1) val showNotifs = collectLastValue(interactor.isShowNotificationsOnLockScreenEnabled()) val store = FakeSnapshotStore() store.store(underTest.setUpSnapshotRestorer(store = store)) val initialSnapshot = store.retrieve() underTest.restoreToSnapshot(snapshot = initialSnapshot) assertThat(showNotifs()).isTrue() } @Test fun setUpAndRestore_Inactive() = testScope.runTest { fakeSecureSettingsRepository.setInt(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0) val showNotifs = collectLastValue(interactor.isShowNotificationsOnLockScreenEnabled()) val store = FakeSnapshotStore() store.store(underTest.setUpSnapshotRestorer(store = store)) val initialSnapshot = store.retrieve() underTest.restoreToSnapshot(snapshot = initialSnapshot) assertThat(showNotifs()).isFalse() } @Test fun setUp_deactivate_restoreToActive() = runTest { fakeSecureSettingsRepository.setInt(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1) val showNotifs = collectLastValue(interactor.isShowNotificationsOnLockScreenEnabled()) val store = FakeSnapshotStore() store.store(underTest.setUpSnapshotRestorer(store = store)) val initialSnapshot = store.retrieve() fakeSecureSettingsRepository.setInt(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0) underTest.restoreToSnapshot(snapshot = initialSnapshot) assertThat(showNotifs()).isTrue() } @Test fun setUp_activate_restoreToInactive() = runTest { fakeSecureSettingsRepository.setInt(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0) val showNotifs = collectLastValue(interactor.isShowNotificationsOnLockScreenEnabled()) val store = FakeSnapshotStore() store.store(underTest.setUpSnapshotRestorer(store = store)) val initialSnapshot = store.retrieve() fakeSecureSettingsRepository.setInt(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1) underTest.restoreToSnapshot(snapshot = initialSnapshot) assertThat(showNotifs()).isFalse() } } tests/robotests/src/com/android/customization/picker/notifications/ui/viewmodel/NotificationSectionViewModelTest.kt +1 −1 Original line number Diff line number Diff line Loading @@ -80,7 +80,7 @@ class NotificationSectionViewModelTest { @Test fun `toggles back and forth`() = testScope.runTest { val isSwitchOn = collectLastValue(underTest.isSwitchOn) val isSwitchOn = collectLastValue(underTest.isSwitchOn()) val initialIsSwitchOn = isSwitchOn() Loading Loading
src/com/android/customization/picker/notifications/domain/interactor/NotificationsSnapshotRestorer.kt +12 −9 Original line number Diff line number Diff line Loading @@ -40,16 +40,19 @@ class NotificationsSnapshotRestorer( store: SnapshotStore, ): RestorableSnapshot { snapshotStore = store // The initial snapshot should be returned and stored before storing additional snapshots. return snapshot( NotificationSnapshotModel(interactor.isShowNotificationsOnLockScreenEnabled().value) ) .also { backgroundScope.launch { interactor.isShowNotificationsOnLockScreenEnabled.collect { interactor.isShowNotificationsOnLockScreenEnabled().collect { storeSnapshot( NotificationSnapshotModel(isShowNotificationsOnLockScreenEnabled = it) ) } } return snapshot( NotificationSnapshotModel(interactor.isShowNotificationsOnLockScreenEnabled.value) ) } } override suspend fun restoreToSnapshot(snapshot: RestorableSnapshot) { Loading
src/com/android/customization/picker/notifications/ui/binder/NotificationSectionBinder.kt +1 −1 Original line number Diff line number Diff line Loading @@ -44,7 +44,7 @@ object NotificationSectionBinder { lifecycleOwner.lifecycleScope.launch { lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { launch { viewModel.isSwitchOn.collect { switch.isChecked = it } } launch { viewModel.isSwitchOn().collect { switch.isChecked = it } } } } } Loading
src/com/android/customization/picker/notifications/ui/viewmodel/NotificationSectionViewModel.kt +2 −2 Original line number Diff line number Diff line Loading @@ -35,14 +35,14 @@ constructor( ) : ViewModel() { /** Whether the switch should be on. */ val isSwitchOn: Flow<Boolean> = interactor.isShowNotificationsOnLockScreenEnabled suspend fun isSwitchOn(): Flow<Boolean> = interactor.isShowNotificationsOnLockScreenEnabled() /** Notifies that the section has been clicked. */ fun onClicked() { viewModelScope.launch { interactor.toggleShowNotificationsOnLockscreenEnabled() logger.logLockScreenNotificationApplied( interactor.isShowNotificationsOnLockScreenEnabled.value interactor.isShowNotificationsOnLockScreenEnabled().value ) } } Loading
tests/robotests/src/com/android/customization/model/notifications/domain/interactor/NotificationsSnapshotRestorerTest.kt 0 → 100644 +128 −0 Original line number Diff line number Diff line /* * Copyright (C) 2024 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package com.android.customization.model.notifications.domain.interactor import android.provider.Settings import androidx.test.filters.SmallTest import com.android.customization.picker.notifications.domain.interactor.NotificationsSnapshotRestorer import com.android.systemui.shared.notifications.data.repository.NotificationSettingsRepository import com.android.systemui.shared.notifications.domain.interactor.NotificationSettingsInteractor import com.android.systemui.shared.settings.data.repository.FakeSecureSettingsRepository import com.android.wallpaper.testing.FakeSnapshotStore import com.android.wallpaper.testing.collectLastValue import com.google.common.truth.Truth.assertThat import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.setMain import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner @OptIn(ExperimentalCoroutinesApi::class) @SmallTest @RunWith(RobolectricTestRunner::class) class NotificationsSnapshotRestorerTest { private lateinit var underTest: NotificationsSnapshotRestorer private lateinit var fakeSecureSettingsRepository: FakeSecureSettingsRepository private lateinit var interactor: NotificationSettingsInteractor private lateinit var testScope: TestScope @Before fun setUp() { val testDispatcher = StandardTestDispatcher() Dispatchers.setMain(testDispatcher) testScope = TestScope(testDispatcher) fakeSecureSettingsRepository = FakeSecureSettingsRepository() interactor = NotificationSettingsInteractor( repository = NotificationSettingsRepository( scope = testScope.backgroundScope, backgroundDispatcher = testDispatcher, secureSettingsRepository = fakeSecureSettingsRepository, ), ) underTest = NotificationsSnapshotRestorer( interactor = interactor, backgroundScope = testScope.backgroundScope ) } @Test fun setUpAndRestore_Active() = testScope.runTest { fakeSecureSettingsRepository.setInt(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1) val showNotifs = collectLastValue(interactor.isShowNotificationsOnLockScreenEnabled()) val store = FakeSnapshotStore() store.store(underTest.setUpSnapshotRestorer(store = store)) val initialSnapshot = store.retrieve() underTest.restoreToSnapshot(snapshot = initialSnapshot) assertThat(showNotifs()).isTrue() } @Test fun setUpAndRestore_Inactive() = testScope.runTest { fakeSecureSettingsRepository.setInt(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0) val showNotifs = collectLastValue(interactor.isShowNotificationsOnLockScreenEnabled()) val store = FakeSnapshotStore() store.store(underTest.setUpSnapshotRestorer(store = store)) val initialSnapshot = store.retrieve() underTest.restoreToSnapshot(snapshot = initialSnapshot) assertThat(showNotifs()).isFalse() } @Test fun setUp_deactivate_restoreToActive() = runTest { fakeSecureSettingsRepository.setInt(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1) val showNotifs = collectLastValue(interactor.isShowNotificationsOnLockScreenEnabled()) val store = FakeSnapshotStore() store.store(underTest.setUpSnapshotRestorer(store = store)) val initialSnapshot = store.retrieve() fakeSecureSettingsRepository.setInt(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0) underTest.restoreToSnapshot(snapshot = initialSnapshot) assertThat(showNotifs()).isTrue() } @Test fun setUp_activate_restoreToInactive() = runTest { fakeSecureSettingsRepository.setInt(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0) val showNotifs = collectLastValue(interactor.isShowNotificationsOnLockScreenEnabled()) val store = FakeSnapshotStore() store.store(underTest.setUpSnapshotRestorer(store = store)) val initialSnapshot = store.retrieve() fakeSecureSettingsRepository.setInt(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1) underTest.restoreToSnapshot(snapshot = initialSnapshot) assertThat(showNotifs()).isFalse() } }
tests/robotests/src/com/android/customization/picker/notifications/ui/viewmodel/NotificationSectionViewModelTest.kt +1 −1 Original line number Diff line number Diff line Loading @@ -80,7 +80,7 @@ class NotificationSectionViewModelTest { @Test fun `toggles back and forth`() = testScope.runTest { val isSwitchOn = collectLastValue(underTest.isSwitchOn) val isSwitchOn = collectLastValue(underTest.isSwitchOn()) val initialIsSwitchOn = isSwitchOn() Loading