diff options
| author | 2024-08-14 18:00:42 +0000 | |
|---|---|---|
| committer | 2024-08-14 18:00:42 +0000 | |
| commit | c958545e4b8f11d6914129ef52d7fb971d9588d9 (patch) | |
| tree | c98aae143a446c8a1f4ee61e3aadcd390fe58d6f | |
| parent | 98d3eca1b1aef3bedfd9f85376279b74f6f80872 (diff) | |
| parent | d283fe7bb30cdc493eb8ba8b88030b864c793907 (diff) | |
Merge "TutorialSchedulerRepositoryTest" into main
2 files changed, 103 insertions, 7 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/data/repository/TutorialSchedulerRepository.kt b/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/data/repository/TutorialSchedulerRepository.kt index b9b38954784e..36b9ac70d290 100644 --- a/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/data/repository/TutorialSchedulerRepository.kt +++ b/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/data/repository/TutorialSchedulerRepository.kt @@ -17,6 +17,7 @@ package com.android.systemui.inputdevice.tutorial.data.repository import android.content.Context +import androidx.annotation.VisibleForTesting import androidx.datastore.core.DataStore import androidx.datastore.preferences.core.Preferences import androidx.datastore.preferences.core.booleanPreferencesKey @@ -33,15 +34,19 @@ import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.map @SysUISingleton -class TutorialSchedulerRepository -@Inject -constructor( - @Application private val applicationContext: Context, - @Background private val backgroundScope: CoroutineScope +class TutorialSchedulerRepository( + private val applicationContext: Context, + backgroundScope: CoroutineScope, + dataStoreName: String ) { + @Inject + constructor( + @Application applicationContext: Context, + @Background backgroundScope: CoroutineScope + ) : this(applicationContext, backgroundScope, dataStoreName = "TutorialScheduler") 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 @@ -81,8 +86,12 @@ constructor( private fun getConnectKey(device: DeviceType) = longPreferencesKey(device.name + CONNECT_TIME_SUFFIX) + @VisibleForTesting + suspend fun clearDataStore() { + applicationContext.dataStore.edit { it.clear() } + } + companion object { - const val DATASTORE_NAME = "TutorialScheduler" const val IS_LAUNCHED_SUFFIX = "_IS_LAUNCHED" const val CONNECT_TIME_SUFFIX = "_CONNECTED_TIME" } diff --git a/packages/SystemUI/tests/src/com/android/systemui/inputdevice/data/repository/TutorialSchedulerRepositoryTest.kt b/packages/SystemUI/tests/src/com/android/systemui/inputdevice/data/repository/TutorialSchedulerRepositoryTest.kt new file mode 100644 index 000000000000..7583399c784a --- /dev/null +++ b/packages/SystemUI/tests/src/com/android/systemui/inputdevice/data/repository/TutorialSchedulerRepositoryTest.kt @@ -0,0 +1,87 @@ +/* + * 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() + } +} |