Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / test / data / extensions / api_test / serial / api / background.js
blobad82b71afaca1c70633b6843d9f3500bdaf65752
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 const serial = chrome.serial;
7 // TODO(miket): opening Bluetooth ports on OSX is unreliable. Investigate.
8 function shouldSkipPort(portName) {
9 return portName.match(/[Bb]luetooth/);
12 var createTestArrayBuffer = function() {
13 var bufferSize = 8;
14 var buffer = new ArrayBuffer(bufferSize);
16 var uint8View = new Uint8Array(buffer);
17 for (var i = 0; i < bufferSize; i++) {
18 uint8View[i] = 42 + i * 2; // An arbitrary pattern.
20 return buffer;
23 var testSerial = function() {
24 var serialPort = null;
25 var connectionId = -1;
26 var receiveTries = 10;
27 var sendBuffer = createTestArrayBuffer();
28 var sendBufferUint8View = new Uint8Array(sendBuffer);
29 var bufferLength = sendBufferUint8View.length;
30 var receiveBuffer = new ArrayBuffer(bufferLength);
31 var receiveBufferUint8View = new Uint8Array(receiveBuffer);
32 var bytesToReceive = bufferLength;
34 var operation = 0;
35 var doNextOperation = function() {
36 switch (operation++) {
37 case 0:
38 serial.getDevices(onGetDevices);
39 break;
40 case 1:
41 var bitrate = 57600;
42 console.log('Connecting to serial device ' + serialPort + ' at ' +
43 bitrate + ' bps.');
44 serial.connect(serialPort, {bitrate: bitrate}, onConnect);
45 break;
46 case 2:
47 serial.setControlSignals(connectionId, {dtr: true}, onSetControlSignals);
48 break;
49 case 3:
50 serial.getControlSignals(connectionId,onGetControlSignals);
51 break;
52 case 4:
53 serial.onReceive.addListener(onReceive);
54 serial.onReceiveError.addListener(onReceiveError);
55 serial.send(connectionId, sendBuffer, onSend);
56 break;
57 case 50: // GOTO 4 EVER
58 serial.disconnect(connectionId, onDisconnect);
59 break;
60 default:
61 // Beware! If you forget to assign a case for your next test, the whole
62 // test suite will appear to succeed!
63 chrome.test.succeed();
64 break;
68 var skipToTearDown = function() {
69 operation = 50;
70 doNextOperation();
73 var repeatOperation = function() {
74 operation--;
75 doNextOperation();
78 var onDisconnect = function(result) {
79 chrome.test.assertTrue(result);
80 doNextOperation();
83 var onReceive = function(receiveInfo) {
84 var data = new Uint8Array(receiveInfo.data);
85 bytesToReceive -= data.length;
86 var receiveBufferIndex = bufferLength - data.length;
87 for (var i = 0; i < data.length; i++)
88 receiveBufferUint8View[i + receiveBufferIndex] = data[i];
89 if (bytesToReceive == 0) {
90 chrome.test.assertEq(sendBufferUint8View, receiveBufferUint8View,
91 'Buffer received was not equal to buffer sent.');
92 doNextOperation();
93 } else if (--receiveTries <= 0) {
94 chrome.test.fail('receive() failed to return requested number of bytes.');
98 var onReceiveError = function(errorInfo) {
99 chrome.test.fail('Failed to receive serial data');
102 var onSend = function(sendInfo) {
103 chrome.test.assertEq(bufferLength, sendInfo.bytesSent,
104 'Failed to send byte.');
107 var onGetControlSignals = function(options) {
108 chrome.test.assertTrue(typeof options.dcd != 'undefined', "No DCD set");
109 chrome.test.assertTrue(typeof options.cts != 'undefined', "No CTS set");
110 chrome.test.assertTrue(typeof options.ri != 'undefined', "No RI set");
111 chrome.test.assertTrue(typeof options.dsr != 'undefined', "No DSR set");
112 doNextOperation();
115 var onSetControlSignals = function(result) {
116 chrome.test.assertTrue(result);
117 doNextOperation();
120 var onConnect = function(connectionInfo) {
121 chrome.test.assertTrue(!!connectionInfo,
122 'Failed to connect to serial port.');
123 connectionId = connectionInfo.connectionId;
124 doNextOperation();
127 var onGetDevices = function(devices) {
128 if (devices.length > 0) {
129 var portNumber = 0;
130 while (portNumber < devices.length) {
131 if (shouldSkipPort(devices[portNumber].path)) {
132 portNumber++;
133 continue;
134 } else
135 break;
137 if (portNumber < devices.length) {
138 serialPort = devices[portNumber].path;
139 doNextOperation();
140 } else {
141 // We didn't find a port that we think we should try.
142 chrome.test.succeed();
144 } else {
145 // No serial device found. This is still considered a success because we
146 // can't rely on specific hardware being present on the machine.
147 chrome.test.succeed();
151 doNextOperation();
154 chrome.test.runTests([testSerial]);