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 | |
Mårten Kongstad | 5c541f6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 17 | #include "androidfw/ConfigDescription.h" |
| 18 | #include "androidfw/StringPiece.h" |
| 19 | |
| 20 | #include "android-base/logging.h" |
| 21 | |
| 22 | #include "gtest/gtest.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 23 | |
| 24 | #include <string> |
| 25 | |
Mårten Kongstad | 5c541f6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 26 | namespace android { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 27 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 28 | static ::testing::AssertionResult TestParse( |
| 29 | const StringPiece& input, ConfigDescription* config = nullptr) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 30 | if (ConfigDescription::Parse(input, config)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 31 | return ::testing::AssertionSuccess() << input << " was successfully parsed"; |
| 32 | } |
| 33 | return ::testing::AssertionFailure() << input << " could not be parsed"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | TEST(ConfigDescriptionTest, ParseFailWhenQualifiersAreOutOfOrder) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 37 | EXPECT_FALSE(TestParse("en-sw600dp-ldrtl")); |
| 38 | EXPECT_FALSE(TestParse("land-en")); |
| 39 | EXPECT_FALSE(TestParse("hdpi-320dpi")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | TEST(ConfigDescriptionTest, ParseFailWhenQualifiersAreNotMatched) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 43 | EXPECT_FALSE(TestParse("en-sw600dp-ILLEGAL")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | TEST(ConfigDescriptionTest, ParseFailWhenQualifiersHaveTrailingDash) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 47 | EXPECT_FALSE(TestParse("en-sw600dp-land-")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | TEST(ConfigDescriptionTest, ParseBasicQualifiers) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 51 | ConfigDescription config; |
| 52 | EXPECT_TRUE(TestParse("", &config)); |
| 53 | EXPECT_EQ(std::string(""), config.toString().string()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 54 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 55 | EXPECT_TRUE(TestParse("fr-land", &config)); |
| 56 | EXPECT_EQ(std::string("fr-land"), config.toString().string()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 57 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 58 | 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | TEST(ConfigDescriptionTest, ParseLocales) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 68 | ConfigDescription config; |
| 69 | EXPECT_TRUE(TestParse("en-rUS", &config)); |
| 70 | EXPECT_EQ(std::string("en-rUS"), config.toString().string()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | TEST(ConfigDescriptionTest, ParseQualifierAddedInApi13) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 74 | ConfigDescription config; |
| 75 | EXPECT_TRUE(TestParse("sw600dp", &config)); |
| 76 | EXPECT_EQ(std::string("sw600dp-v13"), config.toString().string()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 77 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 78 | EXPECT_TRUE(TestParse("sw600dp-v8", &config)); |
| 79 | EXPECT_EQ(std::string("sw600dp-v13"), config.toString().string()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | TEST(ConfigDescriptionTest, ParseCarAttribute) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 83 | ConfigDescription config; |
| 84 | EXPECT_TRUE(TestParse("car", &config)); |
| 85 | EXPECT_EQ(android::ResTable_config::UI_MODE_TYPE_CAR, config.uiMode); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Adam Lesinski | 6425497 | 2015-11-03 16:16:17 -0800 | [diff] [blame] | 88 | TEST(ConfigDescriptionTest, TestParsingRoundQualifier) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 89 | 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 Lesinski | 6425497 | 2015-11-03 16:16:17 -0800 | [diff] [blame] | 95 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 96 | 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 Lesinski | 6425497 | 2015-11-03 16:16:17 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 103 | TEST(ConfigDescriptionTest, TestWideColorGamutQualifier) { |
| 104 | ConfigDescription config; |
| 105 | EXPECT_TRUE(TestParse("widecg", &config)); |
| 106 | EXPECT_EQ(android::ResTable_config::WIDE_COLOR_GAMUT_YES, |
Romain Guy | 4832745 | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 107 | config.colorMode & android::ResTable_config::MASK_WIDE_COLOR_GAMUT); |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 108 | 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 Guy | 4832745 | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 113 | config.colorMode & android::ResTable_config::MASK_WIDE_COLOR_GAMUT); |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 114 | EXPECT_EQ(SDK_O, config.sdkVersion); |
| 115 | EXPECT_EQ(std::string("nowidecg-v26"), config.toString().string()); |
| 116 | } |
| 117 | |
| 118 | TEST(ConfigDescriptionTest, TestHdrQualifier) { |
| 119 | ConfigDescription config; |
| 120 | EXPECT_TRUE(TestParse("highdr", &config)); |
| 121 | EXPECT_EQ(android::ResTable_config::HDR_YES, |
Romain Guy | 4832745 | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 122 | config.colorMode & android::ResTable_config::MASK_HDR); |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 123 | 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 Guy | 4832745 | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 128 | config.colorMode & android::ResTable_config::MASK_HDR); |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 129 | EXPECT_EQ(SDK_O, config.sdkVersion); |
| 130 | EXPECT_EQ(std::string("lowdr-v26"), config.toString().string()); |
| 131 | } |
| 132 | |
Zak Cohen | 1a6acdb | 2016-12-12 15:21:21 -0800 | [diff] [blame] | 133 | TEST(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 Kongstad | 5c541f6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 141 | static inline ConfigDescription ParseConfigOrDie(const android::StringPiece& str) { |
| 142 | ConfigDescription config; |
| 143 | CHECK(ConfigDescription::Parse(str, &config)) << "invalid configuration: " << str; |
| 144 | return config; |
| 145 | } |
Adam Lesinski | a91d5c3 | 2017-08-30 16:12:05 -0700 | [diff] [blame] | 146 | |
Mårten Kongstad | 5c541f6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 147 | TEST(ConfigDescriptionTest, RangeQualifiersDoNotConflict) { |
Adam Lesinski | a91d5c3 | 2017-08-30 16:12:05 -0700 | [diff] [blame] | 148 | 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 Kongstad | 5c541f6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 157 | } // namespace android |