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 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/containers/contains.h"
#include "base/strings/strcat.h"
#include "base/strings/stringprintf.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/extensions/commands/command_service.h"
#include "chrome/browser/extensions/component_loader.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/permissions/active_tab_permission_granter.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_command_controller.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/extensions/extensions_container.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h"
#include "chrome/browser/ui/toolbar/toolbar_actions_model.h"
#include "chrome/browser/ui/views/extensions/extensions_toolbar_container.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/sessions/content/session_tab_helper.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/javascript_test_observer.h"
#include "extensions/browser/extension_action.h"
#include "extensions/browser/extension_action_manager.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/test_event_router_observer.h"
#include "extensions/common/api/extension_action/action_info.h"
#include "extensions/common/api/extension_action/action_info_test_util.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/permissions/permissions_data.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/result_catcher.h"
#include "extensions/test/test_extension_dir.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "third_party/blink/public/common/switches.h"
using content::WebContents;
namespace extensions {
namespace {
// This extension ID is used for tests require a stable ID over multiple
// extension installs.
const char kId[] = "pgoakhfeplldmjheffidklpoklkppipp";
// Default keybinding to use for emulating user-defined shortcut overrides. The
// test extensions use Alt+Shift+F and Alt+Shift+H.
const char kAltShiftG[] = "Alt+Shift+G";
// Name of the command for the "basics" test extension.
const char kBasicsShortcutCommandName[] = "toggle-feature";
#if BUILDFLAG(IS_MAC)
const char kBookmarkKeybinding[] = "Command+D";
#else
const char kBookmarkKeybinding[] = "Ctrl+D";
#endif // BUILDFLAG(IS_MAC)
bool SendBookmarkKeyPressSync(Browser* browser) {
return ui_test_utils::SendKeyPressSync(browser, ui::VKEY_D,
#if BUILDFLAG(IS_MAC)
false, false, false, true
#else
true, false, false, false
#endif
);
}
// Named command for media key overwrite test.
const char kMediaKeyTestCommand[] = "test_mediakeys_update";
// A scoped observer that listens for dom automation messages.
class DomMessageListener : public content::TestMessageHandler {
public:
explicit DomMessageListener(content::WebContents* web_contents);
DomMessageListener(const DomMessageListener&) = delete;
DomMessageListener& operator=(const DomMessageListener&) = delete;
~DomMessageListener() override;
// Wait until a message is received.
void Wait();
// Clears and resets the observer.
void Clear();
const std::string& message() const { return message_; }
private:
// content::TestMessageHandler:
MessageResponse HandleMessage(const std::string& json) override;
void Reset() override;
// The message received. Note that this will be JSON, so if it is a string,
// it will be wrapped in quotes.
std::string message_;
content::JavascriptTestObserver observer_;
};
DomMessageListener::DomMessageListener(content::WebContents* web_contents)
: observer_(web_contents, this) {
}
DomMessageListener::~DomMessageListener() = default;
void DomMessageListener::Wait() {
observer_.Run();
}
void DomMessageListener::Clear() {
// We don't just call this in DomMessageListener::Reset() because the
// JavascriptTestObserver's Reset() method also resets its handler (this).
observer_.Reset();
}
content::TestMessageHandler::MessageResponse DomMessageListener::HandleMessage(
const std::string& json) {
message_ = json;
return DONE;
}
void DomMessageListener::Reset() {
TestMessageHandler::Reset();
message_.clear();
}
// Programmatically (from the extension) sets the action of |extension| to be
// visible on the tab with the given |tab_id|. Expects the action is *not*
// visible to start.
void SetActionVisibleOnTab(Profile* profile,
const Extension& extension,
int tab_id) {
ExtensionActionManager* action_manager = ExtensionActionManager::Get(profile);
const ExtensionAction* extension_action =
action_manager->GetExtensionAction(extension);
ASSERT_TRUE(extension_action);
EXPECT_FALSE(extension_action->GetIsVisible(tab_id));
static constexpr char kScriptTemplate[] =
R"(chrome.pageAction.show(%d, () => {
chrome.test.sendScriptResult(
chrome.runtime.lastError ?
chrome.runtime.lastError.message :
'success');
});)";
base::Value set_result = browsertest_util::ExecuteScriptInBackgroundPage(
profile, extension.id(), base::StringPrintf(kScriptTemplate, tab_id));
EXPECT_EQ("success", set_result);
EXPECT_TRUE(extension_action->GetIsVisible(tab_id));
}
// Sends a keypress with the given |keyboard_code| to the specified |extension|.
// If |expect_dispatch| is true, expects pageAction.onClicked to be dispatched
// to the extension. Otherwise, expects it is not sent.
void SendKeyPressToAction(Browser* browser,
const Extension& extension,
ui::KeyboardCode keyboard_code,
const char* event_name,
bool expect_dispatch) {
ExtensionTestMessageListener click_listener("clicked");
click_listener.set_extension_id(extension.id());
Profile* profile = browser->profile();
EventRouter* event_router = EventRouter::Get(profile);
TestEventRouterObserver event_tracker(event_router);
// Activate the shortcut (Alt+Shift+F).
if (!ui_test_utils::SendKeyPressSync(browser, keyboard_code, false, true,
true, false)) {
ADD_FAILURE() << "Could not send key press!";
return;
}
base::RunLoop().RunUntilIdle();
// Check that the event was dispatched if and only if we expected it to be.
EXPECT_EQ(expect_dispatch,
base::Contains(event_tracker.dispatched_events(), event_name));
// Do a round-trip to the extension renderer. This serves as a pseudo-
// RunUntilIdle()-type of method for the extension renderer itself, since
// test.sendMessage() is FIFO.
// This allows us to return the result of click_listener.was_satisfied(),
// rather than using WaitUntilSatisfied(), which in turn allows this method
// to exercise both the case of expecting dispatch and expecting *not* to
// dispatch.
static constexpr char kScript[] =
R"(chrome.test.sendMessage(
'run loop hack',
() => {
chrome.test.sendScriptResult('success');
});)";
base::Value set_result = browsertest_util::ExecuteScriptInBackgroundPage(
profile, extension.id(), kScript);
EXPECT_EQ("success", set_result);
EXPECT_EQ(expect_dispatch, click_listener.was_satisfied());
}
// Given an |action_type|, returns the corresponding command key.
const char* GetCommandKeyForActionType(ActionInfo::Type action_type) {
const char* command_key = nullptr;
switch (action_type) {
case ActionInfo::Type::kBrowser:
command_key = manifest_values::kBrowserActionCommandEvent;
break;
case ActionInfo::Type::kPage:
command_key = manifest_values::kPageActionCommandEvent;
break;
case ActionInfo::Type::kAction:
command_key = manifest_values::kActionCommandEvent;
break;
}
return command_key;
}
} // namespace
class CommandsApiTest : public ExtensionApiTest {
public:
CommandsApiTest() = default;
~CommandsApiTest() override = default;
void SetUpOnMainThread() override {
ExtensionApiTest::SetUpOnMainThread();
#if BUILDFLAG(IS_MAC)
// ExtensionKeybindingRegistryViews doesn't get registered until BrowserView
// is activated at least once.
// TODO(crbug.com/41386956): Registry creation should happen independent of
// activation. Focus manager lifetime may make this tricky to untangle.
// TODO(crbug.com/40486728): Reassess after activation is restored in the
// focus manager.
ui_test_utils::BrowserActivationWaiter waiter(browser());
ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
waiter.WaitForActivation();
ASSERT_TRUE(browser()->window()->IsActive());
#endif
}
void SetUpCommandLine(base::CommandLine* command_line) override {
ExtensionApiTest::SetUpCommandLine(command_line);
// Some builders are flaky due to slower loading interacting with
// deferred commits. This primarily impacts chromeos for the test
// CommandsApiTest.ContinuePropagation.
command_line->AppendSwitch(blink::switches::kAllowPreCommitInput);
}
protected:
bool IsGrantedForTab(const Extension* extension,
const content::WebContents* web_contents) {
return extension->permissions_data()->HasAPIPermissionForTab(
sessions::SessionTabHelper::IdForTab(web_contents).id(),
mojom::APIPermissionID::kTab);
}
// Returns true if the extension with the given |extension_id| has an active
// command associated with an action of the given |action_type|.
bool HasActiveActionCommand(const ExtensionId& extension_id,
ActionInfo::Type action_type) {
bool active = false;
Command command;
CommandService* const command_service =
CommandService::Get(browser()->profile());
bool found_command = command_service->GetExtensionActionCommand(
extension_id, action_type, CommandService::ALL, &command, &active);
return found_command && active;
}
// Navigates to a test URL and return the ID of the navigated tab.
int NavigateToTestURLAndReturnTabId() {
EXPECT_TRUE(ui_test_utils::NavigateToURL(
browser(),
embedded_test_server()->GetURL("/extensions/test_file.txt")));
return sessions::SessionTabHelper::FromWebContents(
browser()->tab_strip_model()->GetActiveWebContents())
->session_id()
.id();
}
};
class IncognitoCommandsApiTest : public CommandsApiTest,
public testing::WithParamInterface<bool> {};
// A parameterized version to allow testing with different action types.
class ActionCommandsApiTest
: public CommandsApiTest,
public testing::WithParamInterface<ActionInfo::Type> {};
// Test the basic functionality of the Keybinding API:
// - That pressing the shortcut keys should perform actions (activate the
// browser action or send an event).
// - Note: Page action keybindings are tested in PageAction test below.
// - The shortcut keys taken by one extension are not overwritten by the last
// installed extension.
IN_PROC_BROWSER_TEST_F(CommandsApiTest, Basic) {
ASSERT_TRUE(embedded_test_server()->Start());
ASSERT_TRUE(RunExtensionTest("keybinding/basics")) << message_;
const Extension* extension = GetSingleLoadedExtension();
ASSERT_TRUE(extension) << message_;
// Load this extension, which uses the same keybindings but sets the page
// to different colors. This is so we can see that it doesn't interfere. We
// don't test this extension in any other way (it should otherwise be
// immaterial to this test).
ASSERT_TRUE(RunExtensionTest("keybinding/conflicting")) << message_;
// Test that there are two browser actions in the toolbar.
ExtensionsToolbarContainer* extensions_container =
browser()->GetBrowserView().toolbar()->extensions_container();
ASSERT_EQ(2, extensions_container->GetNumberOfActionsForTesting());
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL("/extensions/test_file.txt")));
// activeTab shouldn't have been granted yet.
WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(tab);
EXPECT_FALSE(IsGrantedForTab(extension, tab));
ExtensionTestMessageListener test_listener; // Won't reply.
// Activate the browser action shortcut (Ctrl+Shift+F).
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
browser(), ui::VKEY_F, true, true, false, false));
EXPECT_TRUE(test_listener.WaitUntilSatisfied());
// activeTab should now be granted.
EXPECT_TRUE(IsGrantedForTab(extension, tab));
// Verify the command worked.
EXPECT_EQ(std::string("basics browser action"), test_listener.message());
test_listener.Reset();
// Activate the command shortcut (Ctrl+Shift+Y).
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
browser(), ui::VKEY_Y, true, true, false, false));
EXPECT_TRUE(test_listener.WaitUntilSatisfied());
EXPECT_EQ(std::string(kBasicsShortcutCommandName), test_listener.message());
}
IN_PROC_BROWSER_TEST_F(CommandsApiTest, InactivePageActionDoesntTrigger) {
ASSERT_TRUE(embedded_test_server()->Start());
ASSERT_TRUE(RunExtensionTest("keybinding/page_action")) << message_;
const Extension* extension = GetSingleLoadedExtension();
ASSERT_TRUE(extension) << message_;
const int tab_id = NavigateToTestURLAndReturnTabId();
ExtensionActionManager* action_manager =
ExtensionActionManager::Get(profile());
const ExtensionAction* extension_action =
action_manager->GetExtensionAction(*extension);
ASSERT_TRUE(extension_action);
EXPECT_FALSE(extension_action->GetIsVisible(tab_id));
// If the page action is disabled / hidden, the event shouldn't be dispatched.
bool expect_dispatch = false;
SendKeyPressToAction(browser(), *extension, ui::VKEY_F,
"pageAction.onClicked", expect_dispatch);
}
// Tests that a page action that is unpinned and only shown within the
// extensions menu will still properly trigger when the keybinding is used.
IN_PROC_BROWSER_TEST_F(CommandsApiTest, UnpinnedPageActionTriggers) {
ASSERT_TRUE(embedded_test_server()->Start());
ASSERT_TRUE(RunExtensionTest("keybinding/page_action")) << message_;
const Extension* extension = GetSingleLoadedExtension();
ASSERT_TRUE(extension) << message_;
ExtensionsToolbarContainer* extensions_container =
browser()->GetBrowserView().toolbar()->extensions_container();
RunScheduledLayouts();
EXPECT_FALSE(extensions_container->IsActionVisibleOnToolbar(extension->id()));
const int tab_id = NavigateToTestURLAndReturnTabId();
SetActionVisibleOnTab(profile(), *extension, tab_id);
ASSERT_TRUE(WaitForPageActionVisibilityChangeTo(1));
constexpr bool kExpectDispatch = true;
SendKeyPressToAction(browser(), *extension, ui::VKEY_F,
"pageAction.onClicked", kExpectDispatch);
}
IN_PROC_BROWSER_TEST_F(CommandsApiTest, PageActionKeyUpdated) {
ASSERT_TRUE(embedded_test_server()->Start());
ASSERT_TRUE(RunExtensionTest("keybinding/page_action")) << message_;
const Extension* extension = GetSingleLoadedExtension();
ASSERT_TRUE(extension) << message_;
CommandService* command_service = CommandService::Get(browser()->profile());
// Simulate the user setting the keybinding to Alt+Shift+G.
command_service->UpdateKeybindingPrefs(
extension->id(), manifest_values::kPageActionCommandEvent, kAltShiftG);
const int tab_id = NavigateToTestURLAndReturnTabId();
SetActionVisibleOnTab(profile(), *extension, tab_id);
ASSERT_TRUE(WaitForPageActionVisibilityChangeTo(1));
bool expect_dispatch = true;
SendKeyPressToAction(browser(), *extension, ui::VKEY_G,
"pageAction.onClicked", expect_dispatch);
}
// Verify that keyboard shortcut takes effect without reloading the extension.
// Regression test for https://crbug.com/1190476.
IN_PROC_BROWSER_TEST_F(CommandsApiTest, ActionKeyUpdated) {
ASSERT_TRUE(embedded_test_server()->Start());
ASSERT_TRUE(RunExtensionTest("keybinding/action")) << message_;
const Extension* extension = GetSingleLoadedExtension();
ASSERT_TRUE(extension) << message_;
// Simulate the user changing the keybinding.
CommandService* command_service = CommandService::Get(browser()->profile());
command_service->UpdateKeybindingPrefs(
extension->id(), manifest_values::kActionCommandEvent, "Ctrl+Shift+Y");
// Verify that the action event occurs for the new keyboard shortcut.
ResultCatcher catcher;
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_Y, true, true,
false, false));
ASSERT_TRUE(catcher.GetNextResult());
}
IN_PROC_BROWSER_TEST_F(CommandsApiTest, PageActionOverrideChromeShortcut) {
ASSERT_TRUE(embedded_test_server()->Start());
ASSERT_TRUE(RunExtensionTest("keybinding/page_action")) << message_;
const Extension* extension = GetSingleLoadedExtension();
ASSERT_TRUE(extension) << message_;
CommandService* command_service = CommandService::Get(browser()->profile());
// Simulate the user setting the keybinding to override the print shortcut.
#if BUILDFLAG(IS_MAC)
std::string print_shortcut = "Command+P";
#else
std::string print_shortcut = "Ctrl+P";
#endif
command_service->UpdateKeybindingPrefs(
extension->id(), manifest_values::kPageActionCommandEvent,
print_shortcut);
const int tab_id = NavigateToTestURLAndReturnTabId();
SetActionVisibleOnTab(profile(), *extension, tab_id);
ASSERT_TRUE(WaitForPageActionVisibilityChangeTo(1));
ExtensionTestMessageListener test_listener; // Won't reply.
test_listener.set_extension_id(extension->id());
// Note: The following incantation uses too many custom bits to comfortably
// fit into SendKeyPressToAction(); do it manually.
bool control_is_modifier = false;
bool command_is_modifier = false;
#if BUILDFLAG(IS_MAC)
command_is_modifier = true;
#else
control_is_modifier = true;
#endif
// Activate the omnibox. This checks to ensure that the extension shortcut
// still works even if the WebContents isn't focused.
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_L,
control_is_modifier, false, false,
command_is_modifier));
// Activate the shortcut.
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_P,
control_is_modifier, false, false,
command_is_modifier));
EXPECT_TRUE(test_listener.WaitUntilSatisfied());
EXPECT_EQ("clicked", test_listener.message());
}
// This test validates that the getAll query API function returns registered
// commands as well as synthesized ones and that inactive commands (like the
// synthesized ones are in nature) have no shortcuts.
IN_PROC_BROWSER_TEST_F(CommandsApiTest, SynthesizedCommand) {
ASSERT_TRUE(embedded_test_server()->Start());
ASSERT_TRUE(RunExtensionTest("keybinding/synthesized")) << message_;
}
// This test validates that an extension cannot request a shortcut that is
// already in use by Chrome.
IN_PROC_BROWSER_TEST_F(CommandsApiTest, DontOverwriteSystemShortcuts) {
ASSERT_TRUE(embedded_test_server()->Start());
ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
ASSERT_TRUE(RunExtensionTest("keybinding/dont_overwrite_system")) << message_;
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL("/extensions/test_file.txt")));
// Activate the regular shortcut (Alt+Shift+F).
ExtensionTestMessageListener alt_shift_f_listener("alt_shift_f");
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
browser(), ui::VKEY_F, false, true, true, false));
EXPECT_TRUE(alt_shift_f_listener.WaitUntilSatisfied());
// Try to activate the Ctrl+F shortcut (shouldn't work).
// Since keypresses are sent synchronously, we can check this by first sending
// Ctrl+F (which shouldn't work), followed by Alt+Shift+F (which should work),
// and listening for both. If, by the time we receive the Alt+Shift+F
// response, we haven't received a response for Ctrl+F, it is safe to say we
// won't receive one.
ExtensionTestMessageListener ctrl_f_listener("ctrl_f");
alt_shift_f_listener.Reset();
// Send Ctrl+F.
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_F, true,
false, false, false));
// Send Alt+Shift+F.
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_F, false,
true, true, false));
EXPECT_TRUE(alt_shift_f_listener.WaitUntilSatisfied());
EXPECT_FALSE(ctrl_f_listener.was_satisfied());
}
// This test validates that user-set override of the Chrome bookmark shortcut in
// an extension that does not request it does supersede the same keybinding by
// web pages.
IN_PROC_BROWSER_TEST_F(CommandsApiTest,
OverwriteBookmarkShortcutByUserOverridesWebKeybinding) {
ASSERT_TRUE(embedded_test_server()->Start());
ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
ASSERT_TRUE(RunExtensionTest("keybinding/basics")) << message_;
CommandService* command_service = CommandService::Get(browser()->profile());
const Extension* extension = GetSingleLoadedExtension();
// Simulate the user setting the keybinding to Ctrl+D.
command_service->UpdateKeybindingPrefs(
extension->id(), manifest_values::kBrowserActionCommandEvent,
kBookmarkKeybinding);
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL(
"/extensions/test_file_with_ctrl-d_keybinding.html")));
ExtensionTestMessageListener test_listener;
// Activate the shortcut (Ctrl+D) which should be handled by the extension.
ASSERT_TRUE(SendBookmarkKeyPressSync(browser()));
EXPECT_TRUE(test_listener.WaitUntilSatisfied());
EXPECT_EQ(std::string("basics browser action"), test_listener.message());
}
#if BUILDFLAG(IS_WIN)
// Currently this feature is implemented on Windows only.
#define MAYBE_AllowDuplicatedMediaKeys AllowDuplicatedMediaKeys
#else
#define MAYBE_AllowDuplicatedMediaKeys DISABLED_AllowDuplicatedMediaKeys
#endif
// Test that media keys go to all extensions that register for them.
IN_PROC_BROWSER_TEST_F(CommandsApiTest, MAYBE_AllowDuplicatedMediaKeys) {
ResultCatcher catcher;
ASSERT_TRUE(RunExtensionTest("keybinding/non_global_media_keys_0"))
<< message_;
ASSERT_TRUE(catcher.GetNextResult());
ASSERT_TRUE(RunExtensionTest("keybinding/non_global_media_keys_1"))
<< message_;
ASSERT_TRUE(catcher.GetNextResult());
// Activate the Media Stop key.
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
browser(), ui::VKEY_MEDIA_STOP, false, false, false, false));
// We should get two success result.
ASSERT_TRUE(catcher.GetNextResult());
ASSERT_TRUE(catcher.GetNextResult());
}
IN_PROC_BROWSER_TEST_F(CommandsApiTest, ShortcutAddedOnUpdate) {
base::ScopedAllowBlockingForTesting allow_blocking;
base::ScopedTempDir scoped_temp_dir;
EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
base::FilePath pem_path = test_data_dir_.
AppendASCII("keybinding").AppendASCII("keybinding.pem");
base::FilePath path_v1_unassigned = PackExtensionWithOptions(
test_data_dir_.AppendASCII("keybinding")
.AppendASCII("update")
.AppendASCII("v1_unassigned"),
scoped_temp_dir.GetPath().AppendASCII("v1_unassigned.crx"), pem_path,
base::FilePath());
base::FilePath path_v2 =
PackExtensionWithOptions(test_data_dir_.AppendASCII("keybinding")
.AppendASCII("update")
.AppendASCII("v2"),
scoped_temp_dir.GetPath().AppendASCII("v2.crx"),
pem_path, base::FilePath());
ExtensionRegistry* registry = ExtensionRegistry::Get(browser()->profile());
CommandService* command_service = CommandService::Get(browser()->profile());
// Install v1 of the extension without keybinding assigned.
ASSERT_TRUE(InstallExtension(path_v1_unassigned, 1));
EXPECT_TRUE(registry->enabled_extensions().GetByID(kId) != nullptr);
// Verify it is set to nothing.
ui::Accelerator accelerator = command_service->FindCommandByName(
kId, manifest_values::kBrowserActionCommandEvent).accelerator();
EXPECT_EQ(ui::VKEY_UNKNOWN, accelerator.key_code());
// Update to version 2 with keybinding.
EXPECT_TRUE(UpdateExtension(kId, path_v2, 0));
EXPECT_TRUE(registry->enabled_extensions().GetByID(kId) != nullptr);
// Verify it has a command of Alt+Shift+F.
accelerator = command_service->FindCommandByName(
kId, manifest_values::kBrowserActionCommandEvent).accelerator();
EXPECT_EQ(ui::VKEY_F, accelerator.key_code());
EXPECT_FALSE(accelerator.IsCtrlDown());
EXPECT_TRUE(accelerator.IsShiftDown());
EXPECT_TRUE(accelerator.IsAltDown());
}
IN_PROC_BROWSER_TEST_F(CommandsApiTest, ShortcutChangedOnUpdate) {
base::ScopedAllowBlockingForTesting allow_blocking;
base::ScopedTempDir scoped_temp_dir;
EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
base::FilePath pem_path = test_data_dir_.
AppendASCII("keybinding").AppendASCII("keybinding.pem");
base::FilePath path_v1 =
PackExtensionWithOptions(test_data_dir_.AppendASCII("keybinding")
.AppendASCII("update")
.AppendASCII("v1"),
scoped_temp_dir.GetPath().AppendASCII("v1.crx"),
pem_path, base::FilePath());
base::FilePath path_v2_reassigned = PackExtensionWithOptions(
test_data_dir_.AppendASCII("keybinding")
.AppendASCII("update")
.AppendASCII("v2_reassigned"),
scoped_temp_dir.GetPath().AppendASCII("v2_reassigned.crx"), pem_path,
base::FilePath());
ExtensionRegistry* registry = ExtensionRegistry::Get(browser()->profile());
CommandService* command_service = CommandService::Get(browser()->profile());
// Install v1 of the extension.
ASSERT_TRUE(InstallExtension(path_v1, 1));
EXPECT_TRUE(registry->enabled_extensions().GetByID(kId) != nullptr);
// Verify it has a command of Alt+Shift+F.
ui::Accelerator accelerator = command_service->FindCommandByName(
kId, manifest_values::kBrowserActionCommandEvent).accelerator();
EXPECT_EQ(ui::VKEY_F, accelerator.key_code());
EXPECT_FALSE(accelerator.IsCtrlDown());
EXPECT_TRUE(accelerator.IsShiftDown());
EXPECT_TRUE(accelerator.IsAltDown());
// Update to version 2 with different keybinding assigned.
EXPECT_TRUE(UpdateExtension(kId, path_v2_reassigned, 0));
EXPECT_TRUE(registry->enabled_extensions().GetByID(kId) != nullptr);
// Verify it has a command of Alt+Shift+J.
accelerator = command_service->FindCommandByName(
kId, manifest_values::kBrowserActionCommandEvent).accelerator();
EXPECT_EQ(ui::VKEY_J, accelerator.key_code());
EXPECT_FALSE(accelerator.IsCtrlDown());
EXPECT_TRUE(accelerator.IsShiftDown());
EXPECT_TRUE(accelerator.IsAltDown());
}
IN_PROC_BROWSER_TEST_F(CommandsApiTest, ShortcutRemovedOnUpdate) {
base::ScopedAllowBlockingForTesting allow_blocking;
base::ScopedTempDir scoped_temp_dir;
EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
base::FilePath pem_path = test_data_dir_.
AppendASCII("keybinding").AppendASCII("keybinding.pem");
base::FilePath path_v1 =
PackExtensionWithOptions(test_data_dir_.AppendASCII("keybinding")
.AppendASCII("update")
.AppendASCII("v1"),
scoped_temp_dir.GetPath().AppendASCII("v1.crx"),
pem_path, base::FilePath());
base::FilePath path_v2_unassigned = PackExtensionWithOptions(
test_data_dir_.AppendASCII("keybinding")
.AppendASCII("update")
.AppendASCII("v2_unassigned"),
scoped_temp_dir.GetPath().AppendASCII("v2_unassigned.crx"), pem_path,
base::FilePath());
ExtensionRegistry* registry = ExtensionRegistry::Get(browser()->profile());
CommandService* command_service = CommandService::Get(browser()->profile());
// Install v1 of the extension.
ASSERT_TRUE(InstallExtension(path_v1, 1));
EXPECT_TRUE(registry->enabled_extensions().GetByID(kId) != nullptr);
// Verify it has a command of Alt+Shift+F.
ui::Accelerator accelerator = command_service->FindCommandByName(
kId, manifest_values::kBrowserActionCommandEvent).accelerator();
EXPECT_EQ(ui::VKEY_F, accelerator.key_code());
EXPECT_FALSE(accelerator.IsCtrlDown());
EXPECT_TRUE(accelerator.IsShiftDown());
EXPECT_TRUE(accelerator.IsAltDown());
// Update to version 2 without keybinding assigned.
EXPECT_TRUE(UpdateExtension(kId, path_v2_unassigned, 0));
EXPECT_TRUE(registry->enabled_extensions().GetByID(kId) != nullptr);
// Verify the keybinding gets set to nothing.
accelerator = command_service->FindCommandByName(
kId, manifest_values::kBrowserActionCommandEvent).accelerator();
EXPECT_EQ(ui::VKEY_UNKNOWN, accelerator.key_code());
}
IN_PROC_BROWSER_TEST_F(CommandsApiTest,
ShortcutAddedOnUpdateAfterBeingAssignedByUser) {
base::ScopedAllowBlockingForTesting allow_blocking;
base::ScopedTempDir scoped_temp_dir;
EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
base::FilePath pem_path = test_data_dir_.
AppendASCII("keybinding").AppendASCII("keybinding.pem");
base::FilePath path_v1_unassigned = PackExtensionWithOptions(
test_data_dir_.AppendASCII("keybinding")
.AppendASCII("update")
.AppendASCII("v1_unassigned"),
scoped_temp_dir.GetPath().AppendASCII("v1_unassigned.crx"), pem_path,
base::FilePath());
base::FilePath path_v2 =
PackExtensionWithOptions(test_data_dir_.AppendASCII("keybinding")
.AppendASCII("update")
.AppendASCII("v2"),
scoped_temp_dir.GetPath().AppendASCII("v2.crx"),
pem_path, base::FilePath());
ExtensionRegistry* registry = ExtensionRegistry::Get(browser()->profile());
CommandService* command_service = CommandService::Get(browser()->profile());
// Install v1 of the extension without keybinding assigned.
ASSERT_TRUE(InstallExtension(path_v1_unassigned, 1));
EXPECT_TRUE(registry->enabled_extensions().GetByID(kId) != nullptr);
// Verify it is set to nothing.
ui::Accelerator accelerator = command_service->FindCommandByName(
kId, manifest_values::kBrowserActionCommandEvent).accelerator();
EXPECT_EQ(ui::VKEY_UNKNOWN, accelerator.key_code());
// Simulate the user setting the keybinding to Alt+Shift+G.
command_service->UpdateKeybindingPrefs(
kId, manifest_values::kBrowserActionCommandEvent, kAltShiftG);
// Update to version 2 with keybinding.
EXPECT_TRUE(UpdateExtension(kId, path_v2, 0));
EXPECT_TRUE(registry->enabled_extensions().GetByID(kId) != nullptr);
// Verify the previously-set keybinding is still set.
accelerator = command_service->FindCommandByName(
kId, manifest_values::kBrowserActionCommandEvent).accelerator();
EXPECT_EQ(ui::VKEY_G, accelerator.key_code());
EXPECT_FALSE(accelerator.IsCtrlDown());
EXPECT_TRUE(accelerator.IsShiftDown());
EXPECT_TRUE(accelerator.IsAltDown());
}
IN_PROC_BROWSER_TEST_F(CommandsApiTest,
ShortcutChangedOnUpdateAfterBeingReassignedByUser) {
base::ScopedAllowBlockingForTesting allow_blocking;
base::ScopedTempDir scoped_temp_dir;
EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
base::FilePath pem_path = test_data_dir_.
AppendASCII("keybinding").AppendASCII("keybinding.pem");
base::FilePath path_v1 =
PackExtensionWithOptions(test_data_dir_.AppendASCII("keybinding")
.AppendASCII("update")
.AppendASCII("v1"),
scoped_temp_dir.GetPath().AppendASCII("v1.crx"),
pem_path, base::FilePath());
base::FilePath path_v2_reassigned = PackExtensionWithOptions(
test_data_dir_.AppendASCII("keybinding")
.AppendASCII("update")
.AppendASCII("v2_reassigned"),
scoped_temp_dir.GetPath().AppendASCII("v2_reassigned.crx"), pem_path,
base::FilePath());
ExtensionRegistry* registry = ExtensionRegistry::Get(browser()->profile());
CommandService* command_service = CommandService::Get(browser()->profile());
// Install v1 of the extension.
ASSERT_TRUE(InstallExtension(path_v1, 1));
EXPECT_TRUE(registry->enabled_extensions().GetByID(kId) != nullptr);
// Verify it has a command of Alt+Shift+F.
ui::Accelerator accelerator = command_service->FindCommandByName(
kId, manifest_values::kBrowserActionCommandEvent).accelerator();
EXPECT_EQ(ui::VKEY_F, accelerator.key_code());
EXPECT_FALSE(accelerator.IsCtrlDown());
EXPECT_TRUE(accelerator.IsShiftDown());
EXPECT_TRUE(accelerator.IsAltDown());
// Simulate the user setting the keybinding to Alt+Shift+G.
command_service->UpdateKeybindingPrefs(
kId, manifest_values::kBrowserActionCommandEvent, kAltShiftG);
// Update to version 2 with different keybinding assigned.
EXPECT_TRUE(UpdateExtension(kId, path_v2_reassigned, 0));
EXPECT_TRUE(registry->enabled_extensions().GetByID(kId) != nullptr);
// Verify it has a command of Alt+Shift+G.
accelerator = command_service->FindCommandByName(
kId, manifest_values::kBrowserActionCommandEvent).accelerator();
EXPECT_EQ(ui::VKEY_G, accelerator.key_code());
EXPECT_FALSE(accelerator.IsCtrlDown());
EXPECT_TRUE(accelerator.IsShiftDown());
EXPECT_TRUE(accelerator.IsAltDown());
}
// Test that Media keys do not overwrite previous settings.
IN_PROC_BROWSER_TEST_F(CommandsApiTest,
MediaKeyShortcutChangedOnUpdateAfterBeingReassignedByUser) {
base::ScopedAllowBlockingForTesting allow_blocking;
base::ScopedTempDir scoped_temp_dir;
EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
base::FilePath pem_path = test_data_dir_.
AppendASCII("keybinding").AppendASCII("keybinding.pem");
base::FilePath path_v1 = PackExtensionWithOptions(
test_data_dir_.AppendASCII("keybinding")
.AppendASCII("update")
.AppendASCII("mk_v1"),
scoped_temp_dir.GetPath().AppendASCII("mk_v1.crx"), pem_path,
base::FilePath());
base::FilePath path_v2_reassigned = PackExtensionWithOptions(
test_data_dir_.AppendASCII("keybinding")
.AppendASCII("update")
.AppendASCII("mk_v2"),
scoped_temp_dir.GetPath().AppendASCII("mk_v2.crx"), pem_path,
base::FilePath());
ExtensionRegistry* registry = ExtensionRegistry::Get(browser()->profile());
CommandService* command_service = CommandService::Get(browser()->profile());
// Install v1 of the extension.
ASSERT_TRUE(InstallExtension(path_v1, 1));
EXPECT_TRUE(registry->enabled_extensions().GetByID(kId) != nullptr);
// Verify it has a command of MediaPlayPause.
ui::Accelerator accelerator = command_service->FindCommandByName(
kId, kMediaKeyTestCommand).accelerator();
EXPECT_EQ(ui::VKEY_MEDIA_PLAY_PAUSE, accelerator.key_code());
EXPECT_FALSE(accelerator.IsCtrlDown());
EXPECT_FALSE(accelerator.IsShiftDown());
EXPECT_FALSE(accelerator.IsAltDown());
// Simulate the user setting the keybinding to Alt+Shift+G.
command_service->UpdateKeybindingPrefs(
kId, kMediaKeyTestCommand, kAltShiftG);
// Update to version 2 with different keybinding assigned.
EXPECT_TRUE(UpdateExtension(kId, path_v2_reassigned, 0));
EXPECT_TRUE(registry->enabled_extensions().GetByID(kId) != nullptr);
// Verify it has a command of Alt+Shift+G.
accelerator = command_service->FindCommandByName(
kId, kMediaKeyTestCommand).accelerator();
EXPECT_EQ(ui::VKEY_G, accelerator.key_code());
EXPECT_FALSE(accelerator.IsCtrlDown());
EXPECT_TRUE(accelerator.IsShiftDown());
EXPECT_TRUE(accelerator.IsAltDown());
}
IN_PROC_BROWSER_TEST_F(CommandsApiTest,
ShortcutRemovedOnUpdateAfterBeingReassignedByUser) {
base::ScopedAllowBlockingForTesting allow_blocking;
base::ScopedTempDir scoped_temp_dir;
EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
base::FilePath pem_path = test_data_dir_.
AppendASCII("keybinding").AppendASCII("keybinding.pem");
base::FilePath path_v1 =
PackExtensionWithOptions(test_data_dir_.AppendASCII("keybinding")
.AppendASCII("update")
.AppendASCII("v1"),
scoped_temp_dir.GetPath().AppendASCII("v1.crx"),
pem_path, base::FilePath());
base::FilePath path_v2_unassigned = PackExtensionWithOptions(
test_data_dir_.AppendASCII("keybinding")
.AppendASCII("update")
.AppendASCII("v2_unassigned"),
scoped_temp_dir.GetPath().AppendASCII("v2_unassigned.crx"), pem_path,
base::FilePath());
ExtensionRegistry* registry = ExtensionRegistry::Get(browser()->profile());
CommandService* command_service = CommandService::Get(browser()->profile());
// Install v1 of the extension.
ASSERT_TRUE(InstallExtension(path_v1, 1));
EXPECT_TRUE(registry->enabled_extensions().GetByID(kId) != nullptr);
// Verify it has a command of Alt+Shift+F.
ui::Accelerator accelerator = command_service->FindCommandByName(
kId, manifest_values::kBrowserActionCommandEvent).accelerator();
EXPECT_EQ(ui::VKEY_F, accelerator.key_code());
EXPECT_FALSE(accelerator.IsCtrlDown());
EXPECT_TRUE(accelerator.IsShiftDown());
EXPECT_TRUE(accelerator.IsAltDown());
// Simulate the user reassigning the keybinding to Alt+Shift+G.
command_service->UpdateKeybindingPrefs(
kId, manifest_values::kBrowserActionCommandEvent, kAltShiftG);
// Update to version 2 without keybinding assigned.
EXPECT_TRUE(UpdateExtension(kId, path_v2_unassigned, 0));
EXPECT_TRUE(registry->enabled_extensions().GetByID(kId) != nullptr);
// Verify the keybinding is still set.
accelerator = command_service->FindCommandByName(
kId, manifest_values::kBrowserActionCommandEvent).accelerator();
EXPECT_EQ(ui::VKEY_G, accelerator.key_code());
EXPECT_FALSE(accelerator.IsCtrlDown());
EXPECT_TRUE(accelerator.IsShiftDown());
EXPECT_TRUE(accelerator.IsAltDown());
}
//
#if BUILDFLAG(IS_CHROMEOS) && !defined(NDEBUG)
// TODO(dtseng): Test times out on Chrome OS debug. See http://crbug.com/412456.
#define MAYBE_ContinuePropagation DISABLED_ContinuePropagation
#else
#define MAYBE_ContinuePropagation ContinuePropagation
#endif
IN_PROC_BROWSER_TEST_F(CommandsApiTest, MAYBE_ContinuePropagation) {
// Setup the environment.
ASSERT_TRUE(embedded_test_server()->Start());
ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
ASSERT_TRUE(RunExtensionTest("keybinding/continue_propagation")) << message_;
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL("/extensions/test_file.txt")));
ResultCatcher catcher;
// Activate the shortcut (Ctrl+Shift+F). The page should capture the
// keystroke and not the extension since |onCommand| has no event listener
// initially.
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
browser(), ui::VKEY_F, true, true, false, false));
ASSERT_TRUE(catcher.GetNextResult());
// Now, the extension should have added an |onCommand| event listener.
// Send the same key, but the |onCommand| listener should now receive it.
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
browser(), ui::VKEY_F, true, true, false, false));
ASSERT_TRUE(catcher.GetNextResult());
// The extension should now have removed its |onCommand| event listener.
// Finally, the page should again receive the key.
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
browser(), ui::VKEY_F, true, true, false, false));
ASSERT_TRUE(catcher.GetNextResult());
}
// Test is only applicable on Chrome OS.
#if BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(CommandsApiTest, ChromeOSConversions) {
ASSERT_TRUE(embedded_test_server()->Start());
ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
ASSERT_TRUE(RunExtensionTest("keybinding/chromeos_conversions")) << message_;
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL("/extensions/test_file.txt")));
ResultCatcher catcher;
// Send all expected keys (Search+Shift+{Left, Up, Right, Down}).
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_LEFT, false,
true, false, true));
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_UP, false,
true, false, true));
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_RIGHT, false,
true, false, true));
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_DOWN, false,
true, false, true));
ASSERT_TRUE(catcher.GetNextResult());
}
#endif // BUILDFLAG(IS_CHROMEOS)
// Make sure component extensions retain keybindings after removal then
// re-adding.
IN_PROC_BROWSER_TEST_F(CommandsApiTest, AddRemoveAddComponentExtension) {
ASSERT_TRUE(embedded_test_server()->Start());
ASSERT_TRUE(
RunExtensionTest("keybinding/component", {}, {.load_as_component = true}))
<< message_;
extensions::ComponentLoader::Get(browser()->profile())
->Remove("pkplfbidichfdicaijlchgnapepdginl");
ASSERT_TRUE(
RunExtensionTest("keybinding/component", {}, {.load_as_component = true}))
<< message_;
}
// Validate parameters sent along with an extension event, in response to
// command being triggered.
IN_PROC_BROWSER_TEST_F(CommandsApiTest, TabParameter) {
ASSERT_TRUE(embedded_test_server()->Start());
ASSERT_TRUE(RunExtensionTest("keybinding/tab_parameter")) << message_;
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL("/extensions/test_file.txt")));
const Extension* extension = GetSingleLoadedExtension();
ASSERT_TRUE(extension) << message_;
ResultCatcher catcher;
EXPECT_TRUE(content::WaitForLoadStop(
browser()->tab_strip_model()->GetActiveWebContents()));
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_Y, true, true,
false, false)); // Ctrl+Shift+Y
EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
}
// Test Keybinding in incognito mode.
IN_PROC_BROWSER_TEST_P(IncognitoCommandsApiTest, IncognitoMode) {
ASSERT_TRUE(embedded_test_server()->Start());
bool is_incognito_enabled = GetParam();
ASSERT_TRUE(RunExtensionTest("keybinding/basics", {},
{.allow_in_incognito = is_incognito_enabled}))
<< message_;
// Open incognito window and navigate to test page.
Browser* incognito_browser = OpenURLOffTheRecord(
browser()->profile(),
embedded_test_server()->GetURL("/extensions/test_file.html"));
ASSERT_TRUE(ui_test_utils::NavigateToURL(
incognito_browser,
embedded_test_server()->GetURL("/extensions/test_file.txt")));
TestEventRouterObserver test_observer(
EventRouter::Get(incognito_browser->profile()));
// Activate the browser action shortcut (Ctrl+Shift+F).
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(incognito_browser, ui::VKEY_F,
true, true, false, false));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(is_incognito_enabled,
base::Contains(test_observer.dispatched_events(),
"browserAction.onClicked"));
test_observer.ClearEvents();
// Activate the command shortcut (Ctrl+Shift+Y).
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(incognito_browser, ui::VKEY_Y,
true, true, false, false));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(
is_incognito_enabled,
base::Contains(test_observer.dispatched_events(), "commands.onCommand"));
}
IN_PROC_BROWSER_TEST_P(ActionCommandsApiTest,
TriggeringCommandTriggersListener) {
ASSERT_TRUE(embedded_test_server()->Start());
const ActionInfo::Type action_type = GetParam();
// Load a test extension that has a command that invokes the action, and sends
// a message when the action is invoked.
constexpr char kManifestTemplate[] = R"(
{
"name": "Extension Action Listener Test",
"manifest_version": %d,
"version": "0.1",
"commands": {
"%s": {
"suggested_key": {
"default": "Alt+Shift+U"
}
}
},
"%s": {},
"background": { %s }
}
)";
constexpr char kBackgroundScriptTemplate[] = R"(
chrome.%s.onClicked.addListener(() => {
chrome.test.sendMessage('clicked');
});
chrome.test.sendMessage('ready');
)";
const char* background_specification =
action_type == ActionInfo::Type::kAction
? R"("service_worker": "background.js")"
: R"("scripts": ["background.js"])";
TestExtensionDir test_dir;
test_dir.WriteManifest(base::StringPrintf(
kManifestTemplate, GetManifestVersionForActionType(action_type),
GetCommandKeyForActionType(action_type),
ActionInfo::GetManifestKeyForActionType(action_type),
background_specification));
test_dir.WriteFile(FILE_PATH_LITERAL("background.js"),
base::StringPrintf(kBackgroundScriptTemplate,
GetAPINameForActionType(action_type)));
ExtensionTestMessageListener listener("ready");
const Extension* extension = LoadExtension(test_dir.UnpackedPath());
ASSERT_TRUE(extension);
ASSERT_TRUE(listener.WaitUntilSatisfied());
ASSERT_TRUE(HasActiveActionCommand(extension->id(), action_type));
const int tab_id = NavigateToTestURLAndReturnTabId();
// If the action is a page action, it's hidden by default. Show it.
if (action_type == ActionInfo::Type::kPage) {
SetActionVisibleOnTab(profile(), *extension, tab_id);
ASSERT_TRUE(WaitForPageActionVisibilityChangeTo(1));
}
ExtensionTestMessageListener click_listener("clicked");
EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_U, false,
true, true, false));
EXPECT_TRUE(click_listener.WaitUntilSatisfied());
}
// This test validates that commands.getAll() returns commands associated with
// a registered [page/browser] action.
IN_PROC_BROWSER_TEST_P(ActionCommandsApiTest, GetAllReturnsActionCommand) {
ASSERT_TRUE(embedded_test_server()->Start());
const ActionInfo::Type action_type = GetParam();
// Load a test extension that has a command for the current action type.
constexpr char kManifestTemplate[] = R"(
{
"name": "Extension Commands Get All Test",
"manifest_version": %d,
"version": "0.1",
"commands": {
"%s": {
"suggested_key": {
"default": "Ctrl+Shift+5"
}
}
},
"%s": {},
"background": { %s }
}
)";
constexpr char kBackgroundScriptTemplate[] = R"(
var platformBinding =
/Mac/.test(navigator.platform) ? '⇧⌘5' : 'Ctrl+Shift+5';
chrome.commands.getAll(function(commands) {
chrome.test.assertEq(1, commands.length);
chrome.test.assertEq("%s", commands[0].name);
chrome.test.assertEq("", commands[0].description);
chrome.test.assertEq(platformBinding, commands[0].shortcut);
chrome.test.notifyPass();
});
)";
const char* background_specification =
action_type == ActionInfo::Type::kAction
? R"("service_worker": "background.js")"
: R"("scripts": ["background.js"])";
TestExtensionDir test_dir;
test_dir.WriteManifest(base::StringPrintf(
kManifestTemplate, GetManifestVersionForActionType(action_type),
GetCommandKeyForActionType(action_type),
ActionInfo::GetManifestKeyForActionType(action_type),
background_specification));
test_dir.WriteFile(
FILE_PATH_LITERAL("background.js"),
base::StringPrintf(kBackgroundScriptTemplate,
GetCommandKeyForActionType(action_type)));
EXPECT_TRUE(RunExtensionTest(test_dir.UnpackedPath(), {}, {})) << message_;
}
// Tests that triggering a command associated with an action opens an
// extension's popup.
IN_PROC_BROWSER_TEST_P(ActionCommandsApiTest, TriggeringCommandTriggersPopup) {
ASSERT_TRUE(embedded_test_server()->Start());
const ActionInfo::Type action_type = GetParam();
// Load an extension that specifies a command to invoke the action, and has
// a default popup.
constexpr char kManifestTemplate[] = R"(
{
"name": "Extension Action Listener Test",
"manifest_version": %d,
"version": "0.1",
"commands": {
"%s": {
"suggested_key": {
"default": "Alt+Shift+U"
}
}
},
"%s": {"default_popup": "popup.html"}
}
)";
constexpr char kPopupHtml[] = R"(
<!doctype html>
<html>
<script src="popup.js"></script>
</html>
)";
constexpr char kPopupJs[] = "chrome.test.notifyPass();";
TestExtensionDir test_dir;
test_dir.WriteManifest(base::StringPrintf(
kManifestTemplate, GetManifestVersionForActionType(action_type),
GetCommandKeyForActionType(action_type),
ActionInfo::GetManifestKeyForActionType(action_type)));
test_dir.WriteFile(FILE_PATH_LITERAL("popup.html"), kPopupHtml);
test_dir.WriteFile(FILE_PATH_LITERAL("popup.js"), kPopupJs);
const Extension* extension = LoadExtension(test_dir.UnpackedPath());
ASSERT_TRUE(extension);
ASSERT_TRUE(HasActiveActionCommand(extension->id(), action_type));
const int tab_id = NavigateToTestURLAndReturnTabId();
if (action_type == ActionInfo::Type::kPage) {
// Note: We don't use SetActionVisibleOnTab() here because it relies on a
// background page, which this extension doesn't have.
ExtensionActionManager::Get(profile())
->GetExtensionAction(*extension)
->SetIsVisible(tab_id, true);
ASSERT_TRUE(WaitForPageActionVisibilityChangeTo(1));
}
// Invoke the action, and wait for the popup to show.
ResultCatcher catcher;
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_U, false,
true, true, false));
EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
// Verify popup is shown.
ExtensionsToolbarContainer* extensions_container =
browser()->GetBrowserView().toolbar()->extensions_container();
ToolbarActionViewController* popup_owner =
extensions_container->popup_owner_for_testing();
EXPECT_TRUE(popup_owner);
EXPECT_TRUE(popup_owner->GetPopupNativeView());
}
INSTANTIATE_TEST_SUITE_P(All,
ActionCommandsApiTest,
testing::Values(ActionInfo::Type::kBrowser,
ActionInfo::Type::kPage,
ActionInfo::Type::kAction));
INSTANTIATE_TEST_SUITE_P(All, IncognitoCommandsApiTest, testing::Bool());
} // namespace extensions
|