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