Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / bookmarks / bookmark_bar_folder_view_unittest.mm
blobce802d580337cb3020d5c3f6df5d8a05d3b191df
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.
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.h"
9 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
10 #include "chrome/browser/bookmarks/bookmark_test_helpers.h"
11 #include "chrome/browser/profiles/profile.h"
12 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_folder_controller.h"
13 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_folder_view.h"
14 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h"
15 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_folder_target.h"
16 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
17 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
18 #import "chrome/browser/ui/cocoa/url_drop_target.h"
19 #include "chrome/test/base/testing_profile.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "testing/platform_test.h"
22 #import "third_party/mozilla/NSPasteboard+Utils.h"
23 #import "third_party/ocmock/OCMock/OCMock.h"
24 #import "third_party/ocmock/ocmock_extensions.h"
26 // Allows us to verify BookmarkBarFolderView.
27 @interface BookmarkBarFolderView(TestingAPI)
29 @property(readonly, nonatomic) BOOL dropIndicatorShown;
30 @property(readonly, nonatomic) CGFloat dropIndicatorPosition;
32 -(void)setController:(id<BookmarkButtonControllerProtocol>)controller;
34 @end
36 @implementation BookmarkBarFolderView(TestingAPI)
38 -(void)setController:(id<BookmarkButtonControllerProtocol>)controller {
39   controller_ = controller;
42 -(BOOL)dropIndicatorShown {
43   return dropIndicatorShown_;
46 -(CGFloat)dropIndicatorPosition {
47   return NSMinY([dropIndicator_ frame]);
50 @end
52 namespace {
54 // Some values used for mocks and fakes.
55 const CGFloat kFakeIndicatorPos = 7.0;
56 const NSPoint kPoint = {10, 10};
58 class BookmarkBarFolderViewTest : public CocoaProfileTest {
59  public:
60   virtual void SetUp() {
61     CocoaProfileTest::SetUp();
63     view_.reset([[BookmarkBarFolderView alloc] init]);
65     mock_controller_.reset(GetMockController(
66         YES, BookmarkModelFactory::GetForProfile(profile())));
68     mock_button_.reset(GetMockButton(mock_controller_.get()));
69     [view_ awakeFromNib];
70     [view_ setController:mock_controller_];
71   }
73   virtual void TearDown() {
74     [mock_controller_ verify];
75     CocoaProfileTest::TearDown();
76   }
78   id GetFakePasteboardForType(NSString* dataType) {
79     id pasteboard = [OCMockObject mockForClass:[NSPasteboard class]];
80     if ([dataType isEqualToString:kBookmarkButtonDragType]) {
81       BookmarkButton* button = mock_button_.get();
82       [[[pasteboard stub]
83         andReturn:[NSData dataWithBytes:&button length:sizeof(button)]]
84        dataForType:dataType];
85     } else {
86       [[[pasteboard stub] andReturn:[NSData data]] dataForType:dataType];
87     }
88     [[[pasteboard stub] andReturn:nil] dataForType:OCMOCK_ANY];
89     [[[pasteboard stub] andReturnBool:YES] containsURLData];
90     [[pasteboard stub] getURLs:[OCMArg setTo:nil]
91                      andTitles:[OCMArg setTo:nil]
92            convertingFilenames:YES];
93     return pasteboard;
94   }
96   id GetFakeDragInfoForType(NSString* dataType) {
97     // Need something non-nil to return as the draggingSource.
98     id source = [OCMockObject mockForClass:[NSObject class]];
99     id drag_info = [OCMockObject mockForProtocol:@protocol(NSDraggingInfo)];
100     id pasteboard = GetFakePasteboardForType(dataType);
101     [[[drag_info stub] andReturn:pasteboard] draggingPasteboard];
102     [[[drag_info stub] andReturnNSPoint:kPoint] draggingLocation];
103     [[[drag_info stub] andReturn:source] draggingSource];
104     [[[drag_info stub]
105       andReturnUnsignedInteger:NSDragOperationCopy | NSDragOperationMove]
106      draggingSourceOperationMask];
107     return drag_info;
108   }
110   id GetMockController(BOOL show_indicator, BookmarkModel* model) {
111     id mock_controller =
112         [OCMockObject mockForClass:[BookmarkBarFolderController class]];
113     [[[mock_controller stub] andReturnBool:YES]
114      draggingAllowed:OCMOCK_ANY];
115     [[[mock_controller stub] andReturnBool:show_indicator]
116      shouldShowIndicatorShownForPoint:kPoint];
117     [[[mock_controller stub] andReturnCGFloat:kFakeIndicatorPos]
118      indicatorPosForDragToPoint:kPoint];
119     [[[mock_controller stub] andReturnValue:OCMOCK_VALUE(model)] bookmarkModel];
120     return [mock_controller retain];
121   }
123   id GetMockButton(id mock_controller) {
124     BookmarkModel* bookmark_model =
125         BookmarkModelFactory::GetForProfile(profile());
126     const BookmarkNode* node =
127         bookmark_model->AddURL(bookmark_model->bookmark_bar_node(),
128                                0,
129                                base::ASCIIToUTF16("Test Bookmark"),
130                                GURL("http://www.exmaple.com"));
132     id mock_button = [OCMockObject mockForClass:[BookmarkButton class]];
133     [[[mock_button stub] andReturnValue:OCMOCK_VALUE(node)] bookmarkNode];
134     return [mock_button retain];
135   }
137   base::scoped_nsobject<id> mock_controller_;
138   base::scoped_nsobject<BookmarkBarFolderView> view_;
139   base::scoped_nsobject<id> mock_button_;
142 TEST_F(BookmarkBarFolderViewTest, BookmarkButtonDragAndDrop) {
143   id drag_info = GetFakeDragInfoForType(kBookmarkButtonDragType);
144   [[[mock_controller_ expect] andReturnUnsignedInteger:NSDragOperationNone]
145    draggingEntered:drag_info];
146   [[[mock_controller_ expect] andReturnBool:NO] dragBookmarkData:drag_info];
147   [[[mock_controller_ expect] andReturnBool:YES] dragButton:OCMOCK_ANY
148                                                          to:kPoint
149                                                        copy:NO];
151   EXPECT_EQ([view_ draggingEntered:drag_info], NSDragOperationMove);
152   EXPECT_TRUE([view_ performDragOperation:drag_info]);
155 // When dragging bookmarks across profiles, we should always copy, never move.
156 TEST_F(BookmarkBarFolderViewTest, BookmarkButtonDragAndDropAcrossProfiles) {
157   // |other_profile| is owned by the |testing_profile_manager|.
158   TestingProfile* other_profile =
159       testing_profile_manager()->CreateTestingProfile("other");
160   other_profile->CreateBookmarkModel(true);
161   test::WaitForBookmarkModelToLoad(other_profile);
163   mock_controller_.reset(GetMockController(
164       YES, BookmarkModelFactory::GetForProfile(other_profile)));
165   [view_ setController:mock_controller_];
167   id drag_info = GetFakeDragInfoForType(kBookmarkButtonDragType);
168   [[[mock_controller_ expect] andReturnUnsignedInteger:NSDragOperationNone]
169    draggingEntered:drag_info];
170   [[[mock_controller_ expect] andReturnBool:NO] dragBookmarkData:drag_info];
171   [[[mock_controller_ expect] andReturnBool:YES] dragButton:OCMOCK_ANY
172                                                          to:kPoint
173                                                        copy:YES];
175   EXPECT_EQ([view_ draggingEntered:drag_info], NSDragOperationMove);
176   EXPECT_TRUE([view_ performDragOperation:drag_info]);
179 TEST_F(BookmarkBarFolderViewTest, URLDragAndDrop) {
180   NSArray* dragTypes = [URLDropTargetHandler handledDragTypes];
181   for (NSString* type in dragTypes) {
182     id drag_info = GetFakeDragInfoForType(type);
183     [[[mock_controller_ expect] andReturnUnsignedInteger:NSDragOperationNone]
184      draggingEntered:drag_info];
185     [[[mock_controller_ expect] andReturnBool:NO] dragBookmarkData:drag_info];
186     [[[mock_controller_ expect] andReturnBool:YES] addURLs:OCMOCK_ANY
187                                                 withTitles:OCMOCK_ANY
188                                                         at:kPoint];
189     EXPECT_EQ([view_ draggingEntered:drag_info], NSDragOperationMove);
190     EXPECT_TRUE([view_ performDragOperation:drag_info]);
191     [mock_controller_ verify];
192   }
195 TEST_F(BookmarkBarFolderViewTest, BookmarkButtonDropIndicator) {
196   id drag_info = GetFakeDragInfoForType(kBookmarkButtonDragType);
197   [[[mock_controller_ expect] andReturnUnsignedInteger:NSDragOperationNone]
198    draggingEntered:drag_info];
199   EXPECT_EQ([view_ draggingEntered:drag_info], NSDragOperationMove);
200   [mock_controller_ verify];
201   EXPECT_TRUE([view_ dropIndicatorShown]);
202   EXPECT_EQ([view_ dropIndicatorPosition], kFakeIndicatorPos);
203   mock_controller_.reset(GetMockController(
204       NO, BookmarkModelFactory::GetForProfile(profile())));
205   [view_ setController:mock_controller_];
206   [[[mock_controller_ expect] andReturnUnsignedInteger:NSDragOperationNone]
207    draggingEntered:drag_info];
208   EXPECT_EQ([view_ draggingEntered:drag_info], NSDragOperationMove);
209   EXPECT_FALSE([view_ dropIndicatorShown]);
212 }  // namespace