Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / bookmarks / bookmark_button_cell_unittest.mm
blob4173844ac0ada3f1acf49a43cc1bad4019d47ec1
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 "testing/gtest/include/gtest/gtest.h"
14 #include "testing/platform_test.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/gfx/image/image.h"
17 #include "ui/resources/grit/ui_resources.h"
19 using bookmarks::BookmarkModel;
20 using bookmarks::BookmarkNode;
22 // Simple class to remember how many mouseEntered: and mouseExited:
23 // calls it gets.  Only used by BookmarkMouseForwarding but placed
24 // at the top of the file to keep it outside the anon namespace.
25 @interface ButtonRemembersMouseEnterExit : NSButton {
26  @public
27   int enters_;
28   int exits_;
30 @end
32 @implementation ButtonRemembersMouseEnterExit
33 - (void)mouseEntered:(NSEvent*)event {
34   enters_++;
36 - (void)mouseExited:(NSEvent*)event {
37   exits_++;
39 @end
42 namespace {
44 class BookmarkButtonCellTest : public CocoaProfileTest {
47 // Make sure it's not totally bogus
48 TEST_F(BookmarkButtonCellTest, SizeForBounds) {
49   NSRect frame = NSMakeRect(0, 0, 50, 30);
50   base::scoped_nsobject<NSButton> view([[NSButton alloc] initWithFrame:frame]);
51   base::scoped_nsobject<BookmarkButtonCell> cell(
52       [[BookmarkButtonCell alloc] initTextCell:@"Testing"]);
53   [view setCell:cell.get()];
54   [[test_window() contentView] addSubview:view];
56   NSRect r = NSMakeRect(0, 0, 100, 100);
57   NSSize size = [cell.get() cellSizeForBounds:r];
58   EXPECT_TRUE(size.width > 0 && size.height > 0);
59   EXPECT_TRUE(size.width < 200 && size.height < 200);
62 // Make sure icon-only buttons are squeezed tightly.
63 TEST_F(BookmarkButtonCellTest, IconOnlySqueeze) {
64   NSRect frame = NSMakeRect(0, 0, 50, 30);
65   base::scoped_nsobject<NSButton> view([[NSButton alloc] initWithFrame:frame]);
66   base::scoped_nsobject<BookmarkButtonCell> cell(
67       [[BookmarkButtonCell alloc] initTextCell:@"Testing"]);
68   [view setCell:cell.get()];
69   [[test_window() contentView] addSubview:view];
71   ResourceBundle& rb = ResourceBundle::GetSharedInstance();
72   base::scoped_nsobject<NSImage> image(
73       rb.GetNativeImageNamed(IDR_DEFAULT_FAVICON).CopyNSImage());
74   EXPECT_TRUE(image.get());
76   NSRect r = NSMakeRect(0, 0, 100, 100);
77   [cell setBookmarkCellText:@"  " image:image];
78   CGFloat two_space_width = [cell.get() cellSizeForBounds:r].width;
79   [cell setBookmarkCellText:@" " image:image];
80   CGFloat one_space_width = [cell.get() cellSizeForBounds:r].width;
81   [cell setBookmarkCellText:@"" image:image];
82   CGFloat zero_space_width = [cell.get() cellSizeForBounds:r].width;
84   // Make sure the switch to "no title" is more significant than we
85   // would otherwise see by decreasing the length of the title.
86   CGFloat delta1 = two_space_width - one_space_width;
87   CGFloat delta2 = one_space_width - zero_space_width;
88   EXPECT_GT(delta2, delta1);
92 // Make sure the default from the base class is overridden.
93 TEST_F(BookmarkButtonCellTest, MouseEnterStuff) {
94   base::scoped_nsobject<BookmarkButtonCell> cell(
95       [[BookmarkButtonCell alloc] initTextCell:@"Testing"]);
96   // Setting the menu should have no affect since we either share or
97   // dynamically compose the menu given a node.
98   [cell setMenu:[[[NSMenu alloc] initWithTitle:@"foo"] autorelease]];
99   EXPECT_FALSE([cell menu]);
101   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
102   const BookmarkNode* node = model->bookmark_bar_node();
103   [cell setEmpty:NO];
104   [cell setBookmarkNode:node];
105   EXPECT_TRUE([cell showsBorderOnlyWhileMouseInside]);
107   [cell setEmpty:YES];
108   EXPECT_FALSE([cell.get() showsBorderOnlyWhileMouseInside]);
111 TEST_F(BookmarkButtonCellTest, BookmarkNode) {
112   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
113   base::scoped_nsobject<BookmarkButtonCell> cell(
114       [[BookmarkButtonCell alloc] initTextCell:@"Testing"]);
116   const BookmarkNode* node = model->bookmark_bar_node();
117   [cell setBookmarkNode:node];
118   EXPECT_EQ(node, [cell bookmarkNode]);
120   node = model->other_node();
121   [cell setBookmarkNode:node];
122   EXPECT_EQ(node, [cell bookmarkNode]);
125 TEST_F(BookmarkButtonCellTest, BookmarkMouseForwarding) {
126   base::scoped_nsobject<BookmarkButtonCell> cell(
127       [[BookmarkButtonCell alloc] initTextCell:@"Testing"]);
128   base::scoped_nsobject<ButtonRemembersMouseEnterExit> button(
129       [[ButtonRemembersMouseEnterExit alloc]
130           initWithFrame:NSMakeRect(0, 0, 50, 50)]);
131   [button setCell:cell.get()];
132   EXPECT_EQ(0, button.get()->enters_);
133   EXPECT_EQ(0, button.get()->exits_);
134   NSEvent* event = [NSEvent mouseEventWithType:NSMouseMoved
135                                       location:NSMakePoint(10,10)
136                                  modifierFlags:0
137                                      timestamp:0
138                                   windowNumber:0
139                                        context:nil
140                                    eventNumber:0
141                                     clickCount:0
142                                       pressure:0];
143   [cell mouseEntered:event];
144   EXPECT_TRUE(button.get()->enters_ && !button.get()->exits_);
146   for (int i = 0; i < 3; i++)
147     [cell mouseExited:event];
148   EXPECT_EQ(button.get()->enters_, 1);
149   EXPECT_EQ(button.get()->exits_, 3);
152 // Confirms a cell created in a nib is initialized properly
153 TEST_F(BookmarkButtonCellTest, Awake) {
154   base::scoped_nsobject<BookmarkButtonCell> cell(
155       [[BookmarkButtonCell alloc] init]);
156   [cell awakeFromNib];
157   EXPECT_EQ(NSLeftTextAlignment, [cell alignment]);
160 // Subfolder arrow details.
161 TEST_F(BookmarkButtonCellTest, FolderArrow) {
162   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
163   const BookmarkNode* bar = model->bookmark_bar_node();
164   const BookmarkNode* node = model->AddURL(bar, bar->child_count(),
165                                            base::ASCIIToUTF16("title"),
166                                            GURL("http://www.google.com"));
167   base::scoped_nsobject<BookmarkButtonCell> cell(
168       [[BookmarkButtonCell alloc] initForNode:node
169                                          text:@"small"
170                                         image:nil
171                                menuController:nil]);
172   EXPECT_TRUE(cell.get());
174   NSSize size = [cell cellSize];
175   // sanity check
176   EXPECT_GE(size.width, 2);
177   EXPECT_GE(size.height, 2);
179   // Once we turn on arrow drawing make sure there is now room for it.
180   [cell setDrawFolderArrow:YES];
181   NSSize arrowSize = [cell cellSize];
182   EXPECT_GT(arrowSize.width, size.width);
185 TEST_F(BookmarkButtonCellTest, VerticalTextOffset) {
186   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
187   const BookmarkNode* bar = model->bookmark_bar_node();
188   const BookmarkNode* node = model->AddURL(bar, bar->child_count(),
189                                            base::ASCIIToUTF16("title"),
190                                            GURL("http://www.google.com"));
192   base::scoped_nsobject<GradientButtonCell> gradient_cell(
193       [[GradientButtonCell alloc] initTextCell:@"Testing"]);
194   base::scoped_nsobject<BookmarkButtonCell> bookmark_cell(
195       [[BookmarkButtonCell alloc] initForNode:node
196                                          text:@"small"
197                                         image:nil
198                                menuController:nil]);
200   ASSERT_TRUE(gradient_cell.get());
201   ASSERT_TRUE(bookmark_cell.get());
203   EXPECT_EQ(1, [gradient_cell verticalTextOffset]);
204   EXPECT_EQ(0, [bookmark_cell verticalTextOffset]);
206   EXPECT_NE([bookmark_cell verticalTextOffset],
207             [gradient_cell verticalTextOffset]);
210 }  // namespace