1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.support.design.widget; 18 19import static org.hamcrest.CoreMatchers.is; 20import static org.hamcrest.CoreMatchers.notNullValue; 21import static org.hamcrest.CoreMatchers.nullValue; 22import static org.hamcrest.CoreMatchers.sameInstance; 23import static org.hamcrest.MatcherAssert.assertThat; 24 25import android.support.test.InstrumentationRegistry; 26import android.support.test.espresso.Espresso; 27import android.support.test.espresso.NoMatchingViewException; 28import android.support.test.espresso.ViewAssertion; 29import android.support.test.espresso.action.ViewActions; 30import android.support.v4.view.MotionEventCompat; 31import android.test.suitebuilder.annotation.MediumTest; 32import android.view.MotionEvent; 33import android.view.View; 34import android.widget.FrameLayout; 35 36import org.junit.Before; 37import org.junit.Test; 38 39@MediumTest 40public class BottomSheetBehaviorTouchTest extends 41 BaseInstrumentationTestCase<CoordinatorLayoutActivity> { 42 43 private static final int PEEK_HEIGHT = 100; 44 45 private FrameLayout mBottomSheet; 46 47 private BottomSheetBehavior<FrameLayout> mBehavior; 48 49 private boolean mDown; 50 51 private View.OnTouchListener mOnTouchListener = new View.OnTouchListener() { 52 53 @Override 54 public boolean onTouch(View v, MotionEvent event) { 55 switch (MotionEventCompat.getActionMasked(event)) { 56 case MotionEvent.ACTION_DOWN: 57 mDown = true; 58 break; 59 } 60 return true; 61 } 62 63 }; 64 65 public BottomSheetBehaviorTouchTest() { 66 super(CoordinatorLayoutActivity.class); 67 } 68 69 @Before 70 public void setUpBottomSheet() { 71 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { 72 @Override 73 public void run() { 74 CoordinatorLayoutActivity activity = mActivityTestRule.getActivity(); 75 activity.mContainer.setOnTouchListener(mOnTouchListener); 76 mBottomSheet = new FrameLayout(activity); 77 CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams( 78 CoordinatorLayout.LayoutParams.MATCH_PARENT, 79 CoordinatorLayout.LayoutParams.MATCH_PARENT); 80 mBehavior = new BottomSheetBehavior<>(); 81 mBehavior.setPeekHeight(PEEK_HEIGHT); 82 params.setBehavior(mBehavior); 83 activity.mCoordinatorLayout.addView(mBottomSheet, params); 84 } 85 }); 86 } 87 88 @Test 89 public void testSetUp() { 90 assertThat(mBottomSheet, is(notNullValue())); 91 assertThat(mBehavior, is(sameInstance(BottomSheetBehavior.from(mBottomSheet)))); 92 } 93 94 @Test 95 public void testTouchCoordinatorLayout() { 96 final CoordinatorLayoutActivity activity = mActivityTestRule.getActivity(); 97 mDown = false; 98 Espresso.onView(sameInstance((View) activity.mCoordinatorLayout)) 99 .perform(ViewActions.click()) // Click outside the bottom sheet 100 .check(new ViewAssertion() { 101 @Override 102 public void check(View view, NoMatchingViewException e) { 103 assertThat(e, is(nullValue())); 104 assertThat(view, is(notNullValue())); 105 // Check that the touch event fell through to the container 106 assertThat(mDown, is(true)); 107 } 108 }); 109 } 110 111} 112