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", function() { |
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 14 matching lines...) Expand all Loading... |
25 for (var i = 0; i < waitingCallbacks.length; ++i) { | 25 for (var i = 0; i < waitingCallbacks.length; ++i) { |
26 if (waitingCallbacks[i]) | 26 if (waitingCallbacks[i]) |
27 ++count; | 27 ++count; |
28 } | 28 } |
29 return count; | 29 return count; |
30 } | 30 } |
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 callbacks[i](result); | 36 if (callbacks[i]) |
| 37 callbacks[i](result); |
| 38 } |
37 } | 39 } |
38 | 40 |
39 var exports = {}; | 41 var exports = {}; |
40 exports.asyncWait = asyncWait; | 42 exports.asyncWait = asyncWait; |
41 exports.cancelWait = cancelWait; | 43 exports.cancelWait = cancelWait; |
42 exports.numberOfWaitingCallbacks = numberOfWaitingCallbacks; | 44 exports.numberOfWaitingCallbacks = numberOfWaitingCallbacks; |
43 exports.pumpOnce = pumpOnce; | 45 exports.pumpOnce = pumpOnce; |
44 return exports; | 46 return exports; |
45 }); | 47 }); |
46 | 48 |
47 define([ | 49 define([ |
48 "gin/test/expect", | 50 "gin/test/expect", |
49 "mojo/bindings/js/support", | 51 "mojo/bindings/js/support", |
50 "mojo/bindings/js/core", | 52 "mojo/bindings/js/core", |
51 "mojo/public/bindings/js/connector", | 53 "mojo/public/bindings/js/connection", |
| 54 "mojo/public/bindings/tests/sample_interfaces.mojom", |
52 "mojo/public/bindings/tests/sample_service.mojom", | 55 "mojo/public/bindings/tests/sample_service.mojom", |
53 ], function(expect, mockSupport, core, connector, sample) { | 56 ], function(expect, |
| 57 mockSupport, |
| 58 core, |
| 59 connection, |
| 60 sample_interfaces, |
| 61 sample_service) { |
| 62 testClientServer(); |
| 63 testWriteToClosedPipe(); |
| 64 testRequestResponse(); |
| 65 this.result = "PASS"; |
54 | 66 |
55 var receivedFrobinate = false; | 67 function testClientServer() { |
56 var receivedDidFrobinate = false; | 68 var receivedFrobinate = false; |
| 69 var receivedDidFrobinate = false; |
57 | 70 |
58 // ServiceImpl -------------------------------------------------------------- | 71 // ServiceImpl ------------------------------------------------------------- |
59 | 72 |
60 function ServiceImpl(peer) { | 73 function ServiceImpl(peer) { |
61 this.peer = peer; | 74 this.peer = peer; |
| 75 } |
| 76 |
| 77 ServiceImpl.prototype = Object.create(sample_service.ServiceStub.prototype); |
| 78 |
| 79 ServiceImpl.prototype.frobinate = function(foo, baz, port) { |
| 80 receivedFrobinate = true; |
| 81 |
| 82 expect(foo.name).toBe("Example name"); |
| 83 expect(baz).toBeTruthy(); |
| 84 expect(core.close(port)).toBe(core.RESULT_OK); |
| 85 |
| 86 this.peer.didFrobinate(42); |
| 87 }; |
| 88 |
| 89 // ServiceImpl ------------------------------------------------------------- |
| 90 |
| 91 function ServiceClientImpl(peer) { |
| 92 this.peer = peer; |
| 93 } |
| 94 |
| 95 ServiceClientImpl.prototype = |
| 96 Object.create(sample_service.ServiceClientStub.prototype); |
| 97 |
| 98 ServiceClientImpl.prototype.didFrobinate = function(result) { |
| 99 receivedDidFrobinate = true; |
| 100 |
| 101 expect(result).toBe(42); |
| 102 }; |
| 103 |
| 104 var pipe = core.createMessagePipe(); |
| 105 var anotherPipe = core.createMessagePipe(); |
| 106 var sourcePipe = core.createMessagePipe(); |
| 107 |
| 108 var connection0 = new connection.Connection( |
| 109 pipe.handle0, ServiceImpl, sample_service.ServiceClientProxy); |
| 110 |
| 111 var connection1 = new connection.Connection( |
| 112 pipe.handle1, ServiceClientImpl, sample_service.ServiceProxy); |
| 113 |
| 114 var foo = new sample_service.Foo(); |
| 115 foo.bar = new sample_service.Bar(); |
| 116 foo.name = "Example name"; |
| 117 foo.source = sourcePipe.handle0; |
| 118 connection1.remote.frobinate(foo, true, anotherPipe.handle0); |
| 119 |
| 120 mockSupport.pumpOnce(core.RESULT_OK); |
| 121 |
| 122 expect(receivedFrobinate).toBeTruthy(); |
| 123 expect(receivedDidFrobinate).toBeTruthy(); |
| 124 |
| 125 connection0.close(); |
| 126 connection1.close(); |
| 127 |
| 128 expect(mockSupport.numberOfWaitingCallbacks()).toBe(0); |
| 129 |
| 130 // sourcePipe.handle0 was closed automatically when sent over IPC. |
| 131 expect(core.close(sourcePipe.handle0)).toBe(core.RESULT_INVALID_ARGUMENT); |
| 132 // sourcePipe.handle1 hasn't been closed yet. |
| 133 expect(core.close(sourcePipe.handle1)).toBe(core.RESULT_OK); |
| 134 |
| 135 // anotherPipe.handle0 was closed automatically when sent over IPC. |
| 136 expect(core.close(anotherPipe.handle0)).toBe(core.RESULT_INVALID_ARGUMENT); |
| 137 // anotherPipe.handle1 hasn't been closed yet. |
| 138 expect(core.close(anotherPipe.handle1)).toBe(core.RESULT_OK); |
| 139 |
| 140 // The Connection object is responsible for closing these handles. |
| 141 expect(core.close(pipe.handle0)).toBe(core.RESULT_INVALID_ARGUMENT); |
| 142 expect(core.close(pipe.handle1)).toBe(core.RESULT_INVALID_ARGUMENT); |
62 } | 143 } |
63 | 144 |
64 ServiceImpl.prototype = Object.create(sample.ServiceStub.prototype); | 145 function testWriteToClosedPipe() { |
| 146 var pipe = core.createMessagePipe(); |
65 | 147 |
66 ServiceImpl.prototype.frobinate = function(foo, baz, port) { | 148 var connection1 = new connection.Connection( |
67 receivedFrobinate = true; | 149 pipe.handle1, function() {}, sample_service.ServiceProxy); |
68 | 150 |
69 expect(foo.name).toBe("Example name"); | 151 // Close the other end of the pipe. |
70 expect(baz).toBeTruthy(); | 152 core.close(pipe.handle0); |
71 expect(core.close(port)).toBe(core.RESULT_OK); | |
72 | 153 |
73 this.peer.didFrobinate(42); | 154 // Not observed yet because we haven't pumped events yet. |
74 }; | 155 expect(connection1.encounteredError()).toBeFalsy(); |
75 | 156 |
76 // ServiceImpl -------------------------------------------------------------- | 157 var foo = new sample_service.Foo(); |
| 158 foo.bar = new sample_service.Bar(); |
| 159 // TODO(darin): crbug.com/357043: pass null in place of |foo| here. |
| 160 connection1.remote.frobinate(foo, true, core.kInvalidHandle); |
77 | 161 |
78 function ServiceClientImpl(peer) { | 162 // Write failures are not reported. |
79 this.peer = peer; | 163 expect(connection1.encounteredError()).toBeFalsy(); |
| 164 |
| 165 // Pump events, and then we should start observing the closed pipe. |
| 166 mockSupport.pumpOnce(core.RESULT_OK); |
| 167 |
| 168 expect(connection1.encounteredError()).toBeTruthy(); |
| 169 |
| 170 connection1.close(); |
80 } | 171 } |
81 | 172 |
82 ServiceClientImpl.prototype = | 173 function testRequestResponse() { |
83 Object.create(sample.ServiceClientStub.prototype); | |
84 | 174 |
85 ServiceClientImpl.prototype.didFrobinate = function(result) { | 175 // ProviderImpl ------------------------------------------------------------ |
86 receivedDidFrobinate = true; | |
87 | 176 |
88 expect(result).toBe(42); | 177 function ProviderImpl(peer) { |
89 }; | 178 this.peer = peer; |
| 179 } |
90 | 180 |
91 var pipe = core.createMessagePipe(); | 181 ProviderImpl.prototype = |
92 var anotherPipe = core.createMessagePipe(); | 182 Object.create(sample_interfaces.ProviderStub.prototype); |
93 var sourcePipe = core.createMessagePipe(); | |
94 | 183 |
95 var connection0 = new connector.Connection( | 184 ProviderImpl.prototype.echoString = function(a, callback) { |
96 pipe.handle0, ServiceImpl, sample.ServiceClientProxy); | 185 callback(a); |
| 186 }; |
97 | 187 |
98 var connection1 = new connector.Connection( | 188 ProviderImpl.prototype.echoStrings = function(a, b, callback) { |
99 pipe.handle1, ServiceClientImpl, sample.ServiceProxy); | 189 callback(a, b); |
| 190 }; |
100 | 191 |
101 var foo = new sample.Foo(); | 192 // ProviderClientImpl ------------------------------------------------------ |
102 foo.bar = new sample.Bar(); | |
103 foo.name = "Example name"; | |
104 foo.source = sourcePipe.handle0; | |
105 connection1.remote.frobinate(foo, true, anotherPipe.handle0); | |
106 | 193 |
107 mockSupport.pumpOnce(core.RESULT_OK); | 194 function ProviderClientImpl(peer) { |
| 195 this.peer = peer; |
| 196 } |
108 | 197 |
109 expect(receivedFrobinate).toBeTruthy(); | 198 ProviderClientImpl.prototype = |
110 expect(receivedDidFrobinate).toBeTruthy(); | 199 Object.create(sample_interfaces.ProviderClientStub.prototype); |
111 | 200 |
112 connection0.close(); | 201 ProviderClientImpl.prototype.didFrobinate = function(result) { |
113 connection1.close(); | 202 receivedDidFrobinate = true; |
114 | 203 |
115 expect(mockSupport.numberOfWaitingCallbacks()).toBe(0); | 204 expect(result).toBe(42); |
| 205 }; |
116 | 206 |
117 // sourcePipe.handle0 was closed automatically when sent over IPC. | 207 var pipe = core.createMessagePipe(); |
118 expect(core.close(sourcePipe.handle0)).toBe(core.RESULT_INVALID_ARGUMENT); | |
119 // sourcePipe.handle1 hasn't been closed yet. | |
120 expect(core.close(sourcePipe.handle1)).toBe(core.RESULT_OK); | |
121 | 208 |
122 // anotherPipe.handle0 was closed automatically when sent over IPC. | 209 var connection0 = new connection.Connection( |
123 expect(core.close(anotherPipe.handle0)).toBe(core.RESULT_INVALID_ARGUMENT); | 210 pipe.handle0, ProviderImpl, sample_interfaces.ProviderClientProxy); |
124 // anotherPipe.handle1 hasn't been closed yet. | |
125 expect(core.close(anotherPipe.handle1)).toBe(core.RESULT_OK); | |
126 | 211 |
127 // The Connection object is responsible for closing these handles. | 212 var connection1 = new connection.Connection( |
128 expect(core.close(pipe.handle0)).toBe(core.RESULT_INVALID_ARGUMENT); | 213 pipe.handle1, ProviderClientImpl, sample_interfaces.ProviderProxy); |
129 expect(core.close(pipe.handle1)).toBe(core.RESULT_INVALID_ARGUMENT); | |
130 | 214 |
131 this.result = "PASS"; | 215 var echoedString; |
| 216 |
| 217 // echoString |
| 218 |
| 219 connection1.remote.echoString("hello", function(a) { |
| 220 echoedString = a; |
| 221 }); |
| 222 |
| 223 mockSupport.pumpOnce(core.RESULT_OK); |
| 224 |
| 225 expect(echoedString).toBe("hello"); |
| 226 |
| 227 // echoStrings |
| 228 |
| 229 connection1.remote.echoStrings("hello", "world", function(a, b) { |
| 230 echoedString = a + " " + b; |
| 231 }); |
| 232 |
| 233 mockSupport.pumpOnce(core.RESULT_OK); |
| 234 |
| 235 expect(echoedString).toBe("hello world"); |
| 236 } |
132 }); | 237 }); |
OLD | NEW |