1package org.robolectric.shadows;
2
3import static org.assertj.core.api.Assertions.assertThat;
4import static org.robolectric.RuntimeEnvironment.application;
5
6import android.content.Context;
7import android.util.AttributeSet;
8import android.view.LayoutInflater;
9import android.widget.EditText;
10import java.util.Random;
11import org.junit.Before;
12import org.junit.Test;
13import org.junit.runner.RunWith;
14import org.robolectric.R;
15import org.robolectric.Robolectric;
16import org.robolectric.RobolectricTestRunner;
17import org.robolectric.RuntimeEnvironment;
18
19@RunWith(RobolectricTestRunner.class)
20public class ShadowEditTextTest {
21  private EditText editText;
22
23  @Before
24  public void setup() {
25    AttributeSet attributeSet = Robolectric.buildAttributeSet()
26        .addAttribute(android.R.attr.maxLength, "5")
27        .build();
28
29    editText = new EditText(application, attributeSet);
30  }
31
32  @Test
33  public void shouldRespectMaxLength() throws Exception {
34    editText.setText("0123456678");
35    assertThat(editText.getText().toString()).isEqualTo("01234");
36  }
37
38  @Test
39  public void shouldAcceptNullStrings() {
40    editText.setText(null);
41    assertThat(editText.getText().toString()).isEqualTo("");
42  }
43
44  @Test
45  public void givenInitializingWithAttributeSet_whenMaxLengthDefined_thenRestrictTextLengthToMaxLength() {
46    int maxLength = anyInteger();
47    AttributeSet attrs = Robolectric.buildAttributeSet()
48        .addAttribute(android.R.attr.maxLength, maxLength + "")
49        .build();
50
51    EditText editText = new EditText(RuntimeEnvironment.application, attrs);
52    String excessiveInput = stringOfLength(maxLength * 2);
53
54    editText.setText(excessiveInput);
55
56    assertThat((CharSequence) editText.getText().toString()).isEqualTo(excessiveInput.subSequence(0, maxLength));
57  }
58
59  @Test
60  public void givenInitializingWithAttributeSet_whenMaxLengthNotDefined_thenTextLengthShouldHaveNoRestrictions() {
61    AttributeSet attrs = Robolectric.buildAttributeSet().build();
62    EditText editText = new EditText(RuntimeEnvironment.application, attrs);
63    String input = anyString();
64
65    editText.setText(input);
66
67    assertThat(editText.getText().toString()).isEqualTo(input);
68  }
69
70  @Test
71  public void whenInitializingWithoutAttributeSet_thenTextLengthShouldHaveNoRestrictions() {
72    EditText editText = new EditText(RuntimeEnvironment.application);
73    String input = anyString();
74
75    editText.setText(input);
76
77    assertThat(editText.getText().toString()).isEqualTo(input);
78  }
79
80  @Test
81  public void testSelectAll() {
82    EditText editText = new EditText(RuntimeEnvironment.application);
83    editText.setText("foo");
84
85    editText.selectAll();
86
87    assertThat(editText.getSelectionStart()).isEqualTo(0);
88    assertThat(editText.getSelectionEnd()).isEqualTo(3);
89  }
90
91  @Test
92  public void shouldGetHintFromXml() {
93    Context context = RuntimeEnvironment.application;
94    LayoutInflater inflater = LayoutInflater.from(context);
95    EditText editText = (EditText) inflater.inflate(R.layout.edit_text, null);
96    assertThat(editText.getHint().toString()).isEqualTo("Hello, Hint");
97  }
98
99  private String anyString() {
100    return stringOfLength(anyInteger());
101  }
102
103  private String stringOfLength(int length) {
104    StringBuilder stringBuilder = new StringBuilder();
105
106    for (int i = 0; i < length; i++)
107      stringBuilder.append('x');
108
109    return stringBuilder.toString();
110  }
111
112  private int anyInteger() {
113    return new Random().nextInt(1000) + 1;
114  }
115
116}
117