BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / history_menu_bridge_unittest.mm
blob42655ed6778711f3226d887ef8b9f17f151623cd
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>
6 #include <vector>
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/chrome_tab_restore_service_client.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/core/persistent_tab_restore_service.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"
27 namespace {
29 class MockTRS : public sessions::PersistentTabRestoreService {
30  public:
31   MockTRS(Profile* profile)
32       : sessions::PersistentTabRestoreService(
33             make_scoped_ptr(new ChromeTabRestoreServiceClient(profile)),
34             nullptr) {}
35   MOCK_CONST_METHOD0(entries, const sessions::TabRestoreService::Entries&());
38 class MockBridge : public HistoryMenuBridge {
39  public:
40   MockBridge(Profile* profile)
41       : HistoryMenuBridge(profile),
42         menu_([[NSMenu alloc] initWithTitle:@"History"]) {}
44   NSMenu* HistoryMenu() override { return menu_.get(); }
46  private:
47   base::scoped_nsobject<NSMenu> menu_;
50 class HistoryMenuBridgeTest : public CocoaProfileTest {
51  public:
52   void SetUp() override {
53     CocoaProfileTest::SetUp();
54     profile()->CreateFaviconService();
55     bridge_.reset(new MockBridge(profile()));
56   }
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,
62                         NSInteger tag) {
63     bridge_->ClearMenuSection(menu, tag);
64   }
66   void AddItemToBridgeMenu(HistoryMenuBridge::HistoryItem* item,
67                            NSMenu* menu,
68                            NSInteger tag,
69                            NSInteger index) {
70     bridge_->AddItemToMenu(item, menu, tag, index);
71   }
73   NSMenuItem* AddItemToMenu(NSMenu* menu,
74                             NSString* title,
75                             SEL selector,
76                             int tag) {
77     NSMenuItem* item = [[[NSMenuItem alloc] initWithTitle:title action:NULL
78                                             keyEquivalent:@""] autorelease];
79     [item setTag:tag];
80     if (selector) {
81       [item setAction:selector];
82       [item setTarget:bridge_->controller_.get()];
83     }
84     [menu addItem:item];
85     return item;
86   }
88   HistoryMenuBridge::HistoryItem* CreateItem(const base::string16& title) {
89     HistoryMenuBridge::HistoryItem* item =
90         new HistoryMenuBridge::HistoryItem();
91     item->title = title;
92     item->url = GURL(title);
93     return item;
94   }
96   MockTRS::Tab CreateSessionTab(const std::string& url,
97                                 const std::string& title) {
98     MockTRS::Tab tab;
99     tab.current_navigation_index = 0;
100     tab.navigations.push_back(
101         sessions::SerializedNavigationEntryTestHelper::CreateNavigation(
102             url, title));
103     return tab;
104   }
106   void GetFaviconForHistoryItem(HistoryMenuBridge::HistoryItem* item) {
107     bridge_->GetFaviconForHistoryItem(item);
108   }
110   void GotFaviconData(HistoryMenuBridge::HistoryItem* item,
111                       const favicon_base::FaviconImageResult& image_result) {
112     bridge_->GotFaviconData(item, image_result);
113   }
115   scoped_ptr<MockBridge> bridge_;
118 // Edge case test for clearing until the end of a menu.
119 TEST_F(HistoryMenuBridgeTest, ClearHistoryMenuUntilEnd) {
120   NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"history foo"] autorelease];
121   AddItemToMenu(menu, @"HEADER", NULL, HistoryMenuBridge::kVisitedTitle);
123   NSInteger tag = HistoryMenuBridge::kVisited;
124   AddItemToMenu(menu, @"alpha", @selector(openHistoryMenuItem:), tag);
125   AddItemToMenu(menu, @"bravo", @selector(openHistoryMenuItem:), tag);
126   AddItemToMenu(menu, @"charlie", @selector(openHistoryMenuItem:), tag);
127   AddItemToMenu(menu, @"delta", @selector(openHistoryMenuItem:), tag);
129   ClearMenuSection(menu, HistoryMenuBridge::kVisited);
131   EXPECT_EQ(1, [menu numberOfItems]);
132   EXPECT_NSEQ(@"HEADER",
133       [[menu itemWithTag:HistoryMenuBridge::kVisitedTitle] title]);
136 // Skip menu items that are not hooked up to |-openHistoryMenuItem:|.
137 TEST_F(HistoryMenuBridgeTest, ClearHistoryMenuSkipping) {
138   NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"history foo"] autorelease];
139   AddItemToMenu(menu, @"HEADER", NULL, HistoryMenuBridge::kVisitedTitle);
141   NSInteger tag = HistoryMenuBridge::kVisited;
142   AddItemToMenu(menu, @"alpha", @selector(openHistoryMenuItem:), tag);
143   AddItemToMenu(menu, @"bravo", @selector(openHistoryMenuItem:), tag);
144   AddItemToMenu(menu, @"TITLE", NULL, HistoryMenuBridge::kRecentlyClosedTitle);
145   AddItemToMenu(menu, @"charlie", @selector(openHistoryMenuItem:), tag);
147   ClearMenuSection(menu, tag);
149   EXPECT_EQ(2, [menu numberOfItems]);
150   EXPECT_NSEQ(@"HEADER",
151       [[menu itemWithTag:HistoryMenuBridge::kVisitedTitle] title]);
152   EXPECT_NSEQ(@"TITLE",
153       [[menu itemAtIndex:1] title]);
156 // Edge case test for clearing an empty menu.
157 TEST_F(HistoryMenuBridgeTest, ClearHistoryMenuEmpty) {
158   NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"history foo"] autorelease];
159   AddItemToMenu(menu, @"HEADER", NULL, HistoryMenuBridge::kVisited);
161   ClearMenuSection(menu, HistoryMenuBridge::kVisited);
163   EXPECT_EQ(1, [menu numberOfItems]);
164   EXPECT_NSEQ(@"HEADER",
165       [[menu itemWithTag:HistoryMenuBridge::kVisited] title]);
168 // Test that AddItemToMenu() properly adds HistoryItem objects as menus.
169 TEST_F(HistoryMenuBridgeTest, AddItemToMenu) {
170   NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"history foo"] autorelease];
172   const base::string16 short_url = base::ASCIIToUTF16("http://foo/");
173   const base::string16 long_url = base::ASCIIToUTF16(
174       "http://super-duper-long-url--."
175       "that.cannot.possibly.fit.even-in-80-columns"
176       "or.be.reasonably-displayed-in-a-menu"
177       "without.looking-ridiculous.com/"); // 140 chars total
179   // HistoryItems are owned by the HistoryMenuBridge when AddItemToBridgeMenu()
180   // is called, which places them into the |menu_item_map_|, which owns them.
181   HistoryMenuBridge::HistoryItem* item1 = CreateItem(short_url);
182   AddItemToBridgeMenu(item1, menu, 100, 0);
184   HistoryMenuBridge::HistoryItem* item2 = CreateItem(long_url);
185   AddItemToBridgeMenu(item2, menu, 101, 1);
187   EXPECT_EQ(2, [menu numberOfItems]);
189   EXPECT_EQ(@selector(openHistoryMenuItem:), [[menu itemAtIndex:0] action]);
190   EXPECT_EQ(@selector(openHistoryMenuItem:), [[menu itemAtIndex:1] action]);
192   EXPECT_EQ(100, [[menu itemAtIndex:0] tag]);
193   EXPECT_EQ(101, [[menu itemAtIndex:1] tag]);
195   // Make sure a short title looks fine
196   NSString* s = [[menu itemAtIndex:0] title];
197   EXPECT_EQ(base::SysNSStringToUTF16(s), short_url);
199   // Make sure a super-long title gets trimmed
200   s = [[menu itemAtIndex:0] title];
201   EXPECT_TRUE([s length] < long_url.length());
203   // Confirm tooltips and confirm they are not trimmed (like the item
204   // name might be).  Add tolerance for URL fixer-upping;
205   // e.g. http://foo becomes http://foo/)
206   EXPECT_GE([[[menu itemAtIndex:0] toolTip] length], (2*short_url.length()-5));
207   EXPECT_GE([[[menu itemAtIndex:1] toolTip] length], (2*long_url.length()-5));
210 // Test that the menu is created for a set of simple tabs.
211 TEST_F(HistoryMenuBridgeTest, RecentlyClosedTabs) {
212   scoped_ptr<MockTRS> trs(new MockTRS(profile()));
213   MockTRS::Entries entries;
215   MockTRS::Tab tab1 = CreateSessionTab("http://google.com", "Google");
216   tab1.id = 24;
217   entries.push_back(&tab1);
219   MockTRS::Tab tab2 = CreateSessionTab("http://apple.com", "Apple");
220   tab2.id = 42;
221   entries.push_back(&tab2);
223   using ::testing::ReturnRef;
224   EXPECT_CALL(*trs.get(), entries()).WillOnce(ReturnRef(entries));
226   bridge_->TabRestoreServiceChanged(trs.get());
228   NSMenu* menu = bridge_->HistoryMenu();
229   ASSERT_EQ(2U, [[menu itemArray] count]);
231   NSMenuItem* item1 = [menu itemAtIndex:0];
232   MockBridge::HistoryItem* hist1 = bridge_->HistoryItemForMenuItem(item1);
233   EXPECT_TRUE(hist1);
234   EXPECT_EQ(24, hist1->session_id);
235   EXPECT_NSEQ(@"Google", [item1 title]);
237   NSMenuItem* item2 = [menu itemAtIndex:1];
238   MockBridge::HistoryItem* hist2 = bridge_->HistoryItemForMenuItem(item2);
239   EXPECT_TRUE(hist2);
240   EXPECT_EQ(42, hist2->session_id);
241   EXPECT_NSEQ(@"Apple", [item2 title]);
244 // Test that the menu is created for a mix of windows and tabs.
245 TEST_F(HistoryMenuBridgeTest, RecentlyClosedTabsAndWindows) {
246   scoped_ptr<MockTRS> trs(new MockTRS(profile()));
247   MockTRS::Entries entries;
249   MockTRS::Tab tab1 = CreateSessionTab("http://google.com", "Google");
250   tab1.id = 24;
251   entries.push_back(&tab1);
253   MockTRS::Window win1;
254   win1.id = 30;
255   win1.tabs.push_back(CreateSessionTab("http://foo.com", "foo"));
256   win1.tabs[0].id = 31;
257   win1.tabs.push_back(CreateSessionTab("http://bar.com", "bar"));
258   win1.tabs[1].id = 32;
259   entries.push_back(&win1);
261   MockTRS::Tab tab2 = CreateSessionTab("http://apple.com", "Apple");
262   tab2.id = 42;
263   entries.push_back(&tab2);
265   MockTRS::Window win2;
266   win2.id = 50;
267   win2.tabs.push_back(CreateSessionTab("http://magic.com", "magic"));
268   win2.tabs[0].id = 51;
269   win2.tabs.push_back(CreateSessionTab("http://goats.com", "goats"));
270   win2.tabs[1].id = 52;
271   win2.tabs.push_back(CreateSessionTab("http://teleporter.com", "teleporter"));
272   win2.tabs[1].id = 53;
273   entries.push_back(&win2);
275   using ::testing::ReturnRef;
276   EXPECT_CALL(*trs.get(), entries()).WillOnce(ReturnRef(entries));
278   bridge_->TabRestoreServiceChanged(trs.get());
280   NSMenu* menu = bridge_->HistoryMenu();
281   ASSERT_EQ(4U, [[menu itemArray] count]);
283   NSMenuItem* item1 = [menu itemAtIndex:0];
284   MockBridge::HistoryItem* hist1 = bridge_->HistoryItemForMenuItem(item1);
285   EXPECT_TRUE(hist1);
286   EXPECT_EQ(24, hist1->session_id);
287   EXPECT_NSEQ(@"Google", [item1 title]);
289   NSMenuItem* item2 = [menu itemAtIndex:1];
290   MockBridge::HistoryItem* hist2 = bridge_->HistoryItemForMenuItem(item2);
291   EXPECT_TRUE(hist2);
292   EXPECT_EQ(30, hist2->session_id);
293   EXPECT_EQ(2U, hist2->tabs.size());
294   // Do not test menu item title because it is localized.
295   NSMenu* submenu1 = [item2 submenu];
296   EXPECT_EQ(4U, [[submenu1 itemArray] count]);
297   // Do not test Restore All Tabs because it is localiced.
298   EXPECT_TRUE([[submenu1 itemAtIndex:1] isSeparatorItem]);
299   EXPECT_NSEQ(@"foo", [[submenu1 itemAtIndex:2] title]);
300   EXPECT_NSEQ(@"bar", [[submenu1 itemAtIndex:3] title]);
302   NSMenuItem* item3 = [menu itemAtIndex:2];
303   MockBridge::HistoryItem* hist3 = bridge_->HistoryItemForMenuItem(item3);
304   EXPECT_TRUE(hist3);
305   EXPECT_EQ(42, hist3->session_id);
306   EXPECT_NSEQ(@"Apple", [item3 title]);
308   NSMenuItem* item4 = [menu itemAtIndex:3];
309   MockBridge::HistoryItem* hist4 = bridge_->HistoryItemForMenuItem(item4);
310   EXPECT_TRUE(hist4);
311   EXPECT_EQ(50, hist4->session_id);
312   EXPECT_EQ(3U, hist4->tabs.size());
313   // Do not test menu item title because it is localized.
314   NSMenu* submenu2 = [item4 submenu];
315   EXPECT_EQ(5U, [[submenu2 itemArray] count]);
316   // Do not test Restore All Tabs because it is localiced.
317   EXPECT_TRUE([[submenu2 itemAtIndex:1] isSeparatorItem]);
318   EXPECT_NSEQ(@"magic", [[submenu2 itemAtIndex:2] title]);
319   EXPECT_NSEQ(@"goats", [[submenu2 itemAtIndex:3] title]);
320   EXPECT_NSEQ(@"teleporter", [[submenu2 itemAtIndex:4] title]);
323 // Tests that we properly request an icon from the FaviconService.
324 TEST_F(HistoryMenuBridgeTest, GetFaviconForHistoryItem) {
325   // Create a fake item.
326   HistoryMenuBridge::HistoryItem item;
327   item.title = base::ASCIIToUTF16("Title");
328   item.url = GURL("http://google.com");
330   // Request the icon.
331   GetFaviconForHistoryItem(&item);
333   // Make sure the item was modified properly.
334   EXPECT_TRUE(item.icon_requested);
335   EXPECT_NE(base::CancelableTaskTracker::kBadTaskId, item.icon_task_id);
338 TEST_F(HistoryMenuBridgeTest, GotFaviconData) {
339   // Create a dummy bitmap.
340   SkBitmap bitmap;
341   bitmap.allocN32Pixels(25, 25);
342   bitmap.eraseARGB(255, 255, 0, 0);
344   // Set up the HistoryItem.
345   HistoryMenuBridge::HistoryItem item;
346   item.menu_item.reset([[NSMenuItem alloc] init]);
347   GetFaviconForHistoryItem(&item);
349   // Pretend to be called back.
350   favicon_base::FaviconImageResult image_result;
351   image_result.image = gfx::Image::CreateFrom1xBitmap(bitmap);
352   GotFaviconData(&item, image_result);
354   // Make sure the callback works.
355   EXPECT_FALSE(item.icon_requested);
356   EXPECT_TRUE(item.icon.get());
357   EXPECT_TRUE([item.menu_item image]);
360 }  // namespace