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 "base/stringprintf.h" | 5 #include "base/stringprintf.h" |
6 #include "base/utf_string_conversions.h" | 6 #include "base/utf_string_conversions.h" |
7 #include "chrome/browser/sessions/session_service_factory.h" | 7 #include "chrome/browser/sessions/session_service_factory.h" |
8 #include "chrome/browser/sessions/session_service.h" | 8 #include "chrome/browser/sessions/session_service.h" |
9 #include "chrome/browser/sessions/session_types.h" | 9 #include "chrome/browser/sessions/session_types.h" |
10 #include "chrome/browser/sessions/tab_restore_service.h" | 10 #include "chrome/browser/sessions/tab_restore_service.h" |
| 11 #include "chrome/browser/ui/browser_window.h" |
11 #include "chrome/common/url_constants.h" | 12 #include "chrome/common/url_constants.h" |
12 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
13 #include "chrome/test/base/chrome_render_view_test.h" | 14 #include "chrome/test/base/chrome_render_view_test.h" |
| 15 #include "chrome/test/base/in_process_browser_test.h" |
14 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
15 #include "content/browser/tab_contents/test_tab_contents.h" | 17 #include "content/browser/tab_contents/test_tab_contents.h" |
16 #include "content/public/browser/navigation_controller.h" | 18 #include "content/public/browser/navigation_controller.h" |
17 #include "content/public/browser/navigation_entry.h" | 19 #include "content/public/browser/navigation_entry.h" |
18 #include "content/test/render_view_test.h" | 20 #include "content/test/render_view_test.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" | 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" |
21 | 23 |
22 typedef TabRestoreService::Tab Tab; | 24 typedef TabRestoreService::Tab Tab; |
| 25 typedef TabRestoreService::Window Window; |
23 | 26 |
24 using content::NavigationEntry; | 27 using content::NavigationEntry; |
25 | 28 |
26 // Create subclass that overrides TimeNow so that we can control the time used | 29 // Create subclass that overrides TimeNow so that we can control the time used |
27 // for closed tabs and windows. | 30 // for closed tabs and windows. |
28 class TabRestoreTimeFactory : public TabRestoreService::TimeFactory { | 31 class TabRestoreTimeFactory : public TabRestoreService::TimeFactory { |
29 public: | 32 public: |
30 TabRestoreTimeFactory() : time_(base::Time::Now()) {} | 33 TabRestoreTimeFactory() : time_(base::Time::Now()) {} |
31 | 34 |
32 virtual ~TabRestoreTimeFactory() {} | 35 virtual ~TabRestoreTimeFactory() {} |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 service_->LoadTabsFromLastSession(); | 93 service_->LoadTabsFromLastSession(); |
91 } | 94 } |
92 | 95 |
93 // Adds a window with one tab and url to the profile's session service. | 96 // Adds a window with one tab and url to the profile's session service. |
94 // If |pinned| is true, the tab is marked as pinned in the session service. | 97 // If |pinned| is true, the tab is marked as pinned in the session service. |
95 void AddWindowWithOneTabToSessionService(bool pinned) { | 98 void AddWindowWithOneTabToSessionService(bool pinned) { |
96 SessionService* session_service = | 99 SessionService* session_service = |
97 SessionServiceFactory::GetForProfile(profile()); | 100 SessionServiceFactory::GetForProfile(profile()); |
98 SessionID tab_id; | 101 SessionID tab_id; |
99 SessionID window_id; | 102 SessionID window_id; |
100 session_service->SetWindowType(window_id, Browser::TYPE_TABBED); | 103 session_service->SetWindowType(window_id, Browser::TYPE_TABBED, false); |
101 session_service->SetTabWindow(window_id, tab_id); | 104 session_service->SetTabWindow(window_id, tab_id); |
102 session_service->SetTabIndexInWindow(window_id, tab_id, 0); | 105 session_service->SetTabIndexInWindow(window_id, tab_id, 0); |
103 session_service->SetSelectedTabInWindow(window_id, 0); | 106 session_service->SetSelectedTabInWindow(window_id, 0); |
104 if (pinned) | 107 if (pinned) |
105 session_service->SetPinnedState(window_id, tab_id, true); | 108 session_service->SetPinnedState(window_id, tab_id, true); |
106 scoped_ptr<NavigationEntry> entry(NavigationEntry::Create());; | 109 scoped_ptr<NavigationEntry> entry(NavigationEntry::Create());; |
107 entry->SetURL(url1_); | 110 entry->SetURL(url1_); |
108 session_service->UpdateTabNavigation(window_id, tab_id, 0, *entry.get()); | 111 session_service->UpdateTabNavigation(window_id, tab_id, 0, *entry.get()); |
109 } | 112 } |
110 | 113 |
| 114 // Adds a window with one tab and url to the profile's session service. |
| 115 // If |pinned| is true, the tab is marked as pinned in the session service. |
| 116 void AddApplicationWindowToSessionService() { |
| 117 SessionService* session_service = |
| 118 SessionServiceFactory::GetForProfile(profile()); |
| 119 SessionID tab_id; |
| 120 SessionID window_id; |
| 121 session_service->SetWindowType(window_id, Browser::TYPE_POPUP, true); |
| 122 session_service->SetWindowAppName(window_id, "TestApp"); |
| 123 session_service->SetTabWindow(window_id, tab_id); |
| 124 session_service->SetTabIndexInWindow(window_id, tab_id, 0); |
| 125 session_service->SetSelectedTabInWindow(window_id, 0); |
| 126 scoped_ptr<NavigationEntry> entry(NavigationEntry::Create());; |
| 127 entry->SetURL(url1_); |
| 128 session_service->UpdateTabNavigation(window_id, tab_id, 0, *entry.get()); |
| 129 } |
| 130 |
111 // Creates a SessionService and assigns it to the Profile. The SessionService | 131 // Creates a SessionService and assigns it to the Profile. The SessionService |
112 // is configured with a single window with a single tab pointing at url1_ by | 132 // is configured with a single window with a single tab pointing at url1_ by |
113 // way of AddWindowWithOneTabToSessionService. If |pinned| is true, the | 133 // way of AddWindowWithOneTabToSessionService. If |pinned| is true, the |
114 // tab is marked as pinned in the session service. | 134 // tab is marked as pinned in the session service. |
115 void CreateSessionServiceWithOneWindow(bool pinned) { | 135 void CreateSessionServiceWithOneWindow(bool pinned) { |
116 // The profile takes ownership of this. | 136 // The profile takes ownership of this. |
117 SessionService* session_service = new SessionService(profile()); | 137 SessionService* session_service = new SessionService(profile()); |
118 SessionServiceFactory::SetForTestProfile(profile(), session_service); | 138 SessionServiceFactory::SetForTestProfile(profile(), session_service); |
119 | 139 |
120 AddWindowWithOneTabToSessionService(pinned); | 140 AddWindowWithOneTabToSessionService(pinned); |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 tab = static_cast<Tab*>(entry); | 262 tab = static_cast<Tab*>(entry); |
243 EXPECT_TRUE(tab->pinned); | 263 EXPECT_TRUE(tab->pinned); |
244 ASSERT_EQ(3U, tab->navigations.size()); | 264 ASSERT_EQ(3U, tab->navigations.size()); |
245 EXPECT_TRUE(url1_ == tab->navigations[0].virtual_url()); | 265 EXPECT_TRUE(url1_ == tab->navigations[0].virtual_url()); |
246 EXPECT_TRUE(url2_ == tab->navigations[1].virtual_url()); | 266 EXPECT_TRUE(url2_ == tab->navigations[1].virtual_url()); |
247 EXPECT_TRUE(url3_ == tab->navigations[2].virtual_url()); | 267 EXPECT_TRUE(url3_ == tab->navigations[2].virtual_url()); |
248 EXPECT_EQ(2, tab->current_navigation_index); | 268 EXPECT_EQ(2, tab->current_navigation_index); |
249 EXPECT_TRUE(extension_app_id == tab->extension_app_id); | 269 EXPECT_TRUE(extension_app_id == tab->extension_app_id); |
250 } | 270 } |
251 | 271 |
| 272 // We only restore apps on chromeos. |
| 273 #if defined(OS_CHROMEOS) |
| 274 typedef InProcessBrowserTest TabRestoreServiceBrowserTest; |
| 275 IN_PROC_BROWSER_TEST_F(TabRestoreServiceBrowserTest, RestoreApp) { |
| 276 Profile* profile = browser()->profile(); |
| 277 const char* app_name = "TestApp"; |
| 278 |
| 279 Browser* app_browser = |
| 280 Browser::CreateForApp(Browser::TYPE_POPUP, |
| 281 app_name, |
| 282 gfx::Rect(), |
| 283 profile); |
| 284 |
| 285 app_browser->window()->Close(); |
| 286 TabRestoreService trs(profile); |
| 287 // One entry should be created. |
| 288 ASSERT_EQ(1U, trs.entries().size()); |
| 289 const TabRestoreService::Entry* restored_entry = trs.entries().front(); |
| 290 |
| 291 // It should be a window with an app. |
| 292 ASSERT_EQ(TabRestoreService::WINDOW, restored_entry->type); |
| 293 const Window* restored_window = |
| 294 static_cast<const Window*>(restored_entry); |
| 295 EXPECT_EQ(app_name, restored_window->app_name); |
| 296 |
| 297 } |
| 298 #endif |
| 299 |
252 // Make sure we persist entries to disk that have post data. | 300 // Make sure we persist entries to disk that have post data. |
253 TEST_F(TabRestoreServiceTest, DontPersistPostData) { | 301 TEST_F(TabRestoreServiceTest, DontPersistPostData) { |
254 AddThreeNavigations(); | 302 AddThreeNavigations(); |
255 controller().GetEntryAtIndex(0)->SetHasPostData(true); | 303 controller().GetEntryAtIndex(0)->SetHasPostData(true); |
256 controller().GetEntryAtIndex(1)->SetHasPostData(true); | 304 controller().GetEntryAtIndex(1)->SetHasPostData(true); |
257 controller().GetEntryAtIndex(2)->SetHasPostData(true); | 305 controller().GetEntryAtIndex(2)->SetHasPostData(true); |
258 | 306 |
259 // Have the service record the tab. | 307 // Have the service record the tab. |
260 service_->CreateHistoricalTab(&controller(), -1); | 308 service_->CreateHistoricalTab(&controller(), -1); |
261 ASSERT_EQ(1U, service_->entries().size()); | 309 ASSERT_EQ(1U, service_->entries().size()); |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
600 for (size_t i = 0; i < max_entries + 5; i++) { | 648 for (size_t i = 0; i < max_entries + 5; i++) { |
601 NavigateAndCommit(GURL(StringPrintf("http://%d", static_cast<int>(i)))); | 649 NavigateAndCommit(GURL(StringPrintf("http://%d", static_cast<int>(i)))); |
602 service_->CreateHistoricalTab(&controller(), -1); | 650 service_->CreateHistoricalTab(&controller(), -1); |
603 } | 651 } |
604 | 652 |
605 EXPECT_EQ(max_entries, service_->entries_.size()); | 653 EXPECT_EQ(max_entries, service_->entries_.size()); |
606 // This should not crash. | 654 // This should not crash. |
607 service_->LoadTabsFromLastSession(); | 655 service_->LoadTabsFromLastSession(); |
608 EXPECT_EQ(max_entries, service_->entries_.size()); | 656 EXPECT_EQ(max_entries, service_->entries_.size()); |
609 } | 657 } |
OLD | NEW |