Commit 092dc8bd authored by mvanouwerkerk's avatar mvanouwerkerk Committed by Commit bot

BrowserTest for delivering push message to service worker.

BUG=350377

Review URL: https://codereview.chromium.org/673783003

Cr-Commit-Position: refs/heads/master@{#301376}
parent 75a84f8a
......@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <map>
#include <string>
#include "base/bind.h"
......@@ -17,6 +18,7 @@
#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/gcm_driver/gcm_client.h"
#include "components/infobars/core/confirm_infobar_delegate.h"
#include "components/infobars/core/infobar.h"
#include "components/infobars/core/infobar_manager.h"
......@@ -104,12 +106,16 @@ class PushMessagingBrowserTest : public InProcessBrowserTest {
browser()->profile(), &FakeGCMProfileService::Build));
gcm_service_->set_collect(true);
ui_test_utils::NavigateToURL(
browser(), https_server_->GetURL("files/push_messaging/test.html"));
loadTestPage();
InProcessBrowserTest::SetUpOnMainThread();
}
void loadTestPage() {
ui_test_utils::NavigateToURL(
browser(), https_server_->GetURL("files/push_messaging/test.html"));
}
bool RunScript(const std::string& script, std::string* result) {
return content::ExecuteScriptAndExtractString(
browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(),
......@@ -117,18 +123,15 @@ class PushMessagingBrowserTest : public InProcessBrowserTest {
result);
}
bool RegisterServiceWorker(std::string* result) {
return RunScript("registerServiceWorker()", result);
}
bool RegisterPush(std::string* result) {
return RunScript("registerPush()", result);
}
net::SpawnedTestServer* https_server() const { return https_server_.get(); }
FakeGCMProfileService* gcm_service() const { return gcm_service_; }
PushMessagingServiceImpl* push_service() {
return static_cast<PushMessagingServiceImpl*>(
gcm_service_->push_messaging_service());
}
private:
scoped_ptr<net::SpawnedTestServer> https_server_;
FakeGCMProfileService* gcm_service_;
......@@ -137,32 +140,64 @@ class PushMessagingBrowserTest : public InProcessBrowserTest {
};
IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterSuccess) {
std::string register_worker_result;
ASSERT_TRUE(RegisterServiceWorker(&register_worker_result));
ASSERT_EQ("ok", register_worker_result);
std::string script_result;
ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
ASSERT_EQ("ok - service worker registered", script_result);
InfoBarResponder accepting_responder(browser(), true);
std::string register_push_result;
ASSERT_TRUE(RegisterPush(&register_push_result));
EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1", register_push_result);
ASSERT_TRUE(RunScript("registerPush()", &script_result));
EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1", script_result);
PushMessagingApplicationId expected_id(https_server()->GetURL(""), 0L);
EXPECT_EQ(expected_id.ToString(), gcm_service()->last_registered_app_id());
PushMessagingApplicationId app_id(https_server()->GetURL(""), 0L);
EXPECT_EQ(app_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(&register_worker_result));
ASSERT_EQ("ok", register_worker_result);
std::string script_result;
ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
ASSERT_EQ("ok - service worker registered", script_result);
InfoBarResponder cancelling_responder(browser(), false);
std::string register_push_result;
ASSERT_TRUE(RegisterPush(&register_push_result));
ASSERT_TRUE(RunScript("registerPush()", &script_result));
EXPECT_EQ("AbortError - Registration failed - permission denied",
register_push_result);
script_result);
}
IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventSuccess) {
std::string script_result;
ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
ASSERT_EQ("ok - service worker registered", script_result);
InfoBarResponder accepting_responder(browser(), true);
ASSERT_TRUE(RunScript("registerPush()", &script_result));
EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1", script_result);
PushMessagingApplicationId app_id(https_server()->GetURL(""), 0L);
EXPECT_EQ(app_id.ToString(), gcm_service()->last_registered_app_id());
EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
ASSERT_TRUE(RunScript("isControlled()", &script_result));
ASSERT_EQ("false - is not controlled", script_result);
loadTestPage(); // Reload to become controlled.
ASSERT_TRUE(RunScript("isControlled()", &script_result));
ASSERT_EQ("true - is controlled", script_result);
GCMClient::IncomingMessage message;
GCMClient::MessageData messageData;
messageData.insert(std::pair<std::string, std::string>("data", "testdata"));
message.data = messageData;
push_service()->OnMessage(app_id.ToString(), message);
ASSERT_TRUE(RunScript("pushData.get()", &script_result));
EXPECT_EQ("testdata", script_result);
}
} // namespace gcm
......@@ -2,6 +2,20 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Empty service worker script.
this.onpush = function(event) {
sendMessageToClients('push', event.data);
};
// TODO(mvanouwerkerk): Add test coverage for push event delivery.
function sendMessageToClients(type, data) {
var message = JSON.stringify({
'type': type,
'data': data
});
clients.getAll().then(function(clients) {
clients.forEach(function(client) {
client.postMessage(message);
});
}, function(error) {
console.log(error);
});
}
......@@ -4,8 +4,13 @@
<title>Push API Test</title>
<link rel="manifest" href="manifest.json">
<script>
var pushData = new FutureData();
// Sends data back to the test. This must be in response to an earlier
// request, but it's ok to respond asynchronously. The request blocks until
// the response is sent.
function sendResultToTest(result) {
console.log(result);
console.log('sendResultToTest: ' + result);
if (window.domAutomationController) {
domAutomationController.send('' + result);
}
......@@ -15,21 +20,67 @@
sendResultToTest(error.name + ' - ' + error.message);
}
// A container for a single piece of data. The data does not have to be
// available yet when the getter is called, as all responses to the test are
// asynchronous.
function FutureData() {
this.data = null;
this.waiting = false;
}
// Sends the data to the test if it is available. Otherwise sets the
// |waiting| flag.
FutureData.prototype.get = function() {
if (this.data) {
sendResultToTest(this.data);
} else {
this.waiting = true;
}
};
// Sets a new data value. If the |waiting| flag is on, it is turned off and
// the data is sent to the test.
FutureData.prototype.set = function(data) {
this.data = data;
if (this.waiting) {
sendResultToTest(data);
this.waiting = false;
}
};
function registerServiceWorker() {
navigator.serviceWorker.register('service_worker.js', {scope: './'}).then(function(swRegistration) {
console.log(swRegistration);
sendResultToTest('ok');
}, sendErrorToTest);
navigator.serviceWorker.register('service_worker.js', {scope: './'}).then(
function(swRegistration) {
sendResultToTest('ok - service worker registered');
}, sendErrorToTest);
}
function registerPush() {
navigator.serviceWorker.ready.then(function() {
navigator.push.register().then(function(pushRegistration) {
sendResultToTest(pushRegistration.pushEndpoint + ' - ' + pushRegistration.pushRegistrationId);
sendResultToTest(pushRegistration.pushEndpoint + ' - ' +
pushRegistration.pushRegistrationId);
}, sendErrorToTest);
}, sendErrorToTest);
}
function isControlled() {
if (navigator.serviceWorker.controller) {
sendResultToTest('true - is controlled');
} else {
sendResultToTest('false - is not controlled');
}
}
addEventListener('message', function(event) {
var message = JSON.parse(event.data);
if (message.type == 'push') {
pushData.set(message.data);
}
}, false);
</script>
</head>
<body>Push API Test</body>
<body>
<h1>Push API Test</h1>
</body>
</html>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment