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.shade.data.repository
18
19 import android.view.Display
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.display.data.repository.displayRepository
25 import com.android.systemui.kosmos.testScope
26 import com.android.systemui.kosmos.useUnconfinedTestDispatcher
27 import com.android.systemui.shade.ShadePrimaryDisplayCommand
28 import com.android.systemui.shade.display.FakeShadeDisplayPolicy
29 import com.android.systemui.statusbar.commandline.commandRegistry
30 import com.android.systemui.testKosmos
31 import com.android.systemui.util.settings.fakeGlobalSettings
32 import com.google.common.truth.StringSubject
33 import com.google.common.truth.Truth.assertThat
34 import java.io.PrintWriter
35 import java.io.StringWriter
36 import kotlinx.coroutines.test.runTest
37 import org.junit.Before
38 import org.junit.Test
39 import org.junit.runner.RunWith
40
41 @SmallTest
42 @RunWith(AndroidJUnit4::class)
43 class ShadePrimaryDisplayCommandTest : SysuiTestCase() {
44 private val kosmos = testKosmos().useUnconfinedTestDispatcher()
45 private val testScope = kosmos.testScope
46 private val globalSettings = kosmos.fakeGlobalSettings
47 private val commandRegistry = kosmos.commandRegistry
48 private val displayRepository = kosmos.displayRepository
49 private val defaultPolicy = kosmos.defaultShadeDisplayPolicy
50 private val shadeDisplaysRepository = kosmos.shadeDisplaysRepository
51 private val policies = kosmos.shadeDisplayPolicies
52 private val pw = PrintWriter(StringWriter())
53
54 private val underTest =
55 ShadePrimaryDisplayCommand(
56 globalSettings,
57 commandRegistry,
58 displayRepository,
59 shadeDisplaysRepository,
60 policies,
61 defaultPolicy,
62 )
63
64 @Before
setUpnull65 fun setUp() {
66 underTest.start()
67 }
68
69 @Test
commandShadeDisplayOverride_resetsDisplayIdnull70 fun commandShadeDisplayOverride_resetsDisplayId() =
71 testScope.runTest {
72 val displayId by collectLastValue(shadeDisplaysRepository.pendingDisplayId)
73 assertThat(displayId).isEqualTo(Display.DEFAULT_DISPLAY)
74
75 val newDisplayId = 2
76 displayRepository.addDisplay(displayId = newDisplayId)
77 commandRegistry.onShellCommand(
78 pw,
79 arrayOf("shade_display_override", "any_external_display"),
80 )
81 assertThat(displayId).isEqualTo(newDisplayId)
82
83 commandRegistry.onShellCommand(pw, arrayOf("shade_display_override", "reset"))
84 assertThat(displayId).isEqualTo(Display.DEFAULT_DISPLAY)
85 }
86
87 @Test
commandShadeDisplayOverride_anyExternalDisplay_notOnDefaultAnymorenull88 fun commandShadeDisplayOverride_anyExternalDisplay_notOnDefaultAnymore() =
89 testScope.runTest {
90 val displayId by collectLastValue(shadeDisplaysRepository.pendingDisplayId)
91 assertThat(displayId).isEqualTo(Display.DEFAULT_DISPLAY)
92 val newDisplayId = 2
93 displayRepository.addDisplay(displayId = newDisplayId)
94
95 commandRegistry.onShellCommand(
96 pw,
97 arrayOf("shade_display_override", "any_external_display"),
98 )
99
100 assertThat(displayId).isEqualTo(newDisplayId)
101 }
102
103 @Test
policies_listsAllPoliciesnull104 fun policies_listsAllPolicies() =
105 testScope.runTest {
106 val stringWriter = StringWriter()
107 commandRegistry.onShellCommand(
108 PrintWriter(stringWriter),
109 arrayOf("shade_display_override", "policies"),
110 )
111 val result = stringWriter.toString()
112
113 assertThat(result).containsAllIn(policies.map { it.name })
114 }
115
116 @Test
policies_setsNewPolicynull117 fun policies_setsNewPolicy() =
118 testScope.runTest {
119 val newPolicy = FakeShadeDisplayPolicy.name
120
121 commandRegistry.onShellCommand(pw, arrayOf("shade_display_override", newPolicy))
122
123 assertThat(shadeDisplaysRepository.currentPolicy.name).isEqualTo(newPolicy)
124 }
125 }
126
containsAllInnull127 private fun StringSubject.containsAllIn(strings: List<String>) {
128 strings.forEach { contains(it) }
129 }
130