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.am;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.graphics.Point;
22 import android.support.test.InstrumentationRegistry;
23 import android.support.test.rule.ActivityTestRule;
24 import android.view.Display;
25 import android.view.WindowManager;
26 
27 class AspectRatioTestsBase {
28     // The delta allowed when comparing two floats for equality. We consider them equal if they are
29     // within two significant digits of each other.
30     private static final float FLOAT_EQUALITY_DELTA = .01f;
31 
32     interface AssertAspectRatioCallback {
assertAspectRatio(float actual)33         void assertAspectRatio(float actual);
34     }
35 
runAspectRatioTest(final ActivityTestRule activityRule, final AssertAspectRatioCallback callback)36     void runAspectRatioTest(final ActivityTestRule activityRule,
37             final AssertAspectRatioCallback callback) {
38         final Activity activity = launchActivity(activityRule);
39         runTest(activity, callback);
40         finishActivity(activityRule);
41 
42         // TODO(b/35810513): All this rotation stuff doesn't really work yet. Need to make sure
43         // context is updated correctly here. Also, what does it mean to be holding a reference to
44         // this activity if changing the orientation will cause a relaunch?
45 //        activity.setRequestedOrientation(SCREEN_ORIENTATION_LANDSCAPE);
46 //        waitForIdle();
47 //        callback.assertAspectRatio(getAspectRatio(activity));
48 //
49 //        activity.setRequestedOrientation(SCREEN_ORIENTATION_PORTRAIT);
50 //        waitForIdle();
51 //        callback.assertAspectRatio(getAspectRatio(activity));
52     }
53 
runTest(Activity activity, AssertAspectRatioCallback callback)54     protected void runTest(Activity activity, AssertAspectRatioCallback callback) {
55         callback.assertAspectRatio(getAspectRatio(activity));
56     }
57 
getAspectRatio(Context context)58      static float getAspectRatio(Context context) {
59         final Display display =
60                 context.getSystemService(WindowManager.class).getDefaultDisplay();
61         final Point size = new Point();
62         display.getSize(size);
63         final float longSide = Math.max(size.x, size.y);
64         final float shortSide = Math.min(size.x, size.y);
65         return longSide / shortSide;
66     }
67 
launchActivity(final ActivityTestRule activityRule)68     protected Activity launchActivity(final ActivityTestRule activityRule) {
69         final Activity activity = activityRule.launchActivity(null);
70         waitForIdle();
71         return activity;
72     }
73 
finishActivity(ActivityTestRule activityRule)74     private void finishActivity(ActivityTestRule activityRule) {
75         final Activity activity = activityRule.getActivity();
76         if (activity != null) {
77             activity.finish();
78         }
79     }
80 
waitForIdle()81     private static void waitForIdle() {
82         InstrumentationRegistry.getInstrumentation().waitForIdleSync();
83     }
84 
aspectRatioEqual(float a, float b)85     static boolean aspectRatioEqual(float a, float b) {
86         return Math.abs(a - b) < FLOAT_EQUALITY_DELTA;
87     }
88 
aspectRatioLessThanEqual(float a, float b)89     static boolean aspectRatioLessThanEqual(float a, float b) {
90         return a < b || aspectRatioEqual(a, b);
91     }
92 }
93