Commit 0f7d2f86 authored by Matt Jones's avatar Matt Jones Committed by Commit Bot

Use gesture delegate for scrim component and add test

The GESTURE_DETECTOR property for the scrim is currently unused and
doesn't correctly pass motion events back to the mediator as it was
originally intended to. This patch correctly delegates event handling
to the mediator; there is no behavior change if the GESTURE_DETECTOR
property is null or not set. A test has also been added to ensure
events can be processed by the detector if set.

Bug: 1062099
Change-Id: Ia67f8f6dfa7b16d9f74a71bbe1a73994fe8ed034
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2305921Reviewed-by: default avatarTheresa  <twellington@chromium.org>
Commit-Queue: Matthew Jones <mdjones@chromium.org>
Cr-Commit-Position: refs/heads/master@{#790541}
parent 18640342
...@@ -13,6 +13,8 @@ import static org.junit.Assert.assertTrue; ...@@ -13,6 +13,8 @@ import static org.junit.Assert.assertTrue;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.ColorDrawable;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.FrameLayout; import android.widget.FrameLayout;
...@@ -36,6 +38,7 @@ import org.chromium.content_public.browser.test.util.CriteriaHelper; ...@@ -36,6 +38,7 @@ import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.ui.modelutil.PropertyModel; import org.chromium.ui.modelutil.PropertyModel;
import org.chromium.ui.test.util.DummyUiActivityTestCase; import org.chromium.ui.test.util.DummyUiActivityTestCase;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
/** This class tests the behavior of the scrim component. */ /** This class tests the behavior of the scrim component. */
...@@ -55,6 +58,9 @@ public class ScrimTest extends DummyUiActivityTestCase { ...@@ -55,6 +58,9 @@ public class ScrimTest extends DummyUiActivityTestCase {
private final Callback<Boolean> mVisibilityChangeCallback = private final Callback<Boolean> mVisibilityChangeCallback =
(v) -> mVisibilityChangeCallbackHelper.notifyCalled(); (v) -> mVisibilityChangeCallbackHelper.notifyCalled();
private GestureDetector mCustomGestureDetector;
private CallbackHelper mDelegatedEventHelper;
@Before @Before
public void setUp() throws TimeoutException { public void setUp() throws TimeoutException {
ThreadUtils.runOnUiThreadBlocking(() -> { ThreadUtils.runOnUiThreadBlocking(() -> {
...@@ -68,6 +74,16 @@ public class ScrimTest extends DummyUiActivityTestCase { ...@@ -68,6 +74,16 @@ public class ScrimTest extends DummyUiActivityTestCase {
mScrimCoordinator = mScrimCoordinator =
new ScrimCoordinator(getActivity(), mScrimDelegate, mParent, Color.RED); new ScrimCoordinator(getActivity(), mScrimDelegate, mParent, Color.RED);
mDelegatedEventHelper = new CallbackHelper();
mCustomGestureDetector =
new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
mDelegatedEventHelper.notifyCalled();
return true;
}
});
}); });
} }
...@@ -138,16 +154,43 @@ public class ScrimTest extends DummyUiActivityTestCase { ...@@ -138,16 +154,43 @@ public class ScrimTest extends DummyUiActivityTestCase {
@Test @Test
@SmallTest @SmallTest
@Feature({"Scrim"}) @Feature({"Scrim"})
public void testObserver_clickEvent() throws TimeoutException { public void testObserver_clickEvent() throws ExecutionException, TimeoutException {
showScrim(buildModel(true, false, true, Color.RED), false); showScrim(buildModel(true, false, true, Color.RED), false);
int callCount = mScrimClickCallbackHelper.getCallCount(); int callCount = mScrimClickCallbackHelper.getCallCount();
ScrimMediator mediator = mScrimCoordinator.getMediatorForTesting();
ScrimView scrimView = mScrimCoordinator.getViewForTesting(); ScrimView scrimView = mScrimCoordinator.getViewForTesting();
ThreadUtils.runOnUiThreadBlocking(() -> mediator.onClick(scrimView)); ThreadUtils.runOnUiThreadBlocking(() -> scrimView.callOnClick());
mScrimClickCallbackHelper.waitForCallback(callCount, 1); mScrimClickCallbackHelper.waitForCallback(callCount, 1);
} }
@Test
@SmallTest
@Feature({"Scrim"})
public void testGestureDetector() throws ExecutionException, TimeoutException {
ColorDrawable customDrawable = new ColorDrawable(Color.BLUE);
PropertyModel model =
new PropertyModel.Builder(ScrimProperties.ALL_KEYS)
.with(ScrimProperties.TOP_MARGIN, 0)
.with(ScrimProperties.AFFECTS_STATUS_BAR, false)
.with(ScrimProperties.ANCHOR_VIEW, mAnchorView)
.with(ScrimProperties.SHOW_IN_FRONT_OF_ANCHOR_VIEW, false)
.with(ScrimProperties.CLICK_DELEGATE, mClickDelegate)
.with(ScrimProperties.VISIBILITY_CALLBACK, mVisibilityChangeCallback)
.with(ScrimProperties.BACKGROUND_COLOR, Color.RED)
.with(ScrimProperties.BACKGROUND_DRAWABLE, customDrawable)
.with(ScrimProperties.GESTURE_DETECTOR, mCustomGestureDetector)
.build();
showScrim(model, false);
int gestureCallCount = mDelegatedEventHelper.getCallCount();
int callCount = mScrimClickCallbackHelper.getCallCount();
ScrimView scrimView = mScrimCoordinator.getViewForTesting();
ThreadUtils.runOnUiThreadBlocking(
() -> scrimView.dispatchTouchEvent(
MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, 0, 0, 0)));
mDelegatedEventHelper.waitForCallback(gestureCallCount, 1);
}
@Test @Test
@SmallTest @SmallTest
@Feature({"Scrim"}) @Feature({"Scrim"})
......
...@@ -30,6 +30,7 @@ class ScrimView extends View { ...@@ -30,6 +30,7 @@ class ScrimView extends View {
/** /**
* @param context An Android {@link Context} for creating the view. * @param context An Android {@link Context} for creating the view.
* @param parent The {@link ViewGroup} the scrim should exist in. * @param parent The {@link ViewGroup} the scrim should exist in.
* @param eventDelegate A means of passing motion events back to the mediator for processing.
*/ */
public ScrimView(Context context, ViewGroup parent, @ColorInt int defaultColor, public ScrimView(Context context, ViewGroup parent, @ColorInt int defaultColor,
ScrimCoordinator.TouchEventDelegate eventDelegate) { ScrimCoordinator.TouchEventDelegate eventDelegate) {
...@@ -38,6 +39,7 @@ class ScrimView extends View { ...@@ -38,6 +39,7 @@ class ScrimView extends View {
setFocusable(false); setFocusable(false);
setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO); setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
mDefaultBackgroundColor = defaultColor; mDefaultBackgroundColor = defaultColor;
mEventDelegate = eventDelegate;
setAlpha(0.0f); setAlpha(0.0f);
setVisibility(View.GONE); setVisibility(View.GONE);
...@@ -75,7 +77,7 @@ class ScrimView extends View { ...@@ -75,7 +77,7 @@ class ScrimView extends View {
@Override @Override
public boolean onTouchEvent(MotionEvent e) { public boolean onTouchEvent(MotionEvent e) {
if (mEventDelegate != null) return mEventDelegate.onTouchEvent(e); if (mEventDelegate.onTouchEvent(e)) return true;
return super.onTouchEvent(e); return super.onTouchEvent(e);
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment