| 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/public/js/support", ["timer"], function(timer) { | 6 define("mojo/public/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; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 testWriteToClosedPipe(); | 74 testWriteToClosedPipe(); |
| 75 testRequestResponse().then(function() { | 75 testRequestResponse().then(function() { |
| 76 this.result = "PASS"; | 76 this.result = "PASS"; |
| 77 gc.collectGarbage(); // should not crash | 77 gc.collectGarbage(); // should not crash |
| 78 threading.quit(); | 78 threading.quit(); |
| 79 }.bind(this)).catch(function(e) { | 79 }.bind(this)).catch(function(e) { |
| 80 this.result = "FAIL: " + (e.stack || e); | 80 this.result = "FAIL: " + (e.stack || e); |
| 81 threading.quit(); | 81 threading.quit(); |
| 82 }.bind(this)); | 82 }.bind(this)); |
| 83 | 83 |
| 84 function createPeerConnection(handle, stubClass, proxyClass) { |
| 85 var c = new connection.Connection(handle, stubClass, proxyClass); |
| 86 c.local.peer = c.remote; |
| 87 c.remote.peer = c.local; |
| 88 return c; |
| 89 } |
| 90 |
| 84 function testClientServer() { | 91 function testClientServer() { |
| 85 var receivedFrobinate = false; | 92 var receivedFrobinate = false; |
| 86 var receivedDidFrobinate = false; | 93 var receivedDidFrobinate = false; |
| 87 | 94 |
| 88 // ServiceImpl ------------------------------------------------------------ | 95 // ServiceImpl ------------------------------------------------------------ |
| 89 | 96 |
| 90 function ServiceImpl(peer) { | 97 function ServiceImpl() { |
| 91 this.peer = peer; | |
| 92 } | 98 } |
| 93 | 99 |
| 94 ServiceImpl.prototype = Object.create( | 100 ServiceImpl.prototype = Object.create( |
| 95 sample_service.Service.stubClass.prototype); | 101 sample_service.Service.stubClass.prototype); |
| 96 | 102 |
| 97 ServiceImpl.prototype.frobinate = function(foo, baz, port) { | 103 ServiceImpl.prototype.frobinate = function(foo, baz, port) { |
| 98 receivedFrobinate = true; | 104 receivedFrobinate = true; |
| 99 | 105 |
| 100 expect(foo.name).toBe("Example name"); | 106 expect(foo.name).toBe("Example name"); |
| 101 expect(baz).toBeTruthy(); | 107 expect(baz).toBeTruthy(); |
| 102 expect(core.close(port)).toBe(core.RESULT_OK); | 108 expect(core.close(port)).toBe(core.RESULT_OK); |
| 103 | 109 |
| 104 this.peer.didFrobinate(42); | 110 this.peer.didFrobinate(42); |
| 105 }; | 111 }; |
| 106 | 112 |
| 107 // ServiceClientImpl ------------------------------------------------------ | 113 // ServiceClientImpl ------------------------------------------------------ |
| 108 | 114 |
| 109 function ServiceClientImpl(peer) { | 115 function ServiceClientImpl() { |
| 110 this.peer = peer; | |
| 111 } | 116 } |
| 112 | 117 |
| 113 ServiceClientImpl.prototype = | 118 ServiceClientImpl.prototype = |
| 114 Object.create(sample_service.ServiceClient.stubClass.prototype); | 119 Object.create(sample_service.ServiceClient.stubClass.prototype); |
| 115 | 120 |
| 116 ServiceClientImpl.prototype.didFrobinate = function(result) { | 121 ServiceClientImpl.prototype.didFrobinate = function(result) { |
| 117 receivedDidFrobinate = true; | 122 receivedDidFrobinate = true; |
| 118 | 123 |
| 119 expect(result).toBe(42); | 124 expect(result).toBe(42); |
| 120 }; | 125 }; |
| 121 | 126 |
| 122 var pipe = core.createMessagePipe(); | 127 var pipe = core.createMessagePipe(); |
| 123 var anotherPipe = core.createMessagePipe(); | 128 var anotherPipe = core.createMessagePipe(); |
| 124 var sourcePipe = core.createMessagePipe(); | 129 var sourcePipe = core.createMessagePipe(); |
| 125 | 130 |
| 126 var connection0 = new connection.Connection( | 131 var connection0 = createPeerConnection( |
| 127 pipe.handle0, ServiceImpl, sample_service.ServiceClient.proxyClass); | 132 pipe.handle0, ServiceImpl, sample_service.ServiceClient.proxyClass); |
| 128 | 133 |
| 129 var connection1 = new connection.Connection( | 134 var connection1 = createPeerConnection( |
| 130 pipe.handle1, ServiceClientImpl, sample_service.Service.proxyClass); | 135 pipe.handle1, ServiceClientImpl, sample_service.Service.proxyClass); |
| 131 | 136 |
| 132 var foo = new sample_service.Foo(); | 137 var foo = new sample_service.Foo(); |
| 133 foo.bar = new sample_service.Bar(); | 138 foo.bar = new sample_service.Bar(); |
| 134 foo.name = "Example name"; | 139 foo.name = "Example name"; |
| 135 foo.source = sourcePipe.handle0; | 140 foo.source = sourcePipe.handle0; |
| 136 connection1.remote.frobinate(foo, true, anotherPipe.handle0); | 141 connection1.remote.frobinate(foo, true, anotherPipe.handle0); |
| 137 | 142 |
| 138 mockSupport.pumpOnce(core.RESULT_OK); | 143 mockSupport.pumpOnce(core.RESULT_OK); |
| 139 | 144 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 156 expect(core.close(anotherPipe.handle1)).toBe(core.RESULT_OK); | 161 expect(core.close(anotherPipe.handle1)).toBe(core.RESULT_OK); |
| 157 | 162 |
| 158 // The Connection object is responsible for closing these handles. | 163 // The Connection object is responsible for closing these handles. |
| 159 expect(core.close(pipe.handle0)).toBe(core.RESULT_INVALID_ARGUMENT); | 164 expect(core.close(pipe.handle0)).toBe(core.RESULT_INVALID_ARGUMENT); |
| 160 expect(core.close(pipe.handle1)).toBe(core.RESULT_INVALID_ARGUMENT); | 165 expect(core.close(pipe.handle1)).toBe(core.RESULT_INVALID_ARGUMENT); |
| 161 } | 166 } |
| 162 | 167 |
| 163 function testWriteToClosedPipe() { | 168 function testWriteToClosedPipe() { |
| 164 var pipe = core.createMessagePipe(); | 169 var pipe = core.createMessagePipe(); |
| 165 | 170 |
| 166 var connection1 = new connection.Connection( | 171 var connection1 = createPeerConnection( |
| 167 pipe.handle1, function() {}, sample_service.Service.proxyClass); | 172 pipe.handle1, function() {}, sample_service.Service.proxyClass); |
| 168 | 173 |
| 169 // Close the other end of the pipe. | 174 // Close the other end of the pipe. |
| 170 core.close(pipe.handle0); | 175 core.close(pipe.handle0); |
| 171 | 176 |
| 172 // Not observed yet because we haven't pumped events yet. | 177 // Not observed yet because we haven't pumped events yet. |
| 173 expect(connection1.encounteredError()).toBeFalsy(); | 178 expect(connection1.encounteredError()).toBeFalsy(); |
| 174 | 179 |
| 175 var foo = new sample_service.Foo(); | 180 var foo = new sample_service.Foo(); |
| 176 foo.bar = new sample_service.Bar(); | 181 foo.bar = new sample_service.Bar(); |
| 177 // TODO(darin): crbug.com/357043: pass null in place of |foo| here. | 182 // TODO(darin): crbug.com/357043: pass null in place of |foo| here. |
| 178 connection1.remote.frobinate(foo, true, null); | 183 connection1.remote.frobinate(foo, true, null); |
| 179 | 184 |
| 180 // Write failures are not reported. | 185 // Write failures are not reported. |
| 181 expect(connection1.encounteredError()).toBeFalsy(); | 186 expect(connection1.encounteredError()).toBeFalsy(); |
| 182 | 187 |
| 183 // Pump events, and then we should start observing the closed pipe. | 188 // Pump events, and then we should start observing the closed pipe. |
| 184 mockSupport.pumpOnce(core.RESULT_OK); | 189 mockSupport.pumpOnce(core.RESULT_OK); |
| 185 | 190 |
| 186 expect(connection1.encounteredError()).toBeTruthy(); | 191 expect(connection1.encounteredError()).toBeTruthy(); |
| 187 | 192 |
| 188 connection1.close(); | 193 connection1.close(); |
| 189 } | 194 } |
| 190 | 195 |
| 191 function testRequestResponse() { | 196 function testRequestResponse() { |
| 192 | 197 |
| 193 // ProviderImpl ------------------------------------------------------------ | 198 // ProviderImpl ------------------------------------------------------------ |
| 194 | 199 |
| 195 function ProviderImpl(peer) { | 200 function ProviderImpl() { |
| 196 this.peer = peer; | |
| 197 } | 201 } |
| 198 | 202 |
| 199 ProviderImpl.prototype = | 203 ProviderImpl.prototype = |
| 200 Object.create(sample_interfaces.Provider.stubClass.prototype); | 204 Object.create(sample_interfaces.Provider.stubClass.prototype); |
| 201 | 205 |
| 202 ProviderImpl.prototype.echoString = function(a) { | 206 ProviderImpl.prototype.echoString = function(a) { |
| 203 mockSupport.queuePump(core.RESULT_OK); | 207 mockSupport.queuePump(core.RESULT_OK); |
| 204 return Promise.resolve({a: a}); | 208 return Promise.resolve({a: a}); |
| 205 }; | 209 }; |
| 206 | 210 |
| 207 ProviderImpl.prototype.echoStrings = function(a, b) { | 211 ProviderImpl.prototype.echoStrings = function(a, b) { |
| 208 mockSupport.queuePump(core.RESULT_OK); | 212 mockSupport.queuePump(core.RESULT_OK); |
| 209 return Promise.resolve({a: a, b: b}); | 213 return Promise.resolve({a: a, b: b}); |
| 210 }; | 214 }; |
| 211 | 215 |
| 212 // ProviderClientImpl ------------------------------------------------------ | 216 // ProviderClientImpl ------------------------------------------------------ |
| 213 | 217 |
| 214 function ProviderClientImpl(peer) { | 218 function ProviderClientImpl() { |
| 215 this.peer = peer; | |
| 216 } | 219 } |
| 217 | 220 |
| 218 ProviderClientImpl.prototype = | 221 ProviderClientImpl.prototype = |
| 219 Object.create(sample_interfaces.ProviderClient.stubClass.prototype); | 222 Object.create(sample_interfaces.ProviderClient.stubClass.prototype); |
| 220 | 223 |
| 221 var pipe = core.createMessagePipe(); | 224 var pipe = core.createMessagePipe(); |
| 222 | 225 |
| 223 var connection0 = new connection.Connection( | 226 var connection0 = createPeerConnection( |
| 224 pipe.handle0, | 227 pipe.handle0, |
| 225 ProviderImpl, | 228 ProviderImpl, |
| 226 sample_interfaces.ProviderClient.proxyClass); | 229 sample_interfaces.ProviderClient.proxyClass); |
| 227 | 230 |
| 228 var connection1 = new connection.Connection( | 231 var connection1 = createPeerConnection( |
| 229 pipe.handle1, | 232 pipe.handle1, |
| 230 ProviderClientImpl, | 233 ProviderClientImpl, |
| 231 sample_interfaces.Provider.proxyClass); | 234 sample_interfaces.Provider.proxyClass); |
| 232 | 235 |
| 233 var origReadMessage = core.readMessage; | 236 var origReadMessage = core.readMessage; |
| 234 // echoString | 237 // echoString |
| 235 mockSupport.queuePump(core.RESULT_OK); | 238 mockSupport.queuePump(core.RESULT_OK); |
| 236 return connection1.remote.echoString("hello").then(function(response) { | 239 return connection1.remote.echoString("hello").then(function(response) { |
| 237 expect(response.a).toBe("hello"); | 240 expect(response.a).toBe("hello"); |
| 238 }).then(function() { | 241 }).then(function() { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 252 }).then(function() { | 255 }).then(function() { |
| 253 throw Error("Expected echoString to fail."); | 256 throw Error("Expected echoString to fail."); |
| 254 }, function(error) { | 257 }, function(error) { |
| 255 expect(error.message).toBe("Connection error: " + core.RESULT_UNKNOWN); | 258 expect(error.message).toBe("Connection error: " + core.RESULT_UNKNOWN); |
| 256 | 259 |
| 257 // Clean up. | 260 // Clean up. |
| 258 core.readMessage = origReadMessage; | 261 core.readMessage = origReadMessage; |
| 259 }); | 262 }); |
| 260 } | 263 } |
| 261 }); | 264 }); |
| OLD | NEW |