| Index: chrome/test/data/extensions/api_test/copresence/test_copresence_api.js
|
| diff --git a/chrome/test/data/extensions/api_test/copresence/test_copresence_api.js b/chrome/test/data/extensions/api_test/copresence/test_copresence_api.js
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..056cd537c476090d23c77d20d937d62bcae02c25
|
| --- /dev/null
|
| +++ b/chrome/test/data/extensions/api_test/copresence/test_copresence_api.js
|
| @@ -0,0 +1,95 @@
|
| +// Copyright 2013 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.
|
| +
|
| +function stringToArrayBuffer(str) {
|
| + var utf8_string = unescape(encodeURIComponent(str));
|
| + var buffer = new ArrayBuffer(utf8_string.length);
|
| + var view = new Uint8Array(buffer);
|
| + for (var i = 0; i < utf8_string.length; ++i) {
|
| + view[i] = utf8_string.charCodeAt(i);
|
| + }
|
| + return buffer;
|
| +}
|
| +
|
| +function arrayBufferToString(buffer) {
|
| + var utf8_string = String.fromCharCode.apply(null, new Uint8Array(buffer));
|
| + return decodeURIComponent(escape(utf8_string));
|
| +}
|
| +
|
| +function assertSuccess(status) {
|
| + chrome.test.assertEq('success', status);
|
| + chrome.test.assertNoLastError();
|
| +}
|
| +
|
| +chrome.test.listenOnce(
|
| + chrome.copresence.onMessagesReceived,
|
| + function(subscriptionId, messages) {
|
| + chrome.test.assertEq('response', subscriptionId);
|
| + chrome.test.assertEq(2, messages.length);
|
| + chrome.test.assertEq('joke', messages[0].type);
|
| + chrome.test.assertEq('Who\'s there?',
|
| + arrayBufferToString(messages[0].payload));
|
| + });
|
| +
|
| +var PUBLISH_OPERATION = {
|
| + id: 'call',
|
| + message: {
|
| + type: 'joke',
|
| + payload: stringToArrayBuffer('Knock Knock!')
|
| + },
|
| + timeToLiveMillis: 1000,
|
| + policy: {},
|
| + strategies: {
|
| + onlyBroadcast: true
|
| + }
|
| +};
|
| +
|
| +var SUBSCRIBE_OPERATION = {
|
| + id: 'response',
|
| + filter: {
|
| + type: 'joke'
|
| + },
|
| + timeToLiveMillis: 1000,
|
| + strategies: {
|
| + onlyScan: true
|
| + }
|
| +};
|
| +
|
| +chrome.test.runTests([
|
| + function testPubSub() {
|
| + chrome.copresence.execute(
|
| + [{ publish: PUBLISH_OPERATION }],
|
| + chrome.test.callbackPass(assertSuccess));
|
| +
|
| + chrome.copresence.execute(
|
| + [{ subscribe: SUBSCRIBE_OPERATION }],
|
| + chrome.test.callbackPass(assertSuccess));
|
| + },
|
| +
|
| + function testUnPubSub() {
|
| + chrome.copresence.execute(
|
| + [{
|
| + unpublish: { unpublishId: 'call' }
|
| + },
|
| + {
|
| + unsubscribe: { unsubscribeId: 'response' }
|
| + }],
|
| + chrome.test.callbackPass(assertSuccess));
|
| + },
|
| +
|
| + function testBadId() {
|
| + chrome.copresence.execute(
|
| + [{ unsubscribe: { unsubscribeId: 'invalid' } }],
|
| + chrome.test.callbackFail("Invalid operation in operations array."));
|
| + },
|
| +
|
| + function testMultipleOperations() {
|
| + chrome.copresence.execute(
|
| + [{
|
| + publish: PUBLISH_OPERATION,
|
| + subscribe: SUBSCRIBE_OPERATION
|
| + }],
|
| + chrome.test.callbackFail("Invalid operation in operations array."));
|
| + }
|
| +]);
|
|
|