forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drag_drop_operation_unittest.cc
127 lines (100 loc) · 3.92 KB
/
drag_drop_operation_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
// 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/drag_drop_operation.h"
#include <memory>
#include "ash/shell.h"
#include "base/bind.h"
#include "base/files/file_util.h"
#include "base/run_loop.h"
#include "components/exo/buffer.h"
#include "components/exo/data_source.h"
#include "components/exo/data_source_delegate.h"
#include "components/exo/surface.h"
#include "components/exo/test/exo_test_base.h"
#include "ui/aura/client/drag_drop_client.h"
namespace exo {
namespace {
constexpr char kText[] = "test";
constexpr char kTextMimeType[] = "text/plain";
} // namespace
class TestDataSourceDelegate : public DataSourceDelegate {
public:
// DataSourceDelegate:
void OnDataSourceDestroying(DataSource* source) override {}
void OnTarget(const base::Optional<std::string>& mime_type) override {}
void OnSend(const std::string& mime_type, base::ScopedFD fd) override {
base::WriteFileDescriptor(fd.get(), kText, sizeof(kText));
}
void OnCancelled() override {}
void OnDndDropPerformed() override {}
void OnDndFinished() override {}
void OnAction(DndAction dnd_action) override {}
bool CanAcceptDataEventsForSurface(Surface* surface) const override {
return true;
}
};
class DragDropOperationTest : public test::ExoTestBase,
public aura::client::DragDropClientObserver {
public:
DragDropOperationTest() = default;
~DragDropOperationTest() override = default;
DragDropOperationTest(const DragDropOperationTest&) = delete;
DragDropOperationTest& operator=(const DragDropOperationTest&) = delete;
void SetUp() override {
test::ExoTestBase::SetUp();
aura::client::GetDragDropClient(ash::Shell::GetPrimaryRootWindow())
->AddObserver(this);
}
void TearDown() override {
aura::client::GetDragDropClient(ash::Shell::GetPrimaryRootWindow())
->RemoveObserver(this);
test::ExoTestBase::TearDown();
}
// aura::client::DragDropClientObserver:
void OnDragStarted() override {
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE, std::move(drag_blocked_callback_));
}
void OnDragEnded() override {}
protected:
void set_drag_blocked_callback(base::OnceClosure callback) {
drag_blocked_callback_ = std::move(callback);
}
private:
// Callback running inside the nested RunLoop in StartDragAndDrop().
base::OnceClosure drag_blocked_callback_;
};
TEST_F(DragDropOperationTest, DeleteDuringDragging) {
auto delegate = std::make_unique<TestDataSourceDelegate>();
auto data_source = std::make_unique<DataSource>(delegate.get());
data_source->Offer(kTextMimeType);
auto origin_surface = std::make_unique<Surface>();
ash::Shell::GetPrimaryRootWindow()->AddChild(origin_surface->window());
gfx::Size buffer_size(100, 100);
std::unique_ptr<Buffer> buffer(
new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
auto icon_surface = std::make_unique<Surface>();
icon_surface->Attach(buffer.get());
auto operation = DragDropOperation::Create(
data_source.get(), origin_surface.get(), icon_surface.get(), gfx::Point(),
ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE);
icon_surface->Commit();
base::RunLoop run_loop;
set_drag_blocked_callback(base::BindOnce(
[](std::unique_ptr<DataSource> data_source,
base::WeakPtr<DragDropOperation> operation,
base::OnceClosure quit_closure) {
// This function runs inside the nested RunLoop in
// ash::DragDropController::StartDragAndDrop().
EXPECT_TRUE(operation);
// Deleting DataSource causes DragDropOperation to be deleted as well.
data_source.reset();
EXPECT_FALSE(operation);
std::move(quit_closure).Run();
},
std::move(data_source), operation, run_loop.QuitClosure()));
run_loop.Run();
EXPECT_FALSE(operation);
}
} // namespace exo