Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ce8903ae authored by Xin Li's avatar Xin Li
Browse files

Merge 24Q3 (ab/AP3A.240905.001) to aosp-main-future

Bug: 347831320
Merged-In: If580a54fdb920a6a17f7c605caf5359a868f0ec0
Change-Id: I1ad2d8b89960ebcf41d2da9140dcc317d4f6cc47
parents f8b0d9d5 6b8492e7
Loading
Loading
Loading
Loading
+12 −9
Original line number Diff line number Diff line
@@ -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) {
+1 −1
Original line number Diff line number Diff line
@@ -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 } }
            }
        }
    }
+2 −2
Original line number Diff line number Diff line
@@ -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
            )
        }
    }
+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()
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -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()