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

Unified Diff: mojo/apps/js/bindings/connection_unittests.js

Issue 223043006: Mojo: Use Promises for request/response calls in the JavaScript bindings. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 9 months 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
« no previous file with comments | « no previous file | mojo/apps/js/test/run_apps_js_tests.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/apps/js/bindings/connection_unittests.js
diff --git a/mojo/apps/js/bindings/connection_unittests.js b/mojo/apps/js/bindings/connection_unittests.js
index c65e4273aa64065f0d52ecf4fe3ce418ca5a49dd..3a917b4252448dbb21daec8a654a3add14690206 100644
--- a/mojo/apps/js/bindings/connection_unittests.js
+++ b/mojo/apps/js/bindings/connection_unittests.js
@@ -3,7 +3,7 @@
// found in the LICENSE file.
// Mock out the support module to avoid depending on the message loop.
-define("mojo/bindings/js/support", function() {
+define("mojo/bindings/js/support", ["timer"], function(timer) {
var waitingCallbacks = [];
function WaitCookie(id) {
@@ -38,11 +38,18 @@ define("mojo/bindings/js/support", function() {
}
}
+ // Queue up a pumpOnce call to execute after the stack unwinds. Use
+ // this to trigger a pump after all Promises are executed.
+ function queuePump(result) {
+ timer.createOneShot(0, pumpOnce.bind(undefined, result));
+ }
+
var exports = {};
exports.asyncWait = asyncWait;
exports.cancelWait = cancelWait;
exports.numberOfWaitingCallbacks = numberOfWaitingCallbacks;
exports.pumpOnce = pumpOnce;
+ exports.queuePump = queuePump;
return exports;
});
@@ -53,6 +60,7 @@ define([
"mojo/public/js/bindings/connection",
"mojo/public/interfaces/bindings/tests/sample_interfaces.mojom",
"mojo/public/interfaces/bindings/tests/sample_service.mojom",
+ "mojo/apps/js/bindings/threading",
"gc",
], function(expect,
mockSupport,
@@ -60,12 +68,18 @@ define([
connection,
sample_interfaces,
sample_service,
+ threading,
gc) {
testClientServer();
testWriteToClosedPipe();
- testRequestResponse();
- this.result = "PASS";
- gc.collectGarbage(); // should not crash
+ testRequestResponse().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 testClientServer() {
var receivedFrobinate = false;
@@ -184,12 +198,14 @@ define([
ProviderImpl.prototype =
Object.create(sample_interfaces.ProviderStub.prototype);
- ProviderImpl.prototype.echoString = function(a, callback) {
- callback(a);
+ ProviderImpl.prototype.echoString = function(a) {
+ mockSupport.queuePump(core.RESULT_OK);
+ return Promise.resolve({a: a});
};
- ProviderImpl.prototype.echoStrings = function(a, b, callback) {
- callback(a, b);
+ ProviderImpl.prototype.echoStrings = function(a, b) {
+ mockSupport.queuePump(core.RESULT_OK);
+ return Promise.resolve({a: a, b: b});
};
// ProviderClientImpl ------------------------------------------------------
@@ -201,12 +217,6 @@ define([
ProviderClientImpl.prototype =
Object.create(sample_interfaces.ProviderClientStub.prototype);
- ProviderClientImpl.prototype.didFrobinate = function(result) {
- receivedDidFrobinate = true;
-
- expect(result).toBe(42);
- };
-
var pipe = core.createMessagePipe();
var connection0 = new connection.Connection(
@@ -215,26 +225,17 @@ define([
var connection1 = new connection.Connection(
pipe.handle1, ProviderClientImpl, sample_interfaces.ProviderProxy);
- var echoedString;
-
// echoString
-
- connection1.remote.echoString("hello", function(a) {
- echoedString = a;
+ mockSupport.queuePump(core.RESULT_OK);
+ return connection1.remote.echoString("hello").then(function(response) {
+ expect(response.a).toBe("hello");
+ }).then(function() {
+ // echoStrings
+ mockSupport.queuePump(core.RESULT_OK);
+ return connection1.remote.echoStrings("hello", "world");
+ }).then(function(response) {
+ expect(response.a).toBe("hello");
+ expect(response.b).toBe("world");
});
-
- mockSupport.pumpOnce(core.RESULT_OK);
-
- expect(echoedString).toBe("hello");
-
- // echoStrings
-
- connection1.remote.echoStrings("hello", "world", function(a, b) {
- echoedString = a + " " + b;
- });
-
- mockSupport.pumpOnce(core.RESULT_OK);
-
- expect(echoedString).toBe("hello world");
}
});
« no previous file with comments | « no previous file | mojo/apps/js/test/run_apps_js_tests.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698