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.server.wm;
18 
19 import static androidx.test.InstrumentationRegistry.getInstrumentation;
20 
21 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
22 import static org.hamcrest.Matchers.lessThanOrEqualTo;
23 
24 import android.app.Activity;
25 import android.graphics.Point;
26 import android.view.Display;
27 import android.view.WindowManager;
28 
29 import androidx.test.rule.ActivityTestRule;
30 
31 import com.android.compatibility.common.util.PollingCheck;
32 
33 import org.hamcrest.Matcher;
34 import org.junit.Before;
35 
36 class AspectRatioTestsBase {
37     // The delta allowed when comparing two floats for equality. We consider them equal if they are
38     // within two significant digits of each other.
39     private static final float FLOAT_EQUALITY_DELTA = .01f;
40 
41     interface AssertAspectRatioCallback {
assertAspectRatio(float actual, int displayId, Point size)42         void assertAspectRatio(float actual, int displayId, Point size);
43     }
44 
runAspectRatioTest(final ActivityTestRule activityRule, final AssertAspectRatioCallback callback)45     void runAspectRatioTest(final ActivityTestRule activityRule,
46             final AssertAspectRatioCallback callback) {
47         final Activity activity = launchActivity(activityRule);
48         PollingCheck.waitFor(activity::hasWindowFocus);
49         try {
50             callback.assertAspectRatio(getActivityAspectRatio(activity),
51                     getDisplay(activity).getDisplayId(),
52                     getActivitySize(activity));
53         } finally {
54             finishActivity(activityRule);
55         }
56 
57         // TODO(b/35810513): All this rotation stuff doesn't really work yet. Need to make sure
58         // context is updated correctly here. Also, what does it mean to be holding a reference to
59         // this activity if changing the orientation will cause a relaunch?
60 //        activity.setRequestedOrientation(SCREEN_ORIENTATION_LANDSCAPE);
61 //        waitForIdle();
62 //        callback.assertAspectRatio(getAspectRatio(activity));
63 //
64 //        activity.setRequestedOrientation(SCREEN_ORIENTATION_PORTRAIT);
65 //        waitForIdle();
66 //        callback.assertAspectRatio(getAspectRatio(activity));
67     }
68 
69     @Before
wakeUpAndUnlock()70     public void wakeUpAndUnlock() {
71         UiDeviceUtils.pressWakeupButton();
72         UiDeviceUtils.pressUnlockButton();
73     }
74 
getDefaultDisplayAspectRatio()75     static float getDefaultDisplayAspectRatio() {
76         return getAspectRatio(getInstrumentation().getContext().getSystemService(
77                 WindowManager.class).getDefaultDisplay());
78     }
79 
getActivityAspectRatio(Activity activity)80     private static float getActivityAspectRatio(Activity activity) {
81         return getAspectRatio(getDisplay(activity));
82     }
83 
getActivitySize(Activity activity)84     private static Point getActivitySize(Activity activity) {
85         final Point size = new Point();
86         getDisplay(activity).getSize(size);
87         return size;
88     }
89 
getDisplay(Activity activity)90     private static Display getDisplay(Activity activity) {
91         return activity.getWindow().peekDecorView().getDisplay();
92     }
93 
getAspectRatio(Display display)94     private static float getAspectRatio(Display display) {
95         final Point size = new Point();
96         display.getSize(size);
97         final float longSide = Math.max(size.x, size.y);
98         final float shortSide = Math.min(size.x, size.y);
99         return longSide / shortSide;
100     }
101 
launchActivity(final ActivityTestRule activityRule)102     protected Activity launchActivity(final ActivityTestRule activityRule) {
103         final Activity activity = activityRule.launchActivity(null);
104         waitForIdle();
105         return activity;
106     }
107 
finishActivity(ActivityTestRule activityRule)108     private void finishActivity(ActivityTestRule activityRule) {
109         final Activity activity = activityRule.getActivity();
110         if (activity != null) {
111             activity.finish();
112         }
113     }
114 
waitForIdle()115     private static void waitForIdle() {
116         getInstrumentation().waitForIdleSync();
117     }
118 
greaterThanOrEqualToInexact(float expected)119     static Matcher<Float> greaterThanOrEqualToInexact(float expected) {
120         return greaterThanOrEqualTo(expected - FLOAT_EQUALITY_DELTA);
121     }
122 
lessThanOrEqualToInexact(float expected)123     static Matcher<Float> lessThanOrEqualToInexact(float expected) {
124         return lessThanOrEqualTo(expected + FLOAT_EQUALITY_DELTA);
125     }
126 }
127