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

Commit a22cce22 authored by Quang Luong's avatar Quang Luong Committed by Android (Google) Code Review
Browse files

Merge changes from topics "wifi-interactor-toggle", "wifi-toggle-tile" into main

* changes:
  Add wifi tile with toggle
  Add wifi tile toggle changes to WifiRepository
  Add separate mobile data tile
parents 57c7a87a 8c7fe2ce
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -2167,6 +2167,7 @@ flag {
      purpose: PURPOSE_BUGFIX
    }
}

flag {
    name: "dialog_anim_end_state_update"
    namespace: "systemui"
@@ -2186,3 +2187,12 @@ flag {
      purpose: PURPOSE_BUGFIX
    }
}

flag {

    name: "qs_split_internet_tile"
    namespace: "systemui"
    description: "Splits the Internet tile into Wifi (with internet info) + Mobile Data."
    bug: "201143076"
    is_fixed_read_only: true
}
+187 −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.qs.tiles

import android.os.Handler
import android.testing.TestableLooper
import android.testing.TestableLooper.RunWithLooper
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.internal.logging.MetricsLogger
import com.android.systemui.SysuiTestCase
import com.android.systemui.animation.Expandable
import com.android.systemui.classifier.FalsingManagerFake
import com.android.systemui.plugins.ActivityStarter
import com.android.systemui.plugins.statusbar.StatusBarStateController
import com.android.systemui.qs.QSHost
import com.android.systemui.qs.QsEventLogger
import com.android.systemui.qs.logging.QSLogger
import com.android.systemui.qs.tiles.base.shared.model.QSTileConfigProvider
import com.android.systemui.qs.tiles.base.shared.model.QSTileConfigTestBuilder
import com.android.systemui.qs.tiles.base.shared.model.QSTileState
import com.android.systemui.qs.tiles.base.shared.model.QSTileUIConfig
import com.android.systemui.qs.tiles.impl.cell.domain.interactor.MobileDataTileDataInteractor
import com.android.systemui.qs.tiles.impl.cell.domain.interactor.MobileDataTileUserActionInteractor
import com.android.systemui.qs.tiles.impl.cell.domain.model.MobileDataTileIcon
import com.android.systemui.qs.tiles.impl.cell.domain.model.MobileDataTileModel
import com.android.systemui.qs.tiles.impl.cell.ui.mapper.MobileDataTileMapper
import com.android.systemui.res.R
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.mock
import org.mockito.MockitoAnnotations
import org.mockito.kotlin.any
import org.mockito.kotlin.eq
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever

@SmallTest
@RunWith(AndroidJUnit4::class)
@RunWithLooper(setAsMainLooper = true)
class MobileDataTileTest : SysuiTestCase() {

    @Mock private lateinit var host: QSHost
    @Mock private lateinit var uiEventLogger: QsEventLogger
    @Mock private lateinit var metricsLogger: MetricsLogger
    @Mock private lateinit var statusBarStateController: StatusBarStateController
    @Mock private lateinit var activityStarter: ActivityStarter
    @Mock private lateinit var qsLogger: QSLogger
    @Mock private lateinit var qsTileConfigProvider: QSTileConfigProvider

    // Mocks for the new-arch components
    @Mock private lateinit var dataInteractor: MobileDataTileDataInteractor
    @Mock private lateinit var tileMapper: MobileDataTileMapper
    @Mock private lateinit var userActionInteractor: MobileDataTileUserActionInteractor

    private lateinit var testableLooper: TestableLooper
    private lateinit var underTest: MobileDataTile
    private val tileDataFlow =
        MutableStateFlow<MobileDataTileModel>(
            MobileDataTileModel(
                isSimActive = false,
                isEnabled = false,
                icon = MobileDataTileIcon.SignalIcon(4),
            )
        )

