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 "chrome/common/extensions/extension_message_bundle.h" | 5 #include "chrome/common/extensions/message_bundle.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
11 #include "base/memory/linked_ptr.h" | 11 #include "base/memory/linked_ptr.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "chrome/common/extensions/extension_manifest_constants.h" | 16 #include "chrome/common/extensions/extension_manifest_constants.h" |
17 #include "chrome/common/extensions/extension_error_utils.h" | 17 #include "chrome/common/extensions/extension_error_utils.h" |
18 #include "chrome/common/extensions/extension_l10n_util.h" | 18 #include "chrome/common/extensions/extension_l10n_util.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
20 | 20 |
21 namespace errors = extension_manifest_errors; | 21 namespace errors = extension_manifest_errors; |
22 | 22 |
23 class ExtensionMessageBundleTest : public testing::Test { | 23 namespace extensions { |
| 24 |
| 25 class MessageBundleTest : public testing::Test { |
24 protected: | 26 protected: |
25 enum BadDictionary { | 27 enum BadDictionary { |
26 INVALID_NAME, | 28 INVALID_NAME, |
27 NAME_NOT_A_TREE, | 29 NAME_NOT_A_TREE, |
28 EMPTY_NAME_TREE, | 30 EMPTY_NAME_TREE, |
29 MISSING_MESSAGE, | 31 MISSING_MESSAGE, |
30 PLACEHOLDER_NOT_A_TREE, | 32 PLACEHOLDER_NOT_A_TREE, |
31 EMPTY_PLACEHOLDER_TREE, | 33 EMPTY_PLACEHOLDER_TREE, |
32 CONTENT_MISSING, | 34 CONTENT_MISSING, |
33 MESSAGE_PLACEHOLDER_DOESNT_MATCH, | 35 MESSAGE_PLACEHOLDER_DOESNT_MATCH, |
34 }; | 36 }; |
35 | 37 |
36 // Helper method for dictionary building. | 38 // Helper method for dictionary building. |
37 void SetDictionary(const std::string& name, | 39 void SetDictionary(const std::string& name, |
38 DictionaryValue* subtree, | 40 DictionaryValue* subtree, |
39 DictionaryValue* target) { | 41 DictionaryValue* target) { |
40 target->Set(name, static_cast<Value*>(subtree)); | 42 target->Set(name, static_cast<Value*>(subtree)); |
41 } | 43 } |
42 | 44 |
43 void CreateContentTree(const std::string& name, | 45 void CreateContentTree(const std::string& name, |
44 const std::string& content, | 46 const std::string& content, |
45 DictionaryValue* dict) { | 47 DictionaryValue* dict) { |
46 DictionaryValue* content_tree = new DictionaryValue; | 48 DictionaryValue* content_tree = new DictionaryValue; |
47 content_tree->SetString(ExtensionMessageBundle::kContentKey, content); | 49 content_tree->SetString(MessageBundle::kContentKey, content); |
48 SetDictionary(name, content_tree, dict); | 50 SetDictionary(name, content_tree, dict); |
49 } | 51 } |
50 | 52 |
51 void CreatePlaceholdersTree(DictionaryValue* dict) { | 53 void CreatePlaceholdersTree(DictionaryValue* dict) { |
52 DictionaryValue* placeholders_tree = new DictionaryValue; | 54 DictionaryValue* placeholders_tree = new DictionaryValue; |
53 CreateContentTree("a", "A", placeholders_tree); | 55 CreateContentTree("a", "A", placeholders_tree); |
54 CreateContentTree("b", "B", placeholders_tree); | 56 CreateContentTree("b", "B", placeholders_tree); |
55 CreateContentTree("c", "C", placeholders_tree); | 57 CreateContentTree("c", "C", placeholders_tree); |
56 SetDictionary(ExtensionMessageBundle::kPlaceholdersKey, | 58 SetDictionary(MessageBundle::kPlaceholdersKey, |
57 placeholders_tree, | 59 placeholders_tree, |
58 dict); | 60 dict); |
59 } | 61 } |
60 | 62 |
61 void CreateMessageTree(const std::string& name, | 63 void CreateMessageTree(const std::string& name, |
62 const std::string& message, | 64 const std::string& message, |
63 bool create_placeholder_subtree, | 65 bool create_placeholder_subtree, |
64 DictionaryValue* dict) { | 66 DictionaryValue* dict) { |
65 DictionaryValue* message_tree = new DictionaryValue; | 67 DictionaryValue* message_tree = new DictionaryValue; |
66 if (create_placeholder_subtree) | 68 if (create_placeholder_subtree) |
67 CreatePlaceholdersTree(message_tree); | 69 CreatePlaceholdersTree(message_tree); |
68 message_tree->SetString(ExtensionMessageBundle::kMessageKey, message); | 70 message_tree->SetString(MessageBundle::kMessageKey, message); |
69 SetDictionary(name, message_tree, dict); | 71 SetDictionary(name, message_tree, dict); |
70 } | 72 } |
71 | 73 |
72 // Caller owns the memory. | 74 // Caller owns the memory. |
73 DictionaryValue* CreateGoodDictionary() { | 75 DictionaryValue* CreateGoodDictionary() { |
74 DictionaryValue* dict = new DictionaryValue; | 76 DictionaryValue* dict = new DictionaryValue; |
75 CreateMessageTree("n1", "message1 $a$ $b$", true, dict); | 77 CreateMessageTree("n1", "message1 $a$ $b$", true, dict); |
76 CreateMessageTree("n2", "message2 $c$", true, dict); | 78 CreateMessageTree("n2", "message2 $c$", true, dict); |
77 CreateMessageTree("n3", "message3", false, dict); | 79 CreateMessageTree("n3", "message3", false, dict); |
78 return dict; | 80 return dict; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 } | 119 } |
118 | 120 |
119 return dict; | 121 return dict; |
120 } | 122 } |
121 | 123 |
122 unsigned int ReservedMessagesCount() { | 124 unsigned int ReservedMessagesCount() { |
123 // Update when adding new reserved messages. | 125 // Update when adding new reserved messages. |
124 return 5U; | 126 return 5U; |
125 } | 127 } |
126 | 128 |
127 void CheckReservedMessages(ExtensionMessageBundle* handler) { | 129 void CheckReservedMessages(MessageBundle* handler) { |
128 std::string ui_locale = extension_l10n_util::CurrentLocaleOrDefault(); | 130 std::string ui_locale = extension_l10n_util::CurrentLocaleOrDefault(); |
129 EXPECT_EQ(ui_locale, | 131 EXPECT_EQ(ui_locale, |
130 handler->GetL10nMessage(ExtensionMessageBundle::kUILocaleKey)); | 132 handler->GetL10nMessage(MessageBundle::kUILocaleKey)); |
131 | 133 |
132 std::string text_dir = "ltr"; | 134 std::string text_dir = "ltr"; |
133 if (base::i18n::GetTextDirectionForLocale(ui_locale.c_str()) == | 135 if (base::i18n::GetTextDirectionForLocale(ui_locale.c_str()) == |
134 base::i18n::RIGHT_TO_LEFT) | 136 base::i18n::RIGHT_TO_LEFT) |
135 text_dir = "rtl"; | 137 text_dir = "rtl"; |
136 | 138 |
137 EXPECT_EQ(text_dir, handler->GetL10nMessage( | 139 EXPECT_EQ(text_dir, handler->GetL10nMessage( |
138 ExtensionMessageBundle::kBidiDirectionKey)); | 140 MessageBundle::kBidiDirectionKey)); |
139 } | 141 } |
140 | 142 |
141 bool AppendReservedMessages(const std::string& application_locale) { | 143 bool AppendReservedMessages(const std::string& application_locale) { |
142 std::string error; | 144 std::string error; |
143 return handler_->AppendReservedMessagesForLocale( | 145 return handler_->AppendReservedMessagesForLocale( |
144 application_locale, &error); | 146 application_locale, &error); |
145 } | 147 } |
146 | 148 |
147 std::string CreateMessageBundle() { | 149 std::string CreateMessageBundle() { |
148 std::string error; | 150 std::string error; |
149 handler_.reset(ExtensionMessageBundle::Create(catalogs_, &error)); | 151 handler_.reset(MessageBundle::Create(catalogs_, &error)); |
150 | 152 |
151 return error; | 153 return error; |
152 } | 154 } |
153 | 155 |
154 void ClearDictionary() { | 156 void ClearDictionary() { |
155 handler_->dictionary_.clear(); | 157 handler_->dictionary_.clear(); |
156 } | 158 } |
157 | 159 |
158 scoped_ptr<ExtensionMessageBundle> handler_; | 160 scoped_ptr<MessageBundle> handler_; |
159 std::vector<linked_ptr<DictionaryValue> > catalogs_; | 161 std::vector<linked_ptr<DictionaryValue> > catalogs_; |
160 }; | 162 }; |
161 | 163 |
162 TEST_F(ExtensionMessageBundleTest, ReservedMessagesCount) { | 164 TEST_F(MessageBundleTest, ReservedMessagesCount) { |
163 ASSERT_EQ(5U, ReservedMessagesCount()); | 165 ASSERT_EQ(5U, ReservedMessagesCount()); |
164 } | 166 } |
165 | 167 |
166 TEST_F(ExtensionMessageBundleTest, InitEmptyDictionaries) { | 168 TEST_F(MessageBundleTest, InitEmptyDictionaries) { |
167 CreateMessageBundle(); | 169 CreateMessageBundle(); |
168 EXPECT_TRUE(handler_.get() != NULL); | 170 EXPECT_TRUE(handler_.get() != NULL); |
169 EXPECT_EQ(0U + ReservedMessagesCount(), handler_->size()); | 171 EXPECT_EQ(0U + ReservedMessagesCount(), handler_->size()); |
170 CheckReservedMessages(handler_.get()); | 172 CheckReservedMessages(handler_.get()); |
171 } | 173 } |
172 | 174 |
173 TEST_F(ExtensionMessageBundleTest, InitGoodDefaultDict) { | 175 TEST_F(MessageBundleTest, InitGoodDefaultDict) { |
174 catalogs_.push_back(linked_ptr<DictionaryValue>(CreateGoodDictionary())); | 176 catalogs_.push_back(linked_ptr<DictionaryValue>(CreateGoodDictionary())); |
175 CreateMessageBundle(); | 177 CreateMessageBundle(); |
176 | 178 |
177 EXPECT_TRUE(handler_.get() != NULL); | 179 EXPECT_TRUE(handler_.get() != NULL); |
178 EXPECT_EQ(3U + ReservedMessagesCount(), handler_->size()); | 180 EXPECT_EQ(3U + ReservedMessagesCount(), handler_->size()); |
179 | 181 |
180 EXPECT_EQ("message1 A B", handler_->GetL10nMessage("n1")); | 182 EXPECT_EQ("message1 A B", handler_->GetL10nMessage("n1")); |
181 EXPECT_EQ("message2 C", handler_->GetL10nMessage("n2")); | 183 EXPECT_EQ("message2 C", handler_->GetL10nMessage("n2")); |
182 EXPECT_EQ("message3", handler_->GetL10nMessage("n3")); | 184 EXPECT_EQ("message3", handler_->GetL10nMessage("n3")); |
183 CheckReservedMessages(handler_.get()); | 185 CheckReservedMessages(handler_.get()); |
184 } | 186 } |
185 | 187 |
186 TEST_F(ExtensionMessageBundleTest, InitAppDictConsultedFirst) { | 188 TEST_F(MessageBundleTest, InitAppDictConsultedFirst) { |
187 catalogs_.push_back(linked_ptr<DictionaryValue>(CreateGoodDictionary())); | 189 catalogs_.push_back(linked_ptr<DictionaryValue>(CreateGoodDictionary())); |
188 catalogs_.push_back(linked_ptr<DictionaryValue>(CreateGoodDictionary())); | 190 catalogs_.push_back(linked_ptr<DictionaryValue>(CreateGoodDictionary())); |
189 | 191 |
190 DictionaryValue* app_dict = catalogs_[0].get(); | 192 DictionaryValue* app_dict = catalogs_[0].get(); |
191 // Flip placeholders in message of n1 tree. | 193 // Flip placeholders in message of n1 tree. |
192 app_dict->SetString("n1.message", "message1 $b$ $a$"); | 194 app_dict->SetString("n1.message", "message1 $b$ $a$"); |
193 // Remove one message from app dict. | 195 // Remove one message from app dict. |
194 app_dict->Remove("n2", NULL); | 196 app_dict->Remove("n2", NULL); |
195 // Replace n3 with N3. | 197 // Replace n3 with N3. |
196 app_dict->Remove("n3", NULL); | 198 app_dict->Remove("n3", NULL); |
197 CreateMessageTree("N3", "message3_app_dict", false, app_dict); | 199 CreateMessageTree("N3", "message3_app_dict", false, app_dict); |
198 | 200 |
199 CreateMessageBundle(); | 201 CreateMessageBundle(); |
200 | 202 |
201 EXPECT_TRUE(handler_.get() != NULL); | 203 EXPECT_TRUE(handler_.get() != NULL); |
202 EXPECT_EQ(3U + ReservedMessagesCount(), handler_->size()); | 204 EXPECT_EQ(3U + ReservedMessagesCount(), handler_->size()); |
203 | 205 |
204 EXPECT_EQ("message1 B A", handler_->GetL10nMessage("n1")); | 206 EXPECT_EQ("message1 B A", handler_->GetL10nMessage("n1")); |
205 EXPECT_EQ("message2 C", handler_->GetL10nMessage("n2")); | 207 EXPECT_EQ("message2 C", handler_->GetL10nMessage("n2")); |
206 EXPECT_EQ("message3_app_dict", handler_->GetL10nMessage("n3")); | 208 EXPECT_EQ("message3_app_dict", handler_->GetL10nMessage("n3")); |
207 CheckReservedMessages(handler_.get()); | 209 CheckReservedMessages(handler_.get()); |
208 } | 210 } |
209 | 211 |
210 TEST_F(ExtensionMessageBundleTest, InitBadAppDict) { | 212 TEST_F(MessageBundleTest, InitBadAppDict) { |
211 catalogs_.push_back( | 213 catalogs_.push_back( |
212 linked_ptr<DictionaryValue>(CreateBadDictionary(INVALID_NAME))); | 214 linked_ptr<DictionaryValue>(CreateBadDictionary(INVALID_NAME))); |
213 catalogs_.push_back(linked_ptr<DictionaryValue>(CreateGoodDictionary())); | 215 catalogs_.push_back(linked_ptr<DictionaryValue>(CreateGoodDictionary())); |
214 | 216 |
215 std::string error = CreateMessageBundle(); | 217 std::string error = CreateMessageBundle(); |
216 | 218 |
217 EXPECT_TRUE(handler_.get() == NULL); | 219 EXPECT_TRUE(handler_.get() == NULL); |
218 EXPECT_EQ("Name of a key \"n 5\" is invalid. Only ASCII [a-z], " | 220 EXPECT_EQ("Name of a key \"n 5\" is invalid. Only ASCII [a-z], " |
219 "[A-Z], [0-9] and \"_\" are allowed.", error); | 221 "[A-Z], [0-9] and \"_\" are allowed.", error); |
220 | 222 |
221 catalogs_[0].reset(CreateBadDictionary(NAME_NOT_A_TREE)); | 223 catalogs_[0].reset(CreateBadDictionary(NAME_NOT_A_TREE)); |
222 handler_.reset(ExtensionMessageBundle::Create(catalogs_, &error)); | 224 handler_.reset(MessageBundle::Create(catalogs_, &error)); |
223 EXPECT_TRUE(handler_.get() == NULL); | 225 EXPECT_TRUE(handler_.get() == NULL); |
224 EXPECT_EQ("Not a valid tree for key n4.", error); | 226 EXPECT_EQ("Not a valid tree for key n4.", error); |
225 | 227 |
226 catalogs_[0].reset(CreateBadDictionary(EMPTY_NAME_TREE)); | 228 catalogs_[0].reset(CreateBadDictionary(EMPTY_NAME_TREE)); |
227 handler_.reset(ExtensionMessageBundle::Create(catalogs_, &error)); | 229 handler_.reset(MessageBundle::Create(catalogs_, &error)); |
228 EXPECT_TRUE(handler_.get() == NULL); | 230 EXPECT_TRUE(handler_.get() == NULL); |
229 EXPECT_EQ("There is no \"message\" element for key n4.", error); | 231 EXPECT_EQ("There is no \"message\" element for key n4.", error); |
230 | 232 |
231 catalogs_[0].reset(CreateBadDictionary(MISSING_MESSAGE)); | 233 catalogs_[0].reset(CreateBadDictionary(MISSING_MESSAGE)); |
232 handler_.reset(ExtensionMessageBundle::Create(catalogs_, &error)); | 234 handler_.reset(MessageBundle::Create(catalogs_, &error)); |
233 EXPECT_TRUE(handler_.get() == NULL); | 235 EXPECT_TRUE(handler_.get() == NULL); |
234 EXPECT_EQ("There is no \"message\" element for key n1.", error); | 236 EXPECT_EQ("There is no \"message\" element for key n1.", error); |
235 | 237 |
236 catalogs_[0].reset(CreateBadDictionary(PLACEHOLDER_NOT_A_TREE)); | 238 catalogs_[0].reset(CreateBadDictionary(PLACEHOLDER_NOT_A_TREE)); |
237 handler_.reset(ExtensionMessageBundle::Create(catalogs_, &error)); | 239 handler_.reset(MessageBundle::Create(catalogs_, &error)); |
238 EXPECT_TRUE(handler_.get() == NULL); | 240 EXPECT_TRUE(handler_.get() == NULL); |
239 EXPECT_EQ("Not a valid \"placeholders\" element for key n1.", error); | 241 EXPECT_EQ("Not a valid \"placeholders\" element for key n1.", error); |
240 | 242 |
241 catalogs_[0].reset(CreateBadDictionary(EMPTY_PLACEHOLDER_TREE)); | 243 catalogs_[0].reset(CreateBadDictionary(EMPTY_PLACEHOLDER_TREE)); |
242 handler_.reset(ExtensionMessageBundle::Create(catalogs_, &error)); | 244 handler_.reset(MessageBundle::Create(catalogs_, &error)); |
243 EXPECT_TRUE(handler_.get() == NULL); | 245 EXPECT_TRUE(handler_.get() == NULL); |
244 EXPECT_EQ("Variable $a$ used but not defined.", error); | 246 EXPECT_EQ("Variable $a$ used but not defined.", error); |
245 | 247 |
246 catalogs_[0].reset(CreateBadDictionary(CONTENT_MISSING)); | 248 catalogs_[0].reset(CreateBadDictionary(CONTENT_MISSING)); |
247 handler_.reset(ExtensionMessageBundle::Create(catalogs_, &error)); | 249 handler_.reset(MessageBundle::Create(catalogs_, &error)); |
248 EXPECT_TRUE(handler_.get() == NULL); | 250 EXPECT_TRUE(handler_.get() == NULL); |
249 EXPECT_EQ("Invalid \"content\" element for key n1.", error); | 251 EXPECT_EQ("Invalid \"content\" element for key n1.", error); |
250 | 252 |
251 catalogs_[0].reset(CreateBadDictionary(MESSAGE_PLACEHOLDER_DOESNT_MATCH)); | 253 catalogs_[0].reset(CreateBadDictionary(MESSAGE_PLACEHOLDER_DOESNT_MATCH)); |
252 handler_.reset(ExtensionMessageBundle::Create(catalogs_, &error)); | 254 handler_.reset(MessageBundle::Create(catalogs_, &error)); |
253 EXPECT_TRUE(handler_.get() == NULL); | 255 EXPECT_TRUE(handler_.get() == NULL); |
254 EXPECT_EQ("Variable $a$ used but not defined.", error); | 256 EXPECT_EQ("Variable $a$ used but not defined.", error); |
255 } | 257 } |
256 | 258 |
257 TEST_F(ExtensionMessageBundleTest, ReservedMessagesOverrideDeveloperMessages) { | 259 TEST_F(MessageBundleTest, ReservedMessagesOverrideDeveloperMessages) { |
258 catalogs_.push_back(linked_ptr<DictionaryValue>(CreateGoodDictionary())); | 260 catalogs_.push_back(linked_ptr<DictionaryValue>(CreateGoodDictionary())); |
259 | 261 |
260 DictionaryValue* dict = catalogs_[0].get(); | 262 DictionaryValue* dict = catalogs_[0].get(); |
261 CreateMessageTree(ExtensionMessageBundle::kUILocaleKey, "x", false, dict); | 263 CreateMessageTree(MessageBundle::kUILocaleKey, "x", false, dict); |
262 | 264 |
263 std::string error = CreateMessageBundle(); | 265 std::string error = CreateMessageBundle(); |
264 | 266 |
265 EXPECT_TRUE(handler_.get() == NULL); | 267 EXPECT_TRUE(handler_.get() == NULL); |
266 std::string expected_error = ExtensionErrorUtils::FormatErrorMessage( | 268 std::string expected_error = ExtensionErrorUtils::FormatErrorMessage( |
267 errors::kReservedMessageFound, ExtensionMessageBundle::kUILocaleKey); | 269 errors::kReservedMessageFound, MessageBundle::kUILocaleKey); |
268 EXPECT_EQ(expected_error, error); | 270 EXPECT_EQ(expected_error, error); |
269 } | 271 } |
270 | 272 |
271 TEST_F(ExtensionMessageBundleTest, AppendReservedMessagesForLTR) { | 273 TEST_F(MessageBundleTest, AppendReservedMessagesForLTR) { |
272 CreateMessageBundle(); | 274 CreateMessageBundle(); |
273 | 275 |
274 ASSERT_TRUE(handler_.get() != NULL); | 276 ASSERT_TRUE(handler_.get() != NULL); |
275 ClearDictionary(); | 277 ClearDictionary(); |
276 ASSERT_TRUE(AppendReservedMessages("en_US")); | 278 ASSERT_TRUE(AppendReservedMessages("en_US")); |
277 | 279 |
278 EXPECT_EQ("en_US", | 280 EXPECT_EQ("en_US", |
279 handler_->GetL10nMessage(ExtensionMessageBundle::kUILocaleKey)); | 281 handler_->GetL10nMessage(MessageBundle::kUILocaleKey)); |
280 EXPECT_EQ("ltr", handler_->GetL10nMessage( | 282 EXPECT_EQ("ltr", handler_->GetL10nMessage( |
281 ExtensionMessageBundle::kBidiDirectionKey)); | 283 MessageBundle::kBidiDirectionKey)); |
282 EXPECT_EQ("rtl", handler_->GetL10nMessage( | 284 EXPECT_EQ("rtl", handler_->GetL10nMessage( |
283 ExtensionMessageBundle::kBidiReversedDirectionKey)); | 285 MessageBundle::kBidiReversedDirectionKey)); |
284 EXPECT_EQ("left", handler_->GetL10nMessage( | 286 EXPECT_EQ("left", handler_->GetL10nMessage( |
285 ExtensionMessageBundle::kBidiStartEdgeKey)); | 287 MessageBundle::kBidiStartEdgeKey)); |
286 EXPECT_EQ("right", handler_->GetL10nMessage( | 288 EXPECT_EQ("right", handler_->GetL10nMessage( |
287 ExtensionMessageBundle::kBidiEndEdgeKey)); | 289 MessageBundle::kBidiEndEdgeKey)); |
288 } | 290 } |
289 | 291 |
290 TEST_F(ExtensionMessageBundleTest, AppendReservedMessagesForRTL) { | 292 TEST_F(MessageBundleTest, AppendReservedMessagesForRTL) { |
291 CreateMessageBundle(); | 293 CreateMessageBundle(); |
292 | 294 |
293 ASSERT_TRUE(handler_.get() != NULL); | 295 ASSERT_TRUE(handler_.get() != NULL); |
294 ClearDictionary(); | 296 ClearDictionary(); |
295 ASSERT_TRUE(AppendReservedMessages("he")); | 297 ASSERT_TRUE(AppendReservedMessages("he")); |
296 | 298 |
297 EXPECT_EQ("he", | 299 EXPECT_EQ("he", |
298 handler_->GetL10nMessage(ExtensionMessageBundle::kUILocaleKey)); | 300 handler_->GetL10nMessage(MessageBundle::kUILocaleKey)); |
299 EXPECT_EQ("rtl", handler_->GetL10nMessage( | 301 EXPECT_EQ("rtl", handler_->GetL10nMessage( |
300 ExtensionMessageBundle::kBidiDirectionKey)); | 302 MessageBundle::kBidiDirectionKey)); |
301 EXPECT_EQ("ltr", handler_->GetL10nMessage( | 303 EXPECT_EQ("ltr", handler_->GetL10nMessage( |
302 ExtensionMessageBundle::kBidiReversedDirectionKey)); | 304 MessageBundle::kBidiReversedDirectionKey)); |
303 EXPECT_EQ("right", handler_->GetL10nMessage( | 305 EXPECT_EQ("right", handler_->GetL10nMessage( |
304 ExtensionMessageBundle::kBidiStartEdgeKey)); | 306 MessageBundle::kBidiStartEdgeKey)); |
305 EXPECT_EQ("left", handler_->GetL10nMessage( | 307 EXPECT_EQ("left", handler_->GetL10nMessage( |
306 ExtensionMessageBundle::kBidiEndEdgeKey)); | 308 MessageBundle::kBidiEndEdgeKey)); |
307 } | 309 } |
308 | 310 |
309 TEST_F(ExtensionMessageBundleTest, IsValidNameCheckValidCharacters) { | 311 TEST_F(MessageBundleTest, IsValidNameCheckValidCharacters) { |
310 EXPECT_TRUE(ExtensionMessageBundle::IsValidName(std::string("a__BV_9"))); | 312 EXPECT_TRUE(MessageBundle::IsValidName(std::string("a__BV_9"))); |
311 EXPECT_TRUE(ExtensionMessageBundle::IsValidName(std::string("@@a__BV_9"))); | 313 EXPECT_TRUE(MessageBundle::IsValidName(std::string("@@a__BV_9"))); |
312 EXPECT_FALSE(ExtensionMessageBundle::IsValidName(std::string("$a__BV_9$"))); | 314 EXPECT_FALSE(MessageBundle::IsValidName(std::string("$a__BV_9$"))); |
313 EXPECT_FALSE(ExtensionMessageBundle::IsValidName(std::string("a-BV-9"))); | 315 EXPECT_FALSE(MessageBundle::IsValidName(std::string("a-BV-9"))); |
314 EXPECT_FALSE(ExtensionMessageBundle::IsValidName(std::string("a#BV!9"))); | 316 EXPECT_FALSE(MessageBundle::IsValidName(std::string("a#BV!9"))); |
315 EXPECT_FALSE(ExtensionMessageBundle::IsValidName(std::string("a<b"))); | 317 EXPECT_FALSE(MessageBundle::IsValidName(std::string("a<b"))); |
316 } | 318 } |
317 | 319 |
318 struct ReplaceVariables { | 320 struct ReplaceVariables { |
319 const char* original; | 321 const char* original; |
320 const char* result; | 322 const char* result; |
321 const char* error; | 323 const char* error; |
322 const char* begin_delimiter; | 324 const char* begin_delimiter; |
323 const char* end_delimiter; | 325 const char* end_delimiter; |
324 bool pass; | 326 bool pass; |
325 }; | 327 }; |
326 | 328 |
327 TEST(ExtensionMessageBundle, ReplaceMessagesInText) { | 329 TEST(MessageBundle, ReplaceMessagesInText) { |
328 const char* kMessageBegin = ExtensionMessageBundle::kMessageBegin; | 330 const char* kMessageBegin = MessageBundle::kMessageBegin; |
329 const char* kMessageEnd = ExtensionMessageBundle::kMessageEnd; | 331 const char* kMessageEnd = MessageBundle::kMessageEnd; |
330 const char* kPlaceholderBegin = ExtensionMessageBundle::kPlaceholderBegin; | 332 const char* kPlaceholderBegin = MessageBundle::kPlaceholderBegin; |
331 const char* kPlaceholderEnd = ExtensionMessageBundle::kPlaceholderEnd; | 333 const char* kPlaceholderEnd = MessageBundle::kPlaceholderEnd; |
332 | 334 |
333 static ReplaceVariables test_cases[] = { | 335 static ReplaceVariables test_cases[] = { |
334 // Message replacement. | 336 // Message replacement. |
335 { "This is __MSG_siMPle__ message", "This is simple message", | 337 { "This is __MSG_siMPle__ message", "This is simple message", |
336 "", kMessageBegin, kMessageEnd, true }, | 338 "", kMessageBegin, kMessageEnd, true }, |
337 { "This is __MSG_", "This is __MSG_", | 339 { "This is __MSG_", "This is __MSG_", |
338 "", kMessageBegin, kMessageEnd, true }, | 340 "", kMessageBegin, kMessageEnd, true }, |
339 { "This is __MSG__simple__ message", "This is __MSG__simple__ message", | 341 { "This is __MSG__simple__ message", "This is __MSG__simple__ message", |
340 "Variable __MSG__simple__ used but not defined.", | 342 "Variable __MSG__simple__ used but not defined.", |
341 kMessageBegin, kMessageEnd, false }, | 343 kMessageBegin, kMessageEnd, false }, |
(...skipping 22 matching lines...) Expand all Loading... |
364 "", kPlaceholderBegin, kPlaceholderEnd, true }, | 366 "", kPlaceholderBegin, kPlaceholderEnd, true }, |
365 { "A $simple$$long_v$", "A simpleA pretty long replacement", | 367 { "A $simple$$long_v$", "A simpleA pretty long replacement", |
366 "", kPlaceholderBegin, kPlaceholderEnd, true }, | 368 "", kPlaceholderBegin, kPlaceholderEnd, true }, |
367 { "This is $bad name$", "This is $bad name$", | 369 { "This is $bad name$", "This is $bad name$", |
368 "", kPlaceholderBegin, kPlaceholderEnd, true }, | 370 "", kPlaceholderBegin, kPlaceholderEnd, true }, |
369 { "This is $missing$", "This is $missing$", | 371 { "This is $missing$", "This is $missing$", |
370 "Variable $missing$ used but not defined.", | 372 "Variable $missing$ used but not defined.", |
371 kPlaceholderBegin, kPlaceholderEnd, false }, | 373 kPlaceholderBegin, kPlaceholderEnd, false }, |
372 }; | 374 }; |
373 | 375 |
374 ExtensionMessageBundle::SubstitutionMap messages; | 376 MessageBundle::SubstitutionMap messages; |
375 messages.insert(std::make_pair("simple", "simple")); | 377 messages.insert(std::make_pair("simple", "simple")); |
376 messages.insert(std::make_pair("long", "A pretty long replacement")); | 378 messages.insert(std::make_pair("long", "A pretty long replacement")); |
377 messages.insert(std::make_pair("long_v", "A pretty long replacement")); | 379 messages.insert(std::make_pair("long_v", "A pretty long replacement")); |
378 messages.insert(std::make_pair("bad name", "Doesn't matter")); | 380 messages.insert(std::make_pair("bad name", "Doesn't matter")); |
379 messages.insert(std::make_pair("d1g1ts_are_ok", "I are d1g1t")); | 381 messages.insert(std::make_pair("d1g1ts_are_ok", "I are d1g1t")); |
380 | 382 |
381 for (size_t i = 0; i < arraysize(test_cases); ++i) { | 383 for (size_t i = 0; i < arraysize(test_cases); ++i) { |
382 std::string text = test_cases[i].original; | 384 std::string text = test_cases[i].original; |
383 std::string error; | 385 std::string error; |
384 EXPECT_EQ(test_cases[i].pass, | 386 EXPECT_EQ(test_cases[i].pass, |
385 ExtensionMessageBundle::ReplaceVariables(messages, | 387 MessageBundle::ReplaceVariables(messages, |
386 test_cases[i].begin_delimiter, | 388 test_cases[i].begin_delimiter, |
387 test_cases[i].end_delimiter, | 389 test_cases[i].end_delimiter, |
388 &text, | 390 &text, |
389 &error)); | 391 &error)); |
390 EXPECT_EQ(test_cases[i].result, text); | 392 EXPECT_EQ(test_cases[i].result, text); |
391 } | 393 } |
392 } | 394 } |
393 | 395 |
394 /////////////////////////////////////////////////////////////////////////////// | 396 /////////////////////////////////////////////////////////////////////////////// |
395 // | 397 // |
396 // Renderer helper functions test. | 398 // Renderer helper functions test. |
397 // | 399 // |
398 /////////////////////////////////////////////////////////////////////////////// | 400 /////////////////////////////////////////////////////////////////////////////// |
399 | 401 |
(...skipping 16 matching lines...) Expand all Loading... |
416 // Store a map for given id. | 418 // Store a map for given id. |
417 L10nMessagesMap messages; | 419 L10nMessagesMap messages; |
418 messages.insert(std::make_pair("message_name", "message_value")); | 420 messages.insert(std::make_pair("message_name", "message_value")); |
419 (*GetExtensionToL10nMessagesMap())[extension_id] = messages; | 421 (*GetExtensionToL10nMessagesMap())[extension_id] = messages; |
420 | 422 |
421 L10nMessagesMap* map = GetL10nMessagesMap(extension_id); | 423 L10nMessagesMap* map = GetL10nMessagesMap(extension_id); |
422 ASSERT_TRUE(NULL != map); | 424 ASSERT_TRUE(NULL != map); |
423 EXPECT_EQ(1U, map->size()); | 425 EXPECT_EQ(1U, map->size()); |
424 EXPECT_EQ("message_value", (*map)["message_name"]); | 426 EXPECT_EQ("message_value", (*map)["message_name"]); |
425 } | 427 } |
| 428 |
| 429 } // namespace extensions |
OLD | NEW |