| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/memory/linked_ptr.h" | 8 #include "base/memory/linked_ptr.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 namespace keys = extensions::manifest_keys; | 28 namespace keys = extensions::manifest_keys; |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 TEST(ExtensionL10nUtil, ValidateLocalesWithBadLocale) { | 32 TEST(ExtensionL10nUtil, ValidateLocalesWithBadLocale) { |
| 33 base::ScopedTempDir temp; | 33 base::ScopedTempDir temp; |
| 34 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 34 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 35 | 35 |
| 36 base::FilePath src_path = temp.path().Append(kLocaleFolder); | 36 base::FilePath src_path = temp.path().Append(kLocaleFolder); |
| 37 base::FilePath locale = src_path.AppendASCII("ms"); | 37 base::FilePath locale = src_path.AppendASCII("ms"); |
| 38 ASSERT_TRUE(file_util::CreateDirectory(locale)); | 38 ASSERT_TRUE(base::CreateDirectory(locale)); |
| 39 | 39 |
| 40 base::FilePath messages_file = locale.Append(kMessagesFilename); | 40 base::FilePath messages_file = locale.Append(kMessagesFilename); |
| 41 std::string data = "{ \"name\":"; | 41 std::string data = "{ \"name\":"; |
| 42 ASSERT_TRUE(file_util::WriteFile(messages_file, data.c_str(), data.length())); | 42 ASSERT_TRUE(file_util::WriteFile(messages_file, data.c_str(), data.length())); |
| 43 | 43 |
| 44 base::DictionaryValue manifest; | 44 base::DictionaryValue manifest; |
| 45 manifest.SetString(keys::kDefaultLocale, "en"); | 45 manifest.SetString(keys::kDefaultLocale, "en"); |
| 46 std::string error; | 46 std::string error; |
| 47 EXPECT_FALSE(extension_l10n_util::ValidateExtensionLocales( | 47 EXPECT_FALSE(extension_l10n_util::ValidateExtensionLocales( |
| 48 temp.path(), &manifest, &error)); | 48 temp.path(), &manifest, &error)); |
| 49 EXPECT_THAT(error, | 49 EXPECT_THAT(error, |
| 50 testing::HasSubstr( | 50 testing::HasSubstr( |
| 51 UTF16ToUTF8(messages_file.LossyDisplayName()))); | 51 UTF16ToUTF8(messages_file.LossyDisplayName()))); |
| 52 } | 52 } |
| 53 | 53 |
| 54 TEST(ExtensionL10nUtil, GetValidLocalesEmptyLocaleFolder) { | 54 TEST(ExtensionL10nUtil, GetValidLocalesEmptyLocaleFolder) { |
| 55 base::ScopedTempDir temp; | 55 base::ScopedTempDir temp; |
| 56 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 56 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 57 | 57 |
| 58 base::FilePath src_path = temp.path().Append(kLocaleFolder); | 58 base::FilePath src_path = temp.path().Append(kLocaleFolder); |
| 59 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 59 ASSERT_TRUE(base::CreateDirectory(src_path)); |
| 60 | 60 |
| 61 std::string error; | 61 std::string error; |
| 62 std::set<std::string> locales; | 62 std::set<std::string> locales; |
| 63 EXPECT_FALSE(extension_l10n_util::GetValidLocales(src_path, | 63 EXPECT_FALSE(extension_l10n_util::GetValidLocales(src_path, |
| 64 &locales, | 64 &locales, |
| 65 &error)); | 65 &error)); |
| 66 | 66 |
| 67 EXPECT_TRUE(locales.empty()); | 67 EXPECT_TRUE(locales.empty()); |
| 68 } | 68 } |
| 69 | 69 |
| 70 TEST(ExtensionL10nUtil, GetValidLocalesWithValidLocaleNoMessagesFile) { | 70 TEST(ExtensionL10nUtil, GetValidLocalesWithValidLocaleNoMessagesFile) { |
| 71 base::ScopedTempDir temp; | 71 base::ScopedTempDir temp; |
| 72 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 72 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 73 | 73 |
| 74 base::FilePath src_path = temp.path().Append(kLocaleFolder); | 74 base::FilePath src_path = temp.path().Append(kLocaleFolder); |
| 75 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 75 ASSERT_TRUE(base::CreateDirectory(src_path)); |
| 76 ASSERT_TRUE(file_util::CreateDirectory(src_path.AppendASCII("sr"))); | 76 ASSERT_TRUE(base::CreateDirectory(src_path.AppendASCII("sr"))); |
| 77 | 77 |
| 78 std::string error; | 78 std::string error; |
| 79 std::set<std::string> locales; | 79 std::set<std::string> locales; |
| 80 EXPECT_FALSE(extension_l10n_util::GetValidLocales(src_path, | 80 EXPECT_FALSE(extension_l10n_util::GetValidLocales(src_path, |
| 81 &locales, | 81 &locales, |
| 82 &error)); | 82 &error)); |
| 83 | 83 |
| 84 EXPECT_TRUE(locales.empty()); | 84 EXPECT_TRUE(locales.empty()); |
| 85 } | 85 } |
| 86 | 86 |
| 87 TEST(ExtensionL10nUtil, GetValidLocalesWithUnsupportedLocale) { | 87 TEST(ExtensionL10nUtil, GetValidLocalesWithUnsupportedLocale) { |
| 88 base::ScopedTempDir temp; | 88 base::ScopedTempDir temp; |
| 89 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 89 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 90 | 90 |
| 91 base::FilePath src_path = temp.path().Append(kLocaleFolder); | 91 base::FilePath src_path = temp.path().Append(kLocaleFolder); |
| 92 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 92 ASSERT_TRUE(base::CreateDirectory(src_path)); |
| 93 // Supported locale. | 93 // Supported locale. |
| 94 base::FilePath locale_1 = src_path.AppendASCII("sr"); | 94 base::FilePath locale_1 = src_path.AppendASCII("sr"); |
| 95 ASSERT_TRUE(file_util::CreateDirectory(locale_1)); | 95 ASSERT_TRUE(base::CreateDirectory(locale_1)); |
| 96 std::string data("whatever"); | 96 std::string data("whatever"); |
| 97 ASSERT_TRUE(file_util::WriteFile( | 97 ASSERT_TRUE(file_util::WriteFile( |
| 98 locale_1.Append(kMessagesFilename), | 98 locale_1.Append(kMessagesFilename), |
| 99 data.c_str(), data.length())); | 99 data.c_str(), data.length())); |
| 100 // Unsupported locale. | 100 // Unsupported locale. |
| 101 ASSERT_TRUE(file_util::CreateDirectory(src_path.AppendASCII("xxx_yyy"))); | 101 ASSERT_TRUE(base::CreateDirectory(src_path.AppendASCII("xxx_yyy"))); |
| 102 | 102 |
| 103 std::string error; | 103 std::string error; |
| 104 std::set<std::string> locales; | 104 std::set<std::string> locales; |
| 105 EXPECT_TRUE(extension_l10n_util::GetValidLocales(src_path, | 105 EXPECT_TRUE(extension_l10n_util::GetValidLocales(src_path, |
| 106 &locales, | 106 &locales, |
| 107 &error)); | 107 &error)); |
| 108 | 108 |
| 109 EXPECT_FALSE(locales.empty()); | 109 EXPECT_FALSE(locales.empty()); |
| 110 EXPECT_TRUE(locales.find("sr") != locales.end()); | 110 EXPECT_TRUE(locales.find("sr") != locales.end()); |
| 111 EXPECT_FALSE(locales.find("xxx_yyy") != locales.end()); | 111 EXPECT_FALSE(locales.find("xxx_yyy") != locales.end()); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 EXPECT_TRUE(error.empty()); | 154 EXPECT_TRUE(error.empty()); |
| 155 EXPECT_EQ("Color", bundle->GetL10nMessage("color")); | 155 EXPECT_EQ("Color", bundle->GetL10nMessage("color")); |
| 156 EXPECT_EQ("Not in the US or GB.", bundle->GetL10nMessage("not_in_US_or_GB")); | 156 EXPECT_EQ("Not in the US or GB.", bundle->GetL10nMessage("not_in_US_or_GB")); |
| 157 } | 157 } |
| 158 | 158 |
| 159 TEST(ExtensionL10nUtil, LoadMessageCatalogsMissingFiles) { | 159 TEST(ExtensionL10nUtil, LoadMessageCatalogsMissingFiles) { |
| 160 base::ScopedTempDir temp; | 160 base::ScopedTempDir temp; |
| 161 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 161 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 162 | 162 |
| 163 base::FilePath src_path = temp.path().Append(kLocaleFolder); | 163 base::FilePath src_path = temp.path().Append(kLocaleFolder); |
| 164 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 164 ASSERT_TRUE(base::CreateDirectory(src_path)); |
| 165 | 165 |
| 166 std::set<std::string> valid_locales; | 166 std::set<std::string> valid_locales; |
| 167 valid_locales.insert("sr"); | 167 valid_locales.insert("sr"); |
| 168 valid_locales.insert("en"); | 168 valid_locales.insert("en"); |
| 169 std::string error; | 169 std::string error; |
| 170 EXPECT_TRUE(NULL == extension_l10n_util::LoadMessageCatalogs(src_path, | 170 EXPECT_TRUE(NULL == extension_l10n_util::LoadMessageCatalogs(src_path, |
| 171 "en", | 171 "en", |
| 172 "sr", | 172 "sr", |
| 173 valid_locales, | 173 valid_locales, |
| 174 &error)); | 174 &error)); |
| 175 EXPECT_FALSE(error.empty()); | 175 EXPECT_FALSE(error.empty()); |
| 176 } | 176 } |
| 177 | 177 |
| 178 TEST(ExtensionL10nUtil, LoadMessageCatalogsBadJSONFormat) { | 178 TEST(ExtensionL10nUtil, LoadMessageCatalogsBadJSONFormat) { |
| 179 base::ScopedTempDir temp; | 179 base::ScopedTempDir temp; |
| 180 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 180 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 181 | 181 |
| 182 base::FilePath src_path = temp.path().Append(kLocaleFolder); | 182 base::FilePath src_path = temp.path().Append(kLocaleFolder); |
| 183 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 183 ASSERT_TRUE(base::CreateDirectory(src_path)); |
| 184 | 184 |
| 185 base::FilePath locale = src_path.AppendASCII("sr"); | 185 base::FilePath locale = src_path.AppendASCII("sr"); |
| 186 ASSERT_TRUE(file_util::CreateDirectory(locale)); | 186 ASSERT_TRUE(base::CreateDirectory(locale)); |
| 187 | 187 |
| 188 std::string data = "{ \"name\":"; | 188 std::string data = "{ \"name\":"; |
| 189 base::FilePath messages_file = locale.Append(kMessagesFilename); | 189 base::FilePath messages_file = locale.Append(kMessagesFilename); |
| 190 ASSERT_TRUE(file_util::WriteFile(messages_file, data.c_str(), data.length())); | 190 ASSERT_TRUE(file_util::WriteFile(messages_file, data.c_str(), data.length())); |
| 191 | 191 |
| 192 std::set<std::string> valid_locales; | 192 std::set<std::string> valid_locales; |
| 193 valid_locales.insert("sr"); | 193 valid_locales.insert("sr"); |
| 194 valid_locales.insert("en_US"); | 194 valid_locales.insert("en_US"); |
| 195 std::string error; | 195 std::string error; |
| 196 EXPECT_TRUE(NULL == extension_l10n_util::LoadMessageCatalogs(src_path, | 196 EXPECT_TRUE(NULL == extension_l10n_util::LoadMessageCatalogs(src_path, |
| 197 "en_US", | 197 "en_US", |
| 198 "sr", | 198 "sr", |
| 199 valid_locales, | 199 valid_locales, |
| 200 &error)); | 200 &error)); |
| 201 EXPECT_EQ( | 201 EXPECT_EQ( |
| 202 extensions::ErrorUtils::FormatErrorMessage( | 202 extensions::ErrorUtils::FormatErrorMessage( |
| 203 errors::kLocalesInvalidLocale, | 203 errors::kLocalesInvalidLocale, |
| 204 base::UTF16ToUTF8(messages_file.LossyDisplayName()), | 204 base::UTF16ToUTF8(messages_file.LossyDisplayName()), |
| 205 "Line: 1, column: 10, Unexpected token."), | 205 "Line: 1, column: 10, Unexpected token."), |
| 206 error); | 206 error); |
| 207 } | 207 } |
| 208 | 208 |
| 209 TEST(ExtensionL10nUtil, LoadMessageCatalogsDuplicateKeys) { | 209 TEST(ExtensionL10nUtil, LoadMessageCatalogsDuplicateKeys) { |
| 210 base::ScopedTempDir temp; | 210 base::ScopedTempDir temp; |
| 211 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 211 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 212 | 212 |
| 213 base::FilePath src_path = temp.path().Append(kLocaleFolder); | 213 base::FilePath src_path = temp.path().Append(kLocaleFolder); |
| 214 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 214 ASSERT_TRUE(base::CreateDirectory(src_path)); |
| 215 | 215 |
| 216 base::FilePath locale_1 = src_path.AppendASCII("en"); | 216 base::FilePath locale_1 = src_path.AppendASCII("en"); |
| 217 ASSERT_TRUE(file_util::CreateDirectory(locale_1)); | 217 ASSERT_TRUE(base::CreateDirectory(locale_1)); |
| 218 | 218 |
| 219 std::string data = | 219 std::string data = |
| 220 "{ \"name\": { \"message\": \"something\" }, " | 220 "{ \"name\": { \"message\": \"something\" }, " |
| 221 "\"name\": { \"message\": \"something else\" } }"; | 221 "\"name\": { \"message\": \"something else\" } }"; |
| 222 ASSERT_TRUE( | 222 ASSERT_TRUE( |
| 223 file_util::WriteFile(locale_1.Append(kMessagesFilename), | 223 file_util::WriteFile(locale_1.Append(kMessagesFilename), |
| 224 data.c_str(), data.length())); | 224 data.c_str(), data.length())); |
| 225 | 225 |
| 226 base::FilePath locale_2 = src_path.AppendASCII("sr"); | 226 base::FilePath locale_2 = src_path.AppendASCII("sr"); |
| 227 ASSERT_TRUE(file_util::CreateDirectory(locale_2)); | 227 ASSERT_TRUE(base::CreateDirectory(locale_2)); |
| 228 | 228 |
| 229 ASSERT_TRUE( | 229 ASSERT_TRUE( |
| 230 file_util::WriteFile(locale_2.Append(kMessagesFilename), | 230 file_util::WriteFile(locale_2.Append(kMessagesFilename), |
| 231 data.c_str(), data.length())); | 231 data.c_str(), data.length())); |
| 232 | 232 |
| 233 std::set<std::string> valid_locales; | 233 std::set<std::string> valid_locales; |
| 234 valid_locales.insert("sr"); | 234 valid_locales.insert("sr"); |
| 235 valid_locales.insert("en"); | 235 valid_locales.insert("en"); |
| 236 std::string error; | 236 std::string error; |
| 237 // JSON parser hides duplicates. We are going to get only one key/value | 237 // JSON parser hides duplicates. We are going to get only one key/value |
| (...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 659 std::vector<std::string> fallback_locales; | 659 std::vector<std::string> fallback_locales; |
| 660 extension_l10n_util::GetAllFallbackLocales("en_US", "all", &fallback_locales); | 660 extension_l10n_util::GetAllFallbackLocales("en_US", "all", &fallback_locales); |
| 661 ASSERT_EQ(3U, fallback_locales.size()); | 661 ASSERT_EQ(3U, fallback_locales.size()); |
| 662 | 662 |
| 663 CHECK_EQ("en_US", fallback_locales[0]); | 663 CHECK_EQ("en_US", fallback_locales[0]); |
| 664 CHECK_EQ("en", fallback_locales[1]); | 664 CHECK_EQ("en", fallback_locales[1]); |
| 665 CHECK_EQ("all", fallback_locales[2]); | 665 CHECK_EQ("all", fallback_locales[2]); |
| 666 } | 666 } |
| 667 | 667 |
| 668 } // namespace | 668 } // namespace |
| OLD | NEW |