OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/common/extension_l10n_util.h" | 5 #include "extensions/common/extension_l10n_util.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
208 // JSON parser hides duplicates. We are going to get only one key/value | 208 // JSON parser hides duplicates. We are going to get only one key/value |
209 // pair at the end. | 209 // pair at the end. |
210 std::unique_ptr<MessageBundle> message_bundle( | 210 std::unique_ptr<MessageBundle> message_bundle( |
211 extension_l10n_util::LoadMessageCatalogs(src_path, "en", "sr", &error)); | 211 extension_l10n_util::LoadMessageCatalogs(src_path, "en", "sr", &error)); |
212 EXPECT_TRUE(NULL != message_bundle.get()); | 212 EXPECT_TRUE(NULL != message_bundle.get()); |
213 EXPECT_TRUE(error.empty()); | 213 EXPECT_TRUE(error.empty()); |
214 } | 214 } |
215 | 215 |
216 // Caller owns the returned object. | 216 // Caller owns the returned object. |
217 MessageBundle* CreateManifestBundle() { | 217 MessageBundle* CreateManifestBundle() { |
218 std::unique_ptr<base::DictionaryValue> catalog(new base::DictionaryValue); | 218 auto catalog = base::MakeUnique<base::DictionaryValue>(); |
219 | 219 |
220 base::DictionaryValue* name_tree = new base::DictionaryValue(); | 220 auto name_tree = base::MakeUnique<base::DictionaryValue>(); |
221 name_tree->SetString("message", "name"); | 221 name_tree->SetString("message", "name"); |
222 catalog->Set("name", name_tree); | 222 catalog->Set("name", std::move(name_tree)); |
223 | 223 |
224 base::DictionaryValue* short_name_tree = new base::DictionaryValue(); | 224 auto short_name_tree = base::MakeUnique<base::DictionaryValue>(); |
225 short_name_tree->SetString("message", "short_name"); | 225 short_name_tree->SetString("message", "short_name"); |
226 catalog->Set("short_name", short_name_tree); | 226 catalog->Set("short_name", std::move(short_name_tree)); |
227 | 227 |
228 base::DictionaryValue* description_tree = new base::DictionaryValue(); | 228 auto description_tree = base::MakeUnique<base::DictionaryValue>(); |
229 description_tree->SetString("message", "description"); | 229 description_tree->SetString("message", "description"); |
230 catalog->Set("description", description_tree); | 230 catalog->Set("description", std::move(description_tree)); |
231 | 231 |
232 base::DictionaryValue* action_title_tree = new base::DictionaryValue(); | 232 auto action_title_tree = base::MakeUnique<base::DictionaryValue>(); |
233 action_title_tree->SetString("message", "action title"); | 233 action_title_tree->SetString("message", "action title"); |
234 catalog->Set("title", action_title_tree); | 234 catalog->Set("title", std::move(action_title_tree)); |
235 | 235 |
236 base::DictionaryValue* omnibox_keyword_tree = new base::DictionaryValue(); | 236 auto omnibox_keyword_tree = base::MakeUnique<base::DictionaryValue>(); |
237 omnibox_keyword_tree->SetString("message", "omnibox keyword"); | 237 omnibox_keyword_tree->SetString("message", "omnibox keyword"); |
238 catalog->Set("omnibox_keyword", omnibox_keyword_tree); | 238 catalog->Set("omnibox_keyword", std::move(omnibox_keyword_tree)); |
239 | 239 |
240 base::DictionaryValue* file_handler_title_tree = new base::DictionaryValue(); | 240 auto file_handler_title_tree = base::MakeUnique<base::DictionaryValue>(); |
241 file_handler_title_tree->SetString("message", "file handler title"); | 241 file_handler_title_tree->SetString("message", "file handler title"); |
242 catalog->Set("file_handler_title", file_handler_title_tree); | 242 catalog->Set("file_handler_title", std::move(file_handler_title_tree)); |
243 | 243 |
244 base::DictionaryValue* launch_local_path_tree = new base::DictionaryValue(); | 244 auto launch_local_path_tree = base::MakeUnique<base::DictionaryValue>(); |
245 launch_local_path_tree->SetString("message", "main.html"); | 245 launch_local_path_tree->SetString("message", "main.html"); |
246 catalog->Set("launch_local_path", launch_local_path_tree); | 246 catalog->Set("launch_local_path", std::move(launch_local_path_tree)); |
247 | 247 |
248 base::DictionaryValue* launch_web_url_tree = new base::DictionaryValue(); | 248 auto launch_web_url_tree = base::MakeUnique<base::DictionaryValue>(); |
249 launch_web_url_tree->SetString("message", "http://www.google.com/"); | 249 launch_web_url_tree->SetString("message", "http://www.google.com/"); |
250 catalog->Set("launch_web_url", launch_web_url_tree); | 250 catalog->Set("launch_web_url", std::move(launch_web_url_tree)); |
251 | 251 |
252 base::DictionaryValue* first_command_description_tree = | 252 auto first_command_description_tree = |
253 new base::DictionaryValue(); | 253 base::MakeUnique<base::DictionaryValue>(); |
254 first_command_description_tree->SetString("message", "first command"); | 254 first_command_description_tree->SetString("message", "first command"); |
255 catalog->Set("first_command_description", first_command_description_tree); | 255 catalog->Set("first_command_description", |
256 std::move(first_command_description_tree)); | |
256 | 257 |
257 base::DictionaryValue* second_command_description_tree = | 258 auto second_command_description_tree = |
258 new base::DictionaryValue(); | 259 base::MakeUnique<base::DictionaryValue>(); |
259 second_command_description_tree->SetString("message", "second command"); | 260 second_command_description_tree->SetString("message", "second command"); |
260 catalog->Set("second_command_description", second_command_description_tree); | 261 catalog->Set("second_command_description", |
262 std::move(second_command_description_tree)); | |
261 | 263 |
262 base::DictionaryValue* url_country_tree = new base::DictionaryValue(); | 264 auto url_country_tree = base::MakeUnique<base::DictionaryValue>(); |
263 url_country_tree->SetString("message", "de"); | 265 url_country_tree->SetString("message", "de"); |
264 catalog->Set("country", url_country_tree); | 266 catalog->Set("country", std::move(url_country_tree)); |
265 | 267 |
266 std::vector<std::unique_ptr<base::DictionaryValue>> catalogs; | 268 std::vector<std::unique_ptr<base::DictionaryValue>> catalogs; |
267 catalogs.push_back(std::move(catalog)); | 269 catalogs.push_back(std::move(catalog)); |
268 | 270 |
269 std::string error; | 271 std::string error; |
270 MessageBundle* bundle = MessageBundle::Create(catalogs, &error); | 272 MessageBundle* bundle = MessageBundle::Create(catalogs, &error); |
271 EXPECT_TRUE(bundle); | 273 EXPECT_TRUE(bundle); |
272 EXPECT_TRUE(error.empty()); | 274 EXPECT_TRUE(error.empty()); |
273 | 275 |
274 return bundle; | 276 return bundle; |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
424 ASSERT_TRUE(manifest.GetString(keys::kOmniboxKeyword, &result)); | 426 ASSERT_TRUE(manifest.GetString(keys::kOmniboxKeyword, &result)); |
425 EXPECT_EQ("omnibox keyword", result); | 427 EXPECT_EQ("omnibox keyword", result); |
426 | 428 |
427 EXPECT_TRUE(error.empty()); | 429 EXPECT_TRUE(error.empty()); |
428 } | 430 } |
429 | 431 |
430 TEST(ExtensionL10nUtil, LocalizeManifestWithNameDescriptionFileHandlerTitle) { | 432 TEST(ExtensionL10nUtil, LocalizeManifestWithNameDescriptionFileHandlerTitle) { |
431 base::DictionaryValue manifest; | 433 base::DictionaryValue manifest; |
432 manifest.SetString(keys::kName, "__MSG_name__"); | 434 manifest.SetString(keys::kName, "__MSG_name__"); |
433 manifest.SetString(keys::kDescription, "__MSG_description__"); | 435 manifest.SetString(keys::kDescription, "__MSG_description__"); |
434 base::ListValue* handlers = new base::ListValue(); | 436 base::ListValue* handlers = manifest.SetList( |
Devlin
2017/06/06 14:07:47
nit: Can we restructure this to construct handlers
jdoerrie
2017/06/06 14:48:32
Done, re-getting the handlers is unfortunately sti
| |
435 manifest.Set(keys::kFileBrowserHandlers, handlers); | 437 keys::kFileBrowserHandlers, base::MakeUnique<base::ListValue>()); |
436 handlers->Append(base::MakeUnique<base::DictionaryValue>()); | 438 handlers->Append(base::MakeUnique<base::DictionaryValue>()); |
437 base::DictionaryValue* handler = nullptr; | 439 base::DictionaryValue* handler = nullptr; |
438 handlers->GetDictionary(0, &handler); | 440 handlers->GetDictionary(0, &handler); |
439 handler->SetString(keys::kPageActionDefaultTitle, | 441 handler->SetString(keys::kPageActionDefaultTitle, |
440 "__MSG_file_handler_title__"); | 442 "__MSG_file_handler_title__"); |
441 | 443 |
442 std::string error; | 444 std::string error; |
443 std::unique_ptr<MessageBundle> messages(CreateManifestBundle()); | 445 std::unique_ptr<MessageBundle> messages(CreateManifestBundle()); |
444 | 446 |
445 EXPECT_TRUE( | 447 EXPECT_TRUE( |
446 extension_l10n_util::LocalizeManifest(*messages, &manifest, &error)); | 448 extension_l10n_util::LocalizeManifest(*messages, &manifest, &error)); |
447 | 449 |
448 std::string result; | 450 std::string result; |
449 ASSERT_TRUE(manifest.GetString(keys::kName, &result)); | 451 ASSERT_TRUE(manifest.GetString(keys::kName, &result)); |
450 EXPECT_EQ("name", result); | 452 EXPECT_EQ("name", result); |
451 | 453 |
452 ASSERT_TRUE(manifest.GetString(keys::kDescription, &result)); | 454 ASSERT_TRUE(manifest.GetString(keys::kDescription, &result)); |
453 EXPECT_EQ("description", result); | 455 EXPECT_EQ("description", result); |
454 | 456 |
455 ASSERT_TRUE(handler->GetString(keys::kPageActionDefaultTitle, &result)); | 457 ASSERT_TRUE(handler->GetString(keys::kPageActionDefaultTitle, &result)); |
456 EXPECT_EQ("file handler title", result); | 458 EXPECT_EQ("file handler title", result); |
457 | 459 |
458 EXPECT_TRUE(error.empty()); | 460 EXPECT_TRUE(error.empty()); |
459 } | 461 } |
460 | 462 |
461 TEST(ExtensionL10nUtil, LocalizeManifestWithNameDescriptionCommandDescription) { | 463 TEST(ExtensionL10nUtil, LocalizeManifestWithNameDescriptionCommandDescription) { |
462 base::DictionaryValue manifest; | 464 base::DictionaryValue manifest; |
463 manifest.SetString(keys::kName, "__MSG_name__"); | 465 manifest.SetString(keys::kName, "__MSG_name__"); |
464 manifest.SetString(keys::kDescription, "__MSG_description__"); | 466 manifest.SetString(keys::kDescription, "__MSG_description__"); |
465 base::DictionaryValue* commands = new base::DictionaryValue(); | 467 auto commands = base::MakeUnique<base::DictionaryValue>(); |
466 std::string commands_title(keys::kCommands); | 468 std::string commands_title(keys::kCommands); |
467 manifest.Set(commands_title, commands); | |
468 | 469 |
469 base::DictionaryValue* first_command = new base::DictionaryValue(); | 470 auto first_command = base::MakeUnique<base::DictionaryValue>(); |
470 commands->Set("first_command", first_command); | |
471 first_command->SetString(keys::kDescription, | 471 first_command->SetString(keys::kDescription, |
472 "__MSG_first_command_description__"); | 472 "__MSG_first_command_description__"); |
473 commands->Set("first_command", std::move(first_command)); | |
473 | 474 |
474 base::DictionaryValue* second_command = new base::DictionaryValue(); | 475 auto second_command = base::MakeUnique<base::DictionaryValue>(); |
475 commands->Set("second_command", second_command); | |
476 second_command->SetString(keys::kDescription, | 476 second_command->SetString(keys::kDescription, |
477 "__MSG_second_command_description__"); | 477 "__MSG_second_command_description__"); |
478 commands->Set("second_command", std::move(second_command)); | |
479 manifest.Set(commands_title, std::move(commands)); | |
478 | 480 |
479 std::string error; | 481 std::string error; |
480 std::unique_ptr<MessageBundle> messages(CreateManifestBundle()); | 482 std::unique_ptr<MessageBundle> messages(CreateManifestBundle()); |
481 | 483 |
482 EXPECT_TRUE( | 484 EXPECT_TRUE( |
483 extension_l10n_util::LocalizeManifest(*messages, &manifest, &error)); | 485 extension_l10n_util::LocalizeManifest(*messages, &manifest, &error)); |
484 | 486 |
485 std::string result; | 487 std::string result; |
486 ASSERT_TRUE(manifest.GetString(keys::kName, &result)); | 488 ASSERT_TRUE(manifest.GetString(keys::kName, &result)); |
487 EXPECT_EQ("name", result); | 489 EXPECT_EQ("name", result); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
532 std::string result; | 534 std::string result; |
533 ASSERT_TRUE(manifest.GetString(keys::kShortName, &result)); | 535 ASSERT_TRUE(manifest.GetString(keys::kShortName, &result)); |
534 EXPECT_EQ("__MSG_short_name_bad__", result); | 536 EXPECT_EQ("__MSG_short_name_bad__", result); |
535 } | 537 } |
536 | 538 |
537 TEST(ExtensionL10nUtil, LocalizeManifestWithSearchProviderMsgs) { | 539 TEST(ExtensionL10nUtil, LocalizeManifestWithSearchProviderMsgs) { |
538 base::DictionaryValue manifest; | 540 base::DictionaryValue manifest; |
539 manifest.SetString(keys::kName, "__MSG_name__"); | 541 manifest.SetString(keys::kName, "__MSG_name__"); |
540 manifest.SetString(keys::kDescription, "__MSG_description__"); | 542 manifest.SetString(keys::kDescription, "__MSG_description__"); |
541 | 543 |
542 base::DictionaryValue* search_provider = new base::DictionaryValue; | 544 auto search_provider = base::MakeUnique<base::DictionaryValue>(); |
543 search_provider->SetString("name", "__MSG_country__"); | 545 search_provider->SetString("name", "__MSG_country__"); |
544 search_provider->SetString("keyword", "__MSG_omnibox_keyword__"); | 546 search_provider->SetString("keyword", "__MSG_omnibox_keyword__"); |
545 search_provider->SetString("search_url", "http://www.foo.__MSG_country__"); | 547 search_provider->SetString("search_url", "http://www.foo.__MSG_country__"); |
546 search_provider->SetString("favicon_url", "http://www.foo.__MSG_country__"); | 548 search_provider->SetString("favicon_url", "http://www.foo.__MSG_country__"); |
547 search_provider->SetString("suggest_url", "http://www.foo.__MSG_country__"); | 549 search_provider->SetString("suggest_url", "http://www.foo.__MSG_country__"); |
548 manifest.Set(keys::kOverrideSearchProvider, search_provider); | 550 manifest.Set(keys::kOverrideSearchProvider, std::move(search_provider)); |
549 | 551 |
550 manifest.SetString(keys::kOverrideHomepage, "http://www.foo.__MSG_country__"); | 552 manifest.SetString(keys::kOverrideHomepage, "http://www.foo.__MSG_country__"); |
551 | 553 |
552 base::ListValue* startup_pages = new base::ListValue; | 554 auto startup_pages = base::MakeUnique<base::ListValue>(); |
553 startup_pages->AppendString("http://www.foo.__MSG_country__"); | 555 startup_pages->AppendString("http://www.foo.__MSG_country__"); |
554 manifest.Set(keys::kOverrideStartupPage, startup_pages); | 556 manifest.Set(keys::kOverrideStartupPage, std::move(startup_pages)); |
555 | 557 |
556 std::string error; | 558 std::string error; |
557 std::unique_ptr<MessageBundle> messages(CreateManifestBundle()); | 559 std::unique_ptr<MessageBundle> messages(CreateManifestBundle()); |
558 | 560 |
559 EXPECT_TRUE( | 561 EXPECT_TRUE( |
560 extension_l10n_util::LocalizeManifest(*messages, &manifest, &error)); | 562 extension_l10n_util::LocalizeManifest(*messages, &manifest, &error)); |
561 | 563 |
562 std::string result; | 564 std::string result; |
563 ASSERT_TRUE(manifest.GetString(keys::kName, &result)); | 565 ASSERT_TRUE(manifest.GetString(keys::kName, &result)); |
564 EXPECT_EQ("name", result); | 566 EXPECT_EQ("name", result); |
(...skipping 14 matching lines...) Expand all Loading... | |
579 | 581 |
580 ASSERT_TRUE(manifest.GetString(key_prefix + "favicon_url", &result)); | 582 ASSERT_TRUE(manifest.GetString(key_prefix + "favicon_url", &result)); |
581 EXPECT_EQ("http://www.foo.de", result); | 583 EXPECT_EQ("http://www.foo.de", result); |
582 | 584 |
583 ASSERT_TRUE(manifest.GetString(key_prefix + "suggest_url", &result)); | 585 ASSERT_TRUE(manifest.GetString(key_prefix + "suggest_url", &result)); |
584 EXPECT_EQ("http://www.foo.de", result); | 586 EXPECT_EQ("http://www.foo.de", result); |
585 | 587 |
586 ASSERT_TRUE(manifest.GetString(keys::kOverrideHomepage, &result)); | 588 ASSERT_TRUE(manifest.GetString(keys::kOverrideHomepage, &result)); |
587 EXPECT_EQ("http://www.foo.de", result); | 589 EXPECT_EQ("http://www.foo.de", result); |
588 | 590 |
589 ASSERT_TRUE(manifest.GetList(keys::kOverrideStartupPage, &startup_pages)); | 591 base::ListValue* startup_pages_raw = nullptr; |
590 ASSERT_TRUE(startup_pages->GetString(0, &result)); | 592 ASSERT_TRUE(manifest.GetList(keys::kOverrideStartupPage, &startup_pages_raw)); |
593 ASSERT_TRUE(startup_pages_raw->GetString(0, &result)); | |
591 EXPECT_EQ("http://www.foo.de", result); | 594 EXPECT_EQ("http://www.foo.de", result); |
592 | 595 |
593 EXPECT_TRUE(error.empty()); | 596 EXPECT_TRUE(error.empty()); |
594 } | 597 } |
595 | 598 |
596 // Try with NULL manifest. | 599 // Try with NULL manifest. |
597 TEST(ExtensionL10nUtil, ShouldRelocalizeManifestWithNullManifest) { | 600 TEST(ExtensionL10nUtil, ShouldRelocalizeManifestWithNullManifest) { |
598 EXPECT_FALSE(extension_l10n_util::ShouldRelocalizeManifest(NULL)); | 601 EXPECT_FALSE(extension_l10n_util::ShouldRelocalizeManifest(NULL)); |
599 } | 602 } |
600 | 603 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
641 extension_l10n_util::GetAllFallbackLocales("en_US", "all", &fallback_locales); | 644 extension_l10n_util::GetAllFallbackLocales("en_US", "all", &fallback_locales); |
642 ASSERT_EQ(3U, fallback_locales.size()); | 645 ASSERT_EQ(3U, fallback_locales.size()); |
643 | 646 |
644 CHECK_EQ("en_US", fallback_locales[0]); | 647 CHECK_EQ("en_US", fallback_locales[0]); |
645 CHECK_EQ("en", fallback_locales[1]); | 648 CHECK_EQ("en", fallback_locales[1]); |
646 CHECK_EQ("all", fallback_locales[2]); | 649 CHECK_EQ("all", fallback_locales[2]); |
647 } | 650 } |
648 | 651 |
649 } // namespace | 652 } // namespace |
650 } // namespace extensions | 653 } // namespace extensions |
OLD | NEW |