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