| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 define('mojo/edk/js/tests/js_to_cpp_tests', [ | |
| 6 'console', | |
| 7 'mojo/edk/js/tests/js_to_cpp.mojom', | |
| 8 'mojo/public/js/connection', | |
| 9 'mojo/public/js/connector', | |
| 10 'mojo/public/js/core', | |
| 11 ], function (console, jsToCpp, connection, connector, core) { | |
| 12 var retainedConnection; | |
| 13 var sampleData; | |
| 14 var sampleMessage; | |
| 15 var BAD_VALUE = 13; | |
| 16 var DATA_PIPE_PARAMS = { | |
| 17 flags: core.CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, | |
| 18 elementNumBytes: 1, | |
| 19 capacityNumBytes: 64 | |
| 20 }; | |
| 21 | |
| 22 function JsSideConnection(cppSide) { | |
| 23 this.cppSide_ = cppSide; | |
| 24 cppSide.startTest(); | |
| 25 } | |
| 26 | |
| 27 JsSideConnection.prototype = | |
| 28 Object.create(jsToCpp.JsSide.stubClass.prototype); | |
| 29 | |
| 30 JsSideConnection.prototype.ping = function (arg) { | |
| 31 this.cppSide_.pingResponse(); | |
| 32 }; | |
| 33 | |
| 34 JsSideConnection.prototype.echo = function (numIterations, arg) { | |
| 35 var dataPipe1; | |
| 36 var dataPipe2; | |
| 37 var i; | |
| 38 var messagePipe1; | |
| 39 var messagePipe2; | |
| 40 var specialArg; | |
| 41 | |
| 42 // Ensure expected negative values are negative. | |
| 43 if (arg.si64 > 0) | |
| 44 arg.si64 = BAD_VALUE; | |
| 45 | |
| 46 if (arg.si32 > 0) | |
| 47 arg.si32 = BAD_VALUE; | |
| 48 | |
| 49 if (arg.si16 > 0) | |
| 50 arg.si16 = BAD_VALUE; | |
| 51 | |
| 52 if (arg.si8 > 0) | |
| 53 arg.si8 = BAD_VALUE; | |
| 54 | |
| 55 for (i = 0; i < numIterations; ++i) { | |
| 56 dataPipe1 = core.createDataPipe(DATA_PIPE_PARAMS); | |
| 57 dataPipe2 = core.createDataPipe(DATA_PIPE_PARAMS); | |
| 58 messagePipe1 = core.createMessagePipe(); | |
| 59 messagePipe2 = core.createMessagePipe(); | |
| 60 | |
| 61 arg.data_handle = dataPipe1.consumerHandle; | |
| 62 arg.message_handle = messagePipe1.handle1; | |
| 63 | |
| 64 specialArg = new jsToCpp.EchoArgs(); | |
| 65 specialArg.si64 = -1; | |
| 66 specialArg.si32 = -1; | |
| 67 specialArg.si16 = -1; | |
| 68 specialArg.si8 = -1; | |
| 69 specialArg.name = 'going'; | |
| 70 specialArg.data_handle = dataPipe2.consumerHandle; | |
| 71 specialArg.message_handle = messagePipe2.handle1; | |
| 72 | |
| 73 writeDataPipe(dataPipe1, sampleData); | |
| 74 writeDataPipe(dataPipe2, sampleData); | |
| 75 writeMessagePipe(messagePipe1, sampleMessage); | |
| 76 writeMessagePipe(messagePipe2, sampleMessage); | |
| 77 | |
| 78 this.cppSide_.echoResponse(createEchoArgsList(specialArg, arg)); | |
| 79 | |
| 80 core.close(dataPipe1.producerHandle); | |
| 81 core.close(dataPipe2.producerHandle); | |
| 82 core.close(messagePipe1.handle0); | |
| 83 core.close(messagePipe2.handle0); | |
| 84 } | |
| 85 this.cppSide_.testFinished(); | |
| 86 }; | |
| 87 | |
| 88 JsSideConnection.prototype.bitFlip = function (arg) { | |
| 89 var iteration = 0; | |
| 90 var dataPipe; | |
| 91 var messagePipe; | |
| 92 var proto = connector.Connector.prototype; | |
| 93 var stopSignalled = false; | |
| 94 | |
| 95 proto.realAccept = proto.accept; | |
| 96 proto.accept = function (message) { | |
| 97 var offset = iteration / 8; | |
| 98 var mask; | |
| 99 var value; | |
| 100 if (offset < message.buffer.arrayBuffer.byteLength) { | |
| 101 mask = 1 << (iteration % 8); | |
| 102 value = message.buffer.getUint8(offset) ^ mask; | |
| 103 message.buffer.setUint8(offset, value); | |
| 104 return this.realAccept(message); | |
| 105 } | |
| 106 stopSignalled = true; | |
| 107 return false; | |
| 108 }; | |
| 109 | |
| 110 while (!stopSignalled) { | |
| 111 dataPipe = core.createDataPipe(DATA_PIPE_PARAMS); | |
| 112 messagePipe = core.createMessagePipe(); | |
| 113 writeDataPipe(dataPipe, sampleData); | |
| 114 writeMessagePipe(messagePipe, sampleMessage); | |
| 115 arg.data_handle = dataPipe.consumerHandle; | |
| 116 arg.message_handle = messagePipe.handle1; | |
| 117 | |
| 118 this.cppSide_.bitFlipResponse(createEchoArgsList(arg)); | |
| 119 | |
| 120 core.close(dataPipe.producerHandle); | |
| 121 core.close(messagePipe.handle0); | |
| 122 iteration += 1; | |
| 123 } | |
| 124 | |
| 125 proto.accept = proto.realAccept; | |
| 126 proto.realAccept = null; | |
| 127 this.cppSide_.testFinished(); | |
| 128 }; | |
| 129 | |
| 130 JsSideConnection.prototype.backPointer = function (arg) { | |
| 131 var iteration = 0; | |
| 132 var dataPipe; | |
| 133 var messagePipe; | |
| 134 var proto = connector.Connector.prototype; | |
| 135 var stopSignalled = false; | |
| 136 | |
| 137 proto.realAccept = proto.accept; | |
| 138 proto.accept = function (message) { | |
| 139 var delta = 8 * (1 + iteration % 32); | |
| 140 var offset = 8 * ((iteration / 32) | 0); | |
| 141 if (offset < message.buffer.arrayBuffer.byteLength - 4) { | |
| 142 message.buffer.dataView.setUint32(offset, 0x100000000 - delta, true); | |
| 143 message.buffer.dataView.setUint32(offset + 4, 0xffffffff, true); | |
| 144 return this.realAccept(message); | |
| 145 } | |
| 146 stopSignalled = true; | |
| 147 return false; | |
| 148 }; | |
| 149 | |
| 150 while (!stopSignalled) { | |
| 151 dataPipe = core.createDataPipe(DATA_PIPE_PARAMS); | |
| 152 messagePipe = core.createMessagePipe(); | |
| 153 writeDataPipe(dataPipe, sampleData); | |
| 154 writeMessagePipe(messagePipe, sampleMessage); | |
| 155 arg.data_handle = dataPipe.consumerHandle; | |
| 156 arg.message_handle = messagePipe.handle1; | |
| 157 | |
| 158 this.cppSide_.backPointerResponse(createEchoArgsList(arg)); | |
| 159 | |
| 160 core.close(dataPipe.producerHandle); | |
| 161 core.close(messagePipe.handle0); | |
| 162 iteration += 1; | |
| 163 } | |
| 164 | |
| 165 proto.accept = proto.realAccept; | |
| 166 proto.realAccept = null; | |
| 167 this.cppSide_.testFinished(); | |
| 168 }; | |
| 169 | |
| 170 function writeDataPipe(pipe, data) { | |
| 171 var writeResult = core.writeData( | |
| 172 pipe.producerHandle, data, core.WRITE_DATA_FLAG_ALL_OR_NONE); | |
| 173 | |
| 174 if (writeResult.result != core.RESULT_OK) { | |
| 175 console.log('ERROR: Data pipe write result was ' + writeResult.result); | |
| 176 return false; | |
| 177 } | |
| 178 if (writeResult.numBytes != data.length) { | |
| 179 console.log('ERROR: Data pipe write length was ' + writeResult.numBytes); | |
| 180 return false; | |
| 181 } | |
| 182 return true; | |
| 183 } | |
| 184 | |
| 185 function writeMessagePipe(pipe, arrayBuffer) { | |
| 186 var result = core.writeMessage(pipe.handle0, arrayBuffer, [], 0); | |
| 187 if (result != core.RESULT_OK) { | |
| 188 console.log('ERROR: Message pipe write result was ' + result); | |
| 189 return false; | |
| 190 } | |
| 191 return true; | |
| 192 } | |
| 193 | |
| 194 function createEchoArgsListElement(item, next) { | |
| 195 var list = new jsToCpp.EchoArgsList(); | |
| 196 list.item = item; | |
| 197 list.next = next; | |
| 198 return list; | |
| 199 } | |
| 200 | |
| 201 function createEchoArgsList() { | |
| 202 var genuineArray = Array.prototype.slice.call(arguments); | |
| 203 return genuineArray.reduceRight(function (previous, current) { | |
| 204 return createEchoArgsListElement(current, previous); | |
| 205 }, null); | |
| 206 } | |
| 207 | |
| 208 return function(handle) { | |
| 209 var i; | |
| 210 sampleData = new Uint8Array(DATA_PIPE_PARAMS.capacityNumBytes); | |
| 211 for (i = 0; i < sampleData.length; ++i) { | |
| 212 sampleData[i] = i; | |
| 213 } | |
| 214 sampleMessage = new Uint8Array(DATA_PIPE_PARAMS.capacityNumBytes); | |
| 215 for (i = 0; i < sampleMessage.length; ++i) { | |
| 216 sampleMessage[i] = 255 - i; | |
| 217 } | |
| 218 retainedConnection = new connection.Connection(handle, JsSideConnection, | |
| 219 jsToCpp.CppSide.proxyClass); | |
| 220 }; | |
| 221 }); | |
| OLD | NEW |