Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(25)

Side by Side Diff: chrome/browser/download/notification/download_notification_item_unittest.cc

Issue 1005393003: [Download Notification] Use NotificationUIManager instead of MessageCenter (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed the comments Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_notification_item.h" 5 #include "chrome/browser/download/notification/download_notification_item.h"
6 6
7 #include "base/message_loop/message_loop.h"
7 #include "base/run_loop.h" 8 #include "base/run_loop.h"
8 #include "base/test/test_simple_task_runner.h" 9 #include "base/test/test_simple_task_runner.h"
9 #include "base/thread_task_runner_handle.h" 10 #include "base/thread_task_runner_handle.h"
11 #include "chrome/browser/notifications/notification_test_util.h"
12 #include "chrome/browser/notifications/platform_notification_service_impl.h"
13 #include "chrome/test/base/testing_browser_process.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "chrome/test/base/testing_profile_manager.h"
10 #include "content/public/test/mock_download_item.h" 16 #include "content/public/test/mock_download_item.h"
11 #include "content/public/test/mock_download_manager.h" 17 #include "content/public/test/mock_download_manager.h"
18 #include "content/public/test/test_browser_thread.h"
12 #include "testing/gmock/include/gmock/gmock.h" 19 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/message_center/fake_message_center.h" 21 #include "ui/message_center/fake_message_center.h"
15 22
16 using testing::NiceMock; 23 using testing::NiceMock;
17 using testing::Return; 24 using testing::Return;
18 using testing::_; 25 using testing::_;
19 26
20 namespace { 27 namespace {
21 28
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 size_t on_download_stopped_call_count_; 64 size_t on_download_stopped_call_count_;
58 }; 65 };
59 66
60 } // anonymous namespace 67 } // anonymous namespace
61 68
62 namespace test { 69 namespace test {
63 70
64 class DownloadNotificationItemTest : public testing::Test { 71 class DownloadNotificationItemTest : public testing::Test {
65 public: 72 public:
66 DownloadNotificationItemTest() 73 DownloadNotificationItemTest()
67 : runner_(new base::TestSimpleTaskRunner), runner_handler_(runner_) {} 74 : ui_thread_(content::BrowserThread::UI, &message_loop_),
75 profile_(nullptr) {}
68 76
69 void SetUp() override { 77 void SetUp() override {
70 testing::Test::SetUp(); 78 testing::Test::SetUp();
71 message_center::MessageCenter::Initialize(); 79 message_center::MessageCenter::Initialize();
72 80
81 profile_manager_.reset(
82 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
83 ASSERT_TRUE(profile_manager_->SetUp());
84 profile_ = profile_manager_->CreateTestingProfile("test-user");
85
86 ui_manager_.reset(new StubNotificationUIManager);
87 DownloadNotificationItem::SetStubNotificationUIManagerForTesting(
88 ui_manager_.get());
89
73 download_item_.reset(new NiceMock<content::MockDownloadItem>()); 90 download_item_.reset(new NiceMock<content::MockDownloadItem>());
74 ON_CALL(*download_item_, GetId()).WillByDefault(Return(12345)); 91 ON_CALL(*download_item_, GetId()).WillByDefault(Return(12345));
75 ON_CALL(*download_item_, GetState()) 92 ON_CALL(*download_item_, GetState())
76 .WillByDefault(Return(content::DownloadItem::IN_PROGRESS)); 93 .WillByDefault(Return(content::DownloadItem::IN_PROGRESS));
77 ON_CALL(*download_item_, IsDangerous()).WillByDefault(Return(false)); 94 ON_CALL(*download_item_, IsDangerous()).WillByDefault(Return(false));
78 ON_CALL(*download_item_, GetFileNameToReportUser()) 95 ON_CALL(*download_item_, GetFileNameToReportUser())
79 .WillByDefault(Return(base::FilePath("TITLE.bin"))); 96 .WillByDefault(Return(base::FilePath("TITLE.bin")));
80 ON_CALL(*download_item_, GetDangerType()) 97 ON_CALL(*download_item_, GetDangerType())
81 .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS)); 98 .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
82 ON_CALL(*download_item_, IsDone()).WillByDefault(Return(false)); 99 ON_CALL(*download_item_, IsDone()).WillByDefault(Return(false));
83 } 100 }
84 101
85 void TearDown() override { 102 void TearDown() override {
86 download_notification_item_.reset(); 103 download_notification_item_.reset();
104 profile_manager_.reset();
87 message_center::MessageCenter::Shutdown(); 105 message_center::MessageCenter::Shutdown();
88 testing::Test::TearDown(); 106 testing::Test::TearDown();
89 } 107 }
90 108
91 protected: 109 protected:
92 message_center::MessageCenter* message_center() { 110 message_center::MessageCenter* message_center() {
93 return message_center::MessageCenter::Get(); 111 return message_center::MessageCenter::Get();
94 } 112 }
95 113
96 std::string notification_id() { 114 std::string notification_id() const {
97 return download_notification_item_->notification_->id(); 115 return download_notification_item_->notification_->id();
98 } 116 }
99 117
100 message_center::Notification* notification() { 118 const Notification* notification() const {
101 return message_center()->FindVisibleNotificationById(notification_id()); 119 return ui_manager_->FindById(download_notification_item_->watcher_->id(),
120 NotificationUIManager::GetProfileID(profile_));
121 }
122
123 size_t NotificationCount() const {
124 return ui_manager_
125 ->GetAllIdsByProfileAndSourceOrigin(
126 profile_,
127 GURL(DownloadNotificationItem::kDownloadNotificationOrigin))
128 .size();
129 }
130
131 void RemoveNotification() {
132 ui_manager_->CancelById(download_notification_item_->watcher_->id(),
133 NotificationUIManager::GetProfileID(profile_));
134
135 // Waits, since removing a notification may cause an async job.
136 base::RunLoop().RunUntilIdle();
102 } 137 }
103 138
104 // Trampoline methods to access a private method in DownloadNotificationItem. 139 // Trampoline methods to access a private method in DownloadNotificationItem.
105 void NotificationItemClick() { 140 void NotificationItemClick() {
106 return download_notification_item_->OnNotificationClick(); 141 return download_notification_item_->OnNotificationClick();
107 } 142 }
108 void NotificationItemButtonClick(int index) { 143 void NotificationItemButtonClick(int index) {
109 return download_notification_item_->OnNotificationButtonClick(index); 144 return download_notification_item_->OnNotificationButtonClick(index);
110 } 145 }
111 146
112 bool IsPopupNotification(const std::string& notification_id) { 147 bool ShownAsPopUp() {
113 message_center::NotificationList::PopupNotifications popups = 148 return !download_notification_item_->notification_->shown_as_popup();
Randy Smith (Not in Mondays) 2015/04/15 19:16:31 Why the inversion? I don't see anything in the co
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 } 149 }
122 150
123 void CreateDownloadNotificationItem() { 151 void CreateDownloadNotificationItem() {
124 download_notification_item_.reset( 152 download_notification_item_.reset(new DownloadNotificationItem(
125 new DownloadNotificationItem(download_item_.get(), &delegate_)); 153 download_item_.get(), profile_, &delegate_));
126 } 154 }
127 155
128 scoped_refptr<base::TestSimpleTaskRunner> runner_; 156 base::MessageLoopForUI message_loop_;
129 base::ThreadTaskRunnerHandle runner_handler_; 157 content::TestBrowserThread ui_thread_;
158
159 scoped_ptr<StubNotificationUIManager> ui_manager_;
160
161 scoped_ptr<TestingProfileManager> profile_manager_;
162 Profile* profile_;
130 163
131 MockDownloadNotificationItemDelegate delegate_; 164 MockDownloadNotificationItemDelegate delegate_;
132 scoped_ptr<NiceMock<content::MockDownloadItem>> download_item_; 165 scoped_ptr<NiceMock<content::MockDownloadItem>> download_item_;
133 scoped_ptr<DownloadNotificationItem> download_notification_item_; 166 scoped_ptr<DownloadNotificationItem> download_notification_item_;
134 }; 167 };
135 168
136 TEST_F(DownloadNotificationItemTest, ShowAndCloseNotification) { 169 TEST_F(DownloadNotificationItemTest, ShowAndCloseNotification) {
137 EXPECT_EQ(0u, message_center()->NotificationCount()); 170 EXPECT_EQ(0u, NotificationCount());
138 171
139 // Shows a notification 172 // Shows a notification
140 CreateDownloadNotificationItem(); 173 CreateDownloadNotificationItem();
141 download_item_->NotifyObserversDownloadOpened(); 174 download_item_->NotifyObserversDownloadOpened();
142 175
143 // Confirms that the notification is shown as a popup. 176 // Confirms that the notification is shown as a popup.
144 EXPECT_EQ(1u, message_center()->NotificationCount()); 177 EXPECT_TRUE(ShownAsPopUp());
145 EXPECT_TRUE(IsPopupNotification(notification_id())); 178 EXPECT_EQ(1u, NotificationCount());
146 179
147 // Makes sure the DownloadItem::Cancel() is not called. 180 // Makes sure the DownloadItem::Cancel() is not called.
148 EXPECT_CALL(*download_item_, Cancel(_)).Times(0); 181 EXPECT_CALL(*download_item_, Cancel(_)).Times(0);
149 // Closes it once. 182 // Closes it once.
150 message_center()->RemoveNotification(notification_id(), true); 183 RemoveNotification();
151
152 // Confirms that the notification is shown but is not a popup.
153 EXPECT_EQ(1u, message_center()->NotificationCount());
154 EXPECT_FALSE(IsPopupNotification(notification_id()));
155
156 // Makes sure the DownloadItem::Cancel() is called once.
157 EXPECT_CALL(*download_item_, Cancel(_)).Times(1);
158 // Closes it again.
159 message_center()->RemoveNotification(notification_id(), true);
160 184
161 // Confirms that the notification is closed. 185 // Confirms that the notification is closed.
162 EXPECT_EQ(0u, message_center()->NotificationCount()); 186 EXPECT_EQ(0u, NotificationCount());
187
188 // Makes sure the DownloadItem::Cancel() is never called.
189 EXPECT_CALL(*download_item_, Cancel(_)).Times(0);
163 } 190 }
164 191
165 TEST_F(DownloadNotificationItemTest, PauseAndResumeNotification) { 192 TEST_F(DownloadNotificationItemTest, PauseAndResumeNotification) {
166 // Shows a notification 193 // Shows a notification
167 CreateDownloadNotificationItem(); 194 CreateDownloadNotificationItem();
168 download_item_->NotifyObserversDownloadOpened(); 195 download_item_->NotifyObserversDownloadOpened();
169 196
170 // Confirms that the notification is shown as a popup. 197 // Confirms that the notification is shown as a popup.
171 EXPECT_EQ(message_center()->NotificationCount(), 1u); 198 EXPECT_EQ(1u, NotificationCount());
172 EXPECT_TRUE(IsPopupNotification(notification_id()));
173 199
174 // Pauses and makes sure the DownloadItem::Pause() is called. 200 // Pauses and makes sure the DownloadItem::Pause() is called.
175 EXPECT_CALL(*download_item_, Pause()).Times(1); 201 EXPECT_CALL(*download_item_, Pause()).Times(1);
176 EXPECT_CALL(*download_item_, IsPaused()).WillRepeatedly(Return(true)); 202 EXPECT_CALL(*download_item_, IsPaused()).WillRepeatedly(Return(true));
177 NotificationItemButtonClick(0); 203 NotificationItemButtonClick(0);
178 download_item_->NotifyObserversDownloadUpdated(); 204 download_item_->NotifyObserversDownloadUpdated();
179 205
180 // Resumes and makes sure the DownloadItem::Resume() is called. 206 // Resumes and makes sure the DownloadItem::Resume() is called.
181 EXPECT_CALL(*download_item_, Resume()).Times(1); 207 EXPECT_CALL(*download_item_, Resume()).Times(1);
182 EXPECT_CALL(*download_item_, IsPaused()).WillRepeatedly(Return(false)); 208 EXPECT_CALL(*download_item_, IsPaused()).WillRepeatedly(Return(false));
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 286
261 // Checks OnDownloadRemoved(). 287 // Checks OnDownloadRemoved().
262 EXPECT_EQ(0u, delegate_.GetOnDownloadRemovedCallCount()); 288 EXPECT_EQ(0u, delegate_.GetOnDownloadRemovedCallCount());
263 download_item_->NotifyObserversDownloadRemoved(); 289 download_item_->NotifyObserversDownloadRemoved();
264 EXPECT_EQ(1u, delegate_.GetOnDownloadRemovedCallCount()); 290 EXPECT_EQ(1u, delegate_.GetOnDownloadRemovedCallCount());
265 291
266 download_item_.reset(); 292 download_item_.reset();
267 } 293 }
268 294
269 } // namespace test 295 } // namespace test
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698