1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/common/extension_l10n_util.h"
#include <memory>
#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/path_service.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "extensions/common/constants.h"
#include "extensions/common/error_utils.h"
#include "extensions/common/extension_paths.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/message_bundle.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/zlib/google/compression_utils.h"
#include "ui/base/l10n/l10n_util.h"
using extension_l10n_util::GzippedMessagesPermission;
namespace extensions {
namespace errors = manifest_errors;
namespace keys = manifest_keys;
namespace {
TEST(ExtensionL10nUtil, ValidateLocalesWithBadLocale) {
base::ScopedTempDir temp;
ASSERT_TRUE(temp.CreateUniqueTempDir());
base::FilePath src_path = temp.GetPath().Append(kLocaleFolder);
base::FilePath locale = src_path.AppendASCII("ms");
ASSERT_TRUE(base::CreateDirectory(locale));
base::FilePath messages_file = locale.Append(kMessagesFilename);
std::string data = "{ \"name\":";
ASSERT_TRUE(base::WriteFile(messages_file, data));
auto manifest = base::Value::Dict().Set(keys::kDefaultLocale, "en");
std::string error;
EXPECT_FALSE(extension_l10n_util::ValidateExtensionLocales(temp.GetPath(),
manifest, &error));
EXPECT_THAT(
error,
testing::HasSubstr(base::UTF16ToUTF8(messages_file.LossyDisplayName())));
}
TEST(ExtensionL10nUtil, ValidateLocalesWithErroneousLocalizations) {
base::ScopedTempDir temp;
ASSERT_TRUE(temp.CreateUniqueTempDir());
base::FilePath src_path = temp.GetPath().Append(kLocaleFolder);
ASSERT_TRUE(base::CreateDirectory(src_path));
// Add valid default localization file.
base::FilePath en_locale = src_path.AppendASCII("en");
ASSERT_TRUE(base::CreateDirectory(en_locale));
base::FilePath en_messages_file = en_locale.Append(kMessagesFilename);
const std::string en_data = R"({ "name": { "message": "default" } })";
ASSERT_TRUE(base::WriteFile(en_messages_file, en_data));
// Add additional valid localization file.
base::FilePath sr_locale = src_path.AppendASCII("sr");
ASSERT_TRUE(base::CreateDirectory(sr_locale));
base::FilePath sr_messages_file = sr_locale.Append(kMessagesFilename);
const std::string sr_data = R"({ "name": { "message": "valid" } })";
ASSERT_TRUE(base::WriteFile(sr_messages_file, sr_data));
// Add additional localization file with undefined variable.
base::FilePath de_locale = src_path.AppendASCII("de");
ASSERT_TRUE(base::CreateDirectory(de_locale));
base::FilePath de_messages_file = de_locale.Append(kMessagesFilename);
const std::string de_data = R"({ "name": { "message": "with $VAR$" } })";
ASSERT_TRUE(base::WriteFile(de_messages_file, de_data));
// Add additional localization file with syntax error.
base::FilePath es_locale = src_path.AppendASCII("es");
ASSERT_TRUE(base::CreateDirectory(es_locale));
base::FilePath es_messages_file = es_locale.Append(kMessagesFilename);
const std::string es_data = R"({ "name": { "message": } })";
ASSERT_TRUE(base::WriteFile(es_messages_file, es_data));
// Add additional localization file with missing property.
base::FilePath fr_locale = src_path.AppendASCII("fr");
ASSERT_TRUE(base::CreateDirectory(fr_locale));
base::FilePath fr_messages_file = fr_locale.Append(kMessagesFilename);
const std::string fr_data = R"({ "name": { } })";
ASSERT_TRUE(base::WriteFile(fr_messages_file, fr_data));
const auto manifest = base::Value::Dict().Set(keys::kDefaultLocale, "en");
std::string error;
EXPECT_FALSE(extension_l10n_util::ValidateExtensionLocales(temp.GetPath(),
manifest, &error));
EXPECT_FALSE(base::Contains(
error, base::UTF16ToUTF8(sr_messages_file.LossyDisplayName())));
EXPECT_THAT(error, testing::HasSubstr(ErrorUtils::FormatErrorMessage(
errors::kLocalesInvalidLocale,
base::UTF16ToUTF8(de_messages_file.LossyDisplayName()),
"Variable $VAR$ used but not defined.")));
EXPECT_THAT(error, testing::HasSubstr(ErrorUtils::FormatErrorMessage(
errors::kLocalesInvalidLocale,
base::UTF16ToUTF8(es_messages_file.LossyDisplayName()),
"expected value at line 1 column 24")));
EXPECT_THAT(error, testing::HasSubstr(ErrorUtils::FormatErrorMessage(
errors::kLocalesInvalidLocale,
base::UTF16ToUTF8(fr_messages_file.LossyDisplayName()),
"There is no \"message\" element for key name.")));
}
TEST(ExtensionL10nUtil, GetValidLocalesEmptyLocaleFolder) {
base::ScopedTempDir temp;
ASSERT_TRUE(temp.CreateUniqueTempDir());
base::FilePath src_path = temp.GetPath().Append(kLocaleFolder);
ASSERT_TRUE(base::CreateDirectory(src_path));
std::string error;
std::set<std::string> locales;
EXPECT_FALSE(
extension_l10n_util::GetValidLocales(src_path, &locales, &error));
EXPECT_TRUE(locales.empty());
}
TEST(ExtensionL10nUtil, GetValidLocalesWithValidLocaleNoMessagesFile) {
base::ScopedTempDir temp;
ASSERT_TRUE(temp.CreateUniqueTempDir());
base::FilePath src_path = temp.GetPath().Append(kLocaleFolder);
ASSERT_TRUE(base::CreateDirectory(src_path));
ASSERT_TRUE(base::CreateDirectory(src_path.AppendASCII("sr")));
std::string error;
std::set<std::string> locales;
EXPECT_FALSE(
extension_l10n_util::GetValidLocales(src_path, &locales, &error));
EXPECT_TRUE(locales.empty());
}
TEST(ExtensionL10nUtil, GetValidLocalesWithUnsupportedLocale) {
base::ScopedTempDir temp;
ASSERT_TRUE(temp.CreateUniqueTempDir());
base::FilePath src_path = temp.GetPath().Append(kLocaleFolder);
ASSERT_TRUE(base::CreateDirectory(src_path));
// Supported locale.
base::FilePath locale_1 = src_path.AppendASCII("sr");
ASSERT_TRUE(base::CreateDirectory(locale_1));
ASSERT_TRUE(base::WriteFile(locale_1.Append(kMessagesFilename), ""));
// Unsupported locale.
base::FilePath locale_2 = src_path.AppendASCII("xxx_yyy");
ASSERT_TRUE(base::CreateDirectory(locale_2));
ASSERT_TRUE(base::WriteFile(locale_2.Append(kMessagesFilename), ""));
std::string error;
std::set<std::string> locales;
EXPECT_TRUE(extension_l10n_util::GetValidLocales(src_path, &locales, &error));
EXPECT_FALSE(locales.empty());
EXPECT_TRUE(base::Contains(locales, "sr"));
EXPECT_FALSE(base::Contains(locales, "xxx_yyy"));
}
TEST(ExtensionL10nUtil, GetValidLocalesWithValidLocalesAndMessagesFile) {
base::FilePath install_dir;
ASSERT_TRUE(base::PathService::Get(DIR_TEST_DATA, &install_dir));
install_dir =
install_dir.AppendASCII("extension_with_locales").Append(kLocaleFolder);
std::string error;
std::set<std::string> locales;
EXPECT_TRUE(
extension_l10n_util::GetValidLocales(install_dir, &locales, &error));
EXPECT_EQ(3U, locales.size());
EXPECT_TRUE(base::Contains(locales, "sr"));
EXPECT_TRUE(base::Contains(locales, "en"));
EXPECT_TRUE(base::Contains(locales, "en_US"));
}
TEST(ExtensionL10nUtil, LoadMessageCatalogsValidFallback) {
extension_l10n_util::ScopedLocaleForTest scoped_locale("en-US");
base::FilePath install_dir;
ASSERT_TRUE(base::PathService::Get(DIR_TEST_DATA, &install_dir));
install_dir =
install_dir.AppendASCII("extension_with_locales").Append(kLocaleFolder);
std::string error;
std::unique_ptr<MessageBundle> bundle(
extension_l10n_util::LoadMessageCatalogs(
install_dir, "sr", GzippedMessagesPermission::kDisallow, &error));
ASSERT_TRUE(bundle);
EXPECT_TRUE(error.empty());
EXPECT_EQ("Color", bundle->GetL10nMessage("color"));
EXPECT_EQ("Not in the US or GB.", bundle->GetL10nMessage("not_in_US_or_GB"));
}
TEST(ExtensionL10nUtil, LoadMessageCatalogsLowercaseLocales) {
extension_l10n_util::ScopedLocaleForTest scoped_locale("en-US");
base::FilePath install_dir;
ASSERT_TRUE(base::PathService::Get(DIR_TEST_DATA, &install_dir));
install_dir = install_dir.AppendASCII("extension_with_lowercase_locales")
.Append(kLocaleFolder);
std::string error;
std::unique_ptr<MessageBundle> bundle(
extension_l10n_util::LoadMessageCatalogs(
install_dir, "en-US", GzippedMessagesPermission::kDisallow, &error));
ASSERT_TRUE(bundle);
EXPECT_TRUE(error.empty());
const base::FilePath locale_uppercase_path = install_dir.AppendASCII("en_US");
const base::FilePath locale_lowercase_path = install_dir.AppendASCII("en_us");
if (base::PathExists(locale_uppercase_path) &&
base::PathExists(locale_lowercase_path)) {
// Path system is case-insensitive.
EXPECT_EQ("color lowercase", bundle->GetL10nMessage("color"));
} else {
EXPECT_EQ("", bundle->GetL10nMessage("color"));
}
std::set<std::string> all_locales;
extension_l10n_util::GetAllLocales(&all_locales);
EXPECT_FALSE(extension_l10n_util::ShouldSkipValidation(
install_dir, locale_uppercase_path, all_locales));
EXPECT_FALSE(extension_l10n_util::ShouldSkipValidation(
install_dir, locale_lowercase_path, all_locales));
}
TEST(ExtensionL10nUtil, LoadMessageCatalogsMissingFiles) {
extension_l10n_util::ScopedLocaleForTest scoped_locale("sr");
base::ScopedTempDir temp;
ASSERT_TRUE(temp.CreateUniqueTempDir());
base::FilePath src_path = temp.GetPath().Append(kLocaleFolder);
ASSERT_TRUE(base::CreateDirectory(src_path));
ASSERT_TRUE(base::CreateDirectory(src_path.AppendASCII("en")));
ASSERT_TRUE(base::CreateDirectory(src_path.AppendASCII("sr")));
std::string error;
EXPECT_FALSE(extension_l10n_util::LoadMessageCatalogs(
src_path, "en", GzippedMessagesPermission::kDisallow, &error));
EXPECT_FALSE(error.empty());
}
TEST(ExtensionL10nUtil, LoadMessageCatalogsBadJSONFormat) {
extension_l10n_util::ScopedLocaleForTest scoped_locale("sr");
base::ScopedTempDir temp;
ASSERT_TRUE(temp.CreateUniqueTempDir());
base::FilePath src_path = temp.GetPath().Append(kLocaleFolder);
ASSERT_TRUE(base::CreateDirectory(src_path));
base::FilePath locale = src_path.AppendASCII("sr");
ASSERT_TRUE(base::CreateDirectory(locale));
std::string data = "{ \"name\":";
base::FilePath messages_file = locale.Append(kMessagesFilename);
ASSERT_TRUE(base::WriteFile(messages_file, data));
std::string error;
EXPECT_FALSE(extension_l10n_util::LoadMessageCatalogs(
src_path, "en_US", GzippedMessagesPermission::kDisallow, &error));
EXPECT_NE(std::string::npos,
error.find(ErrorUtils::FormatErrorMessage(
errors::kLocalesInvalidLocale,
base::UTF16ToUTF8(messages_file.LossyDisplayName()),
"EOF while parsing a value at line 1 column 9")));
}
TEST(ExtensionL10nUtil, LoadMessageCatalogsDuplicateKeys) {
extension_l10n_util::ScopedLocaleForTest scoped_locale("sr");
base::ScopedTempDir temp;
ASSERT_TRUE(temp.CreateUniqueTempDir());
base::FilePath src_path = temp.GetPath().Append(kLocaleFolder);
ASSERT_TRUE(base::CreateDirectory(src_path));
base::FilePath locale = src_path.AppendASCII("en");
ASSERT_TRUE(base::CreateDirectory(locale));
std::string data =
"{ \"name\": { \"message\": \"something\" }, "
"\"name\": { \"message\": \"something else\" } }";
ASSERT_TRUE(base::WriteFile(locale.Append(kMessagesFilename), data));
std::string error;
// JSON parser hides duplicates. We are going to get only one key/value
// pair at the end.
std::unique_ptr<MessageBundle> message_bundle(
extension_l10n_util::LoadMessageCatalogs(
src_path, "en", GzippedMessagesPermission::kDisallow, &error));
EXPECT_TRUE(message_bundle.get());
EXPECT_TRUE(error.empty());
}
TEST(ExtensionL10nUtil, LoadMessageCatalogsWithUndefinedVariable) {
extension_l10n_util::ScopedLocaleForTest scoped_locale("sr");
base::ScopedTempDir temp;
ASSERT_TRUE(temp.CreateUniqueTempDir());
base::FilePath src_path = temp.GetPath().Append(kLocaleFolder);
ASSERT_TRUE(base::CreateDirectory(src_path));
base::FilePath locale = src_path.AppendASCII("sr");
ASSERT_TRUE(base::CreateDirectory(locale));
std::string data = R"({ "name": { "message": "with $VAR$" } })";
base::FilePath messages_file = locale.Append(kMessagesFilename);
ASSERT_TRUE(base::WriteFile(messages_file, data));
std::string error;
EXPECT_FALSE(extension_l10n_util::LoadMessageCatalogs(
src_path, "sr", GzippedMessagesPermission::kDisallow, &error));
EXPECT_THAT(error,
testing::HasSubstr("Variable $VAR$ used but not defined."));
}
TEST(ExtensionL10nUtil, LoadMessageCatalogsCompressed) {
extension_l10n_util::ScopedLocaleForTest scoped_locale("sr");
base::ScopedTempDir temp;
ASSERT_TRUE(temp.CreateUniqueTempDir());
base::FilePath src_path = temp.GetPath().Append(kLocaleFolder);
ASSERT_TRUE(base::CreateDirectory(src_path));
base::FilePath locale = src_path.AppendASCII("en");
ASSERT_TRUE(base::CreateDirectory(locale));
// Create a compressed messages.json.gz file.
std::string data = "{ \"name\": { \"message\": \"something\" } }";
std::string compressed_data;
ASSERT_TRUE(compression::GzipCompress(data, &compressed_data));
ASSERT_TRUE(base::WriteFile(
locale.Append(kMessagesFilename).AddExtension(FILE_PATH_LITERAL(".gz")),
compressed_data));
// Test that LoadMessageCatalogs fails with gzip_permission = kDisallow.
std::string error;
std::unique_ptr<MessageBundle> message_bundle(
extension_l10n_util::LoadMessageCatalogs(
src_path, "en", GzippedMessagesPermission::kDisallow, &error));
EXPECT_FALSE(message_bundle.get());
EXPECT_FALSE(error.empty());
// Test that LoadMessageCatalogs succeeds with gzip_permission =
// kAllowForTrustedSource.
error.clear();
message_bundle.reset(extension_l10n_util::LoadMessageCatalogs(
src_path, "en", GzippedMessagesPermission::kAllowForTrustedSource,
&error));
EXPECT_TRUE(message_bundle.get());
EXPECT_TRUE(error.empty());
EXPECT_EQ("something", message_bundle->GetL10nMessage("name"));
}
// Caller owns the returned object.
MessageBundle* CreateManifestBundle() {
base::Value::Dict catalog;
base::Value::Dict name_tree;
name_tree.Set("message", "name");
catalog.Set("name", std::move(name_tree));
base::Value::Dict short_name_tree;
short_name_tree.Set("message", "short_name");
catalog.Set("short_name", std::move(short_name_tree));
base::Value::Dict description_tree;
description_tree.Set("message", "description");
catalog.Set("description", std::move(description_tree));
base::Value::Dict action_title_tree;
action_title_tree.Set("message", "action title");
catalog.Set("title", std::move(action_title_tree));
base::Value::Dict omnibox_keyword_tree;
omnibox_keyword_tree.Set("message", "omnibox keyword");
catalog.Set("omnibox_keyword", std::move(omnibox_keyword_tree));
base::Value::Dict file_handler_title_tree;
file_handler_title_tree.Set("message", "file handler title");
catalog.Set("file_handler_title", std::move(file_handler_title_tree));
base::Value::Dict launch_local_path_tree;
launch_local_path_tree.Set("message", "main.html");
catalog.Set("launch_local_path", std::move(launch_local_path_tree));
base::Value::Dict launch_web_url_tree;
launch_web_url_tree.Set("message", "http://www.google.com/");
catalog.Set("launch_web_url", std::move(launch_web_url_tree));
base::Value::Dict first_command_description_tree;
first_command_description_tree.Set("message", "first command");
catalog.Set("first_command_description",
std::move(first_command_description_tree));
base::Value::Dict second_command_description_tree;
second_command_description_tree.Set("message", "second command");
catalog.Set("second_command_description",
std::move(second_command_description_tree));
base::Value::Dict url_country_tree;
url_country_tree.Set("message", "de");
catalog.Set("country", std::move(url_country_tree));
MessageBundle::CatalogVector catalogs;
catalogs.push_back(std::move(catalog));
std::string error;
MessageBundle* bundle = MessageBundle::Create(catalogs, &error);
EXPECT_TRUE(bundle);
EXPECT_TRUE(error.empty());
return bundle;
}
TEST(ExtensionL10nUtil, LocalizeEmptyManifest) {
base::Value::Dict manifest;
std::string error;
std::unique_ptr<MessageBundle> messages(CreateManifestBundle());
EXPECT_FALSE(
extension_l10n_util::LocalizeManifest(*messages, &manifest, &error));
EXPECT_EQ(std::string(errors::kInvalidName), error);
}
TEST(ExtensionL10nUtil, LocalizeManifestWithoutNameMsgAndEmptyDescription) {
base::Value::Dict manifest;
manifest.Set(keys::kName, "no __MSG");
std::string error;
std::unique_ptr<MessageBundle> messages(CreateManifestBundle());
EXPECT_TRUE(
extension_l10n_util::LocalizeManifest(*messages, &manifest, &error));
const std::string* result = manifest.FindString(keys::kName);
ASSERT_TRUE(result);
EXPECT_EQ("no __MSG", *result);
EXPECT_FALSE(manifest.Find(keys::kDescription));
EXPECT_TRUE(error.empty());
}
TEST(ExtensionL10nUtil, LocalizeManifestWithNameMsgAndEmptyDescription) {
base::Value::Dict manifest;
manifest.Set(keys::kName, "__MSG_name__");
std::string error;
std::unique_ptr<MessageBundle> messages(CreateManifestBundle());
EXPECT_TRUE(
extension_l10n_util::LocalizeManifest(*messages, &manifest, &error));
const std::string* result = manifest.FindString(keys::kName);
ASSERT_TRUE(result);
EXPECT_EQ("name", *result);
EXPECT_FALSE(manifest.Find(keys::kDescription));
EXPECT_TRUE(error.empty());
}
TEST(ExtensionL10nUtil, LocalizeManifestWithLocalLaunchURL) {
base::Value::Dict manifest;
manifest.Set(keys::kName, "name");
manifest.SetByDottedPath(keys::kLaunchLocalPath, "__MSG_launch_local_path__");
std::string error;
std::unique_ptr<MessageBundle> messages(CreateManifestBundle());
EXPECT_TRUE(
extension_l10n_util::LocalizeManifest(*messages, &manifest, &error));
const std::string* result =
manifest.FindStringByDottedPath(keys::kLaunchLocalPath);
ASSERT_TRUE(result);
EXPECT_EQ("main.html", *result);
EXPECT_TRUE(error.empty());
}
TEST(ExtensionL10nUtil, LocalizeManifestWithHostedLaunchURL) {
base::Value::Dict manifest;
manifest.SetByDottedPath(keys::kName, "name");
manifest.SetByDottedPath(keys::kLaunchWebURL, "__MSG_launch_web_url__");
std::string error;
std::unique_ptr<MessageBundle> messages(CreateManifestBundle());
EXPECT_TRUE(
extension_l10n_util::LocalizeManifest(*messages, &manifest, &error));
const std::string* result =
manifest.FindStringByDottedPath(keys::kLaunchWebURL);
ASSERT_TRUE(result);
EXPECT_EQ("http://www.google.com/", *result);
EXPECT_TRUE(error.empty());
}
TEST(ExtensionL10nUtil, LocalizeManifestWithBadNameMsg) {
base::Value::Dict manifest;
manifest.Set(keys::kName, "__MSG_name_is_bad__");
manifest.Set(keys::kDescription, "__MSG_description__");
std::string error;
std::unique_ptr<MessageBundle> messages(CreateManifestBundle());
EXPECT_FALSE(
extension_l10n_util::LocalizeManifest(*messages, &manifest, &error));
const std::string* result = manifest.FindString(keys::kName);
ASSERT_TRUE(result);
EXPECT_EQ("__MSG_name_is_bad__", *result);
result = manifest.FindString(keys::kDescription);
ASSERT_TRUE(result);
EXPECT_EQ("__MSG_description__", *result);
EXPECT_EQ("Variable __MSG_name_is_bad__ used but not defined.", error);
}
TEST(ExtensionL10nUtil, LocalizeManifestWithNameDescriptionDefaultTitleMsgs) {
base::Value::Dict manifest;
manifest.Set(keys::kName, "__MSG_name__");
manifest.Set(keys::kDescription, "__MSG_description__");
std::string action_title(keys::kBrowserAction);
action_title.append(".");
action_title.append(keys::kActionDefaultTitle);
manifest.SetByDottedPath(action_title, "__MSG_title__");
std::string error;
std::unique_ptr<MessageBundle> messages(CreateManifestBundle());
EXPECT_TRUE(
extension_l10n_util::LocalizeManifest(*messages, &manifest, &error));
const std::string* result = manifest.FindString(keys::kName);
ASSERT_TRUE(result);
EXPECT_EQ("name", *result);
result = manifest.FindString(keys::kDescription);
ASSERT_TRUE(result);
EXPECT_EQ("description", *result);
result = manifest.FindStringByDottedPath(action_title);
ASSERT_TRUE(result);
EXPECT_EQ("action title", *result);
EXPECT_TRUE(error.empty());
}
TEST(ExtensionL10nUtil, LocalizeManifestWithNameDescriptionOmniboxMsgs) {
base::Value::Dict manifest;
manifest.Set(keys::kName, "__MSG_name__");
manifest.Set(keys::kDescription, "__MSG_description__");
manifest.SetByDottedPath(keys::kOmniboxKeyword, "__MSG_omnibox_keyword__");
std::string error;
std::unique_ptr<MessageBundle> messages(CreateManifestBundle());
EXPECT_TRUE(
extension_l10n_util::LocalizeManifest(*messages, &manifest, &error));
const std::string* result = manifest.FindString(keys::kName);
ASSERT_TRUE(result);
EXPECT_EQ("name", *result);
result = manifest.FindString(keys::kDescription);
ASSERT_TRUE(result);
EXPECT_EQ("description", *result);
result = manifest.FindStringByDottedPath(keys::kOmniboxKeyword);
ASSERT_TRUE(result);
EXPECT_EQ("omnibox keyword", *result);
EXPECT_TRUE(error.empty());
}
TEST(ExtensionL10nUtil, LocalizeManifestWithNameDescriptionFileHandlerTitle) {
base::Value::Dict manifest;
manifest.Set(keys::kName, "__MSG_name__");
manifest.Set(keys::kDescription, "__MSG_description__");
base::Value::Dict handler;
handler.Set(keys::kActionDefaultTitle, "__MSG_file_handler_title__");
base::Value::List handlers;
handlers.Append(std::move(handler));
manifest.Set(keys::kFileBrowserHandlers, std::move(handlers));
std::string error;
std::unique_ptr<MessageBundle> messages(CreateManifestBundle());
EXPECT_TRUE(
extension_l10n_util::LocalizeManifest(*messages, &manifest, &error));
const std::string* result = manifest.FindString(keys::kName);
ASSERT_TRUE(result);
EXPECT_EQ("name", *result);
result = manifest.FindString(keys::kDescription);
ASSERT_TRUE(result);
EXPECT_EQ("description", *result);
base::Value::List* handlers_raw =
manifest.FindList(keys::kFileBrowserHandlers);
ASSERT_TRUE(handlers_raw);
ASSERT_EQ(handlers_raw->size(), 1u);
base::Value::Dict* handler_raw = (*handlers_raw)[0].GetIfDict();
result = handler_raw->FindString(keys::kActionDefaultTitle);
ASSERT_TRUE(result);
EXPECT_EQ("file handler title", *result);
EXPECT_TRUE(error.empty());
}
TEST(ExtensionL10nUtil, LocalizeManifestWithNameDescriptionCommandDescription) {
base::Value::Dict manifest;
manifest.Set(keys::kName, "__MSG_name__");
manifest.Set(keys::kDescription, "__MSG_description__");
base::Value::Dict commands;
base::Value::Dict first_command;
first_command.Set(keys::kDescription, "__MSG_first_command_description__");
commands.Set("first_command", std::move(first_command));
base::Value::Dict second_command;
second_command.Set(keys::kDescription, "__MSG_second_command_description__");
commands.Set("second_command", std::move(second_command));
manifest.Set(keys::kCommands, std::move(commands));
std::string error;
std::unique_ptr<MessageBundle> messages(CreateManifestBundle());
EXPECT_TRUE(
extension_l10n_util::LocalizeManifest(*messages, &manifest, &error));
const std::string* result = manifest.FindString(keys::kName);
ASSERT_TRUE(result);
EXPECT_EQ("name", *result);
result = manifest.FindString(keys::kDescription);
ASSERT_TRUE(result);
EXPECT_EQ("description", *result);
result =
manifest.FindStringByDottedPath("commands.first_command.description");
ASSERT_TRUE(result);
EXPECT_EQ("first command", *result);
result =
manifest.FindStringByDottedPath("commands.second_command.description");
ASSERT_TRUE(result);
EXPECT_EQ("second command", *result);
EXPECT_TRUE(error.empty());
}
TEST(ExtensionL10nUtil, LocalizeManifestWithShortName) {
base::Value::Dict manifest;
manifest.Set(keys::kName, "extension name");
manifest.Set(keys::kShortName, "__MSG_short_name__");
std::string error;
std::unique_ptr<MessageBundle> messages(CreateManifestBundle());
EXPECT_TRUE(
extension_l10n_util::LocalizeManifest(*messages, &manifest, &error));
EXPECT_TRUE(error.empty());
const std::string* result = manifest.FindString(keys::kShortName);
ASSERT_TRUE(result);
EXPECT_EQ("short_name", *result);
}
TEST(ExtensionL10nUtil, LocalizeManifestWithBadShortName) {
base::Value::Dict manifest;
manifest.Set(keys::kName, "extension name");
manifest.Set(keys::kShortName, "__MSG_short_name_bad__");
std::string error;
std::unique_ptr<MessageBundle> messages(CreateManifestBundle());
EXPECT_FALSE(
extension_l10n_util::LocalizeManifest(*messages, &manifest, &error));
EXPECT_FALSE(error.empty());
const std::string* result = manifest.FindString(keys::kShortName);
ASSERT_TRUE(result);
EXPECT_EQ("__MSG_short_name_bad__", *result);
}
TEST(ExtensionL10nUtil, LocalizeManifestWithSearchProviderMsgs) {
base::Value::Dict manifest;
manifest.Set(keys::kName, "__MSG_name__");
manifest.Set(keys::kDescription, "__MSG_description__");
base::Value::Dict search_provider;
search_provider.Set("name", "__MSG_country__");
search_provider.Set("keyword", "__MSG_omnibox_keyword__");
search_provider.Set("search_url", "http://www.foo.__MSG_country__");
search_provider.Set("favicon_url", "http://www.foo.__MSG_country__");
search_provider.Set("suggest_url", "http://www.foo.__MSG_country__");
manifest.SetByDottedPath(keys::kOverrideSearchProvider,
std::move(search_provider));
manifest.SetByDottedPath(keys::kOverrideHomepage,
"http://www.foo.__MSG_country__");
base::Value::List startup_pages;
startup_pages.Append("http://www.foo.__MSG_country__");
manifest.SetByDottedPath(keys::kOverrideStartupPage,
std::move(startup_pages));
std::string error;
std::unique_ptr<MessageBundle> messages(CreateManifestBundle());
EXPECT_TRUE(
extension_l10n_util::LocalizeManifest(*messages, &manifest, &error));
const std::string* result = manifest.FindString(keys::kName);
ASSERT_TRUE(result);
EXPECT_EQ("name", *result);
result = manifest.FindString(keys::kDescription);
ASSERT_TRUE(result);
EXPECT_EQ("description", *result);
std::string key_prefix(keys::kOverrideSearchProvider);
key_prefix += '.';
result = manifest.FindStringByDottedPath(key_prefix + "name");
ASSERT_TRUE(result);
EXPECT_EQ("de", *result);
result = manifest.FindStringByDottedPath(key_prefix + "keyword");
ASSERT_TRUE(result);
EXPECT_EQ("omnibox keyword", *result);
result = manifest.FindStringByDottedPath(key_prefix + "search_url");
ASSERT_TRUE(result);
EXPECT_EQ("http://www.foo.de", *result);
result = manifest.FindStringByDottedPath(key_prefix + "favicon_url");
ASSERT_TRUE(result);
EXPECT_EQ("http://www.foo.de", *result);
result = manifest.FindStringByDottedPath(key_prefix + "suggest_url");
ASSERT_TRUE(result);
EXPECT_EQ("http://www.foo.de", *result);
result = manifest.FindStringByDottedPath(keys::kOverrideHomepage);
ASSERT_TRUE(result);
EXPECT_EQ("http://www.foo.de", *result);
base::Value::List* startup_pages_raw =
manifest.FindListByDottedPath(keys::kOverrideStartupPage);
ASSERT_TRUE(startup_pages_raw);
ASSERT_FALSE(startup_pages_raw->empty());
ASSERT_TRUE((*startup_pages_raw)[0].is_string());
EXPECT_EQ("http://www.foo.de", (*startup_pages_raw)[0].GetString());
EXPECT_TRUE(error.empty());
}
// Tests that we don't relocalize with default and current locales missing.
TEST(ExtensionL10nUtil, ShouldRelocalizeManifestEmptyManifest) {
base::Value::Dict manifest;
EXPECT_FALSE(extension_l10n_util::ShouldRelocalizeManifest(manifest));
}
// Tests that we relocalize without a current locale.
TEST(ExtensionL10nUtil, ShouldRelocalizeManifestWithDefaultLocale) {
base::Value::Dict manifest;
manifest.Set(keys::kDefaultLocale, "en_US");
EXPECT_TRUE(extension_l10n_util::ShouldRelocalizeManifest(manifest));
}
// Tests that we don't relocalize without a default locale.
TEST(ExtensionL10nUtil, ShouldRelocalizeManifestWithCurrentLocale) {
extension_l10n_util::ScopedLocaleForTest scoped_locale("en-US");
base::Value::Dict manifest;
manifest.Set(keys::kCurrentLocale, "en_US");
EXPECT_FALSE(extension_l10n_util::ShouldRelocalizeManifest(manifest));
}
// Tests that we don't relocalize with same current_locale as system locale.
TEST(ExtensionL10nUtil, ShouldRelocalizeManifestSameCurrentLocale) {
extension_l10n_util::ScopedLocaleForTest scoped_locale("en-US");
base::Value::Dict manifest;
manifest.Set(keys::kDefaultLocale, "en_US");
manifest.Set(keys::kCurrentLocale, "en_US");
EXPECT_FALSE(extension_l10n_util::ShouldRelocalizeManifest(manifest));
}
// Tests that we relocalize with a different current_locale.
TEST(ExtensionL10nUtil, ShouldRelocalizeManifestDifferentCurrentLocale) {
extension_l10n_util::ScopedLocaleForTest scoped_locale("en-US");
base::Value::Dict manifest;
manifest.Set(keys::kDefaultLocale, "en_US");
manifest.Set(keys::kCurrentLocale, "sr");
EXPECT_TRUE(extension_l10n_util::ShouldRelocalizeManifest(manifest));
}
// Tests that we don't relocalize with the same current_locale as preferred
// locale.
TEST(ExtensionL10nUtil, ShouldRelocalizeManifestSameCurrentLocaleAsPreferred) {
extension_l10n_util::ScopedLocaleForTest scoped_locale("en-GB", "en-CA");
base::Value::Dict manifest;
manifest.Set(keys::kDefaultLocale, "en_US");
manifest.Set(keys::kCurrentLocale, "en_CA");
// Preferred and current locale are both en_CA.
EXPECT_FALSE(extension_l10n_util::ShouldRelocalizeManifest(manifest));
}
// Tests that we relocalize with a different current_locale from the preferred
// locale.
TEST(ExtensionL10nUtil,
ShouldRelocalizeManifestDifferentCurrentLocaleThanPreferred) {
extension_l10n_util::ScopedLocaleForTest scoped_locale("en-GB", "en-CA");
base::Value::Dict manifest;
manifest.Set(keys::kDefaultLocale, "en_US");
manifest.Set(keys::kCurrentLocale, "en_GB");
// Requires relocalization as the preferred (en_CA) differs from current
// (en_GB).
EXPECT_TRUE(extension_l10n_util::ShouldRelocalizeManifest(manifest));
}
TEST(ExtensionL10nUtil, GetAllFallbackLocales) {
extension_l10n_util::ScopedLocaleForTest scoped_locale("en-US");
std::vector<std::string> fallback_locales;
extension_l10n_util::GetAllFallbackLocales("all", &fallback_locales);
ASSERT_EQ(3U, fallback_locales.size());
EXPECT_EQ("en_US", fallback_locales[0]);
EXPECT_EQ("en", fallback_locales[1]);
EXPECT_EQ("all", fallback_locales[2]);
}
TEST(ExtensionL10nUtil, GetAllFallbackLocalesWithPreferredLocale) {
extension_l10n_util::ScopedLocaleForTest scoped_locale("en-GB", "en-CA");
std::vector<std::string> fallback_locales;
extension_l10n_util::GetAllFallbackLocales("all", &fallback_locales);
ASSERT_EQ(4U, fallback_locales.size());
EXPECT_EQ("en_CA", fallback_locales[0]);
EXPECT_EQ("en_GB", fallback_locales[1]);
EXPECT_EQ("en", fallback_locales[2]);
EXPECT_EQ("all", fallback_locales[3]);
}
} // namespace
} // namespace extensions
|