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

Side by Side Diff: chrome/browser/ui/browser_close_browsertest.cc

Issue 7466033: Fix warning prompting on closing a window that will cancel downloads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Last little things from Achuith and Miranda. Created 9 years, 2 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) 2011 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/logging.h"
7 #include "base/path_service.h"
8 #include "base/string_number_conversions.h"
9 #include "base/string_util.h"
10 #include "base/stringprintf.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/download/download_service.h"
13 #include "chrome/browser/download/download_service_factory.h"
14 #include "chrome/browser/download/download_test_observer.h"
15 #include "chrome/browser/net/url_request_mock_util.h"
16 #include "chrome/browser/prefs/pref_service.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/browser_window.h"
21 #include "chrome/browser/ui/webui/active_downloads_ui.h"
22 #include "chrome/common/chrome_paths.h"
23 #include "chrome/common/pref_names.h"
24 #include "chrome/test/base/in_process_browser_test.h"
25 #include "chrome/test/base/ui_test_utils.h"
26 #include "content/browser/download/download_item.h"
27 #include "content/browser/net/url_request_slow_download_job.h"
28 #include "content/browser/tab_contents/tab_contents.h"
29 #include "content/common/url_constants.h"
30 #include "content/public/common/page_transition_types.h"
31
32 class BrowserCloseTest : public InProcessBrowserTest {
33 public:
34 // Structure defining test cases for DownloadsCloseCheck.
35 struct DownloadsCloseCheckCase {
36 std::string DebugString() const;
37
38 // Input
39 struct {
40 struct {
41 int windows;
42 int downloads;
43 } regular;
44 struct {
45 int windows;
46 int downloads;
47 } incognito;
48 } profile_a;
49
50 struct {
51 struct {
52 int windows;
53 int downloads;
54 } regular;
55 struct {
56 int windows;
57 int downloads;
58 } incognito;
59 } profile_b;
60
61 // We always probe a window in profile A.
62 enum { REGULAR = 0, INCOGNITO = 1 } window_to_probe;
63
64 // Output
65 Browser::DownloadClosePreventionType type;
66
67 // Unchecked if type == DOWNLOAD_CLOSE_OK.
68 int num_blocking;
69 };
70
71 protected:
72 virtual void SetUpOnMainThread() OVERRIDE {
73 BrowserThread::PostTask(
74 BrowserThread::IO, FROM_HERE,
75 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
76 }
77
78 // Create a second profile to work within multi-profile.
79 Profile* CreateSecondProfile() {
80 FilePath user_data_dir;
81 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
82
83 if (!second_profile_data_dir_.CreateUniqueTempDirUnderPath(user_data_dir))
84 return NULL;
85
86 return g_browser_process->profile_manager()->GetProfile(
87 second_profile_data_dir_.path());
88 }
89
90 // Create |num_downloads| number of downloads that are stalled
91 // (will quickly get to a place where the server won't
92 // provide any more data) so that we can test closing the
93 // browser with active downloads.
94 void CreateStalledDownloads(Browser* browser, int num_downloads) {
95 GURL url(URLRequestSlowDownloadJob::kKnownSizeUrl);
96
97 if (num_downloads == 0)
98 return;
99
100 // Setup an observer waiting for the given number of downloads
101 // to get to IN_PROGRESS.
102 DownloadManager* download_manager =
103 browser->profile()->GetDownloadManager();
104 scoped_ptr<DownloadTestObserver> observer(
105 new DownloadTestObserver(
106 download_manager, num_downloads,
107 DownloadItem::IN_PROGRESS,
108 true, // Bail on select file.
109 DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL));
110
111 // Set of that number of downloads.
112 while (num_downloads--)
113 ui_test_utils::NavigateToURLWithDisposition(
114 browser, url, NEW_BACKGROUND_TAB,
115 ui_test_utils::BROWSER_TEST_NONE);
116
117 // Wait for them.
118 observer->WaitForFinished();
119 }
120
121 // All all downloads created in CreateStalledDownloads() to
122 // complete, and block in this routine until they do complete.
123 void CompleteAllDownloads(Browser* browser) {
124 GURL finish_url(URLRequestSlowDownloadJob::kFinishDownloadUrl);
125 ui_test_utils::NavigateToURL(browser, finish_url);
126
127 // Go through and, for every single profile, wait until there are
128 // no active downloads on that download manager.
129 std::vector<Profile*> profiles(
130 g_browser_process->profile_manager()->GetLoadedProfiles());
131 for (std::vector<Profile*>::const_iterator pit = profiles.begin();
132 pit != profiles.end(); ++pit) {
133 DownloadService* download_service =
134 DownloadServiceFactory::GetForProfile(*pit);
135 if (download_service->HasCreatedDownloadManager()) {
136 DownloadManager *mgr = download_service->GetDownloadManager();
137 scoped_refptr<DownloadTestFlushObserver> observer(
138 new DownloadTestFlushObserver(mgr));
139 observer->WaitForFlush();
140 }
141 if ((*pit)->HasOffTheRecordProfile()) {
142 DownloadService* incognito_download_service =
143 DownloadServiceFactory::GetForProfile(
144 (*pit)->GetOffTheRecordProfile());
145 if (incognito_download_service->HasCreatedDownloadManager()) {
146 DownloadManager *mgr =
147 incognito_download_service->GetDownloadManager();
148 scoped_refptr<DownloadTestFlushObserver> observer(
149 new DownloadTestFlushObserver(mgr));
150 observer->WaitForFlush();
151 }
152 }
153 }
154 }
155
156 // Create a Browser (with associated window) on the specified profile.
157 Browser* CreateBrowserOnProfile(Profile* profile) {
158 Browser* new_browser = Browser::Create(profile);
159 new_browser->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL),
160 content::PAGE_TRANSITION_START_PAGE);
161 ui_test_utils::WaitForNavigation(
162 &new_browser->GetSelectedTabContents()->controller());
163 new_browser->window()->Show();
164 return new_browser;
165 }
166
167 // Adjust the number of browsers and associated windows up or down
168 // to |num_windows|. This routine assumes that there is only a single
169 // browser associated with the profile on entry. |*base_browser| contains
170 // this browser, and the profile is derived from that browser. On output,
171 // if |*base_browser| was destroyed (because |num_windows == 0|), NULL
172 // is assigned to that memory location.
173 bool AdjustBrowsersOnProfile(Browser** base_browser, int num_windows) {
174 int num_downloads_blocking;
175 if (num_windows == 0) {
176 if (Browser::DOWNLOAD_CLOSE_OK !=
177 (*base_browser)->OkToCloseWithInProgressDownloads(
178 &num_downloads_blocking))
179 return false;
180 (*base_browser)->window()->Close();
181 *base_browser = 0;
182 return true;
183 }
184
185 // num_windows > 0
186 Profile* profile((*base_browser)->profile());
187 for (int w = 1; w < num_windows; ++w) {
188 CreateBrowserOnProfile(profile);
189 }
190 return true;
191 }
192
193 int TotalUnclosedBrowsers() {
194 int count = 0;
195 for (BrowserList::const_iterator iter = BrowserList::begin();
196 iter != BrowserList::end(); ++iter)
197 if (!(*iter)->IsAttemptingToCloseBrowser()) {
198 count++;
199 }
200 return count;
201 }
202
203 // Note that this is invalid to call if TotalUnclosedBrowsers() == 0.
204 Browser* FirstUnclosedBrowser() {
205 for (BrowserList::const_iterator iter = BrowserList::begin();
206 iter != BrowserList::end(); ++iter)
207 if (!(*iter)->IsAttemptingToCloseBrowser())
208 return (*iter);
209 return NULL;
210 }
211
212 bool SetupForDownloadCloseCheck() {
213 first_profile_ = browser()->profile();
214
215 bool result = first_profile_data_dir_.CreateUniqueTempDir();
216 EXPECT_TRUE(result);
217 if (!result) return false;
218 first_profile_->GetPrefs()->SetFilePath(
219 prefs::kDownloadDefaultDirectory,
220 first_profile_data_dir_.path());
221
222 second_profile_ = CreateSecondProfile();
223 EXPECT_TRUE(second_profile_);
224 if (!second_profile_) return false;
225
226 return true;
227 }
228
229 // Test a specific DownloadsCloseCheckCase. Returns false if
230 // an assertion has failed and the test should be aborted.
231 bool ExecuteDownloadCloseCheckCase(size_t i) {
232 const DownloadsCloseCheckCase& check_case(download_close_check_cases[i]);
233
234 // Debugging hack to make it easy to run a single case from the
235 // command line.
236 CommandLine* command_line = CommandLine::ForCurrentProcess();
237 if (command_line->HasSwitch("test_argument")) {
238 std::string value =
239 command_line->GetSwitchValueASCII("test_argument");
240 std::vector<std::string> cases;
241 Tokenize(value, ",", &cases);
242 std::vector<std::string>::iterator it = cases.begin();
243 for (; it != cases.end(); ++it) {
244 int test_case_int;
245 base::StringToInt(*it, &test_case_int);
246 if (i == static_cast<size_t>(test_case_int))
247 break;
248 }
249 if (it == cases.end())
250 return true;
251 }
252
253 // Test invariant: so that we don't actually try and close the browser,
254 // we always enter the function with a single browser window open on the
255 // main profile. That means we need to exit the function the same way.
256 // So we setup everything except for the |first_profile_| regular, and then
257 // flip the bit on the main window.
258 // Note that this means that browser() is unreliable in the context
259 // of this function or its callers; we'll be killing that main window
260 // and recreating it fairly frequently.
261 int unclosed_browsers = TotalUnclosedBrowsers();
262 EXPECT_EQ(1, unclosed_browsers);
263 if (1 != unclosed_browsers)
264 return false;
265
266 Browser* entry_browser = FirstUnclosedBrowser();
267 EXPECT_EQ(first_profile_, entry_browser->profile())
268 << "Case" << i
269 << ": " << check_case.DebugString();
270 if (first_profile_ != entry_browser->profile())
271 return false;
272 int total_download_count = DownloadService::TotalDownloadCount();
273 EXPECT_EQ(0, total_download_count)
274 << "Case " << i
275 << ": " << check_case.DebugString();
276 if (0 != total_download_count)
277 return false;
278
279 Profile* first_profile_incognito = first_profile_->GetOffTheRecordProfile();
280 Profile* second_profile_incognito =
281 second_profile_->GetOffTheRecordProfile();
282
283 // For simplicty of coding, we create a window on each profile so that
284 // we can easily create downloads, then we destroy or create windows
285 // as necessary.
286 Browser* browser_a_regular(CreateBrowserOnProfile(first_profile_));
287 Browser* browser_a_incognito(
288 CreateBrowserOnProfile(first_profile_incognito));
289 Browser* browser_b_regular(CreateBrowserOnProfile(second_profile_));
290 Browser* browser_b_incognito(
291 CreateBrowserOnProfile(second_profile_incognito));
292
293 // Kill our entry browser.
294 entry_browser->window()->Close();
295 entry_browser = NULL;
296
297 // Create all downloads needed.
298 CreateStalledDownloads(
299 browser_a_regular, check_case.profile_a.regular.downloads);
300 CreateStalledDownloads(
301 browser_a_incognito, check_case.profile_a.incognito.downloads);
302 CreateStalledDownloads(
303 browser_b_regular, check_case.profile_b.regular.downloads);
304 CreateStalledDownloads(
305 browser_b_incognito, check_case.profile_b.incognito.downloads);
306
307 // Adjust the windows
308 Browser** browsers[] = {
309 &browser_a_regular, &browser_a_incognito,
310 &browser_b_regular, &browser_b_incognito
311 };
312 int window_counts[] = {
313 check_case.profile_a.regular.windows,
314 check_case.profile_a.incognito.windows,
315 check_case.profile_b.regular.windows,
316 check_case.profile_b.incognito.windows,
317 };
318 for (size_t j = 0; j < arraysize(browsers); ++j) {
319 bool result = AdjustBrowsersOnProfile(browsers[j], window_counts[j]);
320 EXPECT_TRUE(result);
321 if (!result)
322 return false;
323 }
324 ui_test_utils::RunAllPendingInMessageLoop();
325
326 #if defined(OS_CHROMEOS)
327 // Get rid of downloads panel on ChromeOS
328 Browser* panel = ActiveDownloadsUI::GetPopup();
329 if (panel)
330 panel->CloseWindow();
331 ui_test_utils::RunAllPendingInMessageLoop();
332 #endif
333
334 // All that work, for this one little test.
335 EXPECT_TRUE((check_case.window_to_probe ==
336 DownloadsCloseCheckCase::REGULAR) ||
337 (check_case.window_to_probe ==
338 DownloadsCloseCheckCase::INCOGNITO));
339 if (!((check_case.window_to_probe ==
340 DownloadsCloseCheckCase::REGULAR) ||
341 (check_case.window_to_probe ==
342 DownloadsCloseCheckCase::INCOGNITO)))
343 return false;
344
345 int num_downloads_blocking;
346 Browser* browser_to_probe =
347 (check_case.window_to_probe == DownloadsCloseCheckCase::REGULAR ?
348 browser_a_regular :
349 browser_a_incognito);
350 Browser::DownloadClosePreventionType type =
351 browser_to_probe->OkToCloseWithInProgressDownloads(
352 &num_downloads_blocking);
353 EXPECT_EQ(check_case.type, type) << "Case " << i
354 << ": " << check_case.DebugString();
355 if (type != Browser::DOWNLOAD_CLOSE_OK)
356 EXPECT_EQ(check_case.num_blocking, num_downloads_blocking)
357 << "Case " << i
358 << ": " << check_case.DebugString();
359
360 // Release all the downloads.
361 CompleteAllDownloads(browser_to_probe);
362
363 // Create a new main window and kill everything else.
364 entry_browser = CreateBrowserOnProfile(first_profile_);
365 for (BrowserList::const_iterator bit = BrowserList::begin();
366 bit != BrowserList::end(); ++bit) {
367 if ((*bit) != entry_browser) {
368 EXPECT_TRUE((*bit)->window());
369 if (!(*bit)->window())
370 return false;
371 (*bit)->window()->Close();
372 }
373 }
374 ui_test_utils::RunAllPendingInMessageLoop();
375
376 return true;
377 }
378
379 static const DownloadsCloseCheckCase download_close_check_cases[];
380
381 // DownloadCloseCheck variables.
382 Profile* first_profile_;
383 Profile* second_profile_;
384
385 ScopedTempDir first_profile_data_dir_;
386 ScopedTempDir second_profile_data_dir_;
387 };
388
389 const BrowserCloseTest::DownloadsCloseCheckCase
390 BrowserCloseTest::download_close_check_cases[] = {
391 // Top level nesting is {profile_a, profile_b}
392 // Second level nesting is {regular, incognito
393 // Third level (inner) nesting is {windows, downloads}
394
395 // Last window (incognito) triggers browser close warning.
396 {{{0, 0}, {1, 1}}, {{0, 0}, {0, 0}},
397 BrowserCloseTest::DownloadsCloseCheckCase::INCOGNITO,
398 Browser::DOWNLOAD_CLOSE_BROWSER_SHUTDOWN, 1},
399
400 // Last incognito window triggers incognito close warning.
401 {{{1, 0}, {1, 1}}, {{0, 0}, {0, 0}},
402 BrowserCloseTest::DownloadsCloseCheckCase::INCOGNITO,
403 Browser::DOWNLOAD_CLOSE_LAST_WINDOW_IN_INCOGNITO_PROFILE, 1},
404
405 // Last incognito window with no downloads triggers no warning.
406 {{{0, 0}, {1, 0}}, {{0, 0}, {0, 0}},
407 BrowserCloseTest::DownloadsCloseCheckCase::INCOGNITO,
408 Browser::DOWNLOAD_CLOSE_OK},
409
410 // Last incognito window with window+download on another incognito profile
411 // triggers no warning.
412 {{{0, 0}, {1, 0}}, {{0, 0}, {1, 1}},
413 BrowserCloseTest::DownloadsCloseCheckCase::INCOGNITO,
414 Browser::DOWNLOAD_CLOSE_OK},
415
416 // Non-last incognito window triggers no warning.
417 {{{0, 0}, {2, 1}}, {{0, 0}, {0, 0}},
418 BrowserCloseTest::DownloadsCloseCheckCase::INCOGNITO,
419 Browser::DOWNLOAD_CLOSE_OK},
420
421 // Non-last regular window triggers no warning.
422 {{{2, 1}, {0, 0}}, {{0, 0}, {0, 0}},
423 BrowserCloseTest::DownloadsCloseCheckCase::REGULAR,
424 Browser::DOWNLOAD_CLOSE_OK},
425
426 // Last regular window triggers browser close.
427 {{{1, 1}, {0, 0}}, {{0, 0}, {0, 0}},
428 BrowserCloseTest::DownloadsCloseCheckCase::REGULAR,
429 Browser::DOWNLOAD_CLOSE_BROWSER_SHUTDOWN, 1},
430
431 // Last regular window triggers browser close for download on different
432 // profile.
433 {{{1, 0}, {0, 0}}, {{0, 1}, {0, 0}},
434 BrowserCloseTest::DownloadsCloseCheckCase::REGULAR,
435 Browser::DOWNLOAD_CLOSE_BROWSER_SHUTDOWN, 1},
436
437 // Last regular window triggers no warning if incognito
438 // active (http://crbug.com/61257).
439 {{{1, 0}, {1, 1}}, {{0, 0}, {0, 0}},
440 BrowserCloseTest::DownloadsCloseCheckCase::REGULAR,
441 Browser::DOWNLOAD_CLOSE_OK},
442
443 // Last regular window triggers no warning if other profile window active.
444 {{{1, 1}, {0, 0}}, {{1, 0}, {0, 0}},
445 BrowserCloseTest::DownloadsCloseCheckCase::REGULAR,
446 Browser::DOWNLOAD_CLOSE_OK},
447
448 // Last regular window triggers no warning if other incognito window
449 // active.
450 {{{1, 0}, {0, 0}}, {{0, 0}, {1, 1}},
451 BrowserCloseTest::DownloadsCloseCheckCase::REGULAR,
452 Browser::DOWNLOAD_CLOSE_OK},
453
454 // Last regular window triggers no warning if incognito active.
455 {{{1, 1}, {1, 0}}, {{0, 0}, {0, 0}},
456 BrowserCloseTest::DownloadsCloseCheckCase::REGULAR,
457 Browser::DOWNLOAD_CLOSE_OK},
458
459 // Test plural for regular.
460 {{{1, 2}, {0, 0}}, {{0, 0}, {0, 0}},
461 BrowserCloseTest::DownloadsCloseCheckCase::REGULAR,
462 Browser::DOWNLOAD_CLOSE_BROWSER_SHUTDOWN, 2},
463
464 // Test plural for incognito.
465 {{{1, 0}, {1, 2}}, {{0, 0}, {0, 0}},
466 BrowserCloseTest::DownloadsCloseCheckCase::INCOGNITO,
467 Browser::DOWNLOAD_CLOSE_LAST_WINDOW_IN_INCOGNITO_PROFILE, 2},
468 };
469
470 std::string BrowserCloseTest::DownloadsCloseCheckCase::DebugString() const {
471 std::string result;
472 result += "{";
473 if (profile_a.regular.windows || profile_a.regular.downloads)
474 result += base::StringPrintf("Regular profile A: (%d w, %d d), ",
475 profile_a.regular.windows,
476 profile_a.regular.downloads);
477 if (profile_a.incognito.windows || profile_a.incognito.downloads)
478 result += base::StringPrintf("Incognito profile A: (%d w, %d d), ",
479 profile_a.incognito.windows,
480 profile_a.incognito.downloads);
481 if (profile_b.regular.windows || profile_b.regular.downloads)
482 result += base::StringPrintf("Regular profile B: (%d w, %d d), ",
483 profile_b.regular.windows,
484 profile_b.regular.downloads);
485 if (profile_b.incognito.windows || profile_b.incognito.downloads)
486 result += base::StringPrintf("Incognito profile B: (%d w, %d d), ",
487 profile_b.incognito.windows,
488 profile_b.incognito.downloads);
489 result += (window_to_probe == REGULAR ? "Probe regular" :
490 window_to_probe == INCOGNITO ? "Probe incognito" :
491 "Probe unknown");
492 result += "} -> ";
493 if (type == Browser::DOWNLOAD_CLOSE_OK) {
494 result += "No warning";
495 } else {
496 result += base::StringPrintf(
497 "%s (%d downloads) warning",
498 (type == Browser::DOWNLOAD_CLOSE_BROWSER_SHUTDOWN ? "Browser shutdown" :
499 type == Browser::DOWNLOAD_CLOSE_LAST_WINDOW_IN_INCOGNITO_PROFILE ?
500 "Incognito close" : "Unknown"),
501 num_blocking);
502 }
503 return result;
504 }
505
506 // The following test is split into three chunks to reduce the chance
507 // of hitting the 25s timeout.
508
509 IN_PROC_BROWSER_TEST_F(BrowserCloseTest, DownloadsCloseCheck_0) {
510 ASSERT_TRUE(SetupForDownloadCloseCheck());
511 for (size_t i = 0; i < arraysize(download_close_check_cases) / 3; ++i) {
512 ExecuteDownloadCloseCheckCase(i);
513 }
514 }
515
516 IN_PROC_BROWSER_TEST_F(BrowserCloseTest, DownloadsCloseCheck_1) {
517 ASSERT_TRUE(SetupForDownloadCloseCheck());
518 for (size_t i = arraysize(download_close_check_cases) / 3;
519 i < 2 * arraysize(download_close_check_cases) / 3; ++i) {
520 ExecuteDownloadCloseCheckCase(i);
521 }
522 }
523
524 IN_PROC_BROWSER_TEST_F(BrowserCloseTest, DownloadsCloseCheck_2) {
525 ASSERT_TRUE(SetupForDownloadCloseCheck());
526 for (size_t i = 2 * arraysize(download_close_check_cases) / 3;
527 i < arraysize(download_close_check_cases); ++i) {
528 ExecuteDownloadCloseCheckCase(i);
529 }
530 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698