Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(547)

Unified Diff: mojo/edk/js/tests/binding_tests.js

Issue 2549683002: Mojo JS bindings: introduce concepts that are more similar to C++ bindings: (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mojo/edk/js/tests/binding_tests.js
diff --git a/mojo/edk/js/tests/binding_tests.js b/mojo/edk/js/tests/binding_tests.js
new file mode 100644
index 0000000000000000000000000000000000000000..3c5f01c2f6653558f1e86d6c9782356d13d52f70
--- /dev/null
+++ b/mojo/edk/js/tests/binding_tests.js
@@ -0,0 +1,80 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+define([
+ "gin/test/expect",
+ "mojo/public/js/bindings",
+ "mojo/public/interfaces/bindings/tests/math_calculator.mojom",
+ "mojo/public/js/threading",
+ "gc",
+], function(expect,
+ bindings,
+ math,
+ threading,
+ gc) {
+ testIsBound()
+ .then(testReusable)
+ .then(function() {
+ this.result = "PASS";
+ gc.collectGarbage(); // should not crash
+ threading.quit();
+ }.bind(this)).catch(function(e) {
+ this.result = "FAIL: " + (e.stack || e);
+ threading.quit();
+ }.bind(this));
+
+ 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 testIsBound() {
+ var binding = new bindings.Binding(math.Calculator, new CalculatorImpl());
+ expect(binding.isBound()).toBeFalsy();
+
+ var calcPtr = new math.CalculatorPtr();
+ var request = bindings.makeRequest(calcPtr);
+ binding.bind(request);
+ expect(binding.isBound()).toBeTruthy();
+
+ binding.close();
+ expect(binding.isBound()).toBeFalsy();
+
+ return Promise.resolve();
+ }
+
+ function testReusable() {
+ var calcPtr1 = new math.CalculatorPtr();
+ var calcPtr2 = new math.CalculatorPtr();
+
+ var calcBinding = new bindings.Binding(math.Calculator,
+ new CalculatorImpl(),
+ bindings.makeRequest(calcPtr1));
+
+ var promise = calcPtr1.add(2).then(function(response) {
+ expect(response.value).toBe(2);
+ calcBinding.bind(bindings.makeRequest(calcPtr2));
+ return calcPtr2.add(2);
+ }).then(function(response) {
+ expect(response.value).toBe(4);
+ return Promise.resolve();
+ });
+
+ return promise;
+ }
+});

Powered by Google App Engine
This is Rietveld 408576698