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

Side by Side 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: Fixed build & test errors on non-ChromeOS platform. Created 5 years, 9 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 unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/download/notification/download_notification_item.h"
6
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"
15
16 using testing::NiceMock;
17 using testing::Return;
18 using testing::_;
19
20 namespace {
21
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) {}
29
30 void OnDownloadRemoved(DownloadNotificationItem* item) override {
31 on_download_removed_call_count_++;
32 }
33
34 void OnDownloadStarted(DownloadNotificationItem* item) override {
35 on_download_started_call_count_++;
36 }
37
38 void OnDownloadStopped(DownloadNotificationItem* item) override {
39 on_download_stopped_call_count_++;
40 }
41
42 size_t GetOnDownloadRemovedCallCount() {
43 return on_download_removed_call_count_;
44 }
45
46 size_t GetOnDownloadStartedCallCount() {
47 return on_download_started_call_count_;
48 }
49
50 size_t GetOnDownloadStoppedCallCount() {
51 return on_download_stopped_call_count_;
52 }
53
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_;
58 };
59
60 } // anonymous namespace
61
62 namespace test {
63
64 class DownloadNotificationItemTest : public testing::Test {
65 public:
66 DownloadNotificationItemTest()
67 : runner_(new base::TestSimpleTaskRunner), runner_handler_(runner_) {}
68
69 void SetUp() override {
70 testing::Test::SetUp();
71 message_center::MessageCenter::Initialize();
72
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));
83 }
84
85 void TearDown() override {
86 download_notification_item_.reset();
87 message_center::MessageCenter::Shutdown();
88 testing::Test::TearDown();
89 }
90
91 protected:
92 message_center::MessageCenter* message_center() {
93 return message_center::MessageCenter::Get();
94 }
95
96 std::string notification_id() {
97 return download_notification_item_->notification_->id();
98 }
99
100 message_center::Notification* notification() {
101 return message_center()->FindVisibleNotificationById(notification_id());
102 }
103
104 // Trampoline methods to access a private method in DownloadNotificationItem.
105 void NotificationItemClick() {
106 return download_notification_item_->OnNotificationClick();
107 }
108 void NotificationItemButtonClick(int index) {
109 return download_notification_item_->OnNotificationButtonClick(index);
110 }
111
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;
118 }
119 }
120 return false;
121 }
122
123 void CreateDownloadNotificationItem() {
124 download_notification_item_.reset(
125 new DownloadNotificationItem(download_item_.get(), &delegate_));
126 }
127
128 scoped_refptr<base::TestSimpleTaskRunner> runner_;
129 base::ThreadTaskRunnerHandle runner_handler_;
130
131 MockDownloadNotificationItemDelegate delegate_;
132 scoped_ptr<NiceMock<content::MockDownloadItem>> download_item_;
133 scoped_ptr<DownloadNotificationItem> download_notification_item_;
134 };
135
136 TEST_F(DownloadNotificationItemTest, ShowAndCloseNotification) {
137 EXPECT_EQ(0u, message_center()->NotificationCount());
138
139 // Shows a notification
140 CreateDownloadNotificationItem();
141 download_item_->NotifyObserversDownloadOpened();
142
143 // Confirms that the notification is shown as a popup.
144 EXPECT_EQ(1u, message_center()->NotificationCount());
145 EXPECT_TRUE(IsPopupNotification(notification_id()));
146
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);
151
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()));
155
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);
160
161 // Confirms that the notification is closed.
162 EXPECT_EQ(0u, message_center()->NotificationCount());
163 }
164
165 TEST_F(DownloadNotificationItemTest, PauseAndResumeNotification) {
166 // Shows a notification
167 CreateDownloadNotificationItem();
168 download_item_->NotifyObserversDownloadOpened();
169
170 // Confirms that the notification is shown as a popup.
171 EXPECT_EQ(message_center()->NotificationCount(), 1u);
172 EXPECT_TRUE(IsPopupNotification(notification_id()));
173
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();
179
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();
185 }
186
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));
191
192 // Shows a notification.
193 CreateDownloadNotificationItem();
194 download_item_->NotifyObserversDownloadOpened();
195 download_item_->NotifyObserversDownloadUpdated();
196
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();
201 }
202
203 TEST_F(DownloadNotificationItemTest, OpenWhenComplete) {
204 // Shows a notification
205 CreateDownloadNotificationItem();
206 download_item_->NotifyObserversDownloadOpened();
207
208 EXPECT_CALL(*download_item_, OpenDownload()).Times(0);
209
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));
217
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));
225
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));
233
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();
239
240 // DownloadItem::OpenDownload must not be called since the file opens
241 // automatically due to the open-when-complete flag.
242 }
243
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());
249
250 download_item_->NotifyObserversDownloadOpened();
251 download_item_->NotifyObserversDownloadUpdated();
252
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());
260
261 // Checks OnDownloadRemoved().
262 EXPECT_EQ(0u, delegate_.GetOnDownloadRemovedCallCount());
263 download_item_->NotifyObserversDownloadRemoved();
264 EXPECT_EQ(1u, delegate_.GetOnDownloadRemovedCallCount());
265
266 download_item_.reset();
267 }
268
269 } // namespace test
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698