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 20 import android.app.Activity; 21 import android.app.Instrumentation; 22 import android.graphics.Rect; 23 import android.test.ActivityInstrumentationTestCase2; 24 import android.test.UiThreadTest; 25 import android.view.MotionEvent; 26 import android.view.TouchDelegate; 27 import android.view.View; 28 import android.view.ViewGroup; 29 import android.widget.Button; 30 import android.widget.LinearLayout; 31 32 public class TouchDelegateTest extends ActivityInstrumentationTestCase2<MockActivity> { 33 private static final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT; 34 private static final int ACTION_DOWN = MotionEvent.ACTION_DOWN; 35 36 private Activity mActivity; 37 private Instrumentation mInstrumentation; 38 private Button mButton; 39 private Rect mRect; 40 41 private int mXInside; 42 private int mYInside; 43 44 private Exception mException; 45 TouchDelegateTest()46 public TouchDelegateTest() { 47 super("com.android.cts.view", MockActivity.class); 48 } 49 50 @Override setUp()51 protected void setUp() throws Exception { 52 super.setUp(); 53 mActivity = getActivity(); 54 mInstrumentation = getInstrumentation(); 55 56 mButton = new Button(mActivity); 57 mActivity.runOnUiThread(new Runnable() { 58 public void run() { 59 try { 60 mActivity.addContentView(mButton, new LinearLayout.LayoutParams(WRAP_CONTENT, 61 WRAP_CONTENT)); 62 } catch (Exception e) { 63 mException = e; 64 } 65 } 66 }); 67 mInstrumentation.waitForIdleSync(); 68 69 if(mException != null) { 70 throw mException; 71 } 72 73 int right = mButton.getRight(); 74 int bottom = mButton.getBottom(); 75 mXInside = (mButton.getLeft() + right) / 3; 76 mYInside = (mButton.getTop() + bottom) / 3; 77 78 mRect = new Rect(); 79 mButton.getHitRect(mRect); 80 } 81 82 @UiThreadTest testOnTouchEvent()83 public void testOnTouchEvent() { 84 // test callback of onTouchEvent 85 View view = new View(mActivity); 86 MockTouchDelegate touchDelegate = new MockTouchDelegate(mRect, mButton); 87 view.setTouchDelegate(touchDelegate); 88 assertFalse(touchDelegate.mOnTouchEventCalled); 89 view.onTouchEvent(MotionEvent.obtain(0, 0, ACTION_DOWN, mXInside, mYInside, 0)); 90 assertTrue(touchDelegate.mOnTouchEventCalled); 91 } 92 93 class MockTouchDelegate extends TouchDelegate { 94 private boolean mOnTouchEventCalled; 95 MockTouchDelegate(Rect bounds, View delegateView)96 public MockTouchDelegate(Rect bounds, View delegateView) { 97 super(bounds, delegateView); 98 } 99 100 @Override onTouchEvent(MotionEvent event)101 public boolean onTouchEvent(MotionEvent event) { 102 mOnTouchEventCalled = true; 103 return true; 104 } 105 } 106 } 107