diff options
-rw-r--r-- | chrome/browser/services/gcm/fake_gcm_profile_service.cc | 5 | ||||
-rw-r--r-- | chrome/browser/services/gcm/push_messaging_browsertest.cc | 168 | ||||
-rw-r--r-- | chrome/browser/services/gcm/push_messaging_constants.cc | 11 | ||||
-rw-r--r-- | chrome/browser/services/gcm/push_messaging_constants.h | 14 | ||||
-rw-r--r-- | chrome/browser/services/gcm/push_messaging_service_impl.cc | 7 | ||||
-rw-r--r-- | chrome/browser/services/gcm/push_messaging_service_impl.h | 2 | ||||
-rw-r--r-- | chrome/chrome_browser.gypi | 2 | ||||
-rw-r--r-- | chrome/chrome_tests.gypi | 1 | ||||
-rw-r--r-- | chrome/test/data/push_messaging/service_worker.js | 7 | ||||
-rw-r--r-- | chrome/test/data/push_messaging/test.html | 34 | ||||
-rwxr-xr-x | net/tools/testserver/testserver.py | 1 |
11 files changed, 250 insertions, 2 deletions
diff --git a/chrome/browser/services/gcm/fake_gcm_profile_service.cc b/chrome/browser/services/gcm/fake_gcm_profile_service.cc index 4855b95..8169ded 100644 --- a/chrome/browser/services/gcm/fake_gcm_profile_service.cc +++ b/chrome/browser/services/gcm/fake_gcm_profile_service.cc @@ -111,7 +111,10 @@ KeyedService* FakeGCMProfileService::Build(content::BrowserContext* context) { } FakeGCMProfileService::FakeGCMProfileService(Profile* profile) - : collect_(false) {} + : collect_(false) { + static_cast<PushMessagingServiceImpl*>(push_messaging_service()) + ->SetProfileForTesting(profile); +} FakeGCMProfileService::~FakeGCMProfileService() {} diff --git a/chrome/browser/services/gcm/push_messaging_browsertest.cc b/chrome/browser/services/gcm/push_messaging_browsertest.cc new file mode 100644 index 0000000..46cd0cb --- /dev/null +++ b/chrome/browser/services/gcm/push_messaging_browsertest.cc @@ -0,0 +1,168 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include <string> + +#include "base/bind.h" +#include "base/command_line.h" +#include "base/message_loop/message_loop.h" +#include "chrome/browser/infobars/infobar_service.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/services/gcm/fake_gcm_profile_service.h" +#include "chrome/browser/services/gcm/gcm_profile_service_factory.h" +#include "chrome/browser/services/gcm/push_messaging_application_id.h" +#include "chrome/browser/services/gcm/push_messaging_constants.h" +#include "chrome/browser/ui/browser.h" +#include "chrome/browser/ui/tabs/tab_strip_model.h" +#include "chrome/test/base/in_process_browser_test.h" +#include "chrome/test/base/ui_test_utils.h" +#include "components/infobars/core/confirm_infobar_delegate.h" +#include "components/infobars/core/infobar.h" +#include "components/infobars/core/infobar_manager.h" +#include "content/public/browser/web_contents.h" +#include "content/public/common/content_switches.h" +#include "content/public/test/browser_test_utils.h" + +namespace gcm { + +namespace { +// Responds to a confirm infobar by accepting or cancelling it. Responds to at +// most one infobar. +class InfoBarResponder : public infobars::InfoBarManager::Observer { + public: + InfoBarResponder(Browser* browser, bool accept) + : infobar_service_(InfoBarService::FromWebContents( + browser->tab_strip_model()->GetActiveWebContents())), + accept_(accept), + has_observed_(false) { + infobar_service_->AddObserver(this); + } + + virtual ~InfoBarResponder() { infobar_service_->RemoveObserver(this); } + + // infobars::InfoBarManager::Observer + virtual void OnInfoBarAdded(infobars::InfoBar* infobar) override { + if (has_observed_) + return; + has_observed_ = true; + ConfirmInfoBarDelegate* delegate = + infobar->delegate()->AsConfirmInfoBarDelegate(); + DCHECK(delegate); + + // Respond to the infobar asynchronously, like a person. + base::MessageLoop::current()->PostTask( + FROM_HERE, + base::Bind( + &InfoBarResponder::Respond, base::Unretained(this), delegate)); + } + + private: + void Respond(ConfirmInfoBarDelegate* delegate) { + if (accept_) { + delegate->Accept(); + } else { + delegate->Cancel(); + } + } + + InfoBarService* infobar_service_; + bool accept_; + bool has_observed_; +}; +} // namespace + +class PushMessagingBrowserTest : public InProcessBrowserTest { + public: + PushMessagingBrowserTest() : gcm_service_(nullptr) {} + virtual ~PushMessagingBrowserTest() {} + + // InProcessBrowserTest: + virtual void SetUpCommandLine(base::CommandLine* command_line) override { + command_line->AppendSwitch( + switches::kEnableExperimentalWebPlatformFeatures); + + InProcessBrowserTest::SetUpCommandLine(command_line); + } + + // InProcessBrowserTest: + virtual void SetUp() override { + https_server_.reset(new net::SpawnedTestServer( + net::SpawnedTestServer::TYPE_HTTPS, + net::BaseTestServer::SSLOptions( + net::BaseTestServer::SSLOptions::CERT_OK), + base::FilePath(FILE_PATH_LITERAL("chrome/test/data/")))); + ASSERT_TRUE(https_server_->Start()); + + InProcessBrowserTest::SetUp(); + } + + // InProcessBrowserTest: + virtual void SetUpOnMainThread() override { + gcm_service_ = static_cast<FakeGCMProfileService*>( + GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( + browser()->profile(), &FakeGCMProfileService::Build)); + gcm_service_->set_collect(true); + + ui_test_utils::NavigateToURL( + browser(), https_server_->GetURL("files/push_messaging/test.html")); + + InProcessBrowserTest::SetUpOnMainThread(); + } + + bool RunScript(const std::string& script, std::string* result) { + return content::ExecuteScriptAndExtractString( + browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(), + script, + result); + } + + bool RegisterServiceWorker(std::string* result) { + return RunScript("registerServiceWorker()", result); + } + + bool RegisterPush(std::string* result) { + return RunScript("registerPush('1234567890')", result); + } + + net::SpawnedTestServer* https_server() const { return https_server_.get(); } + + FakeGCMProfileService* gcm_service() const { return gcm_service_; } + + private: + scoped_ptr<net::SpawnedTestServer> https_server_; + FakeGCMProfileService* gcm_service_; + + DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest); +}; + +IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterSuccess) { + std::string register_worker_result; + ASSERT_TRUE(RegisterServiceWorker(®ister_worker_result)); + ASSERT_EQ("ok", register_worker_result); + + InfoBarResponder accepting_responder(browser(), true); + + std::string register_push_result; + ASSERT_TRUE(RegisterPush(®ister_push_result)); + EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1", register_push_result); + + PushMessagingApplicationId expected_id(https_server()->GetURL(""), 0L); + EXPECT_EQ(expected_id.ToString(), gcm_service()->last_registered_app_id()); + EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); +} + +IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterFailureNoPermission) { + std::string register_worker_result; + ASSERT_TRUE(RegisterServiceWorker(®ister_worker_result)); + ASSERT_EQ("ok", register_worker_result); + + InfoBarResponder cancelling_responder(browser(), false); + + std::string register_push_result; + ASSERT_TRUE(RegisterPush(®ister_push_result)); + EXPECT_EQ("AbortError - Registration failed - permission denied", + register_push_result); +} + +} // namespace gcm diff --git a/chrome/browser/services/gcm/push_messaging_constants.cc b/chrome/browser/services/gcm/push_messaging_constants.cc new file mode 100644 index 0000000..011bfa9 --- /dev/null +++ b/chrome/browser/services/gcm/push_messaging_constants.cc @@ -0,0 +1,11 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/services/gcm/push_messaging_constants.h" + +namespace gcm { + +const char kPushMessagingEndpoint[] = "https://android.googleapis.com/gcm/send"; + +} // namespace gcm diff --git a/chrome/browser/services/gcm/push_messaging_constants.h b/chrome/browser/services/gcm/push_messaging_constants.h new file mode 100644 index 0000000..3e0656a --- /dev/null +++ b/chrome/browser/services/gcm/push_messaging_constants.h @@ -0,0 +1,14 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_SERVICES_GCM_PUSH_MESSAGING_CONSTANTS_H_ +#define CHROME_BROWSER_SERVICES_GCM_PUSH_MESSAGING_CONSTANTS_H_ + +namespace gcm { + +extern const char kPushMessagingEndpoint[]; + +} // namespace gcm + +#endif // CHROME_BROWSER_SERVICES_GCM_PUSH_MESSAGING_CONSTANTS_H_ diff --git a/chrome/browser/services/gcm/push_messaging_service_impl.cc b/chrome/browser/services/gcm/push_messaging_service_impl.cc index ee49c0a..9d352eb 100644 --- a/chrome/browser/services/gcm/push_messaging_service_impl.cc +++ b/chrome/browser/services/gcm/push_messaging_service_impl.cc @@ -14,6 +14,7 @@ #include "chrome/browser/services/gcm/gcm_profile_service.h" #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" #include "chrome/browser/services/gcm/push_messaging_application_id.h" +#include "chrome/browser/services/gcm/push_messaging_constants.h" #include "chrome/browser/services/gcm/push_messaging_permission_context.h" #include "chrome/browser/services/gcm/push_messaging_permission_context_factory.h" #include "chrome/common/chrome_switches.h" @@ -129,6 +130,10 @@ void PushMessagingServiceImpl::OnMessage( } } +void PushMessagingServiceImpl::SetProfileForTesting(Profile* profile) { + profile_ = profile; +} + void PushMessagingServiceImpl::DeliverMessageCallback( const PushMessagingApplicationId& application_id, const GCMClient::IncomingMessage& message, @@ -233,7 +238,7 @@ void PushMessagingServiceImpl::RegisterEnd( const content::PushMessagingService::RegisterCallback& callback, const std::string& registration_id, content::PushRegistrationStatus status) { - GURL endpoint = GURL("https://android.googleapis.com/gcm/send"); + GURL endpoint = GURL(std::string(kPushMessagingEndpoint)); callback.Run(endpoint, registration_id, status); if (status == content::PUSH_REGISTRATION_STATUS_SUCCESS) { // TODO(johnme): Make sure the pref doesn't get out of sync after crashes. diff --git a/chrome/browser/services/gcm/push_messaging_service_impl.h b/chrome/browser/services/gcm/push_messaging_service_impl.h index b38f377..5d74a73 100644 --- a/chrome/browser/services/gcm/push_messaging_service_impl.h +++ b/chrome/browser/services/gcm/push_messaging_service_impl.h @@ -58,6 +58,8 @@ class PushMessagingServiceImpl : public content::PushMessagingService, bool user_gesture, const content::PushMessagingService::RegisterCallback& callback) override; + void SetProfileForTesting(Profile* profile); + private: void DeliverMessageCallback(const PushMessagingApplicationId& application_id, const GCMClient::IncomingMessage& message, diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index fa5d457..246a389 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1108,6 +1108,8 @@ 'browser/services/gcm/gcm_profile_service_factory.h', 'browser/services/gcm/push_messaging_application_id.cc', 'browser/services/gcm/push_messaging_application_id.h', + 'browser/services/gcm/push_messaging_constants.cc', + 'browser/services/gcm/push_messaging_constants.h', 'browser/services/gcm/push_messaging_infobar_delegate.cc', 'browser/services/gcm/push_messaging_infobar_delegate.h', 'browser/services/gcm/push_messaging_permission_context.cc', diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 2c3bcc4..fd4e170 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -526,6 +526,7 @@ 'browser/service_process/service_process_control_browsertest.cc', 'browser/services/gcm/fake_gcm_profile_service.cc', 'browser/services/gcm/fake_gcm_profile_service.h', + 'browser/services/gcm/push_messaging_browsertest.cc', 'browser/sessions/better_session_restore_browsertest.cc', 'browser/sessions/persistent_tab_restore_service_browsertest.cc', 'browser/sessions/session_restore_browsertest.cc', diff --git a/chrome/test/data/push_messaging/service_worker.js b/chrome/test/data/push_messaging/service_worker.js new file mode 100644 index 0000000..116b355 --- /dev/null +++ b/chrome/test/data/push_messaging/service_worker.js @@ -0,0 +1,7 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Empty service worker script. + +// TODO(mvanouwerkerk): Add test coverage for push event delivery. diff --git a/chrome/test/data/push_messaging/test.html b/chrome/test/data/push_messaging/test.html new file mode 100644 index 0000000..4076956 --- /dev/null +++ b/chrome/test/data/push_messaging/test.html @@ -0,0 +1,34 @@ +<!DOCTYPE html> +<html> + <head> + <title>Push API Test</title> + <script> + function sendResultToTest(result) { + console.log(result); + if (window.domAutomationController) { + domAutomationController.send('' + result); + } + } + + function sendErrorToTest(error) { + sendResultToTest(error.name + ' - ' + error.message); + } + + function registerServiceWorker() { + navigator.serviceWorker.register('service_worker.js').then(function(swRegistration) { + console.log(swRegistration); + sendResultToTest('ok'); + }, sendErrorToTest); + } + + function registerPush(senderId) { + navigator.serviceWorker.ready.then(function() { + navigator.push.register(senderId).then(function(pushRegistration) { + sendResultToTest(pushRegistration.pushEndpoint + ' - ' + pushRegistration.pushRegistrationId); + }, sendErrorToTest); + }, sendErrorToTest); + } + </script> + </head> + <body>Push API Test</body> +</html> diff --git a/net/tools/testserver/testserver.py b/net/tools/testserver/testserver.py index 4294414..2fc58cf 100755 --- a/net/tools/testserver/testserver.py +++ b/net/tools/testserver/testserver.py @@ -358,6 +358,7 @@ class TestPageHandler(testserver_base.BasePageHandler): 'gif': 'image/gif', 'jpeg' : 'image/jpeg', 'jpg' : 'image/jpeg', + 'js' : 'application/javascript', 'json': 'application/json', 'pdf' : 'application/pdf', 'txt' : 'text/plain', |