| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 // Mock out the support module to avoid depending on the message loop. | 5 // Mock out the support module to avoid depending on the message loop. |
| 6 define("mojo/bindings/js/support", function() { | 6 define("mojo/bindings/js/support", ["timer"], function(timer) { |
| 7 var waitingCallbacks = []; | 7 var waitingCallbacks = []; |
| 8 | 8 |
| 9 function WaitCookie(id) { | 9 function WaitCookie(id) { |
| 10 this.id = id; | 10 this.id = id; |
| 11 } | 11 } |
| 12 | 12 |
| 13 function asyncWait(handle, flags, callback) { | 13 function asyncWait(handle, flags, callback) { |
| 14 var id = waitingCallbacks.length; | 14 var id = waitingCallbacks.length; |
| 15 waitingCallbacks.push(callback); | 15 waitingCallbacks.push(callback); |
| 16 return new WaitCookie(id); | 16 return new WaitCookie(id); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 function pumpOnce(result) { | 32 function pumpOnce(result) { |
| 33 var callbacks = waitingCallbacks; | 33 var callbacks = waitingCallbacks; |
| 34 waitingCallbacks = []; | 34 waitingCallbacks = []; |
| 35 for (var i = 0; i < callbacks.length; ++i) { | 35 for (var i = 0; i < callbacks.length; ++i) { |
| 36 if (callbacks[i]) | 36 if (callbacks[i]) |
| 37 callbacks[i](result); | 37 callbacks[i](result); |
| 38 } | 38 } |
| 39 } | 39 } |
| 40 | 40 |
| 41 // Queue up a pumpOnce call to execute after the stack unwinds. Use |
| 42 // this to trigger a pump after all Promises are executed. |
| 43 function queuePump(result) { |
| 44 timer.createOneShot(0, pumpOnce.bind(undefined, result)); |
| 45 } |
| 46 |
| 41 var exports = {}; | 47 var exports = {}; |
| 42 exports.asyncWait = asyncWait; | 48 exports.asyncWait = asyncWait; |
| 43 exports.cancelWait = cancelWait; | 49 exports.cancelWait = cancelWait; |
| 44 exports.numberOfWaitingCallbacks = numberOfWaitingCallbacks; | 50 exports.numberOfWaitingCallbacks = numberOfWaitingCallbacks; |
| 45 exports.pumpOnce = pumpOnce; | 51 exports.pumpOnce = pumpOnce; |
| 52 exports.queuePump = queuePump; |
| 46 return exports; | 53 return exports; |
| 47 }); | 54 }); |
| 48 | 55 |
| 49 define([ | 56 define([ |
| 50 "gin/test/expect", | 57 "gin/test/expect", |
| 51 "mojo/bindings/js/support", | 58 "mojo/bindings/js/support", |
| 52 "mojo/bindings/js/core", | 59 "mojo/bindings/js/core", |
| 53 "mojo/public/js/bindings/connection", | 60 "mojo/public/js/bindings/connection", |
| 54 "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom", | 61 "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom", |
| 55 "mojo/public/interfaces/bindings/tests/sample_service.mojom", | 62 "mojo/public/interfaces/bindings/tests/sample_service.mojom", |
| 63 "mojo/apps/js/bindings/threading", |
| 56 "gc", | 64 "gc", |
| 57 ], function(expect, | 65 ], function(expect, |
| 58 mockSupport, | 66 mockSupport, |
| 59 core, | 67 core, |
| 60 connection, | 68 connection, |
| 61 sample_interfaces, | 69 sample_interfaces, |
| 62 sample_service, | 70 sample_service, |
| 71 threading, |
| 63 gc) { | 72 gc) { |
| 64 testClientServer(); | 73 testClientServer(); |
| 65 testWriteToClosedPipe(); | 74 testWriteToClosedPipe(); |
| 66 testRequestResponse(); | 75 testRequestResponse().then(function() { |
| 67 this.result = "PASS"; | 76 this.result = "PASS"; |
| 68 gc.collectGarbage(); // should not crash | 77 gc.collectGarbage(); // should not crash |
| 78 threading.quit(); |
| 79 }.bind(this)).catch(function(e) { |
| 80 this.result = "FAIL: " + (e.stack || e); |
| 81 threading.quit(); |
| 82 }.bind(this)); |
| 69 | 83 |
| 70 function testClientServer() { | 84 function testClientServer() { |
| 71 var receivedFrobinate = false; | 85 var receivedFrobinate = false; |
| 72 var receivedDidFrobinate = false; | 86 var receivedDidFrobinate = false; |
| 73 | 87 |
| 74 // ServiceImpl ------------------------------------------------------------- | 88 // ServiceImpl ------------------------------------------------------------- |
| 75 | 89 |
| 76 function ServiceImpl(peer) { | 90 function ServiceImpl(peer) { |
| 77 this.peer = peer; | 91 this.peer = peer; |
| 78 } | 92 } |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 | 191 |
| 178 // ProviderImpl ------------------------------------------------------------ | 192 // ProviderImpl ------------------------------------------------------------ |
| 179 | 193 |
| 180 function ProviderImpl(peer) { | 194 function ProviderImpl(peer) { |
| 181 this.peer = peer; | 195 this.peer = peer; |
| 182 } | 196 } |
| 183 | 197 |
| 184 ProviderImpl.prototype = | 198 ProviderImpl.prototype = |
| 185 Object.create(sample_interfaces.ProviderStub.prototype); | 199 Object.create(sample_interfaces.ProviderStub.prototype); |
| 186 | 200 |
| 187 ProviderImpl.prototype.echoString = function(a, callback) { | 201 ProviderImpl.prototype.echoString = function(a) { |
| 188 callback(a); | 202 mockSupport.queuePump(core.RESULT_OK); |
| 203 return Promise.resolve({a: a}); |
| 189 }; | 204 }; |
| 190 | 205 |
| 191 ProviderImpl.prototype.echoStrings = function(a, b, callback) { | 206 ProviderImpl.prototype.echoStrings = function(a, b) { |
| 192 callback(a, b); | 207 mockSupport.queuePump(core.RESULT_OK); |
| 208 return Promise.resolve({a: a, b: b}); |
| 193 }; | 209 }; |
| 194 | 210 |
| 195 // ProviderClientImpl ------------------------------------------------------ | 211 // ProviderClientImpl ------------------------------------------------------ |
| 196 | 212 |
| 197 function ProviderClientImpl(peer) { | 213 function ProviderClientImpl(peer) { |
| 198 this.peer = peer; | 214 this.peer = peer; |
| 199 } | 215 } |
| 200 | 216 |
| 201 ProviderClientImpl.prototype = | 217 ProviderClientImpl.prototype = |
| 202 Object.create(sample_interfaces.ProviderClientStub.prototype); | 218 Object.create(sample_interfaces.ProviderClientStub.prototype); |
| 203 | 219 |
| 204 ProviderClientImpl.prototype.didFrobinate = function(result) { | |
| 205 receivedDidFrobinate = true; | |
| 206 | |
| 207 expect(result).toBe(42); | |
| 208 }; | |
| 209 | |
| 210 var pipe = core.createMessagePipe(); | 220 var pipe = core.createMessagePipe(); |
| 211 | 221 |
| 212 var connection0 = new connection.Connection( | 222 var connection0 = new connection.Connection( |
| 213 pipe.handle0, ProviderImpl, sample_interfaces.ProviderClientProxy); | 223 pipe.handle0, ProviderImpl, sample_interfaces.ProviderClientProxy); |
| 214 | 224 |
| 215 var connection1 = new connection.Connection( | 225 var connection1 = new connection.Connection( |
| 216 pipe.handle1, ProviderClientImpl, sample_interfaces.ProviderProxy); | 226 pipe.handle1, ProviderClientImpl, sample_interfaces.ProviderProxy); |
| 217 | 227 |
| 218 var echoedString; | |
| 219 | |
| 220 // echoString | 228 // echoString |
| 221 | 229 mockSupport.queuePump(core.RESULT_OK); |
| 222 connection1.remote.echoString("hello", function(a) { | 230 return connection1.remote.echoString("hello").then(function(response) { |
| 223 echoedString = a; | 231 expect(response.a).toBe("hello"); |
| 232 }).then(function() { |
| 233 // echoStrings |
| 234 mockSupport.queuePump(core.RESULT_OK); |
| 235 return connection1.remote.echoStrings("hello", "world"); |
| 236 }).then(function(response) { |
| 237 expect(response.a).toBe("hello"); |
| 238 expect(response.b).toBe("world"); |
| 224 }); | 239 }); |
| 225 | |
| 226 mockSupport.pumpOnce(core.RESULT_OK); | |
| 227 | |
| 228 expect(echoedString).toBe("hello"); | |
| 229 | |
| 230 // echoStrings | |
| 231 | |
| 232 connection1.remote.echoStrings("hello", "world", function(a, b) { | |
| 233 echoedString = a + " " + b; | |
| 234 }); | |
| 235 | |
| 236 mockSupport.pumpOnce(core.RESULT_OK); | |
| 237 | |
| 238 expect(echoedString).toBe("hello world"); | |
| 239 } | 240 } |
| 240 }); | 241 }); |
| OLD | NEW |