TextViewTest.java revision 4dda01d613890b88a9370e46ec53ef54c123f9fb
133873d2b41a5cd2597b20a4e88eb8942944c0f23Tyler Schultzpackage com.xtremelabs.robolectric.shadows;
2d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips
3d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richardimport android.app.Activity;
44dda01d613890b88a9370e46ec53ef54c123f9fbAndrew Dai & Rick Kawalaimport android.text.*;
5aa03b8dbd148420164db6b62f143ea0dc7710217Rich Humphreyimport android.text.method.ArrowKeyMovementMethod;
6aa03b8dbd148420164db6b62f143ea0dc7710217Rich Humphreyimport android.text.method.MovementMethod;
7d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillipsimport android.text.style.URLSpan;
8d698df6daecfc469b84ace2c7fdaff79e85f21eaKathy Linimport android.view.Gravity;
99d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultzimport android.view.KeyEvent;
109d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultzimport android.view.inputmethod.EditorInfo;
11d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillipsimport android.widget.TextView;
12d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richardimport com.xtremelabs.robolectric.R;
1307257335f88da163910cc0bd039e6163edc38580Gregg Van Hove & Phil Goodwinimport com.xtremelabs.robolectric.WithTestDefaultsRunner;
14ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultzimport org.hamcrest.CoreMatchers;
15ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultzimport org.junit.Before;
16ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultzimport org.junit.Test;
17ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultzimport org.junit.runner.RunWith;
18ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultz
194dda01d613890b88a9370e46ec53ef54c123f9fbAndrew Dai & Rick Kawalaimport java.util.*;
20ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultz
21ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultzimport static com.xtremelabs.robolectric.Robolectric.shadowOf;
22ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultzimport static java.util.Arrays.asList;
23c7ce4d9b6d4f56b6348cb3340d4332a746e0c190Phil Goodwin & Ryan Richardimport static junit.framework.Assert.assertFalse;
244dda01d613890b88a9370e46ec53ef54c123f9fbAndrew Dai & Rick Kawalaimport static org.hamcrest.CoreMatchers.equalTo;
254dda01d613890b88a9370e46ec53ef54c123f9fbAndrew Dai & Rick Kawalaimport static org.hamcrest.CoreMatchers.is;
264dda01d613890b88a9370e46ec53ef54c123f9fbAndrew Dai & Rick Kawalaimport static org.hamcrest.CoreMatchers.not;
274dda01d613890b88a9370e46ec53ef54c123f9fbAndrew Dai & Rick Kawalaimport static org.hamcrest.CoreMatchers.sameInstance;
284dda01d613890b88a9370e46ec53ef54c123f9fbAndrew Dai & Rick Kawalaimport static org.junit.Assert.assertEquals;
294dda01d613890b88a9370e46ec53ef54c123f9fbAndrew Dai & Rick Kawalaimport static org.junit.Assert.assertNull;
304dda01d613890b88a9370e46ec53ef54c123f9fbAndrew Dai & Rick Kawalaimport static org.junit.Assert.assertThat;
314dda01d613890b88a9370e46ec53ef54c123f9fbAndrew Dai & Rick Kawalaimport static org.junit.Assert.assertTrue;
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
38ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultz	private static final String INITIAL_TEXT = "initial text";
39a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor	private static final String NEW_TEXT = "new text";
40a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor	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
1419e562ba543ceec13e2553e043c24b15ff6d999ebPhil Goodwin & Tyler Schultz    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
1479e562ba543ceec13e2553e043c24b15ff6d999ebPhil Goodwin & Tyler Schultz    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    }
1529e562ba543ceec13e2553e043c24b15ff6d999ebPhil Goodwin & Tyler Schultz
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    }
159ab527c2f7aad1f0109cc83efdd448077fb92324aLowell Kirsh & Ryan Richard
160a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    @Test
161a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor	public void givenATextViewWithATextWatcherAdded_WhenSettingTextWithTextResourceId_ShouldNotifyTextWatcher() {
162a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    	MockTextWatcher mockTextWatcher = new MockTextWatcher();
163a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    	textView.addTextChangedListener(mockTextWatcher);
164a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
165ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultz    	textView.setText(R.string.hello);
166a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
167a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		assertEachTextWatcherEventWasInvoked(mockTextWatcher);
168a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    }
169a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
170a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    @Test
171a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor	public void givenATextViewWithATextWatcherAdded_WhenSettingTextWithCharSequence_ShouldNotifyTextWatcher() {
172a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    	MockTextWatcher mockTextWatcher = new MockTextWatcher();
173a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    	textView.addTextChangedListener(mockTextWatcher);
174a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
175a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		textView.setText("text");
176a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
177a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		assertEachTextWatcherEventWasInvoked(mockTextWatcher);
178a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor	}
179a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
180a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    @Test
181a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor	public void givenATextViewWithMultipleTextWatchersAdded_WhenSettingText_ShouldNotifyEachTextWatcher() {
182a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    	List<MockTextWatcher> mockTextWatchers = anyNumberOfTextWatchers();
183ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultz    	for (MockTextWatcher textWatcher : mockTextWatchers) {
184a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    		textView.addTextChangedListener(textWatcher);
185ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultz        }
186a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
187a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		textView.setText("text");
188a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
189ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultz    	for (MockTextWatcher textWatcher : mockTextWatchers) {
190a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    		assertEachTextWatcherEventWasInvoked(textWatcher);
191ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultz        }
192a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor	}
193a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
194a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    @Test
195a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor	public void whenSettingText_ShouldFireBeforeTextChangedWithCorrectArguments() {
196a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		textView.setText(INITIAL_TEXT);
197a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		TextWatcher mockTextWatcher = mock(TextWatcher.class);
198a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		textView.addTextChangedListener(mockTextWatcher);
199a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
200a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		textView.setText(NEW_TEXT);
201a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
202a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		verify(mockTextWatcher).beforeTextChanged(INITIAL_TEXT, 0, INITIAL_TEXT.length(), NEW_TEXT.length());
203a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor	}
204a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
205a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    @Test
206a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor	public void whenSettingText_ShouldFireOnTextChangedWithCorrectArguments() {
207a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    	textView.setText(INITIAL_TEXT);
208a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		TextWatcher mockTextWatcher = mock(TextWatcher.class);
209a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		textView.addTextChangedListener(mockTextWatcher);
210a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
211a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		textView.setText(NEW_TEXT);
212a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
213a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		verify(mockTextWatcher).onTextChanged(NEW_TEXT, 0, INITIAL_TEXT.length(), NEW_TEXT.length());
214a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor	}
215a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
216a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    @Test
217a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor	public void whenSettingText_ShouldFireAfterTextChangedWithCorrectArgument() {
218a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		MockTextWatcher mockTextWatcher = new MockTextWatcher();
219a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		textView.addTextChangedListener(mockTextWatcher);
220a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
221a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		textView.setText(NEW_TEXT);
222a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
223a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		assertThat(mockTextWatcher.afterTextChangeArgument.toString(), equalTo(NEW_TEXT));
224a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor	}
225a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
22692b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    @Test
22792b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    public void whenAppendingText_ShouldAppendNewTextAfterOldOne() {
22892b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    	textView.setText(INITIAL_TEXT);
22992b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    	textView.append(NEW_TEXT);
23092b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen
23192b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    	assertEquals(INITIAL_TEXT + NEW_TEXT, textView.getText());
23292b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    }
23392b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen
23492b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    @Test
23592b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    public void whenAppendingText_ShouldFireBeforeTextChangedWithCorrectArguments() {
23692b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen		textView.setText(INITIAL_TEXT);
23792b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen		TextWatcher mockTextWatcher = mock(TextWatcher.class);
23892b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen		textView.addTextChangedListener(mockTextWatcher);
23992b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen
24092b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen		textView.append(NEW_TEXT);
24192b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen
24292b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen		verify(mockTextWatcher).beforeTextChanged(INITIAL_TEXT, 0, INITIAL_TEXT.length(), INITIAL_TEXT.length() + NEW_TEXT.length());
24392b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    }
24492b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen
24592b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    @Test
24692b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    public void whenAppendingText_ShouldFireOnTextChangedWithCorrectArguments() {
24792b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen		textView.setText(INITIAL_TEXT);
24892b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen		TextWatcher mockTextWatcher = mock(TextWatcher.class);
24992b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen		textView.addTextChangedListener(mockTextWatcher);
25092b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen
25192b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen		textView.append(NEW_TEXT);
25292b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen
25392b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen		verify(mockTextWatcher).onTextChanged(INITIAL_TEXT + NEW_TEXT, 0, INITIAL_TEXT.length(), INITIAL_TEXT.length() + NEW_TEXT.length());
25492b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    }
25592b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen
25692b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    @Test
25792b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    public void whenAppendingText_ShouldFireAfterTextChangedWithCorrectArgument() {
25892b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    	textView.setText(INITIAL_TEXT);
25992b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen		MockTextWatcher mockTextWatcher = new MockTextWatcher();
26092b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen		textView.addTextChangedListener(mockTextWatcher);
26192b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen
26292b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen		textView.append(NEW_TEXT);
26392b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen
26492b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen		assertThat(mockTextWatcher.afterTextChangeArgument.toString(), equalTo(INITIAL_TEXT + NEW_TEXT));
26592b72e88b2f0b73754a709ae432306f0652058deValtteri Virtanen    }
266c7ce4d9b6d4f56b6348cb3340d4332a746e0c190Phil Goodwin & Ryan Richard
267c7ce4d9b6d4f56b6348cb3340d4332a746e0c190Phil Goodwin & Ryan Richard    @Test
268c7ce4d9b6d4f56b6348cb3340d4332a746e0c190Phil Goodwin & Ryan Richard    public void removeTextChangedListener_shouldRemoveTheListener() throws Exception {
269c7ce4d9b6d4f56b6348cb3340d4332a746e0c190Phil Goodwin & Ryan Richard        MockTextWatcher watcher = new MockTextWatcher();
270c7ce4d9b6d4f56b6348cb3340d4332a746e0c190Phil Goodwin & Ryan Richard        textView.addTextChangedListener(watcher);
271c7ce4d9b6d4f56b6348cb3340d4332a746e0c190Phil Goodwin & Ryan Richard        assertTrue(shadowOf(textView).getWatchers().contains(watcher));
272c7ce4d9b6d4f56b6348cb3340d4332a746e0c190Phil Goodwin & Ryan Richard
273c7ce4d9b6d4f56b6348cb3340d4332a746e0c190Phil Goodwin & Ryan Richard        textView.removeTextChangedListener(watcher);
274c7ce4d9b6d4f56b6348cb3340d4332a746e0c190Phil Goodwin & Ryan Richard        assertFalse(shadowOf(textView).getWatchers().contains(watcher));
275c7ce4d9b6d4f56b6348cb3340d4332a746e0c190Phil Goodwin & Ryan Richard    }
276c7ce4d9b6d4f56b6348cb3340d4332a746e0c190Phil Goodwin & Ryan Richard
2774dda01d613890b88a9370e46ec53ef54c123f9fbAndrew Dai & Rick Kawala    @Test
2784dda01d613890b88a9370e46ec53ef54c123f9fbAndrew Dai & Rick Kawala    public void getPaint_returnsMeasureTextEnabledObject() throws Exception {
2794dda01d613890b88a9370e46ec53ef54c123f9fbAndrew Dai & Rick Kawala        assertThat(textView.getPaint().measureText("12345"), equalTo(5f));
2804dda01d613890b88a9370e46ec53ef54c123f9fbAndrew Dai & Rick Kawala    }
2814dda01d613890b88a9370e46ec53ef54c123f9fbAndrew Dai & Rick Kawala
282a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    private List<MockTextWatcher> anyNumberOfTextWatchers() {
283a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		List<MockTextWatcher> mockTextWatchers = new ArrayList<MockTextWatcher>();
284a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		int numberBetweenOneAndTen = new Random().nextInt(10) + 1;
285ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultz		for (int i = 0; i < numberBetweenOneAndTen; i++) {
286a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor			mockTextWatchers.add(new MockTextWatcher());
287ae27d6b8b8a2625fd075798272330227ba29e7daTyler Schultz        }
288a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		return mockTextWatchers;
289a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor	}
290a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
291a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor	private void assertEachTextWatcherEventWasInvoked(MockTextWatcher mockTextWatcher) {
292a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    	assertTrue("Expected each TextWatcher event to have been invoked once", mockTextWatcher.methodsCalled.size() == 3);
293a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
294a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		assertThat(mockTextWatcher.methodsCalled.get(0), equalTo("beforeTextChanged"));
295a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		assertThat(mockTextWatcher.methodsCalled.get(1), equalTo("onTextChanged"));
296a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		assertThat(mockTextWatcher.methodsCalled.get(2), equalTo("afterTextChanged"));
297a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor	}
298a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
299a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor	private List<String> urlStringsFrom(URLSpan[] urlSpans) {
300d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips        List<String> urls = new ArrayList<String>();
301d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips        for (URLSpan urlSpan : urlSpans) {
302d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips            urls.add(urlSpan.getURL());
303d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips        }
304d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips        return urls;
305d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips    }
3069d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultz
3077598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz    private static class TestOnEditorActionListener implements TextView.OnEditorActionListener {
3087598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        private TextView textView;
3097598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        private int sentImeId;
3109d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultz
3117598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        @Override
3127598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        public boolean onEditorAction(TextView textView, int sentImeId, KeyEvent keyEvent) {
3137598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz            this.textView = textView;
3147598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz            this.sentImeId = sentImeId;
3157598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz            return false;
3167598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        }
3179d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultz    }
318a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
319a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    private static class MockTextWatcher implements TextWatcher {
320a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
321a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    	List<String> methodsCalled = new ArrayList<String>();
322a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    	Editable afterTextChangeArgument;
323a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
324a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		@Override
325a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		public void beforeTextChanged(CharSequence s, int start, int count,	int after) {
326a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor			methodsCalled.add("beforeTextChanged");
327a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		}
328a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
329a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		@Override
330a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		public void onTextChanged(CharSequence s, int start, int before, int count) {
331a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor			methodsCalled.add("onTextChanged");
332a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		}
333a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
334a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		@Override
335a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		public void afterTextChanged(Editable s) {
336a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor			methodsCalled.add("afterTextChanged");
337a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor			afterTextChangeArgument = s;
338a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor		}
339a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor
340a7f06139c4d3795b9301db66cc7cdfa6eb6b35ceRobert Taylor    }
341d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips}
342