Chromium Code Reviews| Index: extensions/test/data/serial_unittest.js |
| diff --git a/extensions/test/data/serial_unittest.js b/extensions/test/data/serial_unittest.js |
| index dc7bbea56e06cddd8453f3a73358d177fe90f9fb..7cdc1daa50d49b785afd7f7be4331ac0c9dc9f8d 100644 |
| --- a/extensions/test/data/serial_unittest.js |
| +++ b/extensions/test/data/serial_unittest.js |
| @@ -6,29 +6,69 @@ |
| * Unit tests for the JS serial service client. |
| * |
| * These test that configuration and data are correctly transmitted between the |
| - * client and the service. |
| + * client and the service. They are launched by |
| + * extensions/renderer/api/serial/serial_api_unittest.cc. |
| */ |
| var test = require('test').binding; |
| var serial = require('serial').binding; |
| var unittestBindings = require('test_environment_specific_bindings'); |
| +var utils = require('utils'); |
| + |
| +var timeoutManager = new unittestBindings.TimeoutManager(); |
| +timeoutManager.installGlobals(); |
| + |
| +var BUFFER_SIZE = 10; |
| var connectionId = null; |
| function connect(callback, options) { |
| options = options || { |
| name: 'test connection', |
| - bufferSize: 8192, |
| + bufferSize: BUFFER_SIZE, |
| receiveTimeout: 12345, |
| sendTimeout: 6789, |
| persistent: true, |
| - } |
| + }; |
| serial.connect('device', options, test.callbackPass(function(connectionInfo) { |
| connectionId = connectionInfo.connectionId; |
| - callback(connectionInfo); |
| + if (callback) |
| + callback(connectionInfo); |
| })); |
| } |
| +// Sets a function to be called once when data is received. Returns a promise |
| +// that will resolve once the hook is installed. |
| +function addReceiveHook(callback) { |
| + return requireAsync('serial_service').then(function(serialService) { |
| + var called = false; |
| + var dataReceived = serialService.Connection.prototype.onDataReceived_; |
| + serialService.Connection.prototype.onDataReceived_ = function() { |
| + var result = $Function.apply(dataReceived, this, arguments); |
| + if (!called) |
| + callback(); |
| + called = true; |
| + return result; |
| + }; |
| + }); |
| +} |
| + |
| +// Sets a function to be called once when a receive error is received. Returns a |
| +// promise that will resolve once the hook is installed. |
| +function addReceiveErrorHook(callback) { |
| + return requireAsync('serial_service').then(function(serialService) { |
| + var called = false; |
| + var receiveError = serialService.Connection.prototype.onReceiveError_; |
| + serialService.Connection.prototype.onReceiveError_ = function() { |
| + var result = $Function.apply(receiveError, this, arguments); |
| + if (!called) |
| + callback(); |
| + called = true; |
| + return result; |
| + }; |
| + }); |
| +} |
| + |
| function disconnect() { |
| serial.disconnect(connectionId, test.callbackPass(function(success) { |
| test.assertTrue(success); |
| @@ -41,7 +81,7 @@ function checkClientConnectionInfo(connectionInfo) { |
| test.assertEq('test connection', connectionInfo.name); |
| test.assertEq(12345, connectionInfo.receiveTimeout); |
| test.assertEq(6789, connectionInfo.sendTimeout); |
| - test.assertEq(8192, connectionInfo.bufferSize); |
| + test.assertEq(BUFFER_SIZE, connectionInfo.bufferSize); |
| test.assertFalse(connectionInfo.paused); |
| } |
| @@ -58,6 +98,49 @@ function checkConnectionInfo(connectionInfo) { |
| checkServiceConnectionInfo(connectionInfo); |
| } |
| +function runReceiveErrorTest(expectedError) { |
| + connect(); |
| + test.listenOnce(serial.onReceiveError, function(result) { |
| + serial.getInfo(connectionId, test.callbackPass(function(connectionInfo) { |
| + disconnect(); |
| + test.assertTrue(connectionInfo.paused); |
| + })); |
| + test.assertEq(connectionId, result.connectionId); |
| + test.assertEq(expectedError, result.error); |
| + }); |
| +} |
| + |
| +function runSendErrorTest(expectedError) { |
| + connect(function() { |
| + var buffer = new ArrayBuffer(1); |
| + serial.send(connectionId, buffer, test.callbackPass(function(sendInfo) { |
| + disconnect(); |
| + test.assertEq(0, sendInfo.bytesSent); |
| + test.assertEq(expectedError, sendInfo.error); |
| + })); |
| + }); |
| +} |
| + |
| +function sendData() { |
| + var data = 'data'; |
| + var buffer = new ArrayBuffer(data.length); |
| + var byteBuffer = new Int8Array(buffer); |
| + for (var i = 0; i < data.length; i++) { |
| + byteBuffer[i] = data.charCodeAt(i); |
| + } |
| + return utils.promise(serial.send, connectionId, buffer); |
| +} |
| + |
| +function checkReceivedData(result) { |
| + var data = 'data'; |
| + test.assertEq(connectionId, result.connectionId); |
| + test.assertEq(data.length, result.data.byteLength); |
| + var resultByteBuffer = new Int8Array(result.data); |
| + for (var i = 0; i < data.length; i++) { |
| + test.assertEq(data.charCodeAt(i), resultByteBuffer[i]); |
| + } |
| +} |
| + |
| unittestBindings.exportTests([ |
| function testGetDevices() { |
| serial.getDevices(test.callbackPass(function(devices) { |
| @@ -277,6 +360,218 @@ unittestBindings.exportTests([ |
| }); |
| }, |
| + function testEcho() { |
| + connect(function() { |
| + sendData().then(test.callbackPass(function(sendInfo) { |
| + test.assertEq(4, sendInfo.bytesSent); |
| + test.assertEq(undefined, sendInfo.error); |
| + })); |
| + test.listenOnce(serial.onReceive, function(result) { |
| + checkReceivedData(result); |
| + disconnect(); |
| + }); |
| + }); |
| + }, |
| + |
| + function testSendDuringExistingSend() { |
| + connect(function() { |
| + sendData().then(test.callbackPass(function(sendInfo) { |
| + test.assertEq(4, sendInfo.bytesSent); |
| + test.assertEq(undefined, sendInfo.error); |
| + disconnect(); |
| + })); |
| + sendData().then(test.callbackPass(function(sendInfo) { |
| + test.assertEq(0, sendInfo.bytesSent); |
| + test.assertEq('pending', sendInfo.error); |
| + })); |
| + }); |
| + }, |
| + |
| + function testSendAfterSuccessfulSend() { |
| + connect(function() { |
| + sendData().then(test.callbackPass(function(sendInfo) { |
| + test.assertEq(4, sendInfo.bytesSent); |
| + test.assertEq(undefined, sendInfo.error); |
| + // Test that a second send started after the first completes succeeds. |
| + return sendData(); |
| + })).then(test.callbackPass(function(sendInfo) { |
| + test.assertEq(4, sendInfo.bytesSent); |
| + test.assertEq(undefined, sendInfo.error); |
| + })); |
| + // Check that the correct data is echoed twice. |
| + test.listenOnce(serial.onReceive, function(result) { |
| + checkReceivedData(result); |
| + test.listenOnce(serial.onReceive, function(result) { |
| + checkReceivedData(result); |
| + disconnect(); |
| + }); |
| + }); |
| + }); |
| + }, |
| + |
| + function testSendPartialSuccessWithError() { |
| + connect(function() { |
| + sendData().then(test.callbackPass(function(sendInfo) { |
| + test.assertEq(2, sendInfo.bytesSent); |
| + test.assertEq('system_error', sendInfo.error); |
| + return sendData(); |
| + })).then(test.callbackPass(function(sendInfo) { |
| + test.assertEq(4, sendInfo.bytesSent); |
| + test.assertEq(undefined, sendInfo.error); |
| + disconnect(); |
| + })); |
| + }); |
| + }, |
| + |
| + function testSendTimeout() { |
| + connect(function() { |
| + sendData().then(test.callbackPass(function(sendInfo) { |
| + test.assertEq(0, sendInfo.bytesSent); |
| + test.assertEq('timeout', sendInfo.error); |
| + test.assertEq(5, timeoutManager.currentTime); |
| + disconnect(); |
| + timeoutManager.disableAutorun(); |
| + })); |
| + // Changing the timeout does not effect a send in progress. |
| + serial.update(connectionId, {sendTimeout: 10}, test.callbackPass( |
| + timeoutManager.enableAutorun.bind(timeoutManager))); |
|
raymes
2014/09/02 04:59:32
What about calling these timeoutManager.pause() an
Sam McNally
2014/09/02 07:41:13
Done. For testReceiveTimeout, leaving it running c
raymes
2014/09/03 01:28:32
Hmm do you know why that is the case? This seems s
Sam McNally
2014/09/03 01:56:13
Removed the need to pause as discussed.
|
| + }, {sendTimeout: 5}); |
| + }, |
| + |
| + function testDisableSendTimeout() { |
| + connect(function() { |
| + sendData().then(test.callbackPass(function(sendInfo) { |
| + test.assertEq(0, sendInfo.bytesSent); |
| + test.assertEq('timeout', sendInfo.error); |
| + test.assertEq(6, timeoutManager.currentTime); |
| + disconnect(); |
| + timeoutManager.disableAutorun(); |
| + })); |
| + // Disabling the timeout does not effect a send in progress. |
| + serial.update(connectionId, {sendTimeout: 0}, test.callbackPass( |
| + timeoutManager.enableAutorun.bind(timeoutManager))); |
| + }, {sendTimeout: 6}); |
| + }, |
| + |
| + function testPausedReceive() { |
| + // Wait until the receive hook is installed, then start the test. |
| + addReceiveHook(function() { |
| + // Unpause the connection after the connection has queued the received |
| + // data to ensure the queued data is dispatched when the connection is |
| + // unpaused. |
| + serial.setPaused(connectionId, false, test.callbackPass()); |
| + // Check that setPaused(false) is idempotent. |
| + serial.setPaused(connectionId, false, test.callbackPass()); |
| + }).then(function() { |
| + connect(function() { |
| + // Check that setPaused(true) is idempotent. |
| + serial.setPaused(connectionId, true, test.callbackPass()); |
| + serial.setPaused(connectionId, true, test.callbackPass()); |
| + }); |
| + }); |
| + test.listenOnce(serial.onReceive, function(result) { |
| + checkReceivedData(result); |
| + disconnect(); |
| + }); |
| + }, |
| + |
| + function testPausedReceiveError() { |
| + addReceiveErrorHook(function() { |
| + // Unpause the connection after the connection has queued the receive |
| + // error to ensure the queued error is dispatched when the connection is |
| + // unpaused. |
| + serial.setPaused(connectionId, false, test.callbackPass()); |
| + }).then(test.callbackPass(function() { |
| + connect(function() { |
| + serial.setPaused(connectionId, true, test.callbackPass()); |
| + }); |
| + })); |
| + |
| + test.listenOnce(serial.onReceiveError, function(result) { |
| + serial.getInfo(connectionId, test.callbackPass(function(connectionInfo) { |
| + disconnect(); |
| + test.assertTrue(connectionInfo.paused); |
| + })); |
| + test.assertEq(connectionId, result.connectionId); |
| + test.assertEq('device_lost', result.error); |
| + }); |
| + serial.onReceive.addListener(function() { |
| + test.fail('unexpected onReceive event'); |
| + }); |
| + }, |
| + |
| + function testReceiveTimeout() { |
| + connect(function() { |
| + test.listenOnce(serial.onReceiveError, function(result) { |
| + test.assertEq(connectionId, result.connectionId); |
| + test.assertEq('timeout', result.error); |
| + test.assertEq(20, timeoutManager.currentTime); |
| + serial.getInfo(connectionId, test.callbackPass( |
| + function(connectionInfo) { |
| + test.assertFalse(connectionInfo.paused); |
| + disconnect(); |
| + })); |
| + timeoutManager.disableAutorun(); |
| + }); |
| + // Changing the timeout does not take effect until the current timeout |
| + // expires or a receive completes. |
| + serial.update(connectionId, {receiveTimeout: 10}, test.callbackPass( |
| + timeoutManager.enableAutorun.bind(timeoutManager))); |
| + }, {receiveTimeout: 20}); |
| + }, |
| + |
| + function testDisableReceiveTimeout() { |
| + connect(function() { |
| + test.listenOnce(serial.onReceiveError, function(result) { |
| + test.assertEq(connectionId, result.connectionId); |
| + test.assertEq('timeout', result.error); |
| + test.assertEq(30, timeoutManager.currentTime); |
| + serial.getInfo(connectionId, test.callbackPass( |
| + function(connectionInfo) { |
| + disconnect(); |
| + test.assertFalse(connectionInfo.paused); |
| + })); |
| + timeoutManager.disableAutorun(); |
| + }); |
| + // Disabling the timeout does not take effect until the current timeout |
| + // expires or a receive completes. |
| + serial.update(connectionId, {receiveTimeout: 0}, test.callbackPass( |
| + timeoutManager.enableAutorun.bind(timeoutManager))); |
| + }, {receiveTimeout: 30}); |
| + }, |
| + |
| + function testReceiveErrorDisconnected() { |
| + runReceiveErrorTest('disconnected'); |
| + }, |
| + |
| + function testReceiveErrorTimeout() { |
| + // Note: this tests that a timeout error from the service pauses the |
| + // connection, but in production, the service never sends a timeout error - |
| + // the timeout error is generated internally by the connection and in that |
| + // case doesn't trigger a pause. |
|
raymes
2014/09/02 04:59:32
Should this comment also be on testSendErrorTimeou
Sam McNally
2014/09/02 07:41:13
Removed these tests.
|
| + runReceiveErrorTest('timeout'); |
| + }, |
| + |
| + function testReceiveErrorDeviceLost() { |
| + runReceiveErrorTest('device_lost'); |
| + }, |
| + |
| + function testReceiveErrorSystemError() { |
| + runReceiveErrorTest('system_error'); |
| + }, |
| + |
| + function testSendErrorDisconnected() { |
| + runSendErrorTest('disconnected'); |
| + }, |
| + |
| + function testSendErrorTimeout() { |
| + runSendErrorTest('timeout'); |
| + }, |
| + |
| + function testSendErrorSystemError() { |
| + runSendErrorTest('system_error'); |
| + }, |
| + |
| function testDisconnectUnknownConnectionId() { |
| serial.disconnect(-1, test.callbackFail('Serial connection not found.')); |
| }, |
| @@ -309,4 +604,9 @@ unittestBindings.exportTests([ |
| serial.setPaused( |
| -1, false, test.callbackFail('Serial connection not found.')); |
| }, |
| + |
| + function testSendUnknownConnectionId() { |
| + var buffer = new ArrayBuffer(1); |
| + serial.send(-1, buffer, test.callbackFail('Serial connection not found.')); |
| + }, |
| ], test.runTests, exports); |