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 "ConfigDescription.h"
18 #include "SdkConstants.h"
19 
20 #include "util/StringPiece.h"
21 
22 #include <gtest/gtest.h>
23 #include <string>
24 
25 namespace aapt {
26 
TestParse(const StringPiece & input,ConfigDescription * config=nullptr)27 static ::testing::AssertionResult TestParse(const StringPiece& input,
28                                             ConfigDescription* config = nullptr) {
29     if (ConfigDescription::parse(input, config)) {
30         return ::testing::AssertionSuccess() << input << " was successfully parsed";
31     }
32     return ::testing::AssertionFailure() << input << " could not be parsed";
33 }
34 
TEST(ConfigDescriptionTest,ParseFailWhenQualifiersAreOutOfOrder)35 TEST(ConfigDescriptionTest, ParseFailWhenQualifiersAreOutOfOrder) {
36     EXPECT_FALSE(TestParse("en-sw600dp-ldrtl"));
37     EXPECT_FALSE(TestParse("land-en"));
38     EXPECT_FALSE(TestParse("hdpi-320dpi"));
39 }
40 
TEST(ConfigDescriptionTest,ParseFailWhenQualifiersAreNotMatched)41 TEST(ConfigDescriptionTest, ParseFailWhenQualifiersAreNotMatched) {
42     EXPECT_FALSE(TestParse("en-sw600dp-ILLEGAL"));
43 }
44 
TEST(ConfigDescriptionTest,ParseFailWhenQualifiersHaveTrailingDash)45 TEST(ConfigDescriptionTest, ParseFailWhenQualifiersHaveTrailingDash) {
46     EXPECT_FALSE(TestParse("en-sw600dp-land-"));
47 }
48 
TEST(ConfigDescriptionTest,ParseBasicQualifiers)49 TEST(ConfigDescriptionTest, ParseBasicQualifiers) {
50     ConfigDescription config;
51     EXPECT_TRUE(TestParse("", &config));
52     EXPECT_EQ(std::string(""), config.toString().string());
53 
54     EXPECT_TRUE(TestParse("fr-land", &config));
55     EXPECT_EQ(std::string("fr-land"), config.toString().string());
56 
57     EXPECT_TRUE(TestParse("mcc310-pl-sw720dp-normal-long-port-night-"
58                 "xhdpi-keyssoft-qwerty-navexposed-nonav", &config));
59     EXPECT_EQ(std::string("mcc310-pl-sw720dp-normal-long-port-night-"
60                 "xhdpi-keyssoft-qwerty-navexposed-nonav-v13"), config.toString().string());
61 }
62 
TEST(ConfigDescriptionTest,ParseLocales)63 TEST(ConfigDescriptionTest, ParseLocales) {
64     ConfigDescription config;
65     EXPECT_TRUE(TestParse("en-rUS", &config));
66     EXPECT_EQ(std::string("en-rUS"), config.toString().string());
67 }
68 
TEST(ConfigDescriptionTest,ParseQualifierAddedInApi13)69 TEST(ConfigDescriptionTest, ParseQualifierAddedInApi13) {
70     ConfigDescription config;
71     EXPECT_TRUE(TestParse("sw600dp", &config));
72     EXPECT_EQ(std::string("sw600dp-v13"), config.toString().string());
73 
74     EXPECT_TRUE(TestParse("sw600dp-v8", &config));
75     EXPECT_EQ(std::string("sw600dp-v13"), config.toString().string());
76 }
77 
TEST(ConfigDescriptionTest,ParseCarAttribute)78 TEST(ConfigDescriptionTest, ParseCarAttribute) {
79     ConfigDescription config;
80     EXPECT_TRUE(TestParse("car", &config));
81     EXPECT_EQ(android::ResTable_config::UI_MODE_TYPE_CAR, config.uiMode);
82 }
83 
TEST(ConfigDescriptionTest,TestParsingRoundQualifier)84 TEST(ConfigDescriptionTest, TestParsingRoundQualifier) {
85     ConfigDescription config;
86     EXPECT_TRUE(TestParse("round", &config));
87     EXPECT_EQ(android::ResTable_config::SCREENROUND_YES,
88     config.screenLayout2 & android::ResTable_config::MASK_SCREENROUND);
89     EXPECT_EQ(SDK_MARSHMALLOW, config.sdkVersion);
90     EXPECT_EQ(std::string("round-v23"), config.toString().string());
91 
92     EXPECT_TRUE(TestParse("notround", &config));
93     EXPECT_EQ(android::ResTable_config::SCREENROUND_NO,
94               config.screenLayout2 & android::ResTable_config::MASK_SCREENROUND);
95     EXPECT_EQ(SDK_MARSHMALLOW, config.sdkVersion);
96     EXPECT_EQ(std::string("notround-v23"), config.toString().string());
97 }
98 
99 } // namespace aapt
100