2 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
9 const testSubchannelId = 1;
10 const randomPayload = String(Math.random());
12 // As part of the canary test, verify that we got what we expected.
13 function verifyDetails(details) {
14 chrome.test.assertEq(testSubchannelId, details.subchannelId);
15 chrome.test.assertEq(randomPayload, details.payload);
18 function startTestWithCredentials(paramClientId, paramClientSecret,
20 var callbackPassFunc = chrome.test.callbackPass(verifyDetails);
21 chrome.pushMessaging.onMessage.addListener(
23 // We need to allow time for the invalidation service to ACK before we
25 window.setTimeout(callbackPassFunc(details), 2000);
28 clientId = paramClientId;
29 clientSecret = paramClientSecret;
30 refreshToken = paramRefreshToken;
31 console.log("Getting channel Id");
32 // Start by getting the channel Id, the callback will continue the test.
33 chrome.pushMessaging.getChannelId(
34 chrome.test.callbackPass(getChannelIdCallback));
37 // Once we get the channel ID, start a push.
38 function getChannelIdCallback(details) {
39 console.log("channelId callback arrived, channel Id is '" +
40 details.channelId + "'");
41 var channelId = details.channelId;
42 if ("" == channelId) {
43 chrome.test.fail("No channelId, test failed.");
45 getAccessToken(channelId);
49 // This function will go to the server and ask it to send us
50 // a push message so we can test the round trip. The first stage
51 // in our mission is to get an access token to use to post the
53 function getAccessToken(channelId) {
54 var tokenRequest = new XMLHttpRequest();
55 var tokenUrl = 'https://accounts.google.com/o/oauth2/token';
56 tokenRequest.open('POST', tokenUrl, true);
57 tokenRequest.setRequestHeader('Content-Type',
58 'application/x-www-form-urlencoded');
59 var tokenData = 'client_secret=' + clientSecret + '&' +
60 'grant_type=refresh_token&' +
61 'refresh_token=' + refreshToken + '&' +
62 'client_id=' + clientId;
63 tokenRequest.onreadystatechange = function () {
64 if (tokenRequest.readyState === 4) {
65 if (tokenRequest.status === 200) {
66 console.log("First XHR returned, " + tokenRequest.response);
68 // Parse the access token out of the XHR message.
69 var parsedResponse = JSON.parse(tokenRequest.response);
70 var accessToken = parsedResponse.access_token;
72 askServerToSendPushMessageWithToken(accessToken, channelId);
74 console.log('Error sending first XHR, status is ' +
75 tokenRequest.statusText);
80 // Send the XHR with the data we need.
81 console.log("Sending first XHR, data is " + tokenData);
82 tokenRequest.send(tokenData);
85 // Now that we have an access token, use it to send the message.
86 function askServerToSendPushMessageWithToken(accessToken, channelId) {
87 // Setup the push request, using the access token we just got.
89 var pushURL ='https://www.googleapis.com/gcm_for_chrome/v1/messages';
90 var pushData = { "channelId": channelId, "subchannelId": testSubchannelId,
91 "payload": randomPayload};
92 var pushRequest = new XMLHttpRequest();
93 pushRequest.open('POST', pushURL, true);
94 // Set the headers for the push request, including the parsed accessToken.
95 pushRequest.setRequestHeader('Authorization', 'Bearer ' + accessToken);
96 pushRequest.setRequestHeader('Content-Type', 'application/json');
97 pushRequest.onreadystatechange = function () {
98 if (pushRequest.readyState === 4) {
99 if (pushRequest.status >= 200 && pushRequest.status <= 299) {
100 console.log("second XHR returned, " + pushRequest.response +
101 " status is " + pushRequest.status);
103 console.log('Error sending second XHR, status was ' +
104 pushRequest.status + ' ' +
105 pushRequest.statusText + ', body is ' +
106 pushRequest.response);
111 // Send the push request.
112 console.log("sending second XHR, data is " + JSON.stringify(pushData) +
113 ", and url is " + pushURL);
114 pushRequest.send(JSON.stringify(pushData));