diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2022-12-01 05:34:07 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2022-12-01 05:34:07 +0000 |
commit | 03501c9a09e1e22da9be627b4152d6d0e1914675 (patch) | |
tree | 3c08f809c83369a5498270fdbc66a40c9eb0b698 | |
parent | 11365bed1733f1fff88eb901edd41c78617959e1 (diff) | |
parent | 84d2a411ee68043f722ff3d67136a211569ad0ca (diff) | |
download | cts-03501c9a09e1e22da9be627b4152d6d0e1914675.tar.gz |
Merge "Fix device-specific failures of ActivityTransitionTests" into android13-tests-dev
-rw-r--r-- | tests/framework/base/windowmanager/src/android/server/wm/ActivityTransitionTests.java | 211 |
1 files changed, 106 insertions, 105 deletions
diff --git a/tests/framework/base/windowmanager/src/android/server/wm/ActivityTransitionTests.java b/tests/framework/base/windowmanager/src/android/server/wm/ActivityTransitionTests.java index 748fd2aa9a9..f15cd5b3f7a 100644 --- a/tests/framework/base/windowmanager/src/android/server/wm/ActivityTransitionTests.java +++ b/tests/framework/base/windowmanager/src/android/server/wm/ActivityTransitionTests.java @@ -73,6 +73,7 @@ import java.util.ArrayList; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; +import java.util.function.Function; /** * <p>Build/Install/Run: @@ -187,9 +188,9 @@ public class ActivityTransitionTests extends ActivityManagerTestBase { final int backgroundColor = Color.WHITE; final TestBounds testBounds = getTestBounds(); - ArrayList<Bitmap> screenshots = runAndScreenshotActivityTransition( - TransitionActivityWithWhiteBackground.class); - assertAppRegionOfScreenIsColor(screenshots, backgroundColor, testBounds); + getTestBuilder().setClass(TransitionActivityWithWhiteBackground.class) + .setTestFunction(createAssertAppRegionOfScreenIsColor(backgroundColor, testBounds)) + .run(); } /** @@ -205,9 +206,10 @@ public class ActivityTransitionTests extends ActivityManagerTestBase { R.anim.alpha_0_with_red_backdrop, R.anim.alpha_0_with_red_backdrop); final TestBounds testBounds = getTestBounds(); - ArrayList<Bitmap> screenshots = runAndScreenshotActivityTransition(activityOptions, - TransitionActivityWithWhiteBackground.class); - assertAppRegionOfScreenIsColor(screenshots, backgroundColor, testBounds); + getTestBuilder().setClass(TransitionActivityWithWhiteBackground.class) + .setActivityOptions(activityOptions) + .setTestFunction(createAssertAppRegionOfScreenIsColor(backgroundColor, testBounds)) + .run(); } /** @@ -222,9 +224,10 @@ public class ActivityTransitionTests extends ActivityManagerTestBase { ); final TestBounds testBounds = getTestBounds(); - ArrayList<Bitmap> screenshots = runAndScreenshotActivityTransition(activityOptions, - TransitionActivity.class); - assertAppRegionOfScreenIsColor(screenshots, backgroundColor, testBounds); + getTestBuilder().setClass(TransitionActivityWithWhiteBackground.class) + .setActivityOptions(activityOptions) + .setTestFunction(createAssertAppRegionOfScreenIsColor(backgroundColor, testBounds)) + .run(); } /** @@ -241,9 +244,9 @@ public class ActivityTransitionTests extends ActivityManagerTestBase { extras.putInt(BACKGROUND_COLOR_KEY, backgroundColor); final TestBounds testBounds = getTestBounds(); - final ArrayList<Bitmap> screenshots = runAndScreenshotActivityTransition( - OverridePendingTransitionActivity.class, extras); - assertAppRegionOfScreenIsColor(screenshots, backgroundColor, testBounds); + getTestBuilder().setClass(OverridePendingTransitionActivity.class).setExtras(extras) + .setTestFunction(createAssertAppRegionOfScreenIsColor(backgroundColor, testBounds)) + .run(); } /** @@ -264,12 +267,11 @@ public class ActivityTransitionTests extends ActivityManagerTestBase { final Bundle extras = new Bundle(); extras.putInt(DIRECTION_KEY, LEFT); final TestBounds testBounds = getTestBounds(); - - final ArrayList<Bitmap> screenshots = runAndScreenshotActivityTransition( - EdgeExtensionActivity.class, extras); - final Rect appBounds = getTopAppBounds(); - assertColorChangeXIndex(screenshots, - appBounds.left + (appBounds.right - appBounds.left) * 3 / 4, testBounds); + final Rect appBounds = testBounds.appBounds; + final int xIndex = appBounds.left + (appBounds.right - appBounds.left) * 3 / 4; + getTestBuilder().setClass(EdgeExtensionActivity.class).setExtras(extras) + .setTestFunction(createAssertColorChangeXIndex(xIndex, testBounds)) + .run(); } /** @@ -290,12 +292,11 @@ public class ActivityTransitionTests extends ActivityManagerTestBase { final Bundle extras = new Bundle(); extras.putInt(DIRECTION_KEY, TOP); final TestBounds testBounds = getTestBounds(); - - final ArrayList<Bitmap> screenshots = runAndScreenshotActivityTransition( - EdgeExtensionActivity.class, extras); - final Rect appBounds = getTopAppBounds(); - assertColorChangeXIndex(screenshots, - (appBounds.left + appBounds.right) / 2, testBounds); + final Rect appBounds = testBounds.appBounds; + final int xIndex = (appBounds.left + appBounds.right) / 2; + getTestBuilder().setClass(EdgeExtensionActivity.class).setExtras(extras) + .setTestFunction(createAssertColorChangeXIndex(xIndex, testBounds)) + .run(); } /** @@ -315,12 +316,11 @@ public class ActivityTransitionTests extends ActivityManagerTestBase { final Bundle extras = new Bundle(); extras.putInt(DIRECTION_KEY, RIGHT); final TestBounds testBounds = getTestBounds(); - - final ArrayList<Bitmap> screenshots = runAndScreenshotActivityTransition( - EdgeExtensionActivity.class, extras); - final Rect appBounds = getTopAppBounds(); - assertColorChangeXIndex(screenshots, - appBounds.left + (appBounds.right - appBounds.left) / 4, testBounds); + final Rect appBounds = testBounds.appBounds; + final int xIndex = appBounds.left + (appBounds.right - appBounds.left) / 4; + getTestBuilder().setClass(EdgeExtensionActivity.class).setExtras(extras) + .setTestFunction(createAssertColorChangeXIndex(xIndex, testBounds)) + .run(); } /** @@ -342,16 +342,51 @@ public class ActivityTransitionTests extends ActivityManagerTestBase { final Bundle extras = new Bundle(); extras.putInt(DIRECTION_KEY, BOTTOM); final TestBounds testBounds = getTestBounds(); + final Rect appBounds = testBounds.appBounds; + final int xIndex = (appBounds.left + appBounds.right) / 2; + getTestBuilder().setClass(EdgeExtensionActivity.class).setExtras(extras) + .setTestFunction(createAssertColorChangeXIndex(xIndex, testBounds)) + .run(); + } + + private TestBuilder getTestBuilder() { + return new TestBuilder(); + } + + private class TestBuilder { + private ActivityOptions mActivityOptions = ActivityOptions.makeBasic(); + private Bundle mExtras = Bundle.EMPTY; + private Class<?> mKlass; + private Function<Bitmap, AssertionResult> mTestFunction; + + public TestBuilder setActivityOptions(ActivityOptions activityOptions) { + this.mActivityOptions = activityOptions; + return this; + } + + public TestBuilder setExtras(Bundle extra) { + this.mExtras = extra; + return this; + } + + public TestBuilder setClass(Class<?> klass) { + this.mKlass = klass; + return this; + } + + public TestBuilder setTestFunction(Function<Bitmap, AssertionResult> testFunction) { + this.mTestFunction = testFunction; + return this; + } - final ArrayList<Bitmap> screenshots = runAndScreenshotActivityTransition( - EdgeExtensionActivity.class, extras); - final Rect appBounds = getTopAppBounds(); - assertColorChangeXIndex(screenshots, - (appBounds.left + appBounds.right) / 2, testBounds); + public void run() { + runAndAssertActivityTransition(mActivityOptions, mKlass, mExtras, mTestFunction); + } } private static class TestBounds { public Rect rect; + public Rect appBounds; public ArrayList<Rect> excluded; } @@ -359,27 +394,16 @@ public class ActivityTransitionTests extends ActivityManagerTestBase { final LauncherActivity activity = startLauncherActivity(); final TestBounds bounds = new TestBounds(); bounds.rect = activity.getActivityFullyVisibleRegion(); + bounds.appBounds = getTopAppBounds(); bounds.excluded = activity.getRoundedCornersRegions(); launchHomeActivityNoWait(); removeRootTasksWithActivityTypes(ALL_ACTIVITY_TYPE_BUT_HOME); return bounds; } - private ArrayList<Bitmap> runAndScreenshotActivityTransition(Class<?> klass) { - return runAndScreenshotActivityTransition(klass, Bundle.EMPTY); - } - - private ArrayList<Bitmap> runAndScreenshotActivityTransition( - ActivityOptions activityOptions, Class<?> klass) { - return runAndScreenshotActivityTransition(activityOptions, klass, Bundle.EMPTY); - } - - private ArrayList<Bitmap> runAndScreenshotActivityTransition(Class<?> klass, Bundle extras) { - return runAndScreenshotActivityTransition(ActivityOptions.makeBasic(), klass, extras); - } - - private ArrayList<Bitmap> runAndScreenshotActivityTransition(ActivityOptions activityOptions, - Class<?> klass, Bundle extras) { + private void runAndAssertActivityTransition(ActivityOptions activityOptions, + Class<?> klass, Bundle extras, + Function<Bitmap, AssertionResult> assertFunction) { final LauncherActivity launcherActivity = startLauncherActivity(); launcherActivity.startActivity(activityOptions, klass, extras); @@ -392,16 +416,28 @@ public class ActivityTransitionTests extends ActivityManagerTestBase { SystemClock.sleep(10); } while (!isTransitioning); - // Because of differences in timing between devices we take 5 screenshots approximately - // 100ms apart to ensure we capture at least one screenshot around the beginning of the - // activity transition. - final ArrayList<Bitmap> screenshots = new ArrayList<>(); - for (int i = 0; i < 5; i++) { - screenshots.add(mInstrumentation.getUiAutomation().takeScreenshot()); - SystemClock.sleep(100); + // Because of differences in timing between devices we try the given assert function + // by taking multiple screenshots approximately to ensure we capture at least one screenshot + // around the beginning of the activity transition. + // The Timing issue exists around the beginning, so we use a sleep duration that increases + // exponentially. The total amount of sleep duration is between 5 and 10 seconds, which + // matches the most common wait time in CTS (2^0 + 2^1 + ... + 2^13 = about 8000). + final ArrayList<AssertionResult> failedResults = new ArrayList<>(); + int sleepDurationMilliseconds = 1; + for (int i = 0; i < 13; i++) { + final AssertionResult result = assertFunction.apply( + mInstrumentation.getUiAutomation().takeScreenshot()); + if (!result.isFailure) { + return; + } + failedResults.add(result); + SystemClock.sleep(sleepDurationMilliseconds); + sleepDurationMilliseconds *= 2; } - return screenshots; + fail("No screenshot of the activity transition passed the assertions ::\n" + + String.join(",\n", failedResults.stream().map(Object::toString) + .toArray(String[]::new))); } private boolean rectsContain(ArrayList<Rect> rect, int x, int y) { @@ -413,22 +449,9 @@ public class ActivityTransitionTests extends ActivityManagerTestBase { return false; } - private void assertAppRegionOfScreenIsColor(ArrayList<Bitmap> transitionScreenshots, - int color, TestBounds testBounds) { - ArrayList<AssertionResult> results = new ArrayList<>(); - for (final Bitmap screenshot : transitionScreenshots) { - final AssertionResult result = getIsAppRegionOfScreenOfColorResult(screenshot, color, - testBounds); - if (!result.isFailure) { - // One screenshot passed the assertion - we are good - return; - } - results.add(result); - } - - fail("No screenshot of the activity transition passed the assertions ::\n" - + String.join(",\n", results.stream().map(Object::toString) - .toArray(String[]::new))); + private Function<Bitmap, AssertionResult> createAssertAppRegionOfScreenIsColor(int color, + TestBounds testBounds) { + return (screen) -> getIsAppRegionOfScreenOfColorResult(screen, color, testBounds); } private static class ColorCheckResult extends AssertionResult { @@ -540,22 +563,9 @@ public class ActivityTransitionTests extends ActivityManagerTestBase { private static final AssertionResult FAILURE = new AssertionResult(true); } - private void assertColorChangeXIndex(ArrayList<Bitmap> transitionScreenshots, int xIndex, - TestBounds testBounds) { - final ArrayList<AssertionResult> results = new ArrayList<>(); - for (final Bitmap screenshot : transitionScreenshots) { - final AssertionResult result = assertColorChangeXIndex(screenshot, xIndex, testBounds); - if (!result.isFailure) { - // One screenshot passed the assertion - we are good - return; - } - results.add(result); - } - - fail("Failed to match a color change at xIndex " + xIndex - + " on any of the transitions screenshots ::\n" - + String.join(",\n", results.stream().map(Object::toString) - .toArray(String[]::new))); + private Function<Bitmap, AssertionResult> createAssertColorChangeXIndex(int xIndex, + TestBounds testBounds) { + return (screen) -> assertColorChangeXIndex(screen, xIndex, testBounds); } private AssertionResult assertColorChangeXIndex(Bitmap screen, int xIndex, @@ -571,6 +581,11 @@ public class ActivityTransitionTests extends ActivityManagerTestBase { continue; } + // Edge pixels can have any color depending on the blending strategy of the device. + if (Math.abs(x - xIndex) <= 1) { + continue; + } + final Color expectedColor; if (x < xIndex) { expectedColor = Color.valueOf(Color.BLUE); @@ -591,13 +606,9 @@ public class ActivityTransitionTests extends ActivityManagerTestBase { sRgbColor = rawColor; } - // Increase tolerance on edge pixels since blending might occur there - final float varianceTolerance = Math.abs(x - xIndex) <= 1 - ? 0.6f : COLOR_VALUE_VARIANCE_TOLERANCE; if (arrayEquals(new float[]{ expectedColor.red(), expectedColor.green(), expectedColor.blue()}, - new float[]{sRgbColor.red(), sRgbColor.green(), sRgbColor.blue()}, - varianceTolerance)) { + new float[]{sRgbColor.red(), sRgbColor.green(), sRgbColor.blue()})) { return new ColorCheckResult(new Point(x, y), expectedColor, sRgbColor); } } @@ -606,16 +617,6 @@ public class ActivityTransitionTests extends ActivityManagerTestBase { return AssertionResult.SUCCESS; } - private boolean colorsEqual(Color c1, Color c2) { - return almostEquals(c1.red(), c2.red(), COLOR_VALUE_VARIANCE_TOLERANCE) - && almostEquals(c1.green(), c2.green(), COLOR_VALUE_VARIANCE_TOLERANCE) - && almostEquals(c1.blue(), c2.blue(), COLOR_VALUE_VARIANCE_TOLERANCE); - } - - private boolean almostEquals(float a, float b, float delta) { - return Math.abs(a - b) < delta; - } - private void setDefaultAnimationScale() { mInitialWindowAnimationScale = runShellCommandSafe("settings get global window_animation_scale"); |