Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 <stdint.h> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/threading/platform_thread.h" | 12 #include "base/threading/platform_thread.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 15 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 15 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 16 #include "chrome/browser/notifications/message_center_display_service.h" | 16 #include "chrome/browser/notifications/message_center_display_service.h" |
| 17 #include "chrome/browser/notifications/notification_delegate.h" | 17 #include "chrome/browser/notifications/notification_delegate.h" |
| 18 #include "chrome/browser/notifications/notification_display_service_factory.h" | |
| 18 #include "chrome/browser/notifications/notification_test_util.h" | 19 #include "chrome/browser/notifications/notification_test_util.h" |
| 19 #include "chrome/browser/notifications/platform_notification_service_impl.h" | 20 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
| 21 #include "chrome/browser/notifications/stub_notification_platform_bridge.h" | |
| 20 #include "chrome/test/base/testing_browser_process.h" | 22 #include "chrome/test/base/testing_browser_process.h" |
| 21 #include "chrome/test/base/testing_profile.h" | 23 #include "chrome/test/base/testing_profile.h" |
| 22 #include "chrome/test/base/testing_profile_manager.h" | 24 #include "chrome/test/base/testing_profile_manager.h" |
| 23 #include "components/content_settings/core/browser/host_content_settings_map.h" | 25 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 24 #include "content/public/browser/desktop_notification_delegate.h" | 26 #include "content/public/browser/desktop_notification_delegate.h" |
| 25 #include "content/public/common/notification_resources.h" | 27 #include "content/public/common/notification_resources.h" |
| 26 #include "content/public/common/platform_notification_data.h" | 28 #include "content/public/common/platform_notification_data.h" |
| 27 #include "content/public/test/test_browser_thread_bundle.h" | 29 #include "content/public/test/test_browser_thread_bundle.h" |
| 28 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 78 private: | 80 private: |
| 79 bool displayed_; | 81 bool displayed_; |
| 80 bool clicked_; | 82 bool clicked_; |
| 81 }; | 83 }; |
| 82 | 84 |
| 83 } // namespace | 85 } // namespace |
| 84 | 86 |
| 85 class PlatformNotificationServiceTest : public testing::Test { | 87 class PlatformNotificationServiceTest : public testing::Test { |
| 86 public: | 88 public: |
| 87 void SetUp() override { | 89 void SetUp() override { |
| 88 profile_.reset(new TestingProfile()); | |
| 89 ui_manager_.reset(new StubNotificationUIManager); | |
| 90 display_service_.reset( | |
| 91 new MessageCenterDisplayService(profile_.get(), ui_manager_.get())); | |
| 92 service()->SetNotificationDisplayServiceForTesting(display_service_.get()); | |
| 93 profile_manager_.reset( | 90 profile_manager_.reset( |
| 94 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); | 91 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); |
| 95 ASSERT_TRUE(profile_manager_->SetUp()); | 92 ASSERT_TRUE(profile_manager_->SetUp()); |
| 93 profile_ = profile_manager_->CreateTestingProfile("Miguel"); | |
| 94 std::unique_ptr<NotificationUIManager> ui_manager( | |
| 95 new StubNotificationUIManager); | |
| 96 std::unique_ptr<NotificationPlatformBridge> notification_bridge( | |
| 97 new StubNotificationPlatformBridge()); | |
| 98 | |
| 99 TestingBrowserProcess::GetGlobal()->SetNotificationUIManager( | |
| 100 std::move(ui_manager)); | |
| 101 TestingBrowserProcess::GetGlobal()->SetNotificationPlatformBridge( | |
| 102 std::move(notification_bridge)); | |
| 96 } | 103 } |
| 97 | 104 |
| 98 void TearDown() override { | 105 void TearDown() override { |
| 99 service()->SetNotificationDisplayServiceForTesting(nullptr); | |
| 100 display_service_.reset(); | |
| 101 ui_manager_.reset(); | |
| 102 profile_.reset(); | |
| 103 profile_manager_.reset(); | 106 profile_manager_.reset(); |
| 104 TestingBrowserProcess::DeleteInstance(); | 107 TestingBrowserProcess::DeleteInstance(); |
| 105 } | 108 } |
| 106 | 109 |
| 107 protected: | 110 protected: |
| 108 // Displays a simple, fake notifications and returns a weak pointer to the | 111 // Displays a simple, fake notifications and returns a weak pointer to the |
| 109 // delegate receiving events for it (ownership is transferred to the service). | 112 // delegate receiving events for it (ownership is transferred to the service). |
| 110 MockDesktopNotificationDelegate* CreateSimplePageNotification() const { | 113 MockDesktopNotificationDelegate* CreateSimplePageNotification() const { |
| 111 return CreateSimplePageNotificationWithCloseClosure(nullptr); | 114 return CreateSimplePageNotificationWithCloseClosure(nullptr); |
| 112 } | 115 } |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 129 | 132 |
| 130 return delegate; | 133 return delegate; |
| 131 } | 134 } |
| 132 | 135 |
| 133 // Returns the Platform Notification Service these unit tests are for. | 136 // Returns the Platform Notification Service these unit tests are for. |
| 134 PlatformNotificationServiceImpl* service() const { | 137 PlatformNotificationServiceImpl* service() const { |
| 135 return PlatformNotificationServiceImpl::GetInstance(); | 138 return PlatformNotificationServiceImpl::GetInstance(); |
| 136 } | 139 } |
| 137 | 140 |
| 138 // Returns the Profile to be used for these tests. | 141 // Returns the Profile to be used for these tests. |
| 139 Profile* profile() const { return profile_.get(); } | 142 Profile* profile() const { return profile_; } |
| 140 | 143 |
| 141 // Returns the UI Manager on which notifications will be displayed. | 144 size_t GetNotificationCount() const { |
| 142 StubNotificationUIManager* ui_manager() const { return ui_manager_.get(); } | 145 std::set<std::string> notifications; |
| 146 EXPECT_TRUE(display_service()->GetDisplayed(¬ifications)); | |
| 147 return notifications.size(); | |
| 148 } | |
| 149 | |
| 150 Notification GetDisplayedNotification() { | |
| 151 #if defined(OS_ANDROID) | |
| 152 return static_cast<StubNotificationPlatformBridge*>( | |
| 153 g_browser_process->notification_platform_bridge()) | |
| 154 ->GetNotificationAt(0); | |
| 155 #else | |
| 156 return static_cast<StubNotificationUIManager*>( | |
| 157 g_browser_process->notification_ui_manager()) | |
| 158 ->GetNotificationAt(0); | |
| 159 #endif | |
|
Peter Beverloo
2016/04/25 13:37:29
This is why I'd like this test to use a mocked Not
Miguel Garcia
2016/04/25 17:19:59
Yeah I knew you would not like this one. I was hop
| |
| 160 } | |
| 143 | 161 |
| 144 private: | 162 private: |
| 145 std::unique_ptr<TestingProfile> profile_; | 163 NotificationDisplayService* display_service() const { |
| 146 std::unique_ptr<StubNotificationUIManager> ui_manager_; | 164 return NotificationDisplayServiceFactory::GetForProfile(profile_); |
| 147 std::unique_ptr<MessageCenterDisplayService> display_service_; | 165 } |
| 148 | 166 |
| 149 std::unique_ptr<TestingProfileManager> profile_manager_; | 167 scoped_ptr<TestingProfileManager> profile_manager_; |
| 168 TestingProfile* profile_; | |
| 150 content::TestBrowserThreadBundle thread_bundle_; | 169 content::TestBrowserThreadBundle thread_bundle_; |
| 151 }; | 170 }; |
| 152 | 171 |
| 153 TEST_F(PlatformNotificationServiceTest, DisplayPageDisplayedEvent) { | 172 TEST_F(PlatformNotificationServiceTest, DisplayPageDisplayedEvent) { |
| 154 auto* delegate = CreateSimplePageNotification(); | 173 auto* delegate = CreateSimplePageNotification(); |
| 155 | 174 |
| 156 EXPECT_EQ(1u, ui_manager()->GetNotificationCount()); | 175 EXPECT_EQ(1u, GetNotificationCount()); |
| 157 EXPECT_TRUE(delegate->displayed()); | 176 EXPECT_TRUE(delegate->displayed()); |
| 158 } | 177 } |
| 159 | 178 |
| 160 TEST_F(PlatformNotificationServiceTest, DisplayPageCloseClosure) { | 179 TEST_F(PlatformNotificationServiceTest, DisplayPageCloseClosure) { |
| 161 base::Closure close_closure; | 180 base::Closure close_closure; |
| 162 CreateSimplePageNotificationWithCloseClosure(&close_closure); | 181 CreateSimplePageNotificationWithCloseClosure(&close_closure); |
| 163 | 182 |
| 164 EXPECT_EQ(1u, ui_manager()->GetNotificationCount()); | 183 EXPECT_EQ(1u, GetNotificationCount()); |
| 165 | 184 |
| 166 ASSERT_FALSE(close_closure.is_null()); | 185 ASSERT_FALSE(close_closure.is_null()); |
| 167 close_closure.Run(); | 186 close_closure.Run(); |
| 168 | 187 |
| 169 EXPECT_EQ(0u, ui_manager()->GetNotificationCount()); | 188 EXPECT_EQ(0u, GetNotificationCount()); |
| 170 | |
| 171 // Note that we cannot verify whether the closed event was called on the | 189 // Note that we cannot verify whether the closed event was called on the |
| 172 // delegate given that it'd result in a use-after-free. | 190 // delegate given that it'd result in a use-after-free. |
| 173 } | 191 } |
| 174 | 192 |
| 175 // TODO(peter): Re-enable this test when //content is responsible for creating | 193 // TODO(peter): Re-enable this test when //content is responsible for creating |
| 176 // the notification delegate ids. | 194 // the notification delegate ids. |
| 177 #if !defined(OS_ANDROID) | 195 #if !defined(OS_ANDROID) |
| 178 TEST_F(PlatformNotificationServiceTest, PersistentNotificationDisplay) { | 196 TEST_F(PlatformNotificationServiceTest, PersistentNotificationDisplay) { |
| 179 PlatformNotificationData notification_data; | 197 PlatformNotificationData notification_data; |
| 180 notification_data.title = base::ASCIIToUTF16("My notification's title"); | 198 notification_data.title = base::ASCIIToUTF16("My notification's title"); |
| 181 notification_data.body = base::ASCIIToUTF16("Hello, world!"); | 199 notification_data.body = base::ASCIIToUTF16("Hello, world!"); |
| 182 | 200 |
| 183 service()->DisplayPersistentNotification( | 201 service()->DisplayPersistentNotification( |
| 184 profile(), kPersistentNotificationId, GURL("https://chrome.com/"), | 202 profile(), kPersistentNotificationId, GURL("https://chrome.com/"), |
| 185 notification_data, NotificationResources()); | 203 notification_data, NotificationResources()); |
| 186 | 204 |
| 187 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); | 205 ASSERT_EQ(1u, GetNotificationCount()); |
| 188 | 206 |
| 189 const Notification& notification = ui_manager()->GetNotificationAt(0); | 207 const Notification& notification = GetDisplayedNotification(); |
|
Peter Beverloo
2016/04/25 13:37:29
nit: no const& (GetDisplayedNotification returns b
Miguel Garcia
2016/04/25 17:19:59
Done.
| |
| 190 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); | 208 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); |
| 191 EXPECT_EQ("My notification's title", | 209 EXPECT_EQ("My notification's title", |
| 192 base::UTF16ToUTF8(notification.title())); | 210 base::UTF16ToUTF8(notification.title())); |
| 193 EXPECT_EQ("Hello, world!", | 211 EXPECT_EQ("Hello, world!", |
| 194 base::UTF16ToUTF8(notification.message())); | 212 base::UTF16ToUTF8(notification.message())); |
| 195 | 213 |
| 196 service()->ClosePersistentNotification(profile(), kPersistentNotificationId); | 214 service()->ClosePersistentNotification(profile(), kPersistentNotificationId); |
| 197 EXPECT_EQ(0u, ui_manager()->GetNotificationCount()); | 215 EXPECT_EQ(0u, GetNotificationCount()); |
| 198 } | 216 } |
| 199 #endif // !defined(OS_ANDROID) | 217 #endif // !defined(OS_ANDROID) |
| 200 | 218 |
| 201 TEST_F(PlatformNotificationServiceTest, DisplayPageNotificationMatches) { | 219 TEST_F(PlatformNotificationServiceTest, DisplayPageNotificationMatches) { |
| 202 std::vector<int> vibration_pattern( | 220 std::vector<int> vibration_pattern( |
| 203 kNotificationVibrationPattern, | 221 kNotificationVibrationPattern, |
| 204 kNotificationVibrationPattern + arraysize(kNotificationVibrationPattern)); | 222 kNotificationVibrationPattern + arraysize(kNotificationVibrationPattern)); |
| 205 | 223 |
| 206 PlatformNotificationData notification_data; | 224 PlatformNotificationData notification_data; |
| 207 notification_data.title = base::ASCIIToUTF16("My notification's title"); | 225 notification_data.title = base::ASCIIToUTF16("My notification's title"); |
| 208 notification_data.body = base::ASCIIToUTF16("Hello, world!"); | 226 notification_data.body = base::ASCIIToUTF16("Hello, world!"); |
| 209 notification_data.vibration_pattern = vibration_pattern; | 227 notification_data.vibration_pattern = vibration_pattern; |
| 210 notification_data.silent = true; | 228 notification_data.silent = true; |
| 211 | 229 |
| 212 MockDesktopNotificationDelegate* delegate | 230 MockDesktopNotificationDelegate* delegate |
| 213 = new MockDesktopNotificationDelegate(); | 231 = new MockDesktopNotificationDelegate(); |
| 214 service()->DisplayNotification(profile(), GURL("https://chrome.com/"), | 232 service()->DisplayNotification(profile(), GURL("https://chrome.com/"), |
| 215 notification_data, NotificationResources(), | 233 notification_data, NotificationResources(), |
| 216 base::WrapUnique(delegate), nullptr); | 234 base::WrapUnique(delegate), nullptr); |
| 217 | 235 |
| 218 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); | 236 ASSERT_EQ(1u, GetNotificationCount()); |
| 219 | 237 |
| 220 const Notification& notification = ui_manager()->GetNotificationAt(0); | 238 const Notification& notification = GetDisplayedNotification(); |
| 221 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); | 239 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); |
| 222 EXPECT_EQ("My notification's title", | 240 EXPECT_EQ("My notification's title", |
| 223 base::UTF16ToUTF8(notification.title())); | 241 base::UTF16ToUTF8(notification.title())); |
| 224 EXPECT_EQ("Hello, world!", | 242 EXPECT_EQ("Hello, world!", |
| 225 base::UTF16ToUTF8(notification.message())); | 243 base::UTF16ToUTF8(notification.message())); |
| 226 | 244 |
| 227 EXPECT_THAT(notification.vibration_pattern(), | 245 EXPECT_THAT(notification.vibration_pattern(), |
| 228 testing::ElementsAreArray(kNotificationVibrationPattern)); | 246 testing::ElementsAreArray(kNotificationVibrationPattern)); |
| 229 | 247 |
| 230 EXPECT_TRUE(notification.silent()); | 248 EXPECT_TRUE(notification.silent()); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 244 notification_data.actions[0].title = base::ASCIIToUTF16("Button 1"); | 262 notification_data.actions[0].title = base::ASCIIToUTF16("Button 1"); |
| 245 notification_data.actions[1].title = base::ASCIIToUTF16("Button 2"); | 263 notification_data.actions[1].title = base::ASCIIToUTF16("Button 2"); |
| 246 | 264 |
| 247 NotificationResources notification_resources; | 265 NotificationResources notification_resources; |
| 248 notification_resources.action_icons.resize(notification_data.actions.size()); | 266 notification_resources.action_icons.resize(notification_data.actions.size()); |
| 249 | 267 |
| 250 service()->DisplayPersistentNotification( | 268 service()->DisplayPersistentNotification( |
| 251 profile(), 0u /* persistent notification */, GURL("https://chrome.com/"), | 269 profile(), 0u /* persistent notification */, GURL("https://chrome.com/"), |
| 252 notification_data, notification_resources); | 270 notification_data, notification_resources); |
| 253 | 271 |
| 254 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); | 272 ASSERT_EQ(1u, GetNotificationCount()); |
| 255 | 273 |
| 256 const Notification& notification = ui_manager()->GetNotificationAt(0); | 274 const Notification& notification = GetDisplayedNotification(); |
| 257 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); | 275 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); |
| 258 EXPECT_EQ("My notification's title", base::UTF16ToUTF8(notification.title())); | 276 EXPECT_EQ("My notification's title", base::UTF16ToUTF8(notification.title())); |
| 259 EXPECT_EQ("Hello, world!", base::UTF16ToUTF8(notification.message())); | 277 EXPECT_EQ("Hello, world!", base::UTF16ToUTF8(notification.message())); |
| 260 | 278 |
| 261 EXPECT_THAT(notification.vibration_pattern(), | 279 EXPECT_THAT(notification.vibration_pattern(), |
| 262 testing::ElementsAreArray(kNotificationVibrationPattern)); | 280 testing::ElementsAreArray(kNotificationVibrationPattern)); |
| 263 | 281 |
| 264 EXPECT_TRUE(notification.silent()); | 282 EXPECT_TRUE(notification.silent()); |
| 265 | 283 |
| 266 const auto& buttons = notification.buttons(); | 284 const auto& buttons = notification.buttons(); |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 412 notification = service()->CreateNotificationFromData( | 430 notification = service()->CreateNotificationFromData( |
| 413 profile(), | 431 profile(), |
| 414 GURL("chrome-extension://honijodknafkokifofgiaalefdiedpko/main.html"), | 432 GURL("chrome-extension://honijodknafkokifofgiaalefdiedpko/main.html"), |
| 415 notification_data, NotificationResources(), | 433 notification_data, NotificationResources(), |
| 416 new MockNotificationDelegate("hello")); | 434 new MockNotificationDelegate("hello")); |
| 417 EXPECT_EQ("NotificationTest", | 435 EXPECT_EQ("NotificationTest", |
| 418 base::UTF16ToUTF8(notification.context_message())); | 436 base::UTF16ToUTF8(notification.context_message())); |
| 419 } | 437 } |
| 420 | 438 |
| 421 #endif // defined(ENABLE_EXTENSIONS) | 439 #endif // defined(ENABLE_EXTENSIONS) |
| OLD | NEW |