1 /*
2  * Copyright (C) 2022 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.content.res.Configuration.ORIENTATION_LANDSCAPE;
20 import static android.server.wm.ActivityManagerTestBase.ReportedDisplayMetrics;
21 
22 import android.util.Size;
23 
24 /**
25  * A test helper class that queries or changes the latest DisplayMetrics override in WM by
26  * sending Shell wm commands. Provides handling for errors, orientation types and fallback
27  * behaviors.
28  * Example usage: This can be used to resize a Display in CTS test and trigger configuration
29  * changes.
30  */
31 public class DisplayMetricsSession implements AutoCloseable {
32     private final ReportedDisplayMetrics mInitialDisplayMetrics;
33     private final int mDisplayId;
34 
DisplayMetricsSession(int displayId)35     public DisplayMetricsSession(int displayId) {
36         mDisplayId = displayId;
37         mInitialDisplayMetrics = ReportedDisplayMetrics.getDisplayMetrics(
38                 mDisplayId);
39     }
40 
getInitialDisplayMetrics()41     public ReportedDisplayMetrics getInitialDisplayMetrics() {
42         return mInitialDisplayMetrics;
43     }
44 
getDisplayMetrics()45     public ReportedDisplayMetrics getDisplayMetrics() {
46         return ReportedDisplayMetrics.getDisplayMetrics(mDisplayId);
47     }
48 
changeAspectRatio(double aspectRatio, int orientation)49     public void changeAspectRatio(double aspectRatio, int orientation) {
50         final Size originalSize = mInitialDisplayMetrics.physicalSize;
51         final int smaller = Math.min(originalSize.getWidth(), originalSize.getHeight());
52         final int larger = (int) (smaller * aspectRatio);
53         Size overrideSize;
54         if (orientation == ORIENTATION_LANDSCAPE) {
55             overrideSize = new Size(larger, smaller);
56         } else {
57             overrideSize = new Size(smaller, larger);
58         }
59         overrideDisplayMetrics(overrideSize, mInitialDisplayMetrics.physicalDensity);
60     }
61 
changeDisplayMetrics(double sizeRatio, double densityRatio)62     public void changeDisplayMetrics(double sizeRatio, double densityRatio) {
63         // Given a display may already have an override applied before the test is begun,
64         // resize based upon the override.
65         final Size originalSize;
66         final int density;
67         if (mInitialDisplayMetrics.overrideSize != null) {
68             originalSize = mInitialDisplayMetrics.overrideSize;
69         } else {
70             originalSize = mInitialDisplayMetrics.physicalSize;
71         }
72 
73         if (mInitialDisplayMetrics.overrideDensity != null) {
74             density = mInitialDisplayMetrics.overrideDensity;
75         } else {
76             density = mInitialDisplayMetrics.physicalDensity;
77         }
78 
79         final Size overrideSize = new Size((int)(originalSize.getWidth() * sizeRatio),
80                 (int)(originalSize.getHeight() * sizeRatio));
81         final int overrideDensity = (int)(density * densityRatio);
82         overrideDisplayMetrics(overrideSize, overrideDensity);
83     }
84 
overrideDisplayMetrics(final Size size, final int density)85     public void overrideDisplayMetrics(final Size size, final int density) {
86         mInitialDisplayMetrics.setDisplayMetrics(size, density);
87     }
88 
restoreDisplayMetrics()89     public void restoreDisplayMetrics() {
90         mInitialDisplayMetrics.restoreDisplayMetrics();
91     }
92 
93     @Override
close()94     public void close() {
95         restoreDisplayMetrics();
96     }
97 }
98