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 java.util.ArrayList;
20 import java.util.List;
21 import java.util.Locale;
22 
23 import android.content.res.AssetManager;
24 import android.content.res.Configuration;
25 import android.content.res.Resources;
26 import android.content.res.TypedArray;
27 import android.content.res.Resources.NotFoundException;
28 import android.test.AndroidTestCase;
29 import android.test.suitebuilder.annotation.MediumTest;
30 import android.test.suitebuilder.annotation.SmallTest;
31 import android.util.DisplayMetrics;
32 import android.util.Log;
33 
34 import android.content.cts.R;
35 
36 public class ConfigTest extends AndroidTestCase {
37     enum Properties {
38         LANGUAGE,
39         COUNTRY,
40         SCRIPT,
41         VARIANT,
42         MCC,
43         MNC,
44         TOUCHSCREEN,
45         KEYBOARD,
46         KEYBOARDHIDDEN,
47         NAVIGATION,
48         ORIENTATION,
49         COLOR_MODE,
50         WIDTH,
51         HEIGHT,
52         DENSITY,
53         SCREENLAYOUT,
54         SWIDTH_DP,
55         WIDTH_DP,
56         HEIGHT_DP
57     }
58 
checkValue(final Resources res, final int resId, final String expectedValue)59     private static void checkValue(final Resources res, final int resId,
60             final String expectedValue) {
61         try {
62             final String actual = res.getString(resId);
63             assertNotNull("Returned wrong configuration-based simple value: expected <nothing>, "
64                     + "got '" + actual + "' from resource 0x" + Integer.toHexString(resId),
65                     expectedValue);
66             assertEquals("Returned wrong configuration-based simple value: expected '"
67                     + expectedValue + "', got '" + actual + "' from resource 0x"
68                     + Integer.toHexString(resId), expectedValue, actual);
69         } catch (NotFoundException e) {
70             assertNull("Resource not found for configuration-based simple value: expecting \""
71                     + expectedValue + "\"", expectedValue);
72         }
73     }
74 
checkValue(final Resources res, final int resId, final int[] styleable, final String[] expectedValues)75     private static void checkValue(final Resources res, final int resId,
76             final int[] styleable, final String[] expectedValues) {
77         final Resources.Theme theme = res.newTheme();
78         final TypedArray sa = theme.obtainStyledAttributes(resId, styleable);
79         for (int i = 0; i < styleable.length; i++) {
80             final String actual = sa.getString(i);
81             assertEquals("Returned wrong configuration-based style value: expected '"
82                     + expectedValues[i] + "', got '" + actual + "' from attr "
83                     + i + " of resource 0x" + Integer.toHexString(resId),
84                     actual, expectedValues[i]);
85         }
86         sa.recycle();
87     }
88 
89     private class TotalConfig {
90         final Configuration mConfig;
91         final DisplayMetrics mMetrics;
92 
TotalConfig()93         public TotalConfig() {
94             mConfig = new Configuration();
95             mMetrics = new DisplayMetrics();
96             mConfig.locale = Locale.ROOT;
97         }
98 
setProperty(final Properties p, final int value)99         public void setProperty(final Properties p, final int value) {
100             switch(p) {
101                 case MCC:
102                     mConfig.mcc = value;
103                     break;
104                 case MNC:
105                     mConfig.mnc = value;
106                     break;
107                 case TOUCHSCREEN:
108                     mConfig.touchscreen = value;
109                     break;
110                 case KEYBOARD:
111                     mConfig.keyboard = value;
112                     break;
113                 case KEYBOARDHIDDEN:
114                     mConfig.keyboardHidden = value;
115                     break;
116                 case NAVIGATION:
117                     mConfig.navigation = value;
118                     break;
119                 case ORIENTATION:
120                     mConfig.orientation = value;
121                     break;
122                 case COLOR_MODE:
123                     mConfig.colorMode = value;
124                     break;
125                 case WIDTH:
126                     mMetrics.widthPixels = value;
127                     mMetrics.noncompatWidthPixels = value;
128                     break;
129                 case HEIGHT:
130                     mMetrics.heightPixels = value;
131                     mMetrics.noncompatHeightPixels = value;
132                     break;
133                 case DENSITY:
134                     // this is the ratio from the standard
135                     mMetrics.density = (((float)value)/((float)DisplayMetrics.DENSITY_DEFAULT));
136                     mMetrics.noncompatDensity = mMetrics.density;
137                     mConfig.densityDpi = value;
138                     break;
139                 case SCREENLAYOUT:
140                     mConfig.screenLayout = value;
141                     break;
142                 case SWIDTH_DP:
143                     mConfig.smallestScreenWidthDp = value;
144                     break;
145                 case WIDTH_DP:
146                     mConfig.screenWidthDp = value;
147                     break;
148                 case HEIGHT_DP:
149                     mConfig.screenHeightDp = value;
150                     break;
151                 default:
152                     assert(false);
153                     break;
154             }
155         }
156 
setProperty(final Properties p, final String value)157         public void setProperty(final Properties p, final String value) {
158             switch(p) {
159                 case LANGUAGE:
160                     mConfig.locale = new Locale.Builder()
161                             .setLocale(mConfig.locale)
162                             .setLanguage(value)
163                             .build();
164                     break;
165                 case COUNTRY:
166                     mConfig.locale = new Locale.Builder()
167                             .setLocale(mConfig.locale)
168                             .setRegion(value)
169                             .build();
170                     break;
171                 case SCRIPT:
172                     mConfig.locale = new Locale.Builder()
173                             .setLocale(mConfig.locale)
174                             .setScript(value)
175                             .build();
176                     break;
177                 case VARIANT:
178                     mConfig.locale = new Locale.Builder()
179                             .setLocale(mConfig.locale)
180                             .setVariant(value)
181                             .build();
182                     break;
183                 default:
184                     assert(false);
185                     break;
186             }
187         }
188 
getResources()189         public Resources getResources() {
190             final AssetManager assmgr = new AssetManager();
191             assmgr.addAssetPath(mContext.getPackageResourcePath());
192             return new Resources(assmgr, mMetrics, mConfig);
193         }
194     }
195 
makeEmptyConfig()196     public TotalConfig makeEmptyConfig() {
197         return new TotalConfig();
198     }
199 
makeClassicConfig()200     public TotalConfig makeClassicConfig() {
201         TotalConfig config = new TotalConfig();
202         config.setProperty(Properties.LANGUAGE, "en");
203         config.setProperty(Properties.COUNTRY, "US");
204         config.setProperty(Properties.MCC, 310);
205         config.setProperty(Properties.MNC, 001); // unused
206         config.setProperty(Properties.TOUCHSCREEN, Configuration.TOUCHSCREEN_FINGER);
207         config.setProperty(Properties.KEYBOARD, Configuration.KEYBOARD_QWERTY);
208         config.setProperty(Properties.KEYBOARDHIDDEN, Configuration.KEYBOARDHIDDEN_YES);
209         config.setProperty(Properties.NAVIGATION, Configuration.NAVIGATION_TRACKBALL);
210         config.setProperty(Properties.ORIENTATION, Configuration.ORIENTATION_PORTRAIT);
211         config.setProperty(Properties.SWIDTH_DP, 320);
212         config.setProperty(Properties.WIDTH_DP, 320);
213         config.setProperty(Properties.HEIGHT_DP, 480);
214         config.setProperty(Properties.DENSITY, 160);
215         config.setProperty(Properties.WIDTH, 200);
216         config.setProperty(Properties.HEIGHT, 320);
217         return config;
218     }
219 
checkPair(Resources res, int[] notResIds, int simpleRes, String simpleString, int bagRes, String bagString)220     private static void checkPair(Resources res, int[] notResIds,
221             int simpleRes, String simpleString,
222             int bagRes, String bagString) {
223         boolean willHave = true;
224         if (notResIds != null) {
225             for (int i : notResIds) {
226                 if (i == simpleRes) {
227                     willHave = false;
228                     break;
229                 }
230             }
231         }
232         checkValue(res, simpleRes, willHave ? simpleString : null);
233         checkValue(res, bagRes, R.styleable.TestConfig,
234                 new String[]{willHave ? bagString : null});
235     }
236 
237     @SmallTest
testAllEmptyConfigs()238     public void testAllEmptyConfigs() {
239         /**
240          * Test a resource that contains a value for each possible single
241          * configuration value.
242          */
243         TotalConfig config = makeEmptyConfig();
244         Resources res = config.getResources();
245         checkValue(res, R.configVarying.simple, "simple default");
246         checkValue(res, R.configVarying.bag,
247                 R.styleable.TestConfig, new String[]{"bag default"});
248 
249         config = makeEmptyConfig();
250         config.setProperty(Properties.LANGUAGE, "xx");
251         res = config.getResources();
252         checkValue(res, R.configVarying.simple, "simple xx");
253         checkValue(res, R.configVarying.bag,
254                 R.styleable.TestConfig, new String[]{"bag xx"});
255 
256         config = makeEmptyConfig();
257         config.setProperty(Properties.LANGUAGE, "xx");
258         config.setProperty(Properties.COUNTRY, "YY");
259         res = config.getResources();
260         checkValue(res, R.configVarying.simple, "simple xx-rYY");
261         checkValue(res, R.configVarying.bag,
262                 R.styleable.TestConfig, new String[]{"bag xx-rYY"});
263 
264         config = makeEmptyConfig();
265         config.setProperty(Properties.MCC, 111);
266         res = config.getResources();
267         checkValue(res, R.configVarying.simple, "simple mcc111");
268         checkValue(res, R.configVarying.bag,
269                 R.styleable.TestConfig, new String[]{"bag mcc111"});
270 
271         config = makeEmptyConfig();
272         config.setProperty(Properties.MNC, 222);
273         res = config.getResources();
274         checkValue(res, R.configVarying.simple, "simple mnc222");
275         checkValue(res, R.configVarying.bag,
276                 R.styleable.TestConfig, new String[]{"bag mnc222"});
277 
278         config = makeEmptyConfig();
279         config.setProperty(Properties.TOUCHSCREEN, Configuration.TOUCHSCREEN_NOTOUCH);
280         res = config.getResources();
281         checkValue(res, R.configVarying.simple, "simple notouch");
282         checkValue(res, R.configVarying.bag,
283                 R.styleable.TestConfig, new String[]{"bag notouch"});
284 
285         config = makeEmptyConfig();
286         config.setProperty(Properties.TOUCHSCREEN, Configuration.TOUCHSCREEN_STYLUS);
287         res = config.getResources();
288         checkValue(res, R.configVarying.simple, "simple stylus");
289         checkValue(res, R.configVarying.bag,
290                 R.styleable.TestConfig, new String[]{"bag stylus"});
291 
292         config = makeEmptyConfig();
293         config.setProperty(Properties.KEYBOARD, Configuration.KEYBOARD_NOKEYS);
294         res = config.getResources();
295         checkValue(res, R.configVarying.simple, "simple nokeys");
296         checkValue(res, R.configVarying.bag,
297                 R.styleable.TestConfig, new String[]{"bag nokeys"});
298 
299         config = makeEmptyConfig();
300         config.setProperty(Properties.KEYBOARD, Configuration.KEYBOARD_12KEY);
301         res = config.getResources();
302         checkValue(res, R.configVarying.simple, "simple 12key");
303         checkValue(res, R.configVarying.bag,
304                 R.styleable.TestConfig, new String[]{"bag 12key"});
305 
306         config = makeEmptyConfig();
307         config.setProperty(Properties.KEYBOARDHIDDEN, Configuration.KEYBOARDHIDDEN_NO);
308         res = config.getResources();
309         checkValue(res, R.configVarying.simple, "simple keysexposed");
310         checkValue(res, R.configVarying.bag,
311                 R.styleable.TestConfig, new String[]{"bag keysexposed"});
312 
313         config = makeEmptyConfig();
314         config.setProperty(Properties.NAVIGATION, Configuration.NAVIGATION_NONAV);
315         res = config.getResources();
316         checkValue(res, R.configVarying.simple, "simple nonav");
317         checkValue(res, R.configVarying.bag,
318                 R.styleable.TestConfig, new String[]{"bag nonav"});
319 
320         config = makeEmptyConfig();
321         config.setProperty(Properties.NAVIGATION, Configuration.NAVIGATION_DPAD);
322         res = config.getResources();
323         checkValue(res, R.configVarying.simple, "simple dpad");
324         checkValue(res, R.configVarying.bag,
325                 R.styleable.TestConfig, new String[]{"bag dpad"});
326 
327         config = makeEmptyConfig();
328         config.setProperty(Properties.NAVIGATION, Configuration.NAVIGATION_WHEEL);
329         res = config.getResources();
330         checkValue(res, R.configVarying.simple, "simple wheel");
331         checkValue(res, R.configVarying.bag,
332                 R.styleable.TestConfig, new String[]{"bag wheel"});
333 
334         config = makeEmptyConfig();
335         config.setProperty(Properties.HEIGHT, 480);
336         config.setProperty(Properties.WIDTH, 320);
337         res = config.getResources();
338         checkValue(res, R.configVarying.simple, "simple 480x320");
339         checkValue(res, R.configVarying.bag,
340                 R.styleable.TestConfig, new String[]{"bag 480x320"});
341 
342         config = makeEmptyConfig();
343         config.setProperty(Properties.DENSITY, 240);
344         res = config.getResources();
345         checkValue(res, R.configVarying.simple, "simple 240dpi");
346         checkValue(res, R.configVarying.bag,
347                 R.styleable.TestConfig, new String[]{"bag 240dpi"});
348 
349         config = makeEmptyConfig();
350         config.setProperty(Properties.ORIENTATION, Configuration.ORIENTATION_LANDSCAPE);
351         res = config.getResources();
352         checkValue(res, R.configVarying.simple, "simple landscape");
353         checkValue(res, R.configVarying.bag,
354                 R.styleable.TestConfig, new String[]{"bag landscape"});
355 
356         config = makeEmptyConfig();
357         config.setProperty(Properties.ORIENTATION, Configuration.ORIENTATION_SQUARE);
358         res = config.getResources();
359         checkValue(res, R.configVarying.simple, "simple square");
360         checkValue(res, R.configVarying.bag,
361                 R.styleable.TestConfig, new String[]{"bag square"});
362 
363         config = makeEmptyConfig();
364         config.setProperty(Properties.COLOR_MODE, Configuration.COLOR_MODE_HDR_YES);
365         res = config.getResources();
366         checkValue(res, R.configVarying.simple, "simple hdr");
367         checkValue(res, R.configVarying.bag,
368                 R.styleable.TestConfig, new String[]{"bag hdr"});
369 
370         config = makeEmptyConfig();
371         config.setProperty(Properties.COLOR_MODE, Configuration.COLOR_MODE_HDR_NO);
372         res = config.getResources();
373         checkValue(res, R.configVarying.simple, "simple ldr");
374         checkValue(res, R.configVarying.bag,
375                 R.styleable.TestConfig, new String[]{"bag ldr"});
376 
377         config = makeEmptyConfig();
378         config.setProperty(Properties.COLOR_MODE, Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES);
379         res = config.getResources();
380         checkValue(res, R.configVarying.simple, "simple widecg");
381         checkValue(res, R.configVarying.bag,
382                 R.styleable.TestConfig, new String[]{"bag widecg"});
383 
384         config = makeEmptyConfig();
385         config.setProperty(Properties.COLOR_MODE, Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
386         res = config.getResources();
387         checkValue(res, R.configVarying.simple, "simple nowidecg");
388         checkValue(res, R.configVarying.bag,
389                 R.styleable.TestConfig, new String[]{"bag nowidecg"});
390 
391         config = makeEmptyConfig();
392         config.setProperty(Properties.SCREENLAYOUT, Configuration.SCREENLAYOUT_SIZE_SMALL);
393         res = config.getResources();
394         checkValue(res, R.configVarying.simple, "simple small");
395         checkValue(res, R.configVarying.bag,
396                 R.styleable.TestConfig, new String[]{"bag small"});
397 
398         config = makeEmptyConfig();
399         config.setProperty(Properties.SCREENLAYOUT, Configuration.SCREENLAYOUT_SIZE_NORMAL);
400         res = config.getResources();
401         checkValue(res, R.configVarying.simple, "simple normal");
402         checkValue(res, R.configVarying.bag,
403                 R.styleable.TestConfig, new String[]{"bag normal"});
404 
405         config = makeEmptyConfig();
406         config.setProperty(Properties.SCREENLAYOUT, Configuration.SCREENLAYOUT_SIZE_LARGE);
407         res = config.getResources();
408         checkValue(res, R.configVarying.simple, "simple large");
409         checkValue(res, R.configVarying.bag,
410                 R.styleable.TestConfig, new String[]{"bag large"});
411 
412         config = makeEmptyConfig();
413         config.setProperty(Properties.SCREENLAYOUT, Configuration.SCREENLAYOUT_SIZE_XLARGE);
414         res = config.getResources();
415         checkValue(res, R.configVarying.simple, "simple xlarge");
416         checkValue(res, R.configVarying.bag,
417                 R.styleable.TestConfig, new String[]{"bag xlarge"});
418 
419         config = makeEmptyConfig();
420         config.setProperty(Properties.SWIDTH_DP, 600);
421         res = config.getResources();
422         checkValue(res, R.configVarying.simple, "simple sw600");
423         checkValue(res, R.configVarying.bag,
424                 R.styleable.TestConfig, new String[]{"bag sw600"});
425 
426         config = makeEmptyConfig();
427         config.setProperty(Properties.SWIDTH_DP, 600);
428         res = config.getResources();
429         checkValue(res, R.configVarying.simple, "simple sw600");
430         checkValue(res, R.configVarying.bag,
431                 R.styleable.TestConfig, new String[]{"bag sw600"});
432 
433         config = makeEmptyConfig();
434         config.setProperty(Properties.SWIDTH_DP, 720);
435         res = config.getResources();
436         checkValue(res, R.configVarying.simple, "simple sw720");
437         checkValue(res, R.configVarying.bag,
438                 R.styleable.TestConfig, new String[]{"bag sw720"});
439 
440         config = makeEmptyConfig();
441         config.setProperty(Properties.WIDTH_DP, 600);
442         res = config.getResources();
443         checkValue(res, R.configVarying.simple, "simple w600");
444         checkValue(res, R.configVarying.bag,
445                 R.styleable.TestConfig, new String[]{"bag w600"});
446 
447         config = makeEmptyConfig();
448         config.setProperty(Properties.WIDTH_DP, 720);
449         res = config.getResources();
450         checkValue(res, R.configVarying.simple, "simple w720");
451         checkValue(res, R.configVarying.bag,
452                 R.styleable.TestConfig, new String[]{"bag w720"});
453 
454         config = makeEmptyConfig();
455         config.setProperty(Properties.HEIGHT_DP, 550);
456         res = config.getResources();
457         checkValue(res, R.configVarying.simple, "simple h550");
458         checkValue(res, R.configVarying.bag,
459                 R.styleable.TestConfig, new String[]{"bag h550"});
460 
461         config = makeEmptyConfig();
462         config.setProperty(Properties.HEIGHT_DP, 670);
463         res = config.getResources();
464         checkValue(res, R.configVarying.simple, "simple h670");
465         checkValue(res, R.configVarying.bag,
466                 R.styleable.TestConfig, new String[]{"bag h670"});
467     }
468 
469     @SmallTest
testAllClassicConfigs()470     public void testAllClassicConfigs() {
471         /**
472          * Test a resource that contains a value for each possible single
473          * configuration value.
474          */
475         TotalConfig config = makeClassicConfig();
476         Resources res = config.getResources();
477         checkValue(res, R.configVarying.simple, "simple default");
478         checkValue(res, R.configVarying.bag,
479                 R.styleable.TestConfig, new String[]{"bag default"});
480 
481         config = makeClassicConfig();
482         config.setProperty(Properties.LANGUAGE, "xx");
483         res = config.getResources();
484         checkValue(res, R.configVarying.simple, "simple xx");
485         checkValue(res, R.configVarying.bag,
486                 R.styleable.TestConfig, new String[]{"bag xx"});
487 
488         config = makeClassicConfig();
489         config.setProperty(Properties.LANGUAGE, "xx");
490         config.setProperty(Properties.COUNTRY, "YY");
491         res = config.getResources();
492         checkValue(res, R.configVarying.simple, "simple xx-rYY");
493         checkValue(res, R.configVarying.bag,
494                 R.styleable.TestConfig, new String[]{"bag xx-rYY"});
495 
496         config = makeClassicConfig();
497         config.setProperty(Properties.MCC, 111);
498         res = config.getResources();
499         checkValue(res, R.configVarying.simple, "simple mcc111");
500         checkValue(res, R.configVarying.bag,
501                 R.styleable.TestConfig, new String[]{"bag mcc111"});
502 
503         config = makeClassicConfig();
504         config.setProperty(Properties.MNC, 222);
505         res = config.getResources();
506         checkValue(res, R.configVarying.simple, "simple mnc222");
507         checkValue(res, R.configVarying.bag,
508                 R.styleable.TestConfig, new String[]{"bag mnc222"});
509 
510         config = makeClassicConfig();
511         config.setProperty(Properties.TOUCHSCREEN, Configuration.TOUCHSCREEN_NOTOUCH);
512         res = config.getResources();
513         checkValue(res, R.configVarying.simple, "simple notouch");
514         checkValue(res, R.configVarying.bag,
515                 R.styleable.TestConfig, new String[]{"bag notouch"});
516 
517         config = makeClassicConfig();
518         config.setProperty(Properties.TOUCHSCREEN, Configuration.TOUCHSCREEN_STYLUS);
519         res = config.getResources();
520         checkValue(res, R.configVarying.simple, "simple stylus");
521         checkValue(res, R.configVarying.bag,
522                 R.styleable.TestConfig, new String[]{"bag stylus"});
523 
524         config = makeClassicConfig();
525         config.setProperty(Properties.KEYBOARD, Configuration.KEYBOARD_NOKEYS);
526         res = config.getResources();
527         checkValue(res, R.configVarying.simple, "simple nokeys");
528         checkValue(res, R.configVarying.bag,
529                 R.styleable.TestConfig, new String[]{"bag nokeys"});
530 
531         config = makeClassicConfig();
532         config.setProperty(Properties.KEYBOARD, Configuration.KEYBOARD_12KEY);
533         res = config.getResources();
534         checkValue(res, R.configVarying.simple, "simple 12key 63x57");
535         checkValue(res, R.configVarying.bag,
536                 R.styleable.TestConfig, new String[]{"bag 12key 63x57"});
537 
538         config = makeClassicConfig();
539         config.setProperty(Properties.KEYBOARDHIDDEN, Configuration.KEYBOARDHIDDEN_NO);
540         res = config.getResources();
541         checkValue(res, R.configVarying.simple, "simple keysexposed");
542         checkValue(res, R.configVarying.bag,
543                 R.styleable.TestConfig, new String[]{"bag keysexposed"});
544 
545         config = makeClassicConfig();
546         config.setProperty(Properties.NAVIGATION, Configuration.NAVIGATION_NONAV);
547         res = config.getResources();
548         checkValue(res, R.configVarying.simple, "simple nonav");
549         checkValue(res, R.configVarying.bag,
550                 R.styleable.TestConfig, new String[]{"bag nonav"});
551 
552         config = makeClassicConfig();
553         config.setProperty(Properties.NAVIGATION, Configuration.NAVIGATION_DPAD);
554         res = config.getResources();
555         checkValue(res, R.configVarying.simple, "simple dpad 63x57");
556         checkValue(res, R.configVarying.bag,
557                 R.styleable.TestConfig, new String[]{"bag dpad 63x57"});
558 
559         config = makeClassicConfig();
560         config.setProperty(Properties.NAVIGATION, Configuration.NAVIGATION_WHEEL);
561         res = config.getResources();
562         checkValue(res, R.configVarying.simple, "simple wheel");
563         checkValue(res, R.configVarying.bag,
564                 R.styleable.TestConfig, new String[]{"bag wheel"});
565 
566         config = makeClassicConfig();
567         config.setProperty(Properties.HEIGHT, 480);
568         config.setProperty(Properties.WIDTH, 320);
569         res = config.getResources();
570         checkValue(res, R.configVarying.simple, "simple 480x320");
571         checkValue(res, R.configVarying.bag,
572                 R.styleable.TestConfig, new String[]{"bag 480x320"});
573 
574         config = makeClassicConfig();
575         config.setProperty(Properties.DENSITY, 240);
576         res = config.getResources();
577         checkValue(res, R.configVarying.simple, "simple 240dpi");
578         checkValue(res, R.configVarying.bag,
579                 R.styleable.TestConfig, new String[]{"bag 240dpi"});
580 
581         config = makeClassicConfig();
582         config.setProperty(Properties.ORIENTATION, Configuration.ORIENTATION_LANDSCAPE);
583         res = config.getResources();
584         checkValue(res, R.configVarying.simple, "simple landscape");
585         checkValue(res, R.configVarying.bag,
586                 R.styleable.TestConfig, new String[]{"bag landscape"});
587 
588         config = makeClassicConfig();
589         config.setProperty(Properties.ORIENTATION, Configuration.ORIENTATION_SQUARE);
590         res = config.getResources();
591         checkValue(res, R.configVarying.simple, "simple square");
592         checkValue(res, R.configVarying.bag,
593                 R.styleable.TestConfig, new String[]{"bag square"});
594 
595         config = makeClassicConfig();
596         config.setProperty(Properties.SCREENLAYOUT, Configuration.SCREENLAYOUT_SIZE_SMALL);
597         res = config.getResources();
598         checkValue(res, R.configVarying.simple, "simple small");
599         checkValue(res, R.configVarying.bag,
600                 R.styleable.TestConfig, new String[]{"bag small"});
601 
602         config = makeClassicConfig();
603         config.setProperty(Properties.SCREENLAYOUT, Configuration.SCREENLAYOUT_SIZE_NORMAL);
604         res = config.getResources();
605         checkValue(res, R.configVarying.simple, "simple normal");
606         checkValue(res, R.configVarying.bag,
607                 R.styleable.TestConfig, new String[]{"bag normal"});
608 
609         config = makeClassicConfig();
610         config.setProperty(Properties.SCREENLAYOUT, Configuration.SCREENLAYOUT_SIZE_LARGE);
611         res = config.getResources();
612         checkValue(res, R.configVarying.simple, "simple large");
613         checkValue(res, R.configVarying.bag,
614                 R.styleable.TestConfig, new String[]{"bag large"});
615 
616         config = makeClassicConfig();
617         config.setProperty(Properties.SCREENLAYOUT, Configuration.SCREENLAYOUT_SIZE_XLARGE);
618         res = config.getResources();
619         checkValue(res, R.configVarying.simple, "simple xlarge");
620         checkValue(res, R.configVarying.bag,
621                 R.styleable.TestConfig, new String[]{"bag xlarge"});
622 
623         config = makeClassicConfig();
624         config.setProperty(Properties.SWIDTH_DP, 600);
625         res = config.getResources();
626         checkValue(res, R.configVarying.simple, "simple sw600");
627         checkValue(res, R.configVarying.bag,
628                 R.styleable.TestConfig, new String[]{"bag sw600"});
629 
630         config = makeClassicConfig();
631         config.setProperty(Properties.SWIDTH_DP, 600);
632         config.setProperty(Properties.ORIENTATION, Configuration.ORIENTATION_LANDSCAPE);
633         res = config.getResources();
634         checkValue(res, R.configVarying.simple, "simple sw600 land");
635         checkValue(res, R.configVarying.bag,
636                 R.styleable.TestConfig, new String[]{"bag sw600 land"});
637 
638         config = makeClassicConfig();
639         config.setProperty(Properties.SWIDTH_DP, 720);
640         res = config.getResources();
641         checkValue(res, R.configVarying.simple, "simple sw720");
642         checkValue(res, R.configVarying.bag,
643                 R.styleable.TestConfig, new String[]{"bag sw720"});
644 
645         config = makeClassicConfig();
646         config.setProperty(Properties.WIDTH_DP, 600);
647         res = config.getResources();
648         checkValue(res, R.configVarying.simple, "simple w600");
649         checkValue(res, R.configVarying.bag,
650                 R.styleable.TestConfig, new String[]{"bag w600"});
651 
652         config = makeClassicConfig();
653         config.setProperty(Properties.WIDTH_DP, 720);
654         res = config.getResources();
655         checkValue(res, R.configVarying.simple, "simple w720");
656         checkValue(res, R.configVarying.bag,
657                 R.styleable.TestConfig, new String[]{"bag w720"});
658 
659         config = makeClassicConfig();
660         config.setProperty(Properties.HEIGHT_DP, 550);
661         res = config.getResources();
662         checkValue(res, R.configVarying.simple, "simple h550");
663         checkValue(res, R.configVarying.bag,
664                 R.styleable.TestConfig, new String[]{"bag h550"});
665 
666         config = makeClassicConfig();
667         config.setProperty(Properties.HEIGHT_DP, 670);
668         res = config.getResources();
669         checkValue(res, R.configVarying.simple, "simple h670");
670         checkValue(res, R.configVarying.bag,
671                 R.styleable.TestConfig, new String[]{"bag h670"});
672     }
673 
674     @MediumTest
testDensity()675     public void testDensity() throws Exception {
676         // have 32, 240 and the default 160 content.
677         // rule is that closest wins, with down scaling (larger content)
678         // being twice as nice as upscaling.
679         // transition at H/2 * (-1 +/- sqrt(1+8L/H))
680         // SO, X < 49 goes to 32
681         // 49 >= X < 182 goes to 160
682         // X >= 182 goes to 240
683         TotalConfig config = makeClassicConfig();
684         config.setProperty(Properties.DENSITY, 2);
685         Resources res = config.getResources();
686         checkValue(res, R.configVarying.simple, "simple 32dpi");
687         checkValue(res, R.configVarying.bag,
688                 R.styleable.TestConfig, new String[]{"bag 32dpi"});
689 
690         config = makeClassicConfig();
691         config.setProperty(Properties.DENSITY, 32);
692         res = config.getResources();
693         checkValue(res, R.configVarying.simple, "simple 32dpi");
694         checkValue(res, R.configVarying.bag,
695                 R.styleable.TestConfig, new String[]{"bag 32dpi"});
696 
697         config = makeClassicConfig();
698         config.setProperty(Properties.DENSITY, 48);
699         res = config.getResources();
700         checkValue(res, R.configVarying.simple, "simple 32dpi");
701         checkValue(res, R.configVarying.bag,
702                 R.styleable.TestConfig, new String[]{"bag 32dpi"});
703 
704         config = makeClassicConfig();
705         config.setProperty(Properties.DENSITY, 49);
706         res = config.getResources();
707         checkValue(res, R.configVarying.simple, "simple default");
708         checkValue(res, R.configVarying.bag,
709                 R.styleable.TestConfig, new String[]{"bag default"});
710 
711         config = makeClassicConfig();
712         config.setProperty(Properties.DENSITY, 150);
713         res = config.getResources();
714         checkValue(res, R.configVarying.simple, "simple default");
715         checkValue(res, R.configVarying.bag,
716                 R.styleable.TestConfig, new String[]{"bag default"});
717 
718         config = makeClassicConfig();
719         config.setProperty(Properties.DENSITY, 181);
720         res = config.getResources();
721         checkValue(res, R.configVarying.simple, "simple default");
722         checkValue(res, R.configVarying.bag,
723                 R.styleable.TestConfig, new String[]{"bag default"});
724 
725         config = makeClassicConfig();
726         config.setProperty(Properties.DENSITY, 182);
727         res = config.getResources();
728         checkValue(res, R.configVarying.simple, "simple 240dpi");
729         checkValue(res, R.configVarying.bag,
730                 R.styleable.TestConfig, new String[]{"bag 240dpi"});
731 
732         config = makeClassicConfig();
733         config.setProperty(Properties.DENSITY, 239);
734         res = config.getResources();
735         checkValue(res, R.configVarying.simple, "simple 240dpi");
736         checkValue(res, R.configVarying.bag,
737                 R.styleable.TestConfig, new String[]{"bag 240dpi"});
738 
739         config = makeClassicConfig();
740         config.setProperty(Properties.DENSITY, 490);
741         res = config.getResources();
742         checkValue(res, R.configVarying.simple, "simple 240dpi");
743         checkValue(res, R.configVarying.bag,
744                 R.styleable.TestConfig, new String[]{"bag 240dpi"});
745     }
746 
747     @MediumTest
testScreenSize()748     public void testScreenSize() throws Exception {
749         // ensure that we fall back to the best available screen size
750         // for a given configuration.
751         TotalConfig config = makeClassicConfig();
752         config.setProperty(Properties.SCREENLAYOUT, Configuration.SCREENLAYOUT_SIZE_SMALL);
753         Resources res = config.getResources();
754         checkValue(res, R.configVarying.simple, "simple small");
755         checkValue(res, R.configVarying.small, "small");
756         checkValue(res, R.configVarying.normal, "default");
757         checkValue(res, R.configVarying.large, "default");
758         checkValue(res, R.configVarying.xlarge, "default");
759 
760         config = makeClassicConfig();
761         config.setProperty(Properties.SCREENLAYOUT, Configuration.SCREENLAYOUT_SIZE_NORMAL);
762         res = config.getResources();
763         checkValue(res, R.configVarying.simple, "simple normal");
764         checkValue(res, R.configVarying.small, "default");
765         checkValue(res, R.configVarying.normal, "normal");
766         checkValue(res, R.configVarying.large, "default");
767         checkValue(res, R.configVarying.xlarge, "default");
768 
769         config = makeClassicConfig();
770         config.setProperty(Properties.SCREENLAYOUT, Configuration.SCREENLAYOUT_SIZE_LARGE);
771         res = config.getResources();
772         checkValue(res, R.configVarying.simple, "simple large");
773         checkValue(res, R.configVarying.small, "default");
774         checkValue(res, R.configVarying.normal, "normal");
775         checkValue(res, R.configVarying.large, "large");
776         checkValue(res, R.configVarying.xlarge, "default");
777 
778         config = makeClassicConfig();
779         config.setProperty(Properties.SCREENLAYOUT, Configuration.SCREENLAYOUT_SIZE_XLARGE);
780         res = config.getResources();
781         checkValue(res, R.configVarying.simple, "simple xlarge");
782         checkValue(res, R.configVarying.small, "default");
783         checkValue(res, R.configVarying.normal, "normal");
784         checkValue(res, R.configVarying.large, "large");
785         checkValue(res, R.configVarying.xlarge, "xlarge");
786     }
787 
788     @MediumTest
testNewScreenSize()789     public void testNewScreenSize() throws Exception {
790         // ensure that swNNNdp, wNNNdp, and hNNNdp are working correctly
791         // for various common screen configurations.
792         TotalConfig config = makeClassicConfig();
793         config.setProperty(Properties.SWIDTH_DP, 589);
794         config.setProperty(Properties.WIDTH_DP, 589);
795         config.setProperty(Properties.HEIGHT_DP, 500);
796         config.setProperty(Properties.ORIENTATION, Configuration.ORIENTATION_LANDSCAPE);
797         config.setProperty(Properties.SCREENLAYOUT, Configuration.SCREENLAYOUT_SIZE_LARGE);
798         Resources res = config.getResources();
799         checkValue(res, R.configVarying.simple, "simple large");
800         checkValue(res, R.configVarying.sw, "default");
801         checkValue(res, R.configVarying.w, "default");
802         checkValue(res, R.configVarying.h, "default");
803         checkValue(res, R.configVarying.wh, "default");
804 
805         config = makeClassicConfig();
806         config.setProperty(Properties.SWIDTH_DP, 590);
807         config.setProperty(Properties.WIDTH_DP, 590);
808         config.setProperty(Properties.HEIGHT_DP, 500);
809         config.setProperty(Properties.ORIENTATION, Configuration.ORIENTATION_LANDSCAPE);
810         config.setProperty(Properties.SCREENLAYOUT, Configuration.SCREENLAYOUT_SIZE_LARGE);
811         config.setProperty(Properties.DENSITY, DisplayMetrics.DENSITY_MEDIUM);
812         res = config.getResources();
813         checkValue(res, R.configVarying.simple, "simple sw590 mdpi");
814         checkValue(res, R.configVarying.sw, "590 mdpi");
815 
816         config.setProperty(Properties.DENSITY, DisplayMetrics.DENSITY_HIGH);
817         res = config.getResources();
818         checkValue(res, R.configVarying.simple, "simple sw590 hdpi");
819         checkValue(res, R.configVarying.sw, "590 hdpi");
820 
821         config.setProperty(Properties.DENSITY, DisplayMetrics.DENSITY_XHIGH);
822         res = config.getResources();
823         checkValue(res, R.configVarying.simple, "simple sw590 xhdpi");
824         checkValue(res, R.configVarying.sw, "590 xhdpi");
825 
826         config.setProperty(Properties.SWIDTH_DP, 591);
827         config.setProperty(Properties.WIDTH_DP, 591);
828         config.setProperty(Properties.DENSITY, DisplayMetrics.DENSITY_MEDIUM);
829         res = config.getResources();
830         checkValue(res, R.configVarying.simple, "simple sw591");
831         checkValue(res, R.configVarying.sw, "591");
832 
833         config.setProperty(Properties.DENSITY, DisplayMetrics.DENSITY_HIGH);
834         res = config.getResources();
835         checkValue(res, R.configVarying.simple, "simple sw591 hdpi");
836         checkValue(res, R.configVarying.sw, "591 hdpi");
837 
838         config.setProperty(Properties.DENSITY, DisplayMetrics.DENSITY_XHIGH);
839         res = config.getResources();
840         checkValue(res, R.configVarying.simple, "simple sw591 hdpi");
841         checkValue(res, R.configVarying.sw, "591 hdpi");
842 
843         config = makeClassicConfig();
844         config.setProperty(Properties.SWIDTH_DP, 480);
845         config.setProperty(Properties.WIDTH_DP, 800);
846         config.setProperty(Properties.HEIGHT_DP, 480);
847         config.setProperty(Properties.ORIENTATION, Configuration.ORIENTATION_LANDSCAPE);
848         config.setProperty(Properties.SCREENLAYOUT, Configuration.SCREENLAYOUT_SIZE_LARGE);
849         res = config.getResources();
850         checkValue(res, R.configVarying.simple, "simple w720");
851         checkValue(res, R.configVarying.sw, "default");
852         checkValue(res, R.configVarying.w, "720");
853         checkValue(res, R.configVarying.h, "default");
854         checkValue(res, R.configVarying.wh, "600");
855 
856         config = makeClassicConfig();
857         config.setProperty(Properties.SWIDTH_DP, 600);
858         config.setProperty(Properties.WIDTH_DP, 1024);
859         config.setProperty(Properties.HEIGHT_DP, 552);
860         config.setProperty(Properties.ORIENTATION, Configuration.ORIENTATION_LANDSCAPE);
861         config.setProperty(Properties.SCREENLAYOUT, Configuration.SCREENLAYOUT_SIZE_LARGE);
862         res = config.getResources();
863         checkValue(res, R.configVarying.simple, "simple sw600 land");
864         checkValue(res, R.configVarying.sw, "600 land");
865         checkValue(res, R.configVarying.w, "720");
866         checkValue(res, R.configVarying.h, "550");
867         checkValue(res, R.configVarying.wh, "600-550");
868 
869         config = makeClassicConfig();
870         config.setProperty(Properties.SWIDTH_DP, 600);
871         config.setProperty(Properties.WIDTH_DP, 600);
872         config.setProperty(Properties.HEIGHT_DP, 974);
873         config.setProperty(Properties.ORIENTATION, Configuration.ORIENTATION_PORTRAIT);
874         config.setProperty(Properties.SCREENLAYOUT, Configuration.SCREENLAYOUT_SIZE_LARGE);
875         res = config.getResources();
876         checkValue(res, R.configVarying.simple, "simple sw600");
877         checkValue(res, R.configVarying.sw, "600");
878         checkValue(res, R.configVarying.w, "600");
879         checkValue(res, R.configVarying.h, "670");
880         checkValue(res, R.configVarying.wh, "600-550");
881 
882         config = makeClassicConfig();
883         config.setProperty(Properties.SWIDTH_DP, 719);
884         config.setProperty(Properties.WIDTH_DP, 1279);
885         config.setProperty(Properties.HEIGHT_DP, 669);
886         config.setProperty(Properties.ORIENTATION, Configuration.ORIENTATION_LANDSCAPE);
887         config.setProperty(Properties.SCREENLAYOUT, Configuration.SCREENLAYOUT_SIZE_LARGE);
888         res = config.getResources();
889         checkValue(res, R.configVarying.simple, "simple sw600 land");
890         checkValue(res, R.configVarying.sw, "600 land");
891         checkValue(res, R.configVarying.w, "720");
892         checkValue(res, R.configVarying.h, "550");
893         checkValue(res, R.configVarying.wh, "600-550");
894 
895         config = makeClassicConfig();
896         config.setProperty(Properties.SWIDTH_DP, 800);
897         config.setProperty(Properties.WIDTH_DP, 1280);
898         config.setProperty(Properties.HEIGHT_DP, 672);
899         config.setProperty(Properties.ORIENTATION, Configuration.ORIENTATION_LANDSCAPE);
900         config.setProperty(Properties.SCREENLAYOUT, Configuration.SCREENLAYOUT_SIZE_XLARGE);
901         res = config.getResources();
902         checkValue(res, R.configVarying.simple, "simple sw720");
903         checkValue(res, R.configVarying.sw, "720");
904         checkValue(res, R.configVarying.w, "720");
905         checkValue(res, R.configVarying.h, "670");
906         checkValue(res, R.configVarying.wh, "720-670");
907 
908         config = makeClassicConfig();
909         config.setProperty(Properties.SWIDTH_DP, 800);
910         config.setProperty(Properties.WIDTH_DP, 720);
911         config.setProperty(Properties.HEIGHT_DP, 1230);
912         config.setProperty(Properties.ORIENTATION, Configuration.ORIENTATION_PORTRAIT);
913         config.setProperty(Properties.SCREENLAYOUT, Configuration.SCREENLAYOUT_SIZE_XLARGE);
914         res = config.getResources();
915         checkValue(res, R.configVarying.simple, "simple sw720");
916         checkValue(res, R.configVarying.sw, "720");
917         checkValue(res, R.configVarying.w, "720");
918         checkValue(res, R.configVarying.h, "670");
919         checkValue(res, R.configVarying.wh, "720-670");
920     }
921 
922 // TODO - add tests for special cases - ie, other key params seem ignored if
923 // nokeys is set
924 
925     @MediumTest
testPrecedence()926     public void testPrecedence() {
927         /**
928          * Check for precedence of resources selected when there are multiple
929          * options matching the current config.
930          */
931         TotalConfig config = makeEmptyConfig();
932         config.setProperty(Properties.HEIGHT, 640);
933         config.setProperty(Properties.WIDTH, 400);
934         Resources res = config.getResources();
935         checkValue(res, R.configVarying.simple, "simple 640x400");
936         checkValue(res, R.configVarying.bag,
937                 R.styleable.TestConfig, new String[]{"bag 640x400"});
938 
939         config.setProperty(Properties.NAVIGATION, Configuration.NAVIGATION_NONAV);
940         res = config.getResources();
941         checkValue(res, R.configVarying.simple, "simple nonav");
942         checkValue(res, R.configVarying.bag,
943                 R.styleable.TestConfig, new String[]{"bag nonav"});
944 
945         config.setProperty(Properties.KEYBOARD, Configuration.KEYBOARD_NOKEYS);
946         res = config.getResources();
947         checkValue(res, R.configVarying.simple, "simple nokeys");
948         checkValue(res, R.configVarying.bag,
949                 R.styleable.TestConfig, new String[]{"bag nokeys"});
950 
951         config.setProperty(Properties.KEYBOARDHIDDEN, Configuration.KEYBOARDHIDDEN_NO);
952         res = config.getResources();
953         checkValue(res, R.configVarying.simple, "simple keysexposed");
954         checkValue(res, R.configVarying.bag,
955                 R.styleable.TestConfig, new String[]{"bag keysexposed"});
956 
957         config.setProperty(Properties.TOUCHSCREEN, Configuration.TOUCHSCREEN_NOTOUCH);
958         res = config.getResources();
959         checkValue(res, R.configVarying.simple, "simple notouch");
960         checkValue(res, R.configVarying.bag,
961                 R.styleable.TestConfig, new String[]{"bag notouch"});
962 
963         config.setProperty(Properties.DENSITY, 240);
964         res = config.getResources();
965         checkValue(res, R.configVarying.simple, "simple 240dpi");
966         checkValue(res, R.configVarying.bag,
967                 R.styleable.TestConfig, new String[]{"bag 240dpi"});
968 
969         config.setProperty(Properties.ORIENTATION, Configuration.ORIENTATION_LANDSCAPE);
970         res = config.getResources();
971         checkValue(res, R.configVarying.simple, "simple landscape");
972         checkValue(res, R.configVarying.bag,
973                 R.styleable.TestConfig, new String[]{"bag landscape"});
974 
975         config.setProperty(Properties.COLOR_MODE, Configuration.COLOR_MODE_HDR_YES);
976         res = config.getResources();
977         checkValue(res, R.configVarying.simple, "simple hdr");
978         checkValue(res, R.configVarying.bag,
979                 R.styleable.TestConfig, new String[]{"bag hdr"});
980 
981         config.setProperty(Properties.COLOR_MODE, Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES);
982         res = config.getResources();
983         checkValue(res, R.configVarying.simple, "simple widecg");
984         checkValue(res, R.configVarying.bag,
985                 R.styleable.TestConfig, new String[]{"bag widecg"});
986 
987         config.setProperty(Properties.SCREENLAYOUT, Configuration.SCREENLAYOUT_SIZE_XLARGE);
988         res = config.getResources();
989         checkValue(res, R.configVarying.simple, "simple xlarge");
990         checkValue(res, R.configVarying.bag,
991                 R.styleable.TestConfig, new String[]{"bag xlarge"});
992 
993         config.setProperty(Properties.HEIGHT_DP, 670);
994         res = config.getResources();
995         checkValue(res, R.configVarying.simple, "simple h670");
996         checkValue(res, R.configVarying.bag,
997                 R.styleable.TestConfig, new String[]{"bag h670"});
998 
999         config.setProperty(Properties.WIDTH_DP, 720);
1000         res = config.getResources();
1001         checkValue(res, R.configVarying.simple, "simple 720-670");
1002         checkValue(res, R.configVarying.bag,
1003                 R.styleable.TestConfig, new String[]{"bag 720-670"});
1004 
1005         config.setProperty(Properties.SWIDTH_DP, 720);
1006         res = config.getResources();
1007         checkValue(res, R.configVarying.simple, "simple sw720");
1008         checkValue(res, R.configVarying.bag,
1009                 R.styleable.TestConfig, new String[]{"bag sw720"});
1010 
1011         config.setProperty(Properties.LANGUAGE, "xx");
1012         config.setProperty(Properties.COUNTRY, "YY");
1013         res = config.getResources();
1014         checkValue(res, R.configVarying.simple, "simple xx-rYY");
1015         checkValue(res, R.configVarying.bag,
1016                 R.styleable.TestConfig, new String[]{"bag xx-rYY"});
1017 
1018         config.setProperty(Properties.MCC, 111);
1019         res = config.getResources();
1020         checkValue(res, R.configVarying.simple, "simple mcc111 xx-rYY");
1021         checkValue(res, R.configVarying.bag,
1022                 R.styleable.TestConfig, new String[]{"bag mcc111 xx-rYY"});
1023 
1024         config.setProperty(Properties.MNC, 222);
1025         res = config.getResources();
1026         checkValue(res, R.configVarying.simple, "simple mcc111 mnc222");
1027         checkValue(res, R.configVarying.bag,
1028                 R.styleable.TestConfig, new String[]{"bag mcc111 mnc222"});
1029     }
1030 
1031     @MediumTest
testCombinations()1032     public void testCombinations() {
1033         /**
1034          * Verify that in cases of ties, the specific ordering is followed
1035          */
1036 
1037         /**
1038          * Precidence order: mcc, mnc, locale, swdp, wdp, hdp, screenlayout-size,
1039          * screenlayout-long, orientation, density,
1040          * touchscreen, hidden, keyboard, navigation, width-height
1041          */
1042 
1043         /**
1044          * verify mcc trumps mnc.  Have 110-xx, 220-xx but no 110-220
1045          * so which is selected?  Should be mcc110-xx.
1046          */
1047         TotalConfig config = makeClassicConfig();
1048         config.setProperty(Properties.MCC, 110);
1049         config.setProperty(Properties.MNC, 220);
1050         config.setProperty(Properties.LANGUAGE, "xx");
1051         Resources res = config.getResources();
1052         checkValue(res, R.configVarying.simple, "simple mcc110 xx");
1053         checkValue(res, R.configVarying.bag,
1054                 R.styleable.TestConfig, new String[]{"bag mcc110 xx"});
1055 
1056         /* full A + B + C doesn't exist.  Do we get A + C or B + C?
1057          */
1058         config = makeClassicConfig();
1059         config.setProperty(Properties.MCC, 111);
1060         config.setProperty(Properties.MNC, 222);
1061         config.setProperty(Properties.LANGUAGE, "xx");
1062         res = config.getResources();
1063         checkValue(res, R.configVarying.simple, "simple mcc111 mnc222");
1064         checkValue(res, R.configVarying.bag,
1065                 R.styleable.TestConfig, new String[]{"bag mcc111 mnc222"});
1066 
1067         config = makeClassicConfig();
1068         config.setProperty(Properties.MNC, 222);
1069         config.setProperty(Properties.LANGUAGE, "xx");
1070         config.setProperty(Properties.ORIENTATION,
1071                 Configuration.ORIENTATION_SQUARE);
1072         res = config.getResources();
1073         checkValue(res, R.configVarying.simple, "simple mnc222 xx");
1074         checkValue(res, R.configVarying.bag,
1075                 R.styleable.TestConfig, new String[]{"bag mnc222 xx"});
1076 
1077         config = makeClassicConfig();
1078         config.setProperty(Properties.LANGUAGE, "xx");
1079         config.setProperty(Properties.ORIENTATION,
1080                 Configuration.ORIENTATION_SQUARE);
1081         config.setProperty(Properties.DENSITY, 32);
1082         res = config.getResources();
1083         checkValue(res, R.configVarying.simple, "simple xx square");
1084         checkValue(res, R.configVarying.bag,
1085                 R.styleable.TestConfig, new String[]{"bag xx square"});
1086 
1087         /**
1088          * Verify that proper strings are found for multiple-selectivity case
1089          * (ie, a string set for locale and mcc is found only when both are
1090          * true).
1091          */
1092         config = makeClassicConfig();
1093         config.setProperty(Properties.LANGUAGE, "xx");
1094         config.setProperty(Properties.COUNTRY, "YY");
1095         config.setProperty(Properties.MCC, 111);
1096         res = config.getResources();
1097         checkValue(res, R.configVarying.simple, "simple mcc111 xx-rYY");
1098         checkValue(res, R.configVarying.bag, R.styleable.TestConfig,
1099                 new String[] { "bag mcc111 xx-rYY" });
1100 
1101         config = makeClassicConfig();
1102         config.setProperty(Properties.LANGUAGE, "xx");
1103         config.setProperty(Properties.COUNTRY, "YY");
1104         config.setProperty(Properties.MCC, 333);
1105         res = config.getResources();
1106         checkValue(res, R.configVarying.simple, "simple xx-rYY");
1107         checkValue(res, R.configVarying.bag,
1108                 R.styleable.TestConfig, new String[] { "bag xx-rYY" });
1109 
1110         config = makeClassicConfig();
1111         config.setProperty(Properties.MNC, 333);
1112         res = config.getResources();
1113         checkValue(res, R.configVarying.simple, "simple default");
1114         checkValue(res, R.configVarying.bag,
1115                 R.styleable.TestConfig, new String[]{"bag default"});
1116 
1117         config = makeClassicConfig();
1118         config.setProperty(Properties.ORIENTATION,
1119                 Configuration.ORIENTATION_SQUARE);
1120         config.setProperty(Properties.DENSITY, 32);
1121         config.setProperty(Properties.TOUCHSCREEN,
1122                 Configuration.TOUCHSCREEN_STYLUS);
1123         res = config.getResources();
1124         checkValue(res, R.configVarying.simple, "simple square 32dpi");
1125         checkValue(res, R.configVarying.bag,
1126                 R.styleable.TestConfig, new String[]{"bag square 32dpi"});
1127 
1128         config = makeClassicConfig();
1129         config.setProperty(Properties.DENSITY, 32);
1130         config.setProperty(Properties.TOUCHSCREEN,
1131                 Configuration.TOUCHSCREEN_STYLUS);
1132         config.setProperty(Properties.KEYBOARDHIDDEN,
1133                 Configuration.KEYBOARDHIDDEN_NO);
1134         res = config.getResources();
1135         checkValue(res, R.configVarying.simple, "simple 32dpi stylus");
1136         checkValue(res, R.configVarying.bag,
1137                 R.styleable.TestConfig, new String[]{"bag 32dpi stylus"});
1138 
1139         config = makeClassicConfig();
1140         config.setProperty(Properties.TOUCHSCREEN,
1141                 Configuration.TOUCHSCREEN_STYLUS);
1142         config.setProperty(Properties.KEYBOARDHIDDEN,
1143                 Configuration.KEYBOARDHIDDEN_NO);
1144         config.setProperty(Properties.KEYBOARD, Configuration.KEYBOARD_12KEY);
1145         res = config.getResources();
1146         checkValue(res, R.configVarying.simple, "simple stylus keysexposed");
1147         checkValue(res, R.configVarying.bag,
1148                 R.styleable.TestConfig, new String[]{"bag stylus keysexposed"});
1149 
1150         config = makeClassicConfig();
1151         config.setProperty(Properties.KEYBOARDHIDDEN,
1152                 Configuration.KEYBOARDHIDDEN_NO);
1153         config.setProperty(Properties.KEYBOARD, Configuration.KEYBOARD_12KEY);
1154         config.setProperty(Properties.NAVIGATION,
1155                 Configuration.NAVIGATION_DPAD);
1156         res = config.getResources();
1157         checkValue(res, R.configVarying.simple, "simple keysexposed 12key");
1158         checkValue(res, R.configVarying.bag,
1159                 R.styleable.TestConfig, new String[]{"bag keysexposed 12key"});
1160 
1161         config = makeClassicConfig();
1162         config.setProperty(Properties.KEYBOARD, Configuration.KEYBOARD_12KEY);
1163         config.setProperty(Properties.NAVIGATION,
1164                 Configuration.NAVIGATION_DPAD);
1165         config.setProperty(Properties.HEIGHT, 63);
1166         config.setProperty(Properties.WIDTH, 57);
1167         res = config.getResources();
1168         checkValue(res, R.configVarying.simple, "simple 12key dpad");
1169         checkValue(res, R.configVarying.bag,
1170                 R.styleable.TestConfig, new String[]{"bag 12key dpad"});
1171 
1172         config = makeClassicConfig();
1173         config.setProperty(Properties.NAVIGATION,
1174                 Configuration.NAVIGATION_DPAD);
1175         config.setProperty(Properties.HEIGHT, 640);
1176         config.setProperty(Properties.WIDTH, 400);
1177         res = config.getResources();
1178         checkValue(res, R.configVarying.simple, "simple dpad 63x57");
1179         checkValue(res, R.configVarying.bag,
1180                 R.styleable.TestConfig, new String[]{"bag dpad 63x57"});
1181     }
1182 
1183     @MediumTest
testVersions()1184     public void testVersions() {
1185         // Check that we get the most recent resources that are <= our
1186         // current version.  Note the special version adjustment, so that
1187         // during development the resource version is incremented to the
1188         // next one.
1189         int vers = android.os.Build.VERSION.SDK_INT;
1190         if (!"REL".equals(android.os.Build.VERSION.CODENAME)) {
1191             vers++;
1192         }
1193         String expected = "v" + vers + "cur";
1194         assertEquals(expected, mContext.getResources().getString(R.string.version_cur));
1195         assertEquals("base",  mContext.getResources().getString(R.string.version_old));
1196         assertEquals("v3",  mContext.getResources().getString(R.string.version_v3));
1197     }
1198 
1199     @MediumTest
testNormalLocales()1200     public void testNormalLocales() {
1201         Resources res;
1202         TotalConfig config = makeClassicConfig();
1203         // Hebrew
1204         config.setProperty(Properties.LANGUAGE, "iw");
1205         res = config.getResources();
1206         checkValue(res, R.configVarying.simple, "simple iw");
1207         checkValue(res, R.configVarying.bag,
1208                 R.styleable.TestConfig, new String[]{"bag iw"});
1209 
1210         // Hebrew for Israel
1211         config.setProperty(Properties.LANGUAGE, "iw");
1212         config.setProperty(Properties.COUNTRY, "IL");
1213         res = config.getResources();
1214         checkValue(res, R.configVarying.simple, "simple iw IL");
1215         checkValue(res, R.configVarying.bag,
1216                 R.styleable.TestConfig, new String[]{"bag iw IL"});
1217 
1218         config = makeClassicConfig();
1219         // Macedonian
1220         config.setProperty(Properties.LANGUAGE, "mk");
1221         res = config.getResources();
1222         checkValue(res, R.configVarying.simple, "simple mk");
1223         checkValue(res, R.configVarying.bag,
1224                 R.styleable.TestConfig, new String[]{"bag mk"});
1225 
1226         // Macedonian for Macedonia
1227         config.setProperty(Properties.LANGUAGE, "mk");
1228         config.setProperty(Properties.COUNTRY, "MK");
1229         res = config.getResources();
1230         checkValue(res, R.configVarying.simple, "simple mk MK");
1231         checkValue(res, R.configVarying.bag,
1232                 R.styleable.TestConfig, new String[]{"bag mk MK"});
1233     }
1234 
1235     @MediumTest
testExtendedLocales()1236     public void testExtendedLocales() {
1237         TotalConfig config = makeClassicConfig();
1238         // BCP 47 Locale kok
1239         config.setProperty(Properties.LANGUAGE, "kok");
1240         Resources res = config.getResources();
1241         checkValue(res, R.configVarying.simple, "simple kok");
1242         checkValue(res, R.configVarying.bag,
1243                 R.styleable.TestConfig, new String[]{"bag kok"});
1244 
1245         // BCP 47 Locale kok-IN
1246         config.setProperty(Properties.COUNTRY, "IN");
1247         res = config.getResources();
1248         checkValue(res, R.configVarying.simple, "simple kok IN");
1249         checkValue(res, R.configVarying.bag,
1250                 R.styleable.TestConfig, new String[]{"bag kok IN"});
1251 
1252         // BCP 47 Locale kok-419
1253         config.setProperty(Properties.COUNTRY, "419");
1254         res = config.getResources();
1255         checkValue(res, R.configVarying.simple, "simple kok 419");
1256         checkValue(res, R.configVarying.bag,
1257                 R.styleable.TestConfig, new String[]{"bag kok 419"});
1258 
1259 
1260         // BCP 47 Locale kok-419-VARIANT
1261         config.setProperty(Properties.VARIANT, "VARIANT");
1262         res = config.getResources();
1263         checkValue(res, R.configVarying.simple, "simple kok 419 VARIANT");
1264         checkValue(res, R.configVarying.bag,
1265                 R.styleable.TestConfig, new String[]{"bag kok 419 VARIANT"});
1266 
1267         // BCP 47 Locale kok-Knda
1268         config = makeClassicConfig();
1269         config.setProperty(Properties.LANGUAGE, "kok");
1270         config.setProperty(Properties.SCRIPT, "Knda");
1271         res = config.getResources();
1272         checkValue(res, R.configVarying.simple, "simple kok Knda");
1273         checkValue(res, R.configVarying.bag,
1274                 R.styleable.TestConfig, new String[]{"bag kok Knda"});
1275 
1276         // BCP 47 Locale kok-Knda-419
1277         config.setProperty(Properties.COUNTRY, "419");
1278         res = config.getResources();
1279         checkValue(res, R.configVarying.simple, "simple kok Knda 419");
1280         checkValue(res, R.configVarying.bag,
1281                 R.styleable.TestConfig, new String[]{"bag kok Knda 419"});
1282 
1283         // BCP 47 Locale kok-Knda-419-VARIANT
1284         config.setProperty(Properties.VARIANT, "VARIANT");
1285         res = config.getResources();
1286         checkValue(res, R.configVarying.simple, "simple kok Knda 419 VARIANT");
1287         checkValue(res, R.configVarying.bag,
1288                 R.styleable.TestConfig, new String[]{"bag kok Knda 419 VARIANT"});
1289 
1290         // BCP 47 Locale kok-VARIANT
1291         config = makeClassicConfig();
1292         config.setProperty(Properties.LANGUAGE, "kok");
1293         config.setProperty(Properties.VARIANT, "VARIANT");
1294         res = config.getResources();
1295         checkValue(res, R.configVarying.simple, "simple kok VARIANT");
1296         checkValue(res, R.configVarying.bag,
1297                 R.styleable.TestConfig, new String[]{"bag kok VARIANT"});
1298     }
1299 
1300     @MediumTest
testTlAndFilConversion()1301     public void testTlAndFilConversion() {
1302         TotalConfig config = makeClassicConfig();
1303 
1304         // Ensure that "fil" is mapped to "tl" correctly.
1305         config.setProperty(Properties.LANGUAGE, "fil");
1306         config.setProperty(Properties.COUNTRY, "US");
1307         Resources res = config.getResources();
1308         checkValue(res, R.configVarying.simple, "simple fil");  // We have this resource in 'fil'
1309         checkValue(res, R.configVarying.bag,
1310                 R.styleable.TestConfig, new String[] { "bag tl" });  // But this comes from 'tl'
1311 
1312         // Ensure that "fil-PH" is mapped to "tl-PH" correctly.
1313         config = makeClassicConfig();
1314         config.setProperty(Properties.LANGUAGE, "fil");
1315         config.setProperty(Properties.COUNTRY, "PH");
1316         res = config.getResources();
1317         checkValue(res, R.configVarying.simple, "simple tl PH");
1318         checkValue(res, R.configVarying.bag,
1319                 R.styleable.TestConfig, new String[] { "bag tl PH" });
1320 
1321         // Ensure that "fil-SA" works with no "tl" version.
1322         config = makeClassicConfig();
1323         config.setProperty(Properties.LANGUAGE, "fil");
1324         config.setProperty(Properties.COUNTRY, "SA");
1325         res = config.getResources();
1326         checkValue(res, R.configVarying.simple, "simple fil");  // This comes from 'fil'
1327         checkValue(res, R.configVarying.bag,
1328                 R.styleable.TestConfig, new String[] { "bag fil SA" });  // And this from 'fil-SA'
1329 
1330         // Ensure that "tlh" is not mistakenly treated as a "tl" variant.
1331         config = makeClassicConfig();
1332         config.setProperty(Properties.LANGUAGE, "tlh");
1333         config.setProperty(Properties.COUNTRY, "US");
1334         res = config.getResources();
1335         checkValue(res, R.configVarying.simple, "simple tlh");
1336         checkValue(res, R.configVarying.bag,
1337                 R.styleable.TestConfig, new String[] { "bag tlh" });
1338 
1339         config = makeClassicConfig();
1340         config.setProperty(Properties.LANGUAGE, "tgl");
1341         res = config.getResources();
1342         checkValue(res, R.configVarying.simple, "simple tgl");
1343         checkValue(res, R.configVarying.bag,
1344                 R.styleable.TestConfig, new String[] { "bag tgl" });
1345 
1346         config = makeClassicConfig();
1347         config.setProperty(Properties.LANGUAGE, "tgl");
1348         config.setProperty(Properties.COUNTRY, "PH");
1349         res = config.getResources();
1350         checkValue(res, R.configVarying.simple, "simple tgl PH");
1351         checkValue(res, R.configVarying.bag,
1352                 R.styleable.TestConfig, new String[] { "bag tgl PH" });
1353     }
1354 
1355     @MediumTest
testGetLocalesConvertsTlToFil()1356     public void testGetLocalesConvertsTlToFil() {
1357         TotalConfig config = makeClassicConfig();
1358 
1359         // Check that the list of locales doesn't contain any of the
1360         // "tl" variants. They should've been converted to "fil"
1361         // locales.
1362         AssetManager am = config.getResources().getAssets();
1363         String[] locales = am.getLocales();
1364         final List<String> tlLocales = new ArrayList<String>(4);
1365         final List<String> filLocales = new ArrayList<String>(4);
1366         for (String locale : locales) {
1367             if (locale.startsWith("tl-") || locale.equals("tl")) {
1368                 tlLocales.add(locale);
1369             }
1370 
1371             if (locale.startsWith("fil-") || locale.equals("fil")) {
1372                 filLocales.add(locale);
1373             }
1374         }
1375 
1376         assertEquals(0, tlLocales.size());
1377         assertEquals(3, filLocales.size());
1378         assertTrue(filLocales.contains("fil"));
1379         assertTrue(filLocales.contains("fil-PH"));
1380         assertTrue(filLocales.contains("fil-SA"));
1381     }
1382 }
1383