PrimitiveTest.java revision ea99ab0ba2830711b1f074f34f647e075da1c38d
1/* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.content.res.cts; 18 19import android.content.res.Resources; 20import android.content.res.TypedArray; 21import android.test.AndroidTestCase; 22import android.test.suitebuilder.annotation.SmallTest; 23import android.util.TypedValue; 24import com.android.cts.stub.R; 25 26public class PrimitiveTest extends AndroidTestCase { 27 private Resources mResources; 28 29 @Override 30 protected void setUp() throws Exception { 31 super.setUp(); 32 mResources = mContext.getResources(); 33 } 34 35 private void tryEnum(final int resid, final int expected) { 36 final TypedArray sa = mContext.obtainStyledAttributes(resid, R.styleable.EnumStyle); 37 final int value = sa.getInt(R.styleable.EnumStyle_testEnum, -1); 38 sa.recycle(); 39 40 assertEquals("Expecting value " + expected + " got " + value 41 + ": in resource 0x" + Integer.toHexString(resid), expected, value); 42 } 43 44 @SmallTest 45 public void testEnum() { 46 tryEnum(R.style.TestEnum1, 1); 47 tryEnum(R.style.TestEnum2, 2); 48 tryEnum(R.style.TestEnum10, 10); 49 tryEnum(R.style.TestEnum1_EmptyInherit, 1); 50 } 51 52 private void tryFlag(final int resid, final int expected) { 53 final TypedArray sa = mContext.obtainStyledAttributes(resid, R.styleable.FlagStyle); 54 final int value = sa.getInt(R.styleable.FlagStyle_testFlags, -1); 55 sa.recycle(); 56 57 assertEquals("Expecting value " + expected + " got " + value 58 + ": in resource 0x" + Integer.toHexString(resid), expected, value); 59 } 60 61 @SmallTest 62 public void testFlags() throws Exception { 63 tryFlag(R.style.TestFlag1, 0x1); 64 tryFlag(R.style.TestFlag2, 0x2); 65 tryFlag(R.style.TestFlag31, 0x40000000); 66 tryFlag(R.style.TestFlag1And2, 0x3); 67 tryFlag(R.style.TestFlag1And2And31, 0x40000003); 68 } 69 70 private void tryBoolean(final int resid, final boolean expected) { 71 final TypedValue v = new TypedValue(); 72 mContext.getResources().getValue(resid, v, true); 73 assertEquals(TypedValue.TYPE_INT_BOOLEAN, v.type); 74 assertEquals("Expecting boolean value " + expected + " got " + v 75 + " from TypedValue: in resource 0x" + Integer.toHexString(resid), 76 expected, v.data != 0); 77 assertEquals("Expecting boolean value " + expected + " got " + v 78 + " from getBoolean(): in resource 0x" + Integer.toHexString(resid), 79 expected, mContext.getResources().getBoolean(resid)); 80 } 81 82 @SmallTest 83 public void testBoolean() { 84 tryBoolean(R.bool.trueRes, true); 85 tryBoolean(R.bool.falseRes, false); 86 } 87 88 private void tryString(final int resid, final String expected) { 89 final TypedValue v = new TypedValue(); 90 mContext.getResources().getValue(resid, v, true); 91 assertEquals(TypedValue.TYPE_STRING, v.type); 92 assertEquals("Expecting string value " + expected + " got " + v 93 + ": in resource 0x" + Integer.toHexString(resid), 94 expected, v.string); 95 } 96 97 @SmallTest 98 public void testStringCoerce() { 99 tryString(R.string.coerceIntegerToString, "100"); 100 tryString(R.string.coerceBooleanToString, "true"); 101 tryString(R.string.coerceColorToString, "#fff"); 102 tryString(R.string.coerceFloatToString, "100.0"); 103 tryString(R.string.coerceDimensionToString, "100px"); 104 tryString(R.string.coerceFractionToString, "100%"); 105 } 106 107 private static void checkString(final int resid, final String actual, final String expected) { 108 assertEquals("Expecting string value \"" + expected + "\" got \"" 109 + actual + "\" in resources 0x" + Integer.toHexString(resid), 110 expected, actual); 111 } 112 113 @SmallTest 114 public void testFormattedString() { 115 // Make sure the regular one doesn't format anything 116 checkString(R.string.formattedStringNone, 117 mResources.getString(R.string.formattedStringNone), 118 "Format[]"); 119 checkString(R.string.formattedStringOne, 120 mResources.getString(R.string.formattedStringOne), 121 "Format[%d]"); 122 checkString(R.string.formattedStringTwo, 123 mResources.getString(R.string.formattedStringTwo), 124 "Format[%3$d,%2$s]"); 125 // Make sure the formatted one works 126 checkString(R.string.formattedStringNone, 127 mResources.getString(R.string.formattedStringNone), 128 "Format[]"); 129 checkString(R.string.formattedStringOne, 130 mResources.getString(R.string.formattedStringOne, 42), 131 "Format[42]"); 132 checkString(R.string.formattedStringTwo, 133 mResources.getString(R.string.formattedStringTwo, "unused", "hi", 43), 134 "Format[43,hi]"); 135 } 136} 137 138