| 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 define([ | 5 define([ |
| 6 "gin/test/expect", | 6 "gin/test/expect", |
| 7 "mojo/public/js/connection", | 7 "mojo/public/js/bindings", |
| 8 "mojo/public/js/core", | 8 "mojo/public/js/core", |
| 9 "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom", | 9 "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom", |
| 10 "mojo/public/interfaces/bindings/tests/sample_service.mojom", | 10 "mojo/public/interfaces/bindings/tests/sample_service.mojom", |
| 11 "mojo/public/js/threading", | 11 "mojo/public/js/threading", |
| 12 "gc", | 12 "gc", |
| 13 ], function(expect, | 13 ], function(expect, |
| 14 connection, | 14 bindings, |
| 15 core, | 15 core, |
| 16 sample_interfaces, | 16 sample_interfaces, |
| 17 sample_service, | 17 sample_service, |
| 18 threading, | 18 threading, |
| 19 gc) { | 19 gc) { |
| 20 testClientServer() | 20 testClientServer() |
| 21 .then(testWriteToClosedPipe) | 21 .then(testWriteToClosedPipe) |
| 22 .then(testRequestResponse) | 22 .then(testRequestResponse) |
| 23 .then(function() { | 23 .then(function() { |
| 24 this.result = "PASS"; | 24 this.result = "PASS"; |
| 25 gc.collectGarbage(); // should not crash | 25 gc.collectGarbage(); // should not crash |
| 26 threading.quit(); | 26 threading.quit(); |
| 27 }.bind(this)).catch(function(e) { | 27 }.bind(this)).catch(function(e) { |
| 28 this.result = "FAIL: " + (e.stack || e); | 28 this.result = "FAIL: " + (e.stack || e); |
| 29 threading.quit(); | 29 threading.quit(); |
| 30 }.bind(this)); | 30 }.bind(this)); |
| 31 | 31 |
| 32 function testClientServer() { | 32 function testClientServer() { |
| 33 // ServiceImpl ------------------------------------------------------------ | 33 // ServiceImpl ------------------------------------------------------------ |
| 34 | 34 |
| 35 function ServiceImpl() { | 35 function ServiceImpl() { |
| 36 } | 36 } |
| 37 | 37 |
| 38 ServiceImpl.prototype = Object.create( | |
| 39 sample_service.Service.stubClass.prototype); | |
| 40 | |
| 41 ServiceImpl.prototype.frobinate = function(foo, baz, port) { | 38 ServiceImpl.prototype.frobinate = function(foo, baz, port) { |
| 42 expect(foo.name).toBe("Example name"); | 39 expect(foo.name).toBe("Example name"); |
| 43 expect(baz).toBe(sample_service.Service.BazOptions.REGULAR); | 40 expect(baz).toBe(sample_service.Service.BazOptions.REGULAR); |
| 44 expect(core.close(port)).toBe(core.RESULT_OK); | 41 expect(port.ptr.isBound()).toBeTruthy(); |
| 42 port.ptr.reset(); |
| 45 | 43 |
| 46 return Promise.resolve({result: 42}); | 44 return Promise.resolve({result: 42}); |
| 47 }; | 45 }; |
| 48 | 46 |
| 49 var pipe = core.createMessagePipe(); | 47 var service = new sample_service.ServicePtr(); |
| 50 var anotherPipe = core.createMessagePipe(); | 48 var serviceBinding = new bindings.Binding(sample_service.Service, |
| 49 new ServiceImpl(), |
| 50 bindings.makeRequest(service)); |
| 51 var sourcePipe = core.createMessagePipe(); | 51 var sourcePipe = core.createMessagePipe(); |
| 52 | 52 var port = new sample_service.PortPtr(); |
| 53 var connection0 = new connection.Connection(pipe.handle0, ServiceImpl); | 53 var portRequest = bindings.makeRequest(port); |
| 54 | |
| 55 var connection1 = new connection.Connection( | |
| 56 pipe.handle1, undefined, sample_service.Service.proxyClass); | |
| 57 | 54 |
| 58 var foo = new sample_service.Foo(); | 55 var foo = new sample_service.Foo(); |
| 59 foo.bar = new sample_service.Bar(); | 56 foo.bar = new sample_service.Bar(); |
| 60 foo.name = "Example name"; | 57 foo.name = "Example name"; |
| 61 foo.source = sourcePipe.handle0; | 58 foo.source = sourcePipe.handle0; |
| 62 var promise = connection1.remote.frobinate( | 59 var promise = service.frobinate( |
| 63 foo, sample_service.Service.BazOptions.REGULAR, anotherPipe.handle0) | 60 foo, sample_service.Service.BazOptions.REGULAR, port) |
| 64 .then(function(response) { | 61 .then(function(response) { |
| 65 expect(response.result).toBe(42); | 62 expect(response.result).toBe(42); |
| 66 | 63 |
| 67 connection0.close(); | 64 service.ptr.reset(); |
| 68 connection1.close(); | 65 serviceBinding.close(); |
| 69 | 66 |
| 70 return Promise.resolve(); | 67 return Promise.resolve(); |
| 71 }); | 68 }); |
| 72 | 69 |
| 73 // sourcePipe.handle1 hasn't been closed yet. | 70 // sourcePipe.handle1 hasn't been closed yet. |
| 74 expect(core.close(sourcePipe.handle1)).toBe(core.RESULT_OK); | 71 expect(core.close(sourcePipe.handle1)).toBe(core.RESULT_OK); |
| 75 | 72 |
| 76 // anotherPipe.handle1 hasn't been closed yet. | 73 // portRequest.handle hasn't been closed yet. |
| 77 expect(core.close(anotherPipe.handle1)).toBe(core.RESULT_OK); | 74 expect(core.close(portRequest.handle)).toBe(core.RESULT_OK); |
| 78 | 75 |
| 79 return promise; | 76 return promise; |
| 80 } | 77 } |
| 81 | 78 |
| 82 function testWriteToClosedPipe() { | 79 function testWriteToClosedPipe() { |
| 83 var pipe = core.createMessagePipe(); | 80 var service = new sample_service.ServicePtr(); |
| 84 var anotherPipe = core.createMessagePipe(); | 81 // Discard the interface request. |
| 82 bindings.makeRequest(service); |
| 85 | 83 |
| 86 var connection1 = new connection.Connection( | 84 var promise = service.frobinate( |
| 87 pipe.handle1, function() {}, sample_service.Service.proxyClass); | 85 null, sample_service.Service.BazOptions.REGULAR, null) |
| 88 | |
| 89 // Close the other end of the pipe. | |
| 90 core.close(pipe.handle0); | |
| 91 | |
| 92 // Not observed yet because we haven't pumped events yet. | |
| 93 expect(connection1.encounteredError()).toBeFalsy(); | |
| 94 | |
| 95 var promise = connection1.remote.frobinate( | |
| 96 null, sample_service.Service.BazOptions.REGULAR, anotherPipe.handle0) | |
| 97 .then(function(response) { | 86 .then(function(response) { |
| 98 return Promise.reject("Unexpected response"); | 87 return Promise.reject("Unexpected response"); |
| 99 }).catch(function(e) { | 88 }).catch(function(e) { |
| 100 // We should observe the closed pipe. | 89 // We should observe the closed pipe. |
| 101 expect(connection1.encounteredError()).toBeTruthy(); | |
| 102 return Promise.resolve(); | 90 return Promise.resolve(); |
| 103 }); | 91 }); |
| 104 | 92 |
| 105 // Write failures are not reported. | |
| 106 expect(connection1.encounteredError()).toBeFalsy(); | |
| 107 | |
| 108 return promise; | 93 return promise; |
| 109 } | 94 } |
| 110 | 95 |
| 111 function testRequestResponse() { | 96 function testRequestResponse() { |
| 112 // ProviderImpl ------------------------------------------------------------ | 97 // ProviderImpl ------------------------------------------------------------ |
| 113 | 98 |
| 114 function ProviderImpl() { | 99 function ProviderImpl() { |
| 115 } | 100 } |
| 116 | 101 |
| 117 ProviderImpl.prototype = | |
| 118 Object.create(sample_interfaces.Provider.stubClass.prototype); | |
| 119 | |
| 120 ProviderImpl.prototype.echoString = function(a) { | 102 ProviderImpl.prototype.echoString = function(a) { |
| 121 return Promise.resolve({a: a}); | 103 return Promise.resolve({a: a}); |
| 122 }; | 104 }; |
| 123 | 105 |
| 124 ProviderImpl.prototype.echoStrings = function(a, b) { | 106 ProviderImpl.prototype.echoStrings = function(a, b) { |
| 125 return Promise.resolve({a: a, b: b}); | 107 return Promise.resolve({a: a, b: b}); |
| 126 }; | 108 }; |
| 127 | 109 |
| 128 var pipe = core.createMessagePipe(); | 110 var provider = new sample_interfaces.ProviderPtr(); |
| 129 | 111 var providerBinding = new bindings.Binding(sample_interfaces.Provider, |
| 130 var connection0 = new connection.Connection(pipe.handle0, ProviderImpl); | 112 new ProviderImpl(), |
| 131 | 113 bindings.makeRequest(provider)); |
| 132 var connection1 = new connection.Connection( | 114 var promise = provider.echoString("hello").then(function(response) { |
| 133 pipe.handle1, undefined, sample_interfaces.Provider.proxyClass); | |
| 134 | |
| 135 var promise = connection1.remote.echoString("hello") | |
| 136 .then(function(response) { | |
| 137 expect(response.a).toBe("hello"); | 115 expect(response.a).toBe("hello"); |
| 138 return connection1.remote.echoStrings("hello", "world"); | 116 return provider.echoStrings("hello", "world"); |
| 139 }).then(function(response) { | 117 }).then(function(response) { |
| 140 expect(response.a).toBe("hello"); | 118 expect(response.a).toBe("hello"); |
| 141 expect(response.b).toBe("world"); | 119 expect(response.b).toBe("world"); |
| 142 // Mock a read failure, expect it to fail. | 120 // Mock a read failure, expect it to fail. |
| 143 core.readMessage = function() { | 121 core.readMessage = function() { |
| 144 return { result: core.RESULT_UNKNOWN }; | 122 return { result: core.RESULT_UNKNOWN }; |
| 145 }; | 123 }; |
| 146 return connection1.remote.echoString("goodbye"); | 124 return provider.echoString("goodbye"); |
| 147 }).then(function() { | 125 }).then(function() { |
| 148 throw Error("Expected echoString to fail."); | 126 throw Error("Expected echoString to fail."); |
| 149 }, function(error) { | 127 }, function(error) { |
| 150 expect(error.message).toBe("Connection error: " + core.RESULT_UNKNOWN); | 128 expect(error.message).toBe("Connection error: " + core.RESULT_UNKNOWN); |
| 151 | 129 |
| 152 return Promise.resolve(); | 130 return Promise.resolve(); |
| 153 }); | 131 }); |
| 154 | 132 |
| 155 return promise; | 133 return promise; |
| 156 } | 134 } |
| 157 }); | 135 }); |
| OLD | NEW |