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