| 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> |
| 6 <script src="file:///gen/mojo/public/interfaces/bindings/tests/sample_interfaces
.mojom.js"></script> |
| 5 <script> | 7 <script> |
| 6 'use strict'; | 8 'use strict'; |
| 7 | 9 |
| 8 setup({ explicit_done: true }); | 10 function CalculatorImpl() { |
| 11 this.total = 0; |
| 12 } |
| 9 | 13 |
| 10 define([ | 14 CalculatorImpl.prototype.clear = function() { |
| 11 "mojo/public/js/bindings", | 15 this.total = 0; |
| 12 "mojo/public/js/core", | 16 return Promise.resolve({value: this.total}); |
| 13 "mojo/public/interfaces/bindings/tests/math_calculator.mojom", | 17 }; |
| 14 "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom", | |
| 15 ], function(bindings, core, math, sampleInterfaces) { | |
| 16 | 18 |
| 17 function CalculatorImpl() { | 19 CalculatorImpl.prototype.add = function(value) { |
| 18 this.total = 0; | 20 this.total += value; |
| 19 } | 21 return Promise.resolve({value: this.total}); |
| 22 }; |
| 20 | 23 |
| 21 CalculatorImpl.prototype.clear = function() { | 24 CalculatorImpl.prototype.multiply = function(value) { |
| 22 this.total = 0; | 25 this.total *= value; |
| 23 return Promise.resolve({value: this.total}); | 26 return Promise.resolve({value: this.total}); |
| 24 }; | 27 }; |
| 25 | 28 |
| 26 CalculatorImpl.prototype.add = function(value) { | 29 function IntegerAccessorImpl() { |
| 27 this.total += value; | 30 this.integer = 0; |
| 28 return Promise.resolve({value: this.total}); | 31 } |
| 29 }; | |
| 30 | 32 |
| 31 CalculatorImpl.prototype.multiply = function(value) { | 33 IntegerAccessorImpl.prototype.getInteger = function() { |
| 32 this.total *= value; | 34 return Promise.resolve({data: this.integer}); |
| 33 return Promise.resolve({value: this.total}); | 35 }; |
| 34 }; | |
| 35 | 36 |
| 36 function IntegerAccessorImpl() { | 37 IntegerAccessorImpl.prototype.setInteger = function(value) { |
| 37 this.integer = 0; | 38 this.integer = value; |
| 38 } | 39 }; |
| 39 | 40 |
| 40 IntegerAccessorImpl.prototype.getInteger = function() { | 41 test(() => { |
| 41 return Promise.resolve({data: this.integer}); | 42 var calc = new math.CalculatorPtr(); |
| 42 }; | 43 assert_false(calc.ptr.isBound()); |
| 43 | 44 |
| 44 IntegerAccessorImpl.prototype.setInteger = function(value) { | 45 var request = mojo.makeRequest(calc); |
| 45 this.integer = value; | 46 assert_true(calc.ptr.isBound()); |
| 46 }; | |
| 47 | 47 |
| 48 test(() => { | 48 calc.ptr.reset(); |
| 49 var calc = new math.CalculatorPtr(); | 49 assert_false(calc.ptr.isBound()); |
| 50 assert_false(calc.ptr.isBound()); | 50 }, 'is bound'); |
| 51 | 51 |
| 52 var request = bindings.makeRequest(calc); | 52 promise_test(async () => { |
| 53 assert_true(calc.ptr.isBound()); | 53 var calc = new math.CalculatorPtr(); |
| 54 var calcBinding = new mojo.Binding( |
| 55 math.Calculator, new CalculatorImpl(), mojo.makeRequest(calc)); |
| 54 | 56 |
| 55 calc.ptr.reset(); | 57 assert_equals((await calc.add(2)).value, 2); |
| 56 assert_false(calc.ptr.isBound()); | 58 assert_equals((await calc.multiply(5)).value, 10); |
| 57 }, 'is bound'); | 59 assert_equals((await calc.clear()).value, 0); |
| 60 }, 'end to end'); |
| 58 | 61 |
| 59 promise_test(async () => { | 62 promise_test(async () => { |
| 60 var calc = new math.CalculatorPtr(); | 63 var calc = new math.CalculatorPtr(); |
| 61 var calcBinding = new bindings.Binding(math.Calculator, | 64 var calcImpl1 = new CalculatorImpl(); |
| 62 new CalculatorImpl(), | 65 var calcBinding1 = new mojo.Binding(math.Calculator, calcImpl1, |
| 63 bindings.makeRequest(calc)); | 66 mojo.makeRequest(calc)); |
| 67 var calcImpl2 = new CalculatorImpl(); |
| 68 var calcBinding2 = new mojo.Binding(math.Calculator, calcImpl2); |
| 64 | 69 |
| 65 assert_equals((await calc.add(2)).value, 2); | 70 assert_equals((await calc.add(2)).value, 2); |
| 66 assert_equals((await calc.multiply(5)).value, 10); | 71 calcBinding2.bind(mojo.makeRequest(calc)); |
| 67 assert_equals((await calc.clear()).value, 0); | 72 assert_equals((await calc.add(2)).value, 2); |
| 68 }, 'end to end'); | 73 assert_equals(calcImpl1.total, 2); |
| 74 assert_equals(calcImpl2.total, 2); |
| 75 }, 'reusable'); |
| 69 | 76 |
| 70 promise_test(async () => { | 77 promise_test(async () => { |
| 71 var calc = new math.CalculatorPtr(); | 78 var calc = new math.CalculatorPtr(); |
| 72 var calcImpl1 = new CalculatorImpl(); | 79 var calcBinding = new mojo.Binding(math.Calculator, new CalculatorImpl(), |
| 73 var calcBinding1 = new bindings.Binding(math.Calculator, | 80 mojo.makeRequest(calc)); |
| 74 calcImpl1, | |
| 75 bindings.makeRequest(calc)); | |
| 76 var calcImpl2 = new CalculatorImpl(); | |
| 77 var calcBinding2 = new bindings.Binding(math.Calculator, calcImpl2); | |
| 78 | 81 |
| 79 assert_equals((await calc.add(2)).value, 2); | 82 await new Promise((resolve, reject) => { |
| 80 calcBinding2.bind(bindings.makeRequest(calc)); | 83 calc.ptr.setConnectionErrorHandler(() => { resolve(); }); |
| 81 assert_equals((await calc.add(2)).value, 2); | 84 calcBinding.close(); |
| 82 assert_equals(calcImpl1.total, 2); | 85 }); |
| 83 assert_equals(calcImpl2.total, 2); | 86 }, 'connection error'); |
| 84 }, 'reusable'); | |
| 85 | 87 |
| 86 promise_test(async () => { | 88 promise_test(async () => { |
| 87 var calc = new math.CalculatorPtr(); | 89 var calc = new math.CalculatorPtr(); |
| 88 var calcBinding = new bindings.Binding(math.Calculator, | 90 var calcBinding = new mojo.Binding(math.Calculator, new CalculatorImpl(), |
| 89 new CalculatorImpl(), | 91 mojo.makeRequest(calc)); |
| 90 bindings.makeRequest(calc)); | |
| 91 | 92 |
| 92 await new Promise((resolve, reject) => { | 93 await new Promise((resolve, reject) => { |
| 93 calc.ptr.setConnectionErrorHandler(() => { resolve(); }); | 94 calc.ptr.setConnectionErrorHandler(({customReason, description}) => { |
| 94 calcBinding.close(); | 95 assert_equals(customReason, 42); |
| 96 assert_equals(description, 'hey'); |
| 97 resolve(); |
| 95 }); | 98 }); |
| 96 }, 'connection error'); | 99 calcBinding.closeWithReason({customReason: 42, description: 'hey'}); |
| 100 }); |
| 101 }, 'connection error with reason'); |
| 97 | 102 |
| 98 promise_test(async () => { | 103 promise_test(async () => { |
| 99 var calc = new math.CalculatorPtr(); | 104 var calc = new math.CalculatorPtr(); |
| 100 var calcBinding = new bindings.Binding(math.Calculator, | 105 var newCalc = null; |
| 101 new CalculatorImpl(), | 106 var calcBinding = new mojo.Binding(math.Calculator, new CalculatorImpl(), |
| 102 bindings.makeRequest(calc)); | 107 mojo.makeRequest(calc)); |
| 103 | 108 |
| 104 await new Promise((resolve, reject) => { | 109 assert_equals((await calc.add(2)).value, 2); |
| 105 calc.ptr.setConnectionErrorHandler(({custom_reason, description}) => { | 110 newCalc = new math.CalculatorPtr(); |
| 106 assert_equals(custom_reason, 42); | 111 newCalc.ptr.bind(calc.ptr.passInterface()); |
| 107 assert_equals(description, 'hey'); | 112 assert_false(calc.ptr.isBound()); |
| 108 resolve(); | 113 assert_equals((await newCalc.add(2)).value, 4); |
| 109 }); | 114 |
| 110 calcBinding.closeWithReason({custom_reason: 42, description: 'hey'}); | 115 }, 'pass interface'); |
| 116 |
| 117 promise_test(async () => { |
| 118 var pipe = Mojo.createMessagePipe(); |
| 119 var calc = new math.CalculatorPtr(pipe.handle0); |
| 120 var newCalc = null; |
| 121 var calcBinding = new mojo.Binding(math.Calculator, new CalculatorImpl(), |
| 122 pipe.handle1); |
| 123 |
| 124 assert_equals((await calc.add(2)).value, 2); |
| 125 |
| 126 }, 'bind raw handle'); |
| 127 |
| 128 promise_test(async () => { |
| 129 var integerAccessorPtr = new sample.IntegerAccessorPtr(); |
| 130 |
| 131 var integerAccessorBinding = new mojo.Binding( |
| 132 sample.IntegerAccessor, new IntegerAccessorImpl(), |
| 133 mojo.makeRequest(integerAccessorPtr)); |
| 134 assert_equals(integerAccessorPtr.ptr.version, 0); |
| 135 |
| 136 assert_equals(await integerAccessorPtr.ptr.queryVersion(), 3); |
| 137 assert_equals(integerAccessorPtr.ptr.version, 3); |
| 138 }, 'query version'); |
| 139 |
| 140 promise_test(async () => { |
| 141 var integerAccessorImpl = new IntegerAccessorImpl(); |
| 142 var integerAccessorPtr = new sample.IntegerAccessorPtr(); |
| 143 var integerAccessorBinding = new mojo.Binding( |
| 144 sample.IntegerAccessor, integerAccessorImpl, |
| 145 mojo.makeRequest(integerAccessorPtr)); |
| 146 |
| 147 // Inital version is 0. |
| 148 assert_equals(integerAccessorPtr.ptr.version, 0); |
| 149 |
| 150 integerAccessorPtr.ptr.requireVersion(1); |
| 151 assert_equals(integerAccessorPtr.ptr.version, 1); |
| 152 integerAccessorPtr.setInteger(123, sample.Enum.VALUE); |
| 153 assert_equals((await integerAccessorPtr.getInteger()).data, 123); |
| 154 |
| 155 integerAccessorPtr.ptr.requireVersion(3); |
| 156 assert_equals(integerAccessorPtr.ptr.version, 3); |
| 157 integerAccessorPtr.setInteger(456, sample.Enum.VALUE); |
| 158 assert_equals((await integerAccessorPtr.getInteger()).data, 456); |
| 159 |
| 160 // Require a version that is not supported by the impl side. |
| 161 integerAccessorPtr.ptr.requireVersion(4); |
| 162 assert_equals(integerAccessorPtr.ptr.version, 4); |
| 163 integerAccessorPtr.setInteger(789, sample.Enum.VALUE); |
| 164 |
| 165 await new Promise((resolve, reject) => { |
| 166 integerAccessorPtr.ptr.setConnectionErrorHandler(() => { |
| 167 resolve(); |
| 111 }); | 168 }); |
| 112 }, 'connection error with reason'); | 169 }); |
| 170 assert_equals(integerAccessorImpl.integer, 456); |
| 113 | 171 |
| 114 promise_test(async () => { | 172 }, 'require version'); |
| 115 var calc = new math.CalculatorPtr(); | |
| 116 var newCalc = null; | |
| 117 var calcBinding = new bindings.Binding(math.Calculator, | |
| 118 new CalculatorImpl(), | |
| 119 bindings.makeRequest(calc)); | |
| 120 | |
| 121 assert_equals((await calc.add(2)).value, 2); | |
| 122 newCalc = new math.CalculatorPtr(); | |
| 123 newCalc.ptr.bind(calc.ptr.passInterface()); | |
| 124 assert_false(calc.ptr.isBound()); | |
| 125 assert_equals((await newCalc.add(2)).value, 4); | |
| 126 | |
| 127 }, 'pass interface'); | |
| 128 | |
| 129 promise_test(async () => { | |
| 130 var pipe = core.createMessagePipe(); | |
| 131 var calc = new math.CalculatorPtr(pipe.handle0); | |
| 132 var newCalc = null; | |
| 133 var calcBinding = new bindings.Binding(math.Calculator, | |
| 134 new CalculatorImpl(), | |
| 135 pipe.handle1); | |
| 136 | |
| 137 assert_equals((await calc.add(2)).value, 2); | |
| 138 | |
| 139 }, 'bind raw handle'); | |
| 140 | |
| 141 promise_test(async () => { | |
| 142 var integerAccessorPtr = new sampleInterfaces.IntegerAccessorPtr(); | |
| 143 | |
| 144 var integerAccessorBinding = new bindings.Binding( | |
| 145 sampleInterfaces.IntegerAccessor, | |
| 146 new IntegerAccessorImpl(), | |
| 147 bindings.makeRequest(integerAccessorPtr)); | |
| 148 assert_equals(integerAccessorPtr.ptr.version, 0); | |
| 149 | |
| 150 assert_equals(await integerAccessorPtr.ptr.queryVersion(), 3); | |
| 151 assert_equals(integerAccessorPtr.ptr.version, 3); | |
| 152 }, 'query version'); | |
| 153 | |
| 154 promise_test(async () => { | |
| 155 var integerAccessorImpl = new IntegerAccessorImpl(); | |
| 156 var integerAccessorPtr = new sampleInterfaces.IntegerAccessorPtr(); | |
| 157 var integerAccessorBinding = new bindings.Binding( | |
| 158 sampleInterfaces.IntegerAccessor, | |
| 159 integerAccessorImpl, | |
| 160 bindings.makeRequest(integerAccessorPtr)); | |
| 161 | |
| 162 // Inital version is 0. | |
| 163 assert_equals(integerAccessorPtr.ptr.version, 0); | |
| 164 | |
| 165 integerAccessorPtr.ptr.requireVersion(1); | |
| 166 assert_equals(integerAccessorPtr.ptr.version, 1); | |
| 167 integerAccessorPtr.setInteger(123, sampleInterfaces.Enum.VALUE); | |
| 168 assert_equals((await integerAccessorPtr.getInteger()).data, 123); | |
| 169 | |
| 170 integerAccessorPtr.ptr.requireVersion(3); | |
| 171 assert_equals(integerAccessorPtr.ptr.version, 3); | |
| 172 integerAccessorPtr.setInteger(456, sampleInterfaces.Enum.VALUE); | |
| 173 assert_equals((await integerAccessorPtr.getInteger()).data, 456); | |
| 174 | |
| 175 // Require a version that is not supported by the impl side. | |
| 176 integerAccessorPtr.ptr.requireVersion(4); | |
| 177 assert_equals(integerAccessorPtr.ptr.version, 4); | |
| 178 integerAccessorPtr.setInteger(789, sampleInterfaces.Enum.VALUE); | |
| 179 | |
| 180 await new Promise((resolve, reject) => { | |
| 181 integerAccessorPtr.ptr.setConnectionErrorHandler(() => { | |
| 182 resolve(); | |
| 183 }); | |
| 184 }); | |
| 185 assert_equals(integerAccessorImpl.integer, 456); | |
| 186 | |
| 187 }, 'require version'); | |
| 188 | |
| 189 done(); | |
| 190 }); | |
| 191 | 173 |
| 192 </script> | 174 </script> |
| OLD | NEW |