1 /* 2 * Copyright (C) 2009 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 17 package android.view.cts; 18 19 import static org.mockito.Matchers.any; 20 import static org.mockito.Matchers.anyFloat; 21 import static org.mockito.Mockito.doReturn; 22 import static org.mockito.Mockito.mock; 23 24 import android.app.Activity; 25 import android.os.Bundle; 26 import android.os.Handler; 27 import android.view.GestureDetector; 28 import android.view.MotionEvent; 29 import android.view.View; 30 import android.view.View.OnGenericMotionListener; 31 import android.view.View.OnTouchListener; 32 import android.view.ViewGroup; 33 import android.widget.Button; 34 35 public class GestureDetectorCtsActivity extends Activity { 36 private GestureDetector mGestureDetector; 37 private GestureDetector.SimpleOnGestureListener mOnGestureListener; 38 private Handler mHandler; 39 private View mView; 40 private Button mTop; 41 private Button mButton; 42 private ViewGroup mViewGroup; 43 44 @Override onCreate(Bundle savedInstanceState)45 protected void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 48 mOnGestureListener = mock(GestureDetector.SimpleOnGestureListener.class); 49 doReturn(true).when(mOnGestureListener).onDown(any(MotionEvent.class)); 50 doReturn(true).when(mOnGestureListener).onFling(any(MotionEvent.class), 51 any(MotionEvent.class), anyFloat(), anyFloat()); 52 doReturn(true).when(mOnGestureListener).onScroll(any(MotionEvent.class), 53 any(MotionEvent.class), anyFloat(), anyFloat()); 54 doReturn(true).when(mOnGestureListener).onSingleTapUp(any(MotionEvent.class)); 55 56 mHandler = new Handler(); 57 58 mGestureDetector = new GestureDetector(this, mOnGestureListener, mHandler); 59 mGestureDetector.setOnDoubleTapListener(mOnGestureListener); 60 mGestureDetector.setContextClickListener(mOnGestureListener); 61 mView = new View(this); 62 mButton = new Button(this); 63 mTop = new Button(this); 64 mView.setOnTouchListener(new MockOnTouchListener()); 65 66 mViewGroup = new ViewGroup(this) { 67 @Override 68 protected void onLayout(boolean changed, int l, int t, int r, int b) { 69 } 70 }; 71 mViewGroup.addView(mView); 72 mViewGroup.addView(mTop); 73 mViewGroup.addView(mButton); 74 mViewGroup.setOnTouchListener(new MockOnTouchListener()); 75 setContentView(mViewGroup); 76 77 } 78 getView()79 public View getView() { 80 return mView; 81 } 82 getGestureDetector()83 public GestureDetector getGestureDetector() { 84 return mGestureDetector; 85 } 86 getListener()87 public GestureDetector.SimpleOnGestureListener getListener() { 88 return mOnGestureListener; 89 } 90 91 class MockOnTouchListener implements OnTouchListener, OnGenericMotionListener { 92 onTouch(View v, MotionEvent event)93 public boolean onTouch(View v, MotionEvent event) { 94 mGestureDetector.onTouchEvent(event); 95 return true; 96 } 97 onGenericMotion(View v, MotionEvent event)98 public boolean onGenericMotion(View v, MotionEvent event) { 99 mGestureDetector.onGenericMotionEvent(event); 100 return true; 101 } 102 } 103 104 } 105