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