1 /*
2  * 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.keyboard.docking.ui.viewmodel
18 
19 import android.graphics.Rect
20 import android.view.WindowInsets
21 import android.view.WindowManager
22 import android.view.WindowMetrics
23 import androidx.test.ext.junit.runners.AndroidJUnit4
24 import androidx.test.filters.SmallTest
25 import com.android.systemui.SysuiTestCase
26 import com.android.systemui.common.ui.data.repository.FakeConfigurationRepository
27 import com.android.systemui.common.ui.domain.interactor.ConfigurationInteractorImpl
28 import com.android.systemui.keyboard.data.repository.FakeKeyboardRepository
29 import com.android.systemui.keyboard.docking.domain.interactor.KeyboardDockingIndicationInteractor
30 import com.google.common.truth.Truth.assertThat
31 import kotlinx.coroutines.test.StandardTestDispatcher
32 import kotlinx.coroutines.test.TestScope
33 import kotlinx.coroutines.test.runTest
34 import org.junit.Before
35 import org.junit.Test
36 import org.junit.runner.RunWith
37 import org.mockito.Mockito.spy
38 import org.mockito.MockitoAnnotations
39 import org.mockito.kotlin.doReturn
40 import org.mockito.kotlin.whenever
41 
42 @SmallTest
43 @RunWith(AndroidJUnit4::class)
44 class KeyboardDockingIndicationViewModelTest : SysuiTestCase() {
45     private val testScope = TestScope(StandardTestDispatcher())
46 
47     private lateinit var keyboardRepository: FakeKeyboardRepository
48     private lateinit var configurationRepository: FakeConfigurationRepository
49     private lateinit var underTest: KeyboardDockingIndicationViewModel
50     private lateinit var windowManager: WindowManager
51 
52     @Before
setupnull53     fun setup() {
54         MockitoAnnotations.initMocks(this)
55 
56         keyboardRepository = FakeKeyboardRepository()
57         configurationRepository = FakeConfigurationRepository()
58         windowManager = spy(context.getSystemService(WindowManager::class.java)!!)
59 
60         val keyboardDockingIndicationInteractor =
61             KeyboardDockingIndicationInteractor(keyboardRepository)
62         val configurationInteractor = ConfigurationInteractorImpl(configurationRepository)
63 
64         underTest =
65             KeyboardDockingIndicationViewModel(
66                 windowManager,
67                 context,
68                 keyboardDockingIndicationInteractor,
69                 configurationInteractor,
70                 testScope.backgroundScope,
71             )
72     }
73 
74     @Test
onConfigurationChanged_createsNewConfignull75     fun onConfigurationChanged_createsNewConfig() {
76         val oldBounds = Rect(0, 0, 10, 10)
77         val newBounds = Rect(10, 10, 20, 20)
78         val inset = WindowInsets(Rect(1, 1, 1, 1))
79         val density = 1f
80 
81         doReturn(WindowMetrics(oldBounds, inset, density))
82             .whenever(windowManager)
83             .currentWindowMetrics
84 
85         val firstGlow = underTest.edgeGlow.value
86 
87         testScope.runTest {
88             configurationRepository.onAnyConfigurationChange()
89             // Ensure there's some change in the config so that flow emits the new value.
90             doReturn(WindowMetrics(newBounds, inset, density))
91                 .whenever(windowManager)
92                 .currentWindowMetrics
93 
94             val secondGlow = underTest.edgeGlow.value
95 
96             assertThat(firstGlow.hashCode()).isNotEqualTo(secondGlow.hashCode())
97         }
98     }
99 }
100