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 <map> | |
6 #include <string> | |
7 | |
8 #include "base/bind.h" | |
9 #include "base/command_line.h" | |
10 #include "base/message_loop/message_loop.h" | |
11 #include "base/strings/utf_string_conversions.h" | |
12 #include "chrome/browser/infobars/infobar_service.h" | |
13 #include "chrome/browser/notifications/notification_test_util.h" | |
14 #include "chrome/browser/notifications/platform_notification_service_impl.h" | |
15 #include "chrome/browser/profiles/profile.h" | |
16 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" | |
17 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | |
18 #include "chrome/browser/services/gcm/push_messaging_application_id.h" | |
19 #include "chrome/browser/services/gcm/push_messaging_constants.h" | |
20 #include "chrome/browser/ui/browser.h" | |
21 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
22 #include "chrome/test/base/in_process_browser_test.h" | |
23 #include "chrome/test/base/ui_test_utils.h" | |
24 #include "components/content_settings/core/browser/host_content_settings_map.h" | |
25 #include "components/content_settings/core/common/content_settings.h" | |
26 #include "components/content_settings/core/common/content_settings_types.h" | |
27 #include "components/gcm_driver/gcm_client.h" | |
28 #include "components/infobars/core/confirm_infobar_delegate.h" | |
29 #include "components/infobars/core/infobar.h" | |
30 #include "components/infobars/core/infobar_manager.h" | |
31 #include "content/public/browser/web_contents.h" | |
32 #include "content/public/common/content_switches.h" | |
33 #include "content/public/test/browser_test_utils.h" | |
34 #include "ui/base/window_open_disposition.h" | |
35 | |
36 #if defined(OS_ANDROID) | |
37 #include "base/android/build_info.h" | |
38 #endif | |
39 | |
40 namespace gcm { | |
41 | |
42 namespace { | |
43 // Responds to a confirm infobar by accepting or cancelling it. Responds to at | |
44 // most one infobar. | |
45 class InfoBarResponder : public infobars::InfoBarManager::Observer { | |
46 public: | |
47 InfoBarResponder(Browser* browser, bool accept) | |
48 : infobar_service_(InfoBarService::FromWebContents( | |
49 browser->tab_strip_model()->GetActiveWebContents())), | |
50 accept_(accept), | |
51 has_observed_(false) { | |
52 infobar_service_->AddObserver(this); | |
53 } | |
54 | |
55 ~InfoBarResponder() override { infobar_service_->RemoveObserver(this); } | |
56 | |
57 // infobars::InfoBarManager::Observer | |
58 void OnInfoBarAdded(infobars::InfoBar* infobar) override { | |
59 if (has_observed_) | |
60 return; | |
61 has_observed_ = true; | |
62 ConfirmInfoBarDelegate* delegate = | |
63 infobar->delegate()->AsConfirmInfoBarDelegate(); | |
64 DCHECK(delegate); | |
65 | |
66 // Respond to the infobar asynchronously, like a person. | |
67 base::MessageLoop::current()->PostTask( | |
68 FROM_HERE, | |
69 base::Bind( | |
70 &InfoBarResponder::Respond, base::Unretained(this), delegate)); | |
71 } | |
72 | |
73 private: | |
74 void Respond(ConfirmInfoBarDelegate* delegate) { | |
75 if (accept_) { | |
76 delegate->Accept(); | |
77 } else { | |
78 delegate->Cancel(); | |
79 } | |
80 } | |
81 | |
82 InfoBarService* infobar_service_; | |
83 bool accept_; | |
84 bool has_observed_; | |
85 }; | |
86 | |
87 // Class to instantiate on the stack that is meant to be used with | |
88 // FakeGCMProfileService. The ::Run() method follows the signature of | |
89 // FakeGCMProfileService::UnregisterCallback. | |
90 class UnregistrationCallback { | |
91 public: | |
92 UnregistrationCallback() : done_(false), waiting_(false) {} | |
93 | |
94 void Run(const std::string& app_id) { | |
95 app_id_ = app_id; | |
96 done_ = true; | |
97 if (waiting_) | |
98 base::MessageLoop::current()->Quit(); | |
99 } | |
100 | |
101 void WaitUntilSatisfied() { | |
102 if (done_) | |
103 return; | |
104 | |
105 waiting_ = true; | |
106 while (!done_) | |
107 content::RunMessageLoop(); | |
108 } | |
109 | |
110 const std::string& app_id() { | |
111 return app_id_; | |
112 } | |
113 | |
114 private: | |
115 bool done_; | |
116 bool waiting_; | |
117 std::string app_id_; | |
118 }; | |
119 | |
120 // Class to instantiate on the stack that is meant to be used with | |
121 // StubNotificationUIManager::SetNotificationAddedCallback. Mind that Run() | |
122 // might be invoked prior to WaitUntilSatisfied() being called. | |
123 class NotificationAddedCallback { | |
124 public: | |
125 NotificationAddedCallback() : done_(false), waiting_(false) {} | |
126 | |
127 void Run() { | |
128 done_ = true; | |
129 if (waiting_) | |
130 base::MessageLoop::current()->Quit(); | |
131 } | |
132 | |
133 void WaitUntilSatisfied() { | |
134 if (done_) | |
135 return; | |
136 | |
137 waiting_ = true; | |
138 while (!done_) | |
139 content::RunMessageLoop(); | |
140 } | |
141 | |
142 private: | |
143 bool done_; | |
144 bool waiting_; | |
145 }; | |
146 | |
147 // The Push API depends on Web Notifications, which is only available on Android | |
148 // Jelly Bean and later. | |
149 bool IsPushSupported() { | |
150 #if defined(OS_ANDROID) | |
151 if (base::android::BuildInfo::GetInstance()->sdk_int() < | |
152 base::android::SDK_VERSION_JELLY_BEAN) { | |
153 DVLOG(0) << "The Push API is only supported in Android 4.1 and later."; | |
154 return false; | |
155 } | |
156 #endif | |
157 return true; | |
158 } | |
159 | |
160 } // namespace | |
161 | |
162 class PushMessagingBrowserTest : public InProcessBrowserTest { | |
163 public: | |
164 PushMessagingBrowserTest() : gcm_service_(nullptr) {} | |
165 ~PushMessagingBrowserTest() override {} | |
166 | |
167 // InProcessBrowserTest: | |
168 void SetUpCommandLine(base::CommandLine* command_line) override { | |
169 command_line->AppendSwitch(switches::kEnablePushMessagePayload); | |
170 command_line->AppendSwitch(switches::kEnablePushMessagingHasPermission); | |
171 | |
172 InProcessBrowserTest::SetUpCommandLine(command_line); | |
173 } | |
174 | |
175 // InProcessBrowserTest: | |
176 void SetUp() override { | |
177 https_server_.reset(new net::SpawnedTestServer( | |
178 net::SpawnedTestServer::TYPE_HTTPS, | |
179 net::BaseTestServer::SSLOptions( | |
180 net::BaseTestServer::SSLOptions::CERT_OK), | |
181 base::FilePath(FILE_PATH_LITERAL("chrome/test/data/")))); | |
182 ASSERT_TRUE(https_server_->Start()); | |
183 | |
184 #if defined(ENABLE_NOTIFICATIONS) | |
185 notification_manager_.reset(new StubNotificationUIManager); | |
186 notification_service()->SetNotificationUIManagerForTesting( | |
187 notification_manager()); | |
188 #endif | |
189 | |
190 InProcessBrowserTest::SetUp(); | |
191 } | |
192 | |
193 // InProcessBrowserTest: | |
194 void SetUpOnMainThread() override { | |
195 gcm_service_ = static_cast<FakeGCMProfileService*>( | |
196 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( | |
197 browser()->profile(), &FakeGCMProfileService::Build)); | |
198 gcm_service_->set_collect(true); | |
199 | |
200 LoadTestPage(); | |
201 | |
202 InProcessBrowserTest::SetUpOnMainThread(); | |
203 } | |
204 | |
205 // InProcessBrowserTest: | |
206 void TearDown() override { | |
207 #if defined(ENABLE_NOTIFICATIONS) | |
208 notification_service()->SetNotificationUIManagerForTesting(nullptr); | |
209 #endif | |
210 | |
211 InProcessBrowserTest::TearDown(); | |
212 } | |
213 | |
214 void LoadTestPage(const std::string& path) { | |
215 ui_test_utils::NavigateToURL(browser(), https_server_->GetURL(path)); | |
216 } | |
217 | |
218 void LoadTestPage() { | |
219 LoadTestPage(GetTestURL()); | |
220 } | |
221 | |
222 bool RunScript(const std::string& script, std::string* result) { | |
223 return RunScript(script, result, nullptr); | |
224 } | |
225 | |
226 bool RunScript(const std::string& script, std::string* result, | |
227 content::WebContents* web_contents) { | |
228 if (!web_contents) | |
229 web_contents = browser()->tab_strip_model()->GetActiveWebContents(); | |
230 return content::ExecuteScriptAndExtractString(web_contents->GetMainFrame(), | |
231 script, | |
232 result); | |
233 } | |
234 | |
235 void TryToRegisterSuccessfully( | |
236 const std::string& expected_push_registration_id); | |
237 | |
238 PushMessagingApplicationId GetServiceWorkerAppId( | |
239 int64 service_worker_registration_id); | |
240 | |
241 net::SpawnedTestServer* https_server() const { return https_server_.get(); } | |
242 | |
243 FakeGCMProfileService* gcm_service() const { return gcm_service_; } | |
244 | |
245 #if defined(ENABLE_NOTIFICATIONS) | |
246 StubNotificationUIManager* notification_manager() const { | |
247 return notification_manager_.get(); | |
248 } | |
249 | |
250 PlatformNotificationServiceImpl* notification_service() const { | |
251 return PlatformNotificationServiceImpl::GetInstance(); | |
252 } | |
253 #endif | |
254 | |
255 PushMessagingServiceImpl* push_service() { | |
256 return static_cast<PushMessagingServiceImpl*>( | |
257 gcm_service_->push_messaging_service()); | |
258 } | |
259 | |
260 protected: | |
261 virtual std::string GetTestURL() { | |
262 return "files/push_messaging/test.html"; | |
263 } | |
264 | |
265 private: | |
266 scoped_ptr<net::SpawnedTestServer> https_server_; | |
267 FakeGCMProfileService* gcm_service_; | |
268 scoped_ptr<StubNotificationUIManager> notification_manager_; | |
269 | |
270 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest); | |
271 }; | |
272 | |
273 class PushMessagingBadManifestBrowserTest : public PushMessagingBrowserTest { | |
274 std::string GetTestURL() override { | |
275 return "files/push_messaging/test_bad_manifest.html"; | |
276 } | |
277 }; | |
278 | |
279 IN_PROC_BROWSER_TEST_F(PushMessagingBadManifestBrowserTest, | |
280 RegisterFailsNotVisibleMessages) { | |
281 if (!IsPushSupported()) | |
282 return; | |
283 | |
284 std::string script_result; | |
285 | |
286 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); | |
287 ASSERT_EQ("ok - service worker registered", script_result); | |
288 ASSERT_TRUE(RunScript("registerPush()", &script_result)); | |
289 EXPECT_EQ("AbortError - Registration failed - permission denied", | |
290 script_result); | |
291 } | |
292 | |
293 void PushMessagingBrowserTest::TryToRegisterSuccessfully( | |
294 const std::string& expected_push_registration_id) { | |
295 std::string script_result; | |
296 | |
297 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result)); | |
298 EXPECT_EQ("ok - service worker registered", script_result); | |
299 | |
300 InfoBarResponder accepting_responder(browser(), true); | |
301 EXPECT_TRUE(RunScript("requestNotificationPermission()", &script_result)); | |
302 EXPECT_EQ("permission status - granted", script_result); | |
303 | |
304 EXPECT_TRUE(RunScript("registerPush()", &script_result)); | |
305 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - " | |
306 + expected_push_registration_id, script_result); | |
307 } | |
308 | |
309 PushMessagingApplicationId PushMessagingBrowserTest::GetServiceWorkerAppId( | |
310 int64 service_worker_registration_id) { | |
311 GURL origin = https_server()->GetURL(std::string()).GetOrigin(); | |
312 PushMessagingApplicationId application_id = PushMessagingApplicationId::Get( | |
313 browser()->profile(), origin, service_worker_registration_id); | |
314 EXPECT_TRUE(application_id.IsValid()); | |
315 return application_id; | |
316 } | |
317 | |
318 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | |
319 RegisterSuccessNotificationsGranted) { | |
320 if (!IsPushSupported()) | |
321 return; | |
322 | |
323 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */); | |
324 | |
325 PushMessagingApplicationId app_id = GetServiceWorkerAppId(0LL); | |
326 EXPECT_EQ(app_id.app_id_guid(), gcm_service()->last_registered_app_id()); | |
327 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); | |
328 } | |
329 | |
330 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | |
331 RegisterSuccessNotificationsPrompt) { | |
332 if (!IsPushSupported()) | |
333 return; | |
334 | |
335 std::string script_result; | |
336 | |
337 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); | |
338 ASSERT_EQ("ok - service worker registered", script_result); | |
339 | |
340 InfoBarResponder accepting_responder(browser(), true); | |
341 ASSERT_TRUE(RunScript("registerPush()", &script_result)); | |
342 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1-0", script_result); | |
343 | |
344 PushMessagingApplicationId app_id = GetServiceWorkerAppId(0LL); | |
345 EXPECT_EQ(app_id.app_id_guid(), gcm_service()->last_registered_app_id()); | |
346 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); | |
347 } | |
348 | |
349 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | |
350 RegisterFailureNotificationsBlocked) { | |
351 if (!IsPushSupported()) | |
352 return; | |
353 | |
354 std::string script_result; | |
355 | |
356 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); | |
357 ASSERT_EQ("ok - service worker registered", script_result); | |
358 | |
359 InfoBarResponder cancelling_responder(browser(), false); | |
360 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result)); | |
361 ASSERT_EQ("permission status - denied", script_result); | |
362 | |
363 ASSERT_TRUE(RunScript("registerPush()", &script_result)); | |
364 EXPECT_EQ("AbortError - Registration failed - permission denied", | |
365 script_result); | |
366 } | |
367 | |
368 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterFailureNoManifest) { | |
369 if (!IsPushSupported()) | |
370 return; | |
371 | |
372 std::string script_result; | |
373 | |
374 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); | |
375 ASSERT_EQ("ok - service worker registered", script_result); | |
376 | |
377 InfoBarResponder accepting_responder(browser(), true); | |
378 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result)); | |
379 ASSERT_EQ("permission status - granted", script_result); | |
380 | |
381 ASSERT_TRUE(RunScript("removeManifest()", &script_result)); | |
382 ASSERT_EQ("manifest removed", script_result); | |
383 | |
384 ASSERT_TRUE(RunScript("registerPush()", &script_result)); | |
385 EXPECT_EQ("AbortError - Registration failed - no sender id provided", | |
386 script_result); | |
387 } | |
388 | |
389 // TODO(johnme): Test registering from a worker - see https://crbug.com/437298. | |
390 | |
391 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterPersisted) { | |
392 if (!IsPushSupported()) | |
393 return; | |
394 | |
395 std::string script_result; | |
396 | |
397 // First, test that Service Worker registration IDs are assigned in order of | |
398 // registering the Service Workers, and the (fake) push registration ids are | |
399 // assigned in order of push registration (even when these orders are | |
400 // different). | |
401 | |
402 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */); | |
403 PushMessagingApplicationId app_id_sw0 = GetServiceWorkerAppId(0LL); | |
404 EXPECT_EQ(app_id_sw0.app_id_guid(), gcm_service()->last_registered_app_id()); | |
405 | |
406 LoadTestPage("files/push_messaging/subscope1/test.html"); | |
407 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); | |
408 ASSERT_EQ("ok - service worker registered", script_result); | |
409 | |
410 LoadTestPage("files/push_messaging/subscope2/test.html"); | |
411 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); | |
412 ASSERT_EQ("ok - service worker registered", script_result); | |
413 | |
414 // Note that we need to reload the page after registering, otherwise | |
415 // navigator.serviceWorker.ready is going to be resolved with the parent | |
416 // Service Worker which still controls the page. | |
417 LoadTestPage("files/push_messaging/subscope2/test.html"); | |
418 TryToRegisterSuccessfully("1-1" /* expected_push_registration_id */); | |
419 PushMessagingApplicationId app_id_sw2 = GetServiceWorkerAppId(2LL); | |
420 EXPECT_EQ(app_id_sw2.app_id_guid(), gcm_service()->last_registered_app_id()); | |
421 | |
422 LoadTestPage("files/push_messaging/subscope1/test.html"); | |
423 TryToRegisterSuccessfully("1-2" /* expected_push_registration_id */); | |
424 PushMessagingApplicationId app_id_sw1 = GetServiceWorkerAppId(1LL); | |
425 EXPECT_EQ(app_id_sw1.app_id_guid(), gcm_service()->last_registered_app_id()); | |
426 | |
427 // Now test that the Service Worker registration IDs and push registration IDs | |
428 // generated above were persisted to SW storage, by checking that they are | |
429 // unchanged despite requesting them in a different order. | |
430 // TODO(johnme): Ideally we would restart the browser at this point to check | |
431 // they were persisted to disk, but that's not currently possible since the | |
432 // test server uses random port numbers for each test (even PRE_Foo and Foo), | |
433 // so we wouldn't be able to load the test pages with the same origin. | |
434 | |
435 LoadTestPage("files/push_messaging/subscope1/test.html"); | |
436 TryToRegisterSuccessfully("1-2" /* expected_push_registration_id */); | |
437 EXPECT_EQ(app_id_sw1.app_id_guid(), gcm_service()->last_registered_app_id()); | |
438 | |
439 LoadTestPage("files/push_messaging/subscope2/test.html"); | |
440 TryToRegisterSuccessfully("1-1" /* expected_push_registration_id */); | |
441 EXPECT_EQ(app_id_sw1.app_id_guid(), gcm_service()->last_registered_app_id()); | |
442 | |
443 LoadTestPage(); | |
444 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */); | |
445 EXPECT_EQ(app_id_sw1.app_id_guid(), gcm_service()->last_registered_app_id()); | |
446 } | |
447 | |
448 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventSuccess) { | |
449 if (!IsPushSupported()) | |
450 return; | |
451 | |
452 std::string script_result; | |
453 | |
454 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */); | |
455 | |
456 PushMessagingApplicationId app_id = GetServiceWorkerAppId(0LL); | |
457 EXPECT_EQ(app_id.app_id_guid(), gcm_service()->last_registered_app_id()); | |
458 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); | |
459 | |
460 ASSERT_TRUE(RunScript("isControlled()", &script_result)); | |
461 ASSERT_EQ("false - is not controlled", script_result); | |
462 | |
463 LoadTestPage(); // Reload to become controlled. | |
464 | |
465 ASSERT_TRUE(RunScript("isControlled()", &script_result)); | |
466 ASSERT_EQ("true - is controlled", script_result); | |
467 | |
468 GCMClient::IncomingMessage message; | |
469 message.sender_id = "1234567890"; | |
470 message.data["data"] = "testdata"; | |
471 push_service()->OnMessage(app_id.app_id_guid(), message); | |
472 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); | |
473 EXPECT_EQ("testdata", script_result); | |
474 } | |
475 | |
476 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventNoServiceWorker) { | |
477 if (!IsPushSupported()) | |
478 return; | |
479 | |
480 std::string script_result; | |
481 | |
482 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */); | |
483 | |
484 PushMessagingApplicationId app_id = GetServiceWorkerAppId(0LL); | |
485 EXPECT_EQ(app_id.app_id_guid(), gcm_service()->last_registered_app_id()); | |
486 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); | |
487 | |
488 ASSERT_TRUE(RunScript("isControlled()", &script_result)); | |
489 ASSERT_EQ("false - is not controlled", script_result); | |
490 | |
491 LoadTestPage(); // Reload to become controlled. | |
492 | |
493 ASSERT_TRUE(RunScript("isControlled()", &script_result)); | |
494 ASSERT_EQ("true - is controlled", script_result); | |
495 | |
496 // Unregister service worker. Sending a message should now fail. | |
497 ASSERT_TRUE(RunScript("unregisterServiceWorker()", &script_result)); | |
498 ASSERT_EQ("service worker unregistration status: true", script_result); | |
499 | |
500 // When the push service will receive it next message, given that there is no | |
501 // SW available, it should unregister |app_id|. | |
502 UnregistrationCallback callback; | |
503 gcm_service()->SetUnregisterCallback(base::Bind(&UnregistrationCallback::Run, | |
504 base::Unretained(&callback))); | |
505 | |
506 GCMClient::IncomingMessage message; | |
507 message.sender_id = "1234567890"; | |
508 message.data["data"] = "testdata"; | |
509 push_service()->OnMessage(app_id.app_id_guid(), message); | |
510 | |
511 callback.WaitUntilSatisfied(); | |
512 EXPECT_EQ(app_id.app_id_guid(), callback.app_id()); | |
513 | |
514 // No push data should have been received. | |
515 ASSERT_TRUE(RunScript("resultQueue.popImmediately()", &script_result)); | |
516 EXPECT_EQ("null", script_result); | |
517 } | |
518 | |
519 #if defined(ENABLE_NOTIFICATIONS) | |
520 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | |
521 PushEventEnforcesUserVisibleNotification) { | |
522 if (!IsPushSupported()) | |
523 return; | |
524 | |
525 std::string script_result; | |
526 | |
527 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */); | |
528 | |
529 PushMessagingApplicationId app_id = GetServiceWorkerAppId(0LL); | |
530 EXPECT_EQ(app_id.app_id_guid(), gcm_service()->last_registered_app_id()); | |
531 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); | |
532 | |
533 ASSERT_TRUE(RunScript("isControlled()", &script_result)); | |
534 ASSERT_EQ("false - is not controlled", script_result); | |
535 | |
536 LoadTestPage(); // Reload to become controlled. | |
537 | |
538 ASSERT_TRUE(RunScript("isControlled()", &script_result)); | |
539 ASSERT_EQ("true - is controlled", script_result); | |
540 | |
541 notification_manager()->CancelAll(); | |
542 ASSERT_EQ(0u, notification_manager()->GetNotificationCount()); | |
543 | |
544 // We'll need to specify the web_contents in which to eval script, since we're | |
545 // going to run script in a background tab. | |
546 content::WebContents* web_contents = | |
547 browser()->tab_strip_model()->GetActiveWebContents(); | |
548 | |
549 // If the site is visible in an active tab, we should not force a notification | |
550 // to be shown. Try it twice, since we allow one mistake per 10 push events. | |
551 GCMClient::IncomingMessage message; | |
552 message.sender_id = "1234567890"; | |
553 for (int n = 0; n < 2; n++) { | |
554 message.data["data"] = "testdata"; | |
555 push_service()->OnMessage(app_id.app_id_guid(), message); | |
556 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); | |
557 EXPECT_EQ("testdata", script_result); | |
558 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); | |
559 } | |
560 | |
561 // Open a blank foreground tab so site is no longer visible. | |
562 ui_test_utils::NavigateToURLWithDisposition( | |
563 browser(), GURL("about:blank"), NEW_FOREGROUND_TAB, | |
564 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); | |
565 | |
566 // If the Service Worker push event handler does not show a notification, we | |
567 // should show a forced one, but only on the 2nd occurrence since we allow one | |
568 // mistake per 10 push events. | |
569 message.data["data"] = "testdata"; | |
570 push_service()->OnMessage(app_id.app_id_guid(), message); | |
571 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | |
572 EXPECT_EQ("testdata", script_result); | |
573 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); | |
574 message.data["data"] = "testdata"; | |
575 push_service()->OnMessage(app_id.app_id_guid(), message); | |
576 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | |
577 EXPECT_EQ("testdata", script_result); | |
578 EXPECT_EQ(1u, notification_manager()->GetNotificationCount()); | |
579 EXPECT_EQ(base::ASCIIToUTF16(kPushMessagingForcedNotificationTag), | |
580 notification_manager()->GetNotificationAt(0).replace_id()); | |
581 | |
582 // Currently, this notification will stick around until the user or webapp | |
583 // explicitly dismisses it (though we may change this later). | |
584 message.data["data"] = "shownotification"; | |
585 push_service()->OnMessage(app_id.app_id_guid(), message); | |
586 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | |
587 EXPECT_EQ("shownotification", script_result); | |
588 EXPECT_EQ(2u, notification_manager()->GetNotificationCount()); | |
589 | |
590 notification_manager()->CancelAll(); | |
591 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); | |
592 | |
593 // However if the Service Worker push event handler shows a notification, we | |
594 // should not show a forced one. | |
595 message.data["data"] = "shownotification"; | |
596 for (int n = 0; n < 9; n++) { | |
597 push_service()->OnMessage(app_id.app_id_guid(), message); | |
598 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | |
599 EXPECT_EQ("shownotification", script_result); | |
600 EXPECT_EQ(1u, notification_manager()->GetNotificationCount()); | |
601 EXPECT_EQ(base::ASCIIToUTF16("push_test_tag"), | |
602 notification_manager()->GetNotificationAt(0).replace_id()); | |
603 notification_manager()->CancelAll(); | |
604 } | |
605 | |
606 // Now that 10 push messages in a row have shown notifications, we should | |
607 // allow the next one to mistakenly not show a notification. | |
608 message.data["data"] = "testdata"; | |
609 push_service()->OnMessage(app_id.app_id_guid(), message); | |
610 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | |
611 EXPECT_EQ("testdata", script_result); | |
612 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); | |
613 } | |
614 | |
615 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | |
616 PushEventNotificationWithoutEventWaitUntil) { | |
617 if (!IsPushSupported()) | |
618 return; | |
619 | |
620 std::string script_result; | |
621 content::WebContents* web_contents = | |
622 browser()->tab_strip_model()->GetActiveWebContents(); | |
623 | |
624 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */); | |
625 | |
626 PushMessagingApplicationId app_id = GetServiceWorkerAppId(0LL); | |
627 EXPECT_EQ(app_id.app_id_guid(), gcm_service()->last_registered_app_id()); | |
628 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); | |
629 | |
630 ASSERT_TRUE(RunScript("isControlled()", &script_result)); | |
631 ASSERT_EQ("false - is not controlled", script_result); | |
632 | |
633 LoadTestPage(); // Reload to become controlled. | |
634 | |
635 ASSERT_TRUE(RunScript("isControlled()", &script_result)); | |
636 ASSERT_EQ("true - is controlled", script_result); | |
637 | |
638 NotificationAddedCallback callback; | |
639 notification_manager()->SetNotificationAddedCallback( | |
640 base::Bind(&NotificationAddedCallback::Run, base::Unretained(&callback))); | |
641 | |
642 GCMClient::IncomingMessage message; | |
643 message.sender_id = "1234567890"; | |
644 message.data["data"] = "shownotification-without-waituntil"; | |
645 push_service()->OnMessage(app_id.app_id_guid(), message); | |
646 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | |
647 EXPECT_EQ("immediate:shownotification-without-waituntil", script_result); | |
648 | |
649 callback.WaitUntilSatisfied(); | |
650 | |
651 ASSERT_EQ(1u, notification_manager()->GetNotificationCount()); | |
652 EXPECT_EQ(base::ASCIIToUTF16("push_test_tag"), | |
653 notification_manager()->GetNotificationAt(0).replace_id()); | |
654 | |
655 // Verify that the renderer process hasn't crashed. | |
656 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | |
657 EXPECT_EQ("permission status - granted", script_result); | |
658 } | |
659 #endif | |
660 | |
661 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, HasPermissionSaysDefault) { | |
662 if (!IsPushSupported()) | |
663 return; | |
664 | |
665 std::string script_result; | |
666 | |
667 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); | |
668 ASSERT_EQ("ok - service worker registered", script_result); | |
669 | |
670 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | |
671 ASSERT_EQ("permission status - default", script_result); | |
672 } | |
673 | |
674 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, HasPermissionSaysGranted) { | |
675 if (!IsPushSupported()) | |
676 return; | |
677 | |
678 std::string script_result; | |
679 | |
680 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); | |
681 ASSERT_EQ("ok - service worker registered", script_result); | |
682 | |
683 InfoBarResponder accepting_responder(browser(), true); | |
684 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result)); | |
685 EXPECT_EQ("permission status - granted", script_result); | |
686 | |
687 ASSERT_TRUE(RunScript("registerPush()", &script_result)); | |
688 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1-0", script_result); | |
689 | |
690 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | |
691 EXPECT_EQ("permission status - granted", script_result); | |
692 } | |
693 | |
694 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, HasPermissionSaysDenied) { | |
695 if (!IsPushSupported()) | |
696 return; | |
697 | |
698 std::string script_result; | |
699 | |
700 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); | |
701 ASSERT_EQ("ok - service worker registered", script_result); | |
702 | |
703 InfoBarResponder cancelling_responder(browser(), false); | |
704 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result)); | |
705 EXPECT_EQ("permission status - denied", script_result); | |
706 | |
707 ASSERT_TRUE(RunScript("registerPush()", &script_result)); | |
708 EXPECT_EQ("AbortError - Registration failed - permission denied", | |
709 script_result); | |
710 | |
711 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | |
712 EXPECT_EQ("permission status - denied", script_result); | |
713 } | |
714 | |
715 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, UnregisterSuccess) { | |
716 if (!IsPushSupported()) | |
717 return; | |
718 | |
719 std::string script_result; | |
720 | |
721 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */); | |
722 | |
723 gcm_service()->AddExpectedUnregisterResponse(GCMClient::SUCCESS); | |
724 | |
725 ASSERT_TRUE(RunScript("unregister()", &script_result)); | |
726 EXPECT_EQ("unregister result: true", script_result); | |
727 } | |
728 | |
729 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, UnregisterNetworkError) { | |
730 if (!IsPushSupported()) | |
731 return; | |
732 | |
733 std::string script_result; | |
734 | |
735 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */); | |
736 | |
737 gcm_service()->AddExpectedUnregisterResponse(GCMClient::NETWORK_ERROR); | |
738 | |
739 ASSERT_TRUE(RunScript("unregister()", &script_result)); | |
740 EXPECT_EQ("unregister error: NetworkError: " | |
741 "Unregistration failed - could not connect to push server", | |
742 script_result); | |
743 } | |
744 | |
745 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, UnregisterAbortError) { | |
746 if (!IsPushSupported()) | |
747 return; | |
748 | |
749 std::string script_result; | |
750 | |
751 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */); | |
752 | |
753 gcm_service()->AddExpectedUnregisterResponse(GCMClient::UNKNOWN_ERROR); | |
754 | |
755 ASSERT_TRUE(RunScript("unregister()", &script_result)); | |
756 EXPECT_EQ("unregister error: " | |
757 "AbortError: Unregistration failed - push service error", | |
758 script_result); | |
759 } | |
760 | |
761 #if defined(OS_ANDROID) | |
762 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushUnavailableOnAndroidICS) { | |
763 // This test should only run on Android ICS to confirm that the Push API | |
764 // is not available on that version of Android. | |
765 if (IsPushSupported()) | |
766 return; | |
767 | |
768 std::string script_result; | |
769 ASSERT_TRUE(RunScript("window.PushManager", &script_result)); | |
770 EXPECT_EQ("undefined", script_result); | |
771 } | |
772 #endif | |
773 | |
774 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | |
775 GlobalResetPushPermissionUnregisters) { | |
776 std::string script_result; | |
777 | |
778 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */); | |
779 | |
780 ASSERT_TRUE(RunScript("hasRegistration()", &script_result)); | |
781 EXPECT_EQ("true - registered", script_result); | |
782 | |
783 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | |
784 EXPECT_EQ("permission status - granted", script_result); | |
785 | |
786 browser()->profile()->GetHostContentSettingsMap()-> | |
787 ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING); | |
788 | |
789 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | |
790 EXPECT_EQ("permission status - default", script_result); | |
791 | |
792 ASSERT_TRUE(RunScript("hasRegistration()", &script_result)); | |
793 EXPECT_EQ("false - not registered", script_result); | |
794 } | |
795 | |
796 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | |
797 LocalResetPushPermissionUnregisters) { | |
798 std::string script_result; | |
799 | |
800 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */); | |
801 | |
802 ASSERT_TRUE(RunScript("hasRegistration()", &script_result)); | |
803 EXPECT_EQ("true - registered", script_result); | |
804 | |
805 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | |
806 EXPECT_EQ("permission status - granted", script_result); | |
807 | |
808 GURL origin = https_server()->GetURL(std::string()).GetOrigin(); | |
809 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting( | |
810 ContentSettingsPattern::FromURLNoWildcard(origin), | |
811 ContentSettingsPattern::FromURLNoWildcard(origin), | |
812 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
813 std::string(), | |
814 CONTENT_SETTING_DEFAULT); | |
815 | |
816 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | |
817 EXPECT_EQ("permission status - default", script_result); | |
818 | |
819 ASSERT_TRUE(RunScript("hasRegistration()", &script_result)); | |
820 EXPECT_EQ("false - not registered", script_result); | |
821 } | |
822 | |
823 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | |
824 DenyPushPermissionUnregisters) { | |
825 std::string script_result; | |
826 | |
827 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */); | |
828 | |
829 ASSERT_TRUE(RunScript("hasRegistration()", &script_result)); | |
830 EXPECT_EQ("true - registered", script_result); | |
831 | |
832 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | |
833 EXPECT_EQ("permission status - granted", script_result); | |
834 | |
835 GURL origin = https_server()->GetURL(std::string()).GetOrigin(); | |
836 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting( | |
837 ContentSettingsPattern::FromURLNoWildcard(origin), | |
838 ContentSettingsPattern::FromURLNoWildcard(origin), | |
839 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
840 std::string(), | |
841 CONTENT_SETTING_BLOCK); | |
842 | |
843 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | |
844 EXPECT_EQ("permission status - denied", script_result); | |
845 | |
846 ASSERT_TRUE(RunScript("hasRegistration()", &script_result)); | |
847 EXPECT_EQ("false - not registered", script_result); | |
848 } | |
849 | |
850 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | |
851 GlobalResetNotificationsPermissionUnregisters) { | |
852 std::string script_result; | |
853 | |
854 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */); | |
855 | |
856 ASSERT_TRUE(RunScript("hasRegistration()", &script_result)); | |
857 EXPECT_EQ("true - registered", script_result); | |
858 | |
859 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | |
860 EXPECT_EQ("permission status - granted", script_result); | |
861 | |
862 browser()->profile()->GetHostContentSettingsMap()-> | |
863 ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | |
864 | |
865 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | |
866 EXPECT_EQ("permission status - default", script_result); | |
867 | |
868 ASSERT_TRUE(RunScript("hasRegistration()", &script_result)); | |
869 EXPECT_EQ("false - not registered", script_result); | |
870 } | |
871 | |
872 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | |
873 LocalResetNotificationsPermissionUnregisters) { | |
874 std::string script_result; | |
875 | |
876 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */); | |
877 | |
878 ASSERT_TRUE(RunScript("hasRegistration()", &script_result)); | |
879 EXPECT_EQ("true - registered", script_result); | |
880 | |
881 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | |
882 EXPECT_EQ("permission status - granted", script_result); | |
883 | |
884 GURL origin = https_server()->GetURL(std::string()).GetOrigin(); | |
885 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting( | |
886 ContentSettingsPattern::FromURLNoWildcard(origin), | |
887 ContentSettingsPattern::Wildcard(), | |
888 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
889 std::string(), | |
890 CONTENT_SETTING_DEFAULT); | |
891 | |
892 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | |
893 EXPECT_EQ("permission status - default", script_result); | |
894 | |
895 ASSERT_TRUE(RunScript("hasRegistration()", &script_result)); | |
896 EXPECT_EQ("false - not registered", script_result); | |
897 } | |
898 | |
899 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | |
900 DenyNotificationsPermissionUnregisters) { | |
901 std::string script_result; | |
902 | |
903 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */); | |
904 | |
905 ASSERT_TRUE(RunScript("hasRegistration()", &script_result)); | |
906 EXPECT_EQ("true - registered", script_result); | |
907 | |
908 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | |
909 EXPECT_EQ("permission status - granted", script_result); | |
910 | |
911 GURL origin = https_server()->GetURL(std::string()).GetOrigin(); | |
912 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting( | |
913 ContentSettingsPattern::FromURLNoWildcard(origin), | |
914 ContentSettingsPattern::Wildcard(), | |
915 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
916 std::string(), | |
917 CONTENT_SETTING_BLOCK); | |
918 | |
919 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | |
920 EXPECT_EQ("permission status - denied", script_result); | |
921 | |
922 ASSERT_TRUE(RunScript("hasRegistration()", &script_result)); | |
923 EXPECT_EQ("false - not registered", script_result); | |
924 } | |
925 | |
926 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | |
927 GrantAlreadyGrantedPermissionDoesNotUnregister) { | |
928 std::string script_result; | |
929 | |
930 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */); | |
931 | |
932 ASSERT_TRUE(RunScript("hasRegistration()", &script_result)); | |
933 EXPECT_EQ("true - registered", script_result); | |
934 | |
935 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | |
936 EXPECT_EQ("permission status - granted", script_result); | |
937 | |
938 GURL origin = https_server()->GetURL(std::string()).GetOrigin(); | |
939 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting( | |
940 ContentSettingsPattern::FromURLNoWildcard(origin), | |
941 ContentSettingsPattern::Wildcard(), | |
942 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
943 std::string(), | |
944 CONTENT_SETTING_ALLOW); | |
945 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting( | |
946 ContentSettingsPattern::FromURLNoWildcard(origin), | |
947 ContentSettingsPattern::FromURLNoWildcard(origin), | |
948 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
949 std::string(), | |
950 CONTENT_SETTING_ALLOW); | |
951 | |
952 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | |
953 EXPECT_EQ("permission status - granted", script_result); | |
954 | |
955 ASSERT_TRUE(RunScript("hasRegistration()", &script_result)); | |
956 EXPECT_EQ("true - registered", script_result); | |
957 } | |
958 | |
959 // This test is testing some non-trivial content settings rules and make sure | |
960 // that they are respected with regards to automatic unregistration. In other | |
961 // words, it checks that the push service does not end up unregistering origins | |
962 // that have push permission with some non-common rules. | |
963 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | |
964 AutomaticUnregistrationFollowsContentSettingRules) { | |
965 std::string script_result; | |
966 | |
967 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */); | |
968 | |
969 ASSERT_TRUE(RunScript("hasRegistration()", &script_result)); | |
970 EXPECT_EQ("true - registered", script_result); | |
971 | |
972 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | |
973 EXPECT_EQ("permission status - granted", script_result); | |
974 | |
975 GURL origin = https_server()->GetURL(std::string()).GetOrigin(); | |
976 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting( | |
977 ContentSettingsPattern::Wildcard(), | |
978 ContentSettingsPattern::Wildcard(), | |
979 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
980 std::string(), | |
981 CONTENT_SETTING_ALLOW); | |
982 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting( | |
983 ContentSettingsPattern::FromString("https://*"), | |
984 ContentSettingsPattern::FromString("https://*"), | |
985 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
986 std::string(), | |
987 CONTENT_SETTING_ALLOW); | |
988 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting( | |
989 ContentSettingsPattern::FromURLNoWildcard(origin), | |
990 ContentSettingsPattern::Wildcard(), | |
991 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
992 std::string(), | |
993 CONTENT_SETTING_DEFAULT); | |
994 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting( | |
995 ContentSettingsPattern::FromURLNoWildcard(origin), | |
996 ContentSettingsPattern::FromURLNoWildcard(origin), | |
997 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
998 std::string(), | |
999 CONTENT_SETTING_DEFAULT); | |
1000 | |
1001 // The two first rules should give |origin| the permission to use Push even | |
1002 // if the rules it used to have have been reset. | |
1003 // The Push service should not unsubcribe |origin| because at no point it was | |
1004 // left without permission to use Push. | |
1005 | |
1006 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | |
1007 EXPECT_EQ("permission status - granted", script_result); | |
1008 | |
1009 ASSERT_TRUE(RunScript("hasRegistration()", &script_result)); | |
1010 EXPECT_EQ("true - registered", script_result); | |
1011 } | |
1012 | |
1013 } // namespace gcm | |
OLD | NEW |