From 3b4c3bd4c1d8d611c3f626e34b9507da4926d83c Mon Sep 17 00:00:00 2001 From: AndroidX Test Team Date: Tue, 15 Sep 2026 17:46:22 -0700 Subject: [PATCH] Fix `isDisplayingAtLeast()` matcher to factor in the scale of ancestor views. `isDisplayingAtLeast(N)` determines whether at least N% of a view is visible on the screen by comparing its visible area size against its total view size. Prior to this change, the total view size calculation did not account for ancestor view scales, whereas the visible area size did. Because of this mismatch, `isDisplayingAtLeast(90)` would incorrectly return `false` even when the view was fully visible, specifically when one or more ancestor views were scaled down (shrunk). This change resolves the issue by incorporating ancestor view scaling into the total view size calculation. PiperOrigin-RevId: 982151410 --- espresso/CHANGELOG.md | 1 + .../test/espresso/matcher/ViewMatchers.java | 19 +- .../androidx/test/espresso/matcher/BUILD | 6 +- .../IsDisplayingAtLeastIntegrationTest.java | 201 +++++++++++------- 4 files changed, 141 insertions(+), 86 deletions(-) diff --git a/espresso/CHANGELOG.md b/espresso/CHANGELOG.md index 110c0a51f..e411153da 100644 --- a/espresso/CHANGELOG.md +++ b/espresso/CHANGELOG.md @@ -20,6 +20,7 @@ The following artifacts were released: * Don't suppress AppNotIdleException if dumpThreadStates throws. * Remove Espresso.onIdle tracing * Fix NullPointerException in UiControllerImpl. +* Fix `isDisplayingAtLeast` matcher to factor in the scale of ancestor views. **New Features** diff --git a/espresso/core/java/androidx/test/espresso/matcher/ViewMatchers.java b/espresso/core/java/androidx/test/espresso/matcher/ViewMatchers.java index 4246e8d2f..abc2de5d7 100644 --- a/espresso/core/java/androidx/test/espresso/matcher/ViewMatchers.java +++ b/espresso/core/java/androidx/test/espresso/matcher/ViewMatchers.java @@ -994,12 +994,21 @@ protected boolean matchesSafely(View view, Description mismatchDescription) { Rect screen = getScreenWithoutStatusBarActionBar(view); - float viewHeight = (view.getHeight() > screen.height()) ? screen.height() : view.getHeight(); - float viewWidth = (view.getWidth() > screen.width()) ? screen.width() : view.getWidth(); + // Calculate cumulative scale from the view and its ancestors. + // Note: Assumes an axis-aligned hierarchy (no ancestor rotation). + float totalScaleX = view.getScaleX(); + float totalScaleY = view.getScaleY(); + ViewParent parent = view.getParent(); + while (parent instanceof View) { + View parentView = (View) parent; + totalScaleX *= parentView.getScaleX(); + totalScaleY *= parentView.getScaleY(); + parent = parentView.getParent(); + } - // factor in the View's scaleX and scaleY properties. - viewHeight = Math.min(view.getHeight() * Math.abs(view.getScaleY()), screen.height()); - viewWidth = Math.min(view.getWidth() * Math.abs(view.getScaleX()), screen.width()); + // factor in the View and its ancestors' scaleX and scaleY properties. + float viewHeight = Math.min(view.getHeight() * Math.abs(totalScaleY), screen.height()); + float viewWidth = Math.min(view.getWidth() * Math.abs(totalScaleX), screen.width()); double maxArea = viewHeight * viewWidth; double visibleArea = visibleParts.height() * visibleParts.width(); diff --git a/espresso/core/javatests/androidx/test/espresso/matcher/BUILD b/espresso/core/javatests/androidx/test/espresso/matcher/BUILD index 44ae88845..1458296bd 100644 --- a/espresso/core/javatests/androidx/test/espresso/matcher/BUILD +++ b/espresso/core/javatests/androidx/test/espresso/matcher/BUILD @@ -179,13 +179,14 @@ axt_android_local_test( deps = [ ":utils", "//core", + "//core/javatests/androidx/test/core/app/testing", + "//core/javatests/androidx/test/core/app/testing:manifest", "//espresso/core/java/androidx/test/espresso", "//espresso/core/java/androidx/test/espresso/assertion", "//espresso/core/java/androidx/test/espresso/matcher", "//ext/junit", "//runner/android_junit_runner", "@maven//:org_hamcrest_hamcrest_core", - "@maven//:org_mockito_mockito_core", ], ) @@ -195,6 +196,8 @@ axt_android_library_test( deps = [ ":utils", "//core", + "//core/javatests/androidx/test/core/app/testing", + "//core/javatests/androidx/test/core/app/testing:manifest", "//espresso/core/java/androidx/test/espresso", "//espresso/core/java/androidx/test/espresso/assertion", "//espresso/core/java/androidx/test/espresso/matcher", @@ -202,6 +205,5 @@ axt_android_library_test( "//runner/android_junit_runner", "@maven//:junit_junit", "@maven//:org_hamcrest_hamcrest_core", - "@maven//:org_mockito_mockito_core", ], ) diff --git a/espresso/core/javatests/androidx/test/espresso/matcher/IsDisplayingAtLeastIntegrationTest.java b/espresso/core/javatests/androidx/test/espresso/matcher/IsDisplayingAtLeastIntegrationTest.java index 715b7f675..3f99ff787 100644 --- a/espresso/core/javatests/androidx/test/espresso/matcher/IsDisplayingAtLeastIntegrationTest.java +++ b/espresso/core/javatests/androidx/test/espresso/matcher/IsDisplayingAtLeastIntegrationTest.java @@ -17,43 +17,37 @@ package androidx.test.espresso.matcher; import static androidx.test.core.app.ApplicationProvider.getApplicationContext; +import static androidx.test.espresso.Espresso.onView; +import static androidx.test.espresso.assertion.ViewAssertions.matches; import static androidx.test.espresso.matcher.MatcherTestUtils.getDescription; import static androidx.test.espresso.matcher.MatcherTestUtils.getMismatchDescription; import static androidx.test.espresso.matcher.ViewMatchers.assertThat; import static androidx.test.espresso.matcher.ViewMatchers.isDisplayingAtLeast; import static androidx.test.espresso.matcher.ViewMatchers.withEffectiveVisibility; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertFalse; +import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertThrows; -import static org.junit.Assert.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import android.content.Context; -import android.graphics.Point; -import android.graphics.Rect; + import android.view.View; +import android.view.ViewGroup; +import android.widget.FrameLayout; +import androidx.test.core.app.testing.UiActivity; import androidx.test.espresso.matcher.ViewMatchers.Visibility; +import androidx.test.ext.junit.rules.ActivityScenarioRule; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.LargeTest; -import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.stubbing.Answer; /** Integration tests for {@link ViewMatchers#isDisplayingAtLeast(int)}. */ -// TODO: Use real views instead of mocking. -@LargeTest @RunWith(AndroidJUnit4.class) +@LargeTest public class IsDisplayingAtLeastIntegrationTest { - private Context context; - - @Before - public void setUp() throws Exception { - context = getApplicationContext(); - } + @Rule + public ActivityScenarioRule activityScenarioRule = + new ActivityScenarioRule<>(UiActivity.class); @Test public void invalidPercentageRange() { @@ -61,29 +55,98 @@ public void invalidPercentageRange() { assertThrows(IllegalArgumentException.class, () -> isDisplayingAtLeast(101)); } + @Test + public void fullyDisplayed() { + View[] childHolder = new View[1]; + activityScenarioRule + .getScenario() + .onActivity( + activity -> { + View child = new View(activity); + childHolder[0] = child; + activity.setContentView(child, new ViewGroup.LayoutParams(100, 100)); + }); + + onView(is(childHolder[0])).check(matches(isDisplayingAtLeast(100))); + } + + @Test + public void fullyDisplayed_withScale() { + View[] childHolder = new View[1]; + activityScenarioRule + .getScenario() + .onActivity( + activity -> { + FrameLayout parent = new FrameLayout(activity); + parent.setScaleX(0.5f); + View child = new View(activity); + childHolder[0] = child; + child.setScaleY(0.5f); + parent.addView(child, new FrameLayout.LayoutParams(100, 100)); + activity.setContentView( + parent, + new ViewGroup.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); + }); + + onView(is(childHolder[0])).check(matches(isDisplayingAtLeast(100))); + } + @Test public void partiallyDisplayed() { - GlobalVisibleRectProvider providerMock = mock(GlobalVisibleRectProvider.class); - View view = new GlobalVisibleRectTestView(context, providerMock); + View[] childHolder = new View[1]; + activityScenarioRule + .getScenario() + .onActivity( + activity -> { + FrameLayout parent = new FrameLayout(activity); + View child = new View(activity); + childHolder[0] = child; + parent.addView(child, new FrameLayout.LayoutParams(100, 100)); + activity.setContentView(parent, new ViewGroup.LayoutParams(50, 50)); + }); + + // Set the view to be 100x100: 10,000 pixels, parent 50x50: 2,500 pixels (25% visible) + onView(is(childHolder[0])).check(matches(isDisplayingAtLeast(20))); + onView(is(childHolder[0])).check(matches(not(isDisplayingAtLeast(30)))); + } - view.setVisibility(View.GONE); - assertFalse(isDisplayingAtLeast(5).matches(view)); + @Test + public void partiallyDisplayed_withScale() { + View[] childHolder = new View[1]; + activityScenarioRule + .getScenario() + .onActivity( + activity -> { + FrameLayout parent = new FrameLayout(activity); + parent.setScaleY(-0.9f); + View child = new View(activity); + childHolder[0] = child; + child.setScaleX(0.6f); + parent.addView(child, new FrameLayout.LayoutParams(100, 100)); + activity.setContentView(parent, new ViewGroup.LayoutParams(60, 60)); + }); + + // Scaled child: 60x90 = 5,400 pixels (horizontal range [20, 80]), + // parent 60x60 clips child to 40x54 = 2,160 pixels (40% visible). + onView(is(childHolder[0])).check(matches(isDisplayingAtLeast(39))); + onView(is(childHolder[0])).check(matches(not(isDisplayingAtLeast(41)))); + } - // Set the view to be 100x100: 10,000 pixels - view.setVisibility(View.VISIBLE); - view.layout(0, 0, 100, 100); - when(providerMock.get(any(), any())) - .then( - (Answer) - invocation -> { - // Set the output rectangle to 50x50: 2500 pixels - Rect argRect = invocation.getArgument(0); - argRect.set(0, 0, 50, 50); - return true; - }); - - assertFalse(isDisplayingAtLeast(30).matches(view)); - assertTrue(isDisplayingAtLeast(20).matches(view)); + @Test + public void gone() { + View[] childHolder = new View[1]; + activityScenarioRule + .getScenario() + .onActivity( + activity -> { + View child = new View(activity); + childHolder[0] = child; + child.setVisibility(View.GONE); + activity.setContentView(child, new ViewGroup.LayoutParams(100, 100)); + }); + + onView(is(childHolder[0])).check(matches(not(isDisplayingAtLeast(5)))); } @Test @@ -99,7 +162,7 @@ public void description() { @Test public void mismatchDescription_wrongVisibility() { - View view = new View(context); + View view = new View(getApplicationContext()); view.setVisibility(View.GONE); assertThat( getMismatchDescription(isDisplayingAtLeast(15), view), @@ -108,10 +171,8 @@ public void mismatchDescription_wrongVisibility() { @Test public void mismatchDescription_notVisible() { - GlobalVisibleRectProvider providerMock = mock(GlobalVisibleRectProvider.class); - View view = new GlobalVisibleRectTestView(context, providerMock); + View view = new View(getApplicationContext()); view.setVisibility(View.VISIBLE); - when(providerMock.get(any(), any())).thenReturn(false); assertThat( getMismatchDescription(isDisplayingAtLeast(15), view), is("view was <0> percent visible to the user")); @@ -119,42 +180,24 @@ public void mismatchDescription_notVisible() { @Test public void mismatchDescription_lowVisibility() { - GlobalVisibleRectProvider providerMock = mock(GlobalVisibleRectProvider.class); - View view = new GlobalVisibleRectTestView(context, providerMock); - view.setVisibility(View.VISIBLE); - // Set the area of the view to 100x100 = 10,000 - view.layout(0, 0, 100, 100); - when(providerMock.get(any(), any())) - .then( - (Answer) - invocation -> { - // Set the output rectangle to 50x50: 2500 pixels - Rect argRect = invocation.getArgument(0); - argRect.set(0, 0, 50, 50); - return true; - }); - assertThat( - getMismatchDescription(isDisplayingAtLeast(35), view), - is("view was <25> percent visible to the user")); - } - - /** This interface is used to mock the {@link View#getGlobalVisibleRect(Rect, Point)} method. */ - interface GlobalVisibleRectProvider { - boolean get(Rect r, Point offset); - } - - private static class GlobalVisibleRectTestView extends View { - - private final GlobalVisibleRectProvider provider; - - GlobalVisibleRectTestView(Context context, GlobalVisibleRectProvider provider) { - super(context); - this.provider = provider; - } - - @Override - public final boolean getGlobalVisibleRect(Rect r, Point globalOffset) { - return provider.get(r, globalOffset); - } + View[] childHolder = new View[1]; + activityScenarioRule + .getScenario() + .onActivity( + activity -> { + // Set the area of the view to 100x100 = 10,000, parent to 50x50 = 2,500: 25% visible + FrameLayout parent = new FrameLayout(activity); + View child = new View(activity); + childHolder[0] = child; + parent.addView(child, new FrameLayout.LayoutParams(100, 100)); + activity.setContentView(parent, new ViewGroup.LayoutParams(50, 50)); + }); + + onView(is(childHolder[0])) + .check( + (view, noViewFoundException) -> + assertThat( + getMismatchDescription(isDisplayingAtLeast(35), view), + is("view was <25> percent visible to the user"))); } }