1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/basictypes.h"
6#include "base/command_line.h"
7#include "base/files/file_path.h"
8#include "base/strings/utf_string_conversions.h"
9#include "base/test/test_timeouts.h"
10#include "chrome/app/chrome_command_ids.h"
11#include "chrome/browser/chrome_notification_types.h"
12#include "chrome/browser/ui/browser.h"
13#include "chrome/browser/ui/browser_commands.h"
14#include "chrome/browser/ui/browser_list.h"
15#include "chrome/browser/ui/browser_tabstrip.h"
16#include "chrome/browser/ui/find_bar/find_notification_details.h"
17#include "chrome/browser/ui/tabs/tab_strip_model.h"
18#include "chrome/common/chrome_paths.h"
19#include "chrome/common/url_constants.h"
20#include "chrome/test/base/in_process_browser_test.h"
21#include "chrome/test/base/ui_test_utils.h"
22#include "content/public/browser/navigation_controller.h"
23#include "content/public/browser/notification_service.h"
24#include "content/public/browser/notification_source.h"
25#include "content/public/browser/notification_types.h"
26#include "content/public/browser/page_navigator.h"
27#include "content/public/browser/render_view_host.h"
28#include "content/public/browser/web_contents.h"
29#include "content/public/test/browser_test_utils.h"
30#include "net/base/net_util.h"
31#include "net/test/embedded_test_server/embedded_test_server.h"
32#include "third_party/WebKit/public/web/WebFindOptions.h"
33#include "url/gurl.h"
34
35class TabRestoreTest : public InProcessBrowserTest {
36 public:
37  TabRestoreTest() : active_browser_list_(NULL) {
38    url1_ = ui_test_utils::GetTestUrl(
39        base::FilePath().AppendASCII("session_history"),
40        base::FilePath().AppendASCII("bot1.html"));
41    url2_ = ui_test_utils::GetTestUrl(
42        base::FilePath().AppendASCII("session_history"),
43        base::FilePath().AppendASCII("bot2.html"));
44  }
45
46 protected:
47  virtual void SetUpOnMainThread() OVERRIDE {
48    active_browser_list_ = BrowserList::GetInstance(chrome::GetActiveDesktop());
49    InProcessBrowserTest::SetUpOnMainThread();
50  }
51
52  Browser* GetBrowser(int index) {
53
54    CHECK(static_cast<int>(active_browser_list_->size()) > index);
55    return active_browser_list_->get(index);
56  }
57
58  // Adds tabs to the given browser, all navigated to url1_. Returns
59  // the final number of tabs.
60  int AddSomeTabs(Browser* browser, int how_many) {
61    int starting_tab_count = browser->tab_strip_model()->count();
62
63    for (int i = 0; i < how_many; ++i) {
64      ui_test_utils::NavigateToURLWithDisposition(
65          browser, url1_, NEW_FOREGROUND_TAB,
66          ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
67    }
68    int tab_count = browser->tab_strip_model()->count();
69    EXPECT_EQ(starting_tab_count + how_many, tab_count);
70    return tab_count;
71  }
72
73  void CloseTab(int index) {
74    content::WebContentsDestroyedWatcher destroyed_watcher(
75        browser()->tab_strip_model()->GetWebContentsAt(index));
76    browser()->tab_strip_model()->CloseWebContentsAt(
77        index, TabStripModel::CLOSE_CREATE_HISTORICAL_TAB);
78    destroyed_watcher.Wait();
79  }
80
81  // Uses the undo-close-tab accelerator to undo a close-tab or close-window
82  // operation. The newly restored tab is expected to appear in the
83  // window at index |expected_window_index|, at the |expected_tabstrip_index|,
84  // and to be active. If |expected_window_index| is equal to the number of
85  // current windows, the restored tab is expected to be created in a new
86  // window (since the index is 0-based).
87  void RestoreTab(int expected_window_index,
88                  int expected_tabstrip_index) {
89    int window_count = static_cast<int>(active_browser_list_->size());
90    ASSERT_GT(window_count, 0);
91
92    bool expect_new_window = (expected_window_index == window_count);
93
94    Browser* browser;
95    if (expect_new_window) {
96      browser = active_browser_list_->get(0);
97    } else {
98      browser = GetBrowser(expected_window_index);
99    }
100    int tab_count = browser->tab_strip_model()->count();
101    ASSERT_GT(tab_count, 0);
102
103    // Restore the tab.
104    content::WindowedNotificationObserver tab_added_observer(
105        chrome::NOTIFICATION_TAB_PARENTED,
106        content::NotificationService::AllSources());
107    content::WindowedNotificationObserver tab_loaded_observer(
108        content::NOTIFICATION_LOAD_STOP,
109        content::NotificationService::AllSources());
110    chrome::RestoreTab(browser);
111    tab_added_observer.Wait();
112    tab_loaded_observer.Wait();
113
114    if (expect_new_window) {
115      int new_window_count = static_cast<int>(active_browser_list_->size());
116      EXPECT_EQ(++window_count, new_window_count);
117      browser = GetBrowser(expected_window_index);
118    } else {
119      EXPECT_EQ(++tab_count, browser->tab_strip_model()->count());
120    }
121
122    // Get a handle to the restored tab.
123    ASSERT_GT(browser->tab_strip_model()->count(), expected_tabstrip_index);
124
125    // Ensure that the tab and window are active.
126    EXPECT_EQ(expected_tabstrip_index,
127              browser->tab_strip_model()->active_index());
128  }
129
130  void GoBack(Browser* browser) {
131    content::WindowedNotificationObserver observer(
132        content::NOTIFICATION_LOAD_STOP,
133        content::NotificationService::AllSources());
134    chrome::GoBack(browser, CURRENT_TAB);
135    observer.Wait();
136  }
137
138  void EnsureTabFinishedRestoring(content::WebContents* tab) {
139    content::NavigationController* controller = &tab->GetController();
140    if (!controller->NeedsReload() && !controller->GetPendingEntry() &&
141        !controller->GetWebContents()->IsLoading())
142      return;
143
144    content::WindowedNotificationObserver observer(
145        content::NOTIFICATION_LOAD_STOP,
146        content::Source<content::NavigationController>(controller));
147    observer.Wait();
148  }
149
150  GURL url1_;
151  GURL url2_;
152
153  const BrowserList* active_browser_list_;
154
155 private:
156  DISALLOW_COPY_AND_ASSIGN(TabRestoreTest);
157};
158
159// Close the end tab in the current window, then restore it. The tab should be
160// in its original position, and active.
161IN_PROC_BROWSER_TEST_F(TabRestoreTest, Basic) {
162  int starting_tab_count = browser()->tab_strip_model()->count();
163  int tab_count = AddSomeTabs(browser(), 1);
164
165  int closed_tab_index = tab_count - 1;
166  CloseTab(closed_tab_index);
167  EXPECT_EQ(starting_tab_count, browser()->tab_strip_model()->count());
168
169  ASSERT_NO_FATAL_FAILURE(RestoreTab(0, closed_tab_index));
170
171  // And make sure everything looks right.
172  EXPECT_EQ(starting_tab_count + 1, browser()->tab_strip_model()->count());
173  EXPECT_EQ(closed_tab_index, browser()->tab_strip_model()->active_index());
174  EXPECT_EQ(url1_,
175            browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
176}
177
178// Close a tab not at the end of the current window, then restore it. The tab
179// should be in its original position, and active.
180IN_PROC_BROWSER_TEST_F(TabRestoreTest, MiddleTab) {
181  int starting_tab_count = browser()->tab_strip_model()->count();
182  AddSomeTabs(browser(), 3);
183
184  // Close one in the middle
185  int closed_tab_index = starting_tab_count + 1;
186  CloseTab(closed_tab_index);
187  EXPECT_EQ(starting_tab_count + 2, browser()->tab_strip_model()->count());
188
189  ASSERT_NO_FATAL_FAILURE(RestoreTab(0, closed_tab_index));
190
191  // And make sure everything looks right.
192  EXPECT_EQ(starting_tab_count + 3, browser()->tab_strip_model()->count());
193  EXPECT_EQ(closed_tab_index, browser()->tab_strip_model()->active_index());
194  EXPECT_EQ(url1_,
195            browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
196}
197
198// Close a tab, switch windows, then restore the tab. The tab should be in its
199// original window and position, and active.
200IN_PROC_BROWSER_TEST_F(TabRestoreTest, RestoreToDifferentWindow) {
201  int starting_tab_count = browser()->tab_strip_model()->count();
202  AddSomeTabs(browser(), 3);
203
204  // Close one in the middle
205  int closed_tab_index = starting_tab_count + 1;
206  CloseTab(closed_tab_index);
207  EXPECT_EQ(starting_tab_count + 2, browser()->tab_strip_model()->count());
208
209  // Create a new browser.
210  ui_test_utils::NavigateToURLWithDisposition(
211      browser(), GURL(chrome::kChromeUINewTabURL), NEW_WINDOW,
212      ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
213  EXPECT_EQ(2u, active_browser_list_->size());
214
215  // Restore tab into original browser.
216  ASSERT_NO_FATAL_FAILURE(RestoreTab(0, closed_tab_index));
217
218  // And make sure everything looks right.
219  EXPECT_EQ(starting_tab_count + 3, browser()->tab_strip_model()->count());
220  EXPECT_EQ(closed_tab_index, browser()->tab_strip_model()->active_index());
221  EXPECT_EQ(url1_,
222            browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
223}
224
225// Close a tab, open a new window, close the first window, then restore the
226// tab. It should be in a new window.
227// If this becomes flaky, use http://crbug.com/14774
228IN_PROC_BROWSER_TEST_F(TabRestoreTest, DISABLED_BasicRestoreFromClosedWindow) {
229  // Navigate to url1 then url2.
230  ui_test_utils::NavigateToURL(browser(), url1_);
231  ui_test_utils::NavigateToURL(browser(), url2_);
232
233  // Create a new browser.
234  ui_test_utils::NavigateToURLWithDisposition(
235      browser(), GURL(chrome::kChromeUINewTabURL), NEW_WINDOW,
236      ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
237  EXPECT_EQ(2u, active_browser_list_->size());
238
239  // Close the final tab in the first browser.
240  content::WindowedNotificationObserver window_observer(
241      chrome::NOTIFICATION_BROWSER_CLOSED,
242      content::NotificationService::AllSources());
243  CloseTab(0);
244  window_observer.Wait();
245
246  ASSERT_NO_FATAL_FAILURE(RestoreTab(1, 0));
247
248  // Tab should be in a new window.
249  Browser* browser = GetBrowser(1);
250  content::WebContents* web_contents =
251      browser->tab_strip_model()->GetActiveWebContents();
252  // And make sure the URLs match.
253  EXPECT_EQ(url2_, web_contents->GetURL());
254  GoBack(browser);
255  EXPECT_EQ(url1_, web_contents->GetURL());
256}
257
258#if defined(OS_WIN)
259// Flakily times out: http://crbug.com/171503
260#define MAYBE_DontLoadRestoredTab DISABLED_DontLoadRestoredTab
261#else
262#define MAYBE_DontLoadRestoredTab DontLoadRestoredTab
263#endif
264
265// Restore a tab then make sure it doesn't restore again.
266IN_PROC_BROWSER_TEST_F(TabRestoreTest, MAYBE_DontLoadRestoredTab) {
267  // Add two tabs
268  int starting_tab_count = browser()->tab_strip_model()->count();
269  AddSomeTabs(browser(), 2);
270  ASSERT_EQ(browser()->tab_strip_model()->count(), starting_tab_count + 2);
271
272  // Close one of them.
273  CloseTab(0);
274  ASSERT_EQ(browser()->tab_strip_model()->count(), starting_tab_count + 1);
275
276  // Restore it.
277  ASSERT_NO_FATAL_FAILURE(RestoreTab(0, 0));
278  ASSERT_EQ(browser()->tab_strip_model()->count(), starting_tab_count + 2);
279
280  // Make sure that there's nothing else to restore.
281  ASSERT_EQ(chrome::GetRestoreTabType(browser()),
282            TabStripModelDelegate::RESTORE_NONE);
283}
284
285// Open a window with multiple tabs, close a tab, then close the window.
286// Restore both and make sure the tab goes back into the window.
287IN_PROC_BROWSER_TEST_F(TabRestoreTest, RestoreWindowAndTab) {
288  int starting_tab_count = browser()->tab_strip_model()->count();
289  AddSomeTabs(browser(), 3);
290
291  // Close one in the middle
292  int closed_tab_index = starting_tab_count + 1;
293  CloseTab(closed_tab_index);
294  EXPECT_EQ(starting_tab_count + 2, browser()->tab_strip_model()->count());
295
296  // Create a new browser.
297  ui_test_utils::NavigateToURLWithDisposition(
298      browser(), GURL(chrome::kChromeUINewTabURL), NEW_WINDOW,
299      ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
300  EXPECT_EQ(2u, active_browser_list_->size());
301
302  // Close the first browser.
303  content::WindowedNotificationObserver observer(
304      chrome::NOTIFICATION_BROWSER_CLOSED,
305      content::NotificationService::AllSources());
306  chrome::CloseWindow(browser());
307  observer.Wait();
308  EXPECT_EQ(1u, active_browser_list_->size());
309
310  // Restore the first window. The expected_tabstrip_index (second argument)
311  // indicates the expected active tab.
312  ASSERT_NO_FATAL_FAILURE(RestoreTab(1, starting_tab_count + 1));
313  Browser* browser = GetBrowser(1);
314  EXPECT_EQ(starting_tab_count + 2, browser->tab_strip_model()->count());
315
316  // Restore the closed tab.
317  ASSERT_NO_FATAL_FAILURE(RestoreTab(1, closed_tab_index));
318  EXPECT_EQ(starting_tab_count + 3, browser->tab_strip_model()->count());
319  EXPECT_EQ(url1_,
320            browser->tab_strip_model()->GetActiveWebContents()->GetURL());
321}
322
323// Open a window with two tabs, close both (closing the window), then restore
324// both. Make sure both restored tabs are in the same window.
325IN_PROC_BROWSER_TEST_F(TabRestoreTest, RestoreIntoSameWindow) {
326  ui_test_utils::NavigateToURLWithDisposition(
327      browser(), url1_, NEW_FOREGROUND_TAB,
328      ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
329  // Navigate the rightmost one to url2_ for easier identification.
330  ui_test_utils::NavigateToURLWithDisposition(
331      browser(), url2_, NEW_FOREGROUND_TAB,
332      ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
333
334  // Create a new browser.
335  ui_test_utils::NavigateToURLWithDisposition(
336      browser(), GURL(chrome::kChromeUINewTabURL), NEW_WINDOW,
337      ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
338  EXPECT_EQ(2u, active_browser_list_->size());
339
340  // Close all but one tab in the first browser, left to right.
341  while (browser()->tab_strip_model()->count() > 1)
342    CloseTab(0);
343
344  // Close the last tab, closing the browser.
345  content::WindowedNotificationObserver observer(
346      chrome::NOTIFICATION_BROWSER_CLOSED,
347      content::NotificationService::AllSources());
348  CloseTab(0);
349  observer.Wait();
350  EXPECT_EQ(1u, active_browser_list_->size());
351
352  // Restore the last-closed tab into a new window.
353  ASSERT_NO_FATAL_FAILURE(RestoreTab(1, 0));
354  Browser* browser = GetBrowser(1);
355  EXPECT_EQ(1, browser->tab_strip_model()->count());
356  EXPECT_EQ(url2_,
357            browser->tab_strip_model()->GetActiveWebContents()->GetURL());
358
359  // Restore the next-to-last-closed tab into the same window.
360  ASSERT_NO_FATAL_FAILURE(RestoreTab(1, 0));
361  EXPECT_EQ(2, browser->tab_strip_model()->count());
362  EXPECT_EQ(url1_,
363            browser->tab_strip_model()->GetActiveWebContents()->GetURL());
364}
365
366// Tests that a duplicate history entry is not created when we restore a page
367// to an existing SiteInstance.  (Bug 1230446)
368IN_PROC_BROWSER_TEST_F(TabRestoreTest, RestoreWithExistingSiteInstance) {
369  ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
370
371  GURL http_url1(embedded_test_server()->GetURL("/title1.html"));
372  GURL http_url2(embedded_test_server()->GetURL("/title2.html"));
373  int tab_count = browser()->tab_strip_model()->count();
374
375  // Add a tab
376  ui_test_utils::NavigateToURLWithDisposition(
377      browser(), http_url1, NEW_FOREGROUND_TAB,
378      ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
379  EXPECT_EQ(++tab_count, browser()->tab_strip_model()->count());
380
381  // Navigate to another same-site URL.
382  content::WebContents* tab =
383      browser()->tab_strip_model()->GetWebContentsAt(tab_count - 1);
384  content::WindowedNotificationObserver observer(
385      content::NOTIFICATION_LOAD_STOP,
386      content::NotificationService::AllSources());
387  static_cast<content::WebContentsDelegate*>(browser())->OpenURLFromTab(
388      tab,
389      content::OpenURLParams(http_url2, content::Referrer(), CURRENT_TAB,
390                             content::PAGE_TRANSITION_TYPED, false));
391  observer.Wait();
392
393  // Close the tab.
394  CloseTab(1);
395
396  // Create a new tab to the original site.  Assuming process-per-site is
397  // enabled, this will ensure that the SiteInstance used by the restored tab
398  // will already exist when the restore happens.
399  ui_test_utils::NavigateToURLWithDisposition(
400      browser(), http_url2, NEW_FOREGROUND_TAB,
401      ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
402
403  // Restore the closed tab.
404  ASSERT_NO_FATAL_FAILURE(RestoreTab(0, tab_count - 1));
405
406  // And make sure the URLs match.
407  EXPECT_EQ(http_url2,
408            browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
409  GoBack(browser());
410  EXPECT_EQ(http_url1,
411            browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
412}
413
414// See crbug.com/248574
415#if defined(OS_WIN)
416#define MAYBE_RestoreCrossSiteWithExistingSiteInstance DISABLED_RestoreCrossSiteWithExistingSiteInstance
417#else
418#define MAYBE_RestoreCrossSiteWithExistingSiteInstance RestoreCrossSiteWithExistingSiteInstance
419#endif
420
421// Tests that the SiteInstances used for entries in a restored tab's history
422// are given appropriate max page IDs, even if the renderer for the entry
423// already exists.  (Bug 1204135)
424IN_PROC_BROWSER_TEST_F(TabRestoreTest,
425                       MAYBE_RestoreCrossSiteWithExistingSiteInstance) {
426  ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
427
428  GURL http_url1(embedded_test_server()->GetURL("/title1.html"));
429  GURL http_url2(embedded_test_server()->GetURL("/title2.html"));
430
431  int tab_count = browser()->tab_strip_model()->count();
432
433  // Add a tab
434  ui_test_utils::NavigateToURLWithDisposition(
435      browser(), http_url1, NEW_FOREGROUND_TAB,
436      ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
437  EXPECT_EQ(++tab_count, browser()->tab_strip_model()->count());
438
439  // Navigate to more URLs, then a cross-site URL.
440  ui_test_utils::NavigateToURLWithDisposition(
441      browser(), http_url2, CURRENT_TAB,
442      ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
443  ui_test_utils::NavigateToURLWithDisposition(
444      browser(), http_url1, CURRENT_TAB,
445      ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
446  ui_test_utils::NavigateToURLWithDisposition(
447      browser(), url1_, CURRENT_TAB,
448      ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
449
450  // Close the tab.
451  CloseTab(1);
452
453  // Create a new tab to the original site.  Assuming process-per-site is
454  // enabled, this will ensure that the SiteInstance will already exist when
455  // the user clicks Back in the restored tab.
456  ui_test_utils::NavigateToURLWithDisposition(
457      browser(), http_url2, NEW_FOREGROUND_TAB,
458      ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
459
460  // Restore the closed tab.
461  ASSERT_NO_FATAL_FAILURE(RestoreTab(0, tab_count - 1));
462
463  // And make sure the URLs match.
464  EXPECT_EQ(url1_,
465            browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
466  GoBack(browser());
467  EXPECT_EQ(http_url1,
468            browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
469
470  // Navigating to a new URL should clear the forward list, because the max
471  // page ID of the renderer should have been updated when we restored the tab.
472  ui_test_utils::NavigateToURLWithDisposition(
473      browser(), http_url2, CURRENT_TAB,
474      ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
475  EXPECT_FALSE(chrome::CanGoForward(browser()));
476  EXPECT_EQ(http_url2,
477            browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
478}
479
480IN_PROC_BROWSER_TEST_F(TabRestoreTest, RestoreWindow) {
481  // Create a new window.
482  size_t window_count = active_browser_list_->size();
483  ui_test_utils::NavigateToURLWithDisposition(
484      browser(), GURL(chrome::kChromeUINewTabURL), NEW_WINDOW,
485      ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
486  EXPECT_EQ(++window_count, active_browser_list_->size());
487
488  // Create two more tabs, one with url1, the other url2.
489  int initial_tab_count = browser()->tab_strip_model()->count();
490  ui_test_utils::NavigateToURLWithDisposition(
491      browser(), url1_, NEW_FOREGROUND_TAB,
492      ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
493  ui_test_utils::NavigateToURLWithDisposition(
494      browser(), url2_, NEW_FOREGROUND_TAB,
495      ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
496
497  // Close the window.
498  content::WindowedNotificationObserver close_window_observer(
499      chrome::NOTIFICATION_BROWSER_CLOSED,
500      content::NotificationService::AllSources());
501  chrome::CloseWindow(browser());
502  close_window_observer.Wait();
503  EXPECT_EQ(window_count - 1, active_browser_list_->size());
504
505  // Restore the window.
506  content::WindowedNotificationObserver open_window_observer(
507      chrome::NOTIFICATION_BROWSER_OPENED,
508      content::NotificationService::AllSources());
509  content::WindowedNotificationObserver load_stop_observer(
510      content::NOTIFICATION_LOAD_STOP,
511      content::NotificationService::AllSources());
512  chrome::RestoreTab(active_browser_list_->get(0));
513  open_window_observer.Wait();
514  EXPECT_EQ(window_count, active_browser_list_->size());
515
516  Browser* browser = GetBrowser(1);
517  EXPECT_EQ(initial_tab_count + 2, browser->tab_strip_model()->count());
518  load_stop_observer.Wait();
519
520  content::WebContents* restored_tab =
521      browser->tab_strip_model()->GetWebContentsAt(initial_tab_count);
522  EnsureTabFinishedRestoring(restored_tab);
523  EXPECT_EQ(url1_, restored_tab->GetURL());
524
525  restored_tab =
526      browser->tab_strip_model()->GetWebContentsAt(initial_tab_count + 1);
527  EnsureTabFinishedRestoring(restored_tab);
528  EXPECT_EQ(url2_, restored_tab->GetURL());
529}
530
531// Restore tab with special URL chrome://credits/ and make sure the page loads
532// properly after restore. See http://crbug.com/31905.
533IN_PROC_BROWSER_TEST_F(TabRestoreTest, RestoreTabWithSpecialURL) {
534  // Navigate new tab to a special URL.
535  ui_test_utils::NavigateToURLWithDisposition(
536      browser(), GURL(chrome::kChromeUICreditsURL), NEW_FOREGROUND_TAB,
537      ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
538
539  // Close the tab.
540  CloseTab(1);
541
542  // Restore the closed tab.
543  ASSERT_NO_FATAL_FAILURE(RestoreTab(0, 1));
544  content::WebContents* tab = browser()->tab_strip_model()->GetWebContentsAt(1);
545  EnsureTabFinishedRestoring(tab);
546
547  // See if content is as expected.
548  EXPECT_GT(
549      ui_test_utils::FindInPage(tab, base::ASCIIToUTF16("webkit"), true, false,
550                                NULL, NULL),
551      0);
552}
553
554// Restore tab with special URL in its navigation history, go back to that
555// entry and see that it loads properly. See http://crbug.com/31905
556IN_PROC_BROWSER_TEST_F(TabRestoreTest, RestoreTabWithSpecialURLOnBack) {
557  ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
558
559  const GURL http_url(embedded_test_server()->GetURL("/title1.html"));
560
561  // Navigate new tab to a special URL.
562  ui_test_utils::NavigateToURLWithDisposition(
563      browser(), GURL(chrome::kChromeUICreditsURL), NEW_FOREGROUND_TAB,
564      ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
565
566  // Then navigate to a normal URL.
567  ui_test_utils::NavigateToURL(browser(), http_url);
568
569  // Close the tab.
570  CloseTab(1);
571
572  // Restore the closed tab.
573  ASSERT_NO_FATAL_FAILURE(RestoreTab(0, 1));
574  content::WebContents* tab = browser()->tab_strip_model()->GetWebContentsAt(1);
575  EnsureTabFinishedRestoring(tab);
576  ASSERT_EQ(http_url, tab->GetURL());
577
578  // Go back, and see if content is as expected.
579  GoBack(browser());
580  EXPECT_GT(
581      ui_test_utils::FindInPage(tab, base::ASCIIToUTF16("webkit"), true, false,
582                                NULL, NULL),
583      0);
584}
585