Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(542)

Unified Diff: chrome/browser/download/notification/download_notification_item_unittest.cc

Issue 852043002: Initial Implementation of Download Notification (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments & added tests. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/download/notification/download_notification_item_unittest.cc
diff --git a/chrome/browser/download/notification/download_notification_item_unittest.cc b/chrome/browser/download/notification/download_notification_item_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..30a779dcb11459118021be658d58a73e30c7ae8b
--- /dev/null
+++ b/chrome/browser/download/notification/download_notification_item_unittest.cc
@@ -0,0 +1,257 @@
+// 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_notification_item.h"
+
+#include "base/run_loop.h"
+#include "base/test/test_simple_task_runner.h"
+#include "base/thread_task_runner_handle.h"
+#include "content/public/test/mock_download_item.h"
+#include "content/public/test/mock_download_manager.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "ui/message_center/fake_message_center.h"
+
+using testing::NiceMock;
+using testing::Return;
+using testing::_;
+
+namespace {
+
+class MockDownloadNotificationItemDelegate :
+ public DownloadNotificationItem::Delegate {
+ public:
+ MockDownloadNotificationItemDelegate() :
+ on_download_removed_call_count_(0u),
+ on_download_started_call_count_(0u),
+ on_download_stopped_call_count_(0u) {}
+
+ void OnDownloadRemoved(DownloadNotificationItem* item) override {
+ on_download_removed_call_count_++;
+ }
+
+ void OnDownloadStarted(DownloadNotificationItem* item) override {
+ on_download_started_call_count_++;
+ }
+
+ void OnDownloadStopped(DownloadNotificationItem* item) override {
+ on_download_stopped_call_count_++;
+ }
+
+ size_t GetOnDownloadRemovedCallCount() {
+ return on_download_removed_call_count_;
+ }
+
+ size_t GetOnDownloadStartedCallCount() {
+ return on_download_started_call_count_;
+ }
+
+ size_t GetOnDownloadStoppedCallCount() {
+ return on_download_stopped_call_count_;
+ }
+
+ private:
+ size_t on_download_removed_call_count_;
+ size_t on_download_started_call_count_;
+ size_t on_download_stopped_call_count_;
+};
+
+} // anonymous namespace
+
+namespace test {
+
+class DownloadNotificationItemTest : public testing::Test {
+ public:
+ DownloadNotificationItemTest() :
+ runner_(new base::TestSimpleTaskRunner),
+ runner_handler_(runner_) {}
+
+ void SetUp() override {
+ testing::Test::SetUp();
+ message_center::MessageCenter::Initialize();
+
+ item_.reset(new NiceMock<content::MockDownloadItem>());
+ ON_CALL(*item_, GetId()).WillByDefault(Return(12345));
+ ON_CALL(*item_, GetState())
+ .WillByDefault(Return(content::DownloadItem::IN_PROGRESS));
+ ON_CALL(*item_, IsDangerous()).WillByDefault(Return(false));
+ ON_CALL(*item_, GetFileNameToReportUser())
+ .WillByDefault(Return(base::FilePath("TITLE.bin")));
+ ON_CALL(*item_, GetDangerType())
+ .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
+ ON_CALL(*item_, IsDone()).WillByDefault(Return(false));
+ }
+
+ void TearDown() override {
+ message_center::MessageCenter::Shutdown();
+ testing::Test::TearDown();
+ }
+
+ protected:
+ message_center::MessageCenter* message_center() {
+ return message_center::MessageCenter::Get();
+ }
+
+ std::string notification_id() {
+ return notification_item_->notification_->id();
+ }
+
+ message_center::Notification* notification() {
+ return message_center()->FindVisibleNotificationById(notification_id());
+ }
+
+ // Trampoline methods to access a private method in DownloadNotificationItem.
+ void NotificationItemClick() {
+ return notification_item_->Click();
+ }
+ void NotificationItemButtonClick(int index) {
+ return notification_item_->ButtonClick(index);
+ }
+
+ bool IsPopupNotification(const std::string& notification_id) {
+ message_center::NotificationList::PopupNotifications popups =
+ message_center()->GetPopupNotifications();
+ for (auto it = popups.begin(); it != popups.end(); it++) {
+ if ((*it)->id() == notification_id) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ scoped_refptr<base::TestSimpleTaskRunner> runner_;
+ base::ThreadTaskRunnerHandle runner_handler_;
+
+ MockDownloadNotificationItemDelegate delegate_;
+ scoped_ptr<NiceMock<content::MockDownloadItem> > item_;
+ scoped_refptr<DownloadNotificationItem> notification_item_;
+};
+
+TEST_F(DownloadNotificationItemTest, ShowAndCloseNotificationTest) {
+ EXPECT_EQ(0u, message_center()->NotificationCount());
+
+ // Shows a notification
+ notification_item_ = new DownloadNotificationItem(item_.get(), &delegate_);
+ item_->NotifyObserversDownloadOpened();
+
+ // Confirms that the notification is shown as a popup.
+ EXPECT_EQ(1u, message_center()->NotificationCount());
+ EXPECT_TRUE(IsPopupNotification(notification_id()));
+
+ // Makes sure the DownloadItem::Close() is not called.
asanka 2015/02/20 23:34:40 DownloadItem::Cancel(). Here and below.
yoshiki 2015/02/24 21:30:57 Done.
+ EXPECT_CALL(*item_, Cancel(_)).Times(0);
+ // Closes it once.
+ message_center()->RemoveNotification(notification_id(), true);
+
+ // Confirms that the notification is shown but is not a popup.
+ EXPECT_EQ(1u, message_center()->NotificationCount());
+ EXPECT_FALSE(IsPopupNotification(notification_id()));
+
+ // Makes sure the DownloadItem::Close() is called once.
+ EXPECT_CALL(*item_, Cancel(_)).Times(1);
+ // Closes it again.
+ message_center()->RemoveNotification(notification_id(), true);
+
+ // Confirms that the notification is closed.
+ EXPECT_EQ(0u, message_center()->NotificationCount());
+}
+
+TEST_F(DownloadNotificationItemTest, PauseAndResumeNotificationTest) {
+ // Shows a notification
+ notification_item_ = new DownloadNotificationItem(item_.get(), &delegate_);
+ item_->NotifyObserversDownloadOpened();
+
+ // Confirms that the notification is shown as a popup.
+ EXPECT_EQ(message_center()->NotificationCount(), 1u);
+ EXPECT_TRUE(IsPopupNotification(notification_id()));
+
+ // Pauses and makes sure the DownloadItem::Pause() is called.
+ EXPECT_CALL(*item_, Pause()).Times(1);
+ EXPECT_CALL(*item_, IsPaused()).WillRepeatedly(Return(true));
+ NotificationItemButtonClick(0);
+ item_->NotifyObserversDownloadUpdated();
+
+ // Resumes and makes sure the DownloadItem::Resume() is called.
+ EXPECT_CALL(*item_, Resume()).Times(1);
+ EXPECT_CALL(*item_, IsPaused()).WillRepeatedly(Return(false));
+ NotificationItemButtonClick(0);
+ item_->NotifyObserversDownloadUpdated();
+}
+
+TEST_F(DownloadNotificationItemTest, OpenDownloadTest) {
asanka 2015/02/20 23:34:40 Nit: Individual tests don't need to be called *Tes
yoshiki 2015/02/24 21:30:57 Done.
+ EXPECT_CALL(*item_, GetState())
+ .WillRepeatedly(Return(content::DownloadItem::COMPLETE));
+ EXPECT_CALL(*item_, IsDone()).WillRepeatedly(Return(true));
+
+ // Shows a notification
+ notification_item_ = new DownloadNotificationItem(item_.get(), &delegate_);
+ item_->NotifyObserversDownloadOpened();
+ item_->NotifyObserversDownloadUpdated();
+
+ // Clicks and confirms that the OpenDownload() is called.
+ EXPECT_CALL(*item_, OpenDownload()).Times(1);
+ EXPECT_CALL(*item_, SetOpenWhenComplete(_)).Times(0);
+ NotificationItemClick();
+}
+
+TEST_F(DownloadNotificationItemTest, OpenWhenCompleteTest) {
+ // Shows a notification
+ notification_item_ = new DownloadNotificationItem(item_.get(), &delegate_);
+ item_->NotifyObserversDownloadOpened();
+
+ EXPECT_CALL(*item_, OpenDownload()).Times(0);
+
+ // Toggles open-when-complete (new value: true).
+ EXPECT_CALL(*item_, SetOpenWhenComplete(true)).Times(1).WillOnce(Return());
+ NotificationItemClick();
+ EXPECT_CALL(*item_, GetOpenWhenComplete()).WillRepeatedly(Return(true));
+
+ // Toggles open-when-complete (new value: true).
asanka 2015/02/20 23:34:40 Nit: new value is false?
yoshiki 2015/02/24 21:30:57 Done.
+ EXPECT_CALL(*item_, SetOpenWhenComplete(false)).Times(1).WillOnce(Return());
+ NotificationItemClick();
+ EXPECT_CALL(*item_, GetOpenWhenComplete()).WillRepeatedly(Return(false));
+
+ // Sets open-when-complete.
+ EXPECT_CALL(*item_, SetOpenWhenComplete(true)).Times(1).WillOnce(Return());
+ NotificationItemClick();
+ EXPECT_CALL(*item_, GetOpenWhenComplete()).WillRepeatedly(Return(true));
+
+ // Downloading is completed.
+ EXPECT_CALL(*item_, GetState())
+ .WillRepeatedly(Return(content::DownloadItem::COMPLETE));
+ EXPECT_CALL(*item_, IsDone()).WillRepeatedly(Return(true));
+ item_->NotifyObserversDownloadUpdated();
+
+ // DownloadItem::OpenDownload must not be called since the file opens
+ // automatically due to the open-when-complete flag.
+
+ item_->NotifyObserversDownloadRemoved();
+}
+
+TEST_F(DownloadNotificationItemTest, DownloadNotificationItemDelegateTest) {
+ // Shows a notification
+ EXPECT_EQ(0u, delegate_.GetOnDownloadStartedCallCount());
+ notification_item_ = new DownloadNotificationItem(item_.get(), &delegate_);
+ EXPECT_EQ(1u, delegate_.GetOnDownloadStartedCallCount());
+
+ item_->NotifyObserversDownloadOpened();
+
asanka 2015/02/20 23:34:40 Extra whitespace
yoshiki 2015/02/24 21:30:57 Done.
+ item_->NotifyObserversDownloadUpdated();
+
+ // Check OnDownloadStopped().
+ EXPECT_EQ(0u, delegate_.GetOnDownloadStoppedCallCount());
+ EXPECT_CALL(*item_, GetState())
+ .WillRepeatedly(Return(content::DownloadItem::COMPLETE));
+ EXPECT_CALL(*item_, IsDone()).WillRepeatedly(Return(true));
+ item_->NotifyObserversDownloadUpdated();
+ EXPECT_EQ(1u, delegate_.GetOnDownloadStoppedCallCount());
+
+ EXPECT_EQ(0u, delegate_.GetOnDownloadRemovedCallCount());
+ item_->NotifyObserversDownloadRemoved();
+ EXPECT_EQ(1u, delegate_.GetOnDownloadRemovedCallCount());
+
+ item_.reset();
+}
+
+} // namespace test

Powered by Google App Engine
This is Rietveld 408576698