TextViewTest.java revision ab527c2f7aad1f0109cc83efdd448077fb92324a
133873d2b41a5cd2597b20a4e88eb8942944c0f23Tyler Schultzpackage com.xtremelabs.robolectric.shadows;
2d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips
3d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richardimport android.app.Activity;
4aa03b8dbd148420164db6b62f143ea0dc7710217Rich Humphreyimport android.text.method.ArrowKeyMovementMethod;
5aa03b8dbd148420164db6b62f143ea0dc7710217Rich Humphreyimport android.text.method.MovementMethod;
6d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillipsimport android.text.style.URLSpan;
7d698df6daecfc469b84ace2c7fdaff79e85f21eaKathy Linimport android.view.Gravity;
89d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultzimport android.view.KeyEvent;
99d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultzimport android.view.inputmethod.EditorInfo;
10d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillipsimport android.widget.TextView;
11d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richardimport com.xtremelabs.robolectric.R;
1207257335f88da163910cc0bd039e6163edc38580Gregg Van Hove & Phil Goodwinimport com.xtremelabs.robolectric.WithTestDefaultsRunner;
13cd1a58362a63860cbecd57e5234079fa51652783Rich Humphreyimport org.junit.Before;
14d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillipsimport org.junit.Test;
15d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillipsimport org.junit.runner.RunWith;
16d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips
1767142e6509cdc6c82290e955ae69400462cf52a9Christian Williams & Ian Fisherimport java.util.ArrayList;
1867142e6509cdc6c82290e955ae69400462cf52a9Christian Williams & Ian Fisherimport java.util.List;
1967142e6509cdc6c82290e955ae69400462cf52a9Christian Williams & Ian Fisher
209d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultzimport static com.xtremelabs.robolectric.Robolectric.shadowOf;
21d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillipsimport static java.util.Arrays.asList;
22d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richardimport static org.hamcrest.CoreMatchers.equalTo;
23d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richardimport static org.hamcrest.CoreMatchers.is;
24d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richardimport static org.hamcrest.CoreMatchers.not;
25d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richardimport static org.hamcrest.CoreMatchers.sameInstance;
267598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultzimport static org.junit.Assert.assertNull;
27d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillipsimport static org.junit.Assert.assertThat;
28d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips
2907257335f88da163910cc0bd039e6163edc38580Gregg Van Hove & Phil Goodwin@RunWith(WithTestDefaultsRunner.class)
30d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillipspublic class TextViewTest {
317598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz
327598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz    private TextView textView;
337598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz
34cd1a58362a63860cbecd57e5234079fa51652783Rich Humphrey    @Before
35cd1a58362a63860cbecd57e5234079fa51652783Rich Humphrey    public void setUp() throws Exception {
367598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        textView = new TextView(null);
37cd1a58362a63860cbecd57e5234079fa51652783Rich Humphrey    }
387598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz
39d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips    @Test
409d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultz    public void shouldTriggerTheImeListener() {
417598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        TextView textView = new TextView(null);
427598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        TestOnEditorActionListener actionListener = new TestOnEditorActionListener();
437598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        textView.setOnEditorActionListener(actionListener);
447598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz
457598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        shadowOf(textView).triggerEditorAction(EditorInfo.IME_ACTION_GO);
469d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultz
477598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        assertThat(actionListener.textView, is(textView));
487598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        assertThat(actionListener.sentImeId, equalTo(EditorInfo.IME_ACTION_GO));
499d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultz    }
509d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultz
519d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultz    @Test
52d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips    public void testGetUrls() throws Exception {
53d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips        textView.setText("here's some text http://google.com/\nblah\thttp://another.com/123?456 blah");
54d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips
55d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips        assertThat(urlStringsFrom(textView.getUrls()), equalTo(asList(
56d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips                "http://google.com/",
57d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips                "http://another.com/123?456"
58d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips        )));
59d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips    }
60d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips
61aa03b8dbd148420164db6b62f143ea0dc7710217Rich Humphrey    @Test
62d698df6daecfc469b84ace2c7fdaff79e85f21eaKathy Lin    public void testGetGravity() throws Exception {
637598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        assertThat(textView.getGravity(), not(equalTo(Gravity.CENTER)));
647598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        textView.setGravity(Gravity.CENTER);
657598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        assertThat(textView.getGravity(), equalTo(Gravity.CENTER));
66d698df6daecfc469b84ace2c7fdaff79e85f21eaKathy Lin    }
677598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz
68d698df6daecfc469b84ace2c7fdaff79e85f21eaKathy Lin    @Test
69aa03b8dbd148420164db6b62f143ea0dc7710217Rich Humphrey    public void testMovementMethod() {
70aa03b8dbd148420164db6b62f143ea0dc7710217Rich Humphrey        MovementMethod movement = new ArrowKeyMovementMethod();
717598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz
727598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        assertNull(textView.getMovementMethod());
73aa03b8dbd148420164db6b62f143ea0dc7710217Rich Humphrey        textView.setMovementMethod(movement);
747598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        assertThat(textView.getMovementMethod(), sameInstance(movement));
75aa03b8dbd148420164db6b62f143ea0dc7710217Rich Humphrey    }
767598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz
77cd1a58362a63860cbecd57e5234079fa51652783Rich Humphrey    @Test
78cd1a58362a63860cbecd57e5234079fa51652783Rich Humphrey    public void testLinksClickable() {
797598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        assertThat(textView.getLinksClickable(), equalTo(false));
807598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz
817598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        textView.setLinksClickable(true);
827598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        assertThat(textView.getLinksClickable(), equalTo(true));
837598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz
847598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        textView.setLinksClickable(false);
857598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        assertThat(textView.getLinksClickable(), equalTo(false));
86cd1a58362a63860cbecd57e5234079fa51652783Rich Humphrey    }
87d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips
88d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard    @Test
89a739b57a6b1a99f14d586f609f0f62f48b8e2284Ian Fisher & Tyler Schultz    public void testGetTextAppearanceId() throws Exception {
90a739b57a6b1a99f14d586f609f0f62f48b8e2284Ian Fisher & Tyler Schultz        TextView textView = new TextView(null);
91a739b57a6b1a99f14d586f609f0f62f48b8e2284Ian Fisher & Tyler Schultz        textView.setTextAppearance(null, 5);
92a739b57a6b1a99f14d586f609f0f62f48b8e2284Ian Fisher & Tyler Schultz
93a739b57a6b1a99f14d586f609f0f62f48b8e2284Ian Fisher & Tyler Schultz        assertThat(shadowOf(textView).getTextAppearanceId(), equalTo(5));
94a739b57a6b1a99f14d586f609f0f62f48b8e2284Ian Fisher & Tyler Schultz    }
95a739b57a6b1a99f14d586f609f0f62f48b8e2284Ian Fisher & Tyler Schultz
96ab527c2f7aad1f0109cc83efdd448077fb92324aLowell Kirsh & Ryan Richard    @Test
97d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard    public void shouldSetTextAndTextColorWhileInflatingXmlLayout() throws Exception {
98d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard        Activity activity = new Activity();
99d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard        activity.setContentView(R.layout.text_views);
100d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard
101d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard        TextView black = (TextView) activity.findViewById(R.id.black_text_view);
102d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard        assertThat(black.getText().toString(), equalTo("Black Text"));
103d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard        assertThat(shadowOf(black).getTextColorHexValue(), equalTo(0));
104d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard
105d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard        TextView white = (TextView) activity.findViewById(R.id.white_text_view);
106d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard        assertThat(white.getText().toString(), equalTo("White Text"));
107d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard        assertThat(shadowOf(white).getTextColorHexValue(), equalTo(activity.getResources().getColor(android.R.color.white)));
108d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard
109d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard        TextView grey = (TextView) activity.findViewById(R.id.grey_text_view);
110d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard        assertThat(grey.getText().toString(), equalTo("Grey Text"));
111d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard        assertThat(shadowOf(grey).getTextColorHexValue(), equalTo(activity.getResources().getColor(R.color.grey42)));
112d64bf34de2b01d88c33b05caa746b7163c16d098Ryan Richard    }
113ab527c2f7aad1f0109cc83efdd448077fb92324aLowell Kirsh & Ryan Richard
114d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips    private List<String> urlStringsFrom(URLSpan[] urlSpans) {
115d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips        List<String> urls = new ArrayList<String>();
116d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips        for (URLSpan urlSpan : urlSpans) {
117d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips            urls.add(urlSpan.getURL());
118d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips        }
119d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips        return urls;
120d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips    }
1219d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultz
1227598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz    private static class TestOnEditorActionListener implements TextView.OnEditorActionListener {
1237598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        private TextView textView;
1247598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        private int sentImeId;
1259d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultz
1267598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        @Override
1277598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        public boolean onEditorAction(TextView textView, int sentImeId, KeyEvent keyEvent) {
1287598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz            this.textView = textView;
1297598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz            this.sentImeId = sentImeId;
1307598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz            return false;
1317598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz        }
1329d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4dMike Grafton & Tyler Schultz    }
133d7fd158ed1c423bcd0e0c73dc05fa2ff3f81cb97Christian Williams & Jay Phillips}
134