Home | History | Annotate | Download | only in cts
      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 
     20 import android.app.Activity;
     21 import android.os.SystemClock;
     22 import android.test.ActivityInstrumentationTestCase2;
     23 import android.text.Layout;
     24 import android.text.Spannable;
     25 import android.text.SpannableString;
     26 import android.text.TextPaint;
     27 import android.text.method.Touch;
     28 import android.util.DisplayMetrics;
     29 import android.view.MotionEvent;
     30 import android.widget.TextView;
     31 
     32 public class TouchTest extends ActivityInstrumentationTestCase2<StubActivity> {
     33     private Activity mActivity;
     34     private static final String LONG_TEXT = "Scrolls the specified widget to the specified " +
     35             "coordinates, except constrains the X scrolling position to the horizontal regions " +
     36             "of the text that will be visible after scrolling to the specified Y position." +
     37             "This is the description of the test.";
     38 
     39     private boolean mReturnFromTouchEvent;
     40 
     41     public TouchTest() {
     42         super("com.android.cts.stub", StubActivity.class);
     43     }
     44 
     45     @Override
     46     protected void setUp() throws Exception {
     47         super.setUp();
     48         mActivity = getActivity();
     49     }
     50 
     51     public void testScrollTo() throws Throwable {
     52         final TextView tv = new TextView(mActivity);
     53         runTestOnUiThread(new Runnable() {
     54             public void run() {
     55                 mActivity.setContentView(tv);
     56                 tv.setSingleLine(true);
     57                 tv.setLines(2);
     58             }
     59         });
     60         getInstrumentation().waitForIdleSync();
     61         TextPaint paint = tv.getPaint();
     62         final Layout layout = tv.getLayout();
     63 
     64         runTestOnUiThread(new Runnable() {
     65             public void run() {
     66                 tv.setText(LONG_TEXT);
     67             }
     68         });
     69         getInstrumentation().waitForIdleSync();
     70 
     71         // get the total length of string
     72         final int width = getTextWidth(LONG_TEXT, paint);
     73 
     74         runTestOnUiThread(new Runnable() {
     75             public void run() {
     76                 Touch.scrollTo(tv, layout, width - tv.getWidth() - 1, 0);
     77             }
     78         });
     79         getInstrumentation().waitForIdleSync();
     80         assertEquals(width - tv.getWidth() - 1, tv.getScrollX());
     81         assertEquals(0, tv.getScrollY());
     82 
     83         // the X to which scroll is greater than the total length of string.
     84         runTestOnUiThread(new Runnable() {
     85             public void run() {
     86                 Touch.scrollTo(tv, layout, width + 100, 5);
     87             }
     88         });
     89         getInstrumentation().waitForIdleSync();
     90         assertEquals(width - tv.getWidth(), tv.getScrollX(), 1.0f);
     91         assertEquals(5, tv.getScrollY());
     92 
     93         runTestOnUiThread(new Runnable() {
     94             public void run() {
     95                 Touch.scrollTo(tv, layout, width - 10, 5);
     96             }
     97         });
     98         getInstrumentation().waitForIdleSync();
     99         assertEquals(width - tv.getWidth(), tv.getScrollX(), 1.0f);
    100         assertEquals(5, tv.getScrollY());
    101     }
    102 
    103     public void testOnTouchEvent() throws Throwable {
    104         final TextView tv = new TextView(mActivity);
    105 
    106         // Create a string that is wider than the screen.
    107         DisplayMetrics metrics = mActivity.getResources().getDisplayMetrics();
    108         int screenWidth = metrics.widthPixels;
    109         TextPaint paint = tv.getPaint();
    110         String text = LONG_TEXT;
    111         int textWidth = Math.round(paint.measureText(text));
    112         while (textWidth < screenWidth) {
    113             text += LONG_TEXT;
    114             textWidth = Math.round(paint.measureText(text));
    115         }
    116 
    117         // Drag the difference between the text width and the screen width.
    118         int dragAmount = Math.min(screenWidth, textWidth - screenWidth);
    119         assertTrue(dragAmount > 0);
    120         final String finalText = text;
    121         final SpannableString spannable = new SpannableString(finalText);
    122         runTestOnUiThread(new Runnable() {
    123             public void run() {
    124                 mActivity.setContentView(tv);
    125                 tv.setSingleLine(true);
    126                 tv.setText(finalText);
    127             }
    128         });
    129         getInstrumentation().waitForIdleSync();
    130 
    131         long downTime = SystemClock.uptimeMillis();
    132         long eventTime = SystemClock.uptimeMillis();
    133         final MotionEvent event1 = MotionEvent.obtain(downTime, eventTime,
    134                 MotionEvent.ACTION_DOWN, dragAmount, 0, 0);
    135         final MotionEvent event2 = MotionEvent.obtain(downTime, eventTime,
    136                 MotionEvent.ACTION_MOVE, 0, 0, 0);
    137         final MotionEvent event3 = MotionEvent.obtain(downTime, eventTime,
    138                 MotionEvent.ACTION_UP, 0, 0, 0);
    139         assertEquals(0, tv.getScrollX());
    140         assertEquals(0, tv.getScrollY());
    141         mReturnFromTouchEvent = false;
    142         runTestOnUiThread(new Runnable() {
    143             public void run() {
    144                 mReturnFromTouchEvent = Touch.onTouchEvent(tv, spannable, event1);
    145             }
    146         });
    147         getInstrumentation().waitForIdleSync();
    148         assertTrue(mReturnFromTouchEvent);
    149         // TextView has not been scrolled.
    150         assertEquals(0, tv.getScrollX());
    151         assertEquals(0, tv.getScrollY());
    152         assertEquals(0, Touch.getInitialScrollX(tv, spannable));
    153         assertEquals(0, Touch.getInitialScrollY(tv, spannable));
    154 
    155         mReturnFromTouchEvent = false;
    156         runTestOnUiThread(new Runnable() {
    157             public void run() {
    158                 mReturnFromTouchEvent = Touch.onTouchEvent(tv, spannable, event2);
    159             }
    160         });
    161         getInstrumentation().waitForIdleSync();
    162         assertTrue(mReturnFromTouchEvent);
    163         // TextView has been scrolled.
    164         assertEquals(dragAmount, 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, event3);
    173             }
    174         });
    175         getInstrumentation().waitForIdleSync();
    176         assertTrue(mReturnFromTouchEvent);
    177         // TextView has not been scrolled.
    178         assertEquals(dragAmount, tv.getScrollX());
    179         assertEquals(0, tv.getScrollY());
    180         assertEquals(-1, Touch.getInitialScrollX(tv, spannable));
    181         assertEquals(-1, Touch.getInitialScrollY(tv, spannable));
    182     }
    183 
    184     private int getTextWidth(String str, TextPaint paint) {
    185         float totalWidth = 0f;
    186         float[] widths = new float[str.length()];
    187         paint.getTextWidths(str, widths);
    188         for (float f : widths) {
    189             totalWidth += f;
    190         }
    191         return (int) totalWidth;
    192     }
    193 }
    194