    @Before
    fun setUp() {
        MockitoAnnotations.initMocks(this)
        testableLooper = TestableLooper.get(this)

        // Mock host context for resource loading and user
        whenever(host.context).thenReturn(context)
        whenever(host.userContext).thenReturn(context)
        whenever(host.userId).thenReturn(0)

        // Mock the data interactor to return our controllable flow
        whenever(dataInteractor.tileData()).thenReturn(tileDataFlow)

        // Provide a default config
        whenever(qsTileConfigProvider.getConfig(MobileDataTile.TILE_SPEC))
            .thenReturn(
                QSTileConfigTestBuilder.build {
                    uiConfig =
                        QSTileUIConfig.Resource(
                            iconRes = R.drawable.ic_signal_strength_zero_bar_no_internet,
                            labelRes = R.string.quick_settings_cellular_detail_title,
                        )
                }
            )

        underTest =
            MobileDataTile(
                host,
                uiEventLogger,
                testableLooper.looper,
                Handler(testableLooper.looper),
                FalsingManagerFake(),
                metricsLogger,
                statusBarStateController,
                activityStarter,
                qsLogger,
                qsTileConfigProvider,
                dataInteractor,
                tileMapper,
                userActionInteractor,
            )

        underTest.initialize()
        underTest.setListening(this, true) // Start listening to trigger data collection
        testableLooper.processAllMessages()
    }

    @After
    fun tearDown() {
        underTest.setListening(this, false)
        underTest.destroy()
        testableLooper.processAllMessages()
    }

    @Test
    fun tileDataChanges_triggersMapperWithNewModel() {
        val model =
            MobileDataTileModel(
                isSimActive = true,
                isEnabled = true,
                icon = MobileDataTileIcon.SignalIcon(4),
            )
        // Mock a placeholder state to be returned by the mapper
        whenever(tileMapper.map(any(), any())).thenReturn(mock(QSTileState::class.java))

        // Act: Emit a new model from the data source
        tileDataFlow.value = model
        testableLooper.processAllMessages()

        // Assert: Verify the tile passed the model to the mapper
        verify(tileMapper).map(any(), eq(model))
    }

    @Test
    fun click_callsUserActionInteractor() = runTest {
        // Act: Use the public click() method
        underTest.click(mock(Expandable::class.java))
        testableLooper.processAllMessages()

        // Assert
        verify(userActionInteractor).handleClick(any())
    }

    @Test
    fun isAvailable_fromDataInteractor_isTrue() {
        // Arrange
        whenever(dataInteractor.isAvailable()).thenReturn(true)

        // Act & Assert
        assertThat(underTest.isAvailable).isTrue()
    }

    @Test
    fun isAvailable_fromDataInteractor_isFalse() {
        // Arrange
        whenever(dataInteractor.isAvailable()).thenReturn(false)

        // Act & Assert
        assertThat(underTest.isAvailable).isFalse()
    }
}
+173 −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.qs.tiles

import android.os.Handler
import android.testing.TestableLooper
import android.testing.TestableLooper.RunWithLooper
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.internal.logging.MetricsLogger
import com.android.systemui.SysuiTestCase
import com.android.systemui.animation.Expandable
import com.android.systemui.classifier.FalsingManagerFake
import com.android.systemui.plugins.ActivityStarter
import com.android.systemui.plugins.statusbar.StatusBarStateController
import com.android.systemui.qs.QSHost
import com.android.systemui.qs.QsEventLogger
import com.android.systemui.qs.logging.QSLogger
import com.android.systemui.qs.tiles.base.shared.model.QSTileConfigProvider
import com.android.systemui.qs.tiles.base.shared.model.QSTileConfigTestBuilder
import com.android.systemui.qs.tiles.base.shared.model.QSTileState
import com.android.systemui.qs.tiles.base.shared.model.QSTileUIConfig
import com.android.systemui.qs.tiles.impl.wifi.domain.interactor.WifiTileDataInteractor
import com.android.systemui.qs.tiles.impl.wifi.domain.interactor.WifiTileUserActionInteractor
import com.android.systemui.qs.tiles.impl.wifi.domain.model.WifiTileModel
import com.android.systemui.qs.tiles.impl.wifi.ui.mapper.WifiTileMapper
import com.android.systemui.res.R
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.mock
import org.mockito.MockitoAnnotations
import org.mockito.kotlin.any
import org.mockito.kotlin.eq
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever

