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/utf_string_conversions.h"
7 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
8 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h"
9 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button_cell.h"
10 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "components/bookmarks/browser/bookmark_model.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "testing/platform_test.h"
15 #import "ui/events/test/cocoa_test_event_utils.h"
17 // Fake BookmarkButton delegate to get a pong on mouse entered/exited
18 @interface FakeButtonDelegate : NSObject<BookmarkButtonDelegate> {
23 int didDragToTrashCount_;
27 @implementation FakeButtonDelegate
29 - (void)fillPasteboard:(NSPasteboard*)pboard
30 forDragOfButton:(BookmarkButton*)button {
33 - (void)mouseEnteredButton:(id)buton event:(NSEvent*)event {
37 - (void)mouseExitedButton:(id)buton event:(NSEvent*)event {
41 - (BOOL)dragShouldLockBarVisibility {
45 - (NSWindow*)browserWindow {
49 - (BOOL)canDragBookmarkButtonToTrash:(BookmarkButton*)button {
50 return canDragToTrash_;
53 - (void)didDragBookmarkToTrash:(BookmarkButton*)button {
54 didDragToTrashCount_++;
57 - (void)bookmarkDragDidEnd:(BookmarkButton*)button
58 operation:(NSDragOperation)operation {
64 class BookmarkButtonTest : public CocoaProfileTest {
67 // Make sure nothing leaks
68 TEST_F(BookmarkButtonTest, Create) {
69 base::scoped_nsobject<BookmarkButton> button;
70 button.reset([[BookmarkButton alloc] initWithFrame:NSMakeRect(0,0,500,500)]);
73 // Test folder and empty node queries.
74 TEST_F(BookmarkButtonTest, FolderAndEmptyOrNot) {
75 base::scoped_nsobject<BookmarkButton> button;
76 base::scoped_nsobject<BookmarkButtonCell> cell;
78 button.reset([[BookmarkButton alloc] initWithFrame:NSMakeRect(0,0,500,500)]);
79 cell.reset([[BookmarkButtonCell alloc] initTextCell:@"hi mom"]);
80 [button setCell:cell];
82 EXPECT_TRUE([button isEmpty]);
83 EXPECT_FALSE([button isFolder]);
84 EXPECT_FALSE([button bookmarkNode]);
87 cocoa_test_event_utils::LeftMouseDownAtPoint(NSMakePoint(10,10));
88 // Since this returns (does not actually begin a modal drag), success!
89 [button beginDrag:downEvent];
91 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
92 const BookmarkNode* node = model->bookmark_bar_node();
93 [cell setBookmarkNode:node];
94 EXPECT_FALSE([button isEmpty]);
95 EXPECT_TRUE([button isFolder]);
96 EXPECT_EQ([button bookmarkNode], node);
98 node = model->AddURL(node, 0, base::ASCIIToUTF16("hi mom"),
99 GURL("http://www.google.com"));
100 [cell setBookmarkNode:node];
101 EXPECT_FALSE([button isEmpty]);
102 EXPECT_FALSE([button isFolder]);
103 EXPECT_EQ([button bookmarkNode], node);
106 TEST_F(BookmarkButtonTest, MouseEnterExitRedirect) {
108 cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(10,10),
111 base::scoped_nsobject<BookmarkButton> button;
112 base::scoped_nsobject<BookmarkButtonCell> cell;
113 base::scoped_nsobject<FakeButtonDelegate> delegate(
114 [[FakeButtonDelegate alloc] init]);
115 button.reset([[BookmarkButton alloc] initWithFrame:NSMakeRect(0,0,500,500)]);
116 cell.reset([[BookmarkButtonCell alloc] initTextCell:@"hi mom"]);
117 [button setCell:cell];
118 [button setDelegate:delegate];
120 EXPECT_EQ(0, delegate.get()->entered_);
121 EXPECT_EQ(0, delegate.get()->exited_);
123 [button mouseEntered:moveEvent];
124 EXPECT_EQ(1, delegate.get()->entered_);
125 EXPECT_EQ(0, delegate.get()->exited_);
127 [button mouseExited:moveEvent];
128 [button mouseExited:moveEvent];
129 EXPECT_EQ(1, delegate.get()->entered_);
130 EXPECT_EQ(2, delegate.get()->exited_);
133 TEST_F(BookmarkButtonTest, DragToTrash) {
134 base::scoped_nsobject<BookmarkButton> button;
135 base::scoped_nsobject<BookmarkButtonCell> cell;
136 base::scoped_nsobject<FakeButtonDelegate> delegate(
137 [[FakeButtonDelegate alloc] init]);
138 button.reset([[BookmarkButton alloc] initWithFrame:NSMakeRect(0,0,500,500)]);
139 cell.reset([[BookmarkButtonCell alloc] initTextCell:@"hi mom"]);
140 [button setCell:cell];
141 [button setDelegate:delegate];
143 // Add a deletable bookmark to the button.
144 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
145 const BookmarkNode* barNode = model->bookmark_bar_node();
146 const BookmarkNode* node = model->AddURL(barNode, 0,
147 base::ASCIIToUTF16("hi mom"),
148 GURL("http://www.google.com"));
149 [cell setBookmarkNode:node];
151 // Verify that if canDragBookmarkButtonToTrash is NO then the button can't
152 // be dragged to the trash.
153 delegate.get()->canDragToTrash_ = NO;
154 NSDragOperation operation = [button draggingSourceOperationMaskForLocal:NO];
155 EXPECT_EQ(0u, operation & NSDragOperationDelete);
156 operation = [button draggingSourceOperationMaskForLocal:YES];
157 EXPECT_EQ(0u, operation & NSDragOperationDelete);
159 // Verify that if canDragBookmarkButtonToTrash is YES then the button can
160 // be dragged to the trash.
161 delegate.get()->canDragToTrash_ = YES;
162 operation = [button draggingSourceOperationMaskForLocal:NO];
163 EXPECT_EQ(NSDragOperationDelete, operation & NSDragOperationDelete);
164 operation = [button draggingSourceOperationMaskForLocal:YES];
165 EXPECT_EQ(NSDragOperationDelete, operation & NSDragOperationDelete);
167 // Verify that canDragBookmarkButtonToTrash is called when expected.
168 delegate.get()->canDragToTrash_ = YES;
169 EXPECT_EQ(0, delegate.get()->didDragToTrashCount_);
170 [button draggedImage:nil endedAt:NSZeroPoint operation:NSDragOperationCopy];
171 EXPECT_EQ(0, delegate.get()->didDragToTrashCount_);
172 [button draggedImage:nil endedAt:NSZeroPoint operation:NSDragOperationMove];
173 EXPECT_EQ(0, delegate.get()->didDragToTrashCount_);
174 [button draggedImage:nil endedAt:NSZeroPoint operation:NSDragOperationDelete];
175 EXPECT_EQ(1, delegate.get()->didDragToTrashCount_);