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

Side by Side Diff: ui/views/cocoa/desktop_drag_drop_client_mac_unittest.mm

Issue 1964283002: MacViews: Implemented Drag & Drop (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Nit Created 4 years, 6 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
OLDNEW
(Empty)
1 // Copyright 2016 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 #import "ui/views/cocoa/drag_drop_client_mac.h"
6
7 #import <Cocoa/Cocoa.h>
8
9 #include "base/strings/utf_string_conversions.h"
10 #import "ui/gfx/test/ui_cocoa_test_helper.h"
tapted 2016/05/31 11:49:58 remove? unused now i think
spqchan 2016/05/31 23:06:12 Done.
11 #import "ui/views/cocoa/bridged_content_view.h"
12 #import "ui/views/cocoa/bridged_native_widget.h"
13 #include "ui/views/test/widget_test.h"
14 #include "ui/views/view.h"
15 #include "ui/views/widget/native_widget_mac.h"
16 #include "ui/views/widget/root_view.h"
17 #include "ui/views/widget/widget.h"
18
19 using base::ASCIIToUTF16;
20
21 // Mocks the NSDraggingInfo sent to the DragDropClientMac's DragUpdate() and
22 // Drop() methods. Out of the required methods of NSDraggingInfo, only
23 // draggingLocation and draggingPasteboard are used.
24 @interface MockDraggingInfo : NSObject<NSDraggingInfo> {
25 NSPasteboard* pasteboard_;
26 }
27
28 @property BOOL animatesToDestination;
29 @property NSInteger numberOfValidItemsForDrop;
30 @property NSDraggingFormation draggingFormation;
31 @property(readonly) NSSpringLoadingHighlight springLoadingHighlight;
32
33 @end
34
35 @implementation MockDraggingInfo
36
37 @synthesize animatesToDestination;
38 @synthesize numberOfValidItemsForDrop;
39 @synthesize draggingFormation;
40 @synthesize springLoadingHighlight;
41
42 - (id)initWithPasteboard:(NSPasteboard*)pasteboard {
43 if ((self = [super init])) {
44 pasteboard_ = pasteboard;
45 }
46 return self;
47 }
48
49 - (NSPoint)draggingLocation {
50 return NSMakePoint(50, 50);
51 }
52
53 - (NSPasteboard*)draggingPasteboard {
54 return pasteboard_;
55 }
56
57 - (NSInteger)draggingSequenceNumber {
58 return 0;
59 }
60
61 - (id)draggingSource {
62 return nil;
63 }
64
65 - (NSDragOperation)draggingSourceOperationMask {
66 return NSDragOperationNone;
67 }
68
69 - (NSWindow*)draggingDestinationWindow {
70 return nil;
71 }
72
73 - (NSArray*)namesOfPromisedFilesDroppedAtDestination:(NSURL*)dropDestination {
74 return nil;
75 }
76
77 - (NSImage*)draggedImage {
78 return nil;
79 }
80
81 - (NSPoint)draggedImageLocation {
82 return NSZeroPoint;
83 }
84
85 - (void)slideDraggedImageTo:(NSPoint)aPoint {
86 }
87
88 - (void)
89 enumerateDraggingItemsWithOptions:(NSDraggingItemEnumerationOptions)enumOpts
90 forView:(NSView*)view
91 classes:(NSArray*)classArray
92 searchOptions:(NSDictionary*)searchOptions
93 usingBlock:(void (^)(NSDraggingItem* draggingItem,
94 NSInteger idx,
95 BOOL* stop))block {
96 }
97
98 - (void)resetSpringLoading {
99 }
100
101 @end
102
103 namespace views {
104 namespace test {
105
106 // View object that will receive and process dropped data from the test.
107 class DragDropView : public View {
108 public:
109 DragDropView() {}
110
111 void set_formats(int formats) { formats_ = formats; }
112
113 // Views:
114 bool GetDropFormats(
115 int* formats,
116 std::set<ui::Clipboard::FormatType>* format_types) override {
117 *formats |= formats_;
118 return true;
119 }
120
121 bool CanDrop(const OSExchangeData& data) override { return true; }
122
123 int OnDragUpdated(const ui::DropTargetEvent& event) override {
124 return ui::DragDropTypes::DRAG_COPY;
125 }
126
127 int OnPerformDrop(const ui::DropTargetEvent& event) override {
128 return ui::DragDropTypes::DRAG_COPY;
129 }
130
131 private:
132 // Drop formats accepted by this View object.
133 int formats_ = 0;
134
135 DISALLOW_COPY_AND_ASSIGN(DragDropView);
136 };
137
138 class CocoaDragDropTest : public WidgetTest {
139 public:
140 CocoaDragDropTest() : widget_(new Widget) {}
141
142 DragDropClientMac* drag_drop_client() { return bridge_->drag_drop_client(); }
143
144 void SetUp() override {
145 WidgetTest::SetUp();
146
147 widget_ = CreateTopLevelPlatformWidget();
148 gfx::Rect bounds(0, 0, 100, 100);
149 widget_->SetBounds(bounds);
150
151 bridge_ =
152 NativeWidgetMac::GetBridgeForNativeWindow(widget_->GetNativeWindow());
153 widget_->Show();
154
155 target_ = new DragDropView();
156 widget_->GetContentsView()->AddChildView(target_);
157 target_->SetBoundsRect(bounds);
158
159 drag_drop_client()->operation_ = ui::DragDropTypes::DRAG_COPY;
160 }
161
162 void TearDown() override {
163 widget_->CloseNow();
164 WidgetTest::TearDown();
165 }
166
167 NSDragOperation DragUpdate(NSPasteboard* pasteboard) {
168 DragDropClientMac* client = drag_drop_client();
169 MockDraggingInfo* info =
tapted 2016/05/31 11:49:58 I think these are leaked right now. To fix, maybe
spqchan 2016/05/31 23:06:12 Done.
170 [[MockDraggingInfo alloc] initWithPasteboard:pasteboard];
171 return client->DragUpdate(info);
172 }
173
174 NSDragOperation Drop(NSPasteboard* pasteboard) {
175 DragDropClientMac* client = drag_drop_client();
176 MockDraggingInfo* info =
177 [[MockDraggingInfo alloc] initWithPasteboard:pasteboard];
178 return client->Drop(info);
179 }
180
181 void SetData(OSExchangeData& data) {
182 drag_drop_client()->data_source_.reset(
183 [[CocoaDragDropDataProvider alloc] initWithData:data]);
184 }
185
186 protected:
187 Widget* widget_ = nullptr;
188 BridgedNativeWidget* bridge_ = nullptr;
189 DragDropView* target_ = nullptr;
190
191 DISALLOW_COPY_AND_ASSIGN(CocoaDragDropTest);
192 };
193
194 // Tests if the drag and drop target receives the dropped data.
195 TEST_F(CocoaDragDropTest, BasicDragDrop) {
196 // Create the drop data
197 OSExchangeData data;
198 const base::string16& text = ASCIIToUTF16("text");
199 data.SetString(text);
200 SetData(data);
201
202 target_->set_formats(ui::OSExchangeData::STRING | ui::OSExchangeData::URL);
203
204 // Check if the target receives the data from a drop and returns the expected
205 // operation.
206 EXPECT_NE(DragUpdate(nil), NSDragOperationNone);
tapted 2016/05/31 11:49:58 Can we EXPECT_EQ against something for a stronger
spqchan 2016/05/31 23:06:12 Done.
207 EXPECT_NE(Drop(nil), NSDragOperationNone);
208 }
209
210 // Tests if the drag and drop target rejects the dropped data with the
211 // incorrect format.
212 TEST_F(CocoaDragDropTest, InvalidFormatDragDrop) {
213 OSExchangeData data;
214 const base::string16& text = ASCIIToUTF16("text");
215 data.SetString(text);
216 SetData(data);
217
218 target_->set_formats(ui::OSExchangeData::URL);
219
220 // Check if the target receives the data from a drop and returns the expected
221 // operation.
222 EXPECT_EQ(DragUpdate(nil), NSDragOperationNone);
223 EXPECT_EQ(Drop(nil), NSDragOperationNone);
224 }
225
226 // Tests if the drag and drop target can accept data without an OSExchangeData
227 // object.
228 TEST_F(CocoaDragDropTest, PasteboardToOSExchangeTest) {
229 target_->set_formats(ui::OSExchangeData::STRING);
230
231 NSPasteboard* pasteboard = [NSPasteboard pasteboardWithUniqueName];
232
233 EXPECT_EQ(DragUpdate(pasteboard), NSDragOperationNone);
234 EXPECT_EQ(Drop(pasteboard), NSDragOperationNone);
235 drag_drop_client()->EndDrag();
236
237 [pasteboard setString:@"text" forType:NSPasteboardTypeString];
238
239 EXPECT_NE(DragUpdate(pasteboard), NSDragOperationNone);
tapted 2016/05/31 11:49:58 EXPECT_EQ?
spqchan 2016/05/31 23:06:12 Done.
240 EXPECT_NE(Drop(pasteboard), NSDragOperationNone);
241 }
242
243 } // namespace test
244 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698