OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/scoped_nsobject.h" | 5 #include "base/memory/scoped_nsobject.h" |
6 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h" | 6 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_folder_controller.h" |
7 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_folder_view.h" | 7 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_folder_view.h" |
8 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h" | 8 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h" |
9 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_folder_target.h" | 9 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_folder_target.h" |
10 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" | 10 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
11 #import "chrome/browser/ui/cocoa/url_drop_target.h" | 11 #import "chrome/browser/ui/cocoa/url_drop_target.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "testing/platform_test.h" | 13 #include "testing/platform_test.h" |
14 #import "third_party/mozilla/NSPasteboard+Utils.h" | 14 #import "third_party/mozilla/NSPasteboard+Utils.h" |
| 15 #import "third_party/ocmock/OCMock/OCMock.h" |
15 | 16 |
16 namespace { | 17 namespace { |
17 const CGFloat kFakeIndicatorPos = 7.0; | |
18 }; | |
19 | 18 |
20 // Fake DraggingInfo, fake BookmarkBarController, fake NSPasteboard... | 19 // Some values used for mocks and fakes. |
21 @interface FakeDraggingInfo : NSObject { | 20 const CGFloat kFakeIndicatorPos = 7.0; |
22 @public | 21 const NSPoint kPoint = {10, 10}; |
23 BOOL dragButtonToPong_; | 22 const BOOL kYes = YES; |
24 BOOL dragURLsPong_; | 23 const BOOL kNo = NO; |
25 BOOL dragBookmarkDataPong_; | |
26 BOOL dropIndicatorShown_; | |
27 BOOL draggingEnteredCalled_; | |
28 // Only mock one type of drag data at a time. | |
29 NSString* dragDataType_; | |
30 } | |
31 @property (readwrite) BOOL dropIndicatorShown; | |
32 @property (readwrite) BOOL draggingEnteredCalled; | |
33 @property (copy) NSString* dragDataType; | |
34 @end | |
35 | |
36 @implementation FakeDraggingInfo | |
37 | |
38 @synthesize dropIndicatorShown = dropIndicatorShown_; | |
39 @synthesize draggingEnteredCalled = draggingEnteredCalled_; | |
40 @synthesize dragDataType = dragDataType_; | |
41 | |
42 - (id)init { | |
43 if ((self = [super init])) { | |
44 dropIndicatorShown_ = YES; | |
45 } | |
46 return self; | |
47 } | |
48 | |
49 - (void)dealloc { | |
50 [dragDataType_ release]; | |
51 [super dealloc]; | |
52 } | |
53 | |
54 - (void)reset { | |
55 [dragDataType_ release]; | |
56 dragDataType_ = nil; | |
57 dragButtonToPong_ = NO; | |
58 dragURLsPong_ = NO; | |
59 dragBookmarkDataPong_ = NO; | |
60 dropIndicatorShown_ = YES; | |
61 draggingEnteredCalled_ = NO; | |
62 } | |
63 | |
64 // NSDragInfo mocking functions. | |
65 | |
66 - (id)draggingPasteboard { | |
67 return self; | |
68 } | |
69 | |
70 // So we can look local. | |
71 - (id)draggingSource { | |
72 return self; | |
73 } | |
74 | |
75 - (NSDragOperation)draggingSourceOperationMask { | |
76 return NSDragOperationCopy | NSDragOperationMove; | |
77 } | |
78 | |
79 - (NSPoint)draggingLocation { | |
80 return NSMakePoint(10, 10); | |
81 } | |
82 | |
83 // NSPasteboard mocking functions. | |
84 | |
85 - (BOOL)containsURLData { | |
86 NSArray* urlTypes = [URLDropTargetHandler handledDragTypes]; | |
87 if (dragDataType_) | |
88 return [urlTypes containsObject:dragDataType_]; | |
89 return NO; | |
90 } | |
91 | |
92 - (NSData*)dataForType:(NSString*)type { | |
93 if (dragDataType_ && [dragDataType_ isEqualToString:type]) | |
94 return [NSData data]; // Return something, anything. | |
95 return nil; | |
96 } | |
97 | |
98 // Fake a controller for callback ponging | |
99 | |
100 - (BOOL)dragButton:(BookmarkButton*)button to:(NSPoint)point copy:(BOOL)copy { | |
101 dragButtonToPong_ = YES; | |
102 return YES; | |
103 } | |
104 | |
105 - (BOOL)addURLs:(NSArray*)urls withTitles:(NSArray*)titles at:(NSPoint)point { | |
106 dragURLsPong_ = YES; | |
107 return YES; | |
108 } | |
109 | |
110 - (void)getURLs:(NSArray**)outUrls | |
111 andTitles:(NSArray**)outTitles | |
112 convertingFilenames:(BOOL)convertFilenames { | |
113 } | |
114 | |
115 - (BOOL)dragBookmarkData:(id<NSDraggingInfo>)info { | |
116 dragBookmarkDataPong_ = YES; | |
117 return NO; | |
118 } | |
119 | |
120 // Confirm the pongs. | |
121 | |
122 - (BOOL)dragButtonToPong { | |
123 return dragButtonToPong_; | |
124 } | |
125 | |
126 - (BOOL)dragURLsPong { | |
127 return dragURLsPong_; | |
128 } | |
129 | |
130 - (BOOL)dragBookmarkDataPong { | |
131 return dragBookmarkDataPong_; | |
132 } | |
133 | |
134 - (CGFloat)indicatorPosForDragToPoint:(NSPoint)point { | |
135 return kFakeIndicatorPos; | |
136 } | |
137 | |
138 - (BOOL)shouldShowIndicatorShownForPoint:(NSPoint)point { | |
139 return dropIndicatorShown_; | |
140 } | |
141 | |
142 - (BOOL)draggingAllowed:(id<NSDraggingInfo>)info { | |
143 return YES; | |
144 } | |
145 | |
146 - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)info { | |
147 draggingEnteredCalled_ = YES; | |
148 return NSDragOperationNone; | |
149 } | |
150 | |
151 @end | |
152 | |
153 namespace { | |
154 | 24 |
155 class BookmarkBarFolderViewTest : public CocoaTest { | 25 class BookmarkBarFolderViewTest : public CocoaTest { |
156 public: | 26 public: |
157 virtual void SetUp() { | 27 virtual void SetUp() { |
158 CocoaTest::SetUp(); | 28 CocoaTest::SetUp(); |
159 view_.reset([[BookmarkBarFolderView alloc] init]); | 29 view_.reset([[BookmarkBarFolderView alloc] init]); |
| 30 mock_controller_.reset([GetMockController(YES) retain]); |
| 31 [view_ awakeFromNib]; |
| 32 [view_ setController:mock_controller_]; |
160 } | 33 } |
161 | 34 |
| 35 virtual void TearDown() { |
| 36 [mock_controller_ verify]; |
| 37 CocoaTest::TearDown(); |
| 38 } |
| 39 |
| 40 id GetFakePasteboardForType(NSString* dataType) { |
| 41 id pasteboard = [OCMockObject mockForClass:[NSPasteboard class]]; |
| 42 [[[pasteboard stub] andReturn:[NSData data]] dataForType:dataType]; |
| 43 [[[pasteboard stub] andReturn:nil] dataForType:OCMOCK_ANY]; |
| 44 [[[pasteboard stub] andReturnValue:OCMOCK_VALUE(kYes)] containsURLData]; |
| 45 [[pasteboard stub] getURLs:[OCMArg setTo:nil] |
| 46 andTitles:[OCMArg setTo:nil] |
| 47 convertingFilenames:YES]; |
| 48 return pasteboard; |
| 49 } |
| 50 |
| 51 id GetFakeDragInfoForType(NSString* dataType) { |
| 52 id drag_info = [OCMockObject mockForProtocol:@protocol(NSDraggingInfo)]; |
| 53 id pasteboard = GetFakePasteboardForType(dataType); |
| 54 [[[drag_info stub] andReturn:pasteboard] draggingPasteboard]; |
| 55 [[[drag_info stub] andReturnValue:[NSValue valueWithPoint:kPoint]] |
| 56 draggingLocation]; |
| 57 [[[drag_info stub] andReturn:drag_info] |
| 58 draggingSource]; |
| 59 NSDragOperation drag_operation = NSDragOperationCopy | NSDragOperationMove; |
| 60 [[[drag_info stub] andReturnValue:OCMOCK_VALUE(drag_operation)] |
| 61 draggingSourceOperationMask]; |
| 62 return drag_info; |
| 63 } |
| 64 |
| 65 id GetMockController(BOOL show_indicator) { |
| 66 id mock_controller |
| 67 = [OCMockObject mockForClass:[BookmarkBarFolderController class]]; |
| 68 [[[mock_controller stub] andReturnValue:OCMOCK_VALUE(kYes)] |
| 69 draggingAllowed:OCMOCK_ANY]; |
| 70 [[[mock_controller stub] andReturnValue:OCMOCK_VALUE(show_indicator)] |
| 71 shouldShowIndicatorShownForPoint:kPoint]; |
| 72 [[[mock_controller stub] andReturnValue:OCMOCK_VALUE(kFakeIndicatorPos)] |
| 73 indicatorPosForDragToPoint:kPoint]; |
| 74 return mock_controller; |
| 75 } |
| 76 |
| 77 scoped_nsobject<id> mock_controller_; |
162 scoped_nsobject<BookmarkBarFolderView> view_; | 78 scoped_nsobject<BookmarkBarFolderView> view_; |
163 }; | 79 }; |
164 | 80 |
165 TEST_F(BookmarkBarFolderViewTest, BookmarkButtonDragAndDrop) { | 81 TEST_F(BookmarkBarFolderViewTest, BookmarkButtonDragAndDrop) { |
166 [view_ awakeFromNib]; | 82 id drag_info = GetFakeDragInfoForType(kBookmarkButtonDragType); |
167 scoped_nsobject<FakeDraggingInfo> info([[FakeDraggingInfo alloc] init]); | 83 NSDragOperation expectedValue = NSDragOperationNone; |
168 [view_ setController:info.get()]; | 84 [[[mock_controller_ expect] andReturnValue:OCMOCK_VALUE(expectedValue)] |
169 [info reset]; | 85 draggingEntered:drag_info]; |
| 86 [[[mock_controller_ expect] andReturnValue:OCMOCK_VALUE(kNo)] |
| 87 dragBookmarkData:drag_info]; |
| 88 [[[mock_controller_ expect] andReturnValue:OCMOCK_VALUE(kYes)] |
| 89 dragButton:OCMOCK_ANY to:kPoint copy:NO]; |
170 | 90 |
171 [info setDragDataType:kBookmarkButtonDragType]; | 91 EXPECT_EQ([view_ draggingEntered:drag_info], NSDragOperationMove); |
172 EXPECT_EQ([view_ draggingEntered:(id)info.get()], NSDragOperationMove); | 92 EXPECT_TRUE([view_ performDragOperation:drag_info]); |
173 EXPECT_TRUE([view_ performDragOperation:(id)info.get()]); | |
174 EXPECT_TRUE([info dragButtonToPong]); | |
175 EXPECT_FALSE([info dragURLsPong]); | |
176 EXPECT_TRUE([info dragBookmarkDataPong]); | |
177 } | 93 } |
178 | 94 |
179 TEST_F(BookmarkBarFolderViewTest, URLDragAndDrop) { | 95 TEST_F(BookmarkBarFolderViewTest, URLDragAndDrop) { |
180 [view_ awakeFromNib]; | |
181 scoped_nsobject<FakeDraggingInfo> info([[FakeDraggingInfo alloc] init]); | |
182 [view_ setController:info.get()]; | |
183 [info reset]; | |
184 | |
185 NSArray* dragTypes = [URLDropTargetHandler handledDragTypes]; | 96 NSArray* dragTypes = [URLDropTargetHandler handledDragTypes]; |
186 for (NSString* type in dragTypes) { | 97 for (NSString* type in dragTypes) { |
187 [info setDragDataType:type]; | 98 id drag_info = GetFakeDragInfoForType(type); |
188 EXPECT_EQ([view_ draggingEntered:(id)info.get()], NSDragOperationMove); | 99 NSDragOperation expectedValue = NSDragOperationNone; |
189 EXPECT_TRUE([view_ performDragOperation:(id)info.get()]); | 100 [[[mock_controller_ expect] andReturnValue:OCMOCK_VALUE(expectedValue)] |
190 EXPECT_FALSE([info dragButtonToPong]); | 101 draggingEntered:drag_info]; |
191 EXPECT_TRUE([info dragURLsPong]); | 102 [[[mock_controller_ expect] andReturnValue:OCMOCK_VALUE(kNo)] |
192 EXPECT_TRUE([info dragBookmarkDataPong]); | 103 dragBookmarkData:drag_info]; |
193 [info reset]; | 104 [[[mock_controller_ expect] andReturnValue:OCMOCK_VALUE(kYes)] |
| 105 addURLs:OCMOCK_ANY withTitles:OCMOCK_ANY at:kPoint]; |
| 106 EXPECT_EQ([view_ draggingEntered:drag_info], NSDragOperationMove); |
| 107 EXPECT_TRUE([view_ performDragOperation:drag_info]); |
| 108 [mock_controller_ verify]; |
194 } | 109 } |
195 } | 110 } |
196 | 111 |
197 TEST_F(BookmarkBarFolderViewTest, BookmarkButtonDropIndicator) { | 112 TEST_F(BookmarkBarFolderViewTest, BookmarkButtonDropIndicator) { |
198 [view_ awakeFromNib]; | 113 id drag_info = GetFakeDragInfoForType(kBookmarkButtonDragType); |
199 scoped_nsobject<FakeDraggingInfo> info([[FakeDraggingInfo alloc] init]); | 114 NSDragOperation expectedValue = NSDragOperationNone; |
200 [view_ setController:info.get()]; | 115 [[[mock_controller_ expect] andReturnValue:OCMOCK_VALUE(expectedValue)] |
201 [info reset]; | 116 draggingEntered:drag_info]; |
202 | 117 EXPECT_EQ([view_ draggingEntered:drag_info], NSDragOperationMove); |
203 [info setDragDataType:kBookmarkButtonDragType]; | 118 [mock_controller_ verify]; |
204 EXPECT_FALSE([info draggingEnteredCalled]); | |
205 EXPECT_EQ([view_ draggingEntered:(id)info.get()], NSDragOperationMove); | |
206 EXPECT_TRUE([info draggingEnteredCalled]); // Ensure controller pinged. | |
207 EXPECT_TRUE([view_ dropIndicatorShown]); | 119 EXPECT_TRUE([view_ dropIndicatorShown]); |
208 EXPECT_EQ([view_ dropIndicatorPosition], kFakeIndicatorPos); | 120 EXPECT_EQ([view_ dropIndicatorPosition], kFakeIndicatorPos); |
209 | 121 mock_controller_.reset([GetMockController(NO) retain]); |
210 [info setDropIndicatorShown:NO]; | 122 [view_ setController:mock_controller_]; |
211 EXPECT_EQ([view_ draggingEntered:(id)info.get()], NSDragOperationMove); | 123 [[[mock_controller_ expect] andReturnValue:OCMOCK_VALUE(expectedValue)] |
| 124 draggingEntered:drag_info]; |
| 125 EXPECT_EQ([view_ draggingEntered:drag_info], NSDragOperationMove); |
212 EXPECT_FALSE([view_ dropIndicatorShown]); | 126 EXPECT_FALSE([view_ dropIndicatorShown]); |
213 } | 127 } |
214 | 128 |
215 } // namespace | 129 } // namespace |
OLD | NEW |