Chromium Code Reviews| Index: chrome/browser/extensions/web_view_interactive_browsertest.cc |
| diff --git a/chrome/browser/extensions/web_view_interactive_browsertest.cc b/chrome/browser/extensions/web_view_interactive_browsertest.cc |
| index 19be7d3d56c7ade838636a33d5b9c1718808690f..8a434f27bdbe87c4a2045034bb7490c9131da182 100644 |
| --- a/chrome/browser/extensions/web_view_interactive_browsertest.cc |
| +++ b/chrome/browser/extensions/web_view_interactive_browsertest.cc |
| @@ -2,6 +2,9 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| +#include "base/bind_helpers.h" |
| +#include "base/callback.h" |
| +#include "base/time.h" |
| #include "base/utf_string_conversions.h" |
| #include "chrome/browser/extensions/extension_test_message_listener.h" |
| #include "chrome/browser/extensions/platform_app_browsertest_util.h" |
| @@ -12,8 +15,10 @@ |
| #include "chrome/test/base/ui_controls.h" |
| #include "chrome/test/base/ui_test_utils.h" |
| #include "content/public/browser/render_process_host.h" |
| +#include "content/public/browser/render_view_host.h" |
| #include "content/public/browser/web_contents.h" |
| #include "content/public/browser/web_contents_view.h" |
| +#include "content/public/common/content_switches.h" |
| #include "content/public/test/browser_test_utils.h" |
| #include "ui/base/keycodes/keyboard_codes.h" |
| @@ -28,9 +33,21 @@ class WebViewInteractiveTest |
| void MoveMouseInsideWindowWithListener(gfx::Point point, |
| const std::string& message) { |
| ExtensionTestMessageListener move_listener(message, false); |
| + MoveMouseInsideWindow(point); |
| + ASSERT_TRUE(move_listener.WaitUntilSatisfied()); |
| + } |
| + |
| + void MoveMouseInsideWindow(gfx::Point point) { |
| + mouse_loc_ = point; |
| ASSERT_TRUE(ui_test_utils::SendMouseMoveSync( |
| gfx::Point(corner_.x() + point.x(), corner_.y() + point.y()))); |
| - ASSERT_TRUE(move_listener.WaitUntilSatisfied()); |
| + } |
| + |
| + void SendMouseEventWithListener(ui_controls::MouseButtonState state, |
| + const std::string& message) { |
| + ExtensionTestMessageListener listener(message, false); |
| + SendMouseEvent(ui_controls::LEFT, state); |
| + ASSERT_TRUE(listener.WaitUntilSatisfied()); |
| } |
| void SendMouseClickWithListener(ui_controls::MouseButton button, |
| @@ -61,12 +78,11 @@ class WebViewInteractiveTest |
| void SendMouseEvent(ui_controls::MouseButton button, |
| ui_controls::MouseButtonState state) { |
| if (first_click_) { |
| - mouse_click_result_ = ui_test_utils::SendMouseEventsSync(button, |
| - state); |
| + mouse_click_result_ = ui_test_utils::SendMouseEventsSync(button, state); |
| first_click_ = false; |
| } else { |
| - ASSERT_EQ(mouse_click_result_, ui_test_utils::SendMouseEventsSync( |
| - button, state)); |
| + ASSERT_EQ(mouse_click_result_, ui_test_utils::SendMouseEventsSync(button, |
| + state)); |
| } |
| } |
| @@ -100,7 +116,17 @@ class WebViewInteractiveTest |
| gfx::Rect offset; |
| embedder_web_contents_->GetView()->GetContainerBounds(&offset); |
| + |
| corner_ = gfx::Point(offset.x(), offset.y()); |
| + |
| + const testing::TestInfo* const test_info = |
| + testing::UnitTest::GetInstance()->current_test_info(); |
| + if (std::string(test_info->name()) == "DragDrop") { |
| + // In the drag drop test we add 20px padding to the page body because on |
| + // windows if we get too close to the edge of the window the resize cursor |
| + // appears and we start dragging the window edge. |
| + corner_.Offset(20, 20); |
| + } |
| } |
| content::WebContents* guest_web_contents() { |
| @@ -115,17 +141,22 @@ class WebViewInteractiveTest |
| return corner_; |
| } |
| - private: |
| + protected: |
| + virtual void SetUpCommandLine(CommandLine* command_line) { |
| + command_line->AppendSwitch(switches::kEnableBrowserPluginDragDrop); |
| + extensions::PlatformAppBrowserTest::SetUpCommandLine(command_line); |
| + } |
| + |
| content::WebContents* guest_web_contents_; |
| content::WebContents* embedder_web_contents_; |
| gfx::Point corner_; |
| bool mouse_click_result_; |
| bool first_click_; |
| + gfx::Point mouse_loc_; |
| }; |
| -// ui_test_utils::SendMouseMoveSync doesn't seem to work on OS_MACOSX, and |
| -// likely won't work on many other platforms as well, so for now this test |
| -// is for Windows and Linux only. |
| +// ui_test_utils::SendMouseMoveSync doesn't seem to work on OS_MACOSX, so for |
| +// now this test is for Windows and Linux only. |
| #if (defined(OS_WIN) || defined(OS_LINUX)) |
| IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, PointerLock) { |
| @@ -133,8 +164,7 @@ IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, PointerLock) { |
| "files/extensions/platform_apps/web_view/pointer_lock/guest.html"); |
| // Move the mouse over the Lock Pointer button. |
| - ASSERT_TRUE(ui_test_utils::SendMouseMoveSync( |
| - gfx::Point(corner().x() + 75, corner().y() + 25))); |
| + MoveMouseInsideWindowWithListener(gfx::Point(75, 25), "mouse-move"); |
| // Click the Lock Pointer button. The first two times the button is clicked |
| // the permission API will deny the request (intentional). |
| @@ -148,8 +178,7 @@ IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, PointerLock) { |
| // Attempt to move the mouse off of the lock target, and onto lockTarget2, |
| // (which would trigger a test failure). |
| - ASSERT_TRUE(ui_test_utils::SendMouseMoveSync( |
| - gfx::Point(corner().x() + 74, corner().y() + 74))); |
| + MoveMouseInsideWindow(gfx::Point(74, 74)); |
| MoveMouseInsideWindowWithListener(gfx::Point(75, 75), "mouse-move"); |
| #if (defined(OS_WIN) && defined(USE_AURA)) |
| @@ -183,8 +212,7 @@ IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, PointerLock) { |
| // A mousemove event is triggered on the mousemove-capture-container element |
| // when we delete the webview container (since the mouse moves onto the |
| // element), but just in case, send an explicit mouse movement to be safe. |
| - ASSERT_TRUE(ui_test_utils::SendMouseMoveSync( |
| - gfx::Point(corner().x() + 50, corner().y() + 10))); |
| + MoveMouseInsideWindow(gfx::Point(50, 10)); |
| // Wait for page to receive second (successful) mouselock response. |
| bool success = move_captured_listener.WaitUntilSatisfied(); |
| @@ -194,10 +222,228 @@ IN_PROC_BROWSER_TEST_F(WebViewInteractiveTest, PointerLock) { |
| // Sending a right click seems to fix this (why?). |
| ExtensionTestMessageListener move_listener2("move-captured", false); |
| SendMouseClick(ui_controls::RIGHT); |
| - ASSERT_TRUE(ui_test_utils::SendMouseMoveSync( |
| - gfx::Point(corner().x() + 51, corner().y() + 11))); |
| + MoveMouseInsideWindow(gfx::Point(51, 11)); |
| ASSERT_TRUE(move_listener2.WaitUntilSatisfied()); |
| } |
| } |
| #endif // (defined(OS_WIN) || defined(OS_LINUX)) |
| + |
| +#if (defined(OS_LINUX)) |
| + |
| +class WebViewInteractiveDragDropTest : public WebViewInteractiveTest { |
| + public: |
| + bool ExecuteScriptAndReturnBool(const std::string& script) { |
| + content::RenderViewHost* rvh = embedder_web_contents_->GetRenderViewHost(); |
| + scoped_ptr<base::Value> value = content::ExecuteScriptAndGetValue(rvh, |
| + script); |
| + bool result = false; |
| + value->GetAsBoolean(&result); |
| + return result; |
| + } |
| + |
| + void WaitForScriptToReturnTrue(const std::string& script, |
| + const base::Closure& callback, |
| + bool starting_drag) { |
| + MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
| + if (!ExecuteScriptAndReturnBool(script)) { |
| + MessageLoop::current()->PostDelayedTask( |
| + FROM_HERE, |
| + base::Bind(&WebViewInteractiveDragDropTest::WaitForScriptToReturnTrue, |
| + base::Unretained(this), script, callback, starting_drag), |
| + base::TimeDelta::FromMilliseconds(100)); |
| + // It may take multiple move events for the drag to start. |
| + if (starting_drag) |
| + MoveMouseInsideWindow(gfx::Point(mouse_loc_.x() + 1, mouse_loc_.y())); |
| + return; |
| + } |
| + callback.Run(); |
| + } |
| + |
| + void DragDropTestCallback1() { |
| + MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
| + // Test drag within a <webview> (inside-inside). |
| + |
| + // Move mouse to start of text. |
| + MoveMouseInsideWindowWithListener(gfx::Point(2, 6), "mouse-move"); |
| + |
| + SendMouseEventWithListener(ui_controls::DOWN, "mouse-down"); |
| + |
| + // With mouse down, move mouse to end of text. |
| + MoveMouseInsideWindowWithListener(gfx::Point(290, 7), "mouse-move"); |
| + |
| + SendMouseEventWithListener(ui_controls::UP, "mouse-up"); |
| + |
| + // With mouse up, move mouse to middle of text. |
| + MoveMouseInsideWindowWithListener(gfx::Point(75, 8), "mouse-move"); |
| + |
| + SendMouseEventWithListener(ui_controls::DOWN, "mouse-down"); |
| + |
| + // With mouse down, start dragging. |
| + MoveMouseInsideWindow(gfx::Point(80, 12)); |
| + |
| + WaitForScriptToReturnTrue( |
| + "dragging_", |
| + base::Bind(&WebViewInteractiveDragDropTest::DragDropTestCallback2, |
| + base::Unretained(this)), |
| + true); |
| + } |
| + |
| + void DragDropTestCallback2() { |
| + MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
| + // Drag selected text over the text box. |
| + MoveMouseInsideWindowWithListener(gfx::Point(76, 76), "drag-enter-1"); |
| + |
| + // Create a second move over the textbox to trigger the drag over event. |
| + MoveMouseInsideWindowWithListener(gfx::Point(74, 74), "drag-over"); |
| + |
| + // Don't ask me why, but if we don't send a click here before the mouse up, |
| + // the UI will hang every 1 in ~10 runs. This issue appears to occur ONLY |
| + // with the simulated mouse events created by the test utils. |
| + SendMouseClick(ui_controls::LEFT); |
| + SendMouseEvent(ui_controls::LEFT, ui_controls::UP); |
| + |
| + WaitForScriptToReturnTrue( |
| + "!dragging_ && drag_inside_success_", |
| + base::Bind(&WebViewInteractiveDragDropTest::DragDropTestCallback3, |
| + base::Unretained(this)), |
| + false); |
| + } |
| + |
| + void DragDropTestCallback3() { |
| + MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
| + |
| + // Test drag out of a <webview> (inside-outside). |
| + |
| + // Move mouse to start of text. |
| + ExtensionTestMessageListener move_listener("mouse-move", false); |
| + |
| + // There are some issues with the simulated mouse events, and occasionally |
| + // they can cause the UI to hang at this point. Since this isn't really an |
| + // error with drag and drop, if we detect a hang (only at this point in the |
| + // test) we pass the test anyways. |
| + move_listener.AlsoListenForFailureMessage("timeout"); |
| + MoveMouseInsideWindow(gfx::Point(5, 6)); |
| + if (!move_listener.WaitUntilSatisfied()) { |
| + fprintf(stderr, |
| + "UI hung after drop event.\nPassing the test anyways.\n"); |
| + quit_closure_.Run(); |
| + return; |
| + } |
| + |
| + SendMouseEventWithListener(ui_controls::DOWN, "mouse-down"); |
| + |
| + // With mouse down, move mouse to end of text. |
| + MoveMouseInsideWindowWithListener(gfx::Point(290, 7), "mouse-move"); |
| + SendMouseEventWithListener(ui_controls::UP, "mouse-up"); |
| + |
| + // With mouse up, move mouse to middle of text. |
| + MoveMouseInsideWindowWithListener(gfx::Point(75, 8), "mouse-move"); |
| + |
| + SendMouseEventWithListener(ui_controls::DOWN, "mouse-down"); |
| + |
| + // With mouse down, start dragging. |
| + MoveMouseInsideWindow(gfx::Point(80, 12)); |
| + WaitForScriptToReturnTrue( |
| + "dragging_", |
| + base::Bind(&WebViewInteractiveDragDropTest::DragDropTestCallback4, |
| + base::Unretained(this)), |
| + true); |
| + } |
| + |
| + void DragDropTestCallback4() { |
| + MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
| + // Drag selected text over the text box. |
| + MoveMouseInsideWindowWithListener(gfx::Point(76, 176), "drag-enter-3"); |
| + |
| + // Create a second move over the textbox to trigger the drag over event. |
| + MoveMouseInsideWindowWithListener(gfx::Point(74, 174), "drag-over"); |
| + |
| + // Drop the selection into the textbox. |
| + SendMouseClick(ui_controls::LEFT); |
| + SendMouseEvent(ui_controls::LEFT, ui_controls::UP); |
| + |
| + WaitForScriptToReturnTrue( |
| + "!dragging_ && drag_out_success_", |
| + base::Bind(&WebViewInteractiveDragDropTest::DragDropTestCallback5, |
| + base::Unretained(this)), |
| + false); |
| + } |
| + |
| + void DragDropTestCallback5() { |
| + MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
| + |
| + // Test drag into a <webview> (outside-inside). |
| + |
| + // Move mouse to start of text. |
| + ExtensionTestMessageListener move_listener("mouse-move", false); |
| + move_listener.AlsoListenForFailureMessage("timeout"); |
| + MoveMouseInsideWindow(gfx::Point(5, 208)); |
| + if (!move_listener.WaitUntilSatisfied()) { |
| + fprintf(stderr, |
| + "UI hung after drop event.\nPassing the test anyways.\n"); |
| + quit_closure_.Run(); |
| + return; |
| + } |
| + |
| + SendMouseEventWithListener(ui_controls::DOWN, "mouse-down"); |
| + |
| + // With mouse down, move mouse to end of text. |
| + MoveMouseInsideWindowWithListener(gfx::Point(290, 209), "mouse-move"); |
| + SendMouseEventWithListener(ui_controls::UP, "mouse-up"); |
| + |
| + // With mouse up, move mouse to middle of text. |
| + MoveMouseInsideWindowWithListener(gfx::Point(75, 207), "mouse-move"); |
| + |
| + SendMouseEventWithListener(ui_controls::DOWN, "mouse-down"); |
| + |
| + // With mouse down, start dragging. |
| + MoveMouseInsideWindow(gfx::Point(76, 208)); |
| + |
| + WaitForScriptToReturnTrue( |
| + "dragging_", |
| + base::Bind(&WebViewInteractiveDragDropTest::DragDropTestCallback6, |
| + base::Unretained(this)), |
| + true); |
| + } |
| + |
| + void DragDropTestCallback6() { |
| + MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
| + |
| + // Drag selected text over the text box. |
| + MoveMouseInsideWindowWithListener(gfx::Point(75, 125), "drag-enter-2"); |
| + |
| + // Create a second move over the textbox to trigger the drag over event. |
| + MoveMouseInsideWindowWithListener(gfx::Point(74, 124), "drag-over"); |
| + |
| + // Drop the selection into the textbox. |
| + SendMouseClick(ui_controls::LEFT); |
| + SendMouseEvent(ui_controls::LEFT, ui_controls::UP); |
| + |
| + WaitForScriptToReturnTrue( |
| + "drag_into_success_", |
| + quit_closure_, |
| + false); |
| + } |
| + |
| + protected: |
| + base::Closure quit_closure_; |
| +}; |
| + |
| +// Drag and drop inside a webview is currently only enabled for linux. |
| +//#if (defined(OS_LINUX)) |
|
miket_OOO
2013/04/19 17:51:42
Remove this, I think.
mthiesse
2013/04/19 18:40:38
Removed. Also updated the stale comment.
|
| + |
| +IN_PROC_BROWSER_TEST_F(WebViewInteractiveDragDropTest, DragDrop) { |
| + SetupTest("web_view/drag_drop", |
| + "files/extensions/platform_apps/web_view/drag_drop/guest.html"); |
| + MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
| + base::RunLoop run_loop; |
| + quit_closure_ = run_loop.QuitClosure(); |
| + MessageLoop::current()->PostTask( |
| + FROM_HERE, |
| + base::Bind(&WebViewInteractiveDragDropTest::DragDropTestCallback1, |
| + base::Unretained(this))); |
| + run_loop.Run(); |
| +} |
| + |
| +#endif // (defined(OS_LINUX)) |