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_action_view.h" |
| 13 #include "chrome/browser/ui/views/toolbar/browser_actions_container.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 class MockComponentAction : public ToolbarActionViewController { |
| 23 public: |
| 24 MockComponentAction() : click_count_(0u) {} |
| 25 virtual ~MockComponentAction() {} |
| 26 |
| 27 // ToolbarActionButtonController: |
| 28 virtual Type GetType() const override { return TYPE_COMPONENT_ACTION; } |
| 29 virtual void SetDelegate(ToolbarActionViewDelegate* delegate) override {} |
| 30 virtual gfx::Image GetIcon(content::WebContents* web_contents) override { |
| 31 return ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 32 IDR_BROWSER_ACTION); |
| 33 } |
| 34 virtual base::string16 GetAccessibleName(content::WebContents* web_contents) |
| 35 const override { |
| 36 return base::ASCIIToUTF16("Component Action"); |
| 37 } |
| 38 virtual base::string16 GetTooltip(content::WebContents* web_contents) |
| 39 const override { |
| 40 return GetAccessibleName(web_contents); |
| 41 } |
| 42 virtual bool IsEnabled(content::WebContents* web_contents) const override { |
| 43 return true; |
| 44 } |
| 45 virtual bool HasPopup(content::WebContents* web_contents) const override { |
| 46 return true; |
| 47 } |
| 48 virtual void HidePopup() override {} |
| 49 virtual bool IsMenuRunning() const override { return false; } |
| 50 virtual void ExecuteActionByUser() override { ++click_count_; } |
| 51 |
| 52 size_t click_count() const { return click_count_; } |
| 53 |
| 54 private: |
| 55 size_t click_count_; |
| 56 |
| 57 DISALLOW_COPY_AND_ASSIGN(MockComponentAction); |
| 58 }; |
| 59 |
| 60 class MockComponentToolbarActionsFactory |
| 61 : public ComponentToolbarActionsFactory { |
| 62 public: |
| 63 MockComponentToolbarActionsFactory(); |
| 64 virtual ~MockComponentToolbarActionsFactory(); |
| 65 |
| 66 // ComponentToolbarActionsFactory: |
| 67 virtual ScopedVector<ToolbarActionViewController> |
| 68 GetComponentToolbarActions() override; |
| 69 |
| 70 private: |
| 71 DISALLOW_COPY_AND_ASSIGN(MockComponentToolbarActionsFactory); |
| 72 }; |
| 73 |
| 74 MockComponentToolbarActionsFactory::MockComponentToolbarActionsFactory() { |
| 75 ComponentToolbarActionsFactory::SetTestingFactory(this); |
| 76 } |
| 77 |
| 78 MockComponentToolbarActionsFactory::~MockComponentToolbarActionsFactory() { |
| 79 ComponentToolbarActionsFactory::SetTestingFactory(nullptr); |
| 80 } |
| 81 |
| 82 ScopedVector<ToolbarActionViewController> |
| 83 MockComponentToolbarActionsFactory::GetComponentToolbarActions() { |
| 84 ScopedVector<ToolbarActionViewController> component_actions; |
| 85 component_actions.push_back(new MockComponentAction()); |
| 86 return component_actions.Pass(); |
| 87 } |
| 88 |
| 89 } // namespace |
| 90 |
| 91 class ComponentToolbarActionsBrowserTest : public InProcessBrowserTest { |
| 92 protected: |
| 93 ComponentToolbarActionsBrowserTest() {} |
| 94 virtual ~ComponentToolbarActionsBrowserTest() {} |
| 95 |
| 96 virtual void SetUpCommandLine(base::CommandLine* command_line) override { |
| 97 InProcessBrowserTest::SetUpCommandLine(command_line); |
| 98 enable_redesign_.reset(new extensions::FeatureSwitch::ScopedOverride( |
| 99 extensions::FeatureSwitch::extension_action_redesign(), true)); |
| 100 mock_actions_factory_.reset(new MockComponentToolbarActionsFactory()); |
| 101 } |
| 102 |
| 103 private: |
| 104 scoped_ptr<extensions::FeatureSwitch::ScopedOverride> enable_redesign_; |
| 105 scoped_ptr<MockComponentToolbarActionsFactory> mock_actions_factory_; |
| 106 |
| 107 DISALLOW_COPY_AND_ASSIGN(ComponentToolbarActionsBrowserTest); |
| 108 }; |
| 109 |
| 110 // Test that Component Toolbar Actions appear in the browser actions container |
| 111 // and can receive click events properly. |
| 112 IN_PROC_BROWSER_TEST_F(ComponentToolbarActionsBrowserTest, |
| 113 ComponentToolbarActionsShowUpAndRespondToClicks) { |
| 114 BrowserActionsContainer* browser_actions_container = |
| 115 BrowserView::GetBrowserViewForBrowser(browser()) |
| 116 ->toolbar()->browser_actions(); |
| 117 |
| 118 // There should be only one component action view. |
| 119 ASSERT_EQ(1u, browser_actions_container->num_browser_actions()); |
| 120 |
| 121 BrowserActionView* view = |
| 122 browser_actions_container->GetBrowserActionViewAt(0u); |
| 123 ASSERT_EQ(ToolbarActionViewController::TYPE_COMPONENT_ACTION, |
| 124 view->view_controller()->GetType()); |
| 125 MockComponentAction* mock_component_action = |
| 126 static_cast<MockComponentAction*>(view->view_controller()); |
| 127 |
| 128 // Test that clicking on the component action works. |
| 129 EXPECT_EQ(0u, mock_component_action->click_count()); |
| 130 view->Activate(); |
| 131 EXPECT_EQ(1u, mock_component_action->click_count()); |
| 132 } |
OLD | NEW |