Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / download / notification / download_item_notification_unittest.cc
blob4f9bee88d49aa248bc66092930ab48ebeb10789e
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_item_notification.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "base/test/test_simple_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "chrome/browser/download/notification/download_notification_manager.h"
12 #include "chrome/browser/notifications/notification_test_util.h"
13 #include "chrome/browser/notifications/platform_notification_service_impl.h"
14 #include "chrome/test/base/testing_browser_process.h"
15 #include "chrome/test/base/testing_profile.h"
16 #include "chrome/test/base/testing_profile_manager.h"
17 #include "content/public/test/mock_download_item.h"
18 #include "content/public/test/mock_download_manager.h"
19 #include "content/public/test/test_browser_thread.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "ui/message_center/fake_message_center.h"
24 using testing::NiceMock;
25 using testing::Return;
26 using testing::ReturnRefOfCopy;
27 using testing::_;
29 namespace {
31 const base::FilePath::CharType kDownloadItemTargetPathString[] =
32 FILE_PATH_LITERAL("/tmp/TITLE.bin");
34 } // anonymouse namespace
36 namespace test {
38 class DownloadItemNotificationTest : public testing::Test {
39 public:
40 DownloadItemNotificationTest()
41 : ui_thread_(content::BrowserThread::UI, &message_loop_),
42 profile_(nullptr) {}
44 void SetUp() override {
45 testing::Test::SetUp();
46 message_center::MessageCenter::Initialize();
48 profile_manager_.reset(
49 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
50 ASSERT_TRUE(profile_manager_->SetUp());
51 profile_ = profile_manager_->CreateTestingProfile("test-user");
53 scoped_ptr<NotificationUIManager> ui_manager(new StubNotificationUIManager);
54 TestingBrowserProcess::GetGlobal()->
55 SetNotificationUIManager(ui_manager.Pass());
57 download_notification_manager_.reset(
58 new DownloadNotificationManagerForProfile(profile_, nullptr));
60 base::FilePath download_item_target_path(kDownloadItemTargetPathString);
61 download_item_.reset(new NiceMock<content::MockDownloadItem>());
62 ON_CALL(*download_item_, GetId()).WillByDefault(Return(12345));
63 ON_CALL(*download_item_, GetState())
64 .WillByDefault(Return(content::DownloadItem::IN_PROGRESS));
65 ON_CALL(*download_item_, IsDangerous()).WillByDefault(Return(false));
66 ON_CALL(*download_item_, GetFileNameToReportUser())
67 .WillByDefault(Return(base::FilePath("TITLE.bin")));
68 ON_CALL(*download_item_, GetTargetFilePath())
69 .WillByDefault(ReturnRefOfCopy(download_item_target_path));
70 ON_CALL(*download_item_, GetDangerType())
71 .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
72 ON_CALL(*download_item_, IsDone()).WillByDefault(Return(false));
73 ON_CALL(*download_item_, GetBrowserContext())
74 .WillByDefault(Return(profile_));
77 void TearDown() override {
78 download_item_notification_ = nullptr; // will be free'd in the manager.
79 download_notification_manager_.reset();
80 profile_manager_.reset();
81 message_center::MessageCenter::Shutdown();
82 testing::Test::TearDown();
85 protected:
86 message_center::MessageCenter* message_center() const {
87 return message_center::MessageCenter::Get();
90 NotificationUIManager* ui_manager() const {
91 return TestingBrowserProcess::GetGlobal()->notification_ui_manager();
94 std::string notification_id() const {
95 return download_item_notification_->notification_->id();
98 const Notification* notification() const {
99 return ui_manager()->FindById(
100 download_item_notification_->watcher()->id(),
101 NotificationUIManager::GetProfileID(profile_));
104 size_t NotificationCount() const {
105 return ui_manager()
106 ->GetAllIdsByProfileAndSourceOrigin(
107 NotificationUIManager::GetProfileID(profile_),
108 GURL(DownloadItemNotification::kDownloadNotificationOrigin))
109 .size();
112 void RemoveNotification() {
113 ui_manager()->CancelById(download_item_notification_->watcher()->id(),
114 NotificationUIManager::GetProfileID(profile_));
116 // Waits, since removing a notification may cause an async job.
117 base::RunLoop().RunUntilIdle();
120 // Trampoline methods to access a private method in DownloadItemNotification.
121 void NotificationItemClick() {
122 return download_item_notification_->OnNotificationClick();
124 void NotificationItemButtonClick(int index) {
125 return download_item_notification_->OnNotificationButtonClick(index);
128 bool ShownAsPopUp() {
129 return !notification()->shown_as_popup();
132 void CreateDownloadItemNotification() {
133 download_notification_manager_->OnNewDownloadReady(download_item_.get());
134 download_item_notification_ =
135 download_notification_manager_->items_[download_item_.get()];
138 base::MessageLoopForUI message_loop_;
139 content::TestBrowserThread ui_thread_;
141 scoped_ptr<TestingProfileManager> profile_manager_;
142 Profile* profile_;
144 scoped_ptr<NiceMock<content::MockDownloadItem>> download_item_;
145 scoped_ptr<DownloadNotificationManagerForProfile>
146 download_notification_manager_;
147 DownloadItemNotification* download_item_notification_;
150 TEST_F(DownloadItemNotificationTest, ShowAndCloseNotification) {
151 EXPECT_EQ(0u, NotificationCount());
153 // Shows a notification
154 CreateDownloadItemNotification();
155 download_item_->NotifyObserversDownloadOpened();
157 // Confirms that the notification is shown as a popup.
158 EXPECT_TRUE(ShownAsPopUp());
159 EXPECT_EQ(1u, NotificationCount());
161 // Makes sure the DownloadItem::Cancel() is not called.
162 EXPECT_CALL(*download_item_, Cancel(_)).Times(0);
163 // Closes it once.
164 RemoveNotification();
166 // Confirms that the notification is closed.
167 EXPECT_EQ(0u, NotificationCount());
169 // Makes sure the DownloadItem::Cancel() is never called.
170 EXPECT_CALL(*download_item_, Cancel(_)).Times(0);
173 TEST_F(DownloadItemNotificationTest, PauseAndResumeNotification) {
174 // Shows a notification
175 CreateDownloadItemNotification();
176 download_item_->NotifyObserversDownloadOpened();
178 // Confirms that the notification is shown as a popup.
179 EXPECT_EQ(1u, NotificationCount());
181 // Pauses and makes sure the DownloadItem::Pause() is called.
182 EXPECT_CALL(*download_item_, Pause()).Times(1);
183 EXPECT_CALL(*download_item_, IsPaused()).WillRepeatedly(Return(true));
184 NotificationItemButtonClick(0);
185 download_item_->NotifyObserversDownloadUpdated();
187 // Resumes and makes sure the DownloadItem::Resume() is called.
188 EXPECT_CALL(*download_item_, Resume()).Times(1);
189 EXPECT_CALL(*download_item_, IsPaused()).WillRepeatedly(Return(false));
190 NotificationItemButtonClick(0);
191 download_item_->NotifyObserversDownloadUpdated();
194 TEST_F(DownloadItemNotificationTest, OpenDownload) {
195 EXPECT_CALL(*download_item_, GetState())
196 .WillRepeatedly(Return(content::DownloadItem::COMPLETE));
197 EXPECT_CALL(*download_item_, IsDone()).WillRepeatedly(Return(true));
199 // Shows a notification.
200 CreateDownloadItemNotification();
201 download_item_->NotifyObserversDownloadOpened();
202 download_item_->NotifyObserversDownloadUpdated();
204 // Clicks and confirms that the OpenDownload() is called.
205 EXPECT_CALL(*download_item_, OpenDownload()).Times(1);
206 EXPECT_CALL(*download_item_, SetOpenWhenComplete(_)).Times(0);
207 NotificationItemClick();
210 TEST_F(DownloadItemNotificationTest, OpenWhenComplete) {
211 // Shows a notification
212 CreateDownloadItemNotification();
213 download_item_->NotifyObserversDownloadOpened();
215 EXPECT_CALL(*download_item_, OpenDownload()).Times(0);
217 // Toggles open-when-complete (new value: true).
218 EXPECT_CALL(*download_item_, SetOpenWhenComplete(true))
219 .Times(1)
220 .WillOnce(Return());
221 NotificationItemClick();
222 EXPECT_CALL(*download_item_, GetOpenWhenComplete())
223 .WillRepeatedly(Return(true));
225 // Toggles open-when-complete (new value: false).
226 EXPECT_CALL(*download_item_, SetOpenWhenComplete(false))
227 .Times(1)
228 .WillOnce(Return());
229 NotificationItemClick();
230 EXPECT_CALL(*download_item_, GetOpenWhenComplete())
231 .WillRepeatedly(Return(false));
233 // Sets open-when-complete.
234 EXPECT_CALL(*download_item_, SetOpenWhenComplete(true))
235 .Times(1)
236 .WillOnce(Return());
237 NotificationItemClick();
238 EXPECT_CALL(*download_item_, GetOpenWhenComplete())
239 .WillRepeatedly(Return(true));
241 // Downloading is completed.
242 EXPECT_CALL(*download_item_, GetState())
243 .WillRepeatedly(Return(content::DownloadItem::COMPLETE));
244 EXPECT_CALL(*download_item_, IsDone()).WillRepeatedly(Return(true));
245 download_item_->NotifyObserversDownloadUpdated();
247 // DownloadItem::OpenDownload must not be called since the file opens
248 // automatically due to the open-when-complete flag.
251 } // namespace test