OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/memory/scoped_ptr.h" |
| 6 #include "base/run_loop.h" |
| 7 #include "base/strings/stringprintf.h" |
| 8 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
| 9 #include "chrome/browser/extensions/extension_action.h" |
| 10 #include "chrome/browser/extensions/extension_action_manager.h" |
| 11 #include "chrome/browser/extensions/extension_browsertest.h" |
| 12 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 13 #include "chrome/browser/extensions/test_extension_dir.h" |
| 14 #include "chrome/browser/sessions/session_tab_helper.h" |
| 15 #include "chrome/browser/ui/browser.h" |
| 16 #include "chrome/browser/ui/browser_window.h" |
| 17 #include "chrome/browser/ui/location_bar/location_bar.h" |
| 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 19 #include "extensions/common/extension.h" |
| 20 #include "extensions/common/feature_switch.h" |
| 21 |
| 22 namespace { |
| 23 |
| 24 const char kBackgroundScriptSource[] = |
| 25 "chrome.pageAction.onClicked.addListener(function() {\n" |
| 26 " chrome.test.sendMessage('clicked');\n" |
| 27 "});\n" |
| 28 "chrome.test.sendMessage('registered');\n"; |
| 29 const char kManifestSource[] = |
| 30 "{" |
| 31 " \"name\": \"%s\"," |
| 32 " \"version\": \"1.0\"," |
| 33 " \"manifest_version\": 2," |
| 34 " \"background\": { \"scripts\": [\"background.js\"] }," |
| 35 " \"page_action\": { }" |
| 36 "}"; |
| 37 |
| 38 } // namespace |
| 39 |
| 40 class LocationBarBrowserTest : public ExtensionBrowserTest { |
| 41 public: |
| 42 virtual ~LocationBarBrowserTest() {} |
| 43 |
| 44 protected: |
| 45 // Load an extension with a PageAction that sends a message when clicked. |
| 46 const extensions::Extension* LoadPageActionExtension( |
| 47 extensions::TestExtensionDir* dir); |
| 48 }; |
| 49 |
| 50 const extensions::Extension* LocationBarBrowserTest::LoadPageActionExtension( |
| 51 extensions::TestExtensionDir* dir) { |
| 52 DCHECK(dir); |
| 53 |
| 54 dir->WriteManifest(base::StringPrintf(kManifestSource, "page_action1")); |
| 55 dir->WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundScriptSource); |
| 56 |
| 57 ExtensionTestMessageListener registered_listener("registered", false); |
| 58 const extensions::Extension* extension = LoadExtension(dir->unpacked_path()); |
| 59 registered_listener.WaitUntilSatisfied(); |
| 60 |
| 61 return extension; |
| 62 } |
| 63 |
| 64 // Test that page actions show up properly in the location bar. Since the |
| 65 // page action logic is more fully tested as part of the extensions system, this |
| 66 // only needs to check that they are displayed and clicking on them triggers |
| 67 // the action. |
| 68 IN_PROC_BROWSER_TEST_F(LocationBarBrowserTest, PageActionUITest) { |
| 69 LocationBarTesting* location_bar = |
| 70 browser()->window()->GetLocationBar()->GetLocationBarForTesting(); |
| 71 |
| 72 // At the start, no page actions should exist. |
| 73 EXPECT_EQ(0, location_bar->PageActionCount()); |
| 74 EXPECT_EQ(0, location_bar->PageActionVisibleCount()); |
| 75 |
| 76 // Load two extensions with page actions. |
| 77 extensions::TestExtensionDir test_dir1; |
| 78 const extensions::Extension* page_action1 = |
| 79 LoadPageActionExtension(&test_dir1); |
| 80 ASSERT_TRUE(page_action1); |
| 81 |
| 82 extensions::TestExtensionDir test_dir2; |
| 83 const extensions::Extension* page_action2 = |
| 84 LoadPageActionExtension(&test_dir2); |
| 85 ASSERT_TRUE(page_action2); |
| 86 |
| 87 // Now there should be two page actions, but neither should be visible. |
| 88 EXPECT_EQ(2, location_bar->PageActionCount()); |
| 89 EXPECT_EQ(0, location_bar->PageActionVisibleCount()); |
| 90 |
| 91 // Make the first page action visible. |
| 92 ExtensionAction* action = extensions::ExtensionActionManager::Get( |
| 93 profile())->GetPageAction(*page_action1); |
| 94 content::WebContents* tab = |
| 95 browser()->tab_strip_model()->GetActiveWebContents(); |
| 96 int tab_id = SessionTabHelper::IdForTab(tab); |
| 97 action->SetIsVisible(tab_id, true); |
| 98 extensions::ExtensionActionAPI::Get(profile())->NotifyChange( |
| 99 action, tab, profile()); |
| 100 |
| 101 // Verify that only one action is visible and that it's the proper one. |
| 102 EXPECT_EQ(2, location_bar->PageActionCount()); |
| 103 EXPECT_EQ(1, location_bar->PageActionVisibleCount()); |
| 104 EXPECT_EQ(action, location_bar->GetVisiblePageAction(0u)); |
| 105 |
| 106 // Trigger the visible page action, and ensure it executes. |
| 107 ExtensionTestMessageListener clicked_listener("clicked", false); |
| 108 clicked_listener.set_extension_id(page_action1->id()); |
| 109 location_bar->TestPageActionPressed(0u); |
| 110 EXPECT_TRUE(clicked_listener.WaitUntilSatisfied()); |
| 111 } |
| 112 |
| 113 class LocationBarBrowserTestWithRedesign : public LocationBarBrowserTest { |
| 114 private: |
| 115 virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE; |
| 116 |
| 117 scoped_ptr<extensions::FeatureSwitch::ScopedOverride> enable_redesign_; |
| 118 }; |
| 119 |
| 120 void LocationBarBrowserTestWithRedesign::SetUpCommandLine( |
| 121 base::CommandLine* command_line) { |
| 122 LocationBarBrowserTest::SetUpCommandLine(command_line); |
| 123 enable_redesign_.reset(new extensions::FeatureSwitch::ScopedOverride( |
| 124 extensions::FeatureSwitch::extension_action_redesign(), true)); |
| 125 } |
| 126 |
| 127 // Test that page actions are not displayed in the location bar if the |
| 128 // extension action redesign switch is enabled. |
| 129 IN_PROC_BROWSER_TEST_F(LocationBarBrowserTestWithRedesign, |
| 130 PageActionUITestWithRedesign) { |
| 131 LocationBarTesting* location_bar = |
| 132 browser()->window()->GetLocationBar()->GetLocationBarForTesting(); |
| 133 EXPECT_EQ(0, location_bar->PageActionCount()); |
| 134 EXPECT_EQ(0, location_bar->PageActionVisibleCount()); |
| 135 |
| 136 // Load an extension with a page action. |
| 137 extensions::TestExtensionDir test_dir1; |
| 138 const extensions::Extension* page_action1 = |
| 139 LoadPageActionExtension(&test_dir1); |
| 140 ASSERT_TRUE(page_action1); |
| 141 |
| 142 // We should still have no page actions. |
| 143 EXPECT_EQ(0, location_bar->PageActionCount()); |
| 144 EXPECT_EQ(0, location_bar->PageActionVisibleCount()); |
| 145 |
| 146 // Set the page action to be visible. |
| 147 ExtensionAction* action = extensions::ExtensionActionManager::Get( |
| 148 profile())->GetPageAction(*page_action1); |
| 149 content::WebContents* tab = |
| 150 browser()->tab_strip_model()->GetActiveWebContents(); |
| 151 int tab_id = SessionTabHelper::IdForTab(tab); |
| 152 action->SetIsVisible(tab_id, true); |
| 153 extensions::ExtensionActionAPI::Get(profile())->NotifyChange( |
| 154 action, tab, profile()); |
| 155 |
| 156 // We should still have no page actions. |
| 157 EXPECT_EQ(0, location_bar->PageActionCount()); |
| 158 EXPECT_EQ(0, location_bar->PageActionVisibleCount()); |
| 159 } |
OLD | NEW |