OLD | NEW |
---|---|
(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), | |
68 runner_handler_(runner_) {} | |
69 | |
70 void SetUp() override { | |
71 testing::Test::SetUp(); | |
72 message_center::MessageCenter::Initialize(); | |
73 | |
74 item_.reset(new NiceMock<content::MockDownloadItem>()); | |
75 ON_CALL(*item_, GetId()).WillByDefault(Return(12345)); | |
76 ON_CALL(*item_, GetState()) | |
77 .WillByDefault(Return(content::DownloadItem::IN_PROGRESS)); | |
78 ON_CALL(*item_, IsDangerous()).WillByDefault(Return(false)); | |
79 ON_CALL(*item_, GetFileNameToReportUser()) | |
80 .WillByDefault(Return(base::FilePath("TITLE.bin"))); | |
81 ON_CALL(*item_, GetDangerType()) | |
82 .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS)); | |
83 ON_CALL(*item_, IsDone()).WillByDefault(Return(false)); | |
84 } | |
85 | |
86 void TearDown() override { | |
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 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 notification_item_->Click(); | |
107 } | |
108 void NotificationItemButtonClick(int index) { | |
109 return notification_item_->ButtonClick(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 scoped_refptr<base::TestSimpleTaskRunner> runner_; | |
124 base::ThreadTaskRunnerHandle runner_handler_; | |
125 | |
126 MockDownloadNotificationItemDelegate delegate_; | |
127 scoped_ptr<NiceMock<content::MockDownloadItem> > item_; | |
128 scoped_refptr<DownloadNotificationItem> notification_item_; | |
129 }; | |
130 | |
131 TEST_F(DownloadNotificationItemTest, ShowAndCloseNotificationTest) { | |
132 EXPECT_EQ(0u, message_center()->NotificationCount()); | |
133 | |
134 // Shows a notification | |
135 notification_item_ = new DownloadNotificationItem(item_.get(), &delegate_); | |
136 item_->NotifyObserversDownloadOpened(); | |
137 | |
138 // Confirms that the notification is shown as a popup. | |
139 EXPECT_EQ(1u, message_center()->NotificationCount()); | |
140 EXPECT_TRUE(IsPopupNotification(notification_id())); | |
141 | |
142 // 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.
| |
143 EXPECT_CALL(*item_, Cancel(_)).Times(0); | |
144 // Closes it once. | |
145 message_center()->RemoveNotification(notification_id(), true); | |
146 | |
147 // Confirms that the notification is shown but is not a popup. | |
148 EXPECT_EQ(1u, message_center()->NotificationCount()); | |
149 EXPECT_FALSE(IsPopupNotification(notification_id())); | |
150 | |
151 // Makes sure the DownloadItem::Close() is called once. | |
152 EXPECT_CALL(*item_, Cancel(_)).Times(1); | |
153 // Closes it again. | |
154 message_center()->RemoveNotification(notification_id(), true); | |
155 | |
156 // Confirms that the notification is closed. | |
157 EXPECT_EQ(0u, message_center()->NotificationCount()); | |
158 } | |
159 | |
160 TEST_F(DownloadNotificationItemTest, PauseAndResumeNotificationTest) { | |
161 // Shows a notification | |
162 notification_item_ = new DownloadNotificationItem(item_.get(), &delegate_); | |
163 item_->NotifyObserversDownloadOpened(); | |
164 | |
165 // Confirms that the notification is shown as a popup. | |
166 EXPECT_EQ(message_center()->NotificationCount(), 1u); | |
167 EXPECT_TRUE(IsPopupNotification(notification_id())); | |
168 | |
169 // Pauses and makes sure the DownloadItem::Pause() is called. | |
170 EXPECT_CALL(*item_, Pause()).Times(1); | |
171 EXPECT_CALL(*item_, IsPaused()).WillRepeatedly(Return(true)); | |
172 NotificationItemButtonClick(0); | |
173 item_->NotifyObserversDownloadUpdated(); | |
174 | |
175 // Resumes and makes sure the DownloadItem::Resume() is called. | |
176 EXPECT_CALL(*item_, Resume()).Times(1); | |
177 EXPECT_CALL(*item_, IsPaused()).WillRepeatedly(Return(false)); | |
178 NotificationItemButtonClick(0); | |
179 item_->NotifyObserversDownloadUpdated(); | |
180 } | |
181 | |
182 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.
| |
183 EXPECT_CALL(*item_, GetState()) | |
184 .WillRepeatedly(Return(content::DownloadItem::COMPLETE)); | |
185 EXPECT_CALL(*item_, IsDone()).WillRepeatedly(Return(true)); | |
186 | |
187 // Shows a notification | |
188 notification_item_ = new DownloadNotificationItem(item_.get(), &delegate_); | |
189 item_->NotifyObserversDownloadOpened(); | |
190 item_->NotifyObserversDownloadUpdated(); | |
191 | |
192 // Clicks and confirms that the OpenDownload() is called. | |
193 EXPECT_CALL(*item_, OpenDownload()).Times(1); | |
194 EXPECT_CALL(*item_, SetOpenWhenComplete(_)).Times(0); | |
195 NotificationItemClick(); | |
196 } | |
197 | |
198 TEST_F(DownloadNotificationItemTest, OpenWhenCompleteTest) { | |
199 // Shows a notification | |
200 notification_item_ = new DownloadNotificationItem(item_.get(), &delegate_); | |
201 item_->NotifyObserversDownloadOpened(); | |
202 | |
203 EXPECT_CALL(*item_, OpenDownload()).Times(0); | |
204 | |
205 // Toggles open-when-complete (new value: true). | |
206 EXPECT_CALL(*item_, SetOpenWhenComplete(true)).Times(1).WillOnce(Return()); | |
207 NotificationItemClick(); | |
208 EXPECT_CALL(*item_, GetOpenWhenComplete()).WillRepeatedly(Return(true)); | |
209 | |
210 // 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.
| |
211 EXPECT_CALL(*item_, SetOpenWhenComplete(false)).Times(1).WillOnce(Return()); | |
212 NotificationItemClick(); | |
213 EXPECT_CALL(*item_, GetOpenWhenComplete()).WillRepeatedly(Return(false)); | |
214 | |
215 // Sets open-when-complete. | |
216 EXPECT_CALL(*item_, SetOpenWhenComplete(true)).Times(1).WillOnce(Return()); | |
217 NotificationItemClick(); | |
218 EXPECT_CALL(*item_, GetOpenWhenComplete()).WillRepeatedly(Return(true)); | |
219 | |
220 // Downloading is completed. | |
221 EXPECT_CALL(*item_, GetState()) | |
222 .WillRepeatedly(Return(content::DownloadItem::COMPLETE)); | |
223 EXPECT_CALL(*item_, IsDone()).WillRepeatedly(Return(true)); | |
224 item_->NotifyObserversDownloadUpdated(); | |
225 | |
226 // DownloadItem::OpenDownload must not be called since the file opens | |
227 // automatically due to the open-when-complete flag. | |
228 | |
229 item_->NotifyObserversDownloadRemoved(); | |
230 } | |
231 | |
232 TEST_F(DownloadNotificationItemTest, DownloadNotificationItemDelegateTest) { | |
233 // Shows a notification | |
234 EXPECT_EQ(0u, delegate_.GetOnDownloadStartedCallCount()); | |
235 notification_item_ = new DownloadNotificationItem(item_.get(), &delegate_); | |
236 EXPECT_EQ(1u, delegate_.GetOnDownloadStartedCallCount()); | |
237 | |
238 item_->NotifyObserversDownloadOpened(); | |
239 | |
asanka
2015/02/20 23:34:40
Extra whitespace
yoshiki
2015/02/24 21:30:57
Done.
| |
240 item_->NotifyObserversDownloadUpdated(); | |
241 | |
242 // Check OnDownloadStopped(). | |
243 EXPECT_EQ(0u, delegate_.GetOnDownloadStoppedCallCount()); | |
244 EXPECT_CALL(*item_, GetState()) | |
245 .WillRepeatedly(Return(content::DownloadItem::COMPLETE)); | |
246 EXPECT_CALL(*item_, IsDone()).WillRepeatedly(Return(true)); | |
247 item_->NotifyObserversDownloadUpdated(); | |
248 EXPECT_EQ(1u, delegate_.GetOnDownloadStoppedCallCount()); | |
249 | |
250 EXPECT_EQ(0u, delegate_.GetOnDownloadRemovedCallCount()); | |
251 item_->NotifyObserversDownloadRemoved(); | |
252 EXPECT_EQ(1u, delegate_.GetOnDownloadRemovedCallCount()); | |
253 | |
254 item_.reset(); | |
255 } | |
256 | |
257 } // namespace test | |
OLD | NEW |