Chromium Code Reviews| 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/mac/sdk_forward_declarations.h" | |
| 10 #include "base/strings/utf_string_conversions.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 { | |
| 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_MOVE; | |
| 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 dragging_info_.reset( | |
| 170 [[MockDraggingInfo alloc] initWithPasteboard:pasteboard]); | |
| 171 return client->DragUpdate(dragging_info_.get()); | |
| 172 } | |
| 173 | |
| 174 NSDragOperation Drop(NSPasteboard* pasteboard) { | |
|
tapted
2016/06/01 01:57:01
remove |pasteboard| argument
spqchan
2016/06/01 19:09:28
Done.
| |
| 175 DragDropClientMac* client = drag_drop_client(); | |
| 176 DCHECK(dragging_info_.get()); | |
| 177 NSDragOperation operation = client->Drop(dragging_info_.get()); | |
| 178 dragging_info_.reset(); | |
| 179 return operation; | |
| 180 } | |
| 181 | |
| 182 void SetData(OSExchangeData& data) { | |
| 183 drag_drop_client()->data_source_.reset( | |
| 184 [[CocoaDragDropDataProvider alloc] initWithData:data]); | |
| 185 } | |
| 186 | |
| 187 protected: | |
| 188 Widget* widget_ = nullptr; | |
| 189 BridgedNativeWidget* bridge_ = nullptr; | |
| 190 DragDropView* target_ = nullptr; | |
| 191 base::scoped_nsobject<MockDraggingInfo> dragging_info_; | |
| 192 DISALLOW_COPY_AND_ASSIGN(CocoaDragDropTest); | |
|
tapted
2016/06/01 01:57:01
nit: blank line before, but also this needs to be
spqchan
2016/06/01 19:09:29
Done.
| |
| 193 }; | |
| 194 | |
| 195 // Tests if the drag and drop target receives the dropped data. | |
| 196 TEST_F(CocoaDragDropTest, BasicDragDrop) { | |
| 197 // Create the drop data | |
| 198 OSExchangeData data; | |
| 199 const base::string16& text = ASCIIToUTF16("text"); | |
| 200 data.SetString(text); | |
| 201 SetData(data); | |
| 202 | |
| 203 target_->set_formats(ui::OSExchangeData::STRING | ui::OSExchangeData::URL); | |
| 204 | |
| 205 // Check if the target receives the data from a drop and returns the expected | |
| 206 // operation. | |
| 207 EXPECT_EQ(DragUpdate(nil), NSDragOperationCopy); | |
| 208 EXPECT_EQ(Drop(nil), NSDragOperationMove); | |
| 209 } | |
| 210 | |
| 211 // Tests if the drag and drop target rejects the dropped data with the | |
| 212 // incorrect format. | |
| 213 TEST_F(CocoaDragDropTest, InvalidFormatDragDrop) { | |
| 214 OSExchangeData data; | |
| 215 const base::string16& text = ASCIIToUTF16("text"); | |
| 216 data.SetString(text); | |
| 217 SetData(data); | |
| 218 | |
| 219 target_->set_formats(ui::OSExchangeData::URL); | |
| 220 | |
| 221 // Check if the target receives the data from a drop and returns the expected | |
| 222 // operation. | |
| 223 EXPECT_EQ(DragUpdate(nil), NSDragOperationNone); | |
| 224 EXPECT_EQ(Drop(nil), NSDragOperationNone); | |
| 225 } | |
| 226 | |
| 227 // Tests if the drag and drop target can accept data without an OSExchangeData | |
| 228 // object. | |
| 229 TEST_F(CocoaDragDropTest, PasteboardToOSExchangeTest) { | |
| 230 target_->set_formats(ui::OSExchangeData::STRING); | |
| 231 | |
| 232 NSPasteboard* pasteboard = [NSPasteboard pasteboardWithUniqueName]; | |
| 233 | |
| 234 EXPECT_EQ(DragUpdate(pasteboard), NSDragOperationNone); | |
| 235 EXPECT_EQ(Drop(pasteboard), NSDragOperationNone); | |
| 236 drag_drop_client()->EndDrag(); | |
| 237 | |
| 238 [pasteboard setString:@"text" forType:NSPasteboardTypeString]; | |
| 239 | |
| 240 EXPECT_EQ(DragUpdate(pasteboard), NSDragOperationCopy); | |
| 241 EXPECT_EQ(Drop(pasteboard), NSDragOperationMove); | |
| 242 } | |
| 243 | |
| 244 } // namespace test | |
| 245 } // namespace views | |
| OLD | NEW |