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.server.wm.LocationOnScreenTests.TestActivity.EXTRA_LAYOUT_PARAMS;
20 import static android.server.wm.LocationOnScreenTests.TestActivity.TEST_COLOR_1;
21 import static android.server.wm.LocationOnScreenTests.TestActivity.TEST_COLOR_2;
22 import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
23 import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR;
24 import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
25 import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER;
26 import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
27 
28 import static org.hamcrest.Matchers.is;
29 
30 import android.app.Activity;
31 import android.content.Context;
32 import android.content.Intent;
33 import android.graphics.Bitmap;
34 import android.graphics.Canvas;
35 import android.graphics.Paint;
36 import android.graphics.PixelFormat;
37 import android.graphics.Point;
38 import android.os.Bundle;
39 import android.platform.test.annotations.Presubmit;
40 import android.support.test.InstrumentationRegistry;
41 import android.support.test.filters.FlakyTest;
42 import android.support.test.filters.SmallTest;
43 import android.support.test.rule.ActivityTestRule;
44 import android.support.test.runner.AndroidJUnit4;
45 import android.view.Gravity;
46 import android.view.View;
47 import android.view.ViewGroup;
48 import android.view.Window;
49 import android.view.WindowManager.LayoutParams;
50 import android.widget.FrameLayout;
51 
52 import com.android.compatibility.common.util.BitmapUtils;
53 import com.android.compatibility.common.util.PollingCheck;
54 
55 import org.hamcrest.Matcher;
56 import org.junit.Assert;
57 import org.junit.Before;
58 import org.junit.Rule;
59 import org.junit.Test;
60 import org.junit.rules.ErrorCollector;
61 import org.junit.runner.RunWith;
62 
63 import java.util.function.Supplier;
64 
65 @RunWith(AndroidJUnit4.class)
66 @SmallTest
67 @Presubmit
68 @FlakyTest(detail = "until proven non-flaky")
69 public class LocationOnScreenTests {
70 
71     @Rule
72     public final ErrorCollector mErrorCollector = new ErrorCollector();
73 
74     @Rule
75     public final ActivityTestRule<TestActivity> mDisplayCutoutActivity =
76             new ActivityTestRule<>(TestActivity.class, false /* initialTouchMode */,
77                     false /* launchActivity */);
78 
79     private LayoutParams mLayoutParams;
80     private Context mContext;
81 
82     @Before
setUp()83     public void setUp() {
84         mContext = InstrumentationRegistry.getContext();
85         mLayoutParams = new LayoutParams(MATCH_PARENT, MATCH_PARENT, LayoutParams.TYPE_APPLICATION,
86                 LayoutParams.FLAG_LAYOUT_IN_SCREEN | LayoutParams.FLAG_LAYOUT_INSET_DECOR,
87                 PixelFormat.TRANSLUCENT);
88     }
89 
90     @Test
testLocationOnDisplay_appWindow()91     public void testLocationOnDisplay_appWindow() {
92         runTest(mLayoutParams);
93     }
94 
95     @Test
testLocationOnDisplay_appWindow_fullscreen()96     public void testLocationOnDisplay_appWindow_fullscreen() {
97         mLayoutParams.flags |= LayoutParams.FLAG_FULLSCREEN;
98         runTest(mLayoutParams);
99     }
100 
101     @Test
testLocationOnDisplay_floatingWindow()102     public void testLocationOnDisplay_floatingWindow() {
103         mLayoutParams.height = 50;
104         mLayoutParams.width = 50;
105         mLayoutParams.gravity = Gravity.CENTER;
106         mLayoutParams.flags &= ~(FLAG_LAYOUT_IN_SCREEN | FLAG_LAYOUT_INSET_DECOR);
107         runTest(mLayoutParams);
108     }
109 
110     @Test
testLocationOnDisplay_appWindow_displayCutoutNever()111     public void testLocationOnDisplay_appWindow_displayCutoutNever() {
112         mLayoutParams.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER;
113         runTest(mLayoutParams);
114     }
115 
116     @Test
testLocationOnDisplay_appWindow_displayCutoutShortEdges()117     public void testLocationOnDisplay_appWindow_displayCutoutShortEdges() {
118         mLayoutParams.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
119         runTest(mLayoutParams);
120     }
121 
runTest(LayoutParams lp)122     private void runTest(LayoutParams lp) {
123         final TestActivity activity = launchAndWait(mDisplayCutoutActivity, lp);
124         PollingCheck.waitFor(() -> getOnMainSync(activity::isEnterAnimationComplete));
125 
126         Point actual = getOnMainSync(activity::getViewLocationOnScreen);
127         Point expected = findTestColorsInScreenshot(actual);
128 
129         assertThat("View.locationOnScreen returned incorrect value", actual, is(expected));
130     }
131 
assertThat(String reason, T actual, Matcher<? super T> matcher)132     private <T> void assertThat(String reason, T actual, Matcher<? super T> matcher) {
133         mErrorCollector.checkThat(reason, actual, matcher);
134     }
135 
getOnMainSync(Supplier<R> f)136     private <R> R getOnMainSync(Supplier<R> f) {
137         final Object[] result = new Object[1];
138         runOnMainSync(() -> result[0] = f.get());
139         //noinspection unchecked
140         return (R) result[0];
141     }
142 
runOnMainSync(Runnable runnable)143     private void runOnMainSync(Runnable runnable) {
144         InstrumentationRegistry.getInstrumentation().runOnMainSync(runnable);
145     }
146 
launchAndWait(ActivityTestRule<T> rule, LayoutParams lp)147     private <T extends Activity> T launchAndWait(ActivityTestRule<T> rule,
148             LayoutParams lp) {
149         final T activity = rule.launchActivity(
150                 new Intent().putExtra(EXTRA_LAYOUT_PARAMS, lp));
151         PollingCheck.waitFor(activity::hasWindowFocus);
152         return activity;
153     }
154 
findTestColorsInScreenshot(Point guess)155     private Point findTestColorsInScreenshot(Point guess) {
156         final Bitmap screenshot =
157                 InstrumentationRegistry.getInstrumentation().getUiAutomation().takeScreenshot();
158 
159         // We have a good guess from locationOnScreen - check there first to avoid having to go over
160         // the entire bitmap. Also increases robustness in the extremely unlikely case that those
161         // colors are visible elsewhere.
162         if (isTestColors(screenshot, guess.x, guess.y)) {
163             return guess;
164         }
165 
166         for (int y = 0; y < screenshot.getHeight(); y++) {
167             for (int x = 0; x < screenshot.getWidth() - 1; x++) {
168                 if (isTestColors(screenshot, x, y)) {
169                     return new Point(x, y);
170                 }
171             }
172         }
173         String path = mContext.getExternalFilesDir(null).getPath();
174         String file = "location_on_screen_failure.png";
175         BitmapUtils.saveBitmap(screenshot, path, file);
176         Assert.fail("No match found for TEST_COLOR_1 and TEST_COLOR_2 pixels. Check "
177                 + path + "/" + file);
178         return null;
179     }
180 
isTestColors(Bitmap screenshot, int x, int y)181     private boolean isTestColors(Bitmap screenshot, int x, int y) {
182         return screenshot.getPixel(x, y) == TEST_COLOR_1
183                 && screenshot.getPixel(x + 1, y) == TEST_COLOR_2;
184     }
185 
186     public static class TestActivity extends Activity {
187 
188         static final int TEST_COLOR_1 = 0xff123456;
189         static final int TEST_COLOR_2 = 0xfffedcba;
190         static final String EXTRA_LAYOUT_PARAMS = "extra.layout_params";
191         private View mView;
192         private boolean mEnterAnimationComplete;
193 
194         @Override
onCreate(Bundle savedInstanceState)195         protected void onCreate(Bundle savedInstanceState) {
196             super.onCreate(savedInstanceState);
197             getWindow().requestFeature(Window.FEATURE_NO_TITLE);
198 
199             FrameLayout frame = new FrameLayout(this);
200             frame.setLayoutParams(new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
201             setContentView(frame);
202 
203             mView = new TestView(this);
204             frame.addView(mView, new FrameLayout.LayoutParams(2, 1, Gravity.CENTER));
205 
206             if (getIntent() != null
207                     && getIntent().getParcelableExtra(EXTRA_LAYOUT_PARAMS) != null) {
208                 getWindow().setAttributes(getIntent().getParcelableExtra(EXTRA_LAYOUT_PARAMS));
209             }
210         }
211 
getViewLocationOnScreen()212         public Point getViewLocationOnScreen() {
213             final int[] location = new int[2];
214             mView.getLocationOnScreen(location);
215             return new Point(location[0], location[1]);
216         }
217 
isEnterAnimationComplete()218         public boolean isEnterAnimationComplete() {
219             return mEnterAnimationComplete;
220         }
221 
222         @Override
onEnterAnimationComplete()223         public void onEnterAnimationComplete() {
224             super.onEnterAnimationComplete();
225             mEnterAnimationComplete = true;
226         }
227     }
228 
229     private static class TestView extends View {
230         private Paint mPaint = new Paint();
231 
TestView(Context context)232         public TestView(Context context) {
233             super(context);
234         }
235 
236         @Override
onDraw(Canvas canvas)237         protected void onDraw(Canvas canvas) {
238             super.onDraw(canvas);
239 
240             mPaint.setColor(TEST_COLOR_1);
241             canvas.drawRect(0, 0, 1, 1, mPaint);
242             mPaint.setColor(TEST_COLOR_2);
243             canvas.drawRect(1, 0, 2, 1, mPaint);
244         }
245     }
246 }
247