| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 define([ |
| 6 "gin/test/expect", |
| 7 "mojo/public/js/bindings", |
| 8 "mojo/public/interfaces/bindings/tests/math_calculator.mojom", |
| 9 "mojo/public/js/threading", |
| 10 "gc", |
| 11 ], function(expect, |
| 12 bindings, |
| 13 math, |
| 14 threading, |
| 15 gc) { |
| 16 testIsBound() |
| 17 .then(testEndToEnd) |
| 18 .then(testReusable) |
| 19 .then(function() { |
| 20 this.result = "PASS"; |
| 21 gc.collectGarbage(); // should not crash |
| 22 threading.quit(); |
| 23 }.bind(this)).catch(function(e) { |
| 24 this.result = "FAIL: " + (e.stack || e); |
| 25 threading.quit(); |
| 26 }.bind(this)); |
| 27 |
| 28 function CalculatorImpl() { |
| 29 this.total = 0; |
| 30 } |
| 31 |
| 32 CalculatorImpl.prototype.clear = function() { |
| 33 this.total = 0; |
| 34 return Promise.resolve({value: this.total}); |
| 35 }; |
| 36 |
| 37 CalculatorImpl.prototype.add = function(value) { |
| 38 this.total += value; |
| 39 return Promise.resolve({value: this.total}); |
| 40 }; |
| 41 |
| 42 CalculatorImpl.prototype.multiply = function(value) { |
| 43 this.total *= value; |
| 44 return Promise.resolve({value: this.total}); |
| 45 }; |
| 46 |
| 47 function testIsBound() { |
| 48 var calc = new math.CalculatorPtr(); |
| 49 expect(calc.ptr.isBound()).toBeFalsy(); |
| 50 |
| 51 var request = bindings.makeRequest(calc); |
| 52 expect(calc.ptr.isBound()).toBeTruthy(); |
| 53 |
| 54 calc.ptr.reset(); |
| 55 expect(calc.ptr.isBound()).toBeFalsy(); |
| 56 |
| 57 return Promise.resolve(); |
| 58 } |
| 59 |
| 60 function testEndToEnd() { |
| 61 var calc = new math.CalculatorPtr(); |
| 62 var calcBinding = new bindings.Binding(math.Calculator, |
| 63 new CalculatorImpl(), |
| 64 bindings.makeRequest(calc)); |
| 65 |
| 66 var promise = calc.add(2).then(function(response) { |
| 67 expect(response.value).toBe(2); |
| 68 return calc.multiply(5); |
| 69 }).then(function(response) { |
| 70 expect(response.value).toBe(10); |
| 71 return calc.clear(); |
| 72 }).then(function(response) { |
| 73 expect(response.value).toBe(0); |
| 74 return Promise.resolve(); |
| 75 }); |
| 76 |
| 77 return promise; |
| 78 } |
| 79 |
| 80 function testReusable() { |
| 81 var calc = new math.CalculatorPtr(); |
| 82 var calcImpl1 = new CalculatorImpl(); |
| 83 var calcBinding1 = new bindings.Binding(math.Calculator, |
| 84 calcImpl1, |
| 85 bindings.makeRequest(calc)); |
| 86 var calcImpl2 = new CalculatorImpl(); |
| 87 var calcBinding2 = new bindings.Binding(math.Calculator, calcImpl2); |
| 88 |
| 89 var promise = calc.add(2).then(function(response) { |
| 90 expect(response.value).toBe(2); |
| 91 calcBinding2.bind(bindings.makeRequest(calc)); |
| 92 return calc.add(2); |
| 93 }).then(function(response) { |
| 94 expect(response.value).toBe(2); |
| 95 expect(calcImpl1.total).toBe(2); |
| 96 expect(calcImpl2.total).toBe(2); |
| 97 return Promise.resolve(); |
| 98 }); |
| 99 |
| 100 return promise; |
| 101 } |
| 102 }); |
| OLD | NEW |