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" |
11 #include "chrome/browser/ui/views/tabs/window_finder.h" | |
tapted
2016/06/01 11:29:55
remove?
themblsha
2016/06/03 17:42:08
Done.
| |
12 #import "ui/base/test/windowed_nsnotification_observer.h" | 12 #import "ui/base/test/windowed_nsnotification_observer.h" |
13 #include "ui/gfx/animation/tween.h" | |
13 | 14 |
14 namespace ui_test_utils { | 15 namespace ui_test_utils { |
15 | 16 |
17 namespace { | |
18 | |
19 // Runs a list of drag and drop |operations| on a single RunLoop. | |
20 // | |
21 // We need to execute drag and drop operations as a batch, so the nested | |
22 // RunMoveLoop()'s RunLoop is cancelled before the top-level one from which we | |
23 // post the simulated events, otherwise we'll deadlock. | |
24 class OperationRunner { | |
25 public: | |
26 static void Run(const std::list<DragAndDropOperation>& operations) { | |
27 OperationRunner runner(operations); | |
28 base::RunLoop run_loop; | |
29 runner.quit_closure_ = run_loop.QuitClosure(); | |
30 base::MessageLoop::current()->PostTask( | |
31 FROM_HERE, | |
32 base::Bind(&OperationRunner::Next, base::Unretained(&runner))); | |
33 run_loop.Run(); | |
34 } | |
35 | |
36 private: | |
37 explicit OperationRunner(const std::list<DragAndDropOperation>& operations) | |
38 : operations_(operations) {} | |
39 | |
40 void Next() { | |
41 if (operations_.empty()) { | |
42 base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_); | |
43 return; | |
44 } | |
45 | |
46 const DragAndDropOperation& op = operations_.front(); | |
47 operations_.pop_front(); | |
tapted
2016/06/01 11:29:55
move this to the end of the function, or don't use
themblsha
2016/06/03 17:42:07
ohshi, thanks! Wonder why it wasn't crashing on me
| |
48 auto next = base::Bind(&OperationRunner::Next, base::Unretained(this)); | |
49 switch (op.type()) { | |
50 case DragAndDropOperation::Type::Move: | |
51 ui_controls::SendMouseMoveNotifyWhenDone(op.point().x(), op.point().y(), | |
52 next); | |
53 break; | |
54 | |
55 case DragAndDropOperation::Type::MouseDown: | |
56 ui_controls::SendMouseEventsNotifyWhenDone(ui_controls::LEFT, | |
57 ui_controls::DOWN, next); | |
58 break; | |
59 | |
60 case DragAndDropOperation::Type::MouseUp: | |
61 ui_controls::SendMouseEventsNotifyWhenDone(ui_controls::LEFT, | |
62 ui_controls::UP, next); | |
63 break; | |
64 } | |
65 } | |
66 | |
67 ~OperationRunner() { DCHECK(operations_.empty()); } | |
68 | |
69 base::Closure quit_closure_; | |
70 std::list<DragAndDropOperation> operations_; | |
71 | |
72 DISALLOW_COPY_AND_ASSIGN(OperationRunner); | |
73 }; | |
74 | |
75 } // namespace | |
76 | |
16 void HideNativeWindow(gfx::NativeWindow window) { | 77 void HideNativeWindow(gfx::NativeWindow window) { |
17 [window orderOut:nil]; | 78 [window orderOut:nil]; |
18 } | 79 } |
19 | 80 |
20 bool ShowAndFocusNativeWindow(gfx::NativeWindow window) { | 81 bool ShowAndFocusNativeWindow(gfx::NativeWindow window) { |
21 // Make sure an unbundled program can get the input focus. | 82 // Make sure an unbundled program can get the input focus. |
22 ProcessSerialNumber psn = { 0, kCurrentProcess }; | 83 ProcessSerialNumber psn = { 0, kCurrentProcess }; |
23 TransformProcessType(&psn,kProcessTransformToForegroundApplication); | 84 TransformProcessType(&psn,kProcessTransformToForegroundApplication); |
24 SetFrontProcess(&psn); | 85 SetFrontProcess(&psn); |
25 | 86 |
(...skipping 11 matching lines...) Expand all Loading... | |
37 // This is because normal AppKit menu updating does not get invoked when | 98 // This is because normal AppKit menu updating does not get invoked when |
38 // events are sent via ui_test_utils::SendKeyPressSync. | 99 // events are sent via ui_test_utils::SendKeyPressSync. |
39 BOOL notification_observed = [async_waiter wait]; | 100 BOOL notification_observed = [async_waiter wait]; |
40 base::RunLoop().RunUntilIdle(); // There may be other events queued. Flush. | 101 base::RunLoop().RunUntilIdle(); // There may be other events queued. Flush. |
41 NSMenu* file_menu = [[[NSApp mainMenu] itemWithTag:IDC_FILE_MENU] submenu]; | 102 NSMenu* file_menu = [[[NSApp mainMenu] itemWithTag:IDC_FILE_MENU] submenu]; |
42 [[file_menu delegate] menuNeedsUpdate:file_menu]; | 103 [[file_menu delegate] menuNeedsUpdate:file_menu]; |
43 | 104 |
44 return !async_waiter || notification_observed; | 105 return !async_waiter || notification_observed; |
45 } | 106 } |
46 | 107 |
108 void DragAndDropSequence(const std::list<DragAndDropOperation>& operations) { | |
tapted
2016/06/01 11:29:55
nit: order should match the header:
- move everyt
themblsha
2016/06/03 17:42:08
Thanks. It's so easy to miss, isn't there an autom
tapted
2016/06/06 07:12:36
Sadly not :/
| |
109 OperationRunner::Run(operations); | |
110 } | |
111 | |
112 void DragAndDrop(const gfx::Point& from, const gfx::Point& to, int steps) { | |
113 DCHECK_GE(steps, 1); | |
114 std::list<DragAndDropOperation> operations; | |
115 operations.push_back(DragAndDropOperation::Move(from)); | |
116 operations.push_back(DragAndDropOperation::MouseDown()); | |
117 | |
118 for (int i = 1; i <= steps; ++i) { | |
119 const double progress = static_cast<double>(i) / steps; | |
120 operations.push_back(DragAndDropOperation::Move( | |
121 gfx::Point(gfx::Tween::IntValueBetween(progress, from.x(), to.x()), | |
122 gfx::Tween::IntValueBetween(progress, from.y(), to.y())))); | |
123 } | |
124 | |
125 operations.push_back(DragAndDropOperation::MouseUp()); | |
126 DragAndDropSequence(operations); | |
127 } | |
128 | |
129 // static | |
130 DragAndDropOperation DragAndDropOperation::Move(const gfx::Point& p) { | |
131 return DragAndDropOperation(Type::Move, p); | |
132 } | |
133 | |
134 // static | |
135 DragAndDropOperation DragAndDropOperation::MouseDown() { | |
136 return DragAndDropOperation(Type::MouseDown, gfx::Point()); | |
137 } | |
138 | |
139 // static | |
140 DragAndDropOperation DragAndDropOperation::MouseUp() { | |
141 return DragAndDropOperation(Type::MouseUp, gfx::Point()); | |
142 } | |
143 | |
47 } // namespace ui_test_utils | 144 } // namespace ui_test_utils |
OLD | NEW |