Chromium Code Reviews| 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, | |
|
yzshen1
2017/02/11 01:11:29
4 spaces
please see: https://google.github.io/styl
wangjimmy
2017/02/13 20:43:07
Done.
| |
| 182 new IntegerAccessorImpl(), | |
| 183 bindings.makeRequest(integerAccessorPtr) | |
| 184 ); | |
|
yzshen1
2017/02/11 01:11:30
");" should not be on a separate line.
wangjimmy
2017/02/13 20:43:07
Done.
| |
| 185 expect(integerAccessorPtr.ptr.version).toBe(0); | |
| 186 | |
| 187 return integerAccessorPtr.ptr.queryVersion().then(function(version) { | |
| 188 expect(version).toBe(3); | |
| 189 expect(integerAccessorPtr.ptr.version).toBe(3); | |
| 190 }); | |
| 191 } | |
| 192 | |
| 193 function testRequireVersion() { | |
| 194 var integerAccessorImpl = new IntegerAccessorImpl(); | |
| 195 var integerAccessorPtr = new sampleInterfaces.IntegerAccessorPtr(); | |
| 196 var integerAccessorBinding = new bindings.Binding( | |
| 197 sampleInterfaces.IntegerAccessor, | |
|
yzshen1
2017/02/11 01:11:29
4 spaces.
wangjimmy
2017/02/13 20:43:07
Done.
| |
| 198 integerAccessorImpl, | |
| 199 bindings.makeRequest(integerAccessorPtr)); | |
| 200 | |
| 201 // Inital version is 0. | |
| 202 expect(integerAccessorPtr.ptr.version).toBe(0); | |
| 203 | |
| 204 function requireVersion1() { | |
| 205 integerAccessorPtr.ptr.requireVersion(1); | |
| 206 expect(integerAccessorPtr.ptr.version).toBe(1); | |
| 207 integerAccessorPtr.setInteger(123, sampleInterfaces.Enum.VALUE); | |
| 208 return integerAccessorPtr.getInteger().then(function(responseParams) { | |
| 209 expect(responseParams.data).toBe(123); | |
| 210 }); | |
| 211 } | |
| 212 | |
| 213 function requireVersion3() { | |
| 214 integerAccessorPtr.ptr.requireVersion(3); | |
| 215 expect(integerAccessorPtr.ptr.version).toBe(3); | |
| 216 integerAccessorPtr.setInteger(456, sampleInterfaces.Enum.VALUE); | |
| 217 return integerAccessorPtr.getInteger().then(function(responseParams) { | |
| 218 expect(responseParams.data).toBe(456); | |
| 219 }); | |
| 220 } | |
| 221 | |
| 222 // Require a version that is not supported by the impl side. | |
| 223 function requireVersion4() { | |
| 224 integerAccessorPtr.ptr.requireVersion(4); | |
| 225 expect(integerAccessorPtr.ptr.version).toBe(4); | |
| 226 | |
| 227 var promise = new Promise(function(resolve, reject) { | |
| 228 integerAccessorPtr.ptr.setConnectionErrorHandler(function() { | |
| 229 resolve(); | |
| 230 }); | |
| 231 }); | |
| 232 | |
| 233 // The call to setInteger() after requireVersion(4) is ignored. | |
| 234 integerAccessorPtr.setInteger(789, sampleInterfaces.Enum.VALUE); | |
| 235 expect(integerAccessorImpl.integer).toBe(456); | |
|
yzshen1
2017/02/11 01:11:29
Because the call on line 234 is completed asynchro
wangjimmy
2017/02/13 20:43:07
Done.
| |
| 236 | |
| 237 return promise; | |
| 238 } | |
| 239 | |
| 240 return requireVersion1().then(requireVersion3).then(requireVersion4); | |
| 241 } | |
| 160 }); | 242 }); |
| OLD | NEW |