OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 function stringToArrayBuffer(str) { |
| 6 var utf8_string = unescape(encodeURIComponent(str)); |
| 7 var buffer = new ArrayBuffer(utf8_string.length); |
| 8 var view = new Uint8Array(buffer); |
| 9 for (var i = 0; i < utf8_string.length; ++i) { |
| 10 view[i] = utf8_string.charCodeAt(i); |
| 11 } |
| 12 return buffer; |
| 13 } |
| 14 |
| 15 function arrayBufferToString(buffer) { |
| 16 var utf8_string = String.fromCharCode.apply(null, new Uint8Array(buffer)); |
| 17 return decodeURIComponent(escape(utf8_string)); |
| 18 } |
| 19 |
| 20 function assertSuccess(status) { |
| 21 chrome.test.assertEq('success', status); |
| 22 chrome.test.assertNoLastError(); |
| 23 } |
| 24 |
| 25 chrome.test.listenOnce( |
| 26 chrome.copresence.onMessagesReceived, |
| 27 function(subscriptionId, messages) { |
| 28 chrome.test.assertEq('response', subscriptionId); |
| 29 chrome.test.assertEq(2, messages.length); |
| 30 chrome.test.assertEq('joke', messages[0].type); |
| 31 chrome.test.assertEq('Who\'s there?', |
| 32 arrayBufferToString(messages[0].payload)); |
| 33 }); |
| 34 |
| 35 var PUBLISH_OPERATION = { |
| 36 id: 'call', |
| 37 message: { |
| 38 type: 'joke', |
| 39 payload: stringToArrayBuffer('Knock Knock!') |
| 40 }, |
| 41 timeToLiveMillis: 1000, |
| 42 policy: {}, |
| 43 strategies: { |
| 44 onlyBroadcast: true |
| 45 } |
| 46 }; |
| 47 |
| 48 var SUBSCRIBE_OPERATION = { |
| 49 id: 'response', |
| 50 filter: { |
| 51 type: 'joke' |
| 52 }, |
| 53 timeToLiveMillis: 1000, |
| 54 strategies: { |
| 55 onlyScan: true |
| 56 } |
| 57 }; |
| 58 |
| 59 chrome.test.runTests([ |
| 60 function testPubSub() { |
| 61 chrome.copresence.execute( |
| 62 [{ publish: PUBLISH_OPERATION }], |
| 63 chrome.test.callbackPass(assertSuccess)); |
| 64 |
| 65 chrome.copresence.execute( |
| 66 [{ subscribe: SUBSCRIBE_OPERATION }], |
| 67 chrome.test.callbackPass(assertSuccess)); |
| 68 }, |
| 69 |
| 70 function testUnPubSub() { |
| 71 chrome.copresence.execute( |
| 72 [{ |
| 73 unpublish: { unpublishId: 'call' } |
| 74 }, |
| 75 { |
| 76 unsubscribe: { unsubscribeId: 'response' } |
| 77 }], |
| 78 chrome.test.callbackPass(assertSuccess)); |
| 79 }, |
| 80 |
| 81 function testBadId() { |
| 82 chrome.copresence.execute( |
| 83 [{ unsubscribe: { unsubscribeId: 'invalid' } }], |
| 84 chrome.test.callbackFail("Invalid operation in operations array.")); |
| 85 }, |
| 86 |
| 87 function testMultipleOperations() { |
| 88 chrome.copresence.execute( |
| 89 [{ |
| 90 publish: PUBLISH_OPERATION, |
| 91 subscribe: SUBSCRIBE_OPERATION |
| 92 }], |
| 93 chrome.test.callbackFail("Invalid operation in operations array.")); |
| 94 } |
| 95 ]); |
OLD | NEW |