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

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

Powered by Google App Engine
This is Rietveld 408576698