Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / bluetooth_low_energy / gatt_connection / runtest.js
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.
4
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;
9
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';
20
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 + '>');
27 }
28
29 function expectSuccess() {
30   if (chrome.runtime.lastError)
31     chrome.test.fail('Unexpected error: ' + chrome.runtime.lastError.message);
32 }
33
34 var queue = [];
35
36 function runNext() {
37   if (queue.length == 0) {
38     chrome.test.fail("No more tests!");
39   }
40   (queue.shift())();
41 }
42
43 function makeConnectErrorFunction(error) {
44   return function() {
45     expectError(error);
46
47     ble.connect(deviceAddress0, runNext);
48   };
49 }
50
51 queue = [function() {
52   ble.disconnect(deviceAddress0, runNext);
53 }, function() {
54   expectError(errorNotConnected);
55
56   // Disconnect from deviceAddress1, (not connected)
57   ble.disconnect(deviceAddress1, runNext);
58 }, function () {
59   expectError(errorNotConnected);
60
61   // Connect to device that doesn't exist.
62   ble.connect(badDeviceAddress, runNext);
63 },
64   makeConnectErrorFunction(errorNotFound),
65   makeConnectErrorFunction(errorOperationFailed),
66   makeConnectErrorFunction(errorInProgress),
67   makeConnectErrorFunction(errorAuthFailed),
68   makeConnectErrorFunction(errorAuthFailed),
69   makeConnectErrorFunction(errorCanceled),
70   makeConnectErrorFunction(errorTimeout),
71   makeConnectErrorFunction(errorUnsupportedDevice),
72 function () {
73   expectSuccess();
74
75   // Device 0 already connected.
76   ble.connect(deviceAddress0, runNext);
77 }, function () {
78   expectError(errorAlreadyConnected);
79
80   // Device 1 still disconnected.
81   ble.disconnect(deviceAddress1, runNext);
82 }, function () {
83   expectError(errorNotConnected);
84
85   // Successful connect to device 1.
86   ble.connect(deviceAddress1, runNext);
87 }, function () {
88   expectSuccess();
89
90   // Device 1 already connected.
91   ble.connect(deviceAddress1, runNext);
92 }, function () {
93   expectError(errorAlreadyConnected);
94
95   // Successfully disconnect device 0.
96   ble.disconnect(deviceAddress0, runNext);
97 }, function () {
98   expectSuccess();
99
100   // Cannot disconnect device 0.
101   ble.disconnect(deviceAddress0, runNext);
102 }, function () {
103   expectError(errorNotConnected);
104
105   // Device 1 still connected.
106   ble.connect(deviceAddress1, runNext);
107 }, function () {
108   expectError(errorAlreadyConnected);
109
110   // Successfully disconnect device 1.
111   ble.disconnect(deviceAddress1, runNext);
112 }, function () {
113   expectSuccess();
114
115   // Cannot disconnect device 1.
116   ble.disconnect(deviceAddress1, runNext);
117 }, function () {
118   expectError(errorNotConnected);
119
120   // Re-connect device 0.
121   ble.connect(deviceAddress0, runNext);
122 }, function () {
123   expectSuccess();
124   chrome.test.succeed();
125 }];
126
127 runNext();