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.widget.cts;
     18 
     19 import com.android.cts.widget.R;
     20 import com.android.internal.util.FastMath;
     21 
     22 import org.xmlpull.v1.XmlPullParserException;
     23 
     24 import android.app.Activity;
     25 import android.app.Instrumentation;
     26 import android.app.Instrumentation.ActivityMonitor;
     27 import android.content.Intent;
     28 import android.content.res.ColorStateList;
     29 import android.content.res.Resources.NotFoundException;
     30 import android.cts.util.PollingCheck;
     31 import android.cts.util.WidgetTestUtils;
     32 import android.graphics.Bitmap;
     33 import android.graphics.Color;
     34 import android.graphics.Paint;
     35 import android.graphics.Path;
     36 import android.graphics.Rect;
     37 import android.graphics.RectF;
     38 import android.graphics.Typeface;
     39 import android.graphics.drawable.BitmapDrawable;
     40 import android.graphics.drawable.Drawable;
     41 import android.net.Uri;
     42 import android.os.Bundle;
     43 import android.test.ActivityInstrumentationTestCase2;
     44 import android.test.TouchUtils;
     45 import android.test.UiThreadTest;
     46 import android.text.Editable;
     47 import android.text.InputFilter;
     48 import android.text.InputType;
     49 import android.text.Layout;
     50 import android.text.Selection;
     51 import android.text.Spannable;
     52 import android.text.SpannableString;
     53 import android.text.TextPaint;
     54 import android.text.TextUtils;
     55 import android.text.TextUtils.TruncateAt;
     56 import android.text.TextWatcher;
     57 import android.text.method.ArrowKeyMovementMethod;
     58 import android.text.method.DateKeyListener;
     59 import android.text.method.DateTimeKeyListener;
     60 import android.text.method.DialerKeyListener;
     61 import android.text.method.DigitsKeyListener;
     62 import android.text.method.KeyListener;
     63 import android.text.method.LinkMovementMethod;
     64 import android.text.method.MovementMethod;
     65 import android.text.method.PasswordTransformationMethod;
     66 import android.text.method.QwertyKeyListener;
     67 import android.text.method.SingleLineTransformationMethod;
     68 import android.text.method.TextKeyListener;
     69 import android.text.method.TextKeyListener.Capitalize;
     70 import android.text.method.TimeKeyListener;
     71 import android.text.method.TransformationMethod;
     72 import android.text.style.URLSpan;
     73 import android.text.util.Linkify;
     74 import android.util.DisplayMetrics;
     75 import android.util.TypedValue;
     76 import android.view.ContextMenu;
     77 import android.view.ContextMenu.ContextMenuInfo;
     78 import android.view.Gravity;
     79 import android.view.KeyEvent;
     80 import android.view.View;
     81 import android.view.View.OnCreateContextMenuListener;
     82 import android.view.View.OnLongClickListener;
     83 import android.view.ViewGroup;
     84 import android.view.inputmethod.BaseInputConnection;
     85 import android.view.inputmethod.EditorInfo;
     86 import android.view.inputmethod.ExtractedText;
     87 import android.view.inputmethod.ExtractedTextRequest;
     88 import android.widget.EditText;
     89 import android.widget.FrameLayout;
     90 import android.widget.LinearLayout;
     91 import android.widget.Scroller;
     92 import android.widget.TextView;
     93 import android.widget.TextView.BufferType;
     94 import android.widget.TextView.OnEditorActionListener;
     95 
     96 import java.io.IOException;
     97 
     98 /**
     99  * Test {@link TextView}.
    100  */
    101 public class TextViewTest extends ActivityInstrumentationTestCase2<TextViewCtsActivity> {
    102 
    103     private TextView mTextView;
    104     private Activity mActivity;
    105     private Instrumentation mInstrumentation;
    106     private static final String LONG_TEXT = "This is a really long string which exceeds "
    107             + "the width of the view. New devices have a much larger screen which "
    108             + "actually enables long strings to be displayed with no fading. "
    109             + "I have made this string longer to fix this case. If you are correcting "
    110             + "this text, I would love to see the kind of devices you guys now use!";
    111     private static final long TIMEOUT = 5000;
    112     private CharSequence mTransformedText;
    113 
    114     public TextViewTest() {
    115         super("com.android.cts.widget", TextViewCtsActivity.class);
    116     }
    117 
    118     @Override
    119     protected void setUp() throws Exception {
    120         super.setUp();
    121         mActivity = getActivity();
    122         new PollingCheck() {
    123             @Override
    124                 protected boolean check() {
    125                 return mActivity.hasWindowFocus();
    126             }
    127         }.run();
    128         mInstrumentation = getInstrumentation();
    129     }
    130 
    131     public void testConstructor() {
    132         new TextView(mActivity);
    133 
    134         new TextView(mActivity, null);
    135 
    136         new TextView(mActivity, null, 0);
    137     }
    138 
    139     @UiThreadTest
    140     public void testAccessText() {
    141         TextView tv = findTextView(R.id.textview_text);
    142 
    143         String expected = mActivity.getResources().getString(R.string.text_view_hello);
    144         tv.setText(expected);
    145         assertEquals(expected, tv.getText().toString());
    146 
    147         tv.setText(null);
    148         assertEquals("", tv.getText().toString());
    149     }
    150 
    151     public void testGetLineHeight() {
    152         mTextView = new TextView(mActivity);
    153         assertTrue(mTextView.getLineHeight() > 0);
    154 
    155         mTextView.setLineSpacing(1.2f, 1.5f);
    156         assertTrue(mTextView.getLineHeight() > 0);
    157     }
    158 
    159     public void testGetLayout() {
    160         mActivity.runOnUiThread(new Runnable() {
    161             public void run() {
    162                 mTextView = findTextView(R.id.textview_text);
    163                 mTextView.setGravity(Gravity.CENTER);
    164             }
    165         });
    166         mInstrumentation.waitForIdleSync();
    167         assertNotNull(mTextView.getLayout());
    168 
    169         TestLayoutRunnable runnable = new TestLayoutRunnable(mTextView) {
    170             public void run() {
    171                 // change the text of TextView.
    172                 mTextView.setText("Hello, Android!");
    173                 saveLayout();
    174             }
    175         };
    176         mActivity.runOnUiThread(runnable);
    177         mInstrumentation.waitForIdleSync();
    178         assertNull(runnable.getLayout());
    179         assertNotNull(mTextView.getLayout());
    180     }
    181 
    182     public void testAccessKeyListener() {
    183         mActivity.runOnUiThread(new Runnable() {
    184             public void run() {
    185                 mTextView = findTextView(R.id.textview_text);
    186             }
    187         });
    188         mInstrumentation.waitForIdleSync();
    189 
    190         assertNull(mTextView.getKeyListener());
    191 
    192         final KeyListener digitsKeyListener = DigitsKeyListener.getInstance();
    193 
    194         mActivity.runOnUiThread(new Runnable() {
    195             public void run() {
    196                 mTextView.setKeyListener(digitsKeyListener);
    197             }
    198         });
    199         mInstrumentation.waitForIdleSync();
    200         assertSame(digitsKeyListener, mTextView.getKeyListener());
    201 
    202         final QwertyKeyListener qwertyKeyListener
    203                 = QwertyKeyListener.getInstance(false, Capitalize.NONE);
    204         mActivity.runOnUiThread(new Runnable() {
    205             public void run() {
    206                 mTextView.setKeyListener(qwertyKeyListener);
    207             }
    208         });
    209         mInstrumentation.waitForIdleSync();
    210         assertSame(qwertyKeyListener, mTextView.getKeyListener());
    211     }
    212 
    213     public void testAccessMovementMethod() {
    214         final CharSequence LONG_TEXT = "Scrolls the specified widget to the specified "
    215                 + "coordinates, except constrains the X scrolling position to the horizontal "
    216                 + "regions of the text that will be visible after scrolling to "
    217                 + "the specified Y position.";
    218         final int selectionStart = 10;
    219         final int selectionEnd = LONG_TEXT.length();
    220         final MovementMethod movementMethod = ArrowKeyMovementMethod.getInstance();
    221         mActivity.runOnUiThread(new Runnable() {
    222             public void run() {
    223                 mTextView = findTextView(R.id.textview_text);
    224                 mTextView.setMovementMethod(movementMethod);
    225                 mTextView.setText(LONG_TEXT, BufferType.EDITABLE);
    226                 Selection.setSelection((Editable) mTextView.getText(),
    227                         selectionStart, selectionEnd);
    228                 mTextView.requestFocus();
    229             }
    230         });
    231         mInstrumentation.waitForIdleSync();
    232 
    233         assertSame(movementMethod, mTextView.getMovementMethod());
    234         assertEquals(selectionStart, Selection.getSelectionStart(mTextView.getText()));
    235         assertEquals(selectionEnd, Selection.getSelectionEnd(mTextView.getText()));
    236         sendKeys(KeyEvent.KEYCODE_SHIFT_LEFT, KeyEvent.KEYCODE_ALT_LEFT,
    237                 KeyEvent.KEYCODE_DPAD_UP);
    238         // the selection has been removed.
    239         assertEquals(selectionStart, Selection.getSelectionStart(mTextView.getText()));
    240         assertEquals(selectionStart, Selection.getSelectionEnd(mTextView.getText()));
    241 
    242         mActivity.runOnUiThread(new Runnable() {
    243             public void run() {
    244                 mTextView.setMovementMethod(null);
    245                 Selection.setSelection((Editable) mTextView.getText(),
    246                         selectionStart, selectionEnd);
    247                 mTextView.requestFocus();
    248             }
    249         });
    250         mInstrumentation.waitForIdleSync();
    251 
    252         assertNull(mTextView.getMovementMethod());
    253         assertEquals(selectionStart, Selection.getSelectionStart(mTextView.getText()));
    254         assertEquals(selectionEnd, Selection.getSelectionEnd(mTextView.getText()));
    255         sendKeys(KeyEvent.KEYCODE_SHIFT_LEFT, KeyEvent.KEYCODE_ALT_LEFT,
    256                 KeyEvent.KEYCODE_DPAD_UP);
    257         // the selection will not be changed.
    258         assertEquals(selectionStart, Selection.getSelectionStart(mTextView.getText()));
    259         assertEquals(selectionEnd, Selection.getSelectionEnd(mTextView.getText()));
    260     }
    261 
    262     @UiThreadTest
    263     public void testLength() {
    264         mTextView = findTextView(R.id.textview_text);
    265 
    266         String content = "This is content";
    267         mTextView.setText(content);
    268         assertEquals(content.length(), mTextView.length());
    269 
    270         mTextView.setText("");
    271         assertEquals(0, mTextView.length());
    272 
    273         mTextView.setText(null);
    274         assertEquals(0, mTextView.length());
    275     }
    276 
    277     @UiThreadTest
    278     public void testAccessGravity() {
    279         mActivity.setContentView(R.layout.textview_gravity);
    280 
    281         mTextView = findTextView(R.id.gravity_default);
    282         assertEquals(Gravity.TOP | Gravity.START, mTextView.getGravity());
    283 
    284         mTextView = findTextView(R.id.gravity_bottom);
    285         assertEquals(Gravity.BOTTOM | Gravity.START, mTextView.getGravity());
    286 
    287         mTextView = findTextView(R.id.gravity_right);
    288         assertEquals(Gravity.TOP | Gravity.RIGHT, mTextView.getGravity());
    289 
    290         mTextView = findTextView(R.id.gravity_center);
    291         assertEquals(Gravity.CENTER, mTextView.getGravity());
    292 
    293         mTextView = findTextView(R.id.gravity_fill);
    294         assertEquals(Gravity.FILL, mTextView.getGravity());
    295 
    296         mTextView = findTextView(R.id.gravity_center_vertical_right);
    297         assertEquals(Gravity.CENTER_VERTICAL | Gravity.RIGHT, mTextView.getGravity());
    298 
    299         mTextView.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
    300         assertEquals(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, mTextView.getGravity());
    301         mTextView.setGravity(Gravity.FILL);
    302         assertEquals(Gravity.FILL, mTextView.getGravity());
    303         mTextView.setGravity(Gravity.CENTER);
    304         assertEquals(Gravity.CENTER, mTextView.getGravity());
    305 
    306         mTextView.setGravity(Gravity.NO_GRAVITY);
    307         assertEquals(Gravity.TOP | Gravity.START, mTextView.getGravity());
    308 
    309         mTextView.setGravity(Gravity.RIGHT);
    310         assertEquals(Gravity.TOP | Gravity.RIGHT, mTextView.getGravity());
    311 
    312         mTextView.setGravity(Gravity.FILL_VERTICAL);
    313         assertEquals(Gravity.FILL_VERTICAL | Gravity.START, mTextView.getGravity());
    314 
    315         //test negative input value.
    316         mTextView.setGravity(-1);
    317         assertEquals(-1, mTextView.getGravity());
    318     }
    319 
    320     public void testAccessAutoLinkMask() {
    321         mTextView = findTextView(R.id.textview_text);
    322         final CharSequence text1 =
    323                 new SpannableString("URL: http://www.google.com. mailto: account (at) gmail.com");
    324         mActivity.runOnUiThread(new Runnable() {
    325             public void run() {
    326                 mTextView.setAutoLinkMask(Linkify.ALL);
    327                 mTextView.setText(text1, BufferType.EDITABLE);
    328             }
    329         });
    330         mInstrumentation.waitForIdleSync();
    331         assertEquals(Linkify.ALL, mTextView.getAutoLinkMask());
    332 
    333         Spannable spanString = (Spannable) mTextView.getText();
    334         URLSpan[] spans = spanString.getSpans(0, spanString.length(), URLSpan.class);
    335         assertNotNull(spans);
    336         assertEquals(2, spans.length);
    337         assertEquals("http://www.google.com", spans[0].getURL());
    338         assertEquals("mailto:account (at) gmail.com", spans[1].getURL());
    339 
    340         final CharSequence text2 =
    341             new SpannableString("name: Jack. tel: +41 44 800 8999");
    342         mActivity.runOnUiThread(new Runnable() {
    343             public void run() {
    344                 mTextView.setAutoLinkMask(Linkify.PHONE_NUMBERS);
    345                 mTextView.setText(text2, BufferType.EDITABLE);
    346             }
    347         });
    348         mInstrumentation.waitForIdleSync();
    349         assertEquals(Linkify.PHONE_NUMBERS, mTextView.getAutoLinkMask());
    350 
    351         spanString = (Spannable) mTextView.getText();
    352         spans = spanString.getSpans(0, spanString.length(), URLSpan.class);
    353         assertNotNull(spans);
    354         assertEquals(1, spans.length);
    355         assertEquals("tel:+41448008999", spans[0].getURL());
    356 
    357         layout(R.layout.textview_autolink);
    358         // 1 for web, 2 for email, 4 for phone, 7 for all(web|email|phone)
    359         assertEquals(0, getAutoLinkMask(R.id.autolink_default));
    360         assertEquals(Linkify.WEB_URLS, getAutoLinkMask(R.id.autolink_web));
    361         assertEquals(Linkify.EMAIL_ADDRESSES, getAutoLinkMask(R.id.autolink_email));
    362         assertEquals(Linkify.PHONE_NUMBERS, getAutoLinkMask(R.id.autolink_phone));
    363         assertEquals(Linkify.ALL, getAutoLinkMask(R.id.autolink_all));
    364         assertEquals(Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES,
    365                 getAutoLinkMask(R.id.autolink_compound1));
    366         assertEquals(Linkify.WEB_URLS | Linkify.PHONE_NUMBERS,
    367                 getAutoLinkMask(R.id.autolink_compound2));
    368         assertEquals(Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS,
    369                 getAutoLinkMask(R.id.autolink_compound3));
    370         assertEquals(Linkify.PHONE_NUMBERS | Linkify.ALL,
    371                 getAutoLinkMask(R.id.autolink_compound4));
    372     }
    373 
    374     public void testAccessTextSize() {
    375         DisplayMetrics metrics = mActivity.getResources().getDisplayMetrics();
    376 
    377         mTextView = new TextView(mActivity);
    378         mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 20f);
    379         assertEquals(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 20f, metrics),
    380                 mTextView.getTextSize(), 0.01f);
    381 
    382         mTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20f);
    383         assertEquals(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20f, metrics),
    384                 mTextView.getTextSize(), 0.01f);
    385 
    386         mTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f);
    387         assertEquals(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20f, metrics),
    388                 mTextView.getTextSize(), 0.01f);
    389 
    390         // setTextSize by default unit "sp"
    391         mTextView.setTextSize(20f);
    392         assertEquals(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20f, metrics),
    393                 mTextView.getTextSize(), 0.01f);
    394 
    395         mTextView.setTextSize(200f);
    396         assertEquals(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 200f, metrics),
    397                 mTextView.getTextSize(), 0.01f);
    398     }
    399 
    400     public void testAccessTextColor() {
    401         mTextView = new TextView(mActivity);
    402 
    403         mTextView.setTextColor(Color.GREEN);
    404         assertEquals(Color.GREEN, mTextView.getCurrentTextColor());
    405         assertSame(ColorStateList.valueOf(Color.GREEN), mTextView.getTextColors());
    406 
    407         mTextView.setTextColor(Color.BLACK);
    408         assertEquals(Color.BLACK, mTextView.getCurrentTextColor());
    409         assertSame(ColorStateList.valueOf(Color.BLACK), mTextView.getTextColors());
    410 
    411         mTextView.setTextColor(Color.RED);
    412         assertEquals(Color.RED, mTextView.getCurrentTextColor());
    413         assertSame(ColorStateList.valueOf(Color.RED), mTextView.getTextColors());
    414 
    415         // using ColorStateList
    416         // normal
    417         ColorStateList colors = new ColorStateList(new int[][] {
    418                 new int[] { android.R.attr.state_focused}, new int[0] },
    419                 new int[] { Color.rgb(0, 255, 0), Color.BLACK });
    420         mTextView.setTextColor(colors);
    421         assertSame(colors, mTextView.getTextColors());
    422         assertEquals(Color.BLACK, mTextView.getCurrentTextColor());
    423 
    424         // exceptional
    425         try {
    426             mTextView.setTextColor(null);
    427             fail("Should thrown exception if the colors is null");
    428         } catch (NullPointerException e){
    429         }
    430     }
    431 
    432     public void testGetTextColor() {
    433         // TODO: How to get a suitable TypedArray to test this method.
    434 
    435         try {
    436             TextView.getTextColor(mActivity, null, -1);
    437             fail("There should be a NullPointerException thrown out.");
    438         } catch (NullPointerException e) {
    439         }
    440     }
    441 
    442     public void testSetHighlightColor() {
    443         mTextView = new TextView(mActivity);
    444 
    445         mTextView.setHighlightColor(0x00ff00ff);
    446     }
    447 
    448     public void testSetShadowLayer() {
    449         MockTextView textView = new MockTextView(mActivity);
    450 
    451         // shadow is placed to the left and below the text
    452         textView.setShadowLayer(1.0f, 0.3f, 0.3f, Color.CYAN);
    453         assertTrue(textView.isPaddingOffsetRequired());
    454         assertEquals(0, textView.getLeftPaddingOffset());
    455         assertEquals(0, textView.getTopPaddingOffset());
    456         assertEquals(1, textView.getRightPaddingOffset());
    457         assertEquals(1, textView.getBottomPaddingOffset());
    458 
    459         // shadow is placed to the right and above the text
    460         textView.setShadowLayer(1.0f, -0.8f, -0.8f, Color.CYAN);
    461         assertTrue(textView.isPaddingOffsetRequired());
    462         assertEquals(-1, textView.getLeftPaddingOffset());
    463         assertEquals(-1, textView.getTopPaddingOffset());
    464         assertEquals(0, textView.getRightPaddingOffset());
    465         assertEquals(0, textView.getBottomPaddingOffset());
    466 
    467         // no shadow
    468         textView.setShadowLayer(0.0f, 0.0f, 0.0f, Color.CYAN);
    469         assertFalse(textView.isPaddingOffsetRequired());
    470         assertEquals(0, textView.getLeftPaddingOffset());
    471         assertEquals(0, textView.getTopPaddingOffset());
    472         assertEquals(0, textView.getRightPaddingOffset());
    473         assertEquals(0, textView.getBottomPaddingOffset());
    474     }
    475 
    476     @UiThreadTest
    477     public void testSetSelectAllOnFocus() {
    478         mActivity.setContentView(R.layout.textview_selectallonfocus);
    479         String content = "This is the content";
    480         String blank = "";
    481         mTextView = findTextView(R.id.selectAllOnFocus_default);
    482         mTextView.setText(blank, BufferType.SPANNABLE);
    483         // change the focus
    484         findTextView(R.id.selectAllOnFocus_dummy).requestFocus();
    485         assertFalse(mTextView.isFocused());
    486         mTextView.requestFocus();
    487         assertTrue(mTextView.isFocused());
    488 
    489         assertEquals(-1, mTextView.getSelectionStart());
    490         assertEquals(-1, mTextView.getSelectionEnd());
    491 
    492         mTextView.setText(content, BufferType.SPANNABLE);
    493         mTextView.setSelectAllOnFocus(true);
    494         // change the focus
    495         findTextView(R.id.selectAllOnFocus_dummy).requestFocus();
    496         assertFalse(mTextView.isFocused());
    497         mTextView.requestFocus();
    498         assertTrue(mTextView.isFocused());
    499 
    500         assertEquals(0, mTextView.getSelectionStart());
    501         assertEquals(content.length(), mTextView.getSelectionEnd());
    502 
    503         Selection.setSelection((Spannable) mTextView.getText(), 0);
    504         mTextView.setSelectAllOnFocus(false);
    505         // change the focus
    506         findTextView(R.id.selectAllOnFocus_dummy).requestFocus();
    507         assertFalse(mTextView.isFocused());
    508         mTextView.requestFocus();
    509         assertTrue(mTextView.isFocused());
    510 
    511         assertEquals(0, mTextView.getSelectionStart());
    512         assertEquals(0, mTextView.getSelectionEnd());
    513 
    514         mTextView.setText(blank, BufferType.SPANNABLE);
    515         mTextView.setSelectAllOnFocus(true);
    516         // change the focus
    517         findTextView(R.id.selectAllOnFocus_dummy).requestFocus();
    518         assertFalse(mTextView.isFocused());
    519         mTextView.requestFocus();
    520         assertTrue(mTextView.isFocused());
    521 
    522         assertEquals(0, mTextView.getSelectionStart());
    523         assertEquals(blank.length(), mTextView.getSelectionEnd());
    524 
    525         Selection.setSelection((Spannable) mTextView.getText(), 0);
    526         mTextView.setSelectAllOnFocus(false);
    527         // change the focus
    528         findTextView(R.id.selectAllOnFocus_dummy).requestFocus();
    529         assertFalse(mTextView.isFocused());
    530         mTextView.requestFocus();
    531         assertTrue(mTextView.isFocused());
    532 
    533         assertEquals(0, mTextView.getSelectionStart());
    534         assertEquals(0, mTextView.getSelectionEnd());
    535     }
    536 
    537     public void testGetPaint() {
    538         mTextView = new TextView(mActivity);
    539         TextPaint tp = mTextView.getPaint();
    540         assertNotNull(tp);
    541 
    542         assertEquals(mTextView.getPaintFlags(), tp.getFlags());
    543     }
    544 
    545     @UiThreadTest
    546     public void testAccessLinksClickable() {
    547         mActivity.setContentView(R.layout.textview_hint_linksclickable_freezestext);
    548 
    549         mTextView = findTextView(R.id.hint_linksClickable_freezesText_default);
    550         assertTrue(mTextView.getLinksClickable());
    551 
    552         mTextView = findTextView(R.id.linksClickable_true);
    553         assertTrue(mTextView.getLinksClickable());
    554 
    555         mTextView = findTextView(R.id.linksClickable_false);
    556         assertFalse(mTextView.getLinksClickable());
    557 
    558         mTextView.setLinksClickable(false);
    559         assertFalse(mTextView.getLinksClickable());
    560 
    561         mTextView.setLinksClickable(true);
    562         assertTrue(mTextView.getLinksClickable());
    563 
    564         assertNull(mTextView.getMovementMethod());
    565 
    566         final CharSequence text = new SpannableString("name: Jack. tel: +41 44 800 8999");
    567 
    568         mTextView.setAutoLinkMask(Linkify.PHONE_NUMBERS);
    569         mTextView.setText(text, BufferType.EDITABLE);
    570 
    571         // Movement method will be automatically set to LinkMovementMethod
    572         assertTrue(mTextView.getMovementMethod() instanceof LinkMovementMethod);
    573     }
    574 
    575     public void testAccessHintTextColor() {
    576         mTextView = new TextView(mActivity);
    577         // using int values
    578         // normal
    579         mTextView.setHintTextColor(Color.GREEN);
    580         assertEquals(Color.GREEN, mTextView.getCurrentHintTextColor());
    581         assertSame(ColorStateList.valueOf(Color.GREEN), mTextView.getHintTextColors());
    582 
    583         mTextView.setHintTextColor(Color.BLUE);
    584         assertSame(ColorStateList.valueOf(Color.BLUE), mTextView.getHintTextColors());
    585         assertEquals(Color.BLUE, mTextView.getCurrentHintTextColor());
    586 
    587         mTextView.setHintTextColor(Color.RED);
    588         assertSame(ColorStateList.valueOf(Color.RED), mTextView.getHintTextColors());
    589         assertEquals(Color.RED, mTextView.getCurrentHintTextColor());
    590 
    591         // using ColorStateList
    592         // normal
    593         ColorStateList colors = new ColorStateList(new int[][] {
    594                 new int[] { android.R.attr.state_focused}, new int[0] },
    595                 new int[] { Color.rgb(0, 255, 0), Color.BLACK });
    596         mTextView.setHintTextColor(colors);
    597         assertSame(colors, mTextView.getHintTextColors());
    598         assertEquals(Color.BLACK, mTextView.getCurrentHintTextColor());
    599 
    600         // exceptional
    601         mTextView.setHintTextColor(null);
    602         assertNull(mTextView.getHintTextColors());
    603         assertEquals(mTextView.getCurrentTextColor(), mTextView.getCurrentHintTextColor());
    604     }
    605 
    606     public void testAccessLinkTextColor() {
    607         mTextView = new TextView(mActivity);
    608         // normal
    609         mTextView.setLinkTextColor(Color.GRAY);
    610         assertSame(ColorStateList.valueOf(Color.GRAY), mTextView.getLinkTextColors());
    611         assertEquals(Color.GRAY, mTextView.getPaint().linkColor);
    612 
    613         mTextView.setLinkTextColor(Color.YELLOW);
    614         assertSame(ColorStateList.valueOf(Color.YELLOW), mTextView.getLinkTextColors());
    615         assertEquals(Color.YELLOW, mTextView.getPaint().linkColor);
    616 
    617         mTextView.setLinkTextColor(Color.WHITE);
    618         assertSame(ColorStateList.valueOf(Color.WHITE), mTextView.getLinkTextColors());
    619         assertEquals(Color.WHITE, mTextView.getPaint().linkColor);
    620 
    621         ColorStateList colors = new ColorStateList(new int[][] {
    622                 new int[] { android.R.attr.state_expanded}, new int[0] },
    623                 new int[] { Color.rgb(0, 255, 0), Color.BLACK });
    624         mTextView.setLinkTextColor(colors);
    625         assertSame(colors, mTextView.getLinkTextColors());
    626         assertEquals(Color.BLACK, mTextView.getPaint().linkColor);
    627 
    628         mTextView.setLinkTextColor(null);
    629         assertNull(mTextView.getLinkTextColors());
    630         assertEquals(Color.BLACK, mTextView.getPaint().linkColor);
    631     }
    632 
    633     public void testAccessPaintFlags() {
    634         mTextView = new TextView(mActivity);
    635         assertEquals(Paint.DEV_KERN_TEXT_FLAG | Paint.EMBEDDED_BITMAP_TEXT_FLAG
    636                 | Paint.ANTI_ALIAS_FLAG, mTextView.getPaintFlags());
    637 
    638         mTextView.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG | Paint.FAKE_BOLD_TEXT_FLAG);
    639         assertEquals(Paint.UNDERLINE_TEXT_FLAG | Paint.FAKE_BOLD_TEXT_FLAG,
    640                 mTextView.getPaintFlags());
    641 
    642         mTextView.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.LINEAR_TEXT_FLAG);
    643         assertEquals(Paint.STRIKE_THRU_TEXT_FLAG | Paint.LINEAR_TEXT_FLAG,
    644                 mTextView.getPaintFlags());
    645     }
    646 
    647     public void testHeightAndWidth() {
    648         mTextView = findTextView(R.id.textview_text);
    649         int originalWidth = mTextView.getWidth();
    650         setWidth(mTextView.getWidth() >> 3);
    651         int originalHeight = mTextView.getHeight();
    652 
    653         setMaxHeight(originalHeight + 1);
    654         assertEquals(originalHeight, mTextView.getHeight());
    655 
    656         setMaxHeight(originalHeight - 1);
    657         assertEquals(originalHeight - 1, mTextView.getHeight());
    658 
    659         setMaxHeight(-1);
    660         assertEquals(0, mTextView.getHeight());
    661 
    662         setMaxHeight(Integer.MAX_VALUE);
    663         assertEquals(originalHeight, mTextView.getHeight());
    664 
    665         setMinHeight(originalHeight + 1);
    666         assertEquals(originalHeight + 1, mTextView.getHeight());
    667 
    668         setMinHeight(originalHeight - 1);
    669         assertEquals(originalHeight, mTextView.getHeight());
    670 
    671         setMinHeight(-1);
    672         assertEquals(originalHeight, mTextView.getHeight());
    673 
    674         setMinHeight(0);
    675         setMaxHeight(Integer.MAX_VALUE);
    676 
    677         setHeight(originalHeight + 1);
    678         assertEquals(originalHeight + 1, mTextView.getHeight());
    679 
    680         setHeight(originalHeight - 1);
    681         assertEquals(originalHeight - 1, mTextView.getHeight());
    682 
    683         setHeight(-1);
    684         assertEquals(0, mTextView.getHeight());
    685 
    686         setHeight(originalHeight);
    687         assertEquals(originalHeight, mTextView.getHeight());
    688 
    689         assertEquals(originalWidth >> 3, mTextView.getWidth());
    690 
    691         // Min Width
    692         setMinWidth(originalWidth + 1);
    693         assertEquals(1, mTextView.getLineCount());
    694         assertEquals(originalWidth + 1, mTextView.getWidth());
    695 
    696         setMinWidth(originalWidth - 1);
    697         assertEquals(2, mTextView.getLineCount());
    698         assertEquals(originalWidth - 1, mTextView.getWidth());
    699 
    700         // Width
    701         setWidth(originalWidth + 1);
    702         assertEquals(1, mTextView.getLineCount());
    703         assertEquals(originalWidth + 1, mTextView.getWidth());
    704 
    705         setWidth(originalWidth - 1);
    706         assertEquals(2, mTextView.getLineCount());
    707         assertEquals(originalWidth - 1, mTextView.getWidth());
    708     }
    709 
    710     public void testSetMinEms() {
    711         mTextView = findTextView(R.id.textview_text);
    712         assertEquals(1, mTextView.getLineCount());
    713 
    714         int originalWidth = mTextView.getWidth();
    715         int originalEms = originalWidth / mTextView.getLineHeight();
    716 
    717         setMinEms(originalEms + 1);
    718         assertEquals((originalEms + 1) * mTextView.getLineHeight(), mTextView.getWidth());
    719 
    720         setMinEms(originalEms - 1);
    721         assertEquals(originalWidth, mTextView.getWidth());
    722     }
    723 
    724     public void testSetMaxEms() {
    725         mTextView = findTextView(R.id.textview_text);
    726         assertEquals(1, mTextView.getLineCount());
    727         int originalWidth = mTextView.getWidth();
    728         int originalEms = originalWidth / mTextView.getLineHeight();
    729 
    730         setMaxEms(originalEms + 1);
    731         assertEquals(1, mTextView.getLineCount());
    732         assertEquals(originalWidth, mTextView.getWidth());
    733 
    734         setMaxEms(originalEms - 1);
    735         assertTrue(1 < mTextView.getLineCount());
    736         assertEquals((originalEms - 1) * mTextView.getLineHeight(),
    737                 mTextView.getWidth());
    738     }
    739 
    740     public void testSetEms() {
    741         mTextView = findTextView(R.id.textview_text);
    742         assertEquals("check height", 1, mTextView.getLineCount());
    743         int originalWidth = mTextView.getWidth();
    744         int originalEms = originalWidth / mTextView.getLineHeight();
    745 
    746         setEms(originalEms + 1);
    747         assertEquals(1, mTextView.getLineCount());
    748         assertEquals((originalEms + 1) * mTextView.getLineHeight(),
    749                 mTextView.getWidth());
    750 
    751         setEms(originalEms - 1);
    752         assertTrue((1 < mTextView.getLineCount()));
    753         assertEquals((originalEms - 1) * mTextView.getLineHeight(),
    754                 mTextView.getWidth());
    755     }
    756 
    757     public void testSetLineSpacing() {
    758         mTextView = new TextView(mActivity);
    759         int originalLineHeight = mTextView.getLineHeight();
    760 
    761         // normal
    762         float add = 1.2f;
    763         float mult = 1.4f;
    764         setLineSpacing(add, mult);
    765         assertEquals(FastMath.round(originalLineHeight * mult + add), mTextView.getLineHeight());
    766         add = 0.0f;
    767         mult = 1.4f;
    768         setLineSpacing(add, mult);
    769         assertEquals(FastMath.round(originalLineHeight * mult + add), mTextView.getLineHeight());
    770 
    771         // abnormal
    772         add = -1.2f;
    773         mult = 1.4f;
    774         setLineSpacing(add, mult);
    775         assertEquals(FastMath.round(originalLineHeight * mult + add), mTextView.getLineHeight());
    776         add = -1.2f;
    777         mult = -1.4f;
    778         setLineSpacing(add, mult);
    779         assertEquals(FastMath.round(originalLineHeight * mult + add), mTextView.getLineHeight());
    780         add = 1.2f;
    781         mult = 0.0f;
    782         setLineSpacing(add, mult);
    783         assertEquals(FastMath.round(originalLineHeight * mult + add), mTextView.getLineHeight());
    784 
    785         // edge
    786         add = Float.MIN_VALUE;
    787         mult = Float.MIN_VALUE;
    788         setLineSpacing(add, mult);
    789         float expected = originalLineHeight * mult + add;
    790         assertEquals(FastMath.round(expected), mTextView.getLineHeight());
    791         add = Float.MAX_VALUE;
    792         mult = Float.MAX_VALUE;
    793         setLineSpacing(add, mult);
    794         expected = originalLineHeight * mult + add;
    795         assertEquals(FastMath.round(expected), mTextView.getLineHeight());
    796     }
    797 
    798     public void testInstanceState() {
    799         // Do not test. Implementation details.
    800     }
    801 
    802     public void testAccessFreezesText() throws Throwable {
    803         layout(R.layout.textview_hint_linksclickable_freezestext);
    804 
    805         mTextView = findTextView(R.id.hint_linksClickable_freezesText_default);
    806         assertFalse(mTextView.getFreezesText());
    807 
    808         mTextView = findTextView(R.id.freezesText_true);
    809         assertTrue(mTextView.getFreezesText());
    810 
    811         mTextView = findTextView(R.id.freezesText_false);
    812         assertFalse(mTextView.getFreezesText());
    813 
    814         mTextView.setFreezesText(false);
    815         assertFalse(mTextView.getFreezesText());
    816 
    817         final CharSequence text = "Hello, TextView.";
    818         mActivity.runOnUiThread(new Runnable() {
    819             public void run() {
    820                 mTextView.setText(text);
    821             }
    822         });
    823         mInstrumentation.waitForIdleSync();
    824 
    825         final URLSpan urlSpan = new URLSpan("ctstest://TextView/test");
    826         // TODO: How to simulate the TextView in frozen icicles.
    827         Instrumentation instrumentation = getInstrumentation();
    828         ActivityMonitor am = instrumentation.addMonitor(MockURLSpanTestActivity.class.getName(),
    829                 null, false);
    830 
    831         mActivity.runOnUiThread(new Runnable() {
    832             public void run() {
    833                 Uri uri = Uri.parse(urlSpan.getURL());
    834                 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    835                 mActivity.startActivity(intent);
    836             }
    837         });
    838 
    839         Activity newActivity = am.waitForActivityWithTimeout(TIMEOUT);
    840         assertNotNull(newActivity);
    841         newActivity.finish();
    842         instrumentation.removeMonitor(am);
    843         // the text of TextView is removed.
    844         mTextView = findTextView(R.id.freezesText_false);
    845 
    846         assertEquals(text.toString(), mTextView.getText().toString());
    847 
    848         mTextView.setFreezesText(true);
    849         assertTrue(mTextView.getFreezesText());
    850 
    851         mActivity.runOnUiThread(new Runnable() {
    852             public void run() {
    853                 mTextView.setText(text);
    854             }
    855         });
    856         mInstrumentation.waitForIdleSync();
    857         // TODO: How to simulate the TextView in frozen icicles.
    858         am = instrumentation.addMonitor(MockURLSpanTestActivity.class.getName(),
    859                 null, false);
    860 
    861         mActivity.runOnUiThread(new Runnable() {
    862             public void run() {
    863                 Uri uri = Uri.parse(urlSpan.getURL());
    864                 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    865                 mActivity.startActivity(intent);
    866             }
    867         });
    868 
    869         Activity oldActivity = newActivity;
    870         while (true) {
    871             newActivity = am.waitForActivityWithTimeout(TIMEOUT);
    872             assertNotNull(newActivity);
    873             if (newActivity != oldActivity) {
    874                 break;
    875             }
    876         }
    877         newActivity.finish();
    878         instrumentation.removeMonitor(am);
    879         // the text of TextView is still there.
    880         mTextView = findTextView(R.id.freezesText_false);
    881         assertEquals(text.toString(), mTextView.getText().toString());
    882     }
    883 
    884     public void testSetEditableFactory() {
    885         mTextView = new TextView(mActivity);
    886         String text = "sample";
    887         MockEditableFactory factory = new MockEditableFactory();
    888         mTextView.setEditableFactory(factory);
    889 
    890         factory.reset();
    891         mTextView.setText(text);
    892         assertFalse(factory.hasCalledNewEditable());
    893 
    894         factory.reset();
    895         mTextView.setText(text, BufferType.SPANNABLE);
    896         assertFalse(factory.hasCalledNewEditable());
    897 
    898         factory.reset();
    899         mTextView.setText(text, BufferType.NORMAL);
    900         assertFalse(factory.hasCalledNewEditable());
    901 
    902         factory.reset();
    903         mTextView.setText(text, BufferType.EDITABLE);
    904         assertTrue(factory.hasCalledNewEditable());
    905         assertEquals(text, factory.getSource());
    906 
    907         mTextView.setKeyListener(DigitsKeyListener.getInstance());
    908         factory.reset();
    909         mTextView.setText(text, BufferType.EDITABLE);
    910         assertTrue(factory.hasCalledNewEditable());
    911         assertEquals(text, factory.getSource());
    912 
    913         try {
    914             mTextView.setEditableFactory(null);
    915             fail("The factory can not set to null!");
    916         } catch (NullPointerException e) {
    917         }
    918     }
    919 
    920     public void testSetSpannableFactory() {
    921         mTextView = new TextView(mActivity);
    922         String text = "sample";
    923         MockSpannableFactory factory = new MockSpannableFactory();
    924         mTextView.setSpannableFactory(factory);
    925 
    926         factory.reset();
    927         mTextView.setText(text);
    928         assertFalse(factory.hasCalledNewSpannable());
    929 
    930         factory.reset();
    931         mTextView.setText(text, BufferType.EDITABLE);
    932         assertFalse(factory.hasCalledNewSpannable());
    933 
    934         factory.reset();
    935         mTextView.setText(text, BufferType.NORMAL);
    936         assertFalse(factory.hasCalledNewSpannable());
    937 
    938         factory.reset();
    939         mTextView.setText(text, BufferType.SPANNABLE);
    940         assertTrue(factory.hasCalledNewSpannable());
    941         assertEquals(text, factory.getSource());
    942 
    943         mTextView.setMovementMethod(LinkMovementMethod.getInstance());
    944         factory.reset();
    945         mTextView.setText(text, BufferType.NORMAL);
    946         assertTrue(factory.hasCalledNewSpannable());
    947         assertEquals(text, factory.getSource());
    948 
    949         try {
    950             mTextView.setSpannableFactory(null);
    951             fail("The factory can not set to null!");
    952         } catch (NullPointerException e) {
    953         }
    954     }
    955 
    956     public void testTextChangedListener() {
    957         mTextView = new TextView(mActivity);
    958         MockTextWatcher watcher0 = new MockTextWatcher();
    959         MockTextWatcher watcher1 = new MockTextWatcher();
    960 
    961         mTextView.addTextChangedListener(watcher0);
    962         mTextView.addTextChangedListener(watcher1);
    963 
    964         watcher0.reset();
    965         watcher1.reset();
    966         mTextView.setText("Changed");
    967         assertTrue(watcher0.hasCalledBeforeTextChanged());
    968         assertTrue(watcher0.hasCalledOnTextChanged());
    969         assertTrue(watcher0.hasCalledAfterTextChanged());
    970         assertTrue(watcher1.hasCalledBeforeTextChanged());
    971         assertTrue(watcher1.hasCalledOnTextChanged());
    972         assertTrue(watcher1.hasCalledAfterTextChanged());
    973 
    974         watcher0.reset();
    975         watcher1.reset();
    976         // BeforeTextChanged and OnTextChanged are called though the strings are same
    977         mTextView.setText("Changed");
    978         assertTrue(watcher0.hasCalledBeforeTextChanged());
    979         assertTrue(watcher0.hasCalledOnTextChanged());
    980         assertTrue(watcher0.hasCalledAfterTextChanged());
    981         assertTrue(watcher1.hasCalledBeforeTextChanged());
    982         assertTrue(watcher1.hasCalledOnTextChanged());
    983         assertTrue(watcher1.hasCalledAfterTextChanged());
    984 
    985         watcher0.reset();
    986         watcher1.reset();
    987         // BeforeTextChanged and OnTextChanged are called twice (The text is not
    988         // Editable, so in Append() it calls setText() first)
    989         mTextView.append("and appended");
    990         assertTrue(watcher0.hasCalledBeforeTextChanged());
    991         assertTrue(watcher0.hasCalledOnTextChanged());
    992         assertTrue(watcher0.hasCalledAfterTextChanged());
    993         assertTrue(watcher1.hasCalledBeforeTextChanged());
    994         assertTrue(watcher1.hasCalledOnTextChanged());
    995         assertTrue(watcher1.hasCalledAfterTextChanged());
    996 
    997         watcher0.reset();
    998         watcher1.reset();
    999         // Methods are not called if the string does not change
   1000         mTextView.append("");
   1001         assertFalse(watcher0.hasCalledBeforeTextChanged());
   1002         assertFalse(watcher0.hasCalledOnTextChanged());
   1003         assertFalse(watcher0.hasCalledAfterTextChanged());
   1004         assertFalse(watcher1.hasCalledBeforeTextChanged());
   1005         assertFalse(watcher1.hasCalledOnTextChanged());
   1006         assertFalse(watcher1.hasCalledAfterTextChanged());
   1007 
   1008         watcher0.reset();
   1009         watcher1.reset();
   1010         mTextView.removeTextChangedListener(watcher1);
   1011         mTextView.setText(null);
   1012         assertTrue(watcher0.hasCalledBeforeTextChanged());
   1013         assertTrue(watcher0.hasCalledOnTextChanged());
   1014         assertTrue(watcher0.hasCalledAfterTextChanged());
   1015         assertFalse(watcher1.hasCalledBeforeTextChanged());
   1016         assertFalse(watcher1.hasCalledOnTextChanged());
   1017         assertFalse(watcher1.hasCalledAfterTextChanged());
   1018     }
   1019 
   1020     public void testSetTextKeepState1() {
   1021         mTextView = new TextView(mActivity);
   1022 
   1023         String longString = "very long content";
   1024         String shortString = "short";
   1025 
   1026         // selection is at the exact place which is inside the short string
   1027         mTextView.setText(longString, BufferType.SPANNABLE);
   1028         Selection.setSelection((Spannable) mTextView.getText(), 3);
   1029         mTextView.setTextKeepState(shortString);
   1030         assertEquals(shortString, mTextView.getText().toString());
   1031         assertEquals(3, mTextView.getSelectionStart());
   1032         assertEquals(3, mTextView.getSelectionEnd());
   1033 
   1034         // selection is at the exact place which is outside the short string
   1035         mTextView.setText(longString);
   1036         Selection.setSelection((Spannable) mTextView.getText(), shortString.length() + 1);
   1037         mTextView.setTextKeepState(shortString);
   1038         assertEquals(shortString, mTextView.getText().toString());
   1039         assertEquals(shortString.length(), mTextView.getSelectionStart());
   1040         assertEquals(shortString.length(), mTextView.getSelectionEnd());
   1041 
   1042         // select the sub string which is inside the short string
   1043         mTextView.setText(longString);
   1044         Selection.setSelection((Spannable) mTextView.getText(), 1, 4);
   1045         mTextView.setTextKeepState(shortString);
   1046         assertEquals(shortString, mTextView.getText().toString());
   1047         assertEquals(1, mTextView.getSelectionStart());
   1048         assertEquals(4, mTextView.getSelectionEnd());
   1049 
   1050         // select the sub string which ends outside the short string
   1051         mTextView.setText(longString);
   1052         Selection.setSelection((Spannable) mTextView.getText(), 2, shortString.length() + 1);
   1053         mTextView.setTextKeepState(shortString);
   1054         assertEquals(shortString, mTextView.getText().toString());
   1055         assertEquals(2, mTextView.getSelectionStart());
   1056         assertEquals(shortString.length(), mTextView.getSelectionEnd());
   1057 
   1058         // select the sub string which is outside the short string
   1059         mTextView.setText(longString);
   1060         Selection.setSelection((Spannable) mTextView.getText(),
   1061                 shortString.length() + 1, shortString.length() + 3);
   1062         mTextView.setTextKeepState(shortString);
   1063         assertEquals(shortString, mTextView.getText().toString());
   1064         assertEquals(shortString.length(), mTextView.getSelectionStart());
   1065         assertEquals(shortString.length(), mTextView.getSelectionEnd());
   1066     }
   1067 
   1068     @UiThreadTest
   1069     public void testGetEditableText() {
   1070         TextView tv = findTextView(R.id.textview_text);
   1071 
   1072         String text = "Hello";
   1073         tv.setText(text, BufferType.EDITABLE);
   1074         assertEquals(text, tv.getText().toString());
   1075         assertTrue(tv.getText() instanceof Editable);
   1076         assertEquals(text, tv.getEditableText().toString());
   1077 
   1078         tv.setText(text, BufferType.SPANNABLE);
   1079         assertEquals(text, tv.getText().toString());
   1080         assertTrue(tv.getText() instanceof Spannable);
   1081         assertNull(tv.getEditableText());
   1082 
   1083         tv.setText(null, BufferType.EDITABLE);
   1084         assertEquals("", tv.getText().toString());
   1085         assertTrue(tv.getText() instanceof Editable);
   1086         assertEquals("", tv.getEditableText().toString());
   1087 
   1088         tv.setText(null, BufferType.SPANNABLE);
   1089         assertEquals("", tv.getText().toString());
   1090         assertTrue(tv.getText() instanceof Spannable);
   1091         assertNull(tv.getEditableText());
   1092     }
   1093 
   1094     @UiThreadTest
   1095     public void testSetText2() {
   1096         String string = "This is a test for setting text content by char array";
   1097         char[] input = string.toCharArray();
   1098         TextView tv = findTextView(R.id.textview_text);
   1099 
   1100         tv.setText(input, 0, input.length);
   1101         assertEquals(string, tv.getText().toString());
   1102 
   1103         tv.setText(input, 0, 5);
   1104         assertEquals(string.substring(0, 5), tv.getText().toString());
   1105 
   1106         try {
   1107             tv.setText(input, -1, input.length);
   1108             fail("Should throw exception if the start position is negative!");
   1109         } catch (IndexOutOfBoundsException exception) {
   1110         }
   1111 
   1112         try {
   1113             tv.setText(input, 0, -1);
   1114             fail("Should throw exception if the length is negative!");
   1115         } catch (IndexOutOfBoundsException exception) {
   1116         }
   1117 
   1118         try {
   1119             tv.setText(input, 1, input.length);
   1120             fail("Should throw exception if the end position is out of index!");
   1121         } catch (IndexOutOfBoundsException exception) {
   1122         }
   1123 
   1124         tv.setText(input, 1, 0);
   1125         assertEquals("", tv.getText().toString());
   1126     }
   1127 
   1128     @UiThreadTest
   1129     public void testSetText1() {
   1130         mTextView = findTextView(R.id.textview_text);
   1131 
   1132         String longString = "very long content";
   1133         String shortString = "short";
   1134 
   1135         // selection is at the exact place which is inside the short string
   1136         mTextView.setText(longString, BufferType.SPANNABLE);
   1137         Selection.setSelection((Spannable) mTextView.getText(), 3);
   1138         mTextView.setTextKeepState(shortString, BufferType.EDITABLE);
   1139         assertTrue(mTextView.getText() instanceof Editable);
   1140         assertEquals(shortString, mTextView.getText().toString());
   1141         assertEquals(shortString, mTextView.getEditableText().toString());
   1142         assertEquals(3, mTextView.getSelectionStart());
   1143         assertEquals(3, mTextView.getSelectionEnd());
   1144 
   1145         mTextView.setText(shortString, BufferType.EDITABLE);
   1146         assertTrue(mTextView.getText() instanceof Editable);
   1147         assertEquals(shortString, mTextView.getText().toString());
   1148         assertEquals(shortString, mTextView.getEditableText().toString());
   1149         // there is no selection.
   1150         assertEquals(-1, mTextView.getSelectionStart());
   1151         assertEquals(-1, mTextView.getSelectionEnd());
   1152 
   1153         // selection is at the exact place which is outside the short string
   1154         mTextView.setText(longString);
   1155         Selection.setSelection((Spannable) mTextView.getText(), longString.length());
   1156         mTextView.setTextKeepState(shortString, BufferType.EDITABLE);
   1157         assertTrue(mTextView.getText() instanceof Editable);
   1158         assertEquals(shortString, mTextView.getText().toString());
   1159         assertEquals(shortString, mTextView.getEditableText().toString());
   1160         assertEquals(shortString.length(), mTextView.getSelectionStart());
   1161         assertEquals(shortString.length(), mTextView.getSelectionEnd());
   1162 
   1163         mTextView.setText(shortString, BufferType.EDITABLE);
   1164         assertTrue(mTextView.getText() instanceof Editable);
   1165         assertEquals(shortString, mTextView.getText().toString());
   1166         assertEquals(shortString, mTextView.getEditableText().toString());
   1167         // there is no selection.
   1168         assertEquals(-1, mTextView.getSelectionStart());
   1169         assertEquals(-1, mTextView.getSelectionEnd());
   1170 
   1171         // select the sub string which is inside the short string
   1172         mTextView.setText(longString);
   1173         Selection.setSelection((Spannable) mTextView.getText(), 1, shortString.length() - 1);
   1174         mTextView.setTextKeepState(shortString, BufferType.EDITABLE);
   1175         assertTrue(mTextView.getText() instanceof Editable);
   1176         assertEquals(shortString, mTextView.getText().toString());
   1177         assertEquals(shortString, mTextView.getEditableText().toString());
   1178         assertEquals(1, mTextView.getSelectionStart());
   1179         assertEquals(shortString.length() - 1, mTextView.getSelectionEnd());
   1180 
   1181         mTextView.setText(shortString, BufferType.EDITABLE);
   1182         assertTrue(mTextView.getText() instanceof Editable);
   1183         assertEquals(shortString, mTextView.getText().toString());
   1184         assertEquals(shortString, mTextView.getEditableText().toString());
   1185         // there is no selection.
   1186         assertEquals(-1, mTextView.getSelectionStart());
   1187         assertEquals(-1, mTextView.getSelectionEnd());
   1188 
   1189         // select the sub string which ends outside the short string
   1190         mTextView.setText(longString);
   1191         Selection.setSelection((Spannable) mTextView.getText(), 2, longString.length());
   1192         mTextView.setTextKeepState(shortString, BufferType.EDITABLE);
   1193         assertTrue(mTextView.getText() instanceof Editable);
   1194         assertEquals(shortString, mTextView.getText().toString());
   1195         assertEquals(shortString, mTextView.getEditableText().toString());
   1196         assertEquals(2, mTextView.getSelectionStart());
   1197         assertEquals(shortString.length(), mTextView.getSelectionEnd());
   1198 
   1199         mTextView.setText(shortString, BufferType.EDITABLE);
   1200         assertTrue(mTextView.getText() instanceof Editable);
   1201         assertEquals(shortString, mTextView.getText().toString());
   1202         assertEquals(shortString, mTextView.getEditableText().toString());
   1203         // there is no selection.
   1204         assertEquals(-1, mTextView.getSelectionStart());
   1205         assertEquals(-1, mTextView.getSelectionEnd());
   1206 
   1207         // select the sub string which is outside the short string
   1208         mTextView.setText(longString);
   1209         Selection.setSelection((Spannable) mTextView.getText(),
   1210                 shortString.length() + 1, shortString.length() + 3);
   1211         mTextView.setTextKeepState(shortString, BufferType.EDITABLE);
   1212         assertTrue(mTextView.getText() instanceof Editable);
   1213         assertEquals(shortString, mTextView.getText().toString());
   1214         assertEquals(shortString, mTextView.getEditableText().toString());
   1215         assertEquals(shortString.length(), mTextView.getSelectionStart());
   1216         assertEquals(shortString.length(), mTextView.getSelectionEnd());
   1217 
   1218         mTextView.setText(shortString, BufferType.EDITABLE);
   1219         assertTrue(mTextView.getText() instanceof Editable);
   1220         assertEquals(shortString, mTextView.getText().toString());
   1221         assertEquals(shortString, mTextView.getEditableText().toString());
   1222         // there is no selection.
   1223         assertEquals(-1, mTextView.getSelectionStart());
   1224         assertEquals(-1, mTextView.getSelectionEnd());
   1225     }
   1226 
   1227     @UiThreadTest
   1228     public void testSetText3() {
   1229         TextView tv = findTextView(R.id.textview_text);
   1230 
   1231         int resId = R.string.text_view_hint;
   1232         String result = mActivity.getResources().getString(resId);
   1233 
   1234         tv.setText(resId);
   1235         assertEquals(result, tv.getText().toString());
   1236 
   1237         try {
   1238             tv.setText(-1);
   1239             fail("Should throw exception with illegal id");
   1240         } catch (NotFoundException e) {
   1241         }
   1242     }
   1243 
   1244     @UiThreadTest
   1245     public void testSetText() {
   1246         TextView tv = findTextView(R.id.textview_text);
   1247 
   1248         int resId = R.string.text_view_hint;
   1249         String result = mActivity.getResources().getString(resId);
   1250 
   1251         tv.setText(resId, BufferType.EDITABLE);
   1252         assertEquals(result, tv.getText().toString());
   1253         assertTrue(tv.getText() instanceof Editable);
   1254 
   1255         tv.setText(resId, BufferType.SPANNABLE);
   1256         assertEquals(result, tv.getText().toString());
   1257         assertTrue(tv.getText() instanceof Spannable);
   1258 
   1259         try {
   1260             tv.setText(-1, BufferType.EDITABLE);
   1261             fail("Should throw exception with illegal id");
   1262         } catch (NotFoundException e) {
   1263         }
   1264     }
   1265 
   1266     @UiThreadTest
   1267     public void testAccessHint() {
   1268         mActivity.setContentView(R.layout.textview_hint_linksclickable_freezestext);
   1269 
   1270         mTextView = findTextView(R.id.hint_linksClickable_freezesText_default);
   1271         assertNull(mTextView.getHint());
   1272 
   1273         mTextView = findTextView(R.id.hint_blank);
   1274         assertEquals("", mTextView.getHint());
   1275 
   1276         mTextView = findTextView(R.id.hint_string);
   1277         assertEquals(mActivity.getResources().getString(R.string.text_view_simple_hint),
   1278                 mTextView.getHint());
   1279 
   1280         mTextView = findTextView(R.id.hint_resid);
   1281         assertEquals(mActivity.getResources().getString(R.string.text_view_hint),
   1282                 mTextView.getHint());
   1283 
   1284         mTextView.setHint("This is hint");
   1285         assertEquals("This is hint", mTextView.getHint().toString());
   1286 
   1287         mTextView.setHint(R.string.text_view_hello);
   1288         assertEquals(mActivity.getResources().getString(R.string.text_view_hello),
   1289                 mTextView.getHint().toString());
   1290 
   1291         // Non-exist resid
   1292         try {
   1293             mTextView.setHint(-1);
   1294             fail("Should throw exception if id is illegal");
   1295         } catch (NotFoundException e) {
   1296         }
   1297     }
   1298 
   1299     public void testAccessError() {
   1300         mTextView = findTextView(R.id.textview_text);
   1301         assertNull(mTextView.getError());
   1302 
   1303         final String errorText = "Oops! There is an error";
   1304 
   1305         mActivity.runOnUiThread(new Runnable() {
   1306             public void run() {
   1307                 mTextView.setError(null);
   1308             }
   1309         });
   1310         mInstrumentation.waitForIdleSync();
   1311         assertNull(mTextView.getError());
   1312 
   1313         final Drawable icon = getDrawable(R.drawable.failed);
   1314         mActivity.runOnUiThread(new Runnable() {
   1315             public void run() {
   1316                 mTextView.setError(errorText, icon);
   1317             }
   1318         });
   1319         mInstrumentation.waitForIdleSync();
   1320         assertEquals(errorText, mTextView.getError().toString());
   1321         // can not check whether the drawable is set correctly
   1322 
   1323         mActivity.runOnUiThread(new Runnable() {
   1324             public void run() {
   1325                 mTextView.setError(null, null);
   1326             }
   1327         });
   1328         mInstrumentation.waitForIdleSync();
   1329         assertNull(mTextView.getError());
   1330 
   1331         mActivity.runOnUiThread(new Runnable() {
   1332             public void run() {
   1333                 mTextView.setKeyListener(DigitsKeyListener.getInstance(""));
   1334                 mTextView.setText("", BufferType.EDITABLE);
   1335                 mTextView.setError(errorText);
   1336                 mTextView.requestFocus();
   1337             }
   1338         });
   1339         mInstrumentation.waitForIdleSync();
   1340 
   1341         assertEquals(errorText, mTextView.getError().toString());
   1342 
   1343         mInstrumentation.sendStringSync("a");
   1344         // a key event that will not change the TextView's text
   1345         assertEquals("", mTextView.getText().toString());
   1346         // The icon and error message will not be reset to null
   1347         assertEquals(errorText, mTextView.getError().toString());
   1348 
   1349         mActivity.runOnUiThread(new Runnable() {
   1350             public void run() {
   1351                 mTextView.setKeyListener(DigitsKeyListener.getInstance());
   1352                 mTextView.setText("", BufferType.EDITABLE);
   1353                 mTextView.setError(errorText);
   1354                 mTextView.requestFocus();
   1355             }
   1356         });
   1357         mInstrumentation.waitForIdleSync();
   1358 
   1359         mInstrumentation.sendStringSync("1");
   1360         // a key event cause changes to the TextView's text
   1361         assertEquals("1", mTextView.getText().toString());
   1362         // the error message and icon will be cleared.
   1363         assertNull(mTextView.getError());
   1364     }
   1365 
   1366     public void testAccessFilters() {
   1367         final InputFilter[] expected = { new InputFilter.AllCaps(),
   1368                 new InputFilter.LengthFilter(2) };
   1369 
   1370         final QwertyKeyListener qwertyKeyListener
   1371                 = QwertyKeyListener.getInstance(false, Capitalize.NONE);
   1372         mActivity.runOnUiThread(new Runnable() {
   1373             public void run() {
   1374                 mTextView = findTextView(R.id.textview_text);
   1375                 mTextView.setKeyListener(qwertyKeyListener);
   1376                 mTextView.setText("", BufferType.EDITABLE);
   1377                 mTextView.setFilters(expected);
   1378                 mTextView.requestFocus();
   1379             }
   1380         });
   1381         mInstrumentation.waitForIdleSync();
   1382 
   1383         assertSame(expected, mTextView.getFilters());
   1384 
   1385         mInstrumentation.sendStringSync("a");
   1386         // the text is capitalized by InputFilter.AllCaps
   1387         assertEquals("A", mTextView.getText().toString());
   1388         mInstrumentation.sendStringSync("b");
   1389         // the text is capitalized by InputFilter.AllCaps
   1390         assertEquals("AB", mTextView.getText().toString());
   1391         mInstrumentation.sendStringSync("c");
   1392         // 'C' could not be accepted, because there is a length filter.
   1393         assertEquals("AB", mTextView.getText().toString());
   1394 
   1395         try {
   1396             mTextView.setFilters(null);
   1397             fail("Should throw IllegalArgumentException!");
   1398         } catch (IllegalArgumentException e) {
   1399         }
   1400     }
   1401 
   1402     public void testGetFocusedRect() {
   1403         Rect rc = new Rect();
   1404 
   1405         // Basic
   1406         mTextView = new TextView(mActivity);
   1407         mTextView.getFocusedRect(rc);
   1408         assertEquals(mTextView.getScrollX(), rc.left);
   1409         assertEquals(mTextView.getScrollX() + mTextView.getWidth(), rc.right);
   1410         assertEquals(mTextView.getScrollY(), rc.top);
   1411         assertEquals(mTextView.getScrollY() + mTextView.getHeight(), rc.bottom);
   1412 
   1413         // Single line
   1414         mTextView = findTextView(R.id.textview_text);
   1415         mTextView.getFocusedRect(rc);
   1416         assertEquals(mTextView.getScrollX(), rc.left);
   1417         assertEquals(mTextView.getScrollX() + mTextView.getWidth(), rc.right);
   1418         assertEquals(mTextView.getScrollY(), rc.top);
   1419         assertEquals(mTextView.getScrollY() + mTextView.getHeight(), rc.bottom);
   1420 
   1421         mActivity.runOnUiThread(new Runnable() {
   1422             public void run() {
   1423                 mTextView.setSelected(true);
   1424                 SpannableString text = new SpannableString(mTextView.getText());
   1425                 Selection.setSelection(text, 3, 13);
   1426                 mTextView.setText(text);
   1427             }
   1428         });
   1429         mInstrumentation.waitForIdleSync();
   1430         mTextView.getFocusedRect(rc);
   1431         assertNotNull(mTextView.getLayout());
   1432         /* Cursor coordinates from getPrimaryHorizontal() may have a fractional
   1433          * component, while the result of getFocusedRect is in int coordinates.
   1434          * It's not practical for these to match exactly, so we compare that the
   1435          * integer components match - there can be a fractional pixel
   1436          * discrepancy, which should be okay for all practical applications. */
   1437         assertEquals((int) mTextView.getLayout().getPrimaryHorizontal(3), rc.left);
   1438         assertEquals((int) mTextView.getLayout().getPrimaryHorizontal(13), rc.right);
   1439         assertEquals(mTextView.getLayout().getLineTop(0), rc.top);
   1440         assertEquals(mTextView.getLayout().getLineBottom(0), rc.bottom);
   1441 
   1442         mActivity.runOnUiThread(new Runnable() {
   1443             public void run() {
   1444                 mTextView.setSelected(true);
   1445                 SpannableString text = new SpannableString(mTextView.getText());
   1446                 Selection.setSelection(text, 13, 3);
   1447                 mTextView.setText(text);
   1448             }
   1449         });
   1450         mInstrumentation.waitForIdleSync();
   1451         mTextView.getFocusedRect(rc);
   1452         assertNotNull(mTextView.getLayout());
   1453         assertEquals((int) mTextView.getLayout().getPrimaryHorizontal(3) - 2, rc.left);
   1454         assertEquals((int) mTextView.getLayout().getPrimaryHorizontal(3) + 2, rc.right);
   1455         assertEquals(mTextView.getLayout().getLineTop(0), rc.top);
   1456         assertEquals(mTextView.getLayout().getLineBottom(0), rc.bottom);
   1457 
   1458         // Multi lines
   1459         mTextView = findTextView(R.id.textview_text_two_lines);
   1460         mTextView.getFocusedRect(rc);
   1461         assertEquals(mTextView.getScrollX(), rc.left);
   1462         assertEquals(mTextView.getScrollX() + mTextView.getWidth(), rc.right);
   1463         assertEquals(mTextView.getScrollY(), rc.top);
   1464         assertEquals(mTextView.getScrollY() + mTextView.getHeight(), rc.bottom);
   1465 
   1466         mActivity.runOnUiThread(new Runnable() {
   1467             public void run() {
   1468                 mTextView.setSelected(true);
   1469                 SpannableString text = new SpannableString(mTextView.getText());
   1470                 Selection.setSelection(text, 2, 4);
   1471                 mTextView.setText(text);
   1472             }
   1473         });
   1474         mInstrumentation.waitForIdleSync();
   1475         mTextView.getFocusedRect(rc);
   1476         assertNotNull(mTextView.getLayout());
   1477         assertEquals((int) mTextView.getLayout().getPrimaryHorizontal(2), rc.left);
   1478         assertEquals((int) mTextView.getLayout().getPrimaryHorizontal(4), rc.right);
   1479         assertEquals(mTextView.getLayout().getLineTop(0), rc.top);
   1480         assertEquals(mTextView.getLayout().getLineBottom(0), rc.bottom);
   1481 
   1482         mActivity.runOnUiThread(new Runnable() {
   1483             public void run() {
   1484                 mTextView.setSelected(true);
   1485                 SpannableString text = new SpannableString(mTextView.getText());
   1486                 Selection.setSelection(text, 2, 10); // cross the "\n" and two lines
   1487                 mTextView.setText(text);
   1488             }
   1489         });
   1490         mInstrumentation.waitForIdleSync();
   1491         mTextView.getFocusedRect(rc);
   1492         Path path = new Path();
   1493         mTextView.getLayout().getSelectionPath(2, 10, path);
   1494         RectF rcf = new RectF();
   1495         path.computeBounds(rcf, true);
   1496         assertNotNull(mTextView.getLayout());
   1497         assertEquals(rcf.left - 1, (float) rc.left);
   1498         assertEquals(rcf.right + 1, (float) rc.right);
   1499         assertEquals(mTextView.getLayout().getLineTop(0), rc.top);
   1500         assertEquals(mTextView.getLayout().getLineBottom(1), rc.bottom);
   1501 
   1502         // Exception
   1503         try {
   1504             mTextView.getFocusedRect(null);
   1505             fail("Should throw NullPointerException!");
   1506         } catch (NullPointerException e) {
   1507         }
   1508     }
   1509 
   1510     public void testGetLineCount() {
   1511         mTextView = findTextView(R.id.textview_text);
   1512         // this is an one line text with default setting.
   1513         assertEquals(1, mTextView.getLineCount());
   1514 
   1515         // make it multi-lines
   1516         setMaxWidth(mTextView.getWidth() / 3);
   1517         assertTrue(1 < mTextView.getLineCount());
   1518 
   1519         // make it to an one line
   1520         setMaxWidth(Integer.MAX_VALUE);
   1521         assertEquals(1, mTextView.getLineCount());
   1522 
   1523         // set min lines don't effect the lines count for actual text.
   1524         setMinLines(12);
   1525         assertEquals(1, mTextView.getLineCount());
   1526 
   1527         mTextView = new TextView(mActivity);
   1528         // the internal Layout has not been built.
   1529         assertNull(mTextView.getLayout());
   1530         assertEquals(0, mTextView.getLineCount());
   1531     }
   1532 
   1533     public void testGetLineBounds() {
   1534         Rect rc = new Rect();
   1535         mTextView = new TextView(mActivity);
   1536         assertEquals(0, mTextView.getLineBounds(0, null));
   1537 
   1538         assertEquals(0, mTextView.getLineBounds(0, rc));
   1539         assertEquals(0, rc.left);
   1540         assertEquals(0, rc.right);
   1541         assertEquals(0, rc.top);
   1542         assertEquals(0, rc.bottom);
   1543 
   1544         mTextView = findTextView(R.id.textview_text);
   1545         assertEquals(mTextView.getBaseline(), mTextView.getLineBounds(0, null));
   1546 
   1547         assertEquals(mTextView.getBaseline(), mTextView.getLineBounds(0, rc));
   1548         assertEquals(0, rc.left);
   1549         assertEquals(mTextView.getWidth(), rc.right);
   1550         assertEquals(0, rc.top);
   1551         assertEquals(mTextView.getHeight(), rc.bottom);
   1552 
   1553         mActivity.runOnUiThread(new Runnable() {
   1554             public void run() {
   1555                 mTextView.setPadding(1, 2, 3, 4);
   1556                 mTextView.setGravity(Gravity.BOTTOM);
   1557             }
   1558         });
   1559         mInstrumentation.waitForIdleSync();
   1560         assertEquals(mTextView.getBaseline(), mTextView.getLineBounds(0, rc));
   1561         assertEquals(mTextView.getTotalPaddingLeft(), rc.left);
   1562         assertEquals(mTextView.getWidth() - mTextView.getTotalPaddingRight(), rc.right);
   1563         assertEquals(mTextView.getTotalPaddingTop(), rc.top);
   1564         assertEquals(mTextView.getHeight() - mTextView.getTotalPaddingBottom(), rc.bottom);
   1565     }
   1566 
   1567     public void testGetBaseLine() {
   1568         mTextView = new TextView(mActivity);
   1569         assertEquals(-1, mTextView.getBaseline());
   1570 
   1571         mTextView = findTextView(R.id.textview_text);
   1572         assertEquals(mTextView.getLayout().getLineBaseline(0), mTextView.getBaseline());
   1573 
   1574         mActivity.runOnUiThread(new Runnable() {
   1575             public void run() {
   1576                 mTextView.setPadding(1, 2, 3, 4);
   1577                 mTextView.setGravity(Gravity.BOTTOM);
   1578             }
   1579         });
   1580         mInstrumentation.waitForIdleSync();
   1581         int expected = mTextView.getTotalPaddingTop() + mTextView.getLayout().getLineBaseline(0);
   1582         assertEquals(expected, mTextView.getBaseline());
   1583     }
   1584 
   1585     public void testPressKey() {
   1586         final QwertyKeyListener qwertyKeyListener
   1587                 = QwertyKeyListener.getInstance(false, Capitalize.NONE);
   1588         mActivity.runOnUiThread(new Runnable() {
   1589             public void run() {
   1590                 mTextView = findTextView(R.id.textview_text);
   1591                 mTextView.setKeyListener(qwertyKeyListener);
   1592                 mTextView.setText("", BufferType.EDITABLE);
   1593                 mTextView.requestFocus();
   1594             }
   1595         });
   1596         mInstrumentation.waitForIdleSync();
   1597 
   1598         mInstrumentation.sendStringSync("a");
   1599         assertEquals("a", mTextView.getText().toString());
   1600         mInstrumentation.sendStringSync("b");
   1601         assertEquals("ab", mTextView.getText().toString());
   1602         sendKeys(KeyEvent.KEYCODE_DEL);
   1603         assertEquals("a", mTextView.getText().toString());
   1604     }
   1605 
   1606     public void testSetIncludeFontPadding() {
   1607         mTextView = findTextView(R.id.textview_text);
   1608         mActivity.runOnUiThread(new Runnable() {
   1609             public void run() {
   1610                 mTextView.setWidth(mTextView.getWidth() / 3);
   1611                 mTextView.setPadding(1, 2, 3, 4);
   1612                 mTextView.setGravity(Gravity.BOTTOM);
   1613             }
   1614         });
   1615         mInstrumentation.waitForIdleSync();
   1616 
   1617         int oldHeight = mTextView.getHeight();
   1618         mActivity.runOnUiThread(new Runnable() {
   1619             public void run() {
   1620                 mTextView.setIncludeFontPadding(false);
   1621             }
   1622         });
   1623         mInstrumentation.waitForIdleSync();
   1624 
   1625         assertTrue(mTextView.getHeight() < oldHeight);
   1626     }
   1627 
   1628     public void testScroll() {
   1629         mTextView = new TextView(mActivity);
   1630 
   1631         assertEquals(0, mTextView.getScrollX());
   1632         assertEquals(0, mTextView.getScrollY());
   1633 
   1634         //don't set the Scroller, nothing changed.
   1635         mTextView.computeScroll();
   1636         assertEquals(0, mTextView.getScrollX());
   1637         assertEquals(0, mTextView.getScrollY());
   1638 
   1639         //set the Scroller
   1640         Scroller s = new Scroller(mActivity);
   1641         assertNotNull(s);
   1642         s.startScroll(0, 0, 320, 480, 0);
   1643         s.abortAnimation();
   1644         s.forceFinished(false);
   1645         mTextView.setScroller(s);
   1646 
   1647         mTextView.computeScroll();
   1648         assertEquals(320, mTextView.getScrollX());
   1649         assertEquals(480, mTextView.getScrollY());
   1650     }
   1651 
   1652     public void testDebug() {
   1653         mTextView = new TextView(mActivity);
   1654         mTextView.debug(0);
   1655 
   1656         mTextView.setText("Hello!");
   1657         layout(mTextView);
   1658         mTextView.debug(1);
   1659     }
   1660 
   1661     public void testSelection() {
   1662         mTextView = new TextView(mActivity);
   1663         String text = "This is the content";
   1664         mTextView.setText(text, BufferType.SPANNABLE);
   1665         assertFalse(mTextView.hasSelection());
   1666 
   1667         Selection.selectAll((Spannable) mTextView.getText());
   1668         assertEquals(0, mTextView.getSelectionStart());
   1669         assertEquals(text.length(), mTextView.getSelectionEnd());
   1670         assertTrue(mTextView.hasSelection());
   1671 
   1672         int selectionStart = 5;
   1673         int selectionEnd = 7;
   1674         Selection.setSelection((Spannable) mTextView.getText(), selectionStart);
   1675         assertEquals(selectionStart, mTextView.getSelectionStart());
   1676         assertEquals(selectionStart, mTextView.getSelectionEnd());
   1677         assertFalse(mTextView.hasSelection());
   1678 
   1679         Selection.setSelection((Spannable) mTextView.getText(), selectionStart, selectionEnd);
   1680         assertEquals(selectionStart, mTextView.getSelectionStart());
   1681         assertEquals(selectionEnd, mTextView.getSelectionEnd());
   1682         assertTrue(mTextView.hasSelection());
   1683     }
   1684 
   1685     @UiThreadTest
   1686     public void testAccessEllipsize() {
   1687         mActivity.setContentView(R.layout.textview_ellipsize);
   1688 
   1689         mTextView = findTextView(R.id.ellipsize_default);
   1690         assertNull(mTextView.getEllipsize());
   1691 
   1692         mTextView = findTextView(R.id.ellipsize_none);
   1693         assertNull(mTextView.getEllipsize());
   1694 
   1695         mTextView = findTextView(R.id.ellipsize_start);
   1696         assertSame(TruncateAt.START, mTextView.getEllipsize());
   1697 
   1698         mTextView = findTextView(R.id.ellipsize_middle);
   1699         assertSame(TruncateAt.MIDDLE, mTextView.getEllipsize());
   1700 
   1701         mTextView = findTextView(R.id.ellipsize_end);
   1702         assertSame(TruncateAt.END, mTextView.getEllipsize());
   1703 
   1704         mTextView.setEllipsize(TextUtils.TruncateAt.START);
   1705         assertSame(TextUtils.TruncateAt.START, mTextView.getEllipsize());
   1706 
   1707         mTextView.setEllipsize(TextUtils.TruncateAt.MIDDLE);
   1708         assertSame(TextUtils.TruncateAt.MIDDLE, mTextView.getEllipsize());
   1709 
   1710         mTextView.setEllipsize(TextUtils.TruncateAt.END);
   1711         assertSame(TextUtils.TruncateAt.END, mTextView.getEllipsize());
   1712 
   1713         mTextView.setEllipsize(null);
   1714         assertNull(mTextView.getEllipsize());
   1715 
   1716         mTextView.setWidth(10);
   1717         mTextView.setEllipsize(TextUtils.TruncateAt.START);
   1718         mTextView.setText("ThisIsAVeryLongVeryLongVeryLongVeryLongVeryLongWord");
   1719         mTextView.invalidate();
   1720 
   1721         assertSame(TextUtils.TruncateAt.START, mTextView.getEllipsize());
   1722         // there is no method to check if '...yLongVeryLongWord' is painted in the screen.
   1723     }
   1724 
   1725     public void testSetCursorVisible() {
   1726         mTextView = new TextView(mActivity);
   1727 
   1728         mTextView.setCursorVisible(true);
   1729         mTextView.setCursorVisible(false);
   1730     }
   1731 
   1732     public void testOnWindowFocusChanged() {
   1733         // Do not test. Implementation details.
   1734     }
   1735 
   1736     public void testOnTouchEvent() {
   1737         // Do not test. Implementation details.
   1738     }
   1739 
   1740     public void testOnTrackballEvent() {
   1741         // Do not test. Implementation details.
   1742     }
   1743 
   1744     public void testGetTextColors() {
   1745         // TODO: How to get a suitable TypedArray to test this method.
   1746     }
   1747 
   1748     public void testOnKeyShortcut() {
   1749         // Do not test. Implementation details.
   1750     }
   1751 
   1752     @UiThreadTest
   1753     public void testPerformLongClick() {
   1754         mTextView = findTextView(R.id.textview_text);
   1755         mTextView.setText("This is content");
   1756         MockOnLongClickListener onLongClickListener = new MockOnLongClickListener(true);
   1757         MockOnCreateContextMenuListener onCreateContextMenuListener
   1758                 = new MockOnCreateContextMenuListener(false);
   1759         mTextView.setOnLongClickListener(onLongClickListener);
   1760         mTextView.setOnCreateContextMenuListener(onCreateContextMenuListener);
   1761         assertTrue(mTextView.performLongClick());
   1762         assertTrue(onLongClickListener.hasLongClicked());
   1763         assertFalse(onCreateContextMenuListener.hasCreatedContextMenu());
   1764 
   1765         onLongClickListener = new MockOnLongClickListener(false);
   1766         mTextView.setOnLongClickListener(onLongClickListener);
   1767         mTextView.setOnCreateContextMenuListener(onCreateContextMenuListener);
   1768         assertTrue(mTextView.performLongClick());
   1769         assertTrue(onLongClickListener.hasLongClicked());
   1770         assertTrue(onCreateContextMenuListener.hasCreatedContextMenu());
   1771 
   1772         mTextView.setOnLongClickListener(null);
   1773         onCreateContextMenuListener = new MockOnCreateContextMenuListener(true);
   1774         mTextView.setOnCreateContextMenuListener(onCreateContextMenuListener);
   1775         assertFalse(mTextView.performLongClick());
   1776         assertTrue(onCreateContextMenuListener.hasCreatedContextMenu());
   1777     }
   1778 
   1779     @UiThreadTest
   1780     public void testTextAttr() {
   1781         mTextView = findTextView(R.id.textview_textAttr);
   1782         // getText
   1783         assertEquals(mActivity.getString(R.string.text_view_hello), mTextView.getText().toString());
   1784 
   1785         // getCurrentTextColor
   1786         assertEquals(mActivity.getResources().getColor(R.drawable.black),
   1787                 mTextView.getCurrentTextColor());
   1788         assertEquals(mActivity.getResources().getColor(R.drawable.red),
   1789                 mTextView.getCurrentHintTextColor());
   1790         assertEquals(mActivity.getResources().getColor(R.drawable.red),
   1791                 mTextView.getHintTextColors().getDefaultColor());
   1792         assertEquals(mActivity.getResources().getColor(R.drawable.blue),
   1793                 mTextView.getLinkTextColors().getDefaultColor());
   1794 
   1795         // getTextScaleX
   1796         assertEquals(1.2f, mTextView.getTextScaleX(), 0.01f);
   1797 
   1798         // setTextScaleX
   1799         mTextView.setTextScaleX(2.4f);
   1800         assertEquals(2.4f, mTextView.getTextScaleX(), 0.01f);
   1801 
   1802         mTextView.setTextScaleX(0f);
   1803         assertEquals(0f, mTextView.getTextScaleX(), 0.01f);
   1804 
   1805         mTextView.setTextScaleX(- 2.4f);
   1806         assertEquals(- 2.4f, mTextView.getTextScaleX(), 0.01f);
   1807 
   1808         // getTextSize
   1809         assertEquals(20f, mTextView.getTextSize(), 0.01f);
   1810 
   1811         // getTypeface
   1812         // getTypeface will be null if android:typeface is set to normal,
   1813         // and android:style is not set or is set to normal, and
   1814         // android:fontFamily is not set
   1815         assertNull(mTextView.getTypeface());
   1816 
   1817         mTextView.setTypeface(Typeface.DEFAULT);
   1818         assertSame(Typeface.DEFAULT, mTextView.getTypeface());
   1819         // null type face
   1820         mTextView.setTypeface(null);
   1821         assertNull(mTextView.getTypeface());
   1822 
   1823         // default type face, bold style, note: the type face will be changed
   1824         // after call set method
   1825         mTextView.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
   1826         assertSame(Typeface.BOLD, mTextView.getTypeface().getStyle());
   1827 
   1828         // null type face, BOLD style
   1829         mTextView.setTypeface(null, Typeface.BOLD);
   1830         assertSame(Typeface.BOLD, mTextView.getTypeface().getStyle());
   1831 
   1832         // old type face, null style
   1833         mTextView.setTypeface(Typeface.DEFAULT, 0);
   1834         assertEquals(Typeface.NORMAL, mTextView.getTypeface().getStyle());
   1835     }
   1836 
   1837     @UiThreadTest
   1838     public void testAppend() {
   1839         mTextView = new TextView(mActivity);
   1840 
   1841         // 1: check the original length, should be blank as initialised.
   1842         assertEquals(0, mTextView.getText().length());
   1843 
   1844         // 2: append a string use append(CharSquence) into the original blank
   1845         // buffer, check the content. And upgrading it to BufferType.EDITABLE if it was
   1846         // not already editable.
   1847         assertFalse(mTextView.getText() instanceof Editable);
   1848         mTextView.append("Append.");
   1849         assertEquals("Append.", mTextView.getText().toString());
   1850         assertTrue(mTextView.getText() instanceof Editable);
   1851 
   1852         // 3: append a string from 0~3.
   1853         mTextView.append("Append", 0, 3);
   1854         assertEquals("Append.App", mTextView.getText().toString());
   1855         assertTrue(mTextView.getText() instanceof Editable);
   1856 
   1857         // 4: append a string from 0~0, nothing will be append as expected.
   1858         mTextView.append("Append", 0, 0);
   1859         assertEquals("Append.App", mTextView.getText().toString());
   1860         assertTrue(mTextView.getText() instanceof Editable);
   1861 
   1862         // 5: append a string from -3~3. check the wrong left edge.
   1863         try {
   1864             mTextView.append("Append", -3, 3);
   1865             fail("Should throw StringIndexOutOfBoundsException");
   1866         } catch (StringIndexOutOfBoundsException e) {
   1867         }
   1868 
   1869         // 6: append a string from 3~10. check the wrong right edge.
   1870         try {
   1871             mTextView.append("Append", 3, 10);
   1872             fail("Should throw StringIndexOutOfBoundsException");
   1873         } catch (StringIndexOutOfBoundsException e) {
   1874         }
   1875 
   1876         // 7: append a null string.
   1877         try {
   1878             mTextView.append(null);
   1879             fail("Should throw NullPointerException");
   1880         } catch (NullPointerException e) {
   1881         }
   1882     }
   1883 
   1884     public void testAccessTransformationMethod() {
   1885         // check the password attribute in xml
   1886         mTextView = findTextView(R.id.textview_password);
   1887         assertNotNull(mTextView);
   1888         assertSame(PasswordTransformationMethod.getInstance(),
   1889                 mTextView.getTransformationMethod());
   1890 
   1891         // check the singleLine attribute in xml
   1892         mTextView = findTextView(R.id.textview_singleLine);
   1893         assertNotNull(mTextView);
   1894         assertSame(SingleLineTransformationMethod.getInstance(),
   1895                 mTextView.getTransformationMethod());
   1896 
   1897         final QwertyKeyListener qwertyKeyListener = QwertyKeyListener.getInstance(false,
   1898                 Capitalize.NONE);
   1899         final TransformationMethod method = PasswordTransformationMethod.getInstance();
   1900         // change transformation method by function
   1901         mActivity.runOnUiThread(new Runnable() {
   1902             public void run() {
   1903                 mTextView.setKeyListener(qwertyKeyListener);
   1904                 mTextView.setTransformationMethod(method);
   1905                 mTransformedText = method.getTransformation(mTextView.getText(), mTextView);
   1906 
   1907                 mTextView.requestFocus();
   1908             }
   1909         });
   1910         mInstrumentation.waitForIdleSync();
   1911         assertSame(PasswordTransformationMethod.getInstance(),
   1912                 mTextView.getTransformationMethod());
   1913 
   1914         sendKeys("H E 2*L O");
   1915         mActivity.runOnUiThread(new Runnable() {
   1916             public void run() {
   1917                 mTextView.append(" ");
   1918             }
   1919         });
   1920         mInstrumentation.waitForIdleSync();
   1921 
   1922         // it will get transformed after a while
   1923         new PollingCheck(TIMEOUT) {
   1924             @Override
   1925             protected boolean check() {
   1926                 // "******"
   1927                 return mTransformedText.toString()
   1928                         .equals("\u2022\u2022\u2022\u2022\u2022\u2022");
   1929             }
   1930         }.run();
   1931 
   1932         // set null
   1933         mActivity.runOnUiThread(new Runnable() {
   1934             public void run() {
   1935                 mTextView.setTransformationMethod(null);
   1936             }
   1937         });
   1938         mInstrumentation.waitForIdleSync();
   1939         assertNull(mTextView.getTransformationMethod());
   1940     }
   1941 
   1942     @UiThreadTest
   1943     public void testCompound() {
   1944         mTextView = new TextView(mActivity);
   1945         int padding = 3;
   1946         Drawable[] drawables = mTextView.getCompoundDrawables();
   1947         assertNull(drawables[0]);
   1948         assertNull(drawables[1]);
   1949         assertNull(drawables[2]);
   1950         assertNull(drawables[3]);
   1951 
   1952         // test setCompoundDrawablePadding and getCompoundDrawablePadding
   1953         mTextView.setCompoundDrawablePadding(padding);
   1954         assertEquals(padding, mTextView.getCompoundDrawablePadding());
   1955 
   1956         // using resid, 0 represents null
   1957         mTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.start, R.drawable.pass,
   1958                 R.drawable.failed, 0);
   1959         drawables = mTextView.getCompoundDrawables();
   1960 
   1961         // drawableLeft
   1962         WidgetTestUtils.assertEquals(getBitmap(R.drawable.start),
   1963                 ((BitmapDrawable) drawables[0]).getBitmap());
   1964         // drawableTop
   1965         WidgetTestUtils.assertEquals(getBitmap(R.drawable.pass),
   1966                 ((BitmapDrawable) drawables[1]).getBitmap());
   1967         // drawableRight
   1968         WidgetTestUtils.assertEquals(getBitmap(R.drawable.failed),
   1969                 ((BitmapDrawable) drawables[2]).getBitmap());
   1970         // drawableBottom
   1971         assertNull(drawables[3]);
   1972 
   1973         Drawable left = getDrawable(R.drawable.blue);
   1974         Drawable right = getDrawable(R.drawable.yellow);
   1975         Drawable top = getDrawable(R.drawable.red);
   1976 
   1977         // using drawables directly
   1978         mTextView.setCompoundDrawablesWithIntrinsicBounds(left, top, right, null);
   1979         drawables = mTextView.getCompoundDrawables();
   1980 
   1981         // drawableLeft
   1982         assertSame(left, drawables[0]);
   1983         // drawableTop
   1984         assertSame(top, drawables[1]);
   1985         // drawableRight
   1986         assertSame(right, drawables[2]);
   1987         // drawableBottom
   1988         assertNull(drawables[3]);
   1989 
   1990         // check compound padding
   1991         assertEquals(mTextView.getPaddingLeft() + padding + left.getIntrinsicWidth(),
   1992                 mTextView.getCompoundPaddingLeft());
   1993         assertEquals(mTextView.getPaddingTop() + padding + top.getIntrinsicHeight(),
   1994                 mTextView.getCompoundPaddingTop());
   1995         assertEquals(mTextView.getPaddingRight() + padding + right.getIntrinsicWidth(),
   1996                 mTextView.getCompoundPaddingRight());
   1997         assertEquals(mTextView.getPaddingBottom(), mTextView.getCompoundPaddingBottom());
   1998 
   1999         // set bounds to drawables and set them again.
   2000         left.setBounds(0, 0, 1, 2);
   2001         right.setBounds(0, 0, 3, 4);
   2002         top.setBounds(0, 0, 5, 6);
   2003         // usinf drawables
   2004         mTextView.setCompoundDrawables(left, top, right, null);
   2005         drawables = mTextView.getCompoundDrawables();
   2006 
   2007         // drawableLeft
   2008         assertSame(left, drawables[0]);
   2009         // drawableTop
   2010         assertSame(top, drawables[1]);
   2011         // drawableRight
   2012         assertSame(right, drawables[2]);
   2013         // drawableBottom
   2014         assertNull(drawables[3]);
   2015 
   2016         // check compound padding
   2017         assertEquals(mTextView.getPaddingLeft() + padding + left.getBounds().width(),
   2018                 mTextView.getCompoundPaddingLeft());
   2019         assertEquals(mTextView.getPaddingTop() + padding + top.getBounds().height(),
   2020                 mTextView.getCompoundPaddingTop());
   2021         assertEquals(mTextView.getPaddingRight() + padding + right.getBounds().width(),
   2022                 mTextView.getCompoundPaddingRight());
   2023         assertEquals(mTextView.getPaddingBottom(), mTextView.getCompoundPaddingBottom());
   2024     }
   2025 
   2026     public void testSingleLine() {
   2027         final TextView textView = new TextView(mActivity);
   2028         setSpannableText(textView, "This is a really long sentence"
   2029                 + " which can not be placed in one line on the screen.");
   2030 
   2031         // Narrow layout assures that the text will get wrapped.
   2032         FrameLayout innerLayout = new FrameLayout(mActivity);
   2033         innerLayout.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
   2034         innerLayout.addView(textView);
   2035 
   2036         final FrameLayout layout = new FrameLayout(mActivity);
   2037         layout.addView(innerLayout);
   2038 
   2039         mActivity.runOnUiThread(new Runnable() {
   2040             public void run() {
   2041                 mActivity.setContentView(layout);
   2042                 textView.setSingleLine(true);
   2043             }
   2044         });
   2045         mInstrumentation.waitForIdleSync();
   2046 
   2047         assertEquals(SingleLineTransformationMethod.getInstance(),
   2048                 textView.getTransformationMethod());
   2049         int singleLineWidth = textView.getLayout().getWidth();
   2050         int singleLineHeight = textView.getLayout().getHeight();
   2051 
   2052         mActivity.runOnUiThread(new Runnable() {
   2053             public void run() {
   2054                 textView.setSingleLine(false);
   2055             }
   2056         });
   2057         mInstrumentation.waitForIdleSync();
   2058         assertEquals(null, textView.getTransformationMethod());
   2059         assertTrue(textView.getLayout().getHeight() > singleLineHeight);
   2060         assertTrue(textView.getLayout().getWidth() < singleLineWidth);
   2061 
   2062         // same behaviours as setSingLine(true)
   2063         mActivity.runOnUiThread(new Runnable() {
   2064             public void run() {
   2065                 textView.setSingleLine();
   2066             }
   2067         });
   2068         mInstrumentation.waitForIdleSync();
   2069         assertEquals(SingleLineTransformationMethod.getInstance(),
   2070                 textView.getTransformationMethod());
   2071         assertEquals(singleLineHeight, textView.getLayout().getHeight());
   2072         assertEquals(singleLineWidth, textView.getLayout().getWidth());
   2073     }
   2074 
   2075     @UiThreadTest
   2076     public void testSetMaxLines() {
   2077         mTextView = findTextView(R.id.textview_text);
   2078 
   2079         float[] widths = new float[LONG_TEXT.length()];
   2080         mTextView.getPaint().getTextWidths(LONG_TEXT, widths);
   2081         float totalWidth = 0.0f;
   2082         for (float f : widths) {
   2083             totalWidth += f;
   2084         }
   2085         final int stringWidth = (int) totalWidth;
   2086         mTextView.setWidth(stringWidth >> 2);
   2087         mTextView.setText(LONG_TEXT);
   2088 
   2089         final int maxLines = 2;
   2090         assertTrue(mTextView.getLineCount() > maxLines);
   2091 
   2092         mTextView.setMaxLines(maxLines);
   2093         mTextView.requestLayout();
   2094 
   2095         assertTrue(mTextView.getHeight() <= maxLines * mTextView.getLineHeight());
   2096     }
   2097 
   2098     @UiThreadTest
   2099     public void testSetMaxLinesException() {
   2100         mTextView = new TextView(mActivity);
   2101         mActivity.setContentView(mTextView);
   2102         mTextView.setWidth(mTextView.getWidth() >> 3);
   2103         mTextView.setMaxLines(-1);
   2104     }
   2105 
   2106     public void testSetMinLines() {
   2107         mTextView = findTextView(R.id.textview_text);
   2108         setWidth(mTextView.getWidth() >> 3);
   2109         int originalHeight = mTextView.getHeight();
   2110         int originalLines = mTextView.getLineCount();
   2111 
   2112         setMinLines(originalLines - 1);
   2113         assertTrue((originalLines - 1) * mTextView.getLineHeight() <= mTextView.getHeight());
   2114 
   2115         setMinLines(originalLines + 1);
   2116         assertTrue((originalLines + 1) * mTextView.getLineHeight() <= mTextView.getHeight());
   2117     }
   2118 
   2119     public void testSetLines() {
   2120         mTextView = findTextView(R.id.textview_text);
   2121         // make it multiple lines
   2122         setWidth(mTextView.getWidth() >> 3);
   2123         int originalLines = mTextView.getLineCount();
   2124 
   2125         setLines(originalLines - 1);
   2126         assertTrue((originalLines - 1) * mTextView.getLineHeight() <= mTextView.getHeight());
   2127 
   2128         setLines(originalLines + 1);
   2129         assertTrue((originalLines + 1) * mTextView.getLineHeight() <= mTextView.getHeight());
   2130     }
   2131 
   2132     @UiThreadTest
   2133     public void testSetLinesException() {
   2134         mTextView = new TextView(mActivity);
   2135         mActivity.setContentView(mTextView);
   2136         mTextView.setWidth(mTextView.getWidth() >> 3);
   2137         mTextView.setLines(-1);
   2138     }
   2139 
   2140     @UiThreadTest
   2141     public void testGetExtendedPaddingTop() {
   2142         mTextView = findTextView(R.id.textview_text);
   2143         // Initialized value
   2144         assertEquals(0, mTextView.getExtendedPaddingTop());
   2145 
   2146         // After Set a Drawable
   2147         final Drawable top = getDrawable(R.drawable.red);
   2148         top.setBounds(0, 0, 100, 10);
   2149         mTextView.setCompoundDrawables(null, top, null, null);
   2150         assertEquals(mTextView.getCompoundPaddingTop(), mTextView.getExtendedPaddingTop());
   2151 
   2152         // Change line count
   2153         mTextView.setLines(mTextView.getLineCount() - 1);
   2154         mTextView.setGravity(Gravity.BOTTOM);
   2155 
   2156         assertTrue(mTextView.getExtendedPaddingTop() > 0);
   2157     }
   2158 
   2159     @UiThreadTest
   2160     public void testGetExtendedPaddingBottom() {
   2161         mTextView = findTextView(R.id.textview_text);
   2162         // Initialized value
   2163         assertEquals(0, mTextView.getExtendedPaddingBottom());
   2164 
   2165         // After Set a Drawable
   2166         final Drawable bottom = getDrawable(R.drawable.red);
   2167         bottom.setBounds(0, 0, 100, 10);
   2168         mTextView.setCompoundDrawables(null, null, null, bottom);
   2169         assertEquals(mTextView.getCompoundPaddingBottom(), mTextView.getExtendedPaddingBottom());
   2170 
   2171         // Change line count
   2172         mTextView.setLines(mTextView.getLineCount() - 1);
   2173         mTextView.setGravity(Gravity.CENTER_VERTICAL);
   2174 
   2175         assertTrue(mTextView.getExtendedPaddingBottom() > 0);
   2176     }
   2177 
   2178     public void testGetTotalPaddingTop() {
   2179         mTextView = findTextView(R.id.textview_text);
   2180         // Initialized value
   2181         assertEquals(0, mTextView.getTotalPaddingTop());
   2182 
   2183         // After Set a Drawable
   2184         final Drawable top = getDrawable(R.drawable.red);
   2185         top.setBounds(0, 0, 100, 10);
   2186         mActivity.runOnUiThread(new Runnable() {
   2187             public void run() {
   2188                 mTextView.setCompoundDrawables(null, top, null, null);
   2189                 mTextView.setLines(mTextView.getLineCount() - 1);
   2190                 mTextView.setGravity(Gravity.BOTTOM);
   2191             }
   2192         });
   2193         mInstrumentation.waitForIdleSync();
   2194         assertEquals(mTextView.getExtendedPaddingTop(), mTextView.getTotalPaddingTop());
   2195 
   2196         // Change line count
   2197         setLines(mTextView.getLineCount() + 1);
   2198         int expected = mTextView.getHeight()
   2199                 - mTextView.getExtendedPaddingBottom()
   2200                 - mTextView.getLayout().getLineTop(mTextView.getLineCount());
   2201         assertEquals(expected, mTextView.getTotalPaddingTop());
   2202     }
   2203 
   2204     public void testGetTotalPaddingBottom() {
   2205         mTextView = findTextView(R.id.textview_text);
   2206         // Initialized value
   2207         assertEquals(0, mTextView.getTotalPaddingBottom());
   2208 
   2209         // After Set a Drawable
   2210         final Drawable bottom = getDrawable(R.drawable.red);
   2211         bottom.setBounds(0, 0, 100, 10);
   2212         mActivity.runOnUiThread(new Runnable() {
   2213             public void run() {
   2214                 mTextView.setCompoundDrawables(null, null, null, bottom);
   2215                 mTextView.setLines(mTextView.getLineCount() - 1);
   2216                 mTextView.setGravity(Gravity.CENTER_VERTICAL);
   2217             }
   2218         });
   2219         mInstrumentation.waitForIdleSync();
   2220         assertEquals(mTextView.getExtendedPaddingBottom(), mTextView.getTotalPaddingBottom());
   2221 
   2222         // Change line count
   2223         setLines(mTextView.getLineCount() + 1);
   2224         int expected = ((mTextView.getHeight()
   2225                 - mTextView.getExtendedPaddingBottom()
   2226                 - mTextView.getExtendedPaddingTop()
   2227                 - mTextView.getLayout().getLineBottom(mTextView.getLineCount())) >> 1)
   2228                 + mTextView.getExtendedPaddingBottom();
   2229         assertEquals(expected, mTextView.getTotalPaddingBottom());
   2230     }
   2231 
   2232     @UiThreadTest
   2233     public void testGetTotalPaddingLeft() {
   2234         mTextView = findTextView(R.id.textview_text);
   2235         // Initialized value
   2236         assertEquals(0, mTextView.getTotalPaddingLeft());
   2237 
   2238         // After Set a Drawable
   2239         Drawable left = getDrawable(R.drawable.red);
   2240         left.setBounds(0, 0, 10, 100);
   2241         mTextView.setCompoundDrawables(left, null, null, null);
   2242         mTextView.setGravity(Gravity.RIGHT);
   2243         assertEquals(mTextView.getCompoundPaddingLeft(), mTextView.getTotalPaddingLeft());
   2244 
   2245         // Change width
   2246         mTextView.setWidth(Integer.MAX_VALUE);
   2247         assertEquals(mTextView.getCompoundPaddingLeft(), mTextView.getTotalPaddingLeft());
   2248     }
   2249 
   2250     @UiThreadTest
   2251     public void testGetTotalPaddingRight() {
   2252         mTextView = findTextView(R.id.textview_text);
   2253         // Initialized value
   2254         assertEquals(0, mTextView.getTotalPaddingRight());
   2255 
   2256         // After Set a Drawable
   2257         Drawable right = getDrawable(R.drawable.red);
   2258         right.setBounds(0, 0, 10, 100);
   2259         mTextView.setCompoundDrawables(null, null, right, null);
   2260         mTextView.setGravity(Gravity.CENTER_HORIZONTAL);
   2261         assertEquals(mTextView.getCompoundPaddingRight(), mTextView.getTotalPaddingRight());
   2262 
   2263         // Change width
   2264         mTextView.setWidth(Integer.MAX_VALUE);
   2265         assertEquals(mTextView.getCompoundPaddingRight(), mTextView.getTotalPaddingRight());
   2266     }
   2267 
   2268     public void testGetUrls() {
   2269         mTextView = new TextView(mActivity);
   2270 
   2271         URLSpan[] spans = mTextView.getUrls();
   2272         assertEquals(0, spans.length);
   2273 
   2274         String url = "http://www.google.com";
   2275         String email = "name (at) gmail.com";
   2276         String string = url + " mailto:" + email;
   2277         SpannableString spannable = new SpannableString(string);
   2278         spannable.setSpan(new URLSpan(url), 0, url.length(), 0);
   2279         mTextView.setText(spannable, BufferType.SPANNABLE);
   2280         spans = mTextView.getUrls();
   2281         assertEquals(1, spans.length);
   2282         assertEquals(url, spans[0].getURL());
   2283 
   2284         spannable.setSpan(new URLSpan(email), 0, email.length(), 0);
   2285         mTextView.setText(spannable, BufferType.SPANNABLE);
   2286 
   2287         spans = mTextView.getUrls();
   2288         assertEquals(2, spans.length);
   2289         assertEquals(url, spans[0].getURL());
   2290         assertEquals(email, spans[1].getURL());
   2291 
   2292         // test the situation that param what is not a URLSpan
   2293         spannable.setSpan(new Object(), 0, 9, 0);
   2294         mTextView.setText(spannable, BufferType.SPANNABLE);
   2295         spans = mTextView.getUrls();
   2296         assertEquals(2, spans.length);
   2297     }
   2298 
   2299     public void testSetPadding() {
   2300         mTextView = new TextView(mActivity);
   2301 
   2302         mTextView.setPadding(0, 1, 2, 4);
   2303         assertEquals(0, mTextView.getPaddingLeft());
   2304         assertEquals(1, mTextView.getPaddingTop());
   2305         assertEquals(2, mTextView.getPaddingRight());
   2306         assertEquals(4, mTextView.getPaddingBottom());
   2307 
   2308         mTextView.setPadding(10, 20, 30, 40);
   2309         assertEquals(10, mTextView.getPaddingLeft());
   2310         assertEquals(20, mTextView.getPaddingTop());
   2311         assertEquals(30, mTextView.getPaddingRight());
   2312         assertEquals(40, mTextView.getPaddingBottom());
   2313     }
   2314 
   2315     public void testSetTextAppearance() {
   2316         mTextView = new TextView(mActivity);
   2317 
   2318         mTextView.setTextAppearance(mActivity, R.style.TextAppearance_All);
   2319         assertEquals(mActivity.getResources().getColor(R.drawable.black),
   2320                 mTextView.getCurrentTextColor());
   2321         assertEquals(20f, mTextView.getTextSize(), 0.01f);
   2322         assertEquals(Typeface.BOLD, mTextView.getTypeface().getStyle());
   2323         assertEquals(mActivity.getResources().getColor(R.drawable.red),
   2324                 mTextView.getCurrentHintTextColor());
   2325         assertEquals(mActivity.getResources().getColor(R.drawable.blue),
   2326                 mTextView.getLinkTextColors().getDefaultColor());
   2327 
   2328         mTextView.setTextAppearance(mActivity, R.style.TextAppearance_Colors);
   2329         assertEquals(mActivity.getResources().getColor(R.drawable.black),
   2330                 mTextView.getCurrentTextColor());
   2331         assertEquals(mActivity.getResources().getColor(R.drawable.blue),
   2332                 mTextView.getCurrentHintTextColor());
   2333         assertEquals(mActivity.getResources().getColor(R.drawable.yellow),
   2334                 mTextView.getLinkTextColors().getDefaultColor());
   2335 
   2336         mTextView.setTextAppearance(mActivity, R.style.TextAppearance_NotColors);
   2337         assertEquals(17f, mTextView.getTextSize(), 0.01f);
   2338         assertEquals(Typeface.NORMAL, mTextView.getTypeface().getStyle());
   2339 
   2340         mTextView.setTextAppearance(mActivity, R.style.TextAppearance_Style);
   2341         assertEquals(null, mTextView.getTypeface());
   2342     }
   2343 
   2344     public void testOnPreDraw() {
   2345         // Do not test. Implementation details.
   2346     }
   2347 
   2348     public void testSetHorizontallyScrolling() {
   2349         // make the text view has more than one line
   2350         mTextView = findTextView(R.id.textview_text);
   2351         setWidth(mTextView.getWidth() >> 1);
   2352         assertTrue(mTextView.getLineCount() > 1);
   2353 
   2354         setHorizontallyScrolling(true);
   2355         assertEquals(1, mTextView.getLineCount());
   2356 
   2357         setHorizontallyScrolling(false);
   2358         assertTrue(mTextView.getLineCount() > 1);
   2359     }
   2360 
   2361     public void testComputeHorizontalScrollRange() {
   2362         MockTextView textView = new MockTextView(mActivity);
   2363         // test when layout is null
   2364         assertNull(textView.getLayout());
   2365         assertEquals(textView.getWidth(), textView.computeHorizontalScrollRange());
   2366 
   2367         textView.setFrame(0, 0, 40, 50);
   2368         assertEquals(textView.getWidth(), textView.computeHorizontalScrollRange());
   2369 
   2370         // set the layout
   2371         layout(textView);
   2372         assertEquals(textView.getLayout().getWidth(), textView.computeHorizontalScrollRange());
   2373     }
   2374 
   2375     public void testComputeVerticalScrollRange() {
   2376         MockTextView textView = new MockTextView(mActivity);
   2377         // test when layout is null
   2378         assertNull(textView.getLayout());
   2379         assertEquals(0, textView.computeVerticalScrollRange());
   2380 
   2381         textView.setFrame(0, 0, 40, 50);
   2382         assertEquals(textView.getHeight(), textView.computeVerticalScrollRange());
   2383 
   2384         //set the layout
   2385         layout(textView);
   2386         assertEquals(textView.getLayout().getHeight(), textView.computeVerticalScrollRange());
   2387     }
   2388 
   2389     public void testDrawableStateChanged() {
   2390         MockTextView textView = new MockTextView(mActivity);
   2391 
   2392         textView.reset();
   2393         textView.refreshDrawableState();
   2394         assertTrue(textView.hasCalledDrawableStateChanged());
   2395     }
   2396 
   2397     public void testGetDefaultEditable() {
   2398         MockTextView textView = new MockTextView(mActivity);
   2399 
   2400         //the TextView#getDefaultEditable() does nothing, and always return false.
   2401         assertFalse(textView.getDefaultEditable());
   2402     }
   2403 
   2404     public void testGetDefaultMovementMethod() {
   2405         MockTextView textView = new MockTextView(mActivity);
   2406 
   2407         //the TextView#getDefaultMovementMethod() does nothing, and always return null.
   2408         assertNull(textView.getDefaultMovementMethod());
   2409     }
   2410 
   2411     public void testOnCreateContextMenu() {
   2412         // Do not test. Implementation details.
   2413     }
   2414 
   2415     public void testOnDetachedFromWindow() {
   2416         // Do not test. Implementation details.
   2417     }
   2418 
   2419     public void testOnDraw() {
   2420         // Do not test. Implementation details.
   2421     }
   2422 
   2423     public void testOnFocusChanged() {
   2424         // Do not test. Implementation details.
   2425     }
   2426 
   2427     public void testOnMeasure() {
   2428         // Do not test. Implementation details.
   2429     }
   2430 
   2431     public void testOnTextChanged() {
   2432         // Do not test. Implementation details.
   2433     }
   2434 
   2435     public void testSetFrame() {
   2436         MockTextView textView = new MockTextView(mActivity);
   2437 
   2438         //Assign a new size to this view
   2439         assertTrue(textView.setFrame(0, 0, 320, 480));
   2440         assertEquals(0, textView.getFrameLeft());
   2441         assertEquals(0, textView.getFrameTop());
   2442         assertEquals(320, textView.getFrameRight());
   2443         assertEquals(480, textView.getFrameBottom());
   2444 
   2445         //Assign a same size to this view
   2446         assertFalse(textView.setFrame(0, 0, 320, 480));
   2447 
   2448         //negative input
   2449         assertTrue(textView.setFrame(-1, -1, -1, -1));
   2450         assertEquals(-1, textView.getFrameLeft());
   2451         assertEquals(-1, textView.getFrameTop());
   2452         assertEquals(-1, textView.getFrameRight());
   2453         assertEquals(-1, textView.getFrameBottom());
   2454     }
   2455 
   2456     public void testGetFadingEdgeStrength() {
   2457         final MockTextView textViewLeft = (MockTextView) mActivity.findViewById(
   2458                 R.id.mock_textview_left);
   2459         mActivity.runOnUiThread(new Runnable() {
   2460             public void run() {
   2461                 textViewLeft.setEllipsize(null);
   2462             }
   2463         });
   2464         mInstrumentation.waitForIdleSync();
   2465 
   2466         // fading is shown on right side if the text aligns left
   2467         assertEquals(0.0f, textViewLeft.getLeftFadingEdgeStrength(), 0.01f);
   2468         assertEquals(1.0f, textViewLeft.getRightFadingEdgeStrength(), 0.01f);
   2469 
   2470         final MockTextView textViewRight = (MockTextView) mActivity.findViewById(
   2471                 R.id.mock_textview_right);
   2472         mActivity.runOnUiThread(new Runnable() {
   2473             public void run() {
   2474                 textViewRight.setEllipsize(null);
   2475             }
   2476         });
   2477         mInstrumentation.waitForIdleSync();
   2478         // fading is shown on left side if the text aligns right
   2479         assertEquals(1.0f, textViewRight.getLeftFadingEdgeStrength(), 0.01f);
   2480         assertEquals(0.0f, textViewRight.getRightFadingEdgeStrength(), 0.01f);
   2481 
   2482         final MockTextView textViewCenter = (MockTextView) mActivity.findViewById(
   2483                 R.id.mock_textview_center);
   2484         mActivity.runOnUiThread(new Runnable() {
   2485             public void run() {
   2486                 textViewCenter.setEllipsize(null);
   2487             }
   2488         });
   2489         mInstrumentation.waitForIdleSync();
   2490         // fading is shown on both sides if the text aligns center
   2491         assertEquals(1.0f, textViewCenter.getLeftFadingEdgeStrength(), 0.01f);
   2492         assertEquals(1.0f, textViewCenter.getRightFadingEdgeStrength(), 0.01f);
   2493     }
   2494 
   2495 
   2496     public void testMarquee() {
   2497         final MockTextView textView = new MockTextView(mActivity);
   2498         textView.setText(LONG_TEXT);
   2499         textView.setSingleLine();
   2500         textView.setEllipsize(TruncateAt.MARQUEE);
   2501         textView.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
   2502 
   2503         final FrameLayout layout = new FrameLayout(mActivity);
   2504         layout.addView(textView);
   2505 
   2506         // make the fading to be shown
   2507         textView.setHorizontalFadingEdgeEnabled(true);
   2508 
   2509         mActivity.runOnUiThread(new Runnable() {
   2510             public void run() {
   2511                 mActivity.setContentView(layout);
   2512             }
   2513         });
   2514         mInstrumentation.waitForIdleSync();
   2515 
   2516         TestSelectedRunnable runnable = new TestSelectedRunnable(textView) {
   2517             public void run() {
   2518                 textView.setMarqueeRepeatLimit(-1);
   2519                 // force the marquee to start
   2520                 saveIsSelected1();
   2521                 textView.setSelected(true);
   2522                 saveIsSelected2();
   2523             }
   2524         };
   2525         mActivity.runOnUiThread(runnable);
   2526 
   2527         // wait for the marquee to run
   2528         // fading is shown on both sides if the marquee runs for a while
   2529         new PollingCheck(TIMEOUT) {
   2530             @Override
   2531             protected boolean check() {
   2532                 return textView.getLeftFadingEdgeStrength() > 0.0f
   2533                         && textView.getRightFadingEdgeStrength() > 0.0f;
   2534             }
   2535         }.run();
   2536 
   2537         final float leftFadingEdgeStrength = textView.getLeftFadingEdgeStrength();
   2538         final float rightFadingEdgeStrength = textView.getRightFadingEdgeStrength();
   2539 
   2540         // wait for the marquee to continue
   2541         // the left fading becomes thicker while the right fading becomes thiner
   2542         // as the text moves towards left
   2543         new PollingCheck(TIMEOUT) {
   2544             @Override
   2545             protected boolean check() {
   2546                 return leftFadingEdgeStrength < textView.getLeftFadingEdgeStrength()
   2547                         && rightFadingEdgeStrength > textView.getRightFadingEdgeStrength();
   2548             }
   2549         }.run();
   2550         assertFalse(runnable.getIsSelected1());
   2551         assertTrue(runnable.getIsSelected2());
   2552 
   2553         runnable = new TestSelectedRunnable(textView) {
   2554             public void run() {
   2555                 textView.setMarqueeRepeatLimit(0);
   2556                 // force the marquee to stop
   2557                 saveIsSelected1();
   2558                 textView.setSelected(false);
   2559                 saveIsSelected2();
   2560                 textView.setGravity(Gravity.LEFT);
   2561             }
   2562         };
   2563         // force the marquee to stop
   2564         mActivity.runOnUiThread(runnable);
   2565         mInstrumentation.waitForIdleSync();
   2566         assertTrue(runnable.getIsSelected1());
   2567         assertFalse(runnable.getIsSelected2());
   2568         assertEquals(0.0f, textView.getLeftFadingEdgeStrength(), 0.01f);
   2569         assertTrue(textView.getRightFadingEdgeStrength() > 0.0f);
   2570 
   2571         mActivity.runOnUiThread(new Runnable() {
   2572             public void run() {
   2573                 textView.setGravity(Gravity.RIGHT);
   2574             }
   2575         });
   2576         mInstrumentation.waitForIdleSync();
   2577         assertTrue(textView.getLeftFadingEdgeStrength() > 0.0f);
   2578         assertEquals(0.0f, textView.getRightFadingEdgeStrength(), 0.01f);
   2579 
   2580         mActivity.runOnUiThread(new Runnable() {
   2581             public void run() {
   2582                 textView.setGravity(Gravity.CENTER_HORIZONTAL);
   2583             }
   2584         });
   2585         mInstrumentation.waitForIdleSync();
   2586         // there is no left fading (Is it correct?)
   2587         assertEquals(0.0f, textView.getLeftFadingEdgeStrength(), 0.01f);
   2588         assertTrue(textView.getRightFadingEdgeStrength() > 0.0f);
   2589     }
   2590 
   2591     public void testOnKeyMultiple() {
   2592         // Do not test. Implementation details.
   2593     }
   2594 
   2595     public void testAccessInputExtras() throws XmlPullParserException, IOException {
   2596         TextView textView = new TextView(mActivity);
   2597         textView.setText(null, BufferType.EDITABLE);
   2598         textView.setInputType(InputType.TYPE_CLASS_TEXT);
   2599 
   2600         // do not create the extras
   2601         assertNull(textView.getInputExtras(false));
   2602 
   2603         // create if it does not exist
   2604         Bundle inputExtras = textView.getInputExtras(true);
   2605         assertNotNull(inputExtras);
   2606         assertTrue(inputExtras.isEmpty());
   2607 
   2608         // it is created already
   2609         assertNotNull(textView.getInputExtras(false));
   2610 
   2611         try {
   2612             textView.setInputExtras(R.xml.input_extras);
   2613             fail("Should throw NullPointerException!");
   2614         } catch (NullPointerException e) {
   2615         }
   2616     }
   2617 
   2618     public void testAccessContentType() {
   2619         TextView textView = new TextView(mActivity);
   2620         textView.setText(null, BufferType.EDITABLE);
   2621         textView.setKeyListener(null);
   2622         textView.setTransformationMethod(null);
   2623 
   2624         textView.setInputType(InputType.TYPE_CLASS_DATETIME
   2625                 | InputType.TYPE_DATETIME_VARIATION_NORMAL);
   2626         assertEquals(InputType.TYPE_CLASS_DATETIME
   2627                 | InputType.TYPE_DATETIME_VARIATION_NORMAL, textView.getInputType());
   2628         assertTrue(textView.getKeyListener() instanceof DateTimeKeyListener);
   2629 
   2630         textView.setInputType(InputType.TYPE_CLASS_DATETIME
   2631                 | InputType.TYPE_DATETIME_VARIATION_DATE);
   2632         assertEquals(InputType.TYPE_CLASS_DATETIME
   2633                 | InputType.TYPE_DATETIME_VARIATION_DATE, textView.getInputType());
   2634         assertTrue(textView.getKeyListener() instanceof DateKeyListener);
   2635 
   2636         textView.setInputType(InputType.TYPE_CLASS_DATETIME
   2637                 | InputType.TYPE_DATETIME_VARIATION_TIME);
   2638         assertEquals(InputType.TYPE_CLASS_DATETIME
   2639                 | InputType.TYPE_DATETIME_VARIATION_TIME, textView.getInputType());
   2640         assertTrue(textView.getKeyListener() instanceof TimeKeyListener);
   2641 
   2642         textView.setInputType(InputType.TYPE_CLASS_NUMBER
   2643                 | InputType.TYPE_NUMBER_FLAG_DECIMAL
   2644                 | InputType.TYPE_NUMBER_FLAG_SIGNED);
   2645         assertEquals(InputType.TYPE_CLASS_NUMBER
   2646                 | InputType.TYPE_NUMBER_FLAG_DECIMAL
   2647                 | InputType.TYPE_NUMBER_FLAG_SIGNED, textView.getInputType());
   2648         assertSame(textView.getKeyListener(), DigitsKeyListener.getInstance(true, true));
   2649 
   2650         textView.setInputType(InputType.TYPE_CLASS_PHONE);
   2651         assertEquals(InputType.TYPE_CLASS_PHONE, textView.getInputType());
   2652         assertTrue(textView.getKeyListener() instanceof DialerKeyListener);
   2653 
   2654         textView.setInputType(InputType.TYPE_CLASS_TEXT
   2655                 | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);
   2656         assertEquals(InputType.TYPE_CLASS_TEXT
   2657                 | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT, textView.getInputType());
   2658         assertSame(textView.getKeyListener(), TextKeyListener.getInstance(true, Capitalize.NONE));
   2659 
   2660         textView.setSingleLine();
   2661         assertTrue(textView.getTransformationMethod() instanceof SingleLineTransformationMethod);
   2662         textView.setInputType(InputType.TYPE_CLASS_TEXT
   2663                 | InputType.TYPE_TEXT_FLAG_MULTI_LINE
   2664                 | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
   2665         assertEquals(InputType.TYPE_CLASS_TEXT
   2666                 | InputType.TYPE_TEXT_FLAG_MULTI_LINE
   2667                 | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS, textView.getInputType());
   2668         assertSame(textView.getKeyListener(),
   2669                 TextKeyListener.getInstance(false, Capitalize.CHARACTERS));
   2670         assertNull(textView.getTransformationMethod());
   2671 
   2672         textView.setInputType(InputType.TYPE_CLASS_TEXT
   2673                 | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
   2674         assertEquals(InputType.TYPE_CLASS_TEXT
   2675                 | InputType.TYPE_TEXT_FLAG_CAP_WORDS, textView.getInputType());
   2676         assertSame(textView.getKeyListener(),
   2677                 TextKeyListener.getInstance(false, Capitalize.WORDS));
   2678         assertTrue(textView.getTransformationMethod() instanceof SingleLineTransformationMethod);
   2679 
   2680         textView.setInputType(InputType.TYPE_CLASS_TEXT
   2681                 | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
   2682         assertEquals(InputType.TYPE_CLASS_TEXT
   2683                 | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES, textView.getInputType());
   2684         assertSame(textView.getKeyListener(),
   2685                 TextKeyListener.getInstance(false, Capitalize.SENTENCES));
   2686 
   2687         textView.setInputType(InputType.TYPE_NULL);
   2688         assertEquals(InputType.TYPE_NULL, textView.getInputType());
   2689         assertTrue(textView.getKeyListener() instanceof TextKeyListener);
   2690     }
   2691 
   2692     public void testAccessRawContentType() {
   2693         TextView textView = new TextView(mActivity);
   2694         textView.setText(null, BufferType.EDITABLE);
   2695         textView.setKeyListener(null);
   2696         textView.setTransformationMethod(null);
   2697 
   2698         textView.setRawInputType(InputType.TYPE_CLASS_DATETIME
   2699                 | InputType.TYPE_DATETIME_VARIATION_NORMAL);
   2700         assertEquals(InputType.TYPE_CLASS_DATETIME
   2701                 | InputType.TYPE_DATETIME_VARIATION_NORMAL, textView.getInputType());
   2702         assertNull(textView.getTransformationMethod());
   2703         assertNull(textView.getKeyListener());
   2704 
   2705         textView.setRawInputType(InputType.TYPE_CLASS_DATETIME
   2706                 | InputType.TYPE_DATETIME_VARIATION_DATE);
   2707         assertEquals(InputType.TYPE_CLASS_DATETIME
   2708                 | InputType.TYPE_DATETIME_VARIATION_DATE, textView.getInputType());
   2709         assertNull(textView.getTransformationMethod());
   2710         assertNull(textView.getKeyListener());
   2711 
   2712         textView.setRawInputType(InputType.TYPE_CLASS_DATETIME
   2713                 | InputType.TYPE_DATETIME_VARIATION_TIME);
   2714         assertEquals(InputType.TYPE_CLASS_DATETIME
   2715                 | InputType.TYPE_DATETIME_VARIATION_TIME, textView.getInputType());
   2716         assertNull(textView.getTransformationMethod());
   2717         assertNull(textView.getKeyListener());
   2718 
   2719         textView.setRawInputType(InputType.TYPE_CLASS_NUMBER
   2720                 | InputType.TYPE_NUMBER_FLAG_DECIMAL
   2721                 | InputType.TYPE_NUMBER_FLAG_SIGNED);
   2722         assertEquals(InputType.TYPE_CLASS_NUMBER
   2723                 | InputType.TYPE_NUMBER_FLAG_DECIMAL
   2724                 | InputType.TYPE_NUMBER_FLAG_SIGNED, textView.getInputType());
   2725         assertNull(textView.getTransformationMethod());
   2726         assertNull(textView.getKeyListener());
   2727 
   2728         textView.setRawInputType(InputType.TYPE_CLASS_PHONE);
   2729         assertEquals(InputType.TYPE_CLASS_PHONE, textView.getInputType());
   2730         assertNull(textView.getTransformationMethod());
   2731         assertNull(textView.getKeyListener());
   2732 
   2733         textView.setRawInputType(InputType.TYPE_CLASS_TEXT
   2734                 | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);
   2735         assertEquals(InputType.TYPE_CLASS_TEXT
   2736                 | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT, textView.getInputType());
   2737         assertNull(textView.getTransformationMethod());
   2738         assertNull(textView.getKeyListener());
   2739 
   2740         textView.setSingleLine();
   2741         assertTrue(textView.getTransformationMethod() instanceof SingleLineTransformationMethod);
   2742         textView.setRawInputType(InputType.TYPE_CLASS_TEXT
   2743                 | InputType.TYPE_TEXT_FLAG_MULTI_LINE
   2744                 | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
   2745         assertEquals(InputType.TYPE_CLASS_TEXT
   2746                 | InputType.TYPE_TEXT_FLAG_MULTI_LINE
   2747                 | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS, textView.getInputType());
   2748         assertTrue(textView.getTransformationMethod() instanceof SingleLineTransformationMethod);
   2749         assertNull(textView.getKeyListener());
   2750 
   2751         textView.setRawInputType(InputType.TYPE_CLASS_TEXT
   2752                 | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
   2753         assertEquals(InputType.TYPE_CLASS_TEXT
   2754                 | InputType.TYPE_TEXT_FLAG_CAP_WORDS, textView.getInputType());
   2755         assertTrue(textView.getTransformationMethod() instanceof SingleLineTransformationMethod);
   2756         assertNull(textView.getKeyListener());
   2757 
   2758         textView.setRawInputType(InputType.TYPE_CLASS_TEXT
   2759                 | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
   2760         assertEquals(InputType.TYPE_CLASS_TEXT
   2761                 | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES, textView.getInputType());
   2762         assertTrue(textView.getTransformationMethod() instanceof SingleLineTransformationMethod);
   2763         assertNull(textView.getKeyListener());
   2764 
   2765         textView.setRawInputType(InputType.TYPE_NULL);
   2766         assertTrue(textView.getTransformationMethod() instanceof SingleLineTransformationMethod);
   2767         assertNull(textView.getKeyListener());
   2768     }
   2769 
   2770     public void testOnPrivateIMECommand() {
   2771         // Do not test. Implementation details.
   2772     }
   2773 
   2774     public void testFoo() {
   2775         // Do not test. Implementation details.
   2776     }
   2777 
   2778     public void testVerifyDrawable() {
   2779         MockTextView textView = new MockTextView(mActivity);
   2780 
   2781         Drawable d = getDrawable(R.drawable.pass);
   2782         assertFalse(textView.verifyDrawable(d));
   2783 
   2784         textView.setCompoundDrawables(null, d, null, null);
   2785         assertTrue(textView.verifyDrawable(d));
   2786     }
   2787 
   2788     public void testAccessPrivateImeOptions() {
   2789         mTextView = findTextView(R.id.textview_text);
   2790         assertNull(mTextView.getPrivateImeOptions());
   2791 
   2792         mTextView.setPrivateImeOptions("com.example.myapp.SpecialMode=3");
   2793         assertEquals("com.example.myapp.SpecialMode=3", mTextView.getPrivateImeOptions());
   2794 
   2795         mTextView.setPrivateImeOptions(null);
   2796         assertNull(mTextView.getPrivateImeOptions());
   2797     }
   2798 
   2799     public void testSetOnEditorActionListener() {
   2800         mTextView = findTextView(R.id.textview_text);
   2801 
   2802         MockOnEditorActionListener listener = new MockOnEditorActionListener();
   2803         assertFalse(listener.isOnEditorActionCalled());
   2804 
   2805         mTextView.setOnEditorActionListener(listener);
   2806         assertFalse(listener.isOnEditorActionCalled());
   2807 
   2808         mTextView.onEditorAction(EditorInfo.IME_ACTION_DONE);
   2809         assertTrue(listener.isOnEditorActionCalled());
   2810     }
   2811 
   2812     public void testAccessImeOptions() {
   2813         mTextView = findTextView(R.id.textview_text);
   2814         assertEquals(EditorInfo.IME_NULL, mTextView.getImeOptions());
   2815 
   2816         mTextView.setImeOptions(EditorInfo.IME_ACTION_GO);
   2817         assertEquals(EditorInfo.IME_ACTION_GO, mTextView.getImeOptions());
   2818 
   2819         mTextView.setImeOptions(EditorInfo.IME_ACTION_DONE);
   2820         assertEquals(EditorInfo.IME_ACTION_DONE, mTextView.getImeOptions());
   2821 
   2822         mTextView.setImeOptions(EditorInfo.IME_NULL);
   2823         assertEquals(EditorInfo.IME_NULL, mTextView.getImeOptions());
   2824     }
   2825 
   2826     public void testAccessImeActionLabel() {
   2827         mTextView = findTextView(R.id.textview_text);
   2828         assertNull(mTextView.getImeActionLabel());
   2829         assertEquals(0, mTextView.getImeActionId());
   2830 
   2831         mTextView.setImeActionLabel("pinyin", 1);
   2832         assertEquals("pinyin", mTextView.getImeActionLabel().toString());
   2833         assertEquals(1, mTextView.getImeActionId());
   2834     }
   2835 
   2836     @UiThreadTest
   2837     public void testSetTextLong() {
   2838         final int MAX_COUNT = 1 << 21;
   2839         char[] longText = new char[MAX_COUNT];
   2840         for (int n = 0; n < MAX_COUNT; n++) {
   2841             longText[n] = 'm';
   2842         }
   2843         mTextView = findTextView(R.id.textview_text);
   2844         mTextView.setText(new String(longText));
   2845     }
   2846 
   2847     @UiThreadTest
   2848     public void testSetExtractedText() {
   2849         mTextView = findTextView(R.id.textview_text);
   2850         assertEquals(mActivity.getResources().getString(R.string.text_view_hello),
   2851                 mTextView.getText().toString());
   2852 
   2853         ExtractedText et = new ExtractedText();
   2854         et.text = "test";
   2855 
   2856         mTextView.setExtractedText(et);
   2857         assertEquals("test", mTextView.getText().toString());
   2858     }
   2859 
   2860     public void testMoveCursorToVisibleOffset() throws Throwable {
   2861         mTextView = findTextView(R.id.textview_text);
   2862 
   2863         // not a spannable text
   2864         runTestOnUiThread(new Runnable() {
   2865             public void run() {
   2866                 assertFalse(mTextView.moveCursorToVisibleOffset());
   2867             }
   2868         });
   2869         mInstrumentation.waitForIdleSync();
   2870 
   2871         // a selection range
   2872         final String spannableText = "text";
   2873         mTextView = new TextView(mActivity);
   2874 
   2875         runTestOnUiThread(new Runnable() {
   2876             public void run() {
   2877                 mTextView.setText(spannableText, BufferType.SPANNABLE);
   2878             }
   2879         });
   2880         mInstrumentation.waitForIdleSync();
   2881         Selection.setSelection((Spannable) mTextView.getText(), 0, spannableText.length());
   2882 
   2883         assertEquals(0, mTextView.getSelectionStart());
   2884         assertEquals(spannableText.length(), mTextView.getSelectionEnd());
   2885         runTestOnUiThread(new Runnable() {
   2886             public void run() {
   2887                 assertFalse(mTextView.moveCursorToVisibleOffset());
   2888             }
   2889         });
   2890         mInstrumentation.waitForIdleSync();
   2891 
   2892         // a spannable without range
   2893         runTestOnUiThread(new Runnable() {
   2894             public void run() {
   2895                 mTextView = findTextView(R.id.textview_text);
   2896                 mTextView.setText(spannableText, BufferType.SPANNABLE);
   2897             }
   2898         });
   2899         mInstrumentation.waitForIdleSync();
   2900 
   2901         runTestOnUiThread(new Runnable() {
   2902             public void run() {
   2903                 assertTrue(mTextView.moveCursorToVisibleOffset());
   2904             }
   2905         });
   2906         mInstrumentation.waitForIdleSync();
   2907     }
   2908 
   2909     public void testIsInputMethodTarget() throws Throwable {
   2910         mTextView = findTextView(R.id.textview_text);
   2911         assertFalse(mTextView.isInputMethodTarget());
   2912 
   2913         assertFalse(mTextView.isFocused());
   2914         runTestOnUiThread(new Runnable() {
   2915            @Override
   2916             public void run() {
   2917                mTextView.setFocusable(true);
   2918                mTextView.requestFocus();
   2919             }
   2920         });
   2921         mInstrumentation.waitForIdleSync();
   2922         assertTrue(mTextView.isFocused());
   2923 
   2924         new PollingCheck() {
   2925             @Override
   2926             protected boolean check() {
   2927                 return mTextView.isInputMethodTarget();
   2928             }
   2929         }.run();
   2930     }
   2931 
   2932     public void testBeginEndBatchEdit() {
   2933         mTextView = findTextView(R.id.textview_text);
   2934 
   2935         mTextView.beginBatchEdit();
   2936         mTextView.endBatchEdit();
   2937     }
   2938 
   2939     @UiThreadTest
   2940     public void testBringPointIntoView() throws Throwable {
   2941         mTextView = findTextView(R.id.textview_text);
   2942         assertFalse(mTextView.bringPointIntoView(1));
   2943 
   2944         mTextView.layout(0, 0, 100, 100);
   2945         assertFalse(mTextView.bringPointIntoView(2));
   2946     }
   2947 
   2948     public void testCancelLongPress() {
   2949         mTextView = findTextView(R.id.textview_text);
   2950         TouchUtils.longClickView(this, mTextView);
   2951         mTextView.cancelLongPress();
   2952     }
   2953 
   2954     @UiThreadTest
   2955     public void testClearComposingText() {
   2956         mTextView = findTextView(R.id.textview_text);
   2957         mTextView.setText("Hello world!", BufferType.SPANNABLE);
   2958         Spannable text = (Spannable) mTextView.getText();
   2959 
   2960         assertEquals(-1, BaseInputConnection.getComposingSpanStart(text));
   2961         assertEquals(-1, BaseInputConnection.getComposingSpanStart(text));
   2962 
   2963         BaseInputConnection.setComposingSpans((Spannable) mTextView.getText());
   2964         assertEquals(0, BaseInputConnection.getComposingSpanStart(text));
   2965         assertEquals(0, BaseInputConnection.getComposingSpanStart(text));
   2966 
   2967         mTextView.clearComposingText();
   2968         assertEquals(-1, BaseInputConnection.getComposingSpanStart(text));
   2969         assertEquals(-1, BaseInputConnection.getComposingSpanStart(text));
   2970     }
   2971 
   2972     public void testComputeVerticalScrollExtent() {
   2973         MockTextView textView = new MockTextView(mActivity);
   2974         assertEquals(0, textView.computeVerticalScrollExtent());
   2975 
   2976         Drawable d = getDrawable(R.drawable.pass);
   2977         textView.setCompoundDrawables(null, d, null, d);
   2978 
   2979         assertEquals(0, textView.computeVerticalScrollExtent());
   2980     }
   2981 
   2982     @UiThreadTest
   2983     public void testDidTouchFocusSelect() {
   2984         mTextView = new EditText(mActivity);
   2985         assertFalse(mTextView.didTouchFocusSelect());
   2986 
   2987         mTextView.setFocusable(true);
   2988         mTextView.requestFocus();
   2989         assertTrue(mTextView.didTouchFocusSelect());
   2990     }
   2991 
   2992     public void testExtractText() {
   2993         mTextView = new TextView(mActivity);
   2994 
   2995         ExtractedTextRequest request = new ExtractedTextRequest();
   2996         ExtractedText outText = new ExtractedText();
   2997 
   2998         request.token = 0;
   2999         request.flags = 10;
   3000         request.hintMaxLines = 2;
   3001         request.hintMaxChars = 20;
   3002         assertTrue(mTextView.extractText(request, outText));
   3003 
   3004         mTextView = findTextView(R.id.textview_text);
   3005         assertTrue(mTextView.extractText(request, outText));
   3006 
   3007         assertEquals(mActivity.getResources().getString(R.string.text_view_hello),
   3008                 outText.text.toString());
   3009     }
   3010 
   3011     @UiThreadTest
   3012     public void testTextDirectionDefault() {
   3013         TextView tv = new TextView(mActivity);
   3014         assertEquals(View.TEXT_DIRECTION_INHERIT, tv.getRawTextDirection());
   3015     }
   3016 
   3017     @UiThreadTest
   3018     public void testSetGetTextDirection() {
   3019         TextView tv = new TextView(mActivity);
   3020 
   3021         tv.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG);
   3022         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getRawTextDirection());
   3023 
   3024         tv.setTextDirection(View.TEXT_DIRECTION_ANY_RTL);
   3025         assertEquals(View.TEXT_DIRECTION_ANY_RTL, tv.getRawTextDirection());
   3026 
   3027         tv.setTextDirection(View.TEXT_DIRECTION_INHERIT);
   3028         assertEquals(View.TEXT_DIRECTION_INHERIT, tv.getRawTextDirection());
   3029 
   3030         tv.setTextDirection(View.TEXT_DIRECTION_LTR);
   3031         assertEquals(View.TEXT_DIRECTION_LTR, tv.getRawTextDirection());
   3032 
   3033         tv.setTextDirection(View.TEXT_DIRECTION_RTL);
   3034         assertEquals(View.TEXT_DIRECTION_RTL, tv.getRawTextDirection());
   3035 
   3036         tv.setTextDirection(View.TEXT_DIRECTION_LOCALE);
   3037         assertEquals(View.TEXT_DIRECTION_LOCALE, tv.getRawTextDirection());
   3038     }
   3039 
   3040     @UiThreadTest
   3041     public void testGetResolvedTextDirectionLtr() {
   3042         TextView tv = new TextView(mActivity);
   3043         tv.setText("this is a test");
   3044 
   3045         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection());
   3046 
   3047         tv.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG);
   3048         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection());
   3049 
   3050         tv.setTextDirection(View.TEXT_DIRECTION_ANY_RTL);
   3051         assertEquals(View.TEXT_DIRECTION_ANY_RTL, tv.getTextDirection());
   3052 
   3053         tv.setTextDirection(View.TEXT_DIRECTION_INHERIT);
   3054         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection());
   3055 
   3056         tv.setTextDirection(View.TEXT_DIRECTION_LTR);
   3057         assertEquals(View.TEXT_DIRECTION_LTR, tv.getTextDirection());
   3058 
   3059         tv.setTextDirection(View.TEXT_DIRECTION_RTL);
   3060         assertEquals(View.TEXT_DIRECTION_RTL, tv.getTextDirection());
   3061 
   3062         tv.setTextDirection(View.TEXT_DIRECTION_LOCALE);
   3063         assertEquals(View.TEXT_DIRECTION_LOCALE, tv.getTextDirection());
   3064     }
   3065 
   3066     @UiThreadTest
   3067     public void testGetResolvedTextDirectionLtrWithInheritance() {
   3068         LinearLayout ll = new LinearLayout(mActivity);
   3069         ll.setTextDirection(View.TEXT_DIRECTION_ANY_RTL);
   3070 
   3071         TextView tv = new TextView(mActivity);
   3072         tv.setText("this is a test");
   3073         ll.addView(tv);
   3074 
   3075         tv.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG);
   3076         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection());
   3077 
   3078         tv.setTextDirection(View.TEXT_DIRECTION_ANY_RTL);
   3079         assertEquals(View.TEXT_DIRECTION_ANY_RTL, tv.getTextDirection());
   3080 
   3081         tv.setTextDirection(View.TEXT_DIRECTION_INHERIT);
   3082         assertEquals(View.TEXT_DIRECTION_ANY_RTL, tv.getTextDirection());
   3083 
   3084         tv.setTextDirection(View.TEXT_DIRECTION_LTR);
   3085         assertEquals(View.TEXT_DIRECTION_LTR, tv.getTextDirection());
   3086 
   3087         tv.setTextDirection(View.TEXT_DIRECTION_RTL);
   3088         assertEquals(View.TEXT_DIRECTION_RTL, tv.getTextDirection());
   3089 
   3090         tv.setTextDirection(View.TEXT_DIRECTION_LOCALE);
   3091         assertEquals(View.TEXT_DIRECTION_LOCALE, tv.getTextDirection());
   3092     }
   3093 
   3094     @UiThreadTest
   3095     public void testGetResolvedTextDirectionRtl() {
   3096         TextView tv = new TextView(mActivity);
   3097         tv.setText("\u05DD\u05DE"); // hebrew
   3098 
   3099         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection());
   3100 
   3101         tv.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG);
   3102         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection());
   3103 
   3104         tv.setTextDirection(View.TEXT_DIRECTION_ANY_RTL);
   3105         assertEquals(View.TEXT_DIRECTION_ANY_RTL, tv.getTextDirection());
   3106 
   3107         tv.setTextDirection(View.TEXT_DIRECTION_INHERIT);
   3108         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection());
   3109 
   3110         tv.setTextDirection(View.TEXT_DIRECTION_LTR);
   3111         assertEquals(View.TEXT_DIRECTION_LTR, tv.getTextDirection());
   3112 
   3113         tv.setTextDirection(View.TEXT_DIRECTION_RTL);
   3114         assertEquals(View.TEXT_DIRECTION_RTL, tv.getTextDirection());
   3115 
   3116         tv.setTextDirection(View.TEXT_DIRECTION_LOCALE);
   3117         assertEquals(View.TEXT_DIRECTION_LOCALE, tv.getTextDirection());
   3118     }
   3119 
   3120     @UiThreadTest
   3121     public void testGetResolvedTextDirectionRtlWithInheritance() {
   3122         LinearLayout ll = new LinearLayout(mActivity);
   3123         ll.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG);
   3124 
   3125         TextView tv = new TextView(mActivity);
   3126         tv.setText("\u05DD\u05DE"); // hebrew
   3127         ll.addView(tv);
   3128 
   3129         tv.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG);
   3130         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection());
   3131 
   3132         tv.setTextDirection(View.TEXT_DIRECTION_ANY_RTL);
   3133         assertEquals(View.TEXT_DIRECTION_ANY_RTL, tv.getTextDirection());
   3134 
   3135         tv.setTextDirection(View.TEXT_DIRECTION_INHERIT);
   3136         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection());
   3137 
   3138         tv.setTextDirection(View.TEXT_DIRECTION_LTR);
   3139         assertEquals(View.TEXT_DIRECTION_LTR, tv.getTextDirection());
   3140 
   3141         tv.setTextDirection(View.TEXT_DIRECTION_RTL);
   3142         assertEquals(View.TEXT_DIRECTION_RTL, tv.getTextDirection());
   3143 
   3144         tv.setTextDirection(View.TEXT_DIRECTION_LOCALE);
   3145         assertEquals(View.TEXT_DIRECTION_LOCALE, tv.getTextDirection());
   3146 
   3147         // Force to RTL text direction on the layout
   3148         ll.setTextDirection(View.TEXT_DIRECTION_RTL);
   3149 
   3150         tv.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG);
   3151         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection());
   3152 
   3153         tv.setTextDirection(View.TEXT_DIRECTION_ANY_RTL);
   3154         assertEquals(View.TEXT_DIRECTION_ANY_RTL, tv.getTextDirection());
   3155 
   3156         tv.setTextDirection(View.TEXT_DIRECTION_INHERIT);
   3157         assertEquals(View.TEXT_DIRECTION_RTL, tv.getTextDirection());
   3158 
   3159         tv.setTextDirection(View.TEXT_DIRECTION_LTR);
   3160         assertEquals(View.TEXT_DIRECTION_LTR, tv.getTextDirection());
   3161 
   3162         tv.setTextDirection(View.TEXT_DIRECTION_RTL);
   3163         assertEquals(View.TEXT_DIRECTION_RTL, tv.getTextDirection());
   3164 
   3165         tv.setTextDirection(View.TEXT_DIRECTION_LOCALE);
   3166         assertEquals(View.TEXT_DIRECTION_LOCALE, tv.getTextDirection());
   3167     }
   3168 
   3169     @UiThreadTest
   3170     public void testResetTextDirection() {
   3171         LinearLayout ll = (LinearLayout) mActivity.findViewById(R.id.layout_textviewtest);
   3172         TextView tv = (TextView) mActivity.findViewById(R.id.textview_rtl);
   3173 
   3174         ll.setTextDirection(View.TEXT_DIRECTION_RTL);
   3175         tv.setTextDirection(View.TEXT_DIRECTION_INHERIT);
   3176         assertEquals(View.TEXT_DIRECTION_RTL, tv.getTextDirection());
   3177 
   3178         // No reset when we remove the view
   3179         ll.removeView(tv);
   3180         assertEquals(View.TEXT_DIRECTION_RTL, tv.getTextDirection());
   3181 
   3182         // Reset is done when we add the view
   3183         ll.addView(tv);
   3184         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection());
   3185     }
   3186 
   3187     @UiThreadTest
   3188     public void testTextAlignmentDefault() {
   3189         TextView tv = new TextView(getActivity());
   3190         assertEquals(View.TEXT_ALIGNMENT_GRAVITY, tv.getRawTextAlignment());
   3191         // resolved default text alignment is GRAVITY
   3192         assertEquals(View.TEXT_ALIGNMENT_GRAVITY, tv.getTextAlignment());
   3193     }
   3194 
   3195     @UiThreadTest
   3196     public void testSetGetTextAlignment() {
   3197         TextView tv = new TextView(getActivity());
   3198 
   3199         tv.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
   3200         assertEquals(View.TEXT_ALIGNMENT_GRAVITY, tv.getRawTextAlignment());
   3201 
   3202         tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
   3203         assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getRawTextAlignment());
   3204 
   3205         tv.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
   3206         assertEquals(View.TEXT_ALIGNMENT_TEXT_START, tv.getRawTextAlignment());
   3207 
   3208         tv.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END);
   3209         assertEquals(View.TEXT_ALIGNMENT_TEXT_END, tv.getRawTextAlignment());
   3210 
   3211         tv.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
   3212         assertEquals(View.TEXT_ALIGNMENT_VIEW_START, tv.getRawTextAlignment());
   3213 
   3214         tv.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
   3215         assertEquals(View.TEXT_ALIGNMENT_VIEW_END, tv.getRawTextAlignment());
   3216     }
   3217 
   3218     @UiThreadTest
   3219     public void testGetResolvedTextAlignment() {
   3220         TextView tv = new TextView(getActivity());
   3221 
   3222         assertEquals(View.TEXT_ALIGNMENT_GRAVITY, tv.getTextAlignment());
   3223 
   3224         // Test center alignment first so that we dont hit the default case
   3225         tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
   3226         assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment());
   3227 
   3228         // Test the default case too
   3229         tv.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
   3230         assertEquals(View.TEXT_ALIGNMENT_GRAVITY, tv.getTextAlignment());
   3231 
   3232         tv.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
   3233         assertEquals(View.TEXT_ALIGNMENT_TEXT_START, tv.getTextAlignment());
   3234 
   3235         tv.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END);
   3236         assertEquals(View.TEXT_ALIGNMENT_TEXT_END, tv.getTextAlignment());
   3237 
   3238         tv.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
   3239         assertEquals(View.TEXT_ALIGNMENT_VIEW_START, tv.getTextAlignment());
   3240 
   3241         tv.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
   3242         assertEquals(View.TEXT_ALIGNMENT_VIEW_END, tv.getTextAlignment());
   3243     }
   3244 
   3245     @UiThreadTest
   3246     public void testGetResolvedTextAlignmentWithInheritance() {
   3247         LinearLayout ll = new LinearLayout(getActivity());
   3248         ll.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
   3249 
   3250         TextView tv = new TextView(getActivity());
   3251         ll.addView(tv);
   3252 
   3253         // check defaults
   3254         assertEquals(View.TEXT_ALIGNMENT_GRAVITY, tv.getRawTextAlignment());
   3255         assertEquals(View.TEXT_ALIGNMENT_GRAVITY, tv.getTextAlignment());
   3256 
   3257         // set inherit and check that child is following parent
   3258         tv.setTextAlignment(View.TEXT_ALIGNMENT_INHERIT);
   3259         assertEquals(View.TEXT_ALIGNMENT_INHERIT, tv.getRawTextAlignment());
   3260 
   3261         ll.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
   3262         assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment());
   3263 
   3264         ll.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
   3265         assertEquals(View.TEXT_ALIGNMENT_TEXT_START, tv.getTextAlignment());
   3266 
   3267         ll.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END);
   3268         assertEquals(View.TEXT_ALIGNMENT_TEXT_END, tv.getTextAlignment());
   3269 
   3270         ll.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
   3271         assertEquals(View.TEXT_ALIGNMENT_VIEW_START, tv.getTextAlignment());
   3272 
   3273         ll.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
   3274         assertEquals(View.TEXT_ALIGNMENT_VIEW_END, tv.getTextAlignment());
   3275 
   3276         // now get rid of the inheritance but still change the parent
   3277         tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
   3278 
   3279         ll.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
   3280         assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment());
   3281 
   3282         ll.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
   3283         assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment());
   3284 
   3285         ll.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END);
   3286         assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment());
   3287 
   3288         ll.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
   3289         assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment());
   3290 
   3291         ll.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
   3292         assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment());
   3293     }
   3294 
   3295     @UiThreadTest
   3296     public void testResetTextAlignment() {
   3297         TextViewCtsActivity activity = getActivity();
   3298 
   3299         LinearLayout ll = (LinearLayout) activity.findViewById(R.id.layout_textviewtest);
   3300         TextView tv = (TextView) activity.findViewById(R.id.textview_rtl);
   3301 
   3302         ll.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
   3303         tv.setTextAlignment(View.TEXT_ALIGNMENT_INHERIT);
   3304         assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment());
   3305 
   3306         // No reset when we remove the view
   3307         ll.removeView(tv);
   3308         assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment());
   3309 
   3310         // Reset is done when we add the view
   3311         // Default text alignment is GRAVITY
   3312         ll.addView(tv);
   3313         assertEquals(View.TEXT_ALIGNMENT_GRAVITY, tv.getTextAlignment());
   3314     }
   3315 
   3316     @UiThreadTest
   3317     public void testDrawableResolution() {
   3318         final int LEFT = 0;
   3319         final int TOP = 1;
   3320         final int RIGHT = 2;
   3321         final int BOTTOM = 3;
   3322 
   3323         TextViewCtsActivity activity = getActivity();
   3324 
   3325         // Case 1.1: left / right drawable defined in default LTR mode
   3326         TextView tv = (TextView) activity.findViewById(R.id.textview_drawable_1_1);
   3327         Drawable[] drawables = tv.getCompoundDrawables();
   3328 
   3329         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3330                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3331         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3332                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3333         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green),
   3334                 ((BitmapDrawable) drawables[TOP]).getBitmap());
   3335         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3336                 ((BitmapDrawable) drawables[BOTTOM]).getBitmap());
   3337 
   3338         // Case 1.2: left / right drawable defined in default RTL mode
   3339         tv = (TextView) activity.findViewById(R.id.textview_drawable_1_2);
   3340         drawables = tv.getCompoundDrawables();
   3341 
   3342         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3343                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3344         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3345                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3346         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green),
   3347                 ((BitmapDrawable) drawables[TOP]).getBitmap());
   3348         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3349                 ((BitmapDrawable) drawables[BOTTOM]).getBitmap());
   3350 
   3351         // Case 2.1: start / end drawable defined in LTR mode
   3352         tv = (TextView) activity.findViewById(R.id.textview_drawable_2_1);
   3353         drawables = tv.getCompoundDrawables();
   3354 
   3355         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3356                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3357         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3358                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3359         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green),
   3360                 ((BitmapDrawable) drawables[TOP]).getBitmap());
   3361         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3362                 ((BitmapDrawable) drawables[BOTTOM]).getBitmap());
   3363 
   3364         // Case 2.2: start / end drawable defined in RTL mode
   3365         tv = (TextView) activity.findViewById(R.id.textview_drawable_2_2);
   3366         drawables = tv.getCompoundDrawables();
   3367 
   3368         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3369                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3370         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3371                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3372         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green),
   3373                 ((BitmapDrawable) drawables[TOP]).getBitmap());
   3374         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3375                 ((BitmapDrawable) drawables[BOTTOM]).getBitmap());
   3376 
   3377         // Case 3.1: left / right / start / end drawable defined in LTR mode
   3378         tv = (TextView) activity.findViewById(R.id.textview_drawable_3_1);
   3379         drawables = tv.getCompoundDrawables();
   3380 
   3381         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3382                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3383         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3384                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3385         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green),
   3386                 ((BitmapDrawable) drawables[TOP]).getBitmap());
   3387         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3388                 ((BitmapDrawable) drawables[BOTTOM]).getBitmap());
   3389 
   3390         // Case 3.2: left / right / start / end drawable defined in RTL mode
   3391         tv = (TextView) activity.findViewById(R.id.textview_drawable_3_2);
   3392         drawables = tv.getCompoundDrawables();
   3393 
   3394         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3395                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3396         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3397                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3398         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green),
   3399                 ((BitmapDrawable) drawables[TOP]).getBitmap());
   3400         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3401                 ((BitmapDrawable) drawables[BOTTOM]).getBitmap());
   3402 
   3403         // Case 4.1: start / end drawable defined in LTR mode inside a layout
   3404         // that defines the layout direction
   3405         tv = (TextView) activity.findViewById(R.id.textview_drawable_4_1);
   3406         drawables = tv.getCompoundDrawables();
   3407 
   3408         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3409                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3410         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3411                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3412         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green),
   3413                 ((BitmapDrawable) drawables[TOP]).getBitmap());
   3414         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3415                 ((BitmapDrawable) drawables[BOTTOM]).getBitmap());
   3416 
   3417         // Case 4.2: start / end drawable defined in RTL mode inside a layout
   3418         // that defines the layout direction
   3419         tv = (TextView) activity.findViewById(R.id.textview_drawable_4_2);
   3420         drawables = tv.getCompoundDrawables();
   3421 
   3422         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3423                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3424         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3425                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3426         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green),
   3427                 ((BitmapDrawable) drawables[TOP]).getBitmap());
   3428         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3429                 ((BitmapDrawable) drawables[BOTTOM]).getBitmap());
   3430 
   3431         // Case 5.1: left / right / start / end drawable defined in LTR mode inside a layout
   3432         // that defines the layout direction
   3433         tv = (TextView) activity.findViewById(R.id.textview_drawable_3_1);
   3434         drawables = tv.getCompoundDrawables();
   3435 
   3436         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3437                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3438         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3439                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3440         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green),
   3441                 ((BitmapDrawable) drawables[TOP]).getBitmap());
   3442         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3443                 ((BitmapDrawable) drawables[BOTTOM]).getBitmap());
   3444 
   3445         // Case 5.2: left / right / start / end drawable defined in RTL mode inside a layout
   3446         // that defines the layout direction
   3447         tv = (TextView) activity.findViewById(R.id.textview_drawable_3_2);
   3448         drawables = tv.getCompoundDrawables();
   3449 
   3450         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3451                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3452         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3453                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3454         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green),
   3455                 ((BitmapDrawable) drawables[TOP]).getBitmap());
   3456         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3457                 ((BitmapDrawable) drawables[BOTTOM]).getBitmap());
   3458     }
   3459 
   3460     @UiThreadTest
   3461     public void testDrawableResolution2() {
   3462         final int LEFT = 0;
   3463         final int TOP = 1;
   3464         final int RIGHT = 2;
   3465         final int BOTTOM = 3;
   3466 
   3467         TextViewCtsActivity activity = getActivity();
   3468 
   3469         // Case 1.1: left / right drawable defined in default LTR mode
   3470         TextView tv = (TextView) activity.findViewById(R.id.textview_drawable_1_1);
   3471         Drawable[] drawables = tv.getCompoundDrawables();
   3472 
   3473         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3474                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3475         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3476                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3477         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green),
   3478                 ((BitmapDrawable) drawables[TOP]).getBitmap());
   3479         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3480                 ((BitmapDrawable) drawables[BOTTOM]).getBitmap());
   3481 
   3482         tv.setCompoundDrawables(null, null, getDrawable(R.drawable.icon_yellow), null);
   3483         drawables = tv.getCompoundDrawables();
   3484 
   3485         assertNull(drawables[LEFT]);
   3486         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3487                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3488         assertNull(drawables[TOP]);
   3489         assertNull(drawables[BOTTOM]);
   3490 
   3491         tv = (TextView) activity.findViewById(R.id.textview_drawable_1_2);
   3492         drawables = tv.getCompoundDrawables();
   3493 
   3494         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3495                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3496         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3497                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3498         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green),
   3499                 ((BitmapDrawable) drawables[TOP]).getBitmap());
   3500         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3501                 ((BitmapDrawable) drawables[BOTTOM]).getBitmap());
   3502 
   3503         tv.setCompoundDrawables(getDrawable(R.drawable.icon_yellow), null, null, null);
   3504         drawables = tv.getCompoundDrawables();
   3505 
   3506         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3507                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3508         assertNull(drawables[RIGHT]);
   3509         assertNull(drawables[TOP]);
   3510         assertNull(drawables[BOTTOM]);
   3511 
   3512         tv = (TextView) activity.findViewById(R.id.textview_ltr);
   3513         drawables = tv.getCompoundDrawables();
   3514 
   3515         assertNull(drawables[LEFT]);
   3516         assertNull(drawables[RIGHT]);
   3517         assertNull(drawables[TOP]);
   3518         assertNull(drawables[BOTTOM]);
   3519 
   3520         tv.setCompoundDrawables(getDrawable(R.drawable.icon_blue), null, getDrawable(R.drawable.icon_red), null);
   3521         drawables = tv.getCompoundDrawables();
   3522 
   3523         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3524                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3525         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3526                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3527         assertNull(drawables[TOP]);
   3528         assertNull(drawables[BOTTOM]);
   3529 
   3530         tv.setCompoundDrawablesRelative(getDrawable(R.drawable.icon_yellow), null, null, null);
   3531         drawables = tv.getCompoundDrawables();
   3532 
   3533         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3534                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3535         assertNull(drawables[RIGHT]);
   3536         assertNull(drawables[TOP]);
   3537         assertNull(drawables[BOTTOM]);
   3538     }
   3539 
   3540     private static class MockOnEditorActionListener implements OnEditorActionListener {
   3541         private boolean isOnEditorActionCalled;
   3542 
   3543         public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
   3544             isOnEditorActionCalled = true;
   3545             return true;
   3546         }
   3547 
   3548         public boolean isOnEditorActionCalled() {
   3549             return isOnEditorActionCalled;
   3550         }
   3551     }
   3552 
   3553     private void layout(final TextView textView) {
   3554         mActivity.runOnUiThread(new Runnable() {
   3555             public void run() {
   3556                 mActivity.setContentView(textView);
   3557             }
   3558         });
   3559         mInstrumentation.waitForIdleSync();
   3560     }
   3561 
   3562     private void layout(final int layoutId) {
   3563         mActivity.runOnUiThread(new Runnable() {
   3564             public void run() {
   3565                 mActivity.setContentView(layoutId);
   3566             }
   3567         });
   3568         mInstrumentation.waitForIdleSync();
   3569     }
   3570 
   3571     private TextView findTextView(int id) {
   3572         return (TextView) mActivity.findViewById(id);
   3573     }
   3574 
   3575     private int getAutoLinkMask(int id) {
   3576         return findTextView(id).getAutoLinkMask();
   3577     }
   3578 
   3579     private Bitmap getBitmap(int resid) {
   3580         return ((BitmapDrawable) getDrawable(resid)).getBitmap();
   3581     }
   3582 
   3583     private Drawable getDrawable(int resid) {
   3584         return mActivity.getResources().getDrawable(resid);
   3585     }
   3586 
   3587     private void setMaxWidth(final int pixels) {
   3588         mActivity.runOnUiThread(new Runnable() {
   3589             public void run() {
   3590                 mTextView.setMaxWidth(pixels);
   3591             }
   3592         });
   3593         mInstrumentation.waitForIdleSync();
   3594     }
   3595 
   3596     private void setMinWidth(final int pixels) {
   3597         mActivity.runOnUiThread(new Runnable() {
   3598             public void run() {
   3599                 mTextView.setMinWidth(pixels);
   3600             }
   3601         });
   3602         mInstrumentation.waitForIdleSync();
   3603     }
   3604 
   3605     private void setMaxHeight(final int pixels) {
   3606         mActivity.runOnUiThread(new Runnable() {
   3607             public void run() {
   3608                 mTextView.setMaxHeight(pixels);
   3609             }
   3610         });
   3611         mInstrumentation.waitForIdleSync();
   3612     }
   3613 
   3614     private void setMinHeight(final int pixels) {
   3615         mActivity.runOnUiThread(new Runnable() {
   3616             public void run() {
   3617                 mTextView.setMinHeight(pixels);
   3618             }
   3619         });
   3620         mInstrumentation.waitForIdleSync();
   3621     }
   3622 
   3623     private void setMinLines(final int minlines) {
   3624         mActivity.runOnUiThread(new Runnable() {
   3625             public void run() {
   3626                 mTextView.setMinLines(minlines);
   3627             }
   3628         });
   3629         mInstrumentation.waitForIdleSync();
   3630     }
   3631 
   3632     /**
   3633      * Convenience for {@link TextView#setText(CharSequence, BufferType)}. And
   3634      * the buffer type is fixed to SPANNABLE.
   3635      *
   3636      * @param tv the text view
   3637      * @param content the content
   3638      */
   3639     private void setSpannableText(final TextView tv, final String content) {
   3640         mActivity.runOnUiThread(new Runnable() {
   3641             public void run() {
   3642                 tv.setText(content, BufferType.SPANNABLE);
   3643             }
   3644         });
   3645         mInstrumentation.waitForIdleSync();
   3646     }
   3647 
   3648     private void setLines(final int lines) {
   3649         mActivity.runOnUiThread(new Runnable() {
   3650             public void run() {
   3651                 mTextView.setLines(lines);
   3652             }
   3653         });
   3654         mInstrumentation.waitForIdleSync();
   3655     }
   3656 
   3657     private void setHorizontallyScrolling(final boolean whether) {
   3658         mActivity.runOnUiThread(new Runnable() {
   3659             public void run() {
   3660                 mTextView.setHorizontallyScrolling(whether);
   3661             }
   3662         });
   3663         mInstrumentation.waitForIdleSync();
   3664     }
   3665 
   3666     private void setWidth(final int pixels) {
   3667         mActivity.runOnUiThread(new Runnable() {
   3668             public void run() {
   3669                 mTextView.setWidth(pixels);
   3670             }
   3671         });
   3672         mInstrumentation.waitForIdleSync();
   3673     }
   3674 
   3675     private void setHeight(final int pixels) {
   3676         mActivity.runOnUiThread(new Runnable() {
   3677             public void run() {
   3678                 mTextView.setHeight(pixels);
   3679             }
   3680         });
   3681         mInstrumentation.waitForIdleSync();
   3682     }
   3683 
   3684     private void setMinEms(final int ems) {
   3685         mActivity.runOnUiThread(new Runnable() {
   3686             public void run() {
   3687                 mTextView.setMinEms(ems);
   3688             }
   3689         });
   3690         mInstrumentation.waitForIdleSync();
   3691     }
   3692 
   3693     private void setMaxEms(final int ems) {
   3694         mActivity.runOnUiThread(new Runnable() {
   3695             public void run() {
   3696                 mTextView.setMaxEms(ems);
   3697             }
   3698         });
   3699         mInstrumentation.waitForIdleSync();
   3700     }
   3701 
   3702     private void setEms(final int ems) {
   3703         mActivity.runOnUiThread(new Runnable() {
   3704             public void run() {
   3705                 mTextView.setEms(ems);
   3706             }
   3707         });
   3708         mInstrumentation.waitForIdleSync();
   3709     }
   3710 
   3711     private void setLineSpacing(final float add, final float mult) {
   3712         mActivity.runOnUiThread(new Runnable() {
   3713             public void run() {
   3714                 mTextView.setLineSpacing(add, mult);
   3715             }
   3716         });
   3717         mInstrumentation.waitForIdleSync();
   3718     }
   3719 
   3720     private static abstract class TestSelectedRunnable implements Runnable {
   3721         private TextView mTextView;
   3722         private boolean mIsSelected1;
   3723         private boolean mIsSelected2;
   3724 
   3725         public TestSelectedRunnable(TextView textview) {
   3726             mTextView = textview;
   3727         }
   3728 
   3729         public boolean getIsSelected1() {
   3730             return mIsSelected1;
   3731         }
   3732 
   3733         public boolean getIsSelected2() {
   3734             return mIsSelected2;
   3735         }
   3736 
   3737         public void saveIsSelected1() {
   3738             mIsSelected1 = mTextView.isSelected();
   3739         }
   3740 
   3741         public void saveIsSelected2() {
   3742             mIsSelected2 = mTextView.isSelected();
   3743         }
   3744     }
   3745 
   3746     private static abstract class TestLayoutRunnable implements Runnable {
   3747         private TextView mTextView;
   3748         private Layout mLayout;
   3749 
   3750         public TestLayoutRunnable(TextView textview) {
   3751             mTextView = textview;
   3752         }
   3753 
   3754         public Layout getLayout() {
   3755             return mLayout;
   3756         }
   3757 
   3758         public void saveLayout() {
   3759             mLayout = mTextView.getLayout();
   3760         }
   3761     }
   3762 
   3763     private class MockEditableFactory extends Editable.Factory {
   3764         private boolean mhasCalledNewEditable;
   3765         private CharSequence mSource;
   3766 
   3767         public boolean hasCalledNewEditable() {
   3768             return mhasCalledNewEditable;
   3769         }
   3770 
   3771         public void reset() {
   3772             mhasCalledNewEditable = false;
   3773             mSource = null;
   3774         }
   3775 
   3776         public CharSequence getSource() {
   3777             return mSource;
   3778         }
   3779 
   3780         @Override
   3781         public Editable newEditable(CharSequence source) {
   3782             mhasCalledNewEditable = true;
   3783             mSource = source;
   3784             return super.newEditable(source);
   3785         }
   3786     }
   3787 
   3788     private class MockSpannableFactory extends Spannable.Factory {
   3789         private boolean mHasCalledNewSpannable;
   3790         private CharSequence mSource;
   3791 
   3792         public boolean hasCalledNewSpannable() {
   3793             return mHasCalledNewSpannable;
   3794         }
   3795 
   3796         public void reset() {
   3797             mHasCalledNewSpannable = false;
   3798             mSource = null;
   3799         }
   3800 
   3801         public CharSequence getSource() {
   3802             return mSource;
   3803         }
   3804 
   3805         @Override
   3806         public Spannable newSpannable(CharSequence source) {
   3807             mHasCalledNewSpannable = true;
   3808             mSource = source;
   3809             return super.newSpannable(source);
   3810         }
   3811     }
   3812 
   3813     private static class MockTextWatcher implements TextWatcher {
   3814         private boolean mHasCalledAfterTextChanged;
   3815         private boolean mHasCalledBeforeTextChanged;
   3816         private boolean mHasOnTextChanged;
   3817 
   3818         public void reset(){
   3819             mHasCalledAfterTextChanged = false;
   3820             mHasCalledBeforeTextChanged = false;
   3821             mHasOnTextChanged = false;
   3822         }
   3823 
   3824         public boolean hasCalledAfterTextChanged() {
   3825             return mHasCalledAfterTextChanged;
   3826         }
   3827 
   3828         public boolean hasCalledBeforeTextChanged() {
   3829             return mHasCalledBeforeTextChanged;
   3830         }
   3831 
   3832         public boolean hasCalledOnTextChanged() {
   3833             return mHasOnTextChanged;
   3834         }
   3835 
   3836         public void afterTextChanged(Editable s) {
   3837             mHasCalledAfterTextChanged = true;
   3838         }
   3839 
   3840         public void beforeTextChanged(CharSequence s, int start, int count, int after) {
   3841             mHasCalledBeforeTextChanged = true;
   3842         }
   3843 
   3844         public void onTextChanged(CharSequence s, int start, int before, int count) {
   3845             mHasOnTextChanged = true;
   3846         }
   3847     }
   3848 
   3849     /**
   3850      * The listener interface for receiving mockOnLongClick events. The class
   3851      * that is interested in processing a mockOnLongClick event implements this
   3852      * interface, and the object created with that class is registered with a
   3853      * component using the component's
   3854      * <code>addMockOnLongClickListener<code> method. When
   3855      * the mockOnLongClick event occurs, that object's appropriate
   3856      * method is invoked.
   3857      *
   3858      * @see MockOnLongClickEvent
   3859      */
   3860     private static class MockOnLongClickListener implements OnLongClickListener {
   3861         private boolean mExpectedOnLongClickResult;
   3862         private boolean mHasLongClicked;
   3863 
   3864         MockOnLongClickListener(boolean result) {
   3865             mExpectedOnLongClickResult = result;
   3866         }
   3867 
   3868         public boolean hasLongClicked() {
   3869             return mHasLongClicked;
   3870         }
   3871 
   3872         public boolean onLongClick(View v) {
   3873             mHasLongClicked = true;
   3874             return mExpectedOnLongClickResult;
   3875         }
   3876     }
   3877 
   3878     /**
   3879      * The listener interface for receiving mockOnCreateContextMenu events. The
   3880      * class that is interested in processing a mockOnCreateContextMenu event
   3881      * implements this interface, and the object created with that class is
   3882      * registered with a component using the component's
   3883      * <code>addMockOnCreateContextMenuListener<code> method. When the
   3884      * mockOnCreateContextMenu event occurs, that object's appropriate method is
   3885      * invoked.
   3886      *
   3887      * @see MockOnCreateContextMenuEvent
   3888      */
   3889     private static class MockOnCreateContextMenuListener implements OnCreateContextMenuListener {
   3890         private boolean mIsMenuItemsBlank;
   3891         private boolean mHasCreatedContextMenu;
   3892 
   3893         MockOnCreateContextMenuListener(boolean isBlank) {
   3894             this.mIsMenuItemsBlank = isBlank;
   3895         }
   3896 
   3897         public boolean hasCreatedContextMenu() {
   3898             return mHasCreatedContextMenu;
   3899         }
   3900 
   3901         public void reset() {
   3902             mHasCreatedContextMenu = false;
   3903         }
   3904 
   3905         public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
   3906             mHasCreatedContextMenu = true;
   3907             if (!mIsMenuItemsBlank) {
   3908                 menu.add("menu item");
   3909             }
   3910         }
   3911     }
   3912 }
   3913