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
|
/*
* Copyright (C) 2014 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.
*/
#include <androidfw/ResourceTypes.h>
#include <utils/Log.h>
#include <utils/String8.h>
#include <gtest/gtest.h>
namespace android {
TEST(ResourceTypesTest, ResourceConfig_packAndUnpack2LetterLanguage) {
ResTable_config config;
config.packLanguage("en");
EXPECT_EQ('e', config.language[0]);
EXPECT_EQ('n', config.language[1]);
char out[4] = { 1, 1, 1, 1};
config.unpackLanguage(out);
EXPECT_EQ('e', out[0]);
EXPECT_EQ('n', out[1]);
EXPECT_EQ(0, out[2]);
EXPECT_EQ(0, out[3]);
memset(out, 1, sizeof(out));
config.locale = 0;
config.unpackLanguage(out);
EXPECT_EQ(0, out[0]);
EXPECT_EQ(0, out[1]);
EXPECT_EQ(0, out[2]);
EXPECT_EQ(0, out[3]);
}
TEST(ResourceTypesTest, ResourceConfig_packAndUnpack2LetterRegion) {
ResTable_config config;
config.packRegion("US");
EXPECT_EQ('U', config.country[0]);
EXPECT_EQ('S', config.country[1]);
char out[4] = { 1, 1, 1, 1};
config.unpackRegion(out);
EXPECT_EQ('U', out[0]);
EXPECT_EQ('S', out[1]);
EXPECT_EQ(0, out[2]);
EXPECT_EQ(0, out[3]);
}
TEST(ResourceTypesTest, ResourceConfig_packAndUnpack3LetterLanguage) {
ResTable_config config;
config.packLanguage("eng");
// 1-00110-01 101-00100
EXPECT_EQ(0x99, config.language[0]);
EXPECT_EQ(0xa4, config.language[1]);
char out[4] = { 1, 1, 1, 1};
config.unpackLanguage(out);
EXPECT_EQ('e', out[0]);
EXPECT_EQ('n', out[1]);
EXPECT_EQ('g', out[2]);
EXPECT_EQ(0, out[3]);
}
TEST(ResourceTypesTest, ResourceConfig_packAndUnpack3LetterRegion) {
ResTable_config config;
config.packRegion("419");
char out[4] = { 1, 1, 1, 1};
config.unpackRegion(out);
EXPECT_EQ('4', out[0]);
EXPECT_EQ('1', out[1]);
EXPECT_EQ('9', out[2]);
}
/* static */ void fillIn(const char* lang, const char* country,
const char* script, const char* variant, ResTable_config* out) {
memset(out, 0, sizeof(ResTable_config));
if (lang != NULL) {
out->packLanguage(lang);
}
if (country != NULL) {
out->packRegion(country);
}
if (script != NULL) {
memcpy(out->localeScript, script, 4);
}
if (variant != NULL) {
memcpy(out->localeVariant, variant, strlen(variant));
}
}
TEST(ResourceTypesTest, IsMoreSpecificThan) {
ResTable_config l;
ResTable_config r;
fillIn("en", NULL, NULL, NULL, &l);
fillIn(NULL, NULL, NULL, NULL, &r);
EXPECT_TRUE(l.isMoreSpecificThan(r));
EXPECT_FALSE(r.isMoreSpecificThan(l));
fillIn("eng", NULL, NULL, NULL, &l);
EXPECT_TRUE(l.isMoreSpecificThan(r));
EXPECT_FALSE(r.isMoreSpecificThan(l));
fillIn("eng", "419", NULL, NULL, &r);
EXPECT_FALSE(l.isMoreSpecificThan(r));
EXPECT_TRUE(r.isMoreSpecificThan(l));
fillIn("en", NULL, NULL, NULL, &l);
fillIn("en", "US", NULL, NULL, &r);
EXPECT_FALSE(l.isMoreSpecificThan(r));
EXPECT_TRUE(r.isMoreSpecificThan(l));
fillIn("en", "US", NULL, NULL, &l);
fillIn("en", "US", "Latn", NULL, &r);
EXPECT_FALSE(l.isMoreSpecificThan(r));
EXPECT_TRUE(r.isMoreSpecificThan(l));
fillIn("en", "US", NULL, NULL, &l);
fillIn("en", "US", NULL, "POSIX", &r);
EXPECT_FALSE(l.isMoreSpecificThan(r));
EXPECT_TRUE(r.isMoreSpecificThan(l));
fillIn("en", "US", "Latn", NULL, &l);
fillIn("en", "US", NULL, "POSIX", &r);
EXPECT_FALSE(l.isMoreSpecificThan(r));
EXPECT_TRUE(r.isMoreSpecificThan(l));
}
TEST(ResourceTypesTest, setLocale) {
ResTable_config test;
test.setBcp47Locale("en-US");
EXPECT_EQ('e', test.language[0]);
EXPECT_EQ('n', test.language[1]);
EXPECT_EQ('U', test.country[0]);
EXPECT_EQ('S', test.country[1]);
EXPECT_EQ(0, test.localeScript[0]);
EXPECT_EQ(0, test.localeVariant[0]);
test.setBcp47Locale("eng-419");
char out[4] = { 1, 1, 1, 1};
test.unpackLanguage(out);
EXPECT_EQ('e', out[0]);
EXPECT_EQ('n', out[1]);
EXPECT_EQ('g', out[2]);
EXPECT_EQ(0, out[3]);
memset(out, 1, 4);
test.unpackRegion(out);
EXPECT_EQ('4', out[0]);
EXPECT_EQ('1', out[1]);
EXPECT_EQ('9', out[2]);
test.setBcp47Locale("en-Latn-419");
memset(out, 1, 4);
EXPECT_EQ('e', test.language[0]);
EXPECT_EQ('n', test.language[1]);
EXPECT_EQ(0, memcmp("Latn", test.localeScript, 4));
test.unpackRegion(out);
EXPECT_EQ('4', out[0]);
EXPECT_EQ('1', out[1]);
EXPECT_EQ('9', out[2]);
}
} // namespace android.
|