| 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/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 9 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 10 #include "chrome/browser/extensions/extension_action.h" | 10 #include "chrome/browser/extensions/extension_action.h" |
| 11 #include "chrome/browser/extensions/extension_action_manager.h" | 11 #include "chrome/browser/extensions/extension_action_manager.h" |
| 12 #include "chrome/browser/extensions/extension_action_test_util.h" | 12 #include "chrome/browser/extensions/extension_action_test_util.h" |
| 13 #include "chrome/browser/extensions/extension_apitest.h" | 13 #include "chrome/browser/extensions/extension_apitest.h" |
| 14 #include "chrome/browser/extensions/extension_service.h" | 14 #include "chrome/browser/extensions/extension_service.h" |
| 15 #include "chrome/browser/extensions/extension_tab_util.h" | 15 #include "chrome/browser/extensions/extension_tab_util.h" |
| 16 #include "chrome/browser/extensions/test_extension_dir.h" | 16 #include "chrome/browser/extensions/test_extension_dir.h" |
| 17 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
| 18 #include "chrome/browser/ui/browser_window.h" | 18 #include "chrome/browser/ui/browser_window.h" |
| 19 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 19 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 20 #include "components/bookmarks/browser/bookmark_model.h" | 20 #include "components/bookmarks/browser/bookmark_model.h" |
| 21 #include "content/public/test/browser_test_utils.h" | 21 #include "content/public/test/browser_test_utils.h" |
| 22 #include "extensions/browser/extension_registry.h" | 22 #include "extensions/browser/extension_registry.h" |
| 23 #include "extensions/browser/extension_system.h" | 23 #include "extensions/browser/extension_system.h" |
| 24 #include "extensions/browser/test_extension_registry_observer.h" | |
| 25 #include "extensions/test/extension_test_message_listener.h" | 24 #include "extensions/test/extension_test_message_listener.h" |
| 26 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
| 27 | 26 |
| 28 namespace extensions { | 27 namespace extensions { |
| 29 namespace { | 28 namespace { |
| 30 | 29 |
| 31 const char kDeclarativeContentManifest[] = | 30 const char kDeclarativeContentManifest[] = |
| 32 "{\n" | 31 "{\n" |
| 33 " \"name\": \"Declarative Content apitest\",\n" | 32 " \"name\": \"Declarative Content apitest\",\n" |
| 34 " \"version\": \"0.1\",\n" | 33 " \"version\": \"0.1\",\n" |
| (...skipping 602 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 637 ExecuteScriptInBackgroundPage( | 636 ExecuteScriptInBackgroundPage( |
| 638 extension->id(), | 637 extension->id(), |
| 639 "onPageChanged.removeRules(undefined, function() {\n" | 638 "onPageChanged.removeRules(undefined, function() {\n" |
| 640 " window.domAutomationController.send('removed');\n" | 639 " window.domAutomationController.send('removed');\n" |
| 641 "});\n")); | 640 "});\n")); |
| 642 EXPECT_FALSE(page_action->GetIsVisible(tab_id)); | 641 EXPECT_FALSE(page_action->GetIsVisible(tab_id)); |
| 643 } | 642 } |
| 644 | 643 |
| 645 IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest, | 644 IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest, |
| 646 ShowPageActionWithoutPageAction) { | 645 ShowPageActionWithoutPageAction) { |
| 647 // Load an extension without a page action. | |
| 648 std::string manifest_without_page_action = kDeclarativeContentManifest; | 646 std::string manifest_without_page_action = kDeclarativeContentManifest; |
| 649 base::ReplaceSubstringsAfterOffset( | 647 base::ReplaceSubstringsAfterOffset( |
| 650 &manifest_without_page_action, 0, "\"page_action\": {},", ""); | 648 &manifest_without_page_action, 0, "\"page_action\": {},", ""); |
| 651 ASSERT_NE(kDeclarativeContentManifest, manifest_without_page_action); | 649 ASSERT_NE(kDeclarativeContentManifest, manifest_without_page_action); |
| 652 ext_dir_.WriteManifest(manifest_without_page_action); | 650 ext_dir_.WriteManifest(manifest_without_page_action); |
| 653 ext_dir_.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundHelpers); | 651 ext_dir_.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundHelpers); |
| 652 const Extension* extension = LoadExtension(ext_dir_.unpacked_path()); |
| 653 ASSERT_TRUE(extension); |
| 654 | 654 |
| 655 std::string extension_id; | 655 EXPECT_THAT(ExecuteScriptInBackgroundPage( |
| 656 extension->id(), |
| 657 "setRules([{\n" |
| 658 " conditions: [new PageStateMatcher({\n" |
| 659 " pageUrl: {hostPrefix: \"test\"}})],\n" |
| 660 " actions: [new ShowPageAction()]\n" |
| 661 "}], 'test_rule');\n"), |
| 662 testing::HasSubstr("without a page action")); |
| 656 | 663 |
| 657 std::string script = | 664 content::WebContents* const tab = |
| 658 "setRules([{\n" | 665 browser()->tab_strip_model()->GetWebContentsAt(0); |
| 659 " conditions: [new PageStateMatcher({\n" | 666 NavigateInRenderer(tab, GURL("http://test/")); |
| 660 " pageUrl: {hostPrefix: \"test\"}})],\n" | |
| 661 " actions: [new ShowPageAction()]\n" | |
| 662 "}], 'test_rule');\n"; | |
| 663 | 667 |
| 664 { | 668 EXPECT_EQ(NULL, |
| 665 // Without the extension action redesign, the extension should get an error. | 669 ExtensionActionManager::Get(browser()->profile())-> |
| 666 FeatureSwitch::ScopedOverride override_toolbar_redesign( | 670 GetPageAction(*extension)); |
| 667 FeatureSwitch::extension_action_redesign(), false); | 671 EXPECT_EQ(0u, extension_action_test_util::GetVisiblePageActionCount(tab)); |
| 668 const Extension* extension = LoadExtension(ext_dir_.unpacked_path()); | |
| 669 ASSERT_TRUE(extension); | |
| 670 extension_id = extension->id(); | |
| 671 | |
| 672 EXPECT_THAT(ExecuteScriptInBackgroundPage(extension->id(), script), | |
| 673 testing::HasSubstr("without a page action")); | |
| 674 | |
| 675 content::WebContents* const tab = | |
| 676 browser()->tab_strip_model()->GetWebContentsAt(0); | |
| 677 NavigateInRenderer(tab, GURL("http://test/")); | |
| 678 | |
| 679 // There should be no page action. | |
| 680 EXPECT_EQ(nullptr, ExtensionActionManager::Get(browser()->profile()) | |
| 681 ->GetPageAction(*extension)); | |
| 682 EXPECT_EQ(0u, extension_action_test_util::GetVisiblePageActionCount(tab)); | |
| 683 } | |
| 684 { | |
| 685 // With the extension action redesign, it should work normally. | |
| 686 FeatureSwitch::ScopedOverride override_toolbar_redesign( | |
| 687 FeatureSwitch::extension_action_redesign(), true); | |
| 688 ReloadExtension(extension_id); | |
| 689 const Extension* extension = | |
| 690 ExtensionRegistry::Get(profile())->enabled_extensions().GetByID( | |
| 691 extension_id); | |
| 692 ASSERT_TRUE(extension); | |
| 693 | |
| 694 EXPECT_THAT(ExecuteScriptInBackgroundPage(extension->id(), script), | |
| 695 testing::Not(testing::HasSubstr("without a page action"))); | |
| 696 | |
| 697 content::WebContents* const tab = | |
| 698 browser()->tab_strip_model()->GetWebContentsAt(0); | |
| 699 NavigateInRenderer(tab, GURL("http://test/")); | |
| 700 | |
| 701 // There should be a page action. | |
| 702 EXPECT_TRUE(ExtensionActionManager::Get(browser()->profile()) | |
| 703 ->GetPageAction(*extension)); | |
| 704 EXPECT_EQ(1u, extension_action_test_util::GetVisiblePageActionCount(tab)); | |
| 705 } | |
| 706 } | 672 } |
| 707 | 673 |
| 708 IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest, | 674 IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest, |
| 709 CanonicalizesPageStateMatcherCss) { | 675 CanonicalizesPageStateMatcherCss) { |
| 710 ext_dir_.WriteManifest(kDeclarativeContentManifest); | 676 ext_dir_.WriteManifest(kDeclarativeContentManifest); |
| 711 ext_dir_.WriteFile( | 677 ext_dir_.WriteFile( |
| 712 FILE_PATH_LITERAL("background.js"), | 678 FILE_PATH_LITERAL("background.js"), |
| 713 "var PageStateMatcher = chrome.declarativeContent.PageStateMatcher;\n" | 679 "var PageStateMatcher = chrome.declarativeContent.PageStateMatcher;\n" |
| 714 "function Return(obj) {\n" | 680 "function Return(obj) {\n" |
| 715 " window.domAutomationController.send('' + obj);\n" | 681 " window.domAutomationController.send('' + obj);\n" |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 898 // action interfaces, add a test that checks that a navigation always evaluates | 864 // action interfaces, add a test that checks that a navigation always evaluates |
| 899 // consistent URL state for all conditions. i.e.: if condition1 evaluates to | 865 // consistent URL state for all conditions. i.e.: if condition1 evaluates to |
| 900 // false on url0 and true on url1, and condition2 evaluates to true on url0 and | 866 // false on url0 and true on url1, and condition2 evaluates to true on url0 and |
| 901 // false on url1, navigate from url0 to url1 and validate that no action is | 867 // false on url1, navigate from url0 to url1 and validate that no action is |
| 902 // triggered. Do the same when navigating back to url0. This kind of test is | 868 // triggered. Do the same when navigating back to url0. This kind of test is |
| 903 // unfortunately not feasible with the current implementation and the existing | 869 // unfortunately not feasible with the current implementation and the existing |
| 904 // supported conditions and actions. | 870 // supported conditions and actions. |
| 905 | 871 |
| 906 } // namespace | 872 } // namespace |
| 907 } // namespace extensions | 873 } // namespace extensions |
| OLD | NEW |