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 | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 18 | #include "util/StringPiece.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 19 | |
| 20 | #include <gtest/gtest.h> |
| 21 | #include <string> |
| 22 | |
| 23 | namespace aapt { |
| 24 | |
| 25 | static ::testing::AssertionResult TestParse(const StringPiece& input, |
| 26 | ConfigDescription* config = nullptr) { |
| 27 | if (ConfigDescription::parse(input, config)) { |
| 28 | return ::testing::AssertionSuccess() << input << " was successfully parsed"; |
| 29 | } |
| 30 | return ::testing::AssertionFailure() << input << " could not be parsed"; |
| 31 | } |
| 32 | |
| 33 | TEST(ConfigDescriptionTest, ParseFailWhenQualifiersAreOutOfOrder) { |
| 34 | EXPECT_FALSE(TestParse("en-sw600dp-ldrtl")); |
| 35 | EXPECT_FALSE(TestParse("land-en")); |
| 36 | EXPECT_FALSE(TestParse("hdpi-320dpi")); |
| 37 | } |
| 38 | |
| 39 | TEST(ConfigDescriptionTest, ParseFailWhenQualifiersAreNotMatched) { |
| 40 | EXPECT_FALSE(TestParse("en-sw600dp-ILLEGAL")); |
| 41 | } |
| 42 | |
| 43 | TEST(ConfigDescriptionTest, ParseFailWhenQualifiersHaveTrailingDash) { |
| 44 | EXPECT_FALSE(TestParse("en-sw600dp-land-")); |
| 45 | } |
| 46 | |
| 47 | TEST(ConfigDescriptionTest, ParseBasicQualifiers) { |
| 48 | ConfigDescription config; |
| 49 | EXPECT_TRUE(TestParse("", &config)); |
| 50 | EXPECT_EQ(std::string(""), config.toString().string()); |
| 51 | |
| 52 | EXPECT_TRUE(TestParse("fr-land", &config)); |
| 53 | EXPECT_EQ(std::string("fr-land"), config.toString().string()); |
| 54 | |
| 55 | EXPECT_TRUE(TestParse("mcc310-pl-sw720dp-normal-long-port-night-" |
| 56 | "xhdpi-keyssoft-qwerty-navexposed-nonav", &config)); |
| 57 | EXPECT_EQ(std::string("mcc310-pl-sw720dp-normal-long-port-night-" |
| 58 | "xhdpi-keyssoft-qwerty-navexposed-nonav-v13"), config.toString().string()); |
| 59 | } |
| 60 | |
| 61 | TEST(ConfigDescriptionTest, ParseLocales) { |
| 62 | ConfigDescription config; |
| 63 | EXPECT_TRUE(TestParse("en-rUS", &config)); |
| 64 | EXPECT_EQ(std::string("en-rUS"), config.toString().string()); |
| 65 | } |
| 66 | |
| 67 | TEST(ConfigDescriptionTest, ParseQualifierAddedInApi13) { |
| 68 | ConfigDescription config; |
| 69 | EXPECT_TRUE(TestParse("sw600dp", &config)); |
| 70 | EXPECT_EQ(std::string("sw600dp-v13"), config.toString().string()); |
| 71 | |
| 72 | EXPECT_TRUE(TestParse("sw600dp-v8", &config)); |
| 73 | EXPECT_EQ(std::string("sw600dp-v13"), config.toString().string()); |
| 74 | } |
| 75 | |
| 76 | TEST(ConfigDescriptionTest, ParseCarAttribute) { |
| 77 | ConfigDescription config; |
| 78 | EXPECT_TRUE(TestParse("car", &config)); |
| 79 | EXPECT_EQ(android::ResTable_config::UI_MODE_TYPE_CAR, config.uiMode); |
| 80 | } |
| 81 | |
| 82 | } // namespace aapt |