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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/macros.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/extensions/chrome_test_extension_loader.h"
#include "chrome/browser/extensions/extension_action.h"
#include "chrome/browser/extensions/extension_action_manager.h"
#include "chrome/browser/extensions/extension_action_test_util.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/sessions/session_tab_helper.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/test_extension_registry_observer.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/test_extension_dir.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace extensions {
namespace {
const char kDeclarativeContentManifest[] =
"{\n"
" \"name\": \"Declarative Content apitest\",\n"
" \"version\": \"0.1\",\n"
" \"manifest_version\": 2,\n"
" \"description\": \n"
" \"end-to-end browser test for the declarative Content API\",\n"
" \"background\": {\n"
" \"scripts\": [\"background.js\"]\n"
" },\n"
" \"page_action\": {},\n"
" \"permissions\": [\n"
" \"declarativeContent\", \"bookmarks\"\n"
" ],\n"
" \"incognito\": \"spanning\"\n"
"}\n";
const char kIncognitoSpecificBackground[] =
"var PageStateMatcher = chrome.declarativeContent.PageStateMatcher;\n"
"var ShowAction = chrome.declarativeContent.ShowAction;\n"
"var inIncognitoContext = chrome.extension.inIncognitoContext;\n"
"\n"
"var hostPrefix = chrome.extension.inIncognitoContext ?\n"
" 'test_split' : 'test_normal';\n"
"var rule = {\n"
" conditions: [new PageStateMatcher({\n"
" pageUrl: {hostPrefix: hostPrefix}})],\n"
" actions: [new ShowAction()]\n"
"}\n"
"\n"
"var onPageChanged = chrome.declarativeContent.onPageChanged;\n"
"onPageChanged.removeRules(undefined, function() {\n"
" onPageChanged.addRules([rule], function() {\n"
" chrome.test.sendMessage(\n"
" !inIncognitoContext ? \"ready\" : \"ready (split)\");\n"
" });\n"
"});\n";
const char kBackgroundHelpers[] =
"var PageStateMatcher = chrome.declarativeContent.PageStateMatcher;\n"
"var ShowAction = chrome.declarativeContent.ShowAction;\n"
"var onPageChanged = chrome.declarativeContent.onPageChanged;\n"
"var reply = window.domAutomationController.send.bind(\n"
" window.domAutomationController);\n"
"\n"
"function setRules(rules, responseString) {\n"
" onPageChanged.removeRules(undefined, function() {\n"
" onPageChanged.addRules(rules, function() {\n"
" if (chrome.runtime.lastError) {\n"
" reply(chrome.runtime.lastError.message);\n"
" return;\n"
" }\n"
" reply(responseString);\n"
" });\n"
" });\n"
"};\n"
"\n"
"function addRules(rules, responseString) {\n"
" onPageChanged.addRules(rules, function() {\n"
" if (chrome.runtime.lastError) {\n"
" reply(chrome.runtime.lastError.message);\n"
" return;\n"
" }\n"
" reply(responseString);\n"
" });\n"
"};\n"
"\n"
"function removeRule(id, responseString) {\n"
" onPageChanged.removeRules([id], function() {\n"
" if (chrome.runtime.lastError) {\n"
" reply(chrome.runtime.lastError.message);\n"
" return;\n"
" }\n"
" reply(responseString);\n"
" });\n"
"};\n";
class DeclarativeContentApiTest : public ExtensionApiTest {
public:
DeclarativeContentApiTest() {}
protected:
enum IncognitoMode { SPANNING, SPLIT };
// Checks that the rules are correctly evaluated for an extension in incognito
// mode |mode| when the extension's incognito enable state is
// |is_enabled_in_incognito|.
void CheckIncognito(IncognitoMode mode, bool is_enabled_in_incognito);
// Checks that the rules matching a bookmarked state of |is_bookmarked| are
// correctly evaluated on bookmark events.
void CheckBookmarkEvents(bool is_bookmarked);
TestExtensionDir ext_dir_;
private:
DISALLOW_COPY_AND_ASSIGN(DeclarativeContentApiTest);
};
void DeclarativeContentApiTest::CheckIncognito(IncognitoMode mode,
bool is_enabled_in_incognito) {
std::string manifest = kDeclarativeContentManifest;
if (mode == SPLIT) {
base::ReplaceSubstringsAfterOffset(
&manifest, 0, "\"incognito\": \"spanning\"",
"\"incognito\": \"split\"");
ASSERT_NE(kDeclarativeContentManifest, manifest);
}
ext_dir_.WriteManifest(manifest);
ext_dir_.WriteFile(FILE_PATH_LITERAL("background.js"),
kIncognitoSpecificBackground);
ExtensionTestMessageListener ready("ready", false);
ExtensionTestMessageListener ready_incognito("ready (split)", false);
const Extension* extension =
is_enabled_in_incognito ? LoadExtensionIncognito(ext_dir_.UnpackedPath())
: LoadExtension(ext_dir_.UnpackedPath());
ASSERT_TRUE(extension);
Browser* incognito_browser = CreateIncognitoBrowser();
const ExtensionAction* incognito_page_action =
ExtensionActionManager::Get(incognito_browser->profile())->
GetPageAction(*extension);
ASSERT_TRUE(incognito_page_action);
ASSERT_TRUE(ready.WaitUntilSatisfied());
if (is_enabled_in_incognito && mode == SPLIT)
ASSERT_TRUE(ready_incognito.WaitUntilSatisfied());
content::WebContents* const incognito_tab =
incognito_browser->tab_strip_model()->GetWebContentsAt(0);
const int incognito_tab_id = ExtensionTabUtil::GetTabId(incognito_tab);
EXPECT_FALSE(incognito_page_action->GetIsVisible(incognito_tab_id));
NavigateInRenderer(incognito_tab, GURL("http://test_split/"));
if (mode == SPLIT) {
EXPECT_EQ(is_enabled_in_incognito,
incognito_page_action->GetIsVisible(incognito_tab_id));
} else {
EXPECT_FALSE(incognito_page_action->GetIsVisible(incognito_tab_id));
}
NavigateInRenderer(incognito_tab, GURL("http://test_normal/"));
if (mode != SPLIT) {
EXPECT_EQ(is_enabled_in_incognito,
incognito_page_action->GetIsVisible(incognito_tab_id));
} else {
EXPECT_FALSE(incognito_page_action->GetIsVisible(incognito_tab_id));
}
// Verify that everything works as expected in the non-incognito browser.
const ExtensionAction* page_action =
ExtensionActionManager::Get(browser()->profile())->
GetPageAction(*extension);
content::WebContents* const tab =
browser()->tab_strip_model()->GetWebContentsAt(0);
const int tab_id = ExtensionTabUtil::GetTabId(tab);
EXPECT_FALSE(page_action->GetIsVisible(tab_id));
NavigateInRenderer(tab, GURL("http://test_normal/"));
EXPECT_TRUE(page_action->GetIsVisible(tab_id));
NavigateInRenderer(tab, GURL("http://test_split/"));
EXPECT_FALSE(page_action->GetIsVisible(tab_id));
}
void DeclarativeContentApiTest::CheckBookmarkEvents(bool match_is_bookmarked) {
ext_dir_.WriteManifest(kDeclarativeContentManifest);
ext_dir_.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundHelpers);
content::WebContents* const tab =
browser()->tab_strip_model()->GetWebContentsAt(0);
const int tab_id = ExtensionTabUtil::GetTabId(tab);
const Extension* extension = LoadExtension(ext_dir_.UnpackedPath());
ASSERT_TRUE(extension);
const ExtensionAction* page_action = ExtensionActionManager::Get(
browser()->profile())->GetPageAction(*extension);
ASSERT_TRUE(page_action);
NavigateInRenderer(tab, GURL("http://test1/"));
EXPECT_FALSE(page_action->GetIsVisible(tab_id));
static const char kSetIsBookmarkedRule[] =
"setRules([{\n"
" conditions: [new PageStateMatcher({isBookmarked: %s})],\n"
" actions: [new ShowAction()]\n"
"}], 'test_rule');\n";
EXPECT_EQ("test_rule", ExecuteScriptInBackgroundPage(
extension->id(),
base::StringPrintf(kSetIsBookmarkedRule,
match_is_bookmarked ? "true" : "false")));
EXPECT_EQ(!match_is_bookmarked, page_action->GetIsVisible(tab_id));
// Check rule evaluation on add/remove bookmark.
bookmarks::BookmarkModel* bookmark_model =
BookmarkModelFactory::GetForBrowserContext(browser()->profile());
const bookmarks::BookmarkNode* node =
bookmark_model->AddURL(bookmark_model->other_node(), 0,
base::ASCIIToUTF16("title"),
GURL("http://test1/"));
EXPECT_EQ(match_is_bookmarked, page_action->GetIsVisible(tab_id));
bookmark_model->Remove(node);
EXPECT_EQ(!match_is_bookmarked, page_action->GetIsVisible(tab_id));
// Check rule evaluation on navigate to bookmarked and non-bookmarked URL.
bookmark_model->AddURL(bookmark_model->other_node(), 0,
base::ASCIIToUTF16("title"),
GURL("http://test2/"));
NavigateInRenderer(tab, GURL("http://test2/"));
EXPECT_EQ(match_is_bookmarked, page_action->GetIsVisible(tab_id));
NavigateInRenderer(tab, GURL("http://test3/"));
EXPECT_EQ(!match_is_bookmarked, page_action->GetIsVisible(tab_id));
}
// Disabled due to flake. https://crbug.com/606574.
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest, DISABLED_Overview) {
ext_dir_.WriteManifest(kDeclarativeContentManifest);
ext_dir_.WriteFile(
FILE_PATH_LITERAL("background.js"),
"var declarative = chrome.declarative;\n"
"\n"
"var PageStateMatcher = chrome.declarativeContent.PageStateMatcher;\n"
"var ShowAction = chrome.declarativeContent.ShowAction;\n"
"\n"
"var rule = {\n"
" conditions: [new PageStateMatcher({\n"
" pageUrl: {hostPrefix: \"test1\"}}),\n"
" new PageStateMatcher({\n"
" css: [\"input[type='password']\"]})],\n"
" actions: [new ShowAction()]\n"
"}\n"
"\n"
"var testEvent = chrome.declarativeContent.onPageChanged;\n"
"\n"
"testEvent.removeRules(undefined, function() {\n"
" testEvent.addRules([rule], function() {\n"
" chrome.test.sendMessage(\"ready\")\n"
" });\n"
"});\n");
ExtensionTestMessageListener ready("ready", false);
const Extension* extension = LoadExtension(ext_dir_.UnpackedPath());
ASSERT_TRUE(extension);
const ExtensionAction* page_action =
ExtensionActionManager::Get(browser()->profile())->
GetPageAction(*extension);
ASSERT_TRUE(page_action);
ASSERT_TRUE(ready.WaitUntilSatisfied());
content::WebContents* const tab =
browser()->tab_strip_model()->GetWebContentsAt(0);
const int tab_id = ExtensionTabUtil::GetTabId(tab);
NavigateInRenderer(tab, GURL("http://test1/"));
// The declarative API should show the page action instantly, rather
// than waiting for the extension to run.
EXPECT_TRUE(page_action->GetIsVisible(tab_id));
// Make sure leaving a matching page unshows the page action.
NavigateInRenderer(tab, GURL("http://not_checked/"));
EXPECT_FALSE(page_action->GetIsVisible(tab_id));
// Insert a password field to make sure that's noticed.
// Notice that we touch offsetTop to force a synchronous layout.
ASSERT_TRUE(content::ExecuteScript(
tab, "document.body.innerHTML = '<input type=\"password\">';"
"document.body.offsetTop;"));
// Give the style match a chance to run and send back the matching-selector
// update.
ASSERT_TRUE(content::ExecuteScript(tab, std::string()));
EXPECT_TRUE(page_action->GetIsVisible(tab_id))
<< "Adding a matching element should show the page action.";
// Remove it again to make sure that reverts the action.
// Notice that we touch offsetTop to force a synchronous layout.
ASSERT_TRUE(content::ExecuteScript(
tab, "document.body.innerHTML = 'Hello world';"
"document.body.offsetTop;"));
// Give the style match a chance to run and send back the matching-selector
// update.
ASSERT_TRUE(content::ExecuteScript(tab, std::string()));
EXPECT_FALSE(page_action->GetIsVisible(tab_id))
<< "Removing the matching element should hide the page action again.";
}
// Test that adds two rules pointing to single action instance.
// Regression test for http://crbug.com/574149.
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest, ReusedActionInstance) {
ext_dir_.WriteManifest(kDeclarativeContentManifest);
ext_dir_.WriteFile(
FILE_PATH_LITERAL("background.js"),
"var declarative = chrome.declarative;\n"
"\n"
"var PageStateMatcher = chrome.declarativeContent.PageStateMatcher;\n"
"var ShowAction = chrome.declarativeContent.ShowAction;\n"
"var actionInstance = new ShowAction();\n"
"\n"
"var rule1 = {\n"
" conditions: [new PageStateMatcher({\n"
" pageUrl: {hostPrefix: \"test1\"}})],\n"
" actions: [actionInstance]\n"
"};\n"
"var rule2 = {\n"
" conditions: [new PageStateMatcher({\n"
" pageUrl: {hostPrefix: \"test\"}})],\n"
" actions: [actionInstance]\n"
"};\n"
"\n"
"var testEvent = chrome.declarativeContent.onPageChanged;\n"
"\n"
"testEvent.removeRules(undefined, function() {\n"
" testEvent.addRules([rule1, rule2], function() {\n"
" chrome.test.sendMessage(\"ready\");\n"
" });\n"
"});\n");
ExtensionTestMessageListener ready("ready", false);
const Extension* extension = LoadExtension(ext_dir_.UnpackedPath());
ASSERT_TRUE(extension);
// Wait for declarative rules to be set up.
content::BrowserContext::GetDefaultStoragePartition(profile())
->FlushNetworkInterfaceForTesting();
const ExtensionAction* page_action =
ExtensionActionManager::Get(browser()->profile())
->GetPageAction(*extension);
ASSERT_TRUE(page_action);
ASSERT_TRUE(ready.WaitUntilSatisfied());
content::WebContents* const tab =
browser()->tab_strip_model()->GetWebContentsAt(0);
const int tab_id = ExtensionTabUtil::GetTabId(tab);
// This navigation matches both rules.
NavigateInRenderer(tab, GURL("http://test1/"));
EXPECT_TRUE(page_action->GetIsVisible(tab_id));
}
// Tests that the rules are evaluated at the time they are added or removed.
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest, RulesEvaluatedOnAddRemove) {
ext_dir_.WriteManifest(kDeclarativeContentManifest);
ext_dir_.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundHelpers);
const Extension* extension = LoadExtension(ext_dir_.UnpackedPath());
ASSERT_TRUE(extension);
const ExtensionAction* page_action =
ExtensionActionManager::Get(browser()->profile())->
GetPageAction(*extension);
ASSERT_TRUE(page_action);
content::WebContents* const tab =
browser()->tab_strip_model()->GetWebContentsAt(0);
const int tab_id = ExtensionTabUtil::GetTabId(tab);
NavigateInRenderer(tab, GURL("http://test1/"));
const std::string kAddTestRules =
"addRules([{\n"
" id: '1',\n"
" conditions: [new PageStateMatcher({\n"
" pageUrl: {hostPrefix: \"test1\"}})],\n"
" actions: [new ShowAction()]\n"
"}, {\n"
" id: '2',\n"
" conditions: [new PageStateMatcher({\n"
" pageUrl: {hostPrefix: \"test2\"}})],\n"
" actions: [new ShowAction()]\n"
"}], 'add_rules');\n";
EXPECT_EQ("add_rules",
ExecuteScriptInBackgroundPage(extension->id(), kAddTestRules));
EXPECT_TRUE(page_action->GetIsVisible(tab_id));
const std::string kRemoveTestRule1 = "removeRule('1', 'remove_rule1');\n";
EXPECT_EQ("remove_rule1",
ExecuteScriptInBackgroundPage(extension->id(), kRemoveTestRule1));
EXPECT_FALSE(page_action->GetIsVisible(tab_id));
NavigateInRenderer(tab, GURL("http://test2/"));
EXPECT_TRUE(page_action->GetIsVisible(tab_id));
const std::string kRemoveTestRule2 = "removeRule('2', 'remove_rule2');\n";
EXPECT_EQ("remove_rule2",
ExecuteScriptInBackgroundPage(extension->id(), kRemoveTestRule2));
EXPECT_FALSE(page_action->GetIsVisible(tab_id));
}
class ParameterizedShowActionDeclarativeContentApiTest
: public DeclarativeContentApiTest,
public testing::WithParamInterface<const char*> {
public:
ParameterizedShowActionDeclarativeContentApiTest() {}
~ParameterizedShowActionDeclarativeContentApiTest() override {}
void TestShowAction(base::Optional<ActionInfo::Type> action_type);
private:
DISALLOW_COPY_AND_ASSIGN(ParameterizedShowActionDeclarativeContentApiTest);
};
void ParameterizedShowActionDeclarativeContentApiTest::TestShowAction(
base::Optional<ActionInfo::Type> action_type) {
std::string manifest_with_custom_action = kDeclarativeContentManifest;
std::string action_key;
if (action_type) {
switch (*action_type) {
case ActionInfo::TYPE_BROWSER:
action_key = R"("browser_action": {},)";
break;
case ActionInfo::TYPE_PAGE:
action_key = R"("page_action": {},)";
break;
case ActionInfo::TYPE_SYSTEM_INDICATOR:
NOTREACHED();
}
}
base::ReplaceSubstringsAfterOffset(&manifest_with_custom_action, 0,
"\"page_action\": {},", action_key);
ext_dir_.WriteManifest(manifest_with_custom_action);
ext_dir_.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundHelpers);
ChromeTestExtensionLoader loader(profile());
scoped_refptr<const Extension> extension =
loader.LoadExtension(ext_dir_.UnpackedPath());
ASSERT_TRUE(extension);
// Wait for declarative rules to be set up.
content::BrowserContext::GetDefaultStoragePartition(profile())
->FlushNetworkInterfaceForTesting();
ExtensionAction* action = ExtensionActionManager::Get(browser()->profile())
->GetExtensionAction(*extension);
// Extensions that don't provide an action are given a page action by default
// (for visibility reasons).
ASSERT_TRUE(action);
// Ensure browser actions are hidden (so that the ShowAction() rule has an
// effect).
bool has_browser_action =
action_type && *action_type == ActionInfo::TYPE_BROWSER;
if (has_browser_action)
action->SetIsVisible(ExtensionAction::kDefaultTabId, false);
const char kScript[] =
"setRules([{\n"
" conditions: [new PageStateMatcher({\n"
" pageUrl: {hostPrefix: \"test\"}})],\n"
" actions: [new chrome.declarativeContent.%s()]\n"
"}], 'test_rule');\n";
const char kSuccessStr[] = "test_rule";
std::string result = ExecuteScriptInBackgroundPage(
extension->id(), base::StringPrintf(kScript, GetParam()));
// Since extensions with no action provided are given a page action by default
// (for visibility reasons) and ShowAction() should also work with
// browser actions, both of these should pass.
EXPECT_THAT(result, testing::HasSubstr(kSuccessStr));
content::WebContents* const tab =
browser()->tab_strip_model()->GetWebContentsAt(0);
NavigateInRenderer(tab, GURL("http://test/"));
const int tab_id = SessionTabHelper::IdForTab(tab).id();
EXPECT_TRUE(action->GetIsVisible(tab_id));
// Even an extension with no specified action with have a (synthesized) page
// action. Only browser action extensions are expected to not have a page
// action.
bool expect_page_action = !has_browser_action;
ExtensionAction* page_action =
ExtensionActionManager::Get(browser()->profile())
->GetPageAction(*extension);
EXPECT_EQ(expect_page_action, page_action != nullptr);
EXPECT_EQ(expect_page_action ? 1u : 0u,
extension_action_test_util::GetVisiblePageActionCount(tab));
}
IN_PROC_BROWSER_TEST_P(ParameterizedShowActionDeclarativeContentApiTest,
NoActionInManifest) {
TestShowAction(base::nullopt);
}
IN_PROC_BROWSER_TEST_P(ParameterizedShowActionDeclarativeContentApiTest,
PageActionInManifest) {
TestShowAction(ActionInfo::TYPE_PAGE);
}
IN_PROC_BROWSER_TEST_P(ParameterizedShowActionDeclarativeContentApiTest,
BrowserActionInManifest) {
TestShowAction(ActionInfo::TYPE_BROWSER);
}
// Tests that rules from manifest are added and evaluated properly.
IN_PROC_BROWSER_TEST_P(ParameterizedShowActionDeclarativeContentApiTest,
RulesAddedFromManifest) {
const char manifest[] =
"{\n"
" \"name\": \"Declarative Content apitest\",\n"
" \"version\": \"0.1\",\n"
" \"manifest_version\": 2,\n"
" \"page_action\": {},\n"
" \"permissions\": [\n"
" \"declarativeContent\"\n"
" ],\n"
" \"event_rules\": [{\n"
" \"event\": \"declarativeContent.onPageChanged\",\n"
" \"actions\": [{\n"
" \"type\": \"declarativeContent.%s\"\n"
" }],\n"
" \"conditions\": [{\n"
" \"type\": \"declarativeContent.PageStateMatcher\",\n"
" \"pageUrl\": {\"hostPrefix\": \"test1\"}\n"
" }]\n"
" }]\n"
"}\n";
ext_dir_.WriteManifest(base::StringPrintf(manifest, GetParam()));
const Extension* extension = LoadExtension(ext_dir_.UnpackedPath());
ASSERT_TRUE(extension);
const ExtensionAction* page_action =
ExtensionActionManager::Get(browser()->profile())
->GetPageAction(*extension);
ASSERT_TRUE(page_action);
content::WebContents* const tab =
browser()->tab_strip_model()->GetWebContentsAt(0);
const int tab_id = ExtensionTabUtil::GetTabId(tab);
NavigateInRenderer(tab, GURL("http://blank/"));
EXPECT_FALSE(page_action->GetIsVisible(tab_id));
NavigateInRenderer(tab, GURL("http://test1/"));
EXPECT_TRUE(page_action->GetIsVisible(tab_id));
NavigateInRenderer(tab, GURL("http://test2/"));
EXPECT_FALSE(page_action->GetIsVisible(tab_id));
}
INSTANTIATE_TEST_CASE_P(LegacyShowActionKey,
ParameterizedShowActionDeclarativeContentApiTest,
::testing::Values("ShowPageAction"));
INSTANTIATE_TEST_CASE_P(ModernShowActionKey,
ParameterizedShowActionDeclarativeContentApiTest,
::testing::Values("ShowAction"));
// Tests that rules are not evaluated in incognito browser windows when the
// extension specifies spanning incognito mode but is not enabled for incognito.
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest,
DisabledForSpanningIncognito) {
CheckIncognito(SPANNING, false);
}
// Tests that rules are evaluated in incognito browser windows when the
// extension specifies spanning incognito mode and is enabled for incognito.
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest, EnabledForSpanningIncognito) {
CheckIncognito(SPANNING, true);
}
// Tests that rules are not evaluated in incognito browser windows when the
// extension specifies split incognito mode but is not enabled for incognito.
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest, DisabledForSplitIncognito) {
CheckIncognito(SPLIT, false);
}
// Tests that rules are evaluated in incognito browser windows when the
// extension specifies split incognito mode and is enabled for incognito.
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest, EnabledForSplitIncognito) {
CheckIncognito(SPLIT, true);
}
// Tests that rules are evaluated for an incognito tab that exists at the time
// the rules are added.
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest,
RulesEvaluatedForExistingIncognitoTab) {
Browser* incognito_browser = CreateIncognitoBrowser();
content::WebContents* const incognito_tab =
incognito_browser->tab_strip_model()->GetWebContentsAt(0);
const int incognito_tab_id = ExtensionTabUtil::GetTabId(incognito_tab);
NavigateInRenderer(incognito_tab, GURL("http://test_normal/"));
ext_dir_.WriteManifest(kDeclarativeContentManifest);
ext_dir_.WriteFile(FILE_PATH_LITERAL("background.js"),
kIncognitoSpecificBackground);
ExtensionTestMessageListener ready("ready", false);
const Extension* extension = LoadExtensionIncognito(ext_dir_.UnpackedPath());
ASSERT_TRUE(extension);
ASSERT_TRUE(ready.WaitUntilSatisfied());
const ExtensionAction* incognito_page_action =
ExtensionActionManager::Get(incognito_browser->profile())->
GetPageAction(*extension);
ASSERT_TRUE(incognito_page_action);
// The page action should be shown.
EXPECT_TRUE(incognito_page_action->GetIsVisible(incognito_tab_id));
}
// Sets up rules matching http://test1/ in a normal and incognito browser.
// Frequently times out on ChromiumOS, Linux ASan, and Windows:
// https://crbug.com/512431.
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest,
DISABLED_PRE_RulesPersistence) {
ExtensionTestMessageListener ready("ready", false);
ExtensionTestMessageListener ready_split("ready (split)", false);
// An on-disk extension is required so that it can be reloaded later in the
// RulesPersistence test.
const Extension* extension =
LoadExtensionIncognito(test_data_dir_.AppendASCII("declarative_content")
.AppendASCII("persistence"));
ASSERT_TRUE(extension);
ASSERT_TRUE(ready.WaitUntilSatisfied());
CreateIncognitoBrowser();
ASSERT_TRUE(ready_split.WaitUntilSatisfied());
}
// Reloads the extension from PRE_RulesPersistence and checks that the rules
// continue to work as expected after being persisted and reloaded.
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest, DISABLED_RulesPersistence) {
ExtensionTestMessageListener ready("second run ready", false);
ExtensionTestMessageListener ready_split("second run ready (split)", false);
ASSERT_TRUE(ready.WaitUntilSatisfied());
ExtensionRegistry* registry = ExtensionRegistry::Get(profile());
const Extension* extension =
GetExtensionByPath(registry->enabled_extensions(),
test_data_dir_.AppendASCII("declarative_content")
.AppendASCII("persistence"));
// Check non-incognito browser.
content::WebContents* const tab =
browser()->tab_strip_model()->GetWebContentsAt(0);
const int tab_id = ExtensionTabUtil::GetTabId(tab);
const ExtensionAction* page_action =
ExtensionActionManager::Get(browser()->profile())->
GetPageAction(*extension);
ASSERT_TRUE(page_action);
EXPECT_FALSE(page_action->GetIsVisible(tab_id));
NavigateInRenderer(tab, GURL("http://test_normal/"));
EXPECT_TRUE(page_action->GetIsVisible(tab_id));
NavigateInRenderer(tab, GURL("http://test_split/"));
EXPECT_FALSE(page_action->GetIsVisible(tab_id));
// Check incognito browser.
Browser* incognito_browser = CreateIncognitoBrowser();
ASSERT_TRUE(ready_split.WaitUntilSatisfied());
content::WebContents* const incognito_tab =
incognito_browser->tab_strip_model()->GetWebContentsAt(0);
const int incognito_tab_id = ExtensionTabUtil::GetTabId(incognito_tab);
const ExtensionAction* incognito_page_action =
ExtensionActionManager::Get(incognito_browser->profile())->
GetPageAction(*extension);
ASSERT_TRUE(incognito_page_action);
NavigateInRenderer(incognito_tab, GURL("http://test_split/"));
EXPECT_TRUE(incognito_page_action->GetIsVisible(incognito_tab_id));
NavigateInRenderer(incognito_tab, GURL("http://test_normal/"));
EXPECT_FALSE(incognito_page_action->GetIsVisible(incognito_tab_id));
}
// http://crbug.com/304373
#if defined(OS_WIN)
// Fails on XP: http://crbug.com/515717
#define MAYBE_UninstallWhileActivePageAction \
DISABLED_UninstallWhileActivePageAction
#else
#define MAYBE_UninstallWhileActivePageAction UninstallWhileActivePageAction
#endif
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest,
MAYBE_UninstallWhileActivePageAction) {
ext_dir_.WriteManifest(kDeclarativeContentManifest);
ext_dir_.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundHelpers);
const Extension* extension = LoadExtension(ext_dir_.UnpackedPath());
ASSERT_TRUE(extension);
// Wait for declarative rules to be set up.
content::BrowserContext::GetDefaultStoragePartition(profile())
->FlushNetworkInterfaceForTesting();
const std::string extension_id = extension->id();
const ExtensionAction* page_action = ExtensionActionManager::Get(
browser()->profile())->GetPageAction(*extension);
ASSERT_TRUE(page_action);
const std::string kTestRule =
"setRules([{\n"
" conditions: [new PageStateMatcher({\n"
" pageUrl: {hostPrefix: \"test\"}})],\n"
" actions: [new ShowAction()]\n"
"}], 'test_rule');\n";
EXPECT_EQ("test_rule",
ExecuteScriptInBackgroundPage(extension_id, kTestRule));
content::WebContents* const tab =
browser()->tab_strip_model()->GetWebContentsAt(0);
const int tab_id = ExtensionTabUtil::GetTabId(tab);
NavigateInRenderer(tab, GURL("http://test/"));
EXPECT_TRUE(page_action->GetIsVisible(tab_id));
EXPECT_TRUE(WaitForPageActionVisibilityChangeTo(1));
EXPECT_EQ(1u, extension_action_test_util::GetVisiblePageActionCount(tab));
EXPECT_EQ(1u, extension_action_test_util::GetTotalPageActionCount(tab));
ReloadExtension(extension_id); // Invalidates page_action and extension.
// Wait for declarative rules to be removed.
content::BrowserContext::GetDefaultStoragePartition(profile())
->FlushNetworkInterfaceForTesting();
EXPECT_EQ("test_rule",
ExecuteScriptInBackgroundPage(extension_id, kTestRule));
// TODO(jyasskin): Apply new rules to existing tabs, without waiting for a
// navigation.
NavigateInRenderer(tab, GURL("http://test/"));
EXPECT_TRUE(WaitForPageActionVisibilityChangeTo(1));
EXPECT_EQ(1u, extension_action_test_util::GetVisiblePageActionCount(tab));
EXPECT_EQ(1u, extension_action_test_util::GetTotalPageActionCount(tab));
UnloadExtension(extension_id);
// Wait for declarative rules to be removed.
content::BrowserContext::GetDefaultStoragePartition(profile())
->FlushNetworkInterfaceForTesting();
NavigateInRenderer(tab, GURL("http://test/"));
EXPECT_TRUE(WaitForPageActionVisibilityChangeTo(0));
EXPECT_EQ(0u, extension_action_test_util::GetVisiblePageActionCount(tab));
EXPECT_EQ(0u, extension_action_test_util::GetTotalPageActionCount(tab));
}
// This tests against a renderer crash that was present during development.
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest,
DISABLED_AddExtensionMatchingExistingTabWithDeadFrames) {
ext_dir_.WriteManifest(kDeclarativeContentManifest);
ext_dir_.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundHelpers);
content::WebContents* const tab =
browser()->tab_strip_model()->GetWebContentsAt(0);
const int tab_id = ExtensionTabUtil::GetTabId(tab);
ASSERT_TRUE(content::ExecuteScript(
tab, "document.body.innerHTML = '<iframe src=\"http://test2\">';"));
// Replace the iframe to destroy its WebFrame.
ASSERT_TRUE(content::ExecuteScript(
tab, "document.body.innerHTML = '<span class=\"foo\">';"));
const Extension* extension = LoadExtension(ext_dir_.UnpackedPath());
ASSERT_TRUE(extension);
const ExtensionAction* page_action = ExtensionActionManager::Get(
browser()->profile())->GetPageAction(*extension);
ASSERT_TRUE(page_action);
EXPECT_FALSE(page_action->GetIsVisible(tab_id));
EXPECT_EQ("rule0", ExecuteScriptInBackgroundPage(
extension->id(),
"setRules([{\n"
" conditions: [new PageStateMatcher({\n"
" css: [\"span[class=foo]\"]})],\n"
" actions: [new ShowAction()]\n"
"}], 'rule0');\n"));
// Give the renderer a chance to apply the rules change and notify the
// browser. This takes one time through the Blink message loop to receive
// the rule change and apply the new stylesheet, and a second to dedupe the
// update.
ASSERT_TRUE(content::ExecuteScript(tab, std::string()));
ASSERT_TRUE(content::ExecuteScript(tab, std::string()));
EXPECT_FALSE(tab->IsCrashed());
EXPECT_TRUE(page_action->GetIsVisible(tab_id))
<< "Loading an extension when an open page matches its rules "
<< "should show the page action.";
EXPECT_EQ("removed",
ExecuteScriptInBackgroundPage(
extension->id(),
"onPageChanged.removeRules(undefined, function() {\n"
" window.domAutomationController.send('removed');\n"
"});\n"));
EXPECT_FALSE(page_action->GetIsVisible(tab_id));
}
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest,
CanonicalizesPageStateMatcherCss) {
ext_dir_.WriteManifest(kDeclarativeContentManifest);
ext_dir_.WriteFile(
FILE_PATH_LITERAL("background.js"),
"var PageStateMatcher = chrome.declarativeContent.PageStateMatcher;\n"
"function Return(obj) {\n"
" window.domAutomationController.send('' + obj);\n"
"}\n");
const Extension* extension = LoadExtension(ext_dir_.UnpackedPath());
ASSERT_TRUE(extension);
EXPECT_EQ("input[type=\"password\"]",
ExecuteScriptInBackgroundPage(
extension->id(),
"var psm = new PageStateMatcher(\n"
" {css: [\"input[type='password']\"]});\n"
"Return(psm.css);"));
EXPECT_THAT(ExecuteScriptInBackgroundPage(
extension->id(),
"try {\n"
" new PageStateMatcher({css: 'Not-an-array'});\n"
" Return('Failed to throw');\n"
"} catch (e) {\n"
" Return(e.message);\n"
"}\n"),
testing::ContainsRegex("css.*xpected '?array'?"));
EXPECT_THAT(ExecuteScriptInBackgroundPage(
extension->id(),
"try {\n"
" new PageStateMatcher({css: [null]});\n" // Not a string.
" Return('Failed to throw');\n"
"} catch (e) {\n"
" Return(e.message);\n"
"}\n"),
testing::ContainsRegex("css.*0.*xpected '?string'?"));
EXPECT_THAT(ExecuteScriptInBackgroundPage(
extension->id(),
"try {\n"
// Invalid CSS:
" new PageStateMatcher({css: [\"input''\"]});\n"
" Return('Failed to throw');\n"
"} catch (e) {\n"
" Return(e.message);\n"
"}\n"),
testing::ContainsRegex("valid.*: input''$"));
EXPECT_THAT(ExecuteScriptInBackgroundPage(
extension->id(),
"try {\n"
// "Complex" selector:
" new PageStateMatcher({css: ['div input']});\n"
" Return('Failed to throw');\n"
"} catch (e) {\n"
" Return(e.message);\n"
"}\n"),
testing::ContainsRegex("selector.*: div input$"));
}
// Tests that the rules with isBookmarked: true are evaluated when handling
// bookmarking events.
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest,
IsBookmarkedRulesEvaluatedOnBookmarkEvents) {
CheckBookmarkEvents(true);
}
// Tests that the rules with isBookmarked: false are evaluated when handling
// bookmarking events.
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest,
NotBookmarkedRulesEvaluatedOnBookmarkEvents) {
CheckBookmarkEvents(false);
}
// https://crbug.com/497586
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest,
WebContentsWithoutTabAddedNotificationAtOnLoaded) {
// Add a web contents to the tab strip in a way that doesn't trigger
// NOTIFICATION_TAB_ADDED.
std::unique_ptr<content::WebContents> contents = content::WebContents::Create(
content::WebContents::CreateParams(profile()));
browser()->tab_strip_model()->AppendWebContents(std::move(contents), false);
// The actual extension contents don't matter here -- we're just looking to
// trigger OnExtensionLoaded.
ext_dir_.WriteManifest(kDeclarativeContentManifest);
ext_dir_.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundHelpers);
ASSERT_TRUE(LoadExtension(ext_dir_.UnpackedPath()));
}
// https://crbug.com/501225
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest,
PendingWebContentsClearedOnRemoveRules) {
ext_dir_.WriteManifest(kDeclarativeContentManifest);
ext_dir_.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundHelpers);
const Extension* extension = LoadExtension(ext_dir_.UnpackedPath());
ASSERT_TRUE(extension);
const ExtensionAction* page_action = ExtensionActionManager::Get(
browser()->profile())->GetPageAction(*extension);
ASSERT_TRUE(page_action);
// Create two tabs.
content::WebContents* const tab1 =
browser()->tab_strip_model()->GetWebContentsAt(0);
AddTabAtIndex(1, GURL("http://test2/"), ui::PAGE_TRANSITION_LINK);
content::WebContents* tab2 =
browser()->tab_strip_model()->GetWebContentsAt(1);
// Add a rule matching the second tab.
const std::string kAddTestRules =
"addRules([{\n"
" id: '1',\n"
" conditions: [new PageStateMatcher({\n"
" pageUrl: {hostPrefix: \"test1\"}})],\n"
" actions: [new ShowAction()]\n"
"}, {\n"
" id: '2',\n"
" conditions: [new PageStateMatcher({\n"
" pageUrl: {hostPrefix: \"test2\"}})],\n"
" actions: [new ShowAction()]\n"
"}], 'add_rules');\n";
EXPECT_EQ("add_rules",
ExecuteScriptInBackgroundPage(extension->id(), kAddTestRules));
EXPECT_TRUE(page_action->GetIsVisible(ExtensionTabUtil::GetTabId(tab2)));
// Remove the rule.
const std::string kRemoveTestRule1 = "removeRule('2', 'remove_rule1');\n";
EXPECT_EQ("remove_rule1",
ExecuteScriptInBackgroundPage(extension->id(), kRemoveTestRule1));
// Remove the second tab, then trigger a rule evaluation for the remaining
// tab.
browser()->tab_strip_model()->DetachWebContentsAt(
browser()->tab_strip_model()->GetIndexOfWebContents(tab2));
NavigateInRenderer(tab1, GURL("http://test1/"));
EXPECT_TRUE(page_action->GetIsVisible(ExtensionTabUtil::GetTabId(tab1)));
}
// https://crbug.com/517492
#if defined(OS_WIN)
// Fails on XP: http://crbug.com/515717
#define MAYBE_RemoveAllRulesAfterExtensionUninstall \
DISABLED_RemoveAllRulesAfterExtensionUninstall
#else
#define MAYBE_RemoveAllRulesAfterExtensionUninstall \
RemoveAllRulesAfterExtensionUninstall
#endif
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest,
MAYBE_RemoveAllRulesAfterExtensionUninstall) {
ext_dir_.WriteManifest(kDeclarativeContentManifest);
ext_dir_.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundHelpers);
// Load the extension, add a rule, then uninstall the extension.
const Extension* extension = LoadExtension(ext_dir_.UnpackedPath());
ASSERT_TRUE(extension);
const std::string kAddTestRule =
"addRules([{\n"
" id: '1',\n"
" conditions: [],\n"
" actions: [new ShowAction()]\n"
"}], 'add_rule');\n";
EXPECT_EQ("add_rule",
ExecuteScriptInBackgroundPage(extension->id(), kAddTestRule));
ExtensionService* extension_service = extensions::ExtensionSystem::Get(
browser()->profile())->extension_service();
base::string16 error;
ASSERT_TRUE(extension_service->UninstallExtension(
extension->id(),
UNINSTALL_REASON_FOR_TESTING,
&error));
ASSERT_EQ(base::ASCIIToUTF16(""), error);
// Reload the extension, then add and remove a rule.
extension = LoadExtension(ext_dir_.UnpackedPath());
ASSERT_TRUE(extension);
EXPECT_EQ("add_rule",
ExecuteScriptInBackgroundPage(extension->id(), kAddTestRule));
const std::string kRemoveTestRule1 = "removeRule('1', 'remove_rule1');\n";
EXPECT_EQ("remove_rule1",
ExecuteScriptInBackgroundPage(extension->id(), kRemoveTestRule1));
}
// TODO(wittman): Once ChromeContentRulesRegistry operates on condition and
// action interfaces, add a test that checks that a navigation always evaluates
// consistent URL state for all conditions. i.e.: if condition1 evaluates to
// false on url0 and true on url1, and condition2 evaluates to true on url0 and
// false on url1, navigate from url0 to url1 and validate that no action is
// triggered. Do the same when navigating back to url0. This kind of test is
// unfortunately not feasible with the current implementation and the existing
// supported conditions and actions.
} // namespace
} // namespace extensions
|