@SmallTest
@RunWith(AndroidJUnit4::class)
@RunWithLooper(setAsMainLooper = true)
class WifiTileTest : SysuiTestCase() {

    @Mock private lateinit var host: QSHost
    @Mock private lateinit var uiEventLogger: QsEventLogger
    @Mock private lateinit var metricsLogger: MetricsLogger
    @Mock private lateinit var statusBarStateController: StatusBarStateController
    @Mock private lateinit var activityStarter: ActivityStarter
    @Mock private lateinit var qsLogger: QSLogger
    @Mock private lateinit var qsTileConfigProvider: QSTileConfigProvider

    // Mocks for the new-arch components
    @Mock private lateinit var dataInteractor: WifiTileDataInteractor
    @Mock private lateinit var tileMapper: WifiTileMapper
    @Mock private lateinit var userActionInteractor: WifiTileUserActionInteractor

    private lateinit var testableLooper: TestableLooper
    private lateinit var underTest: WifiTile
    private val tileDataFlow =
        MutableStateFlow<WifiTileModel>(mock(WifiTileModel.Inactive::class.java))

    @Before
    fun setUp() {
        MockitoAnnotations.initMocks(this)
        testableLooper = TestableLooper.get(this)

        whenever(host.context).thenReturn(context)
        whenever(host.userContext).thenReturn(context)
        whenever(host.userId).thenReturn(0)

        whenever(dataInteractor.tileData()).thenReturn(tileDataFlow)

        whenever(qsTileConfigProvider.getConfig(WifiTile.TILE_SPEC))
            .thenReturn(
                QSTileConfigTestBuilder.build {
                    uiConfig =
                        QSTileUIConfig.Resource(
                            iconRes = R.drawable.ic_signal_wifi_off,
                            labelRes = R.string.quick_settings_internet_label,
                        )
                }
            )

        underTest =
            WifiTile(
                host,
                uiEventLogger,
                testableLooper.looper,
                Handler(testableLooper.looper),
                FalsingManagerFake(),
                metricsLogger,
                statusBarStateController,
                activityStarter,
                qsLogger,
                qsTileConfigProvider,
                dataInteractor,
                tileMapper,
                userActionInteractor,
            )

        underTest.initialize()
        underTest.setListening(this, true)
        testableLooper.processAllMessages()
    }

    @After
    fun tearDown() {
        underTest.setListening(this, false)
        underTest.destroy()
        testableLooper.processAllMessages()
    }

    @Test
    fun tileDataChanges_triggersMapperWithNewModel() {
        val model = mock(WifiTileModel.Active::class.java)
        whenever(tileMapper.map(any(), any())).thenReturn(mock(QSTileState::class.java))

        tileDataFlow.value = model
        testableLooper.processAllMessages()

        verify(tileMapper).map(any(), eq(model))
    }

    @Test
    fun click_callsUserActionInteractor() = runTest {
        val expandable = mock(Expandable::class.java)
        underTest.click(expandable)
        testableLooper.processAllMessages()

        verify(userActionInteractor).handleClick(expandable)
    }

    @Test
    fun secondaryClick_callsUserActionInteractor() {
        val expandable = mock(Expandable::class.java)
        underTest.secondaryClick(expandable)
        testableLooper.processAllMessages()

        verify(userActionInteractor).handleSecondaryClick(expandable)
    }

    @Test
    fun isAvailable_fromDataInteractor_isTrue() {
        whenever(dataInteractor.isAvailable()).thenReturn(true)

        assertThat(underTest.isAvailable).isTrue()
    }

    @Test
    fun isAvailable_fromDataInteractor_isFalse() {
        whenever(dataInteractor.isAvailable()).thenReturn(false)

        assertThat(underTest.isAvailable).isFalse()
    }
}
+216 −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.qs.tiles.impl.cell.domain.interactor

