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

Side by Side Diff: chrome/browser/notifications/platform_notification_service_browsertest.cc

Issue 1919203002: Rename notification interactive_ui_tests to "*_interactive_uitest.cc" to avoid confusion. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix rename Created 4 years, 7 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 unified diff | Download patch
OLDNEW
(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 <memory>
6 #include <string>
7 #include <vector>
8
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/path_service.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/time/time.h"
14 #include "chrome/browser/notifications/desktop_notification_profile_util.h"
15 #include "chrome/browser/notifications/message_center_display_service.h"
16 #include "chrome/browser/notifications/notification.h"
17 #include "chrome/browser/notifications/notification_test_util.h"
18 #include "chrome/browser/notifications/platform_notification_service_impl.h"
19 #include "chrome/browser/permissions/permission_manager.h"
20 #include "chrome/browser/ui/browser.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h"
22 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
23 #include "chrome/grit/generated_resources.h"
24 #include "chrome/test/base/in_process_browser_test.h"
25 #include "chrome/test/base/ui_test_utils.h"
26 #include "content/public/browser/permission_type.h"
27 #include "content/public/common/content_switches.h"
28 #include "content/public/test/browser_test_utils.h"
29 #include "net/base/filename_util.h"
30 #include "net/test/embedded_test_server/embedded_test_server.h"
31 #include "testing/gmock/include/gmock/gmock.h"
32 #include "third_party/WebKit/public/platform/modules/permissions/permission_stat us.mojom.h"
33 #include "ui/base/l10n/l10n_util.h"
34
35 // -----------------------------------------------------------------------------
36
37 // Dimensions of the icon.png resource in the notification test data directory.
38 const int kIconWidth = 100;
39 const int kIconHeight = 100;
40
41 // The maximum width and height of badges. Oversized images are scaled down to
42 // these values.
43 const int kMaxBadgeSize = 96;
44
45 const int kNotificationVibrationPattern[] = { 100, 200, 300 };
46 const double kNotificationTimestamp = 621046800000.;
47
48 class PlatformNotificationServiceBrowserTest : public InProcessBrowserTest {
49 public:
50 PlatformNotificationServiceBrowserTest();
51 ~PlatformNotificationServiceBrowserTest() override {}
52
53 // InProcessBrowserTest overrides.
54 void SetUpCommandLine(base::CommandLine* command_line) override;
55 void SetUp() override;
56 void SetUpOnMainThread() override;
57 void TearDown() override;
58
59 protected:
60 // Returns the Platform Notification Service these unit tests are for.
61 PlatformNotificationServiceImpl* service() const {
62 return PlatformNotificationServiceImpl::GetInstance();
63 }
64
65 // Grants permission to display Web Notifications for origin of the test
66 // page that's being used in this browser test.
67 void GrantNotificationPermissionForTest() const;
68
69 bool RequestAndAcceptPermission();
70 bool RequestAndDenyPermission();
71
72 // Returns the UI Manager on which notifications will be displayed.
73 StubNotificationUIManager* ui_manager() const { return ui_manager_.get(); }
74
75 const base::FilePath& server_root() const { return server_root_; }
76
77 // Navigates the browser to the test page indicated by |path|.
78 void NavigateToTestPage(const std::string& path) const;
79
80 // Executes |script| and stores the result as a string in |result|. A boolean
81 // will be returned, indicating whether the script was executed successfully.
82 bool RunScript(const std::string& script, std::string* result) const;
83
84 net::HostPortPair ServerHostPort() const;
85 GURL TestPageUrl() const;
86
87 private:
88 std::string RequestAndRespondToPermission(
89 PermissionBubbleManager::AutoResponseType bubble_response);
90
91 content::WebContents* GetActiveWebContents(Browser* browser) {
92 return browser->tab_strip_model()->GetActiveWebContents();
93 }
94
95 const base::FilePath server_root_;
96 const std::string test_page_url_;
97 std::unique_ptr<StubNotificationUIManager> ui_manager_;
98 std::unique_ptr<MessageCenterDisplayService> display_service_;
99 std::unique_ptr<net::EmbeddedTestServer> https_server_;
100 };
101
102 // -----------------------------------------------------------------------------
103
104 namespace {
105 const char kTestFileName[] = "notifications/platform_notification_service.html";
106 }
107
108 PlatformNotificationServiceBrowserTest::PlatformNotificationServiceBrowserTest()
109 : server_root_(FILE_PATH_LITERAL("chrome/test/data")),
110 // The test server has a base directory that doesn't exist in the
111 // filesystem.
112 test_page_url_(std::string("/") + kTestFileName) {}
113
114 void PlatformNotificationServiceBrowserTest::SetUpCommandLine(
115 base::CommandLine* command_line) {
116 command_line->AppendSwitch(switches::kEnableExperimentalWebPlatformFeatures);
117
118 InProcessBrowserTest::SetUpCommandLine(command_line);
119 }
120
121 void PlatformNotificationServiceBrowserTest::SetUp() {
122 ui_manager_.reset(new StubNotificationUIManager);
123 https_server_.reset(
124 new net::EmbeddedTestServer(net::EmbeddedTestServer::TYPE_HTTPS));
125 https_server_->ServeFilesFromSourceDirectory(server_root_);
126 ASSERT_TRUE(https_server_->Start());
127 InProcessBrowserTest::SetUp();
128 }
129
130 void PlatformNotificationServiceBrowserTest::SetUpOnMainThread() {
131 NavigateToTestPage(test_page_url_);
132 display_service_.reset(
133 new MessageCenterDisplayService(browser()->profile(), ui_manager_.get()));
134 service()->SetNotificationDisplayServiceForTesting(display_service_.get());
135 InProcessBrowserTest::SetUpOnMainThread();
136 }
137
138 void PlatformNotificationServiceBrowserTest::TearDown() {
139 service()->SetNotificationDisplayServiceForTesting(nullptr);
140 }
141
142 void PlatformNotificationServiceBrowserTest::
143 GrantNotificationPermissionForTest() const {
144 GURL origin = TestPageUrl().GetOrigin();
145
146 DesktopNotificationProfileUtil::GrantPermission(browser()->profile(), origin);
147 ASSERT_EQ(CONTENT_SETTING_ALLOW,
148 DesktopNotificationProfileUtil::GetContentSetting(
149 browser()->profile(), origin));
150 }
151
152 void PlatformNotificationServiceBrowserTest::NavigateToTestPage(
153 const std::string& path) const {
154 ui_test_utils::NavigateToURL(browser(), https_server_->GetURL(path));
155 }
156
157 bool PlatformNotificationServiceBrowserTest::RunScript(
158 const std::string& script, std::string* result) const {
159 return content::ExecuteScriptAndExtractString(
160 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(),
161 script,
162 result);
163 }
164
165 net::HostPortPair PlatformNotificationServiceBrowserTest::ServerHostPort()
166 const {
167 return https_server_->host_port_pair();
168 }
169
170 GURL PlatformNotificationServiceBrowserTest::TestPageUrl() const {
171 return https_server_->GetURL(test_page_url_);
172 }
173
174 std::string
175 PlatformNotificationServiceBrowserTest::RequestAndRespondToPermission(
176 PermissionBubbleManager::AutoResponseType bubble_response) {
177 std::string result;
178 content::WebContents* web_contents = GetActiveWebContents(browser());
179 PermissionBubbleManager::FromWebContents(web_contents)
180 ->set_auto_response_for_test(bubble_response);
181 EXPECT_TRUE(RunScript("RequestPermission();", &result));
182 return result;
183 }
184
185 bool PlatformNotificationServiceBrowserTest::RequestAndAcceptPermission() {
186 std::string result =
187 RequestAndRespondToPermission(PermissionBubbleManager::ACCEPT_ALL);
188 return "granted" == result;
189 }
190
191 bool PlatformNotificationServiceBrowserTest::RequestAndDenyPermission() {
192 std::string result =
193 RequestAndRespondToPermission(PermissionBubbleManager::DENY_ALL);
194 return "denied" == result;
195 }
196
197 // -----------------------------------------------------------------------------
198
199 // TODO(peter): Move PlatformNotificationService-related tests over from
200 // notification_browsertest.cc to this file.
201
202 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
203 DisplayPersistentNotificationWithoutPermission) {
204 RequestAndDenyPermission();
205
206 std::string script_result;
207 ASSERT_TRUE(RunScript("DisplayPersistentNotification()", &script_result));
208 EXPECT_EQ(
209 "TypeError: No notification permission has been granted for this origin.",
210 script_result);
211
212 ASSERT_EQ(0u, ui_manager()->GetNotificationCount());
213 }
214
215 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
216 DisplayPersistentNotificationWithPermission) {
217 RequestAndAcceptPermission();
218
219 std::string script_result;
220 ASSERT_TRUE(RunScript("DisplayPersistentNotification('action_none')",
221 &script_result));
222 EXPECT_EQ("ok", script_result);
223
224 ASSERT_EQ(1u, ui_manager()->GetNotificationCount());
225
226 const Notification& notification = ui_manager()->GetNotificationAt(0);
227 notification.delegate()->Click();
228
229 ASSERT_TRUE(RunScript("GetMessageFromWorker()", &script_result));
230 EXPECT_EQ("action_none", script_result);
231
232 ASSERT_EQ(1u, ui_manager()->GetNotificationCount());
233 }
234
235 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
236 WebNotificationOptionsReflection) {
237 ASSERT_NO_FATAL_FAILURE(GrantNotificationPermissionForTest());
238
239 // First, test the default values.
240
241 std::string script_result;
242 ASSERT_TRUE(RunScript("DisplayPersistentNotification('Some title', {})",
243 &script_result));
244 EXPECT_EQ("ok", script_result);
245
246 ASSERT_EQ(1u, ui_manager()->GetNotificationCount());
247
248 // We don't use or check the notification's direction and language.
249 const Notification& default_notification = ui_manager()->GetNotificationAt(0);
250 EXPECT_EQ("Some title", base::UTF16ToUTF8(default_notification.title()));
251 EXPECT_EQ("", base::UTF16ToUTF8(default_notification.message()));
252 EXPECT_EQ("", default_notification.tag());
253 EXPECT_TRUE(default_notification.icon().IsEmpty());
254 EXPECT_TRUE(default_notification.small_image().IsEmpty());
255 EXPECT_FALSE(default_notification.renotify());
256 EXPECT_FALSE(default_notification.silent());
257 EXPECT_FALSE(default_notification.never_timeout());
258 EXPECT_EQ(0u, default_notification.buttons().size());
259
260 // Verifies that the notification's default timestamp is set in the last 30
261 // seconds. This number has no significance, but it needs to be significantly
262 // high to avoid flakiness in the test.
263 EXPECT_NEAR(default_notification.timestamp().ToJsTime(),
264 base::Time::Now().ToJsTime(), 30 * 1000);
265
266 // Now, test the non-default values.
267
268 ASSERT_TRUE(RunScript("DisplayPersistentAllOptionsNotification()",
269 &script_result));
270 EXPECT_EQ("ok", script_result);
271
272 ASSERT_EQ(2u, ui_manager()->GetNotificationCount());
273
274 // We don't use or check the notification's direction and language.
275 const Notification& all_options_notification =
276 ui_manager()->GetNotificationAt(1);
277 EXPECT_EQ("Title", base::UTF16ToUTF8(all_options_notification.title()));
278 EXPECT_EQ("Contents", base::UTF16ToUTF8(all_options_notification.message()));
279 EXPECT_EQ("replace-id", all_options_notification.tag());
280 EXPECT_FALSE(all_options_notification.icon().IsEmpty());
281 EXPECT_EQ(kIconWidth, all_options_notification.icon().Width());
282 EXPECT_EQ(kIconHeight, all_options_notification.icon().Height());
283 EXPECT_FALSE(all_options_notification.small_image().IsEmpty());
284 EXPECT_EQ(kMaxBadgeSize, all_options_notification.small_image().Width());
285 EXPECT_EQ(kMaxBadgeSize, all_options_notification.small_image().Height());
286 EXPECT_TRUE(all_options_notification.renotify());
287 EXPECT_TRUE(all_options_notification.silent());
288 EXPECT_TRUE(all_options_notification.never_timeout());
289 EXPECT_DOUBLE_EQ(kNotificationTimestamp,
290 all_options_notification.timestamp().ToJsTime());
291 EXPECT_EQ(1u, all_options_notification.buttons().size());
292 EXPECT_EQ("actionTitle",
293 base::UTF16ToUTF8(all_options_notification.buttons()[0].title));
294 EXPECT_FALSE(all_options_notification.buttons()[0].icon.IsEmpty());
295 EXPECT_EQ(kIconWidth, all_options_notification.buttons()[0].icon.Width());
296 EXPECT_EQ(kIconHeight, all_options_notification.buttons()[0].icon.Height());
297 }
298
299 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
300 WebNotificationSiteSettingsButton) {
301 ASSERT_NO_FATAL_FAILURE(GrantNotificationPermissionForTest());
302
303 std::string script_result;
304 ASSERT_TRUE(RunScript("DisplayPersistentNotification('Some title', {})",
305 &script_result));
306 EXPECT_EQ("ok", script_result);
307
308 ASSERT_EQ(1u, ui_manager()->GetNotificationCount());
309
310 const Notification& notification = ui_manager()->GetNotificationAt(0);
311 const std::vector<message_center::ButtonInfo>& buttons =
312 notification.buttons();
313 EXPECT_EQ(0u, buttons.size());
314
315 notification.delegate()->SettingsClick();
316 ASSERT_EQ(1u, ui_manager()->GetNotificationCount());
317 content::WebContents* web_contents =
318 browser()->tab_strip_model()->GetActiveWebContents();
319 ASSERT_TRUE(content::WaitForLoadStop(web_contents));
320 ASSERT_EQ("chrome://settings/contentExceptions#notifications",
321 web_contents->GetLastCommittedURL().spec());
322 }
323
324 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
325 WebNotificationOptionsVibrationPattern) {
326 ASSERT_NO_FATAL_FAILURE(GrantNotificationPermissionForTest());
327
328 std::string script_result;
329 ASSERT_TRUE(RunScript("DisplayPersistentNotificationVibrate()",
330 &script_result));
331 EXPECT_EQ("ok", script_result);
332
333 ASSERT_EQ(1u, ui_manager()->GetNotificationCount());
334
335 const Notification& notification = ui_manager()->GetNotificationAt(0);
336 EXPECT_EQ("Title", base::UTF16ToUTF8(notification.title()));
337 EXPECT_EQ("Contents", base::UTF16ToUTF8(notification.message()));
338
339 EXPECT_THAT(notification.vibration_pattern(),
340 testing::ElementsAreArray(kNotificationVibrationPattern));
341 }
342
343 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
344 CloseDisplayedPersistentNotification) {
345 ASSERT_NO_FATAL_FAILURE(GrantNotificationPermissionForTest());
346
347 std::string script_result;
348 ASSERT_TRUE(RunScript("DisplayPersistentNotification('action_close')",
349 &script_result));
350 EXPECT_EQ("ok", script_result);
351
352 ASSERT_EQ(1u, ui_manager()->GetNotificationCount());
353
354 const Notification& notification = ui_manager()->GetNotificationAt(0);
355 notification.delegate()->Click();
356
357 ASSERT_TRUE(RunScript("GetMessageFromWorker()", &script_result));
358 EXPECT_EQ("action_close", script_result);
359
360 ASSERT_EQ(0u, ui_manager()->GetNotificationCount());
361 }
362
363 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
364 UserClosesPersistentNotification) {
365 ASSERT_NO_FATAL_FAILURE(GrantNotificationPermissionForTest());
366
367 std::string script_result;
368 ASSERT_TRUE(
369 RunScript("DisplayPersistentNotification('close_test')", &script_result));
370 EXPECT_EQ("ok", script_result);
371
372 ASSERT_EQ(1u, ui_manager()->GetNotificationCount());
373 const Notification& notification = ui_manager()->GetNotificationAt(0);
374 notification.delegate()->Close(true /* by_user */);
375
376 ASSERT_TRUE(RunScript("GetMessageFromWorker()", &script_result));
377 EXPECT_EQ("closing notification: close_test", script_result);
378 }
379
380 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
381 TestDisplayOriginContextMessage) {
382 RequestAndAcceptPermission();
383
384 // Creates a simple notification.
385 std::string script_result;
386 ASSERT_TRUE(RunScript("DisplayPersistentNotification()", &script_result));
387
388 net::HostPortPair host_port = ServerHostPort();
389
390 const Notification& notification = ui_manager()->GetNotificationAt(0);
391
392 EXPECT_TRUE(notification.context_message().empty());
393 EXPECT_EQ("https://" + host_port.ToString() + "/",
394 notification.origin_url().spec());
395 }
396
397 // TODO(felt): This DCHECKs when bubbles are enabled, when the file_url is
398 // persisted. crbug.com/502057
399 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
400 DISABLED_CheckFilePermissionNotGranted) {
401 // This case should succeed because a normal page URL is used.
402 std::string script_result;
403
404 PermissionManager* permission_manager =
405 PermissionManager::Get(browser()->profile());
406
407 EXPECT_EQ(blink::mojom::PermissionStatus::ASK,
408 permission_manager->GetPermissionStatus(
409 content::PermissionType::NOTIFICATIONS, TestPageUrl(),
410 TestPageUrl()));
411
412 RequestAndAcceptPermission();
413 EXPECT_EQ(blink::mojom::PermissionStatus::GRANTED,
414 permission_manager->GetPermissionStatus(
415 content::PermissionType::NOTIFICATIONS, TestPageUrl(),
416 TestPageUrl()));
417
418 // This case should fail because a file URL is used.
419 base::FilePath dir_source_root;
420 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &dir_source_root));
421 base::FilePath full_file_path =
422 dir_source_root.Append(server_root()).AppendASCII(kTestFileName);
423 GURL file_url(net::FilePathToFileURL(full_file_path));
424
425 ui_test_utils::NavigateToURL(browser(), file_url);
426
427 EXPECT_EQ(blink::mojom::PermissionStatus::ASK,
428 permission_manager->GetPermissionStatus(
429 content::PermissionType::NOTIFICATIONS, file_url, file_url));
430
431 RequestAndAcceptPermission();
432 EXPECT_EQ(blink::mojom::PermissionStatus::ASK,
433 permission_manager->GetPermissionStatus(
434 content::PermissionType::NOTIFICATIONS, file_url, file_url))
435 << "If this test fails, you may have fixed a bug preventing file origins "
436 << "from sending their origin from Blink; if so you need to update the "
437 << "display function for notification origins to show the file path.";
438 }
439
440 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
441 DataUrlAsNotificationImage) {
442 ASSERT_NO_FATAL_FAILURE(GrantNotificationPermissionForTest());
443
444 std::string script_result;
445 ASSERT_TRUE(RunScript("DisplayPersistentNotificationDataUrlImage()",
446 &script_result));
447 EXPECT_EQ("ok", script_result);
448
449 ASSERT_EQ(1u, ui_manager()->GetNotificationCount());
450
451 const Notification& notification = ui_manager()->GetNotificationAt(0);
452 EXPECT_FALSE(notification.icon().IsEmpty());
453
454 EXPECT_EQ("Data URL Title", base::UTF16ToUTF8(notification.title()));
455 EXPECT_EQ(kIconWidth, notification.icon().Width());
456 EXPECT_EQ(kIconHeight, notification.icon().Height());
457 }
458
459 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
460 BlobAsNotificationImage) {
461 ASSERT_NO_FATAL_FAILURE(GrantNotificationPermissionForTest());
462
463 std::string script_result;
464 ASSERT_TRUE(RunScript("DisplayPersistentNotificationBlobImage()",
465 &script_result));
466 EXPECT_EQ("ok", script_result);
467
468 ASSERT_EQ(1u, ui_manager()->GetNotificationCount());
469
470 const Notification& notification = ui_manager()->GetNotificationAt(0);
471 EXPECT_FALSE(notification.icon().IsEmpty());
472
473 EXPECT_EQ("Blob Title", base::UTF16ToUTF8(notification.title()));
474 EXPECT_EQ(kIconWidth, notification.icon().Width());
475 EXPECT_EQ(kIconHeight, notification.icon().Height());
476 }
477
478 IN_PROC_BROWSER_TEST_F(PlatformNotificationServiceBrowserTest,
479 DisplayPersistentNotificationWithActionButtons) {
480 ASSERT_NO_FATAL_FAILURE(GrantNotificationPermissionForTest());
481
482 std::string script_result;
483 ASSERT_TRUE(RunScript("DisplayPersistentNotificationWithActionButtons()",
484 &script_result));
485 EXPECT_EQ("ok", script_result);
486 ASSERT_EQ(1u, ui_manager()->GetNotificationCount());
487
488 const Notification& notification = ui_manager()->GetNotificationAt(0);
489 ASSERT_EQ(2u, notification.buttons().size());
490 EXPECT_EQ("actionTitle1", base::UTF16ToUTF8(notification.buttons()[0].title));
491 EXPECT_EQ("actionTitle2", base::UTF16ToUTF8(notification.buttons()[1].title));
492
493 notification.delegate()->ButtonClick(0);
494 ASSERT_TRUE(RunScript("GetMessageFromWorker()", &script_result));
495 EXPECT_EQ("action_button_click actionId1", script_result);
496
497 notification.delegate()->ButtonClick(1);
498 ASSERT_TRUE(RunScript("GetMessageFromWorker()", &script_result));
499 EXPECT_EQ("action_button_click actionId2", script_result);
500 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698