diff options
Diffstat (limited to 'libs/androidfw/tests')
-rw-r--r-- | libs/androidfw/tests/Android.mk | 3 | ||||
-rw-r--r-- | libs/androidfw/tests/ResourceTypes_test.cpp | 185 |
2 files changed, 187 insertions, 1 deletions
diff --git a/libs/androidfw/tests/Android.mk b/libs/androidfw/tests/Android.mk index 3c55375..977ba80 100644 --- a/libs/androidfw/tests/Android.mk +++ b/libs/androidfw/tests/Android.mk @@ -5,7 +5,8 @@ include $(CLEAR_VARS) # Build the unit tests. test_src_files := \ ObbFile_test.cpp \ - ZipUtils_test.cpp + ZipUtils_test.cpp \ + ResourceTypes_test.cpp shared_libraries := \ libandroidfw \ diff --git a/libs/androidfw/tests/ResourceTypes_test.cpp b/libs/androidfw/tests/ResourceTypes_test.cpp new file mode 100644 index 0000000..4888b4a --- /dev/null +++ b/libs/androidfw/tests/ResourceTypes_test.cpp @@ -0,0 +1,185 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <androidfw/ResourceTypes.h> +#include <utils/Log.h> +#include <utils/String8.h> + +#include <gtest/gtest.h> +namespace android { + +TEST(ResourceTypesTest, ResourceConfig_packAndUnpack2LetterLanguage) { + ResTable_config config; + config.packLanguage("en"); + + EXPECT_EQ('e', config.language[0]); + EXPECT_EQ('n', config.language[1]); + + char out[4] = { 1, 1, 1, 1}; + config.unpackLanguage(out); + EXPECT_EQ('e', out[0]); + EXPECT_EQ('n', out[1]); + EXPECT_EQ(0, out[2]); + EXPECT_EQ(0, out[3]); + + memset(out, 1, sizeof(out)); + config.locale = 0; + config.unpackLanguage(out); + EXPECT_EQ(0, out[0]); + EXPECT_EQ(0, out[1]); + EXPECT_EQ(0, out[2]); + EXPECT_EQ(0, out[3]); +} + +TEST(ResourceTypesTest, ResourceConfig_packAndUnpack2LetterRegion) { + ResTable_config config; + config.packRegion("US"); + + EXPECT_EQ('U', config.country[0]); + EXPECT_EQ('S', config.country[1]); + + char out[4] = { 1, 1, 1, 1}; + config.unpackRegion(out); + EXPECT_EQ('U', out[0]); + EXPECT_EQ('S', out[1]); + EXPECT_EQ(0, out[2]); + EXPECT_EQ(0, out[3]); +} + +TEST(ResourceTypesTest, ResourceConfig_packAndUnpack3LetterLanguage) { + ResTable_config config; + config.packLanguage("eng"); + + // 1-00110-01 101-00100 + EXPECT_EQ(0x99, config.language[0]); + EXPECT_EQ(0xa4, config.language[1]); + + char out[4] = { 1, 1, 1, 1}; + config.unpackLanguage(out); + EXPECT_EQ('e', out[0]); + EXPECT_EQ('n', out[1]); + EXPECT_EQ('g', out[2]); + EXPECT_EQ(0, out[3]); +} + +TEST(ResourceTypesTest, ResourceConfig_packAndUnpack3LetterRegion) { + ResTable_config config; + config.packRegion("419"); + + char out[4] = { 1, 1, 1, 1}; + config.unpackRegion(out); + + EXPECT_EQ('4', out[0]); + EXPECT_EQ('1', out[1]); + EXPECT_EQ('9', out[2]); +} + +/* static */ void fillIn(const char* lang, const char* country, + const char* script, const char* variant, ResTable_config* out) { + memset(out, 0, sizeof(ResTable_config)); + if (lang != NULL) { + out->packLanguage(lang); + } + + if (country != NULL) { + out->packRegion(country); + } + + if (script != NULL) { + memcpy(out->localeScript, script, 4); + } + + if (variant != NULL) { + memcpy(out->localeVariant, variant, strlen(variant)); + } +} + +TEST(ResourceTypesTest, IsMoreSpecificThan) { + ResTable_config l; + ResTable_config r; + + fillIn("en", NULL, NULL, NULL, &l); + fillIn(NULL, NULL, NULL, NULL, &r); + + EXPECT_TRUE(l.isMoreSpecificThan(r)); + EXPECT_FALSE(r.isMoreSpecificThan(l)); + + fillIn("eng", NULL, NULL, NULL, &l); + EXPECT_TRUE(l.isMoreSpecificThan(r)); + EXPECT_FALSE(r.isMoreSpecificThan(l)); + + fillIn("eng", "419", NULL, NULL, &r); + EXPECT_FALSE(l.isMoreSpecificThan(r)); + EXPECT_TRUE(r.isMoreSpecificThan(l)); + + fillIn("en", NULL, NULL, NULL, &l); + fillIn("en", "US", NULL, NULL, &r); + EXPECT_FALSE(l.isMoreSpecificThan(r)); + EXPECT_TRUE(r.isMoreSpecificThan(l)); + + fillIn("en", "US", NULL, NULL, &l); + fillIn("en", "US", "Latn", NULL, &r); + EXPECT_FALSE(l.isMoreSpecificThan(r)); + EXPECT_TRUE(r.isMoreSpecificThan(l)); + + fillIn("en", "US", NULL, NULL, &l); + fillIn("en", "US", NULL, "POSIX", &r); + EXPECT_FALSE(l.isMoreSpecificThan(r)); + EXPECT_TRUE(r.isMoreSpecificThan(l)); + + fillIn("en", "US", "Latn", NULL, &l); + fillIn("en", "US", NULL, "POSIX", &r); + EXPECT_FALSE(l.isMoreSpecificThan(r)); + EXPECT_TRUE(r.isMoreSpecificThan(l)); +} + +TEST(ResourceTypesTest, setLocale) { + ResTable_config test; + test.setBcp47Locale("en-US"); + EXPECT_EQ('e', test.language[0]); + EXPECT_EQ('n', test.language[1]); + EXPECT_EQ('U', test.country[0]); + EXPECT_EQ('S', test.country[1]); + EXPECT_EQ(0, test.localeScript[0]); + EXPECT_EQ(0, test.localeVariant[0]); + + test.setBcp47Locale("eng-419"); + char out[4] = { 1, 1, 1, 1}; + test.unpackLanguage(out); + EXPECT_EQ('e', out[0]); + EXPECT_EQ('n', out[1]); + EXPECT_EQ('g', out[2]); + EXPECT_EQ(0, out[3]); + memset(out, 1, 4); + test.unpackRegion(out); + EXPECT_EQ('4', out[0]); + EXPECT_EQ('1', out[1]); + EXPECT_EQ('9', out[2]); + + + test.setBcp47Locale("en-Latn-419"); + memset(out, 1, 4); + EXPECT_EQ('e', test.language[0]); + EXPECT_EQ('n', test.language[1]); + + EXPECT_EQ(0, memcmp("Latn", test.localeScript, 4)); + test.unpackRegion(out); + EXPECT_EQ('4', out[0]); + EXPECT_EQ('1', out[1]); + EXPECT_EQ('9', out[2]); +} + +} // namespace android. |