blob: 1f351bf7481d944124f2378bb2cb211697287f77 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <string>
20
Adam Lesinskid5083f62017-01-16 15:07:21 -080021#include "androidfw/StringPiece.h"
22
Adam Lesinski64254972015-11-03 16:16:17 -080023#include "SdkConstants.h"
Adam Lesinskid0f116b2016-07-08 15:00:32 -070024#include "test/Test.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080025
26using android::StringPiece;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080028namespace aapt {
29
Adam Lesinskicacb28f2016-10-19 12:18:14 -070030static ::testing::AssertionResult TestParse(
31 const StringPiece& input, ConfigDescription* config = nullptr) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032 if (ConfigDescription::Parse(input, config)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033 return ::testing::AssertionSuccess() << input << " was successfully parsed";
34 }
35 return ::testing::AssertionFailure() << input << " could not be parsed";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080036}
37
38TEST(ConfigDescriptionTest, ParseFailWhenQualifiersAreOutOfOrder) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 EXPECT_FALSE(TestParse("en-sw600dp-ldrtl"));
40 EXPECT_FALSE(TestParse("land-en"));
41 EXPECT_FALSE(TestParse("hdpi-320dpi"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080042}
43
44TEST(ConfigDescriptionTest, ParseFailWhenQualifiersAreNotMatched) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 EXPECT_FALSE(TestParse("en-sw600dp-ILLEGAL"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080046}
47
48TEST(ConfigDescriptionTest, ParseFailWhenQualifiersHaveTrailingDash) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 EXPECT_FALSE(TestParse("en-sw600dp-land-"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080050}
51
52TEST(ConfigDescriptionTest, ParseBasicQualifiers) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 ConfigDescription config;
54 EXPECT_TRUE(TestParse("", &config));
55 EXPECT_EQ(std::string(""), config.toString().string());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080056
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 EXPECT_TRUE(TestParse("fr-land", &config));
58 EXPECT_EQ(std::string("fr-land"), config.toString().string());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080059
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 EXPECT_TRUE(
61 TestParse("mcc310-pl-sw720dp-normal-long-port-night-"
62 "xhdpi-keyssoft-qwerty-navexposed-nonav",
63 &config));
64 EXPECT_EQ(std::string("mcc310-pl-sw720dp-normal-long-port-night-"
65 "xhdpi-keyssoft-qwerty-navexposed-nonav-v13"),
66 config.toString().string());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080067}
68
69TEST(ConfigDescriptionTest, ParseLocales) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 ConfigDescription config;
71 EXPECT_TRUE(TestParse("en-rUS", &config));
72 EXPECT_EQ(std::string("en-rUS"), config.toString().string());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080073}
74
75TEST(ConfigDescriptionTest, ParseQualifierAddedInApi13) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 ConfigDescription config;
77 EXPECT_TRUE(TestParse("sw600dp", &config));
78 EXPECT_EQ(std::string("sw600dp-v13"), config.toString().string());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080079
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 EXPECT_TRUE(TestParse("sw600dp-v8", &config));
81 EXPECT_EQ(std::string("sw600dp-v13"), config.toString().string());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080082}
83
84TEST(ConfigDescriptionTest, ParseCarAttribute) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 ConfigDescription config;
86 EXPECT_TRUE(TestParse("car", &config));
87 EXPECT_EQ(android::ResTable_config::UI_MODE_TYPE_CAR, config.uiMode);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080088}
89
Adam Lesinski64254972015-11-03 16:16:17 -080090TEST(ConfigDescriptionTest, TestParsingRoundQualifier) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 ConfigDescription config;
92 EXPECT_TRUE(TestParse("round", &config));
93 EXPECT_EQ(android::ResTable_config::SCREENROUND_YES,
94 config.screenLayout2 & android::ResTable_config::MASK_SCREENROUND);
95 EXPECT_EQ(SDK_MARSHMALLOW, config.sdkVersion);
96 EXPECT_EQ(std::string("round-v23"), config.toString().string());
Adam Lesinski64254972015-11-03 16:16:17 -080097
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 EXPECT_TRUE(TestParse("notround", &config));
99 EXPECT_EQ(android::ResTable_config::SCREENROUND_NO,
100 config.screenLayout2 & android::ResTable_config::MASK_SCREENROUND);
101 EXPECT_EQ(SDK_MARSHMALLOW, config.sdkVersion);
102 EXPECT_EQ(std::string("notround-v23"), config.toString().string());
Adam Lesinski64254972015-11-03 16:16:17 -0800103}
104
Romain Guyc9ba5592017-01-18 16:34:42 -0800105TEST(ConfigDescriptionTest, TestWideColorGamutQualifier) {
106 ConfigDescription config;
107 EXPECT_TRUE(TestParse("widecg", &config));
108 EXPECT_EQ(android::ResTable_config::WIDE_COLOR_GAMUT_YES,
Romain Guy48327452017-01-23 17:03:35 -0800109 config.colorMode & android::ResTable_config::MASK_WIDE_COLOR_GAMUT);
Romain Guyc9ba5592017-01-18 16:34:42 -0800110 EXPECT_EQ(SDK_O, config.sdkVersion);
111 EXPECT_EQ(std::string("widecg-v26"), config.toString().string());
112
113 EXPECT_TRUE(TestParse("nowidecg", &config));
114 EXPECT_EQ(android::ResTable_config::WIDE_COLOR_GAMUT_NO,
Romain Guy48327452017-01-23 17:03:35 -0800115 config.colorMode & android::ResTable_config::MASK_WIDE_COLOR_GAMUT);
Romain Guyc9ba5592017-01-18 16:34:42 -0800116 EXPECT_EQ(SDK_O, config.sdkVersion);
117 EXPECT_EQ(std::string("nowidecg-v26"), config.toString().string());
118}
119
120TEST(ConfigDescriptionTest, TestHdrQualifier) {
121 ConfigDescription config;
122 EXPECT_TRUE(TestParse("highdr", &config));
123 EXPECT_EQ(android::ResTable_config::HDR_YES,
Romain Guy48327452017-01-23 17:03:35 -0800124 config.colorMode & android::ResTable_config::MASK_HDR);
Romain Guyc9ba5592017-01-18 16:34:42 -0800125 EXPECT_EQ(SDK_O, config.sdkVersion);
126 EXPECT_EQ(std::string("highdr-v26"), config.toString().string());
127
128 EXPECT_TRUE(TestParse("lowdr", &config));
129 EXPECT_EQ(android::ResTable_config::HDR_NO,
Romain Guy48327452017-01-23 17:03:35 -0800130 config.colorMode & android::ResTable_config::MASK_HDR);
Romain Guyc9ba5592017-01-18 16:34:42 -0800131 EXPECT_EQ(SDK_O, config.sdkVersion);
132 EXPECT_EQ(std::string("lowdr-v26"), config.toString().string());
133}
134
Zak Cohen1a6acdb2016-12-12 15:21:21 -0800135TEST(ConfigDescriptionTest, ParseVrAttribute) {
136 ConfigDescription config;
137 EXPECT_TRUE(TestParse("vrheadset", &config));
138 EXPECT_EQ(android::ResTable_config::UI_MODE_TYPE_VR_HEADSET, config.uiMode);
139 EXPECT_EQ(SDK_O, config.sdkVersion);
140 EXPECT_EQ(std::string("vrheadset-v26"), config.toString().string());
141}
142
Adam Lesinskia91d5c32017-08-30 16:12:05 -0700143TEST(ConfigDescriptionTest, RangeQualifiersDoNotConflict) {
144 using test::ParseConfigOrDie;
145
146 EXPECT_FALSE(ParseConfigOrDie("large").ConflictsWith(ParseConfigOrDie("normal-land")));
147 EXPECT_FALSE(ParseConfigOrDie("long-hdpi").ConflictsWith(ParseConfigOrDie("xhdpi")));
148 EXPECT_FALSE(ParseConfigOrDie("sw600dp").ConflictsWith(ParseConfigOrDie("sw700dp")));
149 EXPECT_FALSE(ParseConfigOrDie("v11").ConflictsWith(ParseConfigOrDie("v21")));
150 EXPECT_FALSE(ParseConfigOrDie("h600dp").ConflictsWith(ParseConfigOrDie("h300dp")));
151 EXPECT_FALSE(ParseConfigOrDie("w400dp").ConflictsWith(ParseConfigOrDie("w300dp")));
152 EXPECT_FALSE(ParseConfigOrDie("600x400").ConflictsWith(ParseConfigOrDie("300x200")));
153}
154
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155} // namespace aapt