OLD | NEW |
---|---|
(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" | |
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 { return NSMakePoint(50, 50); } | |
50 | |
51 - (NSPasteboard*)draggingPasteboard { return pasteboard_; } | |
52 | |
53 - (NSInteger)draggingSequenceNumber { return 0; } | |
54 | |
55 - (id)draggingSource { return nil; } | |
56 | |
57 - (NSDragOperation)draggingSourceOperationMask { return NSDragOperationNone; } | |
58 | |
59 - (NSWindow*)draggingDestinationWindow { | |
60 return nil; | |
61 } | |
62 | |
63 - (NSArray*)namesOfPromisedFilesDroppedAtDestination:(NSURL*)dropDestination { | |
64 return nil; | |
65 } | |
66 | |
67 - (NSImage*)draggedImage { | |
68 return nil; | |
69 } | |
70 | |
71 - (NSPoint)draggedImageLocation { | |
72 return NSZeroPoint; | |
73 } | |
74 | |
75 - (void)slideDraggedImageTo:(NSPoint)aPoint { | |
76 } | |
77 | |
78 - (void) | |
79 enumerateDraggingItemsWithOptions:(NSDraggingItemEnumerationOptions)enumOpts | |
80 forView:(NSView*)view | |
81 classes:(NSArray*)classArray | |
82 searchOptions:(NSDictionary*)searchOptions | |
83 usingBlock:(void (^)(NSDraggingItem* draggingItem, | |
84 NSInteger idx, | |
85 BOOL* stop))block { | |
86 } | |
87 | |
88 - (void)resetSpringLoading { | |
89 } | |
90 | |
91 @end | |
92 | |
93 namespace views { | |
94 namespace test { | |
95 | |
96 // View object that will receive and process dropped data from the test. | |
97 class DragDropView : public View { | |
98 public: | |
99 DragDropView() {} | |
100 | |
101 void set_formats(int formats) { formats_ = formats; } | |
102 | |
103 // Views: | |
104 bool GetDropFormats( | |
105 int* formats, | |
106 std::set<ui::Clipboard::FormatType>* format_types) override { | |
107 *formats |= formats_; | |
108 LOG(INFO) << "GetDropFormats"; | |
tapted
2016/05/26 12:31:42
remove stray log
spqchan
2016/05/27 23:25:48
Done.
| |
109 return true; | |
110 } | |
111 | |
112 bool CanDrop(const OSExchangeData& data) override { return true; } | |
113 | |
114 int OnDragUpdated(const ui::DropTargetEvent& event) override { | |
115 return ui::DragDropTypes::DRAG_COPY; | |
116 } | |
117 | |
118 int OnPerformDrop(const ui::DropTargetEvent& event) override { | |
119 return ui::DragDropTypes::DRAG_COPY; | |
120 } | |
121 | |
122 private: | |
123 // Drop formats accepted by this View object. | |
124 int formats_ = 0; | |
125 | |
126 DISALLOW_COPY_AND_ASSIGN(DragDropView); | |
127 }; | |
128 | |
129 class CocoaDragDropTest : public WidgetTest { | |
130 public: | |
131 CocoaDragDropTest() : widget_(new Widget) {} | |
132 | |
133 DragDropClientMac* drag_drop_client() { return bridge_->drag_drop_client(); } | |
134 | |
135 void SetUp() override { | |
136 WidgetTest::SetUp(); | |
137 | |
138 widget_ = CreateTopLevelPlatformWidget(); | |
139 gfx::Rect bounds(0, 0, 100, 100); | |
140 widget_->SetBounds(bounds); | |
141 | |
142 bridge_ = | |
143 NativeWidgetMac::GetBridgeForNativeWindow(widget_->GetNativeWindow()); | |
144 widget_->Show(); | |
145 | |
146 target_ = new DragDropView(); | |
147 widget_->GetRootView()->AddChildView(target_); | |
tapted
2016/05/26 12:31:42
the view should be added to as a child of GetConte
spqchan
2016/05/27 23:25:48
Done.
| |
148 widget_->GetContentsView()->size(); | |
tapted
2016/05/26 12:31:42
remove (since it has no side-effects)
spqchan
2016/05/27 23:25:48
Done.
| |
149 target_->SetBoundsRect(bounds); | |
150 | |
151 drag_drop_client()->operation_ = ui::DragDropTypes::DRAG_COPY; | |
152 } | |
153 | |
154 void TearDown() override { | |
155 widget_->CloseNow(); | |
156 WidgetTest::TearDown(); | |
157 } | |
158 | |
159 NSDragOperation DragUpdate(NSPasteboard* pasteboard) { | |
160 DragDropClientMac* client = drag_drop_client(); | |
161 MockDraggingInfo* info = | |
162 [[MockDraggingInfo alloc] initWithPasteboard:pasteboard]; | |
163 return client->DragUpdate(info); | |
164 } | |
165 | |
166 NSDragOperation Drop(NSPasteboard* pasteboard) { | |
167 DragDropClientMac* client = drag_drop_client(); | |
168 MockDraggingInfo* info = | |
169 [[MockDraggingInfo alloc] initWithPasteboard:pasteboard]; | |
170 return client->Drop(info); | |
171 } | |
172 | |
173 void SetData(OSExchangeData& data) { | |
174 drag_drop_client()->data_source_.reset( | |
175 [[CocoaDragDropDataProvider alloc] initWithData:data]); | |
176 } | |
177 | |
178 protected: | |
179 Widget* widget_ = nullptr; | |
180 BridgedNativeWidget* bridge_ = nullptr; | |
181 DragDropView* target_ = nullptr; | |
182 | |
183 DISALLOW_COPY_AND_ASSIGN(CocoaDragDropTest); | |
184 }; | |
185 | |
186 // Tests if the drag and drop target receives the dropped data. | |
187 TEST_F(CocoaDragDropTest, BasicDragDrop) { | |
188 // Create the drop data | |
189 OSExchangeData data; | |
190 const base::string16& text = ASCIIToUTF16("text"); | |
191 data.SetString(text); | |
192 SetData(data); | |
193 | |
194 target_->set_formats(ui::OSExchangeData::STRING | ui::OSExchangeData::URL); | |
195 | |
196 // Check if the target receives the data from a drop and returns the expected | |
197 // operation. | |
198 EXPECT_NE(DragUpdate(nil), NSDragOperationNone); | |
199 EXPECT_NE(Drop(nil), NSDragOperationNone); | |
200 } | |
201 | |
202 // Tests if the drag and drop target rejects the dropped data with the | |
203 // incorrect format. | |
204 TEST_F(CocoaDragDropTest, InvalidFormatDragDrop) { | |
205 OSExchangeData data; | |
206 const base::string16& text = ASCIIToUTF16("text"); | |
207 data.SetString(text); | |
208 SetData(data); | |
209 | |
210 target_->set_formats(ui::OSExchangeData::URL); | |
211 | |
212 // Check if the target receives the data from a drop and returns the expected | |
213 // operation. | |
214 EXPECT_EQ(DragUpdate(nil), NSDragOperationNone); | |
215 EXPECT_EQ(Drop(nil), NSDragOperationNone); | |
216 } | |
217 | |
218 // Tests if the drag and drop target can accept data without an OSExchangeData | |
219 // object. | |
220 TEST_F(CocoaDragDropTest, PasteboardToOSExchangeTest) { | |
221 target_->set_formats(ui::OSExchangeData::STRING); | |
222 | |
223 NSPasteboard* pasteboard = [NSPasteboard pasteboardWithUniqueName]; | |
224 | |
225 EXPECT_EQ(DragUpdate(pasteboard), NSDragOperationNone); | |
226 EXPECT_EQ(Drop(pasteboard), NSDragOperationNone); | |
227 drag_drop_client()->EndDrag(); | |
228 | |
229 [pasteboard setString:@"text" forType:NSPasteboardTypeString]; | |
230 | |
231 EXPECT_NE(DragUpdate(pasteboard), NSDragOperationNone); | |
232 EXPECT_NE(Drop(pasteboard), NSDragOperationNone); | |
233 } | |
234 | |
235 } // namespace test | |
236 } // namespace views | |
OLD | NEW |