Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import static org.assertj.core.api.Assertions.assertThat;
      4 import static org.robolectric.RuntimeEnvironment.application;
      5 
      6 import android.content.Context;
      7 import android.util.AttributeSet;
      8 import android.view.LayoutInflater;
      9 import android.widget.EditText;
     10 import java.util.Random;
     11 import org.junit.Before;
     12 import org.junit.Test;
     13 import org.junit.runner.RunWith;
     14 import org.robolectric.R;
     15 import org.robolectric.Robolectric;
     16 import org.robolectric.RobolectricTestRunner;
     17 import org.robolectric.RuntimeEnvironment;
     18 
     19 @RunWith(RobolectricTestRunner.class)
     20 public 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