blob: c331dc0f69098d654ef48042154e5c1dd0c78809 [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 Lesinski64254972015-11-03 16:16:17 -080021#include "SdkConstants.h"
Adam Lesinskid0f116b2016-07-08 15:00:32 -070022#include "test/Test.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070023#include "util/StringPiece.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080025namespace aapt {
26
Adam Lesinskicacb28f2016-10-19 12:18:14 -070027static ::testing::AssertionResult TestParse(
28 const StringPiece& input, ConfigDescription* config = nullptr) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029 if (ConfigDescription::Parse(input, config)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070030 return ::testing::AssertionSuccess() << input << " was successfully parsed";
31 }
32 return ::testing::AssertionFailure() << input << " could not be parsed";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080033}
34
35TEST(ConfigDescriptionTest, ParseFailWhenQualifiersAreOutOfOrder) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 EXPECT_FALSE(TestParse("en-sw600dp-ldrtl"));
37 EXPECT_FALSE(TestParse("land-en"));
38 EXPECT_FALSE(TestParse("hdpi-320dpi"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080039}
40
41TEST(ConfigDescriptionTest, ParseFailWhenQualifiersAreNotMatched) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 EXPECT_FALSE(TestParse("en-sw600dp-ILLEGAL"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043}
44
45TEST(ConfigDescriptionTest, ParseFailWhenQualifiersHaveTrailingDash) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 EXPECT_FALSE(TestParse("en-sw600dp-land-"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080047}
48
49TEST(ConfigDescriptionTest, ParseBasicQualifiers) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 ConfigDescription config;
51 EXPECT_TRUE(TestParse("", &config));
52 EXPECT_EQ(std::string(""), config.toString().string());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080053
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 EXPECT_TRUE(TestParse("fr-land", &config));
55 EXPECT_EQ(std::string("fr-land"), config.toString().string());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080056
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 EXPECT_TRUE(
58 TestParse("mcc310-pl-sw720dp-normal-long-port-night-"
59 "xhdpi-keyssoft-qwerty-navexposed-nonav",
60 &config));
61 EXPECT_EQ(std::string("mcc310-pl-sw720dp-normal-long-port-night-"
62 "xhdpi-keyssoft-qwerty-navexposed-nonav-v13"),
63 config.toString().string());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080064}
65
66TEST(ConfigDescriptionTest, ParseLocales) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 ConfigDescription config;
68 EXPECT_TRUE(TestParse("en-rUS", &config));
69 EXPECT_EQ(std::string("en-rUS"), config.toString().string());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070}
71
72TEST(ConfigDescriptionTest, ParseQualifierAddedInApi13) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 ConfigDescription config;
74 EXPECT_TRUE(TestParse("sw600dp", &config));
75 EXPECT_EQ(std::string("sw600dp-v13"), config.toString().string());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 EXPECT_TRUE(TestParse("sw600dp-v8", &config));
78 EXPECT_EQ(std::string("sw600dp-v13"), config.toString().string());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080079}
80
81TEST(ConfigDescriptionTest, ParseCarAttribute) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 ConfigDescription config;
83 EXPECT_TRUE(TestParse("car", &config));
84 EXPECT_EQ(android::ResTable_config::UI_MODE_TYPE_CAR, config.uiMode);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080085}
86
Adam Lesinski64254972015-11-03 16:16:17 -080087TEST(ConfigDescriptionTest, TestParsingRoundQualifier) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 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_MARSHMALLOW, config.sdkVersion);
93 EXPECT_EQ(std::string("round-v23"), config.toString().string());
Adam Lesinski64254972015-11-03 16:16:17 -080094
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 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_MARSHMALLOW, config.sdkVersion);
99 EXPECT_EQ(std::string("notround-v23"), config.toString().string());
Adam Lesinski64254972015-11-03 16:16:17 -0800100}
101
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102} // namespace aapt