blob: 6b2ef5f6a3819b1215946fd56296541b4532a401 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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 Kongstad24c9aa62018-06-20 08:46:41 +020017#include "androidfw/Locale.h"
18#include "androidfw/Util.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080019
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080020#include <string>
21
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022#include "gtest/gtest.h"
23
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020024namespace android {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080025
Adam Lesinskicacb28f2016-10-19 12:18:14 -070026static ::testing::AssertionResult TestLanguage(const char* input,
27 const char* lang) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028 std::vector<std::string> parts = util::SplitAndLowercase(input, '-');
Adam Lesinskicacb28f2016-10-19 12:18:14 -070029 LocaleValue lv;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030 ssize_t count = lv.InitFromParts(std::begin(parts), std::end(parts));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070031 if (count < 0) {
32 return ::testing::AssertionFailure() << " failed to parse '" << input
33 << "'.";
34 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080035
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 if (count != 1) {
37 return ::testing::AssertionFailure()
38 << count << " parts were consumed parsing '" << input
39 << "' but expected 1.";
40 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080041
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 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 Lesinski6f6ceb72014-11-14 14:48:12 -080048
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 return ::testing::AssertionSuccess();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080050}
51
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052static ::testing::AssertionResult TestLanguageRegion(const char* input,
53 const char* lang,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080054 const char* region) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 std::vector<std::string> parts = util::SplitAndLowercase(input, '-');
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 LocaleValue lv;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 ssize_t count = lv.InitFromParts(std::begin(parts), std::end(parts));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 if (count < 0) {
59 return ::testing::AssertionFailure() << " failed to parse '" << input
60 << "'.";
61 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080062
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 if (count != 2) {
64 return ::testing::AssertionFailure()
65 << count << " parts were consumed parsing '" << input
66 << "' but expected 2.";
67 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080068
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 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 Lesinski6f6ceb72014-11-14 14:48:12 -080075
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 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 Lesinski6f6ceb72014-11-14 14:48:12 -080082
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 return ::testing::AssertionSuccess();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084}
85
86TEST(ConfigDescriptionTest, ParseLanguage) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 EXPECT_TRUE(TestLanguage("en", "en"));
88 EXPECT_TRUE(TestLanguage("fr", "fr"));
89 EXPECT_FALSE(TestLanguage("land", ""));
90 EXPECT_TRUE(TestLanguage("fr-land", "fr"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080091
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 EXPECT_TRUE(TestLanguageRegion("fr-rCA", "fr", "CA"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080093}
94
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020095} // namespace android