Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
| 19 | #include <string> |
| 20 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 21 | #include "androidfw/StringPiece.h" |
| 22 | |
Adam Lesinski | 6425497 | 2015-11-03 16:16:17 -0800 | [diff] [blame] | 23 | #include "SdkConstants.h" |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 24 | #include "test/Test.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 25 | |
| 26 | using android::StringPiece; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 27 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 28 | namespace aapt { |
| 29 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 30 | static ::testing::AssertionResult TestParse( |
| 31 | const StringPiece& input, ConfigDescription* config = nullptr) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 32 | if (ConfigDescription::Parse(input, config)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 33 | return ::testing::AssertionSuccess() << input << " was successfully parsed"; |
| 34 | } |
| 35 | return ::testing::AssertionFailure() << input << " could not be parsed"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | TEST(ConfigDescriptionTest, ParseFailWhenQualifiersAreOutOfOrder) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 39 | EXPECT_FALSE(TestParse("en-sw600dp-ldrtl")); |
| 40 | EXPECT_FALSE(TestParse("land-en")); |
| 41 | EXPECT_FALSE(TestParse("hdpi-320dpi")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | TEST(ConfigDescriptionTest, ParseFailWhenQualifiersAreNotMatched) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 45 | EXPECT_FALSE(TestParse("en-sw600dp-ILLEGAL")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | TEST(ConfigDescriptionTest, ParseFailWhenQualifiersHaveTrailingDash) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 49 | EXPECT_FALSE(TestParse("en-sw600dp-land-")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | TEST(ConfigDescriptionTest, ParseBasicQualifiers) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 53 | ConfigDescription config; |
| 54 | EXPECT_TRUE(TestParse("", &config)); |
| 55 | EXPECT_EQ(std::string(""), config.toString().string()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 56 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 57 | EXPECT_TRUE(TestParse("fr-land", &config)); |
| 58 | EXPECT_EQ(std::string("fr-land"), config.toString().string()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 59 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 60 | 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | TEST(ConfigDescriptionTest, ParseLocales) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 70 | ConfigDescription config; |
| 71 | EXPECT_TRUE(TestParse("en-rUS", &config)); |
| 72 | EXPECT_EQ(std::string("en-rUS"), config.toString().string()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | TEST(ConfigDescriptionTest, ParseQualifierAddedInApi13) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 76 | ConfigDescription config; |
| 77 | EXPECT_TRUE(TestParse("sw600dp", &config)); |
| 78 | EXPECT_EQ(std::string("sw600dp-v13"), config.toString().string()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 79 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 80 | EXPECT_TRUE(TestParse("sw600dp-v8", &config)); |
| 81 | EXPECT_EQ(std::string("sw600dp-v13"), config.toString().string()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | TEST(ConfigDescriptionTest, ParseCarAttribute) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 85 | ConfigDescription config; |
| 86 | EXPECT_TRUE(TestParse("car", &config)); |
| 87 | EXPECT_EQ(android::ResTable_config::UI_MODE_TYPE_CAR, config.uiMode); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Adam Lesinski | 6425497 | 2015-11-03 16:16:17 -0800 | [diff] [blame] | 90 | TEST(ConfigDescriptionTest, TestParsingRoundQualifier) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 91 | 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 Lesinski | 6425497 | 2015-11-03 16:16:17 -0800 | [diff] [blame] | 97 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 98 | 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 Lesinski | 6425497 | 2015-11-03 16:16:17 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 105 | TEST(ConfigDescriptionTest, TestWideColorGamutQualifier) { |
| 106 | ConfigDescription config; |
| 107 | EXPECT_TRUE(TestParse("widecg", &config)); |
| 108 | EXPECT_EQ(android::ResTable_config::WIDE_COLOR_GAMUT_YES, |
Romain Guy | 4832745b | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 109 | config.colorMode & android::ResTable_config::MASK_WIDE_COLOR_GAMUT); |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 110 | 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 Guy | 4832745b | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 115 | config.colorMode & android::ResTable_config::MASK_WIDE_COLOR_GAMUT); |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 116 | EXPECT_EQ(SDK_O, config.sdkVersion); |
| 117 | EXPECT_EQ(std::string("nowidecg-v26"), config.toString().string()); |
| 118 | } |
| 119 | |
| 120 | TEST(ConfigDescriptionTest, TestHdrQualifier) { |
| 121 | ConfigDescription config; |
| 122 | EXPECT_TRUE(TestParse("highdr", &config)); |
| 123 | EXPECT_EQ(android::ResTable_config::HDR_YES, |
Romain Guy | 4832745b | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 124 | config.colorMode & android::ResTable_config::MASK_HDR); |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 125 | 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 Guy | 4832745b | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 130 | config.colorMode & android::ResTable_config::MASK_HDR); |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 131 | EXPECT_EQ(SDK_O, config.sdkVersion); |
| 132 | EXPECT_EQ(std::string("lowdr-v26"), config.toString().string()); |
| 133 | } |
| 134 | |
Zak Cohen | 1a6acdb | 2016-12-12 15:21:21 -0800 | [diff] [blame] | 135 | TEST(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 Lesinski | 5d94fb7 | 2017-08-30 16:12:05 -0700 | [diff] [blame] | 143 | TEST(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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 155 | } // namespace aapt |