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

Side by Side Diff: chrome/browser/printing/cloud_print/privet_notifications_unittest.cc

Issue 2446043002: Close privet printer notifications when clicked. (Closed)
Patch Set: Add test, fix potential UAF Created 4 years, 1 month 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/printing/cloud_print/privet_notifications.h" 5 #include "chrome/browser/printing/cloud_print/privet_notifications.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/threading/thread_task_runner_handle.h" 10 #include "base/threading/thread_task_runner_handle.h"
11 #include "chrome/browser/notifications/notification_test_util.h"
12 #include "chrome/browser/printing/cloud_print/privet_http_asynchronous_factory.h " 12 #include "chrome/browser/printing/cloud_print/privet_http_asynchronous_factory.h "
13 #include "chrome/browser/printing/cloud_print/privet_http_impl.h" 13 #include "chrome/browser/printing/cloud_print/privet_http_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/test_browser_thread_bundle.h"
14 #include "net/url_request/test_url_fetcher_factory.h" 18 #include "net/url_request/test_url_fetcher_factory.h"
15 #include "net/url_request/url_request_test_util.h" 19 #include "net/url_request/url_request_test_util.h"
16 #include "testing/gmock/include/gmock/gmock.h" 20 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
18 22
19 using testing::StrictMock; 23 using testing::StrictMock;
20 24
21 using ::testing::_; 25 using ::testing::_;
22 using ::testing::SaveArg; 26 using ::testing::SaveArg;
23 27
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 78
75 std::unique_ptr<PrivetHTTPResolution> CreatePrivetHTTP( 79 std::unique_ptr<PrivetHTTPResolution> CreatePrivetHTTP(
76 const std::string& name) override { 80 const std::string& name) override {
77 return base::MakeUnique<MockResolution>(name, request_context_.get()); 81 return base::MakeUnique<MockResolution>(name, request_context_.get());
78 } 82 }
79 83
80 private: 84 private:
81 scoped_refptr<net::URLRequestContextGetter> request_context_; 85 scoped_refptr<net::URLRequestContextGetter> request_context_;
82 }; 86 };
83 87
84 class PrivetNotificationsListenerTest : public ::testing::Test { 88 class PrivetNotificationsListenerTest : public testing::Test {
85 public: 89 public:
86 PrivetNotificationsListenerTest() 90 PrivetNotificationsListenerTest()
87 : request_context_(new net::TestURLRequestContextGetter( 91 : request_context_(new net::TestURLRequestContextGetter(
88 base::ThreadTaskRunnerHandle::Get())) { 92 base::ThreadTaskRunnerHandle::Get())) {
89 notification_listener_.reset(new PrivetNotificationsListener( 93 notification_listener_.reset(new PrivetNotificationsListener(
90 std::unique_ptr<PrivetHTTPAsynchronousFactory>( 94 std::unique_ptr<PrivetHTTPAsynchronousFactory>(
91 new MockPrivetHttpFactory(request_context_.get())), 95 new MockPrivetHttpFactory(request_context_.get())),
92 &mock_delegate_)); 96 &mock_delegate_));
93 97
94 description_.name = kExampleDeviceHumanName; 98 description_.name = kExampleDeviceHumanName;
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); 177 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK));
174 fetcher->set_response_code(200); 178 fetcher->set_response_code(200);
175 fetcher->delegate()->OnURLFetchComplete(fetcher); 179 fetcher->delegate()->OnURLFetchComplete(fetcher);
176 } 180 }
177 181
178 TEST_F(PrivetNotificationsListenerTest, DictionaryErrorTest) { 182 TEST_F(PrivetNotificationsListenerTest, DictionaryErrorTest) {
179 notification_listener_->DeviceChanged(kExampleDeviceName, description_); 183 notification_listener_->DeviceChanged(kExampleDeviceName, description_);
180 SuccessfulResponseToInfo(kInfoResponseNoUptime); 184 SuccessfulResponseToInfo(kInfoResponseNoUptime);
181 } 185 }
182 186
187 class TestPrivetNotificationService;
188
189 class TestPrivetNotificationDelegate : public PrivetNotificationDelegate {
190 public:
191 TestPrivetNotificationDelegate(TestPrivetNotificationService* service,
192 Profile* profile)
193 : PrivetNotificationDelegate(profile), service_(service) {}
194
195 private:
196 // Refcounted.
197 ~TestPrivetNotificationDelegate() override {}
198
199 // PrivetNotificationDelegate:
200 void OpenTab(const GURL& url) override;
201 void DisableNotifications() override;
202
203 TestPrivetNotificationService* const service_;
204
205 DISALLOW_COPY_AND_ASSIGN(TestPrivetNotificationDelegate);
206 };
207
208 class TestPrivetNotificationService : public PrivetNotificationService {
209 public:
210 explicit TestPrivetNotificationService(Profile* profile)
211 : PrivetNotificationService(profile) {}
212 ~TestPrivetNotificationService() override {}
213
214 const GURL& open_tab_url() const { return open_tab_url_; }
215 size_t open_tab_count() const { return open_tab_count_; }
216 size_t disable_notifications_count() const {
217 return disable_notifications_count_;
218 }
219
220 void OpenTab(const GURL& url) {
221 open_tab_url_ = url;
222 ++open_tab_count_;
223 }
224
225 void DisableNotifications() { ++disable_notifications_count_; }
226
227 private:
228 // PrivetNotificationService:
229 PrivetNotificationDelegate* CreateNotificationDelegate(
230 Profile* profile) override {
231 return new TestPrivetNotificationDelegate(this, profile);
232 }
233
234 GURL open_tab_url_;
235 size_t open_tab_count_ = 0;
236 size_t disable_notifications_count_ = 0;
237
238 DISALLOW_COPY_AND_ASSIGN(TestPrivetNotificationService);
239 };
240
241 void TestPrivetNotificationDelegate::OpenTab(const GURL& url) {
242 service_->OpenTab(url);
243 }
244
245 void TestPrivetNotificationDelegate::DisableNotifications() {
246 service_->DisableNotifications();
247 }
248
249 class PrivetNotificationsNotificationTest : public testing::Test {
250 public:
251 PrivetNotificationsNotificationTest() {}
252 ~PrivetNotificationsNotificationTest() override {}
253
254 void SetUp() override {
255 testing::Test::SetUp();
256
257 profile_manager_ = base::MakeUnique<TestingProfileManager>(
258 TestingBrowserProcess::GetGlobal());
259 ASSERT_TRUE(profile_manager_->SetUp());
260 profile_ = profile_manager_->CreateTestingProfile("test-user");
261
262 TestingBrowserProcess::GetGlobal()->SetNotificationUIManager(
263 base::MakeUnique<StubNotificationUIManager>());
264 }
265
266 void TearDown() override {
267 profile_manager_.reset();
268 testing::Test::TearDown();
269 }
270
271 protected:
272 StubNotificationUIManager* ui_manager() const {
273 return static_cast<StubNotificationUIManager*>(
274 TestingBrowserProcess::GetGlobal()->notification_ui_manager());
275 }
276
277 Profile* profile() { return profile_; }
278
279 private:
280 // The thread bundle must be first so it is destroyed last.
281 content::TestBrowserThreadBundle thread_bundle_;
282
283 std::unique_ptr<TestingProfileManager> profile_manager_;
284 Profile* profile_;
285
286 DISALLOW_COPY_AND_ASSIGN(PrivetNotificationsNotificationTest);
287 };
288
289 TEST_F(PrivetNotificationsNotificationTest, AddToCloudPrint) {
290 TestPrivetNotificationService service(profile());
291 service.PrivetNotify(1 /* devices_active */, true /* added */);
292
293 ASSERT_EQ(1U, ui_manager()->GetNotificationCount());
294 const auto& notification = ui_manager()->GetNotificationAt(0);
295 notification.ButtonClick(0 /* add */);
296
297 EXPECT_EQ("chrome://devices/", service.open_tab_url().spec());
298 EXPECT_EQ(1U, service.open_tab_count());
299 EXPECT_EQ(0U, service.disable_notifications_count());
300 EXPECT_EQ(0U, ui_manager()->GetNotificationCount());
301 }
302
303 TEST_F(PrivetNotificationsNotificationTest, DontShowAgain) {
304 TestPrivetNotificationService service(profile());
305 service.PrivetNotify(1 /* devices_active */, true /* added */);
306
307 ASSERT_EQ(1U, ui_manager()->GetNotificationCount());
308 const auto& notification = ui_manager()->GetNotificationAt(0);
309 notification.ButtonClick(1 /* don't show again */);
310
311 EXPECT_EQ("", service.open_tab_url().spec());
312 EXPECT_EQ(0U, service.open_tab_count());
313 EXPECT_EQ(1U, service.disable_notifications_count());
314 EXPECT_EQ(0U, ui_manager()->GetNotificationCount());
315 }
316
183 } // namespace 317 } // namespace
184 318
185 } // namespace cloud_print 319 } // namespace cloud_print
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698