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.
5 #include "base/location.h"
6 #include "base/run_loop.h"
7 #include "base/single_thread_task_runner.h"
8 #include "chrome/browser/extensions/extension_browsertest.h"
9 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h"
10 #include "chrome/browser/ui/views/frame/browser_view.h"
11 #include "chrome/browser/ui/views/frame/test_with_browser_view.h"
12 #include "chrome/browser/ui/views/tabs/tab_drag_controller_interactive_uitest.h"
13 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h"
14 #include "chrome/browser/ui/views/toolbar/extension_toolbar_menu_view.h"
15 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
16 #include "chrome/browser/ui/views/toolbar/wrench_toolbar_button.h"
17 #include "chrome/test/base/interactive_test_utils.h"
18 #include "extensions/common/feature_switch.h"
20 // Borrowed from chrome/browser/ui/views/bookmarks/bookmark_bar_view_test.cc,
21 // since these are also disabled on Linux for drag and drop.
22 // TODO(erg): Fix DND tests on linux_aura. crbug.com/163931
23 #if defined(OS_LINUX) && defined(USE_AURA)
24 #define MAYBE(x) DISABLED_##x
29 class ToolbarViewInteractiveUITest
: public ExtensionBrowserTest
{
31 ToolbarViewInteractiveUITest();
32 ~ToolbarViewInteractiveUITest() override
;
35 ToolbarView
* toolbar_view() { return toolbar_view_
; }
36 BrowserActionsContainer
* browser_actions() { return browser_actions_
; }
38 // Performs a drag-and-drop operation by moving the mouse to |start|, clicking
39 // the left button, moving the mouse to |end|, and releasing the left button.
40 // TestWhileInDragOperation() is called after the mouse has moved to |end|,
41 // but before the click is released.
42 void DoDragAndDrop(const gfx::Point
& start
, const gfx::Point
& end
);
44 // A function to perform testing actions while in the drag operation from
46 void TestWhileInDragOperation();
49 // Finishes the drag-and-drop operation started in DoDragAndDrop().
50 void FinishDragAndDrop(const base::Closure
& quit_closure
);
52 // InProcessBrowserTest:
53 void SetUpCommandLine(base::CommandLine
* command_line
) override
;
54 void SetUpOnMainThread() override
;
55 void TearDownOnMainThread() override
;
57 ToolbarView
* toolbar_view_
;
59 BrowserActionsContainer
* browser_actions_
;
61 // The drag-and-drop background thread.
62 scoped_ptr
<base::Thread
> dnd_thread_
;
64 // Override the extensions-action-redesign switch.
65 scoped_ptr
<extensions::FeatureSwitch::ScopedOverride
> feature_override_
;
68 ToolbarViewInteractiveUITest::ToolbarViewInteractiveUITest()
69 : toolbar_view_(NULL
),
70 browser_actions_(NULL
) {
73 ToolbarViewInteractiveUITest::~ToolbarViewInteractiveUITest() {
76 void ToolbarViewInteractiveUITest::DoDragAndDrop(const gfx::Point
& start
,
77 const gfx::Point
& end
) {
78 // Much of this function is modeled after methods in ViewEventTestBase (in
79 // particular, the |dnd_thread_|, but it's easier to move that here than try
80 // to make ViewEventTestBase play nice with a BrowserView (for the toolbar).
81 // TODO(devlin): In a perfect world, this would be factored better.
83 // Send the mouse to |start|, and click.
84 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(start
));
85 ASSERT_TRUE(ui_test_utils::SendMouseEventsSync(
86 ui_controls::LEFT
, ui_controls::DOWN
));
88 scoped_refptr
<content::MessageLoopRunner
> runner
=
89 new content::MessageLoopRunner();
91 ui_controls::SendMouseMoveNotifyWhenDone(
94 base::Bind(&ToolbarViewInteractiveUITest::FinishDragAndDrop
,
95 base::Unretained(this),
96 runner
->QuitClosure()));
98 // Also post a move task to the drag and drop thread.
99 if (!dnd_thread_
.get()) {
100 dnd_thread_
.reset(new base::Thread("mouse_move_thread"));
101 dnd_thread_
->Start();
104 dnd_thread_
->task_runner()->PostDelayedTask(
105 FROM_HERE
, base::Bind(base::IgnoreResult(&ui_controls::SendMouseMove
),
107 base::TimeDelta::FromMilliseconds(200));
110 // The wrench menu should have closed once the drag-and-drop completed.
111 EXPECT_FALSE(toolbar_view()->IsWrenchMenuShowing());
114 void ToolbarViewInteractiveUITest::TestWhileInDragOperation() {
115 EXPECT_TRUE(toolbar_view()->IsWrenchMenuShowing());
118 void ToolbarViewInteractiveUITest::FinishDragAndDrop(
119 const base::Closure
& quit_closure
) {
121 TestWhileInDragOperation();
122 ui_controls::SendMouseEvents(ui_controls::LEFT
, ui_controls::UP
);
123 ui_controls::RunClosureAfterAllPendingUIEvents(
127 void ToolbarViewInteractiveUITest::SetUpCommandLine(
128 base::CommandLine
* command_line
) {
129 ExtensionBrowserTest::SetUpCommandLine(command_line
);
130 // We do this before the rest of the setup because it can affect how the views
132 feature_override_
.reset(new extensions::FeatureSwitch::ScopedOverride(
133 extensions::FeatureSwitch::extension_action_redesign(), true));
134 ToolbarActionsBar::disable_animations_for_testing_
= true;
135 WrenchToolbarButton::g_open_wrench_immediately_for_testing
= true;
138 void ToolbarViewInteractiveUITest::SetUpOnMainThread() {
139 ExtensionBrowserTest::SetUpOnMainThread();
140 ExtensionToolbarMenuView::set_close_menu_delay_for_testing(0);
142 toolbar_view_
= BrowserView::GetBrowserViewForBrowser(browser())->toolbar();
143 browser_actions_
= toolbar_view_
->browser_actions();
146 void ToolbarViewInteractiveUITest::TearDownOnMainThread() {
147 ToolbarActionsBar::disable_animations_for_testing_
= false;
148 WrenchToolbarButton::g_open_wrench_immediately_for_testing
= false;
151 IN_PROC_BROWSER_TEST_F(ToolbarViewInteractiveUITest
,
152 MAYBE(TestWrenchMenuOpensOnDrag
)) {
153 // Load an extension that has a browser action.
154 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("api_test")
155 .AppendASCII("browser_action")
156 .AppendASCII("basics")));
157 base::RunLoop().RunUntilIdle(); // Ensure the extension is fully loaded.
159 ASSERT_EQ(1u, browser_actions()->VisibleBrowserActions());
161 ToolbarActionView
* view
= browser_actions()->GetToolbarActionViewAt(0);
164 gfx::Point browser_action_view_loc
= test::GetCenterInScreenCoordinates(view
);
165 gfx::Point wrench_button_loc
=
166 test::GetCenterInScreenCoordinates(toolbar_view()->app_menu());
168 // Perform a drag and drop from the browser action view to the wrench button,
169 // which should open the wrench menu.
170 DoDragAndDrop(browser_action_view_loc
, wrench_button_loc
);