diff options
author | abarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-26 10:49:12 +0000 |
---|---|---|
committer | abarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-26 10:49:12 +0000 |
commit | 25f12f3fce480f8d7f662ad87f18455b21791337 (patch) | |
tree | e613fb7e46494fcbab024541c9c72c43882e38f8 /mojo | |
parent | 03fa9e6e6e1907e00daffc36418326de7bc66ce0 (diff) | |
download | chromium_src-25f12f3fce480f8d7f662ad87f18455b21791337.zip chromium_src-25f12f3fce480f8d7f662ad87f18455b21791337.tar.gz chromium_src-25f12f3fce480f8d7f662ad87f18455b21791337.tar.bz2 |
[Mojo] Add a unit test for connector.js
This CL adds a unit test for connector.js. It shows that we can use the
connector to route messages over IPC channels within the same process.
R=davemoore@chromium.org
BUG=317398
Review URL: https://codereview.chromium.org/85763005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@237304 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo')
-rw-r--r-- | mojo/public/bindings/js/connector.js | 3 | ||||
-rw-r--r-- | mojo/public/bindings/js/connector_unittests.js | 125 | ||||
-rw-r--r-- | mojo/public/bindings/js/test/run_js_tests.cc | 4 |
3 files changed, 130 insertions, 2 deletions
diff --git a/mojo/public/bindings/js/connector.js b/mojo/public/bindings/js/connector.js index c9d0566..06c351b 100644 --- a/mojo/public/bindings/js/connector.js +++ b/mojo/public/bindings/js/connector.js @@ -79,9 +79,8 @@ define([ function Connection(handle, localFactory, remoteFactory) { this.connector_ = new Connector(handle); - this.local = new localFactory(); this.remote = new remoteFactory(this.connector_); - + this.local = new localFactory(this.remote); this.connector_.setIncomingReceiver(this.local); } diff --git a/mojo/public/bindings/js/connector_unittests.js b/mojo/public/bindings/js/connector_unittests.js new file mode 100644 index 0000000..b3d1638 --- /dev/null +++ b/mojo/public/bindings/js/connector_unittests.js @@ -0,0 +1,125 @@ +// Copyright 2013 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. + +// Mock out the support module to avoid depending on the message loop. +define("mojo/public/bindings/js/support", function() { + var waitingCallbacks = []; + + function WaitCookie(id) { + this.id = id; + } + + function asyncWait(handle, flags, callback) { + var id = waitingCallbacks.length; + waitingCallbacks.push(callback); + return new WaitCookie(id); + } + + function cancelWait(cookie) { + waitingCallbacks[cookie.id] = null; + } + + function numberOfWaitingCallbacks() { + var count = 0; + for (var i = 0; i < waitingCallbacks.length; ++i) { + if (waitingCallbacks[i]) + ++count; + } + return count; + } + + function pumpOnce(result) { + var callbacks = waitingCallbacks; + waitingCallbacks = []; + for (var i = 0; i < callbacks.length; ++i) + callbacks[i](result); + } + + var exports = {}; + exports.asyncWait = asyncWait; + exports.cancelWait = cancelWait; + exports.numberOfWaitingCallbacks = numberOfWaitingCallbacks; + exports.pumpOnce = pumpOnce; + return exports; +}); + +define([ + "gin/test/expect", + "mojo/public/bindings/js/support", + "mojo/public/bindings/js/core", + "mojo/public/bindings/js/connector", + "mojom/sample_service", +], function(expect, mockSupport, core, connector, sample) { + + var receivedFrobinate = false; + var receivedDidFrobinate = false; + + // ServiceImpl -------------------------------------------------------------- + + function ServiceImpl(peer) { + this.peer = peer; + } + + ServiceImpl.prototype = Object.create(sample.ServiceStub.prototype); + + ServiceImpl.prototype.frobinate = function(foo, baz, port) { + receivedFrobinate = true; + + expect(foo.name).toBe("Example name"); + expect(baz).toBeTruthy(); + expect(core.close(port)).toBe(core.RESULT_OK); + + this.peer.didFrobinate(42); + }; + + // ServiceImpl -------------------------------------------------------------- + + function ServiceClientImpl(peer) { + this.peer = peer; + } + + ServiceClientImpl.prototype = + Object.create(sample.ServiceClientStub.prototype); + + ServiceClientImpl.prototype.didFrobinate = function(result) { + receivedDidFrobinate = true; + + expect(result).toBe(42); + }; + + var pipe = core.createMessagePipe(); + var anotherPipe = core.createMessagePipe(); + + var connection0 = new connector.Connection( + pipe.handle0, ServiceImpl, sample.ServiceClientProxy); + + var connection1 = new connector.Connection( + pipe.handle1, ServiceClientImpl, sample.ServiceProxy); + + var foo = new sample.Foo(); + foo.bar = new sample.Bar(); + foo.name = "Example name"; + connection1.remote.frobinate(foo, true, anotherPipe.handle0); + + mockSupport.pumpOnce(core.RESULT_OK); + + expect(receivedFrobinate).toBeTruthy(); + expect(receivedDidFrobinate).toBeTruthy(); + + connection0.close(); + connection1.close(); + + expect(mockSupport.numberOfWaitingCallbacks()).toBe(0); + + // anotherPipe.handle0 was closed automatically when sent over IPC. + expect(core.close(anotherPipe.handle0)).toBe(core.RESULT_INVALID_ARGUMENT); + // anotherPipe.handle1 hasn't been closed yet. + expect(core.close(anotherPipe.handle1)).toBe(core.RESULT_OK); + + // The Connection object is responsible for closing these handles. + expect(core.close(pipe.handle0)).toBe(core.RESULT_INVALID_ARGUMENT); + expect(core.close(pipe.handle1)).toBe(core.RESULT_INVALID_ARGUMENT); + + this.result = "PASS"; +}); diff --git a/mojo/public/bindings/js/test/run_js_tests.cc b/mojo/public/bindings/js/test/run_js_tests.cc index c558547..e9a52ed 100644 --- a/mojo/public/bindings/js/test/run_js_tests.cc +++ b/mojo/public/bindings/js/test/run_js_tests.cc @@ -57,6 +57,10 @@ TEST(JSTest, sample_test) { gin::RunTestFromFile(path, &delegate); } +TEST(JSTest, connector) { + RunTest("connector_unittests.js"); +} + } // namespace } // namespace js } // namespace mojo |