OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/download/notification/download_item_notification.h" | 5 #include "chrome/browser/download/notification/download_item_notification.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/test/test_simple_task_runner.h" | 9 #include "base/test/test_simple_task_runner.h" |
10 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 | 28 |
29 namespace { | 29 namespace { |
30 | 30 |
31 const base::FilePath::CharType kDownloadItemTargetPathString[] = | 31 const base::FilePath::CharType kDownloadItemTargetPathString[] = |
32 FILE_PATH_LITERAL("/tmp/TITLE.bin"); | 32 FILE_PATH_LITERAL("/tmp/TITLE.bin"); |
33 | 33 |
34 } // anonymouse namespace | 34 } // anonymouse namespace |
35 | 35 |
36 namespace test { | 36 namespace test { |
37 | 37 |
| 38 class MockMessageCenter : public message_center::FakeMessageCenter { |
| 39 public: |
| 40 MockMessageCenter() {} |
| 41 ~MockMessageCenter() override {} |
| 42 |
| 43 void AddVisibleNotification(message_center::Notification* notification) { |
| 44 visible_notifications_.insert(notification); |
| 45 } |
| 46 |
| 47 const message_center::NotificationList::Notifications& |
| 48 GetVisibleNotifications() override { |
| 49 return visible_notifications_; |
| 50 } |
| 51 |
| 52 private: |
| 53 message_center::NotificationList::Notifications visible_notifications_; |
| 54 |
| 55 DISALLOW_COPY_AND_ASSIGN(MockMessageCenter); |
| 56 }; |
| 57 |
38 class DownloadItemNotificationTest : public testing::Test { | 58 class DownloadItemNotificationTest : public testing::Test { |
39 public: | 59 public: |
40 DownloadItemNotificationTest() | 60 DownloadItemNotificationTest() |
41 : ui_thread_(content::BrowserThread::UI, &message_loop_), | 61 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
42 profile_(nullptr) {} | 62 profile_(nullptr) {} |
43 | 63 |
44 void SetUp() override { | 64 void SetUp() override { |
45 testing::Test::SetUp(); | 65 testing::Test::SetUp(); |
46 message_center::MessageCenter::Initialize(); | 66 message_center::MessageCenter::Initialize(); |
47 | 67 |
48 profile_manager_.reset( | 68 profile_manager_.reset( |
49 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); | 69 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); |
50 ASSERT_TRUE(profile_manager_->SetUp()); | 70 ASSERT_TRUE(profile_manager_->SetUp()); |
51 profile_ = profile_manager_->CreateTestingProfile("test-user"); | 71 profile_ = profile_manager_->CreateTestingProfile("test-user"); |
52 | 72 |
53 scoped_ptr<NotificationUIManager> ui_manager(new StubNotificationUIManager); | 73 scoped_ptr<NotificationUIManager> ui_manager(new StubNotificationUIManager); |
54 TestingBrowserProcess::GetGlobal()-> | 74 TestingBrowserProcess::GetGlobal()-> |
55 SetNotificationUIManager(ui_manager.Pass()); | 75 SetNotificationUIManager(ui_manager.Pass()); |
56 | 76 |
57 download_notification_manager_.reset( | 77 download_notification_manager_.reset( |
58 new DownloadNotificationManagerForProfile(profile_, nullptr)); | 78 new DownloadNotificationManagerForProfile(profile_, nullptr)); |
59 | 79 |
| 80 message_center_.reset(new MockMessageCenter()); |
| 81 download_notification_manager_->OverrideMessageCenterForTest( |
| 82 message_center_.get()); |
| 83 |
60 base::FilePath download_item_target_path(kDownloadItemTargetPathString); | 84 base::FilePath download_item_target_path(kDownloadItemTargetPathString); |
61 download_item_.reset(new NiceMock<content::MockDownloadItem>()); | 85 download_item_.reset(new NiceMock<content::MockDownloadItem>()); |
62 ON_CALL(*download_item_, GetId()).WillByDefault(Return(12345)); | 86 ON_CALL(*download_item_, GetId()).WillByDefault(Return(12345)); |
63 ON_CALL(*download_item_, GetState()) | 87 ON_CALL(*download_item_, GetState()) |
64 .WillByDefault(Return(content::DownloadItem::IN_PROGRESS)); | 88 .WillByDefault(Return(content::DownloadItem::IN_PROGRESS)); |
65 ON_CALL(*download_item_, IsDangerous()).WillByDefault(Return(false)); | 89 ON_CALL(*download_item_, IsDangerous()).WillByDefault(Return(false)); |
66 ON_CALL(*download_item_, GetFileNameToReportUser()) | 90 ON_CALL(*download_item_, GetFileNameToReportUser()) |
67 .WillByDefault(Return(base::FilePath("TITLE.bin"))); | 91 .WillByDefault(Return(base::FilePath("TITLE.bin"))); |
68 ON_CALL(*download_item_, GetTargetFilePath()) | 92 ON_CALL(*download_item_, GetTargetFilePath()) |
69 .WillByDefault(ReturnRefOfCopy(download_item_target_path)); | 93 .WillByDefault(ReturnRefOfCopy(download_item_target_path)); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 } | 152 } |
129 | 153 |
130 bool ShownAsPopUp() { | 154 bool ShownAsPopUp() { |
131 return !notification()->shown_as_popup(); | 155 return !notification()->shown_as_popup(); |
132 } | 156 } |
133 | 157 |
134 void CreateDownloadItemNotification() { | 158 void CreateDownloadItemNotification() { |
135 download_notification_manager_->OnNewDownloadReady(download_item_.get()); | 159 download_notification_manager_->OnNewDownloadReady(download_item_.get()); |
136 download_item_notification_ = | 160 download_item_notification_ = |
137 download_notification_manager_->items_[download_item_.get()]; | 161 download_notification_manager_->items_[download_item_.get()]; |
| 162 message_center_->AddVisibleNotification( |
| 163 download_item_notification_->notification_.get()); |
138 } | 164 } |
139 | 165 |
140 base::MessageLoopForUI message_loop_; | 166 base::MessageLoopForUI message_loop_; |
141 content::TestBrowserThread ui_thread_; | 167 content::TestBrowserThread ui_thread_; |
142 | 168 |
143 scoped_ptr<TestingProfileManager> profile_manager_; | 169 scoped_ptr<TestingProfileManager> profile_manager_; |
144 Profile* profile_; | 170 Profile* profile_; |
145 | 171 |
146 scoped_ptr<NiceMock<content::MockDownloadItem>> download_item_; | 172 scoped_ptr<NiceMock<content::MockDownloadItem>> download_item_; |
147 scoped_ptr<DownloadNotificationManagerForProfile> | 173 scoped_ptr<DownloadNotificationManagerForProfile> |
148 download_notification_manager_; | 174 download_notification_manager_; |
| 175 scoped_ptr<MockMessageCenter> message_center_; |
149 DownloadItemNotification* download_item_notification_; | 176 DownloadItemNotification* download_item_notification_; |
150 }; | 177 }; |
151 | 178 |
152 TEST_F(DownloadItemNotificationTest, ShowAndCloseNotification) { | 179 TEST_F(DownloadItemNotificationTest, ShowAndCloseNotification) { |
153 EXPECT_EQ(0u, NotificationCount()); | 180 EXPECT_EQ(0u, NotificationCount()); |
154 | 181 |
155 // Shows a notification | 182 // Shows a notification |
156 CreateDownloadItemNotification(); | 183 CreateDownloadItemNotification(); |
157 download_item_->NotifyObserversDownloadOpened(); | 184 download_item_->NotifyObserversDownloadOpened(); |
158 | 185 |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 CreateDownloadItemNotification(); | 281 CreateDownloadItemNotification(); |
255 download_item_->NotifyObserversDownloadOpened(); | 282 download_item_->NotifyObserversDownloadOpened(); |
256 | 283 |
257 EXPECT_EQ(message_center::DEFAULT_PRIORITY, notification()->priority()); | 284 EXPECT_EQ(message_center::DEFAULT_PRIORITY, notification()->priority()); |
258 | 285 |
259 download_item_notification_->DisablePopup(); | 286 download_item_notification_->DisablePopup(); |
260 EXPECT_EQ(message_center::LOW_PRIORITY, notification()->priority()); | 287 EXPECT_EQ(message_center::LOW_PRIORITY, notification()->priority()); |
261 } | 288 } |
262 | 289 |
263 } // namespace test | 290 } // namespace test |
OLD | NEW |