 Chromium Code Reviews
 Chromium Code Reviews Issue 1964283002:
  MacViews: Implemented Drag & Drop  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 1964283002:
  MacViews: Implemented Drag & Drop  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| Index: ui/views/cocoa/desktop_drag_drop_client_mac_unittest.mm | 
| diff --git a/ui/views/cocoa/desktop_drag_drop_client_mac_unittest.mm b/ui/views/cocoa/desktop_drag_drop_client_mac_unittest.mm | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..5fbab1488afcfb40fba4573e3c6e9e932bf90523 | 
| --- /dev/null | 
| +++ b/ui/views/cocoa/desktop_drag_drop_client_mac_unittest.mm | 
| @@ -0,0 +1,244 @@ | 
| +// Copyright 2016 The Chromium Authors. All rights reserved. | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +#import "ui/views/cocoa/drag_drop_client_mac.h" | 
| + | 
| +#import <Cocoa/Cocoa.h> | 
| + | 
| +#include "base/strings/utf_string_conversions.h" | 
| +#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.
 | 
| +#import "ui/views/cocoa/bridged_content_view.h" | 
| +#import "ui/views/cocoa/bridged_native_widget.h" | 
| +#include "ui/views/test/widget_test.h" | 
| +#include "ui/views/view.h" | 
| +#include "ui/views/widget/native_widget_mac.h" | 
| +#include "ui/views/widget/root_view.h" | 
| +#include "ui/views/widget/widget.h" | 
| + | 
| +using base::ASCIIToUTF16; | 
| + | 
| +// Mocks the NSDraggingInfo sent to the DragDropClientMac's DragUpdate() and | 
| +// Drop() methods. Out of the required methods of NSDraggingInfo, only | 
| +// draggingLocation and draggingPasteboard are used. | 
| +@interface MockDraggingInfo : NSObject<NSDraggingInfo> { | 
| + NSPasteboard* pasteboard_; | 
| +} | 
| + | 
| +@property BOOL animatesToDestination; | 
| +@property NSInteger numberOfValidItemsForDrop; | 
| +@property NSDraggingFormation draggingFormation; | 
| +@property(readonly) NSSpringLoadingHighlight springLoadingHighlight; | 
| + | 
| +@end | 
| + | 
| +@implementation MockDraggingInfo | 
| + | 
| +@synthesize animatesToDestination; | 
| +@synthesize numberOfValidItemsForDrop; | 
| +@synthesize draggingFormation; | 
| +@synthesize springLoadingHighlight; | 
| + | 
| +- (id)initWithPasteboard:(NSPasteboard*)pasteboard { | 
| + if ((self = [super init])) { | 
| + pasteboard_ = pasteboard; | 
| + } | 
| + return self; | 
| +} | 
| + | 
| +- (NSPoint)draggingLocation { | 
| + return NSMakePoint(50, 50); | 
| +} | 
| + | 
| +- (NSPasteboard*)draggingPasteboard { | 
| + return pasteboard_; | 
| +} | 
| + | 
| +- (NSInteger)draggingSequenceNumber { | 
| + return 0; | 
| +} | 
| + | 
| +- (id)draggingSource { | 
| + return nil; | 
| +} | 
| + | 
| +- (NSDragOperation)draggingSourceOperationMask { | 
| + return NSDragOperationNone; | 
| +} | 
| + | 
| +- (NSWindow*)draggingDestinationWindow { | 
| + return nil; | 
| +} | 
| + | 
| +- (NSArray*)namesOfPromisedFilesDroppedAtDestination:(NSURL*)dropDestination { | 
| + return nil; | 
| +} | 
| + | 
| +- (NSImage*)draggedImage { | 
| + return nil; | 
| +} | 
| + | 
| +- (NSPoint)draggedImageLocation { | 
| + return NSZeroPoint; | 
| +} | 
| + | 
| +- (void)slideDraggedImageTo:(NSPoint)aPoint { | 
| +} | 
| + | 
| +- (void) | 
| +enumerateDraggingItemsWithOptions:(NSDraggingItemEnumerationOptions)enumOpts | 
| + forView:(NSView*)view | 
| + classes:(NSArray*)classArray | 
| + searchOptions:(NSDictionary*)searchOptions | 
| + usingBlock:(void (^)(NSDraggingItem* draggingItem, | 
| + NSInteger idx, | 
| + BOOL* stop))block { | 
| +} | 
| + | 
| +- (void)resetSpringLoading { | 
| +} | 
| + | 
| +@end | 
| + | 
| +namespace views { | 
| +namespace test { | 
| + | 
| +// View object that will receive and process dropped data from the test. | 
| +class DragDropView : public View { | 
| + public: | 
| + DragDropView() {} | 
| + | 
| + void set_formats(int formats) { formats_ = formats; } | 
| + | 
| + // Views: | 
| + bool GetDropFormats( | 
| + int* formats, | 
| + std::set<ui::Clipboard::FormatType>* format_types) override { | 
| + *formats |= formats_; | 
| + return true; | 
| + } | 
| + | 
| + bool CanDrop(const OSExchangeData& data) override { return true; } | 
| + | 
| + int OnDragUpdated(const ui::DropTargetEvent& event) override { | 
| + return ui::DragDropTypes::DRAG_COPY; | 
| + } | 
| + | 
| + int OnPerformDrop(const ui::DropTargetEvent& event) override { | 
| + return ui::DragDropTypes::DRAG_COPY; | 
| + } | 
| + | 
| + private: | 
| + // Drop formats accepted by this View object. | 
| + int formats_ = 0; | 
| + | 
| + DISALLOW_COPY_AND_ASSIGN(DragDropView); | 
| +}; | 
| + | 
| +class CocoaDragDropTest : public WidgetTest { | 
| + public: | 
| + CocoaDragDropTest() : widget_(new Widget) {} | 
| + | 
| + DragDropClientMac* drag_drop_client() { return bridge_->drag_drop_client(); } | 
| + | 
| + void SetUp() override { | 
| + WidgetTest::SetUp(); | 
| + | 
| + widget_ = CreateTopLevelPlatformWidget(); | 
| + gfx::Rect bounds(0, 0, 100, 100); | 
| + widget_->SetBounds(bounds); | 
| + | 
| + bridge_ = | 
| + NativeWidgetMac::GetBridgeForNativeWindow(widget_->GetNativeWindow()); | 
| + widget_->Show(); | 
| + | 
| + target_ = new DragDropView(); | 
| + widget_->GetContentsView()->AddChildView(target_); | 
| + target_->SetBoundsRect(bounds); | 
| + | 
| + drag_drop_client()->operation_ = ui::DragDropTypes::DRAG_COPY; | 
| + } | 
| + | 
| + void TearDown() override { | 
| + widget_->CloseNow(); | 
| + WidgetTest::TearDown(); | 
| + } | 
| + | 
| + NSDragOperation DragUpdate(NSPasteboard* pasteboard) { | 
| + DragDropClientMac* client = drag_drop_client(); | 
| + 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.
 | 
| + [[MockDraggingInfo alloc] initWithPasteboard:pasteboard]; | 
| + return client->DragUpdate(info); | 
| + } | 
| + | 
| + NSDragOperation Drop(NSPasteboard* pasteboard) { | 
| + DragDropClientMac* client = drag_drop_client(); | 
| + MockDraggingInfo* info = | 
| + [[MockDraggingInfo alloc] initWithPasteboard:pasteboard]; | 
| + return client->Drop(info); | 
| + } | 
| + | 
| + void SetData(OSExchangeData& data) { | 
| + drag_drop_client()->data_source_.reset( | 
| + [[CocoaDragDropDataProvider alloc] initWithData:data]); | 
| + } | 
| + | 
| + protected: | 
| + Widget* widget_ = nullptr; | 
| + BridgedNativeWidget* bridge_ = nullptr; | 
| + DragDropView* target_ = nullptr; | 
| + | 
| + DISALLOW_COPY_AND_ASSIGN(CocoaDragDropTest); | 
| +}; | 
| + | 
| +// Tests if the drag and drop target receives the dropped data. | 
| +TEST_F(CocoaDragDropTest, BasicDragDrop) { | 
| + // Create the drop data | 
| + OSExchangeData data; | 
| + const base::string16& text = ASCIIToUTF16("text"); | 
| + data.SetString(text); | 
| + SetData(data); | 
| + | 
| + target_->set_formats(ui::OSExchangeData::STRING | ui::OSExchangeData::URL); | 
| + | 
| + // Check if the target receives the data from a drop and returns the expected | 
| + // operation. | 
| + 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.
 | 
| + EXPECT_NE(Drop(nil), NSDragOperationNone); | 
| +} | 
| + | 
| +// Tests if the drag and drop target rejects the dropped data with the | 
| +// incorrect format. | 
| +TEST_F(CocoaDragDropTest, InvalidFormatDragDrop) { | 
| + OSExchangeData data; | 
| + const base::string16& text = ASCIIToUTF16("text"); | 
| + data.SetString(text); | 
| + SetData(data); | 
| + | 
| + target_->set_formats(ui::OSExchangeData::URL); | 
| + | 
| + // Check if the target receives the data from a drop and returns the expected | 
| + // operation. | 
| + EXPECT_EQ(DragUpdate(nil), NSDragOperationNone); | 
| + EXPECT_EQ(Drop(nil), NSDragOperationNone); | 
| +} | 
| + | 
| +// Tests if the drag and drop target can accept data without an OSExchangeData | 
| +// object. | 
| +TEST_F(CocoaDragDropTest, PasteboardToOSExchangeTest) { | 
| + target_->set_formats(ui::OSExchangeData::STRING); | 
| + | 
| + NSPasteboard* pasteboard = [NSPasteboard pasteboardWithUniqueName]; | 
| + | 
| + EXPECT_EQ(DragUpdate(pasteboard), NSDragOperationNone); | 
| + EXPECT_EQ(Drop(pasteboard), NSDragOperationNone); | 
| + drag_drop_client()->EndDrag(); | 
| + | 
| + [pasteboard setString:@"text" forType:NSPasteboardTypeString]; | 
| + | 
| + EXPECT_NE(DragUpdate(pasteboard), NSDragOperationNone); | 
| 
tapted
2016/05/31 11:49:58
EXPECT_EQ?
 
spqchan
2016/05/31 23:06:12
Done.
 | 
| + EXPECT_NE(Drop(pasteboard), NSDragOperationNone); | 
| +} | 
| + | 
| +} // namespace test | 
| +} // namespace views |