| 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 <map> | 5 #include <map> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/barrier_closure.h" | 8 #include "base/barrier_closure.h" |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/browser/browsing_data/browsing_data_helper.h" | 14 #include "chrome/browser/browsing_data/browsing_data_helper.h" |
| 15 #include "chrome/browser/browsing_data/browsing_data_remover.h" | 15 #include "chrome/browser/browsing_data/browsing_data_remover.h" |
| 16 #include "chrome/browser/browsing_data/browsing_data_remover_test_util.h" | 16 #include "chrome/browser/browsing_data/browsing_data_remover_test_util.h" |
| 17 #include "chrome/browser/infobars/infobar_responder.h" |
| 17 #include "chrome/browser/infobars/infobar_service.h" | 18 #include "chrome/browser/infobars/infobar_service.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" |
| 20 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/push_messaging/push_messaging_application_id.h" | 22 #include "chrome/browser/push_messaging/push_messaging_application_id.h" |
| 22 #include "chrome/browser/push_messaging/push_messaging_constants.h" | 23 #include "chrome/browser/push_messaging/push_messaging_constants.h" |
| 23 #include "chrome/browser/push_messaging/push_messaging_service_factory.h" | 24 #include "chrome/browser/push_messaging/push_messaging_service_factory.h" |
| 24 #include "chrome/browser/push_messaging/push_messaging_service_impl.h" | 25 #include "chrome/browser/push_messaging/push_messaging_service_impl.h" |
| 25 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" | 26 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" |
| 26 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 27 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
| 27 #include "chrome/browser/ui/browser.h" | 28 #include "chrome/browser/ui/browser.h" |
| 28 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 29 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 29 #include "chrome/test/base/in_process_browser_test.h" | 30 #include "chrome/test/base/in_process_browser_test.h" |
| 30 #include "chrome/test/base/ui_test_utils.h" | 31 #include "chrome/test/base/ui_test_utils.h" |
| 31 #include "components/content_settings/core/browser/host_content_settings_map.h" | 32 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 32 #include "components/content_settings/core/common/content_settings.h" | 33 #include "components/content_settings/core/common/content_settings.h" |
| 33 #include "components/content_settings/core/common/content_settings_types.h" | 34 #include "components/content_settings/core/common/content_settings_types.h" |
| 34 #include "components/gcm_driver/gcm_client.h" | 35 #include "components/gcm_driver/gcm_client.h" |
| 35 #include "components/infobars/core/confirm_infobar_delegate.h" | |
| 36 #include "components/infobars/core/infobar.h" | |
| 37 #include "components/infobars/core/infobar_manager.h" | |
| 38 #include "content/public/browser/web_contents.h" | 36 #include "content/public/browser/web_contents.h" |
| 39 #include "content/public/common/content_switches.h" | 37 #include "content/public/common/content_switches.h" |
| 40 #include "content/public/test/browser_test_utils.h" | 38 #include "content/public/test/browser_test_utils.h" |
| 41 #include "content/public/test/test_utils.h" | 39 #include "content/public/test/test_utils.h" |
| 42 #include "ui/base/window_open_disposition.h" | 40 #include "ui/base/window_open_disposition.h" |
| 43 | 41 |
| 44 #if defined(OS_ANDROID) | 42 #if defined(OS_ANDROID) |
| 45 #include "base/android/build_info.h" | 43 #include "base/android/build_info.h" |
| 46 #endif | 44 #endif |
| 47 | 45 |
| 48 namespace { | 46 namespace { |
| 49 // Responds to a confirm infobar by accepting or cancelling it. Responds to at | |
| 50 // most one infobar. | |
| 51 class InfoBarResponder : public infobars::InfoBarManager::Observer { | |
| 52 public: | |
| 53 InfoBarResponder(Browser* browser, bool accept) | |
| 54 : infobar_service_(InfoBarService::FromWebContents( | |
| 55 browser->tab_strip_model()->GetActiveWebContents())), | |
| 56 accept_(accept), | |
| 57 has_observed_(false) { | |
| 58 infobar_service_->AddObserver(this); | |
| 59 } | |
| 60 | |
| 61 ~InfoBarResponder() override { infobar_service_->RemoveObserver(this); } | |
| 62 | |
| 63 // infobars::InfoBarManager::Observer | |
| 64 void OnInfoBarAdded(infobars::InfoBar* infobar) override { | |
| 65 if (has_observed_) | |
| 66 return; | |
| 67 has_observed_ = true; | |
| 68 ConfirmInfoBarDelegate* delegate = | |
| 69 infobar->delegate()->AsConfirmInfoBarDelegate(); | |
| 70 DCHECK(delegate); | |
| 71 | |
| 72 // Respond to the infobar asynchronously, like a person. | |
| 73 base::MessageLoop::current()->PostTask( | |
| 74 FROM_HERE, | |
| 75 base::Bind( | |
| 76 &InfoBarResponder::Respond, base::Unretained(this), delegate)); | |
| 77 } | |
| 78 | |
| 79 private: | |
| 80 void Respond(ConfirmInfoBarDelegate* delegate) { | |
| 81 if (accept_) { | |
| 82 delegate->Accept(); | |
| 83 } else { | |
| 84 delegate->Cancel(); | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 InfoBarService* infobar_service_; | |
| 89 bool accept_; | |
| 90 bool has_observed_; | |
| 91 }; | |
| 92 | |
| 93 // Class to instantiate on the stack that is meant to be used with | 47 // Class to instantiate on the stack that is meant to be used with |
| 94 // FakeGCMProfileService. The ::Run() method follows the signature of | 48 // FakeGCMProfileService. The ::Run() method follows the signature of |
| 95 // FakeGCMProfileService::UnregisterCallback. | 49 // FakeGCMProfileService::UnregisterCallback. |
| 96 class UnregistrationCallback { | 50 class UnregistrationCallback { |
| 97 public: | 51 public: |
| 98 UnregistrationCallback() | 52 UnregistrationCallback() |
| 99 : message_loop_runner_(new content::MessageLoopRunner) {} | 53 : message_loop_runner_(new content::MessageLoopRunner) {} |
| 100 | 54 |
| 101 void Run(const std::string& app_id) { | 55 void Run(const std::string& app_id) { |
| 102 app_id_ = app_id; | 56 app_id_ = app_id; |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 | 169 |
| 216 PushMessagingServiceImpl* push_service() const { return push_service_; } | 170 PushMessagingServiceImpl* push_service() const { return push_service_; } |
| 217 | 171 |
| 218 protected: | 172 protected: |
| 219 virtual std::string GetTestURL() { | 173 virtual std::string GetTestURL() { |
| 220 return "files/push_messaging/test.html"; | 174 return "files/push_messaging/test.html"; |
| 221 } | 175 } |
| 222 | 176 |
| 223 virtual Browser* GetBrowser() const { return browser(); } | 177 virtual Browser* GetBrowser() const { return browser(); } |
| 224 | 178 |
| 179 InfoBarService* GetInfoBarService() { |
| 180 return InfoBarService::FromWebContents( |
| 181 GetBrowser()->tab_strip_model()->GetActiveWebContents()); |
| 182 } |
| 183 |
| 225 private: | 184 private: |
| 226 scoped_ptr<net::SpawnedTestServer> https_server_; | 185 scoped_ptr<net::SpawnedTestServer> https_server_; |
| 227 gcm::FakeGCMProfileService* gcm_service_; | 186 gcm::FakeGCMProfileService* gcm_service_; |
| 228 PushMessagingServiceImpl* push_service_; | 187 PushMessagingServiceImpl* push_service_; |
| 229 scoped_ptr<StubNotificationUIManager> notification_manager_; | 188 scoped_ptr<StubNotificationUIManager> notification_manager_; |
| 230 | 189 |
| 231 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest); | 190 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest); |
| 232 }; | 191 }; |
| 233 | 192 |
| 234 class PushMessagingBadManifestBrowserTest : public PushMessagingBrowserTest { | 193 class PushMessagingBadManifestBrowserTest : public PushMessagingBrowserTest { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 248 script_result); | 207 script_result); |
| 249 } | 208 } |
| 250 | 209 |
| 251 void PushMessagingBrowserTest::TryToRegisterSuccessfully( | 210 void PushMessagingBrowserTest::TryToRegisterSuccessfully( |
| 252 const std::string& expected_push_registration_id) { | 211 const std::string& expected_push_registration_id) { |
| 253 std::string script_result; | 212 std::string script_result; |
| 254 | 213 |
| 255 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result)); | 214 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result)); |
| 256 EXPECT_EQ("ok - service worker registered", script_result); | 215 EXPECT_EQ("ok - service worker registered", script_result); |
| 257 | 216 |
| 258 InfoBarResponder accepting_responder(GetBrowser(), true); | 217 InfoBarResponder accepting_responder(GetInfoBarService(), true); |
| 259 EXPECT_TRUE(RunScript("requestNotificationPermission()", &script_result)); | 218 EXPECT_TRUE(RunScript("requestNotificationPermission()", &script_result)); |
| 260 EXPECT_EQ("permission status - granted", script_result); | 219 EXPECT_EQ("permission status - granted", script_result); |
| 261 | 220 |
| 262 EXPECT_TRUE(RunScript("registerPush()", &script_result)); | 221 EXPECT_TRUE(RunScript("registerPush()", &script_result)); |
| 263 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - " | 222 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - " |
| 264 + expected_push_registration_id, script_result); | 223 + expected_push_registration_id, script_result); |
| 265 } | 224 } |
| 266 | 225 |
| 267 PushMessagingApplicationId PushMessagingBrowserTest::GetServiceWorkerAppId( | 226 PushMessagingApplicationId PushMessagingBrowserTest::GetServiceWorkerAppId( |
| 268 int64 service_worker_registration_id) { | 227 int64 service_worker_registration_id) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 282 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); | 241 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); |
| 283 } | 242 } |
| 284 | 243 |
| 285 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | 244 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, |
| 286 RegisterSuccessNotificationsPrompt) { | 245 RegisterSuccessNotificationsPrompt) { |
| 287 std::string script_result; | 246 std::string script_result; |
| 288 | 247 |
| 289 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); | 248 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); |
| 290 ASSERT_EQ("ok - service worker registered", script_result); | 249 ASSERT_EQ("ok - service worker registered", script_result); |
| 291 | 250 |
| 292 InfoBarResponder accepting_responder(GetBrowser(), true); | 251 InfoBarResponder accepting_responder(GetInfoBarService(), true); |
| 293 ASSERT_TRUE(RunScript("registerPush()", &script_result)); | 252 ASSERT_TRUE(RunScript("registerPush()", &script_result)); |
| 294 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1-0", script_result); | 253 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1-0", script_result); |
| 295 | 254 |
| 296 PushMessagingApplicationId app_id = GetServiceWorkerAppId(0LL); | 255 PushMessagingApplicationId app_id = GetServiceWorkerAppId(0LL); |
| 297 EXPECT_EQ(app_id.app_id_guid(), gcm_service()->last_registered_app_id()); | 256 EXPECT_EQ(app_id.app_id_guid(), gcm_service()->last_registered_app_id()); |
| 298 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); | 257 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); |
| 299 } | 258 } |
| 300 | 259 |
| 301 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | 260 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, |
| 302 RegisterFailureNotificationsBlocked) { | 261 RegisterFailureNotificationsBlocked) { |
| 303 std::string script_result; | 262 std::string script_result; |
| 304 | 263 |
| 305 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); | 264 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); |
| 306 ASSERT_EQ("ok - service worker registered", script_result); | 265 ASSERT_EQ("ok - service worker registered", script_result); |
| 307 | 266 |
| 308 InfoBarResponder cancelling_responder(GetBrowser(), false); | 267 InfoBarResponder cancelling_responder(GetInfoBarService(), false); |
| 309 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result)); | 268 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result)); |
| 310 ASSERT_EQ("permission status - denied", script_result); | 269 ASSERT_EQ("permission status - denied", script_result); |
| 311 | 270 |
| 312 ASSERT_TRUE(RunScript("registerPush()", &script_result)); | 271 ASSERT_TRUE(RunScript("registerPush()", &script_result)); |
| 313 EXPECT_EQ("AbortError - Registration failed - permission denied", | 272 EXPECT_EQ("AbortError - Registration failed - permission denied", |
| 314 script_result); | 273 script_result); |
| 315 } | 274 } |
| 316 | 275 |
| 317 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterFailureNoManifest) { | 276 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterFailureNoManifest) { |
| 318 std::string script_result; | 277 std::string script_result; |
| 319 | 278 |
| 320 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); | 279 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); |
| 321 ASSERT_EQ("ok - service worker registered", script_result); | 280 ASSERT_EQ("ok - service worker registered", script_result); |
| 322 | 281 |
| 323 InfoBarResponder accepting_responder(GetBrowser(), true); | 282 InfoBarResponder accepting_responder(GetInfoBarService(), true); |
| 324 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result)); | 283 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result)); |
| 325 ASSERT_EQ("permission status - granted", script_result); | 284 ASSERT_EQ("permission status - granted", script_result); |
| 326 | 285 |
| 327 ASSERT_TRUE(RunScript("removeManifest()", &script_result)); | 286 ASSERT_TRUE(RunScript("removeManifest()", &script_result)); |
| 328 ASSERT_EQ("manifest removed", script_result); | 287 ASSERT_EQ("manifest removed", script_result); |
| 329 | 288 |
| 330 ASSERT_TRUE(RunScript("registerPush()", &script_result)); | 289 ASSERT_TRUE(RunScript("registerPush()", &script_result)); |
| 331 EXPECT_EQ("AbortError - Registration failed - no sender id provided", | 290 EXPECT_EQ("AbortError - Registration failed - no sender id provided", |
| 332 script_result); | 291 script_result); |
| 333 } | 292 } |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 603 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | 562 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); |
| 604 ASSERT_EQ("permission status - default", script_result); | 563 ASSERT_EQ("permission status - default", script_result); |
| 605 } | 564 } |
| 606 | 565 |
| 607 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, HasPermissionSaysGranted) { | 566 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, HasPermissionSaysGranted) { |
| 608 std::string script_result; | 567 std::string script_result; |
| 609 | 568 |
| 610 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); | 569 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); |
| 611 ASSERT_EQ("ok - service worker registered", script_result); | 570 ASSERT_EQ("ok - service worker registered", script_result); |
| 612 | 571 |
| 613 InfoBarResponder accepting_responder(GetBrowser(), true); | 572 InfoBarResponder accepting_responder(GetInfoBarService(), true); |
| 614 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result)); | 573 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result)); |
| 615 EXPECT_EQ("permission status - granted", script_result); | 574 EXPECT_EQ("permission status - granted", script_result); |
| 616 | 575 |
| 617 ASSERT_TRUE(RunScript("registerPush()", &script_result)); | 576 ASSERT_TRUE(RunScript("registerPush()", &script_result)); |
| 618 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1-0", script_result); | 577 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1-0", script_result); |
| 619 | 578 |
| 620 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | 579 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); |
| 621 EXPECT_EQ("permission status - granted", script_result); | 580 EXPECT_EQ("permission status - granted", script_result); |
| 622 } | 581 } |
| 623 | 582 |
| 624 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, HasPermissionSaysDenied) { | 583 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, HasPermissionSaysDenied) { |
| 625 std::string script_result; | 584 std::string script_result; |
| 626 | 585 |
| 627 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); | 586 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); |
| 628 ASSERT_EQ("ok - service worker registered", script_result); | 587 ASSERT_EQ("ok - service worker registered", script_result); |
| 629 | 588 |
| 630 InfoBarResponder cancelling_responder(GetBrowser(), false); | 589 InfoBarResponder cancelling_responder(GetInfoBarService(), false); |
| 631 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result)); | 590 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result)); |
| 632 EXPECT_EQ("permission status - denied", script_result); | 591 EXPECT_EQ("permission status - denied", script_result); |
| 633 | 592 |
| 634 ASSERT_TRUE(RunScript("registerPush()", &script_result)); | 593 ASSERT_TRUE(RunScript("registerPush()", &script_result)); |
| 635 EXPECT_EQ("AbortError - Registration failed - permission denied", | 594 EXPECT_EQ("AbortError - Registration failed - permission denied", |
| 636 script_result); | 595 script_result); |
| 637 | 596 |
| 638 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); | 597 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); |
| 639 EXPECT_EQ("permission status - denied", script_result); | 598 EXPECT_EQ("permission status - denied", script_result); |
| 640 } | 599 } |
| (...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1041 std::string script_result; | 1000 std::string script_result; |
| 1042 | 1001 |
| 1043 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); | 1002 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); |
| 1044 ASSERT_EQ("ok - service worker registered", script_result); | 1003 ASSERT_EQ("ok - service worker registered", script_result); |
| 1045 | 1004 |
| 1046 // In Incognito mode the promise returned by getSubscription should not hang, | 1005 // In Incognito mode the promise returned by getSubscription should not hang, |
| 1047 // it should just fulfill with null. | 1006 // it should just fulfill with null. |
| 1048 ASSERT_TRUE(RunScript("hasRegistration()", &script_result)); | 1007 ASSERT_TRUE(RunScript("hasRegistration()", &script_result)); |
| 1049 ASSERT_EQ("false - not registered", script_result); | 1008 ASSERT_EQ("false - not registered", script_result); |
| 1050 } | 1009 } |
| OLD | NEW |