133873d2b41a5cd2597b20a4e88eb8942944c0f23Tyler Schultzpackage com.xtremelabs.robolectric.shadows;
2d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips
3d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richardimport android.app.Activity;
4ac2aa24a0f43988de36443e0beb8c2e0aacf2ceePhil Goodwin & Rick Kawalaimport android.graphics.Typeface;
54dda01d613890b88a9370e46ec53ef54c123f9fbAndrew Dai & Rick Kawalaimport android.text.*;
6aa03b8dbd148420164db6b62f143ea0dc7710217Rich Humphreyimport android.text.method.ArrowKeyMovementMethod;
7aa03b8dbd148420164db6b62f143ea0dc7710217Rich Humphreyimport android.text.method.MovementMethod;
8d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillipsimport android.text.style.URLSpan;
9d698df6daecfc469b84ace2c7fdaff79e85f21eaKathy Linimport android.view.Gravity;
109d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultzimport android.view.KeyEvent;
11085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perryimport android.view.MotionEvent;
129d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultzimport android.view.inputmethod.EditorInfo;
13d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillipsimport android.widget.TextView;
14d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richardimport com.xtremelabs.robolectric.R;
15ac2aa24a0f43988de36443e0beb8c2e0aacf2ceePhil Goodwin & Rick Kawalaimport com.xtremelabs.robolectric.Robolectric;
1607257335f88da163910cc0bd039e6163edc38580Gregg Van Hove & Phil Goodwinimport com.xtremelabs.robolectric.WithTestDefaultsRunner;
17e429b89e0edf6b77099f40e7e3af40b0cc1e7fc4Amrit Thakur & Ryan Richardimport junit.framework.Assert;
18ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultzimport org.hamcrest.CoreMatchers;
19ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultzimport org.junit.Before;
20ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultzimport org.junit.Test;
21ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultzimport org.junit.runner.RunWith;
22ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultz
23085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perryimport java.util.ArrayList;
24085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perryimport java.util.List;
25085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perryimport java.util.Random;
26ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultz
27ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultzimport static com.xtremelabs.robolectric.Robolectric.shadowOf;
28ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultzimport static java.util.Arrays.asList;
29c7ce4d9b6d4f56b6348cb3340d4332a746e0c190Phil Goodwin & Ryan Richardimport static junit.framework.Assert.assertFalse;
30085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perryimport static org.hamcrest.CoreMatchers.*;
31085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perryimport static org.junit.Assert.*;
32ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultzimport static org.mockito.Mockito.mock;
33ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultzimport static org.mockito.Mockito.verify;
34d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips
3507257335f88da163910cc0bd039e6163edc38580Gregg Van Hove & Phil Goodwin@RunWith(WithTestDefaultsRunner.class)
36d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillipspublic class TextViewTest {
377598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz
38ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    private static final String INITIAL_TEXT = "initial text";
39ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    private static final String NEW_TEXT = "new text";
40ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    private TextView textView;
41ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultz
42cd1a58362a63860cbecd57e5234079fa51652783Rich Humphrey    @Before
43cd1a58362a63860cbecd57e5234079fa51652783Rich Humphrey    public void setUp() throws Exception {
44ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultz        textView = new TextView(new Activity());
45cd1a58362a63860cbecd57e5234079fa51652783Rich Humphrey    }
467598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz
47d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips    @Test
489d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultz    public void shouldTriggerTheImeListener() {
497598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        TextView textView = new TextView(null);
507598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        TestOnEditorActionListener actionListener = new TestOnEditorActionListener();
517598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        textView.setOnEditorActionListener(actionListener);
527598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz
537598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        shadowOf(textView).triggerEditorAction(EditorInfo.IME_ACTION_GO);
549d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultz
557598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        assertThat(actionListener.textView, is(textView));
567598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        assertThat(actionListener.sentImeId, equalTo(EditorInfo.IME_ACTION_GO));
579d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultz    }
589d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultz
599d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultz    @Test
60d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips    public void testGetUrls() throws Exception {
61d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips        textView.setText("here's some text http://google.com/\nblah\thttp://another.com/123?456 blah");
62d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips
63d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips        assertThat(urlStringsFrom(textView.getUrls()), equalTo(asList(
64d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips                "http://google.com/",
65d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips                "http://another.com/123?456"
66d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips        )));
67d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips    }
68d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips
69aa03b8dbd148420164db6b62f143ea0dc7710217Rich Humphrey    @Test
70d698df6daecfc469b84ace2c7fdaff79e85f21eaKathy Lin    public void testGetGravity() throws Exception {
717598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        assertThat(textView.getGravity(), not(equalTo(Gravity.CENTER)));
727598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        textView.setGravity(Gravity.CENTER);
737598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        assertThat(textView.getGravity(), equalTo(Gravity.CENTER));
74d698df6daecfc469b84ace2c7fdaff79e85f21eaKathy Lin    }
757598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz
76d698df6daecfc469b84ace2c7fdaff79e85f21eaKathy Lin    @Test
77aa03b8dbd148420164db6b62f143ea0dc7710217Rich Humphrey    public void testMovementMethod() {
78aa03b8dbd148420164db6b62f143ea0dc7710217Rich Humphrey        MovementMethod movement = new ArrowKeyMovementMethod();
797598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz
807598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        assertNull(textView.getMovementMethod());
81aa03b8dbd148420164db6b62f143ea0dc7710217Rich Humphrey        textView.setMovementMethod(movement);
827598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        assertThat(textView.getMovementMethod(), sameInstance(movement));
83aa03b8dbd148420164db6b62f143ea0dc7710217Rich Humphrey    }
847598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz
85cd1a58362a63860cbecd57e5234079fa51652783Rich Humphrey    @Test
86cd1a58362a63860cbecd57e5234079fa51652783Rich Humphrey    public void testLinksClickable() {
877598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        assertThat(textView.getLinksClickable(), equalTo(false));
887598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz
897598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        textView.setLinksClickable(true);
907598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        assertThat(textView.getLinksClickable(), equalTo(true));
917598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz
927598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        textView.setLinksClickable(false);
937598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        assertThat(textView.getLinksClickable(), equalTo(false));
94cd1a58362a63860cbecd57e5234079fa51652783Rich Humphrey    }
95d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips
96d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard    @Test
97a739b57a6b1a99f14d586f609f0f62f48b8e2284Ian Fisher & Tyler Schultz    public void testGetTextAppearanceId() throws Exception {
98a739b57a6b1a99f14d586f609f0f62f48b8e2284Ian Fisher & Tyler Schultz        TextView textView = new TextView(null);
99a739b57a6b1a99f14d586f609f0f62f48b8e2284Ian Fisher & Tyler Schultz        textView.setTextAppearance(null, 5);
100a739b57a6b1a99f14d586f609f0f62f48b8e2284Ian Fisher & Tyler Schultz
101a739b57a6b1a99f14d586f609f0f62f48b8e2284Ian Fisher & Tyler Schultz        assertThat(shadowOf(textView).getTextAppearanceId(), equalTo(5));
102a739b57a6b1a99f14d586f609f0f62f48b8e2284Ian Fisher & Tyler Schultz    }
103a739b57a6b1a99f14d586f609f0f62f48b8e2284Ian Fisher & Tyler Schultz
104ab527c2f7aad1f0109cc83efdd448077fb92324aLowell Kirsh & Ryan Richard    @Test
105d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard    public void shouldSetTextAndTextColorWhileInflatingXmlLayout() throws Exception {
106d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard        Activity activity = new Activity();
107d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard        activity.setContentView(R.layout.text_views);
108d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard
109d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard        TextView black = (TextView) activity.findViewById(R.id.black_text_view);
110d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard        assertThat(black.getText().toString(), equalTo("Black Text"));
111d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard        assertThat(shadowOf(black).getTextColorHexValue(), equalTo(0));
112d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard
113d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard        TextView white = (TextView) activity.findViewById(R.id.white_text_view);
114d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard        assertThat(white.getText().toString(), equalTo("White Text"));
115d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard        assertThat(shadowOf(white).getTextColorHexValue(), equalTo(activity.getResources().getColor(android.R.color.white)));
116d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard
117d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard        TextView grey = (TextView) activity.findViewById(R.id.grey_text_view);
118d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard        assertThat(grey.getText().toString(), equalTo("Grey Text"));
119d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard        assertThat(shadowOf(grey).getTextColorHexValue(), equalTo(activity.getResources().getColor(R.color.grey42)));
120d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard    }
12159efa20e4642bd011ef25fd1fc7711e44cb573dcChris Perry
12259efa20e4642bd011ef25fd1fc7711e44cb573dcChris Perry    @Test
12359efa20e4642bd011ef25fd1fc7711e44cb573dcChris Perry    public void shouldSetHintAndHintColorWhileInflatingXmlLayout() throws Exception {
12459efa20e4642bd011ef25fd1fc7711e44cb573dcChris Perry        Activity activity = new Activity();
12559efa20e4642bd011ef25fd1fc7711e44cb573dcChris Perry        activity.setContentView(R.layout.text_views_hints);
12659efa20e4642bd011ef25fd1fc7711e44cb573dcChris Perry
12759efa20e4642bd011ef25fd1fc7711e44cb573dcChris Perry        TextView black = (TextView) activity.findViewById(R.id.black_text_view_hint);
12859efa20e4642bd011ef25fd1fc7711e44cb573dcChris Perry        assertThat(black.getHint().toString(), equalTo("Black Hint"));
12959efa20e4642bd011ef25fd1fc7711e44cb573dcChris Perry        assertThat(shadowOf(black).getHintColorHexValue(), equalTo(0));
13059efa20e4642bd011ef25fd1fc7711e44cb573dcChris Perry
13159efa20e4642bd011ef25fd1fc7711e44cb573dcChris Perry        TextView white = (TextView) activity.findViewById(R.id.white_text_view_hint);
13259efa20e4642bd011ef25fd1fc7711e44cb573dcChris Perry        assertThat(white.getHint().toString(), equalTo("White Hint"));
13359efa20e4642bd011ef25fd1fc7711e44cb573dcChris Perry        assertThat(shadowOf(white).getHintColorHexValue(), equalTo(activity.getResources().getColor(android.R.color.white)));
13459efa20e4642bd011ef25fd1fc7711e44cb573dcChris Perry
13559efa20e4642bd011ef25fd1fc7711e44cb573dcChris Perry        TextView grey = (TextView) activity.findViewById(R.id.grey_text_view_hint);
13659efa20e4642bd011ef25fd1fc7711e44cb573dcChris Perry        assertThat(grey.getHint().toString(), equalTo("Grey Hint"));
13759efa20e4642bd011ef25fd1fc7711e44cb573dcChris Perry        assertThat(shadowOf(grey).getHintColorHexValue(), equalTo(activity.getResources().getColor(R.color.grey42)));
13859efa20e4642bd011ef25fd1fc7711e44cb573dcChris Perry    }
13906f618b803199f287e03891d7ed819e64386c37cSebastien Plisson
14006f618b803199f287e03891d7ed819e64386c37cSebastien Plisson    @Test
141ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    public void shouldNotHaveTransformationMethodByDefault() {
1429e562ba543ceec13e2553e043c24b15ff6d999ebPhil Goodwin & Tyler Schultz        ShadowTextView view = new ShadowTextView();
1439e562ba543ceec13e2553e043c24b15ff6d999ebPhil Goodwin & Tyler Schultz        assertThat(view.getTransformationMethod(), is(CoreMatchers.<Object>nullValue()));
1449e562ba543ceec13e2553e043c24b15ff6d999ebPhil Goodwin & Tyler Schultz    }
1459e562ba543ceec13e2553e043c24b15ff6d999ebPhil Goodwin & Tyler Schultz
1469e562ba543ceec13e2553e043c24b15ff6d999ebPhil Goodwin & Tyler Schultz    @Test
147ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    public void shouldAllowSettingATransformationMethod() {
1489e562ba543ceec13e2553e043c24b15ff6d999ebPhil Goodwin & Tyler Schultz        ShadowTextView view = new ShadowTextView();
1499e562ba543ceec13e2553e043c24b15ff6d999ebPhil Goodwin & Tyler Schultz        view.setTransformationMethod(new ShadowPasswordTransformationMethod());
1509e562ba543ceec13e2553e043c24b15ff6d999ebPhil Goodwin & Tyler Schultz        assertEquals(view.getTransformationMethod().getClass(), ShadowPasswordTransformationMethod.class);
1519e562ba543ceec13e2553e043c24b15ff6d999ebPhil Goodwin & Tyler Schultz    }
152ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
1539e562ba543ceec13e2553e043c24b15ff6d999ebPhil Goodwin & Tyler Schultz    @Test
15406f618b803199f287e03891d7ed819e64386c37cSebastien Plisson    public void testGetInputType() throws Exception {
15506f618b803199f287e03891d7ed819e64386c37cSebastien Plisson        assertThat(textView.getInputType(), not(equalTo(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)));
15606f618b803199f287e03891d7ed819e64386c37cSebastien Plisson        textView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
15706f618b803199f287e03891d7ed819e64386c37cSebastien Plisson        assertThat(textView.getInputType(), equalTo(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
15806f618b803199f287e03891d7ed819e64386c37cSebastien Plisson    }
159ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
160a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    @Test
161ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    public void givenATextViewWithATextWatcherAdded_WhenSettingTextWithTextResourceId_ShouldNotifyTextWatcher() {
162ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        MockTextWatcher mockTextWatcher = new MockTextWatcher();
163ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.addTextChangedListener(mockTextWatcher);
164a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
165ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.setText(R.string.hello);
166ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
167ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        assertEachTextWatcherEventWasInvoked(mockTextWatcher);
168a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    }
169ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
170a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    @Test
171ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    public void givenATextViewWithATextWatcherAdded_WhenSettingTextWithCharSequence_ShouldNotifyTextWatcher() {
172ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        MockTextWatcher mockTextWatcher = new MockTextWatcher();
173ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.addTextChangedListener(mockTextWatcher);
174ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
175ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.setText("text");
176ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
177ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        assertEachTextWatcherEventWasInvoked(mockTextWatcher);
178ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    }
179ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
180a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    @Test
1819125fa3a62463a074b6242cde6335202ee06d216Aaron VonderHaar & Ryan Richard    public void givenATextViewWithATextWatcherAdded_WhenSettingNullText_ShouldNotifyTextWatcher() {
1829125fa3a62463a074b6242cde6335202ee06d216Aaron VonderHaar & Ryan Richard        MockTextWatcher mockTextWatcher = new MockTextWatcher();
1839125fa3a62463a074b6242cde6335202ee06d216Aaron VonderHaar & Ryan Richard        textView.addTextChangedListener(mockTextWatcher);
1849125fa3a62463a074b6242cde6335202ee06d216Aaron VonderHaar & Ryan Richard
1859125fa3a62463a074b6242cde6335202ee06d216Aaron VonderHaar & Ryan Richard        textView.setText(null);
1869125fa3a62463a074b6242cde6335202ee06d216Aaron VonderHaar & Ryan Richard
1879125fa3a62463a074b6242cde6335202ee06d216Aaron VonderHaar & Ryan Richard        assertEachTextWatcherEventWasInvoked(mockTextWatcher);
1889125fa3a62463a074b6242cde6335202ee06d216Aaron VonderHaar & Ryan Richard    }
1899125fa3a62463a074b6242cde6335202ee06d216Aaron VonderHaar & Ryan Richard
1909125fa3a62463a074b6242cde6335202ee06d216Aaron VonderHaar & Ryan Richard    @Test
191ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    public void givenATextViewWithMultipleTextWatchersAdded_WhenSettingText_ShouldNotifyEachTextWatcher() {
192ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        List<MockTextWatcher> mockTextWatchers = anyNumberOfTextWatchers();
193ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        for (MockTextWatcher textWatcher : mockTextWatchers) {
194ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin            textView.addTextChangedListener(textWatcher);
195ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultz        }
196ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
197ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.setText("text");
198ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
199ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        for (MockTextWatcher textWatcher : mockTextWatchers) {
200ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin            assertEachTextWatcherEventWasInvoked(textWatcher);
201ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultz        }
202ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    }
203ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
204a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    @Test
205ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    public void whenSettingText_ShouldFireBeforeTextChangedWithCorrectArguments() {
206ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.setText(INITIAL_TEXT);
207ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        TextWatcher mockTextWatcher = mock(TextWatcher.class);
208ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.addTextChangedListener(mockTextWatcher);
209ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
210ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.setText(NEW_TEXT);
211ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
212ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        verify(mockTextWatcher).beforeTextChanged(INITIAL_TEXT, 0, INITIAL_TEXT.length(), NEW_TEXT.length());
213ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    }
214ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
215a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    @Test
216ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    public void whenSettingText_ShouldFireOnTextChangedWithCorrectArguments() {
217ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.setText(INITIAL_TEXT);
218ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        TextWatcher mockTextWatcher = mock(TextWatcher.class);
219ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.addTextChangedListener(mockTextWatcher);
220ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
221ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.setText(NEW_TEXT);
222ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
223ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        verify(mockTextWatcher).onTextChanged(NEW_TEXT, 0, INITIAL_TEXT.length(), NEW_TEXT.length());
224ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    }
225ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
226a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    @Test
227ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    public void whenSettingText_ShouldFireAfterTextChangedWithCorrectArgument() {
228ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        MockTextWatcher mockTextWatcher = new MockTextWatcher();
229ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.addTextChangedListener(mockTextWatcher);
230ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
231ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.setText(NEW_TEXT);
232ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
233ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        assertThat(mockTextWatcher.afterTextChangeArgument.toString(), equalTo(NEW_TEXT));
234ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    }
235ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
23692b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    @Test
23792b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    public void whenAppendingText_ShouldAppendNewTextAfterOldOne() {
238ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.setText(INITIAL_TEXT);
239ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.append(NEW_TEXT);
240ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
241ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        assertEquals(INITIAL_TEXT + NEW_TEXT, textView.getText());
24292b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    }
243ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
24492b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    @Test
24592b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    public void whenAppendingText_ShouldFireBeforeTextChangedWithCorrectArguments() {
246ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.setText(INITIAL_TEXT);
247ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        TextWatcher mockTextWatcher = mock(TextWatcher.class);
248ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.addTextChangedListener(mockTextWatcher);
249ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
250ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.append(NEW_TEXT);
251ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
252ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        verify(mockTextWatcher).beforeTextChanged(INITIAL_TEXT, 0, INITIAL_TEXT.length(), INITIAL_TEXT.length() + NEW_TEXT.length());
25392b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    }
254ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
25592b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    @Test
25692b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    public void whenAppendingText_ShouldFireOnTextChangedWithCorrectArguments() {
257ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.setText(INITIAL_TEXT);
258ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        TextWatcher mockTextWatcher = mock(TextWatcher.class);
259ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.addTextChangedListener(mockTextWatcher);
260ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
261ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.append(NEW_TEXT);
262ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
263ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        verify(mockTextWatcher).onTextChanged(INITIAL_TEXT + NEW_TEXT, 0, INITIAL_TEXT.length(), INITIAL_TEXT.length() + NEW_TEXT.length());
26492b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    }
265ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
26692b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    @Test
26792b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    public void whenAppendingText_ShouldFireAfterTextChangedWithCorrectArgument() {
268ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.setText(INITIAL_TEXT);
269ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        MockTextWatcher mockTextWatcher = new MockTextWatcher();
270ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.addTextChangedListener(mockTextWatcher);
271ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
272ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.append(NEW_TEXT);
273ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
274ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        assertThat(mockTextWatcher.afterTextChangeArgument.toString(), equalTo(INITIAL_TEXT + NEW_TEXT));
27592b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    }
276c7ce4d9b6d4f56b6348cb3340d4332a746e0c190Phil Goodwin & Ryan Richard
277c7ce4d9b6d4f56b6348cb3340d4332a746e0c190Phil Goodwin & Ryan Richard    @Test
278c7ce4d9b6d4f56b6348cb3340d4332a746e0c190Phil Goodwin & Ryan Richard    public void removeTextChangedListener_shouldRemoveTheListener() throws Exception {
279c7ce4d9b6d4f56b6348cb3340d4332a746e0c190Phil Goodwin & Ryan Richard        MockTextWatcher watcher = new MockTextWatcher();
280c7ce4d9b6d4f56b6348cb3340d4332a746e0c190Phil Goodwin & Ryan Richard        textView.addTextChangedListener(watcher);
281c7ce4d9b6d4f56b6348cb3340d4332a746e0c190Phil Goodwin & Ryan Richard        assertTrue(shadowOf(textView).getWatchers().contains(watcher));
282c7ce4d9b6d4f56b6348cb3340d4332a746e0c190Phil Goodwin & Ryan Richard
283c7ce4d9b6d4f56b6348cb3340d4332a746e0c190Phil Goodwin & Ryan Richard        textView.removeTextChangedListener(watcher);
284c7ce4d9b6d4f56b6348cb3340d4332a746e0c190Phil Goodwin & Ryan Richard        assertFalse(shadowOf(textView).getWatchers().contains(watcher));
285c7ce4d9b6d4f56b6348cb3340d4332a746e0c190Phil Goodwin & Ryan Richard    }
286c7ce4d9b6d4f56b6348cb3340d4332a746e0c190Phil Goodwin & Ryan Richard
2874dda01d613890b88a9370e46ec53ef54c123f9fbAndrew Dai & Rick Kawala    @Test
2884dda01d613890b88a9370e46ec53ef54c123f9fbAndrew Dai & Rick Kawala    public void getPaint_returnsMeasureTextEnabledObject() throws Exception {
2894dda01d613890b88a9370e46ec53ef54c123f9fbAndrew Dai & Rick Kawala        assertThat(textView.getPaint().measureText("12345"), equalTo(5f));
2904dda01d613890b88a9370e46ec53ef54c123f9fbAndrew Dai & Rick Kawala    }
2914dda01d613890b88a9370e46ec53ef54c123f9fbAndrew Dai & Rick Kawala
292ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    @Test
293ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    public void append_whenSelectionIsAtTheEnd_shouldKeepSelectionAtTheEnd() throws Exception {
294ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.setText("1");
295ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        shadowOf(textView).setSelection(0, 0);
296ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.append("2");
297ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        assertEquals(0, textView.getSelectionEnd());
298ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        assertEquals(0, textView.getSelectionStart());
299ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
300ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        shadowOf(textView).setSelection(2, 2);
301ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.append("3");
302ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        assertEquals(3, textView.getSelectionEnd());
303ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        assertEquals(3, textView.getSelectionStart());
304ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    }
305ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
306ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    @Test
307ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    public void append_whenSelectionReachesToEnd_shouldExtendSelectionToTheEnd() throws Exception {
308ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.setText("12");
309ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        shadowOf(textView).setSelection(0, 2);
310ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        textView.append("3");
311ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        assertEquals(3, textView.getSelectionEnd());
312ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        assertEquals(0, textView.getSelectionStart());
313ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    }
314ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
315e429b89e0edf6b77099f40e7e3af40b0cc1e7fc4Amrit Thakur & Ryan Richard    @Test
316e429b89e0edf6b77099f40e7e3af40b0cc1e7fc4Amrit Thakur & Ryan Richard    public void testSetCompountDrawablesWithIntrinsicBounds_int_shouldCreateDrawablesWithResourceIds() throws Exception {
317e429b89e0edf6b77099f40e7e3af40b0cc1e7fc4Amrit Thakur & Ryan Richard        textView.setCompoundDrawablesWithIntrinsicBounds(6, 7, 8, 9);
318e429b89e0edf6b77099f40e7e3af40b0cc1e7fc4Amrit Thakur & Ryan Richard
319e429b89e0edf6b77099f40e7e3af40b0cc1e7fc4Amrit Thakur & Ryan Richard        Assert.assertEquals(6, shadowOf(textView.getCompoundDrawables()[0]).getLoadedFromResourceId());
320e429b89e0edf6b77099f40e7e3af40b0cc1e7fc4Amrit Thakur & Ryan Richard        Assert.assertEquals(7, shadowOf(textView.getCompoundDrawables()[1]).getLoadedFromResourceId());
321e429b89e0edf6b77099f40e7e3af40b0cc1e7fc4Amrit Thakur & Ryan Richard        Assert.assertEquals(8, shadowOf(textView.getCompoundDrawables()[2]).getLoadedFromResourceId());
322e429b89e0edf6b77099f40e7e3af40b0cc1e7fc4Amrit Thakur & Ryan Richard        Assert.assertEquals(9, shadowOf(textView.getCompoundDrawables()[3]).getLoadedFromResourceId());
323e429b89e0edf6b77099f40e7e3af40b0cc1e7fc4Amrit Thakur & Ryan Richard    }
324e429b89e0edf6b77099f40e7e3af40b0cc1e7fc4Amrit Thakur & Ryan Richard
325c14c51ed650b1b61ee07621cb32bacc6d2d79c3fAmrit Thakur & Ryan Richard    @Test
326c14c51ed650b1b61ee07621cb32bacc6d2d79c3fAmrit Thakur & Ryan Richard    public void testSetCompountDrawablesWithIntrinsicBounds_int_shouldNotCreateDrawablesForZero() throws Exception {
327c14c51ed650b1b61ee07621cb32bacc6d2d79c3fAmrit Thakur & Ryan Richard        textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
328c14c51ed650b1b61ee07621cb32bacc6d2d79c3fAmrit Thakur & Ryan Richard
329c14c51ed650b1b61ee07621cb32bacc6d2d79c3fAmrit Thakur & Ryan Richard        Assert.assertNull(textView.getCompoundDrawables()[0]);
330c14c51ed650b1b61ee07621cb32bacc6d2d79c3fAmrit Thakur & Ryan Richard        Assert.assertNull(textView.getCompoundDrawables()[1]);
331c14c51ed650b1b61ee07621cb32bacc6d2d79c3fAmrit Thakur & Ryan Richard        Assert.assertNull(textView.getCompoundDrawables()[2]);
332c14c51ed650b1b61ee07621cb32bacc6d2d79c3fAmrit Thakur & Ryan Richard        Assert.assertNull(textView.getCompoundDrawables()[3]);
333c14c51ed650b1b61ee07621cb32bacc6d2d79c3fAmrit Thakur & Ryan Richard    }
334c14c51ed650b1b61ee07621cb32bacc6d2d79c3fAmrit Thakur & Ryan Richard
335ac2aa24a0f43988de36443e0beb8c2e0aacf2ceePhil Goodwin & Rick Kawala    @Test
336ac2aa24a0f43988de36443e0beb8c2e0aacf2ceePhil Goodwin & Rick Kawala    public void canSetAndGetTypeface() throws Exception {
337ac2aa24a0f43988de36443e0beb8c2e0aacf2ceePhil Goodwin & Rick Kawala        Typeface typeface = Robolectric.newInstanceOf(Typeface.class);
338ac2aa24a0f43988de36443e0beb8c2e0aacf2ceePhil Goodwin & Rick Kawala        textView.setTypeface(typeface);
339ac2aa24a0f43988de36443e0beb8c2e0aacf2ceePhil Goodwin & Rick Kawala        Assert.assertEquals(typeface, textView.getTypeface());
340ac2aa24a0f43988de36443e0beb8c2e0aacf2ceePhil Goodwin & Rick Kawala    }
341ac2aa24a0f43988de36443e0beb8c2e0aacf2ceePhil Goodwin & Rick Kawala
342085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry    @Test
343085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry    public void onTouchEvent_shouldCallMovementMethodOnTouchEventWithSetMotionEvent() throws Exception {
344085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        TestMovementMethod testMovementMethod = new TestMovementMethod();
345085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry
346085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        textView.setMovementMethod(testMovementMethod);
347085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        MotionEvent event = MotionEvent.obtain(0, 0, 0, 0, 0, 0);
348085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        textView.dispatchTouchEvent(event);
349085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry
350085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        assertEquals(testMovementMethod.event, event);
351085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry    }
352085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry
353085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry    @Test
354085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry    public void canSetAndGetLayout() throws Exception {
355085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        StaticLayout layout = new StaticLayout(null, null, 0, null, 0, 0, true);
356085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        shadowOf(textView).setLayout(layout);
357085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        assertEquals(textView.getLayout(), layout);
358085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry    }
359f956f7ee243bb701f1747b30629d1f6d9f703552Jon Boekenoogen
360f956f7ee243bb701f1747b30629d1f6d9f703552Jon Boekenoogen    @Test
361f956f7ee243bb701f1747b30629d1f6d9f703552Jon Boekenoogen    public void testGetError() {
362f956f7ee243bb701f1747b30629d1f6d9f703552Jon Boekenoogen      assertNull(textView.getError());
363f956f7ee243bb701f1747b30629d1f6d9f703552Jon Boekenoogen      CharSequence error = "myError";
364f956f7ee243bb701f1747b30629d1f6d9f703552Jon Boekenoogen      textView.setError(error);
365f956f7ee243bb701f1747b30629d1f6d9f703552Jon Boekenoogen      assertEquals(error, textView.getError());
366f956f7ee243bb701f1747b30629d1f6d9f703552Jon Boekenoogen    }
367085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry
368a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    private List<MockTextWatcher> anyNumberOfTextWatchers() {
369ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        List<MockTextWatcher> mockTextWatchers = new ArrayList<MockTextWatcher>();
370ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        int numberBetweenOneAndTen = new Random().nextInt(10) + 1;
371ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        for (int i = 0; i < numberBetweenOneAndTen; i++) {
372ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin            mockTextWatchers.add(new MockTextWatcher());
373ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultz        }
374ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        return mockTextWatchers;
375ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    }
376ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
377ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    private void assertEachTextWatcherEventWasInvoked(MockTextWatcher mockTextWatcher) {
378ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        assertTrue("Expected each TextWatcher event to have been invoked once", mockTextWatcher.methodsCalled.size() == 3);
379ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
380ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        assertThat(mockTextWatcher.methodsCalled.get(0), equalTo("beforeTextChanged"));
381ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        assertThat(mockTextWatcher.methodsCalled.get(1), equalTo("onTextChanged"));
382ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        assertThat(mockTextWatcher.methodsCalled.get(2), equalTo("afterTextChanged"));
383ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    }
384ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
385ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin    private List<String> urlStringsFrom(URLSpan[] urlSpans) {
386d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips        List<String> urls = new ArrayList<String>();
387d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips        for (URLSpan urlSpan : urlSpans) {
388d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips            urls.add(urlSpan.getURL());
389d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips        }
390d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips        return urls;
391d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips    }
3929d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultz
3937598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz    private static class TestOnEditorActionListener implements TextView.OnEditorActionListener {
3947598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        private TextView textView;
3957598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        private int sentImeId;
3969d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultz
3977598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        @Override
3987598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        public boolean onEditorAction(TextView textView, int sentImeId, KeyEvent keyEvent) {
3997598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz            this.textView = textView;
4007598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz            this.sentImeId = sentImeId;
4017598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz            return false;
4027598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        }
4039d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultz    }
404ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
405a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    private static class MockTextWatcher implements TextWatcher {
406a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
407ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        List<String> methodsCalled = new ArrayList<String>();
408ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        Editable afterTextChangeArgument;
409ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
410ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        @Override
411ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
412ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin            methodsCalled.add("beforeTextChanged");
413ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        }
414ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
415ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        @Override
416ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        public void onTextChanged(CharSequence s, int start, int before, int count) {
417ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin            methodsCalled.add("onTextChanged");
418ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        }
419ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
420ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        @Override
421ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        public void afterTextChanged(Editable s) {
422ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin            methodsCalled.add("afterTextChanged");
423ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin            afterTextChangeArgument = s;
424ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin        }
425ca06528611bf165936f7d5437da41dd9d2039b94Aaron VonderHaar & Phil Goodwin
426a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    }
427085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry
428085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry    private static class TestMovementMethod implements MovementMethod {
429085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        public MotionEvent event;
430085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        public boolean touchEventWasCalled;
431085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry
432085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        @Override
433085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        public void initialize(TextView widget, Spannable text) {
434085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        }
435085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry
436085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        @Override
437085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        public boolean onKeyDown(TextView widget, Spannable text, int keyCode, KeyEvent event) {
438085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry            return false;
439085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        }
440085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry
441085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        @Override
442085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        public boolean onKeyUp(TextView widget, Spannable text, int keyCode, KeyEvent event) {
443085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry            return false;
444085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        }
445085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry
446085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        @Override
447085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        public boolean onKeyOther(TextView view, Spannable text, KeyEvent event) {
448085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry            return false;
449085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        }
450085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry
451085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        @Override
452085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        public void onTakeFocus(TextView widget, Spannable text, int direction) {
453085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        }
454085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry
455085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        @Override
456085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        public boolean onTrackballEvent(TextView widget, Spannable text, MotionEvent event) {
457085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry            return false;
458085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        }
459085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry
460085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        @Override
461085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        public boolean onTouchEvent(TextView widget, Spannable text, MotionEvent event) {
462085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry            this.event = event;
463085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry            touchEventWasCalled = true;
464085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry            return false;
465085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        }
466085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry
467085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        @Override
468085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        public boolean canSelectArbitrarily() {
469085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry            return false;
470085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry        }
471e057483dbccea1e7373aed776bb14ef85aaa93c7Michael Portuesi
472e057483dbccea1e7373aed776bb14ef85aaa93c7Michael Portuesi		@Override
473e057483dbccea1e7373aed776bb14ef85aaa93c7Michael Portuesi		public boolean onGenericMotionEvent(TextView widget, Spannable text,
474e057483dbccea1e7373aed776bb14ef85aaa93c7Michael Portuesi				MotionEvent event) {
475e057483dbccea1e7373aed776bb14ef85aaa93c7Michael Portuesi			return false;
476e057483dbccea1e7373aed776bb14ef85aaa93c7Michael Portuesi		}
477085b7edfe02ef0ae1870b514066bb5b8b2176fadAmrit Thakur & Chris Perry    }
478d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips}
479