Home | History | Annotate | Download | only in wm
      1 /*
      2  * Copyright 2019 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 package android.server.wm;
     17 
     18 import static org.junit.Assert.assertFalse;
     19 import static org.junit.Assert.assertTrue;
     20 
     21 import android.content.Context;
     22 import android.graphics.Canvas;
     23 import android.graphics.Color;
     24 import android.support.test.uiautomator.UiObjectNotFoundException;
     25 import android.view.cts.surfacevalidator.AnimationFactory;
     26 import android.view.cts.surfacevalidator.CapturedActivity;
     27 import android.view.cts.surfacevalidator.PixelChecker;
     28 import android.view.cts.surfacevalidator.PixelColor;
     29 import android.view.cts.surfacevalidator.SurfaceControlTestCase;
     30 import android.view.Gravity;
     31 import android.view.SurfaceControl;
     32 import android.view.SurfaceHolder;
     33 import android.view.SurfaceView;
     34 import android.widget.FrameLayout;
     35 
     36 import androidx.test.rule.ActivityTestRule;
     37 
     38 import org.junit.After;
     39 import org.junit.Before;
     40 import org.junit.Rule;
     41 import org.junit.Test;
     42 import org.junit.rules.TestName;
     43 
     44 public class SurfaceViewSurfaceValidatorTest {
     45     private static final int DEFAULT_LAYOUT_WIDTH = 100;
     46     private static final int DEFAULT_LAYOUT_HEIGHT = 100;
     47     private static final int DEFAULT_BUFFER_WIDTH = 640;
     48     private static final int DEFAULT_BUFFER_HEIGHT = 480;
     49 
     50     @Rule
     51     public ActivityTestRule<CapturedActivity> mActivityRule =
     52             new ActivityTestRule<>(CapturedActivity.class);
     53 
     54     @Rule
     55     public TestName mName = new TestName();
     56     private CapturedActivity mActivity;
     57 
     58     @Before
     59     public void setup() {
     60         mActivity = mActivityRule.getActivity();
     61         mActivity.dismissPermissionDialog();
     62     }
     63 
     64     /**
     65      * Want to be especially sure we don't leave up the permission dialog, so try and dismiss
     66      * after test.
     67      */
     68     @After
     69     public void tearDown() throws UiObjectNotFoundException {
     70         mActivity.dismissPermissionDialog();
     71     }
     72 
     73     class SurfaceFiller implements SurfaceHolder.Callback {
     74         SurfaceView mSurfaceView;
     75 
     76         SurfaceFiller(Context c) {
     77             mSurfaceView = new SurfaceView(c);
     78             mSurfaceView.getHolder().addCallback(this);
     79         }
     80 
     81         public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
     82             Canvas canvas = holder.lockHardwareCanvas();
     83             canvas.drawColor(Color.GREEN);
     84             holder.unlockCanvasAndPost(canvas);
     85         }
     86 
     87         public void surfaceCreated(SurfaceHolder holder) {
     88         }
     89 
     90         public void surfaceDestroyed(SurfaceHolder holder) {
     91         }
     92     }
     93 
     94     /**
     95      * Verify that showing a SurfaceView on top but not drawing in to it will not produce a background.
     96      */
     97     @Test
     98     public void testOnTopHasNoBackground() throws Throwable {
     99         SurfaceControlTestCase.ParentSurfaceConsumer psc =
    100             new SurfaceControlTestCase.ParentSurfaceConsumer () {
    101                     @Override
    102                     public void addChildren(SurfaceControl parent) {
    103                     }
    104             };
    105         PixelChecker pixelChecker = new PixelChecker(PixelColor.BLACK) {
    106                 @Override
    107                 public boolean checkPixels(int pixelCount, int width, int height) {
    108                     return pixelCount == 0;
    109                 }
    110             };
    111         SurfaceControlTestCase t = new SurfaceControlTestCase(psc, null,
    112                 pixelChecker, DEFAULT_LAYOUT_WIDTH, DEFAULT_LAYOUT_HEIGHT,
    113                 DEFAULT_BUFFER_WIDTH, DEFAULT_BUFFER_HEIGHT);
    114         mActivity.verifyTest(t, mName);
    115     }
    116 
    117     class TwoSurfaceViewTest extends SurfaceControlTestCase {
    118         TwoSurfaceViewTest(ParentSurfaceConsumer psc,
    119                 AnimationFactory animationFactory, PixelChecker pixelChecker,
    120                 int layoutWidth, int layoutHeight, int bufferWidth, int bufferHeight) {
    121             super(psc, animationFactory, pixelChecker, layoutWidth, layoutHeight, bufferWidth, bufferHeight);
    122         }
    123         @Override
    124         public void start(Context context, FrameLayout parent) {
    125             super.start(context, parent);
    126             SurfaceFiller sc = new SurfaceFiller(mActivity);
    127             parent.addView(sc.mSurfaceView,
    128                     new FrameLayout.LayoutParams(DEFAULT_LAYOUT_WIDTH,
    129                             DEFAULT_LAYOUT_HEIGHT, Gravity.LEFT | Gravity.TOP));
    130         }
    131     };
    132 
    133     // Here we add a second translucent surface view and verify that the background
    134     // is behind all SurfaceView (e.g. the first is not obscured)
    135     @Test
    136     public void testBackgroundIsBehindAllSurfaceView() throws Throwable {
    137         SurfaceControlTestCase.ParentSurfaceConsumer psc =
    138             new SurfaceControlTestCase.ParentSurfaceConsumer () {
    139                     @Override
    140                     public void addChildren(SurfaceControl parent) {
    141                     }
    142             };
    143         PixelChecker pixelChecker = new PixelChecker(PixelColor.BLACK) {
    144                 @Override
    145                 public boolean checkPixels(int pixelCount, int width, int height) {
    146                     return pixelCount == 0;
    147                 }
    148             };
    149         TwoSurfaceViewTest t = new TwoSurfaceViewTest(psc, null,
    150                 pixelChecker, DEFAULT_LAYOUT_WIDTH, DEFAULT_LAYOUT_HEIGHT,
    151                 DEFAULT_BUFFER_WIDTH, DEFAULT_BUFFER_HEIGHT);
    152         mActivity.verifyTest(t, mName);
    153     }
    154 }
    155