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/time/time.h" |
6 #include "chrome/browser/notifications/notification_test_util.h" | 7 #include "chrome/browser/notifications/notification_test_util.h" |
7 #include "chrome/browser/notifications/platform_notification_service_impl.h" | 8 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
8 #include "chrome/test/base/testing_profile.h" | 9 #include "chrome/test/base/testing_profile.h" |
| 10 #include "components/content_settings/core/browser/host_content_settings_map.h" |
9 #include "content/public/browser/desktop_notification_delegate.h" | 11 #include "content/public/browser/desktop_notification_delegate.h" |
10 #include "content/public/test/test_browser_thread_bundle.h" | 12 #include "content/public/test/test_browser_thread_bundle.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
12 | 14 |
13 namespace { | 15 namespace { |
14 | 16 |
15 class MockDesktopNotificationDelegate | 17 class MockDesktopNotificationDelegate |
16 : public content::DesktopNotificationDelegate { | 18 : public content::DesktopNotificationDelegate { |
17 public: | 19 public: |
18 MockDesktopNotificationDelegate() | 20 MockDesktopNotificationDelegate() |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 MockDesktopNotificationDelegate* CreateSimplePageNotification() const { | 60 MockDesktopNotificationDelegate* CreateSimplePageNotification() const { |
59 return CreateSimplePageNotificationWithCloseClosure(nullptr); | 61 return CreateSimplePageNotificationWithCloseClosure(nullptr); |
60 } | 62 } |
61 | 63 |
62 // Displays a simple, fake notification and returns a weak pointer to the | 64 // Displays a simple, fake notification and returns a weak pointer to the |
63 // delegate receiving events for it (ownership is transferred to the service). | 65 // delegate receiving events for it (ownership is transferred to the service). |
64 // The close closure may be specified if so desired. | 66 // The close closure may be specified if so desired. |
65 MockDesktopNotificationDelegate* CreateSimplePageNotificationWithCloseClosure( | 67 MockDesktopNotificationDelegate* CreateSimplePageNotificationWithCloseClosure( |
66 base::Closure* close_closure) const { | 68 base::Closure* close_closure) const { |
67 content::ShowDesktopNotificationHostMsgParams params; | 69 content::ShowDesktopNotificationHostMsgParams params; |
68 params.origin = GURL("https://example.com/"); | 70 params.origin = GURL("https://chrome.com/"); |
69 params.title = base::ASCIIToUTF16("My Notification"); | 71 params.title = base::ASCIIToUTF16("My Notification"); |
70 params.body = base::ASCIIToUTF16("Hello, world!"); | 72 params.body = base::ASCIIToUTF16("Hello, world!"); |
71 | 73 |
72 MockDesktopNotificationDelegate* delegate = | 74 MockDesktopNotificationDelegate* delegate = |
73 new MockDesktopNotificationDelegate(); | 75 new MockDesktopNotificationDelegate(); |
74 | 76 |
75 service()->DisplayNotification(profile(), | 77 service()->DisplayNotification(profile(), |
76 params, | 78 params, |
77 make_scoped_ptr(delegate), | 79 make_scoped_ptr(delegate), |
78 0 /* render_process_id */, | 80 0 /* render_process_id */, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 | 116 |
115 ASSERT_FALSE(close_closure.is_null()); | 117 ASSERT_FALSE(close_closure.is_null()); |
116 close_closure.Run(); | 118 close_closure.Run(); |
117 | 119 |
118 EXPECT_EQ(0u, ui_manager()->GetNotificationCount()); | 120 EXPECT_EQ(0u, ui_manager()->GetNotificationCount()); |
119 | 121 |
120 // Note that we cannot verify whether the closed event was called on the | 122 // 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. | 123 // delegate given that it'd result in a use-after-free. |
122 } | 124 } |
123 | 125 |
| 126 TEST_F(PlatformNotificationServiceTest, PersistentNotificationDisplay) { |
| 127 content::ShowDesktopNotificationHostMsgParams params; |
| 128 params.origin = GURL("https://chrome.com/"); |
| 129 params.title = base::ASCIIToUTF16("My notification's title"); |
| 130 params.body = base::ASCIIToUTF16("Hello, world!"); |
| 131 |
| 132 service()->DisplayPersistentNotification(profile(), |
| 133 42 /* sw_registration_id */, |
| 134 params, |
| 135 0 /* render_process_id */); |
| 136 |
| 137 ASSERT_EQ(1u, ui_manager()->GetNotificationCount()); |
| 138 |
| 139 const Notification& notification = ui_manager()->GetNotificationAt(0); |
| 140 EXPECT_EQ("https://chrome.com/", notification.origin_url().spec()); |
| 141 EXPECT_EQ("My notification's title", |
| 142 base::UTF16ToUTF8(notification.title())); |
| 143 EXPECT_EQ("Hello, world!", |
| 144 base::UTF16ToUTF8(notification.message())); |
| 145 |
| 146 service()->ClosePersistentNotification(profile(), |
| 147 notification.delegate_id()); |
| 148 EXPECT_EQ(0u, ui_manager()->GetNotificationCount()); |
| 149 } |
| 150 |
124 TEST_F(PlatformNotificationServiceTest, DisplayPageNotificationMatches) { | 151 TEST_F(PlatformNotificationServiceTest, DisplayPageNotificationMatches) { |
125 content::ShowDesktopNotificationHostMsgParams params; | 152 content::ShowDesktopNotificationHostMsgParams params; |
126 params.origin = GURL("https://chrome.com/"); | 153 params.origin = GURL("https://chrome.com/"); |
127 params.title = base::ASCIIToUTF16("My notification's title"); | 154 params.title = base::ASCIIToUTF16("My notification's title"); |
128 params.body = base::ASCIIToUTF16("Hello, world!"); | 155 params.body = base::ASCIIToUTF16("Hello, world!"); |
129 | 156 |
130 MockDesktopNotificationDelegate* delegate | 157 MockDesktopNotificationDelegate* delegate |
131 = new MockDesktopNotificationDelegate(); | 158 = new MockDesktopNotificationDelegate(); |
132 service()->DisplayNotification(profile(), | 159 service()->DisplayNotification(profile(), |
133 params, | 160 params, |
(...skipping 15 matching lines...) Expand all Loading... |
149 base::string16 display_name = | 176 base::string16 display_name = |
150 service()->DisplayNameForOriginInProcessId(profile(), | 177 service()->DisplayNameForOriginInProcessId(profile(), |
151 GURL("https://chrome.com/"), | 178 GURL("https://chrome.com/"), |
152 0 /* render_process_id */); | 179 0 /* render_process_id */); |
153 | 180 |
154 EXPECT_EQ(base::ASCIIToUTF16("chrome.com"), display_name); | 181 EXPECT_EQ(base::ASCIIToUTF16("chrome.com"), display_name); |
155 | 182 |
156 // TODO(peter): Include unit tests for the extension-name translation | 183 // TODO(peter): Include unit tests for the extension-name translation |
157 // functionality of DisplayNameForOriginInProcessId. | 184 // functionality of DisplayNameForOriginInProcessId. |
158 } | 185 } |
| 186 |
| 187 TEST_F(PlatformNotificationServiceTest, NotificationPermissionLastUsage) { |
| 188 // Both page and persistent notifications should update the last usage |
| 189 // time of the notification permission for the origin. |
| 190 GURL origin("https://chrome.com/"); |
| 191 base::Time begin_time; |
| 192 |
| 193 CreateSimplePageNotification(); |
| 194 |
| 195 base::Time after_page_notification = |
| 196 profile()->GetHostContentSettingsMap()->GetLastUsage( |
| 197 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 198 EXPECT_GT(after_page_notification, begin_time); |
| 199 |
| 200 content::ShowDesktopNotificationHostMsgParams params; |
| 201 params.origin = origin; |
| 202 |
| 203 service()->DisplayPersistentNotification(profile(), |
| 204 42 /* sw_registration_id */, |
| 205 params, |
| 206 0 /* render_process_id */); |
| 207 |
| 208 base::Time after_persistent_notification = |
| 209 profile()->GetHostContentSettingsMap()->GetLastUsage( |
| 210 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 211 EXPECT_GT(after_persistent_notification, after_page_notification); |
| 212 } |
OLD | NEW |