Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions espresso/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 4 additions & 2 deletions espresso/core/javatests/androidx/test/espresso/matcher/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)

Expand All @@ -195,13 +196,14 @@ 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",
"//ext/junit",
"//runner/android_junit_runner",
"@maven//:junit_junit",
"@maven//:org_hamcrest_hamcrest_core",
"@maven//:org_mockito_mockito_core",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,73 +17,136 @@
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<UiActivity> activityScenarioRule =
new ActivityScenarioRule<>(UiActivity.class);

@Test
public void invalidPercentageRange() {
assertThrows(IllegalArgumentException.class, () -> isDisplayingAtLeast(-1));
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<Boolean>)
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
Expand All @@ -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),
Expand All @@ -108,53 +171,33 @@ 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"));
}

@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<Boolean>)
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")));
}
}
Loading