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

Side by Side Diff: chrome/browser/sessions/session_restore_uitest.cc

Issue 10202005: Convert the session restore ui_tests to browser_tests. I renabled the disabled tests; hopefully the… (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: renable the mac test Created 8 years, 8 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
(Empty)
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/command_line.h"
6 #include "base/file_path.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/string_number_conversions.h"
9 #include "base/test/test_timeouts.h"
10 #include "chrome/app/chrome_command_ids.h"
11 #include "chrome/browser/defaults.h"
12 #include "chrome/common/chrome_paths.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/common/url_constants.h"
15 #include "chrome/test/automation/automation_proxy.h"
16 #include "chrome/test/automation/browser_proxy.h"
17 #include "chrome/test/automation/tab_proxy.h"
18 #include "chrome/test/automation/window_proxy.h"
19 #include "chrome/test/ui/ui_test.h"
20 #include "googleurl/src/gurl.h"
21 #include "net/base/net_util.h"
22 #include "net/test/test_server.h"
23
24 namespace {
25
26 class SessionRestoreUITest : public UITest {
27 protected:
28 SessionRestoreUITest() : UITest() {
29 FilePath path_prefix = test_data_directory_.AppendASCII("session_history");
30
31 url1_ = net::FilePathToFileURL(path_prefix.AppendASCII("bot1.html"));
32 url2_ = net::FilePathToFileURL(path_prefix.AppendASCII("bot2.html"));
33 url3_ = net::FilePathToFileURL(path_prefix.AppendASCII("bot3.html"));
34 }
35
36 virtual void QuitBrowserAndRestore(int expected_tab_count) {
37 #if defined(OS_CHROMEOS) || defined(OS_MACOSX)
38 set_shutdown_type(ProxyLauncher::USER_QUIT);
39 #endif
40 UITest::TearDown();
41
42 clear_profile_ = false;
43
44 // Clear launch_arguments so that the URL arg doesn't get added on restart.
45 launch_arguments_ = CommandLine(launch_arguments_.GetProgram());
46
47 launch_arguments_.AppendSwitchASCII(switches::kRestoreLastSession,
48 base::IntToString(expected_tab_count));
49 UITest::SetUp();
50 }
51
52 void CloseWindow(int window_index, int initial_count) {
53 scoped_refptr<BrowserProxy> browser_proxy(
54 automation()->GetBrowserWindow(window_index));
55 ASSERT_TRUE(browser_proxy.get());
56 ASSERT_TRUE(browser_proxy->RunCommand(IDC_CLOSE_WINDOW));
57 int window_count;
58 ASSERT_TRUE(automation()->GetBrowserWindowCount(&window_count));
59 ASSERT_EQ(initial_count - 1, window_count);
60 }
61
62 void AssertOneWindowWithOneTab() {
63 int window_count;
64 ASSERT_TRUE(automation()->GetBrowserWindowCount(&window_count));
65 ASSERT_EQ(1, window_count);
66 GURL url;
67 AssertWindowHasOneTab(0, &url);
68 }
69
70 void AssertWindowHasOneTab(int window_index, GURL* url) {
71 scoped_refptr<BrowserProxy> browser_proxy(
72 automation()->GetBrowserWindow(window_index));
73 ASSERT_TRUE(browser_proxy.get());
74
75 int tab_count;
76 ASSERT_TRUE(browser_proxy->GetTabCount(&tab_count));
77 ASSERT_EQ(1, tab_count);
78
79 int active_tab_index;
80 ASSERT_TRUE(browser_proxy->GetActiveTabIndex(&active_tab_index));
81 ASSERT_EQ(0, active_tab_index);
82
83 scoped_refptr<TabProxy> tab_proxy(browser_proxy->GetActiveTab());
84 ASSERT_TRUE(tab_proxy.get());
85 ASSERT_TRUE(tab_proxy->WaitForTabToBeRestored(
86 TestTimeouts::action_max_timeout_ms()));
87
88 ASSERT_TRUE(tab_proxy->GetCurrentURL(url));
89 }
90
91 GURL url1_;
92 GURL url2_;
93 GURL url3_;
94
95 private:
96 DISALLOW_COPY_AND_ASSIGN(SessionRestoreUITest);
97 };
98
99 TEST_F(SessionRestoreUITest, Basic) {
100 NavigateToURL(url1_);
101 NavigateToURL(url2_);
102
103 QuitBrowserAndRestore(1);
104
105 // NOTE: Don't use GetActiveWindow here, when run with the screen locked
106 // active windows returns NULL.
107 int window_count;
108 ASSERT_TRUE(automation()->GetBrowserWindowCount(&window_count));
109 ASSERT_EQ(1, window_count);
110 scoped_refptr<BrowserProxy> browser_proxy(automation()->GetBrowserWindow(0));
111 ASSERT_TRUE(browser_proxy.get());
112 scoped_refptr<TabProxy> tab_proxy(browser_proxy->GetTab(0));
113 ASSERT_TRUE(tab_proxy->WaitForTabToBeRestored(
114 TestTimeouts::action_max_timeout_ms()));
115
116 ASSERT_EQ(url2_, GetActiveTabURL());
117 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, tab_proxy->GoBack());
118 ASSERT_EQ(url1_, GetActiveTabURL());
119 }
120
121 TEST_F(SessionRestoreUITest, RestoresForwardAndBackwardNavs) {
122 NavigateToURL(url1_);
123 NavigateToURL(url2_);
124 NavigateToURL(url3_);
125
126 scoped_refptr<TabProxy> active_tab(GetActiveTab());
127 ASSERT_TRUE(active_tab.get());
128 ASSERT_TRUE(active_tab->GoBack());
129
130 QuitBrowserAndRestore(1);
131
132 // NOTE: Don't use GetActiveWindow here, when run with the screen locked
133 // active windows returns NULL.
134 int window_count;
135 ASSERT_TRUE(automation()->GetBrowserWindowCount(&window_count));
136 ASSERT_EQ(1, window_count);
137 scoped_refptr<BrowserProxy> browser_proxy(automation()->GetBrowserWindow(0));
138 ASSERT_TRUE(browser_proxy.get());
139 scoped_refptr<TabProxy> tab_proxy(browser_proxy->GetTab(0));
140 ASSERT_TRUE(tab_proxy->WaitForTabToBeRestored(
141 TestTimeouts::action_max_timeout_ms()));
142
143 ASSERT_TRUE(GetActiveTabURL() == url2_);
144 ASSERT_TRUE(tab_proxy->GoForward());
145 ASSERT_TRUE(GetActiveTabURL() == url3_);
146 ASSERT_TRUE(tab_proxy->GoBack());
147 ASSERT_TRUE(GetActiveTabURL() == url2_);
148
149 // Test renderer-initiated back/forward as well.
150 GURL go_back_url("javascript:history.back();");
151 ASSERT_TRUE(tab_proxy->NavigateToURL(go_back_url));
152 ASSERT_TRUE(GetActiveTabURL() == url1_);
153 }
154
155 // Tests that the SiteInstances used for entries in a restored tab's history
156 // are given appropriate max page IDs, so that going back to a restored
157 // cross-site page and then forward again works. (Bug 1204135)
158 TEST_F(SessionRestoreUITest, RestoresCrossSiteForwardAndBackwardNavs) {
159 net::TestServer test_server(net::TestServer::TYPE_HTTP,
160 net::TestServer::kLocalhost,
161 FilePath(FILE_PATH_LITERAL("chrome/test/data")));
162 ASSERT_TRUE(test_server.Start());
163
164 GURL cross_site_url(test_server.GetURL("files/title2.html"));
165
166 // Visit URLs on different sites.
167 NavigateToURL(url1_);
168 NavigateToURL(cross_site_url);
169 NavigateToURL(url2_);
170
171 scoped_refptr<TabProxy> active_tab(GetActiveTab());
172 ASSERT_TRUE(active_tab.get());
173 ASSERT_TRUE(active_tab->GoBack());
174
175 QuitBrowserAndRestore(1);
176
177 // NOTE: Don't use GetActiveWindow here, when run with the screen locked
178 // active windows returns NULL.
179 int window_count;
180 ASSERT_TRUE(automation()->GetBrowserWindowCount(&window_count));
181 ASSERT_EQ(1, window_count);
182 scoped_refptr<BrowserProxy> browser_proxy(automation()->GetBrowserWindow(0));
183 ASSERT_TRUE(browser_proxy.get());
184 int tab_count;
185 ASSERT_TRUE(browser_proxy->GetTabCount(&tab_count));
186 ASSERT_EQ(1, tab_count);
187 scoped_refptr<TabProxy> tab_proxy(browser_proxy->GetTab(0));
188 ASSERT_TRUE(tab_proxy.get());
189 ASSERT_TRUE(tab_proxy->WaitForTabToBeRestored(
190 TestTimeouts::action_max_timeout_ms()));
191
192 // Check that back and forward work as expected.
193 GURL url;
194 ASSERT_TRUE(tab_proxy->GetCurrentURL(&url));
195 ASSERT_EQ(cross_site_url, url);
196
197 ASSERT_TRUE(tab_proxy->GoBack());
198 ASSERT_TRUE(tab_proxy->GetCurrentURL(&url));
199 ASSERT_EQ(url1_, url);
200
201 ASSERT_TRUE(tab_proxy->GoForward());
202 ASSERT_TRUE(tab_proxy->GetCurrentURL(&url));
203 ASSERT_EQ(cross_site_url, url);
204
205 // Test renderer-initiated back/forward as well.
206 GURL go_forward_url("javascript:history.forward();");
207 ASSERT_TRUE(tab_proxy->NavigateToURL(go_forward_url));
208 ASSERT_TRUE(tab_proxy->GetCurrentURL(&url));
209 ASSERT_EQ(url2_, url);
210 }
211
212 TEST_F(SessionRestoreUITest, TwoTabsSecondSelected) {
213 NavigateToURL(url1_);
214
215 // NOTE: Don't use GetActiveWindow here, when run with the screen locked
216 // active windows returns NULL.
217 int window_count;
218 ASSERT_TRUE(automation()->GetBrowserWindowCount(&window_count));
219 ASSERT_EQ(1, window_count);
220 scoped_refptr<BrowserProxy> browser_proxy(automation()->GetBrowserWindow(0));
221 ASSERT_TRUE(browser_proxy.get());
222
223 ASSERT_TRUE(browser_proxy->AppendTab(url2_));
224
225 QuitBrowserAndRestore(2);
226 browser_proxy = NULL;
227
228 ASSERT_TRUE(automation()->GetBrowserWindowCount(&window_count));
229 ASSERT_EQ(1, window_count);
230 browser_proxy = automation()->GetBrowserWindow(0);
231
232 int tab_count;
233 ASSERT_TRUE(browser_proxy->GetTabCount(&tab_count));
234 ASSERT_EQ(2, tab_count);
235
236 int active_tab_index;
237 ASSERT_TRUE(browser_proxy->GetActiveTabIndex(&active_tab_index));
238 ASSERT_EQ(1, active_tab_index);
239
240 scoped_refptr<TabProxy> tab_proxy(browser_proxy->GetActiveTab());
241 ASSERT_TRUE(tab_proxy.get());
242 ASSERT_TRUE(tab_proxy->WaitForTabToBeRestored(
243 TestTimeouts::action_max_timeout_ms()));
244
245 ASSERT_EQ(url2_, GetActiveTabURL());
246
247 ASSERT_TRUE(browser_proxy->ActivateTab(0));
248 tab_proxy = browser_proxy->GetActiveTab();
249 ASSERT_TRUE(tab_proxy.get());
250 ASSERT_TRUE(tab_proxy->WaitForTabToBeRestored(
251 TestTimeouts::action_max_timeout_ms()));
252
253 ASSERT_EQ(url1_, GetActiveTabURL());
254 }
255
256 // Creates two tabs, closes one, quits and makes sure only one tab is restored.
257 TEST_F(SessionRestoreUITest, ClosedTabStaysClosed) {
258 NavigateToURL(url1_);
259
260 // NOTE: Don't use GetActiveWindow here, when run with the screen locked
261 // active windows returns NULL.
262 int window_count;
263 ASSERT_TRUE(automation()->GetBrowserWindowCount(&window_count));
264 ASSERT_EQ(1, window_count);
265 scoped_refptr<BrowserProxy> browser_proxy(automation()->GetBrowserWindow(0));
266 ASSERT_TRUE(browser_proxy.get());
267 scoped_refptr<TabProxy> tab_proxy(browser_proxy->GetTab(0));
268 ASSERT_TRUE(tab_proxy.get());
269
270 ASSERT_TRUE(browser_proxy->AppendTab(url2_));
271
272 scoped_refptr<TabProxy> active_tab(browser_proxy->GetActiveTab());
273 ASSERT_TRUE(active_tab.get());
274 ASSERT_TRUE(active_tab->Close(true));
275
276 QuitBrowserAndRestore(1);
277 browser_proxy = NULL;
278 tab_proxy = NULL;
279
280 AssertOneWindowWithOneTab();
281
282 ASSERT_EQ(url1_, GetActiveTabURL());
283 }
284
285 // Test to verify that the print preview tab is not restored.
286 TEST_F(SessionRestoreUITest, DontRestorePrintPreviewTabTest) {
287 NavigateToURL(url1_);
288
289 int window_count;
290 ASSERT_TRUE(automation()->GetBrowserWindowCount(&window_count));
291 ASSERT_EQ(1, window_count);
292 scoped_refptr<BrowserProxy> browser_proxy(automation()->GetBrowserWindow(0));
293 ASSERT_TRUE(browser_proxy.get());
294
295 // Append the print preview tab.
296 GURL printPreviewURL(chrome::kChromeUIPrintURL);
297 ASSERT_TRUE(browser_proxy->AppendTab(printPreviewURL));
298
299 scoped_refptr<TabProxy> active_tab(browser_proxy->GetActiveTab());
300 ASSERT_TRUE(active_tab.get());
301 ASSERT_EQ(printPreviewURL, GetActiveTabURL());
302
303 // Restart and make sure we have only one window with one tab and the url
304 // is url1_.
305 QuitBrowserAndRestore(1);
306 browser_proxy = NULL;
307
308 AssertOneWindowWithOneTab();
309
310 ASSERT_EQ(url1_, GetActiveTabURL());
311 }
312
313 // Creates a tabbed browser and popup and makes sure we restore both.
314 TEST_F(SessionRestoreUITest, NormalAndPopup) {
315 if (!browser_defaults::kRestorePopups)
316 return; // Test only applicable if restoring popups.
317
318 NavigateToURL(url1_);
319
320 // Make sure we have one window.
321 int window_count;
322 ASSERT_TRUE(automation()->GetBrowserWindowCount(&window_count));
323 ASSERT_EQ(1, window_count);
324
325 // Open a popup.
326 ASSERT_TRUE(automation()->OpenNewBrowserWindow(Browser::TYPE_POPUP,
327 true));
328 ASSERT_TRUE(automation()->GetBrowserWindowCount(&window_count));
329 ASSERT_EQ(2, window_count);
330
331 scoped_refptr<BrowserProxy> popup(automation()->GetBrowserWindow(1));
332 ASSERT_TRUE(popup.get());
333
334 scoped_refptr<TabProxy> tab(popup->GetTab(0));
335 ASSERT_TRUE(tab.get());
336
337 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, tab->NavigateToURL(url1_));
338
339 // Simulate an exit by shuting down the session service. If we don't do this
340 // the first window close is treated as though the user closed the window
341 // and won't be restored.
342 ASSERT_TRUE(popup->ShutdownSessionService());
343
344 tab = NULL;
345 popup = NULL;
346
347 // Restart and make sure we have only one window with one tab and the url
348 // is url1_.
349 QuitBrowserAndRestore(1);
350
351 ASSERT_TRUE(automation()->GetBrowserWindowCount(&window_count));
352 ASSERT_EQ(2, window_count);
353
354 scoped_refptr<BrowserProxy> browser_proxy1(
355 automation()->GetBrowserWindow(0));
356 ASSERT_TRUE(browser_proxy1.get());
357
358 scoped_refptr<BrowserProxy> browser_proxy2(
359 automation()->GetBrowserWindow(1));
360 ASSERT_TRUE(browser_proxy2.get());
361
362 Browser::Type type1, type2;
363 ASSERT_TRUE(browser_proxy1->GetType(&type1));
364 ASSERT_TRUE(browser_proxy2->GetType(&type2));
365
366 // The order of whether the normal window or popup is first depends upon
367 // activation order, which is not necessarily consistant across runs.
368 if (type1 == Browser::TYPE_TABBED) {
369 EXPECT_EQ(type2, Browser::TYPE_POPUP);
370 } else {
371 EXPECT_EQ(type1, Browser::TYPE_POPUP);
372 EXPECT_EQ(type2, Browser::TYPE_TABBED);
373 }
374 }
375
376 #if !defined(OS_MACOSX)
377 // This test doesn't apply to the Mac version; see
378 // LaunchAnotherBrowserBlockUntilClosed for details.
379
380 // Launches an app window, closes tabbed browser, launches and makes sure
381 // we restore the tabbed browser url.
382 // Flaky: http://crbug.com/29110
383 TEST_F(SessionRestoreUITest,
384 DISABLED_RestoreAfterClosingTabbedBrowserWithAppAndLaunching) {
385 NavigateToURL(url1_);
386
387 // Launch an app.
388
389 bool include_testing_id_orig = include_testing_id_;
390 include_testing_id_ = false;
391 clear_profile_ = false;
392 CommandLine app_launch_arguments = launch_arguments_;
393 app_launch_arguments.AppendSwitchASCII(switches::kApp, url2_.spec());
394 LaunchAnotherBrowserBlockUntilClosed(app_launch_arguments);
395 ASSERT_TRUE(automation()->WaitForWindowCountToBecome(2));
396
397 // Close the first window. The only window left is the App window.
398 CloseWindow(0, 2);
399
400 // Restore the session, which should bring back the first window with url1_.
401 // First restore the settings so we can connect to the browser.
402 include_testing_id_ = include_testing_id_orig;
403 // Restore the session with 1 tab.
404 QuitBrowserAndRestore(1);
405
406 AssertOneWindowWithOneTab();
407
408 ASSERT_EQ(url1_, GetActiveTabURL());
409 }
410
411 #endif // !OS_MACOSX
412
413 // Creates two windows, closes one, restores, make sure only one window open.
414 TEST_F(SessionRestoreUITest, TwoWindowsCloseOneRestoreOnlyOne) {
415 NavigateToURL(url1_);
416
417 // Make sure we have one window.
418 int window_count;
419 ASSERT_TRUE(automation()->GetBrowserWindowCount(&window_count));
420 ASSERT_EQ(1, window_count);
421
422 // Open a second window.
423 ASSERT_TRUE(automation()->OpenNewBrowserWindow(Browser::TYPE_TABBED,
424 true));
425 ASSERT_TRUE(automation()->GetBrowserWindowCount(&window_count));
426 ASSERT_EQ(2, window_count);
427
428 // Close it.
429 CloseWindow(1, 2);
430
431 // Restart and make sure we have only one window with one tab and the url
432 // is url1_.
433 QuitBrowserAndRestore(1);
434
435 AssertOneWindowWithOneTab();
436
437 ASSERT_EQ(url1_, GetActiveTabURL());
438 }
439
440 // Make sure after a restore the number of processes matches that of the number
441 // of processes running before the restore. This creates a new tab so that
442 // we should have two new tabs running. (This test will pass in both
443 // process-per-site and process-per-site-instance, because we treat the new tab
444 // as a special case in process-per-site-instance so that it only ever uses one
445 // process.)
446 //
447 // Flaky: http://code.google.com/p/chromium/issues/detail?id=52022
448 // Unfortunately, the fix at http://codereview.chromium.org/6546078
449 // breaks NTP background image refreshing, so ThemeSource had to revert to
450 // replacing the existing data source.
451 TEST_F(SessionRestoreUITest, DISABLED_ShareProcessesOnRestore) {
452 scoped_refptr<BrowserProxy> browser_proxy(automation()->GetBrowserWindow(0));
453 ASSERT_TRUE(browser_proxy.get() != NULL);
454 int tab_count;
455 ASSERT_TRUE(browser_proxy->GetTabCount(&tab_count));
456
457 // Create two new tabs.
458 ASSERT_TRUE(browser_proxy->RunCommand(IDC_NEW_TAB));
459 ASSERT_TRUE(browser_proxy->RunCommand(IDC_NEW_TAB));
460 int new_tab_count;
461 ASSERT_TRUE(browser_proxy->GetTabCount(&new_tab_count));
462 ASSERT_EQ(tab_count + 2, new_tab_count);
463
464 int expected_process_count = 0;
465 ASSERT_TRUE(GetBrowserProcessCount(&expected_process_count));
466 int expected_tab_count = new_tab_count;
467
468 // Restart.
469 browser_proxy = NULL;
470 QuitBrowserAndRestore(3);
471
472 // Wait for each tab to finish being restored, then make sure the process
473 // count matches.
474 browser_proxy = automation()->GetBrowserWindow(0);
475 ASSERT_TRUE(browser_proxy.get() != NULL);
476 ASSERT_TRUE(browser_proxy->GetTabCount(&tab_count));
477 ASSERT_EQ(expected_tab_count, tab_count);
478
479 for (int i = 0; i < expected_tab_count; ++i) {
480 scoped_refptr<TabProxy> tab_proxy(browser_proxy->GetTab(i));
481 ASSERT_TRUE(tab_proxy.get() != NULL);
482 ASSERT_TRUE(tab_proxy->WaitForTabToBeRestored(
483 TestTimeouts::action_max_timeout_ms()));
484 }
485
486 int process_count = 0;
487 ASSERT_TRUE(GetBrowserProcessCount(&process_count));
488 ASSERT_EQ(expected_process_count, process_count);
489 }
490
491 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/sessions/session_restore_browsertest.cc ('k') | chrome/browser/sessions/session_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698