| OLD | NEW |
| 1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
| 2 <script src="../resources/testharness.js"></script> | 2 <script src="../resources/testharness.js"></script> |
| 3 <script src="../resources/testharnessreport.js"></script> | 3 <script src="../resources/testharnessreport.js"></script> |
| 4 <script src="../resources/mojo-helpers.js"></script> | 4 <script src="file:///gen/layout_test_data/mojo/public/js/mojo_bindings.js"></scr
ipt> |
| 5 <script src="file:///gen/mojo/public/interfaces/bindings/tests/math_calculator.m
ojom.js"></script> |
| 5 <script> | 6 <script> |
| 6 'use strict'; | 7 'use strict'; |
| 7 | 8 |
| 8 function CalculatorImpl() { | 9 function CalculatorImpl() { |
| 9 this.total = 0; | 10 this.total = 0; |
| 10 } | 11 } |
| 11 | 12 |
| 12 CalculatorImpl.prototype.clear = function() { | 13 CalculatorImpl.prototype.clear = function() { |
| 13 this.total = 0; | 14 this.total = 0; |
| 14 return Promise.resolve({value: this.total}); | 15 return Promise.resolve({value: this.total}); |
| 15 }; | 16 }; |
| 16 | 17 |
| 17 CalculatorImpl.prototype.add = function(value) { | 18 CalculatorImpl.prototype.add = function(value) { |
| 18 this.total += value; | 19 this.total += value; |
| 19 return Promise.resolve({value: this.total}); | 20 return Promise.resolve({value: this.total}); |
| 20 }; | 21 }; |
| 21 | 22 |
| 22 CalculatorImpl.prototype.multiply = function(value) { | 23 CalculatorImpl.prototype.multiply = function(value) { |
| 23 this.total *= value; | 24 this.total *= value; |
| 24 return Promise.resolve({value: this.total}); | 25 return Promise.resolve({value: this.total}); |
| 25 }; | 26 }; |
| 26 | 27 |
| 27 function loadModules(name, func) { | 28 test(() => { |
| 28 return define( | 29 var binding = new mojo.Binding(math.Calculator, new CalculatorImpl()); |
| 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()); | 30 assert_false(binding.isBound()); |
| 46 | 31 |
| 47 var calc = new math.CalculatorPtr(); | 32 var calc = new math.CalculatorPtr(); |
| 48 var request = bindings.makeRequest(calc); | 33 var request = mojo.makeRequest(calc); |
| 49 binding.bind(request); | 34 binding.bind(request); |
| 50 assert_true(binding.isBound()); | 35 assert_true(binding.isBound()); |
| 51 | 36 |
| 52 binding.close(); | 37 binding.close(); |
| 53 assert_false(binding.isBound()); | 38 assert_false(binding.isBound()); |
| 54 }, 'is bound'); | 39 }, 'is bound'); |
| 55 | 40 |
| 56 binding_test(async (bindings, math) => { | 41 promise_test(async () => { |
| 57 var calc1 = new math.CalculatorPtr(); | 42 var calc1 = new math.CalculatorPtr(); |
| 58 var calcBinding = new bindings.Binding(math.Calculator, | 43 var calcBinding = new mojo.Binding(math.Calculator, new CalculatorImpl(), |
| 59 new CalculatorImpl(), | 44 mojo.makeRequest(calc1)); |
| 60 bindings.makeRequest(calc1)); | |
| 61 assert_equals((await calc1.add(2)).value, 2); | 45 assert_equals((await calc1.add(2)).value, 2); |
| 62 | 46 |
| 63 var calc2 = new math.CalculatorPtr(); | 47 var calc2 = new math.CalculatorPtr(); |
| 64 calcBinding.bind(bindings.makeRequest(calc2)); | 48 calcBinding.bind(mojo.makeRequest(calc2)); |
| 65 assert_equals((await calc2.add(2)).value, 4); | 49 assert_equals((await calc2.add(2)).value, 4); |
| 66 }, 'reusable'); | 50 }, 'reusable'); |
| 67 | 51 |
| 68 binding_test(async (bindings, math) => { | 52 promise_test(async () => { |
| 69 var calc = new math.CalculatorPtr(); | 53 var calc = new math.CalculatorPtr(); |
| 70 var calcBinding = new bindings.Binding(math.Calculator, | 54 var calcBinding = new mojo.Binding(math.Calculator, new CalculatorImpl(), |
| 71 new CalculatorImpl(), | 55 mojo.makeRequest(calc)); |
| 72 bindings.makeRequest(calc)); | |
| 73 | 56 |
| 74 await new Promise((resolve, reject) => { | 57 await new Promise((resolve, reject) => { |
| 75 calcBinding.setConnectionErrorHandler(() => { resolve(); }); | 58 calcBinding.setConnectionErrorHandler(() => { resolve(); }); |
| 76 calc.ptr.reset(); | 59 calc.ptr.reset(); |
| 77 }); | 60 }); |
| 78 }, 'connection error'); | 61 }, 'connection error'); |
| 79 | 62 |
| 80 binding_test(async (bindings, math) => { | 63 promise_test(async () => { |
| 81 var calc = new math.CalculatorPtr(); | 64 var calc = new math.CalculatorPtr(); |
| 82 var calcBinding = new bindings.Binding(math.Calculator, | 65 var calcBinding = new mojo.Binding(math.Calculator, new CalculatorImpl(), |
| 83 new CalculatorImpl(), | 66 mojo.makeRequest(calc)); |
| 84 bindings.makeRequest(calc)); | |
| 85 | 67 |
| 86 await new Promise((resolve, reject) => { | 68 await new Promise((resolve, reject) => { |
| 87 calcBinding.setConnectionErrorHandler(({custom_reason, description}) => { | 69 calcBinding.setConnectionErrorHandler(({customReason, description}) => { |
| 88 assert_equals(custom_reason, 32); | 70 assert_equals(customReason, 32); |
| 89 assert_equals(description, 'goodbye'); | 71 assert_equals(description, 'goodbye'); |
| 90 resolve(); | 72 resolve(); |
| 91 }); | 73 }); |
| 92 calc.ptr.resetWithReason({custom_reason: 32, description: 'goodbye'}); | 74 calc.ptr.resetWithReason({customReason: 32, description: 'goodbye'}); |
| 93 }); | 75 }); |
| 94 }, 'connection error with reason'); | 76 }, 'connection error with reason'); |
| 95 | 77 |
| 96 binding_test(async (bindings, math) => { | 78 promise_test(async () => { |
| 97 var calc = new math.CalculatorPtr(); | 79 var calc = new math.CalculatorPtr(); |
| 98 var calcBinding = new bindings.Binding(math.Calculator, | 80 var calcBinding = new mojo.Binding(math.Calculator, new CalculatorImpl(), |
| 99 new CalculatorImpl(), | 81 mojo.makeRequest(calc)); |
| 100 bindings.makeRequest(calc)); | |
| 101 assert_equals((await calc.add(2)).value, 2); | 82 assert_equals((await calc.add(2)).value, 2); |
| 102 | 83 |
| 103 var interfaceRequest = calcBinding.unbind(); | 84 var interfaceRequest = calcBinding.unbind(); |
| 104 assert_false(calcBinding.isBound()); | 85 assert_false(calcBinding.isBound()); |
| 105 | 86 |
| 106 var newCalcBinding = new bindings.Binding(math.Calculator, | 87 var newCalcBinding = new mojo.Binding(math.Calculator, new CalculatorImpl(), |
| 107 new CalculatorImpl(), | 88 interfaceRequest); |
| 108 interfaceRequest); | |
| 109 assert_equals((await calc.add(2)).value, 2); | 89 assert_equals((await calc.add(2)).value, 2); |
| 110 }, 'unbind'); | 90 }, 'unbind'); |
| 111 | 91 |
| 112 binding_test(async (bindings, math) => { | 92 promise_test(async () => { |
| 113 var calc1 = new math.CalculatorPtr(); | 93 var calc1 = new math.CalculatorPtr(); |
| 114 var calc2 = new math.CalculatorPtr(); | 94 var calc2 = new math.CalculatorPtr(); |
| 115 var calcImpl = new CalculatorImpl(); | 95 var calcImpl = new CalculatorImpl(); |
| 116 | 96 |
| 117 var bindingSet = new bindings.BindingSet(math.Calculator); | 97 var bindingSet = new mojo.BindingSet(math.Calculator); |
| 118 assert_true(bindingSet.isEmpty()); | 98 assert_true(bindingSet.isEmpty()); |
| 119 | 99 |
| 120 bindingSet.addBinding(calcImpl, bindings.makeRequest(calc1)); | 100 bindingSet.addBinding(calcImpl, mojo.makeRequest(calc1)); |
| 121 bindingSet.addBinding(calcImpl, bindings.makeRequest(calc2)); | 101 bindingSet.addBinding(calcImpl, mojo.makeRequest(calc2)); |
| 122 assert_false(bindingSet.isEmpty()); | 102 assert_false(bindingSet.isEmpty()); |
| 123 | 103 |
| 124 assert_equals((await calc1.add(3)).value, 3); | 104 assert_equals((await calc1.add(3)).value, 3); |
| 125 assert_equals((await calc2.add(4)).value, 7); | 105 assert_equals((await calc2.add(4)).value, 7); |
| 126 | 106 |
| 127 await new Promise((resolve, reject) => { | 107 await new Promise((resolve, reject) => { |
| 128 bindingSet.setConnectionErrorHandler(() => { resolve(); }); | 108 bindingSet.setConnectionErrorHandler(() => { resolve(); }); |
| 129 calc1.ptr.reset(); | 109 calc1.ptr.reset(); |
| 130 }); | 110 }); |
| 131 | 111 |
| 132 assert_equals((await calc2.add(5)).value, 12); | 112 assert_equals((await calc2.add(5)).value, 12); |
| 133 | 113 |
| 134 bindingSet.closeAllBindings(); | 114 bindingSet.closeAllBindings(); |
| 135 assert_true(bindingSet.isEmpty()); | 115 assert_true(bindingSet.isEmpty()); |
| 136 }, 'binding set'); | 116 }, 'binding set'); |
| 137 | 117 |
| 138 </script> | 118 </script> |
| OLD | NEW |