import android.os.UserHandle
import android.platform.test.annotations.RequiresFlagsDisabled
import android.platform.test.annotations.RequiresFlagsEnabled
import android.platform.test.flag.junit.CheckFlagsRule
import android.platform.test.flag.junit.DeviceFlagsValueProvider
import android.platform.test.flag.junit.FlagsParameterization
import androidx.test.filters.SmallTest
import com.android.settingslib.graph.SignalDrawable
import com.android.systemui.Flags as AconfigFlags
import com.android.systemui.Flags.FLAG_QS_SPLIT_INTERNET_TILE
import com.android.systemui.SysuiTestCase
import com.android.systemui.common.shared.model.ContentDescription
import com.android.systemui.common.shared.model.Icon
import com.android.systemui.flags.Flags
import com.android.systemui.flags.andSceneContainer
import com.android.systemui.flags.fake
import com.android.systemui.flags.featureFlagsClassic
import com.android.systemui.kosmos.collectLastValue
import com.android.systemui.kosmos.runCurrent
import com.android.systemui.kosmos.runTest
import com.android.systemui.kosmos.testScope
import com.android.systemui.log.table.logcatTableLogBuffer
import com.android.systemui.qs.pipeline.shared.pipelineFlagsRepository
import com.android.systemui.qs.tiles.impl.cell.domain.model.MobileDataTileIcon
import com.android.systemui.qs.tiles.impl.cell.domain.model.MobileDataTileModel
import com.android.systemui.res.R
import com.android.systemui.statusbar.pipeline.mobile.data.model.DataConnectionState
import com.android.systemui.statusbar.pipeline.mobile.data.repository.FakeMobileConnectionRepository
import com.android.systemui.statusbar.pipeline.mobile.data.repository.fake
import com.android.systemui.statusbar.pipeline.mobile.data.repository.mobileConnectionsRepository
import com.android.systemui.statusbar.pipeline.mobile.domain.interactor.MobileIconsInteractor
import com.android.systemui.statusbar.pipeline.mobile.domain.interactor.MobileIconsInteractorImpl
import com.android.systemui.statusbar.pipeline.shared.data.repository.connectivityRepository
import com.android.systemui.statusbar.policy.data.repository.userSetupRepository
import com.android.systemui.testKosmos
import com.android.systemui.util.CarrierConfigTracker
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.mock
import platform.test.runner.parameterized.ParameterizedAndroidJunit4
import platform.test.runner.parameterized.Parameters

@SmallTest
@RunWith(ParameterizedAndroidJunit4::class)
@OptIn(ExperimentalCoroutinesApi::class)
class MobileDataTileDataInteractorTest(flags: FlagsParameterization) : SysuiTestCase() {

    @get:Rule val checkFlagsRule: CheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule()

    init {
        mSetFlagsRule.setFlagsParameterization(flags)
    }

    private val kosmos = testKosmos()
    private val testScope = kosmos.testScope

    // Fakes needed for MobileIconsInteractorImpl
    private val connectivityRepository = kosmos.connectivityRepository
    private val mobileConnectionsRepository = kosmos.mobileConnectionsRepository
    private val userSetupRepo = kosmos.userSetupRepository
    private val featureFlags = kosmos.featureFlagsClassic
    private val pipelineFlagsRepository = kosmos.pipelineFlagsRepository
    private val carrierConfigTracker: CarrierConfigTracker = mock()

    // Real MobileIconsInteractor, fed by fakes
    private var mobileIconsInteractor: MobileIconsInteractor =
        MobileIconsInteractorImpl(
            mobileConnectionsRepository,
            carrierConfigTracker,
            logcatTableLogBuffer(kosmos, "MobileIconsInteractorTest"),
            connectivityRepository,
            userSetupRepo,
            testScope.backgroundScope,
            context,
            featureFlags,
        )

    private var underTest: MobileDataTileDataInteractor =
        MobileDataTileDataInteractor(context, mobileIconsInteractor, pipelineFlagsRepository)

    @Before
    fun setUp() {
        featureFlags.fake.set(Flags.FILTER_PROVISIONING_NETWORK_SUBSCRIPTIONS, true)
    }

