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 
   2050         int singleLineWidth = 0;
   2051         int singleLineHeight = 0;
   2052 
   2053         if (textView.getLayout() != null) {
   2054             singleLineWidth = textView.getLayout().getWidth();
   2055             singleLineHeight = textView.getLayout().getHeight();
   2056         }
   2057 
   2058         mActivity.runOnUiThread(new Runnable() {
   2059             public void run() {
   2060                 textView.setSingleLine(false);
   2061             }
   2062         });
   2063         mInstrumentation.waitForIdleSync();
   2064         assertEquals(null, textView.getTransformationMethod());
   2065 
   2066         if (textView.getLayout() != null) {
   2067             assertTrue(textView.getLayout().getHeight() > singleLineHeight);
   2068             assertTrue(textView.getLayout().getWidth() < singleLineWidth);
   2069         }
   2070 
   2071         // same behaviours as setSingLine(true)
   2072         mActivity.runOnUiThread(new Runnable() {
   2073             public void run() {
   2074                 textView.setSingleLine();
   2075             }
   2076         });
   2077         mInstrumentation.waitForIdleSync();
   2078         assertEquals(SingleLineTransformationMethod.getInstance(),
   2079                 textView.getTransformationMethod());
   2080 
   2081         if (textView.getLayout() != null) {
   2082             assertEquals(singleLineHeight, textView.getLayout().getHeight());
   2083             assertEquals(singleLineWidth, textView.getLayout().getWidth());
   2084         }
   2085     }
   2086 
   2087     @UiThreadTest
   2088     public void testSetMaxLines() {
   2089         mTextView = findTextView(R.id.textview_text);
   2090 
   2091         float[] widths = new float[LONG_TEXT.length()];
   2092         mTextView.getPaint().getTextWidths(LONG_TEXT, widths);
   2093         float totalWidth = 0.0f;
   2094         for (float f : widths) {
   2095             totalWidth += f;
   2096         }
   2097         final int stringWidth = (int) totalWidth;
   2098         mTextView.setWidth(stringWidth >> 2);
   2099         mTextView.setText(LONG_TEXT);
   2100 
   2101         final int maxLines = 2;
   2102         assertTrue(mTextView.getLineCount() > maxLines);
   2103 
   2104         mTextView.setMaxLines(maxLines);
   2105         mTextView.requestLayout();
   2106 
   2107         assertTrue(mTextView.getHeight() <= maxLines * mTextView.getLineHeight());
   2108     }
   2109 
   2110     @UiThreadTest
   2111     public void testSetMaxLinesException() {
   2112         mTextView = new TextView(mActivity);
   2113         mActivity.setContentView(mTextView);
   2114         mTextView.setWidth(mTextView.getWidth() >> 3);
   2115         mTextView.setMaxLines(-1);
   2116     }
   2117 
   2118     public void testSetMinLines() {
   2119         mTextView = findTextView(R.id.textview_text);
   2120         setWidth(mTextView.getWidth() >> 3);
   2121         int originalHeight = mTextView.getHeight();
   2122         int originalLines = mTextView.getLineCount();
   2123 
   2124         setMinLines(originalLines - 1);
   2125         assertTrue((originalLines - 1) * mTextView.getLineHeight() <= mTextView.getHeight());
   2126 
   2127         setMinLines(originalLines + 1);
   2128         assertTrue((originalLines + 1) * mTextView.getLineHeight() <= mTextView.getHeight());
   2129     }
   2130 
   2131     public void testSetLines() {
   2132         mTextView = findTextView(R.id.textview_text);
   2133         // make it multiple lines
   2134         setWidth(mTextView.getWidth() >> 3);
   2135         int originalLines = mTextView.getLineCount();
   2136 
   2137         setLines(originalLines - 1);
   2138         assertTrue((originalLines - 1) * mTextView.getLineHeight() <= mTextView.getHeight());
   2139 
   2140         setLines(originalLines + 1);
   2141         assertTrue((originalLines + 1) * mTextView.getLineHeight() <= mTextView.getHeight());
   2142     }
   2143 
   2144     @UiThreadTest
   2145     public void testSetLinesException() {
   2146         mTextView = new TextView(mActivity);
   2147         mActivity.setContentView(mTextView);
   2148         mTextView.setWidth(mTextView.getWidth() >> 3);
   2149         mTextView.setLines(-1);
   2150     }
   2151 
   2152     @UiThreadTest
   2153     public void testGetExtendedPaddingTop() {
   2154         mTextView = findTextView(R.id.textview_text);
   2155         // Initialized value
   2156         assertEquals(0, mTextView.getExtendedPaddingTop());
   2157 
   2158         // After Set a Drawable
   2159         final Drawable top = getDrawable(R.drawable.red);
   2160         top.setBounds(0, 0, 100, 10);
   2161         mTextView.setCompoundDrawables(null, top, null, null);
   2162         assertEquals(mTextView.getCompoundPaddingTop(), mTextView.getExtendedPaddingTop());
   2163 
   2164         // Change line count
   2165         mTextView.setLines(mTextView.getLineCount() - 1);
   2166         mTextView.setGravity(Gravity.BOTTOM);
   2167 
   2168         assertTrue(mTextView.getExtendedPaddingTop() > 0);
   2169     }
   2170 
   2171     @UiThreadTest
   2172     public void testGetExtendedPaddingBottom() {
   2173         mTextView = findTextView(R.id.textview_text);
   2174         // Initialized value
   2175         assertEquals(0, mTextView.getExtendedPaddingBottom());
   2176 
   2177         // After Set a Drawable
   2178         final Drawable bottom = getDrawable(R.drawable.red);
   2179         bottom.setBounds(0, 0, 100, 10);
   2180         mTextView.setCompoundDrawables(null, null, null, bottom);
   2181         assertEquals(mTextView.getCompoundPaddingBottom(), mTextView.getExtendedPaddingBottom());
   2182 
   2183         // Change line count
   2184         mTextView.setLines(mTextView.getLineCount() - 1);
   2185         mTextView.setGravity(Gravity.CENTER_VERTICAL);
   2186 
   2187         assertTrue(mTextView.getExtendedPaddingBottom() > 0);
   2188     }
   2189 
   2190     public void testGetTotalPaddingTop() {
   2191         mTextView = findTextView(R.id.textview_text);
   2192         // Initialized value
   2193         assertEquals(0, mTextView.getTotalPaddingTop());
   2194 
   2195         // After Set a Drawable
   2196         final Drawable top = getDrawable(R.drawable.red);
   2197         top.setBounds(0, 0, 100, 10);
   2198         mActivity.runOnUiThread(new Runnable() {
   2199             public void run() {
   2200                 mTextView.setCompoundDrawables(null, top, null, null);
   2201                 mTextView.setLines(mTextView.getLineCount() - 1);
   2202                 mTextView.setGravity(Gravity.BOTTOM);
   2203             }
   2204         });
   2205         mInstrumentation.waitForIdleSync();
   2206         assertEquals(mTextView.getExtendedPaddingTop(), mTextView.getTotalPaddingTop());
   2207 
   2208         // Change line count
   2209         setLines(mTextView.getLineCount() + 1);
   2210         int expected = mTextView.getHeight()
   2211                 - mTextView.getExtendedPaddingBottom()
   2212                 - mTextView.getLayout().getLineTop(mTextView.getLineCount());
   2213         assertEquals(expected, mTextView.getTotalPaddingTop());
   2214     }
   2215 
   2216     public void testGetTotalPaddingBottom() {
   2217         mTextView = findTextView(R.id.textview_text);
   2218         // Initialized value
   2219         assertEquals(0, mTextView.getTotalPaddingBottom());
   2220 
   2221         // After Set a Drawable
   2222         final Drawable bottom = getDrawable(R.drawable.red);
   2223         bottom.setBounds(0, 0, 100, 10);
   2224         mActivity.runOnUiThread(new Runnable() {
   2225             public void run() {
   2226                 mTextView.setCompoundDrawables(null, null, null, bottom);
   2227                 mTextView.setLines(mTextView.getLineCount() - 1);
   2228                 mTextView.setGravity(Gravity.CENTER_VERTICAL);
   2229             }
   2230         });
   2231         mInstrumentation.waitForIdleSync();
   2232         assertEquals(mTextView.getExtendedPaddingBottom(), mTextView.getTotalPaddingBottom());
   2233 
   2234         // Change line count
   2235         setLines(mTextView.getLineCount() + 1);
   2236         int expected = ((mTextView.getHeight()
   2237                 - mTextView.getExtendedPaddingBottom()
   2238                 - mTextView.getExtendedPaddingTop()
   2239                 - mTextView.getLayout().getLineBottom(mTextView.getLineCount())) >> 1)
   2240                 + mTextView.getExtendedPaddingBottom();
   2241         assertEquals(expected, mTextView.getTotalPaddingBottom());
   2242     }
   2243 
   2244     @UiThreadTest
   2245     public void testGetTotalPaddingLeft() {
   2246         mTextView = findTextView(R.id.textview_text);
   2247         // Initialized value
   2248         assertEquals(0, mTextView.getTotalPaddingLeft());
   2249 
   2250         // After Set a Drawable
   2251         Drawable left = getDrawable(R.drawable.red);
   2252         left.setBounds(0, 0, 10, 100);
   2253         mTextView.setCompoundDrawables(left, null, null, null);
   2254         mTextView.setGravity(Gravity.RIGHT);
   2255         assertEquals(mTextView.getCompoundPaddingLeft(), mTextView.getTotalPaddingLeft());
   2256 
   2257         // Change width
   2258         mTextView.setWidth(Integer.MAX_VALUE);
   2259         assertEquals(mTextView.getCompoundPaddingLeft(), mTextView.getTotalPaddingLeft());
   2260     }
   2261 
   2262     @UiThreadTest
   2263     public void testGetTotalPaddingRight() {
   2264         mTextView = findTextView(R.id.textview_text);
   2265         // Initialized value
   2266         assertEquals(0, mTextView.getTotalPaddingRight());
   2267 
   2268         // After Set a Drawable
   2269         Drawable right = getDrawable(R.drawable.red);
   2270         right.setBounds(0, 0, 10, 100);
   2271         mTextView.setCompoundDrawables(null, null, right, null);
   2272         mTextView.setGravity(Gravity.CENTER_HORIZONTAL);
   2273         assertEquals(mTextView.getCompoundPaddingRight(), mTextView.getTotalPaddingRight());
   2274 
   2275         // Change width
   2276         mTextView.setWidth(Integer.MAX_VALUE);
   2277         assertEquals(mTextView.getCompoundPaddingRight(), mTextView.getTotalPaddingRight());
   2278     }
   2279 
   2280     public void testGetUrls() {
   2281         mTextView = new TextView(mActivity);
   2282 
   2283         URLSpan[] spans = mTextView.getUrls();
   2284         assertEquals(0, spans.length);
   2285 
   2286         String url = "http://www.google.com";
   2287         String email = "name (at) gmail.com";
   2288         String string = url + " mailto:" + email;
   2289         SpannableString spannable = new SpannableString(string);
   2290         spannable.setSpan(new URLSpan(url), 0, url.length(), 0);
   2291         mTextView.setText(spannable, BufferType.SPANNABLE);
   2292         spans = mTextView.getUrls();
   2293         assertEquals(1, spans.length);
   2294         assertEquals(url, spans[0].getURL());
   2295 
   2296         spannable.setSpan(new URLSpan(email), 0, email.length(), 0);
   2297         mTextView.setText(spannable, BufferType.SPANNABLE);
   2298 
   2299         spans = mTextView.getUrls();
   2300         assertEquals(2, spans.length);
   2301         assertEquals(url, spans[0].getURL());
   2302         assertEquals(email, spans[1].getURL());
   2303 
   2304         // test the situation that param what is not a URLSpan
   2305         spannable.setSpan(new Object(), 0, 9, 0);
   2306         mTextView.setText(spannable, BufferType.SPANNABLE);
   2307         spans = mTextView.getUrls();
   2308         assertEquals(2, spans.length);
   2309     }
   2310 
   2311     public void testSetPadding() {
   2312         mTextView = new TextView(mActivity);
   2313 
   2314         mTextView.setPadding(0, 1, 2, 4);
   2315         assertEquals(0, mTextView.getPaddingLeft());
   2316         assertEquals(1, mTextView.getPaddingTop());
   2317         assertEquals(2, mTextView.getPaddingRight());
   2318         assertEquals(4, mTextView.getPaddingBottom());
   2319 
   2320         mTextView.setPadding(10, 20, 30, 40);
   2321         assertEquals(10, mTextView.getPaddingLeft());
   2322         assertEquals(20, mTextView.getPaddingTop());
   2323         assertEquals(30, mTextView.getPaddingRight());
   2324         assertEquals(40, mTextView.getPaddingBottom());
   2325     }
   2326 
   2327     public void testSetTextAppearance() {
   2328         mTextView = new TextView(mActivity);
   2329 
   2330         mTextView.setTextAppearance(mActivity, R.style.TextAppearance_All);
   2331         assertEquals(mActivity.getResources().getColor(R.drawable.black),
   2332                 mTextView.getCurrentTextColor());
   2333         assertEquals(20f, mTextView.getTextSize(), 0.01f);
   2334         assertEquals(Typeface.BOLD, mTextView.getTypeface().getStyle());
   2335         assertEquals(mActivity.getResources().getColor(R.drawable.red),
   2336                 mTextView.getCurrentHintTextColor());
   2337         assertEquals(mActivity.getResources().getColor(R.drawable.blue),
   2338                 mTextView.getLinkTextColors().getDefaultColor());
   2339 
   2340         mTextView.setTextAppearance(mActivity, R.style.TextAppearance_Colors);
   2341         assertEquals(mActivity.getResources().getColor(R.drawable.black),
   2342                 mTextView.getCurrentTextColor());
   2343         assertEquals(mActivity.getResources().getColor(R.drawable.blue),
   2344                 mTextView.getCurrentHintTextColor());
   2345         assertEquals(mActivity.getResources().getColor(R.drawable.yellow),
   2346                 mTextView.getLinkTextColors().getDefaultColor());
   2347 
   2348         mTextView.setTextAppearance(mActivity, R.style.TextAppearance_NotColors);
   2349         assertEquals(17f, mTextView.getTextSize(), 0.01f);
   2350         assertEquals(Typeface.NORMAL, mTextView.getTypeface().getStyle());
   2351 
   2352         mTextView.setTextAppearance(mActivity, R.style.TextAppearance_Style);
   2353         assertEquals(null, mTextView.getTypeface());
   2354     }
   2355 
   2356     public void testOnPreDraw() {
   2357         // Do not test. Implementation details.
   2358     }
   2359 
   2360     public void testSetHorizontallyScrolling() {
   2361         // make the text view has more than one line
   2362         mTextView = findTextView(R.id.textview_text);
   2363         setWidth(mTextView.getWidth() >> 1);
   2364         assertTrue(mTextView.getLineCount() > 1);
   2365 
   2366         setHorizontallyScrolling(true);
   2367         assertEquals(1, mTextView.getLineCount());
   2368 
   2369         setHorizontallyScrolling(false);
   2370         assertTrue(mTextView.getLineCount() > 1);
   2371     }
   2372 
   2373     public void testComputeHorizontalScrollRange() {
   2374         MockTextView textView = new MockTextView(mActivity);
   2375         // test when layout is null
   2376         assertNull(textView.getLayout());
   2377         assertEquals(textView.getWidth(), textView.computeHorizontalScrollRange());
   2378 
   2379         textView.setFrame(0, 0, 40, 50);
   2380         assertEquals(textView.getWidth(), textView.computeHorizontalScrollRange());
   2381 
   2382         // set the layout
   2383         layout(textView);
   2384         assertEquals(textView.getLayout().getWidth(), textView.computeHorizontalScrollRange());
   2385     }
   2386 
   2387     public void testComputeVerticalScrollRange() {
   2388         MockTextView textView = new MockTextView(mActivity);
   2389         // test when layout is null
   2390         assertNull(textView.getLayout());
   2391         assertEquals(0, textView.computeVerticalScrollRange());
   2392 
   2393         textView.setFrame(0, 0, 40, 50);
   2394         assertEquals(textView.getHeight(), textView.computeVerticalScrollRange());
   2395 
   2396         //set the layout
   2397         layout(textView);
   2398         assertEquals(textView.getLayout().getHeight(), textView.computeVerticalScrollRange());
   2399     }
   2400 
   2401     public void testDrawableStateChanged() {
   2402         MockTextView textView = new MockTextView(mActivity);
   2403 
   2404         textView.reset();
   2405         textView.refreshDrawableState();
   2406         assertTrue(textView.hasCalledDrawableStateChanged());
   2407     }
   2408 
   2409     public void testGetDefaultEditable() {
   2410         MockTextView textView = new MockTextView(mActivity);
   2411 
   2412         //the TextView#getDefaultEditable() does nothing, and always return false.
   2413         assertFalse(textView.getDefaultEditable());
   2414     }
   2415 
   2416     public void testGetDefaultMovementMethod() {
   2417         MockTextView textView = new MockTextView(mActivity);
   2418 
   2419         //the TextView#getDefaultMovementMethod() does nothing, and always return null.
   2420         assertNull(textView.getDefaultMovementMethod());
   2421     }
   2422 
   2423     public void testOnCreateContextMenu() {
   2424         // Do not test. Implementation details.
   2425     }
   2426 
   2427     public void testOnDetachedFromWindow() {
   2428         // Do not test. Implementation details.
   2429     }
   2430 
   2431     public void testOnDraw() {
   2432         // Do not test. Implementation details.
   2433     }
   2434 
   2435     public void testOnFocusChanged() {
   2436         // Do not test. Implementation details.
   2437     }
   2438 
   2439     public void testOnMeasure() {
   2440         // Do not test. Implementation details.
   2441     }
   2442 
   2443     public void testOnTextChanged() {
   2444         // Do not test. Implementation details.
   2445     }
   2446 
   2447     public void testSetFrame() {
   2448         MockTextView textView = new MockTextView(mActivity);
   2449 
   2450         //Assign a new size to this view
   2451         assertTrue(textView.setFrame(0, 0, 320, 480));
   2452         assertEquals(0, textView.getFrameLeft());
   2453         assertEquals(0, textView.getFrameTop());
   2454         assertEquals(320, textView.getFrameRight());
   2455         assertEquals(480, textView.getFrameBottom());
   2456 
   2457         //Assign a same size to this view
   2458         assertFalse(textView.setFrame(0, 0, 320, 480));
   2459 
   2460         //negative input
   2461         assertTrue(textView.setFrame(-1, -1, -1, -1));
   2462         assertEquals(-1, textView.getFrameLeft());
   2463         assertEquals(-1, textView.getFrameTop());
   2464         assertEquals(-1, textView.getFrameRight());
   2465         assertEquals(-1, textView.getFrameBottom());
   2466     }
   2467 
   2468     public void testGetFadingEdgeStrength() {
   2469         final MockTextView textViewLeft = (MockTextView) mActivity.findViewById(
   2470                 R.id.mock_textview_left);
   2471         mActivity.runOnUiThread(new Runnable() {
   2472             public void run() {
   2473                 textViewLeft.setEllipsize(null);
   2474             }
   2475         });
   2476         mInstrumentation.waitForIdleSync();
   2477 
   2478         // fading is shown on right side if the text aligns left
   2479         assertEquals(0.0f, textViewLeft.getLeftFadingEdgeStrength(), 0.01f);
   2480         assertEquals(1.0f, textViewLeft.getRightFadingEdgeStrength(), 0.01f);
   2481 
   2482         final MockTextView textViewRight = (MockTextView) mActivity.findViewById(
   2483                 R.id.mock_textview_right);
   2484         mActivity.runOnUiThread(new Runnable() {
   2485             public void run() {
   2486                 textViewRight.setEllipsize(null);
   2487             }
   2488         });
   2489         mInstrumentation.waitForIdleSync();
   2490         // fading is shown on left side if the text aligns right
   2491         assertEquals(1.0f, textViewRight.getLeftFadingEdgeStrength(), 0.01f);
   2492         assertEquals(0.0f, textViewRight.getRightFadingEdgeStrength(), 0.01f);
   2493 
   2494         final MockTextView textViewCenter = (MockTextView) mActivity.findViewById(
   2495                 R.id.mock_textview_center);
   2496         mActivity.runOnUiThread(new Runnable() {
   2497             public void run() {
   2498                 textViewCenter.setEllipsize(null);
   2499             }
   2500         });
   2501         mInstrumentation.waitForIdleSync();
   2502         // fading is shown on both sides if the text aligns center
   2503         assertEquals(1.0f, textViewCenter.getLeftFadingEdgeStrength(), 0.01f);
   2504         assertEquals(1.0f, textViewCenter.getRightFadingEdgeStrength(), 0.01f);
   2505     }
   2506 
   2507 
   2508     public void testMarquee() {
   2509         final MockTextView textView = new MockTextView(mActivity);
   2510         textView.setText(LONG_TEXT);
   2511         textView.setSingleLine();
   2512         textView.setEllipsize(TruncateAt.MARQUEE);
   2513         textView.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
   2514 
   2515         final FrameLayout layout = new FrameLayout(mActivity);
   2516         layout.addView(textView);
   2517 
   2518         // make the fading to be shown
   2519         textView.setHorizontalFadingEdgeEnabled(true);
   2520 
   2521         mActivity.runOnUiThread(new Runnable() {
   2522             public void run() {
   2523                 mActivity.setContentView(layout);
   2524             }
   2525         });
   2526         mInstrumentation.waitForIdleSync();
   2527 
   2528         TestSelectedRunnable runnable = new TestSelectedRunnable(textView) {
   2529             public void run() {
   2530                 textView.setMarqueeRepeatLimit(-1);
   2531                 // force the marquee to start
   2532                 saveIsSelected1();
   2533                 textView.setSelected(true);
   2534                 saveIsSelected2();
   2535             }
   2536         };
   2537         mActivity.runOnUiThread(runnable);
   2538 
   2539         // wait for the marquee to run
   2540         // fading is shown on both sides if the marquee runs for a while
   2541         new PollingCheck(TIMEOUT) {
   2542             @Override
   2543             protected boolean check() {
   2544                 return textView.getLeftFadingEdgeStrength() > 0.0f
   2545                         && textView.getRightFadingEdgeStrength() > 0.0f;
   2546             }
   2547         }.run();
   2548 
   2549         final float leftFadingEdgeStrength = textView.getLeftFadingEdgeStrength();
   2550         final float rightFadingEdgeStrength = textView.getRightFadingEdgeStrength();
   2551 
   2552         // wait for the marquee to continue
   2553         // the left fading becomes thicker while the right fading becomes thiner
   2554         // as the text moves towards left
   2555         new PollingCheck(TIMEOUT) {
   2556             @Override
   2557             protected boolean check() {
   2558                 return leftFadingEdgeStrength < textView.getLeftFadingEdgeStrength()
   2559                         && rightFadingEdgeStrength > textView.getRightFadingEdgeStrength();
   2560             }
   2561         }.run();
   2562         assertFalse(runnable.getIsSelected1());
   2563         assertTrue(runnable.getIsSelected2());
   2564 
   2565         runnable = new TestSelectedRunnable(textView) {
   2566             public void run() {
   2567                 textView.setMarqueeRepeatLimit(0);
   2568                 // force the marquee to stop
   2569                 saveIsSelected1();
   2570                 textView.setSelected(false);
   2571                 saveIsSelected2();
   2572                 textView.setGravity(Gravity.LEFT);
   2573             }
   2574         };
   2575         // force the marquee to stop
   2576         mActivity.runOnUiThread(runnable);
   2577         mInstrumentation.waitForIdleSync();
   2578         assertTrue(runnable.getIsSelected1());
   2579         assertFalse(runnable.getIsSelected2());
   2580         assertEquals(0.0f, textView.getLeftFadingEdgeStrength(), 0.01f);
   2581         assertTrue(textView.getRightFadingEdgeStrength() > 0.0f);
   2582 
   2583         mActivity.runOnUiThread(new Runnable() {
   2584             public void run() {
   2585                 textView.setGravity(Gravity.RIGHT);
   2586             }
   2587         });
   2588         mInstrumentation.waitForIdleSync();
   2589         assertTrue(textView.getLeftFadingEdgeStrength() > 0.0f);
   2590         assertEquals(0.0f, textView.getRightFadingEdgeStrength(), 0.01f);
   2591 
   2592         mActivity.runOnUiThread(new Runnable() {
   2593             public void run() {
   2594                 textView.setGravity(Gravity.CENTER_HORIZONTAL);
   2595             }
   2596         });
   2597         mInstrumentation.waitForIdleSync();
   2598         // there is no left fading (Is it correct?)
   2599         assertEquals(0.0f, textView.getLeftFadingEdgeStrength(), 0.01f);
   2600         assertTrue(textView.getRightFadingEdgeStrength() > 0.0f);
   2601     }
   2602 
   2603     public void testOnKeyMultiple() {
   2604         // Do not test. Implementation details.
   2605     }
   2606 
   2607     public void testAccessInputExtras() throws XmlPullParserException, IOException {
   2608         TextView textView = new TextView(mActivity);
   2609         textView.setText(null, BufferType.EDITABLE);
   2610         textView.setInputType(InputType.TYPE_CLASS_TEXT);
   2611 
   2612         // do not create the extras
   2613         assertNull(textView.getInputExtras(false));
   2614 
   2615         // create if it does not exist
   2616         Bundle inputExtras = textView.getInputExtras(true);
   2617         assertNotNull(inputExtras);
   2618         assertTrue(inputExtras.isEmpty());
   2619 
   2620         // it is created already
   2621         assertNotNull(textView.getInputExtras(false));
   2622 
   2623         try {
   2624             textView.setInputExtras(R.xml.input_extras);
   2625             fail("Should throw NullPointerException!");
   2626         } catch (NullPointerException e) {
   2627         }
   2628     }
   2629 
   2630     public void testAccessContentType() {
   2631         TextView textView = new TextView(mActivity);
   2632         textView.setText(null, BufferType.EDITABLE);
   2633         textView.setKeyListener(null);
   2634         textView.setTransformationMethod(null);
   2635 
   2636         textView.setInputType(InputType.TYPE_CLASS_DATETIME
   2637                 | InputType.TYPE_DATETIME_VARIATION_NORMAL);
   2638         assertEquals(InputType.TYPE_CLASS_DATETIME
   2639                 | InputType.TYPE_DATETIME_VARIATION_NORMAL, textView.getInputType());
   2640         assertTrue(textView.getKeyListener() instanceof DateTimeKeyListener);
   2641 
   2642         textView.setInputType(InputType.TYPE_CLASS_DATETIME
   2643                 | InputType.TYPE_DATETIME_VARIATION_DATE);
   2644         assertEquals(InputType.TYPE_CLASS_DATETIME
   2645                 | InputType.TYPE_DATETIME_VARIATION_DATE, textView.getInputType());
   2646         assertTrue(textView.getKeyListener() instanceof DateKeyListener);
   2647 
   2648         textView.setInputType(InputType.TYPE_CLASS_DATETIME
   2649                 | InputType.TYPE_DATETIME_VARIATION_TIME);
   2650         assertEquals(InputType.TYPE_CLASS_DATETIME
   2651                 | InputType.TYPE_DATETIME_VARIATION_TIME, textView.getInputType());
   2652         assertTrue(textView.getKeyListener() instanceof TimeKeyListener);
   2653 
   2654         textView.setInputType(InputType.TYPE_CLASS_NUMBER
   2655                 | InputType.TYPE_NUMBER_FLAG_DECIMAL
   2656                 | InputType.TYPE_NUMBER_FLAG_SIGNED);
   2657         assertEquals(InputType.TYPE_CLASS_NUMBER
   2658                 | InputType.TYPE_NUMBER_FLAG_DECIMAL
   2659                 | InputType.TYPE_NUMBER_FLAG_SIGNED, textView.getInputType());
   2660         assertSame(textView.getKeyListener(), DigitsKeyListener.getInstance(true, true));
   2661 
   2662         textView.setInputType(InputType.TYPE_CLASS_PHONE);
   2663         assertEquals(InputType.TYPE_CLASS_PHONE, textView.getInputType());
   2664         assertTrue(textView.getKeyListener() instanceof DialerKeyListener);
   2665 
   2666         textView.setInputType(InputType.TYPE_CLASS_TEXT
   2667                 | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);
   2668         assertEquals(InputType.TYPE_CLASS_TEXT
   2669                 | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT, textView.getInputType());
   2670         assertSame(textView.getKeyListener(), TextKeyListener.getInstance(true, Capitalize.NONE));
   2671 
   2672         textView.setSingleLine();
   2673         assertTrue(textView.getTransformationMethod() instanceof SingleLineTransformationMethod);
   2674         textView.setInputType(InputType.TYPE_CLASS_TEXT
   2675                 | InputType.TYPE_TEXT_FLAG_MULTI_LINE
   2676                 | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
   2677         assertEquals(InputType.TYPE_CLASS_TEXT
   2678                 | InputType.TYPE_TEXT_FLAG_MULTI_LINE
   2679                 | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS, textView.getInputType());
   2680         assertSame(textView.getKeyListener(),
   2681                 TextKeyListener.getInstance(false, Capitalize.CHARACTERS));
   2682         assertNull(textView.getTransformationMethod());
   2683 
   2684         textView.setInputType(InputType.TYPE_CLASS_TEXT
   2685                 | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
   2686         assertEquals(InputType.TYPE_CLASS_TEXT
   2687                 | InputType.TYPE_TEXT_FLAG_CAP_WORDS, textView.getInputType());
   2688         assertSame(textView.getKeyListener(),
   2689                 TextKeyListener.getInstance(false, Capitalize.WORDS));
   2690         assertTrue(textView.getTransformationMethod() instanceof SingleLineTransformationMethod);
   2691 
   2692         textView.setInputType(InputType.TYPE_CLASS_TEXT
   2693                 | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
   2694         assertEquals(InputType.TYPE_CLASS_TEXT
   2695                 | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES, textView.getInputType());
   2696         assertSame(textView.getKeyListener(),
   2697                 TextKeyListener.getInstance(false, Capitalize.SENTENCES));
   2698 
   2699         textView.setInputType(InputType.TYPE_NULL);
   2700         assertEquals(InputType.TYPE_NULL, textView.getInputType());
   2701         assertTrue(textView.getKeyListener() instanceof TextKeyListener);
   2702     }
   2703 
   2704     public void testAccessRawContentType() {
   2705         TextView textView = new TextView(mActivity);
   2706         textView.setText(null, BufferType.EDITABLE);
   2707         textView.setKeyListener(null);
   2708         textView.setTransformationMethod(null);
   2709 
   2710         textView.setRawInputType(InputType.TYPE_CLASS_DATETIME
   2711                 | InputType.TYPE_DATETIME_VARIATION_NORMAL);
   2712         assertEquals(InputType.TYPE_CLASS_DATETIME
   2713                 | InputType.TYPE_DATETIME_VARIATION_NORMAL, textView.getInputType());
   2714         assertNull(textView.getTransformationMethod());
   2715         assertNull(textView.getKeyListener());
   2716 
   2717         textView.setRawInputType(InputType.TYPE_CLASS_DATETIME
   2718                 | InputType.TYPE_DATETIME_VARIATION_DATE);
   2719         assertEquals(InputType.TYPE_CLASS_DATETIME
   2720                 | InputType.TYPE_DATETIME_VARIATION_DATE, textView.getInputType());
   2721         assertNull(textView.getTransformationMethod());
   2722         assertNull(textView.getKeyListener());
   2723 
   2724         textView.setRawInputType(InputType.TYPE_CLASS_DATETIME
   2725                 | InputType.TYPE_DATETIME_VARIATION_TIME);
   2726         assertEquals(InputType.TYPE_CLASS_DATETIME
   2727                 | InputType.TYPE_DATETIME_VARIATION_TIME, textView.getInputType());
   2728         assertNull(textView.getTransformationMethod());
   2729         assertNull(textView.getKeyListener());
   2730 
   2731         textView.setRawInputType(InputType.TYPE_CLASS_NUMBER
   2732                 | InputType.TYPE_NUMBER_FLAG_DECIMAL
   2733                 | InputType.TYPE_NUMBER_FLAG_SIGNED);
   2734         assertEquals(InputType.TYPE_CLASS_NUMBER
   2735                 | InputType.TYPE_NUMBER_FLAG_DECIMAL
   2736                 | InputType.TYPE_NUMBER_FLAG_SIGNED, textView.getInputType());
   2737         assertNull(textView.getTransformationMethod());
   2738         assertNull(textView.getKeyListener());
   2739 
   2740         textView.setRawInputType(InputType.TYPE_CLASS_PHONE);
   2741         assertEquals(InputType.TYPE_CLASS_PHONE, textView.getInputType());
   2742         assertNull(textView.getTransformationMethod());
   2743         assertNull(textView.getKeyListener());
   2744 
   2745         textView.setRawInputType(InputType.TYPE_CLASS_TEXT
   2746                 | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);
   2747         assertEquals(InputType.TYPE_CLASS_TEXT
   2748                 | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT, textView.getInputType());
   2749         assertNull(textView.getTransformationMethod());
   2750         assertNull(textView.getKeyListener());
   2751 
   2752         textView.setSingleLine();
   2753         assertTrue(textView.getTransformationMethod() instanceof SingleLineTransformationMethod);
   2754         textView.setRawInputType(InputType.TYPE_CLASS_TEXT
   2755                 | InputType.TYPE_TEXT_FLAG_MULTI_LINE
   2756                 | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
   2757         assertEquals(InputType.TYPE_CLASS_TEXT
   2758                 | InputType.TYPE_TEXT_FLAG_MULTI_LINE
   2759                 | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS, textView.getInputType());
   2760         assertTrue(textView.getTransformationMethod() instanceof SingleLineTransformationMethod);
   2761         assertNull(textView.getKeyListener());
   2762 
   2763         textView.setRawInputType(InputType.TYPE_CLASS_TEXT
   2764                 | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
   2765         assertEquals(InputType.TYPE_CLASS_TEXT
   2766                 | InputType.TYPE_TEXT_FLAG_CAP_WORDS, textView.getInputType());
   2767         assertTrue(textView.getTransformationMethod() instanceof SingleLineTransformationMethod);
   2768         assertNull(textView.getKeyListener());
   2769 
   2770         textView.setRawInputType(InputType.TYPE_CLASS_TEXT
   2771                 | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
   2772         assertEquals(InputType.TYPE_CLASS_TEXT
   2773                 | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES, textView.getInputType());
   2774         assertTrue(textView.getTransformationMethod() instanceof SingleLineTransformationMethod);
   2775         assertNull(textView.getKeyListener());
   2776 
   2777         textView.setRawInputType(InputType.TYPE_NULL);
   2778         assertTrue(textView.getTransformationMethod() instanceof SingleLineTransformationMethod);
   2779         assertNull(textView.getKeyListener());
   2780     }
   2781 
   2782     public void testOnPrivateIMECommand() {
   2783         // Do not test. Implementation details.
   2784     }
   2785 
   2786     public void testFoo() {
   2787         // Do not test. Implementation details.
   2788     }
   2789 
   2790     public void testVerifyDrawable() {
   2791         MockTextView textView = new MockTextView(mActivity);
   2792 
   2793         Drawable d = getDrawable(R.drawable.pass);
   2794         assertFalse(textView.verifyDrawable(d));
   2795 
   2796         textView.setCompoundDrawables(null, d, null, null);
   2797         assertTrue(textView.verifyDrawable(d));
   2798     }
   2799 
   2800     public void testAccessPrivateImeOptions() {
   2801         mTextView = findTextView(R.id.textview_text);
   2802         assertNull(mTextView.getPrivateImeOptions());
   2803 
   2804         mTextView.setPrivateImeOptions("com.example.myapp.SpecialMode=3");
   2805         assertEquals("com.example.myapp.SpecialMode=3", mTextView.getPrivateImeOptions());
   2806 
   2807         mTextView.setPrivateImeOptions(null);
   2808         assertNull(mTextView.getPrivateImeOptions());
   2809     }
   2810 
   2811     public void testSetOnEditorActionListener() {
   2812         mTextView = findTextView(R.id.textview_text);
   2813 
   2814         MockOnEditorActionListener listener = new MockOnEditorActionListener();
   2815         assertFalse(listener.isOnEditorActionCalled());
   2816 
   2817         mTextView.setOnEditorActionListener(listener);
   2818         assertFalse(listener.isOnEditorActionCalled());
   2819 
   2820         mTextView.onEditorAction(EditorInfo.IME_ACTION_DONE);
   2821         assertTrue(listener.isOnEditorActionCalled());
   2822     }
   2823 
   2824     public void testAccessImeOptions() {
   2825         mTextView = findTextView(R.id.textview_text);
   2826         assertEquals(EditorInfo.IME_NULL, mTextView.getImeOptions());
   2827 
   2828         mTextView.setImeOptions(EditorInfo.IME_ACTION_GO);
   2829         assertEquals(EditorInfo.IME_ACTION_GO, mTextView.getImeOptions());
   2830 
   2831         mTextView.setImeOptions(EditorInfo.IME_ACTION_DONE);
   2832         assertEquals(EditorInfo.IME_ACTION_DONE, mTextView.getImeOptions());
   2833 
   2834         mTextView.setImeOptions(EditorInfo.IME_NULL);
   2835         assertEquals(EditorInfo.IME_NULL, mTextView.getImeOptions());
   2836     }
   2837 
   2838     public void testAccessImeActionLabel() {
   2839         mTextView = findTextView(R.id.textview_text);
   2840         assertNull(mTextView.getImeActionLabel());
   2841         assertEquals(0, mTextView.getImeActionId());
   2842 
   2843         mTextView.setImeActionLabel("pinyin", 1);
   2844         assertEquals("pinyin", mTextView.getImeActionLabel().toString());
   2845         assertEquals(1, mTextView.getImeActionId());
   2846     }
   2847 
   2848     public void testSetTextLong() {
   2849         mActivity.runOnUiThread(new Runnable() {
   2850             public void run() {
   2851                 final int MAX_COUNT = 1 << 21;
   2852                 char[] longText = new char[MAX_COUNT];
   2853                 for (int n = 0; n < MAX_COUNT; n++) {
   2854                     longText[n] = 'm';
   2855                 }
   2856                 mTextView = findTextView(R.id.textview_text);
   2857                 mTextView.setText(new String(longText));
   2858             }
   2859         });
   2860         mInstrumentation.waitForIdleSync();
   2861     }
   2862 
   2863     @UiThreadTest
   2864     public void testSetExtractedText() {
   2865         mTextView = findTextView(R.id.textview_text);
   2866         assertEquals(mActivity.getResources().getString(R.string.text_view_hello),
   2867                 mTextView.getText().toString());
   2868 
   2869         ExtractedText et = new ExtractedText();
   2870         et.text = "test";
   2871 
   2872         mTextView.setExtractedText(et);
   2873         assertEquals("test", mTextView.getText().toString());
   2874     }
   2875 
   2876     public void testMoveCursorToVisibleOffset() throws Throwable {
   2877         mTextView = findTextView(R.id.textview_text);
   2878 
   2879         // not a spannable text
   2880         runTestOnUiThread(new Runnable() {
   2881             public void run() {
   2882                 assertFalse(mTextView.moveCursorToVisibleOffset());
   2883             }
   2884         });
   2885         mInstrumentation.waitForIdleSync();
   2886 
   2887         // a selection range
   2888         final String spannableText = "text";
   2889         mTextView = new TextView(mActivity);
   2890 
   2891         runTestOnUiThread(new Runnable() {
   2892             public void run() {
   2893                 mTextView.setText(spannableText, BufferType.SPANNABLE);
   2894             }
   2895         });
   2896         mInstrumentation.waitForIdleSync();
   2897         Selection.setSelection((Spannable) mTextView.getText(), 0, spannableText.length());
   2898 
   2899         assertEquals(0, mTextView.getSelectionStart());
   2900         assertEquals(spannableText.length(), mTextView.getSelectionEnd());
   2901         runTestOnUiThread(new Runnable() {
   2902             public void run() {
   2903                 assertFalse(mTextView.moveCursorToVisibleOffset());
   2904             }
   2905         });
   2906         mInstrumentation.waitForIdleSync();
   2907 
   2908         // a spannable without range
   2909         runTestOnUiThread(new Runnable() {
   2910             public void run() {
   2911                 mTextView = findTextView(R.id.textview_text);
   2912                 mTextView.setText(spannableText, BufferType.SPANNABLE);
   2913             }
   2914         });
   2915         mInstrumentation.waitForIdleSync();
   2916 
   2917         runTestOnUiThread(new Runnable() {
   2918             public void run() {
   2919                 assertTrue(mTextView.moveCursorToVisibleOffset());
   2920             }
   2921         });
   2922         mInstrumentation.waitForIdleSync();
   2923     }
   2924 
   2925     public void testIsInputMethodTarget() throws Throwable {
   2926         mTextView = findTextView(R.id.textview_text);
   2927         assertFalse(mTextView.isInputMethodTarget());
   2928 
   2929         assertFalse(mTextView.isFocused());
   2930         runTestOnUiThread(new Runnable() {
   2931            @Override
   2932             public void run() {
   2933                mTextView.setFocusable(true);
   2934                mTextView.requestFocus();
   2935             }
   2936         });
   2937         mInstrumentation.waitForIdleSync();
   2938         assertTrue(mTextView.isFocused());
   2939 
   2940         new PollingCheck() {
   2941             @Override
   2942             protected boolean check() {
   2943                 return mTextView.isInputMethodTarget();
   2944             }
   2945         }.run();
   2946     }
   2947 
   2948     public void testBeginEndBatchEdit() {
   2949         mTextView = findTextView(R.id.textview_text);
   2950 
   2951         mTextView.beginBatchEdit();
   2952         mTextView.endBatchEdit();
   2953     }
   2954 
   2955     @UiThreadTest
   2956     public void testBringPointIntoView() throws Throwable {
   2957         mTextView = findTextView(R.id.textview_text);
   2958         assertFalse(mTextView.bringPointIntoView(1));
   2959 
   2960         mTextView.layout(0, 0, 100, 100);
   2961         assertFalse(mTextView.bringPointIntoView(2));
   2962     }
   2963 
   2964     public void testCancelLongPress() {
   2965         mTextView = findTextView(R.id.textview_text);
   2966         TouchUtils.longClickView(this, mTextView);
   2967         mTextView.cancelLongPress();
   2968     }
   2969 
   2970     @UiThreadTest
   2971     public void testClearComposingText() {
   2972         mTextView = findTextView(R.id.textview_text);
   2973         mTextView.setText("Hello world!", BufferType.SPANNABLE);
   2974         Spannable text = (Spannable) mTextView.getText();
   2975 
   2976         assertEquals(-1, BaseInputConnection.getComposingSpanStart(text));
   2977         assertEquals(-1, BaseInputConnection.getComposingSpanStart(text));
   2978 
   2979         BaseInputConnection.setComposingSpans((Spannable) mTextView.getText());
   2980         assertEquals(0, BaseInputConnection.getComposingSpanStart(text));
   2981         assertEquals(0, BaseInputConnection.getComposingSpanStart(text));
   2982 
   2983         mTextView.clearComposingText();
   2984         assertEquals(-1, BaseInputConnection.getComposingSpanStart(text));
   2985         assertEquals(-1, BaseInputConnection.getComposingSpanStart(text));
   2986     }
   2987 
   2988     public void testComputeVerticalScrollExtent() {
   2989         MockTextView textView = new MockTextView(mActivity);
   2990         assertEquals(0, textView.computeVerticalScrollExtent());
   2991 
   2992         Drawable d = getDrawable(R.drawable.pass);
   2993         textView.setCompoundDrawables(null, d, null, d);
   2994 
   2995         assertEquals(0, textView.computeVerticalScrollExtent());
   2996     }
   2997 
   2998     @UiThreadTest
   2999     public void testDidTouchFocusSelect() {
   3000         mTextView = new EditText(mActivity);
   3001         assertFalse(mTextView.didTouchFocusSelect());
   3002 
   3003         mTextView.setFocusable(true);
   3004         mTextView.requestFocus();
   3005         assertTrue(mTextView.didTouchFocusSelect());
   3006     }
   3007 
   3008     public void testExtractText() {
   3009         mTextView = new TextView(mActivity);
   3010 
   3011         ExtractedTextRequest request = new ExtractedTextRequest();
   3012         ExtractedText outText = new ExtractedText();
   3013 
   3014         request.token = 0;
   3015         request.flags = 10;
   3016         request.hintMaxLines = 2;
   3017         request.hintMaxChars = 20;
   3018         assertTrue(mTextView.extractText(request, outText));
   3019 
   3020         mTextView = findTextView(R.id.textview_text);
   3021         assertTrue(mTextView.extractText(request, outText));
   3022 
   3023         assertEquals(mActivity.getResources().getString(R.string.text_view_hello),
   3024                 outText.text.toString());
   3025     }
   3026 
   3027     @UiThreadTest
   3028     public void testTextDirectionDefault() {
   3029         TextView tv = new TextView(mActivity);
   3030         assertEquals(View.TEXT_DIRECTION_INHERIT, tv.getRawTextDirection());
   3031     }
   3032 
   3033     @UiThreadTest
   3034     public void testSetGetTextDirection() {
   3035         TextView tv = new TextView(mActivity);
   3036 
   3037         tv.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG);
   3038         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getRawTextDirection());
   3039 
   3040         tv.setTextDirection(View.TEXT_DIRECTION_ANY_RTL);
   3041         assertEquals(View.TEXT_DIRECTION_ANY_RTL, tv.getRawTextDirection());
   3042 
   3043         tv.setTextDirection(View.TEXT_DIRECTION_INHERIT);
   3044         assertEquals(View.TEXT_DIRECTION_INHERIT, tv.getRawTextDirection());
   3045 
   3046         tv.setTextDirection(View.TEXT_DIRECTION_LTR);
   3047         assertEquals(View.TEXT_DIRECTION_LTR, tv.getRawTextDirection());
   3048 
   3049         tv.setTextDirection(View.TEXT_DIRECTION_RTL);
   3050         assertEquals(View.TEXT_DIRECTION_RTL, tv.getRawTextDirection());
   3051 
   3052         tv.setTextDirection(View.TEXT_DIRECTION_LOCALE);
   3053         assertEquals(View.TEXT_DIRECTION_LOCALE, tv.getRawTextDirection());
   3054     }
   3055 
   3056     @UiThreadTest
   3057     public void testGetResolvedTextDirectionLtr() {
   3058         TextView tv = new TextView(mActivity);
   3059         tv.setText("this is a test");
   3060 
   3061         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection());
   3062 
   3063         tv.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG);
   3064         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection());
   3065 
   3066         tv.setTextDirection(View.TEXT_DIRECTION_ANY_RTL);
   3067         assertEquals(View.TEXT_DIRECTION_ANY_RTL, tv.getTextDirection());
   3068 
   3069         tv.setTextDirection(View.TEXT_DIRECTION_INHERIT);
   3070         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection());
   3071 
   3072         tv.setTextDirection(View.TEXT_DIRECTION_LTR);
   3073         assertEquals(View.TEXT_DIRECTION_LTR, tv.getTextDirection());
   3074 
   3075         tv.setTextDirection(View.TEXT_DIRECTION_RTL);
   3076         assertEquals(View.TEXT_DIRECTION_RTL, tv.getTextDirection());
   3077 
   3078         tv.setTextDirection(View.TEXT_DIRECTION_LOCALE);
   3079         assertEquals(View.TEXT_DIRECTION_LOCALE, tv.getTextDirection());
   3080     }
   3081 
   3082     @UiThreadTest
   3083     public void testGetResolvedTextDirectionLtrWithInheritance() {
   3084         LinearLayout ll = new LinearLayout(mActivity);
   3085         ll.setTextDirection(View.TEXT_DIRECTION_ANY_RTL);
   3086 
   3087         TextView tv = new TextView(mActivity);
   3088         tv.setText("this is a test");
   3089         ll.addView(tv);
   3090 
   3091         tv.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG);
   3092         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection());
   3093 
   3094         tv.setTextDirection(View.TEXT_DIRECTION_ANY_RTL);
   3095         assertEquals(View.TEXT_DIRECTION_ANY_RTL, tv.getTextDirection());
   3096 
   3097         tv.setTextDirection(View.TEXT_DIRECTION_INHERIT);
   3098         assertEquals(View.TEXT_DIRECTION_ANY_RTL, tv.getTextDirection());
   3099 
   3100         tv.setTextDirection(View.TEXT_DIRECTION_LTR);
   3101         assertEquals(View.TEXT_DIRECTION_LTR, tv.getTextDirection());
   3102 
   3103         tv.setTextDirection(View.TEXT_DIRECTION_RTL);
   3104         assertEquals(View.TEXT_DIRECTION_RTL, tv.getTextDirection());
   3105 
   3106         tv.setTextDirection(View.TEXT_DIRECTION_LOCALE);
   3107         assertEquals(View.TEXT_DIRECTION_LOCALE, tv.getTextDirection());
   3108     }
   3109 
   3110     @UiThreadTest
   3111     public void testGetResolvedTextDirectionRtl() {
   3112         TextView tv = new TextView(mActivity);
   3113         tv.setText("\u05DD\u05DE"); // hebrew
   3114 
   3115         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection());
   3116 
   3117         tv.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG);
   3118         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection());
   3119 
   3120         tv.setTextDirection(View.TEXT_DIRECTION_ANY_RTL);
   3121         assertEquals(View.TEXT_DIRECTION_ANY_RTL, tv.getTextDirection());
   3122 
   3123         tv.setTextDirection(View.TEXT_DIRECTION_INHERIT);
   3124         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection());
   3125 
   3126         tv.setTextDirection(View.TEXT_DIRECTION_LTR);
   3127         assertEquals(View.TEXT_DIRECTION_LTR, tv.getTextDirection());
   3128 
   3129         tv.setTextDirection(View.TEXT_DIRECTION_RTL);
   3130         assertEquals(View.TEXT_DIRECTION_RTL, tv.getTextDirection());
   3131 
   3132         tv.setTextDirection(View.TEXT_DIRECTION_LOCALE);
   3133         assertEquals(View.TEXT_DIRECTION_LOCALE, tv.getTextDirection());
   3134     }
   3135 
   3136     @UiThreadTest
   3137     public void testGetResolvedTextDirectionRtlWithInheritance() {
   3138         LinearLayout ll = new LinearLayout(mActivity);
   3139         ll.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG);
   3140 
   3141         TextView tv = new TextView(mActivity);
   3142         tv.setText("\u05DD\u05DE"); // hebrew
   3143         ll.addView(tv);
   3144 
   3145         tv.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG);
   3146         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection());
   3147 
   3148         tv.setTextDirection(View.TEXT_DIRECTION_ANY_RTL);
   3149         assertEquals(View.TEXT_DIRECTION_ANY_RTL, tv.getTextDirection());
   3150 
   3151         tv.setTextDirection(View.TEXT_DIRECTION_INHERIT);
   3152         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection());
   3153 
   3154         tv.setTextDirection(View.TEXT_DIRECTION_LTR);
   3155         assertEquals(View.TEXT_DIRECTION_LTR, tv.getTextDirection());
   3156 
   3157         tv.setTextDirection(View.TEXT_DIRECTION_RTL);
   3158         assertEquals(View.TEXT_DIRECTION_RTL, tv.getTextDirection());
   3159 
   3160         tv.setTextDirection(View.TEXT_DIRECTION_LOCALE);
   3161         assertEquals(View.TEXT_DIRECTION_LOCALE, tv.getTextDirection());
   3162 
   3163         // Force to RTL text direction on the layout
   3164         ll.setTextDirection(View.TEXT_DIRECTION_RTL);
   3165 
   3166         tv.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG);
   3167         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection());
   3168 
   3169         tv.setTextDirection(View.TEXT_DIRECTION_ANY_RTL);
   3170         assertEquals(View.TEXT_DIRECTION_ANY_RTL, tv.getTextDirection());
   3171 
   3172         tv.setTextDirection(View.TEXT_DIRECTION_INHERIT);
   3173         assertEquals(View.TEXT_DIRECTION_RTL, tv.getTextDirection());
   3174 
   3175         tv.setTextDirection(View.TEXT_DIRECTION_LTR);
   3176         assertEquals(View.TEXT_DIRECTION_LTR, tv.getTextDirection());
   3177 
   3178         tv.setTextDirection(View.TEXT_DIRECTION_RTL);
   3179         assertEquals(View.TEXT_DIRECTION_RTL, tv.getTextDirection());
   3180 
   3181         tv.setTextDirection(View.TEXT_DIRECTION_LOCALE);
   3182         assertEquals(View.TEXT_DIRECTION_LOCALE, tv.getTextDirection());
   3183     }
   3184 
   3185     @UiThreadTest
   3186     public void testResetTextDirection() {
   3187         LinearLayout ll = (LinearLayout) mActivity.findViewById(R.id.layout_textviewtest);
   3188         TextView tv = (TextView) mActivity.findViewById(R.id.textview_rtl);
   3189 
   3190         ll.setTextDirection(View.TEXT_DIRECTION_RTL);
   3191         tv.setTextDirection(View.TEXT_DIRECTION_INHERIT);
   3192         assertEquals(View.TEXT_DIRECTION_RTL, tv.getTextDirection());
   3193 
   3194         // No reset when we remove the view
   3195         ll.removeView(tv);
   3196         assertEquals(View.TEXT_DIRECTION_RTL, tv.getTextDirection());
   3197 
   3198         // Reset is done when we add the view
   3199         ll.addView(tv);
   3200         assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection());
   3201     }
   3202 
   3203     @UiThreadTest
   3204     public void testTextAlignmentDefault() {
   3205         TextView tv = new TextView(getActivity());
   3206         assertEquals(View.TEXT_ALIGNMENT_GRAVITY, tv.getRawTextAlignment());
   3207         // resolved default text alignment is GRAVITY
   3208         assertEquals(View.TEXT_ALIGNMENT_GRAVITY, tv.getTextAlignment());
   3209     }
   3210 
   3211     @UiThreadTest
   3212     public void testSetGetTextAlignment() {
   3213         TextView tv = new TextView(getActivity());
   3214 
   3215         tv.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
   3216         assertEquals(View.TEXT_ALIGNMENT_GRAVITY, tv.getRawTextAlignment());
   3217 
   3218         tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
   3219         assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getRawTextAlignment());
   3220 
   3221         tv.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
   3222         assertEquals(View.TEXT_ALIGNMENT_TEXT_START, tv.getRawTextAlignment());
   3223 
   3224         tv.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END);
   3225         assertEquals(View.TEXT_ALIGNMENT_TEXT_END, tv.getRawTextAlignment());
   3226 
   3227         tv.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
   3228         assertEquals(View.TEXT_ALIGNMENT_VIEW_START, tv.getRawTextAlignment());
   3229 
   3230         tv.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
   3231         assertEquals(View.TEXT_ALIGNMENT_VIEW_END, tv.getRawTextAlignment());
   3232     }
   3233 
   3234     @UiThreadTest
   3235     public void testGetResolvedTextAlignment() {
   3236         TextView tv = new TextView(getActivity());
   3237 
   3238         assertEquals(View.TEXT_ALIGNMENT_GRAVITY, tv.getTextAlignment());
   3239 
   3240         // Test center alignment first so that we dont hit the default case
   3241         tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
   3242         assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment());
   3243 
   3244         // Test the default case too
   3245         tv.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
   3246         assertEquals(View.TEXT_ALIGNMENT_GRAVITY, tv.getTextAlignment());
   3247 
   3248         tv.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
   3249         assertEquals(View.TEXT_ALIGNMENT_TEXT_START, tv.getTextAlignment());
   3250 
   3251         tv.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END);
   3252         assertEquals(View.TEXT_ALIGNMENT_TEXT_END, tv.getTextAlignment());
   3253 
   3254         tv.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
   3255         assertEquals(View.TEXT_ALIGNMENT_VIEW_START, tv.getTextAlignment());
   3256 
   3257         tv.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
   3258         assertEquals(View.TEXT_ALIGNMENT_VIEW_END, tv.getTextAlignment());
   3259     }
   3260 
   3261     @UiThreadTest
   3262     public void testGetResolvedTextAlignmentWithInheritance() {
   3263         LinearLayout ll = new LinearLayout(getActivity());
   3264         ll.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
   3265 
   3266         TextView tv = new TextView(getActivity());
   3267         ll.addView(tv);
   3268 
   3269         // check defaults
   3270         assertEquals(View.TEXT_ALIGNMENT_GRAVITY, tv.getRawTextAlignment());
   3271         assertEquals(View.TEXT_ALIGNMENT_GRAVITY, tv.getTextAlignment());
   3272 
   3273         // set inherit and check that child is following parent
   3274         tv.setTextAlignment(View.TEXT_ALIGNMENT_INHERIT);
   3275         assertEquals(View.TEXT_ALIGNMENT_INHERIT, tv.getRawTextAlignment());
   3276 
   3277         ll.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
   3278         assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment());
   3279 
   3280         ll.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
   3281         assertEquals(View.TEXT_ALIGNMENT_TEXT_START, tv.getTextAlignment());
   3282 
   3283         ll.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END);
   3284         assertEquals(View.TEXT_ALIGNMENT_TEXT_END, tv.getTextAlignment());
   3285 
   3286         ll.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
   3287         assertEquals(View.TEXT_ALIGNMENT_VIEW_START, tv.getTextAlignment());
   3288 
   3289         ll.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
   3290         assertEquals(View.TEXT_ALIGNMENT_VIEW_END, tv.getTextAlignment());
   3291 
   3292         // now get rid of the inheritance but still change the parent
   3293         tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
   3294 
   3295         ll.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
   3296         assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment());
   3297 
   3298         ll.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
   3299         assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment());
   3300 
   3301         ll.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END);
   3302         assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment());
   3303 
   3304         ll.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
   3305         assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment());
   3306 
   3307         ll.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
   3308         assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment());
   3309     }
   3310 
   3311     @UiThreadTest
   3312     public void testResetTextAlignment() {
   3313         TextViewCtsActivity activity = getActivity();
   3314 
   3315         LinearLayout ll = (LinearLayout) activity.findViewById(R.id.layout_textviewtest);
   3316         TextView tv = (TextView) activity.findViewById(R.id.textview_rtl);
   3317 
   3318         ll.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
   3319         tv.setTextAlignment(View.TEXT_ALIGNMENT_INHERIT);
   3320         assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment());
   3321 
   3322         // No reset when we remove the view
   3323         ll.removeView(tv);
   3324         assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment());
   3325 
   3326         // Reset is done when we add the view
   3327         // Default text alignment is GRAVITY
   3328         ll.addView(tv);
   3329         assertEquals(View.TEXT_ALIGNMENT_GRAVITY, tv.getTextAlignment());
   3330     }
   3331 
   3332     @UiThreadTest
   3333     public void testDrawableResolution() {
   3334         final int LEFT = 0;
   3335         final int TOP = 1;
   3336         final int RIGHT = 2;
   3337         final int BOTTOM = 3;
   3338 
   3339         TextViewCtsActivity activity = getActivity();
   3340 
   3341         // Case 1.1: left / right drawable defined in default LTR mode
   3342         TextView tv = (TextView) activity.findViewById(R.id.textview_drawable_1_1);
   3343         Drawable[] drawables = tv.getCompoundDrawables();
   3344 
   3345         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3346                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3347         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3348                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3349         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green),
   3350                 ((BitmapDrawable) drawables[TOP]).getBitmap());
   3351         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3352                 ((BitmapDrawable) drawables[BOTTOM]).getBitmap());
   3353 
   3354         // Case 1.2: left / right drawable defined in default RTL mode
   3355         tv = (TextView) activity.findViewById(R.id.textview_drawable_1_2);
   3356         drawables = tv.getCompoundDrawables();
   3357 
   3358         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3359                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3360         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3361                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3362         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green),
   3363                 ((BitmapDrawable) drawables[TOP]).getBitmap());
   3364         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3365                 ((BitmapDrawable) drawables[BOTTOM]).getBitmap());
   3366 
   3367         // Case 2.1: start / end drawable defined in LTR mode
   3368         tv = (TextView) activity.findViewById(R.id.textview_drawable_2_1);
   3369         drawables = tv.getCompoundDrawables();
   3370 
   3371         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3372                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3373         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3374                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3375         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green),
   3376                 ((BitmapDrawable) drawables[TOP]).getBitmap());
   3377         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3378                 ((BitmapDrawable) drawables[BOTTOM]).getBitmap());
   3379 
   3380         // Case 2.2: start / end drawable defined in RTL mode
   3381         tv = (TextView) activity.findViewById(R.id.textview_drawable_2_2);
   3382         drawables = tv.getCompoundDrawables();
   3383 
   3384         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3385                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3386         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3387                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3388         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green),
   3389                 ((BitmapDrawable) drawables[TOP]).getBitmap());
   3390         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3391                 ((BitmapDrawable) drawables[BOTTOM]).getBitmap());
   3392 
   3393         // Case 3.1: left / right / start / end drawable defined in LTR mode
   3394         tv = (TextView) activity.findViewById(R.id.textview_drawable_3_1);
   3395         drawables = tv.getCompoundDrawables();
   3396 
   3397         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3398                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3399         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3400                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3401         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green),
   3402                 ((BitmapDrawable) drawables[TOP]).getBitmap());
   3403         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3404                 ((BitmapDrawable) drawables[BOTTOM]).getBitmap());
   3405 
   3406         // Case 3.2: left / right / start / end drawable defined in RTL mode
   3407         tv = (TextView) activity.findViewById(R.id.textview_drawable_3_2);
   3408         drawables = tv.getCompoundDrawables();
   3409 
   3410         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3411                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3412         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3413                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3414         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green),
   3415                 ((BitmapDrawable) drawables[TOP]).getBitmap());
   3416         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3417                 ((BitmapDrawable) drawables[BOTTOM]).getBitmap());
   3418 
   3419         // Case 4.1: start / end drawable defined in LTR mode inside a layout
   3420         // that defines the layout direction
   3421         tv = (TextView) activity.findViewById(R.id.textview_drawable_4_1);
   3422         drawables = tv.getCompoundDrawables();
   3423 
   3424         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3425                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3426         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3427                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3428         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green),
   3429                 ((BitmapDrawable) drawables[TOP]).getBitmap());
   3430         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3431                 ((BitmapDrawable) drawables[BOTTOM]).getBitmap());
   3432 
   3433         // Case 4.2: start / end drawable defined in RTL mode inside a layout
   3434         // that defines the layout direction
   3435         tv = (TextView) activity.findViewById(R.id.textview_drawable_4_2);
   3436         drawables = tv.getCompoundDrawables();
   3437 
   3438         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3439                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3440         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3441                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3442         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green),
   3443                 ((BitmapDrawable) drawables[TOP]).getBitmap());
   3444         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3445                 ((BitmapDrawable) drawables[BOTTOM]).getBitmap());
   3446 
   3447         // Case 5.1: left / right / start / end drawable defined in LTR mode inside a layout
   3448         // that defines the layout direction
   3449         tv = (TextView) activity.findViewById(R.id.textview_drawable_3_1);
   3450         drawables = tv.getCompoundDrawables();
   3451 
   3452         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3453                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3454         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3455                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3456         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green),
   3457                 ((BitmapDrawable) drawables[TOP]).getBitmap());
   3458         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3459                 ((BitmapDrawable) drawables[BOTTOM]).getBitmap());
   3460 
   3461         // Case 5.2: left / right / start / end drawable defined in RTL mode inside a layout
   3462         // that defines the layout direction
   3463         tv = (TextView) activity.findViewById(R.id.textview_drawable_3_2);
   3464         drawables = tv.getCompoundDrawables();
   3465 
   3466         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3467                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3468         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3469                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3470         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green),
   3471                 ((BitmapDrawable) drawables[TOP]).getBitmap());
   3472         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3473                 ((BitmapDrawable) drawables[BOTTOM]).getBitmap());
   3474     }
   3475 
   3476     @UiThreadTest
   3477     public void testDrawableResolution2() {
   3478         final int LEFT = 0;
   3479         final int TOP = 1;
   3480         final int RIGHT = 2;
   3481         final int BOTTOM = 3;
   3482 
   3483         TextViewCtsActivity activity = getActivity();
   3484 
   3485         // Case 1.1: left / right drawable defined in default LTR mode
   3486         TextView tv = (TextView) activity.findViewById(R.id.textview_drawable_1_1);
   3487         Drawable[] drawables = tv.getCompoundDrawables();
   3488 
   3489         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3490                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3491         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3492                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3493         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green),
   3494                 ((BitmapDrawable) drawables[TOP]).getBitmap());
   3495         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3496                 ((BitmapDrawable) drawables[BOTTOM]).getBitmap());
   3497 
   3498         tv.setCompoundDrawables(null, null, getDrawable(R.drawable.icon_yellow), null);
   3499         drawables = tv.getCompoundDrawables();
   3500 
   3501         assertNull(drawables[LEFT]);
   3502         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3503                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3504         assertNull(drawables[TOP]);
   3505         assertNull(drawables[BOTTOM]);
   3506 
   3507         tv = (TextView) activity.findViewById(R.id.textview_drawable_1_2);
   3508         drawables = tv.getCompoundDrawables();
   3509 
   3510         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3511                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3512         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3513                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3514         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green),
   3515                 ((BitmapDrawable) drawables[TOP]).getBitmap());
   3516         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3517                 ((BitmapDrawable) drawables[BOTTOM]).getBitmap());
   3518 
   3519         tv.setCompoundDrawables(getDrawable(R.drawable.icon_yellow), null, null, null);
   3520         drawables = tv.getCompoundDrawables();
   3521 
   3522         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3523                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3524         assertNull(drawables[RIGHT]);
   3525         assertNull(drawables[TOP]);
   3526         assertNull(drawables[BOTTOM]);
   3527 
   3528         tv = (TextView) activity.findViewById(R.id.textview_ltr);
   3529         drawables = tv.getCompoundDrawables();
   3530 
   3531         assertNull(drawables[LEFT]);
   3532         assertNull(drawables[RIGHT]);
   3533         assertNull(drawables[TOP]);
   3534         assertNull(drawables[BOTTOM]);
   3535 
   3536         tv.setCompoundDrawables(getDrawable(R.drawable.icon_blue), null, getDrawable(R.drawable.icon_red), null);
   3537         drawables = tv.getCompoundDrawables();
   3538 
   3539         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue),
   3540                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3541         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red),
   3542                 ((BitmapDrawable) drawables[RIGHT]).getBitmap());
   3543         assertNull(drawables[TOP]);
   3544         assertNull(drawables[BOTTOM]);
   3545 
   3546         tv.setCompoundDrawablesRelative(getDrawable(R.drawable.icon_yellow), null, null, null);
   3547         drawables = tv.getCompoundDrawables();
   3548 
   3549         WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow),
   3550                 ((BitmapDrawable) drawables[LEFT]).getBitmap());
   3551         assertNull(drawables[RIGHT]);
   3552         assertNull(drawables[TOP]);
   3553         assertNull(drawables[BOTTOM]);
   3554     }
   3555 
   3556     private static class MockOnEditorActionListener implements OnEditorActionListener {
   3557         private boolean isOnEditorActionCalled;
   3558 
   3559         public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
   3560             isOnEditorActionCalled = true;
   3561             return true;
   3562         }
   3563 
   3564         public boolean isOnEditorActionCalled() {
   3565             return isOnEditorActionCalled;
   3566         }
   3567     }
   3568 
   3569     private void layout(final TextView textView) {
   3570         mActivity.runOnUiThread(new Runnable() {
   3571             public void run() {
   3572                 mActivity.setContentView(textView);
   3573             }
   3574         });
   3575         mInstrumentation.waitForIdleSync();
   3576     }
   3577 
   3578     private void layout(final int layoutId) {
   3579         mActivity.runOnUiThread(new Runnable() {
   3580             public void run() {
   3581                 mActivity.setContentView(layoutId);
   3582             }
   3583         });
   3584         mInstrumentation.waitForIdleSync();
   3585     }
   3586 
   3587     private TextView findTextView(int id) {
   3588         return (TextView) mActivity.findViewById(id);
   3589     }
   3590 
   3591     private int getAutoLinkMask(int id) {
   3592         return findTextView(id).getAutoLinkMask();
   3593     }
   3594 
   3595     private Bitmap getBitmap(int resid) {
   3596         return ((BitmapDrawable) getDrawable(resid)).getBitmap();
   3597     }
   3598 
   3599     private Drawable getDrawable(int resid) {
   3600         return mActivity.getResources().getDrawable(resid);
   3601     }
   3602 
   3603     private void setMaxWidth(final int pixels) {
   3604         mActivity.runOnUiThread(new Runnable() {
   3605             public void run() {
   3606                 mTextView.setMaxWidth(pixels);
   3607             }
   3608         });
   3609         mInstrumentation.waitForIdleSync();
   3610     }
   3611 
   3612     private void setMinWidth(final int pixels) {
   3613         mActivity.runOnUiThread(new Runnable() {
   3614             public void run() {
   3615                 mTextView.setMinWidth(pixels);
   3616             }
   3617         });
   3618         mInstrumentation.waitForIdleSync();
   3619     }
   3620 
   3621     private void setMaxHeight(final int pixels) {
   3622         mActivity.runOnUiThread(new Runnable() {
   3623             public void run() {
   3624                 mTextView.setMaxHeight(pixels);
   3625             }
   3626         });
   3627         mInstrumentation.waitForIdleSync();
   3628     }
   3629 
   3630     private void setMinHeight(final int pixels) {
   3631         mActivity.runOnUiThread(new Runnable() {
   3632             public void run() {
   3633                 mTextView.setMinHeight(pixels);
   3634             }
   3635         });
   3636         mInstrumentation.waitForIdleSync();
   3637     }
   3638 
   3639     private void setMinLines(final int minlines) {
   3640         mActivity.runOnUiThread(new Runnable() {
   3641             public void run() {
   3642                 mTextView.setMinLines(minlines);
   3643             }
   3644         });
   3645         mInstrumentation.waitForIdleSync();
   3646     }
   3647 
   3648     /**
   3649      * Convenience for {@link TextView#setText(CharSequence, BufferType)}. And
   3650      * the buffer type is fixed to SPANNABLE.
   3651      *
   3652      * @param tv the text view
   3653      * @param content the content
   3654      */
   3655     private void setSpannableText(final TextView tv, final String content) {
   3656         mActivity.runOnUiThread(new Runnable() {
   3657             public void run() {
   3658                 tv.setText(content, BufferType.SPANNABLE);
   3659             }
   3660         });
   3661         mInstrumentation.waitForIdleSync();
   3662     }
   3663 
   3664     private void setLines(final int lines) {
   3665         mActivity.runOnUiThread(new Runnable() {
   3666             public void run() {
   3667                 mTextView.setLines(lines);
   3668             }
   3669         });
   3670         mInstrumentation.waitForIdleSync();
   3671     }
   3672 
   3673     private void setHorizontallyScrolling(final boolean whether) {
   3674         mActivity.runOnUiThread(new Runnable() {
   3675             public void run() {
   3676                 mTextView.setHorizontallyScrolling(whether);
   3677             }
   3678         });
   3679         mInstrumentation.waitForIdleSync();
   3680     }
   3681 
   3682     private void setWidth(final int pixels) {
   3683         mActivity.runOnUiThread(new Runnable() {
   3684             public void run() {
   3685                 mTextView.setWidth(pixels);
   3686             }
   3687         });
   3688         mInstrumentation.waitForIdleSync();
   3689     }
   3690 
   3691     private void setHeight(final int pixels) {
   3692         mActivity.runOnUiThread(new Runnable() {
   3693             public void run() {
   3694                 mTextView.setHeight(pixels);
   3695             }
   3696         });
   3697         mInstrumentation.waitForIdleSync();
   3698     }
   3699 
   3700     private void setMinEms(final int ems) {
   3701         mActivity.runOnUiThread(new Runnable() {
   3702             public void run() {
   3703                 mTextView.setMinEms(ems);
   3704             }
   3705         });
   3706         mInstrumentation.waitForIdleSync();
   3707     }
   3708 
   3709     private void setMaxEms(final int ems) {
   3710         mActivity.runOnUiThread(new Runnable() {
   3711             public void run() {
   3712                 mTextView.setMaxEms(ems);
   3713             }
   3714         });
   3715         mInstrumentation.waitForIdleSync();
   3716     }
   3717 
   3718     private void setEms(final int ems) {
   3719         mActivity.runOnUiThread(new Runnable() {
   3720             public void run() {
   3721                 mTextView.setEms(ems);
   3722             }
   3723         });
   3724         mInstrumentation.waitForIdleSync();
   3725     }
   3726 
   3727     private void setLineSpacing(final float add, final float mult) {
   3728         mActivity.runOnUiThread(new Runnable() {
   3729             public void run() {
   3730                 mTextView.setLineSpacing(add, mult);
   3731             }
   3732         });
   3733         mInstrumentation.waitForIdleSync();
   3734     }
   3735 
   3736     private static abstract class TestSelectedRunnable implements Runnable {
   3737         private TextView mTextView;
   3738         private boolean mIsSelected1;
   3739         private boolean mIsSelected2;
   3740 
   3741         public TestSelectedRunnable(TextView textview) {
   3742             mTextView = textview;
   3743         }
   3744 
   3745         public boolean getIsSelected1() {
   3746             return mIsSelected1;
   3747         }
   3748 
   3749         public boolean getIsSelected2() {
   3750             return mIsSelected2;
   3751         }
   3752 
   3753         public void saveIsSelected1() {
   3754             mIsSelected1 = mTextView.isSelected();
   3755         }
   3756 
   3757         public void saveIsSelected2() {
   3758             mIsSelected2 = mTextView.isSelected();
   3759         }
   3760     }
   3761 
   3762     private static abstract class TestLayoutRunnable implements Runnable {
   3763         private TextView mTextView;
   3764         private Layout mLayout;
   3765 
   3766         public TestLayoutRunnable(TextView textview) {
   3767             mTextView = textview;
   3768         }
   3769 
   3770         public Layout getLayout() {
   3771             return mLayout;
   3772         }
   3773 
   3774         public void saveLayout() {
   3775             mLayout = mTextView.getLayout();
   3776         }
   3777     }
   3778 
   3779     private class MockEditableFactory extends Editable.Factory {
   3780         private boolean mhasCalledNewEditable;
   3781         private CharSequence mSource;
   3782 
   3783         public boolean hasCalledNewEditable() {
   3784             return mhasCalledNewEditable;
   3785         }
   3786 
   3787         public void reset() {
   3788             mhasCalledNewEditable = false;
   3789             mSource = null;
   3790         }
   3791 
   3792         public CharSequence getSource() {
   3793             return mSource;
   3794         }
   3795 
   3796         @Override
   3797         public Editable newEditable(CharSequence source) {
   3798             mhasCalledNewEditable = true;
   3799             mSource = source;
   3800             return super.newEditable(source);
   3801         }
   3802     }
   3803 
   3804     private class MockSpannableFactory extends Spannable.Factory {
   3805         private boolean mHasCalledNewSpannable;
   3806         private CharSequence mSource;
   3807 
   3808         public boolean hasCalledNewSpannable() {
   3809             return mHasCalledNewSpannable;
   3810         }
   3811 
   3812         public void reset() {
   3813             mHasCalledNewSpannable = false;
   3814             mSource = null;
   3815         }
   3816 
   3817         public CharSequence getSource() {
   3818             return mSource;
   3819         }
   3820 
   3821         @Override
   3822         public Spannable newSpannable(CharSequence source) {
   3823             mHasCalledNewSpannable = true;
   3824             mSource = source;
   3825             return super.newSpannable(source);
   3826         }
   3827     }
   3828 
   3829     private static class MockTextWatcher implements TextWatcher {
   3830         private boolean mHasCalledAfterTextChanged;
   3831         private boolean mHasCalledBeforeTextChanged;
   3832         private boolean mHasOnTextChanged;
   3833 
   3834         public void reset(){
   3835             mHasCalledAfterTextChanged = false;
   3836             mHasCalledBeforeTextChanged = false;
   3837             mHasOnTextChanged = false;
   3838         }
   3839 
   3840         public boolean hasCalledAfterTextChanged() {
   3841             return mHasCalledAfterTextChanged;
   3842         }
   3843 
   3844         public boolean hasCalledBeforeTextChanged() {
   3845             return mHasCalledBeforeTextChanged;
   3846         }
   3847 
   3848         public boolean hasCalledOnTextChanged() {
   3849             return mHasOnTextChanged;
   3850         }
   3851 
   3852         public void afterTextChanged(Editable s) {
   3853             mHasCalledAfterTextChanged = true;
   3854         }
   3855 
   3856         public void beforeTextChanged(CharSequence s, int start, int count, int after) {
   3857             mHasCalledBeforeTextChanged = true;
   3858         }
   3859 
   3860         public void onTextChanged(CharSequence s, int start, int before, int count) {
   3861             mHasOnTextChanged = true;
   3862         }
   3863     }
   3864 
   3865     /**
   3866      * The listener interface for receiving mockOnLongClick events. The class
   3867      * that is interested in processing a mockOnLongClick event implements this
   3868      * interface, and the object created with that class is registered with a
   3869      * component using the component's
   3870      * <code>addMockOnLongClickListener<code> method. When
   3871      * the mockOnLongClick event occurs, that object's appropriate
   3872      * method is invoked.
   3873      *
   3874      * @see MockOnLongClickEvent
   3875      */
   3876     private static class MockOnLongClickListener implements OnLongClickListener {
   3877         private boolean mExpectedOnLongClickResult;
   3878         private boolean mHasLongClicked;
   3879 
   3880         MockOnLongClickListener(boolean result) {
   3881             mExpectedOnLongClickResult = result;
   3882         }
   3883 
   3884         public boolean hasLongClicked() {
   3885             return mHasLongClicked;
   3886         }
   3887 
   3888         public boolean onLongClick(View v) {
   3889             mHasLongClicked = true;
   3890             return mExpectedOnLongClickResult;
   3891         }
   3892     }
   3893 
   3894     /**
   3895      * The listener interface for receiving mockOnCreateContextMenu events. The
   3896      * class that is interested in processing a mockOnCreateContextMenu event
   3897      * implements this interface, and the object created with that class is
   3898      * registered with a component using the component's
   3899      * <code>addMockOnCreateContextMenuListener<code> method. When the
   3900      * mockOnCreateContextMenu event occurs, that object's appropriate method is
   3901      * invoked.
   3902      *
   3903      * @see MockOnCreateContextMenuEvent
   3904      */
   3905     private static class MockOnCreateContextMenuListener implements OnCreateContextMenuListener {
   3906         private boolean mIsMenuItemsBlank;
   3907         private boolean mHasCreatedContextMenu;
   3908 
   3909         MockOnCreateContextMenuListener(boolean isBlank) {
   3910             this.mIsMenuItemsBlank = isBlank;
   3911         }
   3912 
   3913         public boolean hasCreatedContextMenu() {
   3914             return mHasCreatedContextMenu;
   3915         }
   3916 
   3917         public void reset() {
   3918             mHasCreatedContextMenu = false;
   3919         }
   3920 
   3921         public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
   3922             mHasCreatedContextMenu = true;
   3923             if (!mIsMenuItemsBlank) {
   3924                 menu.add("menu item");
   3925             }
   3926         }
   3927     }
   3928 }
   3929