| 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 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/memory/ref_counted_memory.h" | 8 #include "base/memory/ref_counted_memory.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/sys_string_conversions.h" | 11 #include "base/sys_string_conversions.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/app/chrome_command_ids.h" | 13 #include "chrome/app/chrome_command_ids.h" |
| 14 #include "chrome/browser/cancelable_request.h" | 14 #include "chrome/browser/cancelable_request.h" |
| 15 #include "chrome/browser/sessions/session_types_test_helper.h" |
| 15 #include "chrome/browser/sessions/tab_restore_service.h" | 16 #include "chrome/browser/sessions/tab_restore_service.h" |
| 16 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" | 17 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" |
| 17 #include "chrome/browser/ui/cocoa/history_menu_bridge.h" | 18 #include "chrome/browser/ui/cocoa/history_menu_bridge.h" |
| 18 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #import "testing/gtest_mac.h" | 21 #import "testing/gtest_mac.h" |
| 21 #include "third_party/skia/include/core/SkBitmap.h" | 22 #include "third_party/skia/include/core/SkBitmap.h" |
| 22 #include "ui/gfx/codec/png_codec.h" | 23 #include "ui/gfx/codec/png_codec.h" |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 } | 84 } |
| 84 | 85 |
| 85 HistoryMenuBridge::HistoryItem* CreateItem(const string16& title) { | 86 HistoryMenuBridge::HistoryItem* CreateItem(const string16& title) { |
| 86 HistoryMenuBridge::HistoryItem* item = | 87 HistoryMenuBridge::HistoryItem* item = |
| 87 new HistoryMenuBridge::HistoryItem(); | 88 new HistoryMenuBridge::HistoryItem(); |
| 88 item->title = title; | 89 item->title = title; |
| 89 item->url = GURL(title); | 90 item->url = GURL(title); |
| 90 return item; | 91 return item; |
| 91 } | 92 } |
| 92 | 93 |
| 93 MockTRS::Tab CreateSessionTab(const GURL& url, const string16& title) { | 94 MockTRS::Tab CreateSessionTab(const std::string& url, |
| 95 const std::string& title) { |
| 94 MockTRS::Tab tab; | 96 MockTRS::Tab tab; |
| 95 tab.current_navigation_index = 0; | 97 tab.current_navigation_index = 0; |
| 96 tab.navigations.push_back( | 98 tab.navigations.push_back( |
| 97 TabNavigation(0, url, content::Referrer(), title, std::string(), | 99 SessionTypesTestHelper::CreateNavigation(url, title)); |
| 98 content::PAGE_TRANSITION_LINK)); | |
| 99 return tab; | 100 return tab; |
| 100 } | 101 } |
| 101 | 102 |
| 102 void GetFaviconForHistoryItem(HistoryMenuBridge::HistoryItem* item) { | 103 void GetFaviconForHistoryItem(HistoryMenuBridge::HistoryItem* item) { |
| 103 bridge_->GetFaviconForHistoryItem(item); | 104 bridge_->GetFaviconForHistoryItem(item); |
| 104 } | 105 } |
| 105 | 106 |
| 106 void GotFaviconData( | 107 void GotFaviconData( |
| 107 FaviconService::Handle handle, | 108 FaviconService::Handle handle, |
| 108 const history::FaviconImageResult& image_result) { | 109 const history::FaviconImageResult& image_result) { |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 // e.g. http://foo becomes http://foo/) | 207 // e.g. http://foo becomes http://foo/) |
| 207 EXPECT_GE([[[menu itemAtIndex:0] toolTip] length], (2*short_url.length()-5)); | 208 EXPECT_GE([[[menu itemAtIndex:0] toolTip] length], (2*short_url.length()-5)); |
| 208 EXPECT_GE([[[menu itemAtIndex:1] toolTip] length], (2*long_url.length()-5)); | 209 EXPECT_GE([[[menu itemAtIndex:1] toolTip] length], (2*long_url.length()-5)); |
| 209 } | 210 } |
| 210 | 211 |
| 211 // Test that the menu is created for a set of simple tabs. | 212 // Test that the menu is created for a set of simple tabs. |
| 212 TEST_F(HistoryMenuBridgeTest, RecentlyClosedTabs) { | 213 TEST_F(HistoryMenuBridgeTest, RecentlyClosedTabs) { |
| 213 scoped_ptr<MockTRS> trs(new MockTRS(profile())); | 214 scoped_ptr<MockTRS> trs(new MockTRS(profile())); |
| 214 MockTRS::Entries entries; | 215 MockTRS::Entries entries; |
| 215 | 216 |
| 216 MockTRS::Tab tab1 = CreateSessionTab(GURL("http://google.com"), | 217 MockTRS::Tab tab1 = CreateSessionTab("http://google.com", "Google"); |
| 217 ASCIIToUTF16("Google")); | |
| 218 tab1.id = 24; | 218 tab1.id = 24; |
| 219 entries.push_back(&tab1); | 219 entries.push_back(&tab1); |
| 220 | 220 |
| 221 MockTRS::Tab tab2 = CreateSessionTab(GURL("http://apple.com"), | 221 MockTRS::Tab tab2 = CreateSessionTab("http://apple.com", "Apple"); |
| 222 ASCIIToUTF16("Apple")); | |
| 223 tab2.id = 42; | 222 tab2.id = 42; |
| 224 entries.push_back(&tab2); | 223 entries.push_back(&tab2); |
| 225 | 224 |
| 226 using ::testing::ReturnRef; | 225 using ::testing::ReturnRef; |
| 227 EXPECT_CALL(*trs.get(), entries()).WillOnce(ReturnRef(entries)); | 226 EXPECT_CALL(*trs.get(), entries()).WillOnce(ReturnRef(entries)); |
| 228 | 227 |
| 229 bridge_->TabRestoreServiceChanged(trs.get()); | 228 bridge_->TabRestoreServiceChanged(trs.get()); |
| 230 | 229 |
| 231 NSMenu* menu = bridge_->HistoryMenu(); | 230 NSMenu* menu = bridge_->HistoryMenu(); |
| 232 ASSERT_EQ(2U, [[menu itemArray] count]); | 231 ASSERT_EQ(2U, [[menu itemArray] count]); |
| 233 | 232 |
| 234 NSMenuItem* item1 = [menu itemAtIndex:0]; | 233 NSMenuItem* item1 = [menu itemAtIndex:0]; |
| 235 MockBridge::HistoryItem* hist1 = bridge_->HistoryItemForMenuItem(item1); | 234 MockBridge::HistoryItem* hist1 = bridge_->HistoryItemForMenuItem(item1); |
| 236 EXPECT_TRUE(hist1); | 235 EXPECT_TRUE(hist1); |
| 237 EXPECT_EQ(24, hist1->session_id); | 236 EXPECT_EQ(24, hist1->session_id); |
| 238 EXPECT_NSEQ(@"Google", [item1 title]); | 237 EXPECT_NSEQ(@"Google", [item1 title]); |
| 239 | 238 |
| 240 NSMenuItem* item2 = [menu itemAtIndex:1]; | 239 NSMenuItem* item2 = [menu itemAtIndex:1]; |
| 241 MockBridge::HistoryItem* hist2 = bridge_->HistoryItemForMenuItem(item2); | 240 MockBridge::HistoryItem* hist2 = bridge_->HistoryItemForMenuItem(item2); |
| 242 EXPECT_TRUE(hist2); | 241 EXPECT_TRUE(hist2); |
| 243 EXPECT_EQ(42, hist2->session_id); | 242 EXPECT_EQ(42, hist2->session_id); |
| 244 EXPECT_NSEQ(@"Apple", [item2 title]); | 243 EXPECT_NSEQ(@"Apple", [item2 title]); |
| 245 } | 244 } |
| 246 | 245 |
| 247 // Test that the menu is created for a mix of windows and tabs. | 246 // Test that the menu is created for a mix of windows and tabs. |
| 248 TEST_F(HistoryMenuBridgeTest, RecentlyClosedTabsAndWindows) { | 247 TEST_F(HistoryMenuBridgeTest, RecentlyClosedTabsAndWindows) { |
| 249 scoped_ptr<MockTRS> trs(new MockTRS(profile())); | 248 scoped_ptr<MockTRS> trs(new MockTRS(profile())); |
| 250 MockTRS::Entries entries; | 249 MockTRS::Entries entries; |
| 251 | 250 |
| 252 MockTRS::Tab tab1 = CreateSessionTab(GURL("http://google.com"), | 251 MockTRS::Tab tab1 = CreateSessionTab("http://google.com", "Google"); |
| 253 ASCIIToUTF16("Google")); | |
| 254 tab1.id = 24; | 252 tab1.id = 24; |
| 255 entries.push_back(&tab1); | 253 entries.push_back(&tab1); |
| 256 | 254 |
| 257 MockTRS::Window win1; | 255 MockTRS::Window win1; |
| 258 win1.id = 30; | 256 win1.id = 30; |
| 259 win1.tabs.push_back( | 257 win1.tabs.push_back(CreateSessionTab("http://foo.com", "foo")); |
| 260 CreateSessionTab(GURL("http://foo.com"), ASCIIToUTF16("foo"))); | |
| 261 win1.tabs[0].id = 31; | 258 win1.tabs[0].id = 31; |
| 262 win1.tabs.push_back( | 259 win1.tabs.push_back(CreateSessionTab("http://bar.com", "bar")); |
| 263 CreateSessionTab(GURL("http://bar.com"), ASCIIToUTF16("bar"))); | |
| 264 win1.tabs[1].id = 32; | 260 win1.tabs[1].id = 32; |
| 265 entries.push_back(&win1); | 261 entries.push_back(&win1); |
| 266 | 262 |
| 267 MockTRS::Tab tab2 = CreateSessionTab(GURL("http://apple.com"), | 263 MockTRS::Tab tab2 = CreateSessionTab("http://apple.com", "Apple"); |
| 268 ASCIIToUTF16("Apple")); | |
| 269 tab2.id = 42; | 264 tab2.id = 42; |
| 270 entries.push_back(&tab2); | 265 entries.push_back(&tab2); |
| 271 | 266 |
| 272 MockTRS::Window win2; | 267 MockTRS::Window win2; |
| 273 win2.id = 50; | 268 win2.id = 50; |
| 274 win2.tabs.push_back( | 269 win2.tabs.push_back(CreateSessionTab("http://magic.com", "magic")); |
| 275 CreateSessionTab(GURL("http://magic.com"), ASCIIToUTF16("magic"))); | |
| 276 win2.tabs[0].id = 51; | 270 win2.tabs[0].id = 51; |
| 277 win2.tabs.push_back( | 271 win2.tabs.push_back(CreateSessionTab("http://goats.com", "goats")); |
| 278 CreateSessionTab(GURL("http://goats.com"), ASCIIToUTF16("goats"))); | |
| 279 win2.tabs[1].id = 52; | 272 win2.tabs[1].id = 52; |
| 280 win2.tabs.push_back( | 273 win2.tabs.push_back(CreateSessionTab("http://teleporter.com", "teleporter")); |
| 281 CreateSessionTab(GURL("http://teleporter.com"), | |
| 282 ASCIIToUTF16("teleporter"))); | |
| 283 win2.tabs[1].id = 53; | 274 win2.tabs[1].id = 53; |
| 284 entries.push_back(&win2); | 275 entries.push_back(&win2); |
| 285 | 276 |
| 286 using ::testing::ReturnRef; | 277 using ::testing::ReturnRef; |
| 287 EXPECT_CALL(*trs.get(), entries()).WillOnce(ReturnRef(entries)); | 278 EXPECT_CALL(*trs.get(), entries()).WillOnce(ReturnRef(entries)); |
| 288 | 279 |
| 289 bridge_->TabRestoreServiceChanged(trs.get()); | 280 bridge_->TabRestoreServiceChanged(trs.get()); |
| 290 | 281 |
| 291 NSMenu* menu = bridge_->HistoryMenu(); | 282 NSMenu* menu = bridge_->HistoryMenu(); |
| 292 ASSERT_EQ(4U, [[menu itemArray] count]); | 283 ASSERT_EQ(4U, [[menu itemArray] count]); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 image_result.image = gfx::Image(bitmap); | 360 image_result.image = gfx::Image(bitmap); |
| 370 GotFaviconData(item.icon_handle, image_result); | 361 GotFaviconData(item.icon_handle, image_result); |
| 371 | 362 |
| 372 // Make sure the callback works. | 363 // Make sure the callback works. |
| 373 EXPECT_FALSE(item.icon_requested); | 364 EXPECT_FALSE(item.icon_requested); |
| 374 EXPECT_TRUE(item.icon.get()); | 365 EXPECT_TRUE(item.icon.get()); |
| 375 EXPECT_TRUE([item.menu_item image]); | 366 EXPECT_TRUE([item.menu_item image]); |
| 376 } | 367 } |
| 377 | 368 |
| 378 } // namespace | 369 } // namespace |
| OLD | NEW |