1 /*
<lambda>null2  * Copyright (C) 2024 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.navigation.domain.interactor
18 
19 import android.view.WindowManagerPolicyConstants
20 import androidx.test.ext.junit.runners.AndroidJUnit4
21 import androidx.test.filters.SmallTest
22 import com.android.systemui.SysuiTestCase
23 import com.android.systemui.coroutines.collectLastValue
24 import com.android.systemui.kosmos.testScope
25 import com.android.systemui.navigationbar.NavigationModeController.ModeChangedListener
26 import com.android.systemui.navigationbar.navigationModeController
27 import com.android.systemui.testKosmos
28 import com.google.common.truth.Truth.assertThat
29 import kotlinx.coroutines.test.runTest
30 import org.junit.Before
31 import org.junit.Test
32 import org.junit.runner.RunWith
33 import org.mockito.kotlin.any
34 import org.mockito.kotlin.whenever
35 
36 @SmallTest
37 @RunWith(AndroidJUnit4::class)
38 class NavigationInteractorTest : SysuiTestCase() {
39 
40     private val kosmos = testKosmos()
41     private val testScope = kosmos.testScope
42     private val navigationModeControllerMock = kosmos.navigationModeController
43 
44     private val underTest = kosmos.navigationInteractor
45 
46     private var currentMode = WindowManagerPolicyConstants.NAV_BAR_MODE_3BUTTON
47     private val modeChangedListeners = mutableListOf<ModeChangedListener>()
48 
49     @Before
50     fun setUp() {
51         whenever(navigationModeControllerMock.addListener(any())).thenAnswer { invocation ->
52             val listener = invocation.arguments[0] as ModeChangedListener
53             modeChangedListeners.add(listener)
54             currentMode
55         }
56     }
57 
58     @Test
59     fun isGesturalMode() =
60         testScope.runTest {
61             val isGesturalMode by collectLastValue(underTest.isGesturalMode)
62             assertThat(isGesturalMode).isFalse()
63 
64             currentMode = WindowManagerPolicyConstants.NAV_BAR_MODE_GESTURAL
65             notifyModeChangedListeners()
66             assertThat(isGesturalMode).isTrue()
67 
68             currentMode = WindowManagerPolicyConstants.NAV_BAR_MODE_3BUTTON
69             notifyModeChangedListeners()
70             assertThat(isGesturalMode).isFalse()
71         }
72 
73     private fun notifyModeChangedListeners() {
74         modeChangedListeners.forEach { listener -> listener.onNavigationModeChanged(currentMode) }
75     }
76 }
77