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 #include "base/string16.h" |
| 7 #include "base/utf_string_conversions.h" |
6 #include "chrome/browser/bookmarks/bookmark_model.h" | 8 #include "chrome/browser/bookmarks/bookmark_model.h" |
7 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
8 #include "chrome/browser/ui/browser_window.h" | |
9 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h" | 10 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h" |
10 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_view.h" | 11 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_view.h" |
11 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h" | 12 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h" |
| 13 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button_cell.h" |
12 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_folder_target.h" | 14 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_folder_target.h" |
13 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" | 15 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" |
14 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" | 16 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
15 #import "chrome/browser/ui/cocoa/url_drop_target.h" | 17 #import "chrome/browser/ui/cocoa/url_drop_target.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "testing/platform_test.h" | 19 #include "testing/platform_test.h" |
18 #import "third_party/mozilla/NSPasteboard+Utils.h" | 20 #import "third_party/mozilla/NSPasteboard+Utils.h" |
19 #import "third_party/ocmock/OCMock/OCMock.h" | |
20 #import "third_party/ocmock/ocmock_extensions.h" | |
21 | 21 |
22 namespace { | 22 namespace { |
23 // Some values used for mocks and fakes. | 23 // Some values used for mocks and fakes. |
24 const CGFloat kFakeIndicatorPos = 7.0; | 24 const CGFloat kFakeIndicatorPos = 7.0; |
25 const NSPoint kPoint = {10, 10}; | 25 const NSPoint kPoint = {10, 10}; |
26 }; | 26 }; |
27 | 27 |
28 // Fake DraggingInfo, fake BookmarkBarController, fake NSPasteboard... | 28 // Fake DraggingInfo, fake BookmarkBarController, fake NSPasteboard... |
29 @interface FakeBookmarkDraggingInfo : NSObject { | 29 @interface FakeBookmarkDraggingInfo : NSObject { |
30 @public | 30 @public |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 | 190 |
191 @end | 191 @end |
192 | 192 |
193 namespace { | 193 namespace { |
194 | 194 |
195 class BookmarkBarViewTest : public CocoaProfileTest { | 195 class BookmarkBarViewTest : public CocoaProfileTest { |
196 public: | 196 public: |
197 virtual void SetUp() { | 197 virtual void SetUp() { |
198 CocoaProfileTest::SetUp(); | 198 CocoaProfileTest::SetUp(); |
199 view_.reset([[BookmarkBarView alloc] init]); | 199 view_.reset([[BookmarkBarView alloc] init]); |
200 window_ = CreateBrowserWindow()->GetNativeHandle(); | |
201 } | |
202 | |
203 id GetMockController() { | |
204 id mock_controller = | |
205 [OCMockObject mockForClass:[BookmarkBarController class]]; | |
206 [[[mock_controller stub] andReturnBool:YES] | |
207 draggingAllowed:OCMOCK_ANY]; | |
208 [[[mock_controller stub] andReturnBool:YES] | |
209 shouldShowIndicatorShownForPoint:kPoint]; | |
210 [[[mock_controller stub] andReturnFloat:kFakeIndicatorPos] | |
211 indicatorPosForDragToPoint:kPoint]; | |
212 [[[mock_controller stub] andReturn:window_] browserWindow]; | |
213 return mock_controller; | |
214 } | 200 } |
215 | 201 |
216 scoped_nsobject<BookmarkBarView> view_; | 202 scoped_nsobject<BookmarkBarView> view_; |
217 NSWindow* window_; // WEAK, owned by CocoaProfileTest | |
218 }; | 203 }; |
219 | 204 |
220 TEST_F(BookmarkBarViewTest, CanDragWindow) { | 205 TEST_F(BookmarkBarViewTest, CanDragWindow) { |
221 EXPECT_FALSE([view_ mouseDownCanMoveWindow]); | 206 EXPECT_FALSE([view_ mouseDownCanMoveWindow]); |
222 } | 207 } |
223 | 208 |
224 TEST_F(BookmarkBarViewTest, BookmarkButtonDragAndDrop) { | 209 TEST_F(BookmarkBarViewTest, BookmarkButtonDragAndDrop) { |
225 scoped_nsobject<FakeBookmarkDraggingInfo> | 210 scoped_nsobject<FakeBookmarkDraggingInfo> |
226 info([[FakeBookmarkDraggingInfo alloc] init]); | 211 info([[FakeBookmarkDraggingInfo alloc] init]); |
227 [view_ setController:info.get()]; | 212 [view_ setController:info.get()]; |
228 [info reset]; | 213 [info reset]; |
229 | 214 |
| 215 BookmarkModel* bookmark_model = profile()->GetBookmarkModel(); |
| 216 const BookmarkNode* node = |
| 217 bookmark_model->AddURL(bookmark_model->bookmark_bar_node(), |
| 218 0, |
| 219 ASCIIToUTF16("Test Bookmark"), |
| 220 GURL("http://www.exmaple.com")); |
| 221 |
| 222 scoped_nsobject<BookmarkButtonCell> button_cell( |
| 223 [[BookmarkButtonCell buttonCellForNode:node |
| 224 contextMenu:nil |
| 225 cellText:nil |
| 226 cellImage:nil] retain]); |
230 scoped_nsobject<BookmarkButton> dragged_button([[BookmarkButton alloc] init]); | 227 scoped_nsobject<BookmarkButton> dragged_button([[BookmarkButton alloc] init]); |
231 [dragged_button setDelegate:GetMockController()]; | 228 [dragged_button setCell:button_cell]; |
232 [info setDraggingSource:dragged_button.get()]; | 229 [info setDraggingSource:dragged_button.get()]; |
233 [info setDragDataType:kBookmarkButtonDragType]; | 230 [info setDragDataType:kBookmarkButtonDragType]; |
234 [info setButton:dragged_button.get()]; | 231 [info setButton:dragged_button.get()]; |
235 [info setBookmarkModel:profile()->GetBookmarkModel()]; | 232 [info setBookmarkModel:bookmark_model]; |
236 EXPECT_EQ([view_ draggingEntered:(id)info.get()], NSDragOperationMove); | 233 EXPECT_EQ([view_ draggingEntered:(id)info.get()], NSDragOperationMove); |
237 EXPECT_TRUE([view_ performDragOperation:(id)info.get()]); | 234 EXPECT_TRUE([view_ performDragOperation:(id)info.get()]); |
238 EXPECT_TRUE([info dragButtonToPong]); | 235 EXPECT_TRUE([info dragButtonToPong]); |
239 EXPECT_FALSE([info dragButtonToShouldCopy]); | 236 EXPECT_FALSE([info dragButtonToShouldCopy]); |
240 EXPECT_FALSE([info dragURLsPong]); | 237 EXPECT_FALSE([info dragURLsPong]); |
241 EXPECT_TRUE([info dragBookmarkDataPong]); | 238 EXPECT_TRUE([info dragBookmarkDataPong]); |
242 } | 239 } |
243 | 240 |
244 // When dragging bookmarks across profiles, we should always copy, never move. | 241 // When dragging bookmarks across profiles, we should always copy, never move. |
245 TEST_F(BookmarkBarViewTest, BookmarkButtonDragAndDropAcrossProfiles) { | 242 TEST_F(BookmarkBarViewTest, BookmarkButtonDragAndDropAcrossProfiles) { |
246 scoped_nsobject<FakeBookmarkDraggingInfo> | 243 scoped_nsobject<FakeBookmarkDraggingInfo> |
247 info([[FakeBookmarkDraggingInfo alloc] init]); | 244 info([[FakeBookmarkDraggingInfo alloc] init]); |
248 [view_ setController:info.get()]; | 245 [view_ setController:info.get()]; |
249 [info reset]; | 246 [info reset]; |
250 | 247 |
251 // |other_profile| is owned by the |testing_profile_manager|. | 248 // |other_profile| is owned by the |testing_profile_manager|. |
252 TestingProfile* other_profile = | 249 TestingProfile* other_profile = |
253 testing_profile_manager()->CreateTestingProfile("other"); | 250 testing_profile_manager()->CreateTestingProfile("other"); |
254 other_profile->CreateBookmarkModel(true); | 251 other_profile->CreateBookmarkModel(true); |
255 other_profile->BlockUntilBookmarkModelLoaded(); | 252 other_profile->BlockUntilBookmarkModelLoaded(); |
256 | 253 |
| 254 BookmarkModel* bookmark_model = profile()->GetBookmarkModel(); |
| 255 const BookmarkNode* node = |
| 256 bookmark_model->AddURL(bookmark_model->bookmark_bar_node(), |
| 257 0, |
| 258 ASCIIToUTF16("Test Bookmark"), |
| 259 GURL("http://www.exmaple.com")); |
| 260 |
| 261 scoped_nsobject<BookmarkButtonCell> button_cell( |
| 262 [[BookmarkButtonCell buttonCellForNode:node |
| 263 contextMenu:nil |
| 264 cellText:nil |
| 265 cellImage:nil] retain]); |
257 scoped_nsobject<BookmarkButton> dragged_button([[BookmarkButton alloc] init]); | 266 scoped_nsobject<BookmarkButton> dragged_button([[BookmarkButton alloc] init]); |
258 [dragged_button setDelegate:GetMockController()]; | 267 [dragged_button setCell:button_cell]; |
259 [info setDraggingSource:dragged_button.get()]; | 268 [info setDraggingSource:dragged_button.get()]; |
260 [info setDragDataType:kBookmarkButtonDragType]; | 269 [info setDragDataType:kBookmarkButtonDragType]; |
261 [info setButton:dragged_button.get()]; | 270 [info setButton:dragged_button.get()]; |
262 [info setBookmarkModel:other_profile->GetBookmarkModel()]; | 271 [info setBookmarkModel:other_profile->GetBookmarkModel()]; |
263 EXPECT_EQ([view_ draggingEntered:(id)info.get()], NSDragOperationMove); | 272 EXPECT_EQ([view_ draggingEntered:(id)info.get()], NSDragOperationMove); |
264 EXPECT_TRUE([view_ performDragOperation:(id)info.get()]); | 273 EXPECT_TRUE([view_ performDragOperation:(id)info.get()]); |
265 EXPECT_TRUE([info dragButtonToPong]); | 274 EXPECT_TRUE([info dragButtonToPong]); |
266 EXPECT_TRUE([info dragButtonToShouldCopy]); | 275 EXPECT_TRUE([info dragButtonToShouldCopy]); |
267 EXPECT_FALSE([info dragURLsPong]); | 276 EXPECT_FALSE([info dragURLsPong]); |
268 EXPECT_TRUE([info dragBookmarkDataPong]); | 277 EXPECT_TRUE([info dragBookmarkDataPong]); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 EXPECT_TRUE([info draggingEnteredCalled]); // Ensure controller pinged. | 309 EXPECT_TRUE([info draggingEnteredCalled]); // Ensure controller pinged. |
301 EXPECT_TRUE([view_ dropIndicatorShown]); | 310 EXPECT_TRUE([view_ dropIndicatorShown]); |
302 EXPECT_EQ([view_ dropIndicatorPosition], kFakeIndicatorPos); | 311 EXPECT_EQ([view_ dropIndicatorPosition], kFakeIndicatorPos); |
303 | 312 |
304 [info setDropIndicatorShown:NO]; | 313 [info setDropIndicatorShown:NO]; |
305 EXPECT_EQ([view_ draggingEntered:(id)info.get()], NSDragOperationMove); | 314 EXPECT_EQ([view_ draggingEntered:(id)info.get()], NSDragOperationMove); |
306 EXPECT_FALSE([view_ dropIndicatorShown]); | 315 EXPECT_FALSE([view_ dropIndicatorShown]); |
307 } | 316 } |
308 | 317 |
309 } // namespace | 318 } // namespace |
OLD | NEW |