blob: ce7f8054e2ca9ed4ffc74b95f79caa371ae50c5f [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
Mårten Kongstad5c541f62018-06-20 08:46:41 +020017#include "androidfw/ConfigDescription.h"
18#include "androidfw/StringPiece.h"
19
20#include "android-base/logging.h"
21
22#include "gtest/gtest.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070023
24#include <string>
25
Mårten Kongstad5c541f62018-06-20 08:46:41 +020026namespace android {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027
Adam Lesinskicacb28f2016-10-19 12:18:14 -070028static ::testing::AssertionResult TestParse(
29 const StringPiece& input, ConfigDescription* config = nullptr) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030 if (ConfigDescription::Parse(input, config)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070031 return ::testing::AssertionSuccess() << input << " was successfully parsed";
32 }
33 return ::testing::AssertionFailure() << input << " could not be parsed";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080034}
35
36TEST(ConfigDescriptionTest, ParseFailWhenQualifiersAreOutOfOrder) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 EXPECT_FALSE(TestParse("en-sw600dp-ldrtl"));
38 EXPECT_FALSE(TestParse("land-en"));
39 EXPECT_FALSE(TestParse("hdpi-320dpi"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080040}
41
42TEST(ConfigDescriptionTest, ParseFailWhenQualifiersAreNotMatched) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 EXPECT_FALSE(TestParse("en-sw600dp-ILLEGAL"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080044}
45
46TEST(ConfigDescriptionTest, ParseFailWhenQualifiersHaveTrailingDash) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 EXPECT_FALSE(TestParse("en-sw600dp-land-"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080048}
49
50TEST(ConfigDescriptionTest, ParseBasicQualifiers) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 ConfigDescription config;
52 EXPECT_TRUE(TestParse("", &config));
53 EXPECT_EQ(std::string(""), config.toString().string());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080054
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 EXPECT_TRUE(TestParse("fr-land", &config));
56 EXPECT_EQ(std::string("fr-land"), config.toString().string());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080057
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 EXPECT_TRUE(
59 TestParse("mcc310-pl-sw720dp-normal-long-port-night-"
60 "xhdpi-keyssoft-qwerty-navexposed-nonav",
61 &config));
62 EXPECT_EQ(std::string("mcc310-pl-sw720dp-normal-long-port-night-"
63 "xhdpi-keyssoft-qwerty-navexposed-nonav-v13"),
64 config.toString().string());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080065}
66
67TEST(ConfigDescriptionTest, ParseLocales) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 ConfigDescription config;
69 EXPECT_TRUE(TestParse("en-rUS", &config));
70 EXPECT_EQ(std::string("en-rUS"), config.toString().string());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080071}
72
73TEST(ConfigDescriptionTest, ParseQualifierAddedInApi13) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 ConfigDescription config;
75 EXPECT_TRUE(TestParse("sw600dp", &config));
76 EXPECT_EQ(std::string("sw600dp-v13"), config.toString().string());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 EXPECT_TRUE(TestParse("sw600dp-v8", &config));
79 EXPECT_EQ(std::string("sw600dp-v13"), config.toString().string());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080080}
81
82TEST(ConfigDescriptionTest, ParseCarAttribute) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 ConfigDescription config;
84 EXPECT_TRUE(TestParse("car", &config));
85 EXPECT_EQ(android::ResTable_config::UI_MODE_TYPE_CAR, config.uiMode);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080086}
87
Adam Lesinski64254972015-11-03 16:16:17 -080088TEST(ConfigDescriptionTest, TestParsingRoundQualifier) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 ConfigDescription config;
90 EXPECT_TRUE(TestParse("round", &config));
91 EXPECT_EQ(android::ResTable_config::SCREENROUND_YES,
92 config.screenLayout2 & android::ResTable_config::MASK_SCREENROUND);
93 EXPECT_EQ(SDK_MARSHMALLOW, config.sdkVersion);
94 EXPECT_EQ(std::string("round-v23"), config.toString().string());
Adam Lesinski64254972015-11-03 16:16:17 -080095
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 EXPECT_TRUE(TestParse("notround", &config));
97 EXPECT_EQ(android::ResTable_config::SCREENROUND_NO,
98 config.screenLayout2 & android::ResTable_config::MASK_SCREENROUND);
99 EXPECT_EQ(SDK_MARSHMALLOW, config.sdkVersion);
100 EXPECT_EQ(std::string("notround-v23"), config.toString().string());
Adam Lesinski64254972015-11-03 16:16:17 -0800101}
102
Romain Guyc9ba5592017-01-18 16:34:42 -0800103TEST(ConfigDescriptionTest, TestWideColorGamutQualifier) {
104 ConfigDescription config;
105 EXPECT_TRUE(TestParse("widecg", &config));
106 EXPECT_EQ(android::ResTable_config::WIDE_COLOR_GAMUT_YES,
Romain Guy48327452017-01-23 17:03:35 -0800107 config.colorMode & android::ResTable_config::MASK_WIDE_COLOR_GAMUT);
Romain Guyc9ba5592017-01-18 16:34:42 -0800108 EXPECT_EQ(SDK_O, config.sdkVersion);
109 EXPECT_EQ(std::string("widecg-v26"), config.toString().string());
110
111 EXPECT_TRUE(TestParse("nowidecg", &config));
112 EXPECT_EQ(android::ResTable_config::WIDE_COLOR_GAMUT_NO,
Romain Guy48327452017-01-23 17:03:35 -0800113 config.colorMode & android::ResTable_config::MASK_WIDE_COLOR_GAMUT);
Romain Guyc9ba5592017-01-18 16:34:42 -0800114 EXPECT_EQ(SDK_O, config.sdkVersion);
115 EXPECT_EQ(std::string("nowidecg-v26"), config.toString().string());
116}
117
118TEST(ConfigDescriptionTest, TestHdrQualifier) {
119 ConfigDescription config;
120 EXPECT_TRUE(TestParse("highdr", &config));
121 EXPECT_EQ(android::ResTable_config::HDR_YES,
Romain Guy48327452017-01-23 17:03:35 -0800122 config.colorMode & android::ResTable_config::MASK_HDR);
Romain Guyc9ba5592017-01-18 16:34:42 -0800123 EXPECT_EQ(SDK_O, config.sdkVersion);
124 EXPECT_EQ(std::string("highdr-v26"), config.toString().string());
125
126 EXPECT_TRUE(TestParse("lowdr", &config));
127 EXPECT_EQ(android::ResTable_config::HDR_NO,
Romain Guy48327452017-01-23 17:03:35 -0800128 config.colorMode & android::ResTable_config::MASK_HDR);
Romain Guyc9ba5592017-01-18 16:34:42 -0800129 EXPECT_EQ(SDK_O, config.sdkVersion);
130 EXPECT_EQ(std::string("lowdr-v26"), config.toString().string());
131}
132
Zak Cohen1a6acdb2016-12-12 15:21:21 -0800133TEST(ConfigDescriptionTest, ParseVrAttribute) {
134 ConfigDescription config;
135 EXPECT_TRUE(TestParse("vrheadset", &config));
136 EXPECT_EQ(android::ResTable_config::UI_MODE_TYPE_VR_HEADSET, config.uiMode);
137 EXPECT_EQ(SDK_O, config.sdkVersion);
138 EXPECT_EQ(std::string("vrheadset-v26"), config.toString().string());
139}
140
Mårten Kongstad5c541f62018-06-20 08:46:41 +0200141static inline ConfigDescription ParseConfigOrDie(const android::StringPiece& str) {
142 ConfigDescription config;
143 CHECK(ConfigDescription::Parse(str, &config)) << "invalid configuration: " << str;
144 return config;
145}
Adam Lesinskia91d5c32017-08-30 16:12:05 -0700146
Mårten Kongstad5c541f62018-06-20 08:46:41 +0200147TEST(ConfigDescriptionTest, RangeQualifiersDoNotConflict) {
Adam Lesinskia91d5c32017-08-30 16:12:05 -0700148 EXPECT_FALSE(ParseConfigOrDie("large").ConflictsWith(ParseConfigOrDie("normal-land")));
149 EXPECT_FALSE(ParseConfigOrDie("long-hdpi").ConflictsWith(ParseConfigOrDie("xhdpi")));
150 EXPECT_FALSE(ParseConfigOrDie("sw600dp").ConflictsWith(ParseConfigOrDie("sw700dp")));
151 EXPECT_FALSE(ParseConfigOrDie("v11").ConflictsWith(ParseConfigOrDie("v21")));
152 EXPECT_FALSE(ParseConfigOrDie("h600dp").ConflictsWith(ParseConfigOrDie("h300dp")));
153 EXPECT_FALSE(ParseConfigOrDie("w400dp").ConflictsWith(ParseConfigOrDie("w300dp")));
154 EXPECT_FALSE(ParseConfigOrDie("600x400").ConflictsWith(ParseConfigOrDie("300x200")));
155}
156
Mårten Kongstad5c541f62018-06-20 08:46:41 +0200157} // namespace android