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_cell.h"
9 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
10 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "components/bookmarks/browser/bookmark_model.h"
13 #include "grit/ui_resources.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "testing/platform_test.h"
16 #include "ui/base/resource/resource_bundle.h"
17 #include "ui/gfx/image/image.h"
19 // Simple class to remember how many mouseEntered: and mouseExited:
20 // calls it gets. Only used by BookmarkMouseForwarding but placed
21 // at the top of the file to keep it outside the anon namespace.
22 @interface ButtonRemembersMouseEnterExit : NSButton {
29 @implementation ButtonRemembersMouseEnterExit
30 - (void)mouseEntered:(NSEvent*)event {
33 - (void)mouseExited:(NSEvent*)event {
41 class BookmarkButtonCellTest : public CocoaProfileTest {
44 // Make sure it's not totally bogus
45 TEST_F(BookmarkButtonCellTest, SizeForBounds) {
46 NSRect frame = NSMakeRect(0, 0, 50, 30);
47 base::scoped_nsobject<NSButton> view([[NSButton alloc] initWithFrame:frame]);
48 base::scoped_nsobject<BookmarkButtonCell> cell(
49 [[BookmarkButtonCell alloc] initTextCell:@"Testing"]);
50 [view setCell:cell.get()];
51 [[test_window() contentView] addSubview:view];
53 NSRect r = NSMakeRect(0, 0, 100, 100);
54 NSSize size = [cell.get() cellSizeForBounds:r];
55 EXPECT_TRUE(size.width > 0 && size.height > 0);
56 EXPECT_TRUE(size.width < 200 && size.height < 200);
59 // Make sure icon-only buttons are squeezed tightly.
60 TEST_F(BookmarkButtonCellTest, IconOnlySqueeze) {
61 NSRect frame = NSMakeRect(0, 0, 50, 30);
62 base::scoped_nsobject<NSButton> view([[NSButton alloc] initWithFrame:frame]);
63 base::scoped_nsobject<BookmarkButtonCell> cell(
64 [[BookmarkButtonCell alloc] initTextCell:@"Testing"]);
65 [view setCell:cell.get()];
66 [[test_window() contentView] addSubview:view];
68 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
69 base::scoped_nsobject<NSImage> image(
70 rb.GetNativeImageNamed(IDR_DEFAULT_FAVICON).CopyNSImage());
71 EXPECT_TRUE(image.get());
73 NSRect r = NSMakeRect(0, 0, 100, 100);
74 [cell setBookmarkCellText:@" " image:image];
75 CGFloat two_space_width = [cell.get() cellSizeForBounds:r].width;
76 [cell setBookmarkCellText:@" " image:image];
77 CGFloat one_space_width = [cell.get() cellSizeForBounds:r].width;
78 [cell setBookmarkCellText:@"" image:image];
79 CGFloat zero_space_width = [cell.get() cellSizeForBounds:r].width;
81 // Make sure the switch to "no title" is more significant than we
82 // would otherwise see by decreasing the length of the title.
83 CGFloat delta1 = two_space_width - one_space_width;
84 CGFloat delta2 = one_space_width - zero_space_width;
85 EXPECT_GT(delta2, delta1);
89 // Make sure the default from the base class is overridden.
90 TEST_F(BookmarkButtonCellTest, MouseEnterStuff) {
91 base::scoped_nsobject<BookmarkButtonCell> cell(
92 [[BookmarkButtonCell alloc] initTextCell:@"Testing"]);
93 // Setting the menu should have no affect since we either share or
94 // dynamically compose the menu given a node.
95 [cell setMenu:[[[NSMenu alloc] initWithTitle:@"foo"] autorelease]];
96 EXPECT_FALSE([cell menu]);
98 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
99 const BookmarkNode* node = model->bookmark_bar_node();
101 [cell setBookmarkNode:node];
102 EXPECT_TRUE([cell showsBorderOnlyWhileMouseInside]);
105 EXPECT_FALSE([cell.get() showsBorderOnlyWhileMouseInside]);
108 TEST_F(BookmarkButtonCellTest, BookmarkNode) {
109 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
110 base::scoped_nsobject<BookmarkButtonCell> cell(
111 [[BookmarkButtonCell alloc] initTextCell:@"Testing"]);
113 const BookmarkNode* node = model->bookmark_bar_node();
114 [cell setBookmarkNode:node];
115 EXPECT_EQ(node, [cell bookmarkNode]);
117 node = model->other_node();
118 [cell setBookmarkNode:node];
119 EXPECT_EQ(node, [cell bookmarkNode]);
122 TEST_F(BookmarkButtonCellTest, BookmarkMouseForwarding) {
123 base::scoped_nsobject<BookmarkButtonCell> cell(
124 [[BookmarkButtonCell alloc] initTextCell:@"Testing"]);
125 base::scoped_nsobject<ButtonRemembersMouseEnterExit> button(
126 [[ButtonRemembersMouseEnterExit alloc]
127 initWithFrame:NSMakeRect(0, 0, 50, 50)]);
128 [button setCell:cell.get()];
129 EXPECT_EQ(0, button.get()->enters_);
130 EXPECT_EQ(0, button.get()->exits_);
131 NSEvent* event = [NSEvent mouseEventWithType:NSMouseMoved
132 location:NSMakePoint(10,10)
140 [cell mouseEntered:event];
141 EXPECT_TRUE(button.get()->enters_ && !button.get()->exits_);
143 for (int i = 0; i < 3; i++)
144 [cell mouseExited:event];
145 EXPECT_EQ(button.get()->enters_, 1);
146 EXPECT_EQ(button.get()->exits_, 3);
149 // Confirms a cell created in a nib is initialized properly
150 TEST_F(BookmarkButtonCellTest, Awake) {
151 base::scoped_nsobject<BookmarkButtonCell> cell(
152 [[BookmarkButtonCell alloc] init]);
154 EXPECT_EQ(NSLeftTextAlignment, [cell alignment]);
157 // Subfolder arrow details.
158 TEST_F(BookmarkButtonCellTest, FolderArrow) {
159 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
160 const BookmarkNode* bar = model->bookmark_bar_node();
161 const BookmarkNode* node = model->AddURL(bar, bar->child_count(),
162 base::ASCIIToUTF16("title"),
163 GURL("http://www.google.com"));
164 base::scoped_nsobject<BookmarkButtonCell> cell(
165 [[BookmarkButtonCell alloc] initForNode:node
168 menuController:nil]);
169 EXPECT_TRUE(cell.get());
171 NSSize size = [cell cellSize];
173 EXPECT_GE(size.width, 2);
174 EXPECT_GE(size.height, 2);
176 // Once we turn on arrow drawing make sure there is now room for it.
177 [cell setDrawFolderArrow:YES];
178 NSSize arrowSize = [cell cellSize];
179 EXPECT_GT(arrowSize.width, size.width);
182 TEST_F(BookmarkButtonCellTest, VerticalTextOffset) {
183 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
184 const BookmarkNode* bar = model->bookmark_bar_node();
185 const BookmarkNode* node = model->AddURL(bar, bar->child_count(),
186 base::ASCIIToUTF16("title"),
187 GURL("http://www.google.com"));
189 base::scoped_nsobject<GradientButtonCell> gradient_cell(
190 [[GradientButtonCell alloc] initTextCell:@"Testing"]);
191 base::scoped_nsobject<BookmarkButtonCell> bookmark_cell(
192 [[BookmarkButtonCell alloc] initForNode:node
195 menuController:nil]);
197 ASSERT_TRUE(gradient_cell.get());
198 ASSERT_TRUE(bookmark_cell.get());
200 EXPECT_EQ(1, [gradient_cell verticalTextOffset]);
201 EXPECT_EQ(0, [bookmark_cell verticalTextOffset]);
203 EXPECT_NE([bookmark_cell verticalTextOffset],
204 [gradient_cell verticalTextOffset]);