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.app.cts;
18 
19 import android.app.Instrumentation;
20 import android.app.stubs.DisplayTestActivity;
21 import android.app.stubs.OrientationTestUtils;
22 import android.graphics.Point;
23 import android.test.ActivityInstrumentationTestCase2;
24 import android.view.Display;
25 import static com.android.compatibility.common.util.PackageUtil.supportsRotation;
26 
27 /**
28  * Tests to verify functionality of {@link Display}.
29  */
30 public class DisplayTest extends ActivityInstrumentationTestCase2<DisplayTestActivity> {
31     private Instrumentation mInstrumentation;
32     private DisplayTestActivity mActivity;
33 
DisplayTest()34     public DisplayTest() {
35         super("android.app.stubs", DisplayTestActivity.class);
36     }
37 
38     @Override
setUp()39     protected void setUp() throws Exception {
40         super.setUp();
41         mInstrumentation = getInstrumentation();
42         mActivity = getActivity();
43     }
44 
45     /**
46      * Tests that the underlying {@link android.view.DisplayAdjustments} in {@link Display} updates.
47      * The method {@link DisplayTestActivity#getDisplay()} fetches the Display directly from the
48      * {@link android.view.WindowManager}. A display fetched before the rotation should have the
49      * updated adjustments after a rotation.
50      */
testRotation()51     public void testRotation() throws Throwable {
52         if (!supportsRotation()) {
53             // Skip rotation test if device doesn't support it.
54             return;
55         }
56 
57         // Get a {@link Display} instance before rotation.
58         final Display origDisplay = mActivity.getDisplay();
59 
60         // Capture the originally reported width and heights
61         final Point origSize = new Point();
62         origDisplay.getSize(origSize);
63 
64         // Change orientation
65         mActivity.configurationChangeObserver.startObserving();
66         OrientationTestUtils.switchOrientation(mActivity);
67 
68         final boolean squareDisplay = (origSize.x == origSize.y);
69 
70         // Don't wait for the configuration to change if the
71         // the display is square. In many cases it won't.
72         if (!squareDisplay) {
73             mActivity.configurationChangeObserver.await();
74         }
75 
76         final Point newOrigSize = new Point();
77         origDisplay.getSize(newOrigSize);
78 
79         // Get a {@link Display} instance after rotation.
80         final Display updatedDisplay = mActivity.getDisplay();
81         final Point updatedSize = new Point();
82         updatedDisplay.getSize(updatedSize);
83 
84         // For square screens the following assertions do not make sense and will always fail.
85         if (!squareDisplay) {
86             // Ensure that the width and height of the original instance no longer are the same. Note
87             // that this will be false if the device width and height are identical.
88             // Note there are cases where width and height may not all be updated, such as on docked
89             // devices where the app is letterboxed. However at least one dimension needs to be
90             // updated.
91             assertFalse("size from original display instance should have changed",
92                     origSize.equals(newOrigSize));
93         }
94 
95         // Ensure that the width and height of the original instance have been updated to match the
96         // values that would be found in a new instance.
97         assertTrue("size from original display instance should match current",
98                 newOrigSize.equals(updatedSize));
99     }
100 }
101