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 DownloadServiceFactory::GetForBrowserContext(incognito_profile) |
| 293 ->SetDownloadManagerDelegateForTesting(incognito_test_delegate.Pass()); |
| 294 } |
| 295 |
| 296 TestChromeDownloadManagerDelegate* GetIncognitoDownloadManagerDelegate() |
| 297 const { |
| 298 Profile* incognito_profile = incognito_browser()->profile(); |
| 299 return static_cast<TestChromeDownloadManagerDelegate*>( |
| 300 DownloadServiceFactory::GetForBrowserContext(incognito_profile)-> |
| 301 GetDownloadManagerDelegate()); |
| 302 } |
| 303 |
285 void CreateDownload() { | 304 void CreateDownload() { |
| 305 return CreateDownloadForBrowser(browser()); |
| 306 } |
| 307 |
| 308 void CreateDownloadForBrowser(Browser* browser) { |
286 GURL url(net::URLRequestSlowDownloadJob::kKnownSizeUrl); | 309 GURL url(net::URLRequestSlowDownloadJob::kKnownSizeUrl); |
287 | 310 |
288 // Starts a download. | 311 // Starts a download. |
289 NotificationAddObserver download_start_notification_observer; | 312 NotificationAddObserver download_start_notification_observer; |
290 ui_test_utils::NavigateToURL(browser(), url); | 313 ui_test_utils::NavigateToURL(browser, url); |
291 EXPECT_TRUE(download_start_notification_observer.Wait()); | 314 EXPECT_TRUE(download_start_notification_observer.Wait()); |
292 | 315 |
293 // Confirms that a notification is created. | 316 // Confirms that a notification is created. |
294 notification_id_ = download_start_notification_observer.notification_id(); | 317 notification_id_ = download_start_notification_observer.notification_id(); |
295 EXPECT_FALSE(notification_id_.empty()); | 318 EXPECT_FALSE(notification_id_.empty()); |
296 ASSERT_TRUE(notification()); | 319 ASSERT_TRUE(notification()); |
297 | 320 |
298 // Confirms that there is only one notification. | 321 // Confirms that there is only one notification. |
299 message_center::NotificationList::Notifications | 322 message_center::NotificationList::Notifications |
300 visible_notifications = GetMessageCenter()->GetVisibleNotifications(); | 323 visible_notifications = GetMessageCenter()->GetVisibleNotifications(); |
301 EXPECT_EQ(1u, visible_notifications.size()); | 324 EXPECT_EQ(1u, visible_notifications.size()); |
302 EXPECT_TRUE(IsInNotifications(visible_notifications, notification_id_)); | 325 EXPECT_TRUE(IsInNotifications(visible_notifications, notification_id_)); |
303 | 326 |
304 // Confirms that a download is also started. | 327 // Confirms that a download is also started. |
305 std::vector<content::DownloadItem*> downloads; | 328 std::vector<content::DownloadItem*> downloads; |
306 GetDownloadManager(browser())->GetAllDownloads(&downloads); | 329 GetDownloadManager(browser)->GetAllDownloads(&downloads); |
307 EXPECT_EQ(1u, downloads.size()); | 330 EXPECT_EQ(1u, downloads.size()); |
308 download_item_ = downloads[0]; | 331 download_item_ = downloads[0]; |
309 ASSERT_TRUE(download_item_); | 332 ASSERT_TRUE(download_item_); |
310 } | 333 } |
311 | 334 |
312 content::DownloadItem* download_item() const { return download_item_; } | 335 content::DownloadItem* download_item() const { return download_item_; } |
313 std::string notification_id() const { return notification_id_; } | 336 std::string notification_id() const { return notification_id_; } |
314 message_center::Notification* notification() const { | 337 message_center::Notification* notification() const { |
315 return GetNotification(notification_id_); | 338 return GetNotification(notification_id_); |
316 } | 339 } |
| 340 Browser* incognito_browser() const { return incognito_browser_; } |
317 | 341 |
318 private: | 342 private: |
319 content::DownloadItem* download_item_ = nullptr; | 343 content::DownloadItem* download_item_ = nullptr; |
| 344 Browser* incognito_browser_ = nullptr; |
320 std::string notification_id_; | 345 std::string notification_id_; |
321 }; | 346 }; |
322 | 347 |
323 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, DownloadFile) { | 348 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, DownloadFile) { |
324 CreateDownload(); | 349 CreateDownload(); |
325 | 350 |
326 EXPECT_EQ(l10n_util::GetStringFUTF16( | 351 EXPECT_EQ(l10n_util::GetStringFUTF16( |
327 IDS_DOWNLOAD_STATUS_IN_PROGRESS_TITLE, | 352 IDS_DOWNLOAD_STATUS_IN_PROGRESS_TITLE, |
328 download_item()->GetFileNameToReportUser().LossyDisplayName()), | 353 download_item()->GetFileNameToReportUser().LossyDisplayName()), |
329 GetNotification(notification_id())->title()); | 354 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()); | 556 EXPECT_EQ(notification_id(), notification_close_observer.Wait()); |
532 EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size()); | 557 EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size()); |
533 | 558 |
534 // Confirms that a download is also cancelled. | 559 // Confirms that a download is also cancelled. |
535 std::vector<content::DownloadItem*> downloads; | 560 std::vector<content::DownloadItem*> downloads; |
536 GetDownloadManager(browser())->GetAllDownloads(&downloads); | 561 GetDownloadManager(browser())->GetAllDownloads(&downloads); |
537 EXPECT_EQ(1u, downloads.size()); | 562 EXPECT_EQ(1u, downloads.size()); |
538 EXPECT_EQ(content::DownloadItem::CANCELLED, downloads[0]->GetState()); | 563 EXPECT_EQ(content::DownloadItem::CANCELLED, downloads[0]->GetState()); |
539 } | 564 } |
540 | 565 |
| 566 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, IncognitoDownloadFile) { |
| 567 PrepareIncognitoBrowser(); |
| 568 |
| 569 // Starts an incognito download. |
| 570 CreateDownloadForBrowser(incognito_browser()); |
| 571 |
| 572 EXPECT_EQ(l10n_util::GetStringFUTF16( |
| 573 IDS_DOWNLOAD_STATUS_IN_PROGRESS_TITLE, |
| 574 download_item()->GetFileNameToReportUser().LossyDisplayName()), |
| 575 GetNotification(notification_id())->title()); |
| 576 EXPECT_EQ(message_center::NOTIFICATION_TYPE_PROGRESS, |
| 577 GetNotification(notification_id())->type()); |
| 578 EXPECT_TRUE(download_item()->GetBrowserContext()->IsOffTheRecord()); |
| 579 |
| 580 // Requests to complete the download. |
| 581 ui_test_utils::NavigateToURL( |
| 582 incognito_browser(), |
| 583 GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl)); |
| 584 |
| 585 // Waits for download completion. |
| 586 while (download_item()->GetState() != content::DownloadItem::COMPLETE) { |
| 587 NotificationUpdateObserver download_change_notification_observer; |
| 588 download_change_notification_observer.Wait(); |
| 589 } |
| 590 |
| 591 EXPECT_EQ(l10n_util::GetStringFUTF16( |
| 592 IDS_DOWNLOAD_STATUS_DOWNLOADED_TITLE, |
| 593 download_item()->GetFileNameToReportUser().LossyDisplayName()), |
| 594 GetNotification(notification_id())->title()); |
| 595 EXPECT_EQ(message_center::NOTIFICATION_TYPE_SIMPLE, |
| 596 GetNotification(notification_id())->type()); |
| 597 |
| 598 // Opens the message center. |
| 599 GetMessageCenter()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER); |
| 600 |
| 601 // Try to open the downloaded item by clicking the notification. |
| 602 EXPECT_FALSE(GetIncognitoDownloadManagerDelegate()->opened()); |
| 603 GetMessageCenter()->ClickOnNotification(notification_id()); |
| 604 EXPECT_TRUE(GetIncognitoDownloadManagerDelegate()->opened()); |
| 605 EXPECT_FALSE(GetDownloadManagerDelegate()->opened()); |
| 606 |
| 607 EXPECT_FALSE(GetNotification(notification_id())); |
| 608 chrome::CloseWindow(incognito_browser()); |
| 609 } |
| 610 |
| 611 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, |
| 612 SimultaneousIncognitoAndNormalDownloads) { |
| 613 PrepareIncognitoBrowser(); |
| 614 |
| 615 GURL url_incognito(net::URLRequestSlowDownloadJob::kUnknownSizeUrl); |
| 616 GURL url_normal(net::URLRequestSlowDownloadJob::kKnownSizeUrl); |
| 617 |
| 618 // Starts the incognito download. |
| 619 NotificationAddObserver download_start_notification_observer1; |
| 620 ui_test_utils::NavigateToURL(incognito_browser(), url_incognito); |
| 621 EXPECT_TRUE(download_start_notification_observer1.Wait()); |
| 622 std::string notification_id1 = |
| 623 download_start_notification_observer1.notification_id(); |
| 624 EXPECT_FALSE(notification_id1.empty()); |
| 625 |
| 626 // Confirms that there is a download. |
| 627 std::vector<content::DownloadItem*> downloads; |
| 628 GetDownloadManager(browser())->GetAllDownloads(&downloads); |
| 629 EXPECT_EQ(0u, downloads.size()); |
| 630 downloads.clear(); |
| 631 GetDownloadManager(incognito_browser())->GetAllDownloads(&downloads); |
| 632 EXPECT_EQ(1u, downloads.size()); |
| 633 content::DownloadItem* download_incognito = downloads[0]; |
| 634 |
| 635 // Starts the normal download. |
| 636 NotificationAddObserver download_start_notification_observer2; |
| 637 ui_test_utils::NavigateToURL(browser(), url_normal); |
| 638 EXPECT_TRUE(download_start_notification_observer2.Wait()); |
| 639 std::string notification_id2 = |
| 640 download_start_notification_observer2.notification_id(); |
| 641 EXPECT_FALSE(notification_id2.empty()); |
| 642 |
| 643 // Confirms that there are 2 downloads. |
| 644 downloads.clear(); |
| 645 GetDownloadManager(browser())->GetAllDownloads(&downloads); |
| 646 content::DownloadItem* download_normal = downloads[0]; |
| 647 EXPECT_EQ(1u, downloads.size()); |
| 648 EXPECT_NE(download_normal, download_incognito); |
| 649 downloads.clear(); |
| 650 GetDownloadManager(incognito_browser())->GetAllDownloads(&downloads); |
| 651 EXPECT_EQ(1u, downloads.size()); |
| 652 EXPECT_EQ(download_incognito, downloads[0]); |
| 653 |
| 654 // Confirms the types of download notifications are correct. |
| 655 EXPECT_EQ(message_center::NOTIFICATION_TYPE_PROGRESS, |
| 656 GetNotification(notification_id1)->type()); |
| 657 EXPECT_EQ(message_center::NOTIFICATION_TYPE_PROGRESS, |
| 658 GetNotification(notification_id2)->type()); |
| 659 |
| 660 EXPECT_TRUE(download_incognito->GetBrowserContext()->IsOffTheRecord()); |
| 661 EXPECT_FALSE(download_normal->GetBrowserContext()->IsOffTheRecord()); |
| 662 |
| 663 // Requests to complete the downloads. |
| 664 ui_test_utils::NavigateToURL( |
| 665 browser(), GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl)); |
| 666 |
| 667 // Waits for the completion of downloads. |
| 668 while (download_normal->GetState() != content::DownloadItem::COMPLETE || |
| 669 download_incognito->GetState() != content::DownloadItem::COMPLETE) { |
| 670 NotificationUpdateObserver download_change_notification_observer; |
| 671 download_change_notification_observer.Wait(); |
| 672 } |
| 673 |
| 674 // Confirms the types of download notifications are correct. |
| 675 EXPECT_EQ(message_center::NOTIFICATION_TYPE_SIMPLE, |
| 676 GetNotification(notification_id1)->type()); |
| 677 EXPECT_EQ(message_center::NOTIFICATION_TYPE_SIMPLE, |
| 678 GetNotification(notification_id2)->type()); |
| 679 |
| 680 chrome::CloseWindow(incognito_browser()); |
| 681 } |
| 682 |
541 ////////////////////////////////////////////////// | 683 ////////////////////////////////////////////////// |
542 // Test with multi profiles | 684 // Test with multi profiles |
543 ////////////////////////////////////////////////// | 685 ////////////////////////////////////////////////// |
544 | 686 |
545 class MultiProfileDownloadNotificationTest | 687 class MultiProfileDownloadNotificationTest |
546 : public DownloadNotificationTestBase { | 688 : public DownloadNotificationTestBase { |
547 public: | 689 public: |
548 ~MultiProfileDownloadNotificationTest() override {} | 690 ~MultiProfileDownloadNotificationTest() override {} |
549 | 691 |
550 void SetUpCommandLine(base::CommandLine* command_line) override { | 692 void SetUpCommandLine(base::CommandLine* command_line) override { |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
683 } | 825 } |
684 | 826 |
685 // Confirms the types of download notifications are correct. | 827 // Confirms the types of download notifications are correct. |
686 EXPECT_EQ(message_center::NOTIFICATION_TYPE_SIMPLE, | 828 EXPECT_EQ(message_center::NOTIFICATION_TYPE_SIMPLE, |
687 GetNotification(notification_id1)->type()); | 829 GetNotification(notification_id1)->type()); |
688 EXPECT_EQ(message_center::NOTIFICATION_TYPE_SIMPLE, | 830 EXPECT_EQ(message_center::NOTIFICATION_TYPE_SIMPLE, |
689 GetNotification(notification_id2)->type()); | 831 GetNotification(notification_id2)->type()); |
690 EXPECT_EQ(message_center::NOTIFICATION_TYPE_SIMPLE, | 832 EXPECT_EQ(message_center::NOTIFICATION_TYPE_SIMPLE, |
691 GetNotification(notification_id3)->type()); | 833 GetNotification(notification_id3)->type()); |
692 } | 834 } |
OLD | NEW |