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 | 6425497 | 2015-11-03 16:16:17 -0800 | [diff] [blame] | 21 | #include "SdkConstants.h" |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 22 | #include "test/Test.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 23 | #include "util/StringPiece.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 24 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 25 | namespace aapt { |
| 26 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 27 | static ::testing::AssertionResult TestParse( |
| 28 | const StringPiece& input, ConfigDescription* config = nullptr) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 29 | if (ConfigDescription::Parse(input, config)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 30 | return ::testing::AssertionSuccess() << input << " was successfully parsed"; |
| 31 | } |
| 32 | return ::testing::AssertionFailure() << input << " could not be parsed"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | TEST(ConfigDescriptionTest, ParseFailWhenQualifiersAreOutOfOrder) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 36 | EXPECT_FALSE(TestParse("en-sw600dp-ldrtl")); |
| 37 | EXPECT_FALSE(TestParse("land-en")); |
| 38 | EXPECT_FALSE(TestParse("hdpi-320dpi")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | TEST(ConfigDescriptionTest, ParseFailWhenQualifiersAreNotMatched) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 42 | EXPECT_FALSE(TestParse("en-sw600dp-ILLEGAL")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | TEST(ConfigDescriptionTest, ParseFailWhenQualifiersHaveTrailingDash) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 46 | EXPECT_FALSE(TestParse("en-sw600dp-land-")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | TEST(ConfigDescriptionTest, ParseBasicQualifiers) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 50 | ConfigDescription config; |
| 51 | EXPECT_TRUE(TestParse("", &config)); |
| 52 | EXPECT_EQ(std::string(""), config.toString().string()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 53 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 54 | EXPECT_TRUE(TestParse("fr-land", &config)); |
| 55 | EXPECT_EQ(std::string("fr-land"), 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( |
| 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | TEST(ConfigDescriptionTest, ParseLocales) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 67 | ConfigDescription config; |
| 68 | EXPECT_TRUE(TestParse("en-rUS", &config)); |
| 69 | EXPECT_EQ(std::string("en-rUS"), config.toString().string()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | TEST(ConfigDescriptionTest, ParseQualifierAddedInApi13) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 73 | ConfigDescription config; |
| 74 | EXPECT_TRUE(TestParse("sw600dp", &config)); |
| 75 | EXPECT_EQ(std::string("sw600dp-v13"), config.toString().string()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 76 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | EXPECT_TRUE(TestParse("sw600dp-v8", &config)); |
| 78 | EXPECT_EQ(std::string("sw600dp-v13"), config.toString().string()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | TEST(ConfigDescriptionTest, ParseCarAttribute) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | ConfigDescription config; |
| 83 | EXPECT_TRUE(TestParse("car", &config)); |
| 84 | EXPECT_EQ(android::ResTable_config::UI_MODE_TYPE_CAR, config.uiMode); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Adam Lesinski | 6425497 | 2015-11-03 16:16:17 -0800 | [diff] [blame] | 87 | TEST(ConfigDescriptionTest, TestParsingRoundQualifier) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | 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 Lesinski | 6425497 | 2015-11-03 16:16:17 -0800 | [diff] [blame] | 94 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 95 | 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 Lesinski | 6425497 | 2015-11-03 16:16:17 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Zak Cohen | 1a6acdb | 2016-12-12 15:21:21 -0800 | [diff] [blame^] | 102 | TEST(ConfigDescriptionTest, ParseVrAttribute) { |
| 103 | ConfigDescription config; |
| 104 | EXPECT_TRUE(TestParse("vrheadset", &config)); |
| 105 | EXPECT_EQ(android::ResTable_config::UI_MODE_TYPE_VR_HEADSET, config.uiMode); |
| 106 | EXPECT_EQ(SDK_O, config.sdkVersion); |
| 107 | EXPECT_EQ(std::string("vrheadset-v26"), config.toString().string()); |
| 108 | } |
| 109 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 110 | } // namespace aapt |