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

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: Created 4 years, 3 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
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"
13 #include "ui/events/test/event_generator.h"
11 #import "ui/views/cocoa/bridged_native_widget.h" 14 #import "ui/views/cocoa/bridged_native_widget.h"
12 #include "ui/views/test/widget_test.h" 15 #include "ui/views/test/widget_test.h"
13 #include "ui/views/view.h" 16 #include "ui/views/view.h"
14 #include "ui/views/widget/native_widget_mac.h" 17 #include "ui/views/widget/native_widget_mac.h"
15 #include "ui/views/widget/widget.h" 18 #include "ui/views/widget/widget.h"
16 19
17 using base::ASCIIToUTF16; 20 using base::ASCIIToUTF16;
18 21
22 @interface NSView (DragSessionTestingDonor)
23 @end
24
25 @implementation NSView (DragSessionTestingDonor)
26 - (NSDraggingSession*)cr_beginDraggingSessionWithItems:(NSArray*)items
27 event:(NSEvent*)event
28 source:(id<NSDraggingSource>)
29 source {
30 return nil;
31 }
32 @end
33
19 // Mocks the NSDraggingInfo sent to the DragDropClientMac's DragUpdate() and 34 // Mocks the NSDraggingInfo sent to the DragDropClientMac's DragUpdate() and
20 // Drop() methods. Out of the required methods of the protocol, only 35 // Drop() methods. Out of the required methods of the protocol, only
21 // draggingLocation and draggingPasteboard are used. 36 // draggingLocation and draggingPasteboard are used.
22 @interface MockDraggingInfo : NSObject<NSDraggingInfo> { 37 @interface MockDraggingInfo : NSObject<NSDraggingInfo> {
23 NSPasteboard* pasteboard_; 38 NSPasteboard* pasteboard_;
24 } 39 }
25 40
26 @property BOOL animatesToDestination; 41 @property BOOL animatesToDestination;
27 @property NSInteger numberOfValidItemsForDrop; 42 @property NSInteger numberOfValidItemsForDrop;
28 @property NSDraggingFormation draggingFormation; 43 @property NSDraggingFormation draggingFormation;
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 SetData(data); 217 SetData(data);
203 218
204 target_->set_formats(ui::OSExchangeData::STRING | ui::OSExchangeData::URL); 219 target_->set_formats(ui::OSExchangeData::STRING | ui::OSExchangeData::URL);
205 220
206 // Check if the target receives the data from a drop and returns the expected 221 // Check if the target receives the data from a drop and returns the expected
207 // operation. 222 // operation.
208 EXPECT_EQ(DragUpdate(nil), NSDragOperationCopy); 223 EXPECT_EQ(DragUpdate(nil), NSDragOperationCopy);
209 EXPECT_EQ(Drop(), NSDragOperationMove); 224 EXPECT_EQ(Drop(), NSDragOperationMove);
210 } 225 }
211 226
227 // Tests that widget releases mouse capture when drag'n'drop is started.
tapted 2016/09/21 10:07:05 Since StartDragAndDrop() spins the runloop, the te
snake 2016/09/21 12:29:52 Done.
228 TEST_F(DragDropClientMacTest, ReleaseCapture) {
229 ui::test::EventGenerator event_generator(GetContext(),
230 widget_->GetNativeWindow());
231 // Generate mouse press to toggle dragging_ flag in BaseView.
tapted 2016/09/21 10:07:05 this comment is out of date
snake 2016/09/21 12:29:52 Done.
232 // StartDragAndDrop() should disable it.
233 event_generator.MoveMouseTo(gfx::Point(50, 50));
234 event_generator.PressLeftButton();
235
236 // DragDropView doesn't actually capture the mouse, so explicitly acquire it
237 // to test that StartDragAndDrop() actually releases it.
238 bridge_->AcquireCapture();
tapted 2016/09/21 10:07:05 Be aware that capture on Windows typically can't b
snake 2016/09/21 12:29:52 Done.
239 EXPECT_TRUE(bridge_->HasCapture());
240
241 // Create the drop data
242 OSExchangeData data;
243 const base::string16& text = ASCIIToUTF16("text");
244 data.SetString(text);
245 SetData(data);
246
247 // There's no way to cleanly stop NSDraggingSession inside unit tests, so just
248 // don't start it at all.
249 base::mac::ScopedObjCClassSwizzler swizzle(
250 [NSView class], @selector(beginDraggingSessionWithItems:event:source:),
251 @selector(cr_beginDraggingSessionWithItems:event:source:));
252
253 // Immediately quit drag'n'drop, or we'll hang.
254 base::ThreadTaskRunnerHandle::Get()->PostTask(
255 FROM_HERE, base::Bind(&DragDropClientMac::EndDrag,
256 base::Unretained(drag_drop_client())));
257
258 // It will call ReleaseCapture(), and BaseView will no longer think it's
tapted 2016/09/21 10:07:05 update comment.
snake 2016/09/21 12:29:52 Done.
259 // dragging.
260 drag_drop_client()->StartDragAndDrop(
261 target_, data, 0, ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE);
262
263 EXPECT_FALSE(bridge_->HasCapture());
264 }
265
212 // Tests if the drag and drop target rejects the dropped data with the 266 // Tests if the drag and drop target rejects the dropped data with the
213 // incorrect format. 267 // incorrect format.
214 TEST_F(DragDropClientMacTest, InvalidFormatDragDrop) { 268 TEST_F(DragDropClientMacTest, InvalidFormatDragDrop) {
215 OSExchangeData data; 269 OSExchangeData data;
216 const base::string16& text = ASCIIToUTF16("text"); 270 const base::string16& text = ASCIIToUTF16("text");
217 data.SetString(text); 271 data.SetString(text);
218 SetData(data); 272 SetData(data);
219 273
220 target_->set_formats(ui::OSExchangeData::URL); 274 target_->set_formats(ui::OSExchangeData::URL);
221 275
(...skipping 17 matching lines...) Expand all
239 293
240 // Add valid data to the pasteboard and check to see if the target accepts 294 // Add valid data to the pasteboard and check to see if the target accepts
241 // it. 295 // it.
242 [pasteboard setString:@"text" forType:NSPasteboardTypeString]; 296 [pasteboard setString:@"text" forType:NSPasteboardTypeString];
243 EXPECT_EQ(DragUpdate(pasteboard), NSDragOperationCopy); 297 EXPECT_EQ(DragUpdate(pasteboard), NSDragOperationCopy);
244 EXPECT_EQ(Drop(), NSDragOperationMove); 298 EXPECT_EQ(Drop(), NSDragOperationMove);
245 } 299 }
246 300
247 } // namespace test 301 } // namespace test
248 } // namespace views 302 } // namespace views
OLDNEW
« ui/views/cocoa/drag_drop_client_mac.mm ('K') | « 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