forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextended_drag_source_unittest.cc
420 lines (360 loc) · 16.8 KB
/
extended_drag_source_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/exo/extended_drag_source.h"
#include <memory>
#include <string>
#include "ash/drag_drop/drag_drop_controller.h"
#include "ash/drag_drop/toplevel_window_drag_delegate.h"
#include "ash/shell.h"
#include "ash/wm/toplevel_window_event_handler.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/gmock_callback_support.h"
#include "components/exo/buffer.h"
#include "components/exo/data_source.h"
#include "components/exo/data_source_delegate.h"
#include "components/exo/seat.h"
#include "components/exo/shell_surface.h"
#include "components/exo/surface.h"
#include "components/exo/test/exo_test_base.h"
#include "components/exo/test/exo_test_data_exchange_delegate.h"
#include "components/exo/test/exo_test_helper.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/client/drag_drop_client.h"
#include "ui/aura/client/drag_drop_delegate.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/dragdrop/drag_drop_types.h"
#include "ui/base/dragdrop/mojom/drag_drop_types.mojom-shared.h"
#include "ui/base/dragdrop/os_exchange_data.h"
#include "ui/events/event_utils.h"
#include "ui/events/test/event_generator.h"
#include "ui/events/test/events_test_utils.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/vector2d.h"
using ::testing::_;
using ::testing::InvokeWithoutArgs;
namespace exo {
namespace {
void DispatchGesture(ui::EventType gesture_type, gfx::Point location) {
ui::GestureEventDetails event_details(gesture_type);
ui::GestureEvent gesture_event(location.x(), location.y(), 0,
ui::EventTimeForNow(), event_details);
ui::EventSource* event_source =
ash::Shell::GetPrimaryRootWindow()->GetHost()->GetEventSource();
ui::EventSourceTestApi event_source_test(event_source);
ui::EventDispatchDetails details =
event_source_test.SendEventToSink(&gesture_event);
CHECK(!details.dispatcher_destroyed);
}
class TestDataSourceDelegate : public DataSourceDelegate {
public:
TestDataSourceDelegate() {}
TestDataSourceDelegate(const TestDataSourceDelegate&) = delete;
TestDataSourceDelegate& operator=(const TestDataSourceDelegate&) = delete;
~TestDataSourceDelegate() override = default;
bool cancelled() const { return cancelled_; }
// Overridden from DataSourceDelegate:
void OnDataSourceDestroying(DataSource* device) override { delete this; }
void OnTarget(const absl::optional<std::string>& mime_type) override {}
void OnSend(const std::string& mime_type, base::ScopedFD fd) override {}
void OnCancelled() override { cancelled_ = true; }
void OnDndDropPerformed() override {}
void OnDndFinished() override { finished_ = true; }
void OnAction(DndAction dnd_action) override {}
bool CanAcceptDataEventsForSurface(Surface* surface) const override {
return true;
}
private:
bool cancelled_ = false;
bool finished_ = false;
};
class TestExtendedDragSourceDelegate : public ExtendedDragSource::Delegate {
public:
TestExtendedDragSourceDelegate(bool allow_drop_no_target, bool lock_cursor)
: allow_drap_no_target_(allow_drop_no_target),
lock_cursor_(lock_cursor) {}
TestExtendedDragSourceDelegate(const TestExtendedDragSourceDelegate&) =
delete;
TestExtendedDragSourceDelegate& operator=(
const TestExtendedDragSourceDelegate&) = delete;
~TestExtendedDragSourceDelegate() override = default;
// ExtendedDragSource::Delegate:
bool ShouldAllowDropAnywhere() const override {
return allow_drap_no_target_;
}
bool ShouldLockCursor() const override { return lock_cursor_; }
void OnSwallowed(const std::string& mime_type) override {
ASSERT_FALSE(swallowed_);
swallowed_ = true;
}
void OnUnswallowed(const std::string& mime_type,
const gfx::Vector2d& offset) override {
ASSERT_TRUE(swallowed_);
swallowed_ = false;
}
void OnDataSourceDestroying() override { delete this; }
private:
const bool allow_drap_no_target_;
const bool lock_cursor_;
bool swallowed_ = true;
};
class ExtendedDragSourceTest : public test::ExoTestBase {
public:
ExtendedDragSourceTest() {}
ExtendedDragSourceTest(const ExtendedDragSourceTest&) = delete;
ExtendedDragSourceTest& operator=(const ExtendedDragSourceTest&) = delete;
~ExtendedDragSourceTest() override = default;
void SetUp() override {
test::ExoTestBase::SetUp();
drag_drop_controller_ = static_cast<ash::DragDropController*>(
aura::client::GetDragDropClient(ash::Shell::GetPrimaryRootWindow()));
ASSERT_TRUE(drag_drop_controller_);
drag_drop_controller_->set_should_block_during_drag_drop(false);
drag_drop_controller_->set_enabled(true);
seat_ =
std::make_unique<Seat>(std::make_unique<TestDataExchangeDelegate>());
data_source_ = std::make_unique<DataSource>(new TestDataSourceDelegate);
extended_drag_source_ = std::make_unique<ExtendedDragSource>(
data_source_.get(), new TestExtendedDragSourceDelegate(
/*allow_drop_no_target=*/true,
/*lock_cursor=*/true));
}
void TearDown() override {
extended_drag_source_.reset();
data_source_.reset();
seat_.reset();
test::ExoTestBase::TearDown();
}
protected:
void StartExtendedDragSession(aura::Window* origin,
gfx::Point start_location,
int operation,
ui::mojom::DragEventSource source) {
auto data = std::make_unique<ui::OSExchangeData>();
data->SetString(u"I am being dragged");
drag_drop_controller_->set_toplevel_window_drag_delegate(
extended_drag_source_.get());
drag_drop_controller_->StartDragAndDrop(std::move(data),
origin->GetRootWindow(), origin,
start_location, operation, source);
}
std::unique_ptr<Buffer> CreateBuffer(gfx::Size size) {
return std::make_unique<Buffer>(
exo_test_helper()->CreateGpuMemoryBuffer(size));
}
ash::DragDropController* drag_drop_controller_ = nullptr;
std::unique_ptr<Seat> seat_;
std::unique_ptr<DataSource> data_source_;
std::unique_ptr<ExtendedDragSource> extended_drag_source_;
};
} // namespace
TEST_F(ExtendedDragSourceTest, DestroySource) {
Surface origin;
// Give |origin| a root window and start DragDropOperation.
GetContext()->AddChild(origin.window());
seat_->StartDrag(data_source_.get(), &origin,
/*icon=*/nullptr, ui::mojom::DragEventSource::kMouse);
// Ensure that destroying the data source invalidates its extended_drag_source
// counterpart for the rest of its lifetime.
EXPECT_TRUE(seat_->get_drag_drop_operation_for_testing());
EXPECT_TRUE(extended_drag_source_->IsActive());
data_source_.reset();
EXPECT_FALSE(seat_->get_drag_drop_operation_for_testing());
EXPECT_FALSE(extended_drag_source_->IsActive());
}
TEST_F(ExtendedDragSourceTest, DragSurfaceAlreadyMapped) {
// Create and map a toplevel shell surface.
auto surface = std::make_unique<Surface>();
auto shell_surface = std::make_unique<ShellSurface>(surface.get());
auto buffer = CreateBuffer({32, 32});
surface->Attach(buffer.get());
surface->Commit();
gfx::Point origin(0, 0);
shell_surface->GetWidget()->SetBounds(gfx::Rect(origin, buffer->GetSize()));
EXPECT_EQ(origin, surface->window()->GetBoundsInRootWindow().origin());
// Set it as the dragged surface when it's already mapped. This allows clients
// to set existing/visible windows as the dragged surface and possibly
// snapping it to another surface, which is required for Chrome's tab drag use
// case, for example.
aura::Window* window = shell_surface->GetWidget()->GetNativeWindow();
extended_drag_source_->Drag(surface.get(), gfx::Vector2d());
EXPECT_EQ(window, extended_drag_source_->GetDraggedWindowForTesting());
EXPECT_TRUE(extended_drag_source_->GetDragOffsetForTesting().has_value());
EXPECT_EQ(gfx::Vector2d(0, 0),
*extended_drag_source_->GetDragOffsetForTesting());
// Start the DND + extended-drag session.
// Creates a mouse-pressed event before starting the drag session.
ui::test::EventGenerator generator(GetContext(), gfx::Point(10, 10));
generator.PressLeftButton();
StartExtendedDragSession(window, gfx::Point(0, 0),
ui::DragDropTypes::DRAG_MOVE,
ui::mojom::DragEventSource::kMouse);
// Verify that dragging it by 190,190, with the current pointer location being
// 10,10 will set the dragged window bounds as expected.
generator.MoveMouseBy(190, 190);
generator.ReleaseLeftButton();
EXPECT_EQ(gfx::Point(200, 200), window->GetBoundsInScreen().origin());
}
// This class installs an observer to the window being dragged.
// The goal is to ensure the drag 'n drop only effectively starts
// off of the aura::WindowObserver::OnWindowVisibilityChanged() hook,
// when it is guarantee the its state is properly set.
class WindowObserverHookChecker : public aura::WindowObserver {
public:
WindowObserverHookChecker(aura::Window* surface_window)
: surface_window_(surface_window) {
DCHECK(!surface_window_->GetRootWindow());
surface_window_->AddObserver(this);
}
~WindowObserverHookChecker() {
DCHECK(dragged_window_);
dragged_window_->RemoveObserver(this);
}
void OnWindowAddedToRootWindow(aura::Window* window) override {
dragged_window_ = surface_window_->GetToplevelWindow();
dragged_window_->AddObserver(this);
surface_window_->RemoveObserver(this);
}
MOCK_METHOD(void,
OnWindowVisibilityChanging,
(aura::Window*, bool),
(override));
MOCK_METHOD(void,
OnWindowVisibilityChanged,
(aura::Window*, bool),
(override));
private:
aura::Window* surface_window_ = nullptr;
aura::Window* dragged_window_ = nullptr;
};
TEST_F(ExtendedDragSourceTest, DragSurfaceNotMappedYet) {
// Create and Map the drag origin surface
auto surface = std::make_unique<Surface>();
auto shell_surface = std::make_unique<ShellSurface>(surface.get());
auto buffer = CreateBuffer({32, 32});
surface->Attach(buffer.get());
surface->Commit();
// Creates a mouse-pressed event before starting the drag session.
ui::test::EventGenerator generator(GetContext(), gfx::Point(10, 10));
generator.PressLeftButton();
// Start the DND + extended-drag session.
StartExtendedDragSession(shell_surface->GetWidget()->GetNativeWindow(),
gfx::Point(0, 0), ui::DragDropTypes::DRAG_MOVE,
ui::mojom::DragEventSource::kMouse);
// Create a new surface to emulate a "detachment" process.
auto detached_surface = std::make_unique<Surface>();
auto detached_shell_surface =
std::make_unique<ShellSurface>(detached_surface.get());
// Set |surface| as the dragged surface while it's still unmapped/invisible.
// This can be used to implement tab detaching in Chrome's tab drag use case,
// for example. Extended drag source will monitor surface mapping and it's
// expected to position it correctly using the provided drag offset here
// relative to the current pointer location.
extended_drag_source_->Drag(detached_surface.get(), gfx::Vector2d(10, 10));
EXPECT_FALSE(extended_drag_source_->GetDraggedWindowForTesting());
EXPECT_TRUE(extended_drag_source_->GetDragOffsetForTesting().has_value());
EXPECT_EQ(gfx::Vector2d(10, 10),
*extended_drag_source_->GetDragOffsetForTesting());
// Ensure drag 'n drop starts after
// ExtendedDragSource::OnDraggedWindowVisibilityChanged()
WindowObserverHookChecker checker(detached_surface->window());
EXPECT_CALL(checker, OnWindowVisibilityChanging(_, _))
.Times(1)
.WillOnce(InvokeWithoutArgs([]() {
auto* toplevel_handler =
ash::Shell::Get()->toplevel_window_event_handler();
EXPECT_FALSE(toplevel_handler->is_drag_in_progress());
}));
EXPECT_CALL(checker, OnWindowVisibilityChanged(_, _))
.Times(1)
.WillOnce(InvokeWithoutArgs([]() {
auto* toplevel_handler =
ash::Shell::Get()->toplevel_window_event_handler();
EXPECT_TRUE(toplevel_handler->is_drag_in_progress());
}));
// Map the |detached_surface|.
auto detached_buffer = CreateBuffer({50, 50});
detached_surface->Attach(detached_buffer.get());
detached_surface->Commit();
// Ensure the toplevel window for the dragged surface set above, is correctly
// detected, after it's mapped.
aura::Window* window = detached_shell_surface->GetWidget()->GetNativeWindow();
EXPECT_TRUE(extended_drag_source_->GetDraggedWindowForTesting());
EXPECT_EQ(window, extended_drag_source_->GetDraggedWindowForTesting());
// Verify that dragging it by 100,100, with drag offset 10,10 and current
// pointer location 50,50 will set the dragged window bounds as expected.
generator.set_current_screen_location(gfx::Point(100, 100));
generator.DragMouseBy(50, 50);
generator.ReleaseLeftButton();
EXPECT_EQ(gfx::Point(140, 140), window->GetBoundsInScreen().origin());
}
TEST_F(ExtendedDragSourceTest, DragSurfaceNotMappedYetWithTouch) {
// Create and Map the drag origin surface
auto surface = std::make_unique<Surface>();
auto shell_surface = std::make_unique<ShellSurface>(surface.get());
auto buffer = CreateBuffer({32, 32});
surface->Attach(buffer.get());
surface->Commit();
// Start the DND + extended-drag session.
StartExtendedDragSession(shell_surface->GetWidget()->GetNativeWindow(),
gfx::Point(0, 0), ui::DragDropTypes::DRAG_MOVE,
ui::mojom::DragEventSource::kTouch);
// Create a new surface to emulate a "detachment" process.
auto detached_surface = std::make_unique<Surface>();
auto detached_shell_surface =
std::make_unique<ShellSurface>(detached_surface.get());
// Set |surface| as the dragged surface while it's still unmapped/invisible.
// This can be used to implement tab detaching in Chrome's tab drag use case,
// for example. Extended drag source will monitor surface mapping and it's
// expected to position it correctly using the provided drag offset here
// relative to the current pointer location.
extended_drag_source_->Drag(detached_surface.get(), gfx::Vector2d(10, 10));
EXPECT_FALSE(extended_drag_source_->GetDraggedWindowForTesting());
EXPECT_TRUE(extended_drag_source_->GetDragOffsetForTesting().has_value());
EXPECT_EQ(gfx::Vector2d(10, 10),
*extended_drag_source_->GetDragOffsetForTesting());
// Initiate the gesture sequence.
DispatchGesture(ui::ET_GESTURE_BEGIN, gfx::Point(10, 10));
// Map the |detached_surface|.
auto detached_buffer = CreateBuffer({50, 50});
detached_surface->Attach(detached_buffer.get());
detached_surface->Commit();
// Ensure the toplevel window for the dragged surface set above, is correctly
// detected, after it's mapped.
aura::Window* window = detached_shell_surface->GetWidget()->GetNativeWindow();
EXPECT_TRUE(extended_drag_source_->GetDraggedWindowForTesting());
EXPECT_EQ(window, extended_drag_source_->GetDraggedWindowForTesting());
// Verify that dragging it by 100,100, with drag offset 10,10 and current
// pointer location 50,50 will set the dragged window bounds as expected.
ui::test::EventGenerator generator(GetContext());
generator.set_current_screen_location(gfx::Point(100, 100));
generator.PressMoveAndReleaseTouchBy(50, 50);
EXPECT_EQ(gfx::Point(140, 140), window->GetBoundsInScreen().origin());
}
TEST_F(ExtendedDragSourceTest, DestroyDraggedSurfaceWhileDragging) {
// Create and map a toplevel shell surface.
gfx::Size buffer_size(32, 32);
std::unique_ptr<Buffer> buffer(
new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
std::unique_ptr<Surface> surface(new Surface);
std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
surface->Attach(buffer.get());
surface->Commit();
gfx::Point origin(0, 0);
shell_surface->GetWidget()->SetBounds(gfx::Rect(origin, buffer_size));
EXPECT_EQ(origin, surface->window()->GetBoundsInRootWindow().origin());
// Start dragging |surface|'s window.
extended_drag_source_->Drag(surface.get(), gfx::Vector2d());
aura::Window* window = shell_surface->GetWidget()->GetNativeWindow();
EXPECT_EQ(window, extended_drag_source_->GetDraggedWindowForTesting());
// Make sure extended drag source gracefully handles window destruction during
// while the drag session is still alive.
shell_surface.reset();
EXPECT_TRUE(surface->window()->GetBoundsInScreen().origin().IsOrigin());
ui::test::EventGenerator generator(GetContext());
generator.DragMouseBy(190, 190);
EXPECT_TRUE(surface->window()->GetBoundsInScreen().origin().IsOrigin());
}
} // namespace exo