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 android.content.cts.R; 20 21 import org.xmlpull.v1.XmlPullParser; 22 import org.xmlpull.v1.XmlPullParserException; 23 24 import android.content.Context; 25 import android.content.cts.util.XmlUtils; 26 import android.content.res.AssetManager; 27 import android.content.res.ColorStateList; 28 import android.content.res.Configuration; 29 import android.content.res.Resources; 30 import android.content.res.Resources.NotFoundException; 31 import android.content.res.TypedArray; 32 import android.content.res.XmlResourceParser; 33 import android.graphics.drawable.Drawable; 34 import android.os.Bundle; 35 import android.os.LocaleList; 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 = "android.content.cts"; 53 private static final String COM_ANDROID_CTS_STUB_IDENTIFIER = 54 "android.content.cts: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 createNewResources()220 public Resources createNewResources() { 221 final DisplayMetrics dm = new DisplayMetrics(); 222 dm.setToDefaults(); 223 final Configuration cfg = new Configuration(); 224 cfg.setToDefaults(); 225 return new Resources(new AssetManager(), dm, cfg); 226 } 227 testUpdateConfiguration()228 public void testUpdateConfiguration() { 229 Resources res = createNewResources(); 230 final Configuration cfg = new Configuration(res.getConfiguration()); 231 assertTrue(cfg.fontScale != 5); 232 233 cfg.fontScale = 5; 234 res.updateConfiguration(cfg, null); 235 assertEquals(5.0f, res.getConfiguration().fontScale, 0.001f); 236 } 237 testUpdateConfiguration_emptyLocaleIsOverridden()238 public void testUpdateConfiguration_emptyLocaleIsOverridden() { 239 Resources res = createNewResources(); 240 res.getConfiguration().setLocales(null); 241 assertTrue(res.getConfiguration().getLocales().isEmpty()); 242 243 final Configuration cfg = new Configuration(); 244 cfg.setToDefaults(); 245 assertTrue(cfg.getLocales().isEmpty()); 246 247 res.updateConfiguration(cfg, null); 248 assertEquals(LocaleList.getDefault(), res.getConfiguration().getLocales()); 249 } 250 testUpdateConfiguration_copyLocales()251 public void testUpdateConfiguration_copyLocales() { 252 Resources res = createNewResources(); 253 final Configuration cfg = new Configuration(res.getConfiguration()); 254 255 cfg.setLocales(LocaleList.forLanguageTags("az-Arab,ru")); 256 257 res.updateConfiguration(cfg, null); 258 259 // Depending on the locales available in the framework resources, the LocaleList may be 260 // re-arranged. Check that any valid permutation is present. 261 final LocaleList locales = res.getConfiguration().getLocales(); 262 assertTrue(LocaleList.forLanguageTags("az-Arab,ru").equals(locales) || 263 LocaleList.forLanguageTags("ru,az-Arab").equals(locales)); 264 } 265 testUpdateConfiguration_emptyAfterUpdate()266 public void testUpdateConfiguration_emptyAfterUpdate() { 267 Resources res = createNewResources(); 268 final Configuration cfg = new Configuration(res.getConfiguration()); 269 cfg.setLocales(LocaleList.forLanguageTags("az-Arab")); 270 271 res.updateConfiguration(cfg, null); 272 assertEquals(LocaleList.forLanguageTags("az-Arab"), res.getConfiguration().getLocales()); 273 274 res.getConfiguration().setLocales(null); 275 cfg.setLocales(null); 276 res.updateConfiguration(cfg, null); 277 assertEquals(LocaleList.getDefault(), res.getConfiguration().getLocales()); 278 } 279 testGetDimensionPixelSize()280 public void testGetDimensionPixelSize() { 281 try { 282 mResources.getDimensionPixelSize(-1); 283 fail("Failed at testGetDimensionPixelSize"); 284 } catch (NotFoundException e) { 285 //expected 286 } 287 288 // app_icon_size is 48px, as defined in cts/tests/res/values/resources_test.xml 289 final int size = mResources.getDimensionPixelSize(R.dimen.app_icon_size); 290 assertEquals(48, size); 291 } 292 testGetDrawable()293 public void testGetDrawable() { 294 try { 295 mResources.getDrawable(-1); 296 fail("Failed at testGetDrawable"); 297 } catch (NotFoundException e) { 298 //expected 299 } 300 301 // testimage is defined in cts/tests/res/drawable/testimage.jpg and measures 212px x 142px 302 final Drawable draw = mResources.getDrawable(R.drawable.testimage); 303 int targetDensity = mResources.getDisplayMetrics().densityDpi; 304 int defaultDensity = DisplayMetrics.DENSITY_DEFAULT; 305 assertNotNull(draw); 306 assertEquals(212 * targetDensity / defaultDensity, draw.getIntrinsicWidth(), 1); 307 assertEquals(142 * targetDensity / defaultDensity, draw.getIntrinsicHeight(), 1); 308 } 309 testGetDrawableForDensity()310 public void testGetDrawableForDensity() { 311 final Drawable ldpi = mResources.getDrawableForDensity( 312 R.drawable.density_test, DisplayMetrics.DENSITY_LOW); 313 assertEquals(300, ldpi.getIntrinsicWidth()); 314 315 final Drawable mdpi = mResources.getDrawableForDensity( 316 R.drawable.density_test, DisplayMetrics.DENSITY_MEDIUM); 317 assertEquals(200, mdpi.getIntrinsicWidth()); 318 319 final Drawable hdpi = mResources.getDrawableForDensity( 320 R.drawable.density_test, DisplayMetrics.DENSITY_HIGH); 321 assertEquals(100, hdpi.getIntrinsicWidth()); 322 } 323 testGetAnimation()324 public void testGetAnimation() throws Exception { 325 try { 326 mResources.getAnimation(-1); 327 fail("Failed at testGetAnimation"); 328 } catch (NotFoundException e) { 329 //expected 330 } 331 332 final XmlResourceParser ani = mResources.getAnimation(R.anim.anim_rotate); 333 assertNotNull(ani); 334 XmlUtils.beginDocument(ani, "rotate"); 335 assertEquals(7, ani.getAttributeCount()); 336 assertEquals("Binary XML file line #18", ani.getPositionDescription()); 337 assertEquals("interpolator", ani.getAttributeName(0)); 338 assertEquals("@17432582", ani.getAttributeValue(0)); 339 } 340 testGetQuantityString1()341 public void testGetQuantityString1() { 342 try { 343 mResources.getQuantityString(-1, 1, ""); 344 fail("Failed at testGetQuantityString1"); 345 } catch (NotFoundException e) { 346 //expected 347 } 348 349 final String strGo = mResources.getQuantityString(R.plurals.plurals_test, 1, ""); 350 assertEquals("A dog", strGo); 351 } 352 testGetQuantityString2()353 public void testGetQuantityString2() { 354 try { 355 mResources.getQuantityString(-1, 1); 356 fail("Failed at testGetQuantityString2"); 357 } catch (NotFoundException e) { 358 //expected 359 } 360 361 final String strGo = mResources.getQuantityString(R.plurals.plurals_test, 1); 362 assertEquals("A dog", strGo); 363 } 364 testGetInteger()365 public void testGetInteger() { 366 try { 367 mResources.getInteger(-1); 368 fail("Failed at testGetInteger"); 369 } catch (NotFoundException e) { 370 //expected 371 } 372 373 final int i = mResources.getInteger(R.integer.resource_test_int); 374 assertEquals(10, i); 375 } 376 testGetValue()377 public void testGetValue() { 378 final TypedValue tv = new TypedValue(); 379 380 try { 381 mResources.getValue("null", tv, false); 382 fail("Failed at testGetValue"); 383 } catch (NotFoundException e) { 384 //expected 385 } 386 387 mResources.getValue("android.content.cts:raw/text", tv, false); 388 assertNotNull(tv); 389 assertEquals("res/raw/text.txt", tv.coerceToString()); 390 } 391 testGetValueForDensity()392 public void testGetValueForDensity() { 393 final TypedValue tv = new TypedValue(); 394 395 mResources.getValueForDensity(R.string.density_string, 396 DisplayMetrics.DENSITY_LOW, tv, false); 397 assertEquals("ldpi", tv.coerceToString()); 398 399 mResources.getValueForDensity(R.string.density_string, 400 DisplayMetrics.DENSITY_MEDIUM, tv, false); 401 assertEquals("mdpi", tv.coerceToString()); 402 403 mResources.getValueForDensity(R.string.density_string, 404 DisplayMetrics.DENSITY_HIGH, tv, false); 405 assertEquals("hdpi", tv.coerceToString()); 406 } 407 testGetAssets()408 public void testGetAssets() { 409 final AssetManager aM = mResources.getAssets(); 410 assertNotNull(aM); 411 assertTrue(aM.isUpToDate()); 412 } 413 testGetSystem()414 public void testGetSystem() { 415 assertNotNull(Resources.getSystem()); 416 } 417 testGetLayout()418 public void testGetLayout() throws Exception { 419 try { 420 mResources.getLayout(-1); 421 fail("Failed at testGetLayout"); 422 } catch (NotFoundException e) { 423 //expected 424 } 425 426 final XmlResourceParser layout = mResources.getLayout(R.layout.abslistview_layout); 427 assertNotNull(layout); 428 XmlUtils.beginDocument(layout, "ViewGroup_Layout"); 429 assertEquals(3, layout.getAttributeCount()); 430 assertEquals("id", layout.getAttributeName(0)); 431 assertEquals("@" + R.id.abslistview_root, layout.getAttributeValue(0)); 432 } 433 testGetBoolean()434 public void testGetBoolean() { 435 try { 436 mResources.getBoolean(-1); 437 fail("Failed at testGetBoolean"); 438 } catch (NotFoundException e) { 439 //expected 440 } 441 442 final boolean b = mResources.getBoolean(R.integer.resource_test_int); 443 assertTrue(b); 444 } 445 testgetFraction()446 public void testgetFraction() { 447 assertEquals(1, (int)mResources.getFraction(R.dimen.frac100perc, 1, 1)); 448 assertEquals(100, (int)mResources.getFraction(R.dimen.frac100perc, 100, 1)); 449 } 450 testParseBundleExtras()451 public void testParseBundleExtras() throws XmlPullParserException, IOException { 452 final Bundle b = new Bundle(); 453 XmlResourceParser parser = mResources.getXml(R.xml.extra); 454 XmlUtils.beginDocument(parser, "tag"); 455 456 assertEquals(0, b.size()); 457 mResources.parseBundleExtras(parser, b); 458 assertEquals(1, b.size()); 459 assertEquals("android", b.getString("google")); 460 } 461 testParseBundleExtra()462 public void testParseBundleExtra() throws XmlPullParserException, IOException { 463 final Bundle b = new Bundle(); 464 XmlResourceParser parser = mResources.getXml(R.xml.extra); 465 466 XmlUtils.beginDocument(parser, "tag"); 467 assertEquals(0, b.size()); 468 mResources.parseBundleExtra("test", parser, b); 469 assertEquals(1, b.size()); 470 assertEquals("Lee", b.getString("Bruce")); 471 } 472 testGetIdentifier()473 public void testGetIdentifier() { 474 475 int resid = mResources.getIdentifier(COM_ANDROID_CTS_STUB_IDENTIFIER, null, null); 476 assertEquals(R.configVarying.simple, resid); 477 478 resid = mResources.getIdentifier(CONFIG_VARYING_SIMPLE, null, PACKAGE_NAME); 479 assertEquals(R.configVarying.simple, resid); 480 481 resid = mResources.getIdentifier(SIMPLE, CONFIG_VARYING, PACKAGE_NAME); 482 assertEquals(R.configVarying.simple, resid); 483 } 484 testGetIntArray()485 public void testGetIntArray() { 486 final int NO_EXIST_ID = -1; 487 try { 488 mResources.getIntArray(NO_EXIST_ID); 489 fail("should throw out NotFoundException"); 490 } catch (NotFoundException e) { 491 // expected 492 } 493 // expected value is defined in res/value/arrays.xml 494 final int[] expectedArray1 = new int[] { 495 0, 0, 0 496 }; 497 final int[] expectedArray2 = new int[] { 498 0, 1, 101 499 }; 500 int[]array1 = mResources.getIntArray(R.array.strings); 501 int[]array2 = mResources.getIntArray(R.array.integers); 502 503 checkArrayEqual(expectedArray1, array1); 504 checkArrayEqual(expectedArray2, array2); 505 506 } 507 checkArrayEqual(int[] array1, int[] array2)508 private void checkArrayEqual(int[] array1, int[] array2) { 509 assertNotNull(array2); 510 assertEquals(array1.length, array2.length); 511 for (int i = 0; i < array1.length; i++) { 512 assertEquals(array1[i], array2[i]); 513 } 514 } 515 testGetQuantityText()516 public void testGetQuantityText() { 517 CharSequence cs; 518 final Resources res = resourcesForLanguage("cs"); 519 520 cs = res.getQuantityText(R.plurals.plurals_test, 0); 521 assertEquals("Some Czech dogs", cs.toString()); 522 523 cs = res.getQuantityText(R.plurals.plurals_test, 1); 524 assertEquals("A Czech dog", cs.toString()); 525 526 cs = res.getQuantityText(R.plurals.plurals_test, 2); 527 assertEquals("Few Czech dogs", cs.toString()); 528 529 cs = res.getQuantityText(R.plurals.plurals_test, 5); 530 assertEquals("Some Czech dogs", cs.toString()); 531 532 cs = res.getQuantityText(R.plurals.plurals_test, 500); 533 assertEquals("Some Czech dogs", cs.toString()); 534 535 } 536 resourcesForLanguage(final String lang)537 private Resources resourcesForLanguage(final String lang) { 538 final Configuration config = new Configuration(); 539 config.updateFrom(mResources.getConfiguration()); 540 config.setLocale(new Locale(lang)); 541 return new Resources(mResources.getAssets(), mResources.getDisplayMetrics(), config); 542 } 543 testGetResourceEntryName()544 public void testGetResourceEntryName() { 545 assertEquals(SIMPLE, mResources.getResourceEntryName(R.configVarying.simple)); 546 } 547 testGetResourceName()548 public void testGetResourceName() { 549 final String fullName = mResources.getResourceName(R.configVarying.simple); 550 assertEquals(COM_ANDROID_CTS_STUB_IDENTIFIER, fullName); 551 552 final String packageName = mResources.getResourcePackageName(R.configVarying.simple); 553 assertEquals(PACKAGE_NAME, packageName); 554 555 final String typeName = mResources.getResourceTypeName(R.configVarying.simple); 556 assertEquals(CONFIG_VARYING, typeName); 557 } 558 testGetStringWithIntParam()559 public void testGetStringWithIntParam() { 560 checkString(R.string.formattedStringNone, 561 mResources.getString(R.string.formattedStringNone), 562 "Format[]"); 563 checkString(R.string.formattedStringOne, 564 mResources.getString(R.string.formattedStringOne), 565 "Format[%d]"); 566 checkString(R.string.formattedStringTwo, mResources.getString(R.string.formattedStringTwo), 567 "Format[%3$d,%2$s]"); 568 // Make sure the formatted one works 569 checkString(R.string.formattedStringNone, 570 mResources.getString(R.string.formattedStringNone), 571 "Format[]"); 572 checkString(R.string.formattedStringOne, 573 mResources.getString(R.string.formattedStringOne, 42), 574 "Format[42]"); 575 checkString(R.string.formattedStringTwo, 576 mResources.getString(R.string.formattedStringTwo, "unused", "hi", 43), 577 "Format[43,hi]"); 578 } 579 checkString(final int resid, final String actual, final String expected)580 private static void checkString(final int resid, final String actual, final String expected) { 581 assertEquals("Expecting string value \"" + expected + "\" got \"" 582 + actual + "\" in resources 0x" + Integer.toHexString(resid), 583 expected, actual); 584 } 585 testGetStringArray()586 public void testGetStringArray() { 587 checkStringArray(R.array.strings, new String[] { 588 "zero", "1", "here" 589 }); 590 checkTextArray(R.array.strings, new String[] { 591 "zero", "1", "here" 592 }); 593 checkStringArray(R.array.integers, new String[] { 594 null, null, null 595 }); 596 checkTextArray(R.array.integers, new String[] { 597 null, null, null 598 }); 599 } 600 checkStringArray(final int resid, final String[] expected)601 private void checkStringArray(final int resid, final String[] expected) { 602 final String[] res = mResources.getStringArray(resid); 603 assertEquals(res.length, expected.length); 604 for (int i = 0; i < expected.length; i++) { 605 checkEntry(resid, i, res[i], expected[i]); 606 } 607 } 608 checkEntry(final int resid, final int index, final Object res, final Object expected)609 private void checkEntry(final int resid, final int index, final Object res, 610 final Object expected) { 611 assertEquals("in resource 0x" + Integer.toHexString(resid) 612 + " at index " + index, expected, res); 613 } 614 checkTextArray(final int resid, final String[] expected)615 private void checkTextArray(final int resid, final String[] expected) { 616 final CharSequence[] res = mResources.getTextArray(resid); 617 assertEquals(res.length, expected.length); 618 for (int i = 0; i < expected.length; i++) { 619 checkEntry(resid, i, res[i], expected[i]); 620 } 621 } 622 testGetValueWithID()623 public void testGetValueWithID() { 624 tryBoolean(R.bool.trueRes, true); 625 tryBoolean(R.bool.falseRes, false); 626 627 tryString(R.string.coerceIntegerToString, "100"); 628 tryString(R.string.coerceBooleanToString, "true"); 629 tryString(R.string.coerceColorToString, "#fff"); 630 tryString(R.string.coerceFloatToString, "100.0"); 631 tryString(R.string.coerceDimensionToString, "100px"); 632 tryString(R.string.coerceFractionToString, "100%"); 633 } 634 tryBoolean(final int resid, final boolean expected)635 private void tryBoolean(final int resid, final boolean expected) { 636 final TypedValue v = new TypedValue(); 637 mContext.getResources().getValue(resid, v, true); 638 assertEquals(TypedValue.TYPE_INT_BOOLEAN, v.type); 639 assertEquals("Expecting boolean value " + expected + " got " + v 640 + " from TypedValue: in resource 0x" + Integer.toHexString(resid), 641 expected, v.data != 0); 642 assertEquals("Expecting boolean value " + expected + " got " + v 643 + " from getBoolean(): in resource 0x" + Integer.toHexString(resid), 644 expected, mContext.getResources().getBoolean(resid)); 645 } 646 tryString(final int resid, final String expected)647 private void tryString(final int resid, final String expected) { 648 final TypedValue v = new TypedValue(); 649 mContext.getResources().getValue(resid, v, true); 650 assertEquals(TypedValue.TYPE_STRING, v.type); 651 assertEquals("Expecting string value " + expected + " got " + v 652 + ": in resource 0x" + Integer.toHexString(resid), 653 expected, v.string); 654 } 655 testRawResource()656 public void testRawResource() throws Exception { 657 assertNotNull(mResources.newTheme()); 658 659 InputStream is = mResources.openRawResource(R.raw.text); 660 verifyTextAsset(is); 661 662 is = mResources.openRawResource(R.raw.text, new TypedValue()); 663 verifyTextAsset(is); 664 665 assertNotNull(mResources.openRawResourceFd(R.raw.text)); 666 } 667 verifyTextAsset(final InputStream is)668 static void verifyTextAsset(final InputStream is) throws IOException { 669 final String expectedString = "OneTwoThreeFourFiveSixSevenEightNineTen"; 670 final byte[] buffer = new byte[10]; 671 672 int readCount; 673 int curIndex = 0; 674 while ((readCount = is.read(buffer, 0, buffer.length)) > 0) { 675 for (int i = 0; i < readCount; i++) { 676 assertEquals("At index " + curIndex 677 + " expected " + expectedString.charAt(curIndex) 678 + " but found " + ((char) buffer[i]), 679 buffer[i], expectedString.charAt(curIndex)); 680 curIndex++; 681 } 682 } 683 684 readCount = is.read(buffer, 0, buffer.length); 685 assertEquals("Reading end of buffer: expected readCount=-1 but got " + readCount, 686 -1, readCount); 687 688 readCount = is.read(buffer, buffer.length, 0); 689 assertEquals("Reading end of buffer length 0: expected readCount=0 but got " + readCount, 690 0, readCount); 691 692 is.close(); 693 } 694 } 695