Chromium Code Reviews| 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_service.h" | 11 #include "chrome/browser/download/download_service.h" |
| 12 #include "chrome/browser/download/download_service_factory.h" | 12 #include "chrome/browser/download/download_service_factory.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/signin/signin_manager_factory.h" | 14 #include "chrome/browser/signin/signin_manager_factory.h" |
| 15 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
| 16 #include "chrome/browser/ui/browser_commands.h" | |
| 16 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
| 17 #include "chrome/grit/chromium_strings.h" | 18 #include "chrome/grit/chromium_strings.h" |
| 18 #include "chrome/grit/generated_resources.h" | 19 #include "chrome/grit/generated_resources.h" |
| 19 #include "chrome/test/base/in_process_browser_test.h" | 20 #include "chrome/test/base/in_process_browser_test.h" |
| 20 #include "chrome/test/base/ui_test_utils.h" | 21 #include "chrome/test/base/ui_test_utils.h" |
| 21 #include "chromeos/chromeos_switches.h" | 22 #include "chromeos/chromeos_switches.h" |
| 22 #include "components/signin/core/browser/signin_manager_base.h" | 23 #include "components/signin/core/browser/signin_manager_base.h" |
| 23 #include "content/public/browser/browser_context.h" | 24 #include "content/public/browser/browser_context.h" |
| 24 #include "content/public/browser/download_item.h" | 25 #include "content/public/browser/download_item.h" |
| 25 #include "content/public/browser/download_manager.h" | 26 #include "content/public/browser/download_manager.h" |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 262 public: | 263 public: |
| 263 ~DownloadNotificationTest() override {} | 264 ~DownloadNotificationTest() override {} |
| 264 | 265 |
| 265 void SetUpOnMainThread() override { | 266 void SetUpOnMainThread() override { |
| 266 Profile* profile = browser()->profile(); | 267 Profile* profile = browser()->profile(); |
| 267 | 268 |
| 268 scoped_ptr<TestChromeDownloadManagerDelegate> test_delegate; | 269 scoped_ptr<TestChromeDownloadManagerDelegate> test_delegate; |
| 269 test_delegate.reset(new TestChromeDownloadManagerDelegate(profile)); | 270 test_delegate.reset(new TestChromeDownloadManagerDelegate(profile)); |
| 270 test_delegate->GetDownloadIdReceiverCallback().Run( | 271 test_delegate->GetDownloadIdReceiverCallback().Run( |
| 271 content::DownloadItem::kInvalidId + 1); | 272 content::DownloadItem::kInvalidId + 1); |
| 272 | |
| 273 DownloadServiceFactory::GetForBrowserContext(profile) | 273 DownloadServiceFactory::GetForBrowserContext(profile) |
| 274 ->SetDownloadManagerDelegateForTesting(test_delegate.Pass()); | 274 ->SetDownloadManagerDelegateForTesting(test_delegate.Pass()); |
| 275 | 275 |
| 276 DownloadNotificationTestBase::SetUpOnMainThread(); | 276 DownloadNotificationTestBase::SetUpOnMainThread(); |
| 277 } | 277 } |
| 278 | 278 |
| 279 TestChromeDownloadManagerDelegate* GetDownloadManagerDelegate() const { | 279 TestChromeDownloadManagerDelegate* GetDownloadManagerDelegate() const { |
| 280 return static_cast<TestChromeDownloadManagerDelegate*>( | 280 return static_cast<TestChromeDownloadManagerDelegate*>( |
| 281 DownloadServiceFactory::GetForBrowserContext(browser()->profile()) | 281 DownloadServiceFactory::GetForBrowserContext(browser()->profile()) |
| 282 ->GetDownloadManagerDelegate()); | 282 ->GetDownloadManagerDelegate()); |
| 283 } | 283 } |
| 284 | 284 |
| 285 void PrepareIncognitoBrowser() { | |
| 286 incognito_browser_ = CreateIncognitoBrowser(); | |
| 287 Profile* incognito_profile = incognito_browser_->profile(); | |
| 288 | |
| 289 scoped_ptr<TestChromeDownloadManagerDelegate> incognito_test_delegate; | |
| 290 incognito_test_delegate.reset( | |
| 291 new TestChromeDownloadManagerDelegate(incognito_profile)); | |
| 292 incognito_test_delegate->GetDownloadIdReceiverCallback().Run( | |
| 293 content::DownloadItem::kInvalidId + 1); | |
| 294 DownloadServiceFactory::GetForBrowserContext(incognito_profile) | |
| 295 ->SetDownloadManagerDelegateForTesting(incognito_test_delegate.Pass()); | |
| 296 } | |
| 297 | |
| 298 TestChromeDownloadManagerDelegate* GetIncognitoDownloadManagerDelegate() | |
| 299 const { | |
| 300 Profile* incognito_profile = incognito_browser()->profile(); | |
| 301 return static_cast<TestChromeDownloadManagerDelegate*>( | |
| 302 DownloadServiceFactory::GetForBrowserContext(incognito_profile)-> | |
| 303 GetDownloadManagerDelegate()); | |
| 304 } | |
| 305 | |
| 285 void CreateDownload() { | 306 void CreateDownload() { |
| 307 return CreateDownloadForBrowser(browser()); | |
| 308 } | |
| 309 | |
| 310 void CreateDownloadForBrowser(Browser* browser) { | |
| 286 GURL url(net::URLRequestSlowDownloadJob::kKnownSizeUrl); | 311 GURL url(net::URLRequestSlowDownloadJob::kKnownSizeUrl); |
| 287 | 312 |
| 288 // Starts a download. | 313 // Starts a download. |
| 289 NotificationAddObserver download_start_notification_observer; | 314 NotificationAddObserver download_start_notification_observer; |
| 290 ui_test_utils::NavigateToURL(browser(), url); | 315 ui_test_utils::NavigateToURL(browser, url); |
| 291 EXPECT_TRUE(download_start_notification_observer.Wait()); | 316 EXPECT_TRUE(download_start_notification_observer.Wait()); |
| 292 | 317 |
| 293 // Confirms that a notification is created. | 318 // Confirms that a notification is created. |
| 294 notification_id_ = download_start_notification_observer.notification_id(); | 319 notification_id_ = download_start_notification_observer.notification_id(); |
| 295 EXPECT_FALSE(notification_id_.empty()); | 320 EXPECT_FALSE(notification_id_.empty()); |
| 296 ASSERT_TRUE(notification()); | 321 ASSERT_TRUE(notification()); |
| 297 | 322 |
| 298 // Confirms that there is only one notification. | 323 // Confirms that there is only one notification. |
| 299 message_center::NotificationList::Notifications | 324 message_center::NotificationList::Notifications |
| 300 visible_notifications = GetMessageCenter()->GetVisibleNotifications(); | 325 visible_notifications = GetMessageCenter()->GetVisibleNotifications(); |
| 301 EXPECT_EQ(1u, visible_notifications.size()); | 326 EXPECT_EQ(1u, visible_notifications.size()); |
| 302 EXPECT_TRUE(IsInNotifications(visible_notifications, notification_id_)); | 327 EXPECT_TRUE(IsInNotifications(visible_notifications, notification_id_)); |
| 303 | 328 |
| 304 // Confirms that a download is also started. | 329 // Confirms that a download is also started. |
| 305 std::vector<content::DownloadItem*> downloads; | 330 std::vector<content::DownloadItem*> downloads; |
| 306 GetDownloadManager(browser())->GetAllDownloads(&downloads); | 331 GetDownloadManager(browser)->GetAllDownloads(&downloads); |
| 307 EXPECT_EQ(1u, downloads.size()); | 332 EXPECT_EQ(1u, downloads.size()); |
| 308 download_item_ = downloads[0]; | 333 download_item_ = downloads[0]; |
| 309 ASSERT_TRUE(download_item_); | 334 ASSERT_TRUE(download_item_); |
| 310 } | 335 } |
| 311 | 336 |
| 312 content::DownloadItem* download_item() const { return download_item_; } | 337 content::DownloadItem* download_item() const { return download_item_; } |
| 313 std::string notification_id() const { return notification_id_; } | 338 std::string notification_id() const { return notification_id_; } |
| 314 message_center::Notification* notification() const { | 339 message_center::Notification* notification() const { |
| 315 return GetNotification(notification_id_); | 340 return GetNotification(notification_id_); |
| 316 } | 341 } |
| 342 Browser* incognito_browser() const { return incognito_browser_; } | |
| 317 | 343 |
| 318 private: | 344 private: |
| 319 content::DownloadItem* download_item_ = nullptr; | 345 content::DownloadItem* download_item_ = nullptr; |
| 346 Browser* incognito_browser_ = nullptr; | |
| 320 std::string notification_id_; | 347 std::string notification_id_; |
| 321 }; | 348 }; |
| 322 | 349 |
| 323 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, DownloadFile) { | 350 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, DownloadFile) { |
| 324 CreateDownload(); | 351 CreateDownload(); |
| 325 | 352 |
| 326 EXPECT_EQ(l10n_util::GetStringFUTF16( | 353 EXPECT_EQ(l10n_util::GetStringFUTF16( |
| 327 IDS_DOWNLOAD_STATUS_IN_PROGRESS_TITLE, | 354 IDS_DOWNLOAD_STATUS_IN_PROGRESS_TITLE, |
| 328 download_item()->GetFileNameToReportUser().LossyDisplayName()), | 355 download_item()->GetFileNameToReportUser().LossyDisplayName()), |
| 329 GetNotification(notification_id())->title()); | 356 GetNotification(notification_id())->title()); |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 531 EXPECT_EQ(notification_id(), notification_close_observer.Wait()); | 558 EXPECT_EQ(notification_id(), notification_close_observer.Wait()); |
| 532 EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size()); | 559 EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size()); |
| 533 | 560 |
| 534 // Confirms that a download is also cancelled. | 561 // Confirms that a download is also cancelled. |
| 535 std::vector<content::DownloadItem*> downloads; | 562 std::vector<content::DownloadItem*> downloads; |
| 536 GetDownloadManager(browser())->GetAllDownloads(&downloads); | 563 GetDownloadManager(browser())->GetAllDownloads(&downloads); |
| 537 EXPECT_EQ(1u, downloads.size()); | 564 EXPECT_EQ(1u, downloads.size()); |
| 538 EXPECT_EQ(content::DownloadItem::CANCELLED, downloads[0]->GetState()); | 565 EXPECT_EQ(content::DownloadItem::CANCELLED, downloads[0]->GetState()); |
| 539 } | 566 } |
| 540 | 567 |
| 568 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, IncognitoDownloadFile) { | |
| 569 PrepareIncognitoBrowser(); | |
| 570 | |
| 571 // Starts an incognito download. | |
| 572 CreateDownloadForBrowser(incognito_browser()); | |
| 573 | |
| 574 EXPECT_EQ(l10n_util::GetStringFUTF16( | |
| 575 IDS_DOWNLOAD_STATUS_IN_PROGRESS_TITLE, | |
| 576 download_item()->GetFileNameToReportUser().LossyDisplayName()), | |
| 577 GetNotification(notification_id())->title()); | |
| 578 EXPECT_EQ(message_center::NOTIFICATION_TYPE_PROGRESS, | |
| 579 GetNotification(notification_id())->type()); | |
| 580 EXPECT_TRUE(download_item()->GetBrowserContext()->IsOffTheRecord()); | |
| 581 | |
| 582 // Requests to complete the download. | |
| 583 ui_test_utils::NavigateToURL( | |
| 584 incognito_browser(), | |
| 585 GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl)); | |
| 586 | |
| 587 // Waits for download completion. | |
| 588 while (download_item()->GetState() != content::DownloadItem::COMPLETE) { | |
| 589 NotificationUpdateObserver download_change_notification_observer; | |
| 590 download_change_notification_observer.Wait(); | |
|
asanka
2015/05/13 15:10:30
Remind me again why we aren't using something like
yoshiki
2015/05/13 17:03:04
I thought we can write it with DownloadTestObserve
| |
| 591 } | |
| 592 | |
| 593 EXPECT_EQ(l10n_util::GetStringFUTF16( | |
| 594 IDS_DOWNLOAD_STATUS_DOWNLOADED_TITLE, | |
| 595 download_item()->GetFileNameToReportUser().LossyDisplayName()), | |
| 596 GetNotification(notification_id())->title()); | |
| 597 EXPECT_EQ(message_center::NOTIFICATION_TYPE_SIMPLE, | |
| 598 GetNotification(notification_id())->type()); | |
| 599 | |
| 600 // Opens the message center. | |
| 601 GetMessageCenter()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER); | |
| 602 | |
| 603 // Try to open the downloaded item by clicking the notification. | |
| 604 EXPECT_FALSE(GetIncognitoDownloadManagerDelegate()->opened()); | |
| 605 GetMessageCenter()->ClickOnNotification(notification_id()); | |
| 606 EXPECT_TRUE(GetIncognitoDownloadManagerDelegate()->opened()); | |
| 607 EXPECT_FALSE(GetDownloadManagerDelegate()->opened()); | |
| 608 | |
| 609 EXPECT_FALSE(GetNotification(notification_id())); | |
| 610 chrome::CloseWindow(incognito_browser()); | |
| 611 } | |
| 612 | |
| 613 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, | |
| 614 SimultaneousIncognitoAndNormalDownloads) { | |
| 615 PrepareIncognitoBrowser(); | |
| 616 | |
| 617 GURL url_ingocnito(net::URLRequestSlowDownloadJob::kUnknownSizeUrl); | |
| 618 GURL url_normal(net::URLRequestSlowDownloadJob::kKnownSizeUrl); | |
| 619 | |
| 620 // Starts the incognito download. | |
| 621 NotificationAddObserver download_start_notification_observer1; | |
| 622 ui_test_utils::NavigateToURL(incognito_browser(), url_incognito); | |
| 623 EXPECT_TRUE(download_start_notification_observer1.Wait()); | |
| 624 std::string notification_id1 = | |
| 625 download_start_notification_observer1.notification_id(); | |
| 626 EXPECT_FALSE(notification_id1.empty()); | |
| 627 | |
| 628 // Confirms that there is a download. | |
| 629 std::vector<content::DownloadItem*> downloads; | |
| 630 GetDownloadManager(browser())->GetAllDownloads(&downloads); | |
| 631 EXPECT_EQ(0u, downloads.size()); | |
| 632 downloads.clear(); | |
| 633 GetDownloadManager(incognito_browser())->GetAllDownloads(&downloads); | |
| 634 EXPECT_EQ(1u, downloads.size()); | |
| 635 content::DownloadItem* download_incognito = downloads[0]; | |
| 636 | |
| 637 // Starts the normal download. | |
| 638 NotificationAddObserver download_start_notification_observer2; | |
| 639 ui_test_utils::NavigateToURL(browser(), url_normal); | |
| 640 EXPECT_TRUE(download_start_notification_observer2.Wait()); | |
| 641 std::string notification_id2 = | |
| 642 download_start_notification_observer2.notification_id(); | |
| 643 EXPECT_FALSE(notification_id2.empty()); | |
| 644 | |
| 645 // Confirms that there are 2 downloads. | |
| 646 downloads.clear(); | |
| 647 GetDownloadManager(browser())->GetAllDownloads(&downloads); | |
| 648 content::DownloadItem* download_normal = downloads[0]; | |
| 649 EXPECT_EQ(1u, downloads.size()); | |
| 650 EXPECT_NE(download_normal, download_incognito); | |
| 651 downloads.clear(); | |
| 652 GetDownloadManager(incognito_browser())->GetAllDownloads(&downloads); | |
| 653 EXPECT_EQ(1u, downloads.size()); | |
| 654 EXPECT_EQ(download_incognito, downloads[0]); | |
| 655 | |
| 656 // Confirms the types of download notifications are correct. | |
| 657 EXPECT_EQ(message_center::NOTIFICATION_TYPE_PROGRESS, | |
| 658 GetNotification(notification_id1)->type()); | |
| 659 EXPECT_EQ(message_center::NOTIFICATION_TYPE_PROGRESS, | |
| 660 GetNotification(notification_id2)->type()); | |
| 661 | |
| 662 EXPECT_TRUE(download_incognito->GetBrowserContext()->IsOffTheRecord()); | |
| 663 EXPECT_FALSE(download_normal->GetBrowserContext()->IsOffTheRecord()); | |
| 664 | |
| 665 // Requests to complete the downloads. | |
| 666 ui_test_utils::NavigateToURL( | |
| 667 browser(), GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl)); | |
| 668 | |
| 669 // Waits for the completion of downloads. | |
| 670 while (download_normal->GetState() != content::DownloadItem::COMPLETE || | |
| 671 download_incognito->GetState() != content::DownloadItem::COMPLETE) { | |
| 672 NotificationUpdateObserver download_change_notification_observer; | |
| 673 download_change_notification_observer.Wait(); | |
| 674 } | |
| 675 | |
| 676 // Confirms the types of download notifications are correct. | |
| 677 EXPECT_EQ(message_center::NOTIFICATION_TYPE_SIMPLE, | |
| 678 GetNotification(notification_id1)->type()); | |
| 679 EXPECT_EQ(message_center::NOTIFICATION_TYPE_SIMPLE, | |
| 680 GetNotification(notification_id2)->type()); | |
| 681 | |
| 682 chrome::CloseWindow(incognito_browser()); | |
| 683 } | |
| 684 | |
| 541 ////////////////////////////////////////////////// | 685 ////////////////////////////////////////////////// |
| 542 // Test with multi profiles | 686 // Test with multi profiles |
| 543 ////////////////////////////////////////////////// | 687 ////////////////////////////////////////////////// |
| 544 | 688 |
| 545 class MultiProfileDownloadNotificationTest | 689 class MultiProfileDownloadNotificationTest |
| 546 : public DownloadNotificationTestBase { | 690 : public DownloadNotificationTestBase { |
| 547 public: | 691 public: |
| 548 ~MultiProfileDownloadNotificationTest() override {} | 692 ~MultiProfileDownloadNotificationTest() override {} |
| 549 | 693 |
| 550 void SetUpCommandLine(base::CommandLine* command_line) override { | 694 void SetUpCommandLine(base::CommandLine* command_line) override { |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 683 } | 827 } |
| 684 | 828 |
| 685 // Confirms the types of download notifications are correct. | 829 // Confirms the types of download notifications are correct. |
| 686 EXPECT_EQ(message_center::NOTIFICATION_TYPE_SIMPLE, | 830 EXPECT_EQ(message_center::NOTIFICATION_TYPE_SIMPLE, |
| 687 GetNotification(notification_id1)->type()); | 831 GetNotification(notification_id1)->type()); |
| 688 EXPECT_EQ(message_center::NOTIFICATION_TYPE_SIMPLE, | 832 EXPECT_EQ(message_center::NOTIFICATION_TYPE_SIMPLE, |
| 689 GetNotification(notification_id2)->type()); | 833 GetNotification(notification_id2)->type()); |
| 690 EXPECT_EQ(message_center::NOTIFICATION_TYPE_SIMPLE, | 834 EXPECT_EQ(message_center::NOTIFICATION_TYPE_SIMPLE, |
| 691 GetNotification(notification_id3)->type()); | 835 GetNotification(notification_id3)->type()); |
| 692 } | 836 } |
| OLD | NEW |