Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / sessions / session_restore_browsertest.cc
blob5d39701b02871866082924e515f05db3cd8282d2
1 // Copyright 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/memory/memory_pressure_listener.h"
10 #include "base/process/launch.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/defaults.h"
16 #include "chrome/browser/first_run/first_run.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/sessions/session_restore.h"
20 #include "chrome/browser/sessions/session_restore_test_helper.h"
21 #include "chrome/browser/sessions/session_service.h"
22 #include "chrome/browser/sessions/session_service_factory.h"
23 #include "chrome/browser/sessions/session_service_test_helper.h"
24 #include "chrome/browser/sessions/tab_restore_service.h"
25 #include "chrome/browser/sessions/tab_restore_service_factory.h"
26 #include "chrome/browser/ui/browser.h"
27 #include "chrome/browser/ui/browser_commands.h"
28 #include "chrome/browser/ui/browser_list.h"
29 #include "chrome/browser/ui/browser_tabstrip.h"
30 #include "chrome/browser/ui/browser_window.h"
31 #include "chrome/browser/ui/host_desktop.h"
32 #include "chrome/browser/ui/tabs/tab_strip_model.h"
33 #include "chrome/common/chrome_switches.h"
34 #include "chrome/common/url_constants.h"
35 #include "chrome/test/base/in_process_browser_test.h"
36 #include "chrome/test/base/test_switches.h"
37 #include "chrome/test/base/ui_test_utils.h"
38 #include "components/sessions/serialized_navigation_entry_test_helper.h"
39 #include "components/sessions/session_types.h"
40 #include "content/public/browser/navigation_controller.h"
41 #include "content/public/browser/navigation_entry.h"
42 #include "content/public/browser/notification_service.h"
43 #include "content/public/browser/notification_types.h"
44 #include "content/public/browser/render_process_host.h"
45 #include "content/public/browser/render_view_host.h"
46 #include "content/public/browser/web_contents.h"
47 #include "content/public/common/bindings_policy.h"
48 #include "content/public/test/browser_test_utils.h"
49 #include "content/public/test/test_navigation_observer.h"
50 #include "sync/protocol/session_specifics.pb.h"
51 #include "ui/base/page_transition_types.h"
53 using sessions::SerializedNavigationEntry;
54 using sessions::SerializedNavigationEntryTestHelper;
56 #if defined(OS_MACOSX)
57 #include "base/mac/scoped_nsautorelease_pool.h"
58 #endif
60 #if defined(USE_AURA)
61 #include "ui/aura/window.h"
62 #endif
64 class SessionRestoreTest : public InProcessBrowserTest {
65 public:
66 SessionRestoreTest() : active_browser_list_(NULL) {}
68 protected:
69 #if defined(OS_CHROMEOS)
70 void SetUpCommandLine(base::CommandLine* command_line) override {
71 // TODO(nkostylev): Investigate if we can remove this switch.
72 command_line->AppendSwitch(switches::kCreateBrowserOnStartupForTests);
73 InProcessBrowserTest::SetUpCommandLine(command_line);
75 #endif
77 void SetUpOnMainThread() override {
78 active_browser_list_ = BrowserList::GetInstance(chrome::GetActiveDesktop());
80 SessionStartupPref pref(SessionStartupPref::LAST);
81 SessionStartupPref::SetStartupPref(browser()->profile(), pref);
82 #if defined(OS_CHROMEOS)
83 const testing::TestInfo* const test_info =
84 testing::UnitTest::GetInstance()->current_test_info();
85 if (strcmp(test_info->name(), "NoSessionRestoreNewWindowChromeOS") != 0) {
86 // Undo the effect of kBrowserAliveWithNoWindows in defaults.cc so that we
87 // can get these test to work without quitting.
88 SessionServiceTestHelper helper(
89 SessionServiceFactory::GetForProfile(browser()->profile()));
90 helper.SetForceBrowserNotAliveWithNoWindows(true);
91 helper.ReleaseService();
93 #endif
95 InProcessBrowserTest::SetUpOnMainThread();
98 bool SetUpUserDataDirectory() override {
99 url1_ = ui_test_utils::GetTestUrl(
100 base::FilePath().AppendASCII("session_history"),
101 base::FilePath().AppendASCII("bot1.html"));
102 url2_ = ui_test_utils::GetTestUrl(
103 base::FilePath().AppendASCII("session_history"),
104 base::FilePath().AppendASCII("bot2.html"));
105 url3_ = ui_test_utils::GetTestUrl(
106 base::FilePath().AppendASCII("session_history"),
107 base::FilePath().AppendASCII("bot3.html"));
109 return InProcessBrowserTest::SetUpUserDataDirectory();
112 void CloseBrowserSynchronously(Browser* browser) {
113 content::WindowedNotificationObserver observer(
114 chrome::NOTIFICATION_BROWSER_CLOSED,
115 content::NotificationService::AllSources());
116 browser->window()->Close();
117 #if defined(OS_MACOSX)
118 // BrowserWindowController depends on the auto release pool being recycled
119 // in the message loop to delete itself, which frees the Browser object
120 // which fires this event.
121 AutoreleasePool()->Recycle();
122 #endif
123 observer.Wait();
126 Browser* QuitBrowserAndRestore(Browser* browser, int expected_tab_count) {
127 return QuitBrowserAndRestoreWithURL(
128 browser, expected_tab_count, GURL(), true);
131 Browser* QuitBrowserAndRestoreWithURL(Browser* browser,
132 int expected_tab_count,
133 const GURL& url,
134 bool no_memory_pressure) {
135 Profile* profile = browser->profile();
137 // Close the browser.
138 g_browser_process->AddRefModule();
139 CloseBrowserSynchronously(browser);
141 // Create a new window, which should trigger session restore.
142 ui_test_utils::BrowserAddedObserver window_observer;
143 SessionRestoreTestHelper restore_observer;
144 if (url.is_empty()) {
145 chrome::NewEmptyWindow(profile, chrome::HOST_DESKTOP_TYPE_NATIVE);
146 } else {
147 chrome::NavigateParams params(profile,
148 url,
149 ui::PAGE_TRANSITION_LINK);
150 chrome::Navigate(&params);
152 Browser* new_browser = window_observer.WaitForSingleNewBrowser();
153 // Stop loading anything more if we are running out of space.
154 if (!no_memory_pressure) {
155 base::MemoryPressureListener::NotifyMemoryPressure(
156 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL);
158 restore_observer.Wait();
160 if (no_memory_pressure)
161 WaitForTabsToLoad(new_browser);
163 g_browser_process->ReleaseModule();
165 return new_browser;
168 void GoBack(Browser* browser) {
169 content::TestNavigationObserver observer(
170 browser->tab_strip_model()->GetActiveWebContents());
171 chrome::GoBack(browser, CURRENT_TAB);
172 observer.Wait();
175 void GoForward(Browser* browser) {
176 content::TestNavigationObserver observer(
177 browser->tab_strip_model()->GetActiveWebContents());
178 chrome::GoForward(browser, CURRENT_TAB);
179 observer.Wait();
182 void AssertOneWindowWithOneTab(Browser* browser) {
183 ASSERT_EQ(1u, active_browser_list_->size());
184 ASSERT_EQ(1, browser->tab_strip_model()->count());
187 int RenderProcessHostCount() {
188 content::RenderProcessHost::iterator hosts =
189 content::RenderProcessHost::AllHostsIterator();
190 int count = 0;
191 while (!hosts.IsAtEnd()) {
192 if (hosts.GetCurrentValue()->HasConnection())
193 count++;
194 hosts.Advance();
196 return count;
199 void WaitForTabsToLoad(Browser* browser) {
200 for (int i = 0; i < browser->tab_strip_model()->count(); ++i) {
201 content::WebContents* contents =
202 browser->tab_strip_model()->GetWebContentsAt(i);
203 contents->GetController().LoadIfNecessary();
204 content::WaitForLoadStop(contents);
208 GURL url1_;
209 GURL url2_;
210 GURL url3_;
212 const BrowserList* active_browser_list_;
215 // Verifies that restored tabs have a root window. This is important
216 // otherwise the wrong information is communicated to the renderer.
217 // (http://crbug.com/342672).
218 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, RestoredTabsShouldHaveWindow) {
219 // Create tabs.
220 ui_test_utils::NavigateToURLWithDisposition(
221 browser(),
222 GURL(url::kAboutBlankURL),
223 NEW_FOREGROUND_TAB,
224 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
225 ui_test_utils::NavigateToURLWithDisposition(
226 browser(),
227 GURL(url::kAboutBlankURL),
228 NEW_BACKGROUND_TAB,
229 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
231 // Restart and session restore the tabs.
232 Browser* restored = QuitBrowserAndRestore(browser(), 3);
233 TabStripModel* tab_strip_model = restored->tab_strip_model();
234 const int tabs = tab_strip_model->count();
235 ASSERT_EQ(3, tabs);
237 // Check the restored tabs have a window to get screen info from.
238 // On Aura it should also have a root window.
239 for (int i = 0; i < tabs; ++i) {
240 content::WebContents* contents = tab_strip_model->GetWebContentsAt(i);
241 EXPECT_TRUE(contents->GetTopLevelNativeWindow());
242 #if defined(USE_AURA)
243 EXPECT_TRUE(contents->GetNativeView()->GetRootWindow());
244 #endif
248 // Verify that restored tabs have correct disposition. Only one tab should
249 // have "visible" visibility state, the rest should not.
250 // (http://crbug.com/155365 http://crbug.com/118269)
251 IN_PROC_BROWSER_TEST_F(SessionRestoreTest,
252 RestoredTabsHaveCorrectVisibilityState) {
253 // Create tabs.
254 GURL test_page(ui_test_utils::GetTestUrl(base::FilePath(),
255 base::FilePath(FILE_PATH_LITERAL("tab-restore-visibilty.html"))));
256 ui_test_utils::NavigateToURLWithDisposition(
257 browser(), test_page, NEW_FOREGROUND_TAB,
258 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
259 ui_test_utils::NavigateToURLWithDisposition(
260 browser(), test_page, NEW_BACKGROUND_TAB,
261 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
263 // Restart and session restore the tabs.
264 content::DOMMessageQueue message_queue;
265 Browser* restored = QuitBrowserAndRestore(browser(), 3);
266 for (int i = 0; i < 2; ++i) {
267 std::string message;
268 EXPECT_TRUE(message_queue.WaitForMessage(&message));
269 EXPECT_EQ("\"READY\"", message);
272 // There should be 3 restored tabs in the new browser.
273 TabStripModel* tab_strip_model = restored->tab_strip_model();
274 const int tabs = tab_strip_model->count();
275 ASSERT_EQ(3, tabs);
277 // The middle tab only should have visible disposition.
278 for (int i = 0; i < tabs; ++i) {
279 content::WebContents* contents = tab_strip_model->GetWebContentsAt(i);
280 std::string document_visibility_state;
281 const char kGetStateJS[] = "window.domAutomationController.send("
282 "window.document.visibilityState);";
283 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
284 contents, kGetStateJS, &document_visibility_state));
285 if (i == 1) {
286 EXPECT_EQ("visible", document_visibility_state);
287 } else {
288 EXPECT_EQ("hidden", document_visibility_state);
293 #if defined(OS_CHROMEOS)
294 // Verify that session restore does not occur when a user opens a browser window
295 // when no other browser windows are open on ChromeOS.
296 // TODO(pkotwicz): Add test which doesn't open incognito browser once
297 // disable-zero-browsers-open-for-tests is removed.
298 // (http://crbug.com/119175)
299 // TODO(pkotwicz): Mac should have the behavior outlined by this test. It should
300 // not do session restore if an incognito window is already open.
301 // (http://crbug.com/120927)
302 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, NoSessionRestoreNewWindowChromeOS) {
303 GURL url(ui_test_utils::GetTestUrl(
304 base::FilePath(base::FilePath::kCurrentDirectory),
305 base::FilePath(FILE_PATH_LITERAL("title1.html"))));
307 // Add a single tab.
308 ui_test_utils::NavigateToURL(browser(), url);
310 Browser* incognito_browser = CreateIncognitoBrowser();
311 chrome::AddTabAt(incognito_browser, GURL(), -1, true);
312 incognito_browser->window()->Show();
314 // Close the normal browser. After this we only have the incognito window
315 // open.
316 CloseBrowserSynchronously(browser());
318 // Create a new window, which should open NTP.
319 ui_test_utils::BrowserAddedObserver browser_added_observer;
320 chrome::NewWindow(incognito_browser);
321 Browser* new_browser = browser_added_observer.WaitForSingleNewBrowser();
323 ASSERT_TRUE(new_browser);
324 EXPECT_EQ(1, new_browser->tab_strip_model()->count());
325 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
326 new_browser->tab_strip_model()->GetWebContentsAt(0)->GetURL());
329 // Test that maximized applications get restored maximized.
330 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, MaximizedApps) {
331 const char* app_name = "TestApp";
332 Browser* app_browser = CreateBrowserForApp(app_name, browser()->profile());
333 app_browser->window()->Maximize();
334 app_browser->window()->Show();
335 EXPECT_TRUE(app_browser->window()->IsMaximized());
336 EXPECT_TRUE(app_browser->is_app());
337 EXPECT_TRUE(app_browser->is_type_popup());
339 // Close the normal browser. After this we only have the app_browser window.
340 CloseBrowserSynchronously(browser());
342 // Create a new window, which should open NTP.
343 ui_test_utils::BrowserAddedObserver browser_added_observer;
344 chrome::NewWindow(app_browser);
345 Browser* new_browser = browser_added_observer.WaitForSingleNewBrowser();
347 ASSERT_TRUE(new_browser);
348 EXPECT_TRUE(app_browser->window()->IsMaximized());
349 EXPECT_TRUE(app_browser->is_app());
350 EXPECT_TRUE(app_browser->is_type_popup());
352 #endif // OS_CHROMEOS
354 #if !defined(OS_CHROMEOS)
355 // This test does not apply to ChromeOS as it does not do session restore when
356 // a new window is opened.
358 #if defined(OS_LINUX) && defined(TOOLKIT_VIEWS)
359 // Crashes on Linux Views: http://crbug.com/39476
360 #define MAYBE_RestoreOnNewWindowWithNoTabbedBrowsers \
361 DISABLED_RestoreOnNewWindowWithNoTabbedBrowsers
362 #else
363 #define MAYBE_RestoreOnNewWindowWithNoTabbedBrowsers \
364 RestoreOnNewWindowWithNoTabbedBrowsers
365 #endif
367 // Makes sure when session restore is triggered in the same process we don't end
368 // up with an extra tab.
369 IN_PROC_BROWSER_TEST_F(SessionRestoreTest,
370 MAYBE_RestoreOnNewWindowWithNoTabbedBrowsers) {
371 const base::FilePath::CharType* kTitle1File =
372 FILE_PATH_LITERAL("title1.html");
373 GURL url(ui_test_utils::GetTestUrl(base::FilePath(
374 base::FilePath::kCurrentDirectory), base::FilePath(kTitle1File)));
375 ui_test_utils::NavigateToURL(browser(), url);
377 // Turn on session restore.
378 SessionStartupPref::SetStartupPref(
379 browser()->profile(),
380 SessionStartupPref(SessionStartupPref::LAST));
382 // Create a new popup.
383 Profile* profile = browser()->profile();
384 Browser* popup =
385 new Browser(Browser::CreateParams(Browser::TYPE_POPUP, profile,
386 browser()->host_desktop_type()));
387 popup->window()->Show();
389 // Close the browser.
390 CloseBrowserSynchronously(browser());
392 // Create a new window, which should trigger session restore.
393 ui_test_utils::BrowserAddedObserver observer;
394 chrome::NewWindow(popup);
395 Browser* new_browser = observer.WaitForSingleNewBrowser();
397 ASSERT_TRUE(new_browser != NULL);
399 // The browser should only have one tab.
400 ASSERT_EQ(1, new_browser->tab_strip_model()->count());
402 // And the first url should be url.
403 EXPECT_EQ(url, new_browser->tab_strip_model()->GetWebContentsAt(0)->GetURL());
405 #endif // !OS_CHROMEOS
407 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, RestoreIndividualTabFromWindow) {
408 GURL url1(ui_test_utils::GetTestUrl(
409 base::FilePath(base::FilePath::kCurrentDirectory),
410 base::FilePath(FILE_PATH_LITERAL("title1.html"))));
411 // Any page that will yield a 200 status code will work here.
412 GURL url2("chrome://version");
413 GURL url3(ui_test_utils::GetTestUrl(
414 base::FilePath(base::FilePath::kCurrentDirectory),
415 base::FilePath(FILE_PATH_LITERAL("title3.html"))));
417 // Add and navigate three tabs.
418 ui_test_utils::NavigateToURL(browser(), url1);
420 content::WindowedNotificationObserver observer(
421 content::NOTIFICATION_LOAD_STOP,
422 content::NotificationService::AllSources());
423 chrome::AddSelectedTabWithURL(browser(), url2,
424 ui::PAGE_TRANSITION_LINK);
425 observer.Wait();
428 content::WindowedNotificationObserver observer(
429 content::NOTIFICATION_LOAD_STOP,
430 content::NotificationService::AllSources());
431 chrome::AddSelectedTabWithURL(browser(), url3,
432 ui::PAGE_TRANSITION_LINK);
433 observer.Wait();
436 TabRestoreService* service =
437 TabRestoreServiceFactory::GetForProfile(browser()->profile());
438 service->ClearEntries();
440 chrome::HostDesktopType host_desktop_type = browser()->host_desktop_type();
442 browser()->window()->Close();
444 // Expect a window with three tabs.
445 ASSERT_EQ(1U, service->entries().size());
446 ASSERT_EQ(TabRestoreService::WINDOW, service->entries().front()->type);
447 const TabRestoreService::Window* window =
448 static_cast<TabRestoreService::Window*>(service->entries().front());
449 EXPECT_EQ(3U, window->tabs.size());
451 // Find the SessionID for entry2. Since the session service was destroyed,
452 // there is no guarantee that the SessionID for the tab has remained the same.
453 base::Time timestamp;
454 int http_status_code = 0;
455 for (std::vector<TabRestoreService::Tab>::const_iterator it =
456 window->tabs.begin(); it != window->tabs.end(); ++it) {
457 const TabRestoreService::Tab& tab = *it;
458 // If this tab held url2, then restore this single tab.
459 if (tab.navigations[0].virtual_url() == url2) {
460 timestamp = tab.navigations[0].timestamp();
461 http_status_code = tab.navigations[0].http_status_code();
462 std::vector<content::WebContents*> content =
463 service->RestoreEntryById(NULL, tab.id, host_desktop_type, UNKNOWN);
464 ASSERT_EQ(1U, content.size());
465 ASSERT_TRUE(content[0]);
466 EXPECT_EQ(url2, content[0]->GetURL());
467 break;
470 EXPECT_FALSE(timestamp.is_null());
471 EXPECT_EQ(200, http_status_code);
473 // Make sure that the restored tab is removed from the service.
474 ASSERT_EQ(1U, service->entries().size());
475 ASSERT_EQ(TabRestoreService::WINDOW, service->entries().front()->type);
476 window = static_cast<TabRestoreService::Window*>(service->entries().front());
477 EXPECT_EQ(2U, window->tabs.size());
479 // Make sure that the restored tab was restored with the correct
480 // timestamp and status code.
481 const content::WebContents* contents =
482 browser()->tab_strip_model()->GetActiveWebContents();
483 ASSERT_TRUE(contents);
484 const content::NavigationEntry* entry =
485 contents->GetController().GetActiveEntry();
486 ASSERT_TRUE(entry);
487 EXPECT_EQ(timestamp, entry->GetTimestamp());
488 EXPECT_EQ(http_status_code, entry->GetHttpStatusCode());
491 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, WindowWithOneTab) {
492 GURL url(ui_test_utils::GetTestUrl(
493 base::FilePath(base::FilePath::kCurrentDirectory),
494 base::FilePath(FILE_PATH_LITERAL("title1.html"))));
496 // Add a single tab.
497 ui_test_utils::NavigateToURL(browser(), url);
499 TabRestoreService* service =
500 TabRestoreServiceFactory::GetForProfile(browser()->profile());
501 service->ClearEntries();
502 EXPECT_EQ(0U, service->entries().size());
504 chrome::HostDesktopType host_desktop_type = browser()->host_desktop_type();
506 // Close the window.
507 browser()->window()->Close();
509 // Expect the window to be converted to a tab by the TRS.
510 EXPECT_EQ(1U, service->entries().size());
511 ASSERT_EQ(TabRestoreService::TAB, service->entries().front()->type);
512 const TabRestoreService::Tab* tab =
513 static_cast<TabRestoreService::Tab*>(service->entries().front());
515 // Restore the tab.
516 std::vector<content::WebContents*> content =
517 service->RestoreEntryById(NULL, tab->id, host_desktop_type, UNKNOWN);
518 ASSERT_EQ(1U, content.size());
519 ASSERT_TRUE(content[0]);
520 EXPECT_EQ(url, content[0]->GetURL());
522 // Make sure the restore was successful.
523 EXPECT_EQ(0U, service->entries().size());
526 #if !defined(OS_CHROMEOS)
527 // This test does not apply to ChromeOS as ChromeOS does not do session
528 // restore when a new window is open.
530 // Verifies we remember the last browser window when closing the last
531 // non-incognito window while an incognito window is open.
532 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, IncognitotoNonIncognito) {
533 GURL url(ui_test_utils::GetTestUrl(
534 base::FilePath(base::FilePath::kCurrentDirectory),
535 base::FilePath(FILE_PATH_LITERAL("title1.html"))));
537 // Add a single tab.
538 ui_test_utils::NavigateToURL(browser(), url);
540 // Create a new incognito window.
541 Browser* incognito_browser = CreateIncognitoBrowser();
542 chrome::AddTabAt(incognito_browser, GURL(), -1, true);
543 incognito_browser->window()->Show();
545 // Close the normal browser. After this we only have the incognito window
546 // open.
547 CloseBrowserSynchronously(browser());
549 // Create a new window, which should trigger session restore.
550 ui_test_utils::BrowserAddedObserver browser_added_observer;
551 chrome::NewWindow(incognito_browser);
552 Browser* new_browser = browser_added_observer.WaitForSingleNewBrowser();
554 // The first tab should have 'url' as its url.
555 ASSERT_TRUE(new_browser);
556 EXPECT_EQ(url, new_browser->tab_strip_model()->GetWebContentsAt(0)->GetURL());
558 #endif // !OS_CHROMEOS
560 namespace {
562 // Verifies that the given NavigationController has exactly two
563 // entries that correspond to the given URLs and that all but the last
564 // entry have null timestamps.
565 void VerifyNavigationEntries(
566 const content::NavigationController& controller,
567 GURL url1, GURL url2) {
568 ASSERT_EQ(2, controller.GetEntryCount());
569 EXPECT_EQ(1, controller.GetCurrentEntryIndex());
570 EXPECT_EQ(url1, controller.GetEntryAtIndex(0)->GetURL());
571 EXPECT_EQ(url2, controller.GetEntryAtIndex(1)->GetURL());
572 EXPECT_TRUE(controller.GetEntryAtIndex(0)->GetTimestamp().is_null());
573 EXPECT_FALSE(controller.GetEntryAtIndex(1)->GetTimestamp().is_null());
576 } // namespace
578 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, RestoreForeignTab) {
579 GURL url1("http://google.com");
580 GURL url2("http://google2.com");
581 SerializedNavigationEntry nav1 =
582 SerializedNavigationEntryTestHelper::CreateNavigation(url1.spec(), "one");
583 SerializedNavigationEntry nav2 =
584 SerializedNavigationEntryTestHelper::CreateNavigation(url2.spec(), "two");
586 // Set up the restore data.
587 sync_pb::SessionTab sync_data;
588 sync_data.set_tab_visual_index(0);
589 sync_data.set_current_navigation_index(1);
590 sync_data.set_pinned(false);
591 sync_data.add_navigation()->CopyFrom(nav1.ToSyncData());
592 sync_data.add_navigation()->CopyFrom(nav2.ToSyncData());
594 sessions::SessionTab tab;
595 tab.SetFromSyncData(sync_data, base::Time::Now());
596 EXPECT_EQ(2U, tab.navigations.size());
597 for (size_t i = 0; i < tab.navigations.size(); ++i)
598 EXPECT_TRUE(tab.navigations[i].timestamp().is_null());
600 ASSERT_EQ(1, browser()->tab_strip_model()->count());
602 // Restore in the current tab.
603 content::WebContents* tab_content = NULL;
605 content::WindowedNotificationObserver observer(
606 content::NOTIFICATION_LOAD_STOP,
607 content::NotificationService::AllSources());
608 tab_content = SessionRestore::RestoreForeignSessionTab(
609 browser()->tab_strip_model()->GetActiveWebContents(), tab, CURRENT_TAB);
610 observer.Wait();
612 ASSERT_EQ(1, browser()->tab_strip_model()->count());
613 content::WebContents* web_contents =
614 browser()->tab_strip_model()->GetWebContentsAt(0);
615 VerifyNavigationEntries(web_contents->GetController(), url1, url2);
616 ASSERT_TRUE(web_contents->GetUserAgentOverride().empty());
617 ASSERT_TRUE(tab_content);
618 ASSERT_EQ(url2, tab_content->GetURL());
620 // Restore in a new tab.
621 tab_content = NULL;
623 content::WindowedNotificationObserver observer(
624 content::NOTIFICATION_LOAD_STOP,
625 content::NotificationService::AllSources());
626 tab_content = SessionRestore::RestoreForeignSessionTab(
627 browser()->tab_strip_model()->GetActiveWebContents(),
628 tab, NEW_BACKGROUND_TAB);
629 observer.Wait();
631 ASSERT_EQ(2, browser()->tab_strip_model()->count());
632 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
633 web_contents = browser()->tab_strip_model()->GetWebContentsAt(1);
634 VerifyNavigationEntries(web_contents->GetController(), url1, url2);
635 ASSERT_TRUE(web_contents->GetUserAgentOverride().empty());
636 ASSERT_TRUE(tab_content);
637 ASSERT_EQ(url2, tab_content->GetURL());
639 // Restore in a new window.
640 Browser* new_browser = NULL;
641 tab_content = NULL;
643 ui_test_utils::BrowserAddedObserver browser_observer;
644 content::WindowedNotificationObserver observer(
645 content::NOTIFICATION_LOAD_STOP,
646 content::NotificationService::AllSources());
647 tab_content = SessionRestore::RestoreForeignSessionTab(
648 browser()->tab_strip_model()->GetActiveWebContents(), tab, NEW_WINDOW);
649 new_browser = browser_observer.WaitForSingleNewBrowser();
650 observer.Wait();
653 ASSERT_EQ(1, new_browser->tab_strip_model()->count());
654 web_contents = new_browser->tab_strip_model()->GetWebContentsAt(0);
655 VerifyNavigationEntries(web_contents->GetController(), url1, url2);
656 ASSERT_TRUE(web_contents->GetUserAgentOverride().empty());
657 ASSERT_TRUE(tab_content);
658 ASSERT_EQ(url2, tab_content->GetURL());
661 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, RestoreForeignSession) {
662 Profile* profile = browser()->profile();
664 GURL url1("http://google.com");
665 GURL url2("http://google2.com");
666 SerializedNavigationEntry nav1 =
667 SerializedNavigationEntryTestHelper::CreateNavigation(url1.spec(), "one");
668 SerializedNavigationEntry nav2 =
669 SerializedNavigationEntryTestHelper::CreateNavigation(url2.spec(), "two");
670 SerializedNavigationEntryTestHelper::SetIsOverridingUserAgent(true, &nav2);
672 // Set up the restore data -- one window with two tabs.
673 std::vector<const sessions::SessionWindow*> session;
674 sessions::SessionWindow window;
675 sessions::SessionTab tab1;
677 sync_pb::SessionTab sync_data;
678 sync_data.set_tab_visual_index(0);
679 sync_data.set_current_navigation_index(0);
680 sync_data.set_pinned(true);
681 sync_data.add_navigation()->CopyFrom(nav1.ToSyncData());
682 tab1.SetFromSyncData(sync_data, base::Time::Now());
684 window.tabs.push_back(&tab1);
686 sessions::SessionTab tab2;
688 sync_pb::SessionTab sync_data;
689 sync_data.set_tab_visual_index(1);
690 sync_data.set_current_navigation_index(0);
691 sync_data.set_pinned(false);
692 sync_data.add_navigation()->CopyFrom(nav2.ToSyncData());
693 tab2.SetFromSyncData(sync_data, base::Time::Now());
695 window.tabs.push_back(&tab2);
697 // Leave tab3 empty. Should have no effect on restored session, but simulates
698 // partially complete foreign session data.
699 sessions::SessionTab tab3;
700 window.tabs.push_back(&tab3);
702 session.push_back(static_cast<const sessions::SessionWindow*>(&window));
703 ui_test_utils::BrowserAddedObserver window_observer;
704 std::vector<Browser*> browsers =
705 SessionRestore::RestoreForeignSessionWindows(
706 profile, browser()->host_desktop_type(), session.begin(),
707 session.end());
708 Browser* new_browser = window_observer.WaitForSingleNewBrowser();
709 ASSERT_TRUE(new_browser);
710 ASSERT_EQ(2u, active_browser_list_->size());
711 ASSERT_EQ(2, new_browser->tab_strip_model()->count());
713 ASSERT_EQ(1u, browsers.size());
714 ASSERT_TRUE(browsers[0]);
715 ASSERT_EQ(2, browsers[0]->tab_strip_model()->count());
717 content::WebContents* web_contents_1 =
718 new_browser->tab_strip_model()->GetWebContentsAt(0);
719 content::WebContents* web_contents_2 =
720 new_browser->tab_strip_model()->GetWebContentsAt(1);
721 ASSERT_EQ(url1, web_contents_1->GetURL());
722 ASSERT_EQ(url2, web_contents_2->GetURL());
724 // Check user agent override state.
725 ASSERT_TRUE(web_contents_1->GetUserAgentOverride().empty());
726 ASSERT_TRUE(web_contents_2->GetUserAgentOverride().empty());
728 content::NavigationEntry* entry =
729 web_contents_1->GetController().GetActiveEntry();
730 ASSERT_TRUE(entry);
731 ASSERT_FALSE(entry->GetIsOverridingUserAgent());
733 entry = web_contents_2->GetController().GetActiveEntry();
734 ASSERT_TRUE(entry);
735 ASSERT_FALSE(entry->GetIsOverridingUserAgent());
737 // The SessionWindow destructor deletes the tabs, so we have to clear them
738 // here to avoid a crash.
739 window.tabs.clear();
742 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, Basic) {
743 ui_test_utils::NavigateToURL(browser(), url1_);
744 ui_test_utils::NavigateToURL(browser(), url2_);
746 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
747 ASSERT_EQ(1u, active_browser_list_->size());
748 ASSERT_EQ(url2_,
749 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
750 GoBack(new_browser);
751 ASSERT_EQ(url1_,
752 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
755 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, NoMemoryPressureLoadsAllTabs) {
756 // Add several tabs to the browser. Restart the browser and check that all
757 // tabs got loaded properly.
758 ui_test_utils::NavigateToURLWithDisposition(
759 browser(),
760 GURL(url::kAboutBlankURL),
761 NEW_FOREGROUND_TAB,
762 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
763 ui_test_utils::NavigateToURLWithDisposition(
764 browser(),
765 GURL(url::kAboutBlankURL),
766 NEW_FOREGROUND_TAB,
767 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
768 Browser* restored =
769 QuitBrowserAndRestoreWithURL(browser(), 1, GURL(), true);
770 TabStripModel* tab_strip_model = restored->tab_strip_model();
772 ASSERT_EQ(1u, active_browser_list_->size());
774 ASSERT_EQ(3, tab_strip_model->count());
775 // All render widgets should be initialized by now.
776 ASSERT_TRUE(
777 tab_strip_model->GetWebContentsAt(0)->GetRenderWidgetHostView() &&
778 tab_strip_model->GetWebContentsAt(1)->GetRenderWidgetHostView() &&
779 tab_strip_model->GetWebContentsAt(2)->GetRenderWidgetHostView());
782 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, MemoryPressureLoadsNotAllTabs) {
783 // Add several tabs to the browser. Restart the browser and check that all
784 // tabs got loaded properly.
785 ui_test_utils::NavigateToURLWithDisposition(
786 browser(),
787 GURL(url::kAboutBlankURL),
788 NEW_FOREGROUND_TAB,
789 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
790 ui_test_utils::NavigateToURLWithDisposition(
791 browser(),
792 GURL(url::kAboutBlankURL),
793 NEW_FOREGROUND_TAB,
794 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
795 // Restore the brwoser, but instead of directly waiting, we issue a critical
796 // memory pressure event and finish then the loading.
797 Browser* restored =
798 QuitBrowserAndRestoreWithURL(browser(), 1, GURL(), false);
800 TabStripModel* tab_strip_model = restored->tab_strip_model();
802 ASSERT_EQ(1u, active_browser_list_->size());
804 ASSERT_EQ(3, tab_strip_model->count());
805 // At least one of the render widgets should not be initialized yet.
806 ASSERT_FALSE(
807 tab_strip_model->GetWebContentsAt(0)->GetRenderWidgetHostView() &&
808 tab_strip_model->GetWebContentsAt(1)->GetRenderWidgetHostView() &&
809 tab_strip_model->GetWebContentsAt(2)->GetRenderWidgetHostView());
812 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, RestoreWebUI) {
813 const GURL webui_url("chrome://omnibox");
814 ui_test_utils::NavigateToURL(browser(), webui_url);
815 const content::WebContents* old_tab =
816 browser()->tab_strip_model()->GetActiveWebContents();
817 EXPECT_EQ(content::BINDINGS_POLICY_WEB_UI,
818 old_tab->GetRenderViewHost()->GetEnabledBindings());
820 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
821 ASSERT_EQ(1u, active_browser_list_->size());
822 const content::WebContents* new_tab =
823 new_browser->tab_strip_model()->GetActiveWebContents();
824 EXPECT_EQ(webui_url, new_tab->GetURL());
825 EXPECT_EQ(content::BINDINGS_POLICY_WEB_UI,
826 new_tab->GetRenderViewHost()->GetEnabledBindings());
829 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, RestoreWebUISettings) {
830 const GURL webui_url("chrome://settings");
831 ui_test_utils::NavigateToURL(browser(), webui_url);
832 const content::WebContents* old_tab =
833 browser()->tab_strip_model()->GetActiveWebContents();
834 EXPECT_EQ(content::BINDINGS_POLICY_WEB_UI,
835 old_tab->GetRenderViewHost()->GetEnabledBindings());
837 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
838 ASSERT_EQ(1u, active_browser_list_->size());
839 const content::WebContents* new_tab =
840 new_browser->tab_strip_model()->GetActiveWebContents();
841 EXPECT_EQ(webui_url, new_tab->GetURL());
842 EXPECT_EQ(content::BINDINGS_POLICY_WEB_UI,
843 new_tab->GetRenderViewHost()->GetEnabledBindings());
846 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, RestoresForwardAndBackwardNavs) {
847 ui_test_utils::NavigateToURL(browser(), url1_);
848 ui_test_utils::NavigateToURL(browser(), url2_);
849 ui_test_utils::NavigateToURL(browser(), url3_);
851 GoBack(browser());
852 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
853 ASSERT_EQ(1u, active_browser_list_->size());
854 ASSERT_EQ(url2_,
855 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
856 GoForward(new_browser);
857 ASSERT_EQ(url3_,
858 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
859 GoBack(new_browser);
860 ASSERT_EQ(url2_,
861 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
863 // Test renderer-initiated back/forward as well.
864 GURL go_back_url("javascript:history.back();");
865 ui_test_utils::NavigateToURL(new_browser, go_back_url);
866 ASSERT_EQ(url1_,
867 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
870 // Tests that the SiteInstances used for entries in a restored tab's history
871 // are given appropriate max page IDs, so that going back to a restored
872 // cross-site page and then forward again works. (Bug 1204135)
873 // This test fails. See http://crbug.com/237497.
874 IN_PROC_BROWSER_TEST_F(SessionRestoreTest,
875 DISABLED_RestoresCrossSiteForwardAndBackwardNavs) {
876 ASSERT_TRUE(test_server()->Start());
878 GURL cross_site_url(test_server()->GetURL("files/title2.html"));
880 // Visit URLs on different sites.
881 ui_test_utils::NavigateToURL(browser(), url1_);
882 ui_test_utils::NavigateToURL(browser(), cross_site_url);
883 ui_test_utils::NavigateToURL(browser(), url2_);
885 GoBack(browser());
886 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
887 ASSERT_EQ(1u, active_browser_list_->size());
888 ASSERT_EQ(1, new_browser->tab_strip_model()->count());
890 // Check that back and forward work as expected.
891 ASSERT_EQ(cross_site_url,
892 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
894 GoBack(new_browser);
895 ASSERT_EQ(url1_,
896 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
898 GoForward(new_browser);
899 ASSERT_EQ(cross_site_url,
900 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
902 // Test renderer-initiated back/forward as well.
903 GURL go_forward_url("javascript:history.forward();");
904 ui_test_utils::NavigateToURL(new_browser, go_forward_url);
905 ASSERT_EQ(url2_,
906 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
909 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, TwoTabsSecondSelected) {
910 ui_test_utils::NavigateToURL(browser(), url1_);
912 ui_test_utils::NavigateToURLWithDisposition(
913 browser(), url2_, NEW_FOREGROUND_TAB,
914 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
916 Browser* new_browser = QuitBrowserAndRestore(browser(), 2);
918 ASSERT_EQ(1u, active_browser_list_->size());
919 ASSERT_EQ(2, new_browser->tab_strip_model()->count());
920 ASSERT_EQ(1, new_browser->tab_strip_model()->active_index());
921 ASSERT_EQ(url2_,
922 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
924 ASSERT_EQ(url1_,
925 new_browser->tab_strip_model()->GetWebContentsAt(0)->GetURL());
928 // Creates two tabs, closes one, quits and makes sure only one tab is restored.
929 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, ClosedTabStaysClosed) {
930 ui_test_utils::NavigateToURL(browser(), url1_);
932 ui_test_utils::NavigateToURLWithDisposition(
933 browser(), url2_, NEW_FOREGROUND_TAB,
934 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
935 chrome::CloseTab(browser());
937 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
939 AssertOneWindowWithOneTab(new_browser);
940 ASSERT_EQ(url1_,
941 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
944 // Ensures active tab properly restored when tabs before it closed.
945 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, ActiveIndexUpdatedAtClose) {
946 ui_test_utils::NavigateToURL(browser(), url1_);
947 ui_test_utils::NavigateToURLWithDisposition(
948 browser(), url2_, NEW_FOREGROUND_TAB,
949 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
950 ui_test_utils::NavigateToURLWithDisposition(
951 browser(), url3_, NEW_BACKGROUND_TAB,
952 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
954 browser()->tab_strip_model()->CloseWebContentsAt(
956 TabStripModel::CLOSE_CREATE_HISTORICAL_TAB);
958 Browser* new_browser = QuitBrowserAndRestore(browser(), 2);
960 ASSERT_EQ(url2_,
961 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
962 ASSERT_EQ(new_browser->tab_strip_model()->active_index(), 0);
965 // Ensures active tab properly restored when tabs are inserted before it .
966 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, ActiveIndexUpdatedAtInsert) {
967 ui_test_utils::NavigateToURL(browser(), url1_);
968 ui_test_utils::NavigateToURLWithDisposition(
969 browser(), url2_, NEW_BACKGROUND_TAB,
970 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
972 chrome::NavigateParams navigate_params(browser(), url3_,
973 ui::PAGE_TRANSITION_TYPED);
974 navigate_params.tabstrip_index = 0;
975 navigate_params.disposition = NEW_BACKGROUND_TAB;
976 ui_test_utils::NavigateToURL(&navigate_params);
978 Browser* new_browser = QuitBrowserAndRestore(browser(), 3);
980 ASSERT_EQ(url1_,
981 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
982 ASSERT_EQ(new_browser->tab_strip_model()->active_index(), 1);
985 #if !defined(OS_CHROMEOS) && !defined(OS_MACOSX)
986 // This test doesn't apply to the Mac version; see GetCommandLineForRelaunch
987 // for details. It was disabled for a long time so might never have worked on
988 // ChromeOS.
990 // Launches an app window, closes tabbed browser, launches and makes sure
991 // we restore the tabbed browser url.
992 // If this test flakes, use http://crbug.com/29110
993 IN_PROC_BROWSER_TEST_F(SessionRestoreTest,
994 RestoreAfterClosingTabbedBrowserWithAppAndLaunching) {
995 #if defined(OS_WIN) && defined(USE_ASH)
996 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
997 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
998 switches::kAshBrowserTests))
999 return;
1000 #endif
1002 ui_test_utils::NavigateToURL(browser(), url1_);
1004 // Launch an app.
1005 base::CommandLine app_launch_arguments = GetCommandLineForRelaunch();
1006 app_launch_arguments.AppendSwitchASCII(switches::kApp, url2_.spec());
1008 ui_test_utils::BrowserAddedObserver window_observer;
1010 base::LaunchProcess(app_launch_arguments, base::LaunchOptionsForTest());
1012 Browser* app_window = window_observer.WaitForSingleNewBrowser();
1013 ASSERT_EQ(2u, active_browser_list_->size());
1015 // Close the first window. The only window left is the App window.
1016 CloseBrowserSynchronously(browser());
1018 // Restore the session, which should bring back the first window with url1_.
1019 Browser* new_browser = QuitBrowserAndRestore(app_window, 1);
1021 AssertOneWindowWithOneTab(new_browser);
1023 ASSERT_EQ(url1_,
1024 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1027 #endif // !defined(OS_CHROMEOS) && !defined(OS_MACOSX)
1029 // Creates two windows, closes one, restores, make sure only one window open.
1030 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, TwoWindowsCloseOneRestoreOnlyOne) {
1031 ui_test_utils::NavigateToURL(browser(), url1_);
1033 // Open a second window.
1034 ui_test_utils::NavigateToURLWithDisposition(
1035 browser(),
1036 GURL(url::kAboutBlankURL),
1037 NEW_WINDOW,
1038 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
1040 ASSERT_EQ(2u, active_browser_list_->size());
1042 // Close it.
1043 Browser* new_window = active_browser_list_->get(1);
1044 CloseBrowserSynchronously(new_window);
1046 // Restart and make sure we have only one window with one tab and the url
1047 // is url1_.
1048 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
1050 AssertOneWindowWithOneTab(new_browser);
1052 ASSERT_EQ(url1_,
1053 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1056 // Make sure after a restore the number of processes matches that of the number
1057 // of processes running before the restore. This creates a new tab so that
1058 // we should have two new tabs running. (This test will pass in both
1059 // process-per-site and process-per-site-instance, because we treat the new tab
1060 // as a special case in process-per-site-instance so that it only ever uses one
1061 // process.)
1063 // Flaky: http://code.google.com/p/chromium/issues/detail?id=52022
1064 // Unfortunately, the fix at http://codereview.chromium.org/6546078
1065 // breaks NTP background image refreshing, so ThemeSource had to revert to
1066 // replacing the existing data source.
1067 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, ShareProcessesOnRestore) {
1068 // Create two new tabs.
1069 ui_test_utils::NavigateToURLWithDisposition(
1070 browser(),
1071 GURL(url::kAboutBlankURL),
1072 NEW_FOREGROUND_TAB,
1073 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1074 ui_test_utils::NavigateToURLWithDisposition(
1075 browser(),
1076 GURL(url::kAboutBlankURL),
1077 NEW_FOREGROUND_TAB,
1078 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1080 int expected_process_count = RenderProcessHostCount();
1082 // Restart.
1083 Browser* new_browser = QuitBrowserAndRestore(browser(), 3);
1085 ASSERT_EQ(3, new_browser->tab_strip_model()->count());
1087 ASSERT_EQ(expected_process_count, RenderProcessHostCount());
1090 // Test that changing the user agent override will persist it to disk.
1091 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, PersistAndRestoreUserAgentOverride) {
1092 // Create a tab with an overridden user agent.
1093 ui_test_utils::NavigateToURL(browser(), url1_);
1094 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
1095 browser()->tab_strip_model()->GetWebContentsAt(0)->
1096 SetUserAgentOverride("override");
1098 // Create a tab without an overridden user agent.
1099 ui_test_utils::NavigateToURLWithDisposition(
1100 browser(), url2_, NEW_FOREGROUND_TAB,
1101 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1102 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
1104 // Kill the original browser then open a new one to trigger a restore.
1105 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
1106 ASSERT_EQ(1u, active_browser_list_->size());
1107 ASSERT_EQ(2, new_browser->tab_strip_model()->count());
1108 ASSERT_EQ(1, new_browser->tab_strip_model()->active_index());
1110 // Confirm that the user agent overrides are properly set.
1111 EXPECT_EQ("override",
1112 new_browser->tab_strip_model()->GetWebContentsAt(0)->
1113 GetUserAgentOverride());
1114 EXPECT_EQ("",
1115 new_browser->tab_strip_model()->GetWebContentsAt(1)->
1116 GetUserAgentOverride());
1119 // Regression test for crbug.com/125958. When restoring a pinned selected tab in
1120 // a setting where there are existing tabs, the selected index computation was
1121 // wrong, leading to the wrong tab getting selected, DCHECKs firing, and the
1122 // pinned tab not getting loaded.
1123 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, RestorePinnedSelectedTab) {
1124 // Create a pinned tab.
1125 ui_test_utils::NavigateToURL(browser(), url1_);
1126 browser()->tab_strip_model()->SetTabPinned(0, true);
1127 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
1128 // Create a nonpinned tab.
1129 ui_test_utils::NavigateToURLWithDisposition(
1130 browser(), url2_, NEW_FOREGROUND_TAB,
1131 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1132 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
1133 // Select the pinned tab.
1134 browser()->tab_strip_model()->ActivateTabAt(0, true);
1135 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
1136 Profile* profile = browser()->profile();
1138 // This will also initiate a session restore, but we're not interested in it.
1139 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
1140 ASSERT_EQ(1u, active_browser_list_->size());
1141 ASSERT_EQ(2, new_browser->tab_strip_model()->count());
1142 ASSERT_EQ(0, new_browser->tab_strip_model()->active_index());
1143 // Close the pinned tab.
1144 chrome::CloseTab(new_browser);
1145 ASSERT_EQ(1, new_browser->tab_strip_model()->count());
1146 ASSERT_EQ(0, new_browser->tab_strip_model()->active_index());
1147 // Use the existing tab to navigate away, so that we can verify it was really
1148 // clobbered.
1149 ui_test_utils::NavigateToURL(new_browser, url3_);
1151 // Restore the session again, clobbering the existing tab.
1152 SessionRestore::RestoreSession(
1153 profile, new_browser,
1154 new_browser->host_desktop_type(),
1155 SessionRestore::CLOBBER_CURRENT_TAB | SessionRestore::SYNCHRONOUS,
1156 std::vector<GURL>());
1158 // The pinned tab is the selected tab.
1159 ASSERT_EQ(2, new_browser->tab_strip_model()->count());
1160 EXPECT_EQ(0, new_browser->tab_strip_model()->active_index());
1161 EXPECT_EQ(url1_,
1162 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1163 EXPECT_EQ(url2_,
1164 new_browser->tab_strip_model()->GetWebContentsAt(1)->GetURL());
1167 // Regression test for crbug.com/240156. When restoring tabs with a navigation,
1168 // the navigation should take active tab focus.
1169 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, RestoreWithNavigateSelectedTab) {
1170 // Create 2 tabs.
1171 ui_test_utils::NavigateToURL(browser(), url1_);
1172 ui_test_utils::NavigateToURLWithDisposition(
1173 browser(), url2_, NEW_FOREGROUND_TAB,
1174 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1176 // Restore the session by calling chrome::Navigate().
1177 Browser* new_browser =
1178 QuitBrowserAndRestoreWithURL(browser(), 3, url3_, true);
1179 ASSERT_EQ(1u, active_browser_list_->size());
1180 ASSERT_EQ(3, new_browser->tab_strip_model()->count());
1181 // Navigated url should be the active tab.
1182 ASSERT_EQ(url3_,
1183 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1186 // Do a clobber restore from the new tab page. This test follows the code path
1187 // of a crash followed by the user clicking restore from the new tab page.
1188 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, ClobberRestoreTest) {
1189 // Create 2 tabs.
1190 ui_test_utils::NavigateToURL(browser(), url1_);
1191 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
1192 ui_test_utils::NavigateToURLWithDisposition(
1193 browser(), url2_, NEW_FOREGROUND_TAB,
1194 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1195 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
1196 Profile* profile = browser()->profile();
1198 // This will also initiate a session restore, but we're not interested in it.
1199 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
1200 ASSERT_EQ(1u, active_browser_list_->size());
1201 ASSERT_EQ(2, new_browser->tab_strip_model()->count());
1202 ASSERT_EQ(1, new_browser->tab_strip_model()->active_index());
1203 // Close the first tab.
1204 chrome::CloseTab(new_browser);
1205 ASSERT_EQ(1, new_browser->tab_strip_model()->count());
1206 ASSERT_EQ(0, new_browser->tab_strip_model()->active_index());
1207 // Use the existing tab to navigate to the NTP.
1208 ui_test_utils::NavigateToURL(new_browser, GURL(chrome::kChromeUINewTabURL));
1210 // Restore the session again, clobbering the existing tab.
1211 SessionRestore::RestoreSession(
1212 profile, new_browser,
1213 new_browser->host_desktop_type(),
1214 SessionRestore::CLOBBER_CURRENT_TAB | SessionRestore::SYNCHRONOUS,
1215 std::vector<GURL>());
1217 // 2 tabs should have been restored, with the existing tab clobbered, giving
1218 // us a total of 2 tabs.
1219 ASSERT_EQ(2, new_browser->tab_strip_model()->count());
1220 EXPECT_EQ(1, new_browser->tab_strip_model()->active_index());
1221 EXPECT_EQ(url1_,
1222 new_browser->tab_strip_model()->GetWebContentsAt(0)->GetURL());
1223 EXPECT_EQ(url2_,
1224 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1227 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, SessionStorage) {
1228 ui_test_utils::NavigateToURL(browser(), url1_);
1229 content::NavigationController* controller =
1230 &browser()->tab_strip_model()->GetActiveWebContents()->GetController();
1231 ASSERT_TRUE(controller->GetDefaultSessionStorageNamespace());
1232 std::string session_storage_persistent_id =
1233 controller->GetDefaultSessionStorageNamespace()->persistent_id();
1234 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
1235 ASSERT_EQ(1u, active_browser_list_->size());
1236 ASSERT_EQ(url1_,
1237 new_browser->tab_strip_model()->GetActiveWebContents()->GetURL());
1238 content::NavigationController* new_controller =
1239 &new_browser->tab_strip_model()->GetActiveWebContents()->GetController();
1240 ASSERT_TRUE(new_controller->GetDefaultSessionStorageNamespace());
1241 std::string restored_session_storage_persistent_id =
1242 new_controller->GetDefaultSessionStorageNamespace()->persistent_id();
1243 EXPECT_EQ(session_storage_persistent_id,
1244 restored_session_storage_persistent_id);
1247 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, SessionStorageAfterTabReplace) {
1248 // Simulate what prerendering does: create a new WebContents with the same
1249 // SessionStorageNamespace as an existing tab, then replace the tab with it.
1251 content::NavigationController* controller =
1252 &browser()->tab_strip_model()->GetActiveWebContents()->GetController();
1253 ASSERT_TRUE(controller->GetDefaultSessionStorageNamespace());
1255 content::SessionStorageNamespaceMap session_storage_namespace_map;
1256 session_storage_namespace_map[std::string()] =
1257 controller->GetDefaultSessionStorageNamespace();
1258 scoped_ptr<content::WebContents> web_contents(
1259 content::WebContents::CreateWithSessionStorage(
1260 content::WebContents::CreateParams(browser()->profile()),
1261 session_storage_namespace_map));
1263 TabStripModel* tab_strip_model = browser()->tab_strip_model();
1264 scoped_ptr<content::WebContents> old_web_contents(
1265 tab_strip_model->ReplaceWebContentsAt(
1266 tab_strip_model->active_index(), web_contents.release()));
1267 // Navigate with the new tab.
1268 ui_test_utils::NavigateToURL(browser(), url2_);
1269 // old_web_contents goes out of scope.
1272 // Check that the sessionStorage data is going to be persisted.
1273 content::NavigationController* controller =
1274 &browser()->tab_strip_model()->GetActiveWebContents()->GetController();
1275 EXPECT_TRUE(
1276 controller->GetDefaultSessionStorageNamespace()->should_persist());
1278 // Quit and restore. Check that no extra tabs were created.
1279 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
1280 ASSERT_EQ(1u, active_browser_list_->size());
1281 EXPECT_EQ(1, new_browser->tab_strip_model()->count());