summaryrefslogtreecommitdiffstats
path: root/tests/AndroidTests/src/com/android/unit_tests/content/ConfigTest.java
blob: a63885ded19dafdb0b7c20072fcbca178c1a3065 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.unit_tests.content;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
import com.android.unit_tests.R;

import java.util.Locale;

public class ConfigTest extends AndroidTestCase {

    private static void checkValue(Resources res, int resId, String expectedValue) {
        try {
            String actual = res.getString(resId);
            assertNotNull("Returned wrong configuration-based simple value: expected <nothing>, got '"
                    + actual + "' from resource 0x"
                    + Integer.toHexString(resId),
                    expectedValue);
            assertEquals("Returned wrong configuration-based simple value: expected "
                    + expectedValue + ", got '" + actual + "' from resource 0x"
                    + Integer.toHexString(resId),
                    expectedValue, actual);
        } catch (Resources.NotFoundException e) {
            assertNull("Resource not found for configuration-based simple value: expecting \""
                    + expectedValue + "\"",
                    expectedValue);
        }
    }

    private static void checkValue(Resources res, int resId,
            int[] styleable, String[] expectedValues) {
        Resources.Theme theme = res.newTheme();
        TypedArray sa = theme.obtainStyledAttributes(resId, styleable);
        for (int i = 0; i < styleable.length; i++) {
            String actual = sa.getString(i);
            assertEquals("Returned wrong configuration-based style value: expected "
                    + expectedValues[i] + ", got '" + actual + "' from attr "
                    + i + " of resource 0x" + Integer.toHexString(resId),
                    actual, expectedValues[i]);
        }
        sa.recycle();
    }

    public Resources getResources(Configuration config,
            int mcc, int mnc, int touchscreen, int keyboard, int keysHidden,
            int navigation, int width, int height) {
        AssetManager assmgr = new AssetManager();
        assmgr.addAssetPath(mContext.getPackageResourcePath());
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        Display d = wm.getDefaultDisplay();
        d.getMetrics(metrics);
        config.mcc = mcc;
        config.mnc = mnc;
        config.touchscreen = touchscreen;
        config.keyboard = keyboard;
        config.keyboardHidden = keysHidden;
        config.navigation = navigation;
        metrics.widthPixels = width;
        metrics.heightPixels = height;
        return new Resources(assmgr, metrics, config);
    }

    private static void checkPair(Resources res, int[] notResIds,
            int simpleRes, String simpleString,
            int bagRes, String bagString) {
        boolean willHave = true;
        if (notResIds != null) {
            for (int i : notResIds) {
                if (i == simpleRes) {
                    willHave = false;
                    break;
                }
            }
        }
        checkValue(res, simpleRes, willHave ? simpleString : null);
        checkValue(res, bagRes, R.styleable.TestConfig,
                new String[]{willHave ? bagString : null});
    }

    private static void checkAllExcept(Resources res, int[] notResIds) {
        checkPair(res, notResIds,
                R.configVarying.simple_default, "only simple default",
                R.configVarying.bag_default, "only bag default");
        checkPair(res, notResIds,
                R.configVarying.simple_mcc111, "only simple mcc111",
                R.configVarying.bag_mcc111, "only bag mcc111");
        checkPair(res, notResIds,
                R.configVarying.simple_mnc222, "only simple mnc222",
                R.configVarying.bag_mnc222, "only bag mnc222");
        checkPair(res, notResIds,
                R.configVarying.simple_xx, "only simple xx",
                R.configVarying.bag_xx, "only bag xx");
        checkPair(res, notResIds,
                R.configVarying.simple_xx_rYY, "only simple xx_rYY",
                R.configVarying.bag_xx_rYY, "only bag xx_rYY");
        checkPair(res, notResIds,
                R.configVarying.simple_notouch, "only simple notouch",
                R.configVarying.bag_notouch, "only bag notouch");
        checkPair(res, notResIds,
                R.configVarying.simple_finger, "only simple finger",
                R.configVarying.bag_finger, "only bag finger");
        checkPair(res, notResIds,
                R.configVarying.simple_stylus, "only simple stylus",
                R.configVarying.bag_stylus, "only bag stylus");
        checkPair(res, notResIds,
                R.configVarying.simple_12key, "only simple 12key",
                R.configVarying.bag_12key, "only bag 12key");
        checkPair(res, notResIds,
                R.configVarying.simple_320x200, "only simple 320x200",
                R.configVarying.bag_320x200, "only bag 320x200");
        checkPair(res, notResIds,
                R.configVarying.simple_480x320, "only simple 480x320",
                R.configVarying.bag_480x320, "only bag 480x320");
    }
    
