1 /*
2  * Copyright (C) 2019 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.server.wm;
18 
19 import static android.server.wm.ActivityManagerTestBase.isTablet;
20 
21 import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
22 
23 import static org.junit.Assume.assumeFalse;
24 import static org.junit.Assume.assumeTrue;
25 
26 import android.app.ActivityManager;
27 import android.content.Context;
28 import android.content.pm.PackageManager;
29 import android.content.res.Configuration;
30 import android.graphics.Insets;
31 import android.util.Log;
32 import android.view.WindowInsets;
33 
34 import androidx.test.InstrumentationRegistry;
35 import androidx.test.rule.ActivityTestRule;
36 
37 import org.junit.AssumptionViolatedException;
38 
39 /**
40  * Common assumptions for system bar tests.
41  *
42  * TODO: Unify with copy in systemui tests.
43  */
44 public final class BarTestUtils {
45 
BarTestUtils()46     private BarTestUtils() {
47     }
48 
assumeHasColoredStatusBar(ActivityTestRule<?> rule)49     public static void assumeHasColoredStatusBar(ActivityTestRule<?> rule) {
50         assumeHasColoredBars();
51         assumeHasStatusBar(rule);
52     }
53 
assumeHasStatusBar(ActivityTestRule<?> rule)54     public static void assumeHasStatusBar(ActivityTestRule<?> rule) {
55         assumeFalse("No status bar when running in VR", isRunningInVr());
56 
57         Insets statusBar = getInsets(rule).getInsetsIgnoringVisibility(
58                 WindowInsets.Type.statusBars());
59         assumeFalse("There must be status bar insets.", statusBar.equals(Insets.NONE));
60     }
61 
assumeHasColoredNavigationBar(ActivityTestRule<?> rule)62     public static void assumeHasColoredNavigationBar(ActivityTestRule<?> rule) {
63         assumeFalse("No colored navigation bar on Tablet", isTablet());
64         assumeHasColoredBars();
65 
66         assumeTrue("Bottom stable inset is non-positive, no navigation bar",
67                 getInsets(rule).getStableInsetBottom() > 0);
68     }
69 
assumeHasColoredBars()70     public static void assumeHasColoredBars() {
71         final PackageManager pm = getInstrumentation().getContext().getPackageManager();
72 
73         assumeHasBars();
74 
75         assumeFalse("Automotive navigation bar is opaque",
76                 pm.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE));
77 
78         assumeTrue("Only highEndGfx devices have colored system bars",
79                 ActivityManager.isHighEndGfx());
80     }
81 
assumeHasBars()82     public static void assumeHasBars() {
83         final PackageManager pm = getInstrumentation().getContext().getPackageManager();
84 
85         assumeFalse("Embedded devices don't have system bars",
86                 getInstrumentation().getContext().getPackageManager().hasSystemFeature(
87                         PackageManager.FEATURE_EMBEDDED));
88 
89         assumeFalse("No bars on watches and TVs", pm.hasSystemFeature(PackageManager.FEATURE_WATCH)
90                 || pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION)
91                 || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK));
92 
93         assumeFalse("No bars on PCs", pm.hasSystemFeature(PackageManager.FEATURE_PC));
94     }
95 
isRunningInVr()96     private static boolean isRunningInVr() {
97         final Context context = InstrumentationRegistry.getContext();
98         final Configuration config = context.getResources().getConfiguration();
99         return (config.uiMode & Configuration.UI_MODE_TYPE_MASK)
100                 == Configuration.UI_MODE_TYPE_VR_HEADSET;
101     }
102 
getInsets(ActivityTestRule<?> rule)103     private static WindowInsets getInsets(ActivityTestRule<?> rule) {
104         final WindowInsets[] insets = new WindowInsets[1];
105         try {
106             rule.runOnUiThread(() -> {
107                 insets[0] = rule.getActivity().getWindow().getDecorView().getRootWindowInsets();
108             });
109         } catch (Throwable t) {
110             throw new RuntimeException(t);
111         }
112         return insets[0];
113     }
114 
isAssumptionViolated(Runnable assumption)115     public static boolean isAssumptionViolated(Runnable assumption) {
116         try {
117             assumption.run();
118             return false;
119         } catch (AssumptionViolatedException e) {
120             Log.i("BarTestUtils", "Assumption violated", e);
121             return true;
122         }
123     }
124 }
125