TextViewTest.java revision 9d1f4261b8e45bd2589eb1b1d9c9ecb8b725ca4d
1package com.xtremelabs.robolectric.shadows;
2
3import android.text.style.URLSpan;
4import android.view.KeyEvent;
5import android.view.inputmethod.EditorInfo;
6import android.widget.TextView;
7import com.xtremelabs.robolectric.WithTestDefaultsRunner;
8import org.junit.Test;
9import org.junit.runner.RunWith;
10
11import java.util.ArrayList;
12import java.util.List;
13
14import static com.xtremelabs.robolectric.Robolectric.shadowOf;
15import static java.util.Arrays.asList;
16import static org.hamcrest.CoreMatchers.equalTo;
17import static org.hamcrest.CoreMatchers.is;
18import static org.junit.Assert.assertThat;
19
20@RunWith(WithTestDefaultsRunner.class)
21public class TextViewTest {
22    @Test
23    public void shouldTriggerTheImeListener() {
24      TextView textView = new TextView(null);
25      TestOnEditorActionListener actionListener = new TestOnEditorActionListener();
26      textView.setOnEditorActionListener(actionListener);
27
28      shadowOf(textView).triggerEditorAction(EditorInfo.IME_ACTION_GO);
29
30      assertThat(actionListener.textView, is(textView));
31      assertThat(actionListener.sentImeId, equalTo(EditorInfo.IME_ACTION_GO));
32    }
33
34    @Test
35    public void testGetUrls() throws Exception {
36        TextView textView = new TextView(null);
37        textView.setText("here's some text http://google.com/\nblah\thttp://another.com/123?456 blah");
38
39        assertThat(urlStringsFrom(textView.getUrls()), equalTo(asList(
40                "http://google.com/",
41                "http://another.com/123?456"
42        )));
43    }
44
45    private List<String> urlStringsFrom(URLSpan[] urlSpans) {
46        List<String> urls = new ArrayList<String>();
47        for (URLSpan urlSpan : urlSpans) {
48            urls.add(urlSpan.getURL());
49        }
50        return urls;
51    }
52
53  private static class TestOnEditorActionListener implements TextView.OnEditorActionListener {
54    private TextView textView;
55    private int sentImeId;
56
57    @Override public boolean onEditorAction(TextView textView, int sentImeId, KeyEvent keyEvent) {
58      this.textView = textView;
59      this.sentImeId = sentImeId;
60      return false;
61    }
62  }
63}
64