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

Commit 113e9181 authored by Ioana Alexandru's avatar Ioana Alexandru Committed by Android (Google) Code Review
Browse files

Merge "Add RemoteInputRepository & interactor." into main

parents f25501aa 8139644e
Loading
Loading
Loading
Loading
+74 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.statusbar.data.repository

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.statusbar.NotificationRemoteInputManager
import com.android.systemui.util.mockito.withArgCaptor
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.verify
import org.mockito.MockitoAnnotations

@SmallTest
@RunWith(AndroidJUnit4::class)
class RemoteInputRepositoryImplTest : SysuiTestCase() {
    @Mock private lateinit var remoteInputManager: NotificationRemoteInputManager

    private lateinit var testScope: TestScope
    private lateinit var underTest: RemoteInputRepositoryImpl

    @Before
    fun setUp() {
        MockitoAnnotations.initMocks(this)

        testScope = TestScope()
        underTest = RemoteInputRepositoryImpl(remoteInputManager)
    }

    @Test
    fun isRemoteInputActive_updatesOnChange() =
        testScope.runTest {
            val active by collectLastValue(underTest.isRemoteInputActive)
            runCurrent()
            assertThat(active).isFalse()

            val callback = withArgCaptor {
                verify(remoteInputManager).addControllerCallback(capture())
            }

            callback.onRemoteInputActive(true)
            runCurrent()
            assertThat(active).isTrue()

            callback.onRemoteInputActive(false)
            runCurrent()
            assertThat(active).isFalse()
        }
}
+64 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.statusbar.domain.interactor

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.statusbar.data.repository.fakeRemoteInputRepository
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith

@SmallTest
@RunWith(AndroidJUnit4::class)
class RemoteInputInteractorTest : SysuiTestCase() {
    private val kosmos = testKosmos()
    private val testScope = kosmos.testScope
    private val fakeRemoteInputRepository = kosmos.fakeRemoteInputRepository
    private val underTest = kosmos.remoteInputInteractor

    @Test
    fun isRemoteInputActive_true() =
        testScope.runTest {
            val active by collectLastValue(underTest.isRemoteInputActive)

            fakeRemoteInputRepository.isRemoteInputActive.value = true
            runCurrent()

            assertThat(active).isTrue()
        }

    @Test
    fun isRemoteInputActive_false() =
        testScope.runTest {
            val active by collectLastValue(underTest.isRemoteInputActive)

            fakeRemoteInputRepository.isRemoteInputActive.value = false
            runCurrent()

            assertThat(active).isFalse()
        }
}
+2 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
package com.android.systemui.statusbar.data

import com.android.systemui.statusbar.data.repository.KeyguardStatusBarRepositoryModule
import com.android.systemui.statusbar.data.repository.RemoteInputRepositoryModule
import com.android.systemui.statusbar.data.repository.StatusBarModeRepositoryModule
import com.android.systemui.statusbar.phone.data.StatusBarPhoneDataLayerModule
import dagger.Module
@@ -24,6 +25,7 @@ import dagger.Module
    includes =
        [
            KeyguardStatusBarRepositoryModule::class,
            RemoteInputRepositoryModule::class,
            StatusBarModeRepositoryModule::class,
            StatusBarPhoneDataLayerModule::class
        ]
+60 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.data.repository

import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.statusbar.NotificationRemoteInputManager
import com.android.systemui.statusbar.RemoteInputController
import dagger.Binds
import dagger.Module
import javax.inject.Inject
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow

/**
 * Repository used for tracking the state of notification remote input (e.g. when the user presses
 * "reply" on a notification and the keyboard opens).
 */
interface RemoteInputRepository {
    /** Whether remote input is currently active for any notification. */
    val isRemoteInputActive: Flow<Boolean>
}

@SysUISingleton
class RemoteInputRepositoryImpl
@Inject
constructor(
    private val notificationRemoteInputManager: NotificationRemoteInputManager,
) : RemoteInputRepository {
    override val isRemoteInputActive: Flow<Boolean> = conflatedCallbackFlow {
        trySend(false) // initial value is false
        val callback =
            object : RemoteInputController.Callback {
                override fun onRemoteInputActive(active: Boolean) {
                    trySend(active)
                }
            }
        notificationRemoteInputManager.addControllerCallback(callback)
        awaitClose { notificationRemoteInputManager.removeControllerCallback(callback) }
    }
}

@Module
interface RemoteInputRepositoryModule {
    @Binds fun bindImpl(impl: RemoteInputRepositoryImpl): RemoteInputRepository
}
+32 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.domain.interactor

import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.statusbar.data.repository.RemoteInputRepository
import javax.inject.Inject
import kotlinx.coroutines.flow.Flow

/**
 * Interactor used for business logic pertaining to the notification remote input (e.g. when the
 * user presses "reply" on a notification and the keyboard opens).
 */
@SysUISingleton
class RemoteInputInteractor @Inject constructor(remoteInputRepository: RemoteInputRepository) {
    /** Is remote input currently active for a notification? */
    val isRemoteInputActive: Flow<Boolean> = remoteInputRepository.isRemoteInputActive
}
Loading