• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2022 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 android.platform.helpers
18 
19 import android.graphics.Bitmap
20 import android.graphics.Color
21 import androidx.test.runner.screenshot.Screenshot
22 import androidx.test.uiautomator.UiDevice
23 
24 fun UiDevice.getScreenBorderColors(): ScreenBorderColors {
25     val screenshot: Bitmap = Screenshot.capture().bitmap
26 
27     val firstColumn = screenshot.columnColor(x = 0)
28     val lastColumn = screenshot.columnColor(x = screenshot.width - 1)
29     return ScreenBorderColors(leftColumn = firstColumn, rightColumn = lastColumn)
30 }
31 
32 /** Represents colors of the first and last screen column. */
33 data class ScreenBorderColors(val leftColumn: List<Color>, val rightColumn: List<Color>) {
darkerThannull34     infix fun darkerThan(other: ScreenBorderColors): Boolean =
35         leftColumn darkerThan other.leftColumn && rightColumn darkerThan other.rightColumn
36 }
37 
38 /** Returns a list of colors of the column at [x]. */
39 fun Bitmap.columnColor(x: Int): List<Color> = (0 until height).map { y -> getColor(x, y) }
40 
41 /** Returns middle element of a list. Used for debugging purposes only. */
middlenull42 fun List<Color>.middle(): Color? = getOrNull(size / 2)
43 
44 /** Returns whether i-th colors in this list have a luminance lower than the i-th one in [other] */
45 infix fun List<Color>.darkerThan(other: List<Color>): Boolean =
46     zip(other).all { (thisColor, otherColor) -> thisColor darkerThan otherColor }
47 
48 /** Returns whether this color is darker than [other] based on [Color.luminance]. */
darkerThannull49 infix fun Color.darkerThan(other: Color): Boolean = luminance() < other.luminance()
50 
51 /** Returns [true] if the entire device screen is completely black. */
52 // TODO(b/262588714): Add tracing once this is moved to uiautomator_utils.
53 //  (androidx.tracing is not available here.)
54 fun UiDevice.hasBlackScreen(): Boolean = Screenshot.capture().bitmap.isBlack()
55 
56 private fun Bitmap.isBlack(): Boolean {
57     for (i in 0 until width) {
58         for (j in 0 until height) {
59             if (getColor(i, j).toArgb() != Color.BLACK) {
60                 return false
61             }
62         }
63     }
64     return true
65 }
66