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 "androidfw/Locale.h" 18 #include "androidfw/Util.h" 19 20 #include <string> 21 22 #include "gtest/gtest.h" 23 24 namespace android { 25 26 static ::testing::AssertionResult TestLanguage(const char* input, 27 const char* lang) { 28 std::vector<std::string> parts = util::SplitAndLowercase(input, '-'); 29 LocaleValue lv; 30 ssize_t count = lv.InitFromParts(std::begin(parts), std::end(parts)); 31 if (count < 0) { 32 return ::testing::AssertionFailure() << " failed to parse '" << input 33 << "'."; 34 } 35 36 if (count != 1) { 37 return ::testing::AssertionFailure() 38 << count << " parts were consumed parsing '" << input 39 << "' but expected 1."; 40 } 41 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 } 48 49 return ::testing::AssertionSuccess(); 50 } 51 52 static ::testing::AssertionResult TestLanguageRegion(const char* input, 53 const char* lang, 54 const char* region) { 55 std::vector<std::string> parts = util::SplitAndLowercase(input, '-'); 56 LocaleValue lv; 57 ssize_t count = lv.InitFromParts(std::begin(parts), std::end(parts)); 58 if (count < 0) { 59 return ::testing::AssertionFailure() << " failed to parse '" << input 60 << "'."; 61 } 62 63 if (count != 2) { 64 return ::testing::AssertionFailure() 65 << count << " parts were consumed parsing '" << input 66 << "' but expected 2."; 67 } 68 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 } 75 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 } 82 83 return ::testing::AssertionSuccess(); 84 } 85 86 TEST(ConfigDescriptionTest, ParseLanguage) { 87 EXPECT_TRUE(TestLanguage("en", "en")); 88 EXPECT_TRUE(TestLanguage("fr", "fr")); 89 EXPECT_FALSE(TestLanguage("land", "")); 90 EXPECT_TRUE(TestLanguage("fr-land", "fr")); 91 92 EXPECT_TRUE(TestLanguageRegion("fr-rCA", "fr", "CA")); 93 } 94 95 } // namespace android 96