| Index: mojo/public/js/router.js
|
| diff --git a/mojo/public/js/router.js b/mojo/public/js/router.js
|
| index e3db0a6a7888252c5aa0768480333c6928f9877e..3f0d96bca5ebe63f3ec945c78413e7f87c291f12 100644
|
| --- a/mojo/public/js/router.js
|
| +++ b/mojo/public/js/router.js
|
| @@ -24,6 +24,7 @@ define("mojo/public/js/router", [
|
| this.nextRequestID_ = 0;
|
| this.completers_ = new Map();
|
| this.payloadValidators_ = [];
|
| + this.testingController_ = null;
|
|
|
| this.connector_.setIncomingReceiver({
|
| accept: this.handleIncomingMessage_.bind(this),
|
| @@ -36,6 +37,7 @@ define("mojo/public/js/router", [
|
| Router.prototype.close = function() {
|
| this.completers_.clear(); // Drop any responders.
|
| this.connector_.close();
|
| + this.testingController_ = null;
|
| };
|
|
|
| Router.prototype.accept = function(message) {
|
| @@ -81,6 +83,11 @@ define("mojo/public/js/router", [
|
| return this.connector_.encounteredError();
|
| };
|
|
|
| + Router.prototype.enableTestingMode = function() {
|
| + this.testingController_ = new RouterTestingController(this.connector_);
|
| + return this.testingController_;
|
| + };
|
| +
|
| Router.prototype.handleIncomingMessage_ = function(message) {
|
| var noError = validator.validationError.NONE;
|
| var messageValidator = new Validator(message);
|
| @@ -95,6 +102,9 @@ define("mojo/public/js/router", [
|
| };
|
|
|
| Router.prototype.handleValidIncomingMessage_ = function(message) {
|
| + if (this.testingController_)
|
| + return;
|
| +
|
| if (message.expectsResponse()) {
|
| if (this.incomingReceiver_) {
|
| this.incomingReceiver_.acceptWithResponder(message, this);
|
| @@ -113,11 +123,20 @@ define("mojo/public/js/router", [
|
| if (this.incomingReceiver_)
|
| this.incomingReceiver_.accept(message);
|
| }
|
| - }
|
| + };
|
|
|
| Router.prototype.handleInvalidIncomingMessage_ = function(message, error) {
|
| - this.close();
|
| - }
|
| + if (!this.testingController_) {
|
| + // TODO(yzshen): Consider logging and notifying the embedder.
|
| + // TODO(yzshen): This should also trigger connection error handler.
|
| + // Consider making accept() return a boolean and let the connector deal
|
| + // with this, as the C++ code does.
|
| + this.close();
|
| + return;
|
| + }
|
| +
|
| + this.testingController_.onInvalidIncomingMessage(error);
|
| + };
|
|
|
| Router.prototype.handleConnectionError_ = function(result) {
|
| this.completers_.forEach(function(value) {
|
| @@ -128,25 +147,30 @@ define("mojo/public/js/router", [
|
| this.close();
|
| };
|
|
|
| - // The TestRouter subclass is only intended to be used in unit tests.
|
| - // It defeats valid message handling and delgates invalid message handling.
|
| + // The RouterTestingController is used in unit tests. It defeats valid message
|
| + // handling and delgates invalid message handling.
|
|
|
| - function TestRouter(handle, connectorFactory) {
|
| - Router.call(this, handle, connectorFactory);
|
| + function RouterTestingController(connector) {
|
| + this.connector_ = connector;
|
| + this.invalidMessageHandler_ = null;
|
| }
|
|
|
| - TestRouter.prototype = Object.create(Router.prototype);
|
| + RouterTestingController.prototype.waitForNextMessage = function() {
|
| + this.connector_.waitForNextMessageForTesting();
|
| + };
|
|
|
| - TestRouter.prototype.handleValidIncomingMessage_ = function() {
|
| + RouterTestingController.prototype.setInvalidIncomingMessageHandler =
|
| + function(callback) {
|
| + this.invalidMessageHandler_ = callback;
|
| };
|
|
|
| - TestRouter.prototype.handleInvalidIncomingMessage_ =
|
| - function(message, error) {
|
| - this.validationErrorHandler(error);
|
| - };
|
| + RouterTestingController.prototype.onInvalidIncomingMessage =
|
| + function(error) {
|
| + if (this.invalidMessageHandler_)
|
| + this.invalidMessageHandler_(error);
|
| + };
|
|
|
| var exports = {};
|
| exports.Router = Router;
|
| - exports.TestRouter = TestRouter;
|
| return exports;
|
| });
|
|
|