sku-pg
Version:
Skulpt is a Javascript implementation of Python 2.x. Python that runs in your browser!
229 lines (181 loc) • 5.47 kB
HTML
<html>
<!--
Copyright 2013 The Closure Library Authors. All Rights Reserved.
Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>
Closure Unit Tests - goog.labs.net.webChannel.WebChannelBaseTransport
</title>
<script type="text/javascript" src="../../../base.js"></script>
<script type="text/javascript">
goog.require('goog.events');
goog.require('goog.functions');
goog.require('goog.labs.net.webChannel.WebChannelBaseTransport');
goog.require('goog.net.WebChannel');
goog.require('goog.testing.PropertyReplacer');
goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<!-- Define unit tests. -->
<script type="text/javascript" >
var webChannel;
var testUrl = 'http://127.0.0.1:8080/test';
var propertyReplacer = new goog.testing.PropertyReplacer();
function setUp() {
propertyReplacer.set(goog.labs.net.webChannel, 'WebChannelBase',
MockWebChannelBase);
}
function tearDown() {
propertyReplacer.reset();
goog.dispose(webChannel);
}
function testOpenWithUrl() {
var webChannelTransport =
new goog.labs.net.webChannel.WebChannelBaseTransport();
webChannel = webChannelTransport.createWebChannel(testUrl);
var eventFired = false;
goog.events.listen(webChannel, goog.net.WebChannel.EventType.OPEN,
function(e) {
eventFired= true;
});
webChannel.open();
assertFalse(eventFired);
var channel = webChannel.channel_;
assertNotNull(channel);
simulateOpenEvent(channel);
assertTrue(eventFired);
}
function testOpenThenCloseChannel() {
var webChannelTransport =
new goog.labs.net.webChannel.WebChannelBaseTransport();
webChannel = webChannelTransport.createWebChannel(testUrl);
var eventFired = false;
goog.events.listen(webChannel, goog.net.WebChannel.EventType.CLOSE,
function(e) {
eventFired= true;
});
webChannel.open();
assertFalse(eventFired);
var channel = webChannel.channel_;
assertNotNull(channel);
simulateCloseEvent(channel);
assertTrue(eventFired);
}
function testChannelError() {
var webChannelTransport =
new goog.labs.net.webChannel.WebChannelBaseTransport();
webChannel = webChannelTransport.createWebChannel(testUrl);
var eventFired = false;
goog.events.listen(webChannel, goog.net.WebChannel.EventType.ERROR,
function(e) {
eventFired= true;
assertEquals(goog.net.WebChannel.ErrorStatus.NETWORK_ERROR, e.status)
});
webChannel.open();
assertFalse(eventFired);
var channel = webChannel.channel_;
assertNotNull(channel);
simulateErrorEvent(channel);
assertTrue(eventFired);
}
function testChannelMessage() {
var webChannelTransport =
new goog.labs.net.webChannel.WebChannelBaseTransport();
webChannel = webChannelTransport.createWebChannel(testUrl);
var eventFired = false;
var data = "foo";
goog.events.listen(webChannel, goog.net.WebChannel.EventType.MESSAGE,
function(e) {
eventFired= true;
assertEquals(e.data, data);
});
webChannel.open();
assertFalse(eventFired);
var channel = webChannel.channel_;
assertNotNull(channel);
simulateMessageEvent(channel, data);
assertTrue(eventFired);
}
/**
* Simulates the WebChannelBase firing the open event for the given channel.
* @param {!MockWebChannelBase} bc The mock WebChannelBase.
*/
function simulateOpenEvent(bc) {
assertNotNull(bc.getHandler());
bc.getHandler().channelOpened();
}
/**
* Simulates the WebChannelBase firing the close event for the given channel.
* @param {!MockWebChannelBase} bc The mock WebChannelBase.
*/
function simulateCloseEvent(bc) {
assertNotNull(bc.getHandler());
bc.getHandler().channelClosed();
}
/**
* Simulates the WebChannelBase firing the error event for the given channel.
* @param {!MockWebChannelBase} bc The mock WebChannelBase.
*/
function simulateErrorEvent(bc) {
assertNotNull(bc.getHandler());
bc.getHandler().channelError();
}
/**
* Simulates the WebChannelBase firing the message event for the given channel.
* @param {!MockWebChannelBase} bc The mock WebChannelBase.
* @param {String} data The message data.
*/
function simulateMessageEvent(bc, data) {
assertNotNull(bc.getHandler());
bc.getHandler().channelHandleArray(bc, data);
}
/**
* Mock WebChannelBase constructor.
* @constructor
*/
MockWebChannelBase = function() {
/** @private {?goog.labs.net.webChannel.WebChannelBase.Handler} */
this.handler_ = null;
};
/**
* Mocks out the setHandler method of the WebChannelBase.
*/
MockWebChannelBase.prototype.setHandler = function(handler) {
this.handler_ = handler;
};
/**
* Mocks out the getHandler method of the WebChannelBase.
*
* @return {?goog.labs.net.webChannel.WebChannelBase.Handler} The handler.
*/
MockWebChannelBase.prototype.getHandler = function(handler) {
return this.handler_;
};
/**
* Mocks out the connect method of the WebChannelBase.
*/
MockWebChannelBase.prototype.connect = function(testPath, channelPath) {
// Nothing to do here.
};
/**
* Mocks out the disconnect method of the WebChannelBase.
*/
MockWebChannelBase.prototype.disconnect = function() {
// Nothing to do here.
};
/**
* Mocks out the sendMap method of the WebChannelBase.
*/
MockWebChannelBase.prototype.sendMap = function(message) {
// Nothing to do here.
};
</script>
</body>
</html>