| 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( |
| 83 [[TestViewDropTarget alloc] initWithController:controller_mock_]); |
| 84 |
| 85 [[controller_mock_ stub] hideDropURLsIndicatorInView:view_.get()]; |
| 86 |
| 87 drop_handler_.reset([[URLDropTargetHandler alloc] initWithView:view_]); |
| 88 } |
| 89 |
| 90 id UrlDropControllerTest::GetFakeDragInfoForPasteboardItem( |
| 91 NSPasteboardItem* item) { |
| 92 // initializing pasteboard |
| 93 [pasteboard_->get() clearContents]; |
| 94 [pasteboard_->get() writeObjects:@[ item ]]; |
| 95 |
| 96 // creating drag_info |
| 97 id source = [OCMockObject mockForClass:[NSObject class]]; |
| 98 id dragInfo = [OCMockObject mockForProtocol:@protocol(NSDraggingInfo)]; |
| 99 [[[dragInfo stub] andReturn:pasteboard_->get()] draggingPasteboard]; |
| 100 [[[dragInfo stub] andReturnNSPoint:kDropPoint] draggingLocation]; |
| 101 [[[dragInfo stub] andReturn:source] draggingSource]; |
| 102 [[[dragInfo stub] andReturnUnsignedInteger: |
| 103 NSDragOperationCopy | |
| 104 NSDragOperationMove] draggingSourceOperationMask]; |
| 105 return dragInfo; |
| 106 } |
| 107 |
| 108 TEST_F(UrlDropControllerTest, DragAndDropText) { |
| 109 constexpr NSString* text = @"query"; |
| 110 [[ControllerMock() expect] dropText:text inView:View() at:kDropPoint]; |
| 111 |
| 112 base::scoped_nsobject<NSPasteboardItem> item = |
| 113 ui::ClipboardUtil::PasteboardItemFromString(text); |
| 114 |
| 115 [View() performDragOperation:GetFakeDragInfoForPasteboardItem(item.get())]; |
| 116 [ControllerMock() verify]; |
| 117 } |
| 118 |
| 119 TEST_F(UrlDropControllerTest, DragAndDropTextParsableAsURL) { |
| 120 constexpr NSString* text = @"query: query"; |
| 121 [[ControllerMock() expect] dropText:text inView:View() at:kDropPoint]; |
| 122 |
| 123 base::scoped_nsobject<NSPasteboardItem> item = |
| 124 ui::ClipboardUtil::PasteboardItemFromString(text); |
| 125 |
| 126 [View() performDragOperation:GetFakeDragInfoForPasteboardItem(item.get())]; |
| 127 [ControllerMock() verify]; |
| 128 } |
| 129 |
| 130 TEST_F(UrlDropControllerTest, DragAndDropURL) { |
| 131 NSArray* urls = [NSArray arrayWithObject:@"http://abc.xyz"]; |
| 132 NSArray* titles = [NSArray arrayWithObject:@"abc"]; |
| 133 |
| 134 [[ControllerMock() expect] dropURLs:urls inView:View() at:kDropPoint]; |
| 135 |
| 136 base::scoped_nsobject<NSPasteboardItem> item = |
| 137 ui::ClipboardUtil::PasteboardItemFromUrls(urls, titles); |
| 138 |
| 139 [View() performDragOperation:GetFakeDragInfoForPasteboardItem(item.get())]; |
| 140 [ControllerMock() verify]; |
| 141 } |
| OLD | NEW |