1 // Copyright 2014 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 var deviceAddress0 = '11:22:33:44:55:66';
6 var deviceAddress1 = '77:88:99:AA:BB:CC';
7 var badDeviceAddress = 'bad-address';
8 var ble = chrome.bluetoothLowEnergy;
10 var errorAlreadyConnected = 'Already connected';
11 var errorNotConnected = 'Not connected';
12 var errorNotFound = 'Instance not found';
13 var errorOperationFailed = 'Operation failed';
14 var errorAuthFailed = 'Authentication failed';
15 var errorInProgress = 'In progress';
16 var errorCanceled = 'Request canceled';
17 var errorTimeout = 'Operation timed out';
18 var errorUnsupportedDevice = 'This device is not supported on ' +
19 'the current platform';
21 function expectError(message) {
22 if (!chrome.runtime.lastError)
23 chrome.test.fail('Expected error: <' + message + '> but there was none.');
24 if (chrome.runtime.lastError.message != message)
25 chrome.test.fail('Expected error: <' + message + '> but it was: <' +
26 chrome.runtime.lastError.message + '>');
29 function expectSuccess() {
30 if (chrome.runtime.lastError)
31 chrome.test.fail('Unexpected error: ' + chrome.runtime.lastError.message);
37 if (queue.length == 0) {
38 chrome.test.fail("No more tests!");
43 function makeConnectErrorFunction(error) {
47 ble.connect(deviceAddress0, runNext);
52 ble.disconnect(deviceAddress0, runNext);
54 expectError(errorNotConnected);
56 // Disconnect from deviceAddress1, (not connected)
57 ble.disconnect(deviceAddress1, runNext);
59 expectError(errorNotConnected);
61 // Connect to device that doesn't exist.
62 ble.connect(badDeviceAddress, runNext);
64 makeConnectErrorFunction(errorNotFound),
65 makeConnectErrorFunction(errorOperationFailed),
66 makeConnectErrorFunction(errorInProgress),
67 makeConnectErrorFunction(errorAuthFailed),
68 makeConnectErrorFunction(errorAuthFailed),
69 makeConnectErrorFunction(errorCanceled),
70 makeConnectErrorFunction(errorTimeout),
71 makeConnectErrorFunction(errorUnsupportedDevice),
75 // Device 0 already connected.
76 ble.connect(deviceAddress0, runNext);
78 expectError(errorAlreadyConnected);
80 // Device 1 still disconnected.
81 ble.disconnect(deviceAddress1, runNext);
83 expectError(errorNotConnected);
85 // Successful connect to device 1.
86 ble.connect(deviceAddress1, runNext);
90 // Device 1 already connected.
91 ble.connect(deviceAddress1, runNext);
93 expectError(errorAlreadyConnected);
95 // Successfully disconnect device 0.
96 ble.disconnect(deviceAddress0, runNext);
100 // Cannot disconnect device 0.
101 ble.disconnect(deviceAddress0, runNext);
103 expectError(errorNotConnected);
105 // Device 1 still connected.
106 ble.connect(deviceAddress1, runNext);
108 expectError(errorAlreadyConnected);
110 // Successfully disconnect device 1.
111 ble.disconnect(deviceAddress1, runNext);
115 // Cannot disconnect device 1.
116 ble.disconnect(deviceAddress1, runNext);
118 expectError(errorNotConnected);
120 // Re-connect device 0.
121 ble.connect(deviceAddress0, runNext);
124 chrome.test.succeed();