| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 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 "chrome/browser/ui/cocoa/url_drop_target.h" |
| 6 |
| 7 #import <Cocoa/Cocoa.h> |
| 8 |
| 9 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #import "third_party/ocmock/OCMock/OCMock.h" |
| 12 #import "third_party/ocmock/ocmock_extensions.h" |
| 13 #include "ui/base/clipboard/clipboard_util_mac.h" |
| 14 |
| 15 namespace { |
| 16 constexpr NSPoint kDropPoint = {10, 10}; |
| 17 } // namespace |
| 18 |
| 19 @interface TestViewDropTarget : NSView<URLDropTarget> { |
| 20 @private |
| 21 id<URLDropTargetController> controller_; |
| 22 base::scoped_nsobject<URLDropTargetHandler> dropHandler_; |
| 23 } |
| 24 |
| 25 @end |
| 26 |
| 27 @implementation TestViewDropTarget |
| 28 |
| 29 - (id)initWithController:(id<URLDropTargetController>)controller { |
| 30 if (self = [super init]) { |
| 31 controller_ = controller; |
| 32 dropHandler_.reset([[URLDropTargetHandler alloc] initWithView:self]); |
| 33 } |
| 34 return self; |
| 35 } |
| 36 |
| 37 // URLDropTarget protocol. |
| 38 - (id<URLDropTargetController>)urlDropController { |
| 39 return controller_; |
| 40 } |
| 41 |
| 42 - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender { |
| 43 return [dropHandler_ draggingEntered:sender]; |
| 44 } |
| 45 |
| 46 - (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender { |
| 47 return [dropHandler_ draggingUpdated:sender]; |
| 48 } |
| 49 |
| 50 - (void)draggingExited:(id<NSDraggingInfo>)sender { |
| 51 return [dropHandler_ draggingExited:sender]; |
| 52 } |
| 53 |
| 54 - (BOOL)performDragOperation:(id<NSDraggingInfo>)sender { |
| 55 return [dropHandler_ performDragOperation:sender]; |
| 56 } |
| 57 |
| 58 @end |
| 59 |
| 60 class UrlDropControllerTest : public CocoaTest { |
| 61 protected: |
| 62 UrlDropControllerTest(); |
| 63 |
| 64 id GetFakeDragInfoForPasteboardItem(NSPasteboardItem* item); |
| 65 |
| 66 URLDropTargetHandler* DropHandler() { return drop_handler_.get(); } |
| 67 id ControllerMock() { return controller_mock_; } |
| 68 TestViewDropTarget* View() { return view_; } |
| 69 |
| 70 private: |
| 71 scoped_refptr<ui::UniquePasteboard> pasteboard_; |
| 72 base::scoped_nsobject<TestViewDropTarget> view_; |
| 73 base::scoped_nsobject<id> controller_mock_; |
| 74 base::scoped_nsobject<URLDropTargetHandler> drop_handler_; |
| 75 }; |
| 76 |
| 77 UrlDropControllerTest::UrlDropControllerTest() |
| 78 : pasteboard_(new ui::UniquePasteboard()) { |
| 79 controller_mock_.reset([[OCMockObject |
| 80 mockForProtocol:@protocol(URLDropTargetController)] retain]); |
| 81 |
| 82 view_.reset([[TestViewDropTarget alloc] initWithController:controller_mock_]); |
| 83 |
| 84 [[controller_mock_ stub] hideDropURLsIndicatorInView:view_.get()]; |
| 85 |
| 86 drop_handler_.reset([[URLDropTargetHandler alloc] initWithView:view_]); |
| 87 } |
| 88 |
| 89 id UrlDropControllerTest::GetFakeDragInfoForPasteboardItem( |
| 90 NSPasteboardItem* item) { |
| 91 // initializing pasteboard |
| 92 [pasteboard_->get() clearContents]; |
| 93 [pasteboard_->get() writeObjects:@[ item ]]; |
| 94 |
| 95 // creating dragInfo |
| 96 id source = [OCMockObject mockForClass:[NSObject class]]; |
| 97 id dragInfo = [OCMockObject mockForProtocol:@protocol(NSDraggingInfo)]; |
| 98 [[[dragInfo stub] andReturn:pasteboard_->get()] draggingPasteboard]; |
| 99 [[[dragInfo stub] andReturnNSPoint:kDropPoint] draggingLocation]; |
| 100 [[[dragInfo stub] andReturn:source] draggingSource]; |
| 101 [[[dragInfo stub] |
| 102 andReturnUnsignedInteger:NSDragOperationCopy | NSDragOperationMove] |
| 103 draggingSourceOperationMask]; |
| 104 return dragInfo; |
| 105 } |
| 106 |
| 107 TEST_F(UrlDropControllerTest, DragAndDropText) { |
| 108 constexpr NSString* text = @"query"; |
| 109 [[ControllerMock() expect] dropText:text inView:View() at:kDropPoint]; |
| 110 |
| 111 base::scoped_nsobject<NSPasteboardItem> item = |
| 112 ui::ClipboardUtil::PasteboardItemFromString(text); |
| 113 |
| 114 [View() performDragOperation:GetFakeDragInfoForPasteboardItem(item.get())]; |
| 115 [ControllerMock() verify]; |
| 116 } |
| 117 |
| 118 TEST_F(UrlDropControllerTest, DragAndDropTextParsableAsURL) { |
| 119 // Different drop targets might choose different behaviors |
| 120 // for processing text parsable as URL. For example: tabstrip runs |
| 121 // Autocomplete on such text and bookmarks always try to parse it. |
| 122 { |
| 123 constexpr NSString* text = @"http://edition.cnn.com/"; |
| 124 [[ControllerMock() expect] dropText:text inView:View() at:kDropPoint]; |
| 125 |
| 126 base::scoped_nsobject<NSPasteboardItem> item = |
| 127 ui::ClipboardUtil::PasteboardItemFromString(text); |
| 128 |
| 129 [View() performDragOperation:GetFakeDragInfoForPasteboardItem(item.get())]; |
| 130 [ControllerMock() verify]; |
| 131 } |
| 132 // Motivational example for not always attempting interpreting strings as |
| 133 // URLs. |
| 134 { |
| 135 constexpr NSString* text = @"query: query"; |
| 136 [[ControllerMock() expect] dropText:text inView:View() at:kDropPoint]; |
| 137 |
| 138 base::scoped_nsobject<NSPasteboardItem> item = |
| 139 ui::ClipboardUtil::PasteboardItemFromString(text); |
| 140 |
| 141 [View() performDragOperation:GetFakeDragInfoForPasteboardItem(item.get())]; |
| 142 [ControllerMock() verify]; |
| 143 } |
| 144 } |
| 145 |
| 146 TEST_F(UrlDropControllerTest, DragAndDropURL) { |
| 147 NSArray* urls = [NSArray arrayWithObject:@"http://abc.xyz"]; |
| 148 NSArray* titles = [NSArray arrayWithObject:@"abc"]; |
| 149 |
| 150 [[ControllerMock() expect] dropURLs:urls inView:View() at:kDropPoint]; |
| 151 |
| 152 base::scoped_nsobject<NSPasteboardItem> item = |
| 153 ui::ClipboardUtil::PasteboardItemFromUrls(urls, titles); |
| 154 |
| 155 [View() performDragOperation:GetFakeDragInfoForPasteboardItem(item.get())]; |
| 156 [ControllerMock() verify]; |
| 157 } |
| OLD | NEW |