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