1 /*
2 * Copyright (C) 2014 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 <utils/String8.h>
18 #include <gtest/gtest.h>
19
20 #include "AaptConfig.h"
21 #include "ConfigDescription.h"
22 #include "SdkConstants.h"
23 #include "TestHelper.h"
24
25 using android::String8;
26
TestParse(const String8 & input,ConfigDescription * config=NULL)27 static ::testing::AssertionResult TestParse(const String8& input, ConfigDescription* config=NULL) {
28 if (AaptConfig::parse(String8(input), config)) {
29 return ::testing::AssertionSuccess() << input << " was successfully parsed";
30 }
31 return ::testing::AssertionFailure() << input << " could not be parsed";
32 }
33
TestParse(const char * input,ConfigDescription * config=NULL)34 static ::testing::AssertionResult TestParse(const char* input, ConfigDescription* config=NULL) {
35 return TestParse(String8(input), config);
36 }
37
TEST(AaptConfigTest,ParseFailWhenQualifiersAreOutOfOrder)38 TEST(AaptConfigTest, ParseFailWhenQualifiersAreOutOfOrder) {
39 EXPECT_FALSE(TestParse("en-sw600dp-ldrtl"));
40 EXPECT_FALSE(TestParse("land-en"));
41 EXPECT_FALSE(TestParse("hdpi-320dpi"));
42 }
43
TEST(AaptConfigTest,ParseFailWhenQualifiersAreNotMatched)44 TEST(AaptConfigTest, ParseFailWhenQualifiersAreNotMatched) {
45 EXPECT_FALSE(TestParse("en-sw600dp-ILLEGAL"));
46 }
47
TEST(AaptConfigTest,ParseFailWhenQualifiersHaveTrailingDash)48 TEST(AaptConfigTest, ParseFailWhenQualifiersHaveTrailingDash) {
49 EXPECT_FALSE(TestParse("en-sw600dp-land-"));
50 }
51
TEST(AaptConfigTest,ParseBasicQualifiers)52 TEST(AaptConfigTest, ParseBasicQualifiers) {
53 ConfigDescription config;
54 EXPECT_TRUE(TestParse("", &config));
55 EXPECT_EQ(String8(""), config.toString());
56
57 EXPECT_TRUE(TestParse("fr-land", &config));
58 EXPECT_EQ(String8("fr-land"), config.toString());
59
60 EXPECT_TRUE(TestParse("mcc310-pl-sw720dp-normal-long-port-night-"
61 "xhdpi-keyssoft-qwerty-navexposed-nonav", &config));
62 EXPECT_EQ(String8("mcc310-pl-sw720dp-normal-long-port-night-"
63 "xhdpi-keyssoft-qwerty-navexposed-nonav-v13"), config.toString());
64 }
65
TEST(AaptConfigTest,ParseLocales)66 TEST(AaptConfigTest, ParseLocales) {
67 ConfigDescription config;
68 EXPECT_TRUE(TestParse("en-rUS", &config));
69 EXPECT_EQ(String8("en-rUS"), config.toString());
70 }
71
TEST(AaptConfigTest,ParseQualifierAddedInApi13)72 TEST(AaptConfigTest, ParseQualifierAddedInApi13) {
73 ConfigDescription config;
74 EXPECT_TRUE(TestParse("sw600dp", &config));
75 EXPECT_EQ(String8("sw600dp-v13"), config.toString());
76
77 EXPECT_TRUE(TestParse("sw600dp-v8", &config));
78 EXPECT_EQ(String8("sw600dp-v13"), config.toString());
79 }
80
TEST(AaptConfigTest,TestParsingOfCarAttribute)81 TEST(AaptConfigTest, TestParsingOfCarAttribute) {
82 ConfigDescription config;
83 EXPECT_TRUE(TestParse("car", &config));
84 EXPECT_EQ(android::ResTable_config::UI_MODE_TYPE_CAR, config.uiMode);
85 }
86
TEST(AaptConfigTest,TestParsingRoundQualifier)87 TEST(AaptConfigTest, TestParsingRoundQualifier) {
88 ConfigDescription config;
89 EXPECT_TRUE(TestParse("round", &config));
90 EXPECT_EQ(android::ResTable_config::SCREENROUND_YES,
91 config.screenLayout2 & android::ResTable_config::MASK_SCREENROUND);
92 EXPECT_EQ(SDK_MNC, config.sdkVersion);
93 EXPECT_EQ(String8("round-v23"), config.toString());
94
95 EXPECT_TRUE(TestParse("notround", &config));
96 EXPECT_EQ(android::ResTable_config::SCREENROUND_NO,
97 config.screenLayout2 & android::ResTable_config::MASK_SCREENROUND);
98 EXPECT_EQ(SDK_MNC, config.sdkVersion);
99 EXPECT_EQ(String8("notround-v23"), config.toString());
100 }
101
TEST(AaptConfigTest,WideColorGamutQualifier)102 TEST(AaptConfigTest, WideColorGamutQualifier) {
103 ConfigDescription config;
104 EXPECT_TRUE(TestParse("widecg", &config));
105 EXPECT_EQ(android::ResTable_config::WIDE_COLOR_GAMUT_YES,
106 config.colorMode & android::ResTable_config::MASK_WIDE_COLOR_GAMUT);
107 EXPECT_EQ(SDK_O, config.sdkVersion);
108 EXPECT_EQ(String8("widecg-v26"), config.toString());
109
110 EXPECT_TRUE(TestParse("nowidecg", &config));
111 EXPECT_EQ(android::ResTable_config::WIDE_COLOR_GAMUT_NO,
112 config.colorMode & android::ResTable_config::MASK_WIDE_COLOR_GAMUT);
113 EXPECT_EQ(SDK_O, config.sdkVersion);
114 EXPECT_EQ(String8("nowidecg-v26"), config.toString());
115 }
116
TEST(AaptConfigTest,HdrQualifier)117 TEST(AaptConfigTest, HdrQualifier) {
118 ConfigDescription config;
119 EXPECT_TRUE(TestParse("highdr", &config));
120 EXPECT_EQ(android::ResTable_config::HDR_YES,
121 config.colorMode & android::ResTable_config::MASK_HDR);
122 EXPECT_EQ(SDK_O, config.sdkVersion);
123 EXPECT_EQ(String8("highdr-v26"), config.toString());
124
125 EXPECT_TRUE(TestParse("lowdr", &config));
126 EXPECT_EQ(android::ResTable_config::HDR_NO,
127 config.colorMode & android::ResTable_config::MASK_HDR);
128 EXPECT_EQ(SDK_O, config.sdkVersion);
129 EXPECT_EQ(String8("lowdr-v26"), config.toString());
130 }