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 testRequestResponse(); |
| 64 this.result = "PASS"; |
54 | 65 |
55 var receivedFrobinate = false; | 66 function testClientServer() { |
56 var receivedDidFrobinate = false; | 67 var receivedFrobinate = false; |
| 68 var receivedDidFrobinate = false; |
57 | 69 |
58 // ServiceImpl -------------------------------------------------------------- | 70 // ServiceImpl ------------------------------------------------------------- |
59 | 71 |
60 function ServiceImpl(peer) { | 72 function ServiceImpl(peer) { |
61 this.peer = peer; | 73 this.peer = peer; |
| 74 } |
| 75 |
| 76 ServiceImpl.prototype = Object.create(sample_service.ServiceStub.prototype); |
| 77 |
| 78 ServiceImpl.prototype.frobinate = function(foo, baz, port) { |
| 79 receivedFrobinate = true; |
| 80 |
| 81 expect(foo.name).toBe("Example name"); |
| 82 expect(baz).toBeTruthy(); |
| 83 expect(core.close(port)).toBe(core.RESULT_OK); |
| 84 |
| 85 this.peer.didFrobinate(42); |
| 86 }; |
| 87 |
| 88 // ServiceImpl ------------------------------------------------------------- |
| 89 |
| 90 function ServiceClientImpl(peer) { |
| 91 this.peer = peer; |
| 92 } |
| 93 |
| 94 ServiceClientImpl.prototype = |
| 95 Object.create(sample_service.ServiceClientStub.prototype); |
| 96 |
| 97 ServiceClientImpl.prototype.didFrobinate = function(result) { |
| 98 receivedDidFrobinate = true; |
| 99 |
| 100 expect(result).toBe(42); |
| 101 }; |
| 102 |
| 103 var pipe = core.createMessagePipe(); |
| 104 var anotherPipe = core.createMessagePipe(); |
| 105 var sourcePipe = core.createMessagePipe(); |
| 106 |
| 107 var connection0 = new connection.Connection( |
| 108 pipe.handle0, ServiceImpl, sample_service.ServiceClientProxy); |
| 109 |
| 110 var connection1 = new connection.Connection( |
| 111 pipe.handle1, ServiceClientImpl, sample_service.ServiceProxy); |
| 112 |
| 113 var foo = new sample_service.Foo(); |
| 114 foo.bar = new sample_service.Bar(); |
| 115 foo.name = "Example name"; |
| 116 foo.source = sourcePipe.handle0; |
| 117 connection1.remote.frobinate(foo, true, anotherPipe.handle0); |
| 118 |
| 119 mockSupport.pumpOnce(core.RESULT_OK); |
| 120 |
| 121 expect(receivedFrobinate).toBeTruthy(); |
| 122 expect(receivedDidFrobinate).toBeTruthy(); |
| 123 |
| 124 connection0.close(); |
| 125 connection1.close(); |
| 126 |
| 127 expect(mockSupport.numberOfWaitingCallbacks()).toBe(0); |
| 128 |
| 129 // sourcePipe.handle0 was closed automatically when sent over IPC. |
| 130 expect(core.close(sourcePipe.handle0)).toBe(core.RESULT_INVALID_ARGUMENT); |
| 131 // sourcePipe.handle1 hasn't been closed yet. |
| 132 expect(core.close(sourcePipe.handle1)).toBe(core.RESULT_OK); |
| 133 |
| 134 // anotherPipe.handle0 was closed automatically when sent over IPC. |
| 135 expect(core.close(anotherPipe.handle0)).toBe(core.RESULT_INVALID_ARGUMENT); |
| 136 // anotherPipe.handle1 hasn't been closed yet. |
| 137 expect(core.close(anotherPipe.handle1)).toBe(core.RESULT_OK); |
| 138 |
| 139 // The Connection object is responsible for closing these handles. |
| 140 expect(core.close(pipe.handle0)).toBe(core.RESULT_INVALID_ARGUMENT); |
| 141 expect(core.close(pipe.handle1)).toBe(core.RESULT_INVALID_ARGUMENT); |
62 } | 142 } |
63 | 143 |
64 ServiceImpl.prototype = Object.create(sample.ServiceStub.prototype); | 144 function testRequestResponse() { |
65 | 145 |
66 ServiceImpl.prototype.frobinate = function(foo, baz, port) { | 146 // ProviderImpl ------------------------------------------------------------ |
67 receivedFrobinate = true; | |
68 | 147 |
69 expect(foo.name).toBe("Example name"); | 148 function ProviderImpl(peer) { |
70 expect(baz).toBeTruthy(); | 149 this.peer = peer; |
71 expect(core.close(port)).toBe(core.RESULT_OK); | 150 } |
72 | 151 |
73 this.peer.didFrobinate(42); | 152 ProviderImpl.prototype = |
74 }; | 153 Object.create(sample_interfaces.ProviderStub.prototype); |
75 | 154 |
76 // ServiceImpl -------------------------------------------------------------- | 155 ProviderImpl.prototype.echoString = function(a, callback) { |
| 156 callback(a); |
| 157 }; |
77 | 158 |
78 function ServiceClientImpl(peer) { | 159 ProviderImpl.prototype.echoStrings = function(a, b, callback) { |
79 this.peer = peer; | 160 callback(a, b); |
| 161 }; |
| 162 |
| 163 // ProviderClientImpl ------------------------------------------------------ |
| 164 |
| 165 function ProviderClientImpl(peer) { |
| 166 this.peer = peer; |
| 167 } |
| 168 |
| 169 ProviderClientImpl.prototype = |
| 170 Object.create(sample_interfaces.ProviderClientStub.prototype); |
| 171 |
| 172 ProviderClientImpl.prototype.didFrobinate = function(result) { |
| 173 receivedDidFrobinate = true; |
| 174 |
| 175 expect(result).toBe(42); |
| 176 }; |
| 177 |
| 178 var pipe = core.createMessagePipe(); |
| 179 |
| 180 var connection0 = new connection.Connection( |
| 181 pipe.handle0, ProviderImpl, sample_interfaces.ProviderClientProxy); |
| 182 |
| 183 var connection1 = new connection.Connection( |
| 184 pipe.handle1, ProviderClientImpl, sample_interfaces.ProviderProxy); |
| 185 |
| 186 var echoedString; |
| 187 |
| 188 // echoString |
| 189 |
| 190 connection1.remote.echoString("hello", function(a) { |
| 191 echoedString = a; |
| 192 }); |
| 193 |
| 194 mockSupport.pumpOnce(core.RESULT_OK); |
| 195 |
| 196 expect(echoedString).toBe("hello"); |
| 197 |
| 198 // echoStrings |
| 199 |
| 200 connection1.remote.echoStrings("hello", "world", function(a, b) { |
| 201 echoedString = a + " " + b; |
| 202 }); |
| 203 |
| 204 mockSupport.pumpOnce(core.RESULT_OK); |
| 205 |
| 206 expect(echoedString).toBe("hello world"); |
80 } | 207 } |
81 | |
82 ServiceClientImpl.prototype = | |
83 Object.create(sample.ServiceClientStub.prototype); | |
84 | |
85 ServiceClientImpl.prototype.didFrobinate = function(result) { | |
86 receivedDidFrobinate = true; | |
87 | |
88 expect(result).toBe(42); | |
89 }; | |
90 | |
91 var pipe = core.createMessagePipe(); | |
92 var anotherPipe = core.createMessagePipe(); | |
93 var sourcePipe = core.createMessagePipe(); | |
94 | |
95 var connection0 = new connector.Connection( | |
96 pipe.handle0, ServiceImpl, sample.ServiceClientProxy); | |
97 | |
98 var connection1 = new connector.Connection( | |
99 pipe.handle1, ServiceClientImpl, sample.ServiceProxy); | |
100 | |
101 var foo = new sample.Foo(); | |
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 | |
107 mockSupport.pumpOnce(core.RESULT_OK); | |
108 | |
109 expect(receivedFrobinate).toBeTruthy(); | |
110 expect(receivedDidFrobinate).toBeTruthy(); | |
111 | |
112 connection0.close(); | |
113 connection1.close(); | |
114 | |
115 expect(mockSupport.numberOfWaitingCallbacks()).toBe(0); | |
116 | |
117 // sourcePipe.handle0 was closed automatically when sent over IPC. | |
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 | |
122 // anotherPipe.handle0 was closed automatically when sent over IPC. | |
123 expect(core.close(anotherPipe.handle0)).toBe(core.RESULT_INVALID_ARGUMENT); | |
124 // anotherPipe.handle1 hasn't been closed yet. | |
125 expect(core.close(anotherPipe.handle1)).toBe(core.RESULT_OK); | |
126 | |
127 // The Connection object is responsible for closing these handles. | |
128 expect(core.close(pipe.handle0)).toBe(core.RESULT_INVALID_ARGUMENT); | |
129 expect(core.close(pipe.handle1)).toBe(core.RESULT_INVALID_ARGUMENT); | |
130 | |
131 this.result = "PASS"; | |
132 }); | 208 }); |
OLD | NEW |