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