Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / bluetooth_socket / connect / 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 address = '11:12:13:14:15:16';
6 var uuid = '8e3ad063-db38-4289-aa8f-b30e4223cf40';
7
8 function testConnect() {
9   chrome.test.assertEq(1, sockets.length);
10   chrome.test.assertEq(socketId, sockets[0].socketId);
11   chrome.test.assertEq(false, sockets[0].persistent);
12   chrome.test.assertEq(undefined, sockets[0].name);
13   chrome.test.assertEq(false, sockets[0].paused);
14   chrome.test.assertEq(true, sockets[0].connected);
15   chrome.test.assertEq(address, sockets[0].address);
16   chrome.test.assertEq(uuid, sockets[0].uuid);
17
18   chrome.test.succeed();
19 }
20
21 function startTests() {
22   chrome.test.runTests([testConnect]);
23 }
24
25 function expectError(message) {
26   if (!chrome.runtime.lastError) {
27     chrome.test.fail("Expected an error");
28   }
29   chrome.test.assertEq(message, chrome.runtime.lastError.message);
30 }
31
32 function failOnError() {
33   if (chrome.runtime.lastError) {
34     chrome.test.fail(chrome.runtime.lastError.message);
35   }
36 }
37
38 function createConnectedSocket(address, uuid, callback) {
39   chrome.bluetoothSocket.create(
40     function(socket) {
41       failOnError();
42       chrome.bluetoothSocket.connect(
43         socket.socketId, address, uuid,
44         function() {
45           callback(socket);
46         });
47     });
48 }
49
50 function runSocketErrorTests(callback) {
51   chrome.bluetoothSocket.connect(1234, address, uuid,
52     function() {
53       expectError("Socket not found");
54
55       createConnectedSocket('aa:aa:aa:aa:aa:aa', uuid,
56         function(socket) {
57           expectError("Device not found");
58
59           createConnectedSocket(address, 'not a valid uuid',
60             function(socket) {
61               expectError("Invalid UUID");
62
63               createConnectedSocket(address, '1234',
64                 function(socket) {
65                   expectError("Permission denied");
66
67                   callback();
68                 });
69             });
70         });
71     });
72 }
73
74 createConnectedSocket(address, uuid,
75   function(socket) {
76     failOnError();
77
78     // Make sure that the socket appears in the sockets list.
79     chrome.bluetoothSocket.getSockets(
80       function(result) {
81         failOnError();
82         sockets = result;
83         socketId = socket.socketId;
84
85         // Run some error checks.
86         runSocketErrorTests(
87           function() {
88             chrome.bluetoothSocket.disconnect(socket.socketId);
89             chrome.test.sendMessage('ready', startTests);
90           });
91       });
92   });