xref: /aosp_15_r20/cts/tests/tests/systemui/src/android/systemui/cts/LightBarTestBase.java (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
1 /*
2  * Copyright (C) 2017 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.systemui.cts;
18 
19 import static androidx.test.InstrumentationRegistry.getInstrumentation;
20 
21 import static org.junit.Assert.fail;
22 import static org.junit.Assume.assumeTrue;
23 
24 import android.graphics.Bitmap;
25 import android.graphics.Rect;
26 import android.util.Log;
27 import android.view.DisplayCutout;
28 import android.view.WindowInsets;
29 
30 import androidx.test.InstrumentationRegistry;
31 
32 import java.io.File;
33 import java.io.FileOutputStream;
34 import java.io.IOException;
35 import java.nio.file.FileSystems;
36 import java.nio.file.Path;
37 import java.util.ArrayList;
38 
39 public class LightBarTestBase {
40 
41     private static final String TAG = "LightBarTestBase";
42 
43     public static final Path DUMP_PATH = FileSystems.getDefault()
44             .getPath("/sdcard/LightBarTestBase/");
45 
46     public static final int WAIT_TIME = 2000;
47 
48     private ArrayList<Rect> mCutouts;
49 
takeStatusBarScreenshot(LightBarBaseActivity activity)50     protected Bitmap takeStatusBarScreenshot(LightBarBaseActivity activity) {
51         Bitmap fullBitmap = getInstrumentation().getUiAutomation().takeScreenshot();
52         return Bitmap.createBitmap(fullBitmap, activity.getLeft(), 0,
53                 activity.getWidth(), activity.getTop());
54     }
55 
takeNavigationBarScreenshot(LightBarBaseActivity activity)56     protected Bitmap takeNavigationBarScreenshot(LightBarBaseActivity activity) {
57         Bitmap fullBitmap = getInstrumentation().getUiAutomation().takeScreenshot();
58         return Bitmap.createBitmap(fullBitmap, activity.getLeft(), activity.getBottom(),
59                 activity.getRight(), fullBitmap.getHeight() - activity.getBottom());
60     }
61 
dumpBitmap(Bitmap bitmap, String name)62     protected void dumpBitmap(Bitmap bitmap, String name) {
63         File dumpDir = DUMP_PATH.toFile();
64         if (!dumpDir.exists()) {
65             dumpDir.mkdirs();
66         }
67 
68         Path filePath = DUMP_PATH.resolve(name + ".png");
69         Log.e(TAG, "Dumping failed bitmap to " + filePath);
70         FileOutputStream fileStream = null;
71         try {
72             fileStream = new FileOutputStream(filePath.toFile());
73             bitmap.compress(Bitmap.CompressFormat.PNG, 85, fileStream);
74             fileStream.flush();
75         } catch (Exception e) {
76             Log.e(TAG, "Dumping bitmap failed.", e);
77         } finally {
78             if (fileStream != null) {
79                 try {
80                     fileStream.close();
81                 } catch (IOException e) {
82                     e.printStackTrace();
83                 }
84             }
85         }
86     }
87 
assumeNavigationBarChangesColor(int backgroundColorPixelCount, int totalPixel)88     protected void assumeNavigationBarChangesColor(int backgroundColorPixelCount, int totalPixel) {
89         assumeTrue("Not enough background pixels. The navigation bar may not be able to change "
90                 + "color.", backgroundColorPixelCount > 0.3f * totalPixel);
91     }
92 
loadCutout(LightBarBaseActivity activity)93     protected ArrayList loadCutout(LightBarBaseActivity activity) {
94         mCutouts = new ArrayList<>();
95         InstrumentationRegistry.getInstrumentation().runOnMainSync(()-> {
96             WindowInsets windowInsets = activity.getRootWindowInsets();
97             DisplayCutout displayCutout = windowInsets.getDisplayCutout();
98             if (displayCutout != null) {
99                 mCutouts.addAll(displayCutout.getBoundingRects());
100             }
101         });
102         return mCutouts;
103     }
104 
isInsideCutout(int x, int y)105     protected boolean isInsideCutout(int x, int y) {
106         for (Rect cutout : mCutouts) {
107             if (cutout.contains(x, y)) {
108                 return true;
109             }
110         }
111         return false;
112     }
113 
assertMoreThan(String what, float expected, float actual, String hint)114     protected void assertMoreThan(String what, float expected, float actual, String hint) {
115         if (!(actual > expected)) {
116             fail(what + ": expected more than " + expected * 100 + "%, but only got " + actual * 100
117                     + "%; " + hint);
118         }
119     }
120 
assertLessThan(String what, float expected, float actual, String hint)121     protected void assertLessThan(String what, float expected, float actual, String hint) {
122         if (!(actual < expected)) {
123             fail(what + ": expected less than " + expected * 100 + "%, but got " + actual * 100
124                     + "%; " + hint);
125         }
126     }
127 }
128