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