| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/macros.h" | 5 #include "base/macros.h" |
| 6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/strings/utf_string_conversions.h" | |
| 8 #include "chrome/browser/extensions/browser_action_test_util.h" | 7 #include "chrome/browser/extensions/browser_action_test_util.h" |
| 9 #include "chrome/browser/ui/browser.h" | 8 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h" | 9 #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h" |
| 10 #include "chrome/browser/ui/toolbar/test_toolbar_action_view_controller.h" |
| 11 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h" | 11 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h" |
| 12 #include "chrome/test/base/in_process_browser_test.h" | 12 #include "chrome/test/base/in_process_browser_test.h" |
| 13 #include "extensions/common/feature_switch.h" | 13 #include "extensions/common/feature_switch.h" |
| 14 #include "grit/theme_resources.h" | |
| 15 #include "ui/base/resource/resource_bundle.h" | |
| 16 #include "ui/gfx/image/image.h" | |
| 17 #include "ui/gfx/image/image_skia.h" | |
| 18 | 14 |
| 19 namespace { | 15 namespace { |
| 20 | 16 |
| 21 const char kMockId[] = "mock_action"; | 17 const char kMockId[] = "mock_action"; |
| 22 | 18 |
| 23 class MockComponentAction : public ToolbarActionViewController { | |
| 24 public: | |
| 25 MockComponentAction() : click_count_(0u), id_(kMockId) {} | |
| 26 ~MockComponentAction() override {} | |
| 27 | |
| 28 // ToolbarActionButtonController: | |
| 29 const std::string& GetId() const override { return id_; } | |
| 30 void SetDelegate(ToolbarActionViewDelegate* delegate) override {} | |
| 31 gfx::Image GetIcon(content::WebContents* web_contents) override { | |
| 32 return ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 33 IDR_BROWSER_ACTION); | |
| 34 } | |
| 35 gfx::ImageSkia GetIconWithBadge() override { | |
| 36 return *GetIcon(nullptr).ToImageSkia(); | |
| 37 } | |
| 38 base::string16 GetActionName() const override { | |
| 39 return base::ASCIIToUTF16("Component Action"); | |
| 40 } | |
| 41 base::string16 GetAccessibleName(content::WebContents* web_contents) | |
| 42 const override { | |
| 43 return GetActionName(); | |
| 44 } | |
| 45 base::string16 GetTooltip(content::WebContents* web_contents) | |
| 46 const override { | |
| 47 return GetActionName(); | |
| 48 } | |
| 49 bool IsEnabled(content::WebContents* web_contents) const override { | |
| 50 return true; | |
| 51 } | |
| 52 bool WantsToRun(content::WebContents* web_contents) const override { | |
| 53 return false; | |
| 54 } | |
| 55 bool HasPopup(content::WebContents* web_contents) const override { | |
| 56 return true; | |
| 57 } | |
| 58 void HidePopup() override {} | |
| 59 gfx::NativeView GetPopupNativeView() override { return nullptr; } | |
| 60 ui::MenuModel* GetContextMenu() override { return nullptr; } | |
| 61 bool CanDrag() const override { return false; } | |
| 62 bool IsMenuRunning() const override { return false; } | |
| 63 bool ExecuteAction(bool by_user) override { | |
| 64 ++click_count_; | |
| 65 return false; | |
| 66 } | |
| 67 void UpdateState() override {} | |
| 68 | |
| 69 size_t click_count() const { return click_count_; } | |
| 70 | |
| 71 private: | |
| 72 size_t click_count_; | |
| 73 std::string id_; | |
| 74 | |
| 75 DISALLOW_COPY_AND_ASSIGN(MockComponentAction); | |
| 76 }; | |
| 77 | |
| 78 class MockComponentToolbarActionsFactory | 19 class MockComponentToolbarActionsFactory |
| 79 : public ComponentToolbarActionsFactory { | 20 : public ComponentToolbarActionsFactory { |
| 80 public: | 21 public: |
| 81 MockComponentToolbarActionsFactory(); | 22 MockComponentToolbarActionsFactory(); |
| 82 virtual ~MockComponentToolbarActionsFactory(); | 23 virtual ~MockComponentToolbarActionsFactory(); |
| 83 | 24 |
| 84 // ComponentToolbarActionsFactory: | 25 // ComponentToolbarActionsFactory: |
| 85 ScopedVector<ToolbarActionViewController> GetComponentToolbarActions() | 26 ScopedVector<ToolbarActionViewController> GetComponentToolbarActions() |
| 86 override; | 27 override; |
| 87 | 28 |
| 88 const std::vector<MockComponentAction*>& weak_actions() const { | 29 const std::vector<TestToolbarActionViewController*>& weak_actions() const { |
| 89 return weak_actions_; | 30 return weak_actions_; |
| 90 } | 31 } |
| 91 | 32 |
| 92 private: | 33 private: |
| 93 // A (weak) set of all created actions. | 34 // A (weak) set of all created actions. |
| 94 std::vector<MockComponentAction*> weak_actions_; | 35 std::vector<TestToolbarActionViewController*> weak_actions_; |
| 95 | 36 |
| 96 DISALLOW_COPY_AND_ASSIGN(MockComponentToolbarActionsFactory); | 37 DISALLOW_COPY_AND_ASSIGN(MockComponentToolbarActionsFactory); |
| 97 }; | 38 }; |
| 98 | 39 |
| 99 MockComponentToolbarActionsFactory::MockComponentToolbarActionsFactory() { | 40 MockComponentToolbarActionsFactory::MockComponentToolbarActionsFactory() { |
| 100 ComponentToolbarActionsFactory::SetTestingFactory(this); | 41 ComponentToolbarActionsFactory::SetTestingFactory(this); |
| 101 } | 42 } |
| 102 | 43 |
| 103 MockComponentToolbarActionsFactory::~MockComponentToolbarActionsFactory() { | 44 MockComponentToolbarActionsFactory::~MockComponentToolbarActionsFactory() { |
| 104 ComponentToolbarActionsFactory::SetTestingFactory(nullptr); | 45 ComponentToolbarActionsFactory::SetTestingFactory(nullptr); |
| 105 } | 46 } |
| 106 | 47 |
| 107 ScopedVector<ToolbarActionViewController> | 48 ScopedVector<ToolbarActionViewController> |
| 108 MockComponentToolbarActionsFactory::GetComponentToolbarActions() { | 49 MockComponentToolbarActionsFactory::GetComponentToolbarActions() { |
| 109 ScopedVector<ToolbarActionViewController> component_actions; | 50 ScopedVector<ToolbarActionViewController> component_actions; |
| 110 MockComponentAction* action = new MockComponentAction(); | 51 TestToolbarActionViewController* action = |
| 52 new TestToolbarActionViewController(kMockId); |
| 111 component_actions.push_back(action); | 53 component_actions.push_back(action); |
| 112 weak_actions_.push_back(action); | 54 weak_actions_.push_back(action); |
| 113 return component_actions.Pass(); | 55 return component_actions.Pass(); |
| 114 } | 56 } |
| 115 | 57 |
| 116 } // namespace | 58 } // namespace |
| 117 | 59 |
| 118 class ComponentToolbarActionsBrowserTest : public InProcessBrowserTest { | 60 class ComponentToolbarActionsBrowserTest : public InProcessBrowserTest { |
| 119 protected: | 61 protected: |
| 120 ComponentToolbarActionsBrowserTest() {} | 62 ComponentToolbarActionsBrowserTest() {} |
| (...skipping 24 matching lines...) Expand all Loading... |
| 145 BrowserActionTestUtil browser_actions_bar(browser()); | 87 BrowserActionTestUtil browser_actions_bar(browser()); |
| 146 | 88 |
| 147 // There should be only one component action view. | 89 // There should be only one component action view. |
| 148 ASSERT_EQ(1, browser_actions_bar.NumberOfBrowserActions()); | 90 ASSERT_EQ(1, browser_actions_bar.NumberOfBrowserActions()); |
| 149 | 91 |
| 150 // Even though the method says "ExtensionId", this actually refers to any id | 92 // Even though the method says "ExtensionId", this actually refers to any id |
| 151 // for the action. | 93 // for the action. |
| 152 EXPECT_EQ(kMockId, browser_actions_bar.GetExtensionId(0)); | 94 EXPECT_EQ(kMockId, browser_actions_bar.GetExtensionId(0)); |
| 153 | 95 |
| 154 // There should only have been one created component action. | 96 // There should only have been one created component action. |
| 155 const std::vector<MockComponentAction*> weak_actions = | 97 const std::vector<TestToolbarActionViewController*>& weak_actions = |
| 156 mock_factory()->weak_actions(); | 98 mock_factory()->weak_actions(); |
| 157 ASSERT_EQ(1u, weak_actions.size()); | 99 ASSERT_EQ(1u, weak_actions.size()); |
| 158 MockComponentAction* mock_component_action = weak_actions[0]; | 100 TestToolbarActionViewController* mock_component_action = weak_actions[0]; |
| 159 ASSERT_TRUE(mock_component_action); | 101 ASSERT_TRUE(mock_component_action); |
| 160 | 102 |
| 161 // Test that clicking on the component action works. | 103 // Test that clicking on the component action works. |
| 162 EXPECT_EQ(0u, mock_component_action->click_count()); | 104 EXPECT_EQ(0, mock_component_action->execute_action_count()); |
| 163 browser_actions_bar.Press(0); | 105 browser_actions_bar.Press(0); |
| 164 EXPECT_EQ(1u, mock_component_action->click_count()); | 106 EXPECT_EQ(1, mock_component_action->execute_action_count()); |
| 165 } | 107 } |
| OLD | NEW |