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