Home | History | Annotate | Download | only in wm
      1 /*
      2  * Copyright (C) 2017 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.app.Components.SPLASHSCREEN_ACTIVITY;
     20 import static android.view.Display.DEFAULT_DISPLAY;
     21 
     22 import static org.hamcrest.MatcherAssert.assertThat;
     23 import static org.hamcrest.Matchers.lessThan;
     24 import static org.junit.Assert.fail;
     25 
     26 import android.graphics.Bitmap;
     27 import android.graphics.Color;
     28 import android.graphics.Rect;
     29 import android.platform.test.annotations.Presubmit;
     30 
     31 import org.junit.Test;
     32 
     33 /**
     34  * Build/Install/Run:
     35  *     atest CtsWindowManagerDeviceTestCases:SplashscreenTests
     36  */
     37 @Presubmit
     38 public class SplashscreenTests extends ActivityManagerTestBase {
     39 
     40     @Test
     41     public void testSplashscreenContent() {
     42         launchActivityNoWait(SPLASHSCREEN_ACTIVITY);
     43         mAmWmState.waitForAppTransitionIdleOnDisplay(DEFAULT_DISPLAY);
     44         mAmWmState.getWmState().getStableBounds();
     45         final Bitmap image = takeScreenshot();
     46         // Use ratios to flexibly accomodate circular or not quite rectangular displays
     47         // Note: Color.BLACK is the pixel color outside of the display region
     48         assertColors(image, mAmWmState.getWmState().getStableBounds(),
     49             Color.RED, 0.50f, Color.BLACK, 0.02f);
     50     }
     51 
     52     private void assertColors(Bitmap img, Rect bounds, int primaryColor,
     53         float expectedPrimaryRatio, int secondaryColor, float acceptableWrongRatio) {
     54 
     55         int primaryPixels = 0;
     56         int secondaryPixels = 0;
     57         int wrongPixels = 0;
     58         for (int x = bounds.left; x < bounds.right; x++) {
     59             for (int y = bounds.top; y < bounds.bottom; y++) {
     60                 assertThat(x, lessThan(img.getWidth()));
     61                 assertThat(y, lessThan(img.getHeight()));
     62                 final int color = img.getPixel(x, y);
     63                 if (primaryColor == color) {
     64                     primaryPixels++;
     65                 } else if (secondaryColor == color) {
     66                     secondaryPixels++;
     67                 } else {
     68                     wrongPixels++;
     69                 }
     70             }
     71         }
     72 
     73         final int totalPixels = bounds.width() * bounds.height();
     74         final float primaryRatio = (float) primaryPixels / totalPixels;
     75         if (primaryRatio < expectedPrimaryRatio) {
     76             fail("Less than " + (expectedPrimaryRatio * 100.0f)
     77                     + "% of pixels have non-primary color primaryPixels=" + primaryPixels
     78                     + " secondaryPixels=" + secondaryPixels + " wrongPixels=" + wrongPixels);
     79         }
     80         // Some pixels might be covered by screen shape decorations, like rounded corners.
     81         // On circular displays, there is an antialiased edge.
     82         final float wrongRatio = (float) wrongPixels / totalPixels;
     83         if (wrongRatio > acceptableWrongRatio) {
     84             fail("More than " + (acceptableWrongRatio * 100.0f)
     85                     + "% of pixels have wrong color primaryPixels=" + primaryPixels
     86                     + " secondaryPixels=" + secondaryPixels + " wrongPixels=" + wrongPixels);
     87         }
     88     }
     89 }
     90