| 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/macros.h" | |
| 6 #include "base/memory/scoped_ptr.h" | |
| 7 #include "base/strings/utf_string_conversions.h" | |
| 8 #include "chrome/browser/ui/browser.h" | |
| 9 #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h" | |
| 10 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h" | |
| 11 #include "chrome/browser/ui/views/frame/browser_view.h" | |
| 12 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h" | |
| 13 #include "chrome/browser/ui/views/toolbar/toolbar_action_view.h" | |
| 14 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" | |
| 15 #include "chrome/test/base/in_process_browser_test.h" | |
| 16 #include "extensions/common/feature_switch.h" | |
| 17 #include "grit/theme_resources.h" | |
| 18 #include "ui/base/resource/resource_bundle.h" | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 const char kMockId[] = "mock_action"; | |
| 23 | |
| 24 class MockComponentAction : public ToolbarActionViewController { | |
| 25 public: | |
| 26 MockComponentAction() : click_count_(0u), id_(kMockId) {} | |
| 27 ~MockComponentAction() override {} | |
| 28 | |
| 29 // ToolbarActionButtonController: | |
| 30 const std::string& GetId() const override { return id_; } | |
| 31 void SetDelegate(ToolbarActionViewDelegate* delegate) override {} | |
| 32 gfx::Image GetIcon(content::WebContents* web_contents) override { | |
| 33 return ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 34 IDR_BROWSER_ACTION); | |
| 35 } | |
| 36 gfx::ImageSkia GetIconWithBadge() override { | |
| 37 return *GetIcon(nullptr).ToImageSkia(); | |
| 38 } | |
| 39 base::string16 GetActionName() const override { | |
| 40 return base::ASCIIToUTF16("Component Action"); | |
| 41 } | |
| 42 base::string16 GetAccessibleName(content::WebContents* web_contents) | |
| 43 const override { | |
| 44 return GetActionName(); | |
| 45 } | |
| 46 base::string16 GetTooltip(content::WebContents* web_contents) | |
| 47 const override { | |
| 48 return GetActionName(); | |
| 49 } | |
| 50 bool IsEnabled(content::WebContents* web_contents) const override { | |
| 51 return true; | |
| 52 } | |
| 53 bool WantsToRun(content::WebContents* web_contents) const override { | |
| 54 return false; | |
| 55 } | |
| 56 bool HasPopup(content::WebContents* web_contents) const override { | |
| 57 return true; | |
| 58 } | |
| 59 void HidePopup() override {} | |
| 60 gfx::NativeView GetPopupNativeView() override { return nullptr; } | |
| 61 ui::MenuModel* GetContextMenu() override { return nullptr; } | |
| 62 bool CanDrag() const override { return false; } | |
| 63 bool IsMenuRunning() const override { return false; } | |
| 64 bool ExecuteAction(bool by_user) override { | |
| 65 ++click_count_; | |
| 66 return false; | |
| 67 } | |
| 68 void UpdateState() override {} | |
| 69 | |
| 70 size_t click_count() const { return click_count_; } | |
| 71 | |
| 72 private: | |
| 73 size_t click_count_; | |
| 74 std::string id_; | |
| 75 | |
| 76 DISALLOW_COPY_AND_ASSIGN(MockComponentAction); | |
| 77 }; | |
| 78 | |
| 79 class MockComponentToolbarActionsFactory | |
| 80 : public ComponentToolbarActionsFactory { | |
| 81 public: | |
| 82 MockComponentToolbarActionsFactory(); | |
| 83 virtual ~MockComponentToolbarActionsFactory(); | |
| 84 | |
| 85 // ComponentToolbarActionsFactory: | |
| 86 ScopedVector<ToolbarActionViewController> GetComponentToolbarActions() | |
| 87 override; | |
| 88 | |
| 89 private: | |
| 90 DISALLOW_COPY_AND_ASSIGN(MockComponentToolbarActionsFactory); | |
| 91 }; | |
| 92 | |
| 93 MockComponentToolbarActionsFactory::MockComponentToolbarActionsFactory() { | |
| 94 ComponentToolbarActionsFactory::SetTestingFactory(this); | |
| 95 } | |
| 96 | |
| 97 MockComponentToolbarActionsFactory::~MockComponentToolbarActionsFactory() { | |
| 98 ComponentToolbarActionsFactory::SetTestingFactory(nullptr); | |
| 99 } | |
| 100 | |
| 101 ScopedVector<ToolbarActionViewController> | |
| 102 MockComponentToolbarActionsFactory::GetComponentToolbarActions() { | |
| 103 ScopedVector<ToolbarActionViewController> component_actions; | |
| 104 component_actions.push_back(new MockComponentAction()); | |
| 105 return component_actions.Pass(); | |
| 106 } | |
| 107 | |
| 108 } // namespace | |
| 109 | |
| 110 class ComponentToolbarActionsBrowserTest : public InProcessBrowserTest { | |
| 111 protected: | |
| 112 ComponentToolbarActionsBrowserTest() {} | |
| 113 ~ComponentToolbarActionsBrowserTest() override {} | |
| 114 | |
| 115 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 116 InProcessBrowserTest::SetUpCommandLine(command_line); | |
| 117 enable_redesign_.reset(new extensions::FeatureSwitch::ScopedOverride( | |
| 118 extensions::FeatureSwitch::extension_action_redesign(), true)); | |
| 119 mock_actions_factory_.reset(new MockComponentToolbarActionsFactory()); | |
| 120 } | |
| 121 | |
| 122 private: | |
| 123 scoped_ptr<extensions::FeatureSwitch::ScopedOverride> enable_redesign_; | |
| 124 scoped_ptr<MockComponentToolbarActionsFactory> mock_actions_factory_; | |
| 125 | |
| 126 DISALLOW_COPY_AND_ASSIGN(ComponentToolbarActionsBrowserTest); | |
| 127 }; | |
| 128 | |
| 129 // Test that Component Toolbar Actions appear in the browser actions container | |
| 130 // and can receive click events properly. | |
| 131 IN_PROC_BROWSER_TEST_F(ComponentToolbarActionsBrowserTest, | |
| 132 ComponentToolbarActionsShowUpAndRespondToClicks) { | |
| 133 BrowserActionsContainer* browser_actions_container = | |
| 134 BrowserView::GetBrowserViewForBrowser(browser()) | |
| 135 ->toolbar()->browser_actions(); | |
| 136 | |
| 137 // There should be only one component action view. | |
| 138 ASSERT_EQ(1u, browser_actions_container->num_toolbar_actions()); | |
| 139 | |
| 140 ToolbarActionView* view = | |
| 141 browser_actions_container->GetToolbarActionViewAt(0u); | |
| 142 ASSERT_EQ(kMockId, view->view_controller()->GetId()); | |
| 143 MockComponentAction* mock_component_action = | |
| 144 static_cast<MockComponentAction*>(view->view_controller()); | |
| 145 | |
| 146 // Test that clicking on the component action works. | |
| 147 EXPECT_EQ(0u, mock_component_action->click_count()); | |
| 148 view->Activate(); | |
| 149 EXPECT_EQ(1u, mock_component_action->click_count()); | |
| 150 } | |
| OLD | NEW |