1package com.xtremelabs.robolectric.shadows;
2
3import static org.hamcrest.CoreMatchers.equalTo;
4import static org.junit.Assert.assertThat;
5import static org.junit.Assert.assertTrue;
6import static org.mockito.Matchers.anyInt;
7import static org.mockito.Matchers.eq;
8import static org.mockito.Mockito.mock;
9import static org.mockito.Mockito.when;
10
11import java.util.Random;
12
13import org.junit.Test;
14import org.junit.runner.RunWith;
15
16import android.util.AttributeSet;
17import android.widget.EditText;
18
19import com.xtremelabs.robolectric.WithTestDefaultsRunner;
20
21@RunWith(WithTestDefaultsRunner.class)
22public class EditTextTest {
23
24    @Test
25    public void shouldBeFocusableByDefault() throws Exception {
26        assertTrue(new EditText(null).isFocusable());
27        assertTrue(new EditText(null).isFocusableInTouchMode());
28    }
29
30    @Test
31    public void givenInitializingWithAttributeSet_whenMaxLengthDefined_thenRestrictTextLengthToMaxLength() {
32        int maxLength = anyInteger();
33        AttributeSet attrs = attributeSetWithMaxLength(maxLength);
34        EditText editText = new EditText(null, attrs);
35        String excessiveInput = stringOfLength(maxLength * 2);
36
37        editText.setText(excessiveInput);
38
39        assertThat(editText.getText().toString(), equalTo(excessiveInput.subSequence(0, maxLength)));
40    }
41
42    @Test
43    public void givenInitializingWithAttributeSet_whenMaxLengthNotDefined_thenTextLengthShouldHaveNoRestrictions() {
44        AttributeSet attrs = attributeSetWithoutMaxLength();
45        EditText editText = new EditText(null, attrs);
46        String input = anyString();
47
48        editText.setText(input);
49
50        assertThat(editText.getText().toString(), equalTo(input));
51    }
52
53    @Test
54    public void whenInitializingWithoutAttributeSet_thenTextLengthShouldHaveNoRestrictions() {
55        EditText editText = new EditText(null);
56        String input = anyString();
57
58        editText.setText(input);
59
60        assertThat(editText.getText().toString(), equalTo(input));
61    }
62
63    private String anyString() {
64        return stringOfLength(anyInteger());
65    }
66
67    private String stringOfLength(int length) {
68        StringBuilder stringBuilder = new StringBuilder();
69
70        for (int i = 0; i < length; i++)
71            stringBuilder.append('x');
72
73        return stringBuilder.toString();
74    }
75
76    private int anyInteger() {
77        return new Random().nextInt(1000) + 1;
78    }
79
80    private AttributeSet attributeSetWithMaxLength(int maxLength) {
81        AttributeSet attrs = mock(AttributeSet.class);
82        when(attrs.getAttributeIntValue(eq("android"), eq("maxLength"), anyInt())).thenReturn(maxLength);
83        return attrs;
84    }
85
86    private AttributeSet attributeSetWithoutMaxLength() {
87        AttributeSet attrs = mock(AttributeSet.class);
88        when(attrs.getAttributeIntValue("android", "maxLength", Integer.MAX_VALUE)).thenReturn(Integer.MAX_VALUE);
89        return attrs;
90    }
91}
92