| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/browser/extensions/extension_l10n_util.h" | 5 #include "chrome/browser/extensions/extension_l10n_util.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 .AppendASCII("Extensions") | 59 .AppendASCII("Extensions") |
| 60 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") | 60 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") |
| 61 .AppendASCII("1.0.0.0") | 61 .AppendASCII("1.0.0.0") |
| 62 .AppendASCII(Extension::kLocaleFolder); | 62 .AppendASCII(Extension::kLocaleFolder); |
| 63 | 63 |
| 64 std::string error; | 64 std::string error; |
| 65 std::set<std::string> locales; | 65 std::set<std::string> locales; |
| 66 EXPECT_TRUE(extension_l10n_util::GetValidLocales(install_dir, | 66 EXPECT_TRUE(extension_l10n_util::GetValidLocales(install_dir, |
| 67 &locales, | 67 &locales, |
| 68 &error)); | 68 &error)); |
| 69 EXPECT_EQ(2U, locales.size()); | 69 EXPECT_EQ(3U, locales.size()); |
| 70 EXPECT_TRUE(locales.find("sr") != locales.end()); | 70 EXPECT_TRUE(locales.find("sr") != locales.end()); |
| 71 EXPECT_TRUE(locales.find("en") != locales.end()); |
| 72 EXPECT_TRUE(locales.find("en_US") != locales.end()); |
| 73 } |
| 74 |
| 75 TEST(ExtensionL10nUtil, LoadMessageCatalogsValidFallback) { |
| 76 FilePath install_dir; |
| 77 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); |
| 78 install_dir = install_dir.AppendASCII("extensions") |
| 79 .AppendASCII("good") |
| 80 .AppendASCII("Extensions") |
| 81 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") |
| 82 .AppendASCII("1.0.0.0") |
| 83 .AppendASCII(Extension::kLocaleFolder); |
| 84 |
| 85 std::string error; |
| 86 std::set<std::string> locales; |
| 87 EXPECT_TRUE(extension_l10n_util::GetValidLocales(install_dir, |
| 88 &locales, |
| 89 &error)); |
| 90 |
| 91 scoped_ptr<ExtensionMessageBundle> bundle( |
| 92 extension_l10n_util::LoadMessageCatalogs( |
| 93 install_dir, "sr", "en_US", locales, &error)); |
| 94 ASSERT_FALSE(NULL == bundle.get()); |
| 95 EXPECT_TRUE(error.empty()); |
| 96 EXPECT_EQ("Color", bundle->GetL10nMessage("color")); |
| 97 EXPECT_EQ("Not in the US or GB.", bundle->GetL10nMessage("not_in_US_or_GB")); |
| 71 } | 98 } |
| 72 | 99 |
| 73 TEST(ExtensionL10nUtil, LoadMessageCatalogsMissingFiles) { | 100 TEST(ExtensionL10nUtil, LoadMessageCatalogsMissingFiles) { |
| 74 ScopedTempDir temp; | 101 ScopedTempDir temp; |
| 75 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 102 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 76 | 103 |
| 77 FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder); | 104 FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder); |
| 78 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 105 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
| 79 | 106 |
| 107 std::set<std::string> valid_locales; |
| 108 valid_locales.insert("sr"); |
| 109 valid_locales.insert("en"); |
| 80 std::string error; | 110 std::string error; |
| 81 EXPECT_TRUE(NULL == extension_l10n_util::LoadMessageCatalogs(src_path, | 111 EXPECT_TRUE(NULL == extension_l10n_util::LoadMessageCatalogs(src_path, |
| 82 "en-US", | 112 "en", |
| 83 "sr", | 113 "sr", |
| 114 valid_locales, |
| 84 &error)); | 115 &error)); |
| 85 EXPECT_FALSE(error.empty()); | 116 EXPECT_FALSE(error.empty()); |
| 86 } | 117 } |
| 87 | 118 |
| 88 TEST(ExtensionL10nUtil, LoadMessageCatalogsBadJSONFormat) { | 119 TEST(ExtensionL10nUtil, LoadMessageCatalogsBadJSONFormat) { |
| 89 ScopedTempDir temp; | 120 ScopedTempDir temp; |
| 90 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 121 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 91 | 122 |
| 92 FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder); | 123 FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder); |
| 93 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 124 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
| 94 | 125 |
| 95 FilePath locale = src_path.AppendASCII("en_US"); | 126 FilePath locale = src_path.AppendASCII("sr"); |
| 96 ASSERT_TRUE(file_util::CreateDirectory(locale)); | 127 ASSERT_TRUE(file_util::CreateDirectory(locale)); |
| 97 | 128 |
| 98 std::string data = "{ \"name\":"; | 129 std::string data = "{ \"name\":"; |
| 99 ASSERT_TRUE( | 130 ASSERT_TRUE( |
| 100 file_util::WriteFile(locale.AppendASCII(Extension::kMessagesFilename), | 131 file_util::WriteFile(locale.AppendASCII(Extension::kMessagesFilename), |
| 101 data.c_str(), data.length())); | 132 data.c_str(), data.length())); |
| 102 | 133 |
| 134 std::set<std::string> valid_locales; |
| 135 valid_locales.insert("sr"); |
| 136 valid_locales.insert("en_US"); |
| 103 std::string error; | 137 std::string error; |
| 104 EXPECT_TRUE(NULL == extension_l10n_util::LoadMessageCatalogs(src_path, | 138 EXPECT_TRUE(NULL == extension_l10n_util::LoadMessageCatalogs(src_path, |
| 105 "en-US", | 139 "en_US", |
| 106 "sr", | 140 "sr", |
| 141 valid_locales, |
| 107 &error)); | 142 &error)); |
| 108 EXPECT_EQ("Line: 1, column: 10, Syntax error.", error); | 143 EXPECT_EQ("Line: 1, column: 10, Syntax error.", error); |
| 109 } | 144 } |
| 110 | 145 |
| 111 TEST(ExtensionL10nUtil, LoadMessageCatalogsDuplicateKeys) { | 146 TEST(ExtensionL10nUtil, LoadMessageCatalogsDuplicateKeys) { |
| 112 ScopedTempDir temp; | 147 ScopedTempDir temp; |
| 113 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 148 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 114 | 149 |
| 115 FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder); | 150 FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder); |
| 116 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 151 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
| 117 | 152 |
| 118 FilePath locale_1 = src_path.AppendASCII("en_US"); | 153 FilePath locale_1 = src_path.AppendASCII("en"); |
| 119 ASSERT_TRUE(file_util::CreateDirectory(locale_1)); | 154 ASSERT_TRUE(file_util::CreateDirectory(locale_1)); |
| 120 | 155 |
| 121 std::string data = | 156 std::string data = |
| 122 "{ \"name\": { \"message\": \"something\" }, " | 157 "{ \"name\": { \"message\": \"something\" }, " |
| 123 "\"name\": { \"message\": \"something else\" } }"; | 158 "\"name\": { \"message\": \"something else\" } }"; |
| 124 ASSERT_TRUE( | 159 ASSERT_TRUE( |
| 125 file_util::WriteFile(locale_1.AppendASCII(Extension::kMessagesFilename), | 160 file_util::WriteFile(locale_1.AppendASCII(Extension::kMessagesFilename), |
| 126 data.c_str(), data.length())); | 161 data.c_str(), data.length())); |
| 127 | 162 |
| 128 FilePath locale_2 = src_path.AppendASCII("sr"); | 163 FilePath locale_2 = src_path.AppendASCII("sr"); |
| 129 ASSERT_TRUE(file_util::CreateDirectory(locale_2)); | 164 ASSERT_TRUE(file_util::CreateDirectory(locale_2)); |
| 130 | 165 |
| 131 ASSERT_TRUE( | 166 ASSERT_TRUE( |
| 132 file_util::WriteFile(locale_2.AppendASCII(Extension::kMessagesFilename), | 167 file_util::WriteFile(locale_2.AppendASCII(Extension::kMessagesFilename), |
| 133 data.c_str(), data.length())); | 168 data.c_str(), data.length())); |
| 134 | 169 |
| 170 std::set<std::string> valid_locales; |
| 171 valid_locales.insert("sr"); |
| 172 valid_locales.insert("en"); |
| 135 std::string error; | 173 std::string error; |
| 136 // JSON parser hides duplicates. We are going to get only one key/value | 174 // JSON parser hides duplicates. We are going to get only one key/value |
| 137 // pair at the end. | 175 // pair at the end. |
| 138 scoped_ptr<ExtensionMessageBundle> message_bundle( | 176 scoped_ptr<ExtensionMessageBundle> message_bundle( |
| 139 extension_l10n_util::LoadMessageCatalogs(src_path, | 177 extension_l10n_util::LoadMessageCatalogs(src_path, |
| 140 "en-US", | 178 "en", |
| 141 "sr", | 179 "sr", |
| 180 valid_locales, |
| 142 &error)); | 181 &error)); |
| 143 EXPECT_TRUE(NULL != message_bundle.get()); | 182 EXPECT_TRUE(NULL != message_bundle.get()); |
| 144 EXPECT_TRUE(error.empty()); | 183 EXPECT_TRUE(error.empty()); |
| 145 } | 184 } |
| 146 | 185 |
| 147 bool PathsAreEqual(const FilePath& path1, const FilePath& path2) { | 186 bool PathsAreEqual(const FilePath& path1, const FilePath& path2) { |
| 148 FilePath::StringType path1_str = path1.value(); | 187 FilePath::StringType path1_str = path1.value(); |
| 149 std::replace(path1_str.begin(), path1_str.end(), '\\', '/'); | 188 std::replace(path1_str.begin(), path1_str.end(), '\\', '/'); |
| 150 | 189 |
| 151 FilePath::StringType path2_str = path2.value(); | 190 FilePath::StringType path2_str = path2.value(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 168 .AppendASCII(current_locale) | 207 .AppendASCII(current_locale) |
| 169 .AppendASCII("foo") | 208 .AppendASCII("foo") |
| 170 .AppendASCII("bar.js"); | 209 .AppendASCII("bar.js"); |
| 171 | 210 |
| 172 FilePath result = extension_l10n_util::GetL10nRelativePath( | 211 FilePath result = extension_l10n_util::GetL10nRelativePath( |
| 173 FilePath(FILE_PATH_LITERAL("foo/bar.js"))); | 212 FilePath(FILE_PATH_LITERAL("foo/bar.js"))); |
| 174 EXPECT_TRUE(PathsAreEqual(locale_path, result)); | 213 EXPECT_TRUE(PathsAreEqual(locale_path, result)); |
| 175 } | 214 } |
| 176 | 215 |
| 177 } // namespace | 216 } // namespace |
| OLD | NEW |