1 /* 2 * Copyright (C) 2008 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.text.method.cts; 18 19 import dalvik.annotation.TestLevel; 20 import dalvik.annotation.TestTargetClass; 21 import dalvik.annotation.TestTargetNew; 22 import dalvik.annotation.TestTargets; 23 import dalvik.annotation.ToBeFixed; 24 25 import android.app.Activity; 26 import android.os.SystemClock; 27 import android.test.ActivityInstrumentationTestCase2; 28 import android.text.Layout; 29 import android.text.Spannable; 30 import android.text.SpannableString; 31 import android.text.TextPaint; 32 import android.text.method.Touch; 33 import android.view.MotionEvent; 34 import android.widget.TextView; 35 36 @TestTargetClass(Touch.class) 37 public class TouchTest extends ActivityInstrumentationTestCase2<StubActivity> { 38 private Activity mActivity; 39 private static final String LONG_TEXT = "Scrolls the specified widget to the specified " + 40 "coordinates, except constrains the X scrolling position to the horizontal regions " + 41 "of the text that will be visible after scrolling to the specified Y position."; 42 private boolean mReturnFromTouchEvent; 43 TouchTest()44 public TouchTest() { 45 super("com.android.cts.stub", StubActivity.class); 46 } 47 48 @Override setUp()49 protected void setUp() throws Exception { 50 super.setUp(); 51 mActivity = getActivity(); 52 } 53 54 @TestTargetNew( 55 level = TestLevel.COMPLETE, 56 method = "scrollTo", 57 args = {TextView.class, Layout.class, int.class, int.class} 58 ) testScrollTo()59 public void testScrollTo() throws Throwable { 60 final TextView tv = new TextView(mActivity); 61 runTestOnUiThread(new Runnable() { 62 public void run() { 63 mActivity.setContentView(tv); 64 tv.setSingleLine(true); 65 tv.setLines(2); 66 } 67 }); 68 getInstrumentation().waitForIdleSync(); 69 TextPaint paint = tv.getPaint(); 70 final Layout layout = tv.getLayout(); 71 72 runTestOnUiThread(new Runnable() { 73 public void run() { 74 tv.setText(LONG_TEXT); 75 } 76 }); 77 getInstrumentation().waitForIdleSync(); 78 79 // get the total length of string 80 final int width = getTextWidth(LONG_TEXT, paint); 81 82 runTestOnUiThread(new Runnable() { 83 public void run() { 84 Touch.scrollTo(tv, layout, width - tv.getWidth() - 1, 0); 85 } 86 }); 87 getInstrumentation().waitForIdleSync(); 88 assertEquals(width - tv.getWidth() - 1, tv.getScrollX()); 89 assertEquals(0, tv.getScrollY()); 90 91 // the X to which scroll is greater than the total length of string. 92 runTestOnUiThread(new Runnable() { 93 public void run() { 94 Touch.scrollTo(tv, layout, width + 100, 5); 95 } 96 }); 97 getInstrumentation().waitForIdleSync(); 98 assertEquals(width - tv.getWidth(), tv.getScrollX()); 99 assertEquals(5, tv.getScrollY()); 100 101 runTestOnUiThread(new Runnable() { 102 public void run() { 103 Touch.scrollTo(tv, layout, width - 10, 5); 104 } 105 }); 106 getInstrumentation().waitForIdleSync(); 107 assertEquals(width - tv.getWidth(), tv.getScrollX()); 108 assertEquals(5, tv.getScrollY()); 109 } 110 111 @TestTargets({ 112 @TestTargetNew( 113 level = TestLevel.COMPLETE, 114 method = "getInitialScrollX", 115 args = {TextView.class, Spannable.class} 116 ), 117 @TestTargetNew( 118 level = TestLevel.COMPLETE, 119 method = "getInitialScrollY", 120 args = {TextView.class, Spannable.class} 121 ), 122 @TestTargetNew( 123 level = TestLevel.COMPLETE, 124 method = "onTouchEvent", 125 args = {TextView.class, Spannable.class, MotionEvent.class} 126 ) 127 }) 128 @ToBeFixed(bug = "1695243", explanation = "Android API javadocs are incomplete, " + 129 "should add @throws clause into javadoc.") testOnTouchEvent()130 public void testOnTouchEvent() throws Throwable { 131 final SpannableString spannable = new SpannableString(LONG_TEXT); 132 final TextView tv = new TextView(mActivity); 133 runTestOnUiThread(new Runnable() { 134 public void run() { 135 mActivity.setContentView(tv); 136 tv.setSingleLine(true); 137 tv.setText(LONG_TEXT); 138 } 139 }); 140 getInstrumentation().waitForIdleSync(); 141 142 TextPaint paint = tv.getPaint(); 143 final int width = getTextWidth(LONG_TEXT, paint); 144 long downTime = SystemClock.uptimeMillis(); 145 long eventTime = SystemClock.uptimeMillis(); 146 int x = width >> 1; 147 final MotionEvent event1 = MotionEvent.obtain(downTime, eventTime, 148 MotionEvent.ACTION_DOWN, x, 0, 0); 149 final MotionEvent event2 = MotionEvent.obtain(downTime, eventTime, 150 MotionEvent.ACTION_MOVE, 0, 0, 0); 151 final MotionEvent event3 = MotionEvent.obtain(downTime, eventTime, 152 MotionEvent.ACTION_UP, 0, 0, 0); 153 assertEquals(0, tv.getScrollX()); 154 assertEquals(0, tv.getScrollY()); 155 mReturnFromTouchEvent = false; 156 runTestOnUiThread(new Runnable() { 157 public void run() { 158 mReturnFromTouchEvent = Touch.onTouchEvent(tv, spannable, event1); 159 } 160 }); 161 getInstrumentation().waitForIdleSync(); 162 assertTrue(mReturnFromTouchEvent); 163 // TextView has not been scrolled. 164 assertEquals(0, tv.getScrollX()); 165 assertEquals(0, tv.getScrollY()); 166 assertEquals(0, Touch.getInitialScrollX(tv, spannable)); 167 assertEquals(0, Touch.getInitialScrollY(tv, spannable)); 168 169 mReturnFromTouchEvent = false; 170 runTestOnUiThread(new Runnable() { 171 public void run() { 172 mReturnFromTouchEvent = Touch.onTouchEvent(tv, spannable, event2); 173 } 174 }); 175 getInstrumentation().waitForIdleSync(); 176 assertTrue(mReturnFromTouchEvent); 177 // TextView has been scrolled. 178 assertEquals(x, tv.getScrollX()); 179 assertEquals(0, tv.getScrollY()); 180 assertEquals(0, Touch.getInitialScrollX(tv, spannable)); 181 assertEquals(0, Touch.getInitialScrollY(tv, spannable)); 182 183 mReturnFromTouchEvent = false; 184 runTestOnUiThread(new Runnable() { 185 public void run() { 186 mReturnFromTouchEvent = Touch.onTouchEvent(tv, spannable, event3); 187 } 188 }); 189 getInstrumentation().waitForIdleSync(); 190 assertTrue(mReturnFromTouchEvent); 191 // TextView has not been scrolled. 192 assertEquals(x, tv.getScrollX()); 193 assertEquals(0, tv.getScrollY()); 194 assertEquals(-1, Touch.getInitialScrollX(tv, spannable)); 195 assertEquals(-1, Touch.getInitialScrollY(tv, spannable)); 196 } 197 getTextWidth(String str, TextPaint paint)198 private int getTextWidth(String str, TextPaint paint) { 199 float totalWidth = 0f; 200 float[] widths = new float[str.length()]; 201 paint.getTextWidths(str, widths); 202 for (float f : widths) { 203 totalWidth += f; 204 } 205 return (int) totalWidth; 206 } 207 } 208