OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 define('mojo/apps/js/test/js_to_cpp_unittest', [ | 5 define('mojo/apps/js/test/js_to_cpp_unittest', [ |
6 'console', | 6 'console', |
7 'mojo/apps/js/test/js_to_cpp.mojom', | 7 'mojo/apps/js/test/js_to_cpp.mojom', |
8 'mojo/public/js/bindings/connection', | 8 'mojo/public/js/bindings/connection', |
9 'mojo/public/js/bindings/connector', | 9 'mojo/public/js/bindings/connector', |
10 'mojo/public/js/bindings/core', | 10 'mojo/public/js/bindings/core', |
(...skipping 11 matching lines...) Expand all Loading... |
22 this.cppSide_ = cppSide; | 22 this.cppSide_ = cppSide; |
23 cppSide.startTest(); | 23 cppSide.startTest(); |
24 } | 24 } |
25 | 25 |
26 JsSideConnection.prototype = Object.create(jsToCpp.JsSideStub.prototype); | 26 JsSideConnection.prototype = Object.create(jsToCpp.JsSideStub.prototype); |
27 | 27 |
28 JsSideConnection.prototype.ping = function (arg) { | 28 JsSideConnection.prototype.ping = function (arg) { |
29 this.cppSide_.pingResponse(); | 29 this.cppSide_.pingResponse(); |
30 }; | 30 }; |
31 | 31 |
32 JsSideConnection.prototype.echo = function (numIterations, list) { | 32 JsSideConnection.prototype.echo = function (numIterations, arg) { |
33 var arg = list.item; | |
34 var dataPipe1; | 33 var dataPipe1; |
35 var dataPipe2; | 34 var dataPipe2; |
36 var i; | 35 var i; |
37 var messagePipe1; | 36 var messagePipe1; |
38 var messagePipe2; | 37 var messagePipe2; |
39 var resultList; | |
40 var specialArg; | 38 var specialArg; |
41 | 39 |
42 // Ensure expected negative values are negative. | 40 // Ensure expected negative values are negative. |
43 if (arg.si64 > 0) | 41 if (arg.si64 > 0) |
44 arg.si64 = BAD_VALUE; | 42 arg.si64 = BAD_VALUE; |
45 | 43 |
46 if (arg.si32 > 0) | 44 if (arg.si32 > 0) |
47 arg.si32 = BAD_VALUE; | 45 arg.si32 = BAD_VALUE; |
48 | 46 |
49 if (arg.si16 > 0) | 47 if (arg.si16 > 0) |
(...skipping 16 matching lines...) Expand all Loading... |
66 specialArg.si32 = -1; | 64 specialArg.si32 = -1; |
67 specialArg.si16 = -1; | 65 specialArg.si16 = -1; |
68 specialArg.si8 = -1; | 66 specialArg.si8 = -1; |
69 specialArg.name = 'going'; | 67 specialArg.name = 'going'; |
70 specialArg.data_handle = dataPipe2.consumerHandle; | 68 specialArg.data_handle = dataPipe2.consumerHandle; |
71 specialArg.message_handle = messagePipe2.handle1; | 69 specialArg.message_handle = messagePipe2.handle1; |
72 | 70 |
73 writeDataPipe(dataPipe1, senderData); | 71 writeDataPipe(dataPipe1, senderData); |
74 writeDataPipe(dataPipe2, senderData); | 72 writeDataPipe(dataPipe2, senderData); |
75 | 73 |
76 resultList = new jsToCpp.EchoArgsList(); | 74 this.cppSide_.echoResponse(createEchoArgsList(specialArg, arg)); |
77 resultList.next = new jsToCpp.EchoArgsList(); | |
78 resultList.item = specialArg; | |
79 resultList.next.item = arg; | |
80 | |
81 this.cppSide_.echoResponse(resultList); | |
82 | 75 |
83 core.close(dataPipe1.producerHandle); | 76 core.close(dataPipe1.producerHandle); |
84 core.close(dataPipe2.producerHandle); | 77 core.close(dataPipe2.producerHandle); |
85 core.close(messagePipe1.handle0); | 78 core.close(messagePipe1.handle0); |
86 core.close(messagePipe2.handle0); | 79 core.close(messagePipe2.handle0); |
87 } | 80 } |
88 this.cppSide_.testFinished(); | 81 this.cppSide_.testFinished(); |
89 }; | 82 }; |
90 | 83 |
91 JsSideConnection.prototype.bitFlip = function (arg) { | 84 JsSideConnection.prototype.bitFlip = function (arg) { |
92 var iteration = 0; | 85 var iteration = 0; |
93 var dataPipe; | 86 var dataPipe; |
94 var messagePipe; | 87 var messagePipe; |
| 88 var proto = connector.Connector.prototype; |
| 89 var resultList; |
95 var stopSignalled = false; | 90 var stopSignalled = false; |
96 var proto = connector.Connector.prototype; | |
97 | 91 |
98 proto.realAccept = proto.accept; | 92 proto.realAccept = proto.accept; |
99 proto.accept = function (message) { | 93 proto.accept = function (message) { |
100 var offset = iteration / 8; | 94 var offset = iteration / 8; |
101 var mask; | 95 var mask; |
102 var value; | 96 var value; |
103 if (offset < message.buffer.arrayBuffer.byteLength) { | 97 if (offset < message.buffer.arrayBuffer.byteLength) { |
104 mask = 1 << (iteration % 8); | 98 mask = 1 << (iteration % 8); |
105 value = message.buffer.dataView.getUint8(offset) ^ mask; | 99 value = message.buffer.dataView.getUint8(offset) ^ mask; |
106 message.buffer.dataView.setUint8(offset, value); | 100 message.buffer.dataView.setUint8(offset, value); |
107 return this.realAccept(message); | 101 return this.realAccept(message); |
108 } | 102 } |
109 stopSignalled = true; | 103 stopSignalled = true; |
110 return false; | 104 return false; |
111 }; | 105 }; |
112 | 106 |
113 while (!stopSignalled) { | 107 while (!stopSignalled) { |
114 dataPipe = core.createDataPipe(DATA_PIPE_PARAMS); | 108 dataPipe = core.createDataPipe(DATA_PIPE_PARAMS); |
115 messagePipe = core.createMessagePipe(); | 109 messagePipe = core.createMessagePipe(); |
116 writeDataPipe(dataPipe, senderData); | 110 writeDataPipe(dataPipe, senderData); |
117 arg.data_handle = dataPipe.consumerHandle; | 111 arg.data_handle = dataPipe.consumerHandle; |
118 arg.message_handle = messagePipe.handle1; | 112 arg.message_handle = messagePipe.handle1; |
119 this.cppSide_.bitFlipResponse(arg); | 113 resultList = createEchoArgsList(arg); |
| 114 this.cppSide_.bitFlipResponse(resultList); |
120 core.close(dataPipe.producerHandle); | 115 core.close(dataPipe.producerHandle); |
121 core.close(messagePipe.handle0); | 116 core.close(messagePipe.handle0); |
122 iteration += 1; | 117 iteration += 1; |
| 118 } |
| 119 |
| 120 proto.accept = proto.realAccept; |
| 121 proto.realAccept = null; |
| 122 this.cppSide_.testFinished(); |
| 123 }; |
| 124 |
| 125 JsSideConnection.prototype.backPointer = function (arg) { |
| 126 var iteration = 0; |
| 127 var dataPipe; |
| 128 var messagePipe; |
| 129 var proto = connector.Connector.prototype; |
| 130 var resultList = createEchoArgsList(arg); |
| 131 var stopSignalled = false; |
| 132 |
| 133 proto.realAccept = proto.accept; |
| 134 proto.accept = function (message) { |
| 135 var delta = 8 * (1 + iteration % 32); |
| 136 var offset = 8 * ((iteration / 32) | 0); |
| 137 if (offset < message.buffer.arrayBuffer.byteLength - 4) { |
| 138 message.buffer.dataView.setUint32(offset, 0x100000000 - delta, true); |
| 139 message.buffer.dataView.setUint32(offset + 4, 0xffffffff, true); |
| 140 return this.realAccept(message); |
| 141 } |
| 142 stopSignalled = true; |
| 143 return false; |
| 144 }; |
| 145 |
| 146 while (!stopSignalled) { |
| 147 dataPipe = core.createDataPipe(DATA_PIPE_PARAMS); |
| 148 messagePipe = core.createMessagePipe(); |
| 149 writeDataPipe(dataPipe, senderData); |
| 150 arg.data_handle = dataPipe.consumerHandle; |
| 151 arg.message_handle = messagePipe.handle1; |
| 152 this.cppSide_.backPointerResponse(resultList); |
| 153 core.close(dataPipe.producerHandle); |
| 154 core.close(messagePipe.handle0); |
| 155 iteration += 1; |
123 } | 156 } |
124 | 157 |
125 proto.accept = proto.realAccept; | 158 proto.accept = proto.realAccept; |
126 proto.realAccept = null; | 159 proto.realAccept = null; |
127 this.cppSide_.testFinished(); | 160 this.cppSide_.testFinished(); |
128 }; | 161 }; |
129 | 162 |
130 function writeDataPipe(pipe, data) { | 163 function writeDataPipe(pipe, data) { |
131 var writeResult = core.writeData( | 164 var writeResult = core.writeData( |
132 pipe.producerHandle, data, core.WRITE_DATA_FLAG_ALL_OR_NONE); | 165 pipe.producerHandle, data, core.WRITE_DATA_FLAG_ALL_OR_NONE); |
133 | 166 |
134 if (writeResult.result != core.RESULT_OK) { | 167 if (writeResult.result != core.RESULT_OK) { |
135 console.log('ERROR: Write result was ' + writeResult.result); | 168 console.log('ERROR: Write result was ' + writeResult.result); |
136 return false; | 169 return false; |
137 } | 170 } |
138 if (writeResult.numBytes != data.length) { | 171 if (writeResult.numBytes != data.length) { |
139 console.log('ERROR: Write length was ' + writeResult.numBytes); | 172 console.log('ERROR: Write length was ' + writeResult.numBytes); |
140 return false; | 173 return false; |
141 } | 174 } |
142 return true; | 175 return true; |
143 } | 176 } |
144 | 177 |
| 178 function createEchoArgsListElement(item, next) { |
| 179 var list = new jsToCpp.EchoArgsList(); |
| 180 list.item = item; |
| 181 list.next = next; |
| 182 return list; |
| 183 } |
| 184 |
| 185 function createEchoArgsList() { |
| 186 var genuineArray = Array.prototype.slice.call(arguments); |
| 187 return genuineArray.reduceRight(function (previous, current) { |
| 188 return createEchoArgsListElement(current, previous); |
| 189 }, null); |
| 190 } |
| 191 |
145 return function(handle) { | 192 return function(handle) { |
146 var i; | 193 var i; |
147 senderData = new Uint8Array(DATA_PIPE_PARAMS.capacityNumBytes); | 194 senderData = new Uint8Array(DATA_PIPE_PARAMS.capacityNumBytes); |
148 for (i = 0; i < senderData.length; ++i) | 195 for (i = 0; i < senderData.length; ++i) |
149 senderData[i] = i; | 196 senderData[i] = i; |
150 | 197 |
151 retainedConnection = new connection.Connection(handle, JsSideConnection, | 198 retainedConnection = new connection.Connection(handle, JsSideConnection, |
152 jsToCpp.CppSideProxy); | 199 jsToCpp.CppSideProxy); |
153 }; | 200 }; |
154 }); | 201 }); |
OLD | NEW |