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

Commit 2d59b1b7 authored by Helen Cheuk's avatar Helen Cheuk Committed by Android (Google) Code Review
Browse files

Merge "[Contextual Edu] Fix repository unit test" into main

parents 9388ab9f 968c450d
Loading
Loading
Loading
Loading
+50 −66
Original line number Diff line number Diff line
@@ -16,11 +16,9 @@

package com.android.systemui.education.data.repository

import android.content.Context
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.SysuiTestableContext
import com.android.systemui.contextualeducation.GestureType.BACK
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.education.data.model.EduDeviceConnectionTime
@@ -30,21 +28,16 @@ import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.testDispatcher
import com.android.systemui.kosmos.testScope
import com.google.common.truth.Truth.assertThat
import java.io.File
import javax.inject.Provider
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Ignore
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith

@SmallTest
@RunWith(AndroidJUnit4::class)
@Ignore("b/384284415")
class ContextualEducationRepositoryTest : SysuiTestCase() {

    private lateinit var underTest: UserContextualEducationRepository
@@ -57,30 +50,23 @@ class ContextualEducationRepositoryTest : SysuiTestCase() {
    private val testUserId = 1111
    private val secondTestUserId = 1112

    // For deleting any test files created after the test
    @get:Rule val tmpFolder: TemporaryFolder = TemporaryFolder.builder().assureDeletion().build()

    @Before
    fun setUp() {
        // Create TestContext here because TemporaryFolder.create() is called in @Before. It is
        // needed before calling TemporaryFolder.newFolder().
        val testContext = TestContext(context, tmpFolder.newFolder())
        underTest =
            UserContextualEducationRepository(
                testContext,
                context,
                dsScopeProvider,
                kosmos.mockEduInputManager,
                kosmos.testDispatcher
                kosmos.testDispatcher,
            )
        underTest.setUser(testUserId)
    }

    @Test
    fun changeRetrievedValueForNewUser() =
        testScope.runTest {
    fun changeRetrievedValueForNewUser() = runTestAndClear {
        // Update data for old user.
        underTest.updateGestureEduModel(BACK) { it.copy(signalCount = 1) }
            val model by collectLastValue(underTest.readGestureEduModelFlow(BACK))
        val model by testScope.collectLastValue(underTest.readGestureEduModelFlow(BACK))
        assertThat(model?.signalCount).isEqualTo(1)

        // User is changed.
@@ -90,17 +76,15 @@ class ContextualEducationRepositoryTest : SysuiTestCase() {
    }

    @Test
    fun changeUserIdForNewUser() =
        testScope.runTest {
            val model by collectLastValue(underTest.readGestureEduModelFlow(BACK))
    fun changeUserIdForNewUser() = runTestAndClear {
        val model by testScope.collectLastValue(underTest.readGestureEduModelFlow(BACK))
        assertThat(model?.userId).isEqualTo(testUserId)
        underTest.setUser(secondTestUserId)
        assertThat(model?.userId).isEqualTo(secondTestUserId)
    }

    @Test
    fun dataChangedOnUpdate() =
        testScope.runTest {
    fun dataChangedOnUpdate() = runTestAndClear {
        val newModel =
            GestureEduModel(
                signalCount = 2,
@@ -109,31 +93,31 @@ class ContextualEducationRepositoryTest : SysuiTestCase() {
                lastEducationTime = kosmos.fakeEduClock.instant(),
                usageSessionStartTime = kosmos.fakeEduClock.instant(),
                userId = testUserId,
                    gestureType = BACK
                gestureType = BACK,
            )
        underTest.updateGestureEduModel(BACK) { newModel }
            val model by collectLastValue(underTest.readGestureEduModelFlow(BACK))
        val model by testScope.collectLastValue(underTest.readGestureEduModelFlow(BACK))
        assertThat(model).isEqualTo(newModel)
    }

    @Test
    fun eduDeviceConnectionTimeDataChangedOnUpdate() =
        testScope.runTest {
    fun eduDeviceConnectionTimeDataChangedOnUpdate() = runTestAndClear {
        val newModel =
            EduDeviceConnectionTime(
                keyboardFirstConnectionTime = kosmos.fakeEduClock.instant(),
                touchpadFirstConnectionTime = kosmos.fakeEduClock.instant(),
            )
        underTest.updateEduDeviceConnectionTime { newModel }
            val model by collectLastValue(underTest.readEduDeviceConnectionTime())
        val model by testScope.collectLastValue(underTest.readEduDeviceConnectionTime())
        assertThat(model).isEqualTo(newModel)
    }

    /** Test context which allows overriding getFilesDir path */
    private class TestContext(context: Context, private val folder: File) :
        SysuiTestableContext(context) {
        override fun getFilesDir(): File {
            return folder
    private fun runTestAndClear(block: suspend () -> Unit) =
        testScope.runTest {
            try {
                block()
            } finally {
                underTest.clear()
            }
        }
}
+6 −0
Original line number Diff line number Diff line
@@ -77,6 +77,8 @@ interface ContextualEducationRepository {
        transform: (EduDeviceConnectionTime) -> EduDeviceConnectionTime
    )

    suspend fun clear()

    val keyboardShortcutTriggered: Flow<GestureType>
}

@@ -278,4 +280,8 @@ constructor(
            preferences.remove(key)
        }
    }

    override suspend fun clear() {
        datastore.filterNotNull().first().edit { it.clear() }
    }
}
+6 −1
Original line number Diff line number Diff line
@@ -96,7 +96,7 @@ class FakeContextualEducationRepository : ContextualEducationRepository {

    override suspend fun updateGestureEduModel(
        gestureType: GestureType,
        transform: (GestureEduModel) -> GestureEduModel
        transform: (GestureEduModel) -> GestureEduModel,
    ) {
        val gestureModels =
            when (gestureType) {
@@ -117,6 +117,11 @@ class FakeContextualEducationRepository : ContextualEducationRepository {
        _eduDeviceConnectionTime.value = transform(currentModel)
    }

    override suspend fun clear() {
        val currentUserMap = userGestureMap[currentUser]!!
        currentUserMap.clear()
    }

    override val keyboardShortcutTriggered: Flow<GestureType>
        get() = _keyboardShortcutTriggered.filterNotNull()