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 "chrome/test/base/testing_profile.h" |
| 11 #include "content/public/test/mock_download_item.h" |
| 12 #include "content/public/test/mock_download_manager.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "ui/message_center/fake_message_center.h" |
| 16 |
| 17 using testing::NiceMock; |
| 18 using testing::Return; |
| 19 using testing::_; |
| 20 |
| 21 namespace { |
| 22 |
| 23 class MockDownloadNotificationItemDelegate |
| 24 : public DownloadNotificationItem::Delegate { |
| 25 public: |
| 26 MockDownloadNotificationItemDelegate() |
| 27 : on_download_removed_call_count_(0u), |
| 28 on_download_started_call_count_(0u), |
| 29 on_download_stopped_call_count_(0u) {} |
| 30 |
| 31 void OnDownloadRemoved(DownloadNotificationItem* item) override { |
| 32 on_download_removed_call_count_++; |
| 33 } |
| 34 |
| 35 void OnDownloadStarted(DownloadNotificationItem* item) override { |
| 36 on_download_started_call_count_++; |
| 37 } |
| 38 |
| 39 void OnDownloadStopped(DownloadNotificationItem* item) override { |
| 40 on_download_stopped_call_count_++; |
| 41 } |
| 42 |
| 43 size_t GetOnDownloadRemovedCallCount() { |
| 44 return on_download_removed_call_count_; |
| 45 } |
| 46 |
| 47 size_t GetOnDownloadStartedCallCount() { |
| 48 return on_download_started_call_count_; |
| 49 } |
| 50 |
| 51 size_t GetOnDownloadStoppedCallCount() { |
| 52 return on_download_stopped_call_count_; |
| 53 } |
| 54 |
| 55 private: |
| 56 size_t on_download_removed_call_count_; |
| 57 size_t on_download_started_call_count_; |
| 58 size_t on_download_stopped_call_count_; |
| 59 }; |
| 60 |
| 61 } // anonymous namespace |
| 62 |
| 63 namespace test { |
| 64 |
| 65 class DownloadNotificationItemTest : public testing::Test { |
| 66 public: |
| 67 DownloadNotificationItemTest() |
| 68 : runner_(new base::TestSimpleTaskRunner), 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 notification_item_ = nullptr; |
| 88 message_center::MessageCenter::Shutdown(); |
| 89 testing::Test::TearDown(); |
| 90 } |
| 91 |
| 92 protected: |
| 93 message_center::MessageCenter* message_center() { |
| 94 return message_center::MessageCenter::Get(); |
| 95 } |
| 96 |
| 97 std::string notification_id() { |
| 98 return notification_item_->notification_->id(); |
| 99 } |
| 100 |
| 101 message_center::Notification* notification() { |
| 102 return message_center()->FindVisibleNotificationById(notification_id()); |
| 103 } |
| 104 |
| 105 // Trampoline methods to access a private method in DownloadNotificationItem. |
| 106 void NotificationItemClick() { |
| 107 return notification_item_->OnNotificationClick(); |
| 108 } |
| 109 void NotificationItemButtonClick(int index) { |
| 110 return notification_item_->OnNotificationButtonClick(index); |
| 111 } |
| 112 |
| 113 bool IsPopupNotification(const std::string& notification_id) { |
| 114 message_center::NotificationList::PopupNotifications popups = |
| 115 message_center()->GetPopupNotifications(); |
| 116 for (auto it = popups.begin(); it != popups.end(); it++) { |
| 117 if ((*it)->id() == notification_id) { |
| 118 return true; |
| 119 } |
| 120 } |
| 121 return false; |
| 122 } |
| 123 |
| 124 DownloadNotificationItem* CreateDownloadNotificationItem() { |
| 125 return new DownloadNotificationItem(item_.get(), &profile_, &delegate_); |
| 126 } |
| 127 |
| 128 scoped_refptr<base::TestSimpleTaskRunner> runner_; |
| 129 base::ThreadTaskRunnerHandle runner_handler_; |
| 130 |
| 131 TestingProfile profile_; |
| 132 MockDownloadNotificationItemDelegate delegate_; |
| 133 scoped_ptr<NiceMock<content::MockDownloadItem>> item_; |
| 134 scoped_refptr<DownloadNotificationItem> notification_item_; |
| 135 }; |
| 136 |
| 137 TEST_F(DownloadNotificationItemTest, ShowAndCloseNotification) { |
| 138 EXPECT_EQ(0u, message_center()->NotificationCount()); |
| 139 |
| 140 // Shows a notification |
| 141 notification_item_ = CreateDownloadNotificationItem(); |
| 142 item_->NotifyObserversDownloadOpened(); |
| 143 |
| 144 // Confirms that the notification is shown as a popup. |
| 145 EXPECT_EQ(1u, message_center()->NotificationCount()); |
| 146 EXPECT_TRUE(IsPopupNotification(notification_id())); |
| 147 |
| 148 // Makes sure the DownloadItem::Cancel() is not called. |
| 149 EXPECT_CALL(*item_, Cancel(_)).Times(0); |
| 150 // Closes it once. |
| 151 message_center()->RemoveNotification(notification_id(), true); |
| 152 |
| 153 // Confirms that the notification is shown but is not a popup. |
| 154 EXPECT_EQ(1u, message_center()->NotificationCount()); |
| 155 EXPECT_FALSE(IsPopupNotification(notification_id())); |
| 156 |
| 157 // Makes sure the DownloadItem::Cancel() is called once. |
| 158 EXPECT_CALL(*item_, Cancel(_)).Times(1); |
| 159 // Closes it again. |
| 160 message_center()->RemoveNotification(notification_id(), true); |
| 161 |
| 162 // Confirms that the notification is closed. |
| 163 EXPECT_EQ(0u, message_center()->NotificationCount()); |
| 164 } |
| 165 |
| 166 TEST_F(DownloadNotificationItemTest, PauseAndResumeNotification) { |
| 167 // Shows a notification |
| 168 notification_item_ = CreateDownloadNotificationItem(); |
| 169 item_->NotifyObserversDownloadOpened(); |
| 170 |
| 171 // Confirms that the notification is shown as a popup. |
| 172 EXPECT_EQ(message_center()->NotificationCount(), 1u); |
| 173 EXPECT_TRUE(IsPopupNotification(notification_id())); |
| 174 |
| 175 // Pauses and makes sure the DownloadItem::Pause() is called. |
| 176 EXPECT_CALL(*item_, Pause()).Times(1); |
| 177 EXPECT_CALL(*item_, IsPaused()).WillRepeatedly(Return(true)); |
| 178 NotificationItemButtonClick(0); |
| 179 item_->NotifyObserversDownloadUpdated(); |
| 180 |
| 181 // Resumes and makes sure the DownloadItem::Resume() is called. |
| 182 EXPECT_CALL(*item_, Resume()).Times(1); |
| 183 EXPECT_CALL(*item_, IsPaused()).WillRepeatedly(Return(false)); |
| 184 NotificationItemButtonClick(0); |
| 185 item_->NotifyObserversDownloadUpdated(); |
| 186 } |
| 187 |
| 188 TEST_F(DownloadNotificationItemTest, OpenDownload) { |
| 189 EXPECT_CALL(*item_, GetState()) |
| 190 .WillRepeatedly(Return(content::DownloadItem::COMPLETE)); |
| 191 EXPECT_CALL(*item_, IsDone()).WillRepeatedly(Return(true)); |
| 192 |
| 193 // Shows a notification. |
| 194 notification_item_ = CreateDownloadNotificationItem(); |
| 195 item_->NotifyObserversDownloadOpened(); |
| 196 item_->NotifyObserversDownloadUpdated(); |
| 197 |
| 198 // Clicks and confirms that the OpenDownload() is called. |
| 199 EXPECT_CALL(*item_, OpenDownload()).Times(1); |
| 200 EXPECT_CALL(*item_, SetOpenWhenComplete(_)).Times(0); |
| 201 NotificationItemClick(); |
| 202 } |
| 203 |
| 204 TEST_F(DownloadNotificationItemTest, OpenWhenComplete) { |
| 205 // Shows a notification |
| 206 notification_item_ = CreateDownloadNotificationItem(); |
| 207 item_->NotifyObserversDownloadOpened(); |
| 208 |
| 209 EXPECT_CALL(*item_, OpenDownload()).Times(0); |
| 210 |
| 211 // Toggles open-when-complete (new value: true). |
| 212 EXPECT_CALL(*item_, SetOpenWhenComplete(true)).Times(1).WillOnce(Return()); |
| 213 NotificationItemClick(); |
| 214 EXPECT_CALL(*item_, GetOpenWhenComplete()).WillRepeatedly(Return(true)); |
| 215 |
| 216 // Toggles open-when-complete (new value: false). |
| 217 EXPECT_CALL(*item_, SetOpenWhenComplete(false)).Times(1).WillOnce(Return()); |
| 218 NotificationItemClick(); |
| 219 EXPECT_CALL(*item_, GetOpenWhenComplete()).WillRepeatedly(Return(false)); |
| 220 |
| 221 // Sets open-when-complete. |
| 222 EXPECT_CALL(*item_, SetOpenWhenComplete(true)).Times(1).WillOnce(Return()); |
| 223 NotificationItemClick(); |
| 224 EXPECT_CALL(*item_, GetOpenWhenComplete()).WillRepeatedly(Return(true)); |
| 225 |
| 226 // Downloading is completed. |
| 227 EXPECT_CALL(*item_, GetState()) |
| 228 .WillRepeatedly(Return(content::DownloadItem::COMPLETE)); |
| 229 EXPECT_CALL(*item_, IsDone()).WillRepeatedly(Return(true)); |
| 230 item_->NotifyObserversDownloadUpdated(); |
| 231 |
| 232 // DownloadItem::OpenDownload must not be called since the file opens |
| 233 // automatically due to the open-when-complete flag. |
| 234 } |
| 235 |
| 236 TEST_F(DownloadNotificationItemTest, DownloadNotificationItemDelegate) { |
| 237 // Shows a notification and checks OnDownloadStarted(). |
| 238 EXPECT_EQ(0u, delegate_.GetOnDownloadStartedCallCount()); |
| 239 notification_item_ = CreateDownloadNotificationItem(); |
| 240 EXPECT_EQ(1u, delegate_.GetOnDownloadStartedCallCount()); |
| 241 |
| 242 item_->NotifyObserversDownloadOpened(); |
| 243 item_->NotifyObserversDownloadUpdated(); |
| 244 |
| 245 // Checks OnDownloadStopped(). |
| 246 EXPECT_EQ(0u, delegate_.GetOnDownloadStoppedCallCount()); |
| 247 EXPECT_CALL(*item_, GetState()) |
| 248 .WillRepeatedly(Return(content::DownloadItem::COMPLETE)); |
| 249 EXPECT_CALL(*item_, IsDone()).WillRepeatedly(Return(true)); |
| 250 item_->NotifyObserversDownloadUpdated(); |
| 251 EXPECT_EQ(1u, delegate_.GetOnDownloadStoppedCallCount()); |
| 252 |
| 253 // Checks OnDownloadRemoved(). |
| 254 EXPECT_EQ(0u, delegate_.GetOnDownloadRemovedCallCount()); |
| 255 item_->NotifyObserversDownloadRemoved(); |
| 256 EXPECT_EQ(1u, delegate_.GetOnDownloadRemovedCallCount()); |
| 257 |
| 258 item_.reset(); |
| 259 } |
| 260 |
| 261 } // namespace test |
OLD | NEW |