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 // TODO(devlin): This flakily times out on Mac. crbug.com/410866 |
| 69 #if defined(OS_MACOSX) |
| 70 #define MAYBE_PageActionUITest DISABLED_PageActionUITeset |
| 71 #else |
| 72 #define MAYBE_PageActionUITest PageActionUITest |
| 73 #endif |
| 74 IN_PROC_BROWSER_TEST_F(LocationBarBrowserTest, MAYBE_PageActionUITest) { |
| 75 LocationBarTesting* location_bar = |
| 76 browser()->window()->GetLocationBar()->GetLocationBarForTesting(); |
| 77 |
| 78 // At the start, no page actions should exist. |
| 79 EXPECT_EQ(0, location_bar->PageActionCount()); |
| 80 EXPECT_EQ(0, location_bar->PageActionVisibleCount()); |
| 81 |
| 82 // Load two extensions with page actions. |
| 83 extensions::TestExtensionDir test_dir1; |
| 84 const extensions::Extension* page_action1 = |
| 85 LoadPageActionExtension(&test_dir1); |
| 86 ASSERT_TRUE(page_action1); |
| 87 |
| 88 extensions::TestExtensionDir test_dir2; |
| 89 const extensions::Extension* page_action2 = |
| 90 LoadPageActionExtension(&test_dir2); |
| 91 ASSERT_TRUE(page_action2); |
| 92 |
| 93 // Now there should be two page actions, but neither should be visible. |
| 94 EXPECT_EQ(2, location_bar->PageActionCount()); |
| 95 EXPECT_EQ(0, location_bar->PageActionVisibleCount()); |
| 96 |
| 97 // Make the first page action visible. |
| 98 ExtensionAction* action = extensions::ExtensionActionManager::Get( |
| 99 profile())->GetPageAction(*page_action1); |
| 100 content::WebContents* tab = |
| 101 browser()->tab_strip_model()->GetActiveWebContents(); |
| 102 int tab_id = SessionTabHelper::IdForTab(tab); |
| 103 action->SetIsVisible(tab_id, true); |
| 104 extensions::ExtensionActionAPI::Get(profile())->NotifyChange( |
| 105 action, tab, profile()); |
| 106 |
| 107 // Verify that only one action is visible and that it's the proper one. |
| 108 EXPECT_EQ(2, location_bar->PageActionCount()); |
| 109 EXPECT_EQ(1, location_bar->PageActionVisibleCount()); |
| 110 EXPECT_EQ(action, location_bar->GetVisiblePageAction(0u)); |
| 111 |
| 112 // Trigger the visible page action, and ensure it executes. |
| 113 ExtensionTestMessageListener clicked_listener("clicked", false); |
| 114 clicked_listener.set_extension_id(page_action1->id()); |
| 115 location_bar->TestPageActionPressed(0u); |
| 116 EXPECT_TRUE(clicked_listener.WaitUntilSatisfied()); |
| 117 } |
| 118 |
| 119 class LocationBarBrowserTestWithRedesign : public LocationBarBrowserTest { |
| 120 private: |
| 121 virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE; |
| 122 |
| 123 scoped_ptr<extensions::FeatureSwitch::ScopedOverride> enable_redesign_; |
| 124 }; |
| 125 |
| 126 void LocationBarBrowserTestWithRedesign::SetUpCommandLine( |
| 127 base::CommandLine* command_line) { |
| 128 LocationBarBrowserTest::SetUpCommandLine(command_line); |
| 129 enable_redesign_.reset(new extensions::FeatureSwitch::ScopedOverride( |
| 130 extensions::FeatureSwitch::extension_action_redesign(), true)); |
| 131 } |
| 132 |
| 133 // Test that page actions are not displayed in the location bar if the |
| 134 // extension action redesign switch is enabled. |
| 135 IN_PROC_BROWSER_TEST_F(LocationBarBrowserTestWithRedesign, |
| 136 PageActionUITestWithRedesign) { |
| 137 LocationBarTesting* location_bar = |
| 138 browser()->window()->GetLocationBar()->GetLocationBarForTesting(); |
| 139 EXPECT_EQ(0, location_bar->PageActionCount()); |
| 140 EXPECT_EQ(0, location_bar->PageActionVisibleCount()); |
| 141 |
| 142 // Load an extension with a page action. |
| 143 extensions::TestExtensionDir test_dir1; |
| 144 const extensions::Extension* page_action1 = |
| 145 LoadPageActionExtension(&test_dir1); |
| 146 ASSERT_TRUE(page_action1); |
| 147 |
| 148 // We should still have no page actions. |
| 149 EXPECT_EQ(0, location_bar->PageActionCount()); |
| 150 EXPECT_EQ(0, location_bar->PageActionVisibleCount()); |
| 151 |
| 152 // Set the page action to be visible. |
| 153 ExtensionAction* action = extensions::ExtensionActionManager::Get( |
| 154 profile())->GetPageAction(*page_action1); |
| 155 content::WebContents* tab = |
| 156 browser()->tab_strip_model()->GetActiveWebContents(); |
| 157 int tab_id = SessionTabHelper::IdForTab(tab); |
| 158 action->SetIsVisible(tab_id, true); |
| 159 extensions::ExtensionActionAPI::Get(profile())->NotifyChange( |
| 160 action, tab, profile()); |
| 161 |
| 162 // We should still have no page actions. |
| 163 EXPECT_EQ(0, location_bar->PageActionCount()); |
| 164 EXPECT_EQ(0, location_bar->PageActionVisibleCount()); |
| 165 } |
OLD | NEW |