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

Unified 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, 2 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/printing/cloud_print/privet_notifications_unittest.cc
diff --git a/chrome/browser/printing/cloud_print/privet_notifications_unittest.cc b/chrome/browser/printing/cloud_print/privet_notifications_unittest.cc
index 9a8a2aa2817f05274239fc003c3eb62b9c2c1a8e..8ef4c48a16eb0ac63f238d48400ed3a7168db0db 100644
--- a/chrome/browser/printing/cloud_print/privet_notifications_unittest.cc
+++ b/chrome/browser/printing/cloud_print/privet_notifications_unittest.cc
@@ -7,10 +7,14 @@
#include <memory>
#include "base/memory/ptr_util.h"
-#include "base/message_loop/message_loop.h"
#include "base/threading/thread_task_runner_handle.h"
+#include "chrome/browser/notifications/notification_test_util.h"
#include "chrome/browser/printing/cloud_print/privet_http_asynchronous_factory.h"
#include "chrome/browser/printing/cloud_print/privet_http_impl.h"
+#include "chrome/test/base/testing_browser_process.h"
+#include "chrome/test/base/testing_profile.h"
+#include "chrome/test/base/testing_profile_manager.h"
+#include "content/public/test/test_browser_thread_bundle.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gmock/include/gmock/gmock.h"
@@ -81,7 +85,7 @@ class MockPrivetHttpFactory : public PrivetHTTPAsynchronousFactory {
scoped_refptr<net::URLRequestContextGetter> request_context_;
};
-class PrivetNotificationsListenerTest : public ::testing::Test {
+class PrivetNotificationsListenerTest : public testing::Test {
public:
PrivetNotificationsListenerTest()
: request_context_(new net::TestURLRequestContextGetter(
@@ -180,6 +184,136 @@ TEST_F(PrivetNotificationsListenerTest, DictionaryErrorTest) {
SuccessfulResponseToInfo(kInfoResponseNoUptime);
}
+class TestPrivetNotificationService;
+
+class TestPrivetNotificationDelegate : public PrivetNotificationDelegate {
+ public:
+ TestPrivetNotificationDelegate(TestPrivetNotificationService* service,
+ Profile* profile)
+ : PrivetNotificationDelegate(profile), service_(service) {}
+
+ private:
+ // Refcounted.
+ ~TestPrivetNotificationDelegate() override {}
+
+ // PrivetNotificationDelegate:
+ void OpenTab(const GURL& url) override;
+ void DisableNotifications() override;
+
+ TestPrivetNotificationService* const service_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestPrivetNotificationDelegate);
+};
+
+class TestPrivetNotificationService : public PrivetNotificationService {
+ public:
+ explicit TestPrivetNotificationService(Profile* profile)
+ : PrivetNotificationService(profile) {}
+ ~TestPrivetNotificationService() override {}
+
+ const GURL& open_tab_url() const { return open_tab_url_; }
+ size_t open_tab_count() const { return open_tab_count_; }
+ size_t disable_notifications_count() const {
+ return disable_notifications_count_;
+ }
+
+ void OpenTab(const GURL& url) {
+ open_tab_url_ = url;
+ ++open_tab_count_;
+ }
+
+ void DisableNotifications() { ++disable_notifications_count_; }
+
+ private:
+ // PrivetNotificationService:
+ PrivetNotificationDelegate* CreateNotificationDelegate(
+ Profile* profile) override {
+ return new TestPrivetNotificationDelegate(this, profile);
+ }
+
+ GURL open_tab_url_;
+ size_t open_tab_count_ = 0;
+ size_t disable_notifications_count_ = 0;
+
+ DISALLOW_COPY_AND_ASSIGN(TestPrivetNotificationService);
+};
+
+void TestPrivetNotificationDelegate::OpenTab(const GURL& url) {
+ service_->OpenTab(url);
+}
+
+void TestPrivetNotificationDelegate::DisableNotifications() {
+ service_->DisableNotifications();
+}
+
+class PrivetNotificationsNotificationTest : public testing::Test {
+ public:
+ PrivetNotificationsNotificationTest() {}
+ ~PrivetNotificationsNotificationTest() override {}
+
+ void SetUp() override {
+ testing::Test::SetUp();
+
+ profile_manager_ = base::MakeUnique<TestingProfileManager>(
+ TestingBrowserProcess::GetGlobal());
+ ASSERT_TRUE(profile_manager_->SetUp());
+ profile_ = profile_manager_->CreateTestingProfile("test-user");
+
+ TestingBrowserProcess::GetGlobal()->SetNotificationUIManager(
+ base::MakeUnique<StubNotificationUIManager>());
+ }
+
+ void TearDown() override {
+ profile_manager_.reset();
+ testing::Test::TearDown();
+ }
+
+ protected:
+ StubNotificationUIManager* ui_manager() const {
+ return static_cast<StubNotificationUIManager*>(
+ TestingBrowserProcess::GetGlobal()->notification_ui_manager());
+ }
+
+ Profile* profile() { return profile_; }
+
+ private:
+ // The thread bundle must be first so it is destroyed last.
+ content::TestBrowserThreadBundle thread_bundle_;
+
+ std::unique_ptr<TestingProfileManager> profile_manager_;
+ Profile* profile_;
+
+ DISALLOW_COPY_AND_ASSIGN(PrivetNotificationsNotificationTest);
+};
+
+TEST_F(PrivetNotificationsNotificationTest, AddToCloudPrint) {
+ TestPrivetNotificationService service(profile());
+ service.PrivetNotify(1 /* devices_active */, true /* added */);
+
+ ASSERT_EQ(1U, ui_manager()->GetNotificationCount());
+ const auto& notification = ui_manager()->GetNotificationAt(0);
+ notification.ButtonClick(0 /* add */);
+
+ EXPECT_EQ("chrome://devices/", service.open_tab_url().spec());
+ EXPECT_EQ(1U, service.open_tab_count());
+ EXPECT_EQ(0U, service.disable_notifications_count());
+ EXPECT_EQ(0U, ui_manager()->GetNotificationCount());
+}
+
+TEST_F(PrivetNotificationsNotificationTest, DontShowAgain) {
+ TestPrivetNotificationService service(profile());
+ service.PrivetNotify(1 /* devices_active */, true /* added */);
+
+ ASSERT_EQ(1U, ui_manager()->GetNotificationCount());
+ const auto& notification = ui_manager()->GetNotificationAt(0);
+ notification.ButtonClick(1 /* don't show again */);
+
+ EXPECT_EQ("", service.open_tab_url().spec());
+ EXPECT_EQ(0U, service.open_tab_count());
+ EXPECT_EQ(1U, service.disable_notifications_count());
+ EXPECT_EQ(0U, ui_manager()->GetNotificationCount());
+}
+
} // namespace
} // namespace cloud_print

Powered by Google App Engine
This is Rietveld 408576698