Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / bluetooth_low_energy / get_characteristic / 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 function testGetCharacteristic() {
6   chrome.test.assertTrue(characteristic != null,
7                          'Characteristic is null.');
8   chrome.test.assertEq('char_id0', characteristic.instanceId);
9   chrome.test.assertEq('00001211-0000-1000-8000-00805f9b34fb',
10                        characteristic.uuid);
11   chrome.test.assertEq(false, characteristic.isLocal);
12   chrome.test.assertEq(serviceId, characteristic.service.instanceId);
13   chrome.test.assertEq(4, characteristic.properties.length);
14   chrome.test.assertTrue(characteristic.properties.indexOf('broadcast') > -1,
15                          '\'broadcast\' not in characteristic.properties');
16   chrome.test.assertTrue(characteristic.properties.indexOf('read') > -1,
17                          '\'read\' not in characteristic.properties');
18   chrome.test.assertTrue(characteristic.properties.indexOf('indicate') > -1,
19                          '\'indicate\' not in characteristic.properties');
20   chrome.test.assertTrue(
21       characteristic.properties.indexOf('writeWithoutResponse') > -1,
22       '\'writeWithoutResponse\' not in characteristic.properties');
23
24   var valueBytes = new Uint8Array(characteristic.value);
25   chrome.test.assertEq(5, characteristic.value.byteLength);
26   chrome.test.assertEq(0x01, valueBytes[0]);
27   chrome.test.assertEq(0x02, valueBytes[1]);
28   chrome.test.assertEq(0x03, valueBytes[2]);
29   chrome.test.assertEq(0x04, valueBytes[3]);
30   chrome.test.assertEq(0x05, valueBytes[4]);
31
32   chrome.test.succeed();
33 }
34
35 var serviceId = 'service_id0';
36 var charId = 'char_id0';
37 var badCharId = 'char_id1';
38
39 var characteristic = null;
40
41 // 1. Unknown characteristic instanceId.
42 chrome.bluetoothLowEnergy.getCharacteristic(badCharId, function (result) {
43   if (result || !chrome.runtime.lastError) {
44     chrome.test.fail('getCharacteristics should have failed');
45   }
46
47   // 2. Known characteristic instanceId, but the mapped device is unknown.
48   chrome.bluetoothLowEnergy.getCharacteristic(charId, function (result) {
49     if (result || !chrome.runtime.lastError) {
50       chrome.test.fail('getCharacteristics should have failed');
51     }
52
53     // 3. Known characteristic instanceId, but the mapped service is unknown.
54     chrome.bluetoothLowEnergy.getCharacteristic(charId, function (result) {
55       if (result || !chrome.runtime.lastError) {
56         chrome.test.fail('getCharacteristics should have failed');
57       }
58
59       // 4. Known characteristic instanceId, but the mapped service does not
60       // know about the characteristic.
61       chrome.bluetoothLowEnergy.getCharacteristic(charId, function (result) {
62         if (result || !chrome.runtime.lastError) {
63           chrome.test.fail('getCharacteristics should have failed');
64         }
65
66         // 5. Success.
67         chrome.bluetoothLowEnergy.getCharacteristic(charId, function (result) {
68           if (chrome.runtime.lastError) {
69             chrome.test.fail(chrome.runtime.lastError.message);
70           }
71
72           characteristic = result;
73
74           chrome.test.sendMessage('ready', function (message) {
75             chrome.test.runTests([testGetCharacteristic]);
76           });
77         });
78       });
79     })
80   });
81 });