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