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 | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 17 | #include "androidfw/Locale.h" |
| 18 | #include "androidfw/Util.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 19 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 20 | #include <string> |
| 21 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 22 | #include "gtest/gtest.h" |
| 23 | |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 24 | namespace android { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 25 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 26 | static ::testing::AssertionResult TestLanguage(const char* input, |
| 27 | const char* lang) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 28 | std::vector<std::string> parts = util::SplitAndLowercase(input, '-'); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 29 | LocaleValue lv; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 30 | ssize_t count = lv.InitFromParts(std::begin(parts), std::end(parts)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 31 | if (count < 0) { |
| 32 | return ::testing::AssertionFailure() << " failed to parse '" << input |
| 33 | << "'."; |
| 34 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 35 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 36 | if (count != 1) { |
| 37 | return ::testing::AssertionFailure() |
| 38 | << count << " parts were consumed parsing '" << input |
| 39 | << "' but expected 1."; |
| 40 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 41 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 42 | if (memcmp(lv.language, lang, std::min(strlen(lang), sizeof(lv.language))) != |
| 43 | 0) { |
| 44 | return ::testing::AssertionFailure() |
| 45 | << "expected " << lang << " but got " |
| 46 | << std::string(lv.language, sizeof(lv.language)) << "."; |
| 47 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 48 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 49 | return ::testing::AssertionSuccess(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 52 | static ::testing::AssertionResult TestLanguageRegion(const char* input, |
| 53 | const char* lang, |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 54 | const char* region) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 55 | std::vector<std::string> parts = util::SplitAndLowercase(input, '-'); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | LocaleValue lv; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 57 | ssize_t count = lv.InitFromParts(std::begin(parts), std::end(parts)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 58 | if (count < 0) { |
| 59 | return ::testing::AssertionFailure() << " failed to parse '" << input |
| 60 | << "'."; |
| 61 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 62 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 63 | if (count != 2) { |
| 64 | return ::testing::AssertionFailure() |
| 65 | << count << " parts were consumed parsing '" << input |
| 66 | << "' but expected 2."; |
| 67 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 68 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 69 | if (memcmp(lv.language, lang, std::min(strlen(lang), sizeof(lv.language))) != |
| 70 | 0) { |
| 71 | return ::testing::AssertionFailure() |
| 72 | << "expected " << input << " but got " |
| 73 | << std::string(lv.language, sizeof(lv.language)) << "."; |
| 74 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 75 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 76 | if (memcmp(lv.region, region, std::min(strlen(region), sizeof(lv.region))) != |
| 77 | 0) { |
| 78 | return ::testing::AssertionFailure() |
| 79 | << "expected " << region << " but got " |
| 80 | << std::string(lv.region, sizeof(lv.region)) << "."; |
| 81 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 82 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 83 | return ::testing::AssertionSuccess(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | TEST(ConfigDescriptionTest, ParseLanguage) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 87 | EXPECT_TRUE(TestLanguage("en", "en")); |
| 88 | EXPECT_TRUE(TestLanguage("fr", "fr")); |
| 89 | EXPECT_FALSE(TestLanguage("land", "")); |
| 90 | EXPECT_TRUE(TestLanguage("fr-land", "fr")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 91 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 92 | EXPECT_TRUE(TestLanguageRegion("fr-rCA", "fr", "CA")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 95 | } // namespace android |