BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / bookmarks / bookmark_bar_folder_view_unittest.mm
blobdc3a67f06f1dd5cce1339386505e78254c083895
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_factory.h"
9 #include "chrome/browser/profiles/profile.h"
10 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_folder_controller.h"
11 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_folder_view.h"
12 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h"
13 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_folder_target.h"
14 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
15 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
16 #import "chrome/browser/ui/cocoa/url_drop_target.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "components/bookmarks/browser/bookmark_model.h"
19 #include "components/bookmarks/test/bookmark_test_helpers.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 using bookmarks::BookmarkModel;
27 using bookmarks::BookmarkNode;
29 // Allows us to verify BookmarkBarFolderView.
30 @interface BookmarkBarFolderView(TestingAPI)
32 @property(readonly, nonatomic) BOOL dropIndicatorShown;
33 @property(readonly, nonatomic) CGFloat dropIndicatorPosition;
35 -(void)setController:(id<BookmarkButtonControllerProtocol>)controller;
37 @end
39 @implementation BookmarkBarFolderView(TestingAPI)
41 -(void)setController:(id<BookmarkButtonControllerProtocol>)controller {
42   controller_ = controller;
45 -(BOOL)dropIndicatorShown {
46   return dropIndicatorShown_;
49 -(CGFloat)dropIndicatorPosition {
50   return NSMinY([dropIndicator_ frame]);
53 @end
55 namespace {
57 // Some values used for mocks and fakes.
58 const CGFloat kFakeIndicatorPos = 7.0;
59 const NSPoint kPoint = {10, 10};
61 class BookmarkBarFolderViewTest : public CocoaProfileTest {
62  public:
63   void SetUp() override {
64     CocoaProfileTest::SetUp();
66     view_.reset([[BookmarkBarFolderView alloc] init]);
68     mock_controller_.reset(GetMockController(
69         YES, BookmarkModelFactory::GetForProfile(profile())));
71     mock_button_.reset(GetMockButton(mock_controller_.get()));
72     [view_ awakeFromNib];
73     [view_ setController:mock_controller_];
74   }
76   void TearDown() override {
77     [mock_controller_ verify];
78     CocoaProfileTest::TearDown();
79   }
81   id GetFakePasteboardForType(NSString* dataType) {
82     id pasteboard = [OCMockObject mockForClass:[NSPasteboard class]];
83     if ([dataType isEqualToString:kBookmarkButtonDragType]) {
84       BookmarkButton* button = mock_button_.get();
85       [[[pasteboard stub]
86         andReturn:[NSData dataWithBytes:&button length:sizeof(button)]]
87        dataForType:dataType];
88     } else {
89       [[[pasteboard stub] andReturn:[NSData data]] dataForType:dataType];
90     }
91     [[[pasteboard stub] andReturn:nil] dataForType:OCMOCK_ANY];
92     [[[pasteboard stub] andReturnBool:YES] containsURLData];
93     [[pasteboard stub] getURLs:[OCMArg setTo:nil]
94                      andTitles:[OCMArg setTo:nil]
95            convertingFilenames:YES];
96     return pasteboard;
97   }
99   id GetFakeDragInfoForType(NSString* dataType) {
100     // Need something non-nil to return as the draggingSource.
101     id source = [OCMockObject mockForClass:[NSObject class]];
102     id drag_info = [OCMockObject mockForProtocol:@protocol(NSDraggingInfo)];
103     id pasteboard = GetFakePasteboardForType(dataType);
104     [[[drag_info stub] andReturn:pasteboard] draggingPasteboard];
105     [[[drag_info stub] andReturnNSPoint:kPoint] draggingLocation];
106     [[[drag_info stub] andReturn:source] draggingSource];
107     [[[drag_info stub]
108       andReturnUnsignedInteger:NSDragOperationCopy | NSDragOperationMove]
109      draggingSourceOperationMask];
110     return drag_info;
111   }
113   id GetMockController(BOOL show_indicator, BookmarkModel* model) {
114     id mock_controller =
115         [OCMockObject mockForClass:[BookmarkBarFolderController class]];
116     [[[mock_controller stub] andReturnBool:YES]
117      draggingAllowed:OCMOCK_ANY];
118     [[[mock_controller stub] andReturnBool:show_indicator]
119      shouldShowIndicatorShownForPoint:kPoint];
120     [[[mock_controller stub] andReturnCGFloat:kFakeIndicatorPos]
121      indicatorPosForDragToPoint:kPoint];
122     [[[mock_controller stub] andReturnValue:OCMOCK_VALUE(model)] bookmarkModel];
123     return [mock_controller retain];
124   }
126   id GetMockButton(id mock_controller) {
127     BookmarkModel* bookmark_model =
128         BookmarkModelFactory::GetForProfile(profile());
129     const BookmarkNode* node =
130         bookmark_model->AddURL(bookmark_model->bookmark_bar_node(),
131                                0,
132                                base::ASCIIToUTF16("Test Bookmark"),
133                                GURL("http://www.exmaple.com"));
135     id mock_button = [OCMockObject mockForClass:[BookmarkButton class]];
136     [[[mock_button stub] andReturnValue:OCMOCK_VALUE(node)] bookmarkNode];
137     return [mock_button retain];
138   }
140   base::scoped_nsobject<id> mock_controller_;
141   base::scoped_nsobject<BookmarkBarFolderView> view_;
142   base::scoped_nsobject<id> mock_button_;
145 TEST_F(BookmarkBarFolderViewTest, BookmarkButtonDragAndDrop) {
146   id drag_info = GetFakeDragInfoForType(kBookmarkButtonDragType);
147   [[[mock_controller_ expect] andReturnUnsignedInteger:NSDragOperationNone]
148    draggingEntered:drag_info];
149   [[[mock_controller_ expect] andReturnBool:NO] dragBookmarkData:drag_info];
150   [[[mock_controller_ expect] andReturnBool:YES] dragButton:OCMOCK_ANY
151                                                          to:kPoint
152                                                        copy:NO];
154   EXPECT_EQ([view_ draggingEntered:drag_info], NSDragOperationMove);
155   EXPECT_TRUE([view_ performDragOperation:drag_info]);
158 // When dragging bookmarks across profiles, we should always copy, never move.
159 TEST_F(BookmarkBarFolderViewTest, BookmarkButtonDragAndDropAcrossProfiles) {
160   // |other_profile| is owned by the |testing_profile_manager|.
161   TestingProfile* other_profile =
162       testing_profile_manager()->CreateTestingProfile("other");
163   other_profile->CreateBookmarkModel(true);
164   bookmarks::test::WaitForBookmarkModelToLoad(
165       BookmarkModelFactory::GetForProfile(other_profile));
167   mock_controller_.reset(GetMockController(
168       YES, BookmarkModelFactory::GetForProfile(other_profile)));
169   [view_ setController:mock_controller_];
171   id drag_info = GetFakeDragInfoForType(kBookmarkButtonDragType);
172   [[[mock_controller_ expect] andReturnUnsignedInteger:NSDragOperationNone]
173    draggingEntered:drag_info];
174   [[[mock_controller_ expect] andReturnBool:NO] dragBookmarkData:drag_info];
175   [[[mock_controller_ expect] andReturnBool:YES] dragButton:OCMOCK_ANY
176                                                          to:kPoint
177                                                        copy:YES];
179   EXPECT_EQ([view_ draggingEntered:drag_info], NSDragOperationMove);
180   EXPECT_TRUE([view_ performDragOperation:drag_info]);
183 TEST_F(BookmarkBarFolderViewTest, URLDragAndDrop) {
184   NSArray* dragTypes = [URLDropTargetHandler handledDragTypes];
185   for (NSString* type in dragTypes) {
186     id drag_info = GetFakeDragInfoForType(type);
187     [[[mock_controller_ expect] andReturnUnsignedInteger:NSDragOperationNone]
188      draggingEntered:drag_info];
189     [[[mock_controller_ expect] andReturnBool:NO] dragBookmarkData:drag_info];
190     [[[mock_controller_ expect] andReturnBool:YES] addURLs:OCMOCK_ANY
191                                                 withTitles:OCMOCK_ANY
192                                                         at:kPoint];
193     EXPECT_EQ([view_ draggingEntered:drag_info], NSDragOperationMove);
194     EXPECT_TRUE([view_ performDragOperation:drag_info]);
195     [mock_controller_ verify];
196   }
199 TEST_F(BookmarkBarFolderViewTest, BookmarkButtonDropIndicator) {
200   id drag_info = GetFakeDragInfoForType(kBookmarkButtonDragType);
201   [[[mock_controller_ expect] andReturnUnsignedInteger:NSDragOperationNone]
202    draggingEntered:drag_info];
203   EXPECT_EQ([view_ draggingEntered:drag_info], NSDragOperationMove);
204   [mock_controller_ verify];
205   EXPECT_TRUE([view_ dropIndicatorShown]);
206   EXPECT_EQ([view_ dropIndicatorPosition], kFakeIndicatorPos);
207   mock_controller_.reset(GetMockController(
208       NO, BookmarkModelFactory::GetForProfile(profile())));
209   [view_ setController:mock_controller_];
210   [[[mock_controller_ expect] andReturnUnsignedInteger:NSDragOperationNone]
211    draggingEntered:drag_info];
212   EXPECT_EQ([view_ draggingEntered:drag_info], NSDragOperationMove);
213   EXPECT_FALSE([view_ dropIndicatorShown]);
216 }  // namespace