| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 11 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 12 #include "chrome/browser/extensions/chrome_test_extension_loader.h" |
| 12 #include "chrome/browser/extensions/extension_action.h" | 13 #include "chrome/browser/extensions/extension_action.h" |
| 13 #include "chrome/browser/extensions/extension_action_manager.h" | 14 #include "chrome/browser/extensions/extension_action_manager.h" |
| 14 #include "chrome/browser/extensions/extension_action_test_util.h" | 15 #include "chrome/browser/extensions/extension_action_test_util.h" |
| 15 #include "chrome/browser/extensions/extension_apitest.h" | 16 #include "chrome/browser/extensions/extension_apitest.h" |
| 16 #include "chrome/browser/extensions/extension_service.h" | 17 #include "chrome/browser/extensions/extension_service.h" |
| 17 #include "chrome/browser/extensions/extension_tab_util.h" | 18 #include "chrome/browser/extensions/extension_tab_util.h" |
| 18 #include "chrome/browser/extensions/test_extension_dir.h" | 19 #include "chrome/browser/extensions/test_extension_dir.h" |
| 19 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
| 20 #include "chrome/browser/ui/browser_window.h" | 21 #include "chrome/browser/ui/browser_window.h" |
| 21 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 22 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| (...skipping 676 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 698 ExecuteScriptInBackgroundPage( | 699 ExecuteScriptInBackgroundPage( |
| 699 extension->id(), | 700 extension->id(), |
| 700 "onPageChanged.removeRules(undefined, function() {\n" | 701 "onPageChanged.removeRules(undefined, function() {\n" |
| 701 " window.domAutomationController.send('removed');\n" | 702 " window.domAutomationController.send('removed');\n" |
| 702 "});\n")); | 703 "});\n")); |
| 703 EXPECT_FALSE(page_action->GetIsVisible(tab_id)); | 704 EXPECT_FALSE(page_action->GetIsVisible(tab_id)); |
| 704 } | 705 } |
| 705 | 706 |
| 706 namespace { | 707 namespace { |
| 707 | 708 |
| 708 // A base class that will test using the show page action capability of | 709 class ShowPageActionWithoutPageActionTest |
| 709 // declarative content when no page action is in the extension's manifest. | 710 : public DeclarativeContentApiTest, |
| 710 // With the toolbar action redesign on, this succeeds; without it, it fails. | 711 public testing::WithParamInterface<bool> {}; |
| 711 // Abstracted out into a base class because the value of the switch needs to be | |
| 712 // set during initialization. | |
| 713 class ShowPageActionWithoutPageActionTest : public DeclarativeContentApiTest { | |
| 714 protected: | |
| 715 explicit ShowPageActionWithoutPageActionTest(bool enable_redesign) | |
| 716 : enable_redesign_(enable_redesign) {} | |
| 717 ~ShowPageActionWithoutPageActionTest() override {} | |
| 718 | |
| 719 void RunTest() { | |
| 720 // Load an extension without a page action. | |
| 721 std::string manifest_without_page_action = kDeclarativeContentManifest; | |
| 722 base::ReplaceSubstringsAfterOffset(&manifest_without_page_action, 0, | |
| 723 "\"page_action\": {},", ""); | |
| 724 ASSERT_NE(kDeclarativeContentManifest, manifest_without_page_action); | |
| 725 ext_dir_.WriteManifest(manifest_without_page_action); | |
| 726 ext_dir_.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundHelpers); | |
| 727 | |
| 728 const Extension* extension = LoadExtension(ext_dir_.UnpackedPath()); | |
| 729 ASSERT_TRUE(extension); | |
| 730 | |
| 731 const char kScript[] = | |
| 732 "setRules([{\n" | |
| 733 " conditions: [new PageStateMatcher({\n" | |
| 734 " pageUrl: {hostPrefix: \"test\"}})],\n" | |
| 735 " actions: [new ShowPageAction()]\n" | |
| 736 "}], 'test_rule');\n"; | |
| 737 const char kErrorSubstr[] = "without a page action"; | |
| 738 std::string result = | |
| 739 ExecuteScriptInBackgroundPage(extension->id(), kScript); | |
| 740 if (enable_redesign_) | |
| 741 EXPECT_THAT(result, testing::Not(testing::HasSubstr(kErrorSubstr))); | |
| 742 else | |
| 743 EXPECT_THAT(result, testing::HasSubstr(kErrorSubstr)); | |
| 744 | |
| 745 content::WebContents* const tab = | |
| 746 browser()->tab_strip_model()->GetWebContentsAt(0); | |
| 747 NavigateInRenderer(tab, GURL("http://test/")); | |
| 748 | |
| 749 bool expect_page_action = enable_redesign_; | |
| 750 ExtensionAction* page_action = | |
| 751 ExtensionActionManager::Get(browser()->profile()) | |
| 752 ->GetPageAction(*extension); | |
| 753 EXPECT_EQ(expect_page_action, page_action != nullptr); | |
| 754 EXPECT_EQ(expect_page_action ? 1u : 0u, | |
| 755 extension_action_test_util::GetVisiblePageActionCount(tab)); | |
| 756 } | |
| 757 | |
| 758 private: | |
| 759 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 760 DeclarativeContentApiTest::SetUpCommandLine(command_line); | |
| 761 override_toolbar_redesign_.reset(new FeatureSwitch::ScopedOverride( | |
| 762 FeatureSwitch::extension_action_redesign(), enable_redesign_)); | |
| 763 } | |
| 764 | |
| 765 bool enable_redesign_; | |
| 766 std::unique_ptr<FeatureSwitch::ScopedOverride> override_toolbar_redesign_; | |
| 767 DISALLOW_COPY_AND_ASSIGN(ShowPageActionWithoutPageActionTest); | |
| 768 }; | |
| 769 | |
| 770 class ShowPageActionWithoutPageActionLegacyTest | |
| 771 : public ShowPageActionWithoutPageActionTest { | |
| 772 protected: | |
| 773 ShowPageActionWithoutPageActionLegacyTest() | |
| 774 : ShowPageActionWithoutPageActionTest(false) {} | |
| 775 }; | |
| 776 | |
| 777 class ShowPageActionWithoutPageActionRedesignTest | |
| 778 : public ShowPageActionWithoutPageActionTest { | |
| 779 protected: | |
| 780 ShowPageActionWithoutPageActionRedesignTest() | |
| 781 : ShowPageActionWithoutPageActionTest(true) {} | |
| 782 }; | |
| 783 | 712 |
| 784 } // namespace | 713 } // namespace |
| 785 | 714 |
| 786 IN_PROC_BROWSER_TEST_F(ShowPageActionWithoutPageActionLegacyTest, Test) { | 715 IN_PROC_BROWSER_TEST_P(ShowPageActionWithoutPageActionTest, Test) { |
| 787 RunTest(); | 716 bool has_browser_action = GetParam(); |
| 717 // Load an extension without a page action. |
| 718 std::string manifest_without_page_action = kDeclarativeContentManifest; |
| 719 std::string replacement = has_browser_action ? "\"browser_action\": {}," : ""; |
| 720 base::ReplaceSubstringsAfterOffset(&manifest_without_page_action, 0, |
| 721 "\"page_action\": {},", |
| 722 replacement.c_str()); |
| 723 ASSERT_NE(kDeclarativeContentManifest, manifest_without_page_action); |
| 724 ext_dir_.WriteManifest(manifest_without_page_action); |
| 725 ext_dir_.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundHelpers); |
| 726 |
| 727 ChromeTestExtensionLoader loader(profile()); |
| 728 scoped_refptr<const Extension> extension = |
| 729 loader.LoadExtension(ext_dir_.UnpackedPath()); |
| 730 ASSERT_TRUE(extension); |
| 731 |
| 732 const char kScript[] = |
| 733 "setRules([{\n" |
| 734 " conditions: [new PageStateMatcher({\n" |
| 735 " pageUrl: {hostPrefix: \"test\"}})],\n" |
| 736 " actions: [new ShowPageAction()]\n" |
| 737 "}], 'test_rule');\n"; |
| 738 const char kErrorSubstr[] = "without a page action"; |
| 739 std::string result = ExecuteScriptInBackgroundPage(extension->id(), kScript); |
| 740 |
| 741 // Extensions with no action provided are given a page action by default |
| 742 // (for visibility reasons). If an extension has a browser action, it |
| 743 // should cause an error. |
| 744 if (has_browser_action) |
| 745 EXPECT_THAT(result, testing::HasSubstr(kErrorSubstr)); |
| 746 else |
| 747 EXPECT_THAT(result, testing::Not(testing::HasSubstr(kErrorSubstr))); |
| 748 |
| 749 content::WebContents* const tab = |
| 750 browser()->tab_strip_model()->GetWebContentsAt(0); |
| 751 NavigateInRenderer(tab, GURL("http://test/")); |
| 752 |
| 753 bool expect_page_action = !has_browser_action; |
| 754 ExtensionAction* page_action = |
| 755 ExtensionActionManager::Get(browser()->profile()) |
| 756 ->GetPageAction(*extension); |
| 757 EXPECT_EQ(expect_page_action, page_action != nullptr); |
| 758 EXPECT_EQ(expect_page_action ? 1u : 0u, |
| 759 extension_action_test_util::GetVisiblePageActionCount(tab)); |
| 788 } | 760 } |
| 789 | 761 |
| 790 IN_PROC_BROWSER_TEST_F(ShowPageActionWithoutPageActionRedesignTest, Test) { | 762 INSTANTIATE_TEST_CASE_P(ShowPageActionWithoutPageActionTest, |
| 791 RunTest(); | 763 ShowPageActionWithoutPageActionTest, |
| 792 } | 764 ::testing::Bool()); |
| 793 | 765 |
| 794 IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest, | 766 IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest, |
| 795 CanonicalizesPageStateMatcherCss) { | 767 CanonicalizesPageStateMatcherCss) { |
| 796 ext_dir_.WriteManifest(kDeclarativeContentManifest); | 768 ext_dir_.WriteManifest(kDeclarativeContentManifest); |
| 797 ext_dir_.WriteFile( | 769 ext_dir_.WriteFile( |
| 798 FILE_PATH_LITERAL("background.js"), | 770 FILE_PATH_LITERAL("background.js"), |
| 799 "var PageStateMatcher = chrome.declarativeContent.PageStateMatcher;\n" | 771 "var PageStateMatcher = chrome.declarativeContent.PageStateMatcher;\n" |
| 800 "function Return(obj) {\n" | 772 "function Return(obj) {\n" |
| 801 " window.domAutomationController.send('' + obj);\n" | 773 " window.domAutomationController.send('' + obj);\n" |
| 802 "}\n"); | 774 "}\n"); |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 984 // action interfaces, add a test that checks that a navigation always evaluates | 956 // action interfaces, add a test that checks that a navigation always evaluates |
| 985 // consistent URL state for all conditions. i.e.: if condition1 evaluates to | 957 // consistent URL state for all conditions. i.e.: if condition1 evaluates to |
| 986 // false on url0 and true on url1, and condition2 evaluates to true on url0 and | 958 // false on url0 and true on url1, and condition2 evaluates to true on url0 and |
| 987 // false on url1, navigate from url0 to url1 and validate that no action is | 959 // false on url1, navigate from url0 to url1 and validate that no action is |
| 988 // triggered. Do the same when navigating back to url0. This kind of test is | 960 // triggered. Do the same when navigating back to url0. This kind of test is |
| 989 // unfortunately not feasible with the current implementation and the existing | 961 // unfortunately not feasible with the current implementation and the existing |
| 990 // supported conditions and actions. | 962 // supported conditions and actions. |
| 991 | 963 |
| 992 } // namespace | 964 } // namespace |
| 993 } // namespace extensions | 965 } // namespace extensions |
| OLD | NEW |