1 /*
2  * Copyright (C) 2009 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 org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertTrue;
23 
24 import android.app.Instrumentation;
25 import android.graphics.PixelFormat;
26 import android.graphics.Region;
27 import android.platform.test.annotations.Presubmit;
28 import android.view.KeyEvent;
29 import android.view.SurfaceHolder;
30 import android.view.SurfaceView;
31 import android.view.ViewGroup;
32 
33 import androidx.test.InstrumentationRegistry;
34 import androidx.test.annotation.UiThreadTest;
35 import androidx.test.filters.MediumTest;
36 import androidx.test.rule.ActivityTestRule;
37 import androidx.test.runner.AndroidJUnit4;
38 
39 import com.android.compatibility.common.util.CtsKeyEventUtil;
40 import com.android.compatibility.common.util.PollingCheck;
41 import com.android.compatibility.common.util.WidgetTestUtils;
42 
43 import org.junit.Before;
44 import org.junit.Rule;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 
48 @Presubmit
49 @MediumTest
50 @RunWith(AndroidJUnit4.class)
51 public class SurfaceViewTest {
52     private Instrumentation mInstrumentation;
53     private SurfaceViewCtsActivity mActivity;
54     private SurfaceViewCtsActivity.MockSurfaceView mMockSurfaceView;
55 
56     @Rule
57     public ActivityTestRule<SurfaceViewCtsActivity> mActivityRule =
58             new ActivityTestRule<>(SurfaceViewCtsActivity.class);
59 
60     @Before
setup()61     public void setup() {
62         mInstrumentation = InstrumentationRegistry.getInstrumentation();
63         mActivity = mActivityRule.getActivity();
64         PollingCheck.waitFor(mActivity::hasWindowFocus);
65         mMockSurfaceView = mActivity.getSurfaceView();
66     }
67 
68     @UiThreadTest
69     @Test
testConstructor()70     public void testConstructor() {
71         new SurfaceView(mActivity);
72         new SurfaceView(mActivity, null);
73         new SurfaceView(mActivity, null, 0);
74     }
75 
76     @Test
testSurfaceView()77     public void testSurfaceView() {
78         final int left = 40;
79         final int top = 30;
80         final int right = 320;
81         final int bottom = 240;
82 
83         assertTrue(mMockSurfaceView.isDraw());
84         assertTrue(mMockSurfaceView.isOnAttachedToWindow());
85         assertTrue(mMockSurfaceView.isDispatchDraw());
86         assertTrue(mMockSurfaceView.isSurfaceCreatedCalled());
87         assertTrue(mMockSurfaceView.isSurfaceChanged());
88 
89         assertTrue(mMockSurfaceView.isOnWindowVisibilityChanged());
90         int expectedVisibility = mMockSurfaceView.getVisibility();
91         int actualVisibility = mMockSurfaceView.getVInOnWindowVisibilityChanged();
92         assertEquals(expectedVisibility, actualVisibility);
93 
94         assertTrue(mMockSurfaceView.isOnMeasureCalled());
95         int expectedWidth = mMockSurfaceView.getMeasuredWidth();
96         int expectedHeight = mMockSurfaceView.getMeasuredHeight();
97         int actualWidth = mMockSurfaceView.getWidthInOnMeasure();
98         int actualHeight = mMockSurfaceView.getHeightInOnMeasure();
99         assertEquals(expectedWidth, actualWidth);
100         assertEquals(expectedHeight, actualHeight);
101 
102         Region region = new Region();
103         region.set(left, top, right, bottom);
104         assertTrue(mMockSurfaceView.gatherTransparentRegion(region));
105 
106         mMockSurfaceView.setFormat(PixelFormat.TRANSPARENT);
107         assertFalse(mMockSurfaceView.gatherTransparentRegion(region));
108 
109         SurfaceHolder actual = mMockSurfaceView.getHolder();
110         assertNotNull(actual);
111         assertTrue(actual instanceof SurfaceHolder);
112     }
113 
114     /**
115      * check point:
116      * check surfaceView size before and after layout
117      */
118     @UiThreadTest
119     @Test
testOnSizeChanged()120     public void testOnSizeChanged() {
121         final int left = 40;
122         final int top = 30;
123         final int right = 320;
124         final int bottom = 240;
125 
126         // change the SurfaceView size
127         int beforeLayoutWidth = mMockSurfaceView.getWidth();
128         int beforeLayoutHeight = mMockSurfaceView.getHeight();
129         mMockSurfaceView.resetOnSizeChangedFlag(false);
130         assertFalse(mMockSurfaceView.isOnSizeChangedCalled());
131         mMockSurfaceView.layout(left, top, right, bottom);
132         assertTrue(mMockSurfaceView.isOnSizeChangedCalled());
133         assertEquals(beforeLayoutWidth, mMockSurfaceView.getOldWidth());
134         assertEquals(beforeLayoutHeight, mMockSurfaceView.getOldHeight());
135         assertEquals(right - left, mMockSurfaceView.getWidth());
136         assertEquals(bottom - top, mMockSurfaceView.getHeight());
137     }
138 
139     /**
140      * check point:
141      * check surfaceView scroll X and y before and after scrollTo
142      */
143     @UiThreadTest
144     @Test
testOnScrollChanged()145     public void testOnScrollChanged() {
146         final int scrollToX = 200;
147         final int scrollToY = 200;
148 
149         int oldHorizontal = mMockSurfaceView.getScrollX();
150         int oldVertical = mMockSurfaceView.getScrollY();
151         assertFalse(mMockSurfaceView.isOnScrollChanged());
152         mMockSurfaceView.scrollTo(scrollToX, scrollToY);
153         assertTrue(mMockSurfaceView.isOnScrollChanged());
154         assertEquals(oldHorizontal, mMockSurfaceView.getOldHorizontal());
155         assertEquals(oldVertical, mMockSurfaceView.getOldVertical());
156         assertEquals(scrollToX, mMockSurfaceView.getScrollX());
157         assertEquals(scrollToY, mMockSurfaceView.getScrollY());
158     }
159 
160     @Test
testOnDetachedFromWindow()161     public void testOnDetachedFromWindow() {
162         assertFalse(mMockSurfaceView.isDetachedFromWindow());
163         assertTrue(mMockSurfaceView.isShown());
164         mActivityRule.finishActivity();
165         PollingCheck.waitFor(() -> mMockSurfaceView.isDetachedFromWindow() &&
166                 !mMockSurfaceView.isShown());
167     }
168 
169     @Test
surfaceInvalidatedWhileDetaching()170     public void surfaceInvalidatedWhileDetaching() throws Throwable {
171         assertTrue(mMockSurfaceView.mSurface.isValid());
172         assertFalse(mMockSurfaceView.isDetachedFromWindow());
173         WidgetTestUtils.runOnMainAndLayoutSync(mActivityRule, () -> {
174             ((ViewGroup)mMockSurfaceView.getParent()).removeView(mMockSurfaceView);
175         }, false);
176         assertTrue(mMockSurfaceView.isDetachedFromWindow());
177         assertFalse(mMockSurfaceView.mSurface.isValid());
178     }
179 }
180