BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / download / notification / download_item_notification_unittest.cc
blob1957ba7337061aa50d43c0ed6523f8a21362e493
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_, GetURL()).WillByDefault(ReturnRefOfCopy(
74 GURL("http://www.example.com/download.bin")));
75 ON_CALL(*download_item_, GetBrowserContext())
76 .WillByDefault(Return(profile_));
79 void TearDown() override {
80 download_item_notification_ = nullptr; // will be free'd in the manager.
81 download_notification_manager_.reset();
82 profile_manager_.reset();
83 message_center::MessageCenter::Shutdown();
84 testing::Test::TearDown();
87 protected:
88 message_center::MessageCenter* message_center() const {
89 return message_center::MessageCenter::Get();
92 NotificationUIManager* ui_manager() const {
93 return TestingBrowserProcess::GetGlobal()->notification_ui_manager();
96 std::string notification_id() const {
97 return download_item_notification_->notification_->id();
100 const Notification* notification() const {
101 return ui_manager()->FindById(
102 download_item_notification_->watcher()->id(),
103 NotificationUIManager::GetProfileID(profile_));
106 size_t NotificationCount() const {
107 return ui_manager()
108 ->GetAllIdsByProfileAndSourceOrigin(
109 NotificationUIManager::GetProfileID(profile_),
110 GURL(DownloadItemNotification::kDownloadNotificationOrigin))
111 .size();
114 void RemoveNotification() {
115 ui_manager()->CancelById(download_item_notification_->watcher()->id(),
116 NotificationUIManager::GetProfileID(profile_));
118 // Waits, since removing a notification may cause an async job.
119 base::RunLoop().RunUntilIdle();
122 // Trampoline methods to access a private method in DownloadItemNotification.
123 void NotificationItemClick() {
124 return download_item_notification_->OnNotificationClick();
126 void NotificationItemButtonClick(int index) {
127 return download_item_notification_->OnNotificationButtonClick(index);
130 bool ShownAsPopUp() {
131 return !notification()->shown_as_popup();
134 void CreateDownloadItemNotification() {
135 download_notification_manager_->OnNewDownloadReady(download_item_.get());
136 download_item_notification_ =
137 download_notification_manager_->items_[download_item_.get()];
140 base::MessageLoopForUI message_loop_;
141 content::TestBrowserThread ui_thread_;
143 scoped_ptr<TestingProfileManager> profile_manager_;
144 Profile* profile_;
146 scoped_ptr<NiceMock<content::MockDownloadItem>> download_item_;
147 scoped_ptr<DownloadNotificationManagerForProfile>
148 download_notification_manager_;
149 DownloadItemNotification* download_item_notification_;
152 TEST_F(DownloadItemNotificationTest, ShowAndCloseNotification) {
153 EXPECT_EQ(0u, NotificationCount());
155 // Shows a notification
156 CreateDownloadItemNotification();
157 download_item_->NotifyObserversDownloadOpened();
159 // Confirms that the notification is shown as a popup.
160 EXPECT_TRUE(ShownAsPopUp());
161 EXPECT_EQ(1u, NotificationCount());
163 // Makes sure the DownloadItem::Cancel() is not called.
164 EXPECT_CALL(*download_item_, Cancel(_)).Times(0);
165 // Closes it once.
166 RemoveNotification();
168 // Confirms that the notification is closed.
169 EXPECT_EQ(0u, NotificationCount());
171 // Makes sure the DownloadItem::Cancel() is never called.
172 EXPECT_CALL(*download_item_, Cancel(_)).Times(0);
175 TEST_F(DownloadItemNotificationTest, PauseAndResumeNotification) {
176 // Shows a notification
177 CreateDownloadItemNotification();
178 download_item_->NotifyObserversDownloadOpened();
180 // Confirms that the notification is shown as a popup.
181 EXPECT_EQ(1u, NotificationCount());
183 // Pauses and makes sure the DownloadItem::Pause() is called.
184 EXPECT_CALL(*download_item_, Pause()).Times(1);
185 EXPECT_CALL(*download_item_, IsPaused()).WillRepeatedly(Return(true));
186 NotificationItemButtonClick(0);
187 download_item_->NotifyObserversDownloadUpdated();
189 // Resumes and makes sure the DownloadItem::Resume() is called.
190 EXPECT_CALL(*download_item_, Resume()).Times(1);
191 EXPECT_CALL(*download_item_, IsPaused()).WillRepeatedly(Return(false));
192 NotificationItemButtonClick(0);
193 download_item_->NotifyObserversDownloadUpdated();
196 TEST_F(DownloadItemNotificationTest, OpenDownload) {
197 EXPECT_CALL(*download_item_, GetState())
198 .WillRepeatedly(Return(content::DownloadItem::COMPLETE));
199 EXPECT_CALL(*download_item_, IsDone()).WillRepeatedly(Return(true));
201 // Shows a notification.
202 CreateDownloadItemNotification();
203 download_item_->NotifyObserversDownloadOpened();
204 download_item_->NotifyObserversDownloadUpdated();
206 // Clicks and confirms that the OpenDownload() is called.
207 EXPECT_CALL(*download_item_, OpenDownload()).Times(1);
208 EXPECT_CALL(*download_item_, SetOpenWhenComplete(_)).Times(0);
209 NotificationItemClick();
212 TEST_F(DownloadItemNotificationTest, OpenWhenComplete) {
213 // Shows a notification
214 CreateDownloadItemNotification();
215 download_item_->NotifyObserversDownloadOpened();
217 EXPECT_CALL(*download_item_, OpenDownload()).Times(0);
219 // Toggles open-when-complete (new value: true).
220 EXPECT_CALL(*download_item_, SetOpenWhenComplete(true))
221 .Times(1)
222 .WillOnce(Return());
223 NotificationItemClick();
224 EXPECT_CALL(*download_item_, GetOpenWhenComplete())
225 .WillRepeatedly(Return(true));
227 // Toggles open-when-complete (new value: false).
228 EXPECT_CALL(*download_item_, SetOpenWhenComplete(false))
229 .Times(1)
230 .WillOnce(Return());
231 NotificationItemClick();
232 EXPECT_CALL(*download_item_, GetOpenWhenComplete())
233 .WillRepeatedly(Return(false));
235 // Sets open-when-complete.
236 EXPECT_CALL(*download_item_, SetOpenWhenComplete(true))
237 .Times(1)
238 .WillOnce(Return());
239 NotificationItemClick();
240 EXPECT_CALL(*download_item_, GetOpenWhenComplete())
241 .WillRepeatedly(Return(true));
243 // Downloading is completed.
244 EXPECT_CALL(*download_item_, GetState())
245 .WillRepeatedly(Return(content::DownloadItem::COMPLETE));
246 EXPECT_CALL(*download_item_, IsDone()).WillRepeatedly(Return(true));
247 download_item_->NotifyObserversDownloadUpdated();
249 // DownloadItem::OpenDownload must not be called since the file opens
250 // automatically due to the open-when-complete flag.
253 } // namespace test