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