Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

Side by Side Diff: ui/views/mus/drag_interactive_uitest.cc

Issue 2759463002: aura-mus: create an interactive ui test for drag and drop. (Closed)
Patch Set: Merge with master Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/views/mus/BUILD.gn ('k') | ui/views/mus/interactive_ui_tests_manifest.json » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 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.
4
5 #include "base/strings/utf_string_conversions.h"
6 #include "services/ui/public/interfaces/window_server_test.mojom.h"
7 #include "services/ui/public/interfaces/window_tree_constants.mojom.h"
8 #include "ui/aura/mus/in_flight_change.h"
9 #include "ui/aura/mus/window_tree_host_mus.h"
10 #include "ui/aura/test/mus/change_completion_waiter.h"
11 #include "ui/events/event.h"
12 #include "ui/events/event_utils.h"
13 #include "ui/views/mus/mus_client.h"
14 #include "ui/views/test/widget_test.h"
15 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
16 #include "ui/views/widget/widget.h"
17
18 namespace views {
19 namespace test {
20 namespace {
21
22 // A view used in DragTestInteractive.DragTest as a drag source.
23 class DraggableView : public views::View {
24 public:
25 DraggableView() {}
26 ~DraggableView() override {}
27
28 // views::View overrides:
29 int GetDragOperations(const gfx::Point& press_pt) override {
30 return ui::DragDropTypes::DRAG_MOVE;
31 }
32 void WriteDragData(const gfx::Point& press_pt,
33 OSExchangeData* data) override {
34 data->SetString(base::UTF8ToUTF16("test"));
35 }
36
37 private:
38 DISALLOW_COPY_AND_ASSIGN(DraggableView);
39 };
40
41 // A view used in DragTestInteractive.DragTest as a drop target.
42 class TargetView : public views::View {
43 public:
44 TargetView() : dropped_(false) {}
45 ~TargetView() override {}
46
47 void WaitForDropped(base::Closure quit_closure) {
48 if (dropped_) {
49 quit_closure.Run();
50 return;
51 }
52
53 quit_closure_ = quit_closure;
54 }
55
56 // views::View overrides:
57 bool GetDropFormats(
58 int* formats,
59 std::set<ui::Clipboard::FormatType>* format_types) override {
60 *formats = ui::OSExchangeData::STRING;
61 return true;
62 }
63 bool AreDropTypesRequired() override { return false; }
64 bool CanDrop(const OSExchangeData& data) override { return true; }
65 int OnDragUpdated(const ui::DropTargetEvent& event) override {
66 return ui::DragDropTypes::DRAG_MOVE;
67 }
68 int OnPerformDrop(const ui::DropTargetEvent& event) override {
69 dropped_ = true;
70 if (quit_closure_)
71 quit_closure_.Run();
72 return ui::DragDropTypes::DRAG_MOVE;
73 }
74
75 bool dropped() const { return dropped_; }
76
77 private:
78 bool dropped_;
79
80 base::Closure quit_closure_;
81
82 DISALLOW_COPY_AND_ASSIGN(TargetView);
83 };
84
85 std::unique_ptr<ui::PointerEvent> CreateMouseMoveEvent(int x, int y) {
86 return base::MakeUnique<ui::PointerEvent>(ui::MouseEvent(
87 ui::ET_MOUSE_MOVED, gfx::Point(x, y), gfx::Point(x, y),
88 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, ui::EF_NONE));
89 }
90
91 std::unique_ptr<ui::PointerEvent> CreateMouseDownEvent(int x, int y) {
92 return base::MakeUnique<ui::PointerEvent>(
93 ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(x, y), gfx::Point(x, y),
94 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
95 ui::EF_LEFT_MOUSE_BUTTON));
96 }
97
98 std::unique_ptr<ui::PointerEvent> CreateMouseUpEvent(int x, int y) {
99 return base::MakeUnique<ui::PointerEvent>(
100 ui::MouseEvent(ui::ET_MOUSE_RELEASED, gfx::Point(x, y), gfx::Point(x, y),
101 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
102 ui::EF_LEFT_MOUSE_BUTTON));
103 }
104
105 } // namespace
106
107 using DragTestInteractive = WidgetTest;
108
109 // Dispatch of events is asynchronous so most of DragTestInteractive.DragTest
110 // consists of callback functions which will perform an action after the
111 // previous action has completed.
112 void DragTest_Part3(int64_t display_id,
113 const base::Closure& quit_closure,
114 bool result) {
115 EXPECT_TRUE(result);
116 quit_closure.Run();
117 }
118
119 void DragTest_Part2(int64_t display_id,
120 const base::Closure& quit_closure,
121 bool result) {
122 EXPECT_TRUE(result);
123 if (!result)
124 quit_closure.Run();
125
126 ui::mojom::WindowServerTest* server_test =
127 MusClient::Get()->GetTestingInterface();
128 server_test->DispatchEvent(
129 display_id, CreateMouseUpEvent(30, 30),
130 base::Bind(&DragTest_Part3, display_id, quit_closure));
131 }
132
133 void DragTest_Part1(int64_t display_id,
134 const base::Closure& quit_closure,
135 bool result) {
136 EXPECT_TRUE(result);
137 if (!result)
138 quit_closure.Run();
139
140 ui::mojom::WindowServerTest* server_test =
141 MusClient::Get()->GetTestingInterface();
142 server_test->DispatchEvent(
143 display_id, CreateMouseMoveEvent(30, 30),
144 base::Bind(&DragTest_Part2, display_id, quit_closure));
145 }
146
147 TEST_F(DragTestInteractive, DragTest) {
148 Widget* source_widget = CreateTopLevelFramelessPlatformWidget();
149 View* source_view = new DraggableView;
150 source_widget->SetContentsView(source_view);
151 source_widget->Show();
152
153 aura::test::ChangeCompletionWaiter source_waiter(
154 MusClient::Get()->window_tree_client(), aura::ChangeType::BOUNDS, true);
155 source_widget->SetBounds(gfx::Rect(0, 0, 20, 20));
156 source_waiter.Wait();
157
158 Widget* target_widget = CreateTopLevelFramelessPlatformWidget();
159 TargetView* target_view = new TargetView;
160 target_widget->SetContentsView(target_view);
161 target_widget->Show();
162
163 aura::test::ChangeCompletionWaiter target_waiter(
164 MusClient::Get()->window_tree_client(), aura::ChangeType::BOUNDS, true);
165 target_widget->SetBounds(gfx::Rect(20, 20, 20, 20));
166 target_waiter.Wait();
167
168 auto* dnwa =
169 static_cast<DesktopNativeWidgetAura*>(source_widget->native_widget());
170 ASSERT_TRUE(dnwa);
171 auto* wth = static_cast<aura::WindowTreeHostMus*>(dnwa->host());
172 ASSERT_TRUE(wth);
173 int64_t display_id = wth->display_id();
174
175 {
176 base::RunLoop run_loop;
177 ui::mojom::WindowServerTest* server_test =
178 MusClient::Get()->GetTestingInterface();
179 server_test->DispatchEvent(
180 display_id, CreateMouseDownEvent(10, 10),
181 base::Bind(&DragTest_Part1, display_id, run_loop.QuitClosure()));
182
183 run_loop.Run();
184 }
185
186 // Wait for the event dispatch to cause the final drop signal.
187 {
188 base::RunLoop run_loop;
189 target_view->WaitForDropped(run_loop.QuitClosure());
190 run_loop.Run();
191 }
192
193 EXPECT_TRUE(target_view->dropped());
194
195 target_widget->Close();
196 source_widget->Close();
197 RunPendingMessages();
198 }
199
200 } // namespace test
201 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/mus/BUILD.gn ('k') | ui/views/mus/interactive_ui_tests_manifest.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698