Home | History | Annotate | Download | only in shadows
      1 package com.xtremelabs.robolectric.shadows;
      2 
      3 import android.util.DisplayMetrics;
      4 import android.util.TypedValue;
      5 import com.xtremelabs.robolectric.WithTestDefaultsRunner;
      6 import org.junit.Test;
      7 import org.junit.runner.RunWith;
      8 
      9 import static org.hamcrest.core.IsEqual.equalTo;
     10 import static org.junit.Assert.assertThat;
     11 
     12 @RunWith(WithTestDefaultsRunner.class)
     13 public class TypedValueTest {
     14 
     15     @Test
     16     public void testApplyDimensionIsWired() throws Exception {
     17         DisplayMetrics metrics = new DisplayMetrics();
     18         metrics.density = 0.5f;
     19         float convertedValue = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, metrics);
     20         assertThat(convertedValue, equalTo(50f));
     21     }
     22 
     23     @Test
     24     public void testCoerceBooleanToString() {
     25         String booleanFalseString = TypedValue.coerceToString(TypedValue.TYPE_INT_BOOLEAN, 0);
     26         String booleanTrueString = TypedValue.coerceToString(TypedValue.TYPE_INT_BOOLEAN, 1);
     27 
     28         assertThat(booleanFalseString, equalTo("false"));
     29         assertThat(booleanTrueString, equalTo("true"));
     30     }
     31 
     32     @Test
     33     public void testCoerceNullToString() {
     34         String nullString = TypedValue.coerceToString(TypedValue.TYPE_NULL, 0);
     35 
     36         assertThat(nullString, equalTo(null));
     37     }
     38 
     39     @Test
     40     public void testCoerceIntegerToString() {
     41         String intString = TypedValue.coerceToString(TypedValue.TYPE_INT_DEC, 37);
     42 
     43         assertThat(intString, equalTo("37"));
     44     }
     45 
     46     @Test
     47     public void testCoerceIntegerToHexString() {
     48         String hexString = TypedValue.coerceToString(TypedValue.TYPE_INT_HEX, 0xcafebabe);
     49 
     50         assertThat(hexString, equalTo("0xcafebabe"));
     51     }
     52 
     53     @Test
     54     public void testCoerceColorToString() {
     55         String colorString = TypedValue.coerceToString(TypedValue.TYPE_INT_COLOR_RGB8, 0xcafebabe);
     56 
     57         assertThat(colorString, equalTo("#cafebabe"));
     58     }
     59 
     60     @Test
     61     public void testSetTo() {
     62         TypedValue expectedValue = new TypedValue();
     63         expectedValue.assetCookie = 1;
     64         expectedValue.data = 3;
     65         expectedValue.density = 4;
     66         expectedValue.resourceId = 5;
     67         expectedValue.string = "string";
     68         expectedValue.type = 6;
     69 
     70         TypedValue actualValue = new TypedValue();
     71         actualValue.setTo(expectedValue);
     72 
     73         assertThat(expectedValue.assetCookie, equalTo(actualValue.assetCookie));
     74         assertThat(expectedValue.data, equalTo(actualValue.data));
     75         assertThat(expectedValue.density, equalTo(actualValue.density));
     76         assertThat(expectedValue.resourceId, equalTo(actualValue.resourceId));
     77         assertThat(expectedValue.string, equalTo(actualValue.string));
     78         assertThat(expectedValue.type, equalTo(actualValue.type));
     79     }
     80 }
     81