Skip to content

Commit c6aa08b

Browse files
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: 979797311
1 parent 8cb345d commit c6aa08b

4 files changed

Lines changed: 141 additions & 86 deletions

File tree

‎espresso/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ The following artifacts were released:
2020
* Don't suppress AppNotIdleException if dumpThreadStates throws.
2121
* Remove Espresso.onIdle tracing
2222
* Fix NullPointerException in UiControllerImpl.
23+
* Fix `isDisplayingAtLeast` matcher to factor in the scale of ancestor views.
2324

2425
**New Features**
2526

‎espresso/core/java/androidx/test/espresso/matcher/ViewMatchers.java‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -994,12 +994,21 @@ protected boolean matchesSafely(View view, Description mismatchDescription) {
994994

995995
Rect screen = getScreenWithoutStatusBarActionBar(view);
996996

997-
float viewHeight = (view.getHeight() > screen.height()) ? screen.height() : view.getHeight();
998-
float viewWidth = (view.getWidth() > screen.width()) ? screen.width() : view.getWidth();
997+
// Calculate cumulative scale from the view and its ancestors.
998+
// Note: Assumes an axis-aligned hierarchy (no ancestor rotation).
999+
float totalScaleX = view.getScaleX();
1000+
float totalScaleY = view.getScaleY();
1001+
ViewParent parent = view.getParent();
1002+
while (parent instanceof View) {
1003+
View parentView = (View) parent;
1004+
totalScaleX *= parentView.getScaleX();
1005+
totalScaleY *= parentView.getScaleY();
1006+
parent = parentView.getParent();
1007+
}
9991008

1000-
// factor in the View's scaleX and scaleY properties.
1001-
viewHeight = Math.min(view.getHeight() * Math.abs(view.getScaleY()), screen.height());
1002-
viewWidth = Math.min(view.getWidth() * Math.abs(view.getScaleX()), screen.width());
1009+
// factor in the View and its ancestors' scaleX and scaleY properties.
1010+
float viewHeight = Math.min(view.getHeight() * Math.abs(totalScaleY), screen.height());
1011+
float viewWidth = Math.min(view.getWidth() * Math.abs(totalScaleX), screen.width());
10031012

10041013
double maxArea = viewHeight * viewWidth;
10051014
double visibleArea = visibleParts.height() * visibleParts.width();

‎espresso/core/javatests/androidx/test/espresso/matcher/BUILD‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,14 @@ axt_android_local_test(
179179
deps = [
180180
":utils",
181181
"//core",
182+
"//core/javatests/androidx/test/core/app/testing",
183+
"//core/javatests/androidx/test/core/app/testing:manifest",
182184
"//espresso/core/java/androidx/test/espresso",
183185
"//espresso/core/java/androidx/test/espresso/assertion",
184186
"//espresso/core/java/androidx/test/espresso/matcher",
185187
"//ext/junit",
186188
"//runner/android_junit_runner",
187189
"@maven//:org_hamcrest_hamcrest_core",
188-
"@maven//:org_mockito_mockito_core",
189190
],
190191
)
191192

@@ -195,13 +196,14 @@ axt_android_library_test(
195196
deps = [
196197
":utils",
197198
"//core",
199+
"//core/javatests/androidx/test/core/app/testing",
200+
"//core/javatests/androidx/test/core/app/testing:manifest",
198201
"//espresso/core/java/androidx/test/espresso",
199202
"//espresso/core/java/androidx/test/espresso/assertion",
200203
"//espresso/core/java/androidx/test/espresso/matcher",
201204
"//ext/junit",
202205
"//runner/android_junit_runner",
203206
"@maven//:junit_junit",
204207
"@maven//:org_hamcrest_hamcrest_core",
205-
"@maven//:org_mockito_mockito_core",
206208
],
207209
)

‎espresso/core/javatests/androidx/test/espresso/matcher/IsDisplayingAtLeastIntegrationTest.java‎

Lines changed: 122 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -17,73 +17,136 @@
1717
package androidx.test.espresso.matcher;
1818

1919
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
20+
import static androidx.test.espresso.Espresso.onView;
21+
import static androidx.test.espresso.assertion.ViewAssertions.matches;
2022
import static androidx.test.espresso.matcher.MatcherTestUtils.getDescription;
2123
import static androidx.test.espresso.matcher.MatcherTestUtils.getMismatchDescription;
2224
import static androidx.test.espresso.matcher.ViewMatchers.assertThat;
2325
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayingAtLeast;
2426
import static androidx.test.espresso.matcher.ViewMatchers.withEffectiveVisibility;
2527
import static org.hamcrest.Matchers.is;
26-
import static org.junit.Assert.assertFalse;
28+
import static org.hamcrest.Matchers.not;
2729
import static org.junit.Assert.assertThrows;
28-
import static org.junit.Assert.assertTrue;
29-
import static org.mockito.ArgumentMatchers.any;
30-
import static org.mockito.Mockito.mock;
31-
import static org.mockito.Mockito.when;
32-
33-
import android.content.Context;
34-
import android.graphics.Point;
35-
import android.graphics.Rect;
30+
3631
import android.view.View;
32+
import android.view.ViewGroup;
33+
import android.widget.FrameLayout;
34+
import androidx.test.core.app.testing.UiActivity;
3735
import androidx.test.espresso.matcher.ViewMatchers.Visibility;
36+
import androidx.test.ext.junit.rules.ActivityScenarioRule;
3837
import androidx.test.ext.junit.runners.AndroidJUnit4;
3938
import androidx.test.filters.LargeTest;
40-
import org.junit.Before;
39+
import org.junit.Rule;
4140
import org.junit.Test;
4241
import org.junit.runner.RunWith;
43-
import org.mockito.stubbing.Answer;
4442

4543
/** Integration tests for {@link ViewMatchers#isDisplayingAtLeast(int)}. */
46-
// TODO: Use real views instead of mocking.
47-
@LargeTest
4844
@RunWith(AndroidJUnit4.class)
45+
@LargeTest
4946
public class IsDisplayingAtLeastIntegrationTest {
5047

51-
private Context context;
52-
53-
@Before
54-
public void setUp() throws Exception {
55-
context = getApplicationContext();
56-
}
48+
@Rule
49+
public ActivityScenarioRule<UiActivity> activityScenarioRule =
50+
new ActivityScenarioRule<>(UiActivity.class);
5751

5852
@Test
5953
public void invalidPercentageRange() {
6054
assertThrows(IllegalArgumentException.class, () -> isDisplayingAtLeast(-1));
6155
assertThrows(IllegalArgumentException.class, () -> isDisplayingAtLeast(101));
6256
}
6357

58+
@Test
59+
public void fullyDisplayed() {
60+
View[] childHolder = new View[1];
61+
activityScenarioRule
62+
.getScenario()
63+
.onActivity(
64+
activity -> {
65+
View child = new View(activity);
66+
childHolder[0] = child;
67+
activity.setContentView(child, new ViewGroup.LayoutParams(100, 100));
68+
});
69+
70+
onView(is(childHolder[0])).check(matches(isDisplayingAtLeast(100)));
71+
}
72+
73+
@Test
74+
public void fullyDisplayed_withScale() {
75+
View[] childHolder = new View[1];
76+
activityScenarioRule
77+
.getScenario()
78+
.onActivity(
79+
activity -> {
80+
FrameLayout parent = new FrameLayout(activity);
81+
parent.setScaleX(0.5f);
82+
View child = new View(activity);
83+
childHolder[0] = child;
84+
child.setScaleY(0.5f);
85+
parent.addView(child, new FrameLayout.LayoutParams(100, 100));
86+
activity.setContentView(
87+
parent,
88+
new ViewGroup.LayoutParams(
89+
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
90+
});
91+
92+
onView(is(childHolder[0])).check(matches(isDisplayingAtLeast(100)));
93+
}
94+
6495
@Test
6596
public void partiallyDisplayed() {
66-
GlobalVisibleRectProvider providerMock = mock(GlobalVisibleRectProvider.class);
67-
View view = new GlobalVisibleRectTestView(context, providerMock);
97+
View[] childHolder = new View[1];
98+
activityScenarioRule
99+
.getScenario()
100+
.onActivity(
101+
activity -> {
102+
FrameLayout parent = new FrameLayout(activity);
103+
View child = new View(activity);
104+
childHolder[0] = child;
105+
parent.addView(child, new FrameLayout.LayoutParams(100, 100));
106+
activity.setContentView(parent, new ViewGroup.LayoutParams(50, 50));
107+
});
108+
109+
// Set the view to be 100x100: 10,000 pixels, parent 50x50: 2,500 pixels (25% visible)
110+
onView(is(childHolder[0])).check(matches(isDisplayingAtLeast(20)));
111+
onView(is(childHolder[0])).check(matches(not(isDisplayingAtLeast(30))));
112+
}
68113

69-
view.setVisibility(View.GONE);
70-
assertFalse(isDisplayingAtLeast(5).matches(view));
114+
@Test
115+
public void partiallyDisplayed_withScale() {
116+
View[] childHolder = new View[1];
117+
activityScenarioRule
118+
.getScenario()
119+
.onActivity(
120+
activity -> {
121+
FrameLayout parent = new FrameLayout(activity);
122+
parent.setScaleY(-0.9f);
123+
View child = new View(activity);
124+
childHolder[0] = child;
125+
child.setScaleX(0.6f);
126+
parent.addView(child, new FrameLayout.LayoutParams(100, 100));
127+
activity.setContentView(parent, new ViewGroup.LayoutParams(60, 60));
128+
});
129+
130+
// Scaled child: 60x90 = 5,400 pixels (horizontal range [20, 80]),
131+
// parent 60x60 clips child to 40x54 = 2,160 pixels (40% visible).
132+
onView(is(childHolder[0])).check(matches(isDisplayingAtLeast(39)));
133+
onView(is(childHolder[0])).check(matches(not(isDisplayingAtLeast(41))));
134+
}
71135

72-
// Set the view to be 100x100: 10,000 pixels
73-
view.setVisibility(View.VISIBLE);
74-
view.layout(0, 0, 100, 100);
75-
when(providerMock.get(any(), any()))
76-
.then(
77-
(Answer<Boolean>)
78-
invocation -> {
79-
// Set the output rectangle to 50x50: 2500 pixels
80-
Rect argRect = invocation.getArgument(0);
81-
argRect.set(0, 0, 50, 50);
82-
return true;
83-
});
84-
85-
assertFalse(isDisplayingAtLeast(30).matches(view));
86-
assertTrue(isDisplayingAtLeast(20).matches(view));
136+
@Test
137+
public void gone() {
138+
View[] childHolder = new View[1];
139+
activityScenarioRule
140+
.getScenario()
141+
.onActivity(
142+
activity -> {
143+
View child = new View(activity);
144+
childHolder[0] = child;
145+
child.setVisibility(View.GONE);
146+
activity.setContentView(child, new ViewGroup.LayoutParams(100, 100));
147+
});
148+
149+
onView(is(childHolder[0])).check(matches(not(isDisplayingAtLeast(5))));
87150
}
88151

89152
@Test
@@ -99,7 +162,7 @@ public void description() {
99162

100163
@Test
101164
public void mismatchDescription_wrongVisibility() {
102-
View view = new View(context);
165+
View view = new View(getApplicationContext());
103166
view.setVisibility(View.GONE);
104167
assertThat(
105168
getMismatchDescription(isDisplayingAtLeast(15), view),
@@ -108,53 +171,33 @@ public void mismatchDescription_wrongVisibility() {
108171

109172
@Test
110173
public void mismatchDescription_notVisible() {
111-
GlobalVisibleRectProvider providerMock = mock(GlobalVisibleRectProvider.class);
112-
View view = new GlobalVisibleRectTestView(context, providerMock);
174+
View view = new View(getApplicationContext());
113175
view.setVisibility(View.VISIBLE);
114-
when(providerMock.get(any(), any())).thenReturn(false);
115176
assertThat(
116177
getMismatchDescription(isDisplayingAtLeast(15), view),
117178
is("view was <0> percent visible to the user"));
118179
}
119180

120181
@Test
121182
public void mismatchDescription_lowVisibility() {
122-
GlobalVisibleRectProvider providerMock = mock(GlobalVisibleRectProvider.class);
123-
View view = new GlobalVisibleRectTestView(context, providerMock);
124-
view.setVisibility(View.VISIBLE);
125-
// Set the area of the view to 100x100 = 10,000
126-
view.layout(0, 0, 100, 100);
127-
when(providerMock.get(any(), any()))
128-
.then(
129-
(Answer<Boolean>)
130-
invocation -> {
131-
// Set the output rectangle to 50x50: 2500 pixels
132-
Rect argRect = invocation.getArgument(0);
133-
argRect.set(0, 0, 50, 50);
134-
return true;
135-
});
136-
assertThat(
137-
getMismatchDescription(isDisplayingAtLeast(35), view),
138-
is("view was <25> percent visible to the user"));
139-
}
140-
141-
/** This interface is used to mock the {@link View#getGlobalVisibleRect(Rect, Point)} method. */
142-
interface GlobalVisibleRectProvider {
143-
boolean get(Rect r, Point offset);
144-
}
145-
146-
private static class GlobalVisibleRectTestView extends View {
147-
148-
private final GlobalVisibleRectProvider provider;
149-
150-
GlobalVisibleRectTestView(Context context, GlobalVisibleRectProvider provider) {
151-
super(context);
152-
this.provider = provider;
153-
}
154-
155-
@Override
156-
public final boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
157-
return provider.get(r, globalOffset);
158-
}
183+
View[] childHolder = new View[1];
184+
activityScenarioRule
185+
.getScenario()
186+
.onActivity(
187+
activity -> {
188+
// Set the area of the view to 100x100 = 10,000, parent to 50x50 = 2,500: 25% visible
189+
FrameLayout parent = new FrameLayout(activity);
190+
View child = new View(activity);
191+
childHolder[0] = child;
192+
parent.addView(child, new FrameLayout.LayoutParams(100, 100));
193+
activity.setContentView(parent, new ViewGroup.LayoutParams(50, 50));
194+
});
195+
196+
onView(is(childHolder[0]))
197+
.check(
198+
(view, noViewFoundException) ->
199+
assertThat(
200+
getMismatchDescription(isDisplayingAtLeast(35), view),
201+
is("view was <25> percent visible to the user")));
159202
}
160203
}

0 commit comments

Comments
 (0)