Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / bluetooth_low_energy / get_characteristics / 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 error;
6
7 function testGetCharacteristics() {
8   if (error !== undefined) {
9     chrome.test.sendMessage('fail');
10     chrome.test.fail(error);
11   }
12   chrome.test.assertEq(2, chrcs.length);
13
14   chrome.test.assertEq('char_id0', chrcs[0].instanceId);
15   chrome.test.assertEq('00001211-0000-1000-8000-00805f9b34fb', chrcs[0].uuid);
16   chrome.test.assertEq(false, chrcs[0].isLocal);
17   chrome.test.assertEq(serviceId, chrcs[0].service.instanceId);
18   chrome.test.assertEq(4, chrcs[0].properties.length);
19   chrome.test.assertTrue(chrcs[0].properties.indexOf('broadcast') > -1,
20                          '\'broadcast\' not in chrcs[0].properties');
21   chrome.test.assertTrue(chrcs[0].properties.indexOf('read') > -1,
22                          '\'read\' not in chrcs[0].properties');
23   chrome.test.assertTrue(chrcs[0].properties.indexOf('indicate') > -1,
24                          '\'indicate\' not in chrcs[0].properties');
25   chrome.test.assertTrue(
26       chrcs[0].properties.indexOf('writeWithoutResponse') > -1,
27       '\'writeWithoutResponse\' not in chrcs[0].properties');
28
29   var valueBytes = new Uint8Array(chrcs[0].value);
30   chrome.test.assertEq(5, chrcs[0].value.byteLength);
31   chrome.test.assertEq(0x01, valueBytes[0]);
32   chrome.test.assertEq(0x02, valueBytes[1]);
33   chrome.test.assertEq(0x03, valueBytes[2]);
34   chrome.test.assertEq(0x04, valueBytes[3]);
35   chrome.test.assertEq(0x05, valueBytes[4]);
36
37   chrome.test.assertEq('char_id1', chrcs[1].instanceId),
38   chrome.test.assertEq('00001212-0000-1000-8000-00805f9b34fb', chrcs[1].uuid);
39   chrome.test.assertEq(false, chrcs[1].isLocal);
40   chrome.test.assertEq(serviceId, chrcs[1].service.instanceId);
41   chrome.test.assertEq(3, chrcs[1].properties.length);
42   chrome.test.assertTrue(chrcs[1].properties.indexOf('read') > -1,
43                          '\'read\' not in chrcs[1].properties');
44   chrome.test.assertTrue(chrcs[1].properties.indexOf('write') > -1,
45                          '\'write\' not in chrcs[1].properties');
46   chrome.test.assertTrue(chrcs[1].properties.indexOf('notify') > -1,
47                          '\'notify\' not in chrcs[1].properties');
48
49   valueBytes = new Uint8Array(chrcs[1].value);
50   chrome.test.assertEq(3, chrcs[1].value.byteLength);
51   chrome.test.assertEq(0x06, valueBytes[0]);
52   chrome.test.assertEq(0x07, valueBytes[1]);
53   chrome.test.assertEq(0x08, valueBytes[2]);
54
55   chrome.test.succeed();
56 }
57
58 var serviceId = 'service_id0';
59 var chrcs = null;
60
61 function earlyError(message) {
62   error = message;
63   chrome.test.runTests([testGetCharacteristics]);
64 }
65
66 function expectSuccess() {
67   if (chrome.runtime.lastError) {
68     earlyError('Unexpected Error: ' + chrome.runtime.lastError.message);
69   }
70   return error !== undefined;
71 }
72
73 chrome.bluetoothLowEnergy.getCharacteristics(serviceId, function (result) {
74   if (result || !chrome.runtime.lastError) {
75     earlyError('getCharacteristics should have failed.');
76     return;
77   }
78
79   chrome.bluetoothLowEnergy.getCharacteristics(serviceId, function (result) {
80     if (expectSuccess())
81       return;
82
83     if (!result || result.length != 0) {
84       earlyError('Characteristics should be empty.');
85       return;
86     }
87
88     chrome.bluetoothLowEnergy.getCharacteristics(serviceId, function (result) {
89       if (expectSuccess())
90         return;
91
92       chrcs = result;
93
94       chrome.test.sendMessage('ready', function (message) {
95         chrome.test.runTests([testGetCharacteristics]);
96       });
97     });
98   });
99 });