Home | History | Annotate | Download | only in overlaytest
      1 package com.android.overlaytest;
      2 
      3 import android.content.res.Configuration;
      4 import android.content.res.Resources;
      5 import android.content.res.XmlResourceParser;
      6 import android.test.AndroidTestCase;
      7 import android.util.AttributeSet;
      8 import android.util.Xml;
      9 import java.io.BufferedReader;
     10 import java.io.InputStream;
     11 import java.io.InputStreamReader;
     12 import java.util.Locale;
     13 
     14 public abstract class OverlayBaseTest extends AndroidTestCase {
     15     private Resources mResources;
     16     protected int mMode; // will be set by subclasses
     17     static final protected int MODE_NO_OVERLAY = 0;
     18     static final protected int MODE_SINGLE_OVERLAY = 1;
     19     static final protected int MODE_MULTIPLE_OVERLAYS = 2;
     20 
     21     protected void setUp() {
     22         mResources = getContext().getResources();
     23     }
     24 
     25     private int calculateRawResourceChecksum(int resId) throws Throwable {
     26         InputStream input = null;
     27         try {
     28             input = mResources.openRawResource(resId);
     29             int ch, checksum = 0;
     30             while ((ch = input.read()) != -1) {
     31                 checksum = (checksum + ch) % 0xffddbb00;
     32             }
     33             return checksum;
     34         } finally {
     35             input.close();
     36         }
     37     }
     38 
     39     private void setLocale(Locale locale) {
     40         Locale.setDefault(locale);
     41         Configuration config = new Configuration();
     42         config.locale = locale;
     43         mResources.updateConfiguration(config, mResources.getDisplayMetrics());
     44     }
     45 
     46     private boolean getExpected(boolean no, boolean so, boolean mo) {
     47         switch (mMode) {
     48             case MODE_NO_OVERLAY:
     49                 return no;
     50             case MODE_SINGLE_OVERLAY:
     51                 return so;
     52             case MODE_MULTIPLE_OVERLAYS:
     53                 return mo;
     54             default:
     55                 fail("Unknown mode!");
     56                 return no;
     57         }
     58     }
     59 
     60     private String getExpected(String no, String so, String mo) {
     61         switch (mMode) {
     62             case MODE_NO_OVERLAY:
     63                 return no;
     64             case MODE_SINGLE_OVERLAY:
     65                 return so;
     66             case MODE_MULTIPLE_OVERLAYS:
     67                 return mo;
     68             default:
     69                 fail("Unknown mode!");
     70                 return no;
     71         }
     72     }
     73 
     74     private int getExpected(int no, int so, int mo) {
     75         switch (mMode) {
     76             case MODE_NO_OVERLAY:
     77                 return no;
     78             case MODE_SINGLE_OVERLAY:
     79                 return so;
     80             case MODE_MULTIPLE_OVERLAYS:
     81                 return mo;
     82             default:
     83                 fail("Unknown mode!");
     84                 return no;
     85         }
     86     }
     87 
     88     private int[] getExpected(int[] no, int[] so, int[] mo) {
     89         switch (mMode) {
     90             case MODE_NO_OVERLAY:
     91                 return no;
     92             case MODE_SINGLE_OVERLAY:
     93                 return so;
     94             case MODE_MULTIPLE_OVERLAYS:
     95                 return mo;
     96             default:
     97                 fail("Unknown mode!");
     98                 return no;
     99         }
    100     }
    101 
    102     private void assertResource(int resId, boolean no, boolean so, boolean mo) throws Throwable {
    103         boolean expected = getExpected(no, so, mo);
    104         boolean actual = mResources.getBoolean(resId);
    105         assertEquals(expected, actual);
    106     }
    107 
    108     private void assertResource(int resId, int no, int so, int mo) throws Throwable {
    109         int expected = getExpected(no, so, mo);
    110         int actual = mResources.getInteger(resId);
    111         assertEquals(expected, actual);
    112     }
    113 
    114     private void assertResource(int resId, String no, String so, String mo) throws Throwable {
    115         String expected = getExpected(no, so, mo);
    116         String actual = mResources.getString(resId);
    117         assertEquals(expected, actual);
    118     }
    119 
    120     private void assertResource(int resId, int[] no, int[] so, int[] mo) throws Throwable {
    121         int[] expected = getExpected(no, so, mo);
    122         int[] actual = mResources.getIntArray(resId);
    123         assertEquals("length:", expected.length, actual.length);
    124         for (int i = 0; i < actual.length; ++i) {
    125             assertEquals("index " + i + ":", actual[i], expected[i]);
    126         }
    127     }
    128 
    129     public void testFrameworkBooleanOverlay() throws Throwable {
    130         // config_annoy_dianne has the value:
    131         // - true when no overlay exists (MODE_NO_OVERLAY)
    132         // - false when a single overlay exists (MODE_SINGLE_OVERLAY)
    133         // - false when multiple overlays exists (MODE_MULTIPLE_OVERLAYS)
    134         final int resId = com.android.internal.R.bool.config_annoy_dianne;
    135         assertResource(resId, true, false, false);
    136     }
    137 
    138     public void testBooleanOverlay() throws Throwable {
    139         // usually_false has the value:
    140         // - false when no overlay exists (MODE_NO_OVERLAY)
    141         // - true when a single overlay exists (MODE_SINGLE_OVERLAY)
    142         // - false when multiple overlays exists (MODE_MULTIPLE_OVERLAYS)
    143         final int resId = R.bool.usually_false;
    144         assertResource(resId, false, true, false);
    145     }
    146 
    147     public void testBoolean() throws Throwable {
    148         // always_true has no overlay
    149         final int resId = R.bool.always_true;
    150         assertResource(resId, true, true, true);
    151     }
    152 
    153     public void testIntegerArrayOverlay() throws Throwable {
    154         // fibonacci has values:
    155         // - eight first values of Fibonacci sequence, when no overlay exists (MODE_NO_OVERLAY)
    156         // - eight first values of Fibonacci sequence (reversed), for single and multiple overlays
    157         //   (MODE_SINGLE_OVERLAY, MODE_MULTIPLE_OVERLAYS)
    158         final int resId = R.array.fibonacci;
    159         assertResource(resId,
    160                 new int[]{1, 1, 2, 3, 5, 8, 13, 21},
    161                 new int[]{21, 13, 8, 5, 3, 2, 1, 1},
    162                 new int[]{21, 13, 8, 5, 3, 2, 1, 1});
    163     }
    164 
    165     public void testIntegerArray() throws Throwable {
    166         // prime_numbers has no overlay
    167         final int resId = R.array.prime_numbers;
    168         final int[] expected = {2, 3, 5, 7, 11, 13, 17, 19};
    169         assertResource(resId, expected, expected, expected);
    170     }
    171 
    172     public void testDrawable() throws Throwable {
    173         // drawable-nodpi/drawable has overlay (default config)
    174         final int resId = R.drawable.drawable;
    175         int actual = calculateRawResourceChecksum(resId);
    176         int expected = 0;
    177         switch (mMode) {
    178             case MODE_NO_OVERLAY:
    179                 expected = 0x00005665;
    180                 break;
    181             case MODE_SINGLE_OVERLAY:
    182             case MODE_MULTIPLE_OVERLAYS:
    183                 expected = 0x000051da;
    184                 break;
    185             default:
    186                 fail("Unknown mode " + mMode);
    187         }
    188         assertEquals(expected, actual);
    189     }
    190 
    191     public void testAppString() throws Throwable {
    192         final int resId = R.string.str;
    193         assertResource(resId, "none", "single", "multiple");
    194     }
    195 
    196     public void testApp2() throws Throwable {
    197         final int resId = R.string.str2; // only in base package and first app overlay
    198         assertResource(resId, "none", "single", "single");
    199     }
    200 
    201     public void testAppXml() throws Throwable {
    202         int expected = getExpected(0, 1, 2);
    203         int actual = -1;
    204         XmlResourceParser parser = mResources.getXml(R.xml.integer);
    205         int type = parser.getEventType();
    206         while (type != XmlResourceParser.END_DOCUMENT && actual == -1) {
    207             if (type == XmlResourceParser.START_TAG && "integer".equals(parser.getName())) {
    208                 AttributeSet as = Xml.asAttributeSet(parser);
    209                 actual = as.getAttributeIntValue(null, "value", -1);
    210             }
    211             type = parser.next();
    212         }
    213         parser.close();
    214         assertEquals(expected, actual);
    215     }
    216 
    217     public void testAppRaw() throws Throwable {
    218         final int resId = R.raw.lorem_ipsum;
    219 
    220         InputStream input = null;
    221         BufferedReader reader = null;
    222         String actual = "";
    223         try {
    224             input = mResources.openRawResource(resId);
    225             reader = new BufferedReader(new InputStreamReader(input));
    226             actual = reader.readLine();
    227         } finally {
    228             reader.close();
    229             input.close();
    230         }
    231 
    232         final String no = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, " +
    233             "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " +
    234             "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip " +
    235             "ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit " +
    236             "esse cillum dolore eu fugiat nulla pariatur. " +
    237             "Excepteur sint occaecat cupidatat non proident, " +
    238             "sunt in culpa qui officia deserunt mollit anim id est laborum.";
    239         final String so = "Lorem ipsum: single overlay.";
    240         final String mo = "Lorem ipsum: multiple overlays.";
    241 
    242         assertEquals(getExpected(no, so, mo), actual);
    243     }
    244 
    245     /*
    246      * testMatrix* tests
    247      *
    248      * The naming convention textMatrixABCDEF refers to in which packages and
    249      * which configurations a resource is defined (1 if the resource is
    250      * defined). If defined, a slot is always given the same value.
    251      *
    252      * SLOT  PACKAGE           CONFIGURATION  VALUE
    253      * A     target package    (default)      100
    254      * B     target package    -sv            200
    255      * C     OverlayAppFirst   (default)      300
    256      * D     OverlayAppFirst   -sv            400
    257      * E     OverlayAppSecond  (default)      500
    258      * F     OverlayAppSecond  -sv            600
    259      *
    260      * Example: in testMatrix101110, the base package defines the
    261      * R.integer.matrix101110 resource for the default configuration (value
    262      * 100), OverlayAppFirst defines it for both default and Swedish
    263      * configurations (values 300 and 400, respectively), and OverlayAppSecond
    264      * defines it for the default configuration (value 500). If both overlays
    265      * are loaded, the expected value after setting the language to Swedish is
    266      * 400.
    267      */
    268     public void testMatrix100000() throws Throwable {
    269         final int resId = R.integer.matrix_100000;
    270         setLocale(new Locale("sv", "SE"));
    271         assertResource(resId, 100, 100, 100);
    272     }
    273 
    274     public void testMatrix100001() throws Throwable {
    275         final int resId = R.integer.matrix_100001;
    276         setLocale(new Locale("sv", "SE"));
    277         assertResource(resId, 100, 100, 600);
    278     }
    279 
    280     public void testMatrix100010() throws Throwable {
    281         final int resId = R.integer.matrix_100010;
    282         setLocale(new Locale("sv", "SE"));
    283         assertResource(resId, 100, 100, 500);
    284     }
    285 
    286     public void testMatrix100011() throws Throwable {
    287         final int resId = R.integer.matrix_100011;
    288         setLocale(new Locale("sv", "SE"));
    289         assertResource(resId, 100, 100, 600);
    290     }
    291 
    292     public void testMatrix100100() throws Throwable {
    293         final int resId = R.integer.matrix_100100;
    294         setLocale(new Locale("sv", "SE"));
    295         assertResource(resId, 100, 400, 400);
    296     }
    297 
    298     public void testMatrix100101() throws Throwable {
    299         final int resId = R.integer.matrix_100101;
    300         setLocale(new Locale("sv", "SE"));
    301         assertResource(resId, 100, 400, 600);
    302     }
    303 
    304     public void testMatrix100110() throws Throwable {
    305         final int resId = R.integer.matrix_100110;
    306         setLocale(new Locale("sv", "SE"));
    307         assertResource(resId, 100, 400, 400);
    308     }
    309 
    310     public void testMatrix100111() throws Throwable {
    311         final int resId = R.integer.matrix_100111;
    312         setLocale(new Locale("sv", "SE"));
    313         assertResource(resId, 100, 400, 600);
    314     }
    315 
    316     public void testMatrix101000() throws Throwable {
    317         final int resId = R.integer.matrix_101000;
    318         setLocale(new Locale("sv", "SE"));
    319         assertResource(resId, 100, 300, 300);
    320     }
    321 
    322     public void testMatrix101001() throws Throwable {
    323         final int resId = R.integer.matrix_101001;
    324         setLocale(new Locale("sv", "SE"));
    325         assertResource(resId, 100, 300, 600);
    326     }
    327 
    328     public void testMatrix101010() throws Throwable {
    329         final int resId = R.integer.matrix_101010;
    330         setLocale(new Locale("sv", "SE"));
    331         assertResource(resId, 100, 300, 500);
    332     }
    333 
    334     public void testMatrix101011() throws Throwable {
    335         final int resId = R.integer.matrix_101011;
    336         setLocale(new Locale("sv", "SE"));
    337         assertResource(resId, 100, 300, 600);
    338     }
    339 
    340     public void testMatrix101100() throws Throwable {
    341         final int resId = R.integer.matrix_101100;
    342         setLocale(new Locale("sv", "SE"));
    343         assertResource(resId, 100, 400, 400);
    344     }
    345 
    346     public void testMatrix101101() throws Throwable {
    347         final int resId = R.integer.matrix_101101;
    348         setLocale(new Locale("sv", "SE"));
    349         assertResource(resId, 100, 400, 600);
    350     }
    351 
    352     public void testMatrix101110() throws Throwable {
    353         final int resId = R.integer.matrix_101110;
    354         setLocale(new Locale("sv", "SE"));
    355         assertResource(resId, 100, 400, 400);
    356     }
    357 
    358     public void testMatrix101111() throws Throwable {
    359         final int resId = R.integer.matrix_101111;
    360         setLocale(new Locale("sv", "SE"));
    361         assertResource(resId, 100, 400, 600);
    362     }
    363 
    364     public void testMatrix110000() throws Throwable {
    365         final int resId = R.integer.matrix_110000;
    366         setLocale(new Locale("sv", "SE"));
    367         assertResource(resId, 200, 200, 200);
    368     }
    369 
    370     public void testMatrix110001() throws Throwable {
    371         final int resId = R.integer.matrix_110001;
    372         setLocale(new Locale("sv", "SE"));
    373         assertResource(resId, 200, 200, 600);
    374     }
    375 
    376     public void testMatrix110010() throws Throwable {
    377         final int resId = R.integer.matrix_110010;
    378         setLocale(new Locale("sv", "SE"));
    379         assertResource(resId, 200, 200, 200);
    380     }
    381 
    382     public void testMatrix110011() throws Throwable {
    383         final int resId = R.integer.matrix_110011;
    384         setLocale(new Locale("sv", "SE"));
    385         assertResource(resId, 200, 200, 600);
    386     }
    387 
    388     public void testMatrix110100() throws Throwable {
    389         final int resId = R.integer.matrix_110100;
    390         setLocale(new Locale("sv", "SE"));
    391         assertResource(resId, 200, 400, 400);
    392     }
    393 
    394     public void testMatrix110101() throws Throwable {
    395         final int resId = R.integer.matrix_110101;
    396         setLocale(new Locale("sv", "SE"));
    397         assertResource(resId, 200, 400, 600);
    398     }
    399 
    400     public void testMatrix110110() throws Throwable {
    401         final int resId = R.integer.matrix_110110;
    402         setLocale(new Locale("sv", "SE"));
    403         assertResource(resId, 200, 400, 400);
    404     }
    405 
    406     public void testMatrix110111() throws Throwable {
    407         final int resId = R.integer.matrix_110111;
    408         setLocale(new Locale("sv", "SE"));
    409         assertResource(resId, 200, 400, 600);
    410     }
    411 
    412     public void testMatrix111000() throws Throwable {
    413         final int resId = R.integer.matrix_111000;
    414         setLocale(new Locale("sv", "SE"));
    415         assertResource(resId, 200, 200, 200);
    416     }
    417 
    418     public void testMatrix111001() throws Throwable {
    419         final int resId = R.integer.matrix_111001;
    420         setLocale(new Locale("sv", "SE"));
    421         assertResource(resId, 200, 200, 600);
    422     }
    423 
    424     public void testMatrix111010() throws Throwable {
    425         final int resId = R.integer.matrix_111010;
    426         setLocale(new Locale("sv", "SE"));
    427         assertResource(resId, 200, 200, 200);
    428     }
    429 
    430     public void testMatrix111011() throws Throwable {
    431         final int resId = R.integer.matrix_111011;
    432         setLocale(new Locale("sv", "SE"));
    433         assertResource(resId, 200, 200, 600);
    434     }
    435 
    436     public void testMatrix111100() throws Throwable {
    437         final int resId = R.integer.matrix_111100;
    438         setLocale(new Locale("sv", "SE"));
    439         assertResource(resId, 200, 400, 400);
    440     }
    441 
    442     public void testMatrix111101() throws Throwable {
    443         final int resId = R.integer.matrix_111101;
    444         setLocale(new Locale("sv", "SE"));
    445         assertResource(resId, 200, 400, 600);
    446     }
    447 
    448     public void testMatrix111110() throws Throwable {
    449         final int resId = R.integer.matrix_111110;
    450         setLocale(new Locale("sv", "SE"));
    451         assertResource(resId, 200, 400, 400);
    452     }
    453 
    454     public void testMatrix111111() throws Throwable {
    455         final int resId = R.integer.matrix_111111;
    456         setLocale(new Locale("sv", "SE"));
    457         assertResource(resId, 200, 400, 600);
    458     }
    459 }
    460