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/sessions/persistent_tab_restore_service.h"
15 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
16 #include "chrome/browser/ui/cocoa/history_menu_bridge.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "components/favicon_base/favicon_types.h"
19 #include "components/sessions/serialized_navigation_entry_test_helper.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #import "testing/gtest_mac.h"
23 #include "third_party/skia/include/core/SkBitmap.h"
24 #include "ui/gfx/codec/png_codec.h"
28 class MockTRS : public PersistentTabRestoreService {
30 MockTRS(Profile* profile) : PersistentTabRestoreService(profile, NULL) {}
31 MOCK_CONST_METHOD0(entries, const TabRestoreService::Entries&());
34 class MockBridge : public HistoryMenuBridge {
36 MockBridge(Profile* profile)
37 : HistoryMenuBridge(profile),
38 menu_([[NSMenu alloc] initWithTitle:@"History"]) {}
40 NSMenu* HistoryMenu() override { return menu_.get(); }
43 base::scoped_nsobject<NSMenu> menu_;
46 class HistoryMenuBridgeTest : public CocoaProfileTest {
48 void SetUp() override {
49 CocoaProfileTest::SetUp();
50 profile()->CreateFaviconService();
51 bridge_.reset(new MockBridge(profile()));
54 // We are a friend of HistoryMenuBridge (and have access to
55 // protected methods), but none of the classes generated by TEST_F()
56 // are. Wraps common commands.
57 void ClearMenuSection(NSMenu* menu,
59 bridge_->ClearMenuSection(menu, tag);
62 void AddItemToBridgeMenu(HistoryMenuBridge::HistoryItem* item,
66 bridge_->AddItemToMenu(item, menu, tag, index);
69 NSMenuItem* AddItemToMenu(NSMenu* menu,
73 NSMenuItem* item = [[[NSMenuItem alloc] initWithTitle:title action:NULL
74 keyEquivalent:@""] autorelease];
77 [item setAction:selector];
78 [item setTarget:bridge_->controller_.get()];
84 HistoryMenuBridge::HistoryItem* CreateItem(const base::string16& title) {
85 HistoryMenuBridge::HistoryItem* item =
86 new HistoryMenuBridge::HistoryItem();
88 item->url = GURL(title);
92 MockTRS::Tab CreateSessionTab(const std::string& url,
93 const std::string& title) {
95 tab.current_navigation_index = 0;
96 tab.navigations.push_back(
97 sessions::SerializedNavigationEntryTestHelper::CreateNavigation(
102 void GetFaviconForHistoryItem(HistoryMenuBridge::HistoryItem* item) {
103 bridge_->GetFaviconForHistoryItem(item);
106 void GotFaviconData(HistoryMenuBridge::HistoryItem* item,
107 const favicon_base::FaviconImageResult& image_result) {
108 bridge_->GotFaviconData(item, image_result);
111 scoped_ptr<MockBridge> bridge_;
114 // Edge case test for clearing until the end of a menu.
115 TEST_F(HistoryMenuBridgeTest, ClearHistoryMenuUntilEnd) {
116 NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"history foo"] autorelease];
117 AddItemToMenu(menu, @"HEADER", NULL, HistoryMenuBridge::kVisitedTitle);
119 NSInteger tag = HistoryMenuBridge::kVisited;
120 AddItemToMenu(menu, @"alpha", @selector(openHistoryMenuItem:), tag);
121 AddItemToMenu(menu, @"bravo", @selector(openHistoryMenuItem:), tag);
122 AddItemToMenu(menu, @"charlie", @selector(openHistoryMenuItem:), tag);
123 AddItemToMenu(menu, @"delta", @selector(openHistoryMenuItem:), tag);
125 ClearMenuSection(menu, HistoryMenuBridge::kVisited);
127 EXPECT_EQ(1, [menu numberOfItems]);
128 EXPECT_NSEQ(@"HEADER",
129 [[menu itemWithTag:HistoryMenuBridge::kVisitedTitle] title]);
132 // Skip menu items that are not hooked up to |-openHistoryMenuItem:|.
133 TEST_F(HistoryMenuBridgeTest, ClearHistoryMenuSkipping) {
134 NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"history foo"] autorelease];
135 AddItemToMenu(menu, @"HEADER", NULL, HistoryMenuBridge::kVisitedTitle);
137 NSInteger tag = HistoryMenuBridge::kVisited;
138 AddItemToMenu(menu, @"alpha", @selector(openHistoryMenuItem:), tag);
139 AddItemToMenu(menu, @"bravo", @selector(openHistoryMenuItem:), tag);
140 AddItemToMenu(menu, @"TITLE", NULL, HistoryMenuBridge::kRecentlyClosedTitle);
141 AddItemToMenu(menu, @"charlie", @selector(openHistoryMenuItem:), tag);
143 ClearMenuSection(menu, tag);
145 EXPECT_EQ(2, [menu numberOfItems]);
146 EXPECT_NSEQ(@"HEADER",
147 [[menu itemWithTag:HistoryMenuBridge::kVisitedTitle] title]);
148 EXPECT_NSEQ(@"TITLE",
149 [[menu itemAtIndex:1] title]);
152 // Edge case test for clearing an empty menu.
153 TEST_F(HistoryMenuBridgeTest, ClearHistoryMenuEmpty) {
154 NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"history foo"] autorelease];
155 AddItemToMenu(menu, @"HEADER", NULL, HistoryMenuBridge::kVisited);
157 ClearMenuSection(menu, HistoryMenuBridge::kVisited);
159 EXPECT_EQ(1, [menu numberOfItems]);
160 EXPECT_NSEQ(@"HEADER",
161 [[menu itemWithTag:HistoryMenuBridge::kVisited] title]);
164 // Test that AddItemToMenu() properly adds HistoryItem objects as menus.
165 TEST_F(HistoryMenuBridgeTest, AddItemToMenu) {
166 NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"history foo"] autorelease];
168 const base::string16 short_url = base::ASCIIToUTF16("http://foo/");
169 const base::string16 long_url = base::ASCIIToUTF16(
170 "http://super-duper-long-url--."
171 "that.cannot.possibly.fit.even-in-80-columns"
172 "or.be.reasonably-displayed-in-a-menu"
173 "without.looking-ridiculous.com/"); // 140 chars total
175 // HistoryItems are owned by the HistoryMenuBridge when AddItemToBridgeMenu()
176 // is called, which places them into the |menu_item_map_|, which owns them.
177 HistoryMenuBridge::HistoryItem* item1 = CreateItem(short_url);
178 AddItemToBridgeMenu(item1, menu, 100, 0);
180 HistoryMenuBridge::HistoryItem* item2 = CreateItem(long_url);
181 AddItemToBridgeMenu(item2, menu, 101, 1);
183 EXPECT_EQ(2, [menu numberOfItems]);
185 EXPECT_EQ(@selector(openHistoryMenuItem:), [[menu itemAtIndex:0] action]);
186 EXPECT_EQ(@selector(openHistoryMenuItem:), [[menu itemAtIndex:1] action]);
188 EXPECT_EQ(100, [[menu itemAtIndex:0] tag]);
189 EXPECT_EQ(101, [[menu itemAtIndex:1] tag]);
191 // Make sure a short title looks fine
192 NSString* s = [[menu itemAtIndex:0] title];
193 EXPECT_EQ(base::SysNSStringToUTF16(s), short_url);
195 // Make sure a super-long title gets trimmed
196 s = [[menu itemAtIndex:0] title];
197 EXPECT_TRUE([s length] < long_url.length());
199 // Confirm tooltips and confirm they are not trimmed (like the item
200 // name might be). Add tolerance for URL fixer-upping;
201 // e.g. http://foo becomes http://foo/)
202 EXPECT_GE([[[menu itemAtIndex:0] toolTip] length], (2*short_url.length()-5));
203 EXPECT_GE([[[menu itemAtIndex:1] toolTip] length], (2*long_url.length()-5));
206 // Test that the menu is created for a set of simple tabs.
207 TEST_F(HistoryMenuBridgeTest, RecentlyClosedTabs) {
208 scoped_ptr<MockTRS> trs(new MockTRS(profile()));
209 MockTRS::Entries entries;
211 MockTRS::Tab tab1 = CreateSessionTab("http://google.com", "Google");
213 entries.push_back(&tab1);
215 MockTRS::Tab tab2 = CreateSessionTab("http://apple.com", "Apple");
217 entries.push_back(&tab2);
219 using ::testing::ReturnRef;
220 EXPECT_CALL(*trs.get(), entries()).WillOnce(ReturnRef(entries));
222 bridge_->TabRestoreServiceChanged(trs.get());
224 NSMenu* menu = bridge_->HistoryMenu();
225 ASSERT_EQ(2U, [[menu itemArray] count]);
227 NSMenuItem* item1 = [menu itemAtIndex:0];
228 MockBridge::HistoryItem* hist1 = bridge_->HistoryItemForMenuItem(item1);
230 EXPECT_EQ(24, hist1->session_id);
231 EXPECT_NSEQ(@"Google", [item1 title]);
233 NSMenuItem* item2 = [menu itemAtIndex:1];
234 MockBridge::HistoryItem* hist2 = bridge_->HistoryItemForMenuItem(item2);
236 EXPECT_EQ(42, hist2->session_id);
237 EXPECT_NSEQ(@"Apple", [item2 title]);
240 // Test that the menu is created for a mix of windows and tabs.
241 TEST_F(HistoryMenuBridgeTest, RecentlyClosedTabsAndWindows) {
242 scoped_ptr<MockTRS> trs(new MockTRS(profile()));
243 MockTRS::Entries entries;
245 MockTRS::Tab tab1 = CreateSessionTab("http://google.com", "Google");
247 entries.push_back(&tab1);
249 MockTRS::Window win1;
251 win1.tabs.push_back(CreateSessionTab("http://foo.com", "foo"));
252 win1.tabs[0].id = 31;
253 win1.tabs.push_back(CreateSessionTab("http://bar.com", "bar"));
254 win1.tabs[1].id = 32;
255 entries.push_back(&win1);
257 MockTRS::Tab tab2 = CreateSessionTab("http://apple.com", "Apple");
259 entries.push_back(&tab2);
261 MockTRS::Window win2;
263 win2.tabs.push_back(CreateSessionTab("http://magic.com", "magic"));
264 win2.tabs[0].id = 51;
265 win2.tabs.push_back(CreateSessionTab("http://goats.com", "goats"));
266 win2.tabs[1].id = 52;
267 win2.tabs.push_back(CreateSessionTab("http://teleporter.com", "teleporter"));
268 win2.tabs[1].id = 53;
269 entries.push_back(&win2);
271 using ::testing::ReturnRef;
272 EXPECT_CALL(*trs.get(), entries()).WillOnce(ReturnRef(entries));
274 bridge_->TabRestoreServiceChanged(trs.get());
276 NSMenu* menu = bridge_->HistoryMenu();
277 ASSERT_EQ(4U, [[menu itemArray] count]);
279 NSMenuItem* item1 = [menu itemAtIndex:0];
280 MockBridge::HistoryItem* hist1 = bridge_->HistoryItemForMenuItem(item1);
282 EXPECT_EQ(24, hist1->session_id);
283 EXPECT_NSEQ(@"Google", [item1 title]);
285 NSMenuItem* item2 = [menu itemAtIndex:1];
286 MockBridge::HistoryItem* hist2 = bridge_->HistoryItemForMenuItem(item2);
288 EXPECT_EQ(30, hist2->session_id);
289 EXPECT_EQ(2U, hist2->tabs.size());
290 // Do not test menu item title because it is localized.
291 NSMenu* submenu1 = [item2 submenu];
292 EXPECT_EQ(4U, [[submenu1 itemArray] count]);
293 // Do not test Restore All Tabs because it is localiced.
294 EXPECT_TRUE([[submenu1 itemAtIndex:1] isSeparatorItem]);
295 EXPECT_NSEQ(@"foo", [[submenu1 itemAtIndex:2] title]);
296 EXPECT_NSEQ(@"bar", [[submenu1 itemAtIndex:3] title]);
298 NSMenuItem* item3 = [menu itemAtIndex:2];
299 MockBridge::HistoryItem* hist3 = bridge_->HistoryItemForMenuItem(item3);
301 EXPECT_EQ(42, hist3->session_id);
302 EXPECT_NSEQ(@"Apple", [item3 title]);
304 NSMenuItem* item4 = [menu itemAtIndex:3];
305 MockBridge::HistoryItem* hist4 = bridge_->HistoryItemForMenuItem(item4);
307 EXPECT_EQ(50, hist4->session_id);
308 EXPECT_EQ(3U, hist4->tabs.size());
309 // Do not test menu item title because it is localized.
310 NSMenu* submenu2 = [item4 submenu];
311 EXPECT_EQ(5U, [[submenu2 itemArray] count]);
312 // Do not test Restore All Tabs because it is localiced.
313 EXPECT_TRUE([[submenu2 itemAtIndex:1] isSeparatorItem]);
314 EXPECT_NSEQ(@"magic", [[submenu2 itemAtIndex:2] title]);
315 EXPECT_NSEQ(@"goats", [[submenu2 itemAtIndex:3] title]);
316 EXPECT_NSEQ(@"teleporter", [[submenu2 itemAtIndex:4] title]);
319 // Tests that we properly request an icon from the FaviconService.
320 TEST_F(HistoryMenuBridgeTest, GetFaviconForHistoryItem) {
321 // Create a fake item.
322 HistoryMenuBridge::HistoryItem item;
323 item.title = base::ASCIIToUTF16("Title");
324 item.url = GURL("http://google.com");
327 GetFaviconForHistoryItem(&item);
329 // Make sure the item was modified properly.
330 EXPECT_TRUE(item.icon_requested);
331 EXPECT_NE(base::CancelableTaskTracker::kBadTaskId, item.icon_task_id);
334 TEST_F(HistoryMenuBridgeTest, GotFaviconData) {
335 // Create a dummy bitmap.
337 bitmap.allocN32Pixels(25, 25);
338 bitmap.eraseARGB(255, 255, 0, 0);
340 // Set up the HistoryItem.
341 HistoryMenuBridge::HistoryItem item;
342 item.menu_item.reset([[NSMenuItem alloc] init]);
343 GetFaviconForHistoryItem(&item);
345 // Pretend to be called back.
346 favicon_base::FaviconImageResult image_result;
347 image_result.image = gfx::Image::CreateFrom1xBitmap(bitmap);
348 GotFaviconData(&item, image_result);
350 // Make sure the callback works.
351 EXPECT_FALSE(item.icon_requested);
352 EXPECT_TRUE(item.icon.get());
353 EXPECT_TRUE([item.menu_item image]);