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_native_widget.h" | |
| 12 #include "ui/views/test/widget_test.h" | |
| 13 #include "ui/views/view.h" | |
| 14 #include "ui/views/widget/native_widget_mac.h" | |
| 15 #include "ui/views/widget/widget.h" | |
| 16 | |
| 17 using base::ASCIIToUTF16; | |
| 18 | |
| 19 // Mocks the NSDraggingInfo sent to the DragDropClientMac's DragUpdate() and | |
| 20 // Drop() methods. Out of the required methods of the protocol, only | |
| 21 // draggingLocation and draggingPasteboard are used. | |
| 22 @interface MockDraggingInfo : NSObject<NSDraggingInfo> { | |
| 23 NSPasteboard* pasteboard_; | |
| 24 } | |
| 25 | |
| 26 @property BOOL animatesToDestination; | |
| 27 @property NSInteger numberOfValidItemsForDrop; | |
| 28 @property NSDraggingFormation draggingFormation; | |
| 29 @property(readonly) NSSpringLoadingHighlight springLoadingHighlight; | |
| 30 | |
| 31 @end | |
| 32 | |
| 33 @implementation MockDraggingInfo | |
| 34 | |
| 35 @synthesize animatesToDestination; | |
| 36 @synthesize numberOfValidItemsForDrop; | |
| 37 @synthesize draggingFormation; | |
| 38 @synthesize springLoadingHighlight; | |
| 39 | |
| 40 - (id)initWithPasteboard:(NSPasteboard*)pasteboard { | |
| 41 if ((self = [super init])) { | |
| 42 pasteboard_ = pasteboard; | |
| 43 } | |
| 44 return self; | |
| 45 } | |
| 46 | |
| 47 - (NSPoint)draggingLocation { | |
| 48 return NSMakePoint(50, 50); | |
| 49 } | |
| 50 | |
| 51 - (NSPasteboard*)draggingPasteboard { | |
| 52 return pasteboard_; | |
| 53 } | |
| 54 | |
| 55 - (NSInteger)draggingSequenceNumber { | |
| 56 return 0; | |
| 57 } | |
| 58 | |
| 59 - (id)draggingSource { | |
| 60 return nil; | |
| 61 } | |
| 62 | |
| 63 - (NSDragOperation)draggingSourceOperationMask { | |
| 64 return NSDragOperationNone; | |
| 65 } | |
| 66 | |
| 67 - (NSWindow*)draggingDestinationWindow { | |
| 68 return nil; | |
| 69 } | |
| 70 | |
| 71 - (NSArray*)namesOfPromisedFilesDroppedAtDestination:(NSURL*)dropDestination { | |
| 72 return nil; | |
| 73 } | |
| 74 | |
| 75 - (NSImage*)draggedImage { | |
| 76 return nil; | |
| 77 } | |
| 78 | |
| 79 - (NSPoint)draggedImageLocation { | |
| 80 return NSZeroPoint; | |
| 81 } | |
| 82 | |
| 83 - (void)slideDraggedImageTo:(NSPoint)aPoint { | |
| 84 } | |
| 85 | |
| 86 - (void) | |
| 87 enumerateDraggingItemsWithOptions:(NSDraggingItemEnumerationOptions)enumOpts | |
| 88 forView:(NSView*)view | |
| 89 classes:(NSArray*)classArray | |
| 90 searchOptions:(NSDictionary*)searchOptions | |
| 91 usingBlock:(void (^)(NSDraggingItem* draggingItem, | |
| 92 NSInteger idx, | |
| 93 BOOL* stop))block { | |
| 94 } | |
| 95 | |
| 96 - (void)resetSpringLoading { | |
| 97 } | |
| 98 | |
| 99 @end | |
| 100 | |
| 101 namespace views { | |
| 102 namespace test { | |
| 103 | |
| 104 // View object that will receive and process dropped data from the test. | |
| 105 class DragDropView : public View { | |
| 106 public: | |
| 107 DragDropView() {} | |
| 108 | |
| 109 void set_formats(int formats) { formats_ = formats; } | |
| 110 | |
| 111 // View: | |
| 112 bool GetDropFormats( | |
| 113 int* formats, | |
| 114 std::set<ui::Clipboard::FormatType>* format_types) override { | |
| 115 *formats |= formats_; | |
| 116 return true; | |
| 117 } | |
| 118 | |
| 119 bool CanDrop(const OSExchangeData& data) override { return true; } | |
| 120 | |
| 121 int OnDragUpdated(const ui::DropTargetEvent& event) override { | |
| 122 return ui::DragDropTypes::DRAG_COPY; | |
| 123 } | |
| 124 | |
| 125 int OnPerformDrop(const ui::DropTargetEvent& event) override { | |
| 126 return ui::DragDropTypes::DRAG_MOVE; | |
| 127 } | |
| 128 | |
| 129 private: | |
| 130 // Drop formats accepted by this View object. | |
| 131 int formats_ = 0; | |
| 132 | |
| 133 DISALLOW_COPY_AND_ASSIGN(DragDropView); | |
| 134 }; | |
| 135 | |
| 136 class CocoaDragDropTest : public WidgetTest { | |
|
tapted
2016/06/02 00:24:51
oops - we should name this class to be consistent
spqchan
2016/06/02 22:01:19
Done.
| |
| 137 public: | |
| 138 CocoaDragDropTest() : widget_(new Widget) {} | |
| 139 | |
| 140 DragDropClientMac* drag_drop_client() { return bridge_->drag_drop_client(); } | |
| 141 | |
| 142 NSDragOperation DragUpdate(NSPasteboard* pasteboard) { | |
| 143 DragDropClientMac* client = drag_drop_client(); | |
| 144 dragging_info_.reset( | |
| 145 [[MockDraggingInfo alloc] initWithPasteboard:pasteboard]); | |
| 146 return client->DragUpdate(dragging_info_.get()); | |
| 147 } | |
| 148 | |
| 149 NSDragOperation Drop() { | |
| 150 DragDropClientMac* client = drag_drop_client(); | |
| 151 DCHECK(dragging_info_.get()); | |
| 152 NSDragOperation operation = client->Drop(dragging_info_.get()); | |
| 153 dragging_info_.reset(); | |
| 154 return operation; | |
| 155 } | |
| 156 | |
| 157 void SetData(OSExchangeData& data) { | |
| 158 drag_drop_client()->data_source_.reset( | |
| 159 [[CocoaDragDropDataProvider alloc] initWithData:data]); | |
| 160 } | |
| 161 | |
| 162 // testing::Test: | |
| 163 void SetUp() override { | |
| 164 WidgetTest::SetUp(); | |
| 165 | |
| 166 widget_ = CreateTopLevelPlatformWidget(); | |
| 167 gfx::Rect bounds(0, 0, 100, 100); | |
| 168 widget_->SetBounds(bounds); | |
| 169 | |
| 170 bridge_ = | |
| 171 NativeWidgetMac::GetBridgeForNativeWindow(widget_->GetNativeWindow()); | |
| 172 widget_->Show(); | |
| 173 | |
| 174 target_ = new DragDropView(); | |
| 175 widget_->GetContentsView()->AddChildView(target_); | |
| 176 target_->SetBoundsRect(bounds); | |
| 177 | |
| 178 drag_drop_client()->operation_ = ui::DragDropTypes::DRAG_COPY; | |
| 179 } | |
| 180 | |
| 181 void TearDown() override { | |
| 182 widget_->CloseNow(); | |
| 183 WidgetTest::TearDown(); | |
| 184 } | |
| 185 | |
| 186 protected: | |
| 187 Widget* widget_ = nullptr; | |
| 188 BridgedNativeWidget* bridge_ = nullptr; | |
| 189 DragDropView* target_ = nullptr; | |
| 190 base::scoped_nsobject<MockDraggingInfo> dragging_info_; | |
| 191 | |
| 192 private: | |
| 193 DISALLOW_COPY_AND_ASSIGN(CocoaDragDropTest); | |
| 194 }; | |
| 195 | |
| 196 // Tests if the drag and drop target receives the dropped data. | |
| 197 TEST_F(CocoaDragDropTest, BasicDragDrop) { | |
| 198 // Create the drop data | |
| 199 OSExchangeData data; | |
| 200 const base::string16& text = ASCIIToUTF16("text"); | |
| 201 data.SetString(text); | |
| 202 SetData(data); | |
| 203 | |
| 204 target_->set_formats(ui::OSExchangeData::STRING | ui::OSExchangeData::URL); | |
| 205 | |
| 206 // Check if the target receives the data from a drop and returns the expected | |
| 207 // operation. | |
| 208 EXPECT_EQ(DragUpdate(nil), NSDragOperationCopy); | |
| 209 EXPECT_EQ(Drop(), NSDragOperationMove); | |
| 210 } | |
| 211 | |
| 212 // Tests if the drag and drop target rejects the dropped data with the | |
| 213 // incorrect format. | |
| 214 TEST_F(CocoaDragDropTest, InvalidFormatDragDrop) { | |
| 215 OSExchangeData data; | |
| 216 const base::string16& text = ASCIIToUTF16("text"); | |
| 217 data.SetString(text); | |
| 218 SetData(data); | |
| 219 | |
| 220 target_->set_formats(ui::OSExchangeData::URL); | |
| 221 | |
| 222 // Check if the target receives the data from a drop and returns the expected | |
| 223 // operation. | |
| 224 EXPECT_EQ(DragUpdate(nil), NSDragOperationNone); | |
| 225 EXPECT_EQ(Drop(), NSDragOperationNone); | |
| 226 } | |
| 227 | |
| 228 // Tests if the drag and drop target can accept data without an OSExchangeData | |
| 229 // object. | |
| 230 TEST_F(CocoaDragDropTest, PasteboardToOSExchangeTest) { | |
| 231 target_->set_formats(ui::OSExchangeData::STRING); | |
| 232 | |
| 233 NSPasteboard* pasteboard = [NSPasteboard pasteboardWithUniqueName]; | |
| 234 | |
| 235 // The test should reject the data if the pasteboard is empty. | |
| 236 EXPECT_EQ(DragUpdate(pasteboard), NSDragOperationNone); | |
| 237 EXPECT_EQ(Drop(), NSDragOperationNone); | |
| 238 drag_drop_client()->EndDrag(); | |
| 239 | |
| 240 // Add valid data to the pasteboard and check to see if the target accepts | |
| 241 // it. | |
| 242 [pasteboard setString:@"text" forType:NSPasteboardTypeString]; | |
| 243 EXPECT_EQ(DragUpdate(pasteboard), NSDragOperationCopy); | |
| 244 EXPECT_EQ(Drop(), NSDragOperationMove); | |
| 245 } | |
| 246 | |
| 247 } // namespace test | |
| 248 } // namespace views | |
| OLD | NEW |