Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(331)

Side by Side Diff: chrome/browser/tab_restore_browsertest.cc

Issue 10807047: Create content\public\test\test_utils.h to hold common test classes that are used by unit and brows… (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/command_line.h" 6 #include "base/command_line.h"
7 #include "base/file_path.h" 7 #include "base/file_path.h"
8 #include "base/test/test_timeouts.h" 8 #include "base/test/test_timeouts.h"
9 #include "base/utf_string_conversions.h" 9 #include "base/utf_string_conversions.h"
10 #include "chrome/app/chrome_command_ids.h" 10 #include "chrome/app/chrome_command_ids.h"
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 browser, url1_, NEW_FOREGROUND_TAB, 58 browser, url1_, NEW_FOREGROUND_TAB,
59 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); 59 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
60 } 60 }
61 int tab_count = browser->tab_count(); 61 int tab_count = browser->tab_count();
62 EXPECT_EQ(starting_tab_count + how_many, tab_count); 62 EXPECT_EQ(starting_tab_count + how_many, tab_count);
63 return tab_count; 63 return tab_count;
64 } 64 }
65 65
66 void CloseTab(int index) { 66 void CloseTab(int index) {
67 content::WebContents* new_tab = chrome::GetWebContentsAt(browser(), index); 67 content::WebContents* new_tab = chrome::GetWebContentsAt(browser(), index);
68 ui_test_utils::WindowedNotificationObserver tab_close_observer( 68 content::WindowedNotificationObserver tab_close_observer(
69 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, 69 content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
70 content::NotificationService::AllSources()); 70 content::NotificationService::AllSources());
71 chrome::CloseWebContents(browser(), new_tab); 71 chrome::CloseWebContents(browser(), new_tab);
72 tab_close_observer.Wait(); 72 tab_close_observer.Wait();
73 } 73 }
74 74
75 // Uses the undo-close-tab accelerator to undo a close-tab or close-window 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 76 // operation. The newly restored tab is expected to appear in the
77 // window at index |expected_window_index|, at the |expected_tabstrip_index|, 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 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 79 // current windows, the restored tab is expected to be created in a new
80 // window (since the index is 0-based). 80 // window (since the index is 0-based).
81 void RestoreTab(int expected_window_index, 81 void RestoreTab(int expected_window_index,
82 int expected_tabstrip_index) { 82 int expected_tabstrip_index) {
83 int window_count = static_cast<int>(BrowserList::size()); 83 int window_count = static_cast<int>(BrowserList::size());
84 ASSERT_GT(window_count, 0); 84 ASSERT_GT(window_count, 0);
85 85
86 bool expect_new_window = (expected_window_index == window_count); 86 bool expect_new_window = (expected_window_index == window_count);
87 87
88 Browser* browser; 88 Browser* browser;
89 if (expect_new_window) { 89 if (expect_new_window) {
90 browser = *(BrowserList::begin()); 90 browser = *(BrowserList::begin());
91 } else { 91 } else {
92 browser = GetBrowser(expected_window_index); 92 browser = GetBrowser(expected_window_index);
93 } 93 }
94 int tab_count = browser->tab_count(); 94 int tab_count = browser->tab_count();
95 ASSERT_GT(tab_count, 0); 95 ASSERT_GT(tab_count, 0);
96 96
97 // Restore the tab. 97 // Restore the tab.
98 ui_test_utils::WindowedNotificationObserver tab_added_observer( 98 content::WindowedNotificationObserver tab_added_observer(
99 chrome::NOTIFICATION_TAB_PARENTED, 99 chrome::NOTIFICATION_TAB_PARENTED,
100 content::NotificationService::AllSources()); 100 content::NotificationService::AllSources());
101 ui_test_utils::WindowedNotificationObserver tab_loaded_observer( 101 content::WindowedNotificationObserver tab_loaded_observer(
102 content::NOTIFICATION_LOAD_STOP, 102 content::NOTIFICATION_LOAD_STOP,
103 content::NotificationService::AllSources()); 103 content::NotificationService::AllSources());
104 chrome::RestoreTab(browser); 104 chrome::RestoreTab(browser);
105 tab_added_observer.Wait(); 105 tab_added_observer.Wait();
106 tab_loaded_observer.Wait(); 106 tab_loaded_observer.Wait();
107 107
108 if (expect_new_window) { 108 if (expect_new_window) {
109 int new_window_count = static_cast<int>(BrowserList::size()); 109 int new_window_count = static_cast<int>(BrowserList::size());
110 EXPECT_EQ(++window_count, new_window_count); 110 EXPECT_EQ(++window_count, new_window_count);
111 browser = GetBrowser(expected_window_index); 111 browser = GetBrowser(expected_window_index);
112 } else { 112 } else {
113 EXPECT_EQ(++tab_count, browser->tab_count()); 113 EXPECT_EQ(++tab_count, browser->tab_count());
114 } 114 }
115 115
116 // Get a handle to the restored tab. 116 // Get a handle to the restored tab.
117 ASSERT_GT(browser->tab_count(), expected_tabstrip_index); 117 ASSERT_GT(browser->tab_count(), expected_tabstrip_index);
118 118
119 // Ensure that the tab and window are active. 119 // Ensure that the tab and window are active.
120 EXPECT_EQ(expected_tabstrip_index, browser->active_index()); 120 EXPECT_EQ(expected_tabstrip_index, browser->active_index());
121 } 121 }
122 122
123 void GoBack(Browser* browser) { 123 void GoBack(Browser* browser) {
124 ui_test_utils::WindowedNotificationObserver observer( 124 content::WindowedNotificationObserver observer(
125 content::NOTIFICATION_LOAD_STOP, 125 content::NOTIFICATION_LOAD_STOP,
126 content::NotificationService::AllSources()); 126 content::NotificationService::AllSources());
127 chrome::GoBack(browser, CURRENT_TAB); 127 chrome::GoBack(browser, CURRENT_TAB);
128 observer.Wait(); 128 observer.Wait();
129 } 129 }
130 130
131 void EnsureTabFinishedRestoring(content::WebContents* tab) { 131 void EnsureTabFinishedRestoring(content::WebContents* tab) {
132 content::NavigationController* controller = &tab->GetController(); 132 content::NavigationController* controller = &tab->GetController();
133 if (!controller->NeedsReload() && !controller->GetPendingEntry() && 133 if (!controller->NeedsReload() && !controller->GetPendingEntry() &&
134 !controller->GetWebContents()->IsLoading()) 134 !controller->GetWebContents()->IsLoading())
135 return; 135 return;
136 136
137 ui_test_utils::WindowedNotificationObserver observer( 137 content::WindowedNotificationObserver observer(
138 content::NOTIFICATION_LOAD_STOP, 138 content::NOTIFICATION_LOAD_STOP,
139 content::Source<content::NavigationController>(controller)); 139 content::Source<content::NavigationController>(controller));
140 observer.Wait(); 140 observer.Wait();
141 } 141 }
142 142
143 GURL url1_; 143 GURL url1_;
144 GURL url2_; 144 GURL url2_;
145 145
146 private: 146 private:
147 DISALLOW_COPY_AND_ASSIGN(TabRestoreTest); 147 DISALLOW_COPY_AND_ASSIGN(TabRestoreTest);
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 ui_test_utils::NavigateToURL(browser(), url1_); 218 ui_test_utils::NavigateToURL(browser(), url1_);
219 ui_test_utils::NavigateToURL(browser(), url2_); 219 ui_test_utils::NavigateToURL(browser(), url2_);
220 220
221 // Create a new browser. 221 // Create a new browser.
222 ui_test_utils::NavigateToURLWithDisposition( 222 ui_test_utils::NavigateToURLWithDisposition(
223 browser(), GURL(chrome::kChromeUINewTabURL), NEW_WINDOW, 223 browser(), GURL(chrome::kChromeUINewTabURL), NEW_WINDOW,
224 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER); 224 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
225 EXPECT_EQ(2u, BrowserList::size()); 225 EXPECT_EQ(2u, BrowserList::size());
226 226
227 // Close the final tab in the first browser. 227 // Close the final tab in the first browser.
228 ui_test_utils::WindowedNotificationObserver window_observer( 228 content::WindowedNotificationObserver window_observer(
229 chrome::NOTIFICATION_BROWSER_CLOSED, 229 chrome::NOTIFICATION_BROWSER_CLOSED,
230 content::NotificationService::AllSources()); 230 content::NotificationService::AllSources());
231 CloseTab(0); 231 CloseTab(0);
232 window_observer.Wait(); 232 window_observer.Wait();
233 233
234 ASSERT_NO_FATAL_FAILURE(RestoreTab(1, 0)); 234 ASSERT_NO_FATAL_FAILURE(RestoreTab(1, 0));
235 235
236 // Tab should be in a new window. 236 // Tab should be in a new window.
237 Browser* browser = GetBrowser(1); 237 Browser* browser = GetBrowser(1);
238 content::WebContents* web_contents = chrome::GetActiveWebContents(browser); 238 content::WebContents* web_contents = chrome::GetActiveWebContents(browser);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 CloseTab(closed_tab_index); 272 CloseTab(closed_tab_index);
273 EXPECT_EQ(starting_tab_count + 2, browser()->tab_count()); 273 EXPECT_EQ(starting_tab_count + 2, browser()->tab_count());
274 274
275 // Create a new browser. 275 // Create a new browser.
276 ui_test_utils::NavigateToURLWithDisposition( 276 ui_test_utils::NavigateToURLWithDisposition(
277 browser(), GURL(chrome::kChromeUINewTabURL), NEW_WINDOW, 277 browser(), GURL(chrome::kChromeUINewTabURL), NEW_WINDOW,
278 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER); 278 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
279 EXPECT_EQ(2u, BrowserList::size()); 279 EXPECT_EQ(2u, BrowserList::size());
280 280
281 // Close the first browser. 281 // Close the first browser.
282 ui_test_utils::WindowedNotificationObserver observer( 282 content::WindowedNotificationObserver observer(
283 chrome::NOTIFICATION_BROWSER_CLOSED, 283 chrome::NOTIFICATION_BROWSER_CLOSED,
284 content::NotificationService::AllSources()); 284 content::NotificationService::AllSources());
285 chrome::CloseWindow(browser()); 285 chrome::CloseWindow(browser());
286 observer.Wait(); 286 observer.Wait();
287 EXPECT_EQ(1u, BrowserList::size()); 287 EXPECT_EQ(1u, BrowserList::size());
288 288
289 // Restore the first window. The expected_tabstrip_index (second argument) 289 // Restore the first window. The expected_tabstrip_index (second argument)
290 // indicates the expected active tab. 290 // indicates the expected active tab.
291 ASSERT_NO_FATAL_FAILURE(RestoreTab(1, starting_tab_count + 1)); 291 ASSERT_NO_FATAL_FAILURE(RestoreTab(1, starting_tab_count + 1));
292 Browser* browser = GetBrowser(1); 292 Browser* browser = GetBrowser(1);
(...skipping 20 matching lines...) Expand all
313 ui_test_utils::NavigateToURLWithDisposition( 313 ui_test_utils::NavigateToURLWithDisposition(
314 browser(), GURL(chrome::kChromeUINewTabURL), NEW_WINDOW, 314 browser(), GURL(chrome::kChromeUINewTabURL), NEW_WINDOW,
315 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER); 315 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
316 EXPECT_EQ(2u, BrowserList::size()); 316 EXPECT_EQ(2u, BrowserList::size());
317 317
318 // Close all but one tab in the first browser, left to right. 318 // Close all but one tab in the first browser, left to right.
319 while (browser()->tab_count() > 1) 319 while (browser()->tab_count() > 1)
320 CloseTab(0); 320 CloseTab(0);
321 321
322 // Close the last tab, closing the browser. 322 // Close the last tab, closing the browser.
323 ui_test_utils::WindowedNotificationObserver observer( 323 content::WindowedNotificationObserver observer(
324 chrome::NOTIFICATION_BROWSER_CLOSED, 324 chrome::NOTIFICATION_BROWSER_CLOSED,
325 content::NotificationService::AllSources()); 325 content::NotificationService::AllSources());
326 CloseTab(0); 326 CloseTab(0);
327 observer.Wait(); 327 observer.Wait();
328 EXPECT_EQ(1u, BrowserList::size()); 328 EXPECT_EQ(1u, BrowserList::size());
329 329
330 // Restore the last-closed tab into a new window. 330 // Restore the last-closed tab into a new window.
331 ASSERT_NO_FATAL_FAILURE(RestoreTab(1, 0)); 331 ASSERT_NO_FATAL_FAILURE(RestoreTab(1, 0));
332 Browser* browser = GetBrowser(1); 332 Browser* browser = GetBrowser(1);
333 EXPECT_EQ(1, browser->tab_count()); 333 EXPECT_EQ(1, browser->tab_count());
(...skipping 16 matching lines...) Expand all
350 350
351 // Add a tab 351 // Add a tab
352 ui_test_utils::NavigateToURLWithDisposition( 352 ui_test_utils::NavigateToURLWithDisposition(
353 browser(), http_url1, NEW_FOREGROUND_TAB, 353 browser(), http_url1, NEW_FOREGROUND_TAB,
354 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); 354 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
355 EXPECT_EQ(++tab_count, browser()->tab_count()); 355 EXPECT_EQ(++tab_count, browser()->tab_count());
356 356
357 // Navigate to another same-site URL. 357 // Navigate to another same-site URL.
358 content::WebContents* tab = 358 content::WebContents* tab =
359 chrome::GetWebContentsAt(browser(), tab_count - 1); 359 chrome::GetWebContentsAt(browser(), tab_count - 1);
360 ui_test_utils::WindowedNotificationObserver observer( 360 content::WindowedNotificationObserver observer(
361 content::NOTIFICATION_LOAD_STOP, 361 content::NOTIFICATION_LOAD_STOP,
362 content::NotificationService::AllSources()); 362 content::NotificationService::AllSources());
363 static_cast<content::WebContentsDelegate*>(browser())->OpenURLFromTab( 363 static_cast<content::WebContentsDelegate*>(browser())->OpenURLFromTab(
364 tab, 364 tab,
365 content::OpenURLParams(http_url2, content::Referrer(), CURRENT_TAB, 365 content::OpenURLParams(http_url2, content::Referrer(), CURRENT_TAB,
366 content::PAGE_TRANSITION_TYPED, false)); 366 content::PAGE_TRANSITION_TYPED, false));
367 observer.Wait(); 367 observer.Wait();
368 368
369 // Close the tab. 369 // Close the tab.
370 CloseTab(1); 370 CloseTab(1);
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
452 // Create two more tabs, one with url1, the other url2. 452 // Create two more tabs, one with url1, the other url2.
453 int initial_tab_count = browser()->tab_count(); 453 int initial_tab_count = browser()->tab_count();
454 ui_test_utils::NavigateToURLWithDisposition( 454 ui_test_utils::NavigateToURLWithDisposition(
455 browser(), url1_, NEW_FOREGROUND_TAB, 455 browser(), url1_, NEW_FOREGROUND_TAB,
456 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); 456 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
457 ui_test_utils::NavigateToURLWithDisposition( 457 ui_test_utils::NavigateToURLWithDisposition(
458 browser(), url2_, NEW_FOREGROUND_TAB, 458 browser(), url2_, NEW_FOREGROUND_TAB,
459 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); 459 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
460 460
461 // Close the window. 461 // Close the window.
462 ui_test_utils::WindowedNotificationObserver close_window_observer( 462 content::WindowedNotificationObserver close_window_observer(
463 chrome::NOTIFICATION_BROWSER_CLOSED, 463 chrome::NOTIFICATION_BROWSER_CLOSED,
464 content::NotificationService::AllSources()); 464 content::NotificationService::AllSources());
465 chrome::CloseWindow(browser()); 465 chrome::CloseWindow(browser());
466 close_window_observer.Wait(); 466 close_window_observer.Wait();
467 EXPECT_EQ(window_count - 1, BrowserList::size()); 467 EXPECT_EQ(window_count - 1, BrowserList::size());
468 468
469 // Restore the window. 469 // Restore the window.
470 ui_test_utils::WindowedNotificationObserver open_window_observer( 470 content::WindowedNotificationObserver open_window_observer(
471 chrome::NOTIFICATION_BROWSER_OPENED, 471 chrome::NOTIFICATION_BROWSER_OPENED,
472 content::NotificationService::AllSources()); 472 content::NotificationService::AllSources());
473 ui_test_utils::WindowedNotificationObserver load_stop_observer( 473 content::WindowedNotificationObserver load_stop_observer(
474 content::NOTIFICATION_LOAD_STOP, 474 content::NOTIFICATION_LOAD_STOP,
475 content::NotificationService::AllSources()); 475 content::NotificationService::AllSources());
476 chrome::RestoreTab(*BrowserList::begin()); 476 chrome::RestoreTab(*BrowserList::begin());
477 open_window_observer.Wait(); 477 open_window_observer.Wait();
478 EXPECT_EQ(window_count, BrowserList::size()); 478 EXPECT_EQ(window_count, BrowserList::size());
479 479
480 Browser* browser = GetBrowser(1); 480 Browser* browser = GetBrowser(1);
481 EXPECT_EQ(initial_tab_count + 2, browser->tab_count()); 481 EXPECT_EQ(initial_tab_count + 2, browser->tab_count());
482 load_stop_observer.Wait(); 482 load_stop_observer.Wait();
483 483
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
536 TabContents* tab = chrome::GetTabContentsAt(browser(), 1); 536 TabContents* tab = chrome::GetTabContentsAt(browser(), 1);
537 EnsureTabFinishedRestoring(tab->web_contents()); 537 EnsureTabFinishedRestoring(tab->web_contents());
538 ASSERT_EQ(http_url, tab->web_contents()->GetURL()); 538 ASSERT_EQ(http_url, tab->web_contents()->GetURL());
539 539
540 // Go back, and see if content is as expected. 540 // Go back, and see if content is as expected.
541 GoBack(browser()); 541 GoBack(browser());
542 EXPECT_GT( 542 EXPECT_GT(
543 ui_test_utils::FindInPage(tab, ASCIIToUTF16("webkit"), true, false, NULL), 543 ui_test_utils::FindInPage(tab, ASCIIToUTF16("webkit"), true, false, NULL),
544 0); 544 0);
545 } 545 }
OLDNEW
« no previous file with comments | « chrome/browser/ssl/ssl_browser_tests.cc ('k') | chrome/browser/task_manager/task_manager_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698