OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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. |
| 4 |
| 5 #include "base/strings/utf_string_conversions.h" |
| 6 #include "chrome/browser/notifications/notification_test_util.h" |
| 7 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
| 8 #include "chrome/test/base/testing_profile.h" |
| 9 #include "content/public/browser/desktop_notification_delegate.h" |
| 10 #include "content/public/test/test_browser_thread_bundle.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 class MockDesktopNotificationDelegate |
| 16 : public content::DesktopNotificationDelegate { |
| 17 public: |
| 18 MockDesktopNotificationDelegate() |
| 19 : displayed_(false), |
| 20 clicked_(false) {} |
| 21 |
| 22 ~MockDesktopNotificationDelegate() override {} |
| 23 |
| 24 // content::DesktopNotificationDelegate implementation. |
| 25 void NotificationDisplayed() override { displayed_ = true; } |
| 26 void NotificationClosed(bool by_user) override {} |
| 27 void NotificationClick() override { clicked_ = true; } |
| 28 |
| 29 bool displayed() const { return displayed_; } |
| 30 bool clicked() const { return clicked_; } |
| 31 |
| 32 private: |
| 33 bool displayed_; |
| 34 bool clicked_; |
| 35 }; |
| 36 |
| 37 } // namespace |
| 38 |
| 39 class PlatformNotificationServiceTest : public testing::Test { |
| 40 public: |
| 41 void SetUp() override { |
| 42 ui_manager_.reset(new StubNotificationUIManager); |
| 43 profile_.reset(new TestingProfile()); |
| 44 |
| 45 service()->SetNotificationUIManagerForTesting(ui_manager_.get()); |
| 46 } |
| 47 |
| 48 void TearDown() override { |
| 49 service()->SetNotificationUIManagerForTesting(nullptr); |
| 50 |
| 51 profile_.reset(); |
| 52 ui_manager_.reset(); |
| 53 } |
| 54 |
| 55 protected: |
| 56 // Displays a simple, fake notifications and returns a weak pointer to the |
| 57 // delegate receiving events for it (ownership is transferred to the service). |
| 58 MockDesktopNotificationDelegate* CreateSimplePageNotification() const { |
| 59 return CreateSimplePageNotificationWithCloseClosure(nullptr); |
| 60 } |
| 61 |
| 62 // Displays a simple, fake notification and returns a weak pointer to the |
| 63 // delegate receiving events for it (ownership is transferred to the service). |
| 64 // The close closure may be specified if so desired. |
| 65 MockDesktopNotificationDelegate* CreateSimplePageNotificationWithCloseClosure( |
| 66 base::Closure* close_closure) const { |
| 67 content::ShowDesktopNotificationHostMsgParams params; |
| 68 params.origin = GURL("https://example.com/"); |
| 69 params.title = base::ASCIIToUTF16("My Notification"); |
| 70 params.body = base::ASCIIToUTF16("Hello, world!"); |
| 71 |
| 72 MockDesktopNotificationDelegate* delegate = |
| 73 new MockDesktopNotificationDelegate(); |
| 74 |
| 75 service()->DisplayNotification(profile(), |
| 76 params, |
| 77 make_scoped_ptr(delegate), |
| 78 0 /* render_process_id */, |
| 79 close_closure); |
| 80 |
| 81 return delegate; |
| 82 } |
| 83 |
| 84 // Returns the Platform Notification Service these unit tests are for. |
| 85 PlatformNotificationServiceImpl* service() const { |
| 86 return PlatformNotificationServiceImpl::GetInstance(); |
| 87 } |
| 88 |
| 89 // Returns the Profile to be used for these tests. |
| 90 Profile* profile() const { return profile_.get(); } |
| 91 |
| 92 // Returns the UI Manager on which notifications will be displayed. |
| 93 StubNotificationUIManager* ui_manager() const { return ui_manager_.get(); } |
| 94 |
| 95 private: |
| 96 scoped_ptr<StubNotificationUIManager> ui_manager_; |
| 97 scoped_ptr<TestingProfile> profile_; |
| 98 |
| 99 content::TestBrowserThreadBundle thread_bundle_; |
| 100 }; |
| 101 |
| 102 TEST_F(PlatformNotificationServiceTest, DisplayPageDisplayedEvent) { |
| 103 auto* delegate = CreateSimplePageNotification(); |
| 104 |
| 105 EXPECT_EQ(1u, ui_manager()->GetNotificationCount()); |
| 106 EXPECT_TRUE(delegate->displayed()); |
| 107 } |
| 108 |
| 109 TEST_F(PlatformNotificationServiceTest, DisplayPageCloseClosure) { |
| 110 base::Closure close_closure; |
| 111 CreateSimplePageNotificationWithCloseClosure(&close_closure); |
| 112 |
| 113 EXPECT_EQ(1u, ui_manager()->GetNotificationCount()); |
| 114 |
| 115 ASSERT_FALSE(close_closure.is_null()); |
| 116 close_closure.Run(); |
| 117 |
| 118 EXPECT_EQ(0u, ui_manager()->GetNotificationCount()); |
| 119 |
| 120 // Note that we cannot verify whether the closed event was called on the |
| 121 // delegate given that it'd result in a use-after-free. |
| 122 } |
| 123 |
| 124 TEST_F(PlatformNotificationServiceTest, DisplayPageNotificationMatches) { |
| 125 content::ShowDesktopNotificationHostMsgParams params; |
| 126 params.origin = GURL("https://chrome.com/"); |
| 127 params.title = base::ASCIIToUTF16("My notification's title"); |
| 128 params.body = base::ASCIIToUTF16("Hello, world!"); |
| 129 |
| 130 MockDesktopNotificationDelegate* delegate |
| 131 = new MockDesktopNotificationDelegate(); |
| 132 service()->DisplayNotification(profile(), |
| 133 params, |
| 134 make_scoped_ptr(delegate), |
| 135 0 /* render_process_id */, |
| 136 nullptr); |
| 137 |
| 138 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); |
| 139 |
| 140 const Notification& notification = ui_manager()->GetNotificationAt(0); |
| 141 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); |
| 142 EXPECT_EQ("My notification's title", |
| 143 base::UTF16ToUTF8(notification.title())); |
| 144 EXPECT_EQ("Hello, world!", |
| 145 base::UTF16ToUTF8(notification.message())); |
| 146 } |
| 147 |
| 148 TEST_F(PlatformNotificationServiceTest, DisplayNameForOrigin) { |
| 149 base::string16 display_name = |
| 150 service()->DisplayNameForOriginInProcessId(profile(), |
| 151 GURL("https://chrome.com/"), |
| 152 0 /* render_process_id */); |
| 153 |
| 154 EXPECT_EQ(base::ASCIIToUTF16("chrome.com"), display_name); |
| 155 |
| 156 // TODO(peter): Include unit tests for the extension-name translation |
| 157 // functionality of DisplayNameForOriginInProcessId. |
| 158 } |
OLD | NEW |