Update broken references to image assets
[chromium-blink-merge.git] / chrome / browser / sessions / tab_restore_browsertest.cc
blobe8b7b760acc774e36c169b87a92158f9526bb5f5
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 "base/basictypes.h"
6 #include "base/command_line.h"
7 #include "base/files/file_path.h"
8 #include "base/run_loop.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/test/test_timeouts.h"
11 #include "chrome/app/chrome_command_ids.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/sessions/tab_restore_service.h"
14 #include "chrome/browser/sessions/tab_restore_service_factory.h"
15 #include "chrome/browser/sessions/tab_restore_service_observer.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_commands.h"
18 #include "chrome/browser/ui/browser_list.h"
19 #include "chrome/browser/ui/browser_tabstrip.h"
20 #include "chrome/browser/ui/find_bar/find_notification_details.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h"
22 #include "chrome/common/chrome_paths.h"
23 #include "chrome/common/url_constants.h"
24 #include "chrome/test/base/in_process_browser_test.h"
25 #include "chrome/test/base/ui_test_utils.h"
26 #include "content/public/browser/navigation_controller.h"
27 #include "content/public/browser/notification_service.h"
28 #include "content/public/browser/notification_source.h"
29 #include "content/public/browser/notification_types.h"
30 #include "content/public/browser/page_navigator.h"
31 #include "content/public/browser/render_view_host.h"
32 #include "content/public/browser/web_contents.h"
33 #include "content/public/test/browser_test_utils.h"
34 #include "net/base/net_util.h"
35 #include "net/test/embedded_test_server/embedded_test_server.h"
36 #include "third_party/WebKit/public/web/WebFindOptions.h"
37 #include "url/gurl.h"
39 // Class used to run a message loop waiting for the TabRestoreService to finish
40 // loading. Does nothing if the TabRestoreService was already loaded.
41 class WaitForLoadObserver : public TabRestoreServiceObserver {
42 public:
43 explicit WaitForLoadObserver(Browser* browser)
44 : tab_restore_service_(
45 TabRestoreServiceFactory::GetForProfile(browser->profile())),
46 do_wait_(!tab_restore_service_->IsLoaded()) {
47 if (do_wait_)
48 tab_restore_service_->AddObserver(this);
51 ~WaitForLoadObserver() override {
52 if (do_wait_)
53 tab_restore_service_->RemoveObserver(this);
56 void Wait() {
57 if (do_wait_)
58 run_loop_.Run();
61 private:
62 // Overridden from TabRestoreServiceObserver:
63 void TabRestoreServiceChanged(TabRestoreService* service) override {}
64 void TabRestoreServiceDestroyed(TabRestoreService* service) override {}
65 void TabRestoreServiceLoaded(TabRestoreService* service) override {
66 DCHECK(do_wait_);
67 run_loop_.Quit();
70 TabRestoreService* tab_restore_service_;
71 const bool do_wait_;
72 base::RunLoop run_loop_;
74 DISALLOW_COPY_AND_ASSIGN(WaitForLoadObserver);
77 class TabRestoreTest : public InProcessBrowserTest {
78 public:
79 TabRestoreTest() : active_browser_list_(NULL) {
80 url1_ = ui_test_utils::GetTestUrl(
81 base::FilePath().AppendASCII("session_history"),
82 base::FilePath().AppendASCII("bot1.html"));
83 url2_ = ui_test_utils::GetTestUrl(
84 base::FilePath().AppendASCII("session_history"),
85 base::FilePath().AppendASCII("bot2.html"));
88 protected:
89 void SetUpOnMainThread() override {
90 active_browser_list_ = BrowserList::GetInstance(chrome::GetActiveDesktop());
91 InProcessBrowserTest::SetUpOnMainThread();
94 Browser* GetBrowser(int index) {
95 CHECK(static_cast<int>(active_browser_list_->size()) > index);
96 return active_browser_list_->get(index);
99 // Adds tabs to the given browser, all navigated to url1_. Returns
100 // the final number of tabs.
101 int AddSomeTabs(Browser* browser, int how_many) {
102 int starting_tab_count = browser->tab_strip_model()->count();
104 for (int i = 0; i < how_many; ++i) {
105 ui_test_utils::NavigateToURLWithDisposition(
106 browser, url1_, NEW_FOREGROUND_TAB,
107 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
109 int tab_count = browser->tab_strip_model()->count();
110 EXPECT_EQ(starting_tab_count + how_many, tab_count);
111 return tab_count;
114 void CloseTab(int index) {
115 content::WebContentsDestroyedWatcher destroyed_watcher(
116 browser()->tab_strip_model()->GetWebContentsAt(index));
117 browser()->tab_strip_model()->CloseWebContentsAt(
118 index, TabStripModel::CLOSE_CREATE_HISTORICAL_TAB);
119 destroyed_watcher.Wait();
122 // Uses the undo-close-tab accelerator to undo a close-tab or close-window
123 // operation. The newly restored tab is expected to appear in the
124 // window at index |expected_window_index|, at the |expected_tabstrip_index|,
125 // and to be active. If |expected_window_index| is equal to the number of
126 // current windows, the restored tab is expected to be created in a new
127 // window (since the index is 0-based).
128 void RestoreTab(int expected_window_index, int expected_tabstrip_index) {
129 int window_count = static_cast<int>(active_browser_list_->size());
130 ASSERT_GT(window_count, 0);
132 bool expect_new_window = (expected_window_index == window_count);
134 Browser* browser;
135 if (expect_new_window) {
136 browser = active_browser_list_->get(0);
137 } else {
138 browser = GetBrowser(expected_window_index);
140 int tab_count = browser->tab_strip_model()->count();
141 ASSERT_GT(tab_count, 0);
143 // Restore the tab.
144 content::WindowedNotificationObserver tab_added_observer(
145 chrome::NOTIFICATION_TAB_PARENTED,
146 content::NotificationService::AllSources());
147 content::WindowedNotificationObserver tab_loaded_observer(
148 content::NOTIFICATION_LOAD_STOP,
149 content::NotificationService::AllSources());
151 WaitForLoadObserver waiter(browser);
152 chrome::RestoreTab(browser);
153 waiter.Wait();
155 tab_added_observer.Wait();
156 tab_loaded_observer.Wait();
158 if (expect_new_window) {
159 int new_window_count = static_cast<int>(active_browser_list_->size());
160 EXPECT_EQ(++window_count, new_window_count);
161 browser = GetBrowser(expected_window_index);
162 } else {
163 EXPECT_EQ(++tab_count, browser->tab_strip_model()->count());
166 // Get a handle to the restored tab.
167 ASSERT_GT(browser->tab_strip_model()->count(), expected_tabstrip_index);
169 // Ensure that the tab and window are active.
170 EXPECT_EQ(expected_tabstrip_index,
171 browser->tab_strip_model()->active_index());
174 void GoBack(Browser* browser) {
175 content::WindowedNotificationObserver observer(
176 content::NOTIFICATION_LOAD_STOP,
177 content::NotificationService::AllSources());
178 chrome::GoBack(browser, CURRENT_TAB);
179 observer.Wait();
182 void EnsureTabFinishedRestoring(content::WebContents* tab) {
183 content::NavigationController* controller = &tab->GetController();
184 if (!controller->NeedsReload() && !controller->GetPendingEntry() &&
185 !controller->GetWebContents()->IsLoading())
186 return;
188 content::WindowedNotificationObserver observer(
189 content::NOTIFICATION_LOAD_STOP,
190 content::Source<content::NavigationController>(controller));
191 observer.Wait();
194 GURL url1_;
195 GURL url2_;
197 const BrowserList* active_browser_list_;
199 private:
200 DISALLOW_COPY_AND_ASSIGN(TabRestoreTest);
203 // Close the end tab in the current window, then restore it. The tab should be
204 // in its original position, and active.
205 IN_PROC_BROWSER_TEST_F(TabRestoreTest, Basic) {
206 int starting_tab_count = browser()->tab_strip_model()->count();
207 int tab_count = AddSomeTabs(browser(), 1);
209 int closed_tab_index = tab_count - 1;
210 CloseTab(closed_tab_index);
211 EXPECT_EQ(starting_tab_count, browser()->tab_strip_model()->count());
213 ASSERT_NO_FATAL_FAILURE(RestoreTab(0, closed_tab_index));
215 // And make sure everything looks right.
216 EXPECT_EQ(starting_tab_count + 1, browser()->tab_strip_model()->count());
217 EXPECT_EQ(closed_tab_index, browser()->tab_strip_model()->active_index());
218 EXPECT_EQ(url1_,
219 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
222 // Close a tab not at the end of the current window, then restore it. The tab
223 // should be in its original position, and active.
224 IN_PROC_BROWSER_TEST_F(TabRestoreTest, MiddleTab) {
225 int starting_tab_count = browser()->tab_strip_model()->count();
226 AddSomeTabs(browser(), 3);
228 // Close one in the middle
229 int closed_tab_index = starting_tab_count + 1;
230 CloseTab(closed_tab_index);
231 EXPECT_EQ(starting_tab_count + 2, browser()->tab_strip_model()->count());
233 ASSERT_NO_FATAL_FAILURE(RestoreTab(0, closed_tab_index));
235 // And make sure everything looks right.
236 EXPECT_EQ(starting_tab_count + 3, browser()->tab_strip_model()->count());
237 EXPECT_EQ(closed_tab_index, browser()->tab_strip_model()->active_index());
238 EXPECT_EQ(url1_,
239 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
242 // Close a tab, switch windows, then restore the tab. The tab should be in its
243 // original window and position, and active.
244 IN_PROC_BROWSER_TEST_F(TabRestoreTest, RestoreToDifferentWindow) {
245 int starting_tab_count = browser()->tab_strip_model()->count();
246 AddSomeTabs(browser(), 3);
248 // Close one in the middle
249 int closed_tab_index = starting_tab_count + 1;
250 CloseTab(closed_tab_index);
251 EXPECT_EQ(starting_tab_count + 2, browser()->tab_strip_model()->count());
253 // Create a new browser.
254 ui_test_utils::NavigateToURLWithDisposition(
255 browser(), GURL(chrome::kChromeUINewTabURL), NEW_WINDOW,
256 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
257 EXPECT_EQ(2u, active_browser_list_->size());
259 // Restore tab into original browser.
260 ASSERT_NO_FATAL_FAILURE(RestoreTab(0, closed_tab_index));
262 // And make sure everything looks right.
263 EXPECT_EQ(starting_tab_count + 3, browser()->tab_strip_model()->count());
264 EXPECT_EQ(closed_tab_index, browser()->tab_strip_model()->active_index());
265 EXPECT_EQ(url1_,
266 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
269 // Close a tab, open a new window, close the first window, then restore the
270 // tab. It should be in a new window.
271 // If this becomes flaky, use http://crbug.com/14774
272 IN_PROC_BROWSER_TEST_F(TabRestoreTest, DISABLED_BasicRestoreFromClosedWindow) {
273 // Navigate to url1 then url2.
274 ui_test_utils::NavigateToURL(browser(), url1_);
275 ui_test_utils::NavigateToURL(browser(), url2_);
277 // Create a new browser.
278 ui_test_utils::NavigateToURLWithDisposition(
279 browser(), GURL(chrome::kChromeUINewTabURL), NEW_WINDOW,
280 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
281 EXPECT_EQ(2u, active_browser_list_->size());
283 // Close the final tab in the first browser.
284 content::WindowedNotificationObserver window_observer(
285 chrome::NOTIFICATION_BROWSER_CLOSED,
286 content::NotificationService::AllSources());
287 CloseTab(0);
288 window_observer.Wait();
290 ASSERT_NO_FATAL_FAILURE(RestoreTab(1, 0));
292 // Tab should be in a new window.
293 Browser* browser = GetBrowser(1);
294 content::WebContents* web_contents =
295 browser->tab_strip_model()->GetActiveWebContents();
296 // And make sure the URLs match.
297 EXPECT_EQ(url2_, web_contents->GetURL());
298 GoBack(browser);
299 EXPECT_EQ(url1_, web_contents->GetURL());
302 #if defined(OS_WIN)
303 // Flakily times out: http://crbug.com/171503
304 #define MAYBE_DontLoadRestoredTab DISABLED_DontLoadRestoredTab
305 #else
306 #define MAYBE_DontLoadRestoredTab DontLoadRestoredTab
307 #endif
309 // Restore a tab then make sure it doesn't restore again.
310 IN_PROC_BROWSER_TEST_F(TabRestoreTest, MAYBE_DontLoadRestoredTab) {
311 // Add two tabs
312 int starting_tab_count = browser()->tab_strip_model()->count();
313 AddSomeTabs(browser(), 2);
314 ASSERT_EQ(browser()->tab_strip_model()->count(), starting_tab_count + 2);
316 // Close one of them.
317 CloseTab(0);
318 ASSERT_EQ(browser()->tab_strip_model()->count(), starting_tab_count + 1);
320 // Restore it.
321 ASSERT_NO_FATAL_FAILURE(RestoreTab(0, 0));
322 ASSERT_EQ(browser()->tab_strip_model()->count(), starting_tab_count + 2);
324 // Make sure that there's nothing else to restore.
325 ASSERT_EQ(chrome::GetRestoreTabType(browser()),
326 TabStripModelDelegate::RESTORE_NONE);
329 // Open a window with multiple tabs, close a tab, then close the window.
330 // Restore both and make sure the tab goes back into the window.
331 IN_PROC_BROWSER_TEST_F(TabRestoreTest, RestoreWindowAndTab) {
332 int starting_tab_count = browser()->tab_strip_model()->count();
333 AddSomeTabs(browser(), 3);
335 // Close one in the middle
336 int closed_tab_index = starting_tab_count + 1;
337 CloseTab(closed_tab_index);
338 EXPECT_EQ(starting_tab_count + 2, browser()->tab_strip_model()->count());
340 // Create a new browser.
341 ui_test_utils::NavigateToURLWithDisposition(
342 browser(), GURL(chrome::kChromeUINewTabURL), NEW_WINDOW,
343 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
344 EXPECT_EQ(2u, active_browser_list_->size());
346 // Close the first browser.
347 content::WindowedNotificationObserver observer(
348 chrome::NOTIFICATION_BROWSER_CLOSED,
349 content::NotificationService::AllSources());
350 chrome::CloseWindow(browser());
351 observer.Wait();
352 EXPECT_EQ(1u, active_browser_list_->size());
354 // Restore the first window. The expected_tabstrip_index (second argument)
355 // indicates the expected active tab.
356 ASSERT_NO_FATAL_FAILURE(RestoreTab(1, starting_tab_count + 1));
357 Browser* browser = GetBrowser(1);
358 EXPECT_EQ(starting_tab_count + 2, browser->tab_strip_model()->count());
360 // Restore the closed tab.
361 ASSERT_NO_FATAL_FAILURE(RestoreTab(1, closed_tab_index));
362 EXPECT_EQ(starting_tab_count + 3, browser->tab_strip_model()->count());
363 EXPECT_EQ(url1_,
364 browser->tab_strip_model()->GetActiveWebContents()->GetURL());
367 // Open a window with two tabs, close both (closing the window), then restore
368 // both. Make sure both restored tabs are in the same window.
369 IN_PROC_BROWSER_TEST_F(TabRestoreTest, RestoreIntoSameWindow) {
370 ui_test_utils::NavigateToURLWithDisposition(
371 browser(), url1_, NEW_FOREGROUND_TAB,
372 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
373 // Navigate the rightmost one to url2_ for easier identification.
374 ui_test_utils::NavigateToURLWithDisposition(
375 browser(), url2_, NEW_FOREGROUND_TAB,
376 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
378 // Create a new browser.
379 ui_test_utils::NavigateToURLWithDisposition(
380 browser(), GURL(chrome::kChromeUINewTabURL), NEW_WINDOW,
381 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
382 EXPECT_EQ(2u, active_browser_list_->size());
384 // Close all but one tab in the first browser, left to right.
385 while (browser()->tab_strip_model()->count() > 1)
386 CloseTab(0);
388 // Close the last tab, closing the browser.
389 content::WindowedNotificationObserver observer(
390 chrome::NOTIFICATION_BROWSER_CLOSED,
391 content::NotificationService::AllSources());
392 CloseTab(0);
393 observer.Wait();
394 EXPECT_EQ(1u, active_browser_list_->size());
396 // Restore the last-closed tab into a new window.
397 ASSERT_NO_FATAL_FAILURE(RestoreTab(1, 0));
398 Browser* browser = GetBrowser(1);
399 EXPECT_EQ(1, browser->tab_strip_model()->count());
400 EXPECT_EQ(url2_,
401 browser->tab_strip_model()->GetActiveWebContents()->GetURL());
403 // Restore the next-to-last-closed tab into the same window.
404 ASSERT_NO_FATAL_FAILURE(RestoreTab(1, 0));
405 EXPECT_EQ(2, browser->tab_strip_model()->count());
406 EXPECT_EQ(url1_,
407 browser->tab_strip_model()->GetActiveWebContents()->GetURL());
410 // Tests that a duplicate history entry is not created when we restore a page
411 // to an existing SiteInstance. (Bug 1230446)
412 IN_PROC_BROWSER_TEST_F(TabRestoreTest, RestoreWithExistingSiteInstance) {
413 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
415 GURL http_url1(embedded_test_server()->GetURL("/title1.html"));
416 GURL http_url2(embedded_test_server()->GetURL("/title2.html"));
417 int tab_count = browser()->tab_strip_model()->count();
419 // Add a tab
420 ui_test_utils::NavigateToURLWithDisposition(
421 browser(), http_url1, NEW_FOREGROUND_TAB,
422 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
423 EXPECT_EQ(++tab_count, browser()->tab_strip_model()->count());
425 // Navigate to another same-site URL.
426 content::WebContents* tab =
427 browser()->tab_strip_model()->GetWebContentsAt(tab_count - 1);
428 content::WindowedNotificationObserver observer(
429 content::NOTIFICATION_LOAD_STOP,
430 content::NotificationService::AllSources());
431 static_cast<content::WebContentsDelegate*>(browser())->OpenURLFromTab(
432 tab,
433 content::OpenURLParams(http_url2, content::Referrer(), CURRENT_TAB,
434 ui::PAGE_TRANSITION_TYPED, false));
435 observer.Wait();
437 // Close the tab.
438 CloseTab(1);
440 // Create a new tab to the original site. Assuming process-per-site is
441 // enabled, this will ensure that the SiteInstance used by the restored tab
442 // will already exist when the restore happens.
443 ui_test_utils::NavigateToURLWithDisposition(
444 browser(), http_url2, NEW_FOREGROUND_TAB,
445 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
447 // Restore the closed tab.
448 ASSERT_NO_FATAL_FAILURE(RestoreTab(0, tab_count - 1));
450 // And make sure the URLs match.
451 EXPECT_EQ(http_url2,
452 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
453 GoBack(browser());
454 EXPECT_EQ(http_url1,
455 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
458 // See crbug.com/248574
459 #if defined(OS_WIN)
460 #define MAYBE_RestoreCrossSiteWithExistingSiteInstance DISABLED_RestoreCrossSiteWithExistingSiteInstance
461 #else
462 #define MAYBE_RestoreCrossSiteWithExistingSiteInstance RestoreCrossSiteWithExistingSiteInstance
463 #endif
465 // Tests that the SiteInstances used for entries in a restored tab's history
466 // are given appropriate max page IDs, even if the renderer for the entry
467 // already exists. (Bug 1204135)
468 IN_PROC_BROWSER_TEST_F(TabRestoreTest,
469 MAYBE_RestoreCrossSiteWithExistingSiteInstance) {
470 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
472 GURL http_url1(embedded_test_server()->GetURL("/title1.html"));
473 GURL http_url2(embedded_test_server()->GetURL("/title2.html"));
475 int tab_count = browser()->tab_strip_model()->count();
477 // Add a tab
478 ui_test_utils::NavigateToURLWithDisposition(
479 browser(), http_url1, NEW_FOREGROUND_TAB,
480 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
481 EXPECT_EQ(++tab_count, browser()->tab_strip_model()->count());
483 // Navigate to more URLs, then a cross-site URL.
484 ui_test_utils::NavigateToURL(browser(), http_url2);
485 ui_test_utils::NavigateToURL(browser(), http_url1);
486 ui_test_utils::NavigateToURL(browser(), url1_);
488 // Close the tab.
489 CloseTab(1);
491 // Create a new tab to the original site. Assuming process-per-site is
492 // enabled, this will ensure that the SiteInstance will already exist when
493 // the user clicks Back in the restored tab.
494 ui_test_utils::NavigateToURLWithDisposition(
495 browser(), http_url2, NEW_FOREGROUND_TAB,
496 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
498 // Restore the closed tab.
499 ASSERT_NO_FATAL_FAILURE(RestoreTab(0, tab_count - 1));
501 // And make sure the URLs match.
502 EXPECT_EQ(url1_,
503 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
504 GoBack(browser());
505 EXPECT_EQ(http_url1,
506 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
508 // Navigating to a new URL should clear the forward list, because the max
509 // page ID of the renderer should have been updated when we restored the tab.
510 ui_test_utils::NavigateToURL(browser(), http_url2);
511 EXPECT_FALSE(chrome::CanGoForward(browser()));
512 EXPECT_EQ(http_url2,
513 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
516 IN_PROC_BROWSER_TEST_F(TabRestoreTest, RestoreWindow) {
517 // Create a new window.
518 size_t window_count = active_browser_list_->size();
519 ui_test_utils::NavigateToURLWithDisposition(
520 browser(), GURL(chrome::kChromeUINewTabURL), NEW_WINDOW,
521 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
522 EXPECT_EQ(++window_count, active_browser_list_->size());
524 // Create two more tabs, one with url1, the other url2.
525 int initial_tab_count = browser()->tab_strip_model()->count();
526 ui_test_utils::NavigateToURLWithDisposition(
527 browser(), url1_, NEW_FOREGROUND_TAB,
528 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
529 ui_test_utils::NavigateToURLWithDisposition(
530 browser(), url2_, NEW_FOREGROUND_TAB,
531 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
533 // Close the window.
534 content::WindowedNotificationObserver close_window_observer(
535 chrome::NOTIFICATION_BROWSER_CLOSED,
536 content::NotificationService::AllSources());
537 chrome::CloseWindow(browser());
538 close_window_observer.Wait();
539 EXPECT_EQ(window_count - 1, active_browser_list_->size());
541 // Restore the window.
542 content::WindowedNotificationObserver open_window_observer(
543 chrome::NOTIFICATION_BROWSER_OPENED,
544 content::NotificationService::AllSources());
545 content::WindowedNotificationObserver load_stop_observer(
546 content::NOTIFICATION_LOAD_STOP,
547 content::NotificationService::AllSources());
548 chrome::RestoreTab(active_browser_list_->get(0));
549 open_window_observer.Wait();
550 EXPECT_EQ(window_count, active_browser_list_->size());
552 Browser* browser = GetBrowser(1);
553 EXPECT_EQ(initial_tab_count + 2, browser->tab_strip_model()->count());
554 load_stop_observer.Wait();
556 content::WebContents* restored_tab =
557 browser->tab_strip_model()->GetWebContentsAt(initial_tab_count);
558 EnsureTabFinishedRestoring(restored_tab);
559 EXPECT_EQ(url1_, restored_tab->GetURL());
561 restored_tab =
562 browser->tab_strip_model()->GetWebContentsAt(initial_tab_count + 1);
563 EnsureTabFinishedRestoring(restored_tab);
564 EXPECT_EQ(url2_, restored_tab->GetURL());
567 // Restore tab with special URL chrome://credits/ and make sure the page loads
568 // properly after restore. See http://crbug.com/31905.
569 IN_PROC_BROWSER_TEST_F(TabRestoreTest, RestoreTabWithSpecialURL) {
570 // Navigate new tab to a special URL.
571 ui_test_utils::NavigateToURLWithDisposition(
572 browser(), GURL(chrome::kChromeUICreditsURL), NEW_FOREGROUND_TAB,
573 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
575 // Close the tab.
576 CloseTab(1);
578 // Restore the closed tab.
579 ASSERT_NO_FATAL_FAILURE(RestoreTab(0, 1));
580 content::WebContents* tab = browser()->tab_strip_model()->GetWebContentsAt(1);
581 EnsureTabFinishedRestoring(tab);
583 // See if content is as expected.
584 EXPECT_GT(
585 ui_test_utils::FindInPage(tab, base::ASCIIToUTF16("webkit"), true, false,
586 NULL, NULL),
590 // Restore tab with special URL in its navigation history, go back to that
591 // entry and see that it loads properly. See http://crbug.com/31905
592 IN_PROC_BROWSER_TEST_F(TabRestoreTest, RestoreTabWithSpecialURLOnBack) {
593 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
595 const GURL http_url(embedded_test_server()->GetURL("/title1.html"));
597 // Navigate new tab to a special URL.
598 ui_test_utils::NavigateToURLWithDisposition(
599 browser(), GURL(chrome::kChromeUICreditsURL), NEW_FOREGROUND_TAB,
600 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
602 // Then navigate to a normal URL.
603 ui_test_utils::NavigateToURL(browser(), http_url);
605 // Close the tab.
606 CloseTab(1);
608 // Restore the closed tab.
609 ASSERT_NO_FATAL_FAILURE(RestoreTab(0, 1));
610 content::WebContents* tab = browser()->tab_strip_model()->GetWebContentsAt(1);
611 EnsureTabFinishedRestoring(tab);
612 ASSERT_EQ(http_url, tab->GetURL());
614 // Go back, and see if content is as expected.
615 GoBack(browser());
616 EXPECT_GT(
617 ui_test_utils::FindInPage(tab, base::ASCIIToUTF16("webkit"), true, false,
618 NULL, NULL),
622 IN_PROC_BROWSER_TEST_F(TabRestoreTest, PRE_RestoreOnStartup) {
623 // This results in a new tab at the end with url1.
624 AddSomeTabs(browser(), 1);
626 while (browser()->tab_strip_model()->count())
627 CloseTab(0);
630 // Verifies restoring a tab works on startup.
631 IN_PROC_BROWSER_TEST_F(TabRestoreTest, RestoreOnStartup) {
632 ASSERT_NO_FATAL_FAILURE(RestoreTab(0, 1));
633 EXPECT_EQ(url1_,
634 browser()->tab_strip_model()->GetWebContentsAt(1)->GetURL());