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.systemui.keyevent.domain.interactor
18 
19 import androidx.test.ext.junit.runners.AndroidJUnit4
20 import androidx.test.filters.SmallTest
21 import com.android.systemui.SysuiTestCase
22 import com.android.systemui.coroutines.collectLastValue
23 import com.android.systemui.keyevent.data.repository.FakeKeyEventRepository
24 import com.google.common.truth.Truth.assertThat
25 import kotlinx.coroutines.test.runTest
26 import org.junit.Before
27 import org.junit.Rule
28 import org.junit.Test
29 import org.junit.runner.RunWith
30 import org.mockito.junit.MockitoJUnit
31 
32 @SmallTest
33 @RunWith(AndroidJUnit4::class)
34 class KeyEventInteractorTest : SysuiTestCase() {
35     @JvmField @Rule var mockitoRule = MockitoJUnit.rule()
36 
37     private lateinit var repository: FakeKeyEventRepository
38 
39     private lateinit var underTest: KeyEventInteractor
40 
41     @Before
setupnull42     fun setup() {
43         repository = FakeKeyEventRepository()
44         underTest =
45             KeyEventInteractor(
46                 repository,
47             )
48     }
49 
50     @Test
dispatchBackKey_notHandledByKeyguardKeyEventInteractor_handledByBackActionInteractornull51     fun dispatchBackKey_notHandledByKeyguardKeyEventInteractor_handledByBackActionInteractor() =
52         runTest {
53             val isPowerDown by collectLastValue(underTest.isPowerButtonDown)
54             repository.setPowerButtonDown(false)
55             assertThat(isPowerDown).isFalse()
56 
57             repository.setPowerButtonDown(true)
58             assertThat(isPowerDown).isTrue()
59         }
60 }
61