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

Commit 279b7d65 authored by Ale Nijamkin's avatar Ale Nijamkin Committed by Android (Google) Code Review
Browse files

Merge changes Ic0324069,I0fde1a5a into main

* changes:
  [flexiglass] Navigation stack.
  [flexiglass] DeviceConfig stack.
parents 63a0f1d3 f6980e1d
Loading
Loading
Loading
Loading
+59 −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.
 */

@file:OptIn(ExperimentalCoroutinesApi::class)

package com.android.systemui.deviceconfig.data.repository

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.concurrency.fakeExecutor
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.kosmos.testScope
import com.android.systemui.testKosmos
import com.android.systemui.util.fakeDeviceConfigProxy
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith

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

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

    private val underTest = kosmos.deviceConfigRepository

    @Test
    fun booleanProperty() =
        testScope.runTest {
            val property by collectLastValue(underTest.property("namespace", "name", false))
            assertThat(property).isFalse()

            dataSource.setProperty("namespace", "name", "true", /* makeDefault= */ false)
            kosmos.fakeExecutor.runAllReady()
            assertThat(property).isTrue()

            dataSource.setProperty("namespace", "name", "false", /* makeDefault= */ false)
            kosmos.fakeExecutor.runAllReady()
            assertThat(property).isFalse()
        }
}
+56 −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.systemui.deviceconfig.domain.interactor

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.concurrency.fakeExecutor
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.kosmos.testScope
import com.android.systemui.testKosmos
import com.android.systemui.util.fakeDeviceConfigProxy
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith

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

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

    private val underTest = kosmos.deviceConfigInteractor

    @Test
    fun booleanProperty() =
        testScope.runTest {
            val property by collectLastValue(underTest.property("namespace", "name", false))
            assertThat(property).isFalse()

            dataSource.setProperty("namespace", "name", "true", /* makeDefault= */ false)
            kosmos.fakeExecutor.runAllReady()
            assertThat(property).isTrue()

            dataSource.setProperty("namespace", "name", "false", /* makeDefault= */ false)
            kosmos.fakeExecutor.runAllReady()
            assertThat(property).isFalse()
        }
}
+76 −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.systemui.navigation.data.repository

import android.view.WindowManagerPolicyConstants
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.kosmos.testScope
import com.android.systemui.navigationbar.NavigationModeController.ModeChangedListener
import com.android.systemui.navigationbar.navigationModeController
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.any
import org.mockito.kotlin.whenever

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

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

    private val underTest = kosmos.navigationRepository

    private var currentMode = WindowManagerPolicyConstants.NAV_BAR_MODE_3BUTTON
    private val modeChangedListeners = mutableListOf<ModeChangedListener>()

    @Before
    fun setUp() {
        whenever(navigationModeControllerMock.addListener(any())).thenAnswer { invocation ->
            val listener = invocation.arguments[0] as ModeChangedListener
            modeChangedListeners.add(listener)
            currentMode
        }
    }

    @Test
    fun isGesturalMode() =
        testScope.runTest {
            val isGesturalMode by collectLastValue(underTest.isGesturalMode)
            assertThat(isGesturalMode).isFalse()

            currentMode = WindowManagerPolicyConstants.NAV_BAR_MODE_GESTURAL
            notifyModeChangedListeners()
            assertThat(isGesturalMode).isTrue()

            currentMode = WindowManagerPolicyConstants.NAV_BAR_MODE_3BUTTON
            notifyModeChangedListeners()
            assertThat(isGesturalMode).isFalse()
        }

    private fun notifyModeChangedListeners() {
        modeChangedListeners.forEach { listener -> listener.onNavigationModeChanged(currentMode) }
    }
}
+76 −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.systemui.navigation.domain.interactor

import android.view.WindowManagerPolicyConstants
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.kosmos.testScope
import com.android.systemui.navigationbar.NavigationModeController.ModeChangedListener
import com.android.systemui.navigationbar.navigationModeController
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.any
import org.mockito.kotlin.whenever

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

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

    private val underTest = kosmos.navigationInteractor

    private var currentMode = WindowManagerPolicyConstants.NAV_BAR_MODE_3BUTTON
    private val modeChangedListeners = mutableListOf<ModeChangedListener>()

    @Before
    fun setUp() {
        whenever(navigationModeControllerMock.addListener(any())).thenAnswer { invocation ->
            val listener = invocation.arguments[0] as ModeChangedListener
            modeChangedListeners.add(listener)
            currentMode
        }
    }

    @Test
    fun isGesturalMode() =
        testScope.runTest {
            val isGesturalMode by collectLastValue(underTest.isGesturalMode)
            assertThat(isGesturalMode).isFalse()

            currentMode = WindowManagerPolicyConstants.NAV_BAR_MODE_GESTURAL
            notifyModeChangedListeners()
            assertThat(isGesturalMode).isTrue()

            currentMode = WindowManagerPolicyConstants.NAV_BAR_MODE_3BUTTON
            notifyModeChangedListeners()
            assertThat(isGesturalMode).isFalse()
        }

    private fun notifyModeChangedListeners() {
        modeChangedListeners.forEach { listener -> listener.onNavigationModeChanged(currentMode) }
    }
}
+59 −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.systemui.deviceconfig.data.repository

import android.provider.DeviceConfig
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.util.DeviceConfigProxy
import com.android.systemui.utils.coroutines.flow.conflatedCallbackFlow
import java.util.concurrent.Executor
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOn

@SysUISingleton
class DeviceConfigRepository
@Inject
constructor(
    @Background private val backgroundExecutor: Executor,
    @Background private val backgroundDispatcher: CoroutineDispatcher,
    private val dataSource: DeviceConfigProxy,
) {

    fun property(namespace: String, name: String, default: Boolean): Flow<Boolean> {
        return conflatedCallbackFlow {
                val listener = { properties: DeviceConfig.Properties ->
                    if (properties.keyset.contains(name)) {
                        trySend(properties.getBoolean(name, default))
                    }
                }

                dataSource.addOnPropertiesChangedListener(
                    namespace,
                    backgroundExecutor,
                    listener,
                )
                trySend(dataSource.getBoolean(namespace, name, default))

                awaitClose { dataSource.removeOnPropertiesChangedListener(listener) }
            }
            .flowOn(backgroundDispatcher)
    }
}
Loading