| // Copyright 2015 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "chrome/browser/download/notification/download_item_notification.h" |
| |
| #include <stddef.h> |
| |
| #include <memory> |
| #include <utility> |
| |
| #include "base/guid.h" |
| #include "base/json/json_reader.h" |
| #include "base/macros.h" |
| #include "base/run_loop.h" |
| #include "base/test/metrics/histogram_tester.h" |
| #include "base/test/test_simple_task_runner.h" |
| #include "base/threading/thread_task_runner_handle.h" |
| #include "chrome/browser/download/chrome_download_manager_delegate.h" |
| #include "chrome/browser/download/notification/download_notification_manager.h" |
| #include "chrome/browser/download/offline_item_utils.h" |
| #include "chrome/browser/notifications/notification_display_service.h" |
| #include "chrome/browser/notifications/notification_display_service_factory.h" |
| #include "chrome/browser/notifications/notification_display_service_tester.h" |
| #include "chrome/browser/notifications/notification_handler.h" |
| #include "chrome/browser/notifications/notification_test_util.h" |
| #include "chrome/browser/notifications/platform_notification_service_impl.h" |
| #include "chrome/browser/safe_browsing/cloud_content_scanning/deep_scanning_test_utils.h" |
| #include "chrome/test/base/testing_browser_process.h" |
| #include "chrome/test/base/testing_profile.h" |
| #include "chrome/test/base/testing_profile_manager.h" |
| #include "components/download/public/common/download_danger_type.h" |
| #include "components/download/public/common/mock_download_item.h" |
| #include "components/enterprise/common/proto/download_item_reroute_info.pb.h" |
| #include "content/public/browser/download_item_utils.h" |
| #include "content/public/test/browser_task_environment.h" |
| #include "content/public/test/mock_download_manager.h" |
| #include "testing/gmock/include/gmock/gmock.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| |
| using testing::_; |
| using testing::NiceMock; |
| using testing::Return; |
| using testing::ReturnRefOfCopy; |
| using testing::ValuesIn; |
| using Provider = enterprise_connectors::FileSystemServiceProvider; |
| using RerouteInfo = enterprise_connectors::DownloadItemRerouteInfo; |
| |
| namespace { |
| |
| const base::FilePath::CharType kDownloadItemTargetPathString[] = |
| FILE_PATH_LITERAL("/tmp/TITLE.bin"); |
| |
| } // anonymouse namespace |
| |
| namespace test { |
| |
| class DownloadItemNotificationTest : public testing::Test { |
| public: |
| DownloadItemNotificationTest() : profile_(nullptr) {} |
| |
| void SetUp() override { |
| testing::Test::SetUp(); |
| |
| profile_manager_ = std::make_unique<TestingProfileManager>( |
| TestingBrowserProcess::GetGlobal()); |
| ASSERT_TRUE(profile_manager_->SetUp()); |
| profile_ = profile_manager_->CreateTestingProfile("test-user"); |
| |
| service_tester_ = |
| std::make_unique<NotificationDisplayServiceTester>(profile_); |
| |
| download_notification_manager_ = |
| std::make_unique<DownloadNotificationManager>(profile_); |
| |
| base::FilePath download_item_target_path(kDownloadItemTargetPathString); |
| download_item_ = std::make_unique<NiceMock<download::MockDownloadItem>>(); |
| ON_CALL(*download_item_, GetId()).WillByDefault(Return(12345)); |
| ON_CALL(*download_item_, GetGuid()) |
| .WillByDefault(ReturnRefOfCopy(base::GenerateGUID())); |
| ON_CALL(*download_item_, GetState()) |
| .WillByDefault(Return(download::DownloadItem::IN_PROGRESS)); |
| ON_CALL(*download_item_, IsDangerous()).WillByDefault(Return(false)); |
| ON_CALL(*download_item_, GetFileNameToReportUser()) |
| .WillByDefault(Return(base::FilePath("TITLE.bin"))); |
| ON_CALL(*download_item_, GetTargetFilePath()) |
| .WillByDefault(ReturnRefOfCopy(download_item_target_path)); |
| ON_CALL(*download_item_, GetRerouteInfo()) |
| .WillByDefault(ReturnRefOfCopy(RerouteInfo())); |
| ON_CALL(*download_item_, GetDangerType()) |
| .WillByDefault(Return(download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS)); |
| ON_CALL(*download_item_, IsDone()).WillByDefault(Return(false)); |
| ON_CALL(*download_item_, GetURL()).WillByDefault(ReturnRefOfCopy( |
| GURL("http://www.example.com/download.bin"))); |
| content::DownloadItemUtils::AttachInfo(download_item_.get(), profile_, |
| nullptr); |
| } |
| |
| void TearDown() override { |
| download_item_notification_ = nullptr; // will be free'd in the manager. |
| download_notification_manager_.reset(); |
| profile_manager_.reset(); |
| testing::Test::TearDown(); |
| } |
| |
| protected: |
| std::string notification_id() const { |
| return download_item_notification_->notification_->id(); |
| } |
| |
| std::unique_ptr<message_center::Notification> LookUpNotification() const { |
| std::vector<message_center::Notification> notifications = |
| service_tester_->GetDisplayedNotificationsForType( |
| NotificationHandler::Type::TRANSIENT); |
| for (const auto& notification : notifications) { |
| if (notification.id() == download_item_notification_->GetNotificationId()) |
| return std::make_unique<message_center::Notification>(notification); |
| } |
| return nullptr; |
| } |
| |
| size_t NotificationCount() const { |
| return service_tester_ |
| ->GetDisplayedNotificationsForType(NotificationHandler::Type::TRANSIENT) |
| .size(); |
| } |
| |
| void RemoveNotification() { |
| service_tester_->RemoveNotification( |
| NotificationHandler::Type::TRANSIENT, |
| download_item_notification_->GetNotificationId(), false); |
| } |
| |
| void CreateDownloadItemNotification() { |
| offline_items_collection::ContentId id( |
| OfflineItemUtils::GetDownloadNamespacePrefix( |
| profile_->IsOffTheRecord()), |
| download_item_->GetGuid()); |
| download_notification_manager_->OnNewDownloadReady(download_item_.get()); |
| |
| download_item_notification_ = |
| download_notification_manager_->items_.at(id).get(); |
| NotificationDisplayServiceFactory::GetForProfile(profile_)->Display( |
| NotificationHandler::Type::TRANSIENT, |
| *download_item_notification_->notification_, /*metadata=*/nullptr); |
| } |
| |
| content::BrowserTaskEnvironment task_environment_; |
| |
| std::unique_ptr<TestingProfileManager> profile_manager_; |
| Profile* profile_; |
| |
| std::unique_ptr<NiceMock<download::MockDownloadItem>> download_item_; |
| std::unique_ptr<DownloadNotificationManager> download_notification_manager_; |
| DownloadItemNotification* download_item_notification_; |
| std::unique_ptr<NotificationDisplayServiceTester> service_tester_; |
| }; |
| |
| TEST_F(DownloadItemNotificationTest, ShowAndCloseNotification) { |
| base::HistogramTester histograms; |
| EXPECT_EQ(0u, NotificationCount()); |
| |
| // Shows a notification |
| CreateDownloadItemNotification(); |
| download_item_->NotifyObserversDownloadOpened(); |
| EXPECT_EQ(1u, NotificationCount()); |
| |
| // Makes sure the DownloadItem::Cancel() is not called. |
| EXPECT_CALL(*download_item_, Cancel(_)).Times(0); |
| // Closes it once. |
| RemoveNotification(); |
| |
| // Confirms that the notification is closed. |
| EXPECT_EQ(0u, NotificationCount()); |
| |
| // Makes sure the DownloadItem::Cancel() is never called. |
| EXPECT_CALL(*download_item_, Cancel(_)).Times(0); |
| |
| // Not logged because the download is safe. |
| histograms.ExpectTotalCount("Download.ShowedDownloadWarning", 0); |
| } |
| |
| TEST_F(DownloadItemNotificationTest, ShowAndCloseDangerousNotification) { |
| base::HistogramTester histograms; |
| EXPECT_CALL(*download_item_, GetDangerType()) |
| .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT)); |
| EXPECT_CALL(*download_item_, IsDangerous()).WillRepeatedly(Return(true)); |
| |
| // Shows a notification |
| CreateDownloadItemNotification(); |
| download_item_->NotifyObserversDownloadOpened(); |
| EXPECT_EQ(1u, NotificationCount()); |
| |
| // Closes it once. |
| RemoveNotification(); |
| |
| // Confirms that the notification is closed. |
| EXPECT_EQ(0u, NotificationCount()); |
| |
| // The download warning showed histogram is logged. |
| histograms.ExpectBucketCount("Download.ShowedDownloadWarning", |
| download::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT, |
| 1); |
| } |
| |
| TEST_F(DownloadItemNotificationTest, PauseAndResumeNotification) { |
| // Shows a notification |
| CreateDownloadItemNotification(); |
| download_item_->NotifyObserversDownloadOpened(); |
| |
| // Confirms that the notification is shown as a popup. |
| EXPECT_EQ(1u, NotificationCount()); |
| |
| // Pauses and makes sure the DownloadItem::Pause() is called. |
| EXPECT_CALL(*download_item_, Pause()).Times(1); |
| EXPECT_CALL(*download_item_, IsPaused()).WillRepeatedly(Return(true)); |
| download_item_notification_->Click(0, absl::nullopt); |
| download_item_->NotifyObserversDownloadUpdated(); |
| |
| // Resumes and makes sure the DownloadItem::Resume() is called. |
| EXPECT_CALL(*download_item_, Resume(true)).Times(1); |
| EXPECT_CALL(*download_item_, IsPaused()).WillRepeatedly(Return(false)); |
| download_item_notification_->Click(0, absl::nullopt); |
| download_item_->NotifyObserversDownloadUpdated(); |
| } |
| |
| TEST_F(DownloadItemNotificationTest, OpenDownload) { |
| EXPECT_CALL(*download_item_, GetState()) |
| .WillRepeatedly(Return(download::DownloadItem::COMPLETE)); |
| EXPECT_CALL(*download_item_, IsDone()).WillRepeatedly(Return(true)); |
| |
| // Shows a notification. |
| CreateDownloadItemNotification(); |
| download_item_->NotifyObserversDownloadOpened(); |
| download_item_->NotifyObserversDownloadUpdated(); |
| |
| // Clicks and confirms that the OpenDownload() is called. |
| EXPECT_CALL(*download_item_, OpenDownload()).Times(1); |
| EXPECT_CALL(*download_item_, SetOpenWhenComplete(_)).Times(0); |
| download_item_notification_->Click(absl::nullopt, absl::nullopt); |
| } |
| |
| TEST_F(DownloadItemNotificationTest, OpenWhenComplete) { |
| // Shows a notification |
| CreateDownloadItemNotification(); |
| download_item_->NotifyObserversDownloadOpened(); |
| |
| EXPECT_CALL(*download_item_, OpenDownload()).Times(0); |
| |
| // Toggles open-when-complete (new value: true). |
| EXPECT_CALL(*download_item_, SetOpenWhenComplete(true)) |
| .Times(1) |
| .WillOnce(Return()); |
| download_item_notification_->Click(absl::nullopt, absl::nullopt); |
| EXPECT_CALL(*download_item_, GetOpenWhenComplete()) |
| .WillRepeatedly(Return(true)); |
| |
| // Toggles open-when-complete (new value: false). |
| EXPECT_CALL(*download_item_, SetOpenWhenComplete(false)) |
| .Times(1) |
| .WillOnce(Return()); |
| download_item_notification_->Click(absl::nullopt, absl::nullopt); |
| EXPECT_CALL(*download_item_, GetOpenWhenComplete()) |
| .WillRepeatedly(Return(false)); |
| |
| // Sets open-when-complete. |
| EXPECT_CALL(*download_item_, SetOpenWhenComplete(true)) |
| .Times(1) |
| .WillOnce(Return()); |
| download_item_notification_->Click(absl::nullopt, absl::nullopt); |
| EXPECT_CALL(*download_item_, GetOpenWhenComplete()) |
| .WillRepeatedly(Return(true)); |
| |
| // Downloading is completed. |
| EXPECT_CALL(*download_item_, GetState()) |
| .WillRepeatedly(Return(download::DownloadItem::COMPLETE)); |
| EXPECT_CALL(*download_item_, IsDone()).WillRepeatedly(Return(true)); |
| download_item_->NotifyObserversDownloadUpdated(); |
| |
| // DownloadItem::OpenDownload must not be called since the file opens |
| // automatically due to the open-when-complete flag. |
| } |
| |
| TEST_F(DownloadItemNotificationTest, DisablePopup) { |
| CreateDownloadItemNotification(); |
| download_item_->NotifyObserversDownloadOpened(); |
| |
| EXPECT_EQ(message_center::DEFAULT_PRIORITY, LookUpNotification()->priority()); |
| |
| download_item_notification_->DisablePopup(); |
| // Priority is low. |
| EXPECT_EQ(message_center::LOW_PRIORITY, LookUpNotification()->priority()); |
| |
| // Downloading is completed. |
| EXPECT_CALL(*download_item_, GetState()) |
| .WillRepeatedly(Return(download::DownloadItem::COMPLETE)); |
| EXPECT_CALL(*download_item_, IsDone()).WillRepeatedly(Return(true)); |
| download_item_->NotifyObserversDownloadUpdated(); |
| |
| // Priority is increased by the download's completion. |
| EXPECT_GT(LookUpNotification()->priority(), message_center::LOW_PRIORITY); |
| } |
| |
| TEST_F(DownloadItemNotificationTest, DeepScanning) { |
| // Setup deep scanning in progress. |
| EXPECT_CALL(*download_item_, GetDangerType()) |
| .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_ASYNC_SCANNING)); |
| auto state = |
| std::make_unique<ChromeDownloadManagerDelegate::SafeBrowsingState>(); |
| download_item_->SetUserData(&ChromeDownloadManagerDelegate:: |
| SafeBrowsingState::kSafeBrowsingUserDataKey, |
| std::move(state)); |
| CreateDownloadItemNotification(); |
| |
| // Can't open while scanning. |
| safe_browsing::SetAnalysisConnector(profile_->GetPrefs(), |
| enterprise_connectors::FILE_DOWNLOADED, |
| R"( |
| { |
| "service_provider": "google", |
| "enable": [{"url_list": ["*"], "tags": ["malware"]}], |
| "block_until_verdict": 1 |
| } |
| )"); |
| EXPECT_CALL(*download_item_, OpenDownload()).Times(0); |
| EXPECT_CALL(*download_item_, SetOpenWhenComplete(true)).Times(1); |
| download_item_notification_->Click(absl::nullopt, absl::nullopt); |
| |
| // Can be opened while scanning. |
| safe_browsing::SetAnalysisConnector(profile_->GetPrefs(), |
| enterprise_connectors::FILE_DOWNLOADED, |
| R"( |
| { |
| "service_provider": "google", |
| "enable": [{"url_list": ["*"], "tags": ["malware"]}], |
| "block_until_verdict": 0 |
| } |
| )"); |
| EXPECT_CALL(*download_item_, OpenDownload()).Times(1); |
| download_item_notification_->Click(absl::nullopt, absl::nullopt); |
| |
| // Scanning finished, warning. |
| EXPECT_CALL(*download_item_, IsDangerous()).WillRepeatedly(Return(true)); |
| EXPECT_CALL(*download_item_, GetDangerType()) |
| .WillRepeatedly( |
| Return(download::DOWNLOAD_DANGER_TYPE_SENSITIVE_CONTENT_WARNING)); |
| EXPECT_CALL(*download_item_, OpenDownload()).Times(0); |
| EXPECT_CALL(*download_item_, SetOpenWhenComplete(true)).Times(0); |
| download_item_notification_->Click(absl::nullopt, absl::nullopt); |
| |
| // Scanning finished, blocked. |
| EXPECT_CALL(*download_item_, IsDangerous()).WillRepeatedly(Return(true)); |
| EXPECT_CALL(*download_item_, GetDangerType()) |
| .WillRepeatedly( |
| Return(download::DOWNLOAD_DANGER_TYPE_SENSITIVE_CONTENT_BLOCK)); |
| EXPECT_CALL(*download_item_, OpenDownload()).Times(0); |
| EXPECT_CALL(*download_item_, SetOpenWhenComplete(true)).Times(0); |
| download_item_notification_->Click(absl::nullopt, absl::nullopt); |
| |
| // Scanning finished, safe. |
| EXPECT_CALL(*download_item_, IsDangerous()).WillRepeatedly(Return(false)); |
| EXPECT_CALL(*download_item_, GetDangerType()) |
| .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_DEEP_SCANNED_SAFE)); |
| EXPECT_CALL(*download_item_, GetState()) |
| .WillRepeatedly(Return(download::DownloadItem::COMPLETE)); |
| EXPECT_CALL(*download_item_, OpenDownload()).Times(1); |
| download_item_notification_->Click(absl::nullopt, absl::nullopt); |
| } |
| |
| struct FileReroutedTestCase { |
| download::DownloadItem::DownloadState state; |
| download::DownloadInterruptReason reason; |
| RerouteInfo reroute_info; |
| }; |
| |
| RerouteInfo MakeTestRerouteInfo(std::string file_id = std::string()) { |
| RerouteInfo info; |
| info.set_service_provider(Provider::BOX); |
| if (file_id.size()) |
| info.mutable_box()->set_file_id(file_id); |
| return info; |
| } |
| |
| RerouteInfo MakeTestRerouteInfoWithError(std::string error_message) { |
| RerouteInfo info; |
| info.set_service_provider(Provider::BOX); |
| info.mutable_box()->set_error_message(error_message); |
| return info; |
| } |
| |
| class DownloadItemNotificationParametrizedTest |
| : public DownloadItemNotificationTest, |
| public ::testing::WithParamInterface<FileReroutedTestCase> {}; |
| |
| TEST_P(DownloadItemNotificationParametrizedTest, |
| CreateDownloadItemNotification) { |
| RerouteInfo reroute_info; |
| reroute_info.set_service_provider(Provider::BOX); |
| |
| // Setup file rerouted to Box info. |
| EXPECT_CALL(*download_item_, GetRerouteInfo()) |
| .WillRepeatedly(ReturnRefOfCopy(GetParam().reroute_info)); |
| EXPECT_CALL(*download_item_, GetState()) |
| .WillRepeatedly(Return(GetParam().state)); |
| |
| switch (GetParam().state) { |
| case (download::DownloadItem::INTERRUPTED): |
| EXPECT_CALL(*download_item_, GetLastReason()) |
| .WillRepeatedly(Return(GetParam().reason)); |
| break; |
| case (download::DownloadItem::COMPLETE): |
| EXPECT_CALL(*download_item_, IsDone()).WillRepeatedly(Return(true)); |
| FALLTHROUGH; |
| default: |
| EXPECT_CALL(*download_item_, GetLastReason()).Times(0); |
| } |
| |
| // Show the download item notification. |
| CreateDownloadItemNotification(); |
| download_item_->NotifyObserversDownloadOpened(); |
| } |
| |
| const FileReroutedTestCase kFileReroutedTestCases[] = { |
| {download::DownloadItem::DownloadState::IN_PROGRESS, |
| download::DOWNLOAD_INTERRUPT_REASON_NONE, MakeTestRerouteInfo()}, |
| {download::DownloadItem::DownloadState::INTERRUPTED, |
| download::DOWNLOAD_INTERRUPT_REASON_NETWORK_TIMEOUT, |
| MakeTestRerouteInfo()}, |
| {download::DownloadItem::DownloadState::INTERRUPTED, |
| download::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED, |
| MakeTestRerouteInfoWithError("400 - \"item_name_invalid\"")}, |
| {download::DownloadItem::DownloadState::COMPLETE, |
| download::DOWNLOAD_INTERRUPT_REASON_NONE, MakeTestRerouteInfo("13579")}}; |
| |
| INSTANTIATE_TEST_SUITE_P(ReroutedByFileSystemConnectorTest, |
| DownloadItemNotificationParametrizedTest, |
| ValuesIn(kFileReroutedTestCases)); |
| } // namespace test |