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.stub.R; 20 import com.android.internal.util.XmlUtils; 21 22 import dalvik.annotation.TestLevel; 23 import dalvik.annotation.TestTargetClass; 24 import dalvik.annotation.TestTargetNew; 25 import dalvik.annotation.TestTargets; 26 import dalvik.annotation.ToBeFixed; 27 28 import org.xmlpull.v1.XmlPullParser; 29 import org.xmlpull.v1.XmlPullParserException; 30 31 import android.content.Context; 32 import android.content.res.AssetManager; 33 import android.content.res.ColorStateList; 34 import android.content.res.Configuration; 35 import android.content.res.Resources; 36 import android.content.res.TypedArray; 37 import android.content.res.XmlResourceParser; 38 import android.content.res.Resources.NotFoundException; 39 import android.graphics.drawable.Drawable; 40 import android.os.Bundle; 41 import android.test.AndroidTestCase; 42 import android.util.AttributeSet; 43 import android.util.DisplayMetrics; 44 import android.util.TypedValue; 45 import android.util.Xml; 46 import android.view.Display; 47 import android.view.WindowManager; 48 49 import java.io.IOException; 50 import java.io.InputStream; 51 import java.util.Locale; 52 53 @TestTargetClass(Resources.class) 54 public class ResourcesTest extends AndroidTestCase { 55 private static final String CONFIG_VARYING = "configVarying"; 56 private static final String SIMPLE = "simple"; 57 private static final String CONFIG_VARYING_SIMPLE = "configVarying/simple"; 58 private static final String PACKAGE_NAME = "com.android.cts.stub"; 59 private static final String COM_ANDROID_CTS_STUB_IDENTIFIER = 60 "com.android.cts.stub:configVarying/simple"; 61 private Resources mResources; 62 63 @Override setUp()64 protected void setUp() throws Exception { 65 super.setUp(); 66 67 mResources = getContext().getResources(); 68 } 69 70 @TestTargets({ 71 @TestTargetNew( 72 level = TestLevel.COMPLETE, 73 method = "Resources", 74 args = {AssetManager.class, DisplayMetrics.class, Configuration.class} 75 ), 76 @TestTargetNew( 77 level = TestLevel.COMPLETE, 78 method = "startPreloading", 79 args = {} 80 ), 81 @TestTargetNew( 82 level = TestLevel.COMPLETE, 83 method = "finishPreloading", 84 args = {} 85 ), 86 @TestTargetNew( 87 level = TestLevel.COMPLETE, 88 method = "flushLayoutCache", 89 args = {} 90 ) 91 }) testResources()92 public void testResources() { 93 final AssetManager am = new AssetManager(); 94 final Configuration cfg = new Configuration(); 95 cfg.keyboard = Configuration.KEYBOARDHIDDEN_YES; 96 final DisplayMetrics dm = new DisplayMetrics(); 97 dm.setToDefaults(); 98 99 final Resources r = new Resources(am, dm, cfg); 100 final Configuration c = r.getConfiguration(); 101 assertEquals(Configuration.KEYBOARDHIDDEN_YES, c.keyboard); 102 } 103 104 @TestTargetNew( 105 level = TestLevel.COMPLETE, 106 method = "getString", 107 args = {int.class, java.lang.Object[].class} 108 ) testGetString()109 public void testGetString() { 110 try { 111 mResources.getString(-1, "%s"); 112 fail("Failed at testGetString2"); 113 } catch (NotFoundException e) { 114 //expected 115 } 116 117 final String strGo = mResources.getString(R.string.go, "%1$s%%", 12); 118 assertEquals("Go", strGo); 119 } 120 121 @TestTargets({ 122 @TestTargetNew( 123 level = TestLevel.COMPLETE, 124 method = "obtainAttributes", 125 args = {AttributeSet.class, int[].class} 126 ), 127 @TestTargetNew( 128 level = TestLevel.COMPLETE, 129 method = "getXml", 130 args = {int.class} 131 ) 132 }) testObtainAttributes()133 public void testObtainAttributes() throws XmlPullParserException, IOException { 134 final XmlPullParser parser = mResources.getXml(R.xml.test_color); 135 XmlUtils.beginDocument(parser, "resources"); 136 final AttributeSet set = Xml.asAttributeSet(parser); 137 final TypedArray testTypedArray = mResources.obtainAttributes(set, R.styleable.Style1); 138 assertNotNull(testTypedArray); 139 assertEquals(2, testTypedArray.length()); 140 assertEquals(0, testTypedArray.getColor(0, 0)); 141 assertEquals(128, testTypedArray.getColor(1, 128)); 142 assertEquals(mResources, testTypedArray.getResources()); 143 testTypedArray.recycle(); 144 } 145 146 @TestTargetNew( 147 level = TestLevel.COMPLETE, 148 method = "obtainTypedArray", 149 args = {int.class} 150 ) testObtainTypedArray()151 public void testObtainTypedArray() { 152 try { 153 mResources.obtainTypedArray(-1); 154 fail("Failed at testObtainTypedArray"); 155 } catch (NotFoundException e) { 156 //expected 157 } 158 159 final TypedArray ta = mResources.obtainTypedArray(R.array.string); 160 assertEquals(3, ta.length()); 161 assertEquals("Test String 1", ta.getString(0)); 162 assertEquals("Test String 2", ta.getString(1)); 163 assertEquals("Test String 3", ta.getString(2)); 164 assertEquals(mResources, ta.getResources()); 165 } 166 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)167 private Resources getResources(final Configuration config, final int mcc, final int mnc, 168 final int touchscreen, final int keyboard, final int keysHidden, final int navigation, 169 final int width, final int height) { 170 final AssetManager assmgr = new AssetManager(); 171 assmgr.addAssetPath(mContext.getPackageResourcePath()); 172 final DisplayMetrics metrics = new DisplayMetrics(); 173 final WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); 174 final Display d = wm.getDefaultDisplay(); 175 d.getMetrics(metrics); 176 config.mcc = mcc; 177 config.mnc = mnc; 178 config.touchscreen = touchscreen; 179 config.keyboard = keyboard; 180 config.keyboardHidden = keysHidden; 181 config.navigation = navigation; 182 metrics.widthPixels = width; 183 metrics.heightPixels = height; 184 return new Resources(assmgr, metrics, config); 185 } 186 checkGetText1(final Resources res, final int resId, final String expectedValue)187 private static void checkGetText1(final Resources res, final int resId, 188 final String expectedValue) { 189 final String actual = res.getText(resId).toString(); 190 assertNotNull("Returned wrong configuration-based simple value: expected <nothing>, " 191 + "got '" + actual + "' from resource 0x" + Integer.toHexString(resId), 192 expectedValue); 193 assertEquals("Returned wrong configuration-based simple value: expected " + expectedValue 194 + ", got '" + actual + "' from resource 0x" + Integer.toHexString(resId), 195 expectedValue, actual); 196 } 197 checkGetText2(final Resources res, final int resId, final String expectedValue)198 private static void checkGetText2(final Resources res, final int resId, 199 final String expectedValue) { 200 final String actual = res.getText(resId, null).toString(); 201 assertNotNull("Returned wrong configuration-based simple value: expected <nothing>, " 202 + "got '" + actual + "' from resource 0x" + Integer.toHexString(resId), 203 expectedValue); 204 assertEquals("Returned wrong configuration-based simple value: expected " + expectedValue 205 + ", got '" + actual + "' from resource 0x" + Integer.toHexString(resId), 206 expectedValue, actual); 207 } 208 209 @TestTargetNew( 210 level = TestLevel.COMPLETE, 211 method = "getText", 212 args = {int.class} 213 ) testGetText1()214 public void testGetText1() { 215 try { 216 mResources.getText(0); 217 fail("Failed at testGetText1"); 218 } catch (NotFoundException e) { 219 //expected 220 } 221 222 final Configuration config = new Configuration(); 223 final Resources res = getResources(config, 0, 0, 0, 0, 0, 0, 0, 0); 224 checkGetText1(res, R.configVarying.simple, "simple default"); 225 } 226 227 @TestTargetNew( 228 level = TestLevel.COMPLETE, 229 method = "getText", 230 args = {int.class, java.lang.CharSequence.class} 231 ) testGetText2()232 public void testGetText2() { 233 assertNull(mResources.getText(0, null)); 234 235 final Configuration config = new Configuration(); 236 final Resources res = getResources(config, 0, 0, 0, 0, 0, 0, 0, 0); 237 checkGetText2(res, R.configVarying.simple, "simple default"); 238 } 239 240 @TestTargetNew( 241 level = TestLevel.PARTIAL, 242 method = "getMovie", 243 args = {int.class} 244 ) 245 @ToBeFixed(bug = "1790416", explanation = "Movie object is null in Resources#getMovie(id)") testGetMovie()246 public void testGetMovie() { 247 try { 248 mResources.getMovie(-1); 249 fail("Failed at testGetMovie"); 250 } catch (NotFoundException e) { 251 //expected 252 } 253 } 254 255 @TestTargetNew( 256 level = TestLevel.COMPLETE, 257 method = "getDimension", 258 args = {int.class} 259 ) testGetDimension()260 public void testGetDimension() { 261 try { 262 mResources.getDimension(-1); 263 fail("Failed at testGetDimension"); 264 } catch (NotFoundException e) { 265 //expected 266 } 267 268 // app_icon_size is 48px, as defined in cts/tests/res/values/resources_test.xml 269 final float dim = mResources.getDimension(R.dimen.app_icon_size); 270 assertEquals(48.0f, dim); 271 } 272 273 @TestTargetNew( 274 level = TestLevel.COMPLETE, 275 method = "getDimensionPixelOffset", 276 args = {int.class} 277 ) testGetDimensionPixelOffset()278 public void testGetDimensionPixelOffset() { 279 try { 280 mResources.getDimensionPixelOffset(-1); 281 fail("Failed at testGetDimensionPixelOffset"); 282 } catch (NotFoundException e) { 283 //expected 284 } 285 286 // app_icon_size is 48px, as defined in cts/tests/res/values/resources_test.xml 287 final int dim = mResources.getDimensionPixelOffset(R.dimen.app_icon_size); 288 assertEquals(48, dim); 289 } 290 291 @TestTargetNew( 292 level = TestLevel.COMPLETE, 293 method = "getColorStateList", 294 args = {int.class} 295 ) testGetColorStateList()296 public void testGetColorStateList() { 297 try { 298 mResources.getColorStateList(-1); 299 fail("Failed at testGetColorStateList"); 300 } catch (NotFoundException e) { 301 //expected 302 } 303 304 final ColorStateList colorStateList = mResources.getColorStateList(R.color.color1); 305 final int[] focusedState = {android.R.attr.state_focused}; 306 final int focusColor = colorStateList.getColorForState(focusedState, R.color.failColor); 307 assertEquals(mResources.getColor(R.color.testcolor1), focusColor); 308 } 309 310 @TestTargetNew( 311 level = TestLevel.COMPLETE, 312 method = "getColor", 313 args = {int.class} 314 ) testGetColor()315 public void testGetColor() { 316 try { 317 mResources.getColor(-1); 318 fail("Failed at testGetColor"); 319 } catch (NotFoundException e) { 320 //expected 321 } 322 323 final int color = mResources.getColor(R.color.testcolor1); 324 assertEquals(0xff00ff00, color); 325 } 326 327 @TestTargets({ 328 @TestTargetNew( 329 level = TestLevel.COMPLETE, 330 method = "updateConfiguration", 331 args = {Configuration.class, DisplayMetrics.class} 332 ), 333 @TestTargetNew( 334 level = TestLevel.COMPLETE, 335 method = "getConfiguration", 336 args = {} 337 ) 338 }) testUpdateConfiguration()339 public void testUpdateConfiguration() { 340 final Configuration cfg = mResources.getConfiguration(); 341 assertTrue(cfg.fontScale != 5); 342 343 cfg.fontScale = 5; 344 mResources.updateConfiguration(cfg, null); 345 Configuration cfgNew = mResources.getConfiguration(); 346 assertEquals(5.0f, cfgNew.fontScale, 0.001f); 347 } 348 349 @TestTargets({ 350 @TestTargetNew( 351 level = TestLevel.COMPLETE, 352 method = "getDisplayMetrics", 353 args = {} 354 ), 355 @TestTargetNew( 356 level = TestLevel.COMPLETE, 357 method = "updateSystemConfiguration", 358 args = {Configuration.class, DisplayMetrics.class} 359 ) 360 }) testGetDisplayMetrics()361 public void testGetDisplayMetrics() { 362 final DisplayMetrics dM1 = mResources.getDisplayMetrics(); 363 dM1.widthPixels = 11; 364 dM1.heightPixels = 27; 365 366 Resources.updateSystemConfiguration(new Configuration(), dM1); 367 368 final DisplayMetrics dM2 = mResources.getDisplayMetrics(); 369 assertEquals(11, dM2.widthPixels); 370 assertEquals(27, dM2.heightPixels); 371 } 372 373 @TestTargetNew( 374 level = TestLevel.COMPLETE, 375 method = "getDimensionPixelSize", 376 args = {int.class} 377 ) testGetDimensionPixelSize()378 public void testGetDimensionPixelSize() { 379 try { 380 mResources.getDimensionPixelSize(-1); 381 fail("Failed at testGetDimensionPixelSize"); 382 } catch (NotFoundException e) { 383 //expected 384 } 385 386 // app_icon_size is 48px, as defined in cts/tests/res/values/resources_test.xml 387 final int size = mResources.getDimensionPixelSize(R.dimen.app_icon_size); 388 assertEquals(48, size); 389 } 390 391 @TestTargetNew( 392 level = TestLevel.COMPLETE, 393 method = "getDrawable", 394 args = {int.class} 395 ) testGetDrawable()396 public void testGetDrawable() { 397 try { 398 mResources.getDrawable(-1); 399 fail("Failed at testGetDrawable"); 400 } catch (NotFoundException e) { 401 //expected 402 } 403 404 // testimage is defined in cts/tests/res/drawable/testimage.jpg and measures 212px x 142px 405 final Drawable draw = mResources.getDrawable(R.drawable.testimage); 406 int targetDensity = mResources.getDisplayMetrics().densityDpi; 407 int defaultDensity = DisplayMetrics.DENSITY_DEFAULT; 408 assertNotNull(draw); 409 assertEquals(212 * targetDensity / defaultDensity, draw.getIntrinsicWidth(), 1); 410 assertEquals(142 * targetDensity / defaultDensity, draw.getIntrinsicHeight(), 1); 411 } 412 413 @TestTargetNew( 414 level = TestLevel.COMPLETE, 415 method = "getAnimation", 416 args = {int.class} 417 ) testGetAnimation()418 public void testGetAnimation() throws Exception { 419 try { 420 mResources.getAnimation(-1); 421 fail("Failed at testGetAnimation"); 422 } catch (NotFoundException e) { 423 //expected 424 } 425 426 final XmlResourceParser ani = mResources.getAnimation(R.anim.anim_rotate); 427 assertNotNull(ani); 428 XmlUtils.beginDocument(ani, "rotate"); 429 assertEquals(7, ani.getAttributeCount()); 430 assertEquals("Binary XML file line #18", ani.getPositionDescription()); 431 assertEquals("interpolator", ani.getAttributeName(0)); 432 assertEquals("@17432582", ani.getAttributeValue(0)); 433 } 434 435 @TestTargetNew( 436 level = TestLevel.COMPLETE, 437 method = "getQuantityString", 438 args = {int.class, int.class, Object[].class} 439 ) testGetQuantityString1()440 public void testGetQuantityString1() { 441 try { 442 mResources.getQuantityString(-1, 1, ""); 443 fail("Failed at testGetQuantityString1"); 444 } catch (NotFoundException e) { 445 //expected 446 } 447 448 final String strGo = mResources.getQuantityString(R.plurals.plurals_test, 1, ""); 449 assertEquals("A dog", strGo); 450 } 451 452 @TestTargetNew( 453 level = TestLevel.COMPLETE, 454 method = "getQuantityString", 455 args = {int.class, int.class} 456 ) testGetQuantityString2()457 public void testGetQuantityString2() { 458 try { 459 mResources.getQuantityString(-1, 1); 460 fail("Failed at testGetQuantityString2"); 461 } catch (NotFoundException e) { 462 //expected 463 } 464 465 final String strGo = mResources.getQuantityString(R.plurals.plurals_test, 1); 466 assertEquals("A dog", strGo); 467 } 468 469 @TestTargetNew( 470 level = TestLevel.COMPLETE, 471 method = "getInteger", 472 args = {int.class} 473 ) testGetInteger()474 public void testGetInteger() { 475 try { 476 mResources.getInteger(-1); 477 fail("Failed at testGetInteger"); 478 } catch (NotFoundException e) { 479 //expected 480 } 481 482 final int i = mResources.getInteger(R.integer.resource_test_int); 483 assertEquals(10, i); 484 } 485 486 @TestTargetNew( 487 level = TestLevel.COMPLETE, 488 method = "getValue", 489 args = {String.class, TypedValue.class, boolean.class} 490 ) testGetValue()491 public void testGetValue() { 492 final TypedValue tv = new TypedValue(); 493 494 try { 495 mResources.getValue("null", tv, false); 496 fail("Failed at testGetValue"); 497 } catch (NotFoundException e) { 498 //expected 499 } 500 501 mResources.getValue("com.android.cts.stub:raw/text", tv, false); 502 assertNotNull(tv); 503 assertEquals("res/raw/text.txt", tv.coerceToString()); 504 } 505 506 @TestTargetNew( 507 level = TestLevel.COMPLETE, 508 method = "getAssets", 509 args = {} 510 ) testGetAssets()511 public void testGetAssets() { 512 final AssetManager aM = mResources.getAssets(); 513 assertNotNull(aM); 514 assertTrue(aM.isUpToDate()); 515 } 516 517 @TestTargetNew( 518 level = TestLevel.COMPLETE, 519 method = "getSystem", 520 args = {} 521 ) testGetSystem()522 public void testGetSystem() { 523 assertNotNull(Resources.getSystem()); 524 } 525 526 @TestTargetNew( 527 level = TestLevel.COMPLETE, 528 method = "getLayout", 529 args = {int.class} 530 ) testGetLayout()531 public void testGetLayout() throws Exception { 532 try { 533 mResources.getLayout(-1); 534 fail("Failed at testGetLayout"); 535 } catch (NotFoundException e) { 536 //expected 537 } 538 539 final XmlResourceParser layout = mResources.getLayout(R.layout.abslistview_layout); 540 assertNotNull(layout); 541 XmlUtils.beginDocument(layout, "ViewGroup_Layout"); 542 assertEquals(3, layout.getAttributeCount()); 543 assertEquals("id", layout.getAttributeName(0)); 544 assertEquals("@" + R.id.abslistview_root, layout.getAttributeValue(0)); 545 } 546 547 @TestTargetNew( 548 level = TestLevel.COMPLETE, 549 method = "getBoolean", 550 args = {int.class} 551 ) testGetBoolean()552 public void testGetBoolean() { 553 try { 554 mResources.getBoolean(-1); 555 fail("Failed at testGetBoolean"); 556 } catch (NotFoundException e) { 557 //expected 558 } 559 560 final boolean b = mResources.getBoolean(R.integer.resource_test_int); 561 assertTrue(b); 562 } 563 564 @TestTargetNew( 565 level = TestLevel.COMPLETE, 566 method = "getFraction", 567 args = {int.class, int.class, int.class} 568 ) testgetFraction()569 public void testgetFraction() { 570 assertEquals(1, (int)mResources.getFraction(R.dimen.frac100perc, 1, 1)); 571 assertEquals(100, (int)mResources.getFraction(R.dimen.frac100perc, 100, 1)); 572 } 573 574 @TestTargets({ 575 @TestTargetNew( 576 level = TestLevel.COMPLETE, 577 method = "parseBundleExtras", 578 args = {XmlResourceParser.class, Bundle.class} 579 ), 580 @TestTargetNew( 581 level = TestLevel.COMPLETE, 582 method = "getXml", 583 args = {int.class} 584 ) 585 }) testParseBundleExtras()586 public void testParseBundleExtras() throws XmlPullParserException, IOException { 587 final Bundle b = new Bundle(); 588 XmlResourceParser parser = mResources.getXml(R.xml.extra); 589 XmlUtils.beginDocument(parser, "tag"); 590 591 assertEquals(0, b.size()); 592 mResources.parseBundleExtras(parser, b); 593 assertEquals(1, b.size()); 594 assertEquals("android", b.getString("google")); 595 } 596 597 @TestTargets({ 598 @TestTargetNew( 599 level = TestLevel.COMPLETE, 600 method = "parseBundleExtra", 601 args = {String.class, AttributeSet.class,Bundle.class} 602 ), 603 @TestTargetNew( 604 level = TestLevel.COMPLETE, 605 method = "getXml", 606 args = {int.class} 607 ) 608 }) testParseBundleExtra()609 public void testParseBundleExtra() throws XmlPullParserException, IOException { 610 final Bundle b = new Bundle(); 611 XmlResourceParser parser = mResources.getXml(R.xml.extra); 612 613 XmlUtils.beginDocument(parser, "tag"); 614 assertEquals(0, b.size()); 615 mResources.parseBundleExtra("test", parser, b); 616 assertEquals(1, b.size()); 617 assertEquals("Lee", b.getString("Bruce")); 618 } 619 620 @TestTargetNew( 621 level = TestLevel.COMPLETE, 622 method = "getIdentifier", 623 args = {String.class, String.class, String.class} 624 ) testGetIdentifier()625 public void testGetIdentifier() { 626 627 int resid = mResources.getIdentifier(COM_ANDROID_CTS_STUB_IDENTIFIER, null, null); 628 assertEquals(R.configVarying.simple, resid); 629 630 resid = mResources.getIdentifier(CONFIG_VARYING_SIMPLE, null, PACKAGE_NAME); 631 assertEquals(R.configVarying.simple, resid); 632 633 resid = mResources.getIdentifier(SIMPLE, CONFIG_VARYING, PACKAGE_NAME); 634 assertEquals(R.configVarying.simple, resid); 635 } 636 637 @TestTargetNew( 638 level = TestLevel.PARTIAL, 639 method = "getIntArray", 640 args = {int.class} 641 ) testGetIntArray()642 public void testGetIntArray() { 643 final int NO_EXIST_ID = -1; 644 try { 645 mResources.getIntArray(NO_EXIST_ID); 646 fail("should throw out NotFoundException"); 647 } catch (NotFoundException e) { 648 // expected 649 } 650 // expected value is defined in res/value/arrays.xml 651 final int[] expectedArray1 = new int[] { 652 0, 0, 0 653 }; 654 final int[] expectedArray2 = new int[] { 655 0, 1, 101 656 }; 657 int[]array1 = mResources.getIntArray(R.array.strings); 658 int[]array2 = mResources.getIntArray(R.array.integers); 659 660 checkArrayEqual(expectedArray1, array1); 661 checkArrayEqual(expectedArray2, array2); 662 663 } 664 checkArrayEqual(int[] array1, int[] array2)665 private void checkArrayEqual(int[] array1, int[] array2) { 666 assertNotNull(array2); 667 assertEquals(array1.length, array2.length); 668 for (int i = 0; i < array1.length; i++) { 669 assertEquals(array1[i], array2[i]); 670 } 671 } 672 673 @TestTargetNew( 674 level = TestLevel.PARTIAL, 675 method = "getQuantityText", 676 args = {int.class, int.class} 677 ) testGetQuantityText()678 public void testGetQuantityText() { 679 CharSequence cs; 680 final Resources res = resourcesForLanguage("cs"); 681 682 cs = res.getQuantityText(R.plurals.plurals_test, 0); 683 assertEquals("Some Czech dogs", cs.toString()); 684 685 cs = res.getQuantityText(R.plurals.plurals_test, 1); 686 assertEquals("A Czech dog", cs.toString()); 687 688 cs = res.getQuantityText(R.plurals.plurals_test, 2); 689 assertEquals("Few Czech dogs", cs.toString()); 690 691 cs = res.getQuantityText(R.plurals.plurals_test, 5); 692 assertEquals("Some Czech dogs", cs.toString()); 693 694 cs = res.getQuantityText(R.plurals.plurals_test, 500); 695 assertEquals("Some Czech dogs", cs.toString()); 696 697 } 698 resourcesForLanguage(final String lang)699 private Resources resourcesForLanguage(final String lang) { 700 final Configuration config = new Configuration(); 701 config.updateFrom(mResources.getConfiguration()); 702 config.locale = new Locale(lang); 703 return new Resources(mResources.getAssets(), mResources.getDisplayMetrics(), config); 704 } 705 706 @TestTargetNew( 707 level = TestLevel.PARTIAL, 708 method = "getResourceEntryName", 709 args = {int.class} 710 ) testGetResourceEntryName()711 public void testGetResourceEntryName() { 712 assertEquals(SIMPLE, mResources.getResourceEntryName(R.configVarying.simple)); 713 } 714 715 @TestTargets({ 716 @TestTargetNew( 717 level = TestLevel.PARTIAL, 718 method = "getResourceName", 719 args = {int.class} 720 ), 721 @TestTargetNew( 722 level = TestLevel.COMPLETE, 723 method = "getResourcePackageName", 724 args = {int.class} 725 ), 726 @TestTargetNew( 727 level = TestLevel.COMPLETE, 728 method = "getResourceTypeName", 729 args = {int.class} 730 ) 731 }) testGetResourceName()732 public void testGetResourceName() { 733 final String fullName = mResources.getResourceName(R.configVarying.simple); 734 assertEquals(COM_ANDROID_CTS_STUB_IDENTIFIER, fullName); 735 736 final String packageName = mResources.getResourcePackageName(R.configVarying.simple); 737 assertEquals(PACKAGE_NAME, packageName); 738 739 final String typeName = mResources.getResourceTypeName(R.configVarying.simple); 740 assertEquals(CONFIG_VARYING, typeName); 741 } 742 743 @TestTargetNew( 744 level = TestLevel.PARTIAL, 745 method = "getString", 746 args = {int.class} 747 ) testGetStringWithIntParam()748 public void testGetStringWithIntParam() { 749 checkString(R.string.formattedStringNone, 750 mResources.getString(R.string.formattedStringNone), 751 "Format[]"); 752 checkString(R.string.formattedStringOne, 753 mResources.getString(R.string.formattedStringOne), 754 "Format[%d]"); 755 checkString(R.string.formattedStringTwo, mResources.getString(R.string.formattedStringTwo), 756 "Format[%3$d,%2$s]"); 757 // Make sure the formatted one works 758 checkString(R.string.formattedStringNone, 759 mResources.getString(R.string.formattedStringNone), 760 "Format[]"); 761 checkString(R.string.formattedStringOne, 762 mResources.getString(R.string.formattedStringOne, 42), 763 "Format[42]"); 764 checkString(R.string.formattedStringTwo, 765 mResources.getString(R.string.formattedStringTwo, "unused", "hi", 43), 766 "Format[43,hi]"); 767 } 768 checkString(final int resid, final String actual, final String expected)769 private static void checkString(final int resid, final String actual, final String expected) { 770 assertEquals("Expecting string value \"" + expected + "\" got \"" 771 + actual + "\" in resources 0x" + Integer.toHexString(resid), 772 expected, actual); 773 } 774 775 @TestTargets({ 776 @TestTargetNew( 777 level = TestLevel.PARTIAL, 778 method = "getStringArray", 779 args = {int.class} 780 ), 781 @TestTargetNew( 782 level = TestLevel.COMPLETE, 783 method = "getTextArray", 784 args = {int.class} 785 ) 786 }) testGetStringArray()787 public void testGetStringArray() { 788 checkStringArray(R.array.strings, new String[] { 789 "zero", "1", "here" 790 }); 791 checkTextArray(R.array.strings, new String[] { 792 "zero", "1", "here" 793 }); 794 checkStringArray(R.array.integers, new String[] { 795 null, null, null 796 }); 797 checkTextArray(R.array.integers, new String[] { 798 null, null, null 799 }); 800 } 801 checkStringArray(final int resid, final String[] expected)802 private void checkStringArray(final int resid, final String[] expected) { 803 final String[] res = mResources.getStringArray(resid); 804 assertEquals(res.length, expected.length); 805 for (int i = 0; i < expected.length; i++) { 806 checkEntry(resid, i, res[i], expected[i]); 807 } 808 } 809 checkEntry(final int resid, final int index, final Object res, final Object expected)810 private void checkEntry(final int resid, final int index, final Object res, 811 final Object expected) { 812 assertEquals("in resource 0x" + Integer.toHexString(resid) 813 + " at index " + index, expected, res); 814 } 815 checkTextArray(final int resid, final String[] expected)816 private void checkTextArray(final int resid, final String[] expected) { 817 final CharSequence[] res = mResources.getTextArray(resid); 818 assertEquals(res.length, expected.length); 819 for (int i = 0; i < expected.length; i++) { 820 checkEntry(resid, i, res[i], expected[i]); 821 } 822 } 823 824 @TestTargetNew( 825 level = TestLevel.PARTIAL, 826 method = "getValue", 827 args = {int.class, TypedValue.class, boolean.class} 828 ) testGetValueWithID()829 public void testGetValueWithID() { 830 tryBoolean(R.bool.trueRes, true); 831 tryBoolean(R.bool.falseRes, false); 832 833 tryString(R.string.coerceIntegerToString, "100"); 834 tryString(R.string.coerceBooleanToString, "true"); 835 tryString(R.string.coerceColorToString, "#fff"); 836 tryString(R.string.coerceFloatToString, "100.0"); 837 tryString(R.string.coerceDimensionToString, "100px"); 838 tryString(R.string.coerceFractionToString, "100%"); 839 } 840 tryBoolean(final int resid, final boolean expected)841 private void tryBoolean(final int resid, final boolean expected) { 842 final TypedValue v = new TypedValue(); 843 mContext.getResources().getValue(resid, v, true); 844 assertEquals(TypedValue.TYPE_INT_BOOLEAN, v.type); 845 assertEquals("Expecting boolean value " + expected + " got " + v 846 + " from TypedValue: in resource 0x" + Integer.toHexString(resid), 847 expected, v.data != 0); 848 assertEquals("Expecting boolean value " + expected + " got " + v 849 + " from getBoolean(): in resource 0x" + Integer.toHexString(resid), 850 expected, mContext.getResources().getBoolean(resid)); 851 } 852 tryString(final int resid, final String expected)853 private void tryString(final int resid, final String expected) { 854 final TypedValue v = new TypedValue(); 855 mContext.getResources().getValue(resid, v, true); 856 assertEquals(TypedValue.TYPE_STRING, v.type); 857 assertEquals("Expecting string value " + expected + " got " + v 858 + ": in resource 0x" + Integer.toHexString(resid), 859 expected, v.string); 860 } 861 862 @TestTargets({ 863 @TestTargetNew( 864 level = TestLevel.COMPLETE, 865 method = "newTheme", 866 args = {} 867 ), 868 @TestTargetNew( 869 level = TestLevel.COMPLETE, 870 method = "openRawResource", 871 args = {int.class} 872 ), 873 @TestTargetNew( 874 level = TestLevel.COMPLETE, 875 method = "openRawResource", 876 args = {int.class, TypedValue.class} 877 ), 878 @TestTargetNew( 879 level = TestLevel.COMPLETE, 880 method = "openRawResourceFd", 881 args = {int.class} 882 ) 883 }) testRawResource()884 public void testRawResource() throws Exception { 885 assertNotNull(mResources.newTheme()); 886 887 InputStream is = mResources.openRawResource(R.raw.text); 888 verifyTextAsset(is); 889 890 is = mResources.openRawResource(R.raw.text, new TypedValue()); 891 verifyTextAsset(is); 892 893 assertNotNull(mResources.openRawResourceFd(R.raw.text)); 894 } 895 verifyTextAsset(final InputStream is)896 static void verifyTextAsset(final InputStream is) throws IOException { 897 final String expectedString = "OneTwoThreeFourFiveSixSevenEightNineTen"; 898 final byte[] buffer = new byte[10]; 899 900 int readCount; 901 int curIndex = 0; 902 while ((readCount = is.read(buffer, 0, buffer.length)) > 0) { 903 for (int i = 0; i < readCount; i++) { 904 assertEquals("At index " + curIndex 905 + " expected " + expectedString.charAt(curIndex) 906 + " but found " + ((char) buffer[i]), 907 buffer[i], expectedString.charAt(curIndex)); 908 curIndex++; 909 } 910 } 911 912 readCount = is.read(buffer, 0, buffer.length); 913 assertEquals("Reading end of buffer: expected readCount=-1 but got " + readCount, 914 -1, readCount); 915 916 readCount = is.read(buffer, buffer.length, 0); 917 assertEquals("Reading end of buffer length 0: expected readCount=0 but got " + readCount, 918 0, readCount); 919 920 is.close(); 921 } 922 } 923