1 /* 2 * Copyright 2018 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 * https://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 static com.google.common.truth.Truth.assertThat; 20 21 import static junit.framework.TestCase.assertEquals; 22 import static junit.framework.TestCase.assertFalse; 23 import static junit.framework.TestCase.assertNotNull; 24 import static junit.framework.TestCase.assertNotSame; 25 import static junit.framework.TestCase.assertNull; 26 import static junit.framework.TestCase.assertTrue; 27 import static junit.framework.TestCase.fail; 28 29 import static org.junit.Assert.assertThrows; 30 31 import android.content.Context; 32 import android.content.cts.R; 33 import android.content.cts.util.XmlUtils; 34 import android.content.pm.ActivityInfo; 35 import android.content.res.AssetManager; 36 import android.content.res.ColorStateList; 37 import android.content.res.Configuration; 38 import android.content.res.Resources; 39 import android.content.res.Resources.NotFoundException; 40 import android.content.res.TypedArray; 41 import android.content.res.XmlResourceParser; 42 import android.graphics.Movie; 43 import android.graphics.Paint; 44 import android.graphics.Typeface; 45 import android.graphics.drawable.AdaptiveIconDrawable; 46 import android.graphics.drawable.ColorDrawable; 47 import android.graphics.drawable.ColorStateListDrawable; 48 import android.graphics.drawable.Drawable; 49 import android.os.Bundle; 50 import android.os.LocaleList; 51 import android.platform.test.annotations.AppModeSdkSandbox; 52 import android.platform.test.annotations.DisabledOnRavenwood; 53 import android.util.AttributeSet; 54 import android.util.DisplayMetrics; 55 import android.util.Log; 56 import android.util.TypedValue; 57 import android.util.Xml; 58 import android.view.LayoutInflater; 59 import android.view.View; 60 61 import androidx.test.ext.junit.runners.AndroidJUnit4; 62 import androidx.test.platform.app.InstrumentationRegistry; 63 64 import org.junit.Before; 65 import org.junit.Test; 66 import org.junit.runner.RunWith; 67 import org.xmlpull.v1.XmlPullParser; 68 import org.xmlpull.v1.XmlPullParserException; 69 70 import java.io.IOException; 71 import java.io.InputStream; 72 import java.util.Locale; 73 import java.util.stream.IntStream; 74 75 @AppModeSdkSandbox(reason = "Allow test in the SDK sandbox (does not prevent other modes).") 76 @RunWith(AndroidJUnit4.class) 77 public class ResourcesTest { 78 getContext()79 private Context getContext() { 80 return InstrumentationRegistry.getInstrumentation().getTargetContext(); 81 } 82 83 private static final String STRING = "string"; 84 private static final String SIMPLE = "simple"; 85 private static final String STRING_SIMPLE = "string/simple"; 86 private static final String PACKAGE_NAME = "android.content.cts"; 87 private static final String COM_ANDROID_CTS_STUB_IDENTIFIER = 88 "android.content.cts:string/simple"; 89 public static final float FONT_SCALING_TOLERANCE = 0.05f; 90 private Resources mResources; 91 92 @Before setUp()93 public void setUp() throws Exception { 94 mResources = getContext().getResources(); 95 } 96 97 @Test testIdNull()98 public void testIdNull() { 99 assertEquals(0, Resources.ID_NULL); 100 } 101 102 @Test testResources()103 public void testResources() { 104 final AssetManager am = new AssetManager(); 105 final Configuration cfg = new Configuration(); 106 cfg.keyboard = Configuration.KEYBOARDHIDDEN_YES; 107 final DisplayMetrics dm = new DisplayMetrics(); 108 dm.setToDefaults(); 109 110 final Resources r = new Resources(am, dm, cfg); 111 final Configuration c = r.getConfiguration(); 112 assertEquals(Configuration.KEYBOARDHIDDEN_YES, c.keyboard); 113 } 114 115 @Test testGetString()116 public void testGetString() { 117 try { 118 mResources.getString(-1, "%s"); 119 fail("Failed at testGetString2"); 120 } catch (NotFoundException e) { 121 //expected 122 } 123 124 final String strGo = mResources.getString(R.string.go, "%1$s%%", 12); 125 assertEquals("Go", strGo); 126 } 127 128 @Test testObtainAttributes()129 public void testObtainAttributes() throws XmlPullParserException, IOException { 130 final XmlPullParser parser = mResources.getXml(R.xml.test_color); 131 XmlUtils.beginDocument(parser, "resources"); 132 final AttributeSet set = Xml.asAttributeSet(parser); 133 final TypedArray testTypedArray = mResources.obtainAttributes(set, R.styleable.Style1); 134 assertNotNull(testTypedArray); 135 assertEquals(2, testTypedArray.length()); 136 assertEquals(0, testTypedArray.getColor(0, 0)); 137 assertEquals(128, testTypedArray.getColor(1, 128)); 138 assertEquals(mResources, testTypedArray.getResources()); 139 testTypedArray.recycle(); 140 } 141 142 @Test testObtainTypedArray()143 public void testObtainTypedArray() { 144 try { 145 mResources.obtainTypedArray(-1); 146 fail("Failed at testObtainTypedArray"); 147 } catch (NotFoundException e) { 148 //expected 149 } 150 151 final TypedArray ta = mResources.obtainTypedArray(R.array.string); 152 assertEquals(3, ta.length()); 153 assertEquals("Test String 1", ta.getString(0)); 154 assertEquals("Test String 2", ta.getString(1)); 155 assertEquals("Test String 3", ta.getString(2)); 156 assertEquals(mResources, ta.getResources()); 157 } 158 159 @Test 160 @DisabledOnRavenwood(blockedBy = Movie.class) testGetMovie()161 public void testGetMovie() { 162 try { 163 mResources.getMovie(-1); 164 fail("Failed at testGetMovie"); 165 } catch (NotFoundException e) { 166 //expected 167 } 168 } 169 170 @Test testGetDimension()171 public void testGetDimension() { 172 try { 173 mResources.getDimension(-1); 174 fail("Failed at testGetDimension"); 175 } catch (NotFoundException e) { 176 //expected 177 } 178 179 // app_icon_size is 48px, as defined in cts/tests/res/values/resources_test.xml 180 final float dim = mResources.getDimension(R.dimen.app_icon_size); 181 assertEquals(48.0f, dim); 182 } 183 184 @Test testGetDimensionPixelOffset()185 public void testGetDimensionPixelOffset() { 186 try { 187 mResources.getDimensionPixelOffset(-1); 188 fail("Failed at testGetDimensionPixelOffset"); 189 } catch (NotFoundException e) { 190 //expected 191 } 192 193 // app_icon_size is 48px, as defined in cts/tests/res/values/resources_test.xml 194 final int dim = mResources.getDimensionPixelOffset(R.dimen.app_icon_size); 195 assertEquals(48, dim); 196 } 197 198 @Test testGetColorStateList()199 public void testGetColorStateList() { 200 try { 201 mResources.getColorStateList(-1); 202 fail("Failed at testGetColorStateList"); 203 } catch (NotFoundException e) { 204 //expected 205 } 206 207 final ColorStateList colorStateList = mResources.getColorStateList(R.color.color1); 208 final int[] focusedState = {android.R.attr.state_focused}; 209 final int focusColor = colorStateList.getColorForState(focusedState, R.color.failColor); 210 assertEquals(mResources.getColor(R.color.testcolor1), focusColor); 211 } 212 213 @Test testGetColorStateListThrows()214 public void testGetColorStateListThrows() { 215 try { 216 // XML that's not a selector or gradient throws 217 mResources.getColorStateList(R.drawable.density_test); 218 fail("Failed at testGetColorStateList"); 219 } catch (NotFoundException expected) { 220 // expected 221 } 222 } 223 224 @Test testGetColor()225 public void testGetColor() { 226 try { 227 mResources.getColor(-1); 228 fail("Failed at testGetColor"); 229 } catch (NotFoundException e) { 230 //expected 231 } 232 233 final int color = mResources.getColor(R.color.testcolor1); 234 assertEquals(0xff00ff00, color); 235 } 236 createNewResources()237 public Resources createNewResources() { 238 final DisplayMetrics dm = new DisplayMetrics(); 239 dm.setToDefaults(); 240 final Configuration cfg = new Configuration(); 241 cfg.setToDefaults(); 242 return new Resources(new AssetManager(), dm, cfg); 243 } 244 245 @Test testUpdateConfiguration()246 public void testUpdateConfiguration() { 247 Resources res = createNewResources(); 248 final Configuration cfg = new Configuration(res.getConfiguration()); 249 assertTrue(cfg.fontScale != 5); 250 251 cfg.fontScale = 5; 252 res.updateConfiguration(cfg, null); 253 assertEquals(5.0f, res.getConfiguration().fontScale, 0.001f); 254 } 255 256 @Test testUpdateConfiguration_emptyLocaleIsOverridden()257 public void testUpdateConfiguration_emptyLocaleIsOverridden() { 258 Resources res = createNewResources(); 259 res.getConfiguration().setLocales(null); 260 assertTrue(res.getConfiguration().getLocales().isEmpty()); 261 262 final Configuration cfg = new Configuration(); 263 cfg.setToDefaults(); 264 assertTrue(cfg.getLocales().isEmpty()); 265 266 res.updateConfiguration(cfg, null); 267 assertEquals(LocaleList.getDefault(), res.getConfiguration().getLocales()); 268 } 269 270 @Test testUpdateConfiguration_copyLocales()271 public void testUpdateConfiguration_copyLocales() { 272 Resources res = createNewResources(); 273 final Configuration cfg = new Configuration(res.getConfiguration()); 274 275 cfg.setLocales(LocaleList.forLanguageTags("az-Arab,ru")); 276 277 res.updateConfiguration(cfg, null); 278 279 // Depending on the locales available in the framework resources, the LocaleList may be 280 // re-arranged. Check that any valid permutation is present. 281 final LocaleList locales = res.getConfiguration().getLocales(); 282 assertTrue(LocaleList.forLanguageTags("az-Arab,ru").equals(locales) || 283 LocaleList.forLanguageTags("ru,az-Arab").equals(locales)); 284 } 285 286 @Test testUpdateConfiguration_emptyAfterUpdate()287 public void testUpdateConfiguration_emptyAfterUpdate() { 288 Resources res = createNewResources(); 289 final Configuration cfg = new Configuration(res.getConfiguration()); 290 cfg.setLocales(LocaleList.forLanguageTags("az-Arab")); 291 292 res.updateConfiguration(cfg, null); 293 assertEquals(LocaleList.forLanguageTags("az-Arab"), res.getConfiguration().getLocales()); 294 295 res.getConfiguration().setLocales(null); 296 cfg.setLocales(null); 297 res.updateConfiguration(cfg, null); 298 assertEquals(LocaleList.getDefault(), res.getConfiguration().getLocales()); 299 } 300 301 @Test testUpdateConfiguration_fontScaleIs1DoesNotUseAdaptiveFontScalingDeriveDimension()302 public void testUpdateConfiguration_fontScaleIs1DoesNotUseAdaptiveFontScalingDeriveDimension() { 303 Resources res = createNewResources(); 304 final DisplayMetrics metrics1x = res.getDisplayMetrics(); 305 306 assertThat(metrics1x.scaledDensity).isEqualTo(metrics1x.density); 307 // Verify all font sizes are not scaled 308 IntStream.range(5, 20) 309 .asDoubleStream() 310 .forEach( 311 pxDouble -> { 312 float px = (float) pxDouble; 313 // DP and SP should be same at font scale factor 1.0 314 float dpExpected = pxToDp(px, metrics1x); 315 float spActual = pxToSp(px, metrics1x); 316 assertThat(spActual).isWithin(FONT_SCALING_TOLERANCE).of(dpExpected); 317 }); 318 assertThat(pxToSp(30f, metrics1x)) 319 .isWithin(FONT_SCALING_TOLERANCE) 320 .of(pxToDp(30f, metrics1x)); 321 assertThat(pxToSp(100f, metrics1x)) 322 .isWithin(FONT_SCALING_TOLERANCE) 323 .of(pxToDp(100f, metrics1x)); 324 } 325 326 @Test testUpdateConfiguration_fontScaleIs1DoesNotUseAdaptiveFontScalingApplyDimension()327 public void testUpdateConfiguration_fontScaleIs1DoesNotUseAdaptiveFontScalingApplyDimension() { 328 Resources res = createNewResources(); 329 final DisplayMetrics metrics1x = res.getDisplayMetrics(); 330 331 assertThat(metrics1x.scaledDensity).isEqualTo(metrics1x.density); 332 // Verify all font sizes are not scaled 333 IntStream.range(5, 20) 334 .asDoubleStream() 335 .forEach( 336 spDouble -> { 337 float sp = (float) spDouble; 338 // DP and SP should be same at font scale factor 1.0 339 float pxExpected = dpToPx(sp, metrics1x); 340 float pxActual = spToPx(sp, metrics1x); 341 assertThat(pxActual).isWithin(FONT_SCALING_TOLERANCE).of(pxExpected); 342 }); 343 assertThat(spToPx(30f, metrics1x)) 344 .isWithin(FONT_SCALING_TOLERANCE) 345 .of(dpToPx(30f, metrics1x)); 346 assertThat(spToPx(100f, metrics1x)) 347 .isWithin(FONT_SCALING_TOLERANCE) 348 .of(dpToPx(100f, metrics1x)); 349 } 350 351 @Test testTypedValue_convertPixelsToDimensionAlias()352 public void testTypedValue_convertPixelsToDimensionAlias() { 353 Resources res = createNewResources(); 354 final DisplayMetrics metrics = res.getDisplayMetrics(); 355 356 IntStream.range(5, 20) 357 .asDoubleStream() 358 .forEach( 359 spDouble -> { 360 float sp = (float) spDouble; 361 assertThat(TypedValue.convertDimensionToPixels( 362 TypedValue.COMPLEX_UNIT_SP, 363 sp, 364 metrics)) 365 .isWithin(FONT_SCALING_TOLERANCE) 366 .of(TypedValue.applyDimension( 367 TypedValue.COMPLEX_UNIT_SP, 368 sp, 369 metrics)); 370 }); 371 372 IntStream.range(5, 20) 373 .asDoubleStream() 374 .forEach( 375 dpDouble -> { 376 float dp = (float) dpDouble; 377 assertThat(TypedValue.convertDimensionToPixels( 378 TypedValue.COMPLEX_UNIT_DIP, 379 dp, 380 metrics)) 381 .isWithin(FONT_SCALING_TOLERANCE) 382 .of(TypedValue.applyDimension( 383 TypedValue.COMPLEX_UNIT_DIP, 384 dp, 385 metrics)); 386 }); 387 } 388 389 @Test testTypedValue_convertDimensionToPixelsAlias()390 public void testTypedValue_convertDimensionToPixelsAlias() { 391 Resources res = createNewResources(); 392 final DisplayMetrics metrics = res.getDisplayMetrics(); 393 394 IntStream.range(5, 20) 395 .asDoubleStream() 396 .forEach( 397 spDouble -> { 398 float sp = (float) spDouble; 399 assertThat(TypedValue.convertPixelsToDimension( 400 TypedValue.COMPLEX_UNIT_SP, 401 sp, 402 metrics)) 403 .isWithin(FONT_SCALING_TOLERANCE) 404 .of(TypedValue.deriveDimension( 405 TypedValue.COMPLEX_UNIT_SP, 406 sp, 407 metrics)); 408 }); 409 410 IntStream.range(5, 20) 411 .asDoubleStream() 412 .forEach( 413 dpDouble -> { 414 float dp = (float) dpDouble; 415 assertThat(TypedValue.convertPixelsToDimension( 416 TypedValue.COMPLEX_UNIT_DIP, 417 dp, 418 metrics)) 419 .isWithin(FONT_SCALING_TOLERANCE) 420 .of(TypedValue.deriveDimension( 421 TypedValue.COMPLEX_UNIT_DIP, 422 dp, 423 metrics)); 424 }); 425 } 426 427 @Test testGetDimensionPixelSize()428 public void testGetDimensionPixelSize() { 429 try { 430 mResources.getDimensionPixelSize(-1); 431 fail("Failed at testGetDimensionPixelSize"); 432 } catch (NotFoundException e) { 433 //expected 434 } 435 436 // app_icon_size is 48px, as defined in cts/tests/res/values/resources_test.xml 437 final int size = mResources.getDimensionPixelSize(R.dimen.app_icon_size); 438 assertEquals(48, size); 439 assertEquals(1, mResources.getDimensionPixelSize(R.dimen.pos_dimen_149)); 440 assertEquals(2, mResources.getDimensionPixelSize(R.dimen.pos_dimen_151)); 441 assertEquals(-1, mResources.getDimensionPixelSize(R.dimen.neg_dimen_149)); 442 assertEquals(-2, mResources.getDimensionPixelSize(R.dimen.neg_dimen_151)); 443 } 444 445 @Test 446 @DisabledOnRavenwood(blockedBy = Drawable.class) testGetDrawable()447 public void testGetDrawable() { 448 try { 449 mResources.getDrawable(-1); 450 fail("Failed at testGetDrawable"); 451 } catch (NotFoundException e) { 452 //expected 453 } 454 455 // testimage is defined in cts/tests/res/drawable/testimage.jpg and measures 212px x 142px 456 final Drawable draw = mResources.getDrawable(R.drawable.testimage); 457 int targetDensity = mResources.getDisplayMetrics().densityDpi; 458 int defaultDensity = DisplayMetrics.DENSITY_DEFAULT; 459 assertNotNull(draw); 460 assertEquals(212 * targetDensity / defaultDensity, draw.getIntrinsicWidth(), 1); 461 assertEquals(142 * targetDensity / defaultDensity, draw.getIntrinsicHeight(), 1); 462 463 // Some apps rely on the fact that this will return null (rather than throwing). 464 assertNull(mResources.getDrawable(R.drawable.fake_image_will_not_decode)); 465 } 466 467 @Test 468 @DisabledOnRavenwood(blockedBy = Drawable.class) testGetDrawable_fakeFrro()469 public void testGetDrawable_fakeFrro() { 470 var thrown = 471 assertThrows( 472 Resources.NotFoundException.class, 473 () -> mResources.getDrawable(R.string.looks_like_frro, null)); 474 assertThat(Log.getStackTraceString(thrown)).contains("invalid frro path"); 475 } 476 477 @Test 478 @DisabledOnRavenwood(blockedBy = Drawable.class) testGetDrawable_ColorResource()479 public void testGetDrawable_ColorResource() { 480 final Drawable drawable = mResources.getDrawable(R.color.testcolor1, null); 481 assertTrue(drawable instanceof ColorDrawable); 482 assertEquals( 483 mResources.getColor(R.color.testcolor1, null), 484 ((ColorDrawable) drawable).getColor() 485 ); 486 } 487 488 @Test 489 @DisabledOnRavenwood(blockedBy = Drawable.class) testGetDrawable_ColorStateListResource()490 public void testGetDrawable_ColorStateListResource() { 491 final Drawable drawable = mResources.getDrawable(R.color.testcolor, null); 492 assertTrue(drawable instanceof ColorStateListDrawable); 493 494 final ColorStateList colorStateList = mResources.getColorStateList( 495 R.color.testcolor, null); 496 assertEquals( 497 colorStateList.getDefaultColor(), 498 ((ColorStateListDrawable) drawable).getColorStateList().getDefaultColor()); 499 } 500 501 @Test 502 @DisabledOnRavenwood(blockedBy = Drawable.class) testGetDrawable_ColorStateListConfigurations()503 public void testGetDrawable_ColorStateListConfigurations() { 504 final Configuration dayConfiguration = new Configuration(mResources.getConfiguration()); 505 final Configuration nightConfiguration = new Configuration(mResources.getConfiguration()); 506 507 dayConfiguration.uiMode = dayConfiguration.uiMode 508 & (~Configuration.UI_MODE_NIGHT_MASK) 509 | Configuration.UI_MODE_NIGHT_NO; 510 511 nightConfiguration.uiMode = nightConfiguration.uiMode 512 & (~Configuration.UI_MODE_NIGHT_MASK) 513 | Configuration.UI_MODE_NIGHT_YES; 514 515 final ColorStateListDrawable dayDrawable = (ColorStateListDrawable) getContext() 516 .createConfigurationContext(dayConfiguration) 517 .getResources() 518 .getDrawable(R.color.testcolor_daynight, null); 519 520 final ColorStateListDrawable nightDrawable = (ColorStateListDrawable) getContext() 521 .createConfigurationContext(nightConfiguration) 522 .getResources() 523 .getDrawable(R.color.testcolor_daynight, null); 524 525 assertEquals( 526 mResources.getColor(android.R.color.white, null), 527 dayDrawable.getColorStateList().getDefaultColor()); 528 529 assertEquals( 530 mResources.getColor(android.R.color.black, null), 531 nightDrawable.getColorStateList().getDefaultColor()); 532 533 assertEquals(ActivityInfo.CONFIG_UI_MODE, dayDrawable.getChangingConfigurations()); 534 assertEquals(ActivityInfo.CONFIG_UI_MODE, nightDrawable.getChangingConfigurations()); 535 } 536 537 @Test 538 @DisabledOnRavenwood(blockedBy = Drawable.class) testGetDrawable_StackOverflowErrorDrawable()539 public void testGetDrawable_StackOverflowErrorDrawable() { 540 try { 541 mResources.getDrawable(R.drawable.drawable_recursive); 542 fail("Failed at testGetDrawable_StackOverflowErrorDrawable"); 543 } catch (NotFoundException e) { 544 //expected 545 } 546 } 547 548 @Test 549 @DisabledOnRavenwood(blockedBy = Drawable.class) testGetDrawable_StackOverflowErrorDrawable_mipmap()550 public void testGetDrawable_StackOverflowErrorDrawable_mipmap() { 551 try { 552 mResources.getDrawable(R.mipmap.icon_recursive); 553 fail("Failed at testGetDrawable_StackOverflowErrorDrawable_mipmap"); 554 } catch (NotFoundException e) { 555 //expected 556 } 557 } 558 559 @Test 560 @DisabledOnRavenwood(blockedBy = Drawable.class) testGetDrawableForDensity()561 public void testGetDrawableForDensity() { 562 final Drawable ldpi = mResources.getDrawableForDensity( 563 R.drawable.density_test, DisplayMetrics.DENSITY_LOW); 564 assertEquals(300, ldpi.getIntrinsicWidth()); 565 566 final Drawable mdpi = mResources.getDrawableForDensity( 567 R.drawable.density_test, DisplayMetrics.DENSITY_MEDIUM); 568 assertEquals(200, mdpi.getIntrinsicWidth()); 569 570 final Drawable hdpi = mResources.getDrawableForDensity( 571 R.drawable.density_test, DisplayMetrics.DENSITY_HIGH); 572 assertEquals(100, hdpi.getIntrinsicWidth()); 573 } 574 575 @Test 576 @DisabledOnRavenwood(blockedBy = Drawable.class) testGetDrawableForDensityWithZeroDensityIsSameAsGetDrawable()577 public void testGetDrawableForDensityWithZeroDensityIsSameAsGetDrawable() { 578 final Drawable defaultDrawable = mResources.getDrawable(R.drawable.density_test, null); 579 assertNotNull(defaultDrawable); 580 581 final Drawable densityDrawable = mResources.getDrawableForDensity(R.drawable.density_test, 582 0 /*density*/, null); 583 assertNotNull(densityDrawable); 584 585 assertEquals(defaultDrawable.getIntrinsicWidth(), densityDrawable.getIntrinsicWidth()); 586 } 587 extractForegroundFromAdaptiveIconDrawable(int id, int density)588 private Drawable extractForegroundFromAdaptiveIconDrawable(int id, int density) { 589 final Drawable drawable = mResources.getDrawableForDensity(id, density, null); 590 assertTrue(drawable instanceof AdaptiveIconDrawable); 591 return ((AdaptiveIconDrawable) drawable).getForeground(); 592 } 593 594 @Test 595 @DisabledOnRavenwood(blockedBy = Drawable.class) testGetDrawableForDensityWithAdaptiveIconDrawable()596 public void testGetDrawableForDensityWithAdaptiveIconDrawable() { 597 final Drawable ldpi = extractForegroundFromAdaptiveIconDrawable(R.drawable.adaptive_icon, 598 DisplayMetrics.DENSITY_LOW); 599 assertNotNull(ldpi); 600 assertEquals(300, ldpi.getIntrinsicWidth()); 601 602 final Drawable mdpi = extractForegroundFromAdaptiveIconDrawable(R.drawable.adaptive_icon, 603 DisplayMetrics.DENSITY_MEDIUM); 604 assertNotNull(mdpi); 605 assertEquals(200, mdpi.getIntrinsicWidth()); 606 607 final Drawable hdpi = extractForegroundFromAdaptiveIconDrawable(R.drawable.adaptive_icon, 608 DisplayMetrics.DENSITY_HIGH); 609 assertNotNull(hdpi); 610 assertEquals(100, hdpi.getIntrinsicWidth()); 611 } 612 613 @Test testGetAnimation()614 public void testGetAnimation() throws Exception { 615 try { 616 mResources.getAnimation(-1); 617 fail("Failed at testGetAnimation"); 618 } catch (NotFoundException e) { 619 //expected 620 } 621 622 final XmlResourceParser ani = mResources.getAnimation(R.anim.anim_rotate); 623 assertNotNull(ani); 624 XmlUtils.beginDocument(ani, "rotate"); 625 assertEquals(7, ani.getAttributeCount()); 626 assertEquals("Binary XML file line #18", ani.getPositionDescription()); 627 assertEquals("interpolator", ani.getAttributeName(0)); 628 assertEquals("@17432582", ani.getAttributeValue(0)); 629 } 630 631 @Test testGetQuantityString1()632 public void testGetQuantityString1() { 633 // Need to do this due to b/339653024. 634 final Resources res = resourcesForLanguage("en"); 635 try { 636 res.getQuantityString(-1, 1, ""); 637 fail("Failed at testGetQuantityString1"); 638 } catch (NotFoundException e) { 639 //expected 640 } 641 642 final String strGo = res.getQuantityString(R.plurals.plurals_test, 1, ""); 643 assertEquals("A dog", strGo); 644 } 645 646 @Test testGetQuantityString2()647 public void testGetQuantityString2() { 648 // Need to do this due to b/339653024. 649 final Resources res = resourcesForLanguage("en"); 650 try { 651 res.getQuantityString(-1, 1); 652 fail("Failed at testGetQuantityString2"); 653 } catch (NotFoundException e) { 654 //expected 655 } 656 657 final String strGo = res.getQuantityString(R.plurals.plurals_test, 1); 658 assertEquals("A dog", strGo); 659 } 660 661 @Test testGetInteger()662 public void testGetInteger() { 663 try { 664 mResources.getInteger(-1); 665 fail("Failed at testGetInteger"); 666 } catch (NotFoundException e) { 667 //expected 668 } 669 670 final int i = mResources.getInteger(R.integer.resource_test_int); 671 assertEquals(10, i); 672 } 673 674 @Test testGetValue()675 public void testGetValue() { 676 final TypedValue tv = new TypedValue(); 677 678 try { 679 mResources.getValue("null", tv, false); 680 fail("Failed at testGetValue"); 681 } catch (NotFoundException e) { 682 //expected 683 } 684 685 mResources.getValue("android.content.cts:raw/text", tv, false); 686 assertNotNull(tv); 687 assertEquals("res/raw/text.txt", tv.coerceToString()); 688 } 689 690 @Test testGetValueForDensity()691 public void testGetValueForDensity() { 692 final TypedValue tv = new TypedValue(); 693 694 mResources.getValueForDensity(R.string.density_string, 695 DisplayMetrics.DENSITY_LOW, tv, false); 696 assertEquals("ldpi", tv.coerceToString()); 697 698 mResources.getValueForDensity(R.string.density_string, 699 DisplayMetrics.DENSITY_MEDIUM, tv, false); 700 assertEquals("mdpi", tv.coerceToString()); 701 702 mResources.getValueForDensity(R.string.density_string, 703 DisplayMetrics.DENSITY_HIGH, tv, false); 704 assertEquals("hdpi", tv.coerceToString()); 705 } 706 707 @Test testGetValueForDensityWithZeroDensityIsSameAsGetValue()708 public void testGetValueForDensityWithZeroDensityIsSameAsGetValue() { 709 final TypedValue defaultTv = new TypedValue(); 710 mResources.getValue(R.string.density_string, defaultTv, false); 711 712 final TypedValue densityTv = new TypedValue(); 713 mResources.getValueForDensity(R.string.density_string, 0 /*density*/, densityTv, false); 714 715 assertEquals(defaultTv.assetCookie, densityTv.assetCookie); 716 assertEquals(defaultTv.data, densityTv.data); 717 assertEquals(defaultTv.type, densityTv.type); 718 assertEquals(defaultTv.string, densityTv.string); 719 } 720 721 @Test testGetAssets()722 public void testGetAssets() { 723 final AssetManager aM = mResources.getAssets(); 724 assertNotNull(aM); 725 assertTrue(aM.isUpToDate()); 726 } 727 728 @Test testGetSystem()729 public void testGetSystem() { 730 assertNotNull(Resources.getSystem()); 731 } 732 733 @Test testGetLayout()734 public void testGetLayout() throws Exception { 735 try { 736 mResources.getLayout(-1); 737 fail("Failed at testGetLayout"); 738 } catch (NotFoundException e) { 739 //expected 740 } 741 742 final XmlResourceParser layout = mResources.getLayout(R.layout.abslistview_layout); 743 assertNotNull(layout); 744 XmlUtils.beginDocument(layout, "ViewGroup_Layout"); 745 assertEquals(3, layout.getAttributeCount()); 746 assertEquals("id", layout.getAttributeName(0)); 747 assertEquals("@" + R.id.abslistview_root, layout.getAttributeValue(0)); 748 } 749 750 @Test testGetBoolean()751 public void testGetBoolean() { 752 try { 753 mResources.getBoolean(-1); 754 fail("Failed at testGetBoolean"); 755 } catch (NotFoundException e) { 756 //expected 757 } 758 759 final boolean b = mResources.getBoolean(R.integer.resource_test_int); 760 assertTrue(b); 761 } 762 763 @Test testgetFraction()764 public void testgetFraction() { 765 assertEquals(1, (int)mResources.getFraction(R.dimen.frac100perc, 1, 1)); 766 assertEquals(100, (int)mResources.getFraction(R.dimen.frac100perc, 100, 1)); 767 } 768 769 @Test testParseBundleExtras()770 public void testParseBundleExtras() throws XmlPullParserException, IOException { 771 final Bundle b = new Bundle(); 772 XmlResourceParser parser = mResources.getXml(R.xml.extra); 773 XmlUtils.beginDocument(parser, "tag"); 774 775 assertEquals(0, b.size()); 776 mResources.parseBundleExtras(parser, b); 777 assertEquals(1, b.size()); 778 assertEquals("android", b.getString("google")); 779 } 780 781 @Test testParseBundleExtra()782 public void testParseBundleExtra() throws XmlPullParserException, IOException { 783 final Bundle b = new Bundle(); 784 XmlResourceParser parser = mResources.getXml(R.xml.extra); 785 786 XmlUtils.beginDocument(parser, "tag"); 787 assertEquals(0, b.size()); 788 mResources.parseBundleExtra("test", parser, b); 789 assertEquals(1, b.size()); 790 assertEquals("Lee", b.getString("Bruce")); 791 } 792 793 @Test testGetIdentifier()794 public void testGetIdentifier() { 795 796 int resid = mResources.getIdentifier(COM_ANDROID_CTS_STUB_IDENTIFIER, null, null); 797 assertEquals(R.string.simple, resid); 798 799 resid = mResources.getIdentifier(STRING_SIMPLE, null, PACKAGE_NAME); 800 assertEquals(R.string.simple, resid); 801 802 resid = mResources.getIdentifier(SIMPLE, STRING, PACKAGE_NAME); 803 assertEquals(R.string.simple, resid); 804 } 805 806 @Test testGetIntArray()807 public void testGetIntArray() { 808 final int NO_EXIST_ID = -1; 809 try { 810 mResources.getIntArray(NO_EXIST_ID); 811 fail("should throw out NotFoundException"); 812 } catch (NotFoundException e) { 813 // expected 814 } 815 // expected value is defined in res/value/arrays.xml 816 final int[] expectedArray1 = new int[] { 817 0, 0, 0 818 }; 819 final int[] expectedArray2 = new int[] { 820 0, 1, 101 821 }; 822 int[]array1 = mResources.getIntArray(R.array.strings); 823 int[]array2 = mResources.getIntArray(R.array.integers); 824 825 checkArrayEqual(expectedArray1, array1); 826 checkArrayEqual(expectedArray2, array2); 827 828 } 829 checkArrayEqual(int[] array1, int[] array2)830 private void checkArrayEqual(int[] array1, int[] array2) { 831 assertNotNull(array2); 832 assertEquals(array1.length, array2.length); 833 for (int i = 0; i < array1.length; i++) { 834 assertEquals(array1[i], array2[i]); 835 } 836 } 837 838 @Test testGetQuantityText()839 public void testGetQuantityText() { 840 CharSequence cs; 841 final Resources res = resourcesForLanguage("cs"); 842 843 cs = res.getQuantityText(R.plurals.plurals_test, 0); 844 assertEquals("Some Czech dogs", cs.toString()); 845 846 cs = res.getQuantityText(R.plurals.plurals_test, 1); 847 assertEquals("A Czech dog", cs.toString()); 848 849 cs = res.getQuantityText(R.plurals.plurals_test, 2); 850 assertEquals("Few Czech dogs", cs.toString()); 851 852 cs = res.getQuantityText(R.plurals.plurals_test, 5); 853 assertEquals("Some Czech dogs", cs.toString()); 854 855 cs = res.getQuantityText(R.plurals.plurals_test, 500); 856 assertEquals("Some Czech dogs", cs.toString()); 857 858 } 859 860 @Test 861 @DisabledOnRavenwood(blockedBy = ColorDrawable.class) testChangingConfiguration()862 public void testChangingConfiguration() { 863 ColorDrawable dr1 = (ColorDrawable) mResources.getDrawable(R.color.varies_uimode); 864 assertEquals(ActivityInfo.CONFIG_UI_MODE, dr1.getChangingConfigurations()); 865 866 // Test again with a drawable obtained from the cache. 867 ColorDrawable dr2 = (ColorDrawable) mResources.getDrawable(R.color.varies_uimode); 868 assertEquals(ActivityInfo.CONFIG_UI_MODE, dr2.getChangingConfigurations()); 869 } 870 resourcesForLanguage(final String lang)871 private Resources resourcesForLanguage(final String lang) { 872 final Configuration config = new Configuration(); 873 config.updateFrom(mResources.getConfiguration()); 874 config.setLocale(new Locale(lang)); 875 return new Resources(mResources.getAssets(), mResources.getDisplayMetrics(), config); 876 } 877 878 @Test testGetResourceEntryName()879 public void testGetResourceEntryName() { 880 assertEquals(SIMPLE, mResources.getResourceEntryName(R.string.simple)); 881 } 882 883 @Test testGetResourceName()884 public void testGetResourceName() { 885 final String fullName = mResources.getResourceName(R.string.simple); 886 assertEquals(COM_ANDROID_CTS_STUB_IDENTIFIER, fullName); 887 888 final String packageName = mResources.getResourcePackageName(R.string.simple); 889 assertEquals(PACKAGE_NAME, packageName); 890 891 final String typeName = mResources.getResourceTypeName(R.string.simple); 892 assertEquals(STRING, typeName); 893 } 894 895 @Test testGetStringWithIntParam()896 public void testGetStringWithIntParam() { 897 checkString(R.string.formattedStringNone, 898 mResources.getString(R.string.formattedStringNone), 899 "Format[]"); 900 checkString(R.string.formattedStringOne, 901 mResources.getString(R.string.formattedStringOne), 902 "Format[%d]"); 903 checkString(R.string.formattedStringTwo, mResources.getString(R.string.formattedStringTwo), 904 "Format[%3$d,%2$s]"); 905 // Make sure the formatted one works 906 checkString(R.string.formattedStringNone, 907 mResources.getString(R.string.formattedStringNone), 908 "Format[]"); 909 checkString(R.string.formattedStringOne, 910 mResources.getString(R.string.formattedStringOne, 42), 911 "Format[42]"); 912 checkString(R.string.formattedStringTwo, 913 mResources.getString(R.string.formattedStringTwo, "unused", "hi", 43), 914 "Format[43,hi]"); 915 } 916 checkString(final int resid, final String actual, final String expected)917 private static void checkString(final int resid, final String actual, final String expected) { 918 assertEquals("Expecting string value \"" + expected + "\" got \"" 919 + actual + "\" in resources 0x" + Integer.toHexString(resid), 920 expected, actual); 921 } 922 923 @Test testGetStringArray()924 public void testGetStringArray() { 925 checkStringArray(R.array.strings, new String[] { 926 "zero", "1", "here" 927 }); 928 checkTextArray(R.array.strings, new String[] { 929 "zero", "1", "here" 930 }); 931 checkStringArray(R.array.integers, new String[] { 932 null, null, null 933 }); 934 checkTextArray(R.array.integers, new String[] { 935 null, null, null 936 }); 937 } 938 checkStringArray(final int resid, final String[] expected)939 private void checkStringArray(final int resid, final String[] expected) { 940 final String[] res = mResources.getStringArray(resid); 941 assertEquals(res.length, expected.length); 942 for (int i = 0; i < expected.length; i++) { 943 checkEntry(resid, i, res[i], expected[i]); 944 } 945 } 946 checkEntry(final int resid, final int index, final Object res, final Object expected)947 private void checkEntry(final int resid, final int index, final Object res, 948 final Object expected) { 949 assertEquals("in resource 0x" + Integer.toHexString(resid) 950 + " at index " + index, expected, res); 951 } 952 checkTextArray(final int resid, final String[] expected)953 private void checkTextArray(final int resid, final String[] expected) { 954 final CharSequence[] res = mResources.getTextArray(resid); 955 assertEquals(res.length, expected.length); 956 for (int i = 0; i < expected.length; i++) { 957 checkEntry(resid, i, res[i], expected[i]); 958 } 959 } 960 961 @Test testGetValueWithID()962 public void testGetValueWithID() { 963 tryBoolean(R.bool.trueRes, true); 964 tryBoolean(R.bool.falseRes, false); 965 966 tryString(R.string.coerceIntegerToString, "100"); 967 tryString(R.string.coerceBooleanToString, "true"); 968 tryString(R.string.coerceColorToString, "#fff"); 969 tryString(R.string.coerceFloatToString, "100.0"); 970 tryString(R.string.coerceDimensionToString, "100px"); 971 tryString(R.string.coerceFractionToString, "100%"); 972 } 973 tryBoolean(final int resid, final boolean expected)974 private void tryBoolean(final int resid, final boolean expected) { 975 final TypedValue v = new TypedValue(); 976 getContext().getResources().getValue(resid, v, true); 977 assertEquals(TypedValue.TYPE_INT_BOOLEAN, v.type); 978 assertEquals("Expecting boolean value " + expected + " got " + v 979 + " from TypedValue: in resource 0x" + Integer.toHexString(resid), 980 expected, v.data != 0); 981 assertEquals("Expecting boolean value " + expected + " got " + v 982 + " from getBoolean(): in resource 0x" + Integer.toHexString(resid), 983 expected, getContext().getResources().getBoolean(resid)); 984 } 985 tryString(final int resid, final String expected)986 private void tryString(final int resid, final String expected) { 987 final TypedValue v = new TypedValue(); 988 getContext().getResources().getValue(resid, v, true); 989 assertEquals(TypedValue.TYPE_STRING, v.type); 990 assertEquals("Expecting string value " + expected + " got " + v 991 + ": in resource 0x" + Integer.toHexString(resid), 992 expected, v.string); 993 } 994 995 @Test testRawResource()996 public void testRawResource() throws Exception { 997 assertNotNull(mResources.newTheme()); 998 999 InputStream is = mResources.openRawResource(R.raw.text); 1000 verifyTextAsset(is); 1001 1002 is = mResources.openRawResource(R.raw.text, new TypedValue()); 1003 verifyTextAsset(is); 1004 1005 assertNotNull(mResources.openRawResourceFd(R.raw.text)); 1006 } 1007 verifyTextAsset(final InputStream is)1008 static void verifyTextAsset(final InputStream is) throws IOException { 1009 final String expectedString = "OneTwoThreeFourFiveSixSevenEightNineTen"; 1010 final byte[] buffer = new byte[10]; 1011 1012 int readCount; 1013 int curIndex = 0; 1014 while ((readCount = is.read(buffer, 0, buffer.length)) > 0) { 1015 for (int i = 0; i < readCount; i++) { 1016 assertEquals("At index " + curIndex 1017 + " expected " + expectedString.charAt(curIndex) 1018 + " but found " + ((char) buffer[i]), 1019 buffer[i], expectedString.charAt(curIndex)); 1020 curIndex++; 1021 } 1022 } 1023 1024 readCount = is.read(buffer, 0, buffer.length); 1025 assertEquals("Reading end of buffer: expected readCount=-1 but got " + readCount, 1026 -1, readCount); 1027 1028 readCount = is.read(buffer, buffer.length, 0); 1029 assertEquals("Reading end of buffer length 0: expected readCount=0 but got " + readCount, 1030 0, readCount); 1031 1032 is.close(); 1033 } 1034 1035 @Test testGetFont_invalidResourceId()1036 public void testGetFont_invalidResourceId() { 1037 try { 1038 mResources.getFont(-1); 1039 fail("Font resource -1 should not be found."); 1040 } catch (NotFoundException e) { 1041 //expected 1042 } 1043 } 1044 1045 @Test testGetFont_fontFile()1046 public void testGetFont_fontFile() { 1047 Typeface font = mResources.getFont(R.font.sample_regular_font); 1048 1049 assertNotNull(font); 1050 assertNotSame(Typeface.DEFAULT, font); 1051 } 1052 1053 @Test testGetFont_xmlFile()1054 public void testGetFont_xmlFile() { 1055 Typeface font = mResources.getFont(R.font.samplexmlfont); 1056 1057 assertNotNull(font); 1058 assertNotSame(Typeface.DEFAULT, font); 1059 } 1060 getLargerTypeface(String text, Typeface typeface1, Typeface typeface2)1061 private Typeface getLargerTypeface(String text, Typeface typeface1, Typeface typeface2) { 1062 Paint p1 = new Paint(); 1063 p1.setTypeface(typeface1); 1064 float width1 = p1.measureText(text); 1065 Paint p2 = new Paint(); 1066 p2.setTypeface(typeface2); 1067 float width2 = p2.measureText(text); 1068 1069 if (width1 > width2) { 1070 return typeface1; 1071 } else if (width1 < width2) { 1072 return typeface2; 1073 } else { 1074 fail("The widths of the text should not be the same"); 1075 return null; 1076 } 1077 } 1078 1079 @Test testGetFont_xmlFileWithTtc()1080 public void testGetFont_xmlFileWithTtc() { 1081 // Here we test that building typefaces by indexing in font collections works correctly. 1082 // We want to ensure that the built typefaces correspond to the fonts with the right index. 1083 // sample_font_collection.ttc contains two fonts (with indices 0 and 1). The first one has 1084 // glyph "a" of 3em width, and all the other glyphs 1em. The second one has glyph "b" of 1085 // 3em width, and all the other glyphs 1em. Hence, we can compare the width of these 1086 // glyphs to assert that ttc indexing works. 1087 Typeface normalFont = mResources.getFont(R.font.sample_ttc_family); 1088 assertNotNull(normalFont); 1089 Typeface italicFont = Typeface.create(normalFont, Typeface.ITALIC); 1090 assertNotNull(italicFont); 1091 1092 assertEquals(getLargerTypeface("a", normalFont, italicFont), normalFont); 1093 assertEquals(getLargerTypeface("b", normalFont, italicFont), italicFont); 1094 } 1095 1096 @Test testGetFont_xmlFileWithVariationSettings()1097 public void testGetFont_xmlFileWithVariationSettings() { 1098 // Here we test that specifying variation settings for fonts in XMLs works. 1099 // We build typefaces from two families containing one font each, using the same font 1100 // resource, but having different values for the 'wdth' tag. Then we measure the painted 1101 // text to ensure that the tag affects the text width. The font resource used supports 1102 // the 'wdth' axis for the dash (-) character. 1103 Typeface typeface1 = mResources.getFont(R.font.sample_variation_settings_family1); 1104 assertNotNull(typeface1); 1105 Typeface typeface2 = mResources.getFont(R.font.sample_variation_settings_family2); 1106 assertNotNull(typeface2); 1107 1108 assertNotSame(typeface1, typeface2); 1109 assertEquals(getLargerTypeface("-", typeface1, typeface2), typeface2); 1110 } 1111 1112 @Test testGetFont_invalidXmlFile()1113 public void testGetFont_invalidXmlFile() { 1114 try { 1115 assertNull(mResources.getFont(R.font.invalid_xmlfamily)); 1116 } catch (NotFoundException e) { 1117 // pass 1118 } 1119 1120 try { 1121 assertNull(mResources.getFont(R.font.invalid_xmlempty)); 1122 } catch (NotFoundException e) { 1123 // pass 1124 } 1125 } 1126 1127 @Test testGetFont_invalidFontFiles()1128 public void testGetFont_invalidFontFiles() { 1129 try { 1130 mResources.getFont(R.font.invalid_xmlfont); 1131 fail(); 1132 } catch (RuntimeException e) { 1133 // pass 1134 } 1135 1136 try { 1137 mResources.getFont(R.font.invalid_font); 1138 fail(); 1139 } catch (RuntimeException e) { 1140 // pass 1141 } 1142 1143 try { 1144 mResources.getFont(R.font.invalid_xmlfont_contains_invalid_font_file); 1145 fail(); 1146 } catch (RuntimeException e) { 1147 // pass 1148 } 1149 1150 try { 1151 mResources.getFont(R.font.invalid_xmlfont_nosource); 1152 fail(); 1153 } catch (RuntimeException e) { 1154 // pass 1155 } 1156 1157 } 1158 1159 @Test testGetFont_brokenFontFiles()1160 public void testGetFont_brokenFontFiles() { 1161 try { 1162 mResources.getFont(R.font.brokenfont); 1163 fail(); 1164 } catch (RuntimeException e) { 1165 // pass 1166 } 1167 1168 try { 1169 mResources.getFont(R.font.broken_xmlfont); 1170 fail(); 1171 } catch (RuntimeException e) { 1172 // pass 1173 } 1174 } 1175 1176 @Test testGetFont_fontFileIsCached()1177 public void testGetFont_fontFileIsCached() { 1178 Typeface font = mResources.getFont(R.font.sample_regular_font); 1179 Typeface font2 = mResources.getFont(R.font.sample_regular_font); 1180 1181 assertEquals(font, font2); 1182 } 1183 1184 @Test testGetFont_xmlFileIsCached()1185 public void testGetFont_xmlFileIsCached() { 1186 Typeface font = mResources.getFont(R.font.samplexmlfont); 1187 Typeface font2 = mResources.getFont(R.font.samplexmlfont); 1188 1189 assertEquals(font, font2); 1190 } 1191 1192 @Test testGetFont_resolveByFontTable()1193 public void testGetFont_resolveByFontTable() { 1194 assertEquals(Typeface.NORMAL, mResources.getFont(R.font.sample_regular_font).getStyle()); 1195 assertEquals(Typeface.BOLD, mResources.getFont(R.font.sample_bold_font).getStyle()); 1196 assertEquals(Typeface.ITALIC, mResources.getFont(R.font.sample_italic_font).getStyle()); 1197 assertEquals(Typeface.BOLD_ITALIC, 1198 mResources.getFont(R.font.sample_bolditalic_font).getStyle()); 1199 1200 assertEquals(Typeface.NORMAL, mResources.getFont(R.font.sample_regular_family).getStyle()); 1201 assertEquals(Typeface.BOLD, mResources.getFont(R.font.sample_bold_family).getStyle()); 1202 assertEquals(Typeface.ITALIC, mResources.getFont(R.font.sample_italic_family).getStyle()); 1203 assertEquals(Typeface.BOLD_ITALIC, 1204 mResources.getFont(R.font.sample_bolditalic_family).getStyle()); 1205 } 1206 1207 @Test 1208 @DisabledOnRavenwood(blockedBy = Drawable.class) testComplexColorDrawableAttributeInflation()1209 public void testComplexColorDrawableAttributeInflation() { 1210 final LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService( 1211 Context.LAYOUT_INFLATER_SERVICE); 1212 1213 final View view = layoutInflater.inflate( 1214 R.layout.complex_color_drawable_attr_layout, null); 1215 assertTrue(view.getBackground() instanceof ColorStateListDrawable); 1216 } 1217 1218 @Test testGetAttributeSetSourceResId()1219 public void testGetAttributeSetSourceResId() { 1220 assertEquals(Resources.ID_NULL, Resources.getAttributeSetSourceResId(null)); 1221 1222 XmlPullParser test_color_parser = mResources.getXml(R.xml.test_color); 1223 AttributeSet test_color_set = Xml.asAttributeSet(test_color_parser); 1224 assertEquals(R.xml.test_color, Resources.getAttributeSetSourceResId(test_color_set)); 1225 1226 XmlPullParser colors_parser = mResources.getXml(R.xml.colors); 1227 AttributeSet colors_set = Xml.asAttributeSet(colors_parser); 1228 assertEquals(R.xml.colors, Resources.getAttributeSetSourceResId(colors_set)); 1229 1230 XmlPullParser content_layout_parser = mResources.getLayout(R.layout.context_layout); 1231 AttributeSet content_layout_set = Xml.asAttributeSet(content_layout_parser); 1232 assertEquals(R.layout.context_layout, 1233 Resources.getAttributeSetSourceResId(content_layout_set)); 1234 1235 XmlPullParser anim_rotate_parser = mResources.getAnimation(R.anim.anim_rotate); 1236 AttributeSet anim_rotate_set = Xml.asAttributeSet(anim_rotate_parser); 1237 assertEquals(R.anim.anim_rotate, Resources.getAttributeSetSourceResId(anim_rotate_set)); 1238 } 1239 1240 @Test testSystemFontFamilyReturnsSystemFont()1241 public void testSystemFontFamilyReturnsSystemFont() { 1242 Typeface typeface = mResources.getFont(R.font.sample_downloadable_font); 1243 assertEquals(typeface, Typeface.create("sans-serif", Typeface.NORMAL)); 1244 } 1245 1246 @Test testThemeCompare()1247 public void testThemeCompare() { 1248 Resources.Theme t1 = mResources.newTheme(); 1249 Resources.Theme t2 = mResources.newTheme(); 1250 assertTrue(t1.equals(t1)); 1251 assertTrue(t1.equals(t2)); 1252 assertTrue(t1.hashCode() == t2.hashCode()); 1253 assertFalse(t1.equals(null)); 1254 assertFalse(t1.equals(this)); 1255 1256 t1.applyStyle(1, false); 1257 assertFalse(t1.equals(t2)); 1258 assertFalse(t1.hashCode() == t2.hashCode()); 1259 t2.applyStyle(1, false); 1260 assertTrue(t1.equals(t2)); 1261 assertTrue(t1.hashCode() == t2.hashCode()); 1262 t1.applyStyle(2, true); 1263 assertFalse(t1.hashCode() == t2.hashCode()); 1264 assertFalse(t1.equals(t2)); 1265 t2.applyStyle(2, false); 1266 assertFalse(t1.equals(t2)); 1267 assertFalse(t1.hashCode() == t2.hashCode()); 1268 } 1269 spToPx(float sp, DisplayMetrics metrics)1270 private static float spToPx(float sp, DisplayMetrics metrics) { 1271 return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, metrics); 1272 } 1273 dpToPx(float dp, DisplayMetrics metrics)1274 private static float dpToPx(float dp, DisplayMetrics metrics) { 1275 return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics); 1276 } 1277 pxToSp(float px, DisplayMetrics metrics)1278 private static float pxToSp(float px, DisplayMetrics metrics) { 1279 return TypedValue.deriveDimension(TypedValue.COMPLEX_UNIT_SP, px, metrics); 1280 } 1281 pxToDp(float px, DisplayMetrics metrics)1282 private static float pxToDp(float px, DisplayMetrics metrics) { 1283 return TypedValue.deriveDimension(TypedValue.COMPLEX_UNIT_DIP, px, metrics); 1284 } 1285 } 1286