| 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 "base/strings/utf_string_conversions.h" | 5 #include "base/strings/utf_string_conversions.h" |
| 6 #include "base/threading/platform_thread.h" | 6 #include "base/threading/platform_thread.h" |
| 7 #include "base/time/time.h" | 7 #include "base/time/time.h" |
| 8 #include "chrome/browser/notifications/notification_test_util.h" | 8 #include "chrome/browser/notifications/notification_test_util.h" |
| 9 #include "chrome/browser/notifications/platform_notification_service_impl.h" | 9 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
| 10 #include "chrome/test/base/testing_profile.h" | 10 #include "chrome/test/base/testing_profile.h" |
| 11 #include "components/content_settings/core/browser/host_content_settings_map.h" | 11 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 12 #include "content/public/browser/desktop_notification_delegate.h" | 12 #include "content/public/browser/desktop_notification_delegate.h" |
| 13 #include "content/public/common/platform_notification_data.h" | 13 #include "content/public/common/platform_notification_data.h" |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | 14 #include "content/public/test/test_browser_thread_bundle.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "third_party/skia/include/core/SkBitmap.h" | 16 #include "third_party/skia/include/core/SkBitmap.h" |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 const int kNotificationVibrationPattern[] = { 100, 200, 300 }; |
| 21 |
| 20 #if !defined(OS_ANDROID) | 22 #if !defined(OS_ANDROID) |
| 21 const int64_t kPersistentNotificationId = 42; | 23 const int64_t kPersistentNotificationId = 42; |
| 22 #endif | 24 #endif |
| 23 | 25 |
| 24 class MockDesktopNotificationDelegate | 26 class MockDesktopNotificationDelegate |
| 25 : public content::DesktopNotificationDelegate { | 27 : public content::DesktopNotificationDelegate { |
| 26 public: | 28 public: |
| 27 MockDesktopNotificationDelegate() | 29 MockDesktopNotificationDelegate() |
| 28 : displayed_(false), | 30 : displayed_(false), |
| 29 clicked_(false) {} | 31 clicked_(false) {} |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 base::UTF16ToUTF8(notification.title())); | 152 base::UTF16ToUTF8(notification.title())); |
| 151 EXPECT_EQ("Hello, world!", | 153 EXPECT_EQ("Hello, world!", |
| 152 base::UTF16ToUTF8(notification.message())); | 154 base::UTF16ToUTF8(notification.message())); |
| 153 | 155 |
| 154 service()->ClosePersistentNotification(profile(), kPersistentNotificationId); | 156 service()->ClosePersistentNotification(profile(), kPersistentNotificationId); |
| 155 EXPECT_EQ(0u, ui_manager()->GetNotificationCount()); | 157 EXPECT_EQ(0u, ui_manager()->GetNotificationCount()); |
| 156 } | 158 } |
| 157 #endif // !defined(OS_ANDROID) | 159 #endif // !defined(OS_ANDROID) |
| 158 | 160 |
| 159 TEST_F(PlatformNotificationServiceTest, DisplayPageNotificationMatches) { | 161 TEST_F(PlatformNotificationServiceTest, DisplayPageNotificationMatches) { |
| 162 std::vector<int> vibration_pattern( |
| 163 kNotificationVibrationPattern, |
| 164 kNotificationVibrationPattern + arraysize(kNotificationVibrationPattern)); |
| 165 |
| 160 content::PlatformNotificationData notification_data; | 166 content::PlatformNotificationData notification_data; |
| 161 notification_data.title = base::ASCIIToUTF16("My notification's title"); | 167 notification_data.title = base::ASCIIToUTF16("My notification's title"); |
| 162 notification_data.body = base::ASCIIToUTF16("Hello, world!"); | 168 notification_data.body = base::ASCIIToUTF16("Hello, world!"); |
| 169 notification_data.vibration_pattern = vibration_pattern; |
| 163 notification_data.silent = true; | 170 notification_data.silent = true; |
| 164 | 171 |
| 165 MockDesktopNotificationDelegate* delegate | 172 MockDesktopNotificationDelegate* delegate |
| 166 = new MockDesktopNotificationDelegate(); | 173 = new MockDesktopNotificationDelegate(); |
| 167 service()->DisplayNotification(profile(), | 174 service()->DisplayNotification(profile(), |
| 168 GURL("https://chrome.com/"), | 175 GURL("https://chrome.com/"), |
| 169 SkBitmap(), | 176 SkBitmap(), |
| 170 notification_data, | 177 notification_data, |
| 171 make_scoped_ptr(delegate), | 178 make_scoped_ptr(delegate), |
| 172 nullptr); | 179 nullptr); |
| 173 | 180 |
| 174 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); | 181 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); |
| 175 | 182 |
| 176 const Notification& notification = ui_manager()->GetNotificationAt(0); | 183 const Notification& notification = ui_manager()->GetNotificationAt(0); |
| 177 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); | 184 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); |
| 178 EXPECT_EQ("My notification's title", | 185 EXPECT_EQ("My notification's title", |
| 179 base::UTF16ToUTF8(notification.title())); | 186 base::UTF16ToUTF8(notification.title())); |
| 180 EXPECT_EQ("Hello, world!", | 187 EXPECT_EQ("Hello, world!", |
| 181 base::UTF16ToUTF8(notification.message())); | 188 base::UTF16ToUTF8(notification.message())); |
| 189 |
| 190 ASSERT_EQ(vibration_pattern.size(), notification.vibration_pattern().size()); |
| 191 for (size_t i = 0; i < vibration_pattern.size(); ++i) |
| 192 EXPECT_EQ(vibration_pattern[i], notification.vibration_pattern()[i]); |
| 193 |
| 182 EXPECT_TRUE(notification.silent()); | 194 EXPECT_TRUE(notification.silent()); |
| 183 } | 195 } |
| 184 | 196 |
| 185 TEST_F(PlatformNotificationServiceTest, DisplayNameForOrigin) { | 197 TEST_F(PlatformNotificationServiceTest, DisplayNameForOrigin) { |
| 186 base::string16 display_name = | 198 base::string16 display_name = |
| 187 service()->DisplayNameForOrigin(profile(), GURL("https://chrome.com/")); | 199 service()->DisplayNameForOrigin(profile(), GURL("https://chrome.com/")); |
| 188 | 200 |
| 189 EXPECT_EQ(base::ASCIIToUTF16("chrome.com"), display_name); | 201 EXPECT_EQ(base::ASCIIToUTF16("chrome.com"), display_name); |
| 190 | 202 |
| 191 // TODO(peter): Include unit tests for the extension-name translation | 203 // TODO(peter): Include unit tests for the extension-name translation |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 | 263 |
| 252 service()->DisplayPersistentNotification( | 264 service()->DisplayPersistentNotification( |
| 253 profile(), 42 /* sw_registration_id */, origin, SkBitmap(), | 265 profile(), 42 /* sw_registration_id */, origin, SkBitmap(), |
| 254 content::PlatformNotificationData()); | 266 content::PlatformNotificationData()); |
| 255 | 267 |
| 256 base::Time after_persistent_notification = | 268 base::Time after_persistent_notification = |
| 257 profile()->GetHostContentSettingsMap()->GetLastUsage( | 269 profile()->GetHostContentSettingsMap()->GetLastUsage( |
| 258 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 270 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 259 EXPECT_GT(after_persistent_notification, after_page_notification); | 271 EXPECT_GT(after_persistent_notification, after_page_notification); |
| 260 } | 272 } |
| OLD | NEW |