1 /*
2  * Copyright (C) 2018 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.view.ViewGroup.LayoutParams.MATCH_PARENT;
20 import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_OVERSCAN;
21 import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
22 import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
23 import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
24 
25 import android.app.Activity;
26 import android.app.AlertDialog;
27 import android.content.Intent;
28 import android.view.Gravity;
29 import android.view.Window;
30 import android.view.WindowManager.LayoutParams;
31 
32 import java.util.function.Consumer;
33 
34 // TODO: Make this an inner class of {@link DialogFrameTest}.
35 public class DialogFrameTestActivity extends Activity {
36 
37     static final String DIALOG_WINDOW_NAME = "TestDialog";
38 
39     // TODO: Passing layout parameters for {@link Dialog} in extra of {@link Intent} instead of
40     // using test case name.
41     // Extra key for test case name.
42     static final String EXTRA_TEST_CASE = "test-case";
43     // Value constants for {@link #EXTRA_TEST_CASE}.
44     static final String TEST_EXPLICIT_POSITION_MATCH_PARENT = "ExplicitPositionMatchParent";
45     static final String TEST_EXPLICIT_POSITION_MATCH_PARENT_NO_LIMITS =
46             "ExplicitPositionMatchParentNoLimits";
47     static final String TEST_EXPLICIT_SIZE = "ExplicitSize";
48     static final String TEST_EXPLICIT_SIZE_BOTTOM_RIGHT_GRAVITY =
49             "ExplicitSizeBottomRightGravity";
50     static final String TEST_EXPLICIT_SIZE_TOP_LEFT_GRAVITY = "ExplicitSizeTopLeftGravity";
51     static final String TEST_MATCH_PARENT = "MatchParent";
52     static final String TEST_MATCH_PARENT_LAYOUT_IN_OVERSCAN = "MatchParentLayoutInOverscan";
53     static final String TEST_NO_FOCUS = "NoFocus";
54     static final String TEST_OVER_SIZED_DIMENSIONS = "OversizedDimensions";
55     static final String TEST_OVER_SIZED_DIMENSIONS_NO_LIMITS = "OversizedDimensionsNoLimits";
56     static final String TEST_WITH_MARGINS = "WithMargins";
57 
58     private AlertDialog mDialog;
59 
60     @Override
onStop()61     protected void onStop() {
62         super.onStop();
63         mDialog.dismiss();
64     }
65 
66     @Override
onResume()67     protected void onResume() {
68         super.onResume();
69         setupTest(getIntent());
70     }
71 
setupTest(Intent intent)72     private void setupTest(Intent intent) {
73         final String testCase = intent.getStringExtra(EXTRA_TEST_CASE);
74         switch (testCase) {
75             case TEST_MATCH_PARENT:
76                 testMatchParent();
77                 break;
78             case TEST_MATCH_PARENT_LAYOUT_IN_OVERSCAN:
79                 testMatchParentLayoutInOverscan();
80                 break;
81             case TEST_EXPLICIT_SIZE:
82                 testExplicitSize();
83                 break;
84             case TEST_EXPLICIT_SIZE_TOP_LEFT_GRAVITY:
85                 testExplicitSizeTopLeftGravity();
86                 break;
87             case TEST_EXPLICIT_SIZE_BOTTOM_RIGHT_GRAVITY:
88                 testExplicitSizeBottomRightGravity();
89                 break;
90             case TEST_OVER_SIZED_DIMENSIONS:
91                 testOversizedDimensions();
92                 break;
93             case TEST_OVER_SIZED_DIMENSIONS_NO_LIMITS:
94                 testOversizedDimensionsNoLimits();
95                 break;
96             case TEST_EXPLICIT_POSITION_MATCH_PARENT:
97                 testExplicitPositionMatchParent();
98                 break;
99             case TEST_EXPLICIT_POSITION_MATCH_PARENT_NO_LIMITS:
100                 testExplicitPositionMatchParentNoLimits();
101                 break;
102             case TEST_NO_FOCUS:
103                 testNoFocus();
104                 break;
105             case TEST_WITH_MARGINS:
106                 testWithMargins();
107                 break;
108             default:
109                 break;
110         }
111     }
112 
doLayoutParamTest(Consumer<LayoutParams> setUp)113     private void doLayoutParamTest(Consumer<LayoutParams> setUp) {
114         mDialog = new AlertDialog.Builder(this).create();
115 
116         mDialog.setMessage("Testing is fun!");
117         mDialog.setTitle(DIALOG_WINDOW_NAME);
118         mDialog.create();
119 
120         Window w = mDialog.getWindow();
121         final LayoutParams params = w.getAttributes();
122         setUp.accept(params);
123         w.setAttributes(params);
124 
125         mDialog.show();
126     }
127 
testMatchParent()128     private void testMatchParent() {
129         doLayoutParamTest(params -> {
130             params.width = MATCH_PARENT;
131             params.height = MATCH_PARENT;
132         });
133     }
134 
testMatchParentLayoutInOverscan()135     private void testMatchParentLayoutInOverscan() {
136         doLayoutParamTest(params -> {
137             params.width = MATCH_PARENT;
138             params.height = MATCH_PARENT;
139             params.flags |= FLAG_LAYOUT_IN_SCREEN;
140             params.flags |= FLAG_LAYOUT_IN_OVERSCAN;
141         });
142     }
143 
testExplicitSize()144     private void testExplicitSize() {
145         doLayoutParamTest(params -> {
146             params.width = 200;
147             params.height = 200;
148         });
149     }
150 
testExplicitSizeTopLeftGravity()151     private void testExplicitSizeTopLeftGravity() {
152         doLayoutParamTest(params -> {
153             params.width = 200;
154             params.height = 200;
155             params.gravity = Gravity.TOP | Gravity.LEFT;
156         });
157     }
158 
testExplicitSizeBottomRightGravity()159     private void testExplicitSizeBottomRightGravity() {
160         doLayoutParamTest(params -> {
161             params.width = 200;
162             params.height = 200;
163             params.gravity = Gravity.BOTTOM | Gravity.RIGHT;
164         });
165     }
166 
testOversizedDimensions()167     private void testOversizedDimensions() {
168         doLayoutParamTest(params -> {
169             params.width = 100000;
170             params.height = 100000;
171         });
172     }
173 
testOversizedDimensionsNoLimits()174     private void testOversizedDimensionsNoLimits() {
175         doLayoutParamTest(params -> {
176             params.width = 5000;
177             params.height = 5000;
178             params.flags |= FLAG_LAYOUT_NO_LIMITS;
179             params.gravity = Gravity.LEFT | Gravity.TOP;
180         });
181     }
182 
testExplicitPositionMatchParent()183     private void testExplicitPositionMatchParent() {
184         doLayoutParamTest(params -> {
185             params.width = MATCH_PARENT;
186             params.height = MATCH_PARENT;
187             params.x = 100;
188             params.y = 100;
189         });
190     }
191 
testExplicitPositionMatchParentNoLimits()192     private void testExplicitPositionMatchParentNoLimits() {
193         doLayoutParamTest(params -> {
194             params.width = MATCH_PARENT;
195             params.height = MATCH_PARENT;
196             params.gravity = Gravity.LEFT | Gravity.TOP;
197             params.flags |= FLAG_LAYOUT_NO_LIMITS;
198             params.x = 100;
199             params.y = 100;
200         });
201     }
202 
testNoFocus()203     private void testNoFocus() {
204         doLayoutParamTest(params -> params.flags |= FLAG_NOT_FOCUSABLE);
205     }
206 
testWithMargins()207     private void testWithMargins() {
208         doLayoutParamTest(params -> {
209             params.gravity = Gravity.LEFT | Gravity.TOP;
210             params.horizontalMargin = .25f;
211             params.verticalMargin = .35f;
212             params.width = 200;
213             params.height = 200;
214             params.x = 0;
215             params.y = 0;
216         });
217     }
218 }
219