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 17 package android.content.res.cts; 18 19 import com.android.cts.content.R; 20 import com.android.internal.util.XmlUtils; 21 22 23 import org.xmlpull.v1.XmlPullParser; 24 import org.xmlpull.v1.XmlPullParserException; 25 26 import android.content.Context; 27 import android.content.res.AssetManager; 28 import android.content.res.ColorStateList; 29 import android.content.res.Configuration; 30 import android.content.res.Resources; 31 import android.content.res.TypedArray; 32 import android.content.res.XmlResourceParser; 33 import android.content.res.Resources.NotFoundException; 34 import android.graphics.drawable.Drawable; 35 import android.os.Bundle; 36 import android.test.AndroidTestCase; 37 import android.util.AttributeSet; 38 import android.util.DisplayMetrics; 39 import android.util.TypedValue; 40 import android.util.Xml; 41 import android.view.Display; 42 import android.view.WindowManager; 43 44 import java.io.IOException; 45 import java.io.InputStream; 46 import java.util.Locale; 47 48 public class ResourcesTest extends AndroidTestCase { 49 private static final String CONFIG_VARYING = "configVarying"; 50 private static final String SIMPLE = "simple"; 51 private static final String CONFIG_VARYING_SIMPLE = "configVarying/simple"; 52 private static final String PACKAGE_NAME = "com.android.cts.content"; 53 private static final String COM_ANDROID_CTS_STUB_IDENTIFIER = 54 "com.android.cts.content:configVarying/simple"; 55 private Resources mResources; 56 57 @Override setUp()58 protected void setUp() throws Exception { 59 super.setUp(); 60 61 mResources = getContext().getResources(); 62 } 63 testResources()64 public void testResources() { 65 final AssetManager am = new AssetManager(); 66 final Configuration cfg = new Configuration(); 67 cfg.keyboard = Configuration.KEYBOARDHIDDEN_YES; 68 final DisplayMetrics dm = new DisplayMetrics(); 69 dm.setToDefaults(); 70 71 final Resources r = new Resources(am, dm, cfg); 72 final Configuration c = r.getConfiguration(); 73 assertEquals(Configuration.KEYBOARDHIDDEN_YES, c.keyboard); 74 } 75 testGetString()76 public void testGetString() { 77 try { 78 mResources.getString(-1, "%s"); 79 fail("Failed at testGetString2"); 80 } catch (NotFoundException e) { 81 //expected 82 } 83 84 final String strGo = mResources.getString(R.string.go, "%1$s%%", 12); 85 assertEquals("Go", strGo); 86 } 87 testObtainAttributes()88 public void testObtainAttributes() throws XmlPullParserException, IOException { 89 final XmlPullParser parser = mResources.getXml(R.xml.test_color); 90 XmlUtils.beginDocument(parser, "resources"); 91 final AttributeSet set = Xml.asAttributeSet(parser); 92 final TypedArray testTypedArray = mResources.obtainAttributes(set, R.styleable.Style1); 93 assertNotNull(testTypedArray); 94 assertEquals(2, testTypedArray.length()); 95 assertEquals(0, testTypedArray.getColor(0, 0)); 96 assertEquals(128, testTypedArray.getColor(1, 128)); 97 assertEquals(mResources, testTypedArray.getResources()); 98 testTypedArray.recycle(); 99 } 100 testObtainTypedArray()101 public void testObtainTypedArray() { 102 try { 103 mResources.obtainTypedArray(-1); 104 fail("Failed at testObtainTypedArray"); 105 } catch (NotFoundException e) { 106 //expected 107 } 108 109 final TypedArray ta = mResources.obtainTypedArray(R.array.string); 110 assertEquals(3, ta.length()); 111 assertEquals("Test String 1", ta.getString(0)); 112 assertEquals("Test String 2", ta.getString(1)); 113 assertEquals("Test String 3", ta.getString(2)); 114 assertEquals(mResources, ta.getResources()); 115 } 116 getResources(final Configuration config, final int mcc, final int mnc, final int touchscreen, final int keyboard, final int keysHidden, final int navigation, final int width, final int height)117 private Resources getResources(final Configuration config, final int mcc, final int mnc, 118 final int touchscreen, final int keyboard, final int keysHidden, final int navigation, 119 final int width, final int height) { 120 final AssetManager assmgr = new AssetManager(); 121 assmgr.addAssetPath(mContext.getPackageResourcePath()); 122 final DisplayMetrics metrics = new DisplayMetrics(); 123 final WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); 124 final Display d = wm.getDefaultDisplay(); 125 d.getMetrics(metrics); 126 config.mcc = mcc; 127 config.mnc = mnc; 128 config.touchscreen = touchscreen; 129 config.keyboard = keyboard; 130 config.keyboardHidden = keysHidden; 131 config.navigation = navigation; 132 metrics.widthPixels = width; 133 metrics.heightPixels = height; 134 return new Resources(assmgr, metrics, config); 135 } 136 checkGetText1(final Resources res, final int resId, final String expectedValue)137 private static void checkGetText1(final Resources res, final int resId, 138 final String expectedValue) { 139 final String actual = res.getText(resId).toString(); 140 assertNotNull("Returned wrong configuration-based simple value: expected <nothing>, " 141 + "got '" + actual + "' from resource 0x" + Integer.toHexString(resId), 142 expectedValue); 143 assertEquals("Returned wrong configuration-based simple value: expected " + expectedValue 144 + ", got '" + actual + "' from resource 0x" + Integer.toHexString(resId), 145 expectedValue, actual); 146 } 147 checkGetText2(final Resources res, final int resId, final String expectedValue)148 private static void checkGetText2(final Resources res, final int resId, 149 final String expectedValue) { 150 final String actual = res.getText(resId, null).toString(); 151 assertNotNull("Returned wrong configuration-based simple value: expected <nothing>, " 152 + "got '" + actual + "' from resource 0x" + Integer.toHexString(resId), 153 expectedValue); 154 assertEquals("Returned wrong configuration-based simple value: expected " + expectedValue 155 + ", got '" + actual + "' from resource 0x" + Integer.toHexString(resId), 156 expectedValue, actual); 157 } 158 testGetMovie()159 public void testGetMovie() { 160 try { 161 mResources.getMovie(-1); 162 fail("Failed at testGetMovie"); 163 } catch (NotFoundException e) { 164 //expected 165 } 166 } 167 testGetDimension()168 public void testGetDimension() { 169 try { 170 mResources.getDimension(-1); 171 fail("Failed at testGetDimension"); 172 } catch (NotFoundException e) { 173 //expected 174 } 175 176 // app_icon_size is 48px, as defined in cts/tests/res/values/resources_test.xml 177 final float dim = mResources.getDimension(R.dimen.app_icon_size); 178 assertEquals(48.0f, dim); 179 } 180 testGetDimensionPixelOffset()181 public void testGetDimensionPixelOffset() { 182 try { 183 mResources.getDimensionPixelOffset(-1); 184 fail("Failed at testGetDimensionPixelOffset"); 185 } catch (NotFoundException e) { 186 //expected 187 } 188 189 // app_icon_size is 48px, as defined in cts/tests/res/values/resources_test.xml 190 final int dim = mResources.getDimensionPixelOffset(R.dimen.app_icon_size); 191 assertEquals(48, dim); 192 } 193 testGetColorStateList()194 public void testGetColorStateList() { 195 try { 196 mResources.getColorStateList(-1); 197 fail("Failed at testGetColorStateList"); 198 } catch (NotFoundException e) { 199 //expected 200 } 201 202 final ColorStateList colorStateList = mResources.getColorStateList(R.color.color1); 203 final int[] focusedState = {android.R.attr.state_focused}; 204 final int focusColor = colorStateList.getColorForState(focusedState, R.color.failColor); 205 assertEquals(mResources.getColor(R.color.testcolor1), focusColor); 206 } 207 testGetColor()208 public void testGetColor() { 209 try { 210 mResources.getColor(-1); 211 fail("Failed at testGetColor"); 212 } catch (NotFoundException e) { 213 //expected 214 } 215 216 final int color = mResources.getColor(R.color.testcolor1); 217 assertEquals(0xff00ff00, color); 218 } 219 testUpdateConfiguration()220 public void testUpdateConfiguration() { 221 final Configuration cfg = mResources.getConfiguration(); 222 assertTrue(cfg.fontScale != 5); 223 224 cfg.fontScale = 5; 225 mResources.updateConfiguration(cfg, null); 226 Configuration cfgNew = mResources.getConfiguration(); 227 assertEquals(5.0f, cfgNew.fontScale, 0.001f); 228 } 229 testGetDimensionPixelSize()230 public void testGetDimensionPixelSize() { 231 try { 232 mResources.getDimensionPixelSize(-1); 233 fail("Failed at testGetDimensionPixelSize"); 234 } catch (NotFoundException e) { 235 //expected 236 } 237 238 // app_icon_size is 48px, as defined in cts/tests/res/values/resources_test.xml 239 final int size = mResources.getDimensionPixelSize(R.dimen.app_icon_size); 240 assertEquals(48, size); 241 } 242 testGetDrawable()243 public void testGetDrawable() { 244 try { 245 mResources.getDrawable(-1); 246 fail("Failed at testGetDrawable"); 247 } catch (NotFoundException e) { 248 //expected 249 } 250 251 // testimage is defined in cts/tests/res/drawable/testimage.jpg and measures 212px x 142px 252 final Drawable draw = mResources.getDrawable(R.drawable.testimage); 253 int targetDensity = mResources.getDisplayMetrics().densityDpi; 254 int defaultDensity = DisplayMetrics.DENSITY_DEFAULT; 255 assertNotNull(draw); 256 assertEquals(212 * targetDensity / defaultDensity, draw.getIntrinsicWidth(), 1); 257 assertEquals(142 * targetDensity / defaultDensity, draw.getIntrinsicHeight(), 1); 258 } 259 testGetAnimation()260 public void testGetAnimation() throws Exception { 261 try { 262 mResources.getAnimation(-1); 263 fail("Failed at testGetAnimation"); 264 } catch (NotFoundException e) { 265 //expected 266 } 267 268 final XmlResourceParser ani = mResources.getAnimation(R.anim.anim_rotate); 269 assertNotNull(ani); 270 XmlUtils.beginDocument(ani, "rotate"); 271 assertEquals(7, ani.getAttributeCount()); 272 assertEquals("Binary XML file line #18", ani.getPositionDescription()); 273 assertEquals("interpolator", ani.getAttributeName(0)); 274 assertEquals("@17432582", ani.getAttributeValue(0)); 275 } 276 testGetQuantityString1()277 public void testGetQuantityString1() { 278 try { 279 mResources.getQuantityString(-1, 1, ""); 280 fail("Failed at testGetQuantityString1"); 281 } catch (NotFoundException e) { 282 //expected 283 } 284 285 final String strGo = mResources.getQuantityString(R.plurals.plurals_test, 1, ""); 286 assertEquals("A dog", strGo); 287 } 288 testGetQuantityString2()289 public void testGetQuantityString2() { 290 try { 291 mResources.getQuantityString(-1, 1); 292 fail("Failed at testGetQuantityString2"); 293 } catch (NotFoundException e) { 294 //expected 295 } 296 297 final String strGo = mResources.getQuantityString(R.plurals.plurals_test, 1); 298 assertEquals("A dog", strGo); 299 } 300 testGetInteger()301 public void testGetInteger() { 302 try { 303 mResources.getInteger(-1); 304 fail("Failed at testGetInteger"); 305 } catch (NotFoundException e) { 306 //expected 307 } 308 309 final int i = mResources.getInteger(R.integer.resource_test_int); 310 assertEquals(10, i); 311 } 312 testGetValue()313 public void testGetValue() { 314 final TypedValue tv = new TypedValue(); 315 316 try { 317 mResources.getValue("null", tv, false); 318 fail("Failed at testGetValue"); 319 } catch (NotFoundException e) { 320 //expected 321 } 322 323 mResources.getValue("com.android.cts.content:raw/text", tv, false); 324 assertNotNull(tv); 325 assertEquals("res/raw/text.txt", tv.coerceToString()); 326 } 327 testGetAssets()328 public void testGetAssets() { 329 final AssetManager aM = mResources.getAssets(); 330 assertNotNull(aM); 331 assertTrue(aM.isUpToDate()); 332 } 333 testGetSystem()334 public void testGetSystem() { 335 assertNotNull(Resources.getSystem()); 336 } 337 testGetLayout()338 public void testGetLayout() throws Exception { 339 try { 340 mResources.getLayout(-1); 341 fail("Failed at testGetLayout"); 342 } catch (NotFoundException e) { 343 //expected 344 } 345 346 final XmlResourceParser layout = mResources.getLayout(R.layout.abslistview_layout); 347 assertNotNull(layout); 348 XmlUtils.beginDocument(layout, "ViewGroup_Layout"); 349 assertEquals(3, layout.getAttributeCount()); 350 assertEquals("id", layout.getAttributeName(0)); 351 assertEquals("@" + R.id.abslistview_root, layout.getAttributeValue(0)); 352 } 353 testGetBoolean()354 public void testGetBoolean() { 355 try { 356 mResources.getBoolean(-1); 357 fail("Failed at testGetBoolean"); 358 } catch (NotFoundException e) { 359 //expected 360 } 361 362 final boolean b = mResources.getBoolean(R.integer.resource_test_int); 363 assertTrue(b); 364 } 365 testgetFraction()366 public void testgetFraction() { 367 assertEquals(1, (int)mResources.getFraction(R.dimen.frac100perc, 1, 1)); 368 assertEquals(100, (int)mResources.getFraction(R.dimen.frac100perc, 100, 1)); 369 } 370 testParseBundleExtras()371 public void testParseBundleExtras() throws XmlPullParserException, IOException { 372 final Bundle b = new Bundle(); 373 XmlResourceParser parser = mResources.getXml(R.xml.extra); 374 XmlUtils.beginDocument(parser, "tag"); 375 376 assertEquals(0, b.size()); 377 mResources.parseBundleExtras(parser, b); 378 assertEquals(1, b.size()); 379 assertEquals("android", b.getString("google")); 380 } 381 testParseBundleExtra()382 public void testParseBundleExtra() throws XmlPullParserException, IOException { 383 final Bundle b = new Bundle(); 384 XmlResourceParser parser = mResources.getXml(R.xml.extra); 385 386 XmlUtils.beginDocument(parser, "tag"); 387 assertEquals(0, b.size()); 388 mResources.parseBundleExtra("test", parser, b); 389 assertEquals(1, b.size()); 390 assertEquals("Lee", b.getString("Bruce")); 391 } 392 testGetIdentifier()393 public void testGetIdentifier() { 394 395 int resid = mResources.getIdentifier(COM_ANDROID_CTS_STUB_IDENTIFIER, null, null); 396 assertEquals(R.configVarying.simple, resid); 397 398 resid = mResources.getIdentifier(CONFIG_VARYING_SIMPLE, null, PACKAGE_NAME); 399 assertEquals(R.configVarying.simple, resid); 400 401 resid = mResources.getIdentifier(SIMPLE, CONFIG_VARYING, PACKAGE_NAME); 402 assertEquals(R.configVarying.simple, resid); 403 } 404 testGetIntArray()405 public void testGetIntArray() { 406 final int NO_EXIST_ID = -1; 407 try { 408 mResources.getIntArray(NO_EXIST_ID); 409 fail("should throw out NotFoundException"); 410 } catch (NotFoundException e) { 411 // expected 412 } 413 // expected value is defined in res/value/arrays.xml 414 final int[] expectedArray1 = new int[] { 415 0, 0, 0 416 }; 417 final int[] expectedArray2 = new int[] { 418 0, 1, 101 419 }; 420 int[]array1 = mResources.getIntArray(R.array.strings); 421 int[]array2 = mResources.getIntArray(R.array.integers); 422 423 checkArrayEqual(expectedArray1, array1); 424 checkArrayEqual(expectedArray2, array2); 425 426 } 427 checkArrayEqual(int[] array1, int[] array2)428 private void checkArrayEqual(int[] array1, int[] array2) { 429 assertNotNull(array2); 430 assertEquals(array1.length, array2.length); 431 for (int i = 0; i < array1.length; i++) { 432 assertEquals(array1[i], array2[i]); 433 } 434 } 435 testGetQuantityText()436 public void testGetQuantityText() { 437 CharSequence cs; 438 final Resources res = resourcesForLanguage("cs"); 439 440 cs = res.getQuantityText(R.plurals.plurals_test, 0); 441 assertEquals("Some Czech dogs", cs.toString()); 442 443 cs = res.getQuantityText(R.plurals.plurals_test, 1); 444 assertEquals("A Czech dog", cs.toString()); 445 446 cs = res.getQuantityText(R.plurals.plurals_test, 2); 447 assertEquals("Few Czech dogs", cs.toString()); 448 449 cs = res.getQuantityText(R.plurals.plurals_test, 5); 450 assertEquals("Some Czech dogs", cs.toString()); 451 452 cs = res.getQuantityText(R.plurals.plurals_test, 500); 453 assertEquals("Some Czech dogs", cs.toString()); 454 455 } 456 resourcesForLanguage(final String lang)457 private Resources resourcesForLanguage(final String lang) { 458 final Configuration config = new Configuration(); 459 config.updateFrom(mResources.getConfiguration()); 460 config.locale = new Locale(lang); 461 return new Resources(mResources.getAssets(), mResources.getDisplayMetrics(), config); 462 } 463 testGetResourceEntryName()464 public void testGetResourceEntryName() { 465 assertEquals(SIMPLE, mResources.getResourceEntryName(R.configVarying.simple)); 466 } 467 testGetResourceName()468 public void testGetResourceName() { 469 final String fullName = mResources.getResourceName(R.configVarying.simple); 470 assertEquals(COM_ANDROID_CTS_STUB_IDENTIFIER, fullName); 471 472 final String packageName = mResources.getResourcePackageName(R.configVarying.simple); 473 assertEquals(PACKAGE_NAME, packageName); 474 475 final String typeName = mResources.getResourceTypeName(R.configVarying.simple); 476 assertEquals(CONFIG_VARYING, typeName); 477 } 478 testGetStringWithIntParam()479 public void testGetStringWithIntParam() { 480 checkString(R.string.formattedStringNone, 481 mResources.getString(R.string.formattedStringNone), 482 "Format[]"); 483 checkString(R.string.formattedStringOne, 484 mResources.getString(R.string.formattedStringOne), 485 "Format[%d]"); 486 checkString(R.string.formattedStringTwo, mResources.getString(R.string.formattedStringTwo), 487 "Format[%3$d,%2$s]"); 488 // Make sure the formatted one works 489 checkString(R.string.formattedStringNone, 490 mResources.getString(R.string.formattedStringNone), 491 "Format[]"); 492 checkString(R.string.formattedStringOne, 493 mResources.getString(R.string.formattedStringOne, 42), 494 "Format[42]"); 495 checkString(R.string.formattedStringTwo, 496 mResources.getString(R.string.formattedStringTwo, "unused", "hi", 43), 497 "Format[43,hi]"); 498 } 499 checkString(final int resid, final String actual, final String expected)500 private static void checkString(final int resid, final String actual, final String expected) { 501 assertEquals("Expecting string value \"" + expected + "\" got \"" 502 + actual + "\" in resources 0x" + Integer.toHexString(resid), 503 expected, actual); 504 } 505 testGetStringArray()506 public void testGetStringArray() { 507 checkStringArray(R.array.strings, new String[] { 508 "zero", "1", "here" 509 }); 510 checkTextArray(R.array.strings, new String[] { 511 "zero", "1", "here" 512 }); 513 checkStringArray(R.array.integers, new String[] { 514 null, null, null 515 }); 516 checkTextArray(R.array.integers, new String[] { 517 null, null, null 518 }); 519 } 520 checkStringArray(final int resid, final String[] expected)521 private void checkStringArray(final int resid, final String[] expected) { 522 final String[] res = mResources.getStringArray(resid); 523 assertEquals(res.length, expected.length); 524 for (int i = 0; i < expected.length; i++) { 525 checkEntry(resid, i, res[i], expected[i]); 526 } 527 } 528 checkEntry(final int resid, final int index, final Object res, final Object expected)529 private void checkEntry(final int resid, final int index, final Object res, 530 final Object expected) { 531 assertEquals("in resource 0x" + Integer.toHexString(resid) 532 + " at index " + index, expected, res); 533 } 534 checkTextArray(final int resid, final String[] expected)535 private void checkTextArray(final int resid, final String[] expected) { 536 final CharSequence[] res = mResources.getTextArray(resid); 537 assertEquals(res.length, expected.length); 538 for (int i = 0; i < expected.length; i++) { 539 checkEntry(resid, i, res[i], expected[i]); 540 } 541 } 542 testGetValueWithID()543 public void testGetValueWithID() { 544 tryBoolean(R.bool.trueRes, true); 545 tryBoolean(R.bool.falseRes, false); 546 547 tryString(R.string.coerceIntegerToString, "100"); 548 tryString(R.string.coerceBooleanToString, "true"); 549 tryString(R.string.coerceColorToString, "#fff"); 550 tryString(R.string.coerceFloatToString, "100.0"); 551 tryString(R.string.coerceDimensionToString, "100px"); 552 tryString(R.string.coerceFractionToString, "100%"); 553 } 554 tryBoolean(final int resid, final boolean expected)555 private void tryBoolean(final int resid, final boolean expected) { 556 final TypedValue v = new TypedValue(); 557 mContext.getResources().getValue(resid, v, true); 558 assertEquals(TypedValue.TYPE_INT_BOOLEAN, v.type); 559 assertEquals("Expecting boolean value " + expected + " got " + v 560 + " from TypedValue: in resource 0x" + Integer.toHexString(resid), 561 expected, v.data != 0); 562 assertEquals("Expecting boolean value " + expected + " got " + v 563 + " from getBoolean(): in resource 0x" + Integer.toHexString(resid), 564 expected, mContext.getResources().getBoolean(resid)); 565 } 566 tryString(final int resid, final String expected)567 private void tryString(final int resid, final String expected) { 568 final TypedValue v = new TypedValue(); 569 mContext.getResources().getValue(resid, v, true); 570 assertEquals(TypedValue.TYPE_STRING, v.type); 571 assertEquals("Expecting string value " + expected + " got " + v 572 + ": in resource 0x" + Integer.toHexString(resid), 573 expected, v.string); 574 } 575 testRawResource()576 public void testRawResource() throws Exception { 577 assertNotNull(mResources.newTheme()); 578 579 InputStream is = mResources.openRawResource(R.raw.text); 580 verifyTextAsset(is); 581 582 is = mResources.openRawResource(R.raw.text, new TypedValue()); 583 verifyTextAsset(is); 584 585 assertNotNull(mResources.openRawResourceFd(R.raw.text)); 586 } 587 verifyTextAsset(final InputStream is)588 static void verifyTextAsset(final InputStream is) throws IOException { 589 final String expectedString = "OneTwoThreeFourFiveSixSevenEightNineTen"; 590 final byte[] buffer = new byte[10]; 591 592 int readCount; 593 int curIndex = 0; 594 while ((readCount = is.read(buffer, 0, buffer.length)) > 0) { 595 for (int i = 0; i < readCount; i++) { 596 assertEquals("At index " + curIndex 597 + " expected " + expectedString.charAt(curIndex) 598 + " but found " + ((char) buffer[i]), 599 buffer[i], expectedString.charAt(curIndex)); 600 curIndex++; 601 } 602 } 603 604 readCount = is.read(buffer, 0, buffer.length); 605 assertEquals("Reading end of buffer: expected readCount=-1 but got " + readCount, 606 -1, readCount); 607 608 readCount = is.read(buffer, buffer.length, 0); 609 assertEquals("Reading end of buffer length 0: expected readCount=0 but got " + readCount, 610 0, readCount); 611 612 is.close(); 613 } 614 } 615