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 | |
Zak Cohen | 1a6acdb | 2016-12-12 15:21:21 -0800 | [diff] [blame] | 105 | TEST(ConfigDescriptionTest, ParseVrAttribute) { |
| 106 | ConfigDescription config; |
| 107 | EXPECT_TRUE(TestParse("vrheadset", &config)); |
| 108 | EXPECT_EQ(android::ResTable_config::UI_MODE_TYPE_VR_HEADSET, config.uiMode); |
| 109 | EXPECT_EQ(SDK_O, config.sdkVersion); |
| 110 | EXPECT_EQ(std::string("vrheadset-v26"), config.toString().string()); |
| 111 | } |
| 112 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 113 | } // namespace aapt |