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(testReusable) | |
18 .then(testConnectionError) | |
19 .then(testUnbind) | |
20 .then(testBindingSet) | |
21 .then(function() { | |
22 this.result = "PASS"; | |
23 gc.collectGarbage(); // should not crash | |
24 threading.quit(); | |
25 }.bind(this)).catch(function(e) { | |
26 this.result = "FAIL: " + (e.stack || e); | |
27 threading.quit(); | |
28 }.bind(this)); | |
29 | |
30 function CalculatorImpl() { | |
31 this.total = 0; | |
32 } | |
33 | |
34 CalculatorImpl.prototype.clear = function() { | |
35 this.total = 0; | |
36 return Promise.resolve({value: this.total}); | |
37 }; | |
38 | |
39 CalculatorImpl.prototype.add = function(value) { | |
40 this.total += value; | |
41 return Promise.resolve({value: this.total}); | |
42 }; | |
43 | |
44 CalculatorImpl.prototype.multiply = function(value) { | |
45 this.total *= value; | |
46 return Promise.resolve({value: this.total}); | |
47 }; | |
48 | |
49 function testIsBound() { | |
50 var binding = new bindings.Binding(math.Calculator, new CalculatorImpl()); | |
51 expect(binding.isBound()).toBeFalsy(); | |
52 | |
53 var calc = new math.CalculatorPtr(); | |
54 var request = bindings.makeRequest(calc); | |
55 binding.bind(request); | |
56 expect(binding.isBound()).toBeTruthy(); | |
57 | |
58 binding.close(); | |
59 expect(binding.isBound()).toBeFalsy(); | |
60 | |
61 return Promise.resolve(); | |
62 } | |
63 | |
64 function testReusable() { | |
65 var calc1 = new math.CalculatorPtr(); | |
66 var calc2 = new math.CalculatorPtr(); | |
67 | |
68 var calcBinding = new bindings.Binding(math.Calculator, | |
69 new CalculatorImpl(), | |
70 bindings.makeRequest(calc1)); | |
71 | |
72 var promise = calc1.add(2).then(function(response) { | |
73 expect(response.value).toBe(2); | |
74 calcBinding.bind(bindings.makeRequest(calc2)); | |
75 return calc2.add(2); | |
76 }).then(function(response) { | |
77 expect(response.value).toBe(4); | |
78 return Promise.resolve(); | |
79 }); | |
80 | |
81 return promise; | |
82 } | |
83 | |
84 function testConnectionError() { | |
85 var calc = new math.CalculatorPtr(); | |
86 var calcBinding = new bindings.Binding(math.Calculator, | |
87 new CalculatorImpl(), | |
88 bindings.makeRequest(calc)); | |
89 | |
90 var promise = new Promise(function(resolve, reject) { | |
91 calcBinding.setConnectionErrorHandler(function() { | |
92 resolve(); | |
93 }); | |
94 calc.ptr.reset(); | |
95 }); | |
96 | |
97 return promise; | |
98 } | |
99 | |
100 function testUnbind() { | |
101 var calc = new math.CalculatorPtr(); | |
102 var calcBinding = new bindings.Binding(math.Calculator, | |
103 new CalculatorImpl(), | |
104 bindings.makeRequest(calc)); | |
105 var newCalcBinding = null; | |
106 | |
107 var promise = calc.add(2).then(function(response) { | |
108 expect(response.value).toBe(2); | |
109 var interfaceRequest = calcBinding.unbind(); | |
110 expect(calcBinding.isBound()).toBeFalsy(); | |
111 newCalcBinding = new bindings.Binding(math.Calculator, | |
112 new CalculatorImpl(), | |
113 interfaceRequest); | |
114 return calc.add(2); | |
115 }).then(function(response) { | |
116 expect(response.value).toBe(2); | |
117 return Promise.resolve(); | |
118 }); | |
119 | |
120 return promise; | |
121 } | |
122 | |
123 function testBindingSet() { | |
124 var calc1 = new math.CalculatorPtr(); | |
125 var calc2 = new math.CalculatorPtr(); | |
126 var calcImpl = new CalculatorImpl(); | |
127 | |
128 var bindingSet = new bindings.BindingSet(math.Calculator); | |
129 expect(bindingSet.isEmpty()).toBeTruthy(); | |
130 bindingSet.addBinding(calcImpl, bindings.makeRequest(calc1)); | |
131 bindingSet.addBinding(calcImpl, bindings.makeRequest(calc2)); | |
132 expect(bindingSet.isEmpty()).toBeFalsy(); | |
133 | |
134 var promise = calc1.add(3).then(function(response) { | |
135 expect(response.value).toBe(3); | |
136 return calc2.add(4); | |
137 }).then(function(response) { | |
138 expect(response.value).toBe(7); | |
139 | |
140 var promiseOfConnectionError = new Promise(function(resolve, reject) { | |
141 bindingSet.setConnectionErrorHandler(function() { | |
142 resolve(); | |
143 }); | |
144 }); | |
145 calc1.ptr.reset(); | |
146 return promiseOfConnectionError; | |
147 }).then(function() { | |
148 return calc2.add(5); | |
149 }).then(function(response) { | |
150 expect(response.value).toBe(12); | |
151 | |
152 bindingSet.closeAllBindings(); | |
153 expect(bindingSet.isEmpty()).toBeTruthy(); | |
154 return Promise.resolve(); | |
155 }); | |
156 | |
157 return promise; | |
158 } | |
159 }); | |
OLD | NEW |