    @Test
    fun tileData_noActiveSim_emitsInactiveModel() =
        kosmos.runTest {
            val tileData by collectLastValue(underTest.tileData())
            mobileConnectionsRepository.fake.setActiveMobileDataSubscriptionId(-1)
            runCurrent()

            val expectedModel =
                MobileDataTileModel(
                    isSimActive = false,
                    isEnabled = false,
                    icon =
                        MobileDataTileIcon.ResourceIcon(
                            Icon.Resource(
                                com.android.settingslib.R.drawable.ic_mobile_4_4_bar,
                                ContentDescription.Loaded(
                                    context.getString(R.string.quick_settings_cellular_detail_title)
                                ),
                            )
                        ),
                )
            assertThat(tileData).isEqualTo(expectedModel)
        }

    @Test
    fun tileData_activeSim_dataDisabled_emitsOffIcon() =
        kosmos.runTest {
            val tileData by collectLastValue(underTest.tileData())
            val mobileConnectionRepo =
                FakeMobileConnectionRepository(SUB_ID, logcatTableLogBuffer(kosmos))
            mobileConnectionsRepository.fake.setMobileConnectionRepositoryMap(
                mapOf(SUB_ID to mobileConnectionRepo)
            )
            mobileConnectionsRepository.fake.activeMobileDataSubscriptionId.value = SUB_ID
            mobileConnectionRepo.dataConnectionState.value = DataConnectionState.Connected

            // Set data to disabled
            mobileConnectionRepo.dataEnabled.value = false
            runCurrent()

            assertThat(tileData?.isSimActive).isTrue()
            assertThat(tileData?.isEnabled).isFalse()
            assertThat(tileData?.icon)
                .isEqualTo(
                    MobileDataTileIcon.ResourceIcon(
                        Icon.Resource(
                            R.drawable.ic_signal_mobile_data_off,
                            ContentDescription.Loaded(
                                context.getString(R.string.quick_settings_cellular_detail_title)
                            ),
                        )
                    )
                )
        }

    @Test
    fun tileData_activeSim_dataEnabled_emitsCellularSignalIcon() =
        kosmos.runTest {
            val tileData by collectLastValue(underTest.tileData())
            val mobileConnectionRepo =
                FakeMobileConnectionRepository(SUB_ID, logcatTableLogBuffer(kosmos))
            mobileConnectionsRepository.fake.setMobileConnectionRepositoryMap(
                mapOf(SUB_ID to mobileConnectionRepo)
            )
            mobileConnectionsRepository.fake.setActiveMobileDataSubscriptionId(SUB_ID)
            mobileConnectionRepo.dataConnectionState.value = DataConnectionState.Connected
            mobileConnectionRepo.numberOfLevels.value = 4
            mobileConnectionRepo.dataEnabled.value = true

            // Update the signal level in the fake repo
            mobileConnectionRepo.setAllLevels(0)
            runCurrent()

            val expectedState = SignalDrawable.getState(0, 4, true)
            assertThat(tileData?.isSimActive).isTrue()
            assertThat(tileData?.isEnabled).isTrue()
            assertThat(tileData?.icon).isEqualTo(MobileDataTileIcon.SignalIcon(expectedState))
        }

    @Test
    @RequiresFlagsEnabled(FLAG_QS_SPLIT_INTERNET_TILE)
    fun availability_flagEnabled_isTrue() =
        kosmos.runTest {
            assertThat(AconfigFlags.qsSplitInternetTile()).isTrue()

            val availability by collectLastValue(underTest.availability(USER))
            assertThat(availability).isTrue()
        }

    @Test
    @RequiresFlagsDisabled(FLAG_QS_SPLIT_INTERNET_TILE)
    fun availability_flagDisabled_isFalse() =
        kosmos.runTest {
            assertThat(AconfigFlags.qsSplitInternetTile()).isFalse()
            val availability by collectLastValue(underTest.availability(USER))
            assertThat(availability).isFalse()
        }

    private companion object {
        const val SUB_ID = 1
        private val USER = UserHandle.of(0)

        @JvmStatic
        @Parameters(name = "{0}")
        fun getParams(): List<FlagsParameterization> {
            return FlagsParameterization.allCombinationsOf().andSceneContainer()
        }
    }
}
+134 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading