mirror of
https://github.com/material-components/material-components-android.git
synced 2026-02-20 08:39:55 +08:00
Merge "Fix FAB showing as pressed when the user drags off it" into nyc-support-25.1-dev
GitOrigin-RevId=099115399b63ce5e24da28017a2266379bd26930 PiperOrigin-RevId: 141511120
This commit is contained in:
parent
8cd7c9fdc4
commit
5cebd32087
@ -530,10 +530,15 @@ public class FloatingActionButton extends VisibilityAwareImageButton {
|
||||
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent ev) {
|
||||
if(getContentRect(mTouchArea) && !mTouchArea.contains((int) ev.getX(), (int) ev.getY())) {
|
||||
return false;
|
||||
switch (ev.getAction()) {
|
||||
case MotionEvent.ACTION_DOWN:
|
||||
// Skipping the gesture if it doesn't start in in the FAB 'content' area
|
||||
if (getContentRect(mTouchArea)
|
||||
&& !mTouchArea.contains((int) ev.getX(), (int) ev.getY())) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return super.onTouchEvent(ev);
|
||||
}
|
||||
|
||||
|
||||
@ -240,6 +240,29 @@ public class TestUtilsActions {
|
||||
};
|
||||
}
|
||||
|
||||
public static ViewAction setClickable(final boolean clickable) {
|
||||
return new ViewAction() {
|
||||
@Override
|
||||
public Matcher<View> getConstraints() {
|
||||
return isDisplayed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "set clickable";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void perform(UiController uiController, View view) {
|
||||
uiController.loopMainThreadUntilIdle();
|
||||
|
||||
view.setClickable(clickable);
|
||||
|
||||
uiController.loopMainThreadUntilIdle();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets compound drawables on {@link TextView}
|
||||
*/
|
||||
|
||||
@ -440,4 +440,21 @@ public class TestUtilsMatchers {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a matcher that matches {@link View}s that are pressed.
|
||||
*/
|
||||
public static Matcher<View> isPressed() {
|
||||
return new TypeSafeMatcher<View>() {
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
description.appendText("is pressed");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matchesSafely(View view) {
|
||||
return view.isPressed();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -23,7 +23,9 @@ import static android.support.design.testutils.FloatingActionButtonActions.setIm
|
||||
import static android.support.design.testutils.FloatingActionButtonActions.setLayoutGravity;
|
||||
import static android.support.design.testutils.FloatingActionButtonActions.setSize;
|
||||
import static android.support.design.testutils.FloatingActionButtonActions.showThenHide;
|
||||
import static android.support.design.testutils.TestUtilsActions.setClickable;
|
||||
import static android.support.design.testutils.TestUtilsActions.setEnabled;
|
||||
import static android.support.design.testutils.TestUtilsMatchers.isPressed;
|
||||
import static android.support.design.testutils.TestUtilsMatchers.withFabBackgroundFill;
|
||||
import static android.support.design.testutils.TestUtilsMatchers.withFabContentAreaOnMargins;
|
||||
import static android.support.design.testutils.TestUtilsMatchers.withFabContentHeight;
|
||||
@ -36,8 +38,13 @@ import static android.support.test.espresso.matcher.ViewMatchers.withId;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Rect;
|
||||
import android.support.design.test.R;
|
||||
import android.support.design.testutils.TestUtils;
|
||||
import android.support.test.espresso.action.CoordinatesProvider;
|
||||
import android.support.test.espresso.action.GeneralSwipeAction;
|
||||
import android.support.test.espresso.action.Press;
|
||||
import android.support.test.espresso.action.Swipe;
|
||||
import android.support.test.filters.SmallTest;
|
||||
import android.view.Gravity;
|
||||
import android.view.View;
|
||||
@ -139,6 +146,47 @@ public class FloatingActionButtonTest
|
||||
.check(matches(not(isDisplayed())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClickableTouchAndDragOffView() {
|
||||
onView(withId(R.id.fab_standard))
|
||||
.perform(setClickable(true))
|
||||
.perform(new GeneralSwipeAction(
|
||||
Swipe.SLOW,
|
||||
new CoordinatesProvider() {
|
||||
@Override
|
||||
public float[] calculateCoordinates(View view) {
|
||||
// Create coordinators that in the center of the FAB's content area
|
||||
final FloatingActionButton fab = (FloatingActionButton) view;
|
||||
|
||||
final int[] xy = new int[2];
|
||||
fab.getLocationOnScreen(xy);
|
||||
final Rect rect = new Rect();
|
||||
fab.getContentRect(rect);
|
||||
|
||||
return new float[] {
|
||||
xy[0] + rect.centerX(),
|
||||
xy[1] + rect.centerY()
|
||||
};
|
||||
}
|
||||
},
|
||||
new CoordinatesProvider() {
|
||||
@Override
|
||||
public float[] calculateCoordinates(View view) {
|
||||
// Create coordinators that in the center horizontally, but well
|
||||
// below the view vertically (by 50% of the height)
|
||||
final int[] xy = new int[2];
|
||||
view.getLocationOnScreen(xy);
|
||||
|
||||
return new float[]{
|
||||
xy[0] + (view.getWidth() / 2f),
|
||||
xy[1] + (view.getHeight() * 1.5f)
|
||||
};
|
||||
}
|
||||
},
|
||||
Press.FINGER))
|
||||
.check(matches(not(isPressed())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetCompatElevation() {
|
||||
onView(withId(R.id.fab_standard))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user