Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 |
| 9 #include <initializer_list> | |
| 9 #include <memory> | 10 #include <memory> |
| 10 #include <vector> | 11 #include <vector> |
| 11 | 12 |
| 12 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 13 #include "base/memory/ref_counted_memory.h" | 14 #include "base/memory/ref_counted_memory.h" |
| 14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 15 #include "base/strings/sys_string_conversions.h" | 16 #include "base/strings/sys_string_conversions.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 17 #include "chrome/app/chrome_command_ids.h" | 18 #include "chrome/app/chrome_command_ids.h" |
| 18 #include "chrome/browser/sessions/chrome_tab_restore_service_client.h" | 19 #include "chrome/browser/sessions/chrome_tab_restore_service_client.h" |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 } | 90 } |
| 90 | 91 |
| 91 HistoryMenuBridge::HistoryItem* CreateItem(const base::string16& title) { | 92 HistoryMenuBridge::HistoryItem* CreateItem(const base::string16& title) { |
| 92 HistoryMenuBridge::HistoryItem* item = | 93 HistoryMenuBridge::HistoryItem* item = |
| 93 new HistoryMenuBridge::HistoryItem(); | 94 new HistoryMenuBridge::HistoryItem(); |
| 94 item->title = title; | 95 item->title = title; |
| 95 item->url = GURL(title); | 96 item->url = GURL(title); |
| 96 return item; | 97 return item; |
| 97 } | 98 } |
| 98 | 99 |
| 99 MockTRS::Tab CreateSessionTab(const std::string& url, | 100 MockTRS::Entries CreateSessionEntries( |
|
Robert Sesek
2016/08/02 18:47:38
Rather than this being a "Create" function, this c
| |
| 100 const std::string& title) { | 101 std::vector<std::unique_ptr<MockTRS::Entry>>* out, |
| 101 MockTRS::Tab tab; | 102 std::initializer_list<MockTRS::Entry*> l) { |
|
Robert Sesek
2016/08/02 18:47:38
naming: something more descriptive than |l|
Sidney San Martín
2016/08/02 19:47:34
Sure. I picked `l` because I remember seeing it as
| |
| 102 tab.current_navigation_index = 0; | 103 MockTRS::Entries ret; |
| 103 tab.navigations.push_back( | 104 out->reserve(out->size() + l.size()); |
| 104 sessions::SerializedNavigationEntryTestHelper::CreateNavigation( | 105 for (auto* entry : l) { |
| 105 url, title)); | 106 ret.emplace_back(entry); |
| 107 out->emplace_back(entry); | |
| 108 } | |
| 109 return ret; | |
| 110 } | |
| 111 | |
| 112 MockTRS::Tab* CreateSessionTab(SessionID::id_type id, | |
|
Robert Sesek
2016/08/02 18:47:38
This could return a unique_ptr<MockTRS::Tab>.
| |
| 113 const std::string& url, | |
| 114 const std::string& title) { | |
| 115 auto tab = new MockTRS::Tab; | |
| 116 tab->id = id; | |
| 117 tab->current_navigation_index = 0; | |
| 118 tab->navigations.push_back( | |
| 119 sessions::SerializedNavigationEntryTestHelper::CreateNavigation(url, | |
| 120 title)); | |
| 106 return tab; | 121 return tab; |
| 107 } | 122 } |
| 108 | 123 |
| 124 MockTRS::Window* CreateSessionWindow( | |
| 125 SessionID::id_type id, | |
| 126 std::initializer_list<MockTRS::Tab*> tabs) { | |
| 127 auto window = new MockTRS::Window; | |
| 128 window->id = id; | |
| 129 window->tabs.reserve(tabs.size()); | |
| 130 for (auto* tab : tabs) { | |
| 131 window->tabs.emplace_back(std::move(*tab)); | |
|
Robert Sesek
2016/08/02 18:47:38
This line is very hard to scrutinize (I've read it
| |
| 132 delete tab; | |
|
Robert Sesek
2016/08/02 18:47:38
... so that you don't need to manually delete here
| |
| 133 } | |
| 134 return window; | |
| 135 } | |
| 136 | |
| 109 void GetFaviconForHistoryItem(HistoryMenuBridge::HistoryItem* item) { | 137 void GetFaviconForHistoryItem(HistoryMenuBridge::HistoryItem* item) { |
| 110 bridge_->GetFaviconForHistoryItem(item); | 138 bridge_->GetFaviconForHistoryItem(item); |
| 111 } | 139 } |
| 112 | 140 |
| 113 void GotFaviconData(HistoryMenuBridge::HistoryItem* item, | 141 void GotFaviconData(HistoryMenuBridge::HistoryItem* item, |
| 114 const favicon_base::FaviconImageResult& image_result) { | 142 const favicon_base::FaviconImageResult& image_result) { |
| 115 bridge_->GotFaviconData(item, image_result); | 143 bridge_->GotFaviconData(item, image_result); |
| 116 } | 144 } |
| 117 | 145 |
| 118 std::unique_ptr<MockBridge> bridge_; | 146 std::unique_ptr<MockBridge> bridge_; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 206 // Confirm tooltips and confirm they are not trimmed (like the item | 234 // Confirm tooltips and confirm they are not trimmed (like the item |
| 207 // name might be). Add tolerance for URL fixer-upping; | 235 // name might be). Add tolerance for URL fixer-upping; |
| 208 // e.g. http://foo becomes http://foo/) | 236 // e.g. http://foo becomes http://foo/) |
| 209 EXPECT_GE([[[menu itemAtIndex:0] toolTip] length], (2*short_url.length()-5)); | 237 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)); | 238 EXPECT_GE([[[menu itemAtIndex:1] toolTip] length], (2*long_url.length()-5)); |
| 211 } | 239 } |
| 212 | 240 |
| 213 // Test that the menu is created for a set of simple tabs. | 241 // Test that the menu is created for a set of simple tabs. |
| 214 TEST_F(HistoryMenuBridgeTest, RecentlyClosedTabs) { | 242 TEST_F(HistoryMenuBridgeTest, RecentlyClosedTabs) { |
| 215 std::unique_ptr<MockTRS> trs(new MockTRS(profile())); | 243 std::unique_ptr<MockTRS> trs(new MockTRS(profile())); |
| 216 MockTRS::Entries entries; | 244 std::vector<std::unique_ptr<MockTRS::Entry>> holdEntries; |
| 217 | 245 auto entries{CreateSessionEntries(&holdEntries, { |
| 218 MockTRS::Tab tab1 = CreateSessionTab("http://google.com", "Google"); | 246 CreateSessionTab(24, "http://google.com", "Google"), |
| 219 tab1.id = 24; | 247 CreateSessionTab(42, "http://apple.com", "Apple"), |
| 220 entries.push_back(&tab1); | 248 })}; |
| 221 | |
| 222 MockTRS::Tab tab2 = CreateSessionTab("http://apple.com", "Apple"); | |
| 223 tab2.id = 42; | |
| 224 entries.push_back(&tab2); | |
| 225 | 249 |
| 226 using ::testing::ReturnRef; | 250 using ::testing::ReturnRef; |
| 227 EXPECT_CALL(*trs.get(), entries()).WillOnce(ReturnRef(entries)); | 251 EXPECT_CALL(*trs.get(), entries()).WillOnce(ReturnRef(entries)); |
| 228 | 252 |
| 229 bridge_->TabRestoreServiceChanged(trs.get()); | 253 bridge_->TabRestoreServiceChanged(trs.get()); |
| 230 | 254 |
| 231 NSMenu* menu = bridge_->HistoryMenu(); | 255 NSMenu* menu = bridge_->HistoryMenu(); |
| 232 ASSERT_EQ(2U, [[menu itemArray] count]); | 256 ASSERT_EQ(2U, [[menu itemArray] count]); |
| 233 | 257 |
| 234 NSMenuItem* item1 = [menu itemAtIndex:0]; | 258 NSMenuItem* item1 = [menu itemAtIndex:0]; |
| 235 MockBridge::HistoryItem* hist1 = bridge_->HistoryItemForMenuItem(item1); | 259 MockBridge::HistoryItem* hist1 = bridge_->HistoryItemForMenuItem(item1); |
| 236 EXPECT_TRUE(hist1); | 260 EXPECT_TRUE(hist1); |
| 237 EXPECT_EQ(24, hist1->session_id); | 261 EXPECT_EQ(24, hist1->session_id); |
| 238 EXPECT_NSEQ(@"Google", [item1 title]); | 262 EXPECT_NSEQ(@"Google", [item1 title]); |
| 239 | 263 |
| 240 NSMenuItem* item2 = [menu itemAtIndex:1]; | 264 NSMenuItem* item2 = [menu itemAtIndex:1]; |
| 241 MockBridge::HistoryItem* hist2 = bridge_->HistoryItemForMenuItem(item2); | 265 MockBridge::HistoryItem* hist2 = bridge_->HistoryItemForMenuItem(item2); |
| 242 EXPECT_TRUE(hist2); | 266 EXPECT_TRUE(hist2); |
| 243 EXPECT_EQ(42, hist2->session_id); | 267 EXPECT_EQ(42, hist2->session_id); |
| 244 EXPECT_NSEQ(@"Apple", [item2 title]); | 268 EXPECT_NSEQ(@"Apple", [item2 title]); |
| 245 } | 269 } |
| 246 | 270 |
| 247 // Test that the menu is created for a mix of windows and tabs. | 271 // Test that the menu is created for a mix of windows and tabs. |
| 248 TEST_F(HistoryMenuBridgeTest, RecentlyClosedTabsAndWindows) { | 272 TEST_F(HistoryMenuBridgeTest, RecentlyClosedTabsAndWindows) { |
| 249 std::unique_ptr<MockTRS> trs(new MockTRS(profile())); | 273 std::unique_ptr<MockTRS> trs(new MockTRS(profile())); |
| 250 MockTRS::Entries entries; | 274 std::vector<std::unique_ptr<MockTRS::Entry>> holdEntries; |
|
Robert Sesek
2016/08/02 18:47:38
naming: hold_entries
Sidney San Martín
2016/08/02 19:47:34
Done.
| |
| 251 | 275 auto entries{CreateSessionEntries(&holdEntries, { |
| 252 MockTRS::Tab tab1 = CreateSessionTab("http://google.com", "Google"); | 276 CreateSessionTab(24, "http://google.com", "Google"), |
| 253 tab1.id = 24; | 277 CreateSessionWindow(30, { |
| 254 entries.push_back(&tab1); | 278 CreateSessionTab(31, "http://foo.com", "foo"), |
| 255 | 279 CreateSessionTab(32, "http://bar.com", "bar"), |
| 256 // TODO(sdy): The tab ids below *must* be set after all of them have been | 280 }), |
| 257 // pushed onto window.tabs. Otherwise, the ids will change when push_back | 281 CreateSessionTab(42, "http://apple.com", "Apple"), |
| 258 // reallocates the vector's storage and calls each tabs' copy ctor. Ugh. | 282 CreateSessionWindow(50, { |
| 259 | 283 CreateSessionTab(51, "http://magic.com", "magic"), |
| 260 MockTRS::Window win1; | 284 CreateSessionTab(52, "http://goats.com", "goats"), |
| 261 win1.id = 30; | 285 CreateSessionTab(53, "http://teleporter.com", "teleporter"), |
| 262 win1.tabs.push_back(CreateSessionTab("http://foo.com", "foo")); | 286 }), |
| 263 win1.tabs.push_back(CreateSessionTab("http://bar.com", "bar")); | 287 })}; |
| 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 | 288 |
| 282 using ::testing::ReturnRef; | 289 using ::testing::ReturnRef; |
| 283 EXPECT_CALL(*trs.get(), entries()).WillOnce(ReturnRef(entries)); | 290 EXPECT_CALL(*trs.get(), entries()).WillOnce(ReturnRef(entries)); |
| 284 | 291 |
| 285 bridge_->TabRestoreServiceChanged(trs.get()); | 292 bridge_->TabRestoreServiceChanged(trs.get()); |
| 286 | 293 |
| 287 NSMenu* menu = bridge_->HistoryMenu(); | 294 NSMenu* menu = bridge_->HistoryMenu(); |
| 288 ASSERT_EQ(4U, [[menu itemArray] count]); | 295 ASSERT_EQ(4U, [[menu itemArray] count]); |
| 289 | 296 |
| 290 NSMenuItem* item1 = [menu itemAtIndex:0]; | 297 NSMenuItem* item1 = [menu itemAtIndex:0]; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 363 image_result.image = gfx::Image::CreateFrom1xBitmap(bitmap); | 370 image_result.image = gfx::Image::CreateFrom1xBitmap(bitmap); |
| 364 GotFaviconData(&item, image_result); | 371 GotFaviconData(&item, image_result); |
| 365 | 372 |
| 366 // Make sure the callback works. | 373 // Make sure the callback works. |
| 367 EXPECT_FALSE(item.icon_requested); | 374 EXPECT_FALSE(item.icon_requested); |
| 368 EXPECT_TRUE(item.icon.get()); | 375 EXPECT_TRUE(item.icon.get()); |
| 369 EXPECT_TRUE([item.menu_item image]); | 376 EXPECT_TRUE([item.menu_item image]); |
| 370 } | 377 } |
| 371 | 378 |
| 372 } // namespace | 379 } // namespace |
| OLD | NEW |