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

Commit d283fe7b authored by yyalan's avatar yyalan
Browse files

TutorialSchedulerRepositoryTest

Bug: 344862874
Flag: com.android.systemui.shared.new_touchpad_gestures_tutorial
Test: TutorialSchedulerRepositoryTest.kt
Change-Id: I7dc3f08f4183082e89d9214b60bfe0f7304b3702
parent d5846c23
Loading
Loading
Loading
Loading
+16 −7
Original line number Original line Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.systemui.inputdevice.tutorial.data.repository
package com.android.systemui.inputdevice.tutorial.data.repository


import android.content.Context
import android.content.Context
import androidx.annotation.VisibleForTesting
import androidx.datastore.core.DataStore
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.booleanPreferencesKey
@@ -33,15 +34,19 @@ import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.map


@SysUISingleton
@SysUISingleton
class TutorialSchedulerRepository
class TutorialSchedulerRepository(
    private val applicationContext: Context,
    backgroundScope: CoroutineScope,
    dataStoreName: String
) {
    @Inject
    @Inject
    constructor(
    constructor(
    @Application private val applicationContext: Context,
        @Application applicationContext: Context,
    @Background private val backgroundScope: CoroutineScope
        @Background backgroundScope: CoroutineScope
) {
    ) : this(applicationContext, backgroundScope, dataStoreName = "TutorialScheduler")


    private val Context.dataStore: DataStore<Preferences> by
    private val Context.dataStore: DataStore<Preferences> by
        preferencesDataStore(name = DATASTORE_NAME, scope = backgroundScope)
        preferencesDataStore(name = dataStoreName, scope = backgroundScope)


    suspend fun isLaunched(deviceType: DeviceType): Boolean = loadData()[deviceType]!!.isLaunched
    suspend fun isLaunched(deviceType: DeviceType): Boolean = loadData()[deviceType]!!.isLaunched


@@ -81,8 +86,12 @@ constructor(
    private fun getConnectKey(device: DeviceType) =
    private fun getConnectKey(device: DeviceType) =
        longPreferencesKey(device.name + CONNECT_TIME_SUFFIX)
        longPreferencesKey(device.name + CONNECT_TIME_SUFFIX)


    @VisibleForTesting
    suspend fun clearDataStore() {
        applicationContext.dataStore.edit { it.clear() }
    }

    companion object {
    companion object {
        const val DATASTORE_NAME = "TutorialScheduler"
        const val IS_LAUNCHED_SUFFIX = "_IS_LAUNCHED"
        const val IS_LAUNCHED_SUFFIX = "_IS_LAUNCHED"
        const val CONNECT_TIME_SUFFIX = "_CONNECTED_TIME"
        const val CONNECT_TIME_SUFFIX = "_CONNECTED_TIME"
    }
    }
+87 −0
Original line number Original line 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.inputdevice.data.repository

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.inputdevice.tutorial.data.repository.DeviceType.KEYBOARD
import com.android.systemui.inputdevice.tutorial.data.repository.DeviceType.TOUCHPAD
import com.android.systemui.inputdevice.tutorial.data.repository.TutorialSchedulerRepository
import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.testScope
import com.google.common.truth.Truth.assertThat
import java.time.Instant
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

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

    private lateinit var underTest: TutorialSchedulerRepository
    private val kosmos = Kosmos()
    private val testScope = kosmos.testScope

    @Before
    fun setup() {
        underTest =
            TutorialSchedulerRepository(
                context,
                testScope.backgroundScope,
                "TutorialSchedulerRepositoryTest"
            )
    }

    @After
    fun clear() {
        testScope.launch { underTest.clearDataStore() }
    }

    @Test
    fun initialState() =
        testScope.runTest {
            assertThat(underTest.wasEverConnected(KEYBOARD)).isFalse()
            assertThat(underTest.wasEverConnected(TOUCHPAD)).isFalse()
            assertThat(underTest.isLaunched(KEYBOARD)).isFalse()
            assertThat(underTest.isLaunched(TOUCHPAD)).isFalse()
        }

    @Test
    fun connectKeyboard() =
        testScope.runTest {
            val now = Instant.now().toEpochMilli()
            underTest.updateConnectTime(KEYBOARD, now)

            assertThat(underTest.wasEverConnected(KEYBOARD)).isTrue()
            assertThat(underTest.connectTime(KEYBOARD)).isEqualTo(now)
            assertThat(underTest.wasEverConnected(TOUCHPAD)).isFalse()
        }

    @Test
    fun launchKeyboard() =
        testScope.runTest {
            underTest.updateLaunch(KEYBOARD)

            assertThat(underTest.isLaunched(KEYBOARD)).isTrue()
            assertThat(underTest.isLaunched(TOUCHPAD)).isFalse()
        }
}