Give names to all utility processes.
[chromium-blink-merge.git] / chrome / browser / download / notification / download_notification_item_unittest.cc
blob693ee03dc6064ce0c6d9be0aa566508ed70934d0
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 "chrome/browser/download/notification/download_notification_item.h"
7 #include "base/run_loop.h"
8 #include "base/test/test_simple_task_runner.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "content/public/test/mock_download_item.h"
11 #include "content/public/test/mock_download_manager.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/message_center/fake_message_center.h"
16 using testing::NiceMock;
17 using testing::Return;
18 using testing::_;
20 namespace {
22 class MockDownloadNotificationItemDelegate
23 : public DownloadNotificationItem::Delegate {
24 public:
25 MockDownloadNotificationItemDelegate()
26 : on_download_removed_call_count_(0u),
27 on_download_started_call_count_(0u),
28 on_download_stopped_call_count_(0u) {}
30 void OnDownloadRemoved(DownloadNotificationItem* item) override {
31 on_download_removed_call_count_++;
34 void OnDownloadStarted(DownloadNotificationItem* item) override {
35 on_download_started_call_count_++;
38 void OnDownloadStopped(DownloadNotificationItem* item) override {
39 on_download_stopped_call_count_++;
42 size_t GetOnDownloadRemovedCallCount() {
43 return on_download_removed_call_count_;
46 size_t GetOnDownloadStartedCallCount() {
47 return on_download_started_call_count_;
50 size_t GetOnDownloadStoppedCallCount() {
51 return on_download_stopped_call_count_;
54 private:
55 size_t on_download_removed_call_count_;
56 size_t on_download_started_call_count_;
57 size_t on_download_stopped_call_count_;
60 } // anonymous namespace
62 namespace test {
64 class DownloadNotificationItemTest : public testing::Test {
65 public:
66 DownloadNotificationItemTest()
67 : runner_(new base::TestSimpleTaskRunner), runner_handler_(runner_) {}
69 void SetUp() override {
70 testing::Test::SetUp();
71 message_center::MessageCenter::Initialize();
73 download_item_.reset(new NiceMock<content::MockDownloadItem>());
74 ON_CALL(*download_item_, GetId()).WillByDefault(Return(12345));
75 ON_CALL(*download_item_, GetState())
76 .WillByDefault(Return(content::DownloadItem::IN_PROGRESS));
77 ON_CALL(*download_item_, IsDangerous()).WillByDefault(Return(false));
78 ON_CALL(*download_item_, GetFileNameToReportUser())
79 .WillByDefault(Return(base::FilePath("TITLE.bin")));
80 ON_CALL(*download_item_, GetDangerType())
81 .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
82 ON_CALL(*download_item_, IsDone()).WillByDefault(Return(false));
85 void TearDown() override {
86 download_notification_item_.reset();
87 message_center::MessageCenter::Shutdown();
88 testing::Test::TearDown();
91 protected:
92 message_center::MessageCenter* message_center() {
93 return message_center::MessageCenter::Get();
96 std::string notification_id() {
97 return download_notification_item_->notification_->id();
100 message_center::Notification* notification() {
101 return message_center()->FindVisibleNotificationById(notification_id());
104 // Trampoline methods to access a private method in DownloadNotificationItem.
105 void NotificationItemClick() {
106 return download_notification_item_->OnNotificationClick();
108 void NotificationItemButtonClick(int index) {
109 return download_notification_item_->OnNotificationButtonClick(index);
112 bool IsPopupNotification(const std::string& notification_id) {
113 message_center::NotificationList::PopupNotifications popups =
114 message_center()->GetPopupNotifications();
115 for (auto it = popups.begin(); it != popups.end(); it++) {
116 if ((*it)->id() == notification_id) {
117 return true;
120 return false;
123 void CreateDownloadNotificationItem() {
124 download_notification_item_.reset(
125 new DownloadNotificationItem(download_item_.get(), &delegate_));
128 scoped_refptr<base::TestSimpleTaskRunner> runner_;
129 base::ThreadTaskRunnerHandle runner_handler_;
131 MockDownloadNotificationItemDelegate delegate_;
132 scoped_ptr<NiceMock<content::MockDownloadItem>> download_item_;
133 scoped_ptr<DownloadNotificationItem> download_notification_item_;
136 TEST_F(DownloadNotificationItemTest, ShowAndCloseNotification) {
137 EXPECT_EQ(0u, message_center()->NotificationCount());
139 // Shows a notification
140 CreateDownloadNotificationItem();
141 download_item_->NotifyObserversDownloadOpened();
143 // Confirms that the notification is shown as a popup.
144 EXPECT_EQ(1u, message_center()->NotificationCount());
145 EXPECT_TRUE(IsPopupNotification(notification_id()));
147 // Makes sure the DownloadItem::Cancel() is not called.
148 EXPECT_CALL(*download_item_, Cancel(_)).Times(0);
149 // Closes it once.
150 message_center()->RemoveNotification(notification_id(), true);
152 // Confirms that the notification is shown but is not a popup.
153 EXPECT_EQ(1u, message_center()->NotificationCount());
154 EXPECT_FALSE(IsPopupNotification(notification_id()));
156 // Makes sure the DownloadItem::Cancel() is called once.
157 EXPECT_CALL(*download_item_, Cancel(_)).Times(1);
158 // Closes it again.
159 message_center()->RemoveNotification(notification_id(), true);
161 // Confirms that the notification is closed.
162 EXPECT_EQ(0u, message_center()->NotificationCount());
165 TEST_F(DownloadNotificationItemTest, PauseAndResumeNotification) {
166 // Shows a notification
167 CreateDownloadNotificationItem();
168 download_item_->NotifyObserversDownloadOpened();
170 // Confirms that the notification is shown as a popup.
171 EXPECT_EQ(message_center()->NotificationCount(), 1u);
172 EXPECT_TRUE(IsPopupNotification(notification_id()));
174 // Pauses and makes sure the DownloadItem::Pause() is called.
175 EXPECT_CALL(*download_item_, Pause()).Times(1);
176 EXPECT_CALL(*download_item_, IsPaused()).WillRepeatedly(Return(true));
177 NotificationItemButtonClick(0);
178 download_item_->NotifyObserversDownloadUpdated();
180 // Resumes and makes sure the DownloadItem::Resume() is called.
181 EXPECT_CALL(*download_item_, Resume()).Times(1);
182 EXPECT_CALL(*download_item_, IsPaused()).WillRepeatedly(Return(false));
183 NotificationItemButtonClick(0);
184 download_item_->NotifyObserversDownloadUpdated();
187 TEST_F(DownloadNotificationItemTest, OpenDownload) {
188 EXPECT_CALL(*download_item_, GetState())
189 .WillRepeatedly(Return(content::DownloadItem::COMPLETE));
190 EXPECT_CALL(*download_item_, IsDone()).WillRepeatedly(Return(true));
192 // Shows a notification.
193 CreateDownloadNotificationItem();
194 download_item_->NotifyObserversDownloadOpened();
195 download_item_->NotifyObserversDownloadUpdated();
197 // Clicks and confirms that the OpenDownload() is called.
198 EXPECT_CALL(*download_item_, OpenDownload()).Times(1);
199 EXPECT_CALL(*download_item_, SetOpenWhenComplete(_)).Times(0);
200 NotificationItemClick();
203 TEST_F(DownloadNotificationItemTest, OpenWhenComplete) {
204 // Shows a notification
205 CreateDownloadNotificationItem();
206 download_item_->NotifyObserversDownloadOpened();
208 EXPECT_CALL(*download_item_, OpenDownload()).Times(0);
210 // Toggles open-when-complete (new value: true).
211 EXPECT_CALL(*download_item_, SetOpenWhenComplete(true))
212 .Times(1)
213 .WillOnce(Return());
214 NotificationItemClick();
215 EXPECT_CALL(*download_item_, GetOpenWhenComplete())
216 .WillRepeatedly(Return(true));
218 // Toggles open-when-complete (new value: false).
219 EXPECT_CALL(*download_item_, SetOpenWhenComplete(false))
220 .Times(1)
221 .WillOnce(Return());
222 NotificationItemClick();
223 EXPECT_CALL(*download_item_, GetOpenWhenComplete())
224 .WillRepeatedly(Return(false));
226 // Sets open-when-complete.
227 EXPECT_CALL(*download_item_, SetOpenWhenComplete(true))
228 .Times(1)
229 .WillOnce(Return());
230 NotificationItemClick();
231 EXPECT_CALL(*download_item_, GetOpenWhenComplete())
232 .WillRepeatedly(Return(true));
234 // Downloading is completed.
235 EXPECT_CALL(*download_item_, GetState())
236 .WillRepeatedly(Return(content::DownloadItem::COMPLETE));
237 EXPECT_CALL(*download_item_, IsDone()).WillRepeatedly(Return(true));
238 download_item_->NotifyObserversDownloadUpdated();
240 // DownloadItem::OpenDownload must not be called since the file opens
241 // automatically due to the open-when-complete flag.
244 TEST_F(DownloadNotificationItemTest, DownloadNotificationItemDelegate) {
245 // Shows a notification and checks OnDownloadStarted().
246 EXPECT_EQ(0u, delegate_.GetOnDownloadStartedCallCount());
247 CreateDownloadNotificationItem();
248 EXPECT_EQ(1u, delegate_.GetOnDownloadStartedCallCount());
250 download_item_->NotifyObserversDownloadOpened();
251 download_item_->NotifyObserversDownloadUpdated();
253 // Checks OnDownloadStopped().
254 EXPECT_EQ(0u, delegate_.GetOnDownloadStoppedCallCount());
255 EXPECT_CALL(*download_item_, GetState())
256 .WillRepeatedly(Return(content::DownloadItem::COMPLETE));
257 EXPECT_CALL(*download_item_, IsDone()).WillRepeatedly(Return(true));
258 download_item_->NotifyObserversDownloadUpdated();
259 EXPECT_EQ(1u, delegate_.GetOnDownloadStoppedCallCount());
261 // Checks OnDownloadRemoved().
262 EXPECT_EQ(0u, delegate_.GetOnDownloadRemovedCallCount());
263 download_item_->NotifyObserversDownloadRemoved();
264 EXPECT_EQ(1u, delegate_.GetOnDownloadRemovedCallCount());
266 download_item_.reset();
269 } // namespace test