CTS for windowLightStatus/NavigationBar and navigationBarDivider

Test: CtsSystemUiTestCases
Bug: 66986286
Merged-In: I2efdf7c76444a2298825eea2d524fc2b64c3f6e6
Change-Id: I2efdf7c76444a2298825eea2d524fc2b64c3f6e6
(cherry picked from commit c00ae3574c88c61f976235dfbb16bcc9eacf0fd8)
diff --git a/tests/tests/systemui/Android.mk b/tests/tests/systemui/Android.mk
index 4c7af5a..6ba1e77 100644
--- a/tests/tests/systemui/Android.mk
+++ b/tests/tests/systemui/Android.mk
@@ -29,7 +29,8 @@
 LOCAL_STATIC_JAVA_LIBRARIES := \
     ctstestrunner \
     android-support-test \
-    legacy-android-test
+    legacy-android-test \
+    ub-uiautomator
 
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
diff --git a/tests/tests/systemui/AndroidManifest.xml b/tests/tests/systemui/AndroidManifest.xml
index fc0d841..453640d 100644
--- a/tests/tests/systemui/AndroidManifest.xml
+++ b/tests/tests/systemui/AndroidManifest.xml
@@ -23,6 +23,8 @@
     <application>
         <activity android:name=".LightBarActivity"
                 android:theme="@android:style/Theme.Material.NoActionBar"></activity>
+        <activity android:name=".LightBarThemeActivity"
+            android:theme="@style/LightBarTheme"></activity>
         <uses-library android:name="android.test.runner" />
     </application>
 
diff --git a/tests/tests/systemui/res/values/colors.xml b/tests/tests/systemui/res/values/colors.xml
new file mode 100644
index 0000000..6f0558a
--- /dev/null
+++ b/tests/tests/systemui/res/values/colors.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2017 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License
+  -->
+<resources>
+    <color name="navigationBarDividerColor">#ff0000</color>
+</resources>
\ No newline at end of file
diff --git a/tests/tests/systemui/res/values/styles.xml b/tests/tests/systemui/res/values/styles.xml
new file mode 100644
index 0000000..dd95114
--- /dev/null
+++ b/tests/tests/systemui/res/values/styles.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<!--
+  ~ Copyright (C) 2017 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License
+  -->
+<resources>
+    <style name="LightBarTheme" parent="@android:style/Theme.Material.Light.NoActionBar">
+        <item name="android:windowLightStatusBar">true</item>
+        <item name="android:windowLightNavigationBar">true</item>
+        <item name="android:navigationBarColor">@android:color/white</item>
+        <item name="android:navigationBarDividerColor">@color/navigationBarDividerColor</item>
+    </style>
+</resources>
\ No newline at end of file
diff --git a/tests/tests/systemui/src/android/systemui/cts/LightBarActivity.java b/tests/tests/systemui/src/android/systemui/cts/LightBarActivity.java
index 6a1e0d7..a826575 100644
--- a/tests/tests/systemui/src/android/systemui/cts/LightBarActivity.java
+++ b/tests/tests/systemui/src/android/systemui/cts/LightBarActivity.java
@@ -15,29 +15,13 @@
  */
 package android.systemui.cts;
 
-import android.app.Activity;
-import android.graphics.Color;
-import android.os.Bundle;
 import android.view.View;
-import android.view.ViewGroup.LayoutParams;
-
 
 /**
  * An activity that exercises SYSTEM_UI_FLAG_LIGHT_STATUS_BAR and
  * SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR.
  */
-public class LightBarActivity extends Activity {
-
-    private View mContent;
-
-    public void onCreate(Bundle bundle){
-        super.onCreate(bundle);
-
-        mContent = new View(this);
-        mContent.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
-                LayoutParams.MATCH_PARENT));
-        setContentView(mContent);
-    }
+public class LightBarActivity extends LightBarBaseActivity {
 
     public void setLightStatusBar(boolean lightStatusBar) {
         setLightBar(lightStatusBar, View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
@@ -56,16 +40,4 @@
         }
         getWindow().getDecorView().setSystemUiVisibility(vis);
     }
-
-    public int getTop() {
-        return mContent.getLocationOnScreen()[1];
-    }
-
-    public int getBottom() {
-        return mContent.getLocationOnScreen()[1] + mContent.getHeight();
-    }
-
-    public int getWidth() {
-        return mContent.getWidth();
-    }
 }
