1 /*
2  * Copyright (C) 2015 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 #include "androidfw/ConfigDescription.h"
18 #include "androidfw/StringPiece.h"
19 
20 #include "android-base/logging.h"
21 
22 #include "gtest/gtest.h"
23 
24 #include <string>
25 
26 namespace android {
27 
TestParse(StringPiece input,ConfigDescription * config=nullptr)28 static ::testing::AssertionResult TestParse(StringPiece input,
29                                             ConfigDescription* config = nullptr) {
30   if (ConfigDescription::Parse(input, config)) {
31     return ::testing::AssertionSuccess() << input << " was successfully parsed";
32   }
33   return ::testing::AssertionFailure() << input << " could not be parsed";
34 }
35 
TEST(ConfigDescriptionTest,ParseFailWhenQualifiersAreOutOfOrder)36 TEST(ConfigDescriptionTest, ParseFailWhenQualifiersAreOutOfOrder) {
37   EXPECT_FALSE(TestParse("en-sw600dp-ldrtl"));
38   EXPECT_FALSE(TestParse("land-en"));
39   EXPECT_FALSE(TestParse("hdpi-320dpi"));
40 }
41 
TEST(ConfigDescriptionTest,ParseFailWhenQualifiersAreNotMatched)42 TEST(ConfigDescriptionTest, ParseFailWhenQualifiersAreNotMatched) {
43   EXPECT_FALSE(TestParse("en-sw600dp-ILLEGAL"));
44 }
45 
TEST(ConfigDescriptionTest,ParseFailWhenQualifiersHaveTrailingDash)46 TEST(ConfigDescriptionTest, ParseFailWhenQualifiersHaveTrailingDash) {
47   EXPECT_FALSE(TestParse("en-sw600dp-land-"));
48 }
49 
TEST(ConfigDescriptionTest,ParseBasicQualifiers)50 TEST(ConfigDescriptionTest, ParseBasicQualifiers) {
51   ConfigDescription config;
52   EXPECT_TRUE(TestParse("", &config));
53   EXPECT_EQ(std::string(""), config.toString().c_str());
54 
55   EXPECT_TRUE(TestParse("fr-land", &config));
56   EXPECT_EQ(std::string("fr-land"), config.toString().c_str());
57 
58   EXPECT_TRUE(
59       TestParse("mcc310-pl-sw720dp-normal-long-port-night-"
60                 "xhdpi-keyssoft-qwerty-navexposed-nonav",
61                 &config));
62   EXPECT_EQ(std::string("mcc310-pl-sw720dp-normal-long-port-night-"
63                         "xhdpi-keyssoft-qwerty-navexposed-nonav-v13"),
64             config.toString().c_str());
65 }
66 
TEST(ConfigDescriptionTest,ParseLocales)67 TEST(ConfigDescriptionTest, ParseLocales) {
68   ConfigDescription config;
69   EXPECT_TRUE(TestParse("en-rUS", &config));
70   EXPECT_EQ(std::string("en-rUS"), config.toString().c_str());
71 }
72 
TEST(ConfigDescriptionTest,ParseQualifierAddedInApi13)73 TEST(ConfigDescriptionTest, ParseQualifierAddedInApi13) {
74   ConfigDescription config;
75   EXPECT_TRUE(TestParse("sw600dp", &config));
76   EXPECT_EQ(std::string("sw600dp-v13"), config.toString().c_str());
77 
78   EXPECT_TRUE(TestParse("sw600dp-v8", &config));
79   EXPECT_EQ(std::string("sw600dp-v13"), config.toString().c_str());
80 }
81 
TEST(ConfigDescriptionTest,ParseCarAttribute)82 TEST(ConfigDescriptionTest, ParseCarAttribute) {
83   ConfigDescription config;
84   EXPECT_TRUE(TestParse("car", &config));
85   EXPECT_EQ(android::ResTable_config::UI_MODE_TYPE_CAR, config.uiMode);
86 }
87 
TEST(ConfigDescriptionTest,TestParsingRoundQualifier)88 TEST(ConfigDescriptionTest, TestParsingRoundQualifier) {
89   ConfigDescription config;
90   EXPECT_TRUE(TestParse("round", &config));
91   EXPECT_EQ(android::ResTable_config::SCREENROUND_YES,
92             config.screenLayout2 & android::ResTable_config::MASK_SCREENROUND);
93   EXPECT_EQ(SDK_MARSHMALLOW, config.sdkVersion);
94   EXPECT_EQ(std::string("round-v23"), config.toString().c_str());
95 
96   EXPECT_TRUE(TestParse("notround", &config));
97   EXPECT_EQ(android::ResTable_config::SCREENROUND_NO,
98             config.screenLayout2 & android::ResTable_config::MASK_SCREENROUND);
99   EXPECT_EQ(SDK_MARSHMALLOW, config.sdkVersion);
100   EXPECT_EQ(std::string("notround-v23"), config.toString().c_str());
101 }
102 
TEST(ConfigDescriptionTest,TestWideColorGamutQualifier)103 TEST(ConfigDescriptionTest, TestWideColorGamutQualifier) {
104   ConfigDescription config;
105   EXPECT_TRUE(TestParse("widecg", &config));
106   EXPECT_EQ(android::ResTable_config::WIDE_COLOR_GAMUT_YES,
107             config.colorMode & android::ResTable_config::MASK_WIDE_COLOR_GAMUT);
108   EXPECT_EQ(SDK_O, config.sdkVersion);
109   EXPECT_EQ(std::string("widecg-v26"), config.toString().c_str());
110 
111   EXPECT_TRUE(TestParse("nowidecg", &config));
112   EXPECT_EQ(android::ResTable_config::WIDE_COLOR_GAMUT_NO,
113             config.colorMode & android::ResTable_config::MASK_WIDE_COLOR_GAMUT);
114   EXPECT_EQ(SDK_O, config.sdkVersion);
115   EXPECT_EQ(std::string("nowidecg-v26"), config.toString().c_str());
116 }
117 
TEST(ConfigDescriptionTest,TestHdrQualifier)118 TEST(ConfigDescriptionTest, TestHdrQualifier) {
119   ConfigDescription config;
120   EXPECT_TRUE(TestParse("highdr", &config));
121   EXPECT_EQ(android::ResTable_config::HDR_YES,
122             config.colorMode & android::ResTable_config::MASK_HDR);
123   EXPECT_EQ(SDK_O, config.sdkVersion);
124   EXPECT_EQ(std::string("highdr-v26"), config.toString().c_str());
125 
126   EXPECT_TRUE(TestParse("lowdr", &config));
127   EXPECT_EQ(android::ResTable_config::HDR_NO,
128             config.colorMode & android::ResTable_config::MASK_HDR);
129   EXPECT_EQ(SDK_O, config.sdkVersion);
130   EXPECT_EQ(std::string("lowdr-v26"), config.toString().c_str());
131 }
132 
TEST(ConfigDescriptionTest,ParseVrAttribute)133 TEST(ConfigDescriptionTest, ParseVrAttribute) {
134   ConfigDescription config;
135   EXPECT_TRUE(TestParse("vrheadset", &config));
136   EXPECT_EQ(android::ResTable_config::UI_MODE_TYPE_VR_HEADSET, config.uiMode);
137   EXPECT_EQ(SDK_O, config.sdkVersion);
138   EXPECT_EQ(std::string("vrheadset-v26"), config.toString().c_str());
139 }
140 
ParseConfigOrDie(android::StringPiece str)141 static inline ConfigDescription ParseConfigOrDie(android::StringPiece str) {
142   ConfigDescription config;
143   CHECK(ConfigDescription::Parse(str, &config)) << "invalid configuration: " << str;
144   return config;
145 }
146 
TEST(ConfigDescriptionTest,RangeQualifiersDoNotConflict)147 TEST(ConfigDescriptionTest, RangeQualifiersDoNotConflict) {
148   EXPECT_FALSE(ParseConfigOrDie("large").ConflictsWith(ParseConfigOrDie("normal-land")));
149   EXPECT_FALSE(ParseConfigOrDie("long-hdpi").ConflictsWith(ParseConfigOrDie("xhdpi")));
150   EXPECT_FALSE(ParseConfigOrDie("sw600dp").ConflictsWith(ParseConfigOrDie("sw700dp")));
151   EXPECT_FALSE(ParseConfigOrDie("v11").ConflictsWith(ParseConfigOrDie("v21")));
152   EXPECT_FALSE(ParseConfigOrDie("h600dp").ConflictsWith(ParseConfigOrDie("h300dp")));
153   EXPECT_FALSE(ParseConfigOrDie("w400dp").ConflictsWith(ParseConfigOrDie("w300dp")));
154   EXPECT_FALSE(ParseConfigOrDie("600x400").ConflictsWith(ParseConfigOrDie("300x200")));
155 }
156 
TEST(ConfigDescriptionTest,TestGrammaticalGenderQualifier)157 TEST(ConfigDescriptionTest, TestGrammaticalGenderQualifier) {
158   ConfigDescription config;
159   EXPECT_TRUE(TestParse("feminine", &config));
160   EXPECT_EQ(android::ResTable_config::GRAMMATICAL_GENDER_FEMININE, config.grammaticalInflection);
161   EXPECT_EQ(SDK_U, config.sdkVersion);
162   EXPECT_EQ(std::string("feminine-v34"), config.toString().c_str());
163 
164   EXPECT_TRUE(TestParse("masculine", &config));
165   EXPECT_EQ(android::ResTable_config::GRAMMATICAL_GENDER_MASCULINE, config.grammaticalInflection);
166   EXPECT_EQ(SDK_U, config.sdkVersion);
167   EXPECT_EQ(std::string("masculine-v34"), config.toString().c_str());
168 
169   EXPECT_TRUE(TestParse("neuter", &config));
170   EXPECT_EQ(android::ResTable_config::GRAMMATICAL_GENDER_NEUTER, config.grammaticalInflection);
171   EXPECT_EQ(SDK_U, config.sdkVersion);
172   EXPECT_EQ(std::string("neuter-v34"), config.toString().c_str());
173 }
174 
175 }  // namespace android
176