| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/memory/scoped_nsobject.h" | 5 #include "base/mac/scoped_nsobject.h" |
| 6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
| 7 #include "chrome/browser/bookmarks/bookmark_model.h" | 7 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 8 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 8 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 9 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button_cell.h" | 9 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button_cell.h" |
| 10 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" | 10 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" |
| 11 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" | 11 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
| 12 #include "grit/ui_resources.h" | 12 #include "grit/ui_resources.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "testing/platform_test.h" | 14 #include "testing/platform_test.h" |
| 15 #include "ui/base/resource/resource_bundle.h" | 15 #include "ui/base/resource/resource_bundle.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 36 | 36 |
| 37 | 37 |
| 38 namespace { | 38 namespace { |
| 39 | 39 |
| 40 class BookmarkButtonCellTest : public CocoaProfileTest { | 40 class BookmarkButtonCellTest : public CocoaProfileTest { |
| 41 }; | 41 }; |
| 42 | 42 |
| 43 // Make sure it's not totally bogus | 43 // Make sure it's not totally bogus |
| 44 TEST_F(BookmarkButtonCellTest, SizeForBounds) { | 44 TEST_F(BookmarkButtonCellTest, SizeForBounds) { |
| 45 NSRect frame = NSMakeRect(0, 0, 50, 30); | 45 NSRect frame = NSMakeRect(0, 0, 50, 30); |
| 46 scoped_nsobject<NSButton> view([[NSButton alloc] initWithFrame:frame]); | 46 base::scoped_nsobject<NSButton> view([[NSButton alloc] initWithFrame:frame]); |
| 47 scoped_nsobject<BookmarkButtonCell> cell( | 47 base::scoped_nsobject<BookmarkButtonCell> cell( |
| 48 [[BookmarkButtonCell alloc] initTextCell:@"Testing"]); | 48 [[BookmarkButtonCell alloc] initTextCell:@"Testing"]); |
| 49 [view setCell:cell.get()]; | 49 [view setCell:cell.get()]; |
| 50 [[test_window() contentView] addSubview:view]; | 50 [[test_window() contentView] addSubview:view]; |
| 51 | 51 |
| 52 NSRect r = NSMakeRect(0, 0, 100, 100); | 52 NSRect r = NSMakeRect(0, 0, 100, 100); |
| 53 NSSize size = [cell.get() cellSizeForBounds:r]; | 53 NSSize size = [cell.get() cellSizeForBounds:r]; |
| 54 EXPECT_TRUE(size.width > 0 && size.height > 0); | 54 EXPECT_TRUE(size.width > 0 && size.height > 0); |
| 55 EXPECT_TRUE(size.width < 200 && size.height < 200); | 55 EXPECT_TRUE(size.width < 200 && size.height < 200); |
| 56 } | 56 } |
| 57 | 57 |
| 58 // Make sure icon-only buttons are squeezed tightly. | 58 // Make sure icon-only buttons are squeezed tightly. |
| 59 TEST_F(BookmarkButtonCellTest, IconOnlySqueeze) { | 59 TEST_F(BookmarkButtonCellTest, IconOnlySqueeze) { |
| 60 NSRect frame = NSMakeRect(0, 0, 50, 30); | 60 NSRect frame = NSMakeRect(0, 0, 50, 30); |
| 61 scoped_nsobject<NSButton> view([[NSButton alloc] initWithFrame:frame]); | 61 base::scoped_nsobject<NSButton> view([[NSButton alloc] initWithFrame:frame]); |
| 62 scoped_nsobject<BookmarkButtonCell> cell( | 62 base::scoped_nsobject<BookmarkButtonCell> cell( |
| 63 [[BookmarkButtonCell alloc] initTextCell:@"Testing"]); | 63 [[BookmarkButtonCell alloc] initTextCell:@"Testing"]); |
| 64 [view setCell:cell.get()]; | 64 [view setCell:cell.get()]; |
| 65 [[test_window() contentView] addSubview:view]; | 65 [[test_window() contentView] addSubview:view]; |
| 66 | 66 |
| 67 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 67 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 68 scoped_nsobject<NSImage> image( | 68 base::scoped_nsobject<NSImage> image( |
| 69 rb.GetNativeImageNamed(IDR_DEFAULT_FAVICON).CopyNSImage()); | 69 rb.GetNativeImageNamed(IDR_DEFAULT_FAVICON).CopyNSImage()); |
| 70 EXPECT_TRUE(image.get()); | 70 EXPECT_TRUE(image.get()); |
| 71 | 71 |
| 72 NSRect r = NSMakeRect(0, 0, 100, 100); | 72 NSRect r = NSMakeRect(0, 0, 100, 100); |
| 73 [cell setBookmarkCellText:@" " image:image]; | 73 [cell setBookmarkCellText:@" " image:image]; |
| 74 CGFloat two_space_width = [cell.get() cellSizeForBounds:r].width; | 74 CGFloat two_space_width = [cell.get() cellSizeForBounds:r].width; |
| 75 [cell setBookmarkCellText:@" " image:image]; | 75 [cell setBookmarkCellText:@" " image:image]; |
| 76 CGFloat one_space_width = [cell.get() cellSizeForBounds:r].width; | 76 CGFloat one_space_width = [cell.get() cellSizeForBounds:r].width; |
| 77 [cell setBookmarkCellText:@"" image:image]; | 77 [cell setBookmarkCellText:@"" image:image]; |
| 78 CGFloat zero_space_width = [cell.get() cellSizeForBounds:r].width; | 78 CGFloat zero_space_width = [cell.get() cellSizeForBounds:r].width; |
| 79 | 79 |
| 80 // Make sure the switch to "no title" is more significant than we | 80 // Make sure the switch to "no title" is more significant than we |
| 81 // would otherwise see by decreasing the length of the title. | 81 // would otherwise see by decreasing the length of the title. |
| 82 CGFloat delta1 = two_space_width - one_space_width; | 82 CGFloat delta1 = two_space_width - one_space_width; |
| 83 CGFloat delta2 = one_space_width - zero_space_width; | 83 CGFloat delta2 = one_space_width - zero_space_width; |
| 84 EXPECT_GT(delta2, delta1); | 84 EXPECT_GT(delta2, delta1); |
| 85 | 85 |
| 86 } | 86 } |
| 87 | 87 |
| 88 // Make sure the default from the base class is overridden. | 88 // Make sure the default from the base class is overridden. |
| 89 TEST_F(BookmarkButtonCellTest, MouseEnterStuff) { | 89 TEST_F(BookmarkButtonCellTest, MouseEnterStuff) { |
| 90 scoped_nsobject<BookmarkButtonCell> cell( | 90 base::scoped_nsobject<BookmarkButtonCell> cell( |
| 91 [[BookmarkButtonCell alloc] initTextCell:@"Testing"]); | 91 [[BookmarkButtonCell alloc] initTextCell:@"Testing"]); |
| 92 // Setting the menu should have no affect since we either share or | 92 // Setting the menu should have no affect since we either share or |
| 93 // dynamically compose the menu given a node. | 93 // dynamically compose the menu given a node. |
| 94 [cell setMenu:[[[NSMenu alloc] initWithTitle:@"foo"] autorelease]]; | 94 [cell setMenu:[[[NSMenu alloc] initWithTitle:@"foo"] autorelease]]; |
| 95 EXPECT_FALSE([cell menu]); | 95 EXPECT_FALSE([cell menu]); |
| 96 | 96 |
| 97 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); | 97 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); |
| 98 const BookmarkNode* node = model->bookmark_bar_node(); | 98 const BookmarkNode* node = model->bookmark_bar_node(); |
| 99 [cell setEmpty:NO]; | 99 [cell setEmpty:NO]; |
| 100 [cell setBookmarkNode:node]; | 100 [cell setBookmarkNode:node]; |
| 101 EXPECT_TRUE([cell showsBorderOnlyWhileMouseInside]); | 101 EXPECT_TRUE([cell showsBorderOnlyWhileMouseInside]); |
| 102 | 102 |
| 103 [cell setEmpty:YES]; | 103 [cell setEmpty:YES]; |
| 104 EXPECT_FALSE([cell.get() showsBorderOnlyWhileMouseInside]); | 104 EXPECT_FALSE([cell.get() showsBorderOnlyWhileMouseInside]); |
| 105 } | 105 } |
| 106 | 106 |
| 107 TEST_F(BookmarkButtonCellTest, BookmarkNode) { | 107 TEST_F(BookmarkButtonCellTest, BookmarkNode) { |
| 108 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); | 108 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); |
| 109 scoped_nsobject<BookmarkButtonCell> cell( | 109 base::scoped_nsobject<BookmarkButtonCell> cell( |
| 110 [[BookmarkButtonCell alloc] initTextCell:@"Testing"]); | 110 [[BookmarkButtonCell alloc] initTextCell:@"Testing"]); |
| 111 | 111 |
| 112 const BookmarkNode* node = model->bookmark_bar_node(); | 112 const BookmarkNode* node = model->bookmark_bar_node(); |
| 113 [cell setBookmarkNode:node]; | 113 [cell setBookmarkNode:node]; |
| 114 EXPECT_EQ(node, [cell bookmarkNode]); | 114 EXPECT_EQ(node, [cell bookmarkNode]); |
| 115 | 115 |
| 116 node = model->other_node(); | 116 node = model->other_node(); |
| 117 [cell setBookmarkNode:node]; | 117 [cell setBookmarkNode:node]; |
| 118 EXPECT_EQ(node, [cell bookmarkNode]); | 118 EXPECT_EQ(node, [cell bookmarkNode]); |
| 119 } | 119 } |
| 120 | 120 |
| 121 TEST_F(BookmarkButtonCellTest, BookmarkMouseForwarding) { | 121 TEST_F(BookmarkButtonCellTest, BookmarkMouseForwarding) { |
| 122 scoped_nsobject<BookmarkButtonCell> cell( | 122 base::scoped_nsobject<BookmarkButtonCell> cell( |
| 123 [[BookmarkButtonCell alloc] initTextCell:@"Testing"]); | 123 [[BookmarkButtonCell alloc] initTextCell:@"Testing"]); |
| 124 scoped_nsobject<ButtonRemembersMouseEnterExit> | 124 base::scoped_nsobject<ButtonRemembersMouseEnterExit> button( |
| 125 button([[ButtonRemembersMouseEnterExit alloc] | 125 [[ButtonRemembersMouseEnterExit alloc] |
| 126 initWithFrame:NSMakeRect(0,0,50,50)]); | 126 initWithFrame:NSMakeRect(0, 0, 50, 50)]); |
| 127 [button setCell:cell.get()]; | 127 [button setCell:cell.get()]; |
| 128 EXPECT_EQ(0, button.get()->enters_); | 128 EXPECT_EQ(0, button.get()->enters_); |
| 129 EXPECT_EQ(0, button.get()->exits_); | 129 EXPECT_EQ(0, button.get()->exits_); |
| 130 NSEvent* event = [NSEvent mouseEventWithType:NSMouseMoved | 130 NSEvent* event = [NSEvent mouseEventWithType:NSMouseMoved |
| 131 location:NSMakePoint(10,10) | 131 location:NSMakePoint(10,10) |
| 132 modifierFlags:0 | 132 modifierFlags:0 |
| 133 timestamp:0 | 133 timestamp:0 |
| 134 windowNumber:0 | 134 windowNumber:0 |
| 135 context:nil | 135 context:nil |
| 136 eventNumber:0 | 136 eventNumber:0 |
| 137 clickCount:0 | 137 clickCount:0 |
| 138 pressure:0]; | 138 pressure:0]; |
| 139 [cell mouseEntered:event]; | 139 [cell mouseEntered:event]; |
| 140 EXPECT_TRUE(button.get()->enters_ && !button.get()->exits_); | 140 EXPECT_TRUE(button.get()->enters_ && !button.get()->exits_); |
| 141 | 141 |
| 142 for (int i = 0; i < 3; i++) | 142 for (int i = 0; i < 3; i++) |
| 143 [cell mouseExited:event]; | 143 [cell mouseExited:event]; |
| 144 EXPECT_EQ(button.get()->enters_, 1); | 144 EXPECT_EQ(button.get()->enters_, 1); |
| 145 EXPECT_EQ(button.get()->exits_, 3); | 145 EXPECT_EQ(button.get()->exits_, 3); |
| 146 } | 146 } |
| 147 | 147 |
| 148 // Confirms a cell created in a nib is initialized properly | 148 // Confirms a cell created in a nib is initialized properly |
| 149 TEST_F(BookmarkButtonCellTest, Awake) { | 149 TEST_F(BookmarkButtonCellTest, Awake) { |
| 150 scoped_nsobject<BookmarkButtonCell> cell([[BookmarkButtonCell alloc] init]); | 150 base::scoped_nsobject<BookmarkButtonCell> cell( |
| 151 [[BookmarkButtonCell alloc] init]); |
| 151 [cell awakeFromNib]; | 152 [cell awakeFromNib]; |
| 152 EXPECT_EQ(NSLeftTextAlignment, [cell alignment]); | 153 EXPECT_EQ(NSLeftTextAlignment, [cell alignment]); |
| 153 } | 154 } |
| 154 | 155 |
| 155 // Subfolder arrow details. | 156 // Subfolder arrow details. |
| 156 TEST_F(BookmarkButtonCellTest, FolderArrow) { | 157 TEST_F(BookmarkButtonCellTest, FolderArrow) { |
| 157 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); | 158 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); |
| 158 const BookmarkNode* bar = model->bookmark_bar_node(); | 159 const BookmarkNode* bar = model->bookmark_bar_node(); |
| 159 const BookmarkNode* node = model->AddURL(bar, bar->child_count(), | 160 const BookmarkNode* node = model->AddURL(bar, bar->child_count(), |
| 160 ASCIIToUTF16("title"), | 161 ASCIIToUTF16("title"), |
| 161 GURL("http://www.google.com")); | 162 GURL("http://www.google.com")); |
| 162 scoped_nsobject<BookmarkButtonCell> cell( | 163 base::scoped_nsobject<BookmarkButtonCell> cell( |
| 163 [[BookmarkButtonCell alloc] initForNode:node | 164 [[BookmarkButtonCell alloc] initForNode:node |
| 164 text:@"small" | 165 text:@"small" |
| 165 image:nil | 166 image:nil |
| 166 menuController:nil]); | 167 menuController:nil]); |
| 167 EXPECT_TRUE(cell.get()); | 168 EXPECT_TRUE(cell.get()); |
| 168 | 169 |
| 169 NSSize size = [cell cellSize]; | 170 NSSize size = [cell cellSize]; |
| 170 // sanity check | 171 // sanity check |
| 171 EXPECT_GE(size.width, 2); | 172 EXPECT_GE(size.width, 2); |
| 172 EXPECT_GE(size.height, 2); | 173 EXPECT_GE(size.height, 2); |
| 173 | 174 |
| 174 // Once we turn on arrow drawing make sure there is now room for it. | 175 // Once we turn on arrow drawing make sure there is now room for it. |
| 175 [cell setDrawFolderArrow:YES]; | 176 [cell setDrawFolderArrow:YES]; |
| 176 NSSize arrowSize = [cell cellSize]; | 177 NSSize arrowSize = [cell cellSize]; |
| 177 EXPECT_GT(arrowSize.width, size.width); | 178 EXPECT_GT(arrowSize.width, size.width); |
| 178 } | 179 } |
| 179 | 180 |
| 180 TEST_F(BookmarkButtonCellTest, VerticalTextOffset) { | 181 TEST_F(BookmarkButtonCellTest, VerticalTextOffset) { |
| 181 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); | 182 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); |
| 182 const BookmarkNode* bar = model->bookmark_bar_node(); | 183 const BookmarkNode* bar = model->bookmark_bar_node(); |
| 183 const BookmarkNode* node = model->AddURL(bar, bar->child_count(), | 184 const BookmarkNode* node = model->AddURL(bar, bar->child_count(), |
| 184 ASCIIToUTF16("title"), | 185 ASCIIToUTF16("title"), |
| 185 GURL("http://www.google.com")); | 186 GURL("http://www.google.com")); |
| 186 | 187 |
| 187 scoped_nsobject<GradientButtonCell> gradient_cell( | 188 base::scoped_nsobject<GradientButtonCell> gradient_cell( |
| 188 [[GradientButtonCell alloc] initTextCell:@"Testing"]); | 189 [[GradientButtonCell alloc] initTextCell:@"Testing"]); |
| 189 scoped_nsobject<BookmarkButtonCell> bookmark_cell( | 190 base::scoped_nsobject<BookmarkButtonCell> bookmark_cell( |
| 190 [[BookmarkButtonCell alloc] initForNode:node | 191 [[BookmarkButtonCell alloc] initForNode:node |
| 191 text:@"small" | 192 text:@"small" |
| 192 image:nil | 193 image:nil |
| 193 menuController:nil]); | 194 menuController:nil]); |
| 194 | 195 |
| 195 ASSERT_TRUE(gradient_cell.get()); | 196 ASSERT_TRUE(gradient_cell.get()); |
| 196 ASSERT_TRUE(bookmark_cell.get()); | 197 ASSERT_TRUE(bookmark_cell.get()); |
| 197 | 198 |
| 198 EXPECT_EQ(1, [gradient_cell verticalTextOffset]); | 199 EXPECT_EQ(1, [gradient_cell verticalTextOffset]); |
| 199 EXPECT_EQ(0, [bookmark_cell verticalTextOffset]); | 200 EXPECT_EQ(0, [bookmark_cell verticalTextOffset]); |
| 200 | 201 |
| 201 EXPECT_NE([bookmark_cell verticalTextOffset], | 202 EXPECT_NE([bookmark_cell verticalTextOffset], |
| 202 [gradient_cell verticalTextOffset]); | 203 [gradient_cell verticalTextOffset]); |
| 203 } | 204 } |
| 204 | 205 |
| 205 } // namespace | 206 } // namespace |
| OLD | NEW |