FileDocCategorySizeDatePackage
PrimitiveTest.javaAPI DocAndroid 1.5 API5513Wed May 06 22:42:02 BST 2009com.android.unit_tests.content

PrimitiveTest

public class PrimitiveTest extends android.test.AndroidTestCase

Fields Summary
private android.content.res.Resources
mResources
Constructors Summary
Methods Summary
private static voidcheckString(int resid, java.lang.String actual, java.lang.String expected)

        assertEquals("Expecting string value \"" + expected + "\" got \""
                + actual + "\" in resources 0x" + Integer.toHexString(resid),
                expected, actual);
    
protected voidsetUp()

        super.setUp();
        mResources = mContext.getResources();
    
public voidtestBoolean()

        tryBoolean(R.bool.trueRes, true);
        tryBoolean(R.bool.falseRes, false);
    
public voidtestEnum()

        tryEnum(R.style.TestEnum1, 1);
        tryEnum(R.style.TestEnum2, 2);
        tryEnum(R.style.TestEnum10, 10);
        tryEnum(R.style.TestEnum1_EmptyInherit, 1);
    
public voidtestFlags()

        tryFlag(R.style.TestFlag1, 0x1);
        tryFlag(R.style.TestFlag2, 0x2);
        tryFlag(R.style.TestFlag31, 0x40000000);
        tryFlag(R.style.TestFlag1And2, 0x3);
        tryFlag(R.style.TestFlag1And2And31, 0x40000003);
    
public voidtestFormattedString()

        // Make sure the regular one doesn't format anything
        checkString(R.string.formattedStringNone,
                mResources.getString(R.string.formattedStringNone),
                "Format[]");
        checkString(R.string.formattedStringOne,
                mResources.getString(R.string.formattedStringOne),
                "Format[%d]");
        checkString(R.string.formattedStringTwo,
                mResources.getString(R.string.formattedStringTwo),
                "Format[%3$d,%2$s]");
        // Make sure the formatted one works
        checkString(R.string.formattedStringNone,
                mResources.getString(R.string.formattedStringNone),
                "Format[]");
        checkString(R.string.formattedStringOne,
                mResources.getString(R.string.formattedStringOne, 42),
                "Format[42]");
        checkString(R.string.formattedStringTwo,
                mResources.getString(R.string.formattedStringTwo, "unused", "hi", 43),
                "Format[43,hi]");
    
public voidtestStringCoerce()

        tryString(R.string.coerceIntegerToString, "100");
        tryString(R.string.coerceBooleanToString, "true");
        tryString(R.string.coerceColorToString, "#fff");
        tryString(R.string.coerceFloatToString, "100.0");
        tryString(R.string.coerceDimensionToString, "100px");
        tryString(R.string.coerceFractionToString, "100%");
    
private voidtryBoolean(int resid, boolean expected)

        TypedValue v = new TypedValue();
        mContext.getResources().getValue(resid, v, true);
        assertEquals(TypedValue.TYPE_INT_BOOLEAN, v.type);
        assertEquals("Expecting boolean value " + expected + " got " + v
                + " from TypedValue: in resource 0x" + Integer.toHexString(resid),
                expected, v.data != 0);
        assertEquals("Expecting boolean value " + expected + " got " + v
                + " from getBoolean(): in resource 0x" + Integer.toHexString(resid),
                expected, mContext.getResources().getBoolean(resid));
    
private voidtryEnum(int resid, int expected)

        TypedArray sa =
                mContext.obtainStyledAttributes(resid, R.styleable.EnumStyle);
        int value = sa.getInt(R.styleable.EnumStyle_testEnum, -1);
        sa.recycle();

        assertEquals("Expecting value " + expected + " got " + value
                + ": in resource 0x" + Integer.toHexString(resid),
                expected, value);
    
private voidtryFlag(int resid, int expected)

        TypedArray sa =
                mContext.obtainStyledAttributes(resid, R.styleable.FlagStyle);
        int value = sa.getInt(R.styleable.FlagStyle_testFlags, -1);
        sa.recycle();

        assertEquals("Expecting value " + expected + " got " + value
                + ": in resource 0x" + Integer.toHexString(resid),
                expected, value);
    
private voidtryString(int resid, java.lang.String expected)

        TypedValue v = new TypedValue();
        mContext.getResources().getValue(resid, v, true);
        assertEquals(TypedValue.TYPE_STRING, v.type);
        assertEquals("Expecting string value " + expected + " got " + v
                + ": in resource 0x" + Integer.toHexString(resid),
                expected, v.string);