Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / sessions / session_restore_browsertest.cc
blob029200b4a1c84b936f11e237619a3a790798a7d6
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.
5 #include <vector>
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/process/launch.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/time/time.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/defaults.h"
15 #include "chrome/browser/first_run/first_run.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "chrome/browser/sessions/session_restore.h"
19 #include "chrome/browser/sessions/session_service.h"
20 #include "chrome/browser/sessions/session_service_factory.h"
21 #include "chrome/browser/sessions/session_service_test_helper.h"
22 #include "chrome/browser/sessions/session_types.h"
23 #include "chrome/browser/sessions/tab_restore_service.h"
24 #include "chrome/browser/sessions/tab_restore_service_factory.h"
25 #include "chrome/browser/ui/browser.h"
26 #include "chrome/browser/ui/browser_commands.h"
27 #include "chrome/browser/ui/browser_list.h"
28 #include "chrome/browser/ui/browser_tabstrip.h"
29 #include "chrome/browser/ui/browser_window.h"
30 #include "chrome/browser/ui/host_desktop.h"
31 #include "chrome/browser/ui/tabs/tab_strip_model.h"
32 #include "chrome/common/chrome_switches.h"
33 #include "chrome/common/url_constants.h"
34 #include "chrome/test/base/in_process_browser_test.h"
35 #include "chrome/test/base/test_switches.h"
36 #include "chrome/test/base/ui_test_utils.h"
37 #include "components/sessions/serialized_navigation_entry_test_helper.h"
38 #include "content/public/browser/navigation_controller.h"
39 #include "content/public/browser/navigation_entry.h"
40 #include "content/public/browser/notification_service.h"
41 #include "content/public/browser/notification_types.h"
42 #include "content/public/browser/render_process_host.h"
43 #include "content/public/browser/render_view_host.h"
44 #include "content/public/browser/web_contents.h"
45 #include "content/public/common/bindings_policy.h"
46 #include "content/public/common/page_transition_types.h"
47 #include "content/public/test/browser_test_utils.h"
48 #include "content/public/test/test_navigation_observer.h"
49 #include "sync/protocol/session_specifics.pb.h"
51 using sessions::SerializedNavigationEntry;
52 using sessions::SerializedNavigationEntryTestHelper;
54 #if defined(OS_MACOSX)
55 #include "base/mac/scoped_nsautorelease_pool.h"
56 #endif
58 #if defined(USE_AURA)
59 #include "ui/aura/window.h"
60 #endif
62 class SessionRestoreTest : public InProcessBrowserTest {
63 public:
64 SessionRestoreTest() : active_browser_list_(NULL) {}
66 protected:
67 #if defined(OS_CHROMEOS)
68 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
69 // TODO(nkostylev): Investigate if we can remove this switch.
70 command_line->AppendSwitch(switches::kCreateBrowserOnStartupForTests);
71 InProcessBrowserTest::SetUpCommandLine(command_line);
73 #endif
75 virtual void SetUpOnMainThread() OVERRIDE {
76 active_browser_list_ = BrowserList::GetInstance(chrome::GetActiveDesktop());
78 SessionStartupPref pref(SessionStartupPref::LAST);
79 SessionStartupPref::SetStartupPref(browser()->profile(), pref);
80 #if defined(OS_CHROMEOS)
81 const testing::TestInfo* const test_info =
82 testing::UnitTest::GetInstance()->current_test_info();
83 if (strcmp(test_info->name(), "NoSessionRestoreNewWindowChromeOS") != 0) {
84 // Undo the effect of kBrowserAliveWithNoWindows in defaults.cc so that we
85 // can get these test to work without quitting.
86 SessionServiceTestHelper helper(
87 SessionServiceFactory::GetForProfile(browser()->profile()));
88 helper.SetForceBrowserNotAliveWithNoWindows(true);
89 helper.ReleaseService();
91 #endif
93 InProcessBrowserTest::SetUpOnMainThread();
96 virtual bool SetUpUserDataDirectory() OVERRIDE {
97 url1_ = ui_test_utils::GetTestUrl(
98 base::FilePath().AppendASCII("session_history"),
99 base::FilePath().AppendASCII("bot1.html"));
100 url2_ = ui_test_utils::GetTestUrl(
101 base::FilePath().AppendASCII("session_history"),
102 base::FilePath().AppendASCII("bot2.html"));
103 url3_ = ui_test_utils::GetTestUrl(
104 base::FilePath().AppendASCII("session_history"),
105 base::FilePath().AppendASCII("bot3.html"));
107 return InProcessBrowserTest::SetUpUserDataDirectory();
110 void CloseBrowserSynchronously(Browser* browser) {
111 content::WindowedNotificationObserver observer(
112 chrome::NOTIFICATION_BROWSER_CLOSED,
113 content::NotificationService::AllSources());
114 browser->window()->Close();
115 #if defined(OS_MACOSX)
116 // BrowserWindowController depends on the auto release pool being recycled
117 // in the message loop to delete itself, which frees the Browser object
118 // which fires this event.
119 AutoreleasePool()->Recycle();
120 #endif
121 observer.Wait();
124 Browser* QuitBrowserAndRestore(Browser* browser, int expected_tab_count) {
125 return QuitBrowserAndRestoreWithURL(browser, expected_tab_count, GURL());
128 Browser* QuitBrowserAndRestoreWithURL(Browser* browser,
129 int expected_tab_count,
130 const GURL& url) {
131 Profile* profile = browser->profile();
133 // Close the browser.
134 g_browser_process->AddRefModule();
135 CloseBrowserSynchronously(browser);
137 // Create a new window, which should trigger session restore.
138 ui_test_utils::BrowserAddedObserver window_observer;
139 content::WindowedNotificationObserver restore_observer(
140 chrome::NOTIFICATION_SESSION_RESTORE_DONE,
141 content::NotificationService::AllSources());
142 if (url.is_empty()) {
143 chrome::NewEmptyWindow(profile, chrome::HOST_DESKTOP_TYPE_NATIVE);
144 } else {
145 chrome::NavigateParams params(profile,
146 url,
147 content::PAGE_TRANSITION_LINK);
148 chrome::Navigate(&params);
150 Browser* new_browser = window_observer.WaitForSingleNewBrowser();
151 restore_observer.Wait();
152 g_browser_process->ReleaseModule();
154 return new_browser;
157 void GoBack(Browser* browser) {
158 content::TestNavigationObserver observer(
159 browser->tab_strip_model()->GetActiveWebContents());
160 chrome::GoBack(browser, CURRENT_TAB);
161 observer.Wait();
164 void GoForward(Browser* browser) {
165 content::TestNavigationObserver observer(
166 browser->tab_strip_model()->GetActiveWebContents());
167 chrome::GoForward(browser, CURRENT_TAB);
168 observer.Wait();
171 void AssertOneWindowWithOneTab(Browser* browser) {
172 ASSERT_EQ(1u, active_browser_list_->size());
173 ASSERT_EQ(1, browser->tab_strip_model()->count());
176 int RenderProcessHostCount() {
177 content::RenderProcessHost::iterator hosts =
178 content::RenderProcessHost::AllHostsIterator();
179 int count = 0;
180 while (!hosts.IsAtEnd()) {
181 if (hosts.GetCurrentValue()->HasConnection())
182 count++;
183 hosts.Advance();
185 return count;
188 GURL url1_;
189 GURL url2_;
190 GURL url3_;
192 const BrowserList* active_browser_list_;
195 #if defined(USE_AURA)
196 // Verifies that restored tabs have a root window. This is important
197 // otherwise the wrong information is communicated to the renderer.
198 // (http://crbug.com/342672).
199 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, RestoredTabsShouldHaveRootWindow) {
200 // Create tabs.
201 ui_test_utils::NavigateToURLWithDisposition(
202 browser(), GURL(content::kAboutBlankURL), NEW_FOREGROUND_TAB,
203 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
204 ui_test_utils::NavigateToURLWithDisposition(
205 browser(), GURL(content::kAboutBlankURL), NEW_BACKGROUND_TAB,
206 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
208 // Restart and session restore the tabs.
209 Browser* restored = QuitBrowserAndRestore(browser(), 3);
210 TabStripModel* tab_strip_model = restored->tab_strip_model();
211 const int tabs = tab_strip_model->count();
212 ASSERT_EQ(3, tabs);
214 // Check the restored tabs have a root window.
215 for (int i = 0; i < tabs; ++i) {
216 content::WebContents* contents = tab_strip_model->GetWebContentsAt(i);
217 gfx::NativeView window = contents->GetNativeView();
218 bool tab_has_root_window = !!window->GetRootWindow();
219 EXPECT_TRUE(tab_has_root_window);
222 #endif // USE_AURA
224 // Verify that restored tabs have correct disposition. Only one tab should
225 // have "visible" visibility state, the rest should not.
226 // (http://crbug.com/155365 http://crbug.com/118269)
227 IN_PROC_BROWSER_TEST_F(SessionRestoreTest,
228 RestoredTabsHaveCorrectVisibilityState) {
229 // Create tabs.
230 GURL test_page(ui_test_utils::GetTestUrl(base::FilePath(),
231 base::FilePath(FILE_PATH_LITERAL("tab-restore-visibilty.html"))));
232 ui_test_utils::NavigateToURLWithDisposition(
233 browser(), test_page, NEW_FOREGROUND_TAB,
234 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
235 ui_test_utils::NavigateToURLWithDisposition(
236 browser(), test_page, NEW_BACKGROUND_TAB,
237 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
239 // Restart and session restore the tabs.
240 content::DOMMessageQueue message_queue;
241 Browser* restored = QuitBrowserAndRestore(browser(), 3);
242 for (int i = 0; i < 2; ++i) {
243 std::string message;
244 EXPECT_TRUE(message_queue.WaitForMessage(&message));
245 EXPECT_EQ("\"READY\"", message);
248 // There should be 3 restored tabs in the new browser.
249 TabStripModel* tab_strip_model = restored->tab_strip_model();
250 const int tabs = tab_strip_model->count();
251 ASSERT_EQ(3, tabs);
253 // The middle tab only should have visible disposition.
254 for (int i = 0; i < tabs; ++i) {
255 content::WebContents* contents = tab_strip_model->GetWebContentsAt(i);
256 std::string document_visibility_state;
257 const char kGetStateJS[] = "window.domAutomationController.send("
258 "window.document.visibilityState);";
259 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
260 contents, kGetStateJS, &document_visibility_state));
261 if (i == 1) {
262 EXPECT_EQ("visible", document_visibility_state);
263 } else {
264 EXPECT_EQ("hidden", document_visibility_state);
269 #if defined(OS_CHROMEOS)
270 // Verify that session restore does not occur when a user opens a browser window
271 // when no other browser windows are open on ChromeOS.
272 // TODO(pkotwicz): Add test which doesn't open incognito browser once
273 // disable-zero-browsers-open-for-tests is removed.
274 // (http://crbug.com/119175)
275 // TODO(pkotwicz): Mac should have the behavior outlined by this test. It should
276 // not do session restore if an incognito window is already open.
277 // (http://crbug.com/120927)
278 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, NoSessionRestoreNewWindowChromeOS) {
279 GURL url(ui_test_utils::GetTestUrl(
280 base::FilePath(base::FilePath::kCurrentDirectory),
281 base::FilePath(FILE_PATH_LITERAL("title1.html"))));
283 // Add a single tab.
284 ui_test_utils::NavigateToURL(browser(), url);
286 Browser* incognito_browser = CreateIncognitoBrowser();
287 chrome::AddTabAt(incognito_browser, GURL(), -1, true);
288 incognito_browser->window()->Show();
290 // Close the normal browser. After this we only have the incognito window
291 // open.
292 CloseBrowserSynchronously(browser());
294 // Create a new window, which should open NTP.
295 ui_test_utils::BrowserAddedObserver browser_added_observer;
296 chrome::NewWindow(incognito_browser);
297 Browser* new_browser = browser_added_observer.WaitForSingleNewBrowser();
299 ASSERT_TRUE(new_browser);
300 EXPECT_EQ(1, new_browser->tab_strip_model()->count());
301 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
302 new_browser->tab_strip_model()->GetWebContentsAt(0)->GetURL());
305 // Test that maximized applications get restored maximized.
306 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, MaximizedApps) {
307 const char* app_name = "TestApp";
308 Browser* app_browser = CreateBrowserForApp(app_name, browser()->profile());
309 app_browser->window()->Maximize();
310 app_browser->window()->Show();
311 EXPECT_TRUE(app_browser->window()->IsMaximized());
312 EXPECT_TRUE(app_browser->is_app());
313 EXPECT_TRUE(app_browser->is_type_popup());
315 // Close the normal browser. After this we only have the app_browser window.
316 CloseBrowserSynchronously(browser());
318 // Create a new window, which should open NTP.
319 ui_test_utils::BrowserAddedObserver browser_added_observer;
320 chrome::NewWindow(app_browser);
321 Browser* new_browser = browser_added_observer.WaitForSingleNewBrowser();
323 ASSERT_TRUE(new_browser);
324 EXPECT_TRUE(app_browser->window()->IsMaximized());
325 EXPECT_TRUE(app_browser->is_app());
326 EXPECT_TRUE(app_browser->is_type_popup());
328 #endif // OS_CHROMEOS
330 #if !defined(OS_CHROMEOS)
331 // This test does not apply to ChromeOS as it does not do session restore when
332 // a new window is opened.
334 #if defined(OS_LINUX) && defined(TOOLKIT_VIEWS)
335 // Crashes on Linux Views: http://crbug.com/39476
336 #define MAYBE_RestoreOnNewWindowWithNoTabbedBrowsers \
337 DISABLED_RestoreOnNewWindowWithNoTabbedBrowsers
338 #else
339 #define MAYBE_RestoreOnNewWindowWithNoTabbedBrowsers \
340 RestoreOnNewWindowWithNoTabbedBrowsers
341 #endif
343 // Makes sure when session restore is triggered in the same process we don't end
344 // up with an extra tab.
345 IN_PROC_BROWSER_TEST_F(SessionRestoreTest,
346 MAYBE_RestoreOnNewWindowWithNoTabbedBrowsers) {
347 const base::FilePath::CharType* kTitle1File =
348 FILE_PATH_LITERAL("title1.html");
349 GURL url(ui_test_utils::GetTestUrl(base::FilePath(
350 base::FilePath::kCurrentDirectory), base::FilePath(kTitle1File)));
351 ui_test_utils::NavigateToURL(browser(), url);
353 // Turn on session restore.
354 SessionStartupPref::SetStartupPref(
355 browser()->profile(),
356 SessionStartupPref(SessionStartupPref::LAST));
358 // Create a new popup.
359 Profile* profile = browser()->profile();
360 Browser* popup =
361 new Browser(Browser::CreateParams(Browser::TYPE_POPUP, profile,
362 browser()->host_desktop_type()));
363 popup->window()->Show();
365 // Close the browser.
366 CloseBrowserSynchronously(browser());
368 // Create a new window, which should trigger session restore.
369 ui_test_utils::BrowserAddedObserver observer;
370 chrome::NewWindow(popup);
371 Browser* new_browser = observer.WaitForSingleNewBrowser();
373 ASSERT_TRUE(new_browser != NULL);
375 // The browser should only have one tab.
376 ASSERT_EQ(1, new_browser->tab_strip_model()->count());
378 // And the first url should be url.
379 EXPECT_EQ(url, new_browser->tab_strip_model()->GetWebContentsAt(0)->GetURL());
381 #endif // !OS_CHROMEOS
383 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, RestoreIndividualTabFromWindow) {
384 GURL url1(ui_test_utils::GetTestUrl(
385 base::FilePath(base::FilePath::kCurrentDirectory),
386 base::FilePath(FILE_PATH_LITERAL("title1.html"))));
387 // Any page that will yield a 200 status code will work here.
388 GURL url2("about:version");
389 GURL url3(ui_test_utils::GetTestUrl(
390 base::FilePath(base::FilePath::kCurrentDirectory),
391 base::FilePath(FILE_PATH_LITERAL("title3.html"))));
393 // Add and navigate three tabs.
394 ui_test_utils::NavigateToURL(browser(), url1);
396 content::WindowedNotificationObserver observer(
397 content::NOTIFICATION_LOAD_STOP,
398 content::NotificationService::AllSources());
399 chrome::AddSelectedTabWithURL(browser(), url2,
400 content::PAGE_TRANSITION_LINK);
401 observer.Wait();
404 content::WindowedNotificationObserver observer(
405 content::NOTIFICATION_LOAD_STOP,
406 content::NotificationService::AllSources());
407 chrome::AddSelectedTabWithURL(browser(), url3,
408 content::PAGE_TRANSITION_LINK);
409 observer.Wait();
412 TabRestoreService* service =
413 TabRestoreServiceFactory::GetForProfile(browser()->profile());
414 service->ClearEntries();
416 chrome::HostDesktopType host_desktop_type = browser()->host_desktop_type();
418 browser()->window()->Close();
420 // Expect a window with three tabs.
421 ASSERT_EQ(1U, service->entries().size());
422 ASSERT_EQ(TabRestoreService::WINDOW, service->entries().front()->type);
423 const TabRestoreService::Window* window =
424 static_cast<TabRestoreService::Window*>(service->entries().front());
425 EXPECT_EQ(3U, window->tabs.size());
427 // Find the SessionID for entry2. Since the session service was destroyed,
428 // there is no guarantee that the SessionID for the tab has remained the same.
429 base::Time timestamp;
430 int http_status_code = 0;
431 for (std::vector<TabRestoreService::Tab>::const_iterator it =
432 window->tabs.begin(); it != window->tabs.end(); ++it) {
433 const TabRestoreService::Tab& tab = *it;
434 // If this tab held url2, then restore this single tab.
435 if (tab.navigations[0].virtual_url() == url2) {
436 timestamp = tab.navigations[0].timestamp();
437 http_status_code = tab.navigations[0].http_status_code();
438 std::vector<content::WebContents*> content =
439 service->RestoreEntryById(NULL, tab.id, host_desktop_type, UNKNOWN);
440 ASSERT_EQ(1U, content.size());
441 ASSERT_TRUE(content[0]);
442 EXPECT_EQ(url2, content[0]->GetURL());
443 break;
446 EXPECT_FALSE(timestamp.is_null());
447 EXPECT_EQ(200, http_status_code);
449 // Make sure that the restored tab is removed from the service.
450 ASSERT_EQ(1U, service->entries().size());
451 ASSERT_EQ(TabRestoreService::WINDOW, service->entries().front()->type);
452 window = static_cast<TabRestoreService::Window*>(service->entries().front());
453 EXPECT_EQ(2U, window->tabs.size());
455 // Make sure that the restored tab was restored with the correct
456 // timestamp and status code.
457 const content::WebContents* contents =
458 browser()->tab_strip_model()->GetActiveWebContents();
459 ASSERT_TRUE(contents);
460 const content::NavigationEntry* entry =
461 contents->GetController().GetActiveEntry();
462 ASSERT_TRUE(entry);
463 EXPECT_EQ(timestamp, entry->GetTimestamp());
464 EXPECT_EQ(http_status_code, entry->GetHttpStatusCode());
467 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, WindowWithOneTab) {
468 GURL url(ui_test_utils::GetTestUrl(
469 base::FilePath(base::FilePath::kCurrentDirectory),
470 base::FilePath(FILE_PATH_LITERAL("title1.html"))));
472 // Add a single tab.
473 ui_test_utils::NavigateToURL(browser(), url);
475 TabRestoreService* service =
476 TabRestoreServiceFactory::GetForProfile(browser()->profile());
477 service->ClearEntries();
478 EXPECT_EQ(0U, service->entries().size());
480 chrome::HostDesktopType host_desktop_type = browser()->host_desktop_type();
482 // Close the window.
483 browser()->window()->Close();
485 // Expect the window to be converted to a tab by the TRS.
486 EXPECT_EQ(1U, service->entries().size());
487 ASSERT_EQ(TabRestoreService::TAB, service->entries().front()->type);
488 const TabRestoreService::Tab* tab =
489 static_cast<TabRestoreService::Tab*>(service->entries().front());
491 // Restore the tab.
492 std::vector<content::WebContents*> content =
493 service->RestoreEntryById(NULL, tab->id, host_desktop_type, UNKNOWN);
494 ASSERT_EQ(1U, content.size());
495 ASSERT_TRUE(content[0]);
496 EXPECT_EQ(url, content[0]->GetURL());
498 // Make sure the restore was successful.
499 EXPECT_EQ(0U, service->entries().size());
502 #if !defined(OS_CHROMEOS)
503 // This test does not apply to ChromeOS as ChromeOS does not do session
504 // restore when a new window is open.
506 // Verifies we remember the last browser window when closing the last
507 // non-incognito window while an incognito window is open.
508 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, IncognitotoNonIncognito) {
509 GURL url(ui_test_utils::GetTestUrl(
510 base::FilePath(base::FilePath::kCurrentDirectory),
511 base::FilePath(FILE_PATH_LITERAL("title1.html"))));
513 // Add a single tab.
514 ui_test_utils::NavigateToURL(browser(), url);
516 // Create a new incognito window.
517 Browser* incognito_browser = CreateIncognitoBrowser();
518 chrome::AddTabAt(incognito_browser, GURL(), -1, true);
519 incognito_browser->window()->Show();
521 // Close the normal browser. After this we only have the incognito window
522 // open.
523 CloseBrowserSynchronously(browser());
525 // Create a new window, which should trigger session restore.
526 ui_test_utils::BrowserAddedObserver browser_added_observer;
527 chrome::NewWindow(incognito_browser);
528 Browser* new_browser = browser_added_observer.WaitForSingleNewBrowser();
530 // The first tab should have 'url' as its url.
531 ASSERT_TRUE(new_browser);
532 EXPECT_EQ(url, new_browser->tab_strip_model()->GetWebContentsAt(0)->GetURL());
534 #endif // !OS_CHROMEOS
536 namespace {
538 // Verifies that the given NavigationController has exactly two
539 // entries that correspond to the given URLs and that all but the last
540 // entry have null timestamps.
541 void VerifyNavigationEntries(
542 const content::NavigationController& controller,
543 GURL url1, GURL url2) {
544 ASSERT_EQ(2, controller.GetEntryCount());
545 EXPECT_EQ(1, controller.GetCurrentEntryIndex());
546 EXPECT_EQ(url1, controller.GetEntryAtIndex(0)->GetURL());
547 EXPECT_EQ(url2, controller.GetEntryAtIndex(1)->GetURL());
548 EXPECT_TRUE(controller.GetEntryAtIndex(0)->GetTimestamp().is_null());
549 EXPECT_FALSE(controller.GetEntryAtIndex(1)->GetTimestamp().is_null());
552 } // namespace
554 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, RestoreForeignTab) {
555 GURL url1("http://google.com");
556 GURL url2("http://google2.com");
557 SerializedNavigationEntry nav1 =
558 SerializedNavigationEntryTestHelper::CreateNavigation(url1.spec(), "one");
559 SerializedNavigationEntry nav2 =
560 SerializedNavigationEntryTestHelper::CreateNavigation(url2.spec(), "two");
562 // Set up the restore data.
563 sync_pb::SessionTab sync_data;
564 sync_data.set_tab_visual_index(0);
565 sync_data.set_current_navigation_index(1);
566 sync_data.set_pinned(false);
567 sync_data.add_navigation()->CopyFrom(nav1.ToSyncData());
568 sync_data.add_navigation()->CopyFrom(nav2.ToSyncData());
570 SessionTab tab;
571 tab.SetFromSyncData(sync_data, base::Time::Now());
572 EXPECT_EQ(2U, tab.navigations.size());
573 for (size_t i = 0; i < tab.navigations.size(); ++i)
574 EXPECT_TRUE(tab.navigations[i].timestamp().is_null());
576 ASSERT_EQ(1, browser()->tab_strip_model()->count());
578 // Restore in the current tab.
579 content::WebContents* tab_content = NULL;
581 content::WindowedNotificationObserver observer(
582 content::NOTIFICATION_LOAD_STOP,
583 content::NotificationService::AllSources());
584 tab_content = SessionRestore::RestoreForeignSessionTab(
585 browser()->tab_strip_model()->GetActiveWebContents(), tab, CURRENT_TAB);
586 observer.Wait();
588 ASSERT_EQ(1, browser()->tab_strip_model()->count());
589 content::WebContents* web_contents =
590 browser()->tab_strip_model()->GetWebContentsAt(0);
591 VerifyNavigationEntries(web_contents->GetController(), url1, url2);
592 ASSERT_TRUE(web_contents->GetUserAgentOverride().empty());
593 ASSERT_TRUE(tab_content);
594 ASSERT_EQ(url2, tab_content->GetURL());
596 // Restore in a new tab.
597 tab_content = NULL;
599 content::WindowedNotificationObserver observer(
600 content::NOTIFICATION_LOAD_STOP,
601 content::NotificationService::AllSources());
602 tab_content = SessionRestore::RestoreForeignSessionTab(
603 browser()->tab_strip_model()->GetActiveWebContents(),
604 tab, NEW_BACKGROUND_TAB);
605 observer.Wait();
607 ASSERT_EQ(2, browser()->tab_strip_model()->count());
608 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
609 web_contents = browser()->tab_strip_model()->GetWebContentsAt(1);
610 VerifyNavigationEntries(web_contents->GetController(), url1, url2);
611 ASSERT_TRUE(web_contents->GetUserAgentOverride().empty());
612 ASSERT_TRUE(tab_content);
613 ASSERT_EQ(url2, tab_content->GetURL());
615 // Restore in a new window.
616 Browser* new_browser = NULL;
617 tab_content = NULL;
619 ui_test_utils::BrowserAddedObserver browser_observer;
620 content::WindowedNotificationObserver observer(
621 content::NOTIFICATION_LOAD_STOP,
622 content::NotificationService::AllSources());
623 tab_content = SessionRestore::RestoreForeignSessionTab(
624 browser()->tab_strip_model()->GetActiveWebContents(), tab, NEW_WINDOW);
625 new_browser = browser_observer.WaitForSingleNewBrowser();
626 observer.Wait();
629 ASSERT_EQ(1, new_browser->tab_strip_model()->count());
630 web_contents = new_browser->tab_strip_model()->GetWebContentsAt(0);
631 VerifyNavigationEntries(web_contents->GetController(), url1, url2);
632 ASSERT_TRUE(web_contents->GetUserAgentOverride().empty());
633 ASSERT_TRUE(tab_content);
634 ASSERT_EQ(url2, tab_content->GetURL());
637 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, RestoreForeignSession) {
638 Profile* profile = browser()->profile();
640 GURL url1("http://google.com");
641 GURL url2("http://google2.com");
642 SerializedNavigationEntry nav1 =
643 SerializedNavigationEntryTestHelper::CreateNavigation(url1.spec(), "one");
644 SerializedNavigationEntry nav2 =
645 SerializedNavigationEntryTestHelper::CreateNavigation(url2.spec(), "two");
646 SerializedNavigationEntryTestHelper::SetIsOverridingUserAgent(true, &nav2);
648 // Set up the restore data -- one window with two tabs.
649 std::vector<const SessionWindow*> session;
650 SessionWindow window;
651 SessionTab tab1;
653 sync_pb::SessionTab sync_data;
654 sync_data.set_tab_visual_index(0);
655 sync_data.set_current_navigation_index(0);
656 sync_data.set_pinned(true);
657 sync_data.add_navigation()->CopyFrom(nav1.ToSyncData());
658 tab1.SetFromSyncData(sync_data, base::Time::Now());
660 window.tabs.push_back(&tab1);
662 SessionTab tab2;
664 sync_pb::SessionTab sync_data;
665 sync_data.set_tab_visual_index(1);
666 sync_data.set_current_navigation_index(0);
667 sync_data.set_pinned(false);
668 sync_data.add_navigation()->CopyFrom(nav2.ToSyncData());
669 tab2.SetFromSyncData(sync_data, base::Time::Now());
671 window.tabs.push_back(&tab2);
673 // Leave tab3 empty. Should have no effect on restored session, but simulates
674 // partially complete foreign session data.
675 SessionTab tab3;
676 window.tabs.push_back(&tab3);
678 session.push_back(static_cast<const SessionWindow*>(&window));
679 ui_test_utils::BrowserAddedObserver window_observer;
680 std::vector<Browser*> browsers =
681 SessionRestore::RestoreForeignSessionWindows(
682 profile, browser()->host_desktop_type(), session.begin(),
683 session.end());
684 Browser* new_browser = window_observer.WaitForSingleNewBrowser();
685 ASSERT_TRUE(new_browser);
686 ASSERT_EQ(2u, active_browser_list_->size());
687 ASSERT_EQ(2, new_browser->tab_strip_model()->count());
689 ASSERT_EQ(1u, browsers.size());
690 ASSERT_TRUE(browsers[0]);
691 ASSERT_EQ(2, browsers[0]->tab_strip_model()->count());
693 content::WebContents* web_contents_1 =
694 new_browser->tab_strip_model()->GetWebContentsAt(0);
695 content::WebContents* web_contents_2 =
696 new_browser->tab_strip_model()->GetWebContentsAt(1);
697 ASSERT_EQ(url1, web_contents_1->GetURL());
698 ASSERT_EQ(url2, web_contents_2->GetURL());
700 // Check user agent override state.
701 ASSERT_TRUE(web_contents_1->GetUserAgentOverride().empty());
702 ASSERT_TRUE(web_contents_2->GetUserAgentOverride().empty());
704 content::NavigationEntry* entry =
705 web_contents_1->GetController().GetActiveEntry();
706 ASSERT_TRUE(entry);
707 ASSERT_FALSE(entry->GetIsOverridingUserAgent());
709 entry = web_contents_2->GetController().GetActiveEntry();
710 ASSERT_TRUE(entry);
711 ASSERT_FALSE(entry->GetIsOverridingUserAgent());
713 // The SessionWindow destructor deletes the tabs, so we have to clear them
714 // here to avoid a crash.
715 window.tabs.clear();
718 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, Basic) {
719 ui_test_utils::NavigateToURL(browser(), url1_);
720 ui_test_utils::NavigateToURL(browser(), url2_);
722 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
723 ASSERT_EQ(1u, active_browser_list_->size());
724 ASSERT_EQ(url2_,
725 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
726 GoBack(new_browser);
727 ASSERT_EQ(url1_,
728 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
731 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, RestoreWebUI) {
732 const GURL webui_url("chrome://omnibox");
733 ui_test_utils::NavigateToURL(browser(), webui_url);
734 const content::WebContents* old_tab =
735 browser()->tab_strip_model()->GetActiveWebContents();
736 EXPECT_EQ(content::BINDINGS_POLICY_WEB_UI,
737 old_tab->GetRenderViewHost()->GetEnabledBindings());
739 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
740 ASSERT_EQ(1u, active_browser_list_->size());
741 const content::WebContents* new_tab =
742 new_browser->tab_strip_model()->GetActiveWebContents();
743 EXPECT_EQ(webui_url, new_tab->GetURL());
744 EXPECT_EQ(content::BINDINGS_POLICY_WEB_UI,
745 new_tab->GetRenderViewHost()->GetEnabledBindings());
748 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, RestoreWebUISettings) {
749 const GURL webui_url("chrome://settings");
750 ui_test_utils::NavigateToURL(browser(), webui_url);
751 const content::WebContents* old_tab =
752 browser()->tab_strip_model()->GetActiveWebContents();
753 EXPECT_EQ(content::BINDINGS_POLICY_WEB_UI,
754 old_tab->GetRenderViewHost()->GetEnabledBindings());
756 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
757 ASSERT_EQ(1u, active_browser_list_->size());
758 const content::WebContents* new_tab =
759 new_browser->tab_strip_model()->GetActiveWebContents();
760 EXPECT_EQ(webui_url, new_tab->GetURL());
761 EXPECT_EQ(content::BINDINGS_POLICY_WEB_UI,
762 new_tab->GetRenderViewHost()->GetEnabledBindings());
765 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, RestoresForwardAndBackwardNavs) {
766 ui_test_utils::NavigateToURL(browser(), url1_);
767 ui_test_utils::NavigateToURL(browser(), url2_);
768 ui_test_utils::NavigateToURL(browser(), url3_);
770 GoBack(browser());
771 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
772 ASSERT_EQ(1u, active_browser_list_->size());
773 ASSERT_EQ(url2_,
774 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
775 GoForward(new_browser);
776 ASSERT_EQ(url3_,
777 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
778 GoBack(new_browser);
779 ASSERT_EQ(url2_,
780 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
782 // Test renderer-initiated back/forward as well.
783 GURL go_back_url("javascript:history.back();");
784 ui_test_utils::NavigateToURL(new_browser, go_back_url);
785 ASSERT_EQ(url1_,
786 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
789 // Tests that the SiteInstances used for entries in a restored tab's history
790 // are given appropriate max page IDs, so that going back to a restored
791 // cross-site page and then forward again works. (Bug 1204135)
792 // This test fails. See http://crbug.com/237497.
793 IN_PROC_BROWSER_TEST_F(SessionRestoreTest,
794 DISABLED_RestoresCrossSiteForwardAndBackwardNavs) {
795 ASSERT_TRUE(test_server()->Start());
797 GURL cross_site_url(test_server()->GetURL("files/title2.html"));
799 // Visit URLs on different sites.
800 ui_test_utils::NavigateToURL(browser(), url1_);
801 ui_test_utils::NavigateToURL(browser(), cross_site_url);
802 ui_test_utils::NavigateToURL(browser(), url2_);
804 GoBack(browser());
805 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
806 ASSERT_EQ(1u, active_browser_list_->size());
807 ASSERT_EQ(1, new_browser->tab_strip_model()->count());
809 // Check that back and forward work as expected.
810 ASSERT_EQ(cross_site_url,
811 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
813 GoBack(new_browser);
814 ASSERT_EQ(url1_,
815 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
817 GoForward(new_browser);
818 ASSERT_EQ(cross_site_url,
819 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
821 // Test renderer-initiated back/forward as well.
822 GURL go_forward_url("javascript:history.forward();");
823 ui_test_utils::NavigateToURL(new_browser, go_forward_url);
824 ASSERT_EQ(url2_,
825 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
828 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, TwoTabsSecondSelected) {
829 ui_test_utils::NavigateToURL(browser(), url1_);
831 ui_test_utils::NavigateToURLWithDisposition(
832 browser(), url2_, NEW_FOREGROUND_TAB,
833 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
835 Browser* new_browser = QuitBrowserAndRestore(browser(), 2);
837 ASSERT_EQ(1u, active_browser_list_->size());
838 ASSERT_EQ(2, new_browser->tab_strip_model()->count());
839 ASSERT_EQ(1, new_browser->tab_strip_model()->active_index());
840 ASSERT_EQ(url2_,
841 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
843 ASSERT_EQ(url1_,
844 new_browser->tab_strip_model()->GetWebContentsAt(0)->GetURL());
847 // Creates two tabs, closes one, quits and makes sure only one tab is restored.
848 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, ClosedTabStaysClosed) {
849 ui_test_utils::NavigateToURL(browser(), url1_);
851 ui_test_utils::NavigateToURLWithDisposition(
852 browser(), url2_, NEW_FOREGROUND_TAB,
853 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
854 chrome::CloseTab(browser());
856 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
858 AssertOneWindowWithOneTab(new_browser);
859 ASSERT_EQ(url1_,
860 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
863 // Ensures active tab properly restored when tabs before it closed.
864 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, ActiveIndexUpdatedAtClose) {
865 ui_test_utils::NavigateToURL(browser(), url1_);
866 ui_test_utils::NavigateToURLWithDisposition(
867 browser(), url2_, NEW_FOREGROUND_TAB,
868 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
869 ui_test_utils::NavigateToURLWithDisposition(
870 browser(), url3_, NEW_BACKGROUND_TAB,
871 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
873 browser()->tab_strip_model()->CloseWebContentsAt(
875 TabStripModel::CLOSE_CREATE_HISTORICAL_TAB);
877 Browser* new_browser = QuitBrowserAndRestore(browser(), 2);
879 ASSERT_EQ(url2_,
880 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
881 ASSERT_EQ(new_browser->tab_strip_model()->active_index(), 0);
884 // Ensures active tab properly restored when tabs are inserted before it .
885 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, ActiveIndexUpdatedAtInsert) {
886 ui_test_utils::NavigateToURL(browser(), url1_);
887 ui_test_utils::NavigateToURLWithDisposition(
888 browser(), url2_, NEW_BACKGROUND_TAB,
889 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
891 chrome::NavigateParams navigate_params(browser(), url3_,
892 content::PAGE_TRANSITION_TYPED);
893 navigate_params.tabstrip_index = 0;
894 navigate_params.disposition = NEW_BACKGROUND_TAB;
895 ui_test_utils::NavigateToURL(&navigate_params);
897 Browser* new_browser = QuitBrowserAndRestore(browser(), 3);
899 ASSERT_EQ(url1_,
900 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
901 ASSERT_EQ(new_browser->tab_strip_model()->active_index(), 1);
904 #if !defined(OS_CHROMEOS) && !defined(OS_MACOSX)
905 // This test doesn't apply to the Mac version; see GetCommandLineForRelaunch
906 // for details. It was disabled for a long time so might never have worked on
907 // ChromeOS.
909 // Launches an app window, closes tabbed browser, launches and makes sure
910 // we restore the tabbed browser url.
911 // If this test flakes, use http://crbug.com/29110
912 IN_PROC_BROWSER_TEST_F(SessionRestoreTest,
913 RestoreAfterClosingTabbedBrowserWithAppAndLaunching) {
914 #if defined(OS_WIN) && defined(USE_ASH)
915 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
916 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
917 return;
918 #endif
920 ui_test_utils::NavigateToURL(browser(), url1_);
922 // Launch an app.
923 CommandLine app_launch_arguments = GetCommandLineForRelaunch();
924 app_launch_arguments.AppendSwitchASCII(switches::kApp, url2_.spec());
926 ui_test_utils::BrowserAddedObserver window_observer;
928 base::LaunchProcess(app_launch_arguments, base::LaunchOptionsForTest(), NULL);
930 Browser* app_window = window_observer.WaitForSingleNewBrowser();
931 ASSERT_EQ(2u, active_browser_list_->size());
933 // Close the first window. The only window left is the App window.
934 CloseBrowserSynchronously(browser());
936 // Restore the session, which should bring back the first window with url1_.
937 Browser* new_browser = QuitBrowserAndRestore(app_window, 1);
939 AssertOneWindowWithOneTab(new_browser);
941 ASSERT_EQ(url1_,
942 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
945 #endif // !defined(OS_CHROMEOS) && !defined(OS_MACOSX)
947 // Creates two windows, closes one, restores, make sure only one window open.
948 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, TwoWindowsCloseOneRestoreOnlyOne) {
949 ui_test_utils::NavigateToURL(browser(), url1_);
951 // Open a second window.
952 ui_test_utils::NavigateToURLWithDisposition(
953 browser(), GURL(content::kAboutBlankURL), NEW_WINDOW,
954 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
956 ASSERT_EQ(2u, active_browser_list_->size());
958 // Close it.
959 Browser* new_window = active_browser_list_->get(1);
960 CloseBrowserSynchronously(new_window);
962 // Restart and make sure we have only one window with one tab and the url
963 // is url1_.
964 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
966 AssertOneWindowWithOneTab(new_browser);
968 ASSERT_EQ(url1_,
969 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
972 // Make sure after a restore the number of processes matches that of the number
973 // of processes running before the restore. This creates a new tab so that
974 // we should have two new tabs running. (This test will pass in both
975 // process-per-site and process-per-site-instance, because we treat the new tab
976 // as a special case in process-per-site-instance so that it only ever uses one
977 // process.)
979 // Flaky: http://code.google.com/p/chromium/issues/detail?id=52022
980 // Unfortunately, the fix at http://codereview.chromium.org/6546078
981 // breaks NTP background image refreshing, so ThemeSource had to revert to
982 // replacing the existing data source.
983 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, ShareProcessesOnRestore) {
984 // Create two new tabs.
985 ui_test_utils::NavigateToURLWithDisposition(
986 browser(), GURL(content::kAboutBlankURL), NEW_FOREGROUND_TAB,
987 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
988 ui_test_utils::NavigateToURLWithDisposition(
989 browser(), GURL(content::kAboutBlankURL), NEW_FOREGROUND_TAB,
990 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
992 int expected_process_count = RenderProcessHostCount();
994 // Restart.
995 Browser* new_browser = QuitBrowserAndRestore(browser(), 3);
997 ASSERT_EQ(3, new_browser->tab_strip_model()->count());
999 ASSERT_EQ(expected_process_count, RenderProcessHostCount());
1002 // Test that changing the user agent override will persist it to disk.
1003 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, PersistAndRestoreUserAgentOverride) {
1004 // Create a tab with an overridden user agent.
1005 ui_test_utils::NavigateToURL(browser(), url1_);
1006 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
1007 browser()->tab_strip_model()->GetWebContentsAt(0)->
1008 SetUserAgentOverride("override");
1010 // Create a tab without an overridden user agent.
1011 ui_test_utils::NavigateToURLWithDisposition(
1012 browser(), url2_, NEW_FOREGROUND_TAB,
1013 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1014 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
1016 // Kill the original browser then open a new one to trigger a restore.
1017 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
1018 ASSERT_EQ(1u, active_browser_list_->size());
1019 ASSERT_EQ(2, new_browser->tab_strip_model()->count());
1020 ASSERT_EQ(1, new_browser->tab_strip_model()->active_index());
1022 // Confirm that the user agent overrides are properly set.
1023 EXPECT_EQ("override",
1024 new_browser->tab_strip_model()->GetWebContentsAt(0)->
1025 GetUserAgentOverride());
1026 EXPECT_EQ("",
1027 new_browser->tab_strip_model()->GetWebContentsAt(1)->
1028 GetUserAgentOverride());
1031 // Regression test for crbug.com/125958. When restoring a pinned selected tab in
1032 // a setting where there are existing tabs, the selected index computation was
1033 // wrong, leading to the wrong tab getting selected, DCHECKs firing, and the
1034 // pinned tab not getting loaded.
1035 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, RestorePinnedSelectedTab) {
1036 // Create a pinned tab.
1037 ui_test_utils::NavigateToURL(browser(), url1_);
1038 browser()->tab_strip_model()->SetTabPinned(0, true);
1039 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
1040 // Create a nonpinned tab.
1041 ui_test_utils::NavigateToURLWithDisposition(
1042 browser(), url2_, NEW_FOREGROUND_TAB,
1043 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1044 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
1045 // Select the pinned tab.
1046 browser()->tab_strip_model()->ActivateTabAt(0, true);
1047 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
1048 Profile* profile = browser()->profile();
1050 // This will also initiate a session restore, but we're not interested in it.
1051 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
1052 ASSERT_EQ(1u, active_browser_list_->size());
1053 ASSERT_EQ(2, new_browser->tab_strip_model()->count());
1054 ASSERT_EQ(0, new_browser->tab_strip_model()->active_index());
1055 // Close the pinned tab.
1056 chrome::CloseTab(new_browser);
1057 ASSERT_EQ(1, new_browser->tab_strip_model()->count());
1058 ASSERT_EQ(0, new_browser->tab_strip_model()->active_index());
1059 // Use the existing tab to navigate away, so that we can verify it was really
1060 // clobbered.
1061 ui_test_utils::NavigateToURL(new_browser, url3_);
1063 // Restore the session again, clobbering the existing tab.
1064 SessionRestore::RestoreSession(
1065 profile, new_browser,
1066 new_browser->host_desktop_type(),
1067 SessionRestore::CLOBBER_CURRENT_TAB | SessionRestore::SYNCHRONOUS,
1068 std::vector<GURL>());
1070 // The pinned tab is the selected tab.
1071 ASSERT_EQ(2, new_browser->tab_strip_model()->count());
1072 EXPECT_EQ(0, new_browser->tab_strip_model()->active_index());
1073 EXPECT_EQ(url1_,
1074 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1075 EXPECT_EQ(url2_,
1076 new_browser->tab_strip_model()->GetWebContentsAt(1)->GetURL());
1079 // Regression test for crbug.com/240156. When restoring tabs with a navigation,
1080 // the navigation should take active tab focus.
1081 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, RestoreWithNavigateSelectedTab) {
1082 // Create 2 tabs.
1083 ui_test_utils::NavigateToURL(browser(), url1_);
1084 ui_test_utils::NavigateToURLWithDisposition(
1085 browser(), url2_, NEW_FOREGROUND_TAB,
1086 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1088 // Restore the session by calling chrome::Navigate().
1089 Browser* new_browser = QuitBrowserAndRestoreWithURL(browser(), 3, url3_);
1090 ASSERT_EQ(1u, active_browser_list_->size());
1091 ASSERT_EQ(3, new_browser->tab_strip_model()->count());
1092 // Navigated url should be the active tab.
1093 ASSERT_EQ(url3_,
1094 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1097 // Do a clobber restore from the new tab page. This test follows the code path
1098 // of a crash followed by the user clicking restore from the new tab page.
1099 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, ClobberRestoreTest) {
1100 // Create 2 tabs.
1101 ui_test_utils::NavigateToURL(browser(), url1_);
1102 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
1103 ui_test_utils::NavigateToURLWithDisposition(
1104 browser(), url2_, NEW_FOREGROUND_TAB,
1105 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1106 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
1107 Profile* profile = browser()->profile();
1109 // This will also initiate a session restore, but we're not interested in it.
1110 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
1111 ASSERT_EQ(1u, active_browser_list_->size());
1112 ASSERT_EQ(2, new_browser->tab_strip_model()->count());
1113 ASSERT_EQ(1, new_browser->tab_strip_model()->active_index());
1114 // Close the first tab.
1115 chrome::CloseTab(new_browser);
1116 ASSERT_EQ(1, new_browser->tab_strip_model()->count());
1117 ASSERT_EQ(0, new_browser->tab_strip_model()->active_index());
1118 // Use the existing tab to navigate to the NTP.
1119 ui_test_utils::NavigateToURL(new_browser, GURL(chrome::kChromeUINewTabURL));
1121 // Restore the session again, clobbering the existing tab.
1122 SessionRestore::RestoreSession(
1123 profile, new_browser,
1124 new_browser->host_desktop_type(),
1125 SessionRestore::CLOBBER_CURRENT_TAB | SessionRestore::SYNCHRONOUS,
1126 std::vector<GURL>());
1128 // 2 tabs should have been restored, with the existing tab clobbered, giving
1129 // us a total of 2 tabs.
1130 ASSERT_EQ(2, new_browser->tab_strip_model()->count());
1131 EXPECT_EQ(1, new_browser->tab_strip_model()->active_index());
1132 EXPECT_EQ(url1_,
1133 new_browser->tab_strip_model()->GetWebContentsAt(0)->GetURL());
1134 EXPECT_EQ(url2_,
1135 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1138 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, SessionStorage) {
1139 ui_test_utils::NavigateToURL(browser(), url1_);
1140 content::NavigationController* controller =
1141 &browser()->tab_strip_model()->GetActiveWebContents()->GetController();
1142 ASSERT_TRUE(controller->GetDefaultSessionStorageNamespace());
1143 std::string session_storage_persistent_id =
1144 controller->GetDefaultSessionStorageNamespace()->persistent_id();
1145 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
1146 ASSERT_EQ(1u, active_browser_list_->size());
1147 ASSERT_EQ(url1_,
1148 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1149 content::NavigationController* new_controller =
1150 &new_browser->tab_strip_model()->GetActiveWebContents()->GetController();
1151 ASSERT_TRUE(new_controller->GetDefaultSessionStorageNamespace());
1152 std::string restored_session_storage_persistent_id =
1153 new_controller->GetDefaultSessionStorageNamespace()->persistent_id();
1154 EXPECT_EQ(session_storage_persistent_id,
1155 restored_session_storage_persistent_id);
1158 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, SessionStorageAfterTabReplace) {
1159 // Simulate what prerendering does: create a new WebContents with the same
1160 // SessionStorageNamespace as an existing tab, then replace the tab with it.
1162 content::NavigationController* controller =
1163 &browser()->tab_strip_model()->GetActiveWebContents()->GetController();
1164 ASSERT_TRUE(controller->GetDefaultSessionStorageNamespace());
1166 content::SessionStorageNamespaceMap session_storage_namespace_map;
1167 session_storage_namespace_map[std::string()] =
1168 controller->GetDefaultSessionStorageNamespace();
1169 scoped_ptr<content::WebContents> web_contents(
1170 content::WebContents::CreateWithSessionStorage(
1171 content::WebContents::CreateParams(browser()->profile()),
1172 session_storage_namespace_map));
1174 TabStripModel* tab_strip_model = browser()->tab_strip_model();
1175 scoped_ptr<content::WebContents> old_web_contents(
1176 tab_strip_model->ReplaceWebContentsAt(
1177 tab_strip_model->active_index(), web_contents.release()));
1178 // Navigate with the new tab.
1179 ui_test_utils::NavigateToURL(browser(), url2_);
1180 // old_web_contents goes out of scope.
1183 // Check that the sessionStorage data is going to be persisted.
1184 content::NavigationController* controller =
1185 &browser()->tab_strip_model()->GetActiveWebContents()->GetController();
1186 EXPECT_TRUE(
1187 controller->GetDefaultSessionStorageNamespace()->should_persist());
1189 // Quit and restore. Check that no extra tabs were created.
1190 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
1191 ASSERT_EQ(1u, active_browser_list_->size());
1192 EXPECT_EQ(1, new_browser->tab_strip_model()->count());