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" |
24 #include "extensions/test/extension_test_message_listener.h" | 25 #include "extensions/test/extension_test_message_listener.h" |
25 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
26 | 27 |
27 namespace extensions { | 28 namespace extensions { |
28 namespace { | 29 namespace { |
29 | 30 |
30 const char kDeclarativeContentManifest[] = | 31 const char kDeclarativeContentManifest[] = |
31 "{\n" | 32 "{\n" |
32 " \"name\": \"Declarative Content apitest\",\n" | 33 " \"name\": \"Declarative Content apitest\",\n" |
33 " \"version\": \"0.1\",\n" | 34 " \"version\": \"0.1\",\n" |
(...skipping 602 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
636 ExecuteScriptInBackgroundPage( | 637 ExecuteScriptInBackgroundPage( |
637 extension->id(), | 638 extension->id(), |
638 "onPageChanged.removeRules(undefined, function() {\n" | 639 "onPageChanged.removeRules(undefined, function() {\n" |
639 " window.domAutomationController.send('removed');\n" | 640 " window.domAutomationController.send('removed');\n" |
640 "});\n")); | 641 "});\n")); |
641 EXPECT_FALSE(page_action->GetIsVisible(tab_id)); | 642 EXPECT_FALSE(page_action->GetIsVisible(tab_id)); |
642 } | 643 } |
643 | 644 |
644 IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest, | 645 IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest, |
645 ShowPageActionWithoutPageAction) { | 646 ShowPageActionWithoutPageAction) { |
| 647 // Load an extension without a page action. |
646 std::string manifest_without_page_action = kDeclarativeContentManifest; | 648 std::string manifest_without_page_action = kDeclarativeContentManifest; |
647 base::ReplaceSubstringsAfterOffset( | 649 base::ReplaceSubstringsAfterOffset( |
648 &manifest_without_page_action, 0, "\"page_action\": {},", ""); | 650 &manifest_without_page_action, 0, "\"page_action\": {},", ""); |
649 ASSERT_NE(kDeclarativeContentManifest, manifest_without_page_action); | 651 ASSERT_NE(kDeclarativeContentManifest, manifest_without_page_action); |
650 ext_dir_.WriteManifest(manifest_without_page_action); | 652 ext_dir_.WriteManifest(manifest_without_page_action); |
651 ext_dir_.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundHelpers); | 653 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 EXPECT_THAT(ExecuteScriptInBackgroundPage( | 655 std::string extension_id; |
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")); | |
663 | 656 |
664 content::WebContents* const tab = | 657 std::string script = |
665 browser()->tab_strip_model()->GetWebContentsAt(0); | 658 "setRules([{\n" |
666 NavigateInRenderer(tab, GURL("http://test/")); | 659 " conditions: [new PageStateMatcher({\n" |
| 660 " pageUrl: {hostPrefix: \"test\"}})],\n" |
| 661 " actions: [new ShowPageAction()]\n" |
| 662 "}], 'test_rule');\n"; |
667 | 663 |
668 EXPECT_EQ(NULL, | 664 { |
669 ExtensionActionManager::Get(browser()->profile())-> | 665 // Without the extension action redesign, the extension should get an error. |
670 GetPageAction(*extension)); | 666 FeatureSwitch::ScopedOverride override_toolbar_redesign( |
671 EXPECT_EQ(0u, extension_action_test_util::GetVisiblePageActionCount(tab)); | 667 FeatureSwitch::extension_action_redesign(), false); |
| 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 } |
672 } | 706 } |
673 | 707 |
674 IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest, | 708 IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest, |
675 CanonicalizesPageStateMatcherCss) { | 709 CanonicalizesPageStateMatcherCss) { |
676 ext_dir_.WriteManifest(kDeclarativeContentManifest); | 710 ext_dir_.WriteManifest(kDeclarativeContentManifest); |
677 ext_dir_.WriteFile( | 711 ext_dir_.WriteFile( |
678 FILE_PATH_LITERAL("background.js"), | 712 FILE_PATH_LITERAL("background.js"), |
679 "var PageStateMatcher = chrome.declarativeContent.PageStateMatcher;\n" | 713 "var PageStateMatcher = chrome.declarativeContent.PageStateMatcher;\n" |
680 "function Return(obj) {\n" | 714 "function Return(obj) {\n" |
681 " window.domAutomationController.send('' + obj);\n" | 715 " window.domAutomationController.send('' + obj);\n" |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
864 // action interfaces, add a test that checks that a navigation always evaluates | 898 // action interfaces, add a test that checks that a navigation always evaluates |
865 // consistent URL state for all conditions. i.e.: if condition1 evaluates to | 899 // consistent URL state for all conditions. i.e.: if condition1 evaluates to |
866 // false on url0 and true on url1, and condition2 evaluates to true on url0 and | 900 // false on url0 and true on url1, and condition2 evaluates to true on url0 and |
867 // false on url1, navigate from url0 to url1 and validate that no action is | 901 // false on url1, navigate from url0 to url1 and validate that no action is |
868 // triggered. Do the same when navigating back to url0. This kind of test is | 902 // triggered. Do the same when navigating back to url0. This kind of test is |
869 // unfortunately not feasible with the current implementation and the existing | 903 // unfortunately not feasible with the current implementation and the existing |
870 // supported conditions and actions. | 904 // supported conditions and actions. |
871 | 905 |
872 } // namespace | 906 } // namespace |
873 } // namespace extensions | 907 } // namespace extensions |
OLD | NEW |