Commit b2ca5e69 authored by Evan Stade's avatar Evan Stade Committed by Commit Bot

Update download notification browser tests.

Use NotificationDisplayService instead of MessageCenter in tests.

Bug: 783018
Change-Id: I2bc849e90f5f899144d015c83755f8eade62e185
Reviewed-on: https://chromium-review.googlesource.com/867291
Commit-Queue: Evan Stade <estade@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Cr-Commit-Position: refs/heads/master@{#530411}
parent e0b8201c
......@@ -16,6 +16,7 @@
#include "chrome/browser/download/download_core_service.h"
#include "chrome/browser/download/download_core_service_factory.h"
#include "chrome/browser/download/download_prefs.h"
#include "chrome/browser/notifications/notification_display_service_tester.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/ui/browser.h"
......@@ -37,9 +38,6 @@
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/url_request/url_request_slow_download_job.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/message_center_observer.h"
#include "ui/message_center/public/cpp/message_center_switches.h"
#include "url/gurl.h"
namespace {
......@@ -66,181 +64,11 @@ static const TestAccountInfo kTestAccounts[] = {
{"charlie@invalid.domain", "10003", "hashcharl", "Charlie"},
};
template <typename T>
bool IsInNotifications(
const T& notifications,
const std::string& id) {
for (const auto& notification : notifications) {
if (notification->id() == id)
return true;
}
return false;
}
// Base class observing notification events.
class MessageCenterChangeObserver
: public message_center::MessageCenterObserver {
public:
MessageCenterChangeObserver() {
message_center::MessageCenter::Get()->AddObserver(this);
}
~MessageCenterChangeObserver() override {
message_center::MessageCenter::Get()->RemoveObserver(this);
}
protected:
void RunLoop() {
base::MessageLoop::ScopedNestableTaskAllower allow(
base::MessageLoop::current());
run_loop_->Run();
}
void QuitRunLoop() {
run_loop_->Quit();
}
void ResetRunLoop() {
run_loop_.reset(new base::RunLoop);
}
private:
std::unique_ptr<base::RunLoop> run_loop_;
DISALLOW_COPY_AND_ASSIGN(MessageCenterChangeObserver);
};
// Class observing of "ADD" notification events.
class NotificationAddObserver : public MessageCenterChangeObserver {
public:
NotificationAddObserver() : count_(1) {}
explicit NotificationAddObserver(int count) : count_(count) {
MessageCenterChangeObserver();
}
~NotificationAddObserver() override {}
bool Wait() {
if (count_ <= 0)
return count_ == 0;
waiting_ = true;
ResetRunLoop();
RunLoop();
waiting_ = false;
return count_ == 0;
}
// message_center::MessageCenterObserver:
void OnNotificationAdded(const std::string& notification_id) override {
count_--;
notification_ids_.push_back(notification_id);
if (waiting_ && count_ == 0)
QuitRunLoop();
}
const std::string& notification_id() const { return notification_ids_.at(0); }
const std::vector<std::string>& notification_ids() const {
return notification_ids_;
}
private:
std::vector<std::string> notification_ids_;
bool waiting_ = false;
int count_;
DISALLOW_COPY_AND_ASSIGN(NotificationAddObserver);
};
// Class observing of "UPDATE" notification events.
class NotificationUpdateObserver : public MessageCenterChangeObserver {
public:
NotificationUpdateObserver() {}
explicit NotificationUpdateObserver(const std::string& id)
: target_notification_id_(id) {}
~NotificationUpdateObserver() override {}
std::string Wait() {
if (!notification_id_.empty())
return notification_id_;
waiting_ = true;
ResetRunLoop();
RunLoop();
waiting_ = false;
std::string notification_id(notification_id_);
notification_id_.clear();
return notification_id;
}
void OnNotificationUpdated(const std::string& notification_id) override {
if (notification_id_.empty()) {
if (!target_notification_id_.empty() &&
target_notification_id_ != notification_id) {
return;
}
notification_id_ = notification_id;
if (waiting_)
QuitRunLoop();
}
}
void Reset() {
notification_id_.clear();
}
private:
std::string notification_id_;
std::string target_notification_id_;
bool waiting_ = false;
DISALLOW_COPY_AND_ASSIGN(NotificationUpdateObserver);
};
// Class observing of "REMOVE" notification events.
class NotificationRemoveObserver : public MessageCenterChangeObserver {
public:
NotificationRemoveObserver() {}
~NotificationRemoveObserver() override {}
std::string Wait() {
if (!notification_id_.empty())
return notification_id_;
waiting_ = true;
ResetRunLoop();
RunLoop();
waiting_ = false;
return notification_id_;
}
// message_center::MessageCenterObserver:
void OnNotificationRemoved(
const std::string& notification_id, bool by_user) override {
if (notification_id_.empty()) {
notification_id_ = notification_id;
if (waiting_)
QuitRunLoop();
}
}
private:
std::string notification_id_;
bool waiting_ = false;
DISALLOW_COPY_AND_ASSIGN(NotificationRemoveObserver);
};
class TestChromeDownloadManagerDelegate : public ChromeDownloadManagerDelegate {
public:
explicit TestChromeDownloadManagerDelegate(Profile* profile)
: ChromeDownloadManagerDelegate(profile), opened_(false) {}
~TestChromeDownloadManagerDelegate() override {}
~TestChromeDownloadManagerDelegate() override = default;
// ChromeDownloadManagerDelegate override:
void OpenDownload(content::DownloadItem* item) override { opened_ = true; }
......@@ -259,14 +87,22 @@ class TestChromeDownloadManagerDelegate : public ChromeDownloadManagerDelegate {
bool opened_;
};
// Utility method to retrieve a message center.
message_center::MessageCenter* GetMessageCenter() {
return message_center::MessageCenter::Get();
// Utility method to retrieve a notification object by id. Warning: this will
// check the last display service that was created. If there's a normal and an
// incognito one, you may want to be explicit.
base::Optional<message_center::Notification> GetNotification(
const std::string& id) {
return NotificationDisplayServiceTester::Get()->GetNotification(id);
}
// Utility method to retrieve a notification object by id.
message_center::Notification* GetNotification(const std::string& id) {
return GetMessageCenter()->FindVisibleNotificationById(id);
// Waits for a notification to be updated/added on |display_service|.
void WaitForDownloadNotificationForDisplayService(
NotificationDisplayServiceTester* display_service) {
base::RunLoop run_loop;
display_service->SetNotificationAddedClosure(base::BindRepeating(
[](base::RunLoop* run_loop) { run_loop->Quit(); }, &run_loop));
run_loop.Run();
display_service->SetNotificationAddedClosure(base::RepeatingClosure());
}
} // anonnymous namespace
......@@ -274,17 +110,19 @@ message_center::Notification* GetNotification(const std::string& id) {
// Base class for tests
class DownloadNotificationTestBase : public InProcessBrowserTest {
public:
~DownloadNotificationTestBase() override {}
DownloadNotificationTestBase() = default;
~DownloadNotificationTestBase() override = default;
void SetUpOnMainThread() override {
ASSERT_TRUE(embedded_test_server()->Start());
display_service_ = std::make_unique<NotificationDisplayServiceTester>(
browser()->profile());
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::BindOnce(&net::URLRequestSlowDownloadJob::AddUrlHandler));
GetMessageCenter()->DisableTimersForTest();
ASSERT_TRUE(downloads_directory_.CreateUniqueTempDir());
ASSERT_TRUE(SetDownloadsDirectory(browser()));
}
......@@ -310,9 +148,24 @@ class DownloadNotificationTestBase : public InProcessBrowserTest {
return content::BrowserContext::GetDownloadManager(browser->profile());
}
private:
// Requests to complete the download and wait for it.
void CompleteTheDownload(size_t wait_count = 1u) {
content::DownloadTestObserverTerminal download_terminal_observer(
GetDownloadManager(browser()), wait_count,
content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL);
ui_test_utils::NavigateToURL(
browser(), GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl));
download_terminal_observer.WaitForFinished();
}
std::unique_ptr<NotificationDisplayServiceTester> display_service_;
std::unique_ptr<NotificationDisplayServiceTester> incognito_display_service_;
// Location of the downloads directory for these tests
base::ScopedTempDir downloads_directory_;
private:
DISALLOW_COPY_AND_ASSIGN(DownloadNotificationTestBase);
};
//////////////////////////////////////////////////
......@@ -321,7 +174,8 @@ class DownloadNotificationTestBase : public InProcessBrowserTest {
class DownloadNotificationTest : public DownloadNotificationTestBase {
public:
~DownloadNotificationTest() override {}
DownloadNotificationTest() = default;
~DownloadNotificationTest() override = default;
void SetUpOnMainThread() override {
Profile* profile = browser()->profile();
......@@ -354,6 +208,10 @@ class DownloadNotificationTest : public DownloadNotificationTestBase {
DownloadCoreServiceFactory::GetForBrowserContext(incognito_profile)
->SetDownloadManagerDelegateForTesting(
std::move(incognito_test_delegate));
incognito_display_service_ =
std::make_unique<NotificationDisplayServiceTester>(
incognito_browser()->profile());
}
TestChromeDownloadManagerDelegate* GetIncognitoDownloadManagerDelegate()
......@@ -370,23 +228,34 @@ class DownloadNotificationTest : public DownloadNotificationTestBase {
GURL(net::URLRequestSlowDownloadJob::kKnownSizeUrl));
}
// Returns the correct display service for the given Browser. If |browser| is
// null, returns the service for browser().
NotificationDisplayServiceTester* GetDisplayServiceForBrowser(
Browser* browser) {
return (browser && browser == DownloadNotificationTest::incognito_browser())
? incognito_display_service_.get()
: display_service_.get();
}
void WaitForDownloadNotification(Browser* browser = nullptr) {
WaitForDownloadNotificationForDisplayService(
GetDisplayServiceForBrowser(browser));
}
void CreateDownloadForBrowserAndURL(Browser* browser, GURL url) {
// Starts a download.
NotificationAddObserver download_start_notification_observer;
ui_test_utils::NavigateToURL(browser, url);
EXPECT_TRUE(download_start_notification_observer.Wait());
// Confirms that a notification is created.
notification_id_ = download_start_notification_observer.notification_id();
WaitForDownloadNotification(browser);
auto download_notifications =
GetDisplayServiceForBrowser(browser)->GetDisplayedNotificationsForType(
NotificationHandler::Type::DOWNLOAD);
ASSERT_EQ(1u, download_notifications.size());
notification_id_ = download_notifications[0].id();
EXPECT_FALSE(notification_id_.empty());
ASSERT_TRUE(notification());
// Confirms that there is only one notification.
message_center::NotificationList::Notifications
visible_notifications = GetMessageCenter()->GetVisibleNotifications();
EXPECT_EQ(1u, visible_notifications.size());
EXPECT_TRUE(IsInNotifications(visible_notifications, notification_id_));
// Confirms that a download is also started.
std::vector<content::DownloadItem*> downloads;
GetDownloadManager(browser)->GetAllDownloads(&downloads);
......@@ -395,9 +264,42 @@ class DownloadNotificationTest : public DownloadNotificationTestBase {
ASSERT_TRUE(download_item_);
}
void CloseNotification() {
EXPECT_TRUE(notification());
display_service_->RemoveNotification(NotificationHandler::Type::DOWNLOAD,
notification_id(), true /* by_user */);
EXPECT_FALSE(notification());
EXPECT_EQ(0u, GetDownloadNotifications().size());
}
void VerifyDownloadState(content::DownloadItem::DownloadState state) {
std::vector<content::DownloadItem*> downloads;
GetDownloadManager(browser())->GetAllDownloads(&downloads);
ASSERT_EQ(1u, downloads.size());
EXPECT_EQ(state, downloads[0]->GetState());
}
void VerifyUpdatePropagatesToNotification(content::DownloadItem* item) {
bool notification_updated = false;
display_service_->SetNotificationAddedClosure(base::BindRepeating(
[](bool* updated) { *updated = true; }, &notification_updated));
item->UpdateObservers();
EXPECT_TRUE(notification_updated);
display_service_->SetNotificationAddedClosure(base::RepeatingClosure());
}
void InterruptTheDownload() {
content::DownloadTestObserverInterrupted download_interrupted_observer(
GetDownloadManager(browser()), 1u, /* wait_count */
content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL);
ui_test_utils::NavigateToURL(
browser(), GURL(net::URLRequestSlowDownloadJob::kErrorDownloadUrl));
download_interrupted_observer.WaitForFinished();
}
content::DownloadItem* download_item() const { return download_item_; }
std::string notification_id() const { return notification_id_; }
message_center::Notification* notification() const {
base::Optional<message_center::Notification> notification() const {
return GetNotification(notification_id_);
}
Browser* incognito_browser() const { return incognito_browser_; }
......@@ -405,11 +307,17 @@ class DownloadNotificationTest : public DownloadNotificationTestBase {
return DownloadPrefs::FromDownloadManager(GetDownloadManager(browser()))->
DownloadPath();
}
std::vector<message_center::Notification> GetDownloadNotifications() {
return display_service_->GetDisplayedNotificationsForType(
NotificationHandler::Type::DOWNLOAD);
}
private:
content::DownloadItem* download_item_ = nullptr;
Browser* incognito_browser_ = nullptr;
std::string notification_id_;
DISALLOW_COPY_AND_ASSIGN(DownloadNotificationTest);
};
IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, DownloadFile) {
......@@ -418,47 +326,32 @@ IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, DownloadFile) {
EXPECT_EQ(l10n_util::GetStringFUTF16(
IDS_DOWNLOAD_STATUS_IN_PROGRESS_TITLE,
download_item()->GetFileNameToReportUser().LossyDisplayName()),
GetNotification(notification_id())->title());
EXPECT_EQ(message_center::NOTIFICATION_TYPE_PROGRESS,
GetNotification(notification_id())->type());
notification()->title());
EXPECT_EQ(message_center::NOTIFICATION_TYPE_PROGRESS, notification()->type());
// Confirms that the download update is delivered to the notification.
NotificationUpdateObserver download_notification_periodically_update_observer;
download_item()->UpdateObservers();
download_notification_periodically_update_observer.Wait();
EXPECT_TRUE(notification());
VerifyUpdatePropagatesToNotification(download_item());
// Requests to complete the download.
ui_test_utils::NavigateToURL(
browser(), GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl));
// Waits for download completion.
NotificationUpdateObserver
download_change_notification_observer(notification_id());
while (download_item()->GetState() != content::DownloadItem::COMPLETE) {
download_change_notification_observer.Wait();
download_change_notification_observer.Reset();
}
CompleteTheDownload();
// Checks strings.
ASSERT_TRUE(notification());
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_COMPLETE_TITLE),
GetNotification(notification_id())->title());
notification()->title());
EXPECT_EQ(download_item()->GetFileNameToReportUser().LossyDisplayName(),
GetNotification(notification_id())->message());
notification()->message());
EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT,
GetNotification(notification_id())->type());
notification()->type());
// Confirms that there is only one notification.
message_center::NotificationList::Notifications
visible_notifications = GetMessageCenter()->GetVisibleNotifications();
EXPECT_EQ(1u, visible_notifications.size());
EXPECT_TRUE(IsInNotifications(visible_notifications, notification_id()));
// Opens the message center.
GetMessageCenter()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER);
ASSERT_EQ(1u, GetDownloadNotifications().size());
// Try to open the downloaded item by clicking the notification.
EXPECT_FALSE(GetDownloadManagerDelegate()->opened());
GetMessageCenter()->ClickOnNotification(notification_id());
display_service_->SimulateClick(NotificationHandler::Type::DOWNLOAD,
notification_id(), base::nullopt,
base::nullopt);
EXPECT_TRUE(GetDownloadManagerDelegate()->opened());
EXPECT_FALSE(GetNotification(notification_id()));
......@@ -482,22 +375,13 @@ IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, DownloadDangerousFile) {
download_item()->GetDangerType());
EXPECT_TRUE(download_item()->IsDangerous());
// Opens the message center.
GetMessageCenter()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER);
NotificationRemoveObserver notification_close_observer;
NotificationAddObserver notification_add_observer;
// Clicks the "keep" button.
display_service_->SimulateClick(NotificationHandler::Type::DOWNLOAD,
notification_id(), 1, // 2nd button: "Keep"
base::nullopt);
// Cicks the "keep" button.
notification()->ButtonClick(1); // 2nd button: "Keep"
// Clicking makes the message center closed.
GetMessageCenter()->SetVisibility(message_center::VISIBILITY_TRANSIENT);
// Confirms that the notification is closed and re-shown.
EXPECT_EQ(notification_id(), notification_close_observer.Wait());
notification_add_observer.Wait();
EXPECT_EQ(notification_id(), notification_add_observer.notification_id());
EXPECT_EQ(1u, GetMessageCenter()->GetVisibleNotifications().size());
// The notification is closed and re-shown.
EXPECT_TRUE(notification());
// Checks the download status.
EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED,
......@@ -533,29 +417,23 @@ IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, DiscardDangerousFile) {
download_item()->GetDangerType());
EXPECT_TRUE(download_item()->IsDangerous());
// Opens the message center.
GetMessageCenter()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER);
// Ensures the notification exists.
EXPECT_EQ(1u, GetMessageCenter()->GetVisibleNotifications().size());
NotificationRemoveObserver notification_close_observer;
EXPECT_TRUE(notification());
// Clicks the "Discard" button.
notification()->ButtonClick(0); // 1st button: "Discard"
// Clicking makes the message center closed.
GetMessageCenter()->SetVisibility(message_center::VISIBILITY_TRANSIENT);
display_service_->SimulateClick(NotificationHandler::Type::DOWNLOAD,
notification_id(),
0, // 1st button: "Discard"
base::nullopt);
// Confirms that the notification is closed.
EXPECT_EQ(notification_id(), notification_close_observer.Wait());
// Ensures the notification has closed.
EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size());
EXPECT_FALSE(notification());
// Wait for the download completion.
download_terminal_observer.WaitForFinished();
// Checks there is neither any download nor any notification.
EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size());
EXPECT_FALSE(notification());
EXPECT_EQ(0u, GetDownloadNotifications().size());
std::vector<content::DownloadItem*> downloads;
GetDownloadManager(browser())->GetAllDownloads(&downloads);
EXPECT_EQ(0u, downloads.size());
......@@ -577,138 +455,61 @@ IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, DownloadImageFile) {
// Wait for the download completion.
download_terminal_observer.WaitForFinished();
// Waits for download completion.
NotificationUpdateObserver
download_change_notification_observer(notification_id());
while (GetNotification(notification_id())->image().IsEmpty()) {
download_change_notification_observer.Wait();
download_change_notification_observer.Reset();
}
WaitForDownloadNotification();
EXPECT_FALSE(notification()->image().IsEmpty());
}
IN_PROC_BROWSER_TEST_F(DownloadNotificationTest,
CloseNotificationAfterDownload) {
CreateDownload();
// Requests to complete the download.
ui_test_utils::NavigateToURL(
browser(), GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl));
// Waits for download completion.
NotificationUpdateObserver
download_change_notification_observer(notification_id());
while (download_item()->GetState() != content::DownloadItem::COMPLETE) {
download_change_notification_observer.Wait();
download_change_notification_observer.Reset();
}
// Opens the message center.
GetMessageCenter()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER);
CompleteTheDownload();
// Closes the notification.
NotificationRemoveObserver notification_close_observer;
GetMessageCenter()->RemoveNotification(notification_id(), true /* by_user */);
EXPECT_EQ(notification_id(), notification_close_observer.Wait());
CloseNotification();
EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size());
// Confirms that a download is also started.
std::vector<content::DownloadItem*> downloads;
GetDownloadManager(browser())->GetAllDownloads(&downloads);
EXPECT_EQ(1u, downloads.size());
EXPECT_EQ(content::DownloadItem::COMPLETE, downloads[0]->GetState());
VerifyDownloadState(content::DownloadItem::COMPLETE);
}
IN_PROC_BROWSER_TEST_F(DownloadNotificationTest,
CloseNotificationWhileDownloading) {
CreateDownload();
// Closes the notification.
NotificationRemoveObserver notification_close_observer;
GetMessageCenter()->RemoveNotification(notification_id(), true /* by_user */);
EXPECT_EQ(notification_id(), notification_close_observer.Wait());
EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size());
// Confirms that a download is still in progress.
std::vector<content::DownloadItem*> downloads;
content::DownloadManager* download_manager = GetDownloadManager(browser());
download_manager->GetAllDownloads(&downloads);
EXPECT_EQ(1u, downloads.size());
EXPECT_EQ(content::DownloadItem::IN_PROGRESS, downloads[0]->GetState());
// Installs observers before requesting the completion.
NotificationAddObserver download_notification_add_observer;
content::DownloadTestObserverTerminal download_terminal_observer(
download_manager,
1u, /* wait_count */
content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL);
CloseNotification();
// Requests to complete the download and wait for it.
ui_test_utils::NavigateToURL(
browser(), GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl));
download_terminal_observer.WaitForFinished();
VerifyDownloadState(content::DownloadItem::IN_PROGRESS);
// Waits that new notification.
download_notification_add_observer.Wait();
CompleteTheDownload();
// Confirms that there is only one notification.
message_center::NotificationList::Notifications
visible_notifications = GetMessageCenter()->GetVisibleNotifications();
EXPECT_EQ(1u, visible_notifications.size());
EXPECT_TRUE(IsInNotifications(visible_notifications, notification_id()));
EXPECT_TRUE(notification());
}
IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, InterruptDownload) {
CreateDownload();
// Installs observers before requesting.
NotificationUpdateObserver
download_notification_update_observer(notification_id());
content::DownloadTestObserverInterrupted download_terminal_observer(
GetDownloadManager(browser()),
1u, /* wait_count */
content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL);
// Requests to fail the download and wait for it.
ui_test_utils::NavigateToURL(
browser(), GURL(net::URLRequestSlowDownloadJob::kErrorDownloadUrl));
download_terminal_observer.WaitForFinished();
// Waits that new notification.
download_notification_update_observer.Wait();
InterruptTheDownload();
// Confirms that there is only one notification.
message_center::NotificationList::Notifications
visible_notifications = GetMessageCenter()->GetVisibleNotifications();
EXPECT_EQ(1u, visible_notifications.size());
EXPECT_TRUE(IsInNotifications(visible_notifications, notification_id()));
EXPECT_EQ(1u, GetDownloadNotifications().size());
ASSERT_TRUE(notification());
// Checks strings.
EXPECT_EQ(l10n_util::GetStringFUTF16(
IDS_DOWNLOAD_STATUS_DOWNLOAD_FAILED_TITLE,
download_item()->GetFileNameToReportUser().LossyDisplayName()),
GetNotification(notification_id())->title());
EXPECT_NE(GetNotification(notification_id())->message().find(
l10n_util::GetStringFUTF16(
IDS_DOWNLOAD_STATUS_INTERRUPTED,
l10n_util::GetStringUTF16(
IDS_DOWNLOAD_INTERRUPTED_DESCRIPTION_NETWORK_ERROR))),
notification()->title());
EXPECT_NE(notification()->message().find(l10n_util::GetStringFUTF16(
IDS_DOWNLOAD_STATUS_INTERRUPTED,
l10n_util::GetStringUTF16(
IDS_DOWNLOAD_INTERRUPTED_DESCRIPTION_NETWORK_ERROR))),
std::string::npos);
EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT,
GetNotification(notification_id())->type());
notification()->type());
}
IN_PROC_BROWSER_TEST_F(DownloadNotificationTest,
InterruptDownloadAfterClosingNotification) {
CreateDownload();
// Closes the notification.
NotificationRemoveObserver notification_close_observer;
GetMessageCenter()->RemoveNotification(notification_id(), true /* by_user */);
EXPECT_EQ(notification_id(), notification_close_observer.Wait());
EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size());
CloseNotification();
// Confirms that a download is still in progress.
std::vector<content::DownloadItem*> downloads;
......@@ -718,36 +519,24 @@ IN_PROC_BROWSER_TEST_F(DownloadNotificationTest,
EXPECT_EQ(content::DownloadItem::IN_PROGRESS, downloads[0]->GetState());
// Installs observers before requesting the completion.
NotificationAddObserver download_notification_add_observer;
content::DownloadTestObserverInterrupted download_terminal_observer(
download_manager,
1u, /* wait_count */
content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL);
// Requests to fail the download and wait for it.
ui_test_utils::NavigateToURL(
browser(), GURL(net::URLRequestSlowDownloadJob::kErrorDownloadUrl));
download_terminal_observer.WaitForFinished();
// Waits that new notification.
download_notification_add_observer.Wait();
InterruptTheDownload();
// Confirms that there is only one notification.
message_center::NotificationList::Notifications
visible_notifications = GetMessageCenter()->GetVisibleNotifications();
EXPECT_EQ(1u, visible_notifications.size());
EXPECT_TRUE(IsInNotifications(visible_notifications, notification_id()));
EXPECT_EQ(1u, GetDownloadNotifications().size());
ASSERT_TRUE(notification());
}
IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, DownloadRemoved) {
CreateDownload();
NotificationRemoveObserver notification_close_observer;
EXPECT_TRUE(notification());
download_item()->Remove();
EXPECT_EQ(notification_id(), notification_close_observer.Wait());
// Confirms that the notification is removed.
EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size());
EXPECT_FALSE(notification());
// Confirms that the download item is removed.
std::vector<content::DownloadItem*> downloads;
......@@ -760,43 +549,27 @@ IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, DownloadMultipleFiles) {
GURL url2(net::URLRequestSlowDownloadJob::kKnownSizeUrl);
// Starts the 1st download.
NotificationAddObserver download_start_notification_observer1;
ui_test_utils::NavigateToURL(browser(), url1);
EXPECT_TRUE(download_start_notification_observer1.Wait());
std::string notification_id1 =
download_start_notification_observer1.notification_id();
WaitForDownloadNotification();
auto notifications = GetDownloadNotifications();
ASSERT_EQ(1u, notifications.size());
std::string notification_id1 = notifications[0].id();
EXPECT_FALSE(notification_id1.empty());
// Confirms that there is a download.
std::vector<content::DownloadItem*> downloads;
GetDownloadManager(browser())->GetAllDownloads(&downloads);
EXPECT_EQ(1u, downloads.size());
ASSERT_EQ(1u, downloads.size());
content::DownloadItem* download1 = downloads[0];
// Confirms that there is a notifications.
message_center::NotificationList::Notifications
visible_notifications = GetMessageCenter()->GetVisibleNotifications();
EXPECT_EQ(1u, visible_notifications.size());
EXPECT_TRUE(IsInNotifications(visible_notifications, notification_id1));
// Confirms that there is a popup notifications.
message_center::NotificationList::PopupNotifications
popup_notifications = GetMessageCenter()->GetPopupNotifications();
EXPECT_EQ(1u, popup_notifications.size());
EXPECT_TRUE(IsInNotifications(popup_notifications, notification_id1));
// Starts the 2nd download and waits for a notification.
NotificationAddObserver download_start_notification_observer2(2);
// Starts the 2nd download.
ui_test_utils::NavigateToURL(browser(), url2);
// 2 notifications should be added. One is for new download (url2), and the
// other one is for reshowing the existing download (url1) as a low-priority
// notification.
EXPECT_TRUE(download_start_notification_observer2.Wait());
WaitForDownloadNotification();
// Confirms that there are 2 downloads.
downloads.clear();
GetDownloadManager(browser())->GetAllDownloads(&downloads);
EXPECT_EQ(2u, downloads.size());
ASSERT_EQ(2u, downloads.size());
content::DownloadItem* download2;
if (download1 == downloads[0])
download2 = downloads[1];
......@@ -806,31 +579,18 @@ IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, DownloadMultipleFiles) {
NOTREACHED();
EXPECT_NE(download1, download2);
notifications = GetDownloadNotifications();
// Confirms that there are 2 notifications.
visible_notifications = GetMessageCenter()->GetVisibleNotifications();
EXPECT_EQ(2u, visible_notifications.size());
EXPECT_EQ(2u, notifications.size());
std::string notification_id2;
for (auto* notification : visible_notifications) {
if (notification->id() == notification_id1) {
for (const auto& notification : notifications) {
if (notification.id() == notification_id1)
continue;
} else if (notification->type() ==
message_center::NOTIFICATION_TYPE_PROGRESS) {
notification_id2 = (notification->id());
}
}
EXPECT_TRUE(!notification_id2.empty());
// Confirms that the both notifications are visible either as popup or in the
// message center.
EXPECT_TRUE(IsInNotifications(visible_notifications, notification_id1));
EXPECT_TRUE(IsInNotifications(visible_notifications, notification_id2));
// Confirms that the new one is popup, and the old one is not.
popup_notifications = GetMessageCenter()->GetPopupNotifications();
EXPECT_EQ(1u, popup_notifications.size());
EXPECT_FALSE(IsInNotifications(popup_notifications, notification_id1));
EXPECT_TRUE(IsInNotifications(popup_notifications, notification_id2));
notification_id2 = notification.id();
}
EXPECT_FALSE(notification_id2.empty());
// Confirms that the old one is low priority, and the new one is default.
const int in_progress_priority1 =
......@@ -842,39 +602,23 @@ IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, DownloadMultipleFiles) {
// Confirms that the updates of both download are delivered to the
// notifications.
NotificationUpdateObserver
notification_periodically_update_observer1(notification_id1);
download1->UpdateObservers();
notification_periodically_update_observer1.Wait();
NotificationUpdateObserver
notification_periodically_update_observer2(notification_id2);
download2->UpdateObservers();
notification_periodically_update_observer2.Wait();
VerifyUpdatePropagatesToNotification(download1);
VerifyUpdatePropagatesToNotification(download2);
// Requests to complete the downloads.
ui_test_utils::NavigateToURL(
browser(), GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl));
// Waits for the completion of downloads.
NotificationUpdateObserver download_change_notification_observer;
while (download1->GetState() != content::DownloadItem::COMPLETE ||
download2->GetState() != content::DownloadItem::COMPLETE) {
download_change_notification_observer.Wait();
download_change_notification_observer.Reset();
}
// Confirms the correct type of notification while download is in progress.
EXPECT_EQ(message_center::NOTIFICATION_TYPE_PROGRESS,
GetNotification(notification_id1)->type());
EXPECT_EQ(message_center::NOTIFICATION_TYPE_PROGRESS,
GetNotification(notification_id2)->type());
// Confirms that the both notifications are visible either as popup or in the
// message center.
visible_notifications = GetMessageCenter()->GetVisibleNotifications();
EXPECT_EQ(2u, visible_notifications.size());
EXPECT_TRUE(IsInNotifications(visible_notifications, notification_id1));
EXPECT_TRUE(IsInNotifications(visible_notifications, notification_id2));
// Requests to complete the downloads.
CompleteTheDownload(2);
// Confirms that the both are popup'd.
popup_notifications = GetMessageCenter()->GetPopupNotifications();
EXPECT_EQ(2u, popup_notifications.size());
EXPECT_TRUE(IsInNotifications(popup_notifications, notification_id1));
EXPECT_TRUE(IsInNotifications(popup_notifications, notification_id2));
// Confirms that the both notifications are visible.
notifications = GetDownloadNotifications();
EXPECT_EQ(2u, notifications.size());
ASSERT_TRUE(GetNotification(notification_id1));
ASSERT_TRUE(GetNotification(notification_id2));
// Confirms that both increase in priority when finished.
EXPECT_GT(GetNotification(notification_id1)->priority(),
......@@ -893,95 +637,58 @@ IN_PROC_BROWSER_TEST_F(DownloadNotificationTest,
DownloadMultipleFilesOneByOne) {
CreateDownload();
content::DownloadItem* first_download_item = download_item();
content::DownloadItem* second_download_item = nullptr;
std::string first_notification_id = notification_id();
std::string second_notification_id;
// Requests to complete the first download.
ui_test_utils::NavigateToURL(
browser(), GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl));
// Waits for completion of the first download.
NotificationUpdateObserver
download_change_notification_observer1(first_notification_id);
while (first_download_item->GetState() != content::DownloadItem::COMPLETE) {
download_change_notification_observer1.Wait();
download_change_notification_observer1.Reset();
}
CompleteTheDownload();
EXPECT_EQ(content::DownloadItem::COMPLETE, first_download_item->GetState());
// Checks the message center.
EXPECT_EQ(1u, GetMessageCenter()->GetVisibleNotifications().size());
EXPECT_TRUE(notification());
// Starts the second download.
GURL url(net::URLRequestSlowDownloadJob::kKnownSizeUrl);
NotificationAddObserver download_start_notification_observer;
ui_test_utils::NavigateToURL(browser(), url);
EXPECT_TRUE(download_start_notification_observer.Wait());
WaitForDownloadNotification();
// Confirms that the second notification is created.
second_notification_id =
download_start_notification_observer.notification_id();
auto notifications = GetDownloadNotifications();
ASSERT_EQ(2u, notifications.size());
std::string second_notification_id =
notifications[(notifications[0].id() == notification_id() ? 1 : 0)].id();
EXPECT_FALSE(second_notification_id.empty());
ASSERT_TRUE(GetNotification(second_notification_id));
// Confirms that there are two notifications, including the second
// notification.
message_center::NotificationList::Notifications
visible_notifications = GetMessageCenter()->GetVisibleNotifications();
EXPECT_EQ(2u, visible_notifications.size());
EXPECT_TRUE(IsInNotifications(visible_notifications, first_notification_id));
EXPECT_TRUE(IsInNotifications(visible_notifications, second_notification_id));
// Confirms that the second download is also started.
std::vector<content::DownloadItem*> downloads;
GetDownloadManager(browser())->GetAllDownloads(&downloads);
EXPECT_EQ(2u, downloads.size());
EXPECT_TRUE(first_download_item == downloads[0] ||
first_download_item == downloads[1]);
// Stores the second download.
if (first_download_item == downloads[0])
second_download_item = downloads[1];
else
second_download_item = downloads[0];
content::DownloadItem* second_download_item =
downloads[first_download_item == downloads[0] ? 1 : 0];
EXPECT_EQ(content::DownloadItem::IN_PROGRESS,
second_download_item->GetState());
// Requests to complete the second download.
ui_test_utils::NavigateToURL(
browser(), GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl));
// Waits for completion of the second download.
NotificationUpdateObserver
download_change_notification_observer2(second_notification_id);
while (second_download_item->GetState() != content::DownloadItem::COMPLETE) {
download_change_notification_observer2.Wait();
download_change_notification_observer2.Reset();
}
CompleteTheDownload();
// Opens the message center.
GetMessageCenter()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER);
// Checks the message center.
EXPECT_EQ(2u, GetMessageCenter()->GetVisibleNotifications().size());
EXPECT_EQ(2u, GetDownloadNotifications().size());
}
IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, CancelDownload) {
CreateDownload();
// Opens the message center.
GetMessageCenter()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER);
// Cancels the notification by clicking the "cancel" button.
display_service_->SimulateClick(NotificationHandler::Type::DOWNLOAD,
notification_id(), 1, base::nullopt);
EXPECT_FALSE(notification());
EXPECT_EQ(0u, GetDownloadNotifications().size());
// Cancels the notification by clicking the "cancel' button.
NotificationRemoveObserver notification_close_observer;
notification()->ButtonClick(1);
EXPECT_EQ(notification_id(), notification_close_observer.Wait());
EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size());
// Confirms that a download is also cancelled.
// Confirms that the download is cancelled.
std::vector<content::DownloadItem*> downloads;
GetDownloadManager(browser())->GetAllDownloads(&downloads);
EXPECT_EQ(1u, downloads.size());
ASSERT_EQ(1u, downloads.size());
EXPECT_EQ(content::DownloadItem::CANCELLED, downloads[0]->GetState());
}
......@@ -989,30 +696,12 @@ IN_PROC_BROWSER_TEST_F(DownloadNotificationTest,
DownloadCancelledByUserExternally) {
CreateDownload();
// Cancels the notification by clicking the "cancel' button.
NotificationRemoveObserver notification_close_observer;
// Cancels the notification through the DownloadItem.
download_item()->Cancel(true /* by_user */);
EXPECT_EQ(notification_id(), notification_close_observer.Wait());
EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size());
// Confirms that a download is also cancelled.
std::vector<content::DownloadItem*> downloads;
GetDownloadManager(browser())->GetAllDownloads(&downloads);
EXPECT_EQ(1u, downloads.size());
EXPECT_EQ(content::DownloadItem::CANCELLED, downloads[0]->GetState());
}
IN_PROC_BROWSER_TEST_F(DownloadNotificationTest,
DownloadCancelledExternally) {
CreateDownload();
EXPECT_FALSE(notification());
EXPECT_EQ(0u, GetDownloadNotifications().size());
// Cancels the notification by clicking the "cancel' button.
NotificationRemoveObserver notification_close_observer;
download_item()->Cancel(false /* by_user */);
EXPECT_EQ(notification_id(), notification_close_observer.Wait());
EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size());
// Confirms that a download is also cancelled.
// Confirms that the download is cancelled.
std::vector<content::DownloadItem*> downloads;
GetDownloadManager(browser())->GetAllDownloads(&downloads);
EXPECT_EQ(1u, downloads.size());
......@@ -1030,41 +719,36 @@ IN_PROC_BROWSER_TEST_F(DownloadNotificationTest, IncognitoDownloadFile) {
EXPECT_EQ(l10n_util::GetStringFUTF16(
IDS_DOWNLOAD_STATUS_IN_PROGRESS_TITLE,
download_item()->GetFileNameToReportUser().LossyDisplayName()),
GetNotification(notification_id())->title());
EXPECT_EQ(message_center::NOTIFICATION_TYPE_PROGRESS,
GetNotification(notification_id())->type());
notification()->title());
EXPECT_EQ(message_center::NOTIFICATION_TYPE_PROGRESS, notification()->type());
EXPECT_TRUE(download_item()->GetBrowserContext()->IsOffTheRecord());
// Requests to complete the download.
content::DownloadTestObserverTerminal download_terminal_observer(
GetDownloadManager(incognito_browser()), 1,
content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL);
ui_test_utils::NavigateToURL(
incognito_browser(),
GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl));
// Waits for download completion.
NotificationUpdateObserver
download_change_notification_observer(notification_id());
while (download_item()->GetState() != content::DownloadItem::COMPLETE) {
download_change_notification_observer.Wait();
download_change_notification_observer.Reset();
}
download_terminal_observer.WaitForFinished();
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_COMPLETE_TITLE),
GetNotification(notification_id())->title());
notification()->title());
EXPECT_EQ(download_item()->GetFileNameToReportUser().LossyDisplayName(),
GetNotification(notification_id())->message());
notification()->message());
EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT,
GetNotification(notification_id())->type());
// Opens the message center.
GetMessageCenter()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER);
notification()->type());
// Try to open the downloaded item by clicking the notification.
EXPECT_TRUE(incognito_display_service_->GetNotification(notification_id()));
EXPECT_FALSE(GetIncognitoDownloadManagerDelegate()->opened());
GetMessageCenter()->ClickOnNotification(notification_id());
incognito_display_service_->SimulateClick(NotificationHandler::Type::DOWNLOAD,
notification_id(), base::nullopt,
base::nullopt);
EXPECT_TRUE(GetIncognitoDownloadManagerDelegate()->opened());
EXPECT_FALSE(GetDownloadManagerDelegate()->opened());
EXPECT_FALSE(GetNotification(notification_id()));
EXPECT_FALSE(incognito_display_service_->GetNotification(notification_id()));
chrome::CloseWindow(incognito_browser());
}
......@@ -1076,11 +760,13 @@ IN_PROC_BROWSER_TEST_F(DownloadNotificationTest,
GURL url_normal(net::URLRequestSlowDownloadJob::kKnownSizeUrl);
// Starts the incognito download.
NotificationAddObserver download_start_notification_observer1;
ui_test_utils::NavigateToURL(incognito_browser(), url_incognito);
EXPECT_TRUE(download_start_notification_observer1.Wait());
std::string notification_id1 =
download_start_notification_observer1.notification_id();
WaitForDownloadNotification(incognito_browser());
auto incognito_notifications =
incognito_display_service_->GetDisplayedNotificationsForType(
NotificationHandler::Type::DOWNLOAD);
ASSERT_EQ(1u, incognito_notifications.size());
std::string notification_id1 = incognito_notifications[0].id();
EXPECT_FALSE(notification_id1.empty());
// Confirms that there is a download.
......@@ -1093,11 +779,11 @@ IN_PROC_BROWSER_TEST_F(DownloadNotificationTest,
content::DownloadItem* download_incognito = downloads[0];
// Starts the normal download.
NotificationAddObserver download_start_notification_observer2;
ui_test_utils::NavigateToURL(browser(), url_normal);
EXPECT_TRUE(download_start_notification_observer2.Wait());
std::string notification_id2 =
download_start_notification_observer2.notification_id();
WaitForDownloadNotification();
auto normal_notifications = GetDownloadNotifications();
ASSERT_EQ(1u, normal_notifications.size());
std::string notification_id2 = normal_notifications[0].id();
EXPECT_FALSE(notification_id2.empty());
// Confirms that there are 2 downloads.
......@@ -1112,33 +798,34 @@ IN_PROC_BROWSER_TEST_F(DownloadNotificationTest,
EXPECT_EQ(download_incognito, downloads[0]);
// Confirms the types of download notifications are correct.
auto incognito_notification =
incognito_display_service_->GetNotification(notification_id1);
ASSERT_TRUE(incognito_notification);
EXPECT_EQ(message_center::NOTIFICATION_TYPE_PROGRESS,
GetNotification(notification_id1)->type());
EXPECT_EQ(-1, GetNotification(notification_id1)->progress());
incognito_notification->type());
EXPECT_EQ(-1, incognito_notification->progress());
auto normal_notification =
display_service_->GetNotification(notification_id2);
ASSERT_TRUE(normal_notification);
EXPECT_EQ(message_center::NOTIFICATION_TYPE_PROGRESS,
GetNotification(notification_id2)->type());
EXPECT_LE(0, GetNotification(notification_id2)->progress());
normal_notification->type());
EXPECT_LE(0, normal_notification->progress());
EXPECT_TRUE(download_incognito->GetBrowserContext()->IsOffTheRecord());
EXPECT_FALSE(download_normal->GetBrowserContext()->IsOffTheRecord());
// Requests to complete the downloads.
ui_test_utils::NavigateToURL(
browser(), GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl));
// Waits for the completion of downloads.
NotificationUpdateObserver download_change_notification_observer;
while (download_normal->GetState() != content::DownloadItem::COMPLETE ||
download_incognito->GetState() != content::DownloadItem::COMPLETE) {
download_change_notification_observer.Wait();
download_change_notification_observer.Reset();
}
// Request to complete the normal download.
CompleteTheDownload();
// Confirms the types of download notifications are correct.
incognito_notification =
incognito_display_service_->GetNotification(notification_id1);
EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT,
GetNotification(notification_id1)->type());
incognito_notification->type());
normal_notification = display_service_->GetNotification(notification_id2);
EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT,
GetNotification(notification_id2)->type());
normal_notification->type());
chrome::CloseWindow(incognito_browser());
}
......@@ -1150,7 +837,8 @@ IN_PROC_BROWSER_TEST_F(DownloadNotificationTest,
class MultiProfileDownloadNotificationTest
: public DownloadNotificationTestBase {
public:
~MultiProfileDownloadNotificationTest() override {}
MultiProfileDownloadNotificationTest() = default;
~MultiProfileDownloadNotificationTest() override = default;
void SetUpCommandLine(base::CommandLine* command_line) override {
DownloadNotificationTestBase::SetUpCommandLine(command_line);
......@@ -1197,6 +885,12 @@ class MultiProfileDownloadNotificationTest
chromeos::ProfileHelper::GetProfileByUserIdHashForTest(info.hash))
->SetAuthenticatedAccountInfo(info.gaia_id, info.email);
}
std::unique_ptr<NotificationDisplayServiceTester> display_service1_;
std::unique_ptr<NotificationDisplayServiceTester> display_service2_;
private:
DISALLOW_COPY_AND_ASSIGN(MultiProfileDownloadNotificationTest);
};
IN_PROC_BROWSER_TEST_F(MultiProfileDownloadNotificationTest,
......@@ -1216,10 +910,14 @@ IN_PROC_BROWSER_TEST_F(MultiProfileDownloadNotificationTest,
Browser* browser2 = CreateBrowser(profile2);
EXPECT_NE(browser1, browser2);
display_service1_ =
std::make_unique<NotificationDisplayServiceTester>(profile1);
display_service2_ =
std::make_unique<NotificationDisplayServiceTester>(profile2);
// First user starts a download.
NotificationAddObserver download_start_notification_observer1;
ui_test_utils::NavigateToURL(browser1, url);
download_start_notification_observer1.Wait();
WaitForDownloadNotificationForDisplayService(display_service1_.get());
// Confirms that the download is started.
std::vector<content::DownloadItem*> downloads;
......@@ -1228,17 +926,20 @@ IN_PROC_BROWSER_TEST_F(MultiProfileDownloadNotificationTest,
content::DownloadItem* download1 = downloads[0];
// Confirms that a download notification is generated.
std::string notification_id_user1 =
download_start_notification_observer1.notification_id();
auto notifications1 = display_service1_->GetDisplayedNotificationsForType(
NotificationHandler::Type::DOWNLOAD);
ASSERT_EQ(1u, notifications1.size());
std::string notification_id_user1 = notifications1[0].id();
EXPECT_FALSE(notification_id_user1.empty());
// Second user starts a download.
NotificationAddObserver download_start_notification_observer2;
ui_test_utils::NavigateToURL(browser2, url);
download_start_notification_observer2.Wait();
std::string notification_id_user2_1 =
download_start_notification_observer2.notification_id();
EXPECT_FALSE(notification_id_user2_1.empty());
WaitForDownloadNotificationForDisplayService(display_service2_.get());
auto notifications2 = display_service2_->GetDisplayedNotificationsForType(
NotificationHandler::Type::DOWNLOAD);
ASSERT_EQ(1u, notifications2.size());
std::string notification_id_user2 = notifications2[0].id();
EXPECT_FALSE(notification_id_user2.empty());
// Confirms that the second user has only 1 download.
downloads.clear();
......@@ -1246,12 +947,11 @@ IN_PROC_BROWSER_TEST_F(MultiProfileDownloadNotificationTest,
ASSERT_EQ(1u, downloads.size());
// Second user starts another download.
NotificationAddObserver download_start_notification_observer3;
ui_test_utils::NavigateToURL(browser2, url);
download_start_notification_observer3.Wait();
std::string notification_id_user2_2 =
download_start_notification_observer3.notification_id();
EXPECT_FALSE(notification_id_user2_2.empty());
WaitForDownloadNotificationForDisplayService(display_service2_.get());
notifications2 = display_service2_->GetDisplayedNotificationsForType(
NotificationHandler::Type::DOWNLOAD);
ASSERT_EQ(2u, notifications2.size());
// Confirms that the second user has 2 downloads.
downloads.clear();
......@@ -1272,39 +972,41 @@ IN_PROC_BROWSER_TEST_F(MultiProfileDownloadNotificationTest,
// Confirms the types of download notifications are correct.
// Normal notification for user1.
EXPECT_EQ(message_center::NOTIFICATION_TYPE_PROGRESS,
GetNotification(notification_id_user1)->type());
EXPECT_EQ(-1, GetNotification(notification_id_user1)->progress());
// Normal notification for user2.
EXPECT_EQ(message_center::NOTIFICATION_TYPE_PROGRESS,
GetNotification(notification_id_user2_1)->type());
EXPECT_EQ(-1, GetNotification(notification_id_user2_1)->progress());
EXPECT_EQ(message_center::NOTIFICATION_TYPE_PROGRESS,
GetNotification(notification_id_user2_2)->type());
EXPECT_EQ(-1, GetNotification(notification_id_user2_2)->progress());
display_service1_->GetNotification(notification_id_user1)->type());
EXPECT_EQ(
-1,
display_service1_->GetNotification(notification_id_user1)->progress());
// Normal notifications for user2.
notifications2 = display_service2_->GetDisplayedNotificationsForType(
NotificationHandler::Type::DOWNLOAD);
EXPECT_EQ(2u, notifications2.size());
for (const auto& notification : notifications2) {
EXPECT_EQ(message_center::NOTIFICATION_TYPE_PROGRESS, notification.type());
EXPECT_EQ(-1, notification.progress());
}
// Requests to complete the downloads.
NotificationUpdateObserver download_change_notification_observer;
content::DownloadTestObserverTerminal download_terminal_observer(
GetDownloadManager(browser1), 1u /* wait_count */,
content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL);
content::DownloadTestObserverTerminal download_terminal_observer2(
GetDownloadManager(browser2), 2u /* wait_count */,
content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL);
ui_test_utils::NavigateToURL(
browser(), GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl));
// Waits for the completion of downloads.
while (download1->GetState() != content::DownloadItem::COMPLETE ||
download2->GetState() != content::DownloadItem::COMPLETE ||
download3->GetState() != content::DownloadItem::COMPLETE) {
// Requests again, since sometimes the request may fail.
ui_test_utils::NavigateToURL(
browser(), GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl));
download_change_notification_observer.Wait();
}
browser1, GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl));
ui_test_utils::NavigateToURL(
browser2, GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl));
download_terminal_observer.WaitForFinished();
download_terminal_observer2.WaitForFinished();
// Confirms the types of download notifications are correct.
EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT,
GetNotification(notification_id_user1)->type());
EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT,
GetNotification(notification_id_user2_1)->type());
// Normal notifications for user2.
EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT,
GetNotification(notification_id_user2_1)->type());
EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT,
GetNotification(notification_id_user2_2)->type());
display_service1_->GetNotification(notification_id_user1)->type());
notifications2 = display_service2_->GetDisplayedNotificationsForType(
NotificationHandler::Type::DOWNLOAD);
EXPECT_EQ(2u, notifications2.size());
for (const auto& notification : notifications2) {
EXPECT_EQ(message_center::NOTIFICATION_TYPE_BASE_FORMAT,
notification.type());
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment