Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import static java.util.Arrays.asList;
      4 import static org.assertj.core.api.Assertions.assertThat;
      5 import static org.junit.Assert.assertEquals;
      6 import static org.junit.Assert.assertFalse;
      7 import static org.junit.Assert.assertNotNull;
      8 import static org.junit.Assert.assertNull;
      9 import static org.junit.Assert.assertSame;
     10 import static org.junit.Assert.assertTrue;
     11 import static org.mockito.Matchers.eq;
     12 import static org.mockito.Mockito.mock;
     13 import static org.mockito.Mockito.verify;
     14 import static org.robolectric.Robolectric.buildActivity;
     15 import static org.robolectric.Shadows.shadowOf;
     16 
     17 import android.app.Activity;
     18 import android.graphics.Typeface;
     19 import android.text.Editable;
     20 import android.text.InputFilter;
     21 import android.text.InputType;
     22 import android.text.Selection;
     23 import android.text.Spannable;
     24 import android.text.SpannableStringBuilder;
     25 import android.text.TextWatcher;
     26 import android.text.method.ArrowKeyMovementMethod;
     27 import android.text.method.MovementMethod;
     28 import android.text.method.PasswordTransformationMethod;
     29 import android.text.style.URLSpan;
     30 import android.text.util.Linkify;
     31 import android.util.TypedValue;
     32 import android.view.Gravity;
     33 import android.view.KeyEvent;
     34 import android.view.MotionEvent;
     35 import android.view.inputmethod.EditorInfo;
     36 import android.widget.FrameLayout;
     37 import android.widget.TextView;
     38 import java.util.ArrayList;
     39 import java.util.List;
     40 import java.util.Locale;
     41 import java.util.Random;
     42 import org.junit.Before;
     43 import org.junit.Test;
     44 import org.junit.runner.RunWith;
     45 import org.mockito.ArgumentCaptor;
     46 import org.robolectric.R;
     47 import org.robolectric.RobolectricTestRunner;
     48 import org.robolectric.RuntimeEnvironment;
     49 import org.robolectric.android.controller.ActivityController;
     50 import org.robolectric.shadow.api.Shadow;
     51 
     52 @RunWith(RobolectricTestRunner.class)
     53 public class ShadowTextViewTest {
     54 
     55   private static final String INITIAL_TEXT = "initial text";
     56   private static final String NEW_TEXT = "new text";
     57   private TextView textView;
     58   private ActivityController<Activity> activityController;
     59 
     60   @Before
     61   public void setUp() throws Exception {
     62     activityController = buildActivity(Activity.class);
     63     Activity activity = activityController.create().get();
     64     textView = new TextView(activity);
     65     activity.setContentView(textView);
     66     activityController.start().resume().visible();
     67   }
     68 
     69   @Test
     70   public void shouldTriggerTheImeListener() {
     71     TestOnEditorActionListener actionListener = new TestOnEditorActionListener();
     72     textView.setOnEditorActionListener(actionListener);
     73 
     74     textView.onEditorAction(EditorInfo.IME_ACTION_GO);
     75 
     76     assertThat(actionListener.textView).isSameAs(textView);
     77     assertThat(actionListener.sentImeId).isEqualTo(EditorInfo.IME_ACTION_GO);
     78   }
     79 
     80   @Test
     81   public void shouldCreateGetterForEditorActionListener() {
     82     TestOnEditorActionListener actionListener = new TestOnEditorActionListener();
     83 
     84     textView.setOnEditorActionListener(actionListener);
     85 
     86     assertThat(shadowOf(textView).getOnEditorActionListener()).isSameAs(actionListener);
     87   }
     88 
     89   @Test
     90   public void testGetUrls() throws Exception {
     91     Locale.setDefault(Locale.ENGLISH);
     92     textView.setAutoLinkMask(Linkify.ALL);
     93     textView.setText("here's some text http://google.com/\nblah\thttp://another.com/123?456 blah");
     94 
     95     assertThat(urlStringsFrom(textView.getUrls())).isEqualTo(asList(
     96             "http://google.com",
     97             "http://another.com/123?456"
     98     ));
     99   }
    100 
    101   @Test
    102   public void testGetGravity() throws Exception {
    103     assertThat(textView.getGravity()).isNotEqualTo(Gravity.CENTER);
    104     textView.setGravity(Gravity.CENTER);
    105     assertThat(textView.getGravity()).isEqualTo(Gravity.CENTER);
    106   }
    107 
    108   @Test
    109   public void testMovementMethod() {
    110     MovementMethod movement = new ArrowKeyMovementMethod();
    111 
    112     assertNull(textView.getMovementMethod());
    113     textView.setMovementMethod(movement);
    114     assertThat(textView.getMovementMethod()).isSameAs(movement);
    115   }
    116 
    117   @Test
    118   public void testLinksClickable() {
    119     assertThat(textView.getLinksClickable()).isTrue();
    120 
    121     textView.setLinksClickable(false);
    122     assertThat(textView.getLinksClickable()).isFalse();
    123 
    124     textView.setLinksClickable(true);
    125     assertThat(textView.getLinksClickable()).isTrue();
    126   }
    127 
    128   @Test
    129   public void testGetTextAppearanceId() throws Exception {
    130     textView.setTextAppearance(RuntimeEnvironment.application, android.R.style.TextAppearance_Small);
    131 
    132     assertThat(shadowOf(textView).getTextAppearanceId()).isEqualTo(android.R.style.TextAppearance_Small);
    133   }
    134 
    135   @Test
    136   public void shouldSetTextAndTextColorWhileInflatingXmlLayout() throws Exception {
    137     Activity activity = activityController.get();
    138     activity.setContentView(R.layout.text_views);
    139 
    140     TextView black = (TextView) activity.findViewById(R.id.black_text_view);
    141     assertThat(black.getText().toString()).isEqualTo("Black Text");
    142     assertThat(black.getCurrentTextColor()).isEqualTo(0xff000000);
    143 
    144     TextView white = (TextView) activity.findViewById(R.id.white_text_view);
    145     assertThat(white.getText().toString()).isEqualTo("White Text");
    146     assertThat(white.getCurrentTextColor()).isEqualTo(activity.getResources().getColor(android.R.color.white));
    147 
    148     TextView grey = (TextView) activity.findViewById(R.id.grey_text_view);
    149     assertThat(grey.getText().toString()).isEqualTo("Grey Text");
    150     assertThat(grey.getCurrentTextColor()).isEqualTo(activity.getResources().getColor(R.color.grey42));
    151   }
    152 
    153   @Test
    154   public void shouldSetHintAndHintColorWhileInflatingXmlLayout() throws Exception {
    155     Activity activity = activityController.get();
    156     activity.setContentView(R.layout.text_views_hints);
    157 
    158     TextView black = (TextView) activity.findViewById(R.id.black_text_view_hint);
    159     assertThat(black.getHint().toString()).isEqualTo("Black Hint");
    160     assertThat(black.getCurrentHintTextColor()).isEqualTo(0xff000000);
    161 
    162     TextView white = (TextView) activity.findViewById(R.id.white_text_view_hint);
    163     assertThat(white.getHint().toString()).isEqualTo("White Hint");
    164     assertThat(white.getCurrentHintTextColor()).isEqualTo(activity.getResources().getColor(android.R.color.white));
    165 
    166     TextView grey = (TextView) activity.findViewById(R.id.grey_text_view_hint);
    167     assertThat(grey.getHint().toString()).isEqualTo("Grey Hint");
    168     assertThat(grey.getCurrentHintTextColor()).isEqualTo(activity.getResources().getColor(R.color.grey42));
    169   }
    170 
    171   @Test
    172   public void shouldNotHaveTransformationMethodByDefault() {
    173     assertThat(textView.getTransformationMethod()).isNull();
    174   }
    175 
    176   @Test
    177   public void shouldAllowSettingATransformationMethod() {
    178     textView.setTransformationMethod(PasswordTransformationMethod.getInstance());
    179     assertThat(textView.getTransformationMethod()).isInstanceOf(PasswordTransformationMethod.class);
    180   }
    181 
    182   @Test
    183   public void testGetInputType() throws Exception {
    184     assertThat(textView.getInputType()).isNotEqualTo(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    185     textView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    186     assertThat(textView.getInputType()).isEqualTo(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    187   }
    188 
    189   @Test
    190   public void givenATextViewWithATextWatcherAdded_WhenSettingTextWithTextResourceId_ShouldNotifyTextWatcher() {
    191     MockTextWatcher mockTextWatcher = new MockTextWatcher();
    192     textView.addTextChangedListener(mockTextWatcher);
    193 
    194     textView.setText(R.string.hello);
    195 
    196     assertEachTextWatcherEventWasInvoked(mockTextWatcher);
    197   }
    198 
    199   @Test
    200   public void givenATextViewWithATextWatcherAdded_WhenSettingTextWithCharSequence_ShouldNotifyTextWatcher() {
    201     MockTextWatcher mockTextWatcher = new MockTextWatcher();
    202     textView.addTextChangedListener(mockTextWatcher);
    203 
    204     textView.setText("text");
    205 
    206     assertEachTextWatcherEventWasInvoked(mockTextWatcher);
    207   }
    208 
    209   @Test
    210   public void givenATextViewWithATextWatcherAdded_WhenSettingNullText_ShouldNotifyTextWatcher() {
    211     MockTextWatcher mockTextWatcher = new MockTextWatcher();
    212     textView.addTextChangedListener(mockTextWatcher);
    213 
    214     textView.setText(null);
    215 
    216     assertEachTextWatcherEventWasInvoked(mockTextWatcher);
    217   }
    218 
    219   @Test
    220   public void givenATextViewWithMultipleTextWatchersAdded_WhenSettingText_ShouldNotifyEachTextWatcher() {
    221     List<MockTextWatcher> mockTextWatchers = anyNumberOfTextWatchers();
    222     for (MockTextWatcher textWatcher : mockTextWatchers) {
    223       textView.addTextChangedListener(textWatcher);
    224     }
    225 
    226     textView.setText("text");
    227 
    228     for (MockTextWatcher textWatcher : mockTextWatchers) {
    229       assertEachTextWatcherEventWasInvoked(textWatcher);
    230     }
    231   }
    232 
    233   @Test
    234   public void whenSettingText_ShouldFireBeforeTextChangedWithCorrectArguments() {
    235     textView.setText(INITIAL_TEXT);
    236     TextWatcher mockTextWatcher = mock(TextWatcher.class);
    237     textView.addTextChangedListener(mockTextWatcher);
    238 
    239     textView.setText(NEW_TEXT);
    240 
    241     verify(mockTextWatcher).beforeTextChanged(INITIAL_TEXT, 0, INITIAL_TEXT.length(), NEW_TEXT.length());
    242   }
    243 
    244   @Test
    245   public void whenSettingText_ShouldFireOnTextChangedWithCorrectArguments() {
    246     textView.setText(INITIAL_TEXT);
    247     TextWatcher mockTextWatcher = mock(TextWatcher.class);
    248     textView.addTextChangedListener(mockTextWatcher);
    249 
    250     textView.setText(NEW_TEXT);
    251 
    252     ArgumentCaptor<SpannableStringBuilder> builderCaptor = ArgumentCaptor.forClass(SpannableStringBuilder.class);
    253     verify(mockTextWatcher).onTextChanged(builderCaptor.capture(), eq(0), eq(INITIAL_TEXT.length()), eq(NEW_TEXT.length()));
    254     assertThat(builderCaptor.getValue().toString()).isEqualTo(NEW_TEXT);
    255   }
    256 
    257   @Test
    258   public void whenSettingText_ShouldFireAfterTextChangedWithCorrectArgument() {
    259     MockTextWatcher mockTextWatcher = new MockTextWatcher();
    260     textView.addTextChangedListener(mockTextWatcher);
    261 
    262     textView.setText(NEW_TEXT);
    263 
    264     assertThat(mockTextWatcher.afterTextChangeArgument.toString()).isEqualTo(NEW_TEXT);
    265   }
    266 
    267   @Test
    268   public void whenAppendingText_ShouldAppendNewTextAfterOldOne() {
    269     textView.setText(INITIAL_TEXT);
    270     textView.append(NEW_TEXT);
    271 
    272     assertThat(textView.getText().toString()).isEqualTo(INITIAL_TEXT + NEW_TEXT);
    273   }
    274 
    275   @Test
    276   public void whenAppendingText_ShouldFireBeforeTextChangedWithCorrectArguments() {
    277     textView.setText(INITIAL_TEXT);
    278     TextWatcher mockTextWatcher = mock(TextWatcher.class);
    279     textView.addTextChangedListener(mockTextWatcher);
    280 
    281     textView.append(NEW_TEXT);
    282 
    283     verify(mockTextWatcher).beforeTextChanged(eq(INITIAL_TEXT), eq(0), eq(INITIAL_TEXT.length()), eq(INITIAL_TEXT.length()));
    284   }
    285 
    286   @Test
    287   public void whenAppendingText_ShouldFireOnTextChangedWithCorrectArguments() {
    288     textView.setText(INITIAL_TEXT);
    289     TextWatcher mockTextWatcher = mock(TextWatcher.class);
    290     textView.addTextChangedListener(mockTextWatcher);
    291 
    292     textView.append(NEW_TEXT);
    293 
    294     ArgumentCaptor<SpannableStringBuilder> builderCaptor = ArgumentCaptor.forClass(SpannableStringBuilder.class);
    295     verify(mockTextWatcher).onTextChanged(builderCaptor.capture(), eq(0), eq(INITIAL_TEXT.length()), eq(INITIAL_TEXT.length()));
    296     assertThat(builderCaptor.getValue().toString()).isEqualTo(INITIAL_TEXT + NEW_TEXT);
    297   }
    298 
    299   @Test
    300   public void whenAppendingText_ShouldFireAfterTextChangedWithCorrectArgument() {
    301     textView.setText(INITIAL_TEXT);
    302     MockTextWatcher mockTextWatcher = new MockTextWatcher();
    303     textView.addTextChangedListener(mockTextWatcher);
    304 
    305     textView.append(NEW_TEXT);
    306 
    307     assertThat(mockTextWatcher.afterTextChangeArgument.toString()).isEqualTo(INITIAL_TEXT + NEW_TEXT);
    308   }
    309 
    310   @Test
    311   public void removeTextChangedListener_shouldRemoveTheListener() throws Exception {
    312     MockTextWatcher watcher = new MockTextWatcher();
    313     textView.addTextChangedListener(watcher);
    314     assertTrue(shadowOf(textView).getWatchers().contains(watcher));
    315 
    316     textView.removeTextChangedListener(watcher);
    317     assertFalse(shadowOf(textView).getWatchers().contains(watcher));
    318   }
    319 
    320   @Test
    321   public void getPaint_returnsMeasureTextEnabledObject() throws Exception {
    322     assertThat(textView.getPaint().measureText("12345")).isEqualTo(5f);
    323   }
    324 
    325   @Test
    326   public void append_whenSelectionIsAtTheEnd_shouldKeepSelectionAtTheEnd() throws Exception {
    327     textView.setText("1", TextView.BufferType.EDITABLE);
    328     Selection.setSelection(textView.getEditableText(), 0, 0);
    329     textView.append("2");
    330     assertEquals(0, textView.getSelectionEnd());
    331     assertEquals(0, textView.getSelectionStart());
    332 
    333     Selection.setSelection(textView.getEditableText(), 2, 2);
    334     textView.append("3");
    335     assertEquals(3, textView.getSelectionEnd());
    336     assertEquals(3, textView.getSelectionStart());
    337   }
    338 
    339   @Test
    340   public void append_whenSelectionReachesToEnd_shouldExtendSelectionToTheEnd() throws Exception {
    341     textView.setText("12", TextView.BufferType.EDITABLE);
    342     Selection.setSelection(textView.getEditableText(), 0, 2);
    343     textView.append("3");
    344     assertEquals(3, textView.getSelectionEnd());
    345     assertEquals(0, textView.getSelectionStart());
    346   }
    347 
    348   @Test
    349   public void testSetCompountDrawablesWithIntrinsicBounds_int_shouldCreateDrawablesWithResourceIds() throws Exception {
    350     textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.an_image, R.drawable.an_other_image, R.drawable.third_image, R.drawable.fourth_image);
    351 
    352     assertEquals(R.drawable.an_image, shadowOf(textView.getCompoundDrawables()[0]).getCreatedFromResId());
    353     assertEquals(R.drawable.an_other_image, shadowOf(textView.getCompoundDrawables()[1]).getCreatedFromResId());
    354     assertEquals(R.drawable.third_image, shadowOf(textView.getCompoundDrawables()[2]).getCreatedFromResId());
    355     assertEquals(R.drawable.fourth_image, shadowOf(textView.getCompoundDrawables()[3]).getCreatedFromResId());
    356   }
    357 
    358   @Test
    359   public void testSetCompountDrawablesWithIntrinsicBounds_int_shouldNotCreateDrawablesForZero() throws Exception {
    360     textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
    361 
    362     assertNull(textView.getCompoundDrawables()[0]);
    363     assertNull(textView.getCompoundDrawables()[1]);
    364     assertNull(textView.getCompoundDrawables()[2]);
    365     assertNull(textView.getCompoundDrawables()[3]);
    366   }
    367 
    368   @Test
    369   public void canSetAndGetTypeface() throws Exception {
    370     Typeface typeface = Shadow.newInstanceOf(Typeface.class);
    371     textView.setTypeface(typeface);
    372     assertSame(typeface, textView.getTypeface());
    373   }
    374 
    375   @Test
    376   public void onTouchEvent_shouldCallMovementMethodOnTouchEventWithSetMotionEvent() throws Exception {
    377     TestMovementMethod testMovementMethod = new TestMovementMethod();
    378     textView.setMovementMethod(testMovementMethod);
    379     textView.setLayoutParams(new FrameLayout.LayoutParams(100, 100));
    380     textView.measure(100, 100);
    381 
    382     MotionEvent event = MotionEvent.obtain(0, 0, 0, 0, 0, 0);
    383     textView.dispatchTouchEvent(event);
    384 
    385     assertEquals(testMovementMethod.event, event);
    386   }
    387 
    388   @Test
    389   public void testGetError() {
    390     assertNull(textView.getError());
    391     CharSequence error = "myError";
    392     textView.setError(error);
    393     assertEquals(error, textView.getError());
    394   }
    395 
    396   @Test
    397   public void canSetAndGetInputFilters() throws Exception {
    398     final InputFilter[] expectedFilters = new InputFilter[]{new InputFilter.LengthFilter(1)};
    399     textView.setFilters(expectedFilters);
    400     assertThat(textView.getFilters()).isSameAs(expectedFilters);
    401   }
    402 
    403   @Test
    404   public void testHasSelectionReturnsTrue() {
    405     textView.setText("1", TextView.BufferType.SPANNABLE);
    406     textView.onTextContextMenuItem(android.R.id.selectAll);
    407     assertTrue(textView.hasSelection());
    408   }
    409 
    410   @Test
    411   public void testHasSelectionReturnsFalse() {
    412     textView.setText("1", TextView.BufferType.SPANNABLE);
    413     assertFalse(textView.hasSelection());
    414   }
    415 
    416   @Test
    417   public void whenSettingTextToNull_WatchersSeeEmptyString() {
    418     TextWatcher mockTextWatcher = mock(TextWatcher.class);
    419     textView.addTextChangedListener(mockTextWatcher);
    420 
    421     textView.setText(null);
    422 
    423     ArgumentCaptor<SpannableStringBuilder> builderCaptor = ArgumentCaptor.forClass(SpannableStringBuilder.class);
    424     verify(mockTextWatcher).onTextChanged(builderCaptor.capture(), eq(0), eq(0), eq(0));
    425     assertThat(builderCaptor.getValue().toString()).isEmpty();
    426   }
    427 
    428   @Test
    429   public void getPaint_returnsNonNull() {
    430     assertNotNull(textView.getPaint());
    431   }
    432 
    433   @Test
    434   public void testNoArgAppend() {
    435     textView.setText("a");
    436     textView.append("b");
    437     assertThat(textView.getText().toString()).isEqualTo("ab");
    438   }
    439 
    440   @Test
    441   public void setTextSize_shouldHandleDips() throws Exception {
    442     shadowOf(RuntimeEnvironment.application.getResources()).setDensity(1.5f);
    443     textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);
    444     assertThat(textView.getTextSize()).isEqualTo(15f);
    445     textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
    446     assertThat(textView.getTextSize()).isEqualTo(30f);
    447   }
    448 
    449   @Test
    450   public void setTextSize_shouldHandleSp() throws Exception {
    451     textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
    452     assertThat(textView.getTextSize()).isEqualTo(10f);
    453 
    454     shadowOf(RuntimeEnvironment.application.getResources()).setScaledDensity(1.5f);
    455 
    456     textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
    457     assertThat(textView.getTextSize()).isEqualTo(15f);
    458   }
    459 
    460   @Test
    461   public void setTextSize_shouldHandlePixels() throws Exception {
    462     shadowOf(RuntimeEnvironment.application.getResources()).setDensity(1.5f);
    463     textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 10);
    464     assertThat(textView.getTextSize()).isEqualTo(10f);
    465     textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 20);
    466     assertThat(textView.getTextSize()).isEqualTo(20f);
    467   }
    468 
    469   @Test
    470   public void getPaintFlagsAndSetPaintFlags_shouldWork() {
    471     assertThat(textView.getPaintFlags()).isEqualTo(0);
    472     textView.setPaintFlags(100);
    473     assertThat(textView.getPaintFlags()).isEqualTo(100);
    474   }
    475 
    476   @Test
    477   public void setCompoundDrawablesWithIntrinsicBounds_setsValues() {
    478     textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.l0_red, R.drawable.l1_orange, R.drawable.l2_yellow, R.drawable.l3_green);
    479     assertThat(shadowOf(textView).getCompoundDrawablesWithIntrinsicBoundsLeft()).isEqualTo(R.drawable.l0_red);
    480     assertThat(shadowOf(textView).getCompoundDrawablesWithIntrinsicBoundsTop()).isEqualTo(R.drawable.l1_orange);
    481     assertThat(shadowOf(textView).getCompoundDrawablesWithIntrinsicBoundsRight()).isEqualTo(R.drawable.l2_yellow);
    482     assertThat(shadowOf(textView).getCompoundDrawablesWithIntrinsicBoundsBottom()).isEqualTo(R.drawable.l3_green);
    483   }
    484 
    485   private List<MockTextWatcher> anyNumberOfTextWatchers() {
    486     List<MockTextWatcher> mockTextWatchers = new ArrayList<>();
    487     int numberBetweenOneAndTen = new Random().nextInt(10) + 1;
    488     for (int i = 0; i < numberBetweenOneAndTen; i++) {
    489       mockTextWatchers.add(new MockTextWatcher());
    490     }
    491     return mockTextWatchers;
    492   }
    493 
    494   private void assertEachTextWatcherEventWasInvoked(MockTextWatcher mockTextWatcher) {
    495     assertTrue("Expected each TextWatcher event to have been invoked once", mockTextWatcher.methodsCalled.size() == 3);
    496 
    497     assertThat(mockTextWatcher.methodsCalled.get(0)).isEqualTo("beforeTextChanged");
    498     assertThat(mockTextWatcher.methodsCalled.get(1)).isEqualTo("onTextChanged");
    499     assertThat(mockTextWatcher.methodsCalled.get(2)).isEqualTo("afterTextChanged");
    500   }
    501 
    502   private List<String> urlStringsFrom(URLSpan[] urlSpans) {
    503     List<String> urls = new ArrayList<>();
    504     for (URLSpan urlSpan : urlSpans) {
    505       urls.add(urlSpan.getURL());
    506     }
    507     return urls;
    508   }
    509 
    510   private static class TestOnEditorActionListener implements TextView.OnEditorActionListener {
    511     private TextView textView;
    512     private int sentImeId;
    513 
    514     @Override
    515     public boolean onEditorAction(TextView textView, int sentImeId, KeyEvent keyEvent) {
    516       this.textView = textView;
    517       this.sentImeId = sentImeId;
    518       return false;
    519     }
    520   }
    521 
    522   private static class MockTextWatcher implements TextWatcher {
    523 
    524     List<String> methodsCalled = new ArrayList<>();
    525     Editable afterTextChangeArgument;
    526 
    527     @Override
    528     public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    529       methodsCalled.add("beforeTextChanged");
    530     }
    531 
    532     @Override
    533     public void onTextChanged(CharSequence s, int start, int before, int count) {
    534       methodsCalled.add("onTextChanged");
    535     }
    536 
    537     @Override
    538     public void afterTextChanged(Editable s) {
    539       methodsCalled.add("afterTextChanged");
    540       afterTextChangeArgument = s;
    541     }
    542 
    543   }
    544 
    545   private static class TestMovementMethod implements MovementMethod {
    546     public MotionEvent event;
    547     public boolean touchEventWasCalled;
    548 
    549     @Override
    550     public void initialize(TextView widget, Spannable text) {
    551     }
    552 
    553     @Override
    554     public boolean onKeyDown(TextView widget, Spannable text, int keyCode, KeyEvent event) {
    555       return false;
    556     }
    557 
    558     @Override
    559     public boolean onKeyUp(TextView widget, Spannable text, int keyCode, KeyEvent event) {
    560       return false;
    561     }
    562 
    563     @Override
    564     public boolean onKeyOther(TextView view, Spannable text, KeyEvent event) {
    565       return false;
    566     }
    567 
    568     @Override
    569     public void onTakeFocus(TextView widget, Spannable text, int direction) {
    570     }
    571 
    572     @Override
    573     public boolean onTrackballEvent(TextView widget, Spannable text, MotionEvent event) {
    574       return false;
    575     }
    576 
    577     @Override
    578     public boolean onTouchEvent(TextView widget, Spannable text, MotionEvent event) {
    579       this.event = event;
    580       touchEventWasCalled = true;
    581       return false;
    582     }
    583 
    584     @Override
    585     public boolean canSelectArbitrarily() {
    586       return false;
    587     }
    588 
    589     @Override
    590     public boolean onGenericMotionEvent(TextView widget, Spannable text,
    591                                         MotionEvent event) {
    592       return false;
    593     }
    594   }
    595 }