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

Commit 63728b4f authored by Liana Kazanova (xWF)'s avatar Liana Kazanova (xWF) Committed by Android (Google) Code Review
Browse files

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

parents 0da3169c c8725b3a
Loading
Loading
Loading
Loading
+66 −50
Original line number Diff line number Diff line
@@ -16,9 +16,11 @@

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
@@ -28,16 +30,21 @@ 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
@@ -50,23 +57,30 @@ 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(
                context,
                testContext,
                dsScopeProvider,
                kosmos.mockEduInputManager,
                kosmos.testDispatcher,
                kosmos.testDispatcher
            )
        underTest.setUser(testUserId)
    }

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

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

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

    @Test
    fun dataChangedOnUpdate() = runTestAndClear {
    fun dataChangedOnUpdate() =
        testScope.runTest {
            val newModel =
                GestureEduModel(
                    signalCount = 2,
@@ -93,31 +109,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 testScope.collectLastValue(underTest.readGestureEduModelFlow(BACK))
            val model by collectLastValue(underTest.readGestureEduModelFlow(BACK))
            assertThat(model).isEqualTo(newModel)
        }

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

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

    suspend fun clear()

    val keyboardShortcutTriggered: Flow<GestureType>
}

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

    override suspend fun clear() {
        datastore.filterNotNull().first().edit { it.clear() }
    }
}
+1 −6
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,11 +117,6 @@ 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()