1 // Copyright 2020 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 "components/exo/drag_drop_operation.h"
6 
7 #include <memory>
8 
9 #include "ash/shell.h"
10 #include "base/bind.h"
11 #include "base/files/file_util.h"
12 #include "base/run_loop.h"
13 #include "components/exo/buffer.h"
14 #include "components/exo/data_source.h"
15 #include "components/exo/data_source_delegate.h"
16 #include "components/exo/file_helper.h"
17 #include "components/exo/surface.h"
18 #include "components/exo/test/exo_test_base.h"
19 #include "components/exo/test/exo_test_file_helper.h"
20 #include "ui/aura/client/drag_drop_client.h"
21 #include "ui/base/dragdrop/mojom/drag_drop_types.mojom-shared.h"
22 #include "ui/gfx/geometry/point_f.h"
23 
24 namespace exo {
25 namespace {
26 
27 constexpr char kText[] = "test";
28 constexpr char kTextMimeType[] = "text/plain";
29 
30 }  // namespace
31 
32 class TestDataSourceDelegate : public DataSourceDelegate {
33  public:
34   // DataSourceDelegate:
OnDataSourceDestroying(DataSource * source)35   void OnDataSourceDestroying(DataSource* source) override {}
36 
OnTarget(const base::Optional<std::string> & mime_type)37   void OnTarget(const base::Optional<std::string>& mime_type) override {}
38 
OnSend(const std::string & mime_type,base::ScopedFD fd)39   void OnSend(const std::string& mime_type, base::ScopedFD fd) override {
40     base::WriteFileDescriptor(fd.get(), kText, sizeof(kText));
41   }
42 
OnCancelled()43   void OnCancelled() override {}
44 
OnDndDropPerformed()45   void OnDndDropPerformed() override {}
46 
OnDndFinished()47   void OnDndFinished() override {}
48 
OnAction(DndAction dnd_action)49   void OnAction(DndAction dnd_action) override {}
50 
CanAcceptDataEventsForSurface(Surface * surface) const51   bool CanAcceptDataEventsForSurface(Surface* surface) const override {
52     return true;
53   }
54 };
55 
56 class DragDropOperationTest : public test::ExoTestBase,
57                               public aura::client::DragDropClientObserver {
58  public:
59   DragDropOperationTest() = default;
60   ~DragDropOperationTest() override = default;
61   DragDropOperationTest(const DragDropOperationTest&) = delete;
62   DragDropOperationTest& operator=(const DragDropOperationTest&) = delete;
63 
SetUp()64   void SetUp() override {
65     test::ExoTestBase::SetUp();
66     aura::client::GetDragDropClient(ash::Shell::GetPrimaryRootWindow())
67         ->AddObserver(this);
68   }
69 
TearDown()70   void TearDown() override {
71     aura::client::GetDragDropClient(ash::Shell::GetPrimaryRootWindow())
72         ->RemoveObserver(this);
73     test::ExoTestBase::TearDown();
74   }
75 
76   // aura::client::DragDropClientObserver:
OnDragStarted()77   void OnDragStarted() override {
78     base::SequencedTaskRunnerHandle::Get()->PostTask(
79         FROM_HERE, std::move(drag_blocked_callback_));
80   }
81 
OnDragEnded()82   void OnDragEnded() override {}
83 
84  protected:
set_drag_blocked_callback(base::OnceClosure callback)85   void set_drag_blocked_callback(base::OnceClosure callback) {
86     drag_blocked_callback_ = std::move(callback);
87   }
88 
89  private:
90   // Callback running inside the nested RunLoop in StartDragAndDrop().
91   base::OnceClosure drag_blocked_callback_;
92 };
93 
TEST_F(DragDropOperationTest,DeleteDuringDragging)94 TEST_F(DragDropOperationTest, DeleteDuringDragging) {
95   TestFileHelper file_helper;
96 
97   auto delegate = std::make_unique<TestDataSourceDelegate>();
98   auto data_source = std::make_unique<DataSource>(delegate.get());
99   data_source->Offer(kTextMimeType);
100 
101   auto origin_surface = std::make_unique<Surface>();
102   ash::Shell::GetPrimaryRootWindow()->AddChild(origin_surface->window());
103 
104   gfx::Size buffer_size(100, 100);
105   std::unique_ptr<Buffer> buffer(
106       new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
107   auto icon_surface = std::make_unique<Surface>();
108   icon_surface->Attach(buffer.get());
109 
110   auto operation = DragDropOperation::Create(
111       &file_helper, data_source.get(), origin_surface.get(), icon_surface.get(),
112       gfx::PointF(), ui::mojom::DragEventSource::kMouse);
113   icon_surface->Commit();
114 
115   base::RunLoop run_loop;
116   set_drag_blocked_callback(base::BindOnce(
117       [](std::unique_ptr<DataSource> data_source,
118          base::WeakPtr<DragDropOperation> operation,
119          base::OnceClosure quit_closure) {
120         // This function runs inside the nested RunLoop in
121         // ash::DragDropController::StartDragAndDrop().
122         EXPECT_TRUE(operation);
123         // Deleting DataSource causes DragDropOperation to be deleted as well.
124         data_source.reset();
125         EXPECT_FALSE(operation);
126         std::move(quit_closure).Run();
127       },
128       std::move(data_source), operation, run_loop.QuitClosure()));
129   run_loop.Run();
130   EXPECT_FALSE(operation);
131 }
132 
133 }  // namespace exo
134