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 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
|
// 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 "chrome/browser/ui/toolbar/toolbar_actions_model.h"
#include <stddef.h>
#include <array>
#include <memory>
#include <string>
#include "base/containers/contains.h"
#include "base/files/file_util.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/json/json_reader.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/values.h"
#include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
#include "chrome/browser/extensions/extension_action_test_util.h"
#include "chrome/browser/extensions/extension_service_user_test_base.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/browser/extensions/unpacked_installer.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/toolbar/test_toolbar_action_view_controller.h"
#include "chrome/common/pref_names.h"
#include "components/crx_file/id_util.h"
#include "components/policy/core/common/policy_map.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/web_contents_tester.h"
#include "extensions/browser/extension_action_manager.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registrar.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/pref_names.h"
#include "extensions/browser/test_extension_registry_observer.h"
#include "extensions/browser/uninstall_reason.h"
#include "extensions/common/api/extension_action/action_info.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/manifest.h"
#include "extensions/test/test_extension_dir.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace {
using extensions::mojom::ManifestLocation;
// A simple observer that tracks the number of times certain events occur.
class ToolbarActionsModelTestObserver : public ToolbarActionsModel::Observer {
public:
explicit ToolbarActionsModelTestObserver(ToolbarActionsModel* model);
ToolbarActionsModelTestObserver(const ToolbarActionsModelTestObserver&) =
delete;
ToolbarActionsModelTestObserver& operator=(
const ToolbarActionsModelTestObserver&) = delete;
~ToolbarActionsModelTestObserver() override;
size_t inserted_count() const { return inserted_count_; }
size_t removed_count() const { return removed_count_; }
size_t initialized_count() const { return initialized_count_; }
const std::vector<ToolbarActionsModel::ActionId>& last_pinned_action_ids()
const {
return last_pinned_action_ids_;
}
private:
// ToolbarActionsModel::Observer:
void OnToolbarActionAdded(
const ToolbarActionsModel::ActionId& action_id) override {
++inserted_count_;
}
void OnToolbarActionRemoved(
const ToolbarActionsModel::ActionId& id) override {
++removed_count_;
}
void OnToolbarActionUpdated(
const ToolbarActionsModel::ActionId& id) override {}
void OnToolbarModelInitialized() override { ++initialized_count_; }
void OnToolbarPinnedActionsChanged() override {
last_pinned_action_ids_ = model_->pinned_action_ids();
}
const raw_ptr<ToolbarActionsModel> model_;
size_t inserted_count_ = 0;
size_t removed_count_ = 0;
size_t initialized_count_ = 0;
std::vector<ToolbarActionsModel::ActionId> last_pinned_action_ids_;
};
ToolbarActionsModelTestObserver::ToolbarActionsModelTestObserver(
ToolbarActionsModel* model)
: model_(model) {
model_->AddObserver(this);
}
ToolbarActionsModelTestObserver::~ToolbarActionsModelTestObserver() {
model_->RemoveObserver(this);
}
} // namespace
class ToolbarActionsModelUnitTest
: public extensions::ExtensionServiceUserTestBase {
public:
ToolbarActionsModelUnitTest() = default;
ToolbarActionsModelUnitTest(const ToolbarActionsModelUnitTest&) = delete;
ToolbarActionsModelUnitTest& operator=(const ToolbarActionsModelUnitTest&) =
delete;
~ToolbarActionsModelUnitTest() override = default;
protected:
// Initialize the ExtensionService, ToolbarActionsModel, and ExtensionSystem.
void Init();
void InitToolbarModelAndObserver();
void TearDown() override;
// Adds or removes the given |extension| and verify success.
[[nodiscard]] testing::AssertionResult AddExtension(
const scoped_refptr<const extensions::Extension>& extension);
[[nodiscard]] testing::AssertionResult RemoveExtension(
const scoped_refptr<const extensions::Extension>& extension);
// Adds three extensions, all with browser actions.
[[nodiscard]] testing::AssertionResult AddBrowserActionExtensions();
// Adds three extensions, one each for browser action, page action, and no
// action, and are added in that order.
[[nodiscard]] testing::AssertionResult AddActionExtensions();
// Returns true if the |toobar_model_| has an action with the given |id|.
bool ModelHasActionForId(const std::string& id) const;
// Test that certain histograms are emitted for user and non-user profiles
// (for ChromeOS Ash we look at user accounts vs profiles).
void RunEmitUserHistogramsTest(int incremented_histogram_count);
ToolbarActionsModel* toolbar_model() { return toolbar_model_; }
const ToolbarActionsModelTestObserver* observer() const {
return model_observer_.get();
}
size_t num_actions() const { return toolbar_model_->action_ids().size(); }
const extensions::Extension* browser_action_a() const {
return browser_action_a_.get();
}
const extensions::Extension* browser_action_b() const {
return browser_action_b_.get();
}
const extensions::Extension* browser_action_c() const {
return browser_action_c_.get();
}
const extensions::Extension* browser_action() const {
return browser_action_extension_.get();
}
const extensions::Extension* page_action() const {
return page_action_extension_.get();
}
const extensions::Extension* no_action() const {
return no_action_extension_.get();
}
private:
// Verifies that all extensions in |extensions| are added successfully.
testing::AssertionResult AddAndVerifyExtensions(
const extensions::ExtensionList& extensions);
// The toolbar model associated with the testing profile.
raw_ptr<ToolbarActionsModel> toolbar_model_;
// The test observer to track events. Must come after toolbar_model_ so that
// it is destroyed and removes itself as an observer first.
std::unique_ptr<ToolbarActionsModelTestObserver> model_observer_;
// Sample extensions with only browser actions.
scoped_refptr<const extensions::Extension> browser_action_a_;
scoped_refptr<const extensions::Extension> browser_action_b_;
scoped_refptr<const extensions::Extension> browser_action_c_;
// Sample extensions with different kinds of actions.
scoped_refptr<const extensions::Extension> browser_action_extension_;
scoped_refptr<const extensions::Extension> page_action_extension_;
scoped_refptr<const extensions::Extension> no_action_extension_;
};
void ToolbarActionsModelUnitTest::Init() {
InitializeEmptyExtensionService();
InitToolbarModelAndObserver();
}
void ToolbarActionsModelUnitTest::InitToolbarModelAndObserver() {
toolbar_model_ =
extensions::extension_action_test_util::CreateToolbarModelForProfile(
profile());
model_observer_ =
std::make_unique<ToolbarActionsModelTestObserver>(toolbar_model_);
}
void ToolbarActionsModelUnitTest::TearDown() {
model_observer_.reset();
extensions::ExtensionServiceUserTestBase::TearDown();
}
void ToolbarActionsModelUnitTest::RunEmitUserHistogramsTest(
int incremented_histogram_count) {
base::HistogramTester histograms;
InitToolbarModelAndObserver();
histograms.ExpectTotalCount("ExtensionToolbarModel.BrowserActionsCount", 1);
histograms.ExpectTotalCount("Extension.Toolbar.BrowserActionsCount2",
incremented_histogram_count);
}
testing::AssertionResult ToolbarActionsModelUnitTest::AddExtension(
const scoped_refptr<const extensions::Extension>& extension) {
if (registry()->enabled_extensions().GetByID(extension->id())) {
return testing::AssertionFailure()
<< "Extension " << extension->name() << " already installed!";
}
registrar()->AddExtension(extension.get());
if (!registry()->enabled_extensions().GetByID(extension->id())) {
return testing::AssertionFailure()
<< "Failed to install extension: " << extension->name();
}
return testing::AssertionSuccess();
}
testing::AssertionResult ToolbarActionsModelUnitTest::RemoveExtension(
const scoped_refptr<const extensions::Extension>& extension) {
if (!registry()->enabled_extensions().GetByID(extension->id())) {
return testing::AssertionFailure()
<< "Extension " << extension->name() << " not installed!";
}
registrar()->RemoveExtension(extension->id(),
extensions::UnloadedExtensionReason::DISABLE);
if (registry()->enabled_extensions().GetByID(extension->id())) {
return testing::AssertionFailure()
<< "Failed to unload extension: " << extension->name();
}
return testing::AssertionSuccess();
}
testing::AssertionResult ToolbarActionsModelUnitTest::AddActionExtensions() {
browser_action_extension_ =
extensions::ExtensionBuilder("browser_action")
.SetAction(extensions::ActionInfo::Type::kBrowser)
.SetLocation(ManifestLocation::kInternal)
.Build();
page_action_extension_ = extensions::ExtensionBuilder("page_action")
.SetAction(extensions::ActionInfo::Type::kPage)
.SetLocation(ManifestLocation::kInternal)
.Build();
no_action_extension_ = extensions::ExtensionBuilder("no_action")
.SetLocation(ManifestLocation::kInternal)
.Build();
extensions::ExtensionList extensions;
extensions.push_back(browser_action_extension_);
extensions.push_back(page_action_extension_);
extensions.push_back(no_action_extension_);
return AddAndVerifyExtensions(extensions);
}
testing::AssertionResult
ToolbarActionsModelUnitTest::AddBrowserActionExtensions() {
browser_action_a_ = extensions::ExtensionBuilder("browser_actionA")
.SetAction(extensions::ActionInfo::Type::kBrowser)
.SetLocation(ManifestLocation::kInternal)
.Build();
browser_action_b_ = extensions::ExtensionBuilder("browser_actionB")
.SetAction(extensions::ActionInfo::Type::kBrowser)
.SetLocation(ManifestLocation::kInternal)
.Build();
browser_action_c_ = extensions::ExtensionBuilder("browser_actionC")
.SetAction(extensions::ActionInfo::Type::kBrowser)
.SetLocation(ManifestLocation::kInternal)
.Build();
extensions::ExtensionList extensions;
extensions.push_back(browser_action_a_);
extensions.push_back(browser_action_b_);
extensions.push_back(browser_action_c_);
return AddAndVerifyExtensions(extensions);
}
bool ToolbarActionsModelUnitTest::ModelHasActionForId(
const std::string& id) const {
for (const auto& toolbar_action_id : toolbar_model_->action_ids()) {
if (toolbar_action_id == id) {
return true;
}
}
return false;
}
testing::AssertionResult ToolbarActionsModelUnitTest::AddAndVerifyExtensions(
const extensions::ExtensionList& extensions) {
for (const auto& extension : extensions) {
if (!AddExtension(extension)) {
return testing::AssertionFailure()
<< "Failed to install extension: " << extension->name();
}
}
return testing::AssertionSuccess();
}
// A basic test for extensions with browser actions showing up in the toolbar.
TEST_F(ToolbarActionsModelUnitTest, BasicToolbarActionsModelTest) {
Init();
// Starts empty.
EXPECT_EQ(0u, observer()->inserted_count());
EXPECT_THAT(toolbar_model()->action_ids(), ::testing::IsEmpty());
EXPECT_THAT(toolbar_model()->pinned_action_ids(), ::testing::IsEmpty());
// Load an extension with a browser action.
scoped_refptr<const extensions::Extension> extension =
extensions::ExtensionBuilder("browser_action")
.SetAction(extensions::ActionInfo::Type::kBrowser)
.SetLocation(ManifestLocation::kInternal)
.Build();
ASSERT_TRUE(AddExtension(extension));
// We should now find our extension in the model.
EXPECT_EQ(1u, observer()->inserted_count());
EXPECT_THAT(toolbar_model()->action_ids(),
::testing::UnorderedElementsAre(extension->id()));
// It should be unpinned.
EXPECT_THAT(toolbar_model()->pinned_action_ids(), ::testing::IsEmpty());
// Remove the extension and verify it is removed in the model.
ASSERT_TRUE(RemoveExtension(extension));
EXPECT_EQ(1u, observer()->removed_count());
EXPECT_THAT(toolbar_model()->action_ids(), ::testing::IsEmpty());
EXPECT_THAT(toolbar_model()->pinned_action_ids(), ::testing::IsEmpty());
}
// Test that new extension actions are always visible on installation and
// inserted at the "end" of the visible section.
TEST_F(ToolbarActionsModelUnitTest, NewToolbarExtensionsAreUnpinned) {
Init();
// Three extensions with actions.
scoped_refptr<const extensions::Extension> extension_a =
extensions::ExtensionBuilder("a")
.SetAction(extensions::ActionInfo::Type::kBrowser)
.SetLocation(ManifestLocation::kInternal)
.Build();
scoped_refptr<const extensions::Extension> extension_b =
extensions::ExtensionBuilder("b")
.SetAction(extensions::ActionInfo::Type::kBrowser)
.SetLocation(ManifestLocation::kInternal)
.Build();
scoped_refptr<const extensions::Extension> extension_c =
extensions::ExtensionBuilder("c")
.SetAction(extensions::ActionInfo::Type::kBrowser)
.SetLocation(ManifestLocation::kInternal)
.Build();
// We should start off without any actions.
EXPECT_EQ(0u, num_actions());
// Add one action. It should be unpinned.
EXPECT_TRUE(AddExtension(extension_a.get()));
EXPECT_EQ(1u, num_actions());
EXPECT_THAT(toolbar_model()->pinned_action_ids(), ::testing::IsEmpty());
// Add a second. It should also be unpinned (even with existing extensions,
// default state is unpinned).
EXPECT_TRUE(AddExtension(extension_b.get()));
EXPECT_EQ(2u, num_actions());
EXPECT_THAT(toolbar_model()->pinned_action_ids(), ::testing::IsEmpty());
// Pin the second. It should now be the only pinned icon.
toolbar_model()->SetActionVisibility(extension_b->id(), true);
EXPECT_EQ(2u, num_actions());
EXPECT_THAT(toolbar_model()->pinned_action_ids(),
::testing::ElementsAre(extension_b->id()));
// Add a third extension. It should be unpinned (pin state should not carry
// to new extensions).
EXPECT_TRUE(AddExtension(extension_c.get()));
EXPECT_EQ(3u, num_actions());
EXPECT_THAT(toolbar_model()->pinned_action_ids(),
::testing::ElementsAre(extension_b->id()));
}
// Test that the model contains all types of extensions, except those which
// should not be displayed on the toolbar (like component extensions).
TEST_F(ToolbarActionsModelUnitTest, TestToolbarExtensionTypesEnabledSwitch) {
Init();
ASSERT_TRUE(AddActionExtensions());
// extensions with page actions and no action should also be displayed in the
// toolbar.
EXPECT_THAT(
toolbar_model()->action_ids(),
testing::UnorderedElementsAre(browser_action()->id(), page_action()->id(),
no_action()->id()));
// Extensions that are installed by default shouldn't be given an icon.
auto default_installed_manifest =
base::Value::Dict()
.Set("name", "default installed")
.Set("description", "A default installed extension")
.Set("manifest_version", 2)
.Set("version", "1.0.0.0");
scoped_refptr<const extensions::Extension> default_installed_extension =
extensions::ExtensionBuilder()
.SetManifest(std::move(default_installed_manifest))
.SetID(crx_file::id_util::GenerateId("default"))
.SetLocation(ManifestLocation::kInternal)
.AddFlags(extensions::Extension::WAS_INSTALLED_BY_DEFAULT)
.Build();
EXPECT_TRUE(AddExtension(default_installed_extension.get()));
EXPECT_EQ(3u, num_actions());
EXPECT_FALSE(ModelHasActionForId(default_installed_extension->id()));
// Component extensions shouldn't be given an icon.
scoped_refptr<const extensions::Extension> component_extension_no_action =
extensions::ExtensionBuilder("component ext no action")
.SetLocation(ManifestLocation::kComponent)
.Build();
EXPECT_TRUE(AddExtension(component_extension_no_action.get()));
EXPECT_EQ(3u, num_actions());
EXPECT_FALSE(ModelHasActionForId(component_extension_no_action->id()));
// Sanity check: A new extension that's installed from the webstore should
// have an icon.
scoped_refptr<const extensions::Extension> internal_extension_no_action =
extensions::ExtensionBuilder("internal ext no action")
.SetLocation(ManifestLocation::kInternal)
.Build();
EXPECT_TRUE(AddExtension(internal_extension_no_action.get()));
EXPECT_EQ(4u, num_actions());
EXPECT_TRUE(ModelHasActionForId(internal_extension_no_action->id()));
}
TEST_F(ToolbarActionsModelUnitTest, PinnedStateIsTransferredToIncognito) {
Init();
ASSERT_TRUE(AddBrowserActionExtensions());
// Give two extensions incognito access.
// Note: We use ExtensionPrefs::SetIsIncognitoEnabled instead of
// util::SetIsIncognitoEnabled because the latter tries to reload the
// extension, which requries a filepath associated with the extension (and,
// for this test, reloading the extension is irrelevant to us).
extensions::ExtensionPrefs* extension_prefs =
extensions::ExtensionPrefs::Get(profile());
extension_prefs->SetIsIncognitoEnabled(browser_action_b()->id(), true);
extension_prefs->SetIsIncognitoEnabled(browser_action_c()->id(), true);
// Pin extensions A and C. State is A, C, [B].
toolbar_model()->SetActionVisibility(browser_action_a()->id(), true);
toolbar_model()->SetActionVisibility(browser_action_c()->id(), true);
EXPECT_THAT(toolbar_model()->pinned_action_ids(),
::testing::ElementsAre(browser_action_a()->id(),
browser_action_c()->id()));
// Get an incognito profile and toolbar.
ToolbarActionsModel* incognito_model =
extensions::extension_action_test_util::CreateToolbarModelForProfile(
profile()->GetPrimaryOTRProfile(/*create_if_needed=*/true));
// We should have two actions in the incognito bar, C and B. The pinned state
// should be preserved, so C should be pinned.
EXPECT_THAT(incognito_model->action_ids(),
::testing::ElementsAre(browser_action_b()->id(),
browser_action_c()->id()));
EXPECT_THAT(incognito_model->pinned_action_ids(),
::testing::ElementsAre(browser_action_c()->id()));
// Pinning from the original profile transfers to the incognito profile, so
// pinning B results in a change.
toolbar_model()->SetActionVisibility(browser_action_b()->id(), true);
EXPECT_THAT(incognito_model->pinned_action_ids(),
::testing::ElementsAre(browser_action_c()->id(),
browser_action_b()->id()));
// Similarly, unpinning C transfers to the incognito profile.
toolbar_model()->SetActionVisibility(browser_action_c()->id(), false);
EXPECT_THAT(incognito_model->pinned_action_ids(),
::testing::ElementsAre(browser_action_b()->id()));
}
TEST_F(ToolbarActionsModelUnitTest,
MovingPinnedActionsTransfersBetweenIncognito) {
Init();
ASSERT_TRUE(AddBrowserActionExtensions());
// Give all extensions incognito access.
// Note: We use ExtensionPrefs::SetIsIncognitoEnabled instead of
// util::SetIsIncognitoEnabled because the latter tries to reload the
// extension, which requires a filepath associated with the extension (and,
// for this test, reloading the extension is irrelevant to us).
extensions::ExtensionPrefs* extension_prefs =
extensions::ExtensionPrefs::Get(profile());
extension_prefs->SetIsIncognitoEnabled(browser_action_a()->id(), true);
extension_prefs->SetIsIncognitoEnabled(browser_action_b()->id(), true);
extension_prefs->SetIsIncognitoEnabled(browser_action_c()->id(), true);
// Pin all extensions, to allow moving them around.
toolbar_model()->SetActionVisibility(browser_action_a()->id(), true);
toolbar_model()->SetActionVisibility(browser_action_b()->id(), true);
toolbar_model()->SetActionVisibility(browser_action_c()->id(), true);
// Get an incognito profile and toolbar.
ToolbarActionsModel* incognito_model =
extensions::extension_action_test_util::CreateToolbarModelForProfile(
profile()->GetPrimaryOTRProfile(/*create_if_needed=*/true));
// The incognito pinned actions should be A, B, C (matching the order from
// the on-the-record profile).
EXPECT_THAT(
incognito_model->pinned_action_ids(),
::testing::ElementsAre(browser_action_a()->id(), browser_action_b()->id(),
browser_action_c()->id()));
// Moving extension C to index 0 affects both profiles.
toolbar_model()->MovePinnedAction(browser_action_c()->id(), 0);
EXPECT_THAT(
toolbar_model()->pinned_action_ids(),
::testing::ElementsAre(browser_action_c()->id(), browser_action_a()->id(),
browser_action_b()->id()));
EXPECT_THAT(
incognito_model->pinned_action_ids(),
::testing::ElementsAre(browser_action_c()->id(), browser_action_a()->id(),
browser_action_b()->id()));
}
// Test that enabling extensions incognito with an active incognito profile
// works.
TEST_F(ToolbarActionsModelUnitTest, ActionsToolbarIncognitoEnableExtension) {
Init();
static constexpr char kManifest[] =
"{"
" \"name\": \"%s\","
" \"version\": \"1.0\","
" \"manifest_version\": 2,"
" \"browser_action\": {}"
"}";
// For this test, we need to have "real" extension files, because we need to
// be able to reload them during the incognito process. Since the toolbar
// needs to be notified of the reload, we need it this time (as opposed to
// above, where we simply set the prefs before the incognito bar was
// created.
extensions::TestExtensionDir dir1;
dir1.WriteManifest(base::StringPrintf(kManifest, "incognito1"));
extensions::TestExtensionDir dir2;
dir2.WriteManifest(base::StringPrintf(kManifest, "incognito2"));
auto dirs = std::to_array<extensions::TestExtensionDir*>({&dir1, &dir2});
auto extensions =
std::to_array<const extensions::Extension*>({nullptr, nullptr});
for (size_t i = 0; i < std::size(dirs); ++i) {
// The extension id will be calculated from the file path; we need this to
// wait for the extension to load.
base::FilePath path_for_id =
base::MakeAbsoluteFilePath(dirs[i]->UnpackedPath());
std::string id = crx_file::id_util::GenerateIdForPath(path_for_id);
extensions::TestExtensionRegistryObserver observer(registry(), id);
extensions::UnpackedInstaller::Create(profile())->Load(
dirs[i]->UnpackedPath());
observer.WaitForExtensionLoaded();
extensions[i] = registry()->enabled_extensions().GetByID(id);
ASSERT_TRUE(extensions[i]);
}
// For readability, alias to A and B. Since we'll be reloading these
// extensions, we also can't rely on pointers.
std::string extension_a = extensions[0]->id();
std::string extension_b = extensions[1]->id();
// Pin Extension A in the on-the-record profile.
toolbar_model()->SetActionVisibility(extension_a, true);
EXPECT_THAT(toolbar_model()->pinned_action_ids(),
::testing::ElementsAre(extension_a));
// Get an incognito profile and toolbar.
ToolbarActionsModel* incognito_model =
extensions::extension_action_test_util::CreateToolbarModelForProfile(
profile()->GetPrimaryOTRProfile(/*create_if_needed=*/true));
ToolbarActionsModelTestObserver incognito_observer(incognito_model);
// Right now, no extensions are enabled in incognito mode.
EXPECT_THAT(incognito_model->action_ids(), ::testing::IsEmpty());
// Set extension B (which is unpinned) to be enabled in incognito. This
// results in b reloading, so wait for it.
{
extensions::TestExtensionRegistryObserver observer(registry(), extension_b);
extensions::util::SetIsIncognitoEnabled(extension_b, profile(), true);
observer.WaitForExtensionLoaded();
}
// Now, we should have one icon in the incognito bar. But, since B is
// unpinned in the main bar, it shouldn't be visible.
EXPECT_THAT(incognito_model->action_ids(),
::testing::ElementsAre(extension_b));
EXPECT_THAT(incognito_model->pinned_action_ids(), ::testing::IsEmpty());
// Also enable extension A for incognito (again, wait for the reload).
{
extensions::TestExtensionRegistryObserver observer(registry(), extension_a);
extensions::util::SetIsIncognitoEnabled(extension_a, profile(), true);
observer.WaitForExtensionLoaded();
}
// Now, both extensions should be enabled in incognito mode. Extension A
// should be pinned (since it's pinned in the main bar).
EXPECT_THAT(incognito_model->action_ids(),
::testing::UnorderedElementsAre(extension_a, extension_b));
EXPECT_THAT(incognito_model->pinned_action_ids(),
::testing::ElementsAre(extension_a));
}
// Test that observers receive no Added notifications until after the
// ExtensionSystem has initialized.
TEST_F(ToolbarActionsModelUnitTest, ModelWaitsForExtensionSystemReady) {
InitializeEmptyExtensionService();
ToolbarActionsModel* toolbar_model = extensions::extension_action_test_util::
CreateToolbarModelForProfileWithoutWaitingForReady(profile());
ToolbarActionsModelTestObserver model_observer(toolbar_model);
EXPECT_TRUE(AddBrowserActionExtensions());
// Since the model hasn't been initialized (the ExtensionSystem::ready task
// hasn't been run), there should be no insertion notifications.
EXPECT_EQ(0u, model_observer.inserted_count());
EXPECT_EQ(0u, model_observer.initialized_count());
EXPECT_FALSE(toolbar_model->actions_initialized());
// Run the ready task.
static_cast<extensions::TestExtensionSystem*>(
extensions::ExtensionSystem::Get(profile()))
->SetReady();
// Run tasks posted to TestExtensionSystem.
base::RunLoop().RunUntilIdle();
// We should still have no insertions, but should have an initialized count.
EXPECT_TRUE(toolbar_model->actions_initialized());
EXPECT_EQ(0u, model_observer.inserted_count());
EXPECT_EQ(1u, model_observer.initialized_count());
}
// Test that user-script extensions show up on the toolbar.
TEST_F(ToolbarActionsModelUnitTest, AddUserScriptExtension) {
Init();
scoped_refptr<const extensions::Extension> extension =
extensions::ExtensionBuilder("a")
.SetLocation(ManifestLocation::kInternal)
.MergeManifest(
base::Value::Dict().Set("converted_from_user_script", true))
.Build();
// We should start off without any actions.
EXPECT_EQ(0u, num_actions());
// Add the extension and verify it gets an icon.
EXPECT_TRUE(AddExtension(extension.get()));
EXPECT_THAT(toolbar_model()->action_ids(),
::testing::UnorderedElementsAre(extension->id()));
}
TEST_F(ToolbarActionsModelUnitTest, IsActionPinnedCorrespondsToPinningState) {
Init();
ASSERT_TRUE(AddBrowserActionExtensions());
// The actions should initially not be pinned.
EXPECT_FALSE(toolbar_model()->IsActionPinned(browser_action_a()->id()));
// Pinning is reflected in |IsActionPinned|.
toolbar_model()->SetActionVisibility(browser_action_a()->id(), true);
EXPECT_TRUE(toolbar_model()->IsActionPinned(browser_action_a()->id()));
// Removing pinning should also be reflected in |IsActionPinned|.
toolbar_model()->SetActionVisibility(browser_action_a()->id(), false);
EXPECT_FALSE(toolbar_model()->IsActionPinned(browser_action_a()->id()));
}
TEST_F(ToolbarActionsModelUnitTest,
TogglingVisibilityAppendsToPinnedExtensions) {
Init();
ASSERT_TRUE(AddBrowserActionExtensions());
EXPECT_THAT(toolbar_model()->pinned_action_ids(), testing::IsEmpty());
toolbar_model()->SetActionVisibility(browser_action_a()->id(), true);
EXPECT_THAT(toolbar_model()->pinned_action_ids(),
testing::ElementsAre(browser_action_a()->id()));
// Pin the remaining two extensions.
toolbar_model()->SetActionVisibility(browser_action_b()->id(), true);
toolbar_model()->SetActionVisibility(browser_action_c()->id(), true);
// Verify that they are added in order.
EXPECT_THAT(
toolbar_model()->pinned_action_ids(),
testing::ElementsAre(browser_action_a()->id(), browser_action_b()->id(),
browser_action_c()->id()));
// Verify that unpinning an extension updates the list of pinned ids.
toolbar_model()->SetActionVisibility(browser_action_b()->id(), false);
EXPECT_THAT(
toolbar_model()->pinned_action_ids(),
testing::ElementsAre(browser_action_a()->id(), browser_action_c()->id()));
// Verify that re-pinning an extension adds it back to the end of the list.
toolbar_model()->SetActionVisibility(browser_action_b()->id(), true);
EXPECT_THAT(
toolbar_model()->pinned_action_ids(),
testing::ElementsAre(browser_action_a()->id(), browser_action_c()->id(),
browser_action_b()->id()));
}
TEST_F(ToolbarActionsModelUnitTest, ChangesToPinningNotifiesObserver) {
Init();
ASSERT_TRUE(AddBrowserActionExtensions());
// The observer should not think that any extensions are initially pinned.
EXPECT_THAT(observer()->last_pinned_action_ids(), testing::IsEmpty());
// Verify that pinning the action notifies the observer.
toolbar_model()->SetActionVisibility(browser_action_a()->id(), true);
EXPECT_THAT(observer()->last_pinned_action_ids(),
testing::ElementsAre(browser_action_a()->id()));
// Verify that un-pinning an action also notifies the observer.
toolbar_model()->SetActionVisibility(browser_action_a()->id(), false);
EXPECT_THAT(observer()->last_pinned_action_ids(), testing::IsEmpty());
}
TEST_F(ToolbarActionsModelUnitTest, ChangesToPinningSavedInExtensionPrefs) {
Init();
ASSERT_TRUE(AddBrowserActionExtensions());
extensions::ExtensionPrefs* const extension_prefs =
extensions::ExtensionPrefs::Get(profile());
// The preferences shouldn't have any extensions initially pinned.
EXPECT_THAT(extension_prefs->GetPinnedExtensions(), testing::IsEmpty());
// Verify that pinned extensions are reflected in preferences.
toolbar_model()->SetActionVisibility(browser_action_a()->id(), true);
toolbar_model()->SetActionVisibility(browser_action_b()->id(), true);
toolbar_model()->SetActionVisibility(browser_action_c()->id(), true);
EXPECT_THAT(
extension_prefs->GetPinnedExtensions(),
testing::ElementsAre(browser_action_a()->id(), browser_action_b()->id(),
browser_action_c()->id()));
// Verify that un-pinning an action is also reflected in preferences.
toolbar_model()->SetActionVisibility(browser_action_b()->id(), false);
EXPECT_THAT(
extension_prefs->GetPinnedExtensions(),
testing::ElementsAre(browser_action_a()->id(), browser_action_c()->id()));
// Verify that re-pinning is added last.
toolbar_model()->SetActionVisibility(browser_action_b()->id(), true);
EXPECT_THAT(
extension_prefs->GetPinnedExtensions(),
testing::ElementsAre(browser_action_a()->id(), browser_action_c()->id(),
browser_action_b()->id()));
}
TEST_F(ToolbarActionsModelUnitTest, ChangesToExtensionPrefsReflectedInModel) {
Init();
ASSERT_TRUE(AddBrowserActionExtensions());
extensions::ExtensionPrefs* const extension_prefs =
extensions::ExtensionPrefs::Get(profile());
// No actions should be initially pinned.
EXPECT_THAT(toolbar_model()->pinned_action_ids(), testing::IsEmpty());
// Update preferences to indicate that extensions A and C are pinned.
extensions::ExtensionIdList pinned_extension_list = {
browser_action_a()->id(), browser_action_c()->id()};
// Verify that setting the extension preferences updates the model.
extension_prefs->SetPinnedExtensions(pinned_extension_list);
EXPECT_EQ(pinned_extension_list, extension_prefs->GetPinnedExtensions());
EXPECT_EQ(pinned_extension_list, toolbar_model()->pinned_action_ids());
// Verify that the observer is notified as well.
EXPECT_EQ(pinned_extension_list, observer()->last_pinned_action_ids());
}
TEST_F(ToolbarActionsModelUnitTest,
MismatchInPinnedExtensionPreferencesNotReflectedInModel) {
Init();
ASSERT_TRUE(AddBrowserActionExtensions());
extensions::ExtensionPrefs* const extension_prefs =
extensions::ExtensionPrefs::Get(profile());
// No actions should be initially pinned.
EXPECT_THAT(toolbar_model()->pinned_action_ids(), testing::IsEmpty());
// Update preferences to indicate that extensions A and C are pinned.
extensions::ExtensionIdList pinned_extension_list = {
browser_action_a()->id(), browser_action_c()->id()};
extensions::ExtensionIdList pinned_extension_list_with_additional_id =
pinned_extension_list;
pinned_extension_list_with_additional_id.push_back("bogus id");
// Verify that setting the extension preferences updates the model and that
// the additional extension id is filtered out in the model.
extension_prefs->SetPinnedExtensions(
pinned_extension_list_with_additional_id);
EXPECT_EQ(pinned_extension_list_with_additional_id,
extension_prefs->GetPinnedExtensions());
EXPECT_EQ(pinned_extension_list, toolbar_model()->pinned_action_ids());
// Verify that the observer is notified as well.
EXPECT_EQ(pinned_extension_list, observer()->last_pinned_action_ids());
}
TEST_F(ToolbarActionsModelUnitTest, PinnedExtensionsFilteredOnInitialization) {
Init();
ASSERT_TRUE(AddBrowserActionExtensions());
extensions::ExtensionPrefs* const extension_prefs =
extensions::ExtensionPrefs::Get(profile());
// Update preferences to indicate that extensions A and a "bogus id" one is
// set.
extensions::ExtensionIdList pinned_extension_list_with_additional_id = {
browser_action_a()->id(), "bogus id"};
extension_prefs->SetPinnedExtensions(
pinned_extension_list_with_additional_id);
// Create a model after setting the prefs, this is done to ensure that the
// pinned preferences are loaded and correctly filtered.
ToolbarActionsModel model_created_after_prefs_set(profile(), extension_prefs);
// Wait for load to happen (::OnReady is posted from ToolbarActionModel's
// constructor).
base::RunLoop().RunUntilIdle();
EXPECT_EQ(pinned_extension_list_with_additional_id,
extension_prefs->GetPinnedExtensions());
// Verify that the new model loads the same action_ids() and
// pinned_action_ids() from ExtensionPrefs that |toolbar_model()| should have
// saved.
EXPECT_EQ(toolbar_model()->pinned_action_ids(),
model_created_after_prefs_set.pinned_action_ids());
EXPECT_EQ(toolbar_model()->action_ids(),
model_created_after_prefs_set.action_ids());
// Verify that the new model's pinned action IDs have been pruned down to only
// extension a.
EXPECT_THAT(model_created_after_prefs_set.pinned_action_ids(),
testing::ElementsAre(browser_action_a()->id()));
}
TEST_F(ToolbarActionsModelUnitTest, ChangesToPinnedOrderSavedInExtensionPrefs) {
Init();
ASSERT_TRUE(AddBrowserActionExtensions());
extensions::ExtensionPrefs* const extension_prefs =
extensions::ExtensionPrefs::Get(profile());
// The preferences shouldn't have any extensions initially pinned.
EXPECT_THAT(extension_prefs->GetPinnedExtensions(), testing::IsEmpty());
// Verify that pinned extensions are reflected in preferences.
toolbar_model()->SetActionVisibility(browser_action_a()->id(), true);
toolbar_model()->SetActionVisibility(browser_action_b()->id(), true);
toolbar_model()->SetActionVisibility(browser_action_c()->id(), true);
EXPECT_THAT(
extension_prefs->GetPinnedExtensions(),
testing::ElementsAre(browser_action_a()->id(), browser_action_b()->id(),
browser_action_c()->id()));
// Verify that moving an action left to right is reflected in preferences.
// Note: Use index 1 (instead of 2) because moving to the end of the list
// is handled differently.
toolbar_model()->MovePinnedAction(browser_action_a()->id(), 1);
EXPECT_THAT(
extension_prefs->GetPinnedExtensions(),
testing::ElementsAre(browser_action_b()->id(), browser_action_a()->id(),
browser_action_c()->id()));
// Verify that moving an action right to left is reflected in preferences.
toolbar_model()->MovePinnedAction(browser_action_a()->id(), 0);
EXPECT_THAT(
extension_prefs->GetPinnedExtensions(),
testing::ElementsAre(browser_action_a()->id(), browser_action_b()->id(),
browser_action_c()->id()));
// Verify that moving an action to index greater than rightmost index is
// reflected in preferences as at the right end.
toolbar_model()->MovePinnedAction(browser_action_b()->id(), 4);
EXPECT_THAT(
extension_prefs->GetPinnedExtensions(),
testing::ElementsAre(browser_action_a()->id(), browser_action_c()->id(),
browser_action_b()->id()));
// "Moving" an icon to its current position should be a no-op.
toolbar_model()->MovePinnedAction(browser_action_c()->id(), 1);
EXPECT_THAT(
extension_prefs->GetPinnedExtensions(),
testing::ElementsAre(browser_action_a()->id(), browser_action_c()->id(),
browser_action_b()->id()));
// Repeat the above tests, but add in extra IDs into prefs (representing
// extensions that could be installed, but not loaded). These unloaded
// extensions' states should be preserved.
constexpr char kExtraId1[] = "extra1";
constexpr char kExtraId2[] = "extra2";
extension_prefs->SetPinnedExtensions({browser_action_a()->id(), kExtraId1,
kExtraId2, browser_action_c()->id(),
browser_action_b()->id()});
EXPECT_THAT(
toolbar_model()->pinned_action_ids(),
testing::ElementsAre(browser_action_a()->id(), browser_action_c()->id(),
browser_action_b()->id()));
// Move right to left.
toolbar_model()->MovePinnedAction(browser_action_c()->id(), 0);
EXPECT_THAT(
extension_prefs->GetPinnedExtensions(),
testing::ElementsAre(browser_action_c()->id(), browser_action_a()->id(),
kExtraId1, kExtraId2, browser_action_b()->id()));
// Move left to right.
toolbar_model()->MovePinnedAction(browser_action_c()->id(), 1);
EXPECT_THAT(
extension_prefs->GetPinnedExtensions(),
testing::ElementsAre(browser_action_a()->id(), kExtraId1, kExtraId2,
browser_action_c()->id(), browser_action_b()->id()));
// Move past the right-most index (of the visible actions).
toolbar_model()->MovePinnedAction(browser_action_a()->id(), 4);
EXPECT_THAT(
extension_prefs->GetPinnedExtensions(),
testing::ElementsAre(kExtraId1, kExtraId2, browser_action_c()->id(),
browser_action_b()->id(), browser_action_a()->id()));
// "Move" to the current position.
toolbar_model()->MovePinnedAction(browser_action_b()->id(), 1);
EXPECT_THAT(
extension_prefs->GetPinnedExtensions(),
testing::ElementsAre(kExtraId1, kExtraId2, browser_action_c()->id(),
browser_action_b()->id(), browser_action_a()->id()));
}
TEST_F(ToolbarActionsModelUnitTest, PinStateErasedOnUninstallation) {
Init();
scoped_refptr<const extensions::Extension> extension =
extensions::ExtensionBuilder("extension")
.SetAction(extensions::ActionInfo::Type::kBrowser)
.SetLocation(ManifestLocation::kInternal)
.Build();
// Add and pin an extension.
EXPECT_TRUE(AddExtension(extension));
EXPECT_FALSE(toolbar_model()->IsActionPinned(extension->id()));
extensions::ExtensionPrefs* const prefs =
extensions::ExtensionPrefs::Get(profile());
EXPECT_THAT(prefs->GetPinnedExtensions(), testing::IsEmpty());
toolbar_model()->SetActionVisibility(extension->id(), true);
EXPECT_TRUE(toolbar_model()->IsActionPinned(extension->id()));
EXPECT_THAT(prefs->GetPinnedExtensions(),
testing::ElementsAre(extension->id()));
// Uninstall the extension. The pin state should be forgotten.
registrar()->UninstallExtension(
extension->id(), extensions::UNINSTALL_REASON_FOR_TESTING, nullptr);
EXPECT_FALSE(toolbar_model()->IsActionPinned(extension->id()));
EXPECT_THAT(prefs->GetPinnedExtensions(), testing::IsEmpty());
// Re-add the extension. It should be in the default (unpinned) state.
EXPECT_TRUE(AddExtension(extension));
EXPECT_FALSE(toolbar_model()->IsActionPinned(extension->id()));
EXPECT_THAT(prefs->GetPinnedExtensions(), testing::IsEmpty());
}
TEST_F(ToolbarActionsModelUnitTest, ForcePinnedByPolicy) {
Init();
// Set the extension to force-pin via enterprise policy.
std::string extension_id = crx_file::id_util::GenerateId("qwertyuiop");
std::string json = base::StringPrintf(
R"({
"%s": {
"toolbar_pin": "force_pinned"
}
})",
extension_id.c_str());
std::optional<base::Value> parsed = base::JSONReader::Read(json);
policy::PolicyMap map;
map.Set("ExtensionSettings", policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_PLATFORM,
std::move(parsed), nullptr);
policy_provider()->UpdateChromePolicy(map);
scoped_refptr<const extensions::Extension> extension =
extensions::ExtensionBuilder("test")
.SetAction(extensions::ActionInfo::Type::kBrowser)
.SetLocation(ManifestLocation::kInternal)
.SetID(extension_id)
.Build();
// Add an extension. It should auto-pin because of the ExtensionSettings
// policy.
EXPECT_TRUE(AddExtension(extension));
EXPECT_TRUE(toolbar_model()->IsActionPinned(extension->id()));
auto* prefs = extensions::ExtensionPrefs::Get(profile());
EXPECT_FALSE(base::Contains(prefs->GetPinnedExtensions(), extension_id));
// Pin all other extensions, to allow moving them around.
ASSERT_TRUE(AddBrowserActionExtensions());
const auto& id_a = browser_action_a()->id();
const auto& id_b = browser_action_b()->id();
const auto& id_c = browser_action_c()->id();
toolbar_model()->SetActionVisibility(id_a, true);
toolbar_model()->SetActionVisibility(id_b, true);
toolbar_model()->SetActionVisibility(id_c, true);
// Force-pinned extensions aren't saved in the pref.
EXPECT_THAT(prefs->GetPinnedExtensions(),
testing::ElementsAre(id_a, id_b, id_c));
EXPECT_THAT(toolbar_model()->pinned_action_ids(),
testing::ElementsAre(id_a, id_b, id_c, extension_id));
// Try to move the force-pinned extension. This shouldn't do anything because
// they can't be moved. See crbug.com/1266952.
toolbar_model()->MovePinnedAction(extension_id, 1);
EXPECT_THAT(prefs->GetPinnedExtensions(),
testing::ElementsAre(id_a, id_b, id_c));
EXPECT_THAT(toolbar_model()->pinned_action_ids(),
testing::ElementsAre(id_a, id_b, id_c, extension_id));
// Try to move other extensions. This should work fine.
toolbar_model()->MovePinnedAction(id_a, 1);
EXPECT_THAT(prefs->GetPinnedExtensions(),
testing::ElementsAre(id_b, id_a, id_c));
EXPECT_THAT(toolbar_model()->pinned_action_ids(),
testing::ElementsAre(id_b, id_a, id_c, extension_id));
toolbar_model()->MovePinnedAction(id_a, 2);
EXPECT_THAT(prefs->GetPinnedExtensions(),
testing::ElementsAre(id_b, id_c, id_a));
EXPECT_THAT(toolbar_model()->pinned_action_ids(),
testing::ElementsAre(id_b, id_c, id_a, extension_id));
toolbar_model()->MovePinnedAction(id_a, 0);
EXPECT_THAT(prefs->GetPinnedExtensions(),
testing::ElementsAre(id_a, id_b, id_c));
EXPECT_THAT(toolbar_model()->pinned_action_ids(),
testing::ElementsAre(id_a, id_b, id_c, extension_id));
// Try to move an extension to the right of the force-pinned one. This will
// not work, and the force-pinned one will stay to the right. But the other
// extension will still get moved as far right as it can.
toolbar_model()->MovePinnedAction(id_a, 3);
EXPECT_THAT(prefs->GetPinnedExtensions(),
testing::ElementsAre(id_b, id_c, id_a));
EXPECT_THAT(toolbar_model()->pinned_action_ids(),
testing::ElementsAre(id_b, id_c, id_a, extension_id));
// Again, but using an index greater than the rightmost index (mostly to check
// for crashes).
toolbar_model()->MovePinnedAction(id_a, 0);
EXPECT_THAT(prefs->GetPinnedExtensions(),
testing::ElementsAre(id_a, id_b, id_c));
EXPECT_THAT(toolbar_model()->pinned_action_ids(),
testing::ElementsAre(id_a, id_b, id_c, extension_id));
toolbar_model()->MovePinnedAction(id_a, 4);
EXPECT_THAT(prefs->GetPinnedExtensions(),
testing::ElementsAre(id_b, id_c, id_a));
EXPECT_THAT(toolbar_model()->pinned_action_ids(),
testing::ElementsAre(id_b, id_c, id_a, extension_id));
}
// Tests that the pin state (and position) for extensions that are unloaded
// (but *not* uninstalled) is preserved, even if the pinning order was modified
// while they were unloaded.
// Regression test for crbug.com/1203899.
TEST_F(ToolbarActionsModelUnitTest, UnloadedExtensionsPinnedStatePreserved) {
Init();
ASSERT_TRUE(AddBrowserActionExtensions());
EXPECT_THAT(toolbar_model()->action_ids(),
::testing::UnorderedElementsAre(browser_action_a()->id(),
browser_action_b()->id(),
browser_action_c()->id()));
EXPECT_THAT(toolbar_model()->pinned_action_ids(), ::testing::IsEmpty());
// Pin all of them.
toolbar_model()->SetActionVisibility(browser_action_a()->id(), true);
toolbar_model()->SetActionVisibility(browser_action_b()->id(), true);
toolbar_model()->SetActionVisibility(browser_action_c()->id(), true);
EXPECT_THAT(
toolbar_model()->pinned_action_ids(),
::testing::ElementsAre(browser_action_a()->id(), browser_action_b()->id(),
browser_action_c()->id()));
// Disable extension A. It should no longer be reflected in the pinned
// extensions (or the actions at all).
registrar()->DisableExtension(
browser_action_a()->id(),
{extensions::disable_reason::DISABLE_USER_ACTION});
EXPECT_THAT(toolbar_model()->action_ids(),
::testing::UnorderedElementsAre(browser_action_b()->id(),
browser_action_c()->id()));
EXPECT_THAT(toolbar_model()->pinned_action_ids(),
::testing::ElementsAre(browser_action_b()->id(),
browser_action_c()->id()));
// Re-enable extension A. It should retain it's pinned status (and position,
// at index 0).
registrar()->EnableExtension(browser_action_a()->id());
EXPECT_THAT(toolbar_model()->action_ids(),
::testing::UnorderedElementsAre(browser_action_a()->id(),
browser_action_b()->id(),
browser_action_c()->id()));
EXPECT_THAT(
toolbar_model()->pinned_action_ids(),
::testing::ElementsAre(browser_action_a()->id(), browser_action_b()->id(),
browser_action_c()->id()));
// Repeat the unload, reload flow, but move a pinned action
// (https://crbug.com/1203899) and unpin an action
// (https://crbug.com/1205561) between the unload and the reload.
registrar()->DisableExtension(
browser_action_a()->id(),
{extensions::disable_reason::DISABLE_USER_ACTION});
toolbar_model()->MovePinnedAction(browser_action_b()->id(), 1u);
toolbar_model()->SetActionVisibility(browser_action_b()->id(), false);
// Interim: state should include both B and C, but only C should be pinned.
EXPECT_THAT(toolbar_model()->action_ids(),
::testing::UnorderedElementsAre(browser_action_b()->id(),
browser_action_c()->id()));
EXPECT_THAT(toolbar_model()->pinned_action_ids(),
::testing::ElementsAre(browser_action_c()->id()));
// Reload - state should include all of A, B, C, with pinned order of A, C.
registrar()->EnableExtension(browser_action_a()->id());
EXPECT_THAT(toolbar_model()->action_ids(),
::testing::UnorderedElementsAre(browser_action_a()->id(),
browser_action_b()->id(),
browser_action_c()->id()));
EXPECT_THAT(toolbar_model()->pinned_action_ids(),
::testing::ElementsAre(browser_action_a()->id(),
browser_action_c()->id()));
}
TEST_F(ToolbarActionsModelUnitTest, InitActionList_EmitUserHistograms) {
InitializeEmptyExtensionService();
ASSERT_NO_FATAL_FAILURE(MaybeSetUpTestUser(
/*is_guest=*/false));
RunEmitUserHistogramsTest(/*incremented_histogram_count=*/1);
}
TEST_F(ToolbarActionsModelUnitTest, InitActionList_NonUserEmitHistograms) {
InitializeEmptyExtensionService();
ASSERT_NO_FATAL_FAILURE(MaybeSetUpTestUser(
/*is_guest=*/true));
RunEmitUserHistogramsTest(/*incremented_histogram_count=*/0);
}
|