| 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 |
| 435 manifest.Set(keys::kFileBrowserHandlers, handlers); | 437 base::DictionaryValue handler; |
| 436 handlers->Append(base::MakeUnique<base::DictionaryValue>()); | 438 handler.SetString(keys::kPageActionDefaultTitle, |
| 437 base::DictionaryValue* handler = nullptr; | 439 "__MSG_file_handler_title__"); |
| 438 handlers->GetDictionary(0, &handler); | 440 auto handlers = base::MakeUnique<base::ListValue>(); |
| 439 handler->SetString(keys::kPageActionDefaultTitle, | 441 handlers->GetList().push_back(std::move(handler)); |
| 440 "__MSG_file_handler_title__"); | 442 manifest.Set(keys::kFileBrowserHandlers, std::move(handlers)); |
| 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 base::ListValue* handlers_raw = nullptr; |
| 458 manifest.GetList(keys::kFileBrowserHandlers, &handlers_raw); |
| 459 base::DictionaryValue* handler_raw = nullptr; |
| 460 handlers_raw->GetList()[0].GetAsDictionary(&handler_raw); |
| 461 ASSERT_TRUE(handler_raw->GetString(keys::kPageActionDefaultTitle, &result)); |
| 456 EXPECT_EQ("file handler title", result); | 462 EXPECT_EQ("file handler title", result); |
| 457 | 463 |
| 458 EXPECT_TRUE(error.empty()); | 464 EXPECT_TRUE(error.empty()); |
| 459 } | 465 } |
| 460 | 466 |
| 461 TEST(ExtensionL10nUtil, LocalizeManifestWithNameDescriptionCommandDescription) { | 467 TEST(ExtensionL10nUtil, LocalizeManifestWithNameDescriptionCommandDescription) { |
| 462 base::DictionaryValue manifest; | 468 base::DictionaryValue manifest; |
| 463 manifest.SetString(keys::kName, "__MSG_name__"); | 469 manifest.SetString(keys::kName, "__MSG_name__"); |
| 464 manifest.SetString(keys::kDescription, "__MSG_description__"); | 470 manifest.SetString(keys::kDescription, "__MSG_description__"); |
| 465 base::DictionaryValue* commands = new base::DictionaryValue(); | 471 auto commands = base::MakeUnique<base::DictionaryValue>(); |
| 466 std::string commands_title(keys::kCommands); | 472 std::string commands_title(keys::kCommands); |
| 467 manifest.Set(commands_title, commands); | |
| 468 | 473 |
| 469 base::DictionaryValue* first_command = new base::DictionaryValue(); | 474 auto first_command = base::MakeUnique<base::DictionaryValue>(); |
| 470 commands->Set("first_command", first_command); | |
| 471 first_command->SetString(keys::kDescription, | 475 first_command->SetString(keys::kDescription, |
| 472 "__MSG_first_command_description__"); | 476 "__MSG_first_command_description__"); |
| 477 commands->Set("first_command", std::move(first_command)); |
| 473 | 478 |
| 474 base::DictionaryValue* second_command = new base::DictionaryValue(); | 479 auto second_command = base::MakeUnique<base::DictionaryValue>(); |
| 475 commands->Set("second_command", second_command); | |
| 476 second_command->SetString(keys::kDescription, | 480 second_command->SetString(keys::kDescription, |
| 477 "__MSG_second_command_description__"); | 481 "__MSG_second_command_description__"); |
| 482 commands->Set("second_command", std::move(second_command)); |
| 483 manifest.Set(commands_title, std::move(commands)); |
| 478 | 484 |
| 479 std::string error; | 485 std::string error; |
| 480 std::unique_ptr<MessageBundle> messages(CreateManifestBundle()); | 486 std::unique_ptr<MessageBundle> messages(CreateManifestBundle()); |
| 481 | 487 |
| 482 EXPECT_TRUE( | 488 EXPECT_TRUE( |
| 483 extension_l10n_util::LocalizeManifest(*messages, &manifest, &error)); | 489 extension_l10n_util::LocalizeManifest(*messages, &manifest, &error)); |
| 484 | 490 |
| 485 std::string result; | 491 std::string result; |
| 486 ASSERT_TRUE(manifest.GetString(keys::kName, &result)); | 492 ASSERT_TRUE(manifest.GetString(keys::kName, &result)); |
| 487 EXPECT_EQ("name", result); | 493 EXPECT_EQ("name", result); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 532 std::string result; | 538 std::string result; |
| 533 ASSERT_TRUE(manifest.GetString(keys::kShortName, &result)); | 539 ASSERT_TRUE(manifest.GetString(keys::kShortName, &result)); |
| 534 EXPECT_EQ("__MSG_short_name_bad__", result); | 540 EXPECT_EQ("__MSG_short_name_bad__", result); |
| 535 } | 541 } |
| 536 | 542 |
| 537 TEST(ExtensionL10nUtil, LocalizeManifestWithSearchProviderMsgs) { | 543 TEST(ExtensionL10nUtil, LocalizeManifestWithSearchProviderMsgs) { |
| 538 base::DictionaryValue manifest; | 544 base::DictionaryValue manifest; |
| 539 manifest.SetString(keys::kName, "__MSG_name__"); | 545 manifest.SetString(keys::kName, "__MSG_name__"); |
| 540 manifest.SetString(keys::kDescription, "__MSG_description__"); | 546 manifest.SetString(keys::kDescription, "__MSG_description__"); |
| 541 | 547 |
| 542 base::DictionaryValue* search_provider = new base::DictionaryValue; | 548 auto search_provider = base::MakeUnique<base::DictionaryValue>(); |
| 543 search_provider->SetString("name", "__MSG_country__"); | 549 search_provider->SetString("name", "__MSG_country__"); |
| 544 search_provider->SetString("keyword", "__MSG_omnibox_keyword__"); | 550 search_provider->SetString("keyword", "__MSG_omnibox_keyword__"); |
| 545 search_provider->SetString("search_url", "http://www.foo.__MSG_country__"); | 551 search_provider->SetString("search_url", "http://www.foo.__MSG_country__"); |
| 546 search_provider->SetString("favicon_url", "http://www.foo.__MSG_country__"); | 552 search_provider->SetString("favicon_url", "http://www.foo.__MSG_country__"); |
| 547 search_provider->SetString("suggest_url", "http://www.foo.__MSG_country__"); | 553 search_provider->SetString("suggest_url", "http://www.foo.__MSG_country__"); |
| 548 manifest.Set(keys::kOverrideSearchProvider, search_provider); | 554 manifest.Set(keys::kOverrideSearchProvider, std::move(search_provider)); |
| 549 | 555 |
| 550 manifest.SetString(keys::kOverrideHomepage, "http://www.foo.__MSG_country__"); | 556 manifest.SetString(keys::kOverrideHomepage, "http://www.foo.__MSG_country__"); |
| 551 | 557 |
| 552 base::ListValue* startup_pages = new base::ListValue; | 558 auto startup_pages = base::MakeUnique<base::ListValue>(); |
| 553 startup_pages->AppendString("http://www.foo.__MSG_country__"); | 559 startup_pages->AppendString("http://www.foo.__MSG_country__"); |
| 554 manifest.Set(keys::kOverrideStartupPage, startup_pages); | 560 manifest.Set(keys::kOverrideStartupPage, std::move(startup_pages)); |
| 555 | 561 |
| 556 std::string error; | 562 std::string error; |
| 557 std::unique_ptr<MessageBundle> messages(CreateManifestBundle()); | 563 std::unique_ptr<MessageBundle> messages(CreateManifestBundle()); |
| 558 | 564 |
| 559 EXPECT_TRUE( | 565 EXPECT_TRUE( |
| 560 extension_l10n_util::LocalizeManifest(*messages, &manifest, &error)); | 566 extension_l10n_util::LocalizeManifest(*messages, &manifest, &error)); |
| 561 | 567 |
| 562 std::string result; | 568 std::string result; |
| 563 ASSERT_TRUE(manifest.GetString(keys::kName, &result)); | 569 ASSERT_TRUE(manifest.GetString(keys::kName, &result)); |
| 564 EXPECT_EQ("name", result); | 570 EXPECT_EQ("name", result); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 579 | 585 |
| 580 ASSERT_TRUE(manifest.GetString(key_prefix + "favicon_url", &result)); | 586 ASSERT_TRUE(manifest.GetString(key_prefix + "favicon_url", &result)); |
| 581 EXPECT_EQ("http://www.foo.de", result); | 587 EXPECT_EQ("http://www.foo.de", result); |
| 582 | 588 |
| 583 ASSERT_TRUE(manifest.GetString(key_prefix + "suggest_url", &result)); | 589 ASSERT_TRUE(manifest.GetString(key_prefix + "suggest_url", &result)); |
| 584 EXPECT_EQ("http://www.foo.de", result); | 590 EXPECT_EQ("http://www.foo.de", result); |
| 585 | 591 |
| 586 ASSERT_TRUE(manifest.GetString(keys::kOverrideHomepage, &result)); | 592 ASSERT_TRUE(manifest.GetString(keys::kOverrideHomepage, &result)); |
| 587 EXPECT_EQ("http://www.foo.de", result); | 593 EXPECT_EQ("http://www.foo.de", result); |
| 588 | 594 |
| 589 ASSERT_TRUE(manifest.GetList(keys::kOverrideStartupPage, &startup_pages)); | 595 base::ListValue* startup_pages_raw = nullptr; |
| 590 ASSERT_TRUE(startup_pages->GetString(0, &result)); | 596 ASSERT_TRUE(manifest.GetList(keys::kOverrideStartupPage, &startup_pages_raw)); |
| 597 ASSERT_TRUE(startup_pages_raw->GetString(0, &result)); |
| 591 EXPECT_EQ("http://www.foo.de", result); | 598 EXPECT_EQ("http://www.foo.de", result); |
| 592 | 599 |
| 593 EXPECT_TRUE(error.empty()); | 600 EXPECT_TRUE(error.empty()); |
| 594 } | 601 } |
| 595 | 602 |
| 596 // Try with NULL manifest. | 603 // Try with NULL manifest. |
| 597 TEST(ExtensionL10nUtil, ShouldRelocalizeManifestWithNullManifest) { | 604 TEST(ExtensionL10nUtil, ShouldRelocalizeManifestWithNullManifest) { |
| 598 EXPECT_FALSE(extension_l10n_util::ShouldRelocalizeManifest(NULL)); | 605 EXPECT_FALSE(extension_l10n_util::ShouldRelocalizeManifest(NULL)); |
| 599 } | 606 } |
| 600 | 607 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 641 extension_l10n_util::GetAllFallbackLocales("en_US", "all", &fallback_locales); | 648 extension_l10n_util::GetAllFallbackLocales("en_US", "all", &fallback_locales); |
| 642 ASSERT_EQ(3U, fallback_locales.size()); | 649 ASSERT_EQ(3U, fallback_locales.size()); |
| 643 | 650 |
| 644 CHECK_EQ("en_US", fallback_locales[0]); | 651 CHECK_EQ("en_US", fallback_locales[0]); |
| 645 CHECK_EQ("en", fallback_locales[1]); | 652 CHECK_EQ("en", fallback_locales[1]); |
| 646 CHECK_EQ("all", fallback_locales[2]); | 653 CHECK_EQ("all", fallback_locales[2]); |
| 647 } | 654 } |
| 648 | 655 |
| 649 } // namespace | 656 } // namespace |
| 650 } // namespace extensions | 657 } // namespace extensions |
| OLD | NEW |