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.controls.ui
18 
19 import android.content.Intent
20 import android.content.res.Configuration
21 import android.service.dreams.IDreamManager
22 import android.testing.TestableLooper
23 import androidx.test.ext.junit.runners.AndroidJUnit4
24 import androidx.test.filters.SmallTest
25 import androidx.test.rule.ActivityTestRule
26 import com.android.systemui.SysuiTestCase
27 import com.android.systemui.activity.SingleActivityFactory
28 import com.android.systemui.broadcast.BroadcastDispatcher
29 import com.android.systemui.controls.settings.ControlsSettingsDialogManager
30 import com.android.systemui.flags.FeatureFlags
31 import com.android.systemui.statusbar.policy.KeyguardStateController
32 import org.junit.Before
33 import org.junit.Rule
34 import org.junit.Test
35 import org.junit.runner.RunWith
36 import org.mockito.Mock
37 import org.mockito.Mockito.times
38 import org.mockito.Mockito.verify
39 import org.mockito.MockitoAnnotations
40 
41 @SmallTest
42 @RunWith(AndroidJUnit4::class)
43 @TestableLooper.RunWithLooper
44 class ControlsActivityTest : SysuiTestCase() {
45     @Mock private lateinit var uiController: ControlsUiController
46     @Mock private lateinit var broadcastDispatcher: BroadcastDispatcher
47     @Mock private lateinit var dreamManager: IDreamManager
48     @Mock private lateinit var featureFlags: FeatureFlags
49     @Mock private lateinit var controlsSettingsDialogManager: ControlsSettingsDialogManager
50     @Mock private lateinit var keyguardStateController: KeyguardStateController
51 
52     @Rule
53     @JvmField
54     var activityRule =
55         ActivityTestRule(
<lambda>null56             /* activityFactory= */ SingleActivityFactory {
57                 TestableControlsActivity(
58                     uiController,
59                     broadcastDispatcher,
60                     dreamManager,
61                     featureFlags,
62                     controlsSettingsDialogManager,
63                     keyguardStateController,
64                 )
65             },
66             /* initialTouchMode= */ false,
67             /* launchActivity= */ false,
68         )
69 
70     @Before
setupnull71     fun setup() {
72         MockitoAnnotations.initMocks(this)
73         activityRule.launchActivity(Intent())
74     }
75 
76     @Test
testOrientationChangeForwardsToUiControllernull77     fun testOrientationChangeForwardsToUiController() {
78         val currentConfig = activityRule.activity.resources.configuration
79         val newConfig = Configuration(currentConfig)
80         newConfig.orientation = switchOrientation(currentConfig.orientation)
81         activityRule.runOnUiThread { activityRule.activity.onConfigurationChanged(newConfig) }
82 
83         verify(uiController).onSizeChange()
84     }
85 
86     @Test
testScreenChangeForwardsToUiControllernull87     fun testScreenChangeForwardsToUiController() {
88         val currentConfig = activityRule.activity.resources.configuration
89         val newConfig = Configuration(currentConfig)
90         swapHeightWidth(newConfig)
91         activityRule.runOnUiThread { activityRule.activity.onConfigurationChanged(newConfig) }
92 
93         verify(uiController).onSizeChange()
94     }
95 
96     @Test
testChangeSmallestScreenSizeForwardsToUiControllernull97     fun testChangeSmallestScreenSizeForwardsToUiController() {
98         val currentConfig = activityRule.activity.resources.configuration
99         val newConfig = Configuration(currentConfig)
100         newConfig.smallestScreenWidthDp *= 2
101         newConfig.screenWidthDp *= 2
102         activityRule.runOnUiThread { activityRule.activity.onConfigurationChanged(newConfig) }
103 
104         verify(uiController).onSizeChange()
105     }
106 
107     @Test
testConfigurationChangeSupportsInPlaceChangenull108     fun testConfigurationChangeSupportsInPlaceChange() {
109         val config = Configuration(activityRule.activity.resources.configuration)
110 
111         config.orientation = switchOrientation(config.orientation)
112         activityRule.runOnUiThread { activityRule.activity.onConfigurationChanged(config) }
113         config.orientation = switchOrientation(config.orientation)
114         activityRule.runOnUiThread { activityRule.activity.onConfigurationChanged(config) }
115 
116         verify(uiController, times(2)).onSizeChange()
117     }
118 
switchOrientationnull119     private fun switchOrientation(orientation: Int): Int {
120         return if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
121             Configuration.ORIENTATION_PORTRAIT
122         } else {
123             Configuration.ORIENTATION_LANDSCAPE
124         }
125     }
126 
swapHeightWidthnull127     private fun swapHeightWidth(configuration: Configuration) {
128         val oldHeight = configuration.screenHeightDp
129         val oldWidth = configuration.screenWidthDp
130         configuration.screenHeightDp = oldWidth
131         configuration.screenWidthDp = oldHeight
132     }
133 }
134