| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/ref_counted_memory.h" | 8 #include "base/ref_counted_memory.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/sys_string_conversions.h" | 10 #include "base/sys_string_conversions.h" |
| 11 #include "chrome/app/chrome_dll_resource.h" | 11 #include "chrome/app/chrome_dll_resource.h" |
| 12 #include "chrome/browser/browser.h" | 12 #include "chrome/browser/browser.h" |
| 13 #include "chrome/browser/cancelable_request.h" | 13 #include "chrome/browser/cancelable_request.h" |
| 14 #include "chrome/browser/cocoa/browser_test_helper.h" | 14 #include "chrome/browser/cocoa/browser_test_helper.h" |
| 15 #include "chrome/browser/cocoa/cocoa_test_helper.h" | 15 #include "chrome/browser/cocoa/cocoa_test_helper.h" |
| 16 #include "chrome/browser/cocoa/history_menu_bridge.h" | 16 #include "chrome/browser/cocoa/history_menu_bridge.h" |
| 17 #include "chrome/browser/sessions/tab_restore_service.h" | 17 #include "chrome/browser/sessions/tab_restore_service.h" |
| 18 #include "gfx/codec/png_codec.h" | 18 #include "gfx/codec/png_codec.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #import "testing/gtest_mac.h" |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 class MockTRS : public TabRestoreService { | 25 class MockTRS : public TabRestoreService { |
| 25 public: | 26 public: |
| 26 MockTRS(Profile* profile) : TabRestoreService(profile, NULL) {} | 27 MockTRS(Profile* profile) : TabRestoreService(profile, NULL) {} |
| 27 MOCK_CONST_METHOD0(entries, const TabRestoreService::Entries&()); | 28 MOCK_CONST_METHOD0(entries, const TabRestoreService::Entries&()); |
| 28 }; | 29 }; |
| 29 | 30 |
| 30 class MockBridge : public HistoryMenuBridge { | 31 class MockBridge : public HistoryMenuBridge { |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 | 126 |
| 126 NSInteger tag = HistoryMenuBridge::kMostVisited; | 127 NSInteger tag = HistoryMenuBridge::kMostVisited; |
| 127 AddItemToMenu(menu, @"alpha", @selector(openHistoryMenuItem:), tag); | 128 AddItemToMenu(menu, @"alpha", @selector(openHistoryMenuItem:), tag); |
| 128 AddItemToMenu(menu, @"bravo", @selector(openHistoryMenuItem:), tag); | 129 AddItemToMenu(menu, @"bravo", @selector(openHistoryMenuItem:), tag); |
| 129 AddItemToMenu(menu, @"charlie", @selector(openHistoryMenuItem:), tag); | 130 AddItemToMenu(menu, @"charlie", @selector(openHistoryMenuItem:), tag); |
| 130 AddItemToMenu(menu, @"delta", @selector(openHistoryMenuItem:), tag); | 131 AddItemToMenu(menu, @"delta", @selector(openHistoryMenuItem:), tag); |
| 131 | 132 |
| 132 ClearMenuSection(menu, HistoryMenuBridge::kMostVisited); | 133 ClearMenuSection(menu, HistoryMenuBridge::kMostVisited); |
| 133 | 134 |
| 134 EXPECT_EQ(1, [menu numberOfItems]); | 135 EXPECT_EQ(1, [menu numberOfItems]); |
| 135 EXPECT_TRUE([@"HEADER" isEqualToString: | 136 EXPECT_NSEQ(@"HEADER", |
| 136 [[menu itemWithTag:HistoryMenuBridge::kMostVisitedTitle] title]]); | 137 [[menu itemWithTag:HistoryMenuBridge::kMostVisitedTitle] title]); |
| 137 } | 138 } |
| 138 | 139 |
| 139 // Skip menu items that are not hooked up to |-openHistoryMenuItem:|. | 140 // Skip menu items that are not hooked up to |-openHistoryMenuItem:|. |
| 140 TEST_F(HistoryMenuBridgeTest, ClearHistoryMenuSkipping) { | 141 TEST_F(HistoryMenuBridgeTest, ClearHistoryMenuSkipping) { |
| 141 NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"history foo"] autorelease]; | 142 NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"history foo"] autorelease]; |
| 142 AddItemToMenu(menu, @"HEADER", NULL, HistoryMenuBridge::kMostVisitedTitle); | 143 AddItemToMenu(menu, @"HEADER", NULL, HistoryMenuBridge::kMostVisitedTitle); |
| 143 | 144 |
| 144 NSInteger tag = HistoryMenuBridge::kMostVisited; | 145 NSInteger tag = HistoryMenuBridge::kMostVisited; |
| 145 AddItemToMenu(menu, @"alpha", @selector(openHistoryMenuItem:), tag); | 146 AddItemToMenu(menu, @"alpha", @selector(openHistoryMenuItem:), tag); |
| 146 AddItemToMenu(menu, @"bravo", @selector(openHistoryMenuItem:), tag); | 147 AddItemToMenu(menu, @"bravo", @selector(openHistoryMenuItem:), tag); |
| 147 AddItemToMenu(menu, @"TITLE", NULL, HistoryMenuBridge::kRecentlyClosedTitle); | 148 AddItemToMenu(menu, @"TITLE", NULL, HistoryMenuBridge::kRecentlyClosedTitle); |
| 148 AddItemToMenu(menu, @"charlie", @selector(openHistoryMenuItem:), tag); | 149 AddItemToMenu(menu, @"charlie", @selector(openHistoryMenuItem:), tag); |
| 149 | 150 |
| 150 ClearMenuSection(menu, tag); | 151 ClearMenuSection(menu, tag); |
| 151 | 152 |
| 152 EXPECT_EQ(2, [menu numberOfItems]); | 153 EXPECT_EQ(2, [menu numberOfItems]); |
| 153 EXPECT_TRUE([@"HEADER" isEqualToString: | 154 EXPECT_NSEQ(@"HEADER", |
| 154 [[menu itemWithTag:HistoryMenuBridge::kMostVisitedTitle] title]]); | 155 [[menu itemWithTag:HistoryMenuBridge::kMostVisitedTitle] title]); |
| 155 EXPECT_TRUE([@"TITLE" isEqualToString: | 156 EXPECT_NSEQ(@"TITLE", |
| 156 [[menu itemAtIndex:1] title]]); | 157 [[menu itemAtIndex:1] title]); |
| 157 } | 158 } |
| 158 | 159 |
| 159 // Edge case test for clearing an empty menu. | 160 // Edge case test for clearing an empty menu. |
| 160 TEST_F(HistoryMenuBridgeTest, ClearHistoryMenuEmpty) { | 161 TEST_F(HistoryMenuBridgeTest, ClearHistoryMenuEmpty) { |
| 161 NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"history foo"] autorelease]; | 162 NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"history foo"] autorelease]; |
| 162 AddItemToMenu(menu, @"HEADER", NULL, HistoryMenuBridge::kMostVisited); | 163 AddItemToMenu(menu, @"HEADER", NULL, HistoryMenuBridge::kMostVisited); |
| 163 | 164 |
| 164 ClearMenuSection(menu, HistoryMenuBridge::kMostVisited); | 165 ClearMenuSection(menu, HistoryMenuBridge::kMostVisited); |
| 165 | 166 |
| 166 EXPECT_EQ(1, [menu numberOfItems]); | 167 EXPECT_EQ(1, [menu numberOfItems]); |
| 167 EXPECT_TRUE([@"HEADER" isEqualToString: | 168 EXPECT_NSEQ(@"HEADER", |
| 168 [[menu itemWithTag:HistoryMenuBridge::kMostVisited] title]]); | 169 [[menu itemWithTag:HistoryMenuBridge::kMostVisited] title]); |
| 169 } | 170 } |
| 170 | 171 |
| 171 // Test that AddItemToMenu() properly adds HistoryItem objects as menus. | 172 // Test that AddItemToMenu() properly adds HistoryItem objects as menus. |
| 172 TEST_F(HistoryMenuBridgeTest, AddItemToMenu) { | 173 TEST_F(HistoryMenuBridgeTest, AddItemToMenu) { |
| 173 NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"history foo"] autorelease]; | 174 NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"history foo"] autorelease]; |
| 174 | 175 |
| 175 const string16 short_url = ASCIIToUTF16("http://foo/"); | 176 const string16 short_url = ASCIIToUTF16("http://foo/"); |
| 176 const string16 long_url = ASCIIToUTF16("http://super-duper-long-url--." | 177 const string16 long_url = ASCIIToUTF16("http://super-duper-long-url--." |
| 177 "that.cannot.possibly.fit.even-in-80-columns" | 178 "that.cannot.possibly.fit.even-in-80-columns" |
| 178 "or.be.reasonably-displayed-in-a-menu" | 179 "or.be.reasonably-displayed-in-a-menu" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 | 230 |
| 230 bridge_->TabRestoreServiceChanged(trs.get()); | 231 bridge_->TabRestoreServiceChanged(trs.get()); |
| 231 | 232 |
| 232 NSMenu* menu = bridge_->HistoryMenu(); | 233 NSMenu* menu = bridge_->HistoryMenu(); |
| 233 ASSERT_EQ(2U, [[menu itemArray] count]); | 234 ASSERT_EQ(2U, [[menu itemArray] count]); |
| 234 | 235 |
| 235 NSMenuItem* item1 = [menu itemAtIndex:0]; | 236 NSMenuItem* item1 = [menu itemAtIndex:0]; |
| 236 MockBridge::HistoryItem* hist1 = bridge_->HistoryItemForMenuItem(item1); | 237 MockBridge::HistoryItem* hist1 = bridge_->HistoryItemForMenuItem(item1); |
| 237 EXPECT_TRUE(hist1); | 238 EXPECT_TRUE(hist1); |
| 238 EXPECT_EQ(24, hist1->session_id); | 239 EXPECT_EQ(24, hist1->session_id); |
| 239 EXPECT_TRUE([@"Google" isEqualToString:[item1 title]]); | 240 EXPECT_NSEQ(@"Google", [item1 title]); |
| 240 | 241 |
| 241 NSMenuItem* item2 = [menu itemAtIndex:1]; | 242 NSMenuItem* item2 = [menu itemAtIndex:1]; |
| 242 MockBridge::HistoryItem* hist2 = bridge_->HistoryItemForMenuItem(item2); | 243 MockBridge::HistoryItem* hist2 = bridge_->HistoryItemForMenuItem(item2); |
| 243 EXPECT_TRUE(hist2); | 244 EXPECT_TRUE(hist2); |
| 244 EXPECT_EQ(42, hist2->session_id); | 245 EXPECT_EQ(42, hist2->session_id); |
| 245 EXPECT_TRUE([@"Apple" isEqualToString:[item2 title]]); | 246 EXPECT_NSEQ(@"Apple", [item2 title]); |
| 246 } | 247 } |
| 247 | 248 |
| 248 // Test that the menu is created for a mix of windows and tabs. | 249 // Test that the menu is created for a mix of windows and tabs. |
| 249 TEST_F(HistoryMenuBridgeTest, RecentlyClosedTabsAndWindows) { | 250 TEST_F(HistoryMenuBridgeTest, RecentlyClosedTabsAndWindows) { |
| 250 scoped_refptr<MockTRS> trs(new MockTRS(browser_test_helper_.profile())); | 251 scoped_refptr<MockTRS> trs(new MockTRS(browser_test_helper_.profile())); |
| 251 MockTRS::Entries entries; | 252 MockTRS::Entries entries; |
| 252 | 253 |
| 253 MockTRS::Tab tab1 = CreateSessionTab(GURL("http://google.com"), | 254 MockTRS::Tab tab1 = CreateSessionTab(GURL("http://google.com"), |
| 254 ASCIIToUTF16("Google")); | 255 ASCIIToUTF16("Google")); |
| 255 tab1.id = 24; | 256 tab1.id = 24; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 | 290 |
| 290 bridge_->TabRestoreServiceChanged(trs.get()); | 291 bridge_->TabRestoreServiceChanged(trs.get()); |
| 291 | 292 |
| 292 NSMenu* menu = bridge_->HistoryMenu(); | 293 NSMenu* menu = bridge_->HistoryMenu(); |
| 293 ASSERT_EQ(4U, [[menu itemArray] count]); | 294 ASSERT_EQ(4U, [[menu itemArray] count]); |
| 294 | 295 |
| 295 NSMenuItem* item1 = [menu itemAtIndex:0]; | 296 NSMenuItem* item1 = [menu itemAtIndex:0]; |
| 296 MockBridge::HistoryItem* hist1 = bridge_->HistoryItemForMenuItem(item1); | 297 MockBridge::HistoryItem* hist1 = bridge_->HistoryItemForMenuItem(item1); |
| 297 EXPECT_TRUE(hist1); | 298 EXPECT_TRUE(hist1); |
| 298 EXPECT_EQ(24, hist1->session_id); | 299 EXPECT_EQ(24, hist1->session_id); |
| 299 EXPECT_TRUE([@"Google" isEqualToString:[item1 title]]); | 300 EXPECT_NSEQ(@"Google", [item1 title]); |
| 300 | 301 |
| 301 NSMenuItem* item2 = [menu itemAtIndex:1]; | 302 NSMenuItem* item2 = [menu itemAtIndex:1]; |
| 302 MockBridge::HistoryItem* hist2 = bridge_->HistoryItemForMenuItem(item2); | 303 MockBridge::HistoryItem* hist2 = bridge_->HistoryItemForMenuItem(item2); |
| 303 EXPECT_TRUE(hist2); | 304 EXPECT_TRUE(hist2); |
| 304 EXPECT_EQ(30, hist2->session_id); | 305 EXPECT_EQ(30, hist2->session_id); |
| 305 EXPECT_EQ(2U, hist2->tabs.size()); | 306 EXPECT_EQ(2U, hist2->tabs.size()); |
| 306 // Do not test menu item title because it is localized. | 307 // Do not test menu item title because it is localized. |
| 307 NSMenu* submenu1 = [item2 submenu]; | 308 NSMenu* submenu1 = [item2 submenu]; |
| 308 EXPECT_EQ(4U, [[submenu1 itemArray] count]); | 309 EXPECT_EQ(4U, [[submenu1 itemArray] count]); |
| 309 // Do not test Restore All Tabs because it is localiced. | 310 // Do not test Restore All Tabs because it is localiced. |
| 310 EXPECT_TRUE([[submenu1 itemAtIndex:1] isSeparatorItem]); | 311 EXPECT_TRUE([[submenu1 itemAtIndex:1] isSeparatorItem]); |
| 311 EXPECT_TRUE([@"foo" isEqualToString:[[submenu1 itemAtIndex:2] title]]); | 312 EXPECT_NSEQ(@"foo", [[submenu1 itemAtIndex:2] title]); |
| 312 EXPECT_TRUE([@"bar" isEqualToString:[[submenu1 itemAtIndex:3] title]]); | 313 EXPECT_NSEQ(@"bar", [[submenu1 itemAtIndex:3] title]); |
| 313 | 314 |
| 314 NSMenuItem* item3 = [menu itemAtIndex:2]; | 315 NSMenuItem* item3 = [menu itemAtIndex:2]; |
| 315 MockBridge::HistoryItem* hist3 = bridge_->HistoryItemForMenuItem(item3); | 316 MockBridge::HistoryItem* hist3 = bridge_->HistoryItemForMenuItem(item3); |
| 316 EXPECT_TRUE(hist3); | 317 EXPECT_TRUE(hist3); |
| 317 EXPECT_EQ(42, hist3->session_id); | 318 EXPECT_EQ(42, hist3->session_id); |
| 318 EXPECT_TRUE([@"Apple" isEqualToString:[item3 title]]); | 319 EXPECT_NSEQ(@"Apple", [item3 title]); |
| 319 | 320 |
| 320 NSMenuItem* item4 = [menu itemAtIndex:3]; | 321 NSMenuItem* item4 = [menu itemAtIndex:3]; |
| 321 MockBridge::HistoryItem* hist4 = bridge_->HistoryItemForMenuItem(item4); | 322 MockBridge::HistoryItem* hist4 = bridge_->HistoryItemForMenuItem(item4); |
| 322 EXPECT_TRUE(hist4); | 323 EXPECT_TRUE(hist4); |
| 323 EXPECT_EQ(50, hist4->session_id); | 324 EXPECT_EQ(50, hist4->session_id); |
| 324 EXPECT_EQ(3U, hist4->tabs.size()); | 325 EXPECT_EQ(3U, hist4->tabs.size()); |
| 325 // Do not test menu item title because it is localized. | 326 // Do not test menu item title because it is localized. |
| 326 NSMenu* submenu2 = [item4 submenu]; | 327 NSMenu* submenu2 = [item4 submenu]; |
| 327 EXPECT_EQ(5U, [[submenu2 itemArray] count]); | 328 EXPECT_EQ(5U, [[submenu2 itemArray] count]); |
| 328 // Do not test Restore All Tabs because it is localiced. | 329 // Do not test Restore All Tabs because it is localiced. |
| 329 EXPECT_TRUE([[submenu2 itemAtIndex:1] isSeparatorItem]); | 330 EXPECT_TRUE([[submenu2 itemAtIndex:1] isSeparatorItem]); |
| 330 EXPECT_TRUE([@"magic" isEqualToString:[[submenu2 itemAtIndex:2] title]]); | 331 EXPECT_NSEQ(@"magic", [[submenu2 itemAtIndex:2] title]); |
| 331 EXPECT_TRUE([@"goats" isEqualToString:[[submenu2 itemAtIndex:3] title]]); | 332 EXPECT_NSEQ(@"goats", [[submenu2 itemAtIndex:3] title]); |
| 332 EXPECT_TRUE([@"teleporter" isEqualToString:[[submenu2 itemAtIndex:4] title]]); | 333 EXPECT_NSEQ(@"teleporter", [[submenu2 itemAtIndex:4] title]); |
| 333 } | 334 } |
| 334 | 335 |
| 335 // Tests that we properly request an icon from the FaviconService. | 336 // Tests that we properly request an icon from the FaviconService. |
| 336 TEST_F(HistoryMenuBridgeTest, GetFaviconForHistoryItem) { | 337 TEST_F(HistoryMenuBridgeTest, GetFaviconForHistoryItem) { |
| 337 // Create a fake item. | 338 // Create a fake item. |
| 338 HistoryMenuBridge::HistoryItem item; | 339 HistoryMenuBridge::HistoryItem item; |
| 339 item.title = ASCIIToUTF16("Title"); | 340 item.title = ASCIIToUTF16("Title"); |
| 340 item.url = GURL("http://google.com"); | 341 item.url = GURL("http://google.com"); |
| 341 | 342 |
| 342 // Request the icon. | 343 // Request the icon. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 // Pretend to be called back. | 376 // Pretend to be called back. |
| 376 GotFaviconData(item.icon_handle, true, bytes, false, GURL()); | 377 GotFaviconData(item.icon_handle, true, bytes, false, GURL()); |
| 377 | 378 |
| 378 // Make sure the callback works. | 379 // Make sure the callback works. |
| 379 EXPECT_EQ(false, item.icon_requested); | 380 EXPECT_EQ(false, item.icon_requested); |
| 380 EXPECT_TRUE(item.icon.get()); | 381 EXPECT_TRUE(item.icon.get()); |
| 381 EXPECT_TRUE([item.menu_item image]); | 382 EXPECT_TRUE([item.menu_item image]); |
| 382 } | 383 } |
| 383 | 384 |
| 384 } // namespace | 385 } // namespace |
| OLD | NEW |