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

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

Issue 2337233004: MacViews: Fix sending mouse exit event and releasing capture on D&D. (Closed)
Patch Set: Fix review issues. Created 4 years, 2 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
« no previous file with comments | « ui/views/cocoa/drag_drop_client_mac.mm ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "ui/views/cocoa/drag_drop_client_mac.h" 5 #import "ui/views/cocoa/drag_drop_client_mac.h"
6 6
7 #import <Cocoa/Cocoa.h> 7 #import <Cocoa/Cocoa.h>
8 8
9 #import "base/mac/scoped_objc_class_swizzler.h"
9 #include "base/mac/sdk_forward_declarations.h" 10 #include "base/mac/sdk_forward_declarations.h"
10 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "base/threading/thread_task_runner_handle.h"
11 #import "ui/views/cocoa/bridged_native_widget.h" 13 #import "ui/views/cocoa/bridged_native_widget.h"
12 #include "ui/views/test/widget_test.h" 14 #include "ui/views/test/widget_test.h"
13 #include "ui/views/view.h" 15 #include "ui/views/view.h"
14 #include "ui/views/widget/native_widget_mac.h" 16 #include "ui/views/widget/native_widget_mac.h"
15 #include "ui/views/widget/widget.h" 17 #include "ui/views/widget/widget.h"
16 18
17 using base::ASCIIToUTF16; 19 using base::ASCIIToUTF16;
18 20
21 @interface NSView (DragSessionTestingDonor)
22 @end
23
24 @implementation NSView (DragSessionTestingDonor)
25 - (NSDraggingSession*)cr_beginDraggingSessionWithItems:(NSArray*)items
26 event:(NSEvent*)event
27 source:(id<NSDraggingSource>)
28 source {
29 return nil;
30 }
31 @end
32
19 // Mocks the NSDraggingInfo sent to the DragDropClientMac's DragUpdate() and 33 // Mocks the NSDraggingInfo sent to the DragDropClientMac's DragUpdate() and
20 // Drop() methods. Out of the required methods of the protocol, only 34 // Drop() methods. Out of the required methods of the protocol, only
21 // draggingLocation and draggingPasteboard are used. 35 // draggingLocation and draggingPasteboard are used.
22 @interface MockDraggingInfo : NSObject<NSDraggingInfo> { 36 @interface MockDraggingInfo : NSObject<NSDraggingInfo> {
23 NSPasteboard* pasteboard_; 37 NSPasteboard* pasteboard_;
24 } 38 }
25 39
26 @property BOOL animatesToDestination; 40 @property BOOL animatesToDestination;
27 @property NSInteger numberOfValidItemsForDrop; 41 @property NSInteger numberOfValidItemsForDrop;
28 @property NSDraggingFormation draggingFormation; 42 @property NSDraggingFormation draggingFormation;
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 SetData(data); 216 SetData(data);
203 217
204 target_->set_formats(ui::OSExchangeData::STRING | ui::OSExchangeData::URL); 218 target_->set_formats(ui::OSExchangeData::STRING | ui::OSExchangeData::URL);
205 219
206 // Check if the target receives the data from a drop and returns the expected 220 // Check if the target receives the data from a drop and returns the expected
207 // operation. 221 // operation.
208 EXPECT_EQ(DragUpdate(nil), NSDragOperationCopy); 222 EXPECT_EQ(DragUpdate(nil), NSDragOperationCopy);
209 EXPECT_EQ(Drop(), NSDragOperationMove); 223 EXPECT_EQ(Drop(), NSDragOperationMove);
210 } 224 }
211 225
226 // Ensure that capture is released before the end of a drag and drop operation.
227 TEST_F(DragDropClientMacTest, ReleaseCapture) {
228 // DragDropView doesn't actually capture the mouse, so explicitly acquire it
229 // to test that StartDragAndDrop() actually releases it.
230 // Although this is not an interactive UI test, acquiring capture should be OK
231 // since the runloop will exit before the system has any opportunity to
232 // capture anything.
233 bridge_->AcquireCapture();
234 EXPECT_TRUE(bridge_->HasCapture());
235
236 // Create the drop data
237 OSExchangeData data;
238 const base::string16& text = ASCIIToUTF16("text");
239 data.SetString(text);
240 SetData(data);
241
242 // There's no way to cleanly stop NSDraggingSession inside unit tests, so just
243 // don't start it at all.
244 base::mac::ScopedObjCClassSwizzler swizzle(
245 [NSView class], @selector(beginDraggingSessionWithItems:event:source:),
246 @selector(cr_beginDraggingSessionWithItems:event:source:));
247
248 // Immediately quit drag'n'drop, or we'll hang.
249 base::ThreadTaskRunnerHandle::Get()->PostTask(
250 FROM_HERE, base::Bind(&DragDropClientMac::EndDrag,
251 base::Unretained(drag_drop_client())));
252
253 // It will call ReleaseCapture().
254 drag_drop_client()->StartDragAndDrop(
255 target_, data, 0, ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE);
256
257 // The capture should be released.
258 EXPECT_FALSE(bridge_->HasCapture());
259 }
260
212 // Tests if the drag and drop target rejects the dropped data with the 261 // Tests if the drag and drop target rejects the dropped data with the
213 // incorrect format. 262 // incorrect format.
214 TEST_F(DragDropClientMacTest, InvalidFormatDragDrop) { 263 TEST_F(DragDropClientMacTest, InvalidFormatDragDrop) {
215 OSExchangeData data; 264 OSExchangeData data;
216 const base::string16& text = ASCIIToUTF16("text"); 265 const base::string16& text = ASCIIToUTF16("text");
217 data.SetString(text); 266 data.SetString(text);
218 SetData(data); 267 SetData(data);
219 268
220 target_->set_formats(ui::OSExchangeData::URL); 269 target_->set_formats(ui::OSExchangeData::URL);
221 270
(...skipping 17 matching lines...) Expand all
239 288
240 // Add valid data to the pasteboard and check to see if the target accepts 289 // Add valid data to the pasteboard and check to see if the target accepts
241 // it. 290 // it.
242 [pasteboard setString:@"text" forType:NSPasteboardTypeString]; 291 [pasteboard setString:@"text" forType:NSPasteboardTypeString];
243 EXPECT_EQ(DragUpdate(pasteboard), NSDragOperationCopy); 292 EXPECT_EQ(DragUpdate(pasteboard), NSDragOperationCopy);
244 EXPECT_EQ(Drop(), NSDragOperationMove); 293 EXPECT_EQ(Drop(), NSDragOperationMove);
245 } 294 }
246 295
247 } // namespace test 296 } // namespace test
248 } // namespace views 297 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/cocoa/drag_drop_client_mac.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698