Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / bookmarks / bookmark_bar_view_unittest.mm
1 // Copyright (c) 2012 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 #include "base/mac/scoped_nsobject.h"
6 #include "base/strings/string16.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
9 #include "chrome/browser/profiles/profile.h"
10 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h"
11 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_view.h"
12 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h"
13 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button_cell.h"
14 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_folder_target.h"
15 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
16 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
17 #import "chrome/browser/ui/cocoa/url_drop_target.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "components/bookmarks/core/browser/bookmark_model.h"
20 #include "components/bookmarks/core/test/bookmark_test_helpers.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "testing/platform_test.h"
23 #import "third_party/mozilla/NSPasteboard+Utils.h"
24
25 namespace {
26 // Some values used for mocks and fakes.
27 const CGFloat kFakeIndicatorPos = 7.0;
28 const NSPoint kPoint = {10, 10};
29 };
30
31 // Fake DraggingInfo, fake BookmarkBarController, fake NSPasteboard...
32 @interface FakeBookmarkDraggingInfo : NSObject {
33  @public
34   BOOL dragButtonToPong_;
35   BOOL dragButtonToShouldCopy_;
36   BOOL dragURLsPong_;
37   BOOL dragBookmarkDataPong_;
38   BOOL dropIndicatorShown_;
39   BOOL draggingEnteredCalled_;
40   // Only mock one type of drag data at a time.
41   NSString* dragDataType_;
42   BookmarkButton* button_;  // weak
43   BookmarkModel* bookmarkModel_;  // weak
44   id draggingSource_;
45 }
46 @property (nonatomic) BOOL dropIndicatorShown;
47 @property (nonatomic) BOOL draggingEnteredCalled;
48 @property (nonatomic, copy) NSString* dragDataType;
49 @property (nonatomic, assign) BookmarkButton* button;
50 @property (nonatomic, assign) BookmarkModel* bookmarkModel;
51 @end
52
53 @implementation FakeBookmarkDraggingInfo
54
55 @synthesize dropIndicatorShown = dropIndicatorShown_;
56 @synthesize draggingEnteredCalled = draggingEnteredCalled_;
57 @synthesize dragDataType = dragDataType_;
58 @synthesize button = button_;
59 @synthesize bookmarkModel = bookmarkModel_;
60
61 - (id)init {
62   if ((self = [super init])) {
63     dropIndicatorShown_ = YES;
64   }
65   return self;
66 }
67
68 - (void)dealloc {
69   [dragDataType_ release];
70   [super dealloc];
71 }
72
73 - (void)reset {
74   [dragDataType_ release];
75   dragDataType_ = nil;
76   dragButtonToPong_ = NO;
77   dragURLsPong_ = NO;
78   dragBookmarkDataPong_ = NO;
79   dropIndicatorShown_ = YES;
80   draggingEnteredCalled_ = NO;
81   draggingSource_ = self;
82 }
83
84 - (void)setDraggingSource:(id)draggingSource {
85   draggingSource_ = draggingSource;
86 }
87
88 // NSDragInfo mocking functions.
89
90 - (id)draggingPasteboard {
91   return self;
92 }
93
94 // So we can look local.
95 - (id)draggingSource {
96   return draggingSource_;
97 }
98
99 - (NSDragOperation)draggingSourceOperationMask {
100   return NSDragOperationCopy | NSDragOperationMove;
101 }
102
103 - (NSPoint)draggingLocation {
104   return kPoint;
105 }
106
107 // NSPasteboard mocking functions.
108
109 - (BOOL)containsURLData {
110   NSArray* urlTypes = [URLDropTargetHandler handledDragTypes];
111   if (dragDataType_)
112     return [urlTypes containsObject:dragDataType_];
113   return NO;
114 }
115
116 - (NSData*)dataForType:(NSString*)type {
117   if (dragDataType_ && [dragDataType_ isEqualToString:type]) {
118     if (button_)
119       return [NSData dataWithBytes:&button_ length:sizeof(button_)];
120     else
121       return [NSData data];  // Return something, anything.
122   }
123   return nil;
124 }
125
126 // Fake a controller for callback ponging
127
128 - (BOOL)dragButton:(BookmarkButton*)button to:(NSPoint)point copy:(BOOL)copy {
129   dragButtonToPong_ = YES;
130   dragButtonToShouldCopy_ = copy;
131   return YES;
132 }
133
134 - (BOOL)addURLs:(NSArray*)urls withTitles:(NSArray*)titles at:(NSPoint)point {
135   dragURLsPong_ = YES;
136   return YES;
137 }
138
139 - (void)getURLs:(NSArray**)outUrls
140     andTitles:(NSArray**)outTitles
141     convertingFilenames:(BOOL)convertFilenames {
142 }
143
144 - (BOOL)dragBookmarkData:(id<NSDraggingInfo>)info {
145   dragBookmarkDataPong_ = YES;
146   return NO;
147 }
148
149 - (BOOL)canEditBookmarks {
150   return YES;
151 }
152
153 // Confirm the pongs.
154
155 - (BOOL)dragButtonToPong {
156   return dragButtonToPong_;
157 }
158
159 - (BOOL)dragButtonToShouldCopy {
160   return dragButtonToShouldCopy_;
161 }
162
163 - (BOOL)dragURLsPong {
164   return dragURLsPong_;
165 }
166
167 - (BOOL)dragBookmarkDataPong {
168   return dragBookmarkDataPong_;
169 }
170
171 - (CGFloat)indicatorPosForDragToPoint:(NSPoint)point {
172   return kFakeIndicatorPos;
173 }
174
175 - (BOOL)shouldShowIndicatorShownForPoint:(NSPoint)point {
176   return dropIndicatorShown_;
177 }
178
179 - (BOOL)draggingAllowed:(id<NSDraggingInfo>)info {
180   return YES;
181 }
182
183 - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)info {
184   draggingEnteredCalled_ = YES;
185   return NSDragOperationNone;
186 }
187
188 - (void)setDropInsertionPos:(CGFloat)where {
189 }
190
191 - (void)clearDropInsertionPos {
192 }
193
194 @end
195
196 namespace {
197
198 class BookmarkBarViewTest : public CocoaProfileTest {
199  public:
200   virtual void SetUp() {
201     CocoaProfileTest::SetUp();
202     view_.reset([[BookmarkBarView alloc] init]);
203   }
204
205   base::scoped_nsobject<BookmarkBarView> view_;
206 };
207
208 TEST_F(BookmarkBarViewTest, CanDragWindow) {
209   EXPECT_FALSE([view_ mouseDownCanMoveWindow]);
210 }
211
212 TEST_F(BookmarkBarViewTest, BookmarkButtonDragAndDrop) {
213   base::scoped_nsobject<FakeBookmarkDraggingInfo> info(
214       [[FakeBookmarkDraggingInfo alloc] init]);
215   [view_ setController:info.get()];
216   [info reset];
217
218   BookmarkModel* bookmark_model =
219       BookmarkModelFactory::GetForProfile(profile());
220   const BookmarkNode* node =
221       bookmark_model->AddURL(bookmark_model->bookmark_bar_node(),
222                              0,
223                              base::ASCIIToUTF16("Test Bookmark"),
224                              GURL("http://www.exmaple.com"));
225
226   base::scoped_nsobject<BookmarkButtonCell> button_cell(
227       [[BookmarkButtonCell buttonCellForNode:node
228                                         text:nil
229                                        image:nil
230                               menuController:nil] retain]);
231   base::scoped_nsobject<BookmarkButton> dragged_button(
232       [[BookmarkButton alloc] init]);
233   [dragged_button setCell:button_cell];
234   [info setDraggingSource:dragged_button.get()];
235   [info setDragDataType:kBookmarkButtonDragType];
236   [info setButton:dragged_button.get()];
237   [info setBookmarkModel:bookmark_model];
238   EXPECT_EQ([view_ draggingEntered:(id)info.get()], NSDragOperationMove);
239   EXPECT_TRUE([view_ performDragOperation:(id)info.get()]);
240   EXPECT_TRUE([info dragButtonToPong]);
241   EXPECT_FALSE([info dragButtonToShouldCopy]);
242   EXPECT_FALSE([info dragURLsPong]);
243   EXPECT_TRUE([info dragBookmarkDataPong]);
244 }
245
246 // When dragging bookmarks across profiles, we should always copy, never move.
247 TEST_F(BookmarkBarViewTest, BookmarkButtonDragAndDropAcrossProfiles) {
248   base::scoped_nsobject<FakeBookmarkDraggingInfo> info(
249       [[FakeBookmarkDraggingInfo alloc] init]);
250   [view_ setController:info.get()];
251   [info reset];
252
253   // |other_profile| is owned by the |testing_profile_manager|.
254   TestingProfile* other_profile =
255       testing_profile_manager()->CreateTestingProfile("other");
256   other_profile->CreateBookmarkModel(true);
257
258   BookmarkModel* bookmark_model =
259       BookmarkModelFactory::GetForProfile(profile());
260   test::WaitForBookmarkModelToLoad(bookmark_model);
261
262   const BookmarkNode* node =
263       bookmark_model->AddURL(bookmark_model->bookmark_bar_node(),
264                              0,
265                              base::ASCIIToUTF16("Test Bookmark"),
266                              GURL("http://www.exmaple.com"));
267
268   base::scoped_nsobject<BookmarkButtonCell> button_cell(
269       [[BookmarkButtonCell buttonCellForNode:node
270                                         text:nil
271                                        image:nil
272                               menuController:nil] retain]);
273   base::scoped_nsobject<BookmarkButton> dragged_button(
274       [[BookmarkButton alloc] init]);
275   [dragged_button setCell:button_cell];
276   [info setDraggingSource:dragged_button.get()];
277   [info setDragDataType:kBookmarkButtonDragType];
278   [info setButton:dragged_button.get()];
279   [info setBookmarkModel:BookmarkModelFactory::GetForProfile(other_profile)];
280   EXPECT_EQ([view_ draggingEntered:(id)info.get()], NSDragOperationMove);
281   EXPECT_TRUE([view_ performDragOperation:(id)info.get()]);
282   EXPECT_TRUE([info dragButtonToPong]);
283   EXPECT_TRUE([info dragButtonToShouldCopy]);
284   EXPECT_FALSE([info dragURLsPong]);
285   EXPECT_TRUE([info dragBookmarkDataPong]);
286 }
287
288 TEST_F(BookmarkBarViewTest, URLDragAndDrop) {
289   base::scoped_nsobject<FakeBookmarkDraggingInfo> info(
290       [[FakeBookmarkDraggingInfo alloc] init]);
291   [view_ setController:info.get()];
292   [info reset];
293
294   NSArray* dragTypes = [URLDropTargetHandler handledDragTypes];
295   for (NSString* type in dragTypes) {
296     [info setDragDataType:type];
297     EXPECT_EQ([view_ draggingEntered:(id)info.get()], NSDragOperationCopy);
298     EXPECT_TRUE([view_ performDragOperation:(id)info.get()]);
299     EXPECT_FALSE([info dragButtonToPong]);
300     EXPECT_TRUE([info dragURLsPong]);
301     EXPECT_TRUE([info dragBookmarkDataPong]);
302     [info reset];
303   }
304 }
305
306 TEST_F(BookmarkBarViewTest, BookmarkButtonDropIndicator) {
307   base::scoped_nsobject<FakeBookmarkDraggingInfo> info(
308       [[FakeBookmarkDraggingInfo alloc] init]);
309   [view_ setController:info.get()];
310   [info reset];
311
312   base::scoped_nsobject<BookmarkButton> dragged_button(
313       [[BookmarkButton alloc] init]);
314   [info setDraggingSource:dragged_button.get()];
315   [info setDragDataType:kBookmarkButtonDragType];
316   EXPECT_FALSE([info draggingEnteredCalled]);
317   EXPECT_EQ([view_ draggingEntered:(id)info.get()], NSDragOperationMove);
318   EXPECT_TRUE([info draggingEnteredCalled]);  // Ensure controller pinged.
319   EXPECT_TRUE([view_ dropIndicatorShown]);
320   EXPECT_EQ([view_ dropIndicatorPosition], kFakeIndicatorPos);
321
322   [info setDropIndicatorShown:NO];
323   EXPECT_EQ([view_ draggingEntered:(id)info.get()], NSDragOperationMove);
324   EXPECT_FALSE([view_ dropIndicatorShown]);
325 }
326
327 }  // namespace