|
| 1 | +package com.android.overlaytest; |
| 2 | + |
| 3 | +import android.content.res.Configuration; |
| 4 | +import android.content.res.Resources; |
| 5 | +import android.test.AndroidTestCase; |
| 6 | +import java.io.InputStream; |
| 7 | +import java.util.Locale; |
| 8 | + |
| 9 | +public abstract class OverlayBaseTest extends AndroidTestCase { |
| 10 | + private Resources mResources; |
| 11 | + protected boolean mWithOverlay; // will be set by subclasses |
| 12 | + |
| 13 | + protected void setUp() { |
| 14 | + mResources = getContext().getResources(); |
| 15 | + } |
| 16 | + |
| 17 | + private int calculateRawResourceChecksum(int resId) throws Throwable { |
| 18 | + InputStream input = null; |
| 19 | + try { |
| 20 | + input = mResources.openRawResource(resId); |
| 21 | + int ch, checksum = 0; |
| 22 | + while ((ch = input.read()) != -1) { |
| 23 | + checksum = (checksum + ch) % 0xffddbb00; |
| 24 | + } |
| 25 | + return checksum; |
| 26 | + } finally { |
| 27 | + input.close(); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + private void setLocale(String code) { |
| 32 | + Locale locale = new Locale(code); |
| 33 | + Locale.setDefault(locale); |
| 34 | + Configuration config = new Configuration(); |
| 35 | + config.locale = locale; |
| 36 | + mResources.updateConfiguration(config, mResources.getDisplayMetrics()); |
| 37 | + } |
| 38 | + |
| 39 | + private void assertResource(int resId, boolean ewo, boolean ew) throws Throwable { |
| 40 | + boolean expected = mWithOverlay ? ew : ewo; |
| 41 | + boolean actual = mResources.getBoolean(resId); |
| 42 | + assertEquals(expected, actual); |
| 43 | + } |
| 44 | + |
| 45 | + private void assertResource(int resId, String ewo, String ew) throws Throwable { |
| 46 | + String expected = mWithOverlay ? ew : ewo; |
| 47 | + String actual = mResources.getString(resId); |
| 48 | + assertEquals(expected, actual); |
| 49 | + } |
| 50 | + |
| 51 | + private void assertResource(int resId, int[] ewo, int[] ew) throws Throwable { |
| 52 | + int[] expected = mWithOverlay ? ew : ewo; |
| 53 | + int[] actual = mResources.getIntArray(resId); |
| 54 | + assertEquals("length:", expected.length, actual.length); |
| 55 | + for (int i = 0; i < actual.length; ++i) { |
| 56 | + assertEquals("index " + i + ":", actual[i], expected[i]); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public void testBooleanOverlay() throws Throwable { |
| 61 | + // config_automatic_brightness_available has overlay (default config) |
| 62 | + final int resId = com.android.internal.R.bool.config_automatic_brightness_available; |
| 63 | + assertResource(resId, false, true); |
| 64 | + } |
| 65 | + |
| 66 | + public void testBoolean() throws Throwable { |
| 67 | + // config_bypass_keyguard_if_slider_open has no overlay |
| 68 | + final int resId = com.android.internal.R.bool.config_bypass_keyguard_if_slider_open; |
| 69 | + assertResource(resId, true, true); |
| 70 | + } |
| 71 | + |
| 72 | + public void testStringOverlay() throws Throwable { |
| 73 | + // phoneTypeCar has an overlay (default config), which shouldn't shadow |
| 74 | + // the Swedish translation |
| 75 | + final int resId = com.android.internal.R.string.phoneTypeCar; |
| 76 | + setLocale("sv_SE"); |
| 77 | + assertResource(resId, "Bil", "Bil"); |
| 78 | + } |
| 79 | + |
| 80 | + public void testStringSwedishOverlay() throws Throwable { |
| 81 | + // phoneTypeWork has overlay (no default config, only for lang=sv) |
| 82 | + final int resId = com.android.internal.R.string.phoneTypeWork; |
| 83 | + setLocale("en_US"); |
| 84 | + assertResource(resId, "Work", "Work"); |
| 85 | + setLocale("sv_SE"); |
| 86 | + assertResource(resId, "Arbete", "Jobb"); |
| 87 | + } |
| 88 | + |
| 89 | + public void testString() throws Throwable { |
| 90 | + // phoneTypeHome has no overlay |
| 91 | + final int resId = com.android.internal.R.string.phoneTypeHome; |
| 92 | + setLocale("en_US"); |
| 93 | + assertResource(resId, "Home", "Home"); |
| 94 | + setLocale("sv_SE"); |
| 95 | + assertResource(resId, "Hem", "Hem"); |
| 96 | + } |
| 97 | + |
| 98 | + public void testIntegerArrayOverlay() throws Throwable { |
| 99 | + // config_scrollBarrierVibePattern has overlay (default config) |
| 100 | + final int resId = com.android.internal.R.array.config_scrollBarrierVibePattern; |
| 101 | + assertResource(resId, new int[]{0, 15, 10, 10}, new int[]{100, 200, 300}); |
| 102 | + } |
| 103 | + |
| 104 | + public void testIntegerArray() throws Throwable { |
| 105 | + // config_virtualKeyVibePattern has no overlay |
| 106 | + final int resId = com.android.internal.R.array.config_virtualKeyVibePattern; |
| 107 | + final int[] expected = {0, 10, 20, 30}; |
| 108 | + assertResource(resId, expected, expected); |
| 109 | + } |
| 110 | + |
| 111 | + public void testAsset() throws Throwable { |
| 112 | + // drawable/default_background.jpg has overlay (default config) |
| 113 | + final int resId = com.android.internal.R.drawable.default_wallpaper; |
| 114 | + int actual = calculateRawResourceChecksum(resId); |
| 115 | + int expected = mWithOverlay ? 0x000051da : 0x0014ebce; |
| 116 | + assertEquals(expected, actual); |
| 117 | + } |
| 118 | +} |
0 commit comments