99e2830eafaaaa47eaee59def909eaf316687c94
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / bluetooth_low_energy / get_descriptor / 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 testGetDescriptor() {
8   if (error !== undefined) {
9     chrome.test.sendMessage('fail');
10     chrome.test.fail(error);
11   }
12   chrome.test.assertTrue(descriptor != null, '\'descriptor\' is null');
13
14   chrome.test.assertEq('desc_id0', descriptor.instanceId);
15   chrome.test.assertEq('00001221-0000-1000-8000-00805f9b34fb', descriptor.uuid);
16   chrome.test.assertEq(false, descriptor.isLocal);
17   chrome.test.assertEq(charId, descriptor.characteristic.instanceId);
18
19   var valueBytes = new Uint8Array(descriptor.value);
20   chrome.test.assertEq(3, descriptor.value.byteLength);
21   chrome.test.assertEq(0x01, valueBytes[0]);
22   chrome.test.assertEq(0x02, valueBytes[1]);
23   chrome.test.assertEq(0x03, valueBytes[2]);
24
25   chrome.test.succeed();
26 }
27
28 var getDescriptor = chrome.bluetoothLowEnergy.getDescriptor;
29
30 var charId = 'char_id0';
31 var descId = 'desc_id0';
32 var badDescId = 'desc_id1';
33
34 var descriptor = null;
35
36 function earlyError(message) {
37   error = message;
38   chrome.test.runTests([testGetDescriptor]);
39 }
40
41 function expectError(result) {
42   if (result || !chrome.runtime.lastError) {
43     earlyError('getDescriptor should have failed');
44   }
45   return error !== undefined;
46 }
47
48 // 1. Unknown descriptor instanceId.
49 getDescriptor(badDescId, function (result) {
50   if (result || !chrome.runtime.lastError) {
51     earlyError('getDescriptor should have failed for \'badDescId\'');
52     return;
53   }
54
55   // 2. Known descriptor instanceId, but the mapped device is unknown.
56   getDescriptor(descId, function (result) {
57     if (expectError(result))
58       return;
59
60     // 3. Known descriptor instanceId, but the mapped service is unknown.
61     getDescriptor(descId, function (result) {
62       if (expectError(result))
63         return;
64
65       // 4. Known descriptor instanceId, but the mapped characteristic is
66       // unknown.
67       getDescriptor(descId, function (result) {
68         if (expectError(result))
69           return;
70
71         // 5. Known descriptor instanceId, but the mapped the characteristic
72         // does not know about the descriptor.
73         getDescriptor(descId, function (result) {
74           if (expectError(result))
75             return;
76
77           // 6. Success.
78           getDescriptor(descId, function (result) {
79             if (chrome.runtime.lastError) {
80               earlyError(chrome.runtime.lastError.message);
81               return;
82             }
83
84             descriptor = result;
85
86             chrome.test.sendMessage('ready', function (message) {
87               chrome.test.runTests([testGetDescriptor]);
88             });
89           });
90         });
91       });
92     });
93   });
94 });