1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.fingerprint2.ui.enrollment.modules.enrolling.rfps.viewmodel 18 19 import androidx.arch.core.executor.testing.InstantTaskExecutorRule 20 import com.android.settings.biometrics.fingerprint2.ui.enrollment.modules.enrolling.rfps.ui.viewmodel.RFPSIconTouchViewModel 21 import com.google.common.truth.Truth.assertThat 22 import kotlinx.coroutines.Dispatchers 23 import kotlinx.coroutines.launch 24 import kotlinx.coroutines.test.StandardTestDispatcher 25 import kotlinx.coroutines.test.TestScope 26 import kotlinx.coroutines.test.resetMain 27 import kotlinx.coroutines.test.runCurrent 28 import kotlinx.coroutines.test.runTest 29 import kotlinx.coroutines.test.setMain 30 import org.junit.After 31 import org.junit.Before 32 import org.junit.Rule 33 import org.junit.Test 34 import org.junit.runner.RunWith 35 import org.mockito.junit.MockitoJUnit 36 import org.mockito.junit.MockitoJUnitRunner 37 38 @RunWith(MockitoJUnitRunner::class) 39 class RFPSIconTouchViewModelTest { 40 @JvmField @Rule var rule = MockitoJUnit.rule() 41 42 @get:Rule val instantTaskRule = InstantTaskExecutorRule() 43 44 private var backgroundDispatcher = StandardTestDispatcher() 45 private var testScope = TestScope(backgroundDispatcher) 46 private lateinit var rfpsIconTouchViewModel: RFPSIconTouchViewModel 47 48 @Before setupnull49 fun setup() { 50 Dispatchers.setMain(backgroundDispatcher) 51 testScope = TestScope(backgroundDispatcher) 52 rfpsIconTouchViewModel = RFPSIconTouchViewModel() 53 } 54 55 @After tearDownnull56 fun tearDown() { 57 Dispatchers.resetMain() 58 } 59 60 @Test initShouldNotShowDialognull61 fun initShouldNotShowDialog() = 62 testScope.runTest { 63 var shouldShowDialog = false 64 65 val job = launch { rfpsIconTouchViewModel.shouldShowDialog.collect { shouldShowDialog = it } } 66 67 runCurrent() 68 69 assertThat(shouldShowDialog).isFalse() 70 job.cancel() 71 } 72 73 @Test shouldShowDialogTestnull74 fun shouldShowDialogTest() = 75 testScope.runTest { 76 var shouldShowDialog = false 77 78 val job = launch { rfpsIconTouchViewModel.shouldShowDialog.collect { shouldShowDialog = it } } 79 80 rfpsIconTouchViewModel.userTouchedFingerprintIcon() 81 rfpsIconTouchViewModel.userTouchedFingerprintIcon() 82 rfpsIconTouchViewModel.userTouchedFingerprintIcon() 83 84 runCurrent() 85 86 assertThat(shouldShowDialog).isTrue() 87 job.cancel() 88 } 89 90 @Test stateShouldBeFalseAfterResetnull91 fun stateShouldBeFalseAfterReset() = 92 testScope.runTest { 93 var shouldShowDialog = false 94 95 val job = launch { rfpsIconTouchViewModel.shouldShowDialog.collect { shouldShowDialog = it } } 96 97 rfpsIconTouchViewModel.userTouchedFingerprintIcon() 98 rfpsIconTouchViewModel.userTouchedFingerprintIcon() 99 rfpsIconTouchViewModel.userTouchedFingerprintIcon() 100 101 runCurrent() 102 103 assertThat(shouldShowDialog).isTrue() 104 105 rfpsIconTouchViewModel.userTouchedFingerprintIcon() 106 runCurrent() 107 108 assertThat(shouldShowDialog).isFalse() 109 110 job.cancel() 111 } 112 113 @Test toggleMultipleTimesnull114 fun toggleMultipleTimes() = 115 testScope.runTest { 116 var shouldShowDialog = false 117 118 val job = launch { rfpsIconTouchViewModel.shouldShowDialog.collect { shouldShowDialog = it } } 119 120 rfpsIconTouchViewModel.userTouchedFingerprintIcon() 121 rfpsIconTouchViewModel.userTouchedFingerprintIcon() 122 rfpsIconTouchViewModel.userTouchedFingerprintIcon() 123 124 runCurrent() 125 126 assertThat(shouldShowDialog).isTrue() 127 128 rfpsIconTouchViewModel.userTouchedFingerprintIcon() 129 runCurrent() 130 131 assertThat(shouldShowDialog).isFalse() 132 133 rfpsIconTouchViewModel.userTouchedFingerprintIcon() 134 rfpsIconTouchViewModel.userTouchedFingerprintIcon() 135 136 runCurrent() 137 assertThat(shouldShowDialog).isTrue() 138 139 job.cancel() 140 } 141 } 142