diff --git a/tests/tests/systemui/src/android/systemui/cts/LightBarBaseActivity.java b/tests/tests/systemui/src/android/systemui/cts/LightBarBaseActivity.java
new file mode 100644
index 0000000..1b228f3
--- /dev/null
+++ b/tests/tests/systemui/src/android/systemui/cts/LightBarBaseActivity.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package android.systemui.cts;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.view.View;
+import android.view.ViewGroup.LayoutParams;
+
+public class LightBarBaseActivity extends Activity {
+
+    private View mContent;
+
+    @Override
+    protected void onCreate(Bundle bundle){
+        super.onCreate(bundle);
+        mContent = new View(this);
+        mContent.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
+                LayoutParams.MATCH_PARENT));
+        setContentView(mContent);
+    }
+
+    public int getSystemUiVisibility() {
+        return mContent.getWindowSystemUiVisibility();
+    }
+
+    public int getTop() {
+        return mContent.getLocationOnScreen()[1];
+    }
+
+    public int getBottom() {
+        return mContent.getLocationOnScreen()[1] + mContent.getHeight();
+    }
+
+    public int getWidth() {
+        return mContent.getWidth();
+    }
+}
diff --git a/tests/tests/systemui/src/android/systemui/cts/LightBarTestBase.java b/tests/tests/systemui/src/android/systemui/cts/LightBarTestBase.java
new file mode 100644
index 0000000..0eb5569
--- /dev/null
+++ b/tests/tests/systemui/src/android/systemui/cts/LightBarTestBase.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package android.systemui.cts;
+
+import static android.support.test.InstrumentationRegistry.getInstrumentation;
+
+import android.graphics.Bitmap;
+import android.util.Log;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+public class LightBarTestBase {
+
+    private static final String TAG = "LightBarTestBase";
+
+    public static final String DUMP_PATH = "/sdcard/lightstatustest.png";
+
+    protected Bitmap takeStatusBarScreenshot(LightBarBaseActivity activity) {
+        Bitmap fullBitmap = getInstrumentation().getUiAutomation().takeScreenshot();
+        return Bitmap.createBitmap(fullBitmap, 0, 0, activity.getWidth(), activity.getTop());
+    }
+
+    protected Bitmap takeNavigationBarScreenshot(LightBarBaseActivity activity) {
+        Bitmap fullBitmap = getInstrumentation().getUiAutomation().takeScreenshot();
+        return Bitmap.createBitmap(fullBitmap, 0, activity.getBottom(), activity.getWidth(),
+                fullBitmap.getHeight() - activity.getBottom());
+    }
+
+    protected void dumpBitmap(Bitmap bitmap) {
+        Log.e(TAG, "Dumping failed bitmap to " + DUMP_PATH);
+        FileOutputStream fileStream = null;
+        try {
+            fileStream = new FileOutputStream(DUMP_PATH);
+            bitmap.compress(Bitmap.CompressFormat.PNG, 85, fileStream);
+            fileStream.flush();
+        } catch (Exception e) {
+            Log.e(TAG, "Dumping bitmap failed.", e);
+        } finally {
+            if (fileStream != null) {
+                try {
+                    fileStream.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+}
diff --git a/tests/tests/systemui/src/android/systemui/cts/LightBarTests.java b/tests/tests/systemui/src/android/systemui/cts/LightBarTests.java
index 56ee558..5d45256 100644
--- a/tests/tests/systemui/src/android/systemui/cts/LightBarTests.java
+++ b/tests/tests/systemui/src/android/systemui/cts/LightBarTests.java
@@ -16,22 +16,26 @@
 
 package android.systemui.cts;
 
+import static android.support.test.InstrumentationRegistry.getInstrumentation;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
 import android.app.ActivityManager;
 import android.app.UiAutomation;
 import android.content.pm.PackageManager;
 import android.graphics.Bitmap;
 import android.graphics.Color;
 import android.os.SystemClock;
-import android.support.test.InstrumentationRegistry;
-import android.test.ActivityInstrumentationTestCase2;
-import android.util.Log;
+import android.support.test.rule.ActivityTestRule;
+import android.support.test.runner.AndroidJUnit4;
 import android.view.InputDevice;
 import android.view.KeyCharacterMap;
 import android.view.KeyEvent;
 import android.view.MotionEvent;
 
-import java.io.FileOutputStream;
-import java.io.IOException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
 
 /**
  * Test for light status bar.
@@ -39,28 +43,18 @@
  * mmma cts/tests/tests/systemui
  * cts-tradefed run commandAndExit cts-dev --module CtsSystemUiTestCases --test android.systemui.cts.LightBarTests --disable-reboot --skip-device-info --skip-all-system-status-check --skip-preconditions
  */
-public class LightBarTests extends ActivityInstrumentationTestCase2<LightBarActivity> {
+@RunWith(AndroidJUnit4.class)
+public class LightBarTests extends LightBarTestBase {
 
     public static final String TAG = "LightStatusBarTests";
 
-    public static final String DUMP_PATH = "/sdcard/lightstatustest.png";
-
     private static final int WAIT_TIME = 2000;
 
-    public LightBarTests() {
-        super(LightBarActivity.class);
-    }
+    @Rule
+    public ActivityTestRule<LightBarActivity> mActivityRule = new ActivityTestRule<>(
+            LightBarActivity.class);
 
-    @Override
-    protected void setUp() throws Exception {
-        super.setUp();
-
-        // As the way to access Instrumentation is changed in the new runner, we need to inject it
-        // manually into ActivityInstrumentationTestCase2. ActivityInstrumentationTestCase2 will
-        // be marked as deprecated and replaced with ActivityTestRule.
-        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
-    }
-
+    @Test
     public void testLightStatusBarIcons() throws Throwable {
         PackageManager pm = getInstrumentation().getContext().getPackageManager();
         if (pm.hasSystemFeature(PackageManager.FEATURE_WATCH)
@@ -78,11 +72,12 @@
         requestLightBars(Color.RED /* background */);
         Thread.sleep(WAIT_TIME);
 
-        Bitmap bitmap = takeStatusBarScreenshot();
+        Bitmap bitmap = takeStatusBarScreenshot(mActivityRule.getActivity());
         Stats s = evaluateLightBarBitmap(bitmap, Color.RED /* background */);
         assertLightStats(bitmap, s);
     }
 
+    @Test
     public void testLightNavigationBar() throws Throwable {
         PackageManager pm = getInstrumentation().getContext().getPackageManager();
         if (pm.hasSystemFeature(PackageManager.FEATURE_WATCH)
@@ -106,12 +101,12 @@
         Thread.sleep(WAIT_TIME);
 
         // Inject a cancelled interaction with the nav bar to ensure it is at full opacity.
-        int x = getActivity().getWidth() / 2;
-        int y = getActivity().getBottom() + 10;
+        int x = mActivityRule.getActivity().getWidth() / 2;
+        int y = mActivityRule.getActivity().getBottom() + 10;
         injectCanceledTap(x, y);
         Thread.sleep(WAIT_TIME);
 
-        Bitmap bitmap = takeNavigationBarScreenshot();
+        Bitmap bitmap = takeNavigationBarScreenshot(mActivityRule.getActivity());
         Stats s = evaluateLightBarBitmap(bitmap, Color.RED /* background */);
         assertLightStats(bitmap, s);
     }
@@ -160,7 +155,6 @@
             success = true;
         } finally {
             if (!success) {
-                Log.e(TAG, "Dumping failed bitmap to " + DUMP_PATH);
                 dumpBitmap(bitmap);
             }
         }
@@ -181,8 +175,8 @@
     }
 
     private void requestLightBars(final int background) throws Throwable {
-        final LightBarActivity activity = getActivity();
-        runTestOnUiThread(() -> {
+        final LightBarActivity activity = mActivityRule.getActivity();
+        activity.runOnUiThread(() -> {
             activity.getWindow().setStatusBarColor(background);
             activity.getWindow().setNavigationBarColor(background);
             activity.setLightStatusBar(true);
@@ -259,25 +253,6 @@
         return s;
     }
 
-    private void dumpBitmap(Bitmap bitmap) {
-        FileOutputStream fileStream = null;
-        try {
-            fileStream = new FileOutputStream(DUMP_PATH);
-            bitmap.compress(Bitmap.CompressFormat.PNG, 85, fileStream);
-            fileStream.flush();
-        } catch (Exception e) {
-            Log.e(TAG, "Dumping bitmap failed.", e);
-        } finally {
-            if (fileStream != null) {
-                try {
-                    fileStream.close();
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-            }
-        }
-    }
-
     private int mixSrcOver(int background, int foreground) {
         int bgAlpha = Color.alpha(background);
         int bgRed = Color.red(background);
@@ -294,16 +269,4 @@
                     fgGreen + (255 - fgAlpha) * bgGreen / 255,
                     fgBlue + (255 - fgAlpha) * bgBlue / 255);
     }
-
-    private Bitmap takeStatusBarScreenshot() {
-        Bitmap fullBitmap = getInstrumentation().getUiAutomation().takeScreenshot();
-        return Bitmap.createBitmap(fullBitmap, 0, 0,
-                getActivity().getWidth(), getActivity().getTop());
-    }
-
-    private Bitmap takeNavigationBarScreenshot() {
-        Bitmap fullBitmap = getInstrumentation().getUiAutomation().takeScreenshot();
-        return Bitmap.createBitmap(fullBitmap, 0, getActivity().getBottom(),
-                getActivity().getWidth(), fullBitmap.getHeight() - getActivity().getBottom());
-    }
 }
diff --git a/tests/tests/systemui/src/android/systemui/cts/LightBarThemeActivity.java b/tests/tests/systemui/src/android/systemui/cts/LightBarThemeActivity.java
new file mode 100644
index 0000000..d28d2b5
--- /dev/null
+++ b/tests/tests/systemui/src/android/systemui/cts/LightBarThemeActivity.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package android.systemui.cts;
+
+public class LightBarThemeActivity extends LightBarBaseActivity {
+}
diff --git a/tests/tests/systemui/src/android/systemui/cts/LightBarThemeTest.java b/tests/tests/systemui/src/android/systemui/cts/LightBarThemeTest.java
new file mode 100644
index 0000000..b385ecd
--- /dev/null
+++ b/tests/tests/systemui/src/android/systemui/cts/LightBarThemeTest.java
@@ -0,0 +1,79 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package android.systemui.cts;
+
+import static android.support.test.InstrumentationRegistry.getInstrumentation;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import android.graphics.Bitmap;
+import android.support.test.rule.ActivityTestRule;
+import android.support.test.runner.AndroidJUnit4;
+import android.support.test.uiautomator.UiDevice;
+import android.view.View;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Tests for light system bars that set the flag via theme.
+ *
+ * mmma cts/tests/tests/systemui
+ * cts-tradefed run commandAndExit cts-dev --module CtsSystemUiTestCases --test android.systemui.cts.LightBarThemeTest --disable-reboot --skip-device-info --skip-all-system-status-check --skip-preconditions
+ */
+@RunWith(AndroidJUnit4.class)
+public class LightBarThemeTest extends LightBarTestBase {
+
+    private UiDevice mDevice;
+
+    @Rule
+    public ActivityTestRule<LightBarThemeActivity> mActivityRule = new ActivityTestRule<>(
+            LightBarThemeActivity.class);
+
+    @Before
+    public void setUp() {
+        mDevice = UiDevice.getInstance(getInstrumentation());
+    }
+
+    @Test
+    public void testThemeSetsFlags() throws Exception {
+        final int visibility = mActivityRule.getActivity().getSystemUiVisibility();
+        assertTrue((visibility & View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR) != 0);
+        assertTrue((visibility & View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR) != 0);
+    }
+
+    @Test
+    public void testNavigationBarDivider() throws Exception {
+
+        // Wait until the activity is fully visible
+        mDevice.waitForIdle();
+
+        final int dividerColor = getInstrumentation().getContext().getColor(
+                R.color.navigationBarDividerColor);
+        final Bitmap bitmap = takeNavigationBarScreenshot(mActivityRule.getActivity());
+        int[] pixels = new int[bitmap.getHeight() * bitmap.getWidth()];
+        bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
+        for (int col = 0; col < bitmap.getWidth(); col++) {
+            if (dividerColor != pixels[col]) {
+                dumpBitmap(bitmap);
+                fail("Invalid color exptected=" + dividerColor + " actual=" + pixels[col]);
+            }
+        }
+    }
+}