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 #import <Cocoa/Cocoa.h>
8 #include "base/memory/ref_counted_memory.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/sys_string_conversions.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/app/chrome_command_ids.h"
14 #include "chrome/browser/common/cancelable_request.h"
15 #include "chrome/browser/sessions/persistent_tab_restore_service.h"
16 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
17 #include "chrome/browser/ui/cocoa/history_menu_bridge.h"
18 #include "chrome/common/favicon/favicon_types.h"
19 #include "chrome/test/base/testing_profile.h"
20 #include "components/sessions/serialized_navigation_entry_test_helper.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #import "testing/gtest_mac.h"
24 #include "third_party/skia/include/core/SkBitmap.h"
25 #include "ui/gfx/codec/png_codec.h"
29 class MockTRS : public PersistentTabRestoreService {
31 MockTRS(Profile* profile) : PersistentTabRestoreService(profile, NULL) {}
32 MOCK_CONST_METHOD0(entries, const TabRestoreService::Entries&());
35 class MockBridge : public HistoryMenuBridge {
37 MockBridge(Profile* profile)
38 : HistoryMenuBridge(profile),
39 menu_([[NSMenu alloc] initWithTitle:@"History"]) {}
41 virtual NSMenu* HistoryMenu() OVERRIDE {
46 base::scoped_nsobject<NSMenu> menu_;
49 class HistoryMenuBridgeTest : public CocoaProfileTest {
52 virtual void SetUp() {
53 CocoaProfileTest::SetUp();
54 profile()->CreateFaviconService();
55 bridge_.reset(new MockBridge(profile()));
58 // We are a friend of HistoryMenuBridge (and have access to
59 // protected methods), but none of the classes generated by TEST_F()
60 // are. Wraps common commands.
61 void ClearMenuSection(NSMenu* menu,
63 bridge_->ClearMenuSection(menu, tag);
66 void AddItemToBridgeMenu(HistoryMenuBridge::HistoryItem* item,
70 bridge_->AddItemToMenu(item, menu, tag, index);
73 NSMenuItem* AddItemToMenu(NSMenu* menu,
77 NSMenuItem* item = [[[NSMenuItem alloc] initWithTitle:title action:NULL
78 keyEquivalent:@""] autorelease];
81 [item setAction:selector];
82 [item setTarget:bridge_->controller_.get()];
88 HistoryMenuBridge::HistoryItem* CreateItem(const base::string16& title) {
89 HistoryMenuBridge::HistoryItem* item =
90 new HistoryMenuBridge::HistoryItem();
92 item->url = GURL(title);
96 MockTRS::Tab CreateSessionTab(const std::string& url,
97 const std::string& title) {
99 tab.current_navigation_index = 0;
100 tab.navigations.push_back(
101 sessions::SerializedNavigationEntryTestHelper::CreateNavigation(
106 void GetFaviconForHistoryItem(HistoryMenuBridge::HistoryItem* item) {
107 bridge_->GetFaviconForHistoryItem(item);
111 HistoryMenuBridge::HistoryItem* item,
112 const chrome::FaviconImageResult& image_result) {
113 bridge_->GotFaviconData(item, image_result);
116 CancelableRequestConsumerTSimple<HistoryMenuBridge::HistoryItem*>&
118 return bridge_->favicon_consumer_;
121 scoped_ptr<MockBridge> bridge_;
124 // Edge case test for clearing until the end of a menu.
125 TEST_F(HistoryMenuBridgeTest, ClearHistoryMenuUntilEnd) {
126 NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"history foo"] autorelease];
127 AddItemToMenu(menu, @"HEADER", NULL, HistoryMenuBridge::kVisitedTitle);
129 NSInteger tag = HistoryMenuBridge::kVisited;
130 AddItemToMenu(menu, @"alpha", @selector(openHistoryMenuItem:), tag);
131 AddItemToMenu(menu, @"bravo", @selector(openHistoryMenuItem:), tag);
132 AddItemToMenu(menu, @"charlie", @selector(openHistoryMenuItem:), tag);
133 AddItemToMenu(menu, @"delta", @selector(openHistoryMenuItem:), tag);
135 ClearMenuSection(menu, HistoryMenuBridge::kVisited);
137 EXPECT_EQ(1, [menu numberOfItems]);
138 EXPECT_NSEQ(@"HEADER",
139 [[menu itemWithTag:HistoryMenuBridge::kVisitedTitle] title]);
142 // Skip menu items that are not hooked up to |-openHistoryMenuItem:|.
143 TEST_F(HistoryMenuBridgeTest, ClearHistoryMenuSkipping) {
144 NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"history foo"] autorelease];
145 AddItemToMenu(menu, @"HEADER", NULL, HistoryMenuBridge::kVisitedTitle);
147 NSInteger tag = HistoryMenuBridge::kVisited;
148 AddItemToMenu(menu, @"alpha", @selector(openHistoryMenuItem:), tag);
149 AddItemToMenu(menu, @"bravo", @selector(openHistoryMenuItem:), tag);
150 AddItemToMenu(menu, @"TITLE", NULL, HistoryMenuBridge::kRecentlyClosedTitle);
151 AddItemToMenu(menu, @"charlie", @selector(openHistoryMenuItem:), tag);
153 ClearMenuSection(menu, tag);
155 EXPECT_EQ(2, [menu numberOfItems]);
156 EXPECT_NSEQ(@"HEADER",
157 [[menu itemWithTag:HistoryMenuBridge::kVisitedTitle] title]);
158 EXPECT_NSEQ(@"TITLE",
159 [[menu itemAtIndex:1] title]);
162 // Edge case test for clearing an empty menu.
163 TEST_F(HistoryMenuBridgeTest, ClearHistoryMenuEmpty) {
164 NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"history foo"] autorelease];
165 AddItemToMenu(menu, @"HEADER", NULL, HistoryMenuBridge::kVisited);
167 ClearMenuSection(menu, HistoryMenuBridge::kVisited);
169 EXPECT_EQ(1, [menu numberOfItems]);
170 EXPECT_NSEQ(@"HEADER",
171 [[menu itemWithTag:HistoryMenuBridge::kVisited] title]);
174 // Test that AddItemToMenu() properly adds HistoryItem objects as menus.
175 TEST_F(HistoryMenuBridgeTest, AddItemToMenu) {
176 NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"history foo"] autorelease];
178 const base::string16 short_url = base::ASCIIToUTF16("http://foo/");
179 const base::string16 long_url = base::ASCIIToUTF16(
180 "http://super-duper-long-url--."
181 "that.cannot.possibly.fit.even-in-80-columns"
182 "or.be.reasonably-displayed-in-a-menu"
183 "without.looking-ridiculous.com/"); // 140 chars total
185 // HistoryItems are owned by the HistoryMenuBridge when AddItemToBridgeMenu()
186 // is called, which places them into the |menu_item_map_|, which owns them.
187 HistoryMenuBridge::HistoryItem* item1 = CreateItem(short_url);
188 AddItemToBridgeMenu(item1, menu, 100, 0);
190 HistoryMenuBridge::HistoryItem* item2 = CreateItem(long_url);
191 AddItemToBridgeMenu(item2, menu, 101, 1);
193 EXPECT_EQ(2, [menu numberOfItems]);
195 EXPECT_EQ(@selector(openHistoryMenuItem:), [[menu itemAtIndex:0] action]);
196 EXPECT_EQ(@selector(openHistoryMenuItem:), [[menu itemAtIndex:1] action]);
198 EXPECT_EQ(100, [[menu itemAtIndex:0] tag]);
199 EXPECT_EQ(101, [[menu itemAtIndex:1] tag]);
201 // Make sure a short title looks fine
202 NSString* s = [[menu itemAtIndex:0] title];
203 EXPECT_EQ(base::SysNSStringToUTF16(s), short_url);
205 // Make sure a super-long title gets trimmed
206 s = [[menu itemAtIndex:0] title];
207 EXPECT_TRUE([s length] < long_url.length());
209 // Confirm tooltips and confirm they are not trimmed (like the item
210 // name might be). Add tolerance for URL fixer-upping;
211 // e.g. http://foo becomes http://foo/)
212 EXPECT_GE([[[menu itemAtIndex:0] toolTip] length], (2*short_url.length()-5));
213 EXPECT_GE([[[menu itemAtIndex:1] toolTip] length], (2*long_url.length()-5));
216 // Test that the menu is created for a set of simple tabs.
217 TEST_F(HistoryMenuBridgeTest, RecentlyClosedTabs) {
218 scoped_ptr<MockTRS> trs(new MockTRS(profile()));
219 MockTRS::Entries entries;
221 MockTRS::Tab tab1 = CreateSessionTab("http://google.com", "Google");
223 entries.push_back(&tab1);
225 MockTRS::Tab tab2 = CreateSessionTab("http://apple.com", "Apple");
227 entries.push_back(&tab2);
229 using ::testing::ReturnRef;
230 EXPECT_CALL(*trs.get(), entries()).WillOnce(ReturnRef(entries));
232 bridge_->TabRestoreServiceChanged(trs.get());
234 NSMenu* menu = bridge_->HistoryMenu();
235 ASSERT_EQ(2U, [[menu itemArray] count]);
237 NSMenuItem* item1 = [menu itemAtIndex:0];
238 MockBridge::HistoryItem* hist1 = bridge_->HistoryItemForMenuItem(item1);
240 EXPECT_EQ(24, hist1->session_id);
241 EXPECT_NSEQ(@"Google", [item1 title]);
243 NSMenuItem* item2 = [menu itemAtIndex:1];
244 MockBridge::HistoryItem* hist2 = bridge_->HistoryItemForMenuItem(item2);
246 EXPECT_EQ(42, hist2->session_id);
247 EXPECT_NSEQ(@"Apple", [item2 title]);
250 // Test that the menu is created for a mix of windows and tabs.
251 TEST_F(HistoryMenuBridgeTest, RecentlyClosedTabsAndWindows) {
252 scoped_ptr<MockTRS> trs(new MockTRS(profile()));
253 MockTRS::Entries entries;
255 MockTRS::Tab tab1 = CreateSessionTab("http://google.com", "Google");
257 entries.push_back(&tab1);
259 MockTRS::Window win1;
261 win1.tabs.push_back(CreateSessionTab("http://foo.com", "foo"));
262 win1.tabs[0].id = 31;
263 win1.tabs.push_back(CreateSessionTab("http://bar.com", "bar"));
264 win1.tabs[1].id = 32;
265 entries.push_back(&win1);
267 MockTRS::Tab tab2 = CreateSessionTab("http://apple.com", "Apple");
269 entries.push_back(&tab2);
271 MockTRS::Window win2;
273 win2.tabs.push_back(CreateSessionTab("http://magic.com", "magic"));
274 win2.tabs[0].id = 51;
275 win2.tabs.push_back(CreateSessionTab("http://goats.com", "goats"));
276 win2.tabs[1].id = 52;
277 win2.tabs.push_back(CreateSessionTab("http://teleporter.com", "teleporter"));
278 win2.tabs[1].id = 53;
279 entries.push_back(&win2);
281 using ::testing::ReturnRef;
282 EXPECT_CALL(*trs.get(), entries()).WillOnce(ReturnRef(entries));
284 bridge_->TabRestoreServiceChanged(trs.get());
286 NSMenu* menu = bridge_->HistoryMenu();
287 ASSERT_EQ(4U, [[menu itemArray] count]);
289 NSMenuItem* item1 = [menu itemAtIndex:0];
290 MockBridge::HistoryItem* hist1 = bridge_->HistoryItemForMenuItem(item1);
292 EXPECT_EQ(24, hist1->session_id);
293 EXPECT_NSEQ(@"Google", [item1 title]);
295 NSMenuItem* item2 = [menu itemAtIndex:1];
296 MockBridge::HistoryItem* hist2 = bridge_->HistoryItemForMenuItem(item2);
298 EXPECT_EQ(30, hist2->session_id);
299 EXPECT_EQ(2U, hist2->tabs.size());
300 // Do not test menu item title because it is localized.
301 NSMenu* submenu1 = [item2 submenu];
302 EXPECT_EQ(4U, [[submenu1 itemArray] count]);
303 // Do not test Restore All Tabs because it is localiced.
304 EXPECT_TRUE([[submenu1 itemAtIndex:1] isSeparatorItem]);
305 EXPECT_NSEQ(@"foo", [[submenu1 itemAtIndex:2] title]);
306 EXPECT_NSEQ(@"bar", [[submenu1 itemAtIndex:3] title]);
308 NSMenuItem* item3 = [menu itemAtIndex:2];
309 MockBridge::HistoryItem* hist3 = bridge_->HistoryItemForMenuItem(item3);
311 EXPECT_EQ(42, hist3->session_id);
312 EXPECT_NSEQ(@"Apple", [item3 title]);
314 NSMenuItem* item4 = [menu itemAtIndex:3];
315 MockBridge::HistoryItem* hist4 = bridge_->HistoryItemForMenuItem(item4);
317 EXPECT_EQ(50, hist4->session_id);
318 EXPECT_EQ(3U, hist4->tabs.size());
319 // Do not test menu item title because it is localized.
320 NSMenu* submenu2 = [item4 submenu];
321 EXPECT_EQ(5U, [[submenu2 itemArray] count]);
322 // Do not test Restore All Tabs because it is localiced.
323 EXPECT_TRUE([[submenu2 itemAtIndex:1] isSeparatorItem]);
324 EXPECT_NSEQ(@"magic", [[submenu2 itemAtIndex:2] title]);
325 EXPECT_NSEQ(@"goats", [[submenu2 itemAtIndex:3] title]);
326 EXPECT_NSEQ(@"teleporter", [[submenu2 itemAtIndex:4] title]);
329 // Tests that we properly request an icon from the FaviconService.
330 TEST_F(HistoryMenuBridgeTest, GetFaviconForHistoryItem) {
331 // Create a fake item.
332 HistoryMenuBridge::HistoryItem item;
333 item.title = base::ASCIIToUTF16("Title");
334 item.url = GURL("http://google.com");
337 GetFaviconForHistoryItem(&item);
339 // Make sure the item was modified properly.
340 EXPECT_TRUE(item.icon_requested);
341 EXPECT_NE(CancelableTaskTracker::kBadTaskId, item.icon_task_id);
344 TEST_F(HistoryMenuBridgeTest, GotFaviconData) {
345 // Create a dummy bitmap.
347 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 25, 25);
348 bitmap.allocPixels();
349 bitmap.eraseRGB(255, 0, 0);
351 // Set up the HistoryItem.
352 HistoryMenuBridge::HistoryItem item;
353 item.menu_item.reset([[NSMenuItem alloc] init]);
354 GetFaviconForHistoryItem(&item);
356 // Pretend to be called back.
357 chrome::FaviconImageResult image_result;
358 image_result.image = gfx::Image::CreateFrom1xBitmap(bitmap);
359 GotFaviconData(&item, image_result);
361 // Make sure the callback works.
362 EXPECT_FALSE(item.icon_requested);
363 EXPECT_TRUE(item.icon.get());
364 EXPECT_TRUE([item.menu_item image]);