Home | History | Annotate | Download | only in cts
      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.systemui.cts;
     18 
     19 import static android.support.test.InstrumentationRegistry.getInstrumentation;
     20 import static org.junit.Assert.fail;
     21 import static org.junit.Assume.assumeFalse;
     22 import static org.junit.Assume.assumeTrue;
     23 
     24 import android.app.ActivityManager;
     25 import android.content.Context;
     26 import android.content.pm.PackageManager;
     27 import android.content.res.Configuration;
     28 import android.graphics.Bitmap;
     29 import android.support.test.InstrumentationRegistry;
     30 import android.util.Log;
     31 import android.view.KeyCharacterMap;
     32 import android.view.KeyEvent;
     33 
     34 import java.io.FileOutputStream;
     35 import java.io.IOException;
     36 
     37 public class LightBarTestBase {
     38 
     39     private static final String TAG = "LightBarTestBase";
     40 
     41     public static final String DUMP_PATH = "/sdcard/lightstatustest.png";
     42 
     43     protected Bitmap takeStatusBarScreenshot(LightBarBaseActivity activity) {
     44         Bitmap fullBitmap = getInstrumentation().getUiAutomation().takeScreenshot();
     45         return Bitmap.createBitmap(fullBitmap, 0, 0, activity.getWidth(), activity.getTop());
     46     }
     47 
     48     protected Bitmap takeNavigationBarScreenshot(LightBarBaseActivity activity) {
     49         Bitmap fullBitmap = getInstrumentation().getUiAutomation().takeScreenshot();
     50         return Bitmap.createBitmap(fullBitmap, 0, activity.getBottom(), activity.getWidth(),
     51                 fullBitmap.getHeight() - activity.getBottom());
     52     }
     53 
     54     protected void dumpBitmap(Bitmap bitmap) {
     55         Log.e(TAG, "Dumping failed bitmap to " + DUMP_PATH);
     56         FileOutputStream fileStream = null;
     57         try {
     58             fileStream = new FileOutputStream(DUMP_PATH);
     59             bitmap.compress(Bitmap.CompressFormat.PNG, 85, fileStream);
     60             fileStream.flush();
     61         } catch (Exception e) {
     62             Log.e(TAG, "Dumping bitmap failed.", e);
     63         } finally {
     64             if (fileStream != null) {
     65                 try {
     66                     fileStream.close();
     67                 } catch (IOException e) {
     68                     e.printStackTrace();
     69                 }
     70             }
     71         }
     72     }
     73 
     74     private boolean hasVirtualNavigationBar() {
     75         boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
     76         boolean hasHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);
     77         return !hasBackKey || !hasHomeKey;
     78     }
     79 
     80     private boolean isRunningInVr() {
     81         final Context context = InstrumentationRegistry.getContext();
     82         final Configuration config = context.getResources().getConfiguration();
     83         return (config.uiMode & Configuration.UI_MODE_TYPE_MASK)
     84                 == Configuration.UI_MODE_TYPE_VR_HEADSET;
     85     }
     86 
     87     private void assumeBasics() {
     88         final PackageManager pm = getInstrumentation().getContext().getPackageManager();
     89 
     90         // No bars on embedded devices.
     91         assumeFalse(getInstrumentation().getContext().getPackageManager().hasSystemFeature(
     92                 PackageManager.FEATURE_EMBEDDED));
     93 
     94         // No bars on TVs and watches.
     95         assumeFalse(pm.hasSystemFeature(PackageManager.FEATURE_WATCH)
     96                 || pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION)
     97                 || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK));
     98 
     99 
    100         // Non-highEndGfx devices don't do colored system bars.
    101         assumeTrue(ActivityManager.isHighEndGfx());
    102     }
    103 
    104     protected void assumeHasColoredStatusBar() {
    105         assumeBasics();
    106 
    107         // No status bar when running in Vr
    108         assumeFalse(isRunningInVr());
    109     }
    110 
    111     protected void assumeHasColorNavigationBar() {
    112         assumeBasics();
    113 
    114         // No virtual navigation bar, so no effect.
    115         assumeTrue(hasVirtualNavigationBar());
    116     }
    117 
    118     protected void checkNavigationBarDivider(LightBarBaseActivity activity, int dividerColor) {
    119         final Bitmap bitmap = takeNavigationBarScreenshot(activity);
    120         int[] pixels = new int[bitmap.getHeight() * bitmap.getWidth()];
    121         bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
    122         for (int col = 0; col < bitmap.getWidth(); col++) {
    123             if (dividerColor != pixels[col]) {
    124                 dumpBitmap(bitmap);
    125                 fail("Invalid color exptected=" + dividerColor + " actual=" + pixels[col]);
    126             }
    127         }
    128     }
    129 }
    130