Fix the buttons on navigation bar may have the state

During the CTS running, the buttons on navigation bar may draw by
different states. In order to prevent the tests from flakytest,
to make the test to assert less than 30% difference.

Bug: 118762362
Test: atest CtsSystemUiTestCases

Change-Id: I670d5aa316f90af3789451665a849f2cdbf1d65e
diff --git a/tests/tests/systemui/src/android/systemui/cts/LightBarTestBase.java b/tests/tests/systemui/src/android/systemui/cts/LightBarTestBase.java
index a56ab29..14d4bcb 100644
--- a/tests/tests/systemui/src/android/systemui/cts/LightBarTestBase.java
+++ b/tests/tests/systemui/src/android/systemui/cts/LightBarTestBase.java
@@ -163,16 +163,28 @@
         }
         assumeNavigationBarChangesColor(backgroundColorPixelCount, pixels.length);
 
+        int diffCount = 0;
         for (int col = 0; col < bitmap.getWidth(); col++) {
             if (isInsideCutout(col, shiftY)) {
                 continue;
             }
 
             if (dividerColor != pixels[col]) {
+                diffCount++;
+            }
+        }
+
+        boolean success = false;
+        try {
+            assertLessThan(String.format(Locale.ENGLISH,
+                    "There are invalid color pixels. expected= 0x%08x", dividerColor),
+                    0.3f, (float) diffCount / (float)bitmap.getWidth(),
+                    "Is the divider colored according to android:navigationBarDividerColor "
+                            + " in the theme?");
+            success = true;
+        } finally {
+            if (!success) {
                 dumpBitmap(bitmap, methodName);
-                fail(String.format(Locale.ENGLISH,
-                        "Invalid color expected= 0x%08x, actual= 0x%08x",
-                        dividerColor, pixels[col]));
             }
         }
     }
@@ -202,4 +214,18 @@
         }
         return false;
     }
+
+    protected void assertMoreThan(String what, float expected, float actual, String hint) {
+        if (!(actual > expected)) {
+            fail(what + ": expected more than " + expected * 100 + "%, but only got " + actual * 100
+                    + "%; " + hint);
+        }
+    }
+
+    protected void assertLessThan(String what, float expected, float actual, String hint) {
+        if (!(actual < expected)) {
+            fail(what + ": expected less than " + expected * 100 + "%, but got " + actual * 100
+                    + "%; " + hint);
+        }
+    }
 }
diff --git a/tests/tests/systemui/src/android/systemui/cts/LightBarTests.java b/tests/tests/systemui/src/android/systemui/cts/LightBarTests.java
index 5566a0d..c9234d7 100644
--- a/tests/tests/systemui/src/android/systemui/cts/LightBarTests.java
+++ b/tests/tests/systemui/src/android/systemui/cts/LightBarTests.java
@@ -155,16 +155,16 @@
             assumeNavigationBarChangesColor(s.backgroundPixels, s.totalPixels());
 
             assertMoreThan("Not enough pixels colored as in the spec", 0.3f,
-                    (float) s.iconPixels / s.foregroundPixels(),
+                    (float) s.iconPixels / (float) s.foregroundPixels(),
                     "Are the bar icons colored according to the spec "
                             + "(60% black and 24% black)?");
 
             assertLessThan("Too many lighter pixels lighter than the background", 0.05f,
-                    (float) s.sameHueLightPixels / s.foregroundPixels(),
+                    (float) s.sameHueLightPixels / (float) s.foregroundPixels(),
                     "Are the bar icons dark?");
 
             assertLessThan("Too many pixels with a changed hue", 0.05f,
-                    (float) s.unexpectedHuePixels / s.foregroundPixels(),
+                    (float) s.unexpectedHuePixels / (float) s.foregroundPixels(),
                     "Are the bar icons color-free?");
 
             success = true;
@@ -175,20 +175,6 @@
         }
     }
 
-    private void assertMoreThan(String what, float expected, float actual, String hint) {
-        if (!(actual > expected)) {
-            fail(what + ": expected more than " + expected * 100 + "%, but only got " + actual * 100
-                    + "%; " + hint);
-        }
-    }
-
-    private void assertLessThan(String what, float expected, float actual, String hint) {
-        if (!(actual < expected)) {
-            fail(what + ": expected less than " + expected * 100 + "%, but got " + actual * 100
-                    + "%; " + hint);
-        }
-    }
-
     private void requestLightBars(final int background) throws Throwable {
         final LightBarActivity activity = mActivityRule.getActivity();
         activity.runOnUiThread(() -> {