    @SmallTest
    public void testDefaultNavigationMethod() throws Exception {
        assertEquals(mContext.getResources().getConfiguration().navigation, 
                Configuration.NAVIGATION_TRACKBALL);
    }

    @SmallTest
    public void testAllConfigs() throws Exception {
        /**
         * Test a resource that contains a value for each possible single
         * configuration value.
         */
        Configuration config = new Configuration();
        Resources res = getResources(config, 0, 0, 0, 0, 0, 0, 0, 0);
        checkValue(res, R.configVarying.simple, "simple default");
        checkValue(res, R.configVarying.bag,
                R.styleable.TestConfig, new String[]{"bag default"});

        config.locale = new Locale("xx");
        res = getResources(config, 0, 0, 0, 0, 0, 0, 0, 0);
        checkValue(res, R.configVarying.simple, "simple xx");
        checkValue(res, R.configVarying.bag,
                R.styleable.TestConfig, new String[]{"bag xx"});

        config.locale = new Locale("xx", "YY");
        res = getResources(config, 0, 0, 0, 0, 0, 0, 0, 0);
        checkValue(res, R.configVarying.simple, "simple xx-rYY");
        checkValue(res, R.configVarying.bag,
                R.styleable.TestConfig, new String[]{"bag xx-rYY"});

        config = new Configuration();
        res = getResources(config, 111, 0, 0, 0, 0, 0, 0, 0);
        checkValue(res, R.configVarying.simple, "simple mcc111");
        checkValue(res, R.configVarying.bag,
                R.styleable.TestConfig, new String[]{"bag mcc111"});

        res = getResources(config, 0, 222, 0, 0, 0, 0, 0, 0);
        checkValue(res, R.configVarying.simple, "simple mnc222");
        checkValue(res, R.configVarying.bag,
                R.styleable.TestConfig, new String[]{"bag mnc222"});

        res = getResources(config, 0, 0, Configuration.TOUCHSCREEN_NOTOUCH, 0, 0, 0, 0, 0);
        checkValue(res, R.configVarying.simple, "simple notouch");
        checkValue(res, R.configVarying.bag,
                R.styleable.TestConfig, new String[]{"bag notouch"});

        res = getResources(config, 0, 0, Configuration.TOUCHSCREEN_FINGER, 0, 0, 0, 0, 0);
        checkValue(res, R.configVarying.simple, "simple finger");
        checkValue(res, R.configVarying.bag,
                R.styleable.TestConfig, new String[]{"bag finger"});

        res = getResources(config, 0, 0, Configuration.TOUCHSCREEN_STYLUS, 0, 0, 0, 0, 0);
        checkValue(res, R.configVarying.simple, "simple stylus");
        checkValue(res, R.configVarying.bag,
                R.styleable.TestConfig, new String[]{"bag stylus"});

        res = getResources(config, 0, 0, 0, Configuration.KEYBOARD_NOKEYS, 0, 0, 0, 0);
        checkValue(res, R.configVarying.simple, "simple nokeys");
        checkValue(res, R.configVarying.bag,
                R.styleable.TestConfig, new String[]{"bag nokeys"});

        res = getResources(config, 0, 0, 0, Configuration.KEYBOARD_QWERTY, 0, 0, 0, 0);
        checkValue(res, R.configVarying.simple, "simple qwerty");
        checkValue(res, R.configVarying.bag,
                R.styleable.TestConfig, new String[]{"bag qwerty"});

        res = getResources(config, 0, 0, 0, Configuration.KEYBOARD_12KEY, 0, 0, 0, 0);
        checkValue(res, R.configVarying.simple, "simple 12key");
        checkValue(res, R.configVarying.bag,
                R.styleable.TestConfig, new String[]{"bag 12key"});

        res = getResources(config, 0, 0, 0, 0, Configuration.KEYBOARDHIDDEN_YES, 0, 0, 0);
        checkValue(res, R.configVarying.simple, "simple keyshidden");
        checkValue(res, R.configVarying.bag,
                R.styleable.TestConfig, new String[]{"bag keyshidden"});

        res = getResources(config, 0, 0, 0, 0, Configuration.KEYBOARDHIDDEN_NO, 0, 0, 0);
        checkValue(res, R.configVarying.simple, "simple keysexposed");
        checkValue(res, R.configVarying.bag,
                R.styleable.TestConfig, new String[]{"bag keysexposed"});

        res = getResources(config, 0, 0, 0, 0, 0, Configuration.NAVIGATION_NONAV, 0, 0);
        checkValue(res, R.configVarying.simple, "simple nonav");
        checkValue(res, R.configVarying.bag,
                R.styleable.TestConfig, new String[]{"bag nonav"});

        res = getResources(config, 0, 0, 0, 0, 0, Configuration.NAVIGATION_DPAD, 0, 0);
        checkValue(res, R.configVarying.simple, "simple dpad");
        checkValue(res, R.configVarying.bag,
                R.styleable.TestConfig, new String[]{"bag dpad"});

        res = getResources(config, 0, 0, 0, 0, 0, Configuration.NAVIGATION_TRACKBALL, 0, 0);
        checkValue(res, R.configVarying.simple, "simple trackball");
        checkValue(res, R.configVarying.bag,
                R.styleable.TestConfig, new String[]{"bag trackball"});

        res = getResources(config, 0, 0, 0, 0, 0, Configuration.NAVIGATION_WHEEL, 0, 0);
        checkValue(res, R.configVarying.simple, "simple wheel");
        checkValue(res, R.configVarying.bag,
                R.styleable.TestConfig, new String[]{"bag wheel"});

        res = getResources(config, 0, 0, 0, 0, 0, 0, 320, 200);
        checkValue(res, R.configVarying.simple, "simple 320x200");
        checkValue(res, R.configVarying.bag,
                R.styleable.TestConfig, new String[]{"bag 320x200"});

        res = getResources(config, 0, 0, 0, 0, 0, 0, 480, 320);
        checkValue(res, R.configVarying.simple, "simple 480x320");
        checkValue(res, R.configVarying.bag,
                R.styleable.TestConfig, new String[]{"bag 480x320"});
    }

    @MediumTest
    public void testSingleConfig() throws Exception {
        /**
         * Test resources that contain a value for only one possible configuration
         * value.  XXX This is not yet complete.
         */
        Configuration config = new Configuration();
        Resources res = getResources(config, 0, 0, 0, 0, 0, 0, 0, 0);
        checkAllExcept(res, new int[]{
                R.configVarying.simple_xx,
                R.configVarying.simple_xx_rYY});

        config.locale = new Locale("xx");
        res = getResources(config, 0, 0, 0, 0, 0, 0, 0, 0);
        checkAllExcept(res, null);

        config.locale = new Locale("xx", "YY");
        res = getResources(config, 0, 0, 0, 0, 0, 0, 0, 0);
        checkAllExcept(res, null);

        config.locale = new Locale("xx", "ZZ");
        res = getResources(config, 0, 0, 0, 0, 0, 0, 0, 0);
        checkAllExcept(res, new int[]{R.configVarying.simple_xx_rYY});

        config = new Configuration();
        res = getResources(config, 111, 0, 0, 0, 0, 0, 0, 0);
        checkAllExcept(res, new int[]{
                R.configVarying.simple_xx,
                R.configVarying.simple_xx_rYY});

        res = getResources(config, 0, 222, 0, 0, 0, 0, 0, 0);
        checkAllExcept(res, new int[]{
                R.configVarying.simple_xx,
                R.configVarying.simple_xx_rYY});

        res = getResources(config, 0, 0, Configuration.TOUCHSCREEN_NOTOUCH, 0, 0, 0, 0, 0);
        checkAllExcept(res, new int[]{
                R.configVarying.simple_xx,
                R.configVarying.simple_xx_rYY,
                R.configVarying.simple_finger,
                R.configVarying.simple_stylus});

        res = getResources(config, 0, 0, Configuration.TOUCHSCREEN_FINGER, 0, 0, 0, 0, 0);
        checkAllExcept(res, new int[]{
                R.configVarying.simple_xx,
                R.configVarying.simple_xx_rYY,
                R.configVarying.simple_notouch,
                R.configVarying.simple_stylus});

        res = getResources(config, 0, 0, Configuration.TOUCHSCREEN_STYLUS, 0, 0, 0, 0, 0);
        checkAllExcept(res, new int[]{
                R.configVarying.simple_xx,
                R.configVarying.simple_xx_rYY,
                R.configVarying.simple_notouch,
                R.configVarying.simple_finger});

        res = getResources(config, 0, 0, 0, Configuration.KEYBOARD_12KEY, 0, 0, 0, 0);
        checkAllExcept(res, new int[]{
                R.configVarying.simple_xx,
                R.configVarying.simple_xx_rYY});

        res = getResources(config, 0, 0, 0, 0, 0, 0, 320, 200);
        checkAllExcept(res, new int[]{
                R.configVarying.simple_xx,
                R.configVarying.simple_xx_rYY,
                R.configVarying.simple_480x320});

        res = getResources(config, 0, 0, 0, 0, 0, 0, 480, 320);
        checkAllExcept(res, new int[]{
                R.configVarying.simple_xx,
                R.configVarying.simple_xx_rYY,
                R.configVarying.simple_320x200});
    }
}