Skip to content
Open
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,16 +994,23 @@ 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();
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();
int displayedPercentage = (int) ((visibleArea / maxArea) * 100);
int displayedPercentage = maxArea > 0 ? (int) ((visibleArea / maxArea) * 100) : 0;

if (displayedPercentage < areaPercentage) {
mismatchDescription
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.CheckedTextView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
Expand Down Expand Up @@ -767,6 +768,32 @@ public void isDisplayingAtLeastTest() {
assertTrue(isDisplayingAtLeast(20).matches(view));
}

@Test
public void isDisplayingAtLeast_withParentScale() {
GlobalVisibleRectProvider providerMock = mock(GlobalVisibleRectProvider.class);
FrameLayout parent = new FrameLayout(context);
View child = new GlobalVisibleRectTestView(context, providerMock);
parent.addView(child);

// Set the view to be 100x100 with scale(0.5, -0.5): 2,500 pixels
child.setVisibility(View.VISIBLE);
child.layout(0, 0, 100, 100);
child.setScaleX(0.5f);
parent.setScaleY(-0.5f);
when(providerMock.get(any(), any()))
.then(
(Answer<Boolean>)
invocation -> {
// Set the output rectangle to 40x40: 1,600 pixels
Rect argRect = invocation.getArgument(0);
argRect.set(0, 0, 40, 40);
return true;
});

assertFalse(isDisplayingAtLeast(70).matches(child));
assertTrue(isDisplayingAtLeast(60).matches(child));
}

@Test
public void isDisplayingAtLeast_description() {
assertThat(
Expand Down
Loading