Chromium Code Reviews| Index: third_party/WebKit/LayoutTests/mojo/binding.html |
| diff --git a/third_party/WebKit/LayoutTests/mojo/binding.html b/third_party/WebKit/LayoutTests/mojo/binding.html |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..121d0abc3354f3f0b8efdcfbbfd4db44f31aec5a |
| --- /dev/null |
| +++ b/third_party/WebKit/LayoutTests/mojo/binding.html |
| @@ -0,0 +1,149 @@ |
| +<!DOCTYPE html> |
| +<script src="../resources/testharness.js"></script> |
| +<script src="../resources/testharnessreport.js"></script> |
| +<script src="../resources/mojo-helpers.js"></script> |
| +<script> |
| +'use strict'; |
| + |
| +function CalculatorImpl() { |
| + this.total = 0; |
| +} |
| + |
| +CalculatorImpl.prototype.clear = function() { |
| + this.total = 0; |
| + return Promise.resolve({value: this.total}); |
| +}; |
| + |
| +CalculatorImpl.prototype.add = function(value) { |
| + this.total += value; |
| + return Promise.resolve({value: this.total}); |
| +}; |
| + |
| +CalculatorImpl.prototype.multiply = function(value) { |
| + this.total *= value; |
| + return Promise.resolve({value: this.total}); |
| +}; |
| + |
| +function loadModules(name, func) { |
| + return define( |
| + name, |
| + [ |
| + 'mojo/public/js/bindings', |
| + 'mojo/public/interfaces/bindings/tests/math_calculator.mojom' |
| + ], |
| + func); |
| +}; |
| + |
| +function binding_test(func, name, properties) { |
| + promise_test(() => { |
| + return loadModules(name, func); |
| + }, name, properties); |
| +} |
| + |
| +binding_test((bindings, math) => { |
| + var binding = new bindings.Binding(math.Calculator, new CalculatorImpl()); |
| + assert_false(binding.isBound()); |
| + |
| + var calc = new math.CalculatorPtr(); |
| + var request = bindings.makeRequest(calc); |
| + binding.bind(request); |
| + assert_true(binding.isBound()); |
| + |
| + binding.close(); |
| + assert_false(binding.isBound()); |
| +}, 'is bound'); |
| + |
| +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
|
| + var calc1 = new math.CalculatorPtr(); |
| + var calc2 = new math.CalculatorPtr(); |
| + |
| + var calcBinding = new bindings.Binding(math.Calculator, |
| + new CalculatorImpl(), |
| + bindings.makeRequest(calc1)); |
| + |
| + var promise = calc1.add(2).then(function(response) { |
| + assert_equals(response.value, 2); |
| + calcBinding.bind(bindings.makeRequest(calc2)); |
| + return calc2.add(2); |
| + }).then(function(response) { |
| + assert_equals(response.value, 4); |
| + }); |
| + |
| + return promise; |
| +}, 'reusable'); |
| + |
| +binding_test((bindings, math) => { |
| + var calc = new math.CalculatorPtr(); |
| + var calcBinding = new bindings.Binding(math.Calculator, |
| + new CalculatorImpl(), |
| + bindings.makeRequest(calc)); |
| + |
| + var promise = new Promise(function(resolve, reject) { |
| + calcBinding.setConnectionErrorHandler(function() { |
| + resolve(); |
| + }); |
| + calc.ptr.reset(); |
| + }); |
| + |
| + return promise; |
| +}, 'connection error'); |
| + |
| +binding_test((bindings, math) => { |
| + var calc = new math.CalculatorPtr(); |
| + var calcBinding = new bindings.Binding(math.Calculator, |
| + new CalculatorImpl(), |
| + bindings.makeRequest(calc)); |
| + var newCalcBinding = null; |
| + |
| + var promise = calc.add(2).then(function(response) { |
| + assert_equals(response.value, 2); |
| + var interfaceRequest = calcBinding.unbind(); |
| + assert_false(calcBinding.isBound()); |
| + newCalcBinding = new bindings.Binding(math.Calculator, |
| + new CalculatorImpl(), |
| + interfaceRequest); |
| + return calc.add(2); |
| + }).then(function(response) { |
| + assert_equals(response.value, 2); |
| + }); |
| + |
| + return promise; |
| +}, 'unbind'); |
| + |
| +binding_test((bindings, math) => { |
| + var calc1 = new math.CalculatorPtr(); |
| + var calc2 = new math.CalculatorPtr(); |
| + var calcImpl = new CalculatorImpl(); |
| + |
| + var bindingSet = new bindings.BindingSet(math.Calculator); |
| + assert_true(bindingSet.isEmpty()); |
| + bindingSet.addBinding(calcImpl, bindings.makeRequest(calc1)); |
| + bindingSet.addBinding(calcImpl, bindings.makeRequest(calc2)); |
| + assert_false(bindingSet.isEmpty()); |
| + |
| + var promise = calc1.add(3).then(function(response) { |
| + assert_equals(response.value, 3); |
| + return calc2.add(4); |
| + }).then(function(response) { |
| + assert_equals(response.value, 7); |
| + |
| + var promiseOfConnectionError = new Promise(function(resolve, reject) { |
| + bindingSet.setConnectionErrorHandler(function() { |
| + resolve(); |
| + }); |
| + }); |
| + calc1.ptr.reset(); |
| + return promiseOfConnectionError; |
| + }).then(function() { |
| + return calc2.add(5); |
| + }).then(function(response) { |
| + assert_equals(response.value, 12); |
| + |
| + bindingSet.closeAllBindings(); |
| + assert_true(bindingSet.isEmpty()); |
| + }); |
| + |
| + return promise; |
| +}, 'binding set'); |
| + |
| +</script> |