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_path.h" | 5 #include "base/file_path.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/files/scoped_temp_dir.h" |
7 #include "base/memory/linked_ptr.h" | 8 #include "base/memory/linked_ptr.h" |
8 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
9 #include "base/path_service.h" | 10 #include "base/path_service.h" |
10 #include "base/scoped_temp_dir.h" | |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
13 #include "chrome/common/extensions/extension.h" | 13 #include "chrome/common/extensions/extension.h" |
| 14 #include "chrome/common/extensions/extension_l10n_util.h" |
14 #include "chrome/common/extensions/extension_manifest_constants.h" | 15 #include "chrome/common/extensions/extension_manifest_constants.h" |
15 #include "chrome/common/extensions/extension_l10n_util.h" | |
16 #include "chrome/common/extensions/message_bundle.h" | 16 #include "chrome/common/extensions/message_bundle.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
19 | 19 |
20 using extensions::Extension; | 20 using extensions::Extension; |
21 using extensions::ExtensionInfo; | 21 using extensions::ExtensionInfo; |
22 using extensions::MessageBundle; | 22 using extensions::MessageBundle; |
23 | 23 |
24 namespace errors = extension_manifest_errors; | 24 namespace errors = extension_manifest_errors; |
25 namespace keys = extension_manifest_keys; | 25 namespace keys = extension_manifest_keys; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 DISABLED_ShouldRelocalizeManifestWithDefaultLocale | 63 DISABLED_ShouldRelocalizeManifestWithDefaultLocale |
64 #define ShouldRelocalizeManifestWithCurrentLocale \ | 64 #define ShouldRelocalizeManifestWithCurrentLocale \ |
65 DISABLED_ShouldRelocalizeManifestWithCurrentLocale | 65 DISABLED_ShouldRelocalizeManifestWithCurrentLocale |
66 #define ShouldRelocalizeManifestSameCurrentLocale \ | 66 #define ShouldRelocalizeManifestSameCurrentLocale \ |
67 DISABLED_ShouldRelocalizeManifestSameCurrentLocale | 67 DISABLED_ShouldRelocalizeManifestSameCurrentLocale |
68 #define ShouldRelocalizeManifestDifferentCurrentLocale \ | 68 #define ShouldRelocalizeManifestDifferentCurrentLocale \ |
69 DISABLED_ShouldRelocalizeManifestDifferentCurrentLocale | 69 DISABLED_ShouldRelocalizeManifestDifferentCurrentLocale |
70 #endif | 70 #endif |
71 | 71 |
72 TEST(ExtensionL10nUtil, GetValidLocalesEmptyLocaleFolder) { | 72 TEST(ExtensionL10nUtil, GetValidLocalesEmptyLocaleFolder) { |
73 ScopedTempDir temp; | 73 base::ScopedTempDir temp; |
74 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 74 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
75 | 75 |
76 FilePath src_path = temp.path().Append(Extension::kLocaleFolder); | 76 FilePath src_path = temp.path().Append(Extension::kLocaleFolder); |
77 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 77 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
78 | 78 |
79 std::string error; | 79 std::string error; |
80 std::set<std::string> locales; | 80 std::set<std::string> locales; |
81 EXPECT_FALSE(extension_l10n_util::GetValidLocales(src_path, | 81 EXPECT_FALSE(extension_l10n_util::GetValidLocales(src_path, |
82 &locales, | 82 &locales, |
83 &error)); | 83 &error)); |
84 | 84 |
85 EXPECT_TRUE(locales.empty()); | 85 EXPECT_TRUE(locales.empty()); |
86 } | 86 } |
87 | 87 |
88 TEST(ExtensionL10nUtil, GetValidLocalesWithValidLocaleNoMessagesFile) { | 88 TEST(ExtensionL10nUtil, GetValidLocalesWithValidLocaleNoMessagesFile) { |
89 ScopedTempDir temp; | 89 base::ScopedTempDir temp; |
90 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 90 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
91 | 91 |
92 FilePath src_path = temp.path().Append(Extension::kLocaleFolder); | 92 FilePath src_path = temp.path().Append(Extension::kLocaleFolder); |
93 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 93 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
94 ASSERT_TRUE(file_util::CreateDirectory(src_path.AppendASCII("sr"))); | 94 ASSERT_TRUE(file_util::CreateDirectory(src_path.AppendASCII("sr"))); |
95 | 95 |
96 std::string error; | 96 std::string error; |
97 std::set<std::string> locales; | 97 std::set<std::string> locales; |
98 EXPECT_FALSE(extension_l10n_util::GetValidLocales(src_path, | 98 EXPECT_FALSE(extension_l10n_util::GetValidLocales(src_path, |
99 &locales, | 99 &locales, |
100 &error)); | 100 &error)); |
101 | 101 |
102 EXPECT_TRUE(locales.empty()); | 102 EXPECT_TRUE(locales.empty()); |
103 } | 103 } |
104 | 104 |
105 TEST(ExtensionL10nUtil, GetValidLocalesWithUnsupportedLocale) { | 105 TEST(ExtensionL10nUtil, GetValidLocalesWithUnsupportedLocale) { |
106 ScopedTempDir temp; | 106 base::ScopedTempDir temp; |
107 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 107 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
108 | 108 |
109 FilePath src_path = temp.path().Append(Extension::kLocaleFolder); | 109 FilePath src_path = temp.path().Append(Extension::kLocaleFolder); |
110 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 110 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
111 // Supported locale. | 111 // Supported locale. |
112 FilePath locale_1 = src_path.AppendASCII("sr"); | 112 FilePath locale_1 = src_path.AppendASCII("sr"); |
113 ASSERT_TRUE(file_util::CreateDirectory(locale_1)); | 113 ASSERT_TRUE(file_util::CreateDirectory(locale_1)); |
114 std::string data("whatever"); | 114 std::string data("whatever"); |
115 ASSERT_TRUE(file_util::WriteFile( | 115 ASSERT_TRUE(file_util::WriteFile( |
116 locale_1.Append(Extension::kMessagesFilename), | 116 locale_1.Append(Extension::kMessagesFilename), |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 | 168 |
169 scoped_ptr<MessageBundle> bundle(extension_l10n_util::LoadMessageCatalogs( | 169 scoped_ptr<MessageBundle> bundle(extension_l10n_util::LoadMessageCatalogs( |
170 install_dir, "sr", "en_US", locales, &error)); | 170 install_dir, "sr", "en_US", locales, &error)); |
171 ASSERT_FALSE(NULL == bundle.get()); | 171 ASSERT_FALSE(NULL == bundle.get()); |
172 EXPECT_TRUE(error.empty()); | 172 EXPECT_TRUE(error.empty()); |
173 EXPECT_EQ("Color", bundle->GetL10nMessage("color")); | 173 EXPECT_EQ("Color", bundle->GetL10nMessage("color")); |
174 EXPECT_EQ("Not in the US or GB.", bundle->GetL10nMessage("not_in_US_or_GB")); | 174 EXPECT_EQ("Not in the US or GB.", bundle->GetL10nMessage("not_in_US_or_GB")); |
175 } | 175 } |
176 | 176 |
177 TEST(ExtensionL10nUtil, LoadMessageCatalogsMissingFiles) { | 177 TEST(ExtensionL10nUtil, LoadMessageCatalogsMissingFiles) { |
178 ScopedTempDir temp; | 178 base::ScopedTempDir temp; |
179 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 179 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
180 | 180 |
181 FilePath src_path = temp.path().Append(Extension::kLocaleFolder); | 181 FilePath src_path = temp.path().Append(Extension::kLocaleFolder); |
182 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 182 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
183 | 183 |
184 std::set<std::string> valid_locales; | 184 std::set<std::string> valid_locales; |
185 valid_locales.insert("sr"); | 185 valid_locales.insert("sr"); |
186 valid_locales.insert("en"); | 186 valid_locales.insert("en"); |
187 std::string error; | 187 std::string error; |
188 EXPECT_TRUE(NULL == extension_l10n_util::LoadMessageCatalogs(src_path, | 188 EXPECT_TRUE(NULL == extension_l10n_util::LoadMessageCatalogs(src_path, |
189 "en", | 189 "en", |
190 "sr", | 190 "sr", |
191 valid_locales, | 191 valid_locales, |
192 &error)); | 192 &error)); |
193 EXPECT_FALSE(error.empty()); | 193 EXPECT_FALSE(error.empty()); |
194 } | 194 } |
195 | 195 |
196 TEST(ExtensionL10nUtil, LoadMessageCatalogsBadJSONFormat) { | 196 TEST(ExtensionL10nUtil, LoadMessageCatalogsBadJSONFormat) { |
197 ScopedTempDir temp; | 197 base::ScopedTempDir temp; |
198 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 198 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
199 | 199 |
200 FilePath src_path = temp.path().Append(Extension::kLocaleFolder); | 200 FilePath src_path = temp.path().Append(Extension::kLocaleFolder); |
201 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 201 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
202 | 202 |
203 FilePath locale = src_path.AppendASCII("sr"); | 203 FilePath locale = src_path.AppendASCII("sr"); |
204 ASSERT_TRUE(file_util::CreateDirectory(locale)); | 204 ASSERT_TRUE(file_util::CreateDirectory(locale)); |
205 | 205 |
206 std::string data = "{ \"name\":"; | 206 std::string data = "{ \"name\":"; |
207 ASSERT_TRUE( | 207 ASSERT_TRUE( |
208 file_util::WriteFile(locale.Append(Extension::kMessagesFilename), | 208 file_util::WriteFile(locale.Append(Extension::kMessagesFilename), |
209 data.c_str(), data.length())); | 209 data.c_str(), data.length())); |
210 | 210 |
211 std::set<std::string> valid_locales; | 211 std::set<std::string> valid_locales; |
212 valid_locales.insert("sr"); | 212 valid_locales.insert("sr"); |
213 valid_locales.insert("en_US"); | 213 valid_locales.insert("en_US"); |
214 std::string error; | 214 std::string error; |
215 EXPECT_TRUE(NULL == extension_l10n_util::LoadMessageCatalogs(src_path, | 215 EXPECT_TRUE(NULL == extension_l10n_util::LoadMessageCatalogs(src_path, |
216 "en_US", | 216 "en_US", |
217 "sr", | 217 "sr", |
218 valid_locales, | 218 valid_locales, |
219 &error)); | 219 &error)); |
220 EXPECT_EQ("Line: 1, column: 10, Unexpected token.", error); | 220 EXPECT_EQ("Line: 1, column: 10, Unexpected token.", error); |
221 } | 221 } |
222 | 222 |
223 TEST(ExtensionL10nUtil, LoadMessageCatalogsDuplicateKeys) { | 223 TEST(ExtensionL10nUtil, LoadMessageCatalogsDuplicateKeys) { |
224 ScopedTempDir temp; | 224 base::ScopedTempDir temp; |
225 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 225 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
226 | 226 |
227 FilePath src_path = temp.path().Append(Extension::kLocaleFolder); | 227 FilePath src_path = temp.path().Append(Extension::kLocaleFolder); |
228 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 228 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
229 | 229 |
230 FilePath locale_1 = src_path.AppendASCII("en"); | 230 FilePath locale_1 = src_path.AppendASCII("en"); |
231 ASSERT_TRUE(file_util::CreateDirectory(locale_1)); | 231 ASSERT_TRUE(file_util::CreateDirectory(locale_1)); |
232 | 232 |
233 std::string data = | 233 std::string data = |
234 "{ \"name\": { \"message\": \"something\" }, " | 234 "{ \"name\": { \"message\": \"something\" }, " |
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
621 std::vector<std::string> fallback_locales; | 621 std::vector<std::string> fallback_locales; |
622 extension_l10n_util::GetAllFallbackLocales("en_US", "all", &fallback_locales); | 622 extension_l10n_util::GetAllFallbackLocales("en_US", "all", &fallback_locales); |
623 ASSERT_EQ(3U, fallback_locales.size()); | 623 ASSERT_EQ(3U, fallback_locales.size()); |
624 | 624 |
625 CHECK_EQ("en_US", fallback_locales[0]); | 625 CHECK_EQ("en_US", fallback_locales[0]); |
626 CHECK_EQ("en", fallback_locales[1]); | 626 CHECK_EQ("en", fallback_locales[1]); |
627 CHECK_EQ("all", fallback_locales[2]); | 627 CHECK_EQ("all", fallback_locales[2]); |
628 } | 628 } |
629 | 629 |
630 } // namespace | 630 } // namespace |
OLD | NEW |