blob: 66b7d477611885ffe4c810a110222fc5721f540c [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 Lesinski64254972015-11-03 16:16:17 -080018#include "SdkConstants.h"
Adam Lesinskid0f116b2016-07-08 15:00:32 -070019#include "test/Test.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070020#include "util/StringPiece.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080021
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080022#include <string>
23
24namespace aapt {
25
Adam Lesinskicacb28f2016-10-19 12:18:14 -070026static ::testing::AssertionResult TestParse(
27 const StringPiece& input, ConfigDescription* config = nullptr) {
28 if (ConfigDescription::parse(input, config)) {
29 return ::testing::AssertionSuccess() << input << " was successfully parsed";
30 }
31 return ::testing::AssertionFailure() << input << " could not be parsed";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080032}
33
34TEST(ConfigDescriptionTest, ParseFailWhenQualifiersAreOutOfOrder) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 EXPECT_FALSE(TestParse("en-sw600dp-ldrtl"));
36 EXPECT_FALSE(TestParse("land-en"));
37 EXPECT_FALSE(TestParse("hdpi-320dpi"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080038}
39
40TEST(ConfigDescriptionTest, ParseFailWhenQualifiersAreNotMatched) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 EXPECT_FALSE(TestParse("en-sw600dp-ILLEGAL"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080042}
43
44TEST(ConfigDescriptionTest, ParseFailWhenQualifiersHaveTrailingDash) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 EXPECT_FALSE(TestParse("en-sw600dp-land-"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080046}
47
48TEST(ConfigDescriptionTest, ParseBasicQualifiers) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 ConfigDescription config;
50 EXPECT_TRUE(TestParse("", &config));
51 EXPECT_EQ(std::string(""), config.toString().string());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080052
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 EXPECT_TRUE(TestParse("fr-land", &config));
54 EXPECT_EQ(std::string("fr-land"), config.toString().string());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 EXPECT_TRUE(
57 TestParse("mcc310-pl-sw720dp-normal-long-port-night-"
58 "xhdpi-keyssoft-qwerty-navexposed-nonav",
59 &config));
60 EXPECT_EQ(std::string("mcc310-pl-sw720dp-normal-long-port-night-"
61 "xhdpi-keyssoft-qwerty-navexposed-nonav-v13"),
62 config.toString().string());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080063}
64
65TEST(ConfigDescriptionTest, ParseLocales) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 ConfigDescription config;
67 EXPECT_TRUE(TestParse("en-rUS", &config));
68 EXPECT_EQ(std::string("en-rUS"), config.toString().string());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080069}
70
71TEST(ConfigDescriptionTest, ParseQualifierAddedInApi13) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 ConfigDescription config;
73 EXPECT_TRUE(TestParse("sw600dp", &config));
74 EXPECT_EQ(std::string("sw600dp-v13"), config.toString().string());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080075
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 EXPECT_TRUE(TestParse("sw600dp-v8", &config));
77 EXPECT_EQ(std::string("sw600dp-v13"), config.toString().string());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080078}
79
80TEST(ConfigDescriptionTest, ParseCarAttribute) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 ConfigDescription config;
82 EXPECT_TRUE(TestParse("car", &config));
83 EXPECT_EQ(android::ResTable_config::UI_MODE_TYPE_CAR, config.uiMode);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084}
85
Adam Lesinski64254972015-11-03 16:16:17 -080086TEST(ConfigDescriptionTest, TestParsingRoundQualifier) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 ConfigDescription config;
88 EXPECT_TRUE(TestParse("round", &config));
89 EXPECT_EQ(android::ResTable_config::SCREENROUND_YES,
90 config.screenLayout2 & android::ResTable_config::MASK_SCREENROUND);
91 EXPECT_EQ(SDK_MARSHMALLOW, config.sdkVersion);
92 EXPECT_EQ(std::string("round-v23"), config.toString().string());
Adam Lesinski64254972015-11-03 16:16:17 -080093
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 EXPECT_TRUE(TestParse("notround", &config));
95 EXPECT_EQ(android::ResTable_config::SCREENROUND_NO,
96 config.screenLayout2 & android::ResTable_config::MASK_SCREENROUND);
97 EXPECT_EQ(SDK_MARSHMALLOW, config.sdkVersion);
98 EXPECT_EQ(std::string("notround-v23"), config.toString().string());
Adam Lesinski64254972015-11-03 16:16:17 -080099}
100
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101} // namespace aapt