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