Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(172)

Side by Side Diff: chrome/browser/ui/cocoa/history_menu_bridge_unittest.mm

Issue 2192253002: Refactor history menu tests for clearer setup (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Smaller refactor Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "chrome/browser/ui/cocoa/history_menu_bridge.h" 5 #include "chrome/browser/ui/cocoa/history_menu_bridge.h"
6 6
7 #import <Cocoa/Cocoa.h> 7 #import <Cocoa/Cocoa.h>
8 8
Robert Sesek 2016/08/02 15:06:27 #include <initializer_list>
Sidney San Martín 2016/08/02 16:27:14 Done. Is there a tool to help catch indirectly-inc
9 #include <memory> 9 #include <memory>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/memory/ref_counted_memory.h" 13 #include "base/memory/ref_counted_memory.h"
14 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
15 #include "base/strings/sys_string_conversions.h" 15 #include "base/strings/sys_string_conversions.h"
16 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
17 #include "chrome/app/chrome_command_ids.h" 17 #include "chrome/app/chrome_command_ids.h"
18 #include "chrome/browser/sessions/chrome_tab_restore_service_client.h" 18 #include "chrome/browser/sessions/chrome_tab_restore_service_client.h"
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 } 89 }
90 90
91 HistoryMenuBridge::HistoryItem* CreateItem(const base::string16& title) { 91 HistoryMenuBridge::HistoryItem* CreateItem(const base::string16& title) {
92 HistoryMenuBridge::HistoryItem* item = 92 HistoryMenuBridge::HistoryItem* item =
93 new HistoryMenuBridge::HistoryItem(); 93 new HistoryMenuBridge::HistoryItem();
94 item->title = title; 94 item->title = title;
95 item->url = GURL(title); 95 item->url = GURL(title);
96 return item; 96 return item;
97 } 97 }
98 98
99 MockTRS::Tab CreateSessionTab(const std::string& url, 99 MockTRS::Entries CreateSessionEntries(
100 const std::string& title) { 100 std::initializer_list<MockTRS::Entry*> l) {
101 MockTRS::Tab tab; 101 MockTRS::Entries ret;
102 tab.current_navigation_index = 0; 102 for (auto* entry : l) {
103 tab.navigations.push_back( 103 ret.emplace_back(entry);
104 sessions::SerializedNavigationEntryTestHelper::CreateNavigation( 104 }
105 url, title)); 105 return ret;
106 }
107
108 MockTRS::Tab* CreateSessionTab(SessionID::id_type id,
109 const std::string& url,
110 const std::string& title) {
111 auto tab = new MockTRS::Tab;
Robert Sesek 2016/08/02 15:06:27 Who owns this object? It doesn't look like anythin
Sidney San Martín 2016/08/02 16:27:14 Oof, good catch. `Entries` and `Window::tabs` both
112 tab->id = id;
113 tab->current_navigation_index = 0;
114 tab->navigations.push_back(
115 sessions::SerializedNavigationEntryTestHelper::CreateNavigation(url,
116 title));
106 return tab; 117 return tab;
107 } 118 }
108 119
120 MockTRS::Window* CreateSessionWindow(
121 SessionID::id_type id,
122 std::initializer_list<MockTRS::Tab*> tabs) {
123 auto window = new MockTRS::Window;
124 window->id = id;
125 window->tabs.reserve(tabs.size());
126 for (auto* tab : tabs) {
127 window->tabs.emplace_back(std::move(*tab));
128 }
129 return window;
130 }
131
109 void GetFaviconForHistoryItem(HistoryMenuBridge::HistoryItem* item) { 132 void GetFaviconForHistoryItem(HistoryMenuBridge::HistoryItem* item) {
110 bridge_->GetFaviconForHistoryItem(item); 133 bridge_->GetFaviconForHistoryItem(item);
111 } 134 }
112 135
113 void GotFaviconData(HistoryMenuBridge::HistoryItem* item, 136 void GotFaviconData(HistoryMenuBridge::HistoryItem* item,
114 const favicon_base::FaviconImageResult& image_result) { 137 const favicon_base::FaviconImageResult& image_result) {
115 bridge_->GotFaviconData(item, image_result); 138 bridge_->GotFaviconData(item, image_result);
116 } 139 }
117 140
118 std::unique_ptr<MockBridge> bridge_; 141 std::unique_ptr<MockBridge> bridge_;
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 // Confirm tooltips and confirm they are not trimmed (like the item 229 // Confirm tooltips and confirm they are not trimmed (like the item
207 // name might be). Add tolerance for URL fixer-upping; 230 // name might be). Add tolerance for URL fixer-upping;
208 // e.g. http://foo becomes http://foo/) 231 // e.g. http://foo becomes http://foo/)
209 EXPECT_GE([[[menu itemAtIndex:0] toolTip] length], (2*short_url.length()-5)); 232 EXPECT_GE([[[menu itemAtIndex:0] toolTip] length], (2*short_url.length()-5));
210 EXPECT_GE([[[menu itemAtIndex:1] toolTip] length], (2*long_url.length()-5)); 233 EXPECT_GE([[[menu itemAtIndex:1] toolTip] length], (2*long_url.length()-5));
211 } 234 }
212 235
213 // Test that the menu is created for a set of simple tabs. 236 // Test that the menu is created for a set of simple tabs.
214 TEST_F(HistoryMenuBridgeTest, RecentlyClosedTabs) { 237 TEST_F(HistoryMenuBridgeTest, RecentlyClosedTabs) {
215 std::unique_ptr<MockTRS> trs(new MockTRS(profile())); 238 std::unique_ptr<MockTRS> trs(new MockTRS(profile()));
216 MockTRS::Entries entries; 239 auto entries{CreateSessionEntries({
217 240 CreateSessionTab(24, "http://google.com", "Google"),
218 MockTRS::Tab tab1 = CreateSessionTab("http://google.com", "Google"); 241 CreateSessionTab(42, "http://apple.com", "Apple"),
219 tab1.id = 24; 242 })};
220 entries.push_back(&tab1);
221
222 MockTRS::Tab tab2 = CreateSessionTab("http://apple.com", "Apple");
223 tab2.id = 42;
224 entries.push_back(&tab2);
225 243
226 using ::testing::ReturnRef; 244 using ::testing::ReturnRef;
227 EXPECT_CALL(*trs.get(), entries()).WillOnce(ReturnRef(entries)); 245 EXPECT_CALL(*trs.get(), entries()).WillOnce(ReturnRef(entries));
228 246
229 bridge_->TabRestoreServiceChanged(trs.get()); 247 bridge_->TabRestoreServiceChanged(trs.get());
230 248
231 NSMenu* menu = bridge_->HistoryMenu(); 249 NSMenu* menu = bridge_->HistoryMenu();
232 ASSERT_EQ(2U, [[menu itemArray] count]); 250 ASSERT_EQ(2U, [[menu itemArray] count]);
233 251
234 NSMenuItem* item1 = [menu itemAtIndex:0]; 252 NSMenuItem* item1 = [menu itemAtIndex:0];
235 MockBridge::HistoryItem* hist1 = bridge_->HistoryItemForMenuItem(item1); 253 MockBridge::HistoryItem* hist1 = bridge_->HistoryItemForMenuItem(item1);
236 EXPECT_TRUE(hist1); 254 EXPECT_TRUE(hist1);
237 EXPECT_EQ(24, hist1->session_id); 255 EXPECT_EQ(24, hist1->session_id);
238 EXPECT_NSEQ(@"Google", [item1 title]); 256 EXPECT_NSEQ(@"Google", [item1 title]);
239 257
240 NSMenuItem* item2 = [menu itemAtIndex:1]; 258 NSMenuItem* item2 = [menu itemAtIndex:1];
241 MockBridge::HistoryItem* hist2 = bridge_->HistoryItemForMenuItem(item2); 259 MockBridge::HistoryItem* hist2 = bridge_->HistoryItemForMenuItem(item2);
242 EXPECT_TRUE(hist2); 260 EXPECT_TRUE(hist2);
243 EXPECT_EQ(42, hist2->session_id); 261 EXPECT_EQ(42, hist2->session_id);
244 EXPECT_NSEQ(@"Apple", [item2 title]); 262 EXPECT_NSEQ(@"Apple", [item2 title]);
245 } 263 }
246 264
247 // Test that the menu is created for a mix of windows and tabs. 265 // Test that the menu is created for a mix of windows and tabs.
248 TEST_F(HistoryMenuBridgeTest, RecentlyClosedTabsAndWindows) { 266 TEST_F(HistoryMenuBridgeTest, RecentlyClosedTabsAndWindows) {
249 std::unique_ptr<MockTRS> trs(new MockTRS(profile())); 267 std::unique_ptr<MockTRS> trs(new MockTRS(profile()));
250 MockTRS::Entries entries; 268 auto entries{CreateSessionEntries({
251 269 CreateSessionTab(24, "http://google.com", "Google"),
252 MockTRS::Tab tab1 = CreateSessionTab("http://google.com", "Google"); 270 CreateSessionWindow(30, {
253 tab1.id = 24; 271 CreateSessionTab(31, "http://foo.com", "foo"),
254 entries.push_back(&tab1); 272 CreateSessionTab(32, "http://bar.com", "bar"),
255 273 }),
256 // TODO(sdy): The tab ids below *must* be set after all of them have been 274 CreateSessionTab(42, "http://apple.com", "Apple"),
257 // pushed onto window.tabs. Otherwise, the ids will change when push_back 275 CreateSessionWindow(50, {
258 // reallocates the vector's storage and calls each tabs' copy ctor. Ugh. 276 CreateSessionTab(51, "http://magic.com", "magic"),
259 277 CreateSessionTab(52, "http://goats.com", "goats"),
260 MockTRS::Window win1; 278 CreateSessionTab(53, "http://teleporter.com", "teleporter"),
261 win1.id = 30; 279 }),
262 win1.tabs.push_back(CreateSessionTab("http://foo.com", "foo")); 280 })};
263 win1.tabs.push_back(CreateSessionTab("http://bar.com", "bar"));
264 win1.tabs[0].id = 31;
265 win1.tabs[1].id = 32;
266 entries.push_back(&win1);
267
268 MockTRS::Tab tab2 = CreateSessionTab("http://apple.com", "Apple");
269 tab2.id = 42;
270 entries.push_back(&tab2);
271
272 MockTRS::Window win2;
273 win2.id = 50;
274 win2.tabs.push_back(CreateSessionTab("http://magic.com", "magic"));
275 win2.tabs.push_back(CreateSessionTab("http://goats.com", "goats"));
276 win2.tabs.push_back(CreateSessionTab("http://teleporter.com", "teleporter"));
277 win2.tabs[0].id = 51;
278 win2.tabs[1].id = 52;
279 win2.tabs[2].id = 53;
280 entries.push_back(&win2);
281 281
282 using ::testing::ReturnRef; 282 using ::testing::ReturnRef;
283 EXPECT_CALL(*trs.get(), entries()).WillOnce(ReturnRef(entries)); 283 EXPECT_CALL(*trs.get(), entries()).WillOnce(ReturnRef(entries));
284 284
285 bridge_->TabRestoreServiceChanged(trs.get()); 285 bridge_->TabRestoreServiceChanged(trs.get());
286 286
287 NSMenu* menu = bridge_->HistoryMenu(); 287 NSMenu* menu = bridge_->HistoryMenu();
288 ASSERT_EQ(4U, [[menu itemArray] count]); 288 ASSERT_EQ(4U, [[menu itemArray] count]);
289 289
290 NSMenuItem* item1 = [menu itemAtIndex:0]; 290 NSMenuItem* item1 = [menu itemAtIndex:0];
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
363 image_result.image = gfx::Image::CreateFrom1xBitmap(bitmap); 363 image_result.image = gfx::Image::CreateFrom1xBitmap(bitmap);
364 GotFaviconData(&item, image_result); 364 GotFaviconData(&item, image_result);
365 365
366 // Make sure the callback works. 366 // Make sure the callback works.
367 EXPECT_FALSE(item.icon_requested); 367 EXPECT_FALSE(item.icon_requested);
368 EXPECT_TRUE(item.icon.get()); 368 EXPECT_TRUE(item.icon.get());
369 EXPECT_TRUE([item.menu_item image]); 369 EXPECT_TRUE([item.menu_item image]);
370 } 370 }
371 371
372 } // namespace 372 } // namespace
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698