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
3 // found in the LICENSE file.
5 #include "base/command_line.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/path_service.h"
8 #include "base/run_loop.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/chromeos/profiles/profile_helper.h"
11 #include "chrome/browser/download/chrome_download_manager_delegate.h"
12 #include "chrome/browser/download/download_prefs.h"
13 #include "chrome/browser/download/download_service.h"
14 #include "chrome/browser/download/download_service_factory.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/signin/signin_manager_factory.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_commands.h"
19 #include "chrome/common/chrome_paths.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/grit/chromium_strings.h"
22 #include "chrome/grit/generated_resources.h"
23 #include "chrome/test/base/in_process_browser_test.h"
24 #include "chrome/test/base/ui_test_utils.h"
25 #include "chromeos/chromeos_switches.h"
26 #include "components/signin/core/browser/signin_manager_base.h"
27 #include "content/public/browser/browser_context.h"
28 #include "content/public/browser/download_item.h"
29 #include "content/public/browser/download_manager.h"
30 #include "content/public/test/download_test_observer.h"
31 #include "grit/theme_resources.h"
32 #include "net/test/embedded_test_server/embedded_test_server.h"
33 #include "net/test/url_request/url_request_slow_download_job.h"
34 #include "ui/base/l10n/l10n_util.h"
35 #include "ui/message_center/message_center.h"
36 #include "ui/message_center/message_center_observer.h"
42 DUMMY_ACCOUNT_INDEX
= 0,
43 PRIMARY_ACCOUNT_INDEX
= 1,
44 SECONDARY_ACCOUNT_INDEX_START
= 2,
47 // Structure to describe an account info.
48 struct TestAccountInfo
{
49 const char* const email
;
50 const char* const gaia_id
;
51 const char* const hash
;
52 const char* const display_name
;
55 // Accounts for multi profile test.
56 static const TestAccountInfo kTestAccounts
[] = {
57 {"__dummy__@invalid.domain", "10000", "hashdummy", "Dummy Account"},
58 {"alice@invalid.domain", "10001", "hashalice", "Alice"},
59 {"bob@invalid.domain", "10002", "hashbobbo", "Bob"},
60 {"charlie@invalid.domain", "10003", "hashcharl", "Charlie"},
63 bool IsInNotifications(
64 const message_center::NotificationList::Notifications
& notifications
,
65 const std::string
& id
) {
66 for (const auto& notification
: notifications
) {
67 if (notification
->id() == id
)
73 // Base class observing notification events.
74 class MessageCenterChangeObserver
75 : public message_center::MessageCenterObserver
{
77 MessageCenterChangeObserver() {
78 message_center::MessageCenter::Get()->AddObserver(this);
81 ~MessageCenterChangeObserver() override
{
82 message_center::MessageCenter::Get()->RemoveObserver(this);
87 base::MessageLoop::ScopedNestableTaskAllower
allow(
88 base::MessageLoop::current());
97 run_loop_
.reset(new base::RunLoop
);
101 scoped_ptr
<base::RunLoop
> run_loop_
;
103 DISALLOW_COPY_AND_ASSIGN(MessageCenterChangeObserver
);
106 // Class observing of "ADD" notification events.
107 class NotificationAddObserver
: public MessageCenterChangeObserver
{
109 NotificationAddObserver() : count_(1) {}
110 explicit NotificationAddObserver(int count
) : count_(count
) {
111 MessageCenterChangeObserver();
113 ~NotificationAddObserver() override
{}
126 // message_center::MessageCenterObserver:
127 void OnNotificationAdded(const std::string
& notification_id
) override
{
130 notification_ids_
.push_back(notification_id
);
132 if (waiting_
&& count_
== 0)
136 const std::string
& notification_id() const { return notification_ids_
.at(0); }
137 const std::vector
<std::string
>& notification_ids() const {
138 return notification_ids_
;
142 std::vector
<std::string
> notification_ids_
;
143 bool waiting_
= false;
146 DISALLOW_COPY_AND_ASSIGN(NotificationAddObserver
);
149 // Class observing of "UPDATE" notification events.
150 class NotificationUpdateObserver
: public MessageCenterChangeObserver
{
152 NotificationUpdateObserver() {}
153 ~NotificationUpdateObserver() override
{}
156 if (!notification_id_
.empty())
157 return notification_id_
;
164 std::string
notification_id(notification_id_
);
165 notification_id_
.clear();
166 return notification_id
;
169 void OnNotificationUpdated(const std::string
& notification_id
) override
{
170 if (notification_id_
.empty()) {
171 notification_id_
= notification_id
;
179 std::string notification_id_
;
180 bool waiting_
= false;
182 DISALLOW_COPY_AND_ASSIGN(NotificationUpdateObserver
);
185 // Class observing of "REMOVE" notification events.
186 class NotificationRemoveObserver
: public MessageCenterChangeObserver
{
188 NotificationRemoveObserver() {}
189 ~NotificationRemoveObserver() override
{}
192 if (!notification_id_
.empty())
193 return notification_id_
;
199 return notification_id_
;
202 // message_center::MessageCenterObserver:
203 void OnNotificationRemoved(
204 const std::string
& notification_id
, bool by_user
) override
{
205 if (notification_id_
.empty()) {
206 notification_id_
= notification_id
;
214 std::string notification_id_
;
215 bool waiting_
= false;
217 DISALLOW_COPY_AND_ASSIGN(NotificationRemoveObserver
);
220 class TestChromeDownloadManagerDelegate
: public ChromeDownloadManagerDelegate
{
222 explicit TestChromeDownloadManagerDelegate(Profile
* profile
)
223 : ChromeDownloadManagerDelegate(profile
), opened_(false) {}
224 ~TestChromeDownloadManagerDelegate() override
{}
226 // ChromeDownloadManagerDelegate override:
227 void OpenDownload(content::DownloadItem
* item
) override
{ opened_
= true; }
229 // Return if the download is opened.
230 bool opened() const { return opened_
; }
236 // Utility method to retrieve a message center.
237 message_center::MessageCenter
* GetMessageCenter() {
238 return message_center::MessageCenter::Get();
241 // Utility method to retrieve a notification object by id.
242 message_center::Notification
* GetNotification(const std::string
& id
) {
243 return GetMessageCenter()->FindVisibleNotificationById(id
);
246 } // anonnymous namespace
248 // Base class for tests
249 class DownloadNotificationTestBase
: public InProcessBrowserTest
{
251 ~DownloadNotificationTestBase() override
{}
253 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
254 // TODO(yoshiki): Remove this after the download notification launches.
255 command_line
->AppendSwitchASCII(switches::kEnableDownloadNotification
,
259 void SetUp() override
{
260 base::FilePath test_data_dir
;
261 PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
);
262 embedded_test_server()->ServeFilesFromDirectory(test_data_dir
);
264 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
265 embedded_test_server()->StopThread();
266 InProcessBrowserTest::SetUp();
269 void SetUpOnMainThread() override
{
270 embedded_test_server()->RestartThreadAndListen();
272 content::BrowserThread::PostTask(
273 content::BrowserThread::IO
, FROM_HERE
,
274 base::Bind(&net::URLRequestSlowDownloadJob::AddUrlHandler
));
277 content::DownloadManager
* GetDownloadManager(Browser
* browser
) {
278 return content::BrowserContext::GetDownloadManager(browser
->profile());
282 //////////////////////////////////////////////////
283 // Test with a single profile
284 //////////////////////////////////////////////////
286 class DownloadNotificationTest
: public DownloadNotificationTestBase
{
288 ~DownloadNotificationTest() override
{}
290 void SetUpOnMainThread() override
{
291 Profile
* profile
= browser()->profile();
293 scoped_ptr
<TestChromeDownloadManagerDelegate
> test_delegate
;
294 test_delegate
.reset(new TestChromeDownloadManagerDelegate(profile
));
295 test_delegate
->GetDownloadIdReceiverCallback().Run(
296 content::DownloadItem::kInvalidId
+ 1);
297 DownloadServiceFactory::GetForBrowserContext(profile
)
298 ->SetDownloadManagerDelegateForTesting(test_delegate
.Pass());
300 DownloadNotificationTestBase::SetUpOnMainThread();
303 TestChromeDownloadManagerDelegate
* GetDownloadManagerDelegate() const {
304 return static_cast<TestChromeDownloadManagerDelegate
*>(
305 DownloadServiceFactory::GetForBrowserContext(browser()->profile())
306 ->GetDownloadManagerDelegate());
309 void PrepareIncognitoBrowser() {
310 incognito_browser_
= CreateIncognitoBrowser();
311 Profile
* incognito_profile
= incognito_browser_
->profile();
313 scoped_ptr
<TestChromeDownloadManagerDelegate
> incognito_test_delegate
;
314 incognito_test_delegate
.reset(
315 new TestChromeDownloadManagerDelegate(incognito_profile
));
316 DownloadServiceFactory::GetForBrowserContext(incognito_profile
)
317 ->SetDownloadManagerDelegateForTesting(incognito_test_delegate
.Pass());
320 TestChromeDownloadManagerDelegate
* GetIncognitoDownloadManagerDelegate()
322 Profile
* incognito_profile
= incognito_browser()->profile();
323 return static_cast<TestChromeDownloadManagerDelegate
*>(
324 DownloadServiceFactory::GetForBrowserContext(incognito_profile
)->
325 GetDownloadManagerDelegate());
328 void CreateDownload() {
329 return CreateDownloadForBrowserAndURL(
331 GURL(net::URLRequestSlowDownloadJob::kKnownSizeUrl
));
334 void CreateDownloadForBrowserAndURL(Browser
* browser
, GURL url
) {
335 // Starts a download.
336 NotificationAddObserver download_start_notification_observer
;
337 ui_test_utils::NavigateToURL(browser
, url
);
338 EXPECT_TRUE(download_start_notification_observer
.Wait());
340 // Confirms that a notification is created.
341 notification_id_
= download_start_notification_observer
.notification_id();
342 EXPECT_FALSE(notification_id_
.empty());
343 ASSERT_TRUE(notification());
345 // Confirms that there is only one notification.
346 message_center::NotificationList::Notifications
347 visible_notifications
= GetMessageCenter()->GetVisibleNotifications();
348 EXPECT_EQ(1u, visible_notifications
.size());
349 EXPECT_TRUE(IsInNotifications(visible_notifications
, notification_id_
));
351 // Confirms that a download is also started.
352 std::vector
<content::DownloadItem
*> downloads
;
353 GetDownloadManager(browser
)->GetAllDownloads(&downloads
);
354 EXPECT_EQ(1u, downloads
.size());
355 download_item_
= downloads
[0];
356 ASSERT_TRUE(download_item_
);
359 content::DownloadItem
* download_item() const { return download_item_
; }
360 std::string
notification_id() const { return notification_id_
; }
361 message_center::Notification
* notification() const {
362 return GetNotification(notification_id_
);
364 Browser
* incognito_browser() const { return incognito_browser_
; }
365 base::FilePath
GetDownloadPath() {
366 return DownloadPrefs::FromDownloadManager(GetDownloadManager(browser()))->
371 content::DownloadItem
* download_item_
= nullptr;
372 Browser
* incognito_browser_
= nullptr;
373 std::string notification_id_
;
376 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest
, DownloadFile
) {
379 EXPECT_EQ(l10n_util::GetStringFUTF16(
380 IDS_DOWNLOAD_STATUS_IN_PROGRESS_TITLE
,
381 download_item()->GetFileNameToReportUser().LossyDisplayName()),
382 GetNotification(notification_id())->title());
383 EXPECT_EQ(message_center::NOTIFICATION_TYPE_PROGRESS
,
384 GetNotification(notification_id())->type());
386 NotificationUpdateObserver download_notification_update_observer
;
388 // Requests to complete the download.
389 ui_test_utils::NavigateToURL(
390 browser(), GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl
));
392 // Waits for download completion.
393 while (download_item()->GetState() != content::DownloadItem::COMPLETE
) {
394 NotificationUpdateObserver download_change_notification_observer
;
395 download_change_notification_observer
.Wait();
398 // Waits for new notification.
399 download_notification_update_observer
.Wait();
402 EXPECT_EQ(l10n_util::GetStringFUTF16(
403 IDS_DOWNLOAD_STATUS_DOWNLOADED_TITLE
,
404 download_item()->GetFileNameToReportUser().LossyDisplayName()),
405 GetNotification(notification_id())->title());
406 EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT
,
407 GetNotification(notification_id())->type());
409 // Confirms that there is only one notification.
410 message_center::NotificationList::Notifications
411 visible_notifications
= GetMessageCenter()->GetVisibleNotifications();
412 EXPECT_EQ(1u, visible_notifications
.size());
413 EXPECT_TRUE(IsInNotifications(visible_notifications
, notification_id()));
415 // Opens the message center.
416 GetMessageCenter()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER
);
418 // Try to open the downloaded item by clicking the notification.
419 EXPECT_FALSE(GetDownloadManagerDelegate()->opened());
420 GetMessageCenter()->ClickOnNotification(notification_id());
421 EXPECT_TRUE(GetDownloadManagerDelegate()->opened());
423 EXPECT_FALSE(GetNotification(notification_id()));
426 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest
, DownloadDangerousFile
) {
427 GURL
download_url(embedded_test_server()->GetURL(
428 "/downloads/dangerous/dangerous.swf"));
430 content::DownloadTestObserverTerminal
download_terminal_observer(
431 GetDownloadManager(browser()),
433 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_IGNORE
);
435 CreateDownloadForBrowserAndURL(browser(), download_url
);
437 base::FilePath filename
= download_item()->GetFileNameToReportUser();
439 // Checks the download status.
440 EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE
,
441 download_item()->GetDangerType());
442 EXPECT_TRUE(download_item()->IsDangerous());
444 // Opens the message center.
445 GetMessageCenter()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER
);
447 NotificationRemoveObserver notification_close_observer
;
448 NotificationAddObserver notification_add_observer
;
450 // Cicks the "keep" button.
451 notification()->ButtonClick(1); // 2nd button: "Keep"
452 // Clicking makes the message center closed.
453 GetMessageCenter()->SetVisibility(message_center::VISIBILITY_TRANSIENT
);
455 // Confirms that the notification is closed and re-shown.
456 EXPECT_EQ(notification_id(), notification_close_observer
.Wait());
457 notification_add_observer
.Wait();
458 EXPECT_EQ(notification_id(), notification_add_observer
.notification_id());
459 EXPECT_EQ(1u, GetMessageCenter()->GetVisibleNotifications().size());
461 // Checks the download status.
462 EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED
,
463 download_item()->GetDangerType());
464 EXPECT_FALSE(download_item()->IsDangerous());
466 // Wait for the download completion.
467 download_terminal_observer
.WaitForFinished();
469 // Checks the download status.
470 EXPECT_FALSE(download_item()->IsDangerous());
471 EXPECT_EQ(content::DownloadItem::COMPLETE
, download_item()->GetState());
473 // Checks the downloaded file.
474 EXPECT_TRUE(base::PathExists(GetDownloadPath().Append(filename
.BaseName())));
477 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest
, DiscardDangerousFile
) {
478 GURL
download_url(embedded_test_server()->GetURL(
479 "/downloads/dangerous/dangerous.swf"));
481 content::DownloadTestObserverTerminal
download_terminal_observer(
482 GetDownloadManager(browser()),
484 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_IGNORE
);
486 CreateDownloadForBrowserAndURL(browser(), download_url
);
488 base::FilePath filename
= download_item()->GetFileNameToReportUser();
490 // Checks the download status.
491 EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE
,
492 download_item()->GetDangerType());
493 EXPECT_TRUE(download_item()->IsDangerous());
495 // Opens the message center.
496 GetMessageCenter()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER
);
497 // Ensures the notification exists.
498 EXPECT_EQ(1u, GetMessageCenter()->GetVisibleNotifications().size());
500 NotificationRemoveObserver notification_close_observer
;
502 // Clicks the "Discard" button.
503 notification()->ButtonClick(0); // 1st button: "Discard"
504 // Clicking makes the message center closed.
505 GetMessageCenter()->SetVisibility(message_center::VISIBILITY_TRANSIENT
);
507 // Confirms that the notification is closed.
508 EXPECT_EQ(notification_id(), notification_close_observer
.Wait());
510 // Ensures the notification has closed.
511 EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size());
513 // Wait for the download completion.
514 download_terminal_observer
.WaitForFinished();
516 // Checks there is neither any download nor any notification.
517 EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size());
518 std::vector
<content::DownloadItem
*> downloads
;
519 GetDownloadManager(browser())->GetAllDownloads(&downloads
);
520 EXPECT_EQ(0u, downloads
.size());
522 // Checks the downloaded file doesn't exist.
523 EXPECT_FALSE(base::PathExists(GetDownloadPath().Append(filename
.BaseName())));
526 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest
, DownloadImageFile
) {
527 GURL
download_url(embedded_test_server()->GetURL(
528 "/downloads/image-octet-stream.png"));
530 content::DownloadTestObserverTerminal
download_terminal_observer(
531 GetDownloadManager(browser()), 1u, /* wait_count */
532 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_IGNORE
);
534 CreateDownloadForBrowserAndURL(browser(), download_url
);
536 // Wait for the download completion.
537 download_terminal_observer
.WaitForFinished();
539 // Waits for download completion.
540 while (GetNotification(notification_id())->image().IsEmpty()) {
541 NotificationUpdateObserver download_change_notification_observer
;
542 download_change_notification_observer
.Wait();
546 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest
,
547 CloseNotificationAfterDownload
) {
550 // Requests to complete the download.
551 ui_test_utils::NavigateToURL(
552 browser(), GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl
));
554 // Waits for download completion.
555 while (download_item()->GetState() != content::DownloadItem::COMPLETE
) {
556 NotificationUpdateObserver download_change_notification_observer
;
557 download_change_notification_observer
.Wait();
560 // Opens the message center.
561 GetMessageCenter()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER
);
563 // Closes the notification.
564 NotificationRemoveObserver notification_close_observer
;
565 GetMessageCenter()->RemoveNotification(notification_id(), true /* by_user */);
566 EXPECT_EQ(notification_id(), notification_close_observer
.Wait());
568 EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size());
570 // Confirms that a download is also started.
571 std::vector
<content::DownloadItem
*> downloads
;
572 GetDownloadManager(browser())->GetAllDownloads(&downloads
);
573 EXPECT_EQ(1u, downloads
.size());
574 EXPECT_EQ(content::DownloadItem::COMPLETE
, downloads
[0]->GetState());
577 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest
,
578 CloseNotificationWhileDownloading
) {
581 // Closes the notification.
582 NotificationRemoveObserver notification_close_observer
;
583 GetMessageCenter()->RemoveNotification(notification_id(), true /* by_user */);
584 EXPECT_EQ(notification_id(), notification_close_observer
.Wait());
586 EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size());
588 // Confirms that a download is still in progress.
589 std::vector
<content::DownloadItem
*> downloads
;
590 content::DownloadManager
* download_manager
= GetDownloadManager(browser());
591 download_manager
->GetAllDownloads(&downloads
);
592 EXPECT_EQ(1u, downloads
.size());
593 EXPECT_EQ(content::DownloadItem::IN_PROGRESS
, downloads
[0]->GetState());
595 // Installs observers before requesting the completion.
596 NotificationAddObserver download_notification_add_observer
;
597 content::DownloadTestObserverTerminal
download_terminal_observer(
600 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL
);
602 // Requests to complete the download and wait for it.
603 ui_test_utils::NavigateToURL(
604 browser(), GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl
));
605 download_terminal_observer
.WaitForFinished();
607 // Waits that new notification.
608 download_notification_add_observer
.Wait();
610 // Confirms that there is only one notification.
611 message_center::NotificationList::Notifications
612 visible_notifications
= GetMessageCenter()->GetVisibleNotifications();
613 EXPECT_EQ(1u, visible_notifications
.size());
614 EXPECT_TRUE(IsInNotifications(visible_notifications
, notification_id()));
617 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest
, InterruptDownload
) {
620 // Installs observers before requesting.
621 NotificationUpdateObserver download_notification_update_observer
;
622 content::DownloadTestObserverTerminal
download_terminal_observer(
623 GetDownloadManager(browser()),
625 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL
);
627 // Requests to fail the download and wait for it.
628 ui_test_utils::NavigateToURL(
629 browser(), GURL(net::URLRequestSlowDownloadJob::kErrorDownloadUrl
));
630 download_terminal_observer
.WaitForFinished();
632 // Waits that new notification.
633 download_notification_update_observer
.Wait();
635 // Confirms that there is only one notification.
636 message_center::NotificationList::Notifications
637 visible_notifications
= GetMessageCenter()->GetVisibleNotifications();
638 EXPECT_EQ(1u, visible_notifications
.size());
639 EXPECT_TRUE(IsInNotifications(visible_notifications
, notification_id()));
642 EXPECT_EQ(l10n_util::GetStringFUTF16(
643 IDS_DOWNLOAD_STATUS_DOWNLOAD_FAILED_TITLE
,
644 download_item()->GetFileNameToReportUser().LossyDisplayName()),
645 GetNotification(notification_id())->title());
646 EXPECT_EQ(l10n_util::GetStringFUTF16(
647 IDS_DOWNLOAD_STATUS_INTERRUPTED
,
648 l10n_util::GetStringUTF16(
649 IDS_DOWNLOAD_INTERRUPTED_DESCRIPTION_NETWORK_ERROR
)),
650 GetNotification(notification_id())->message().substr(48));
651 EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT
,
652 GetNotification(notification_id())->type());
655 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest
,
656 InterruptDownloadAfterClosingNotification
) {
659 // Closes the notification.
660 NotificationRemoveObserver notification_close_observer
;
661 GetMessageCenter()->RemoveNotification(notification_id(), true /* by_user */);
662 EXPECT_EQ(notification_id(), notification_close_observer
.Wait());
664 EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size());
666 // Confirms that a download is still in progress.
667 std::vector
<content::DownloadItem
*> downloads
;
668 content::DownloadManager
* download_manager
= GetDownloadManager(browser());
669 download_manager
->GetAllDownloads(&downloads
);
670 EXPECT_EQ(1u, downloads
.size());
671 EXPECT_EQ(content::DownloadItem::IN_PROGRESS
, downloads
[0]->GetState());
673 // Installs observers before requesting the completion.
674 NotificationAddObserver download_notification_add_observer
;
675 content::DownloadTestObserverTerminal
download_terminal_observer(
678 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL
);
680 // Requests to fail the download and wait for it.
681 ui_test_utils::NavigateToURL(
682 browser(), GURL(net::URLRequestSlowDownloadJob::kErrorDownloadUrl
));
683 download_terminal_observer
.WaitForFinished();
685 // Waits that new notification.
686 download_notification_add_observer
.Wait();
688 // Confirms that there is only one notification.
689 message_center::NotificationList::Notifications
690 visible_notifications
= GetMessageCenter()->GetVisibleNotifications();
691 EXPECT_EQ(1u, visible_notifications
.size());
692 EXPECT_TRUE(IsInNotifications(visible_notifications
, notification_id()));
695 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest
, DownloadRemoved
) {
698 NotificationRemoveObserver notification_close_observer
;
699 download_item()->Remove();
700 EXPECT_EQ(notification_id(), notification_close_observer
.Wait());
702 // Confirms that the notification is removed.
703 EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size());
705 // Confirms that the download item is removed.
706 std::vector
<content::DownloadItem
*> downloads
;
707 GetDownloadManager(browser())->GetAllDownloads(&downloads
);
708 EXPECT_EQ(0u, downloads
.size());
711 #if defined(MEMORY_SANITIZER)
712 # define MAYBE_DownloadMultipleFiles DISABLED_DownloadMultipleFiles
714 # define MAYBE_DownloadMultipleFiles DownloadMultipleFiles
717 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest
, MAYBE_DownloadMultipleFiles
) {
718 GURL
url1(net::URLRequestSlowDownloadJob::kUnknownSizeUrl
);
719 GURL
url2(net::URLRequestSlowDownloadJob::kKnownSizeUrl
);
721 // Starts the 1st download.
722 NotificationAddObserver download_start_notification_observer1
;
723 ui_test_utils::NavigateToURL(browser(), url1
);
724 EXPECT_TRUE(download_start_notification_observer1
.Wait());
725 std::string notification_id1
=
726 download_start_notification_observer1
.notification_id();
727 EXPECT_FALSE(notification_id1
.empty());
729 // Confirms that there is a download.
730 std::vector
<content::DownloadItem
*> downloads
;
731 GetDownloadManager(browser())->GetAllDownloads(&downloads
);
732 EXPECT_EQ(1u, downloads
.size());
733 content::DownloadItem
* download1or2
= downloads
[0];
735 // Starts the 2nd download and waits for a notification.
736 NotificationAddObserver download_start_notification_observer2
;
737 ui_test_utils::NavigateToURL(browser(), url2
);
738 EXPECT_TRUE(download_start_notification_observer2
.Wait());
740 // Confirms that there are 2 downloads.
742 GetDownloadManager(browser())->GetAllDownloads(&downloads
);
743 content::DownloadItem
* download1
= downloads
[0];
744 content::DownloadItem
* download2
= downloads
[1];
745 EXPECT_EQ(2u, downloads
.size());
746 EXPECT_NE(download1
, download2
);
747 EXPECT_TRUE(download1
== download1or2
|| download2
== download1or2
);
749 // Confirms that there are 2 notifications.
750 message_center::NotificationList::Notifications
751 visible_notifications
= GetMessageCenter()->GetVisibleNotifications();
752 EXPECT_EQ(2u, visible_notifications
.size());
754 std::string notification_id2
;
755 for (auto notification
: visible_notifications
) {
756 if (notification
->id() == notification_id1
) {
758 } else if (notification
->type() ==
759 message_center::NOTIFICATION_TYPE_PROGRESS
) {
760 notification_id2
= (notification
->id());
763 EXPECT_TRUE(!notification_id2
.empty());
765 // Requests to complete the downloads.
766 ui_test_utils::NavigateToURL(
767 browser(), GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl
));
769 // Waits for the completion of downloads.
770 NotificationUpdateObserver download_change_notification_observer
;
771 while (download1
->GetState() != content::DownloadItem::COMPLETE
||
772 download2
->GetState() != content::DownloadItem::COMPLETE
) {
773 download_change_notification_observer
.Wait();
776 visible_notifications
= GetMessageCenter()->GetVisibleNotifications();
777 EXPECT_EQ(2u, visible_notifications
.size());
778 EXPECT_TRUE(IsInNotifications(visible_notifications
,
780 EXPECT_TRUE(IsInNotifications(visible_notifications
,
783 // Confirms the types of download notifications are correct.
784 EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT
,
785 GetNotification(notification_id1
)->type());
786 EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT
,
787 GetNotification(notification_id2
)->type());
790 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest
,
791 DownloadMultipleFilesOneByOne
) {
793 content::DownloadItem
* first_download_item
= download_item();
794 content::DownloadItem
* second_download_item
= nullptr;
795 std::string first_notification_id
= notification_id();
796 std::string second_notification_id
;
798 // Requests to complete the first download.
799 ui_test_utils::NavigateToURL(
800 browser(), GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl
));
802 // Waits for completion of the first download.
803 while (first_download_item
->GetState() != content::DownloadItem::COMPLETE
) {
804 NotificationUpdateObserver download_change_notification_observer
;
805 download_change_notification_observer
.Wait();
807 EXPECT_EQ(content::DownloadItem::COMPLETE
, first_download_item
->GetState());
809 // Checks the message center.
810 EXPECT_EQ(1u, GetMessageCenter()->GetVisibleNotifications().size());
812 // Starts the second download.
813 GURL
url(net::URLRequestSlowDownloadJob::kKnownSizeUrl
);
814 NotificationAddObserver download_start_notification_observer
;
815 ui_test_utils::NavigateToURL(browser(), url
);
816 EXPECT_TRUE(download_start_notification_observer
.Wait());
818 // Confirms that the second notification is created.
819 second_notification_id
=
820 download_start_notification_observer
.notification_id();
821 EXPECT_FALSE(second_notification_id
.empty());
822 ASSERT_TRUE(GetNotification(second_notification_id
));
824 // Confirms that there are two notifications, including the second
826 message_center::NotificationList::Notifications
827 visible_notifications
= GetMessageCenter()->GetVisibleNotifications();
828 EXPECT_EQ(2u, visible_notifications
.size());
829 EXPECT_TRUE(IsInNotifications(visible_notifications
, first_notification_id
));
830 EXPECT_TRUE(IsInNotifications(visible_notifications
, second_notification_id
));
832 // Confirms that the second download is also started.
833 std::vector
<content::DownloadItem
*> downloads
;
834 GetDownloadManager(browser())->GetAllDownloads(&downloads
);
835 EXPECT_EQ(2u, downloads
.size());
836 EXPECT_TRUE(first_download_item
== downloads
[0] ||
837 first_download_item
== downloads
[1]);
838 // Stores the second download.
839 if (first_download_item
== downloads
[0])
840 second_download_item
= downloads
[1];
842 second_download_item
= downloads
[0];
844 EXPECT_EQ(content::DownloadItem::IN_PROGRESS
,
845 second_download_item
->GetState());
847 // Requests to complete the second download.
848 ui_test_utils::NavigateToURL(
849 browser(), GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl
));
851 // Waits for completion of the second download.
852 while (second_download_item
->GetState() != content::DownloadItem::COMPLETE
) {
853 NotificationUpdateObserver download_change_notification_observer
;
854 download_change_notification_observer
.Wait();
857 // Opens the message center.
858 GetMessageCenter()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER
);
859 // Checks the message center.
860 EXPECT_EQ(2u, GetMessageCenter()->GetVisibleNotifications().size());
863 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest
, CancelDownload
) {
866 // Opens the message center.
867 GetMessageCenter()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER
);
869 // Cancels the notification by clicking the "cancel' button.
870 NotificationRemoveObserver notification_close_observer
;
871 notification()->ButtonClick(1);
872 EXPECT_EQ(notification_id(), notification_close_observer
.Wait());
873 EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size());
875 // Confirms that a download is also cancelled.
876 std::vector
<content::DownloadItem
*> downloads
;
877 GetDownloadManager(browser())->GetAllDownloads(&downloads
);
878 EXPECT_EQ(1u, downloads
.size());
879 EXPECT_EQ(content::DownloadItem::CANCELLED
, downloads
[0]->GetState());
882 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest
,
883 DownloadCancelledByUserExternally
) {
886 // Cancels the notification by clicking the "cancel' button.
887 NotificationRemoveObserver notification_close_observer
;
888 download_item()->Cancel(true /* by_user */);
889 EXPECT_EQ(notification_id(), notification_close_observer
.Wait());
890 EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size());
892 // Confirms that a download is also cancelled.
893 std::vector
<content::DownloadItem
*> downloads
;
894 GetDownloadManager(browser())->GetAllDownloads(&downloads
);
895 EXPECT_EQ(1u, downloads
.size());
896 EXPECT_EQ(content::DownloadItem::CANCELLED
, downloads
[0]->GetState());
899 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest
,
900 DownloadCancelledExternally
) {
903 // Cancels the notification by clicking the "cancel' button.
904 NotificationRemoveObserver notification_close_observer
;
905 download_item()->Cancel(false /* by_user */);
906 EXPECT_EQ(notification_id(), notification_close_observer
.Wait());
907 EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size());
909 // Confirms that a download is also cancelled.
910 std::vector
<content::DownloadItem
*> downloads
;
911 GetDownloadManager(browser())->GetAllDownloads(&downloads
);
912 EXPECT_EQ(1u, downloads
.size());
913 EXPECT_EQ(content::DownloadItem::CANCELLED
, downloads
[0]->GetState());
916 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest
, IncognitoDownloadFile
) {
917 PrepareIncognitoBrowser();
919 // Starts an incognito download.
920 CreateDownloadForBrowserAndURL(
922 GURL(net::URLRequestSlowDownloadJob::kKnownSizeUrl
));
924 EXPECT_EQ(l10n_util::GetStringFUTF16(
925 IDS_DOWNLOAD_STATUS_IN_PROGRESS_TITLE
,
926 download_item()->GetFileNameToReportUser().LossyDisplayName()),
927 GetNotification(notification_id())->title());
928 EXPECT_EQ(message_center::NOTIFICATION_TYPE_PROGRESS
,
929 GetNotification(notification_id())->type());
930 EXPECT_TRUE(download_item()->GetBrowserContext()->IsOffTheRecord());
932 // Requests to complete the download.
933 ui_test_utils::NavigateToURL(
935 GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl
));
937 // Waits for download completion.
938 while (download_item()->GetState() != content::DownloadItem::COMPLETE
) {
939 NotificationUpdateObserver download_change_notification_observer
;
940 download_change_notification_observer
.Wait();
943 EXPECT_EQ(l10n_util::GetStringFUTF16(
944 IDS_DOWNLOAD_STATUS_DOWNLOADED_TITLE
,
945 download_item()->GetFileNameToReportUser().LossyDisplayName()),
946 GetNotification(notification_id())->title());
947 EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT
,
948 GetNotification(notification_id())->type());
950 // Opens the message center.
951 GetMessageCenter()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER
);
953 // Try to open the downloaded item by clicking the notification.
954 EXPECT_FALSE(GetIncognitoDownloadManagerDelegate()->opened());
955 GetMessageCenter()->ClickOnNotification(notification_id());
956 EXPECT_TRUE(GetIncognitoDownloadManagerDelegate()->opened());
957 EXPECT_FALSE(GetDownloadManagerDelegate()->opened());
959 EXPECT_FALSE(GetNotification(notification_id()));
960 chrome::CloseWindow(incognito_browser());
963 IN_PROC_BROWSER_TEST_F(DownloadNotificationTest
,
964 SimultaneousIncognitoAndNormalDownloads
) {
965 PrepareIncognitoBrowser();
967 GURL
url_incognito(net::URLRequestSlowDownloadJob::kUnknownSizeUrl
);
968 GURL
url_normal(net::URLRequestSlowDownloadJob::kKnownSizeUrl
);
970 // Starts the incognito download.
971 NotificationAddObserver download_start_notification_observer1
;
972 ui_test_utils::NavigateToURL(incognito_browser(), url_incognito
);
973 EXPECT_TRUE(download_start_notification_observer1
.Wait());
974 std::string notification_id1
=
975 download_start_notification_observer1
.notification_id();
976 EXPECT_FALSE(notification_id1
.empty());
978 // Confirms that there is a download.
979 std::vector
<content::DownloadItem
*> downloads
;
980 GetDownloadManager(browser())->GetAllDownloads(&downloads
);
981 EXPECT_EQ(0u, downloads
.size());
983 GetDownloadManager(incognito_browser())->GetAllDownloads(&downloads
);
984 EXPECT_EQ(1u, downloads
.size());
985 content::DownloadItem
* download_incognito
= downloads
[0];
987 // Starts the normal download.
988 NotificationAddObserver download_start_notification_observer2
;
989 ui_test_utils::NavigateToURL(browser(), url_normal
);
990 EXPECT_TRUE(download_start_notification_observer2
.Wait());
991 std::string notification_id2
=
992 download_start_notification_observer2
.notification_id();
993 EXPECT_FALSE(notification_id2
.empty());
995 // Confirms that there are 2 downloads.
997 GetDownloadManager(browser())->GetAllDownloads(&downloads
);
998 content::DownloadItem
* download_normal
= downloads
[0];
999 EXPECT_EQ(1u, downloads
.size());
1000 EXPECT_NE(download_normal
, download_incognito
);
1002 GetDownloadManager(incognito_browser())->GetAllDownloads(&downloads
);
1003 EXPECT_EQ(1u, downloads
.size());
1004 EXPECT_EQ(download_incognito
, downloads
[0]);
1006 // Confirms the types of download notifications are correct.
1007 EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT
,
1008 GetNotification(notification_id1
)->type());
1009 EXPECT_EQ(message_center::NOTIFICATION_TYPE_PROGRESS
,
1010 GetNotification(notification_id2
)->type());
1012 EXPECT_TRUE(download_incognito
->GetBrowserContext()->IsOffTheRecord());
1013 EXPECT_FALSE(download_normal
->GetBrowserContext()->IsOffTheRecord());
1015 // Requests to complete the downloads.
1016 ui_test_utils::NavigateToURL(
1017 browser(), GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl
));
1019 // Waits for the completion of downloads.
1020 while (download_normal
->GetState() != content::DownloadItem::COMPLETE
||
1021 download_incognito
->GetState() != content::DownloadItem::COMPLETE
) {
1022 NotificationUpdateObserver download_change_notification_observer
;
1023 download_change_notification_observer
.Wait();
1026 // Confirms the types of download notifications are correct.
1027 EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT
,
1028 GetNotification(notification_id1
)->type());
1029 EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT
,
1030 GetNotification(notification_id2
)->type());
1032 chrome::CloseWindow(incognito_browser());
1035 //////////////////////////////////////////////////
1036 // Test with multi profiles
1037 //////////////////////////////////////////////////
1039 class MultiProfileDownloadNotificationTest
1040 : public DownloadNotificationTestBase
{
1042 ~MultiProfileDownloadNotificationTest() override
{}
1044 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
1045 DownloadNotificationTestBase::SetUpCommandLine(command_line
);
1047 // Logs in to a dummy profile.
1048 command_line
->AppendSwitchASCII(chromeos::switches::kLoginUser
,
1049 kTestAccounts
[DUMMY_ACCOUNT_INDEX
].email
);
1050 command_line
->AppendSwitchASCII(chromeos::switches::kLoginProfile
,
1051 kTestAccounts
[DUMMY_ACCOUNT_INDEX
].hash
);
1054 // Logs in to the primary profile.
1055 void SetUpOnMainThread() override
{
1056 const TestAccountInfo
& info
= kTestAccounts
[PRIMARY_ACCOUNT_INDEX
];
1058 AddUser(info
, true);
1059 DownloadNotificationTestBase::SetUpOnMainThread();
1062 // Loads all users to the current session and sets up necessary fields.
1063 // This is used for preparing all accounts in PRE_ test setup, and for testing
1064 // actual login behavior.
1065 void AddAllUsers() {
1066 for (size_t i
= 0; i
< arraysize(kTestAccounts
); ++i
)
1067 AddUser(kTestAccounts
[i
], i
>= SECONDARY_ACCOUNT_INDEX_START
);
1070 Profile
* GetProfileByIndex(int index
) {
1071 return chromeos::ProfileHelper::GetProfileByUserIdHash(
1072 kTestAccounts
[index
].hash
);
1075 // Adds a new user for testing to the current session.
1076 void AddUser(const TestAccountInfo
& info
, bool log_in
) {
1077 user_manager::UserManager
* const user_manager
=
1078 user_manager::UserManager::Get();
1080 user_manager
->UserLoggedIn(info
.email
, info
.hash
, false);
1081 user_manager
->SaveUserDisplayName(info
.email
,
1082 base::UTF8ToUTF16(info
.display_name
));
1083 SigninManagerFactory::GetForProfile(
1084 chromeos::ProfileHelper::GetProfileByUserIdHash(info
.hash
))
1085 ->SetAuthenticatedAccountInfo(info
.gaia_id
, info
.email
);
1089 IN_PROC_BROWSER_TEST_F(MultiProfileDownloadNotificationTest
,
1090 PRE_DownloadMultipleFiles
) {
1094 IN_PROC_BROWSER_TEST_F(MultiProfileDownloadNotificationTest
,
1095 DownloadMultipleFiles
) {
1098 GURL
url(net::URLRequestSlowDownloadJob::kUnknownSizeUrl
);
1100 Profile
* profile1
= GetProfileByIndex(1);
1101 Profile
* profile2
= GetProfileByIndex(2);
1102 Browser
* browser1
= CreateBrowser(profile1
);
1103 Browser
* browser2
= CreateBrowser(profile2
);
1104 EXPECT_NE(browser1
, browser2
);
1106 // First user starts a download.
1107 NotificationAddObserver download_start_notification_observer1
;
1108 ui_test_utils::NavigateToURL(browser1
, url
);
1109 download_start_notification_observer1
.Wait();
1111 // Confirms that the download is started.
1112 std::vector
<content::DownloadItem
*> downloads
;
1113 GetDownloadManager(browser1
)->GetAllDownloads(&downloads
);
1114 EXPECT_EQ(1u, downloads
.size());
1115 content::DownloadItem
* download1
= downloads
[0];
1117 // Confirms that a download notification is generated.
1118 std::string notification_id_user1
=
1119 download_start_notification_observer1
.notification_id();
1120 EXPECT_FALSE(notification_id_user1
.empty());
1122 // Second user starts a download.
1123 NotificationAddObserver download_start_notification_observer2
;
1124 ui_test_utils::NavigateToURL(browser2
, url
);
1125 download_start_notification_observer2
.Wait();
1126 std::string notification_id_user2_1
=
1127 download_start_notification_observer2
.notification_id();
1128 EXPECT_FALSE(notification_id_user2_1
.empty());
1130 // Confirms that the second user has only 1 download.
1132 GetDownloadManager(browser2
)->GetAllDownloads(&downloads
);
1133 ASSERT_EQ(1u, downloads
.size());
1135 // Second user starts another download.
1136 NotificationAddObserver download_start_notification_observer3
;
1137 ui_test_utils::NavigateToURL(browser2
, url
);
1138 download_start_notification_observer3
.Wait();
1139 std::string notification_id_user2_2
=
1140 download_start_notification_observer3
.notification_id();
1141 EXPECT_FALSE(notification_id_user2_2
.empty());
1143 // Confirms that the second user has 2 downloads.
1145 GetDownloadManager(browser2
)->GetAllDownloads(&downloads
);
1146 ASSERT_EQ(2u, downloads
.size());
1147 content::DownloadItem
* download2
= downloads
[0];
1148 content::DownloadItem
* download3
= downloads
[1];
1149 EXPECT_NE(download1
, download2
);
1150 EXPECT_NE(download1
, download3
);
1151 EXPECT_NE(download2
, download3
);
1153 // Confirms that the first user still has only 1 download.
1155 GetDownloadManager(browser1
)->GetAllDownloads(&downloads
);
1156 ASSERT_EQ(1u, downloads
.size());
1157 EXPECT_EQ(download1
, downloads
[0]);
1159 // Confirms the types of download notifications are correct.
1160 // Normal notification for user1.
1161 EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT
,
1162 GetNotification(notification_id_user1
)->type());
1163 // Normal notification for user2.
1164 EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT
,
1165 GetNotification(notification_id_user2_1
)->type());
1166 EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT
,
1167 GetNotification(notification_id_user2_2
)->type());
1169 // Requests to complete the downloads.
1170 NotificationUpdateObserver download_change_notification_observer
;
1171 ui_test_utils::NavigateToURL(
1172 browser(), GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl
));
1174 // Waits for the completion of downloads.
1175 while (download1
->GetState() != content::DownloadItem::COMPLETE
||
1176 download2
->GetState() != content::DownloadItem::COMPLETE
||
1177 download3
->GetState() != content::DownloadItem::COMPLETE
) {
1178 // Requests again, since sometimes the request may fail.
1179 ui_test_utils::NavigateToURL(
1180 browser(), GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl
));
1181 download_change_notification_observer
.Wait();
1184 // Confirms the types of download notifications are correct.
1185 EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT
,
1186 GetNotification(notification_id_user1
)->type());
1187 EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT
,
1188 GetNotification(notification_id_user2_1
)->type());
1189 // Normal notifications for user2.
1190 EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT
,
1191 GetNotification(notification_id_user2_1
)->type());
1192 EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT
,
1193 GetNotification(notification_id_user2_2
)->type());