Chromium Code Reviews| 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 | |
| 3 // that can be 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.copresence.onMessagesReceived.addListener( | |
| 26 function(subscriptionId, messages) { | |
| 27 chrome.test.assertEq('response', subscriptionId); | |
| 28 chrome.test.assertEq(2, messages.length); | |
| 29 chrome.test.assertEq('joke', messages[0].type); | |
| 30 chrome.test.assertEq('Who\'s there?', | |
| 31 arrayBufferToString(messages[0].payload)); | |
| 32 chrome.test.succeed(); | |
| 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 assertSuccess); | |
|
rkc
2014/08/07 08:56:57
kalman@ already mentioned that you need callbackPa
Charlie
2014/08/07 22:24:15
Yeah I didn't understand the conventions for wrapp
| |
| 64 | |
| 65 chrome.copresence.execute( | |
| 66 [{ subscribe: SUBSCRIBE_OPERATION }], | |
| 67 assertSuccess); | |
|
not at google - send to devlin
2014/08/06 21:45:27
2 general comments:
- it'd be good to enforce som
Charlie
2014/08/07 22:24:15
Done. I think.
| |
| 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 |