OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 9 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
10 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 10 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
| 11 #include "chrome/browser/download/download_prefs.h" |
11 #include "chrome/browser/download/download_service.h" | 12 #include "chrome/browser/download/download_service.h" |
12 #include "chrome/browser/download/download_service_factory.h" | 13 #include "chrome/browser/download/download_service_factory.h" |
13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/signin/signin_manager_factory.h" | 15 #include "chrome/browser/signin/signin_manager_factory.h" |
15 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/browser_commands.h" | 17 #include "chrome/browser/ui/browser_commands.h" |
17 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
18 #include "chrome/grit/chromium_strings.h" | 19 #include "chrome/grit/chromium_strings.h" |
19 #include "chrome/grit/generated_resources.h" | 20 #include "chrome/grit/generated_resources.h" |
20 #include "chrome/test/base/in_process_browser_test.h" | 21 #include "chrome/test/base/in_process_browser_test.h" |
21 #include "chrome/test/base/ui_test_utils.h" | 22 #include "chrome/test/base/ui_test_utils.h" |
22 #include "chromeos/chromeos_switches.h" | 23 #include "chromeos/chromeos_switches.h" |
23 #include "components/signin/core/browser/signin_manager_base.h" | 24 #include "components/signin/core/browser/signin_manager_base.h" |
24 #include "content/public/browser/browser_context.h" | 25 #include "content/public/browser/browser_context.h" |
25 #include "content/public/browser/download_item.h" | 26 #include "content/public/browser/download_item.h" |
26 #include "content/public/browser/download_manager.h" | 27 #include "content/public/browser/download_manager.h" |
| 28 #include "content/public/test/download_test_observer.h" |
27 #include "grit/theme_resources.h" | 29 #include "grit/theme_resources.h" |
28 #include "net/test/url_request/url_request_slow_download_job.h" | 30 #include "net/test/url_request/url_request_slow_download_job.h" |
29 #include "ui/base/l10n/l10n_util.h" | 31 #include "ui/base/l10n/l10n_util.h" |
30 #include "ui/message_center/message_center.h" | 32 #include "ui/message_center/message_center.h" |
31 #include "ui/message_center/message_center_observer.h" | 33 #include "ui/message_center/message_center_observer.h" |
32 #include "url/gurl.h" | 34 #include "url/gurl.h" |
33 | 35 |
34 namespace { | 36 namespace { |
35 | 37 |
36 enum { | 38 enum { |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 | 297 |
296 TestChromeDownloadManagerDelegate* GetIncognitoDownloadManagerDelegate() | 298 TestChromeDownloadManagerDelegate* GetIncognitoDownloadManagerDelegate() |
297 const { | 299 const { |
298 Profile* incognito_profile = incognito_browser()->profile(); | 300 Profile* incognito_profile = incognito_browser()->profile(); |
299 return static_cast<TestChromeDownloadManagerDelegate*>( | 301 return static_cast<TestChromeDownloadManagerDelegate*>( |
300 DownloadServiceFactory::GetForBrowserContext(incognito_profile)-> | 302 DownloadServiceFactory::GetForBrowserContext(incognito_profile)-> |
301 GetDownloadManagerDelegate()); | 303 GetDownloadManagerDelegate()); |
302 } | 304 } |
303 | 305 |
304 void CreateDownload() { | 306 void CreateDownload() { |
305 return CreateDownloadForBrowser(browser()); | 307 return CreateDownloadForBrowserAndURL( |
| 308 browser(), |
| 309 GURL(net::URLRequestSlowDownloadJob::kKnownSizeUrl)); |
306 } | 310 } |
307 | 311 |
308 void CreateDownloadForBrowser(Browser* browser) { | 312 void CreateDownloadForBrowserAndURL(Browser* browser, GURL url) { |
309 GURL url(net::URLRequestSlowDownloadJob::kKnownSizeUrl); | |
310 | |
311 // Starts a download. | 313 // Starts a download. |
312 NotificationAddObserver download_start_notification_observer; | 314 NotificationAddObserver download_start_notification_observer; |
313 ui_test_utils::NavigateToURL(browser, url); | 315 ui_test_utils::NavigateToURL(browser, url); |
314 EXPECT_TRUE(download_start_notification_observer.Wait()); | 316 EXPECT_TRUE(download_start_notification_observer.Wait()); |
315 | 317 |
316 // Confirms that a notification is created. | 318 // Confirms that a notification is created. |
317 notification_id_ = download_start_notification_observer.notification_id(); | 319 notification_id_ = download_start_notification_observer.notification_id(); |
318 EXPECT_FALSE(notification_id_.empty()); | 320 EXPECT_FALSE(notification_id_.empty()); |
319 ASSERT_TRUE(notification()); | 321 ASSERT_TRUE(notification()); |
320 | 322 |
(...skipping 10 matching lines...) Expand all Loading... |
331 download_item_ = downloads[0]; | 333 download_item_ = downloads[0]; |
332 ASSERT_TRUE(download_item_); | 334 ASSERT_TRUE(download_item_); |
333 } | 335 } |
334 | 336 |
335 content::DownloadItem* download_item() const { return download_item_; } | 337 content::DownloadItem* download_item() const { return download_item_; } |
336 std::string notification_id() const { return notification_id_; } | 338 std::string notification_id() const { return notification_id_; } |
337 message_center::Notification* notification() const { | 339 message_center::Notification* notification() const { |
338 return GetNotification(notification_id_); | 340 return GetNotification(notification_id_); |
339 } | 341 } |
340 Browser* incognito_browser() const { return incognito_browser_; } | 342 Browser* incognito_browser() const { return incognito_browser_; } |
| 343 base::FilePath GetDownloadPath() { |
| 344 return DownloadPrefs::FromDownloadManager(GetDownloadManager(browser()))-> |
| 345 DownloadPath(); |
| 346 } |
341 | 347 |
342 private: | 348 private: |
343 content::DownloadItem* download_item_ = nullptr; | 349 content::DownloadItem* download_item_ = nullptr; |
344 Browser* incognito_browser_ = nullptr; | 350 Browser* incognito_browser_ = nullptr; |
345 std::string notification_id_; | 351 std::string notification_id_; |
346 }; | 352 }; |
347 | 353 |
348 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, DownloadFile) { | 354 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, DownloadFile) { |
349 CreateDownload(); | 355 CreateDownload(); |
350 | 356 |
(...skipping 25 matching lines...) Expand all Loading... |
376 GetMessageCenter()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER); | 382 GetMessageCenter()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER); |
377 | 383 |
378 // Try to open the downloaded item by clicking the notification. | 384 // Try to open the downloaded item by clicking the notification. |
379 EXPECT_FALSE(GetDownloadManagerDelegate()->opened()); | 385 EXPECT_FALSE(GetDownloadManagerDelegate()->opened()); |
380 GetMessageCenter()->ClickOnNotification(notification_id()); | 386 GetMessageCenter()->ClickOnNotification(notification_id()); |
381 EXPECT_TRUE(GetDownloadManagerDelegate()->opened()); | 387 EXPECT_TRUE(GetDownloadManagerDelegate()->opened()); |
382 | 388 |
383 EXPECT_FALSE(GetNotification(notification_id())); | 389 EXPECT_FALSE(GetNotification(notification_id())); |
384 } | 390 } |
385 | 391 |
| 392 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, DownloadDangerousFile) { |
| 393 ASSERT_TRUE(test_server()->Start()); |
| 394 GURL download_url( |
| 395 test_server()->GetURL("files/downloads/dangerous/dangerous.swf")); |
| 396 |
| 397 content::DownloadTestObserverTerminal download_terminal_observer( |
| 398 GetDownloadManager(browser()), |
| 399 1u, /* wait_count */ |
| 400 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_IGNORE); |
| 401 |
| 402 CreateDownloadForBrowserAndURL(browser(), download_url); |
| 403 |
| 404 base::FilePath filename = download_item()->GetFileNameToReportUser(); |
| 405 |
| 406 // Checks the download status. |
| 407 EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, |
| 408 download_item()->GetDangerType()); |
| 409 EXPECT_TRUE(download_item()->IsDangerous()); |
| 410 |
| 411 // Opens the message center. |
| 412 GetMessageCenter()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER); |
| 413 |
| 414 NotificationRemoveObserver notification_close_observer; |
| 415 NotificationAddObserver notification_add_observer; |
| 416 |
| 417 // Cicks the "keep" button. |
| 418 notification()->ButtonClick(1); // 2nd button: "Keep" |
| 419 // Clicking makes the message center closed. |
| 420 GetMessageCenter()->SetVisibility(message_center::VISIBILITY_TRANSIENT); |
| 421 |
| 422 // Confirms that the notification is closed and re-shown. |
| 423 EXPECT_EQ(notification_id(), notification_close_observer.Wait()); |
| 424 notification_add_observer.Wait(); |
| 425 EXPECT_EQ(notification_id(), notification_add_observer.notification_id()); |
| 426 EXPECT_EQ(1u, GetMessageCenter()->GetVisibleNotifications().size()); |
| 427 |
| 428 // Checks the download status. |
| 429 EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED, |
| 430 download_item()->GetDangerType()); |
| 431 EXPECT_FALSE(download_item()->IsDangerous()); |
| 432 |
| 433 // Wait for the download completion. |
| 434 download_terminal_observer.WaitForFinished(); |
| 435 |
| 436 // Checks the download status. |
| 437 EXPECT_FALSE(download_item()->IsDangerous()); |
| 438 EXPECT_EQ(content::DownloadItem::COMPLETE, download_item()->GetState()); |
| 439 |
| 440 // Checks the downloaded file. |
| 441 EXPECT_TRUE(base::PathExists(GetDownloadPath().Append(filename.BaseName()))); |
| 442 } |
| 443 |
| 444 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, DiscardDangerousFile) { |
| 445 ASSERT_TRUE(test_server()->Start()); |
| 446 GURL download_url( |
| 447 test_server()->GetURL("files/downloads/dangerous/dangerous.swf")); |
| 448 |
| 449 content::DownloadTestObserverTerminal download_terminal_observer( |
| 450 GetDownloadManager(browser()), |
| 451 1u, /* wait_count */ |
| 452 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_IGNORE); |
| 453 |
| 454 CreateDownloadForBrowserAndURL(browser(), download_url); |
| 455 |
| 456 base::FilePath filename = download_item()->GetFileNameToReportUser(); |
| 457 |
| 458 // Checks the download status. |
| 459 EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, |
| 460 download_item()->GetDangerType()); |
| 461 EXPECT_TRUE(download_item()->IsDangerous()); |
| 462 |
| 463 // Opens the message center. |
| 464 GetMessageCenter()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER); |
| 465 |
| 466 NotificationRemoveObserver notification_close_observer; |
| 467 |
| 468 // Clicks the "Discard" button. |
| 469 notification()->ButtonClick(0); // 1st button: "Discard" |
| 470 // Clicking makes the message center closed. |
| 471 GetMessageCenter()->SetVisibility(message_center::VISIBILITY_TRANSIENT); |
| 472 |
| 473 // Confirms that the notification is closed. |
| 474 EXPECT_EQ(notification_id(), notification_close_observer.Wait()); |
| 475 |
| 476 // Wait for the download completion. |
| 477 download_terminal_observer.WaitForFinished(); |
| 478 |
| 479 // Checks there is neither any download nor any notification. |
| 480 EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size()); |
| 481 std::vector<content::DownloadItem*> downloads; |
| 482 GetDownloadManager(browser())->GetAllDownloads(&downloads); |
| 483 EXPECT_EQ(0u, downloads.size()); |
| 484 |
| 485 // Checks the downloaded file doesn't exist. |
| 486 EXPECT_FALSE(base::PathExists(GetDownloadPath().Append(filename.BaseName()))); |
| 487 } |
| 488 |
386 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, | 489 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, |
387 CloseNotificationAfterDownload) { | 490 CloseNotificationAfterDownload) { |
388 CreateDownload(); | 491 CreateDownload(); |
389 | 492 |
390 // Requests to complete the download. | 493 // Requests to complete the download. |
391 ui_test_utils::NavigateToURL( | 494 ui_test_utils::NavigateToURL( |
392 browser(), GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl)); | 495 browser(), GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl)); |
393 | 496 |
394 // Waits for download completion. | 497 // Waits for download completion. |
395 while (download_item()->GetState() != content::DownloadItem::COMPLETE) { | 498 while (download_item()->GetState() != content::DownloadItem::COMPLETE) { |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
560 std::vector<content::DownloadItem*> downloads; | 663 std::vector<content::DownloadItem*> downloads; |
561 GetDownloadManager(browser())->GetAllDownloads(&downloads); | 664 GetDownloadManager(browser())->GetAllDownloads(&downloads); |
562 EXPECT_EQ(1u, downloads.size()); | 665 EXPECT_EQ(1u, downloads.size()); |
563 EXPECT_EQ(content::DownloadItem::CANCELLED, downloads[0]->GetState()); | 666 EXPECT_EQ(content::DownloadItem::CANCELLED, downloads[0]->GetState()); |
564 } | 667 } |
565 | 668 |
566 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, IncognitoDownloadFile) { | 669 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, IncognitoDownloadFile) { |
567 PrepareIncognitoBrowser(); | 670 PrepareIncognitoBrowser(); |
568 | 671 |
569 // Starts an incognito download. | 672 // Starts an incognito download. |
570 CreateDownloadForBrowser(incognito_browser()); | 673 CreateDownloadForBrowserAndURL( |
| 674 incognito_browser(), |
| 675 GURL(net::URLRequestSlowDownloadJob::kKnownSizeUrl)); |
571 | 676 |
572 EXPECT_EQ(l10n_util::GetStringFUTF16( | 677 EXPECT_EQ(l10n_util::GetStringFUTF16( |
573 IDS_DOWNLOAD_STATUS_IN_PROGRESS_TITLE, | 678 IDS_DOWNLOAD_STATUS_IN_PROGRESS_TITLE, |
574 download_item()->GetFileNameToReportUser().LossyDisplayName()), | 679 download_item()->GetFileNameToReportUser().LossyDisplayName()), |
575 GetNotification(notification_id())->title()); | 680 GetNotification(notification_id())->title()); |
576 EXPECT_EQ(message_center::NOTIFICATION_TYPE_PROGRESS, | 681 EXPECT_EQ(message_center::NOTIFICATION_TYPE_PROGRESS, |
577 GetNotification(notification_id())->type()); | 682 GetNotification(notification_id())->type()); |
578 EXPECT_TRUE(download_item()->GetBrowserContext()->IsOffTheRecord()); | 683 EXPECT_TRUE(download_item()->GetBrowserContext()->IsOffTheRecord()); |
579 | 684 |
580 // Requests to complete the download. | 685 // Requests to complete the download. |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
825 } | 930 } |
826 | 931 |
827 // Confirms the types of download notifications are correct. | 932 // Confirms the types of download notifications are correct. |
828 EXPECT_EQ(message_center::NOTIFICATION_TYPE_SIMPLE, | 933 EXPECT_EQ(message_center::NOTIFICATION_TYPE_SIMPLE, |
829 GetNotification(notification_id1)->type()); | 934 GetNotification(notification_id1)->type()); |
830 EXPECT_EQ(message_center::NOTIFICATION_TYPE_SIMPLE, | 935 EXPECT_EQ(message_center::NOTIFICATION_TYPE_SIMPLE, |
831 GetNotification(notification_id2)->type()); | 936 GetNotification(notification_id2)->type()); |
832 EXPECT_EQ(message_center::NOTIFICATION_TYPE_SIMPLE, | 937 EXPECT_EQ(message_center::NOTIFICATION_TYPE_SIMPLE, |
833 GetNotification(notification_id3)->type()); | 938 GetNotification(notification_id3)->type()); |
834 } | 939 } |
OLD | NEW |