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