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

Commit 1a823307 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "[SB][ComposeIcons] Add DataSaverStatusInteractor" into main

parents af980cf5 759fcffd
Loading
Loading
Loading
Loading
+57 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.systemui.statusbar.policy.domain.interactor

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.kosmos.collectLastValue
import com.android.systemui.kosmos.runTest
import com.android.systemui.kosmos.useUnconfinedTestDispatcher
import com.android.systemui.statusbar.policy.fakeDataSaverController
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith

@SmallTest
@RunWith(AndroidJUnit4::class)
class DataSaverStatusInteractorTest : SysuiTestCase() {

    private val kosmos = testKosmos().useUnconfinedTestDispatcher()
    private val underTest = kosmos.dataSaverStatusInteractor

    @Test
    fun isEnabled_initial_matchesFakeControllerDefaults() =
        kosmos.runTest {
            val state by collectLastValue(underTest.isEnabled)
            assertThat(state).isEqualTo(fakeDataSaverController.isDataSaverEnabled)
            assertThat(state).isEqualTo(false)
        }

    @Test
    fun isEnabled_updatesOnDataSaverChanged() =
        kosmos.runTest {
            val state by collectLastValue(underTest.isEnabled)

            fakeDataSaverController.setDataSaverEnabled(true)
            assertThat(state).isEqualTo(true)

            fakeDataSaverController.setDataSaverEnabled(false)
            assertThat(state).isEqualTo(false)
        }
}
+55 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.systemui.statusbar.policy.domain.interactor

import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.statusbar.policy.DataSaverController
import com.android.systemui.utils.coroutines.flow.conflatedCallbackFlow
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.stateIn

/** Interactor responsible for determining if data saver is enabled. */
@SysUISingleton
class DataSaverStatusInteractor
@Inject
constructor(
    private val controller: DataSaverController,
    @Background private val scope: CoroutineScope,
    @Background private val bgDispatcher: CoroutineDispatcher,
) {
    /** The current data saver state. */
    val isEnabled: StateFlow<Boolean> =
        conflatedCallbackFlow {
                val callback =
                    DataSaverController.Listener { isDataSaving -> trySend(isDataSaving) }
                controller.addCallback(callback)
                awaitClose { controller.removeCallback(callback) }
            }
            .flowOn(bgDispatcher)
            .stateIn(
                initialValue = controller.isDataSaverEnabled(),
                scope = scope,
                started = SharingStarted.WhileSubscribed(),
            )
}
+22 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.systemui.statusbar.policy

import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.Kosmos.Fixture
import com.android.systemui.utils.leaks.FakeDataSaverController

val Kosmos.fakeDataSaverController by Fixture { FakeDataSaverController(leakCheck) }
+31 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.systemui.statusbar.policy.domain.interactor

import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.testDispatcher
import com.android.systemui.kosmos.testScope
import com.android.systemui.statusbar.policy.fakeDataSaverController

val Kosmos.dataSaverStatusInteractor by
    Kosmos.Fixture {
        DataSaverStatusInteractor(
            controller = fakeDataSaverController,
            scope = testScope.backgroundScope,
            bgDispatcher = testDispatcher,
        )
    }