Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/test/base/interactive_test_utils.h" | 5 #include "chrome/test/base/interactive_test_utils.h" |
| 6 | 6 |
| 7 #include <Carbon/Carbon.h> | 7 #include <Carbon/Carbon.h> |
| 8 #import <Cocoa/Cocoa.h> | 8 #import <Cocoa/Cocoa.h> |
| 9 | 9 |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 #include "chrome/app/chrome_command_ids.h" | 10 #include "chrome/app/chrome_command_ids.h" |
| 12 #import "ui/base/test/windowed_nsnotification_observer.h" | 11 #import "ui/base/test/windowed_nsnotification_observer.h" |
| 12 #include "ui/gfx/animation/tween.h" | |
| 13 | 13 |
| 14 namespace ui_test_utils { | 14 namespace ui_test_utils { |
| 15 | 15 |
| 16 namespace { | |
| 17 | |
| 18 // Runs a list of drag and drop |operations| on a single RunLoop. | |
| 19 // | |
| 20 // We need to execute drag and drop operations as a batch, so the nested | |
| 21 // RunMoveLoop()'s RunLoop is cancelled before the top-level one from which we | |
| 22 // post the simulated events, otherwise we'll deadlock. | |
| 23 class OperationRunner { | |
| 24 public: | |
| 25 static void Run(const std::list<DragAndDropOperation>& operations) { | |
| 26 OperationRunner runner(operations); | |
| 27 base::RunLoop run_loop; | |
| 28 runner.quit_closure_ = run_loop.QuitClosure(); | |
| 29 base::MessageLoop::current()->PostTask( | |
| 30 FROM_HERE, | |
| 31 base::Bind(&OperationRunner::Next, base::Unretained(&runner))); | |
| 32 run_loop.Run(); | |
| 33 } | |
| 34 | |
| 35 private: | |
| 36 explicit OperationRunner(const std::list<DragAndDropOperation>& operations) | |
| 37 : operations_(operations) {} | |
| 38 | |
| 39 void Next() { | |
| 40 if (operations_.empty()) { | |
| 41 base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_); | |
| 42 return; | |
| 43 } | |
| 44 | |
| 45 const DragAndDropOperation op = operations_.front(); | |
| 46 operations_.pop_front(); | |
| 47 auto next = base::Bind(&OperationRunner::Next, base::Unretained(this)); | |
| 48 switch (op.type()) { | |
| 49 case DragAndDropOperation::Type::Move: | |
| 50 ui_controls::SendMouseMoveNotifyWhenDone(op.point().x(), op.point().y(), | |
| 51 next); | |
| 52 break; | |
| 53 | |
| 54 case DragAndDropOperation::Type::MouseDown: | |
| 55 ui_controls::SendMouseEventsNotifyWhenDone(ui_controls::LEFT, | |
| 56 ui_controls::DOWN, next); | |
| 57 break; | |
| 58 | |
| 59 case DragAndDropOperation::Type::MouseUp: | |
| 60 ui_controls::SendMouseEventsNotifyWhenDone(ui_controls::LEFT, | |
| 61 ui_controls::UP, next); | |
| 62 break; | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 ~OperationRunner() { DCHECK(operations_.empty()); } | |
| 67 | |
| 68 base::Closure quit_closure_; | |
| 69 std::list<DragAndDropOperation> operations_; | |
| 70 | |
| 71 DISALLOW_COPY_AND_ASSIGN(OperationRunner); | |
| 72 }; | |
| 73 | |
| 74 } // namespace | |
| 75 | |
| 76 void DragAndDropSequence(const std::list<DragAndDropOperation>& operations) { | |
| 77 OperationRunner::Run(operations); | |
| 78 } | |
| 79 | |
| 80 void DragAndDrop(const gfx::Point& from, const gfx::Point& to, int steps) { | |
| 81 DCHECK_GE(steps, 1); | |
| 82 std::list<DragAndDropOperation> operations; | |
| 83 operations.push_back(DragAndDropOperation::Move(from)); | |
| 84 operations.push_back(DragAndDropOperation::MouseDown()); | |
| 85 | |
| 86 for (int i = 1; i <= steps; ++i) { | |
| 87 const double progress = static_cast<double>(i) / steps; | |
| 88 operations.push_back(DragAndDropOperation::Move( | |
| 89 gfx::Point(gfx::Tween::IntValueBetween(progress, from.x(), to.x()), | |
| 90 gfx::Tween::IntValueBetween(progress, from.y(), to.y())))); | |
| 91 } | |
| 92 | |
| 93 operations.push_back(DragAndDropOperation::MouseUp()); | |
| 94 DragAndDropSequence(operations); | |
| 95 } | |
| 96 | |
| 16 void HideNativeWindow(gfx::NativeWindow window) { | 97 void HideNativeWindow(gfx::NativeWindow window) { |
| 17 [window orderOut:nil]; | 98 [window orderOut:nil]; |
| 18 } | 99 } |
| 19 | 100 |
| 20 bool ShowAndFocusNativeWindow(gfx::NativeWindow window) { | 101 bool ShowAndFocusNativeWindow(gfx::NativeWindow window) { |
| 21 // Make sure an unbundled program can get the input focus. | 102 // Make sure an unbundled program can get the input focus. |
| 22 ProcessSerialNumber psn = { 0, kCurrentProcess }; | 103 ProcessSerialNumber psn = { 0, kCurrentProcess }; |
| 23 TransformProcessType(&psn,kProcessTransformToForegroundApplication); | 104 TransformProcessType(&psn,kProcessTransformToForegroundApplication); |
| 24 SetFrontProcess(&psn); | 105 SetFrontProcess(&psn); |
| 25 | 106 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 37 // This is because normal AppKit menu updating does not get invoked when | 118 // This is because normal AppKit menu updating does not get invoked when |
| 38 // events are sent via ui_test_utils::SendKeyPressSync. | 119 // events are sent via ui_test_utils::SendKeyPressSync. |
| 39 BOOL notification_observed = [async_waiter wait]; | 120 BOOL notification_observed = [async_waiter wait]; |
| 40 base::RunLoop().RunUntilIdle(); // There may be other events queued. Flush. | 121 base::RunLoop().RunUntilIdle(); // There may be other events queued. Flush. |
| 41 NSMenu* file_menu = [[[NSApp mainMenu] itemWithTag:IDC_FILE_MENU] submenu]; | 122 NSMenu* file_menu = [[[NSApp mainMenu] itemWithTag:IDC_FILE_MENU] submenu]; |
| 42 [[file_menu delegate] menuNeedsUpdate:file_menu]; | 123 [[file_menu delegate] menuNeedsUpdate:file_menu]; |
| 43 | 124 |
| 44 return !async_waiter || notification_observed; | 125 return !async_waiter || notification_observed; |
| 45 } | 126 } |
| 46 | 127 |
| 128 // static | |
| 129 DragAndDropOperation DragAndDropOperation::Move(const gfx::Point& p) { | |
|
tapted
2016/06/06 07:12:36
nit (per previous comment) This should move up as
themblsha
2016/06/06 17:20:27
Whoops, sorry.
| |
| 130 return DragAndDropOperation(Type::Move, p); | |
| 131 } | |
| 132 | |
| 133 // static | |
| 134 DragAndDropOperation DragAndDropOperation::MouseDown() { | |
| 135 return DragAndDropOperation(Type::MouseDown, gfx::Point()); | |
| 136 } | |
| 137 | |
| 138 // static | |
| 139 DragAndDropOperation DragAndDropOperation::MouseUp() { | |
| 140 return DragAndDropOperation(Type::MouseUp, gfx::Point()); | |
| 141 } | |
| 142 | |
| 47 } // namespace ui_test_utils | 143 } // namespace ui_test_utils |
| OLD | NEW |