| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/bindings", | 7 "mojo/public/js/bindings", |
| 8 "mojo/public/js/core", | 8 "mojo/public/js/core", |
| 9 "mojo/public/interfaces/bindings/tests/math_calculator.mojom", | 9 "mojo/public/interfaces/bindings/tests/math_calculator.mojom", |
| 10 "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom", |
| 10 "mojo/public/js/threading", | 11 "mojo/public/js/threading", |
| 11 "gc", | 12 "gc", |
| 12 ], function(expect, | 13 ], function(expect, |
| 13 bindings, | 14 bindings, |
| 14 core, | 15 core, |
| 15 math, | 16 math, |
| 17 sampleInterfaces, |
| 16 threading, | 18 threading, |
| 17 gc) { | 19 gc) { |
| 18 testIsBound() | 20 testIsBound() |
| 19 .then(testEndToEnd) | 21 .then(testEndToEnd) |
| 20 .then(testReusable) | 22 .then(testReusable) |
| 21 .then(testConnectionError) | 23 .then(testConnectionError) |
| 22 .then(testPassInterface) | 24 .then(testPassInterface) |
| 23 .then(testBindRawHandle) | 25 .then(testBindRawHandle) |
| 26 .then(testQueryVersion) |
| 27 .then(testRequireVersion) |
| 24 .then(function() { | 28 .then(function() { |
| 25 this.result = "PASS"; | 29 this.result = "PASS"; |
| 26 gc.collectGarbage(); // should not crash | 30 gc.collectGarbage(); // should not crash |
| 27 threading.quit(); | 31 threading.quit(); |
| 28 }.bind(this)).catch(function(e) { | 32 }.bind(this)).catch(function(e) { |
| 29 this.result = "FAIL: " + (e.stack || e); | 33 this.result = "FAIL: " + (e.stack || e); |
| 30 threading.quit(); | 34 threading.quit(); |
| 31 }.bind(this)); | 35 }.bind(this)); |
| 32 | 36 |
| 33 function CalculatorImpl() { | 37 function CalculatorImpl() { |
| 34 this.total = 0; | 38 this.total = 0; |
| 35 } | 39 } |
| 36 | 40 |
| 37 CalculatorImpl.prototype.clear = function() { | 41 CalculatorImpl.prototype.clear = function() { |
| 38 this.total = 0; | 42 this.total = 0; |
| 39 return Promise.resolve({value: this.total}); | 43 return Promise.resolve({value: this.total}); |
| 40 }; | 44 }; |
| 41 | 45 |
| 42 CalculatorImpl.prototype.add = function(value) { | 46 CalculatorImpl.prototype.add = function(value) { |
| 43 this.total += value; | 47 this.total += value; |
| 44 return Promise.resolve({value: this.total}); | 48 return Promise.resolve({value: this.total}); |
| 45 }; | 49 }; |
| 46 | 50 |
| 47 CalculatorImpl.prototype.multiply = function(value) { | 51 CalculatorImpl.prototype.multiply = function(value) { |
| 48 this.total *= value; | 52 this.total *= value; |
| 49 return Promise.resolve({value: this.total}); | 53 return Promise.resolve({value: this.total}); |
| 50 }; | 54 }; |
| 51 | 55 |
| 56 function IntegerAccessorImpl() { |
| 57 this.integer = 0; |
| 58 } |
| 59 |
| 60 IntegerAccessorImpl.prototype.getInteger = function() { |
| 61 return Promise.resolve({data: this.integer}); |
| 62 }; |
| 63 |
| 64 IntegerAccessorImpl.prototype.setInteger = function(value) { |
| 65 this.integer = value; |
| 66 }; |
| 67 |
| 52 function testIsBound() { | 68 function testIsBound() { |
| 53 var calc = new math.CalculatorPtr(); | 69 var calc = new math.CalculatorPtr(); |
| 54 expect(calc.ptr.isBound()).toBeFalsy(); | 70 expect(calc.ptr.isBound()).toBeFalsy(); |
| 55 | 71 |
| 56 var request = bindings.makeRequest(calc); | 72 var request = bindings.makeRequest(calc); |
| 57 expect(calc.ptr.isBound()).toBeTruthy(); | 73 expect(calc.ptr.isBound()).toBeTruthy(); |
| 58 | 74 |
| 59 calc.ptr.reset(); | 75 calc.ptr.reset(); |
| 60 expect(calc.ptr.isBound()).toBeFalsy(); | 76 expect(calc.ptr.isBound()).toBeFalsy(); |
| 61 | 77 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 new CalculatorImpl(), | 166 new CalculatorImpl(), |
| 151 pipe.handle1); | 167 pipe.handle1); |
| 152 | 168 |
| 153 var promise = calc.add(2).then(function(response) { | 169 var promise = calc.add(2).then(function(response) { |
| 154 expect(response.value).toBe(2); | 170 expect(response.value).toBe(2); |
| 155 return Promise.resolve(); | 171 return Promise.resolve(); |
| 156 }); | 172 }); |
| 157 | 173 |
| 158 return promise; | 174 return promise; |
| 159 } | 175 } |
| 176 |
| 177 function testQueryVersion() { |
| 178 var integerAccessorPtr = new sampleInterfaces.IntegerAccessorPtr(); |
| 179 |
| 180 var integerAccessorBinding = new bindings.Binding( |
| 181 sampleInterfaces.IntegerAccessor, |
| 182 new IntegerAccessorImpl(), |
| 183 bindings.makeRequest(integerAccessorPtr)); |
| 184 expect(integerAccessorPtr.ptr.version).toBe(0); |
| 185 |
| 186 return integerAccessorPtr.ptr.queryVersion().then(function(version) { |
| 187 expect(version).toBe(3); |
| 188 expect(integerAccessorPtr.ptr.version).toBe(3); |
| 189 }); |
| 190 } |
| 191 |
| 192 function testRequireVersion() { |
| 193 var integerAccessorImpl = new IntegerAccessorImpl(); |
| 194 var integerAccessorPtr = new sampleInterfaces.IntegerAccessorPtr(); |
| 195 var integerAccessorBinding = new bindings.Binding( |
| 196 sampleInterfaces.IntegerAccessor, |
| 197 integerAccessorImpl, |
| 198 bindings.makeRequest(integerAccessorPtr)); |
| 199 |
| 200 // Inital version is 0. |
| 201 expect(integerAccessorPtr.ptr.version).toBe(0); |
| 202 |
| 203 function requireVersion1() { |
| 204 integerAccessorPtr.ptr.requireVersion(1); |
| 205 expect(integerAccessorPtr.ptr.version).toBe(1); |
| 206 integerAccessorPtr.setInteger(123, sampleInterfaces.Enum.VALUE); |
| 207 return integerAccessorPtr.getInteger().then(function(responseParams) { |
| 208 expect(responseParams.data).toBe(123); |
| 209 }); |
| 210 } |
| 211 |
| 212 function requireVersion3() { |
| 213 integerAccessorPtr.ptr.requireVersion(3); |
| 214 expect(integerAccessorPtr.ptr.version).toBe(3); |
| 215 integerAccessorPtr.setInteger(456, sampleInterfaces.Enum.VALUE); |
| 216 return integerAccessorPtr.getInteger().then(function(responseParams) { |
| 217 expect(responseParams.data).toBe(456); |
| 218 }); |
| 219 } |
| 220 |
| 221 // Require a version that is not supported by the impl side. |
| 222 function requireVersion4() { |
| 223 integerAccessorPtr.ptr.requireVersion(4); |
| 224 expect(integerAccessorPtr.ptr.version).toBe(4); |
| 225 integerAccessorPtr.setInteger(789, sampleInterfaces.Enum.VALUE); |
| 226 |
| 227 var promise = new Promise(function(resolve, reject) { |
| 228 integerAccessorPtr.ptr.setConnectionErrorHandler(function() { |
| 229 // The call to setInteger() after requireVersion(4) is ignored. |
| 230 expect(integerAccessorImpl.integer).toBe(456); |
| 231 resolve(); |
| 232 }); |
| 233 }); |
| 234 |
| 235 return promise; |
| 236 } |
| 237 |
| 238 return requireVersion1().then(requireVersion3).then(requireVersion4); |
| 239 } |
| 160 }); | 240 }); |
| OLD | NEW |