aboutsummaryrefslogtreecommitdiff
path: root/v1/src/test/java/com/xtremelabs/robolectric/shadows/TypedValueTest.java
blob: 7cf2d5556533527d84b3cac5b6de54229b088c7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.xtremelabs.robolectric.shadows;

import android.util.DisplayMetrics;
import android.util.TypedValue;
import com.xtremelabs.robolectric.WithTestDefaultsRunner;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;

@RunWith(WithTestDefaultsRunner.class)
public class TypedValueTest {

    @Test
    public void testApplyDimensionIsWired() throws Exception {
        DisplayMetrics metrics = new DisplayMetrics();
        metrics.density = 0.5f;
        float convertedValue = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, metrics);
        assertThat(convertedValue, equalTo(50f));
    }
    
    @Test
    public void testCoerceBooleanToString() {
        String booleanFalseString = TypedValue.coerceToString(TypedValue.TYPE_INT_BOOLEAN, 0);
        String booleanTrueString = TypedValue.coerceToString(TypedValue.TYPE_INT_BOOLEAN, 1);
        
        assertThat(booleanFalseString, equalTo("false"));
        assertThat(booleanTrueString, equalTo("true"));
    }
    
    @Test
    public void testCoerceNullToString() {
        String nullString = TypedValue.coerceToString(TypedValue.TYPE_NULL, 0);
        
        assertThat(nullString, equalTo(null));
    }
    
    @Test
    public void testCoerceIntegerToString() {
        String intString = TypedValue.coerceToString(TypedValue.TYPE_INT_DEC, 37);
        
        assertThat(intString, equalTo("37"));
    }
    
    @Test
    public void testCoerceIntegerToHexString() {
        String hexString = TypedValue.coerceToString(TypedValue.TYPE_INT_HEX, 0xcafebabe);
        
        assertThat(hexString, equalTo("0xcafebabe"));
    }
    
    @Test
    public void testCoerceColorToString() {
        String colorString = TypedValue.coerceToString(TypedValue.TYPE_INT_COLOR_RGB8, 0xcafebabe);
        
        assertThat(colorString, equalTo("#cafebabe"));
    }
    
    @Test
    public void testSetTo() {
        TypedValue expectedValue = new TypedValue();
        expectedValue.assetCookie = 1;
        expectedValue.data = 3;
        expectedValue.density = 4;
        expectedValue.resourceId = 5;
        expectedValue.string = "string";
        expectedValue.type = 6;
        
        TypedValue actualValue = new TypedValue();
        actualValue.setTo(expectedValue);
        
        assertThat(expectedValue.assetCookie, equalTo(actualValue.assetCookie));
        assertThat(expectedValue.data, equalTo(actualValue.data));
        assertThat(expectedValue.density, equalTo(actualValue.density));
        assertThat(expectedValue.resourceId, equalTo(actualValue.resourceId));
        assertThat(expectedValue.string, equalTo(actualValue.string));
        assertThat(expectedValue.type, equalTo(actualValue.type));
    }
}