Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 <!DOCTYPE html> | |
| 2 <script src="../resources/testharness.js"></script> | |
| 3 <script src="../resources/testharnessreport.js"></script> | |
| 4 <script src="../resources/mojo-helpers.js"></script> | |
| 5 <script> | |
| 6 'use strict'; | |
| 7 | |
| 8 function CalculatorImpl() { | |
| 9 this.total = 0; | |
| 10 } | |
| 11 | |
| 12 CalculatorImpl.prototype.clear = function() { | |
| 13 this.total = 0; | |
| 14 return Promise.resolve({value: this.total}); | |
| 15 }; | |
| 16 | |
| 17 CalculatorImpl.prototype.add = function(value) { | |
| 18 this.total += value; | |
| 19 return Promise.resolve({value: this.total}); | |
| 20 }; | |
| 21 | |
| 22 CalculatorImpl.prototype.multiply = function(value) { | |
| 23 this.total *= value; | |
| 24 return Promise.resolve({value: this.total}); | |
| 25 }; | |
| 26 | |
| 27 function loadModules(name, func) { | |
| 28 return define( | |
| 29 name, | |
| 30 [ | |
| 31 'mojo/public/js/bindings', | |
| 32 'mojo/public/interfaces/bindings/tests/math_calculator.mojom' | |
| 33 ], | |
| 34 func); | |
| 35 }; | |
| 36 | |
| 37 function binding_test(func, name, properties) { | |
| 38 promise_test(() => { | |
| 39 return loadModules(name, func); | |
| 40 }, name, properties); | |
| 41 } | |
| 42 | |
| 43 binding_test((bindings, math) => { | |
| 44 var binding = new bindings.Binding(math.Calculator, new CalculatorImpl()); | |
| 45 assert_false(binding.isBound()); | |
| 46 | |
| 47 var calc = new math.CalculatorPtr(); | |
| 48 var request = bindings.makeRequest(calc); | |
| 49 binding.bind(request); | |
| 50 assert_true(binding.isBound()); | |
| 51 | |
| 52 binding.close(); | |
| 53 assert_false(binding.isBound()); | |
| 54 }, 'is bound'); | |
| 55 | |
| 56 binding_test((bindings, math) => { | |
|
jbroman
2017/03/09 20:09:35
Nice! I realize this is mostly code movement, but
alokp
2017/03/09 22:13:38
This is awesome. TIL
| |
| 57 var calc1 = new math.CalculatorPtr(); | |
| 58 var calc2 = new math.CalculatorPtr(); | |
| 59 | |
| 60 var calcBinding = new bindings.Binding(math.Calculator, | |
| 61 new CalculatorImpl(), | |
| 62 bindings.makeRequest(calc1)); | |
| 63 | |
| 64 var promise = calc1.add(2).then(function(response) { | |
| 65 assert_equals(response.value, 2); | |
| 66 calcBinding.bind(bindings.makeRequest(calc2)); | |
| 67 return calc2.add(2); | |
| 68 }).then(function(response) { | |
| 69 assert_equals(response.value, 4); | |
| 70 }); | |
| 71 | |
| 72 return promise; | |
| 73 }, 'reusable'); | |
| 74 | |
| 75 binding_test((bindings, math) => { | |
| 76 var calc = new math.CalculatorPtr(); | |
| 77 var calcBinding = new bindings.Binding(math.Calculator, | |
| 78 new CalculatorImpl(), | |
| 79 bindings.makeRequest(calc)); | |
| 80 | |
| 81 var promise = new Promise(function(resolve, reject) { | |
| 82 calcBinding.setConnectionErrorHandler(function() { | |
| 83 resolve(); | |
| 84 }); | |
| 85 calc.ptr.reset(); | |
| 86 }); | |
| 87 | |
| 88 return promise; | |
| 89 }, 'connection error'); | |
| 90 | |
| 91 binding_test((bindings, math) => { | |
| 92 var calc = new math.CalculatorPtr(); | |
| 93 var calcBinding = new bindings.Binding(math.Calculator, | |
| 94 new CalculatorImpl(), | |
| 95 bindings.makeRequest(calc)); | |
| 96 var newCalcBinding = null; | |
| 97 | |
| 98 var promise = calc.add(2).then(function(response) { | |
| 99 assert_equals(response.value, 2); | |
| 100 var interfaceRequest = calcBinding.unbind(); | |
| 101 assert_false(calcBinding.isBound()); | |
| 102 newCalcBinding = new bindings.Binding(math.Calculator, | |
| 103 new CalculatorImpl(), | |
| 104 interfaceRequest); | |
| 105 return calc.add(2); | |
| 106 }).then(function(response) { | |
| 107 assert_equals(response.value, 2); | |
| 108 }); | |
| 109 | |
| 110 return promise; | |
| 111 }, 'unbind'); | |
| 112 | |
| 113 binding_test((bindings, math) => { | |
| 114 var calc1 = new math.CalculatorPtr(); | |
| 115 var calc2 = new math.CalculatorPtr(); | |
| 116 var calcImpl = new CalculatorImpl(); | |
| 117 | |
| 118 var bindingSet = new bindings.BindingSet(math.Calculator); | |
| 119 assert_true(bindingSet.isEmpty()); | |
| 120 bindingSet.addBinding(calcImpl, bindings.makeRequest(calc1)); | |
| 121 bindingSet.addBinding(calcImpl, bindings.makeRequest(calc2)); | |
| 122 assert_false(bindingSet.isEmpty()); | |
| 123 | |
| 124 var promise = calc1.add(3).then(function(response) { | |
| 125 assert_equals(response.value, 3); | |
| 126 return calc2.add(4); | |
| 127 }).then(function(response) { | |
| 128 assert_equals(response.value, 7); | |
| 129 | |
| 130 var promiseOfConnectionError = new Promise(function(resolve, reject) { | |
| 131 bindingSet.setConnectionErrorHandler(function() { | |
| 132 resolve(); | |
| 133 }); | |
| 134 }); | |
| 135 calc1.ptr.reset(); | |
| 136 return promiseOfConnectionError; | |
| 137 }).then(function() { | |
| 138 return calc2.add(5); | |
| 139 }).then(function(response) { | |
| 140 assert_equals(response.value, 12); | |
| 141 | |
| 142 bindingSet.closeAllBindings(); | |
| 143 assert_true(bindingSet.isEmpty()); | |
| 144 }); | |
| 145 | |
| 146 return promise; | |
| 147 }, 'binding set'); | |
| 148 | |
| 149 </script> | |
| OLD | NEW |