Fix ViewDragHelper usage in BottomSheetBehavior

BottomSheetBehavior no longer sends ACTION_MOVE to ViewDragHelper if
ACTION_DOWN has not been sent.

Bug: 29934950
Change-Id: I9bc5c53f53f98a372b7feaf1aa85b954bd0cdf23
diff --git a/design/src/android/support/design/widget/BottomSheetBehavior.java b/design/src/android/support/design/widget/BottomSheetBehavior.java
index 1337352..43f3063 100644
--- a/design/src/android/support/design/widget/BottomSheetBehavior.java
+++ b/design/src/android/support/design/widget/BottomSheetBehavior.java
@@ -253,6 +253,7 @@
     @Override
     public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
         if (!child.isShown()) {
+            mIgnoreEvents = true;
             return false;
         }
         int action = MotionEventCompat.getActionMasked(event);
diff --git a/design/tests/src/android/support/design/widget/BottomSheetBehaviorTest.java b/design/tests/src/android/support/design/widget/BottomSheetBehaviorTest.java
index 4c03fd6..1862d72 100644
--- a/design/tests/src/android/support/design/widget/BottomSheetBehaviorTest.java
+++ b/design/tests/src/android/support/design/widget/BottomSheetBehaviorTest.java
@@ -55,6 +55,7 @@
 import android.widget.TextView;
 
 import org.hamcrest.Matcher;
+import org.hamcrest.Matchers;
 import org.junit.Test;
 
 public class BottomSheetBehaviorTest extends
@@ -183,7 +184,7 @@
 
         @Override
         public Matcher<View> getConstraints() {
-            return ViewMatchers.isDisplayed();
+            return Matchers.any(View.class);
         }
 
         @Override
@@ -213,7 +214,6 @@
                         MotionEvents.sendCancel(uiController, downEvent);
                         throw new RuntimeException("Cannot drag: failed to send a move event.");
                     }
-                    BottomSheetBehavior behavior = BottomSheetBehavior.from(view);
                 }
                 int duration = ViewConfiguration.getPressedStateDuration();
                 if (duration > 0) {
@@ -460,6 +460,46 @@
 
     @Test
     @MediumTest
+    public void testInvisibleThenVisible() {
+        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
+            @Override
+            public void run() {
+                // The bottom sheet is initially invisible
+                getBottomSheet().setVisibility(View.INVISIBLE);
+                // Then it becomes visible when the CoL is touched
+                getCoordinatorLayout().setOnTouchListener(new View.OnTouchListener() {
+                    @Override
+                    public boolean onTouch(View view, MotionEvent e) {
+                        if (e.getAction() == MotionEvent.ACTION_DOWN) {
+                            getBottomSheet().setVisibility(View.VISIBLE);
+                            return true;
+                        }
+                        return false;
+                    }
+                });
+                assertThat(getBehavior().getState(), is(BottomSheetBehavior.STATE_COLLAPSED));
+            }
+        });
+        // Drag over the CoL
+        Espresso.onView(ViewMatchers.withId(R.id.coordinator))
+                // Drag (and not release)
+                .perform(new DragAction(
+                        GeneralLocation.BOTTOM_CENTER,
+                        GeneralLocation.TOP_CENTER,
+                        Press.FINGER))
+                .check(new ViewAssertion() {
+                    @Override
+                    public void check(View view, NoMatchingViewException e) {
+                        // The bottom sheet should not react to the touch events
+                        assertThat(getBottomSheet(), is(ViewMatchers.isDisplayed()));
+                        assertThat(getBehavior().getState(),
+                                is(BottomSheetBehavior.STATE_COLLAPSED));
+                    }
+                });
+    }
+
+    @Test
+    @MediumTest
     public void testNestedScroll() {
         final ViewGroup bottomSheet = getBottomSheet();
         final BottomSheetBehavior behavior = getBehavior();