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