-
Notifications
You must be signed in to change notification settings - Fork 585
/
ActivitySystemUiControllerTest.kt
351 lines (302 loc) · 13.9 KB
/
ActivitySystemUiControllerTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.accompanist.systemuicontroller
import android.os.Build
import android.view.View
import android.view.Window
import androidx.activity.ComponentActivity
import androidx.compose.ui.graphics.Color
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.FlakyTest
import androidx.test.filters.SdkSuppress
import com.google.accompanist.internal.test.IgnoreOnRobolectric
import com.google.accompanist.internal.test.waitUntil
import com.google.accompanist.internal.test.withActivity
import com.google.common.truth.Truth.assertThat
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.experimental.categories.Category
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class ActivitySystemUiControllerTest {
@get:Rule
val rule = ActivityScenarioRule(ComponentActivity::class.java)
private lateinit var window: Window
private lateinit var contentView: View
@Before
fun setup() {
window = rule.scenario.withActivity { it.window }
contentView = rule.scenario.withActivity { it.findViewById(android.R.id.content)!! }
if (Build.VERSION.SDK_INT >= 29) {
// On API 29+, the system can modify the bar colors to maintain contrast.
// We disable that here to make it simple to assert expected values
rule.scenario.onActivity {
window.apply {
isNavigationBarContrastEnforced = false
isStatusBarContrastEnforced = false
}
}
}
}
@Test
fun statusBarColor() {
rule.scenario.onActivity {
// Now create an AndroidSystemUiController() and set the status bar color
val controller = AndroidSystemUiController(contentView, window)
controller.setStatusBarColor(Color.Blue, darkIcons = false)
}
// Assert that the color was set
assertThat(Color(window.statusBarColor)).isEqualTo(Color.Blue)
}
@Test
fun navigationBarColor() {
rule.scenario.onActivity {
// Now create an AndroidSystemUiController() and set the status bar color
val controller = AndroidSystemUiController(contentView, window)
controller.setNavigationBarColor(Color.Green, darkIcons = false)
}
assertThat(Color(window.navigationBarColor)).isEqualTo(Color.Green)
}
@Test
fun systemBarColor() {
// Now create an AndroidSystemUiController() and set the system bar colors
rule.scenario.onActivity {
val controller = AndroidSystemUiController(contentView, window)
controller.setSystemBarsColor(Color.Red, darkIcons = false)
}
// Assert that the colors were set
assertThat(Color(window.statusBarColor)).isEqualTo(Color.Red)
assertThat(Color(window.navigationBarColor)).isEqualTo(Color.Red)
}
@Test
@Category(IgnoreOnRobolectric::class) // Robolectric implements the new behavior from 23+
@SdkSuppress(maxSdkVersion = 22)
fun statusBarIcons_scrim() {
// Now create an AndroidSystemUiController() and set the navigation bar with dark icons
rule.scenario.onActivity {
val controller = AndroidSystemUiController(contentView, window)
controller.setStatusBarColor(Color.White, darkIcons = true) {
// Here we can provide custom logic to 'darken' the color to maintain contrast.
// We return red just to assert below.
Color.Red
}
}
// Assert that the colors were set to our 'darkened' color
assertThat(Color(window.statusBarColor)).isEqualTo(Color.Red)
// Assert that the system couldn't apply the native light icons
rule.scenario.onActivity {
val windowInsetsController = WindowCompat.getInsetsController(window, contentView)
assertThat(windowInsetsController.isAppearanceLightStatusBars).isFalse()
}
}
@Test
@SdkSuppress(minSdkVersion = 23)
fun statusBarIcons_native() {
// Now create an AndroidSystemUiController() and set the status bar with dark icons
rule.scenario.onActivity {
val controller = AndroidSystemUiController(contentView, window)
controller.setStatusBarColor(Color.White, darkIcons = true) {
// Here we can provide custom logic to 'darken' the color to maintain contrast.
// We return red just to assert below.
Color.Red
}
}
// Assert that the colors were darkened color is not used
assertThat(Color(window.statusBarColor)).isEqualTo(Color.White)
// Assert that the system applied the native light icons
rule.scenario.onActivity {
val windowInsetsController = WindowCompat.getInsetsController(window, contentView)
assertThat(windowInsetsController.isAppearanceLightStatusBars).isTrue()
}
}
@Test
@Category(IgnoreOnRobolectric::class) // Robolectric implements the new behavior from 25+
@SdkSuppress(maxSdkVersion = 25)
fun navigationBarIcons_scrim() {
// Now create an AndroidSystemUiController() and set the navigation bar with dark icons
rule.scenario.onActivity {
val controller = AndroidSystemUiController(contentView, window)
controller.setNavigationBarColor(Color.White, darkIcons = true) {
// Here we can provide custom logic to 'darken' the color to maintain contrast.
// We return red just to assert below.
Color.Red
}
}
// Assert that the colors were set to our 'darkened' color
assertThat(Color(window.navigationBarColor)).isEqualTo(Color.Red)
// Assert that the system couldn't apply the native light icons
rule.scenario.onActivity {
val windowInsetsController = WindowCompat.getInsetsController(window, contentView)
assertThat(windowInsetsController.isAppearanceLightNavigationBars).isFalse()
}
}
@Test
@SdkSuppress(minSdkVersion = 26)
fun navigationBar_native() {
// Now create an AndroidSystemUiController() and set the navigation bar with dark icons
rule.scenario.onActivity {
val controller = AndroidSystemUiController(contentView, window)
controller.setNavigationBarColor(Color.White, darkIcons = true) {
// Here we can provide custom logic to 'darken' the color to maintain contrast.
// We return red just to assert below.
Color.Red
}
}
// Assert that the colors were darkened color is not used
assertThat(Color(window.navigationBarColor)).isEqualTo(Color.White)
// Assert that the system applied the native light icons
rule.scenario.onActivity {
val windowInsetsController = WindowCompat.getInsetsController(window, contentView)
assertThat(windowInsetsController.isAppearanceLightNavigationBars).isTrue()
}
}
@Test
@SdkSuppress(minSdkVersion = 29)
fun navigationBar_contrastEnforced() {
rule.scenario.onActivity {
// Now create an AndroidSystemUiController()
val controller = AndroidSystemUiController(contentView, window)
// Assert that the contrast is not enforced initially
assertThat(controller.isNavigationBarContrastEnforced).isFalse()
// and set the navigation bar with dark icons and enforce contrast
controller.setNavigationBarColor(
Color.Transparent,
darkIcons = true,
navigationBarContrastEnforced = true
) {
// Here we can provide custom logic to 'darken' the color to maintain contrast.
// We return red just to assert below.
Color.Red
}
// Assert that the colors were darkened color is not used
assertThat(Color(window.navigationBarColor)).isEqualTo(Color.Transparent)
// Assert that the system applied the contrast enforced property
assertThat(window.isNavigationBarContrastEnforced).isTrue()
// Assert that the controller reflects that the contrast is enforced
assertThat(controller.isNavigationBarContrastEnforced).isTrue()
}
}
@Test
@SdkSuppress(minSdkVersion = 30) // TODO: https://issuetracker.google.com/issues/189366125
fun systemBarsBehavior_showBarsByTouch() {
val controller = rule.scenario.withActivity {
AndroidSystemUiController(contentView, window)
}
rule.scenario.onActivity {
controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_TOUCH
}
assertThat(WindowCompat.getInsetsController(window, contentView).systemBarsBehavior)
.isEqualTo(WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_TOUCH)
}
@Test
@SdkSuppress(minSdkVersion = 30) // TODO: https://issuetracker.google.com/issues/189366125
fun systemBarsBehavior_showBarsBySwipe() {
val controller = rule.scenario.withActivity {
AndroidSystemUiController(contentView, window)
}
rule.scenario.onActivity {
controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_SWIPE
}
assertThat(WindowCompat.getInsetsController(window, contentView).systemBarsBehavior)
.isEqualTo(WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_SWIPE)
}
@Test
@SdkSuppress(minSdkVersion = 30) // TODO: https://issuetracker.google.com/issues/189366125
fun systemBarsBehavior_showTransientBarsBySwipe() {
val controller = rule.scenario.withActivity {
AndroidSystemUiController(contentView, window)
}
rule.scenario.onActivity {
controller.systemBarsBehavior =
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
assertThat(WindowCompat.getInsetsController(window, contentView).systemBarsBehavior)
.isEqualTo(WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE)
}
@Test
@FlakyTest(detail = "https://github.com/google/accompanist/issues/491")
@SdkSuppress(minSdkVersion = 23) // rootWindowInsets which work
@Category(IgnoreOnRobolectric::class)
fun statusBarsVisibility() {
// Now create an AndroidSystemUiController() and set the system bar colors
val controller = rule.scenario.withActivity {
AndroidSystemUiController(contentView, window)
}
// First show the bars
rule.scenario.onActivity {
controller.isStatusBarVisible = true
}
waitUntil { isRootWindowTypeVisible(WindowInsetsCompat.Type.statusBars()) }
// Now hide the bars
rule.scenario.onActivity {
controller.isStatusBarVisible = false
}
waitUntil { !isRootWindowTypeVisible(WindowInsetsCompat.Type.statusBars()) }
}
@Test
@FlakyTest(detail = "https://github.com/google/accompanist/issues/491")
@SdkSuppress(minSdkVersion = 23) // rootWindowInsets which work
@Category(IgnoreOnRobolectric::class)
fun navigationBarsVisibility() {
// Now create an AndroidSystemUiController() and set the system bar colors
val controller = rule.scenario.withActivity {
AndroidSystemUiController(contentView, window)
}
// First show the bars
rule.scenario.onActivity {
controller.isNavigationBarVisible = true
}
waitUntil { isRootWindowTypeVisible(WindowInsetsCompat.Type.navigationBars()) }
// Now hide the bars
rule.scenario.onActivity {
controller.isNavigationBarVisible = false
}
waitUntil { !isRootWindowTypeVisible(WindowInsetsCompat.Type.navigationBars()) }
}
@Test
@Category(IgnoreOnRobolectric::class)
@FlakyTest(detail = "https://github.com/google/accompanist/issues/491")
@SdkSuppress(minSdkVersion = 23) // rootWindowInsets which work
fun systemBarsVisibility() {
// Now create an AndroidSystemUiController() and set the system bar colors
val controller = rule.scenario.withActivity {
AndroidSystemUiController(contentView, window)
}
// First show the bars
rule.scenario.onActivity {
controller.isSystemBarsVisible = true
}
waitUntil { isRootWindowTypeVisible(WindowInsetsCompat.Type.navigationBars()) }
waitUntil { isRootWindowTypeVisible(WindowInsetsCompat.Type.statusBars()) }
// Now hide the bars
rule.scenario.onActivity {
controller.isSystemBarsVisible = false
}
waitUntil { !isRootWindowTypeVisible(WindowInsetsCompat.Type.navigationBars()) }
waitUntil { !isRootWindowTypeVisible(WindowInsetsCompat.Type.statusBars()) }
}
private fun isRootWindowTypeVisible(type: Int): Boolean {
return rule.scenario.withActivity {
ViewCompat.getRootWindowInsets(contentView)!!.isVisible(type)
}
}
}