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

Side by Side Diff: chrome/browser/sessions/persistent_tab_restore_service_browsertest.cc

Issue 10989027: Split TabRestoreService into InMemoryTRS and PersistentTRS (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: GYP out InMemoryTRS on non-Android and fix incorrect comments Created 8 years, 1 month 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 | Annotate | Revision Log
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/sessions/persistent_tab_restore_service.h"
6
7 #include "base/compiler_specific.h"
5 #include "base/stringprintf.h" 8 #include "base/stringprintf.h"
6 #include "base/utf_string_conversions.h" 9 #include "base/utf_string_conversions.h"
7 #include "chrome/browser/sessions/session_service.h" 10 #include "chrome/browser/sessions/session_service.h"
8 #include "chrome/browser/sessions/session_service_factory.h" 11 #include "chrome/browser/sessions/session_service_factory.h"
9 #include "chrome/browser/sessions/session_types.h" 12 #include "chrome/browser/sessions/session_types.h"
10 #include "chrome/browser/sessions/session_types_test_helper.h" 13 #include "chrome/browser/sessions/session_types_test_helper.h"
11 #include "chrome/browser/sessions/tab_restore_service.h"
12 #include "chrome/browser/sessions/tab_restore_service_factory.h" 14 #include "chrome/browser/sessions/tab_restore_service_factory.h"
13 #include "chrome/browser/ui/browser_window.h" 15 #include "chrome/browser/ui/browser_window.h"
14 #include "chrome/common/chrome_notification_types.h" 16 #include "chrome/common/chrome_notification_types.h"
15 #include "chrome/common/url_constants.h" 17 #include "chrome/common/url_constants.h"
16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 18 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
17 #include "chrome/test/base/chrome_render_view_test.h" 19 #include "chrome/test/base/chrome_render_view_test.h"
18 #include "chrome/test/base/in_process_browser_test.h" 20 #include "chrome/test/base/in_process_browser_test.h"
19 #include "chrome/test/base/testing_profile.h" 21 #include "chrome/test/base/testing_profile.h"
20 #include "content/public/browser/browser_thread.h" 22 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/navigation_controller.h" 23 #include "content/public/browser/navigation_controller.h"
22 #include "content/public/browser/navigation_entry.h" 24 #include "content/public/browser/navigation_entry.h"
23 #include "content/public/browser/notification_service.h" 25 #include "content/public/browser/notification_service.h"
24 #include "content/public/browser/notification_types.h" 26 #include "content/public/browser/notification_types.h"
25 #include "content/public/browser/web_contents.h" 27 #include "content/public/browser/web_contents.h"
26 #include "content/public/test/render_view_test.h" 28 #include "content/public/test/render_view_test.h"
27 #include "content/public/test/test_browser_thread.h" 29 #include "content/public/test/test_browser_thread.h"
28 #include "content/public/test/test_utils.h" 30 #include "content/public/test/test_utils.h"
29 #include "content/public/test/web_contents_tester.h" 31 #include "content/public/test/web_contents_tester.h"
30 #include "testing/gtest/include/gtest/gtest.h" 32 #include "testing/gtest/include/gtest/gtest.h"
31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" 33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
32 34
33 typedef TabRestoreService::Tab Tab; 35 typedef TabRestoreService::Tab Tab;
34 typedef TabRestoreService::Window Window; 36 typedef TabRestoreService::Window Window;
35 using content::WebContentsTester; 37 using content::WebContentsTester;
36 38
37 using content::NavigationEntry; 39 using content::NavigationEntry;
38 40
39 // Create subclass that overrides TimeNow so that we can control the time used 41 // Create subclass that overrides TimeNow so that we can control the time used
40 // for closed tabs and windows. 42 // for closed tabs and windows.
41 class TabRestoreTimeFactory : public TabRestoreService::TimeFactory { 43 class PersistentTabRestoreTimeFactory : public TabRestoreService::TimeFactory {
42 public: 44 public:
43 TabRestoreTimeFactory() : time_(base::Time::Now()) {} 45 PersistentTabRestoreTimeFactory() : time_(base::Time::Now()) {}
44 46
45 virtual ~TabRestoreTimeFactory() {} 47 virtual ~PersistentTabRestoreTimeFactory() {}
46 48
47 virtual base::Time TimeNow() { 49 virtual base::Time TimeNow() {
48 return time_; 50 return time_;
49 } 51 }
50 52
51 private: 53 private:
52 base::Time time_; 54 base::Time time_;
53 }; 55 };
54 56
55 class TabRestoreServiceTest : public ChromeRenderViewHostTestHarness { 57 class PersistentTabRestoreServiceTest : public ChromeRenderViewHostTestHarness {
56 public: 58 public:
57 TabRestoreServiceTest() 59 PersistentTabRestoreServiceTest()
58 : ui_thread_(content::BrowserThread::UI, &message_loop_) { 60 : ui_thread_(content::BrowserThread::UI, &message_loop_) {
59 url1_ = GURL("http://1"); 61 url1_ = GURL("http://1");
60 url2_ = GURL("http://2"); 62 url2_ = GURL("http://2");
61 url3_ = GURL("http://3"); 63 url3_ = GURL("http://3");
62 user_agent_override_ = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19" 64 user_agent_override_ = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19"
63 " (KHTML, like Gecko) Chrome/18.0.1025.45 Safari/535.19"; 65 " (KHTML, like Gecko) Chrome/18.0.1025.45 Safari/535.19";
64 } 66 }
65 67
66 ~TabRestoreServiceTest() { 68 ~PersistentTabRestoreServiceTest() {
67 } 69 }
68 70
69 protected: 71 protected:
70 // testing::Test overrides 72 static const size_t kMaxEntries;
71 virtual void SetUp() { 73
74 // testing::Test:
75 virtual void SetUp() OVERRIDE {
72 WebKit::initialize(webkit_platform_support_.Get()); 76 WebKit::initialize(webkit_platform_support_.Get());
73 ChromeRenderViewHostTestHarness::SetUp(); 77 ChromeRenderViewHostTestHarness::SetUp();
74 time_factory_ = new TabRestoreTimeFactory(); 78 time_factory_ = new PersistentTabRestoreTimeFactory();
75 service_.reset(new TabRestoreService(profile(), time_factory_)); 79 service_.reset(new PersistentTabRestoreService(profile(), time_factory_));
76 } 80 }
77 81
78 virtual void TearDown() { 82 virtual void TearDown() OVERRIDE {
79 service_->Shutdown(); 83 service_->Shutdown();
80 service_.reset(); 84 service_.reset();
81 delete time_factory_; 85 delete time_factory_;
82 ChromeRenderViewHostTestHarness::TearDown(); 86 ChromeRenderViewHostTestHarness::TearDown();
83 WebKit::shutdown(); 87 WebKit::shutdown();
84 } 88 }
85 89
90 TabRestoreService::Entries* mutable_entries() {
91 return service_->mutable_entries();
92 }
93
94 void PruneEntries() {
95 service_->PruneEntries();
96 }
97
86 void AddThreeNavigations() { 98 void AddThreeNavigations() {
87 // Navigate to three URLs. 99 // Navigate to three URLs.
88 NavigateAndCommit(url1_); 100 NavigateAndCommit(url1_);
89 NavigateAndCommit(url2_); 101 NavigateAndCommit(url2_);
90 NavigateAndCommit(url3_); 102 NavigateAndCommit(url3_);
91 } 103 }
92 104
93 void NavigateToIndex(int index) { 105 void NavigateToIndex(int index) {
94 // Navigate back. We have to do this song and dance as NavigationController 106 // Navigate back. We have to do this song and dance as NavigationController
95 // isn't happy if you navigate immediately while going back. 107 // isn't happy if you navigate immediately while going back.
96 controller().GoToIndex(index); 108 controller().GoToIndex(index);
97 WebContentsTester::For(web_contents())->CommitPendingNavigation(); 109 WebContentsTester::For(web_contents())->CommitPendingNavigation();
98 } 110 }
99 111
100 void RecreateService() { 112 void RecreateService() {
101 // Must set service to null first so that it is destroyed before the new 113 // Must set service to null first so that it is destroyed before the new
102 // one is created. 114 // one is created.
103 service_->Shutdown(); 115 service_->Shutdown();
104 service_.reset(); 116 service_.reset();
105 service_.reset(new TabRestoreService(profile(), time_factory_)); 117 service_.reset(new PersistentTabRestoreService(profile(), time_factory_));
106 service_->LoadTabsFromLastSession(); 118 service_->LoadTabsFromLastSession();
107 } 119 }
108 120
109 // Adds a window with one tab and url to the profile's session service. 121 // Adds a window with one tab and url to the profile's session service.
110 // If |pinned| is true, the tab is marked as pinned in the session service. 122 // If |pinned| is true, the tab is marked as pinned in the session service.
111 void AddWindowWithOneTabToSessionService(bool pinned) { 123 void AddWindowWithOneTabToSessionService(bool pinned) {
112 SessionService* session_service = 124 SessionService* session_service =
113 SessionServiceFactory::GetForProfile(profile()); 125 SessionServiceFactory::GetForProfile(profile());
114 SessionID tab_id; 126 SessionID tab_id;
115 SessionID window_id; 127 SessionID window_id;
(...skipping 21 matching lines...) Expand all
137 AddWindowWithOneTabToSessionService(pinned); 149 AddWindowWithOneTabToSessionService(pinned);
138 150
139 // Set this, otherwise previous session won't be loaded. 151 // Set this, otherwise previous session won't be loaded.
140 profile()->set_last_session_exited_cleanly(false); 152 profile()->set_last_session_exited_cleanly(false);
141 } 153 }
142 154
143 GURL url1_; 155 GURL url1_;
144 GURL url2_; 156 GURL url2_;
145 GURL url3_; 157 GURL url3_;
146 std::string user_agent_override_; 158 std::string user_agent_override_;
147 scoped_ptr<TabRestoreService> service_; 159 scoped_ptr<PersistentTabRestoreService> service_;
148 TabRestoreTimeFactory* time_factory_; 160 PersistentTabRestoreTimeFactory* time_factory_;
149 content::RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox 161 content::RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox
150 webkit_platform_support_; 162 webkit_platform_support_;
151 content::TestBrowserThread ui_thread_; 163 content::TestBrowserThread ui_thread_;
152 }; 164 };
153 165
154 TEST_F(TabRestoreServiceTest, Basic) { 166 const size_t PersistentTabRestoreServiceTest::kMaxEntries =
167 TabRestoreServiceHelper::kMaxEntries;
168
169 TEST_F(PersistentTabRestoreServiceTest, Basic) {
155 AddThreeNavigations(); 170 AddThreeNavigations();
156 171
157 // Have the service record the tab. 172 // Have the service record the tab.
158 service_->CreateHistoricalTab(web_contents(), -1); 173 service_->CreateHistoricalTab(web_contents(), -1);
159 174
160 // Make sure an entry was created. 175 // Make sure an entry was created.
161 ASSERT_EQ(1U, service_->entries().size()); 176 ASSERT_EQ(1U, service_->entries().size());
162 177
163 // Make sure the entry matches. 178 // Make sure the entry matches.
164 TabRestoreService::Entry* entry = service_->entries().front(); 179 TabRestoreService::Entry* entry = service_->entries().front();
(...skipping 29 matching lines...) Expand all
194 EXPECT_EQ(url2_, tab->navigations[1].virtual_url()); 209 EXPECT_EQ(url2_, tab->navigations[1].virtual_url());
195 EXPECT_EQ(url3_, tab->navigations[2].virtual_url()); 210 EXPECT_EQ(url3_, tab->navigations[2].virtual_url());
196 EXPECT_EQ(user_agent_override_, tab->user_agent_override); 211 EXPECT_EQ(user_agent_override_, tab->user_agent_override);
197 EXPECT_EQ(1, tab->current_navigation_index); 212 EXPECT_EQ(1, tab->current_navigation_index);
198 EXPECT_EQ(time_factory_->TimeNow().ToInternalValue(), 213 EXPECT_EQ(time_factory_->TimeNow().ToInternalValue(),
199 tab->timestamp.ToInternalValue()); 214 tab->timestamp.ToInternalValue());
200 } 215 }
201 216
202 // Make sure TabRestoreService doesn't create an entry for a tab with no 217 // Make sure TabRestoreService doesn't create an entry for a tab with no
203 // navigations. 218 // navigations.
204 TEST_F(TabRestoreServiceTest, DontCreateEmptyTab) { 219 TEST_F(PersistentTabRestoreServiceTest, DontCreateEmptyTab) {
205 service_->CreateHistoricalTab(web_contents(), -1); 220 service_->CreateHistoricalTab(web_contents(), -1);
206 EXPECT_TRUE(service_->entries().empty()); 221 EXPECT_TRUE(service_->entries().empty());
207 } 222 }
208 223
209 // Tests restoring a single tab. 224 // Tests restoring a single tab.
210 TEST_F(TabRestoreServiceTest, Restore) { 225 TEST_F(PersistentTabRestoreServiceTest, Restore) {
211 AddThreeNavigations(); 226 AddThreeNavigations();
212 227
213 // Have the service record the tab. 228 // Have the service record the tab.
214 service_->CreateHistoricalTab(web_contents(), -1); 229 service_->CreateHistoricalTab(web_contents(), -1);
215 230
216 // Recreate the service and have it load the tabs. 231 // Recreate the service and have it load the tabs.
217 RecreateService(); 232 RecreateService();
218 233
219 // One entry should be created. 234 // One entry should be created.
220 ASSERT_EQ(1U, service_->entries().size()); 235 ASSERT_EQ(1U, service_->entries().size());
221 236
222 // And verify the entry. 237 // And verify the entry.
223 TabRestoreService::Entry* entry = service_->entries().front(); 238 PersistentTabRestoreService::Entry* entry = service_->entries().front();
224 ASSERT_EQ(TabRestoreService::TAB, entry->type); 239 ASSERT_EQ(TabRestoreService::TAB, entry->type);
225 Tab* tab = static_cast<Tab*>(entry); 240 Tab* tab = static_cast<Tab*>(entry);
226 EXPECT_FALSE(tab->pinned); 241 EXPECT_FALSE(tab->pinned);
227 ASSERT_EQ(3U, tab->navigations.size()); 242 ASSERT_EQ(3U, tab->navigations.size());
228 EXPECT_TRUE(url1_ == tab->navigations[0].virtual_url()); 243 EXPECT_TRUE(url1_ == tab->navigations[0].virtual_url());
229 EXPECT_TRUE(url2_ == tab->navigations[1].virtual_url()); 244 EXPECT_TRUE(url2_ == tab->navigations[1].virtual_url());
230 EXPECT_TRUE(url3_ == tab->navigations[2].virtual_url()); 245 EXPECT_TRUE(url3_ == tab->navigations[2].virtual_url());
231 EXPECT_EQ(2, tab->current_navigation_index); 246 EXPECT_EQ(2, tab->current_navigation_index);
232 EXPECT_EQ(time_factory_->TimeNow().ToInternalValue(), 247 EXPECT_EQ(time_factory_->TimeNow().ToInternalValue(),
233 tab->timestamp.ToInternalValue()); 248 tab->timestamp.ToInternalValue());
234 } 249 }
235 250
236 // Tests restoring a single pinned tab. 251 // Tests restoring a single pinned tab.
237 TEST_F(TabRestoreServiceTest, RestorePinnedAndApp) { 252 TEST_F(PersistentTabRestoreServiceTest, RestorePinnedAndApp) {
238 AddThreeNavigations(); 253 AddThreeNavigations();
239 254
240 // Have the service record the tab. 255 // Have the service record the tab.
241 service_->CreateHistoricalTab(web_contents(), -1); 256 service_->CreateHistoricalTab(web_contents(), -1);
242 257
243 // One entry should be created. 258 // One entry should be created.
244 ASSERT_EQ(1U, service_->entries().size()); 259 ASSERT_EQ(1U, service_->entries().size());
245 260
246 // We have to explicitly mark the tab as pinned as there is no browser for 261 // We have to explicitly mark the tab as pinned as there is no browser for
247 // these tests. 262 // these tests.
(...skipping 19 matching lines...) Expand all
267 EXPECT_TRUE(url1_ == tab->navigations[0].virtual_url()); 282 EXPECT_TRUE(url1_ == tab->navigations[0].virtual_url());
268 EXPECT_TRUE(url2_ == tab->navigations[1].virtual_url()); 283 EXPECT_TRUE(url2_ == tab->navigations[1].virtual_url());
269 EXPECT_TRUE(url3_ == tab->navigations[2].virtual_url()); 284 EXPECT_TRUE(url3_ == tab->navigations[2].virtual_url());
270 EXPECT_EQ(2, tab->current_navigation_index); 285 EXPECT_EQ(2, tab->current_navigation_index);
271 EXPECT_TRUE(extension_app_id == tab->extension_app_id); 286 EXPECT_TRUE(extension_app_id == tab->extension_app_id);
272 } 287 }
273 288
274 // We only restore apps on chromeos. 289 // We only restore apps on chromeos.
275 #if defined(USE_AURA) 290 #if defined(USE_AURA)
276 291
277 typedef InProcessBrowserTest TabRestoreServiceBrowserTest; 292 typedef InProcessBrowserTest PersistentTabRestoreServiceBrowserTest;
278 293
279 IN_PROC_BROWSER_TEST_F(TabRestoreServiceBrowserTest, RestoreApp) { 294 IN_PROC_BROWSER_TEST_F(PersistentTabRestoreServiceBrowserTest, RestoreApp) {
280 Profile* profile = browser()->profile(); 295 Profile* profile = browser()->profile();
281 TabRestoreService* trs = TabRestoreServiceFactory::GetForProfile(profile); 296 TabRestoreService* trs = TabRestoreServiceFactory::GetForProfile(profile);
282 const char* app_name = "TestApp"; 297 const char* app_name = "TestApp";
283 298
284 Browser* app_browser = CreateBrowserForApp(app_name, profile); 299 Browser* app_browser = CreateBrowserForApp(app_name, profile);
285 app_browser->window()->Close(); 300 app_browser->window()->Close();
286 content::WindowedNotificationObserver observer( 301 content::WindowedNotificationObserver observer(
287 chrome::NOTIFICATION_BROWSER_CLOSED, 302 chrome::NOTIFICATION_BROWSER_CLOSED,
288 content::Source<Browser>(app_browser)); 303 content::Source<Browser>(app_browser));
289 observer.Wait(); 304 observer.Wait();
290 305
291 // One entry should be created. 306 // One entry should be created.
292 ASSERT_EQ(1U, trs->entries().size()); 307 ASSERT_EQ(1U, trs->entries().size());
293 const TabRestoreService::Entry* restored_entry = trs->entries().front(); 308 const TabRestoreService::Entry* restored_entry = trs->entries().front();
294 309
295 // It should be a window with an app. 310 // It should be a window with an app.
296 ASSERT_EQ(TabRestoreService::WINDOW, restored_entry->type); 311 ASSERT_EQ(TabRestoreService::WINDOW, restored_entry->type);
297 const Window* restored_window = 312 const Window* restored_window =
298 static_cast<const Window*>(restored_entry); 313 static_cast<const Window*>(restored_entry);
299 EXPECT_EQ(app_name, restored_window->app_name); 314 EXPECT_EQ(app_name, restored_window->app_name);
300 } 315 }
301 #endif // defined(USE_AURA) 316 #endif // defined(USE_AURA)
302 317
303 // Make sure we persist entries to disk that have post data. 318 // Make sure we persist entries to disk that have post data.
304 TEST_F(TabRestoreServiceTest, DontPersistPostData) { 319 TEST_F(PersistentTabRestoreServiceTest, DontPersistPostData) {
305 AddThreeNavigations(); 320 AddThreeNavigations();
306 controller().GetEntryAtIndex(0)->SetHasPostData(true); 321 controller().GetEntryAtIndex(0)->SetHasPostData(true);
307 controller().GetEntryAtIndex(1)->SetHasPostData(true); 322 controller().GetEntryAtIndex(1)->SetHasPostData(true);
308 controller().GetEntryAtIndex(2)->SetHasPostData(true); 323 controller().GetEntryAtIndex(2)->SetHasPostData(true);
309 324
310 // Have the service record the tab. 325 // Have the service record the tab.
311 service_->CreateHistoricalTab(web_contents(), -1); 326 service_->CreateHistoricalTab(web_contents(), -1);
312 ASSERT_EQ(1U, service_->entries().size()); 327 ASSERT_EQ(1U, service_->entries().size());
313 328
314 // Recreate the service and have it load the tabs. 329 // Recreate the service and have it load the tabs.
315 RecreateService(); 330 RecreateService();
316 331
317 // One entry should be created. 332 // One entry should be created.
318 ASSERT_EQ(1U, service_->entries().size()); 333 ASSERT_EQ(1U, service_->entries().size());
319 334
320 const TabRestoreService::Entry* restored_entry = service_->entries().front(); 335 const TabRestoreService::Entry* restored_entry = service_->entries().front();
321 ASSERT_EQ(TabRestoreService::TAB, restored_entry->type); 336 ASSERT_EQ(TabRestoreService::TAB, restored_entry->type);
322 337
323 const Tab* restored_tab = 338 const Tab* restored_tab =
324 static_cast<const Tab*>(restored_entry); 339 static_cast<const Tab*>(restored_entry);
325 // There should be 3 navs. 340 // There should be 3 navs.
326 ASSERT_EQ(3U, restored_tab->navigations.size()); 341 ASSERT_EQ(3U, restored_tab->navigations.size());
327 EXPECT_EQ(time_factory_->TimeNow().ToInternalValue(), 342 EXPECT_EQ(time_factory_->TimeNow().ToInternalValue(),
328 restored_tab->timestamp.ToInternalValue()); 343 restored_tab->timestamp.ToInternalValue());
329 } 344 }
330 345
331 // Make sure we don't persist entries to disk that have post data. This 346 // Make sure we don't persist entries to disk that have post data. This
332 // differs from DontPersistPostData1 in that all the navigations have post 347 // differs from DontPersistPostData1 in that all the navigations have post
333 // data, so that nothing should be persisted. 348 // data, so that nothing should be persisted.
334 TEST_F(TabRestoreServiceTest, DontLoadTwice) { 349 TEST_F(PersistentTabRestoreServiceTest, DontLoadTwice) {
335 AddThreeNavigations(); 350 AddThreeNavigations();
336 351
337 // Have the service record the tab. 352 // Have the service record the tab.
338 service_->CreateHistoricalTab(web_contents(), -1); 353 service_->CreateHistoricalTab(web_contents(), -1);
339 ASSERT_EQ(1U, service_->entries().size()); 354 ASSERT_EQ(1U, service_->entries().size());
340 355
341 // Recreate the service and have it load the tabs. 356 // Recreate the service and have it load the tabs.
342 RecreateService(); 357 RecreateService();
343 358
344 service_->LoadTabsFromLastSession(); 359 service_->LoadTabsFromLastSession();
345 360
346 // There should only be one entry. 361 // There should only be one entry.
347 ASSERT_EQ(1U, service_->entries().size()); 362 ASSERT_EQ(1U, service_->entries().size());
348 } 363 }
349 364
350 // Makes sure we load the previous session as necessary. 365 // Makes sure we load the previous session as necessary.
351 TEST_F(TabRestoreServiceTest, LoadPreviousSession) { 366 TEST_F(PersistentTabRestoreServiceTest, LoadPreviousSession) {
352 CreateSessionServiceWithOneWindow(false); 367 CreateSessionServiceWithOneWindow(false);
353 368
354 SessionServiceFactory::GetForProfile(profile())-> 369 SessionServiceFactory::GetForProfile(profile())->
355 MoveCurrentSessionToLastSession(); 370 MoveCurrentSessionToLastSession();
356 371
357 service_->LoadTabsFromLastSession(); 372 service_->LoadTabsFromLastSession();
358 373
359 // Make sure we get back one entry with one tab whose url is url1. 374 // Make sure we get back one entry with one tab whose url is url1.
360 ASSERT_EQ(1U, service_->entries().size()); 375 ASSERT_EQ(1U, service_->entries().size());
361 TabRestoreService::Entry* entry2 = service_->entries().front(); 376 TabRestoreService::Entry* entry2 = service_->entries().front();
362 ASSERT_EQ(TabRestoreService::WINDOW, entry2->type); 377 ASSERT_EQ(TabRestoreService::WINDOW, entry2->type);
363 TabRestoreService::Window* window = 378 TabRestoreService::Window* window =
364 static_cast<TabRestoreService::Window*>(entry2); 379 static_cast<TabRestoreService::Window*>(entry2);
365 ASSERT_EQ(1U, window->tabs.size()); 380 ASSERT_EQ(1U, window->tabs.size());
366 EXPECT_EQ(0, window->timestamp.ToInternalValue()); 381 EXPECT_EQ(0, window->timestamp.ToInternalValue());
367 EXPECT_EQ(0, window->selected_tab_index); 382 EXPECT_EQ(0, window->selected_tab_index);
368 ASSERT_EQ(1U, window->tabs[0].navigations.size()); 383 ASSERT_EQ(1U, window->tabs[0].navigations.size());
369 EXPECT_EQ(0, window->tabs[0].current_navigation_index); 384 EXPECT_EQ(0, window->tabs[0].current_navigation_index);
370 EXPECT_EQ(0, window->tabs[0].timestamp.ToInternalValue()); 385 EXPECT_EQ(0, window->tabs[0].timestamp.ToInternalValue());
371 EXPECT_TRUE(url1_ == window->tabs[0].navigations[0].virtual_url()); 386 EXPECT_TRUE(url1_ == window->tabs[0].navigations[0].virtual_url());
372 } 387 }
373 388
374 // Makes sure we don't attempt to load previous sessions after a restore. 389 // Makes sure we don't attempt to load previous sessions after a restore.
375 TEST_F(TabRestoreServiceTest, DontLoadAfterRestore) { 390 TEST_F(PersistentTabRestoreServiceTest, DontLoadAfterRestore) {
376 CreateSessionServiceWithOneWindow(false); 391 CreateSessionServiceWithOneWindow(false);
377 392
378 SessionServiceFactory::GetForProfile(profile())-> 393 SessionServiceFactory::GetForProfile(profile())->
379 MoveCurrentSessionToLastSession(); 394 MoveCurrentSessionToLastSession();
380 395
381 profile()->set_restored_last_session(true); 396 profile()->set_restored_last_session(true);
382 397
383 service_->LoadTabsFromLastSession(); 398 service_->LoadTabsFromLastSession();
384 399
385 // Because we restored a session TabRestoreService shouldn't load the tabs. 400 // Because we restored a session PersistentTabRestoreService shouldn't load
401 // the tabs.
386 ASSERT_EQ(0U, service_->entries().size()); 402 ASSERT_EQ(0U, service_->entries().size());
387 } 403 }
388 404
389 // Makes sure we don't attempt to load previous sessions after a clean exit. 405 // Makes sure we don't attempt to load previous sessions after a clean exit.
390 TEST_F(TabRestoreServiceTest, DontLoadAfterCleanExit) { 406 TEST_F(PersistentTabRestoreServiceTest, DontLoadAfterCleanExit) {
391 CreateSessionServiceWithOneWindow(false); 407 CreateSessionServiceWithOneWindow(false);
392 408
393 SessionServiceFactory::GetForProfile(profile())-> 409 SessionServiceFactory::GetForProfile(profile())->
394 MoveCurrentSessionToLastSession(); 410 MoveCurrentSessionToLastSession();
395 411
396 profile()->set_last_session_exited_cleanly(true); 412 profile()->set_last_session_exited_cleanly(true);
397 413
398 service_->LoadTabsFromLastSession(); 414 service_->LoadTabsFromLastSession();
399 415
400 ASSERT_EQ(0U, service_->entries().size()); 416 ASSERT_EQ(0U, service_->entries().size());
401 } 417 }
402 418
403 TEST_F(TabRestoreServiceTest, LoadPreviousSessionAndTabs) { 419 TEST_F(PersistentTabRestoreServiceTest, LoadPreviousSessionAndTabs) {
404 CreateSessionServiceWithOneWindow(false); 420 CreateSessionServiceWithOneWindow(false);
405 421
406 SessionServiceFactory::GetForProfile(profile())-> 422 SessionServiceFactory::GetForProfile(profile())->
407 MoveCurrentSessionToLastSession(); 423 MoveCurrentSessionToLastSession();
408 424
409 AddThreeNavigations(); 425 AddThreeNavigations();
410 426
411 service_->CreateHistoricalTab(web_contents(), -1); 427 service_->CreateHistoricalTab(web_contents(), -1);
412 428
413 RecreateService(); 429 RecreateService();
(...skipping 22 matching lines...) Expand all
436 ASSERT_EQ(3U, tab->navigations.size()); 452 ASSERT_EQ(3U, tab->navigations.size());
437 EXPECT_EQ(2, tab->current_navigation_index); 453 EXPECT_EQ(2, tab->current_navigation_index);
438 EXPECT_EQ(time_factory_->TimeNow().ToInternalValue(), 454 EXPECT_EQ(time_factory_->TimeNow().ToInternalValue(),
439 tab->timestamp.ToInternalValue()); 455 tab->timestamp.ToInternalValue());
440 EXPECT_TRUE(url1_ == tab->navigations[0].virtual_url()); 456 EXPECT_TRUE(url1_ == tab->navigations[0].virtual_url());
441 EXPECT_TRUE(url2_ == tab->navigations[1].virtual_url()); 457 EXPECT_TRUE(url2_ == tab->navigations[1].virtual_url());
442 EXPECT_TRUE(url3_ == tab->navigations[2].virtual_url()); 458 EXPECT_TRUE(url3_ == tab->navigations[2].virtual_url());
443 } 459 }
444 460
445 // Make sure pinned state is correctly loaded from session service. 461 // Make sure pinned state is correctly loaded from session service.
446 TEST_F(TabRestoreServiceTest, LoadPreviousSessionAndTabsPinned) { 462 TEST_F(PersistentTabRestoreServiceTest, LoadPreviousSessionAndTabsPinned) {
447 CreateSessionServiceWithOneWindow(true); 463 CreateSessionServiceWithOneWindow(true);
448 464
449 SessionServiceFactory::GetForProfile(profile())-> 465 SessionServiceFactory::GetForProfile(profile())->
450 MoveCurrentSessionToLastSession(); 466 MoveCurrentSessionToLastSession();
451 467
452 AddThreeNavigations(); 468 AddThreeNavigations();
453 469
454 service_->CreateHistoricalTab(web_contents(), -1); 470 service_->CreateHistoricalTab(web_contents(), -1);
455 471
456 RecreateService(); 472 RecreateService();
(...skipping 18 matching lines...) Expand all
475 ASSERT_EQ(TabRestoreService::TAB, entry->type); 491 ASSERT_EQ(TabRestoreService::TAB, entry->type);
476 Tab* tab = static_cast<Tab*>(entry); 492 Tab* tab = static_cast<Tab*>(entry);
477 ASSERT_FALSE(tab->pinned); 493 ASSERT_FALSE(tab->pinned);
478 ASSERT_EQ(3U, tab->navigations.size()); 494 ASSERT_EQ(3U, tab->navigations.size());
479 EXPECT_EQ(2, tab->current_navigation_index); 495 EXPECT_EQ(2, tab->current_navigation_index);
480 EXPECT_TRUE(url1_ == tab->navigations[0].virtual_url()); 496 EXPECT_TRUE(url1_ == tab->navigations[0].virtual_url());
481 EXPECT_TRUE(url2_ == tab->navigations[1].virtual_url()); 497 EXPECT_TRUE(url2_ == tab->navigations[1].virtual_url());
482 EXPECT_TRUE(url3_ == tab->navigations[2].virtual_url()); 498 EXPECT_TRUE(url3_ == tab->navigations[2].virtual_url());
483 } 499 }
484 500
485 // Creates TabRestoreService::kMaxEntries + 1 windows in the session service 501 // Creates kMaxEntries + 1 windows in the session service and makes sure we only
486 // and makes sure we only get back TabRestoreService::kMaxEntries on restore. 502 // get back kMaxEntries on restore.
487 TEST_F(TabRestoreServiceTest, ManyWindowsInSessionService) { 503 TEST_F(PersistentTabRestoreServiceTest, ManyWindowsInSessionService) {
488 CreateSessionServiceWithOneWindow(false); 504 CreateSessionServiceWithOneWindow(false);
489 505
490 for (size_t i = 0; i < TabRestoreService::kMaxEntries; ++i) 506 for (size_t i = 0; i < kMaxEntries; ++i)
491 AddWindowWithOneTabToSessionService(false); 507 AddWindowWithOneTabToSessionService(false);
492 508
493 SessionServiceFactory::GetForProfile(profile())-> 509 SessionServiceFactory::GetForProfile(profile())->
494 MoveCurrentSessionToLastSession(); 510 MoveCurrentSessionToLastSession();
495 511
496 AddThreeNavigations(); 512 AddThreeNavigations();
497 513
498 service_->CreateHistoricalTab(web_contents(), -1); 514 service_->CreateHistoricalTab(web_contents(), -1);
499 515
500 RecreateService(); 516 RecreateService();
501 517
502 // We should get back kMaxEntries entries. We added more, but 518 // We should get back kMaxEntries entries. We added more, but
503 // TabRestoreService only allows up to kMaxEntries. 519 // TabRestoreService only allows up to kMaxEntries.
504 ASSERT_EQ(TabRestoreService::kMaxEntries, service_->entries().size()); 520 ASSERT_EQ(kMaxEntries, service_->entries().size());
505 521
506 // The first entry should come from the session service. 522 // The first entry should come from the session service.
507 TabRestoreService::Entry* entry = service_->entries().front(); 523 TabRestoreService::Entry* entry = service_->entries().front();
508 ASSERT_EQ(TabRestoreService::WINDOW, entry->type); 524 ASSERT_EQ(TabRestoreService::WINDOW, entry->type);
509 TabRestoreService::Window* window = 525 TabRestoreService::Window* window =
510 static_cast<TabRestoreService::Window*>(entry); 526 static_cast<TabRestoreService::Window*>(entry);
511 ASSERT_EQ(1U, window->tabs.size()); 527 ASSERT_EQ(1U, window->tabs.size());
512 EXPECT_EQ(0, window->selected_tab_index); 528 EXPECT_EQ(0, window->selected_tab_index);
513 EXPECT_EQ(0, window->timestamp.ToInternalValue()); 529 EXPECT_EQ(0, window->timestamp.ToInternalValue());
514 ASSERT_EQ(1U, window->tabs[0].navigations.size()); 530 ASSERT_EQ(1U, window->tabs[0].navigations.size());
515 EXPECT_EQ(0, window->tabs[0].current_navigation_index); 531 EXPECT_EQ(0, window->tabs[0].current_navigation_index);
516 EXPECT_EQ(0, window->tabs[0].timestamp.ToInternalValue()); 532 EXPECT_EQ(0, window->tabs[0].timestamp.ToInternalValue());
517 EXPECT_TRUE(url1_ == window->tabs[0].navigations[0].virtual_url()); 533 EXPECT_TRUE(url1_ == window->tabs[0].navigations[0].virtual_url());
518 } 534 }
519 535
520 // Makes sure we restore timestamps correctly. 536 // Makes sure we restore timestamps correctly.
521 TEST_F(TabRestoreServiceTest, TimestampSurvivesRestore) { 537 TEST_F(PersistentTabRestoreServiceTest, TimestampSurvivesRestore) {
522 base::Time tab_timestamp(base::Time::FromInternalValue(123456789)); 538 base::Time tab_timestamp(base::Time::FromInternalValue(123456789));
523 539
524 AddThreeNavigations(); 540 AddThreeNavigations();
525 541
526 // Have the service record the tab. 542 // Have the service record the tab.
527 service_->CreateHistoricalTab(web_contents(), -1); 543 service_->CreateHistoricalTab(web_contents(), -1);
528 544
529 // Make sure an entry was created. 545 // Make sure an entry was created.
530 ASSERT_EQ(1U, service_->entries().size()); 546 ASSERT_EQ(1U, service_->entries().size());
531 547
(...skipping 30 matching lines...) Expand all
562 EXPECT_EQ(tab_timestamp.ToInternalValue(), 578 EXPECT_EQ(tab_timestamp.ToInternalValue(),
563 restored_tab->timestamp.ToInternalValue()); 579 restored_tab->timestamp.ToInternalValue());
564 ASSERT_EQ(old_navigations.size(), restored_tab->navigations.size()); 580 ASSERT_EQ(old_navigations.size(), restored_tab->navigations.size());
565 for (size_t i = 0; i < restored_tab->navigations.size(); ++i) { 581 for (size_t i = 0; i < restored_tab->navigations.size(); ++i) {
566 EXPECT_EQ( 582 EXPECT_EQ(
567 SessionTypesTestHelper::GetTimestamp(old_navigations[i]), 583 SessionTypesTestHelper::GetTimestamp(old_navigations[i]),
568 SessionTypesTestHelper::GetTimestamp(restored_tab->navigations[i])); 584 SessionTypesTestHelper::GetTimestamp(restored_tab->navigations[i]));
569 } 585 }
570 } 586 }
571 587
572 TEST_F(TabRestoreServiceTest, PruneEntries) { 588 TEST_F(PersistentTabRestoreServiceTest, PruneEntries) {
573 service_->ClearEntries(); 589 service_->ClearEntries();
574 ASSERT_TRUE(service_->entries().empty()); 590 ASSERT_TRUE(service_->entries().empty());
575 591
576 const size_t max_entries = TabRestoreService::kMaxEntries; 592 const size_t max_entries = kMaxEntries;
577 for (size_t i = 0; i < max_entries + 5; i++) { 593 for (size_t i = 0; i < max_entries + 5; i++) {
578 TabNavigation navigation = 594 TabNavigation navigation =
579 SessionTypesTestHelper::CreateNavigation( 595 SessionTypesTestHelper::CreateNavigation(
580 StringPrintf("http://%d", static_cast<int>(i)), 596 StringPrintf("http://%d", static_cast<int>(i)),
581 StringPrintf("%d", static_cast<int>(i))); 597 StringPrintf("%d", static_cast<int>(i)));
582 598
583 Tab* tab = new Tab(); 599 Tab* tab = new Tab();
584 tab->navigations.push_back(navigation); 600 tab->navigations.push_back(navigation);
585 tab->current_navigation_index = 0; 601 tab->current_navigation_index = 0;
586 602
587 service_->entries_.push_back(tab); 603 mutable_entries()->push_back(tab);
588 } 604 }
589 605
590 // Only keep kMaxEntries around. 606 // Only keep kMaxEntries around.
591 EXPECT_EQ(max_entries + 5, service_->entries_.size()); 607 EXPECT_EQ(max_entries + 5, service_->entries().size());
592 service_->PruneEntries(); 608 PruneEntries();
593 EXPECT_EQ(max_entries, service_->entries_.size()); 609 EXPECT_EQ(max_entries, service_->entries().size());
594 // Pruning again does nothing. 610 // Pruning again does nothing.
595 service_->PruneEntries(); 611 PruneEntries();
596 EXPECT_EQ(max_entries, service_->entries_.size()); 612 EXPECT_EQ(max_entries, service_->entries().size());
597 613
598 // Prune older first. 614 // Prune older first.
599 const char kRecentUrl[] = "http://recent"; 615 const char kRecentUrl[] = "http://recent";
600 TabNavigation navigation = 616 TabNavigation navigation =
601 SessionTypesTestHelper::CreateNavigation(kRecentUrl, "Most recent"); 617 SessionTypesTestHelper::CreateNavigation(kRecentUrl, "Most recent");
602 Tab* tab = new Tab(); 618 Tab* tab = new Tab();
603 tab->navigations.push_back(navigation); 619 tab->navigations.push_back(navigation);
604 tab->current_navigation_index = 0; 620 tab->current_navigation_index = 0;
605 service_->entries_.push_front(tab); 621 mutable_entries()->push_front(tab);
606 EXPECT_EQ(max_entries + 1, service_->entries_.size()); 622 EXPECT_EQ(max_entries + 1, service_->entries().size());
607 service_->PruneEntries(); 623 PruneEntries();
608 EXPECT_EQ(max_entries, service_->entries_.size()); 624 EXPECT_EQ(max_entries, service_->entries().size());
609 EXPECT_EQ(GURL(kRecentUrl), 625 EXPECT_EQ(GURL(kRecentUrl),
610 static_cast<Tab*>(service_->entries_.front())-> 626 static_cast<Tab*>(service_->entries().front())->
611 navigations[0].virtual_url()); 627 navigations[0].virtual_url());
612 628
613 // Ignore NTPs. 629 // Ignore NTPs.
614 navigation = 630 navigation =
615 SessionTypesTestHelper::CreateNavigation( 631 SessionTypesTestHelper::CreateNavigation(
616 chrome::kChromeUINewTabURL, "New tab"); 632 chrome::kChromeUINewTabURL, "New tab");
617 633
618 tab = new Tab(); 634 tab = new Tab();
619 tab->navigations.push_back(navigation); 635 tab->navigations.push_back(navigation);
620 tab->current_navigation_index = 0; 636 tab->current_navigation_index = 0;
621 service_->entries_.push_front(tab); 637 mutable_entries()->push_front(tab);
622 638
623 EXPECT_EQ(max_entries + 1, service_->entries_.size()); 639 EXPECT_EQ(max_entries + 1, service_->entries().size());
624 service_->PruneEntries(); 640 PruneEntries();
625 EXPECT_EQ(max_entries, service_->entries_.size()); 641 EXPECT_EQ(max_entries, service_->entries().size());
626 EXPECT_EQ(GURL(kRecentUrl), 642 EXPECT_EQ(GURL(kRecentUrl),
627 static_cast<Tab*>(service_->entries_.front())-> 643 static_cast<Tab*>(service_->entries().front())->
628 navigations[0].virtual_url()); 644 navigations[0].virtual_url());
629 645
630 // Don't prune pinned NTPs. 646 // Don't prune pinned NTPs.
631 tab = new Tab(); 647 tab = new Tab();
632 tab->pinned = true; 648 tab->pinned = true;
633 tab->current_navigation_index = 0; 649 tab->current_navigation_index = 0;
634 tab->navigations.push_back(navigation); 650 tab->navigations.push_back(navigation);
635 service_->entries_.push_front(tab); 651 mutable_entries()->push_front(tab);
636 EXPECT_EQ(max_entries + 1, service_->entries_.size()); 652 EXPECT_EQ(max_entries + 1, service_->entries().size());
637 service_->PruneEntries(); 653 PruneEntries();
638 EXPECT_EQ(max_entries, service_->entries_.size()); 654 EXPECT_EQ(max_entries, service_->entries().size());
639 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), 655 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
640 static_cast<Tab*>(service_->entries_.front())-> 656 static_cast<Tab*>(service_->entries().front())->
641 navigations[0].virtual_url()); 657 navigations[0].virtual_url());
642 658
643 // Don't prune NTPs that have multiple navigations. 659 // Don't prune NTPs that have multiple navigations.
644 // (Erase the last NTP first.) 660 // (Erase the last NTP first.)
645 delete service_->entries_.front(); 661 delete service_->entries().front();
646 service_->entries_.erase(service_->entries_.begin()); 662 mutable_entries()->erase(mutable_entries()->begin());
647 tab = new Tab(); 663 tab = new Tab();
648 tab->current_navigation_index = 1; 664 tab->current_navigation_index = 1;
649 tab->navigations.push_back(navigation); 665 tab->navigations.push_back(navigation);
650 tab->navigations.push_back(navigation); 666 tab->navigations.push_back(navigation);
651 service_->entries_.push_front(tab); 667 mutable_entries()->push_front(tab);
652 EXPECT_EQ(max_entries, service_->entries_.size()); 668 EXPECT_EQ(max_entries, service_->entries().size());
653 service_->PruneEntries(); 669 PruneEntries();
654 EXPECT_EQ(max_entries, service_->entries_.size()); 670 EXPECT_EQ(max_entries, service_->entries().size());
655 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), 671 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
656 static_cast<Tab*>(service_->entries_.front())-> 672 static_cast<Tab*>(service_->entries().front())->
657 navigations[1].virtual_url()); 673 navigations[1].virtual_url());
658 } 674 }
659 675
660 // Regression test for crbug.com/106082 676 // Regression test for crbug.com/106082
661 TEST_F(TabRestoreServiceTest, PruneIsCalled) { 677 TEST_F(PersistentTabRestoreServiceTest, PruneIsCalled) {
662 CreateSessionServiceWithOneWindow(false); 678 CreateSessionServiceWithOneWindow(false);
663 679
664 SessionServiceFactory::GetForProfile(profile())-> 680 SessionServiceFactory::GetForProfile(profile())->
665 MoveCurrentSessionToLastSession(); 681 MoveCurrentSessionToLastSession();
666 682
667 profile()->set_restored_last_session(true); 683 profile()->set_restored_last_session(true);
668 684
669 const size_t max_entries = TabRestoreService::kMaxEntries; 685 const size_t max_entries = kMaxEntries;
670 for (size_t i = 0; i < max_entries + 5; i++) { 686 for (size_t i = 0; i < max_entries + 5; i++) {
671 NavigateAndCommit(GURL(StringPrintf("http://%d", static_cast<int>(i)))); 687 NavigateAndCommit(GURL(StringPrintf("http://%d", static_cast<int>(i))));
672 service_->CreateHistoricalTab(web_contents(), -1); 688 service_->CreateHistoricalTab(web_contents(), -1);
673 } 689 }
674 690
675 EXPECT_EQ(max_entries, service_->entries_.size()); 691 EXPECT_EQ(max_entries, service_->entries().size());
676 // This should not crash. 692 // This should not crash.
677 service_->LoadTabsFromLastSession(); 693 service_->LoadTabsFromLastSession();
678 EXPECT_EQ(max_entries, service_->entries_.size()); 694 EXPECT_EQ(max_entries, service_->entries().size());
679 } 695 }
OLDNEW
« no previous file with comments | « chrome/browser/sessions/persistent_tab_restore_service.cc ('k') | chrome/browser/sessions/session_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698