blob: 6de80717553f9580956be06ec270ae84a5efbbe3 [file] [log] [blame]
[email protected]e18b9c272012-04-11 17:32:501// 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/file_path.h"
8#include "base/test/test_timeouts.h"
9#include "base/utf_string_conversions.h"
10#include "chrome/app/chrome_command_ids.h"
11#include "chrome/browser/ui/browser.h"
[email protected]a37d4b02012-06-25 21:56:1012#include "chrome/browser/ui/browser_commands.h"
[email protected]e18b9c272012-04-11 17:32:5013#include "chrome/browser/ui/browser_list.h"
[email protected]52877dbc62012-06-29 22:22:0314#include "chrome/browser/ui/browser_tabstrip.h"
[email protected]e18b9c272012-04-11 17:32:5015#include "chrome/browser/ui/find_bar/find_notification_details.h"
[email protected]871dc682012-06-11 19:35:3316#include "chrome/browser/ui/tab_contents/tab_contents.h"
[email protected]e18b9c272012-04-11 17:32:5017#include "chrome/common/chrome_notification_types.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 "googleurl/src/gurl.h"
30#include "net/base/net_util.h"
31#include "net/test/test_server.h"
32#include "third_party/WebKit/Source/WebKit/chromium/public/WebFindOptions.h"
33
34class TabRestoreTest : public InProcessBrowserTest {
35 public:
36 TabRestoreTest() : InProcessBrowserTest() {
37 url1_ = ui_test_utils::GetTestUrl(
38 FilePath().AppendASCII("session_history"),
39 FilePath().AppendASCII("bot1.html"));
40 url2_ = ui_test_utils::GetTestUrl(
41 FilePath().AppendASCII("session_history"),
42 FilePath().AppendASCII("bot2.html"));
43 }
44
45 protected:
46 Browser* GetBrowser(int index) {
47 CHECK(static_cast<int>(BrowserList::size()) > index);
48 return *(BrowserList::begin() + index);
49 }
50
51 // Adds tabs to the given browser, all navigated to url1_. Returns
52 // the final number of tabs.
53 int AddSomeTabs(Browser* browser, int how_many) {
54 int starting_tab_count = browser->tab_count();
55
56 for (int i = 0; i < how_many; ++i) {
57 ui_test_utils::NavigateToURLWithDisposition(
58 browser, url1_, NEW_FOREGROUND_TAB,
59 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
60 }
61 int tab_count = browser->tab_count();
62 EXPECT_EQ(starting_tab_count + how_many, tab_count);
63 return tab_count;
64 }
65
66 void CloseTab(int index) {
[email protected]52877dbc62012-06-29 22:22:0367 content::WebContents* new_tab = chrome::GetWebContentsAt(browser(), index);
[email protected]a7fe9112012-07-20 02:34:4568 content::WindowedNotificationObserver tab_close_observer(
69 content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
70 content::NotificationService::AllSources());
[email protected]52877dbc62012-06-29 22:22:0371 chrome::CloseWebContents(browser(), new_tab);
[email protected]e18b9c272012-04-11 17:32:5072 tab_close_observer.Wait();
73 }
74
75 // Uses the undo-close-tab accelerator to undo a close-tab or close-window
76 // operation. The newly restored tab is expected to appear in the
77 // window at index |expected_window_index|, at the |expected_tabstrip_index|,
78 // and to be active. If |expected_window_index| is equal to the number of
79 // current windows, the restored tab is expected to be created in a new
80 // window (since the index is 0-based).
81 void RestoreTab(int expected_window_index,
82 int expected_tabstrip_index) {
83 int window_count = static_cast<int>(BrowserList::size());
84 ASSERT_GT(window_count, 0);
85
86 bool expect_new_window = (expected_window_index == window_count);
87
88 Browser* browser;
89 if (expect_new_window) {
90 browser = *(BrowserList::begin());
91 } else {
92 browser = GetBrowser(expected_window_index);
93 }
94 int tab_count = browser->tab_count();
95 ASSERT_GT(tab_count, 0);
96
97 // Restore the tab.
[email protected]a7fe9112012-07-20 02:34:4598 content::WindowedNotificationObserver tab_added_observer(
99 chrome::NOTIFICATION_TAB_PARENTED,
100 content::NotificationService::AllSources());
101 content::WindowedNotificationObserver tab_loaded_observer(
102 content::NOTIFICATION_LOAD_STOP,
103 content::NotificationService::AllSources());
[email protected]855370052012-07-10 19:30:32104 chrome::RestoreTab(browser);
[email protected]e18b9c272012-04-11 17:32:50105 tab_added_observer.Wait();
106 tab_loaded_observer.Wait();
107
108 if (expect_new_window) {
109 int new_window_count = static_cast<int>(BrowserList::size());
110 EXPECT_EQ(++window_count, new_window_count);
111 browser = GetBrowser(expected_window_index);
112 } else {
113 EXPECT_EQ(++tab_count, browser->tab_count());
114 }
115
116 // Get a handle to the restored tab.
117 ASSERT_GT(browser->tab_count(), expected_tabstrip_index);
118
119 // Ensure that the tab and window are active.
120 EXPECT_EQ(expected_tabstrip_index, browser->active_index());
121 }
122
123 void GoBack(Browser* browser) {
[email protected]a7fe9112012-07-20 02:34:45124 content::WindowedNotificationObserver observer(
[email protected]e18b9c272012-04-11 17:32:50125 content::NOTIFICATION_LOAD_STOP,
126 content::NotificationService::AllSources());
[email protected]a37d4b02012-06-25 21:56:10127 chrome::GoBack(browser, CURRENT_TAB);
[email protected]e18b9c272012-04-11 17:32:50128 observer.Wait();
129 }
130
131 void EnsureTabFinishedRestoring(content::WebContents* tab) {
132 content::NavigationController* controller = &tab->GetController();
133 if (!controller->NeedsReload() && !controller->GetPendingEntry() &&
134 !controller->GetWebContents()->IsLoading())
135 return;
136
[email protected]a7fe9112012-07-20 02:34:45137 content::WindowedNotificationObserver observer(
[email protected]e18b9c272012-04-11 17:32:50138 content::NOTIFICATION_LOAD_STOP,
139 content::Source<content::NavigationController>(controller));
140 observer.Wait();
141 }
142
143 GURL url1_;
144 GURL url2_;
145
146 private:
147 DISALLOW_COPY_AND_ASSIGN(TabRestoreTest);
148};
149
150// Close the end tab in the current window, then restore it. The tab should be
151// in its original position, and active.
152IN_PROC_BROWSER_TEST_F(TabRestoreTest, Basic) {
153 int starting_tab_count = browser()->tab_count();
154 int tab_count = AddSomeTabs(browser(), 1);
155
156 int closed_tab_index = tab_count - 1;
157 CloseTab(closed_tab_index);
158 EXPECT_EQ(starting_tab_count, browser()->tab_count());
159
160 ASSERT_NO_FATAL_FAILURE(RestoreTab(0, closed_tab_index));
161
162 // And make sure everything looks right.
163 EXPECT_EQ(starting_tab_count + 1, browser()->tab_count());
164 EXPECT_EQ(closed_tab_index, browser()->active_index());
[email protected]52877dbc62012-06-29 22:22:03165 EXPECT_EQ(url1_, chrome::GetActiveWebContents(browser())->GetURL());
[email protected]e18b9c272012-04-11 17:32:50166}
167
168// Close a tab not at the end of the current window, then restore it. The tab
169// should be in its original position, and active.
170IN_PROC_BROWSER_TEST_F(TabRestoreTest, MiddleTab) {
171 int starting_tab_count = browser()->tab_count();
172 AddSomeTabs(browser(), 3);
173
174 // Close one in the middle
175 int closed_tab_index = starting_tab_count + 1;
176 CloseTab(closed_tab_index);
177 EXPECT_EQ(starting_tab_count + 2, browser()->tab_count());
178
179 ASSERT_NO_FATAL_FAILURE(RestoreTab(0, closed_tab_index));
180
181 // And make sure everything looks right.
182 EXPECT_EQ(starting_tab_count + 3, browser()->tab_count());
183 EXPECT_EQ(closed_tab_index, browser()->active_index());
[email protected]52877dbc62012-06-29 22:22:03184 EXPECT_EQ(url1_, chrome::GetActiveWebContents(browser())->GetURL());
[email protected]e18b9c272012-04-11 17:32:50185}
186
187// Close a tab, switch windows, then restore the tab. The tab should be in its
188// original window and position, and active.
189IN_PROC_BROWSER_TEST_F(TabRestoreTest, RestoreToDifferentWindow) {
190 int starting_tab_count = browser()->tab_count();
191 AddSomeTabs(browser(), 3);
192
193 // Close one in the middle
194 int closed_tab_index = starting_tab_count + 1;
195 CloseTab(closed_tab_index);
196 EXPECT_EQ(starting_tab_count + 2, browser()->tab_count());
197
198 // Create a new browser.
199 ui_test_utils::NavigateToURLWithDisposition(
200 browser(), GURL(chrome::kChromeUINewTabURL), NEW_WINDOW,
201 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
202 EXPECT_EQ(2u, BrowserList::size());
203
204 // Restore tab into original browser.
205 ASSERT_NO_FATAL_FAILURE(RestoreTab(0, closed_tab_index));
206
207 // And make sure everything looks right.
208 EXPECT_EQ(starting_tab_count + 3, browser()->tab_count());
209 EXPECT_EQ(closed_tab_index, browser()->active_index());
[email protected]52877dbc62012-06-29 22:22:03210 EXPECT_EQ(url1_, chrome::GetActiveWebContents(browser())->GetURL());
[email protected]e18b9c272012-04-11 17:32:50211}
212
213// Close a tab, open a new window, close the first window, then restore the
214// tab. It should be in a new window.
215// If this becomes flaky, use http://crbug.com/14774
[email protected]ef355f082012-04-19 18:36:30216IN_PROC_BROWSER_TEST_F(TabRestoreTest, FLAKY_BasicRestoreFromClosedWindow) {
[email protected]e18b9c272012-04-11 17:32:50217 // Navigate to url1 then url2.
218 ui_test_utils::NavigateToURL(browser(), url1_);
219 ui_test_utils::NavigateToURL(browser(), url2_);
220
221 // Create a new browser.
222 ui_test_utils::NavigateToURLWithDisposition(
223 browser(), GURL(chrome::kChromeUINewTabURL), NEW_WINDOW,
224 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
225 EXPECT_EQ(2u, BrowserList::size());
226
227 // Close the final tab in the first browser.
[email protected]a7fe9112012-07-20 02:34:45228 content::WindowedNotificationObserver window_observer(
[email protected]e18b9c272012-04-11 17:32:50229 chrome::NOTIFICATION_BROWSER_CLOSED,
230 content::NotificationService::AllSources());
231 CloseTab(0);
232 window_observer.Wait();
233
234 ASSERT_NO_FATAL_FAILURE(RestoreTab(1, 0));
235
236 // Tab should be in a new window.
237 Browser* browser = GetBrowser(1);
[email protected]52877dbc62012-06-29 22:22:03238 content::WebContents* web_contents = chrome::GetActiveWebContents(browser);
[email protected]e18b9c272012-04-11 17:32:50239 // And make sure the URLs matches.
240 EXPECT_EQ(url2_, web_contents->GetURL());
241 GoBack(browser);
242 EXPECT_EQ(url1_, web_contents->GetURL());
243}
244
245// Restore a tab then make sure it doesn't restore again.
246IN_PROC_BROWSER_TEST_F(TabRestoreTest, DontLoadRestoredTab) {
247 // Add two tabs
248 int starting_tab_count = browser()->tab_count();
249 AddSomeTabs(browser(), 2);
250 ASSERT_EQ(browser()->tab_count(), starting_tab_count + 2);
251
252 // Close one of them.
253 CloseTab(0);
254 ASSERT_EQ(browser()->tab_count(), starting_tab_count + 1);
255
256 // Restore it.
257 ASSERT_NO_FATAL_FAILURE(RestoreTab(0, 0));
258 ASSERT_EQ(browser()->tab_count(), starting_tab_count + 2);
259
260 // Make sure that there's nothing else to restore.
[email protected]5d98294912012-06-27 22:57:40261 ASSERT_FALSE(chrome::CanRestoreTab(browser()));
[email protected]e18b9c272012-04-11 17:32:50262}
263
264// Open a window with multiple tabs, close a tab, then close the window.
265// Restore both and make sure the tab goes back into the window.
266IN_PROC_BROWSER_TEST_F(TabRestoreTest, RestoreWindowAndTab) {
267 int starting_tab_count = browser()->tab_count();
268 AddSomeTabs(browser(), 3);
269
270 // Close one in the middle
271 int closed_tab_index = starting_tab_count + 1;
272 CloseTab(closed_tab_index);
273 EXPECT_EQ(starting_tab_count + 2, browser()->tab_count());
274
275 // Create a new browser.
276 ui_test_utils::NavigateToURLWithDisposition(
277 browser(), GURL(chrome::kChromeUINewTabURL), NEW_WINDOW,
278 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
279 EXPECT_EQ(2u, BrowserList::size());
280
281 // Close the first browser.
[email protected]a7fe9112012-07-20 02:34:45282 content::WindowedNotificationObserver observer(
[email protected]e18b9c272012-04-11 17:32:50283 chrome::NOTIFICATION_BROWSER_CLOSED,
284 content::NotificationService::AllSources());
[email protected]a37d4b02012-06-25 21:56:10285 chrome::CloseWindow(browser());
[email protected]e18b9c272012-04-11 17:32:50286 observer.Wait();
287 EXPECT_EQ(1u, BrowserList::size());
288
289 // Restore the first window. The expected_tabstrip_index (second argument)
290 // indicates the expected active tab.
291 ASSERT_NO_FATAL_FAILURE(RestoreTab(1, starting_tab_count + 1));
292 Browser* browser = GetBrowser(1);
293 EXPECT_EQ(starting_tab_count + 2, browser->tab_count());
294
295 // Restore the closed tab.
296 ASSERT_NO_FATAL_FAILURE(RestoreTab(1, closed_tab_index));
297 EXPECT_EQ(starting_tab_count + 3, browser->tab_count());
[email protected]52877dbc62012-06-29 22:22:03298 EXPECT_EQ(url1_, chrome::GetActiveWebContents(browser)->GetURL());
[email protected]e18b9c272012-04-11 17:32:50299}
300
301// Open a window with two tabs, close both (closing the window), then restore
302// both. Make sure both restored tabs are in the same window.
303IN_PROC_BROWSER_TEST_F(TabRestoreTest, RestoreIntoSameWindow) {
304 ui_test_utils::NavigateToURLWithDisposition(
305 browser(), url1_, NEW_FOREGROUND_TAB,
306 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
307 // Navigate the rightmost one to url2_ for easier identification.
308 ui_test_utils::NavigateToURLWithDisposition(
309 browser(), url2_, NEW_FOREGROUND_TAB,
310 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
311
312 // Create a new browser.
313 ui_test_utils::NavigateToURLWithDisposition(
314 browser(), GURL(chrome::kChromeUINewTabURL), NEW_WINDOW,
315 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
316 EXPECT_EQ(2u, BrowserList::size());
317
318 // Close all but one tab in the first browser, left to right.
319 while (browser()->tab_count() > 1)
320 CloseTab(0);
321
322 // Close the last tab, closing the browser.
[email protected]a7fe9112012-07-20 02:34:45323 content::WindowedNotificationObserver observer(
[email protected]e18b9c272012-04-11 17:32:50324 chrome::NOTIFICATION_BROWSER_CLOSED,
325 content::NotificationService::AllSources());
326 CloseTab(0);
327 observer.Wait();
328 EXPECT_EQ(1u, BrowserList::size());
329
330 // Restore the last-closed tab into a new window.
331 ASSERT_NO_FATAL_FAILURE(RestoreTab(1, 0));
332 Browser* browser = GetBrowser(1);
333 EXPECT_EQ(1, browser->tab_count());
[email protected]52877dbc62012-06-29 22:22:03334 EXPECT_EQ(url2_, chrome::GetActiveWebContents(browser)->GetURL());
[email protected]e18b9c272012-04-11 17:32:50335
336 // Restore the next-to-last-closed tab into the same window.
337 ASSERT_NO_FATAL_FAILURE(RestoreTab(1, 0));
338 EXPECT_EQ(2, browser->tab_count());
[email protected]52877dbc62012-06-29 22:22:03339 EXPECT_EQ(url1_, chrome::GetActiveWebContents(browser)->GetURL());
[email protected]e18b9c272012-04-11 17:32:50340}
341
342// Tests that a duplicate history entry is not created when we restore a page
343// to an existing SiteInstance. (Bug 1230446)
344IN_PROC_BROWSER_TEST_F(TabRestoreTest, RestoreWithExistingSiteInstance) {
345 ASSERT_TRUE(test_server()->Start());
346
347 GURL http_url1(test_server()->GetURL("files/title1.html"));
348 GURL http_url2(test_server()->GetURL("files/title2.html"));
349 int tab_count = browser()->tab_count();
350
351 // Add a tab
352 ui_test_utils::NavigateToURLWithDisposition(
353 browser(), http_url1, NEW_FOREGROUND_TAB,
354 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
355 EXPECT_EQ(++tab_count, browser()->tab_count());
356
357 // Navigate to another same-site URL.
[email protected]52877dbc62012-06-29 22:22:03358 content::WebContents* tab =
359 chrome::GetWebContentsAt(browser(), tab_count - 1);
[email protected]a7fe9112012-07-20 02:34:45360 content::WindowedNotificationObserver observer(
[email protected]e18b9c272012-04-11 17:32:50361 content::NOTIFICATION_LOAD_STOP,
362 content::NotificationService::AllSources());
363 static_cast<content::WebContentsDelegate*>(browser())->OpenURLFromTab(
364 tab,
365 content::OpenURLParams(http_url2, content::Referrer(), CURRENT_TAB,
366 content::PAGE_TRANSITION_TYPED, false));
367 observer.Wait();
368
369 // Close the tab.
370 CloseTab(1);
371
372 // Create a new tab to the original site. Assuming process-per-site is
373 // enabled, this will ensure that the SiteInstance used by the restored tab
374 // will already exist when the restore happens.
375 ui_test_utils::NavigateToURLWithDisposition(
376 browser(), http_url2, NEW_FOREGROUND_TAB,
377 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
378
379 // Restore the closed tab.
380 ASSERT_NO_FATAL_FAILURE(RestoreTab(0, tab_count - 1));
381
382 // And make sure the URLs match.
[email protected]52877dbc62012-06-29 22:22:03383 EXPECT_EQ(http_url2, chrome::GetActiveWebContents(browser())->GetURL());
[email protected]e18b9c272012-04-11 17:32:50384 GoBack(browser());
[email protected]52877dbc62012-06-29 22:22:03385 EXPECT_EQ(http_url1, chrome::GetActiveWebContents(browser())->GetURL());
[email protected]e18b9c272012-04-11 17:32:50386}
387
388// Tests that the SiteInstances used for entries in a restored tab's history
389// are given appropriate max page IDs, even if the renderer for the entry
390// already exists. (Bug 1204135)
391IN_PROC_BROWSER_TEST_F(TabRestoreTest,
392 RestoreCrossSiteWithExistingSiteInstance) {
393 ASSERT_TRUE(test_server()->Start());
394
395 GURL http_url1(test_server()->GetURL("files/title1.html"));
396 GURL http_url2(test_server()->GetURL("files/title2.html"));
397
398 int tab_count = browser()->tab_count();
399
400 // Add a tab
401 ui_test_utils::NavigateToURLWithDisposition(
402 browser(), http_url1, NEW_FOREGROUND_TAB,
403 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
404 EXPECT_EQ(++tab_count, browser()->tab_count());
405
406 // Navigate to more URLs, then a cross-site URL.
407 ui_test_utils::NavigateToURLWithDisposition(
408 browser(), http_url2, CURRENT_TAB,
409 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
410 ui_test_utils::NavigateToURLWithDisposition(
411 browser(), http_url1, CURRENT_TAB,
412 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
413 ui_test_utils::NavigateToURLWithDisposition(
414 browser(), url1_, CURRENT_TAB,
415 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
416
417 // Close the tab.
418 CloseTab(1);
419
420 // Create a new tab to the original site. Assuming process-per-site is
421 // enabled, this will ensure that the SiteInstance will already exist when
422 // the user clicks Back in the restored tab.
423 ui_test_utils::NavigateToURLWithDisposition(
424 browser(), http_url2, NEW_FOREGROUND_TAB,
425 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
426
427 // Restore the closed tab.
428 ASSERT_NO_FATAL_FAILURE(RestoreTab(0, tab_count - 1));
429
430 // And make sure the URLs match.
[email protected]52877dbc62012-06-29 22:22:03431 EXPECT_EQ(url1_, chrome::GetActiveWebContents(browser())->GetURL());
[email protected]e18b9c272012-04-11 17:32:50432 GoBack(browser());
[email protected]52877dbc62012-06-29 22:22:03433 EXPECT_EQ(http_url1, chrome::GetActiveWebContents(browser())->GetURL());
[email protected]e18b9c272012-04-11 17:32:50434
435 // Navigating to a new URL should clear the forward list, because the max
436 // page ID of the renderer should have been updated when we restored the tab.
437 ui_test_utils::NavigateToURLWithDisposition(
438 browser(), http_url2, CURRENT_TAB,
439 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
[email protected]a37d4b02012-06-25 21:56:10440 EXPECT_FALSE(chrome::CanGoForward(browser()));
[email protected]52877dbc62012-06-29 22:22:03441 EXPECT_EQ(http_url2, chrome::GetActiveWebContents(browser())->GetURL());
[email protected]e18b9c272012-04-11 17:32:50442}
443
444IN_PROC_BROWSER_TEST_F(TabRestoreTest, RestoreWindow) {
445 // Create a new window.
446 size_t window_count = BrowserList::size();
447 ui_test_utils::NavigateToURLWithDisposition(
448 browser(), GURL(chrome::kChromeUINewTabURL), NEW_WINDOW,
449 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
450 EXPECT_EQ(++window_count, BrowserList::size());
451
452 // Create two more tabs, one with url1, the other url2.
453 int initial_tab_count = browser()->tab_count();
454 ui_test_utils::NavigateToURLWithDisposition(
455 browser(), url1_, NEW_FOREGROUND_TAB,
456 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
457 ui_test_utils::NavigateToURLWithDisposition(
458 browser(), url2_, NEW_FOREGROUND_TAB,
459 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
460
461 // Close the window.
[email protected]a7fe9112012-07-20 02:34:45462 content::WindowedNotificationObserver close_window_observer(
[email protected]e18b9c272012-04-11 17:32:50463 chrome::NOTIFICATION_BROWSER_CLOSED,
464 content::NotificationService::AllSources());
[email protected]a37d4b02012-06-25 21:56:10465 chrome::CloseWindow(browser());
[email protected]e18b9c272012-04-11 17:32:50466 close_window_observer.Wait();
467 EXPECT_EQ(window_count - 1, BrowserList::size());
468
469 // Restore the window.
[email protected]a7fe9112012-07-20 02:34:45470 content::WindowedNotificationObserver open_window_observer(
[email protected]e18b9c272012-04-11 17:32:50471 chrome::NOTIFICATION_BROWSER_OPENED,
472 content::NotificationService::AllSources());
[email protected]a7fe9112012-07-20 02:34:45473 content::WindowedNotificationObserver load_stop_observer(
[email protected]e18b9c272012-04-11 17:32:50474 content::NOTIFICATION_LOAD_STOP,
475 content::NotificationService::AllSources());
[email protected]855370052012-07-10 19:30:32476 chrome::RestoreTab(*BrowserList::begin());
[email protected]e18b9c272012-04-11 17:32:50477 open_window_observer.Wait();
478 EXPECT_EQ(window_count, BrowserList::size());
479
480 Browser* browser = GetBrowser(1);
481 EXPECT_EQ(initial_tab_count + 2, browser->tab_count());
482 load_stop_observer.Wait();
483
484 content::WebContents* restored_tab =
[email protected]52877dbc62012-06-29 22:22:03485 chrome::GetWebContentsAt(browser, initial_tab_count);
[email protected]e18b9c272012-04-11 17:32:50486 EnsureTabFinishedRestoring(restored_tab);
487 EXPECT_EQ(url1_, restored_tab->GetURL());
488
[email protected]52877dbc62012-06-29 22:22:03489 restored_tab = chrome::GetWebContentsAt(browser, initial_tab_count + 1);
[email protected]e18b9c272012-04-11 17:32:50490 EnsureTabFinishedRestoring(restored_tab);
491 EXPECT_EQ(url2_, restored_tab->GetURL());
492}
493
494// Restore tab with special URL chrome://credits/ and make sure the page loads
495// properly after restore. See http://crbug.com/31905.
496IN_PROC_BROWSER_TEST_F(TabRestoreTest, RestoreTabWithSpecialURL) {
497 // Navigate new tab to a special URL.
498 ui_test_utils::NavigateToURLWithDisposition(
499 browser(), GURL(chrome::kChromeUICreditsURL), NEW_FOREGROUND_TAB,
500 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
501
502 // Close the tab.
503 CloseTab(1);
504
505 // Restore the closed tab.
506 ASSERT_NO_FATAL_FAILURE(RestoreTab(0, 1));
[email protected]ba3d09a12012-11-07 12:13:52507 content::WebContents* tab = chrome::GetWebContentsAt(browser(), 1);
508 EnsureTabFinishedRestoring(tab);
[email protected]e18b9c272012-04-11 17:32:50509
510 // See if content is as expected.
511 EXPECT_GT(
[email protected]864ebb52012-08-17 17:53:02512 ui_test_utils::FindInPage(tab, ASCIIToUTF16("webkit"), true, false, NULL,
513 NULL),
[email protected]e18b9c272012-04-11 17:32:50514 0);
515}
516
517// Restore tab with special URL in its navigation history, go back to that
518// entry and see that it loads properly. See http://crbug.com/31905
519IN_PROC_BROWSER_TEST_F(TabRestoreTest, RestoreTabWithSpecialURLOnBack) {
520 ASSERT_TRUE(test_server()->Start());
521
522 const GURL http_url(test_server()->GetURL("files/title1.html"));
523
524 // Navigate new tab to a special URL.
525 ui_test_utils::NavigateToURLWithDisposition(
526 browser(), GURL(chrome::kChromeUICreditsURL), NEW_FOREGROUND_TAB,
527 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
528
529 // Then navigate to a normal URL.
530 ui_test_utils::NavigateToURL(browser(), http_url);
531
532 // Close the tab.
533 CloseTab(1);
534
535 // Restore the closed tab.
536 ASSERT_NO_FATAL_FAILURE(RestoreTab(0, 1));
[email protected]ba3d09a12012-11-07 12:13:52537 content::WebContents* tab = chrome::GetWebContentsAt(browser(), 1);
538 EnsureTabFinishedRestoring(tab);
539 ASSERT_EQ(http_url, tab->GetURL());
[email protected]e18b9c272012-04-11 17:32:50540
541 // Go back, and see if content is as expected.
542 GoBack(browser());
543 EXPECT_GT(
[email protected]864ebb52012-08-17 17:53:02544 ui_test_utils::FindInPage(tab, ASCIIToUTF16("webkit"), true, false, NULL,
545 NULL),
[email protected]e18b9c272012-04-11 17:32:50546 0);
547}