Commit 5f71564c authored by jamuraa's avatar jamuraa Committed by Commit bot

Short-circuit failures in BLE js tests

Use chrome.test.sendMessage instead of chrome.test.fail so we
short-circuit instead of timeout on javascript failures where we're
waiting for a go signal.

BUG=388958
R=armansito@chromium.org

Review URL: https://codereview.chromium.org/593163002

Cr-Commit-Position: refs/heads/master@{#297292}
parent a8741d03
...@@ -2,7 +2,13 @@ ...@@ -2,7 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
var error;
function testCharacteristicProperties() { function testCharacteristicProperties() {
if (error !== undefined) {
chrome.test.sendMessage('fail');
chrome.test.fail(error);
}
chrome.test.assertEq(requestCount, characteristics.length); chrome.test.assertEq(requestCount, characteristics.length);
for (var i = 0; i < requestCount; i++) { for (var i = 0; i < requestCount; i++) {
...@@ -43,14 +49,21 @@ function compareProperties(a, b) { ...@@ -43,14 +49,21 @@ function compareProperties(a, b) {
} }
function failOnError() { function failOnError() {
if (error !== undefined)
return true;
if (chrome.runtime.lastError) { if (chrome.runtime.lastError) {
chrome.test.fail(chrome.runtime.lastError.message); error = 'Unexpected error: ' + chrome.runtime.lastError.message;
chrome.test.runTests([testCharacteristicProperties]);
} }
return false;
} }
for (var i = 0; i < requestCount; i++) { for (var i = 0; i < requestCount; i++) {
chrome.bluetoothLowEnergy.getCharacteristic(charId, function (result) { chrome.bluetoothLowEnergy.getCharacteristic(charId, function (result) {
failOnError(); if (failOnError())
return;
characteristics.push(result); characteristics.push(result);
if (characteristics.length == requestCount) { if (characteristics.length == requestCount) {
......
...@@ -10,12 +10,14 @@ var errorInProgress = 'In progress'; ...@@ -10,12 +10,14 @@ var errorInProgress = 'In progress';
function expectError(message) { function expectError(message) {
if (!chrome.runtime.lastError || if (!chrome.runtime.lastError ||
chrome.runtime.lastError.message != message) chrome.runtime.lastError.message != message)
chrome.test.fail('Expected error: ' + message); chrome.test.sendMessage('Expected error: <' + message + '> got <'
+ chrome.runtime.lastError.message + '>');
} }
function expectSuccess() { function expectSuccess() {
if (chrome.runtime.lastError) if (chrome.runtime.lastError)
chrome.test.fail('Unexpected error: ' + chrome.runtime.lastError.message); chrome.test.sendMessage('Unexpected error: '
+ chrome.runtime.lastError.message);
} }
ble.connect(deviceAddress0, function () { ble.connect(deviceAddress0, function () {
......
...@@ -2,7 +2,13 @@ ...@@ -2,7 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
var error;
function testGetCharacteristic() { function testGetCharacteristic() {
if (error !== undefined) {
chrome.test.sendMessage('fail');
chrome.test.fail(error);
}
chrome.test.assertTrue(characteristic != null, chrome.test.assertTrue(characteristic != null,
'Characteristic is null.'); 'Characteristic is null.');
chrome.test.assertEq('char_id0', characteristic.instanceId); chrome.test.assertEq('char_id0', characteristic.instanceId);
...@@ -38,35 +44,41 @@ var badCharId = 'char_id1'; ...@@ -38,35 +44,41 @@ var badCharId = 'char_id1';
var characteristic = null; var characteristic = null;
// 1. Unknown characteristic instanceId. function expectFailed(result) {
chrome.bluetoothLowEnergy.getCharacteristic(badCharId, function (result) {
if (result || !chrome.runtime.lastError) { if (result || !chrome.runtime.lastError) {
chrome.test.fail('getCharacteristics should have failed'); error = 'getCharacteristic call should have failed';
chrome.test.runTests([testGetCharacteristic]);
return false;
} }
return true;
}
// 1. Unknown characteristic instanceId.
chrome.bluetoothLowEnergy.getCharacteristic(badCharId, function (result) {
if (!expectFailed(result))
return;
// 2. Known characteristic instanceId, but the mapped device is unknown. // 2. Known characteristic instanceId, but the mapped device is unknown.
chrome.bluetoothLowEnergy.getCharacteristic(charId, function (result) { chrome.bluetoothLowEnergy.getCharacteristic(charId, function (result) {
if (result || !chrome.runtime.lastError) { if (!expectFailed(result))
chrome.test.fail('getCharacteristics should have failed'); return;
}
// 3. Known characteristic instanceId, but the mapped service is unknown. // 3. Known characteristic instanceId, but the mapped service is unknown.
chrome.bluetoothLowEnergy.getCharacteristic(charId, function (result) { chrome.bluetoothLowEnergy.getCharacteristic(charId, function (result) {
if (result || !chrome.runtime.lastError) { if (!expectFailed(result))
chrome.test.fail('getCharacteristics should have failed'); return;
}
// 4. Known characteristic instanceId, but the mapped service does not // 4. Known characteristic instanceId, but the mapped service does not
// know about the characteristic. // know about the characteristic.
chrome.bluetoothLowEnergy.getCharacteristic(charId, function (result) { chrome.bluetoothLowEnergy.getCharacteristic(charId, function (result) {
if (result || !chrome.runtime.lastError) { if (!expectFailed(result))
chrome.test.fail('getCharacteristics should have failed'); return;
}
// 5. Success. // 5. Success.
chrome.bluetoothLowEnergy.getCharacteristic(charId, function (result) { chrome.bluetoothLowEnergy.getCharacteristic(charId, function (result) {
if (chrome.runtime.lastError) { if (chrome.runtime.lastError) {
chrome.test.fail(chrome.runtime.lastError.message); error = 'Unexpected error: ' + chrome.runtime.lastError.message;
chrome.test.runTests([testGetCharacteristic]);
} }
characteristic = result; characteristic = result;
......
...@@ -2,7 +2,13 @@ ...@@ -2,7 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
var error;
function testGetCharacteristics() { function testGetCharacteristics() {
if (error !== undefined) {
chrome.test.sendMessage('fail');
chrome.test.fail(error);
}
chrome.test.assertEq(2, chrcs.length); chrome.test.assertEq(2, chrcs.length);
chrome.test.assertEq('char_id0', chrcs[0].instanceId); chrome.test.assertEq('char_id0', chrcs[0].instanceId);
...@@ -52,25 +58,37 @@ function testGetCharacteristics() { ...@@ -52,25 +58,37 @@ function testGetCharacteristics() {
var serviceId = 'service_id0'; var serviceId = 'service_id0';
var chrcs = null; var chrcs = null;
function failOnError() { function earlyError(message) {
error = message;
chrome.test.runTests([testGetCharacteristics]);
}
function expectSuccess() {
if (chrome.runtime.lastError) { if (chrome.runtime.lastError) {
chrome.test.fail(chrome.runtime.lastError.message); earlyError('Unexpected Error: ' + chrome.runtime.lastError.message);
} }
return error !== undefined;
} }
chrome.bluetoothLowEnergy.getCharacteristics(serviceId, function (result) { chrome.bluetoothLowEnergy.getCharacteristics(serviceId, function (result) {
if (result || !chrome.runtime.lastError) { if (result || !chrome.runtime.lastError) {
chrome.test.fail('getCharacteristics should have failed.'); earlyError('getCharacteristics should have failed.');
return;
} }
chrome.bluetoothLowEnergy.getCharacteristics(serviceId, function (result) { chrome.bluetoothLowEnergy.getCharacteristics(serviceId, function (result) {
failOnError(); if (expectSuccess())
return;
if (!result || result.length != 0) { if (!result || result.length != 0) {
chrome.test.fail('Characteristics should be empty.'); earlyError('Characteristics should be empty.');
return;
} }
chrome.bluetoothLowEnergy.getCharacteristics(serviceId, function (result) { chrome.bluetoothLowEnergy.getCharacteristics(serviceId, function (result) {
failOnError(); if (expectSuccess())
return;
chrcs = result; chrcs = result;
chrome.test.sendMessage('ready', function (message) { chrome.test.sendMessage('ready', function (message) {
......
...@@ -2,7 +2,13 @@ ...@@ -2,7 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
var error;
function testGetDescriptor() { function testGetDescriptor() {
if (error !== undefined) {
chrome.test.sendMessage('fail');
chrome.test.fail(error);
}
chrome.test.assertTrue(descriptor != null, '\'descriptor\' is null'); chrome.test.assertTrue(descriptor != null, '\'descriptor\' is null');
chrome.test.assertEq('desc_id0', descriptor.instanceId); chrome.test.assertEq('desc_id0', descriptor.instanceId);
...@@ -27,42 +33,52 @@ var badDescId = 'desc_id1'; ...@@ -27,42 +33,52 @@ var badDescId = 'desc_id1';
var descriptor = null; var descriptor = null;
function earlyError(message) {
error = message;
chrome.test.runTests([testGetDescriptor]);
}
function expectError(result) {
if (result || !chrome.runtime.lastError) {
earlyError('getDescriptor should have failed');
}
return error !== undefined;
}
// 1. Unknown descriptor instanceId. // 1. Unknown descriptor instanceId.
getDescriptor(badDescId, function (result) { getDescriptor(badDescId, function (result) {
if (result || !chrome.runtime.lastError) { if (result || !chrome.runtime.lastError) {
chrome.test.fail('getDescriptor should have failed for \'badDescId\''); earlyError('getDescriptor should have failed for \'badDescId\'');
return;
} }
// 2. Known descriptor instanceId, but the mapped device is unknown. // 2. Known descriptor instanceId, but the mapped device is unknown.
getDescriptor(descId, function (result) { getDescriptor(descId, function (result) {
if (result || !chrome.runtime.lastError) { if (expectError(result))
chrome.test.fail('getDescriptor should have failed'); return;
}
// 3. Known descriptor instanceId, but the mapped service is unknown. // 3. Known descriptor instanceId, but the mapped service is unknown.
getDescriptor(descId, function (result) { getDescriptor(descId, function (result) {
if (result || !chrome.runtime.lastError) { if (expectError(result))
chrome.test.fail('getDescriptor should have failed'); return;
}
// 4. Known descriptor instanceId, but the mapped characteristic is // 4. Known descriptor instanceId, but the mapped characteristic is
// unknown. // unknown.
getDescriptor(descId, function (result) { getDescriptor(descId, function (result) {
if (result || !chrome.runtime.lastError) { if (expectError(result))
chrome.test.fail('getDescriptor should have failed'); return;
}
// 5. Known descriptor instanceId, but the mapped the characteristic // 5. Known descriptor instanceId, but the mapped the characteristic
// does not know about the descriptor. // does not know about the descriptor.
getDescriptor(descId, function (result) { getDescriptor(descId, function (result) {
if (result || !chrome.runtime.lastError) { if (expectError(result))
chrome.test.fail('getDescriptor should have failed'); return;
}
// 6. Success. // 6. Success.
getDescriptor(descId, function (result) { getDescriptor(descId, function (result) {
if (chrome.runtime.lastError) { if (chrome.runtime.lastError) {
chrome.test.fail(chrome.runtime.lastError.message); earlyError(chrome.runtime.lastError.message);
return;
} }
descriptor = result; descriptor = result;
......
...@@ -2,7 +2,13 @@ ...@@ -2,7 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
var error;
function testGetDescriptors() { function testGetDescriptors() {
if (error !== undefined) {
chrome.test.sendMessage('fail');
chrome.test.fail(error);
}
chrome.test.assertEq(2, descrs.length); chrome.test.assertEq(2, descrs.length);
chrome.test.assertEq('desc_id0', descrs[0].instanceId); chrome.test.assertEq('desc_id0', descrs[0].instanceId);
...@@ -35,34 +41,48 @@ var badCharId = 'char_id1'; ...@@ -35,34 +41,48 @@ var badCharId = 'char_id1';
var descrs = null; var descrs = null;
function earlyError(message) {
error = message;
chrome.test.runTests([testGetDescriptors]);
}
function failOnError() { function failOnError() {
if (chrome.runtime.lastError) { if (chrome.runtime.lastError) {
chrome.test.fail(chrome.runtime.lastError.message); earlyError(chrome.runtime.lastError.message);
return true;
} }
return false;
} }
// 1. Unknown characteristic ID. // 1. Unknown characteristic ID.
getDescriptors(badCharId, function (result) { getDescriptors(badCharId, function (result) {
if (result || !chrome.runtime.lastError) { if (result || !chrome.runtime.lastError) {
chrome.test.fail('getDescriptors should have failed for \'badCharId\''); earlyError('getDescriptors should have failed for \'badCharId\'');
return;
} }
// 2. Known ID, unknown characteristic. // 2. Known ID, unknown characteristic.
getDescriptors(charId, function (result) { getDescriptors(charId, function (result) {
if (result || !chrome.runtime.lastError) { if (result || !chrome.runtime.lastError) {
chrome.test.fail('getDescriptors should have failed'); earlyError('getDescriptors should have failed');
return;
} }
// 3. Empty descriptors. // 3. Empty descriptors.
getDescriptors(charId, function (result) { getDescriptors(charId, function (result) {
failOnError(); if (failOnError())
return;
if (!result || result.length != 0) { if (!result || result.length != 0) {
chrome.test.fail('Descriptors should be empty'); earlyError('Descriptors should be empty');
return;
} }
// 4. Success. // 4. Success.
getDescriptors(charId, function (result) { getDescriptors(charId, function (result) {
failOnError(); if (failOnError())
return;
descrs = result; descrs = result;
chrome.test.sendMessage('ready', function (message) { chrome.test.sendMessage('ready', function (message) {
......
...@@ -2,7 +2,13 @@ ...@@ -2,7 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
var error;
function testGetIncludedServices() { function testGetIncludedServices() {
if (error !== undefined) {
chrome.test.sendMessage('fail');
chrome.test.fail(error);
}
chrome.test.assertTrue(services != null, '\'services\' is null'); chrome.test.assertTrue(services != null, '\'services\' is null');
chrome.test.assertEq(1, services.length); chrome.test.assertEq(1, services.length);
chrome.test.assertEq(includedId, services[0].instanceId); chrome.test.assertEq(includedId, services[0].instanceId);
...@@ -14,28 +20,41 @@ var serviceId = 'service_id0'; ...@@ -14,28 +20,41 @@ var serviceId = 'service_id0';
var includedId = 'service_id1'; var includedId = 'service_id1';
var services = null; var services = null;
function earlyError(message) {
error = message;
chrome.test.runTests([testGetIncludedServices]);
}
function failOnError() { function failOnError() {
if (chrome.runtime.lastError) { if (chrome.runtime.lastError) {
chrome.test.fail(chrome.runtime.lastError.message); earlyError(chrome.runtime.lastError.message);
return true;
} }
return false;
} }
chrome.bluetoothLowEnergy.getIncludedServices(serviceId, function (result) { chrome.bluetoothLowEnergy.getIncludedServices(serviceId, function (result) {
// No mapping for |serviceId|. // No mapping for |serviceId|.
if (result || !chrome.runtime.lastError) { if (result || !chrome.runtime.lastError) {
chrome.test.fail('getIncludedServices should have failed'); earlyError('getIncludedServices should have failed');
return;
} }
chrome.test.sendMessage('ready', function (message) { chrome.test.sendMessage('ready', function (message) {
chrome.bluetoothLowEnergy.getIncludedServices(serviceId, function (result) { chrome.bluetoothLowEnergy.getIncludedServices(serviceId, function (result) {
failOnError(); if (failOnError())
return;
if (!result || result.length != 0) { if (!result || result.length != 0) {
chrome.test.fail('Included services should be empty.'); earlyError('Included services should be empty.');
return;
} }
chrome.bluetoothLowEnergy.getIncludedServices(serviceId, chrome.bluetoothLowEnergy.getIncludedServices(serviceId,
function (result) { function (result) {
failOnError(); if (failOnError())
return;
services = result; services = result;
chrome.test.sendMessage('ready', function (message) { chrome.test.sendMessage('ready', function (message) {
......
...@@ -7,7 +7,7 @@ var charId = 'char_id0'; ...@@ -7,7 +7,7 @@ var charId = 'char_id0';
getCharacteristic(charId, function (result) { getCharacteristic(charId, function (result) {
if (chrome.runtime.lastError) { if (chrome.runtime.lastError) {
chrome.test.fail(chrome.runtime.lastError.message); chrome.test.sendMessage(chrome.runtime.lastError.message);
} }
chrome.test.assertEq(charId, result.instanceId); chrome.test.assertEq(charId, result.instanceId);
...@@ -15,7 +15,7 @@ getCharacteristic(charId, function (result) { ...@@ -15,7 +15,7 @@ getCharacteristic(charId, function (result) {
chrome.test.sendMessage('ready', function (message) { chrome.test.sendMessage('ready', function (message) {
getCharacteristic(charId, function (result) { getCharacteristic(charId, function (result) {
if (result || !chrome.runtime.lastError) { if (result || !chrome.runtime.lastError) {
chrome.test.fail('Call to getCharacteristic should have failed'); chrome.test.sendMessage('Call to getCharacteristic should have failed');
} }
chrome.test.sendMessage('ready', function (message) { chrome.test.sendMessage('ready', function (message) {
......
...@@ -7,7 +7,7 @@ var descId = 'desc_id0'; ...@@ -7,7 +7,7 @@ var descId = 'desc_id0';
getDescriptor(descId, function (result) { getDescriptor(descId, function (result) {
if (chrome.runtime.lastError) { if (chrome.runtime.lastError) {
chrome.test.fail(chrome.runtime.lastError.message); chrome.test.sendMessage(chrome.runtime.lastError.message);
} }
chrome.test.assertEq(descId, result.instanceId); chrome.test.assertEq(descId, result.instanceId);
...@@ -15,7 +15,7 @@ getDescriptor(descId, function (result) { ...@@ -15,7 +15,7 @@ getDescriptor(descId, function (result) {
chrome.test.sendMessage('ready', function (message) { chrome.test.sendMessage('ready', function (message) {
getDescriptor(descId, function (result) { getDescriptor(descId, function (result) {
if (result || !chrome.runtime.lastError) { if (result || !chrome.runtime.lastError) {
chrome.test.fail('Call to getDescriptor should have failed'); chrome.test.sendMessage('Call to getDescriptor should have failed');
} }
chrome.test.sendMessage('ready', function (message) { chrome.test.sendMessage('ready', function (message) {
......
...@@ -6,7 +6,7 @@ chrome.bluetoothLowEnergy.onServiceAdded.addListener(function (result) { ...@@ -6,7 +6,7 @@ chrome.bluetoothLowEnergy.onServiceAdded.addListener(function (result) {
// getService should return this service. // getService should return this service.
chrome.bluetoothLowEnergy.getService(result.instanceId, function (service) { chrome.bluetoothLowEnergy.getService(result.instanceId, function (service) {
if (chrome.runtime.lastError) { if (chrome.runtime.lastError) {
chrome.test.fail(chrome.runtime.lastError.message); chrome.test.sendMessage(chrome.runtime.lastError.message);
} }
chrome.test.assertEq(result.instanceId, service.instanceId); chrome.test.assertEq(result.instanceId, service.instanceId);
...@@ -19,7 +19,7 @@ chrome.bluetoothLowEnergy.onServiceRemoved.addListener(function (result) { ...@@ -19,7 +19,7 @@ chrome.bluetoothLowEnergy.onServiceRemoved.addListener(function (result) {
// getService should return error. // getService should return error.
chrome.bluetoothLowEnergy.getService(result.instanceId, function (service) { chrome.bluetoothLowEnergy.getService(result.instanceId, function (service) {
if (service || !chrome.runtime.lastError) { if (service || !chrome.runtime.lastError) {
chrome.test.fail('Call to getService should have failed.'); chrome.test.sendMessage('Call to getService should have failed.');
} }
chrome.test.sendMessage('getServiceFail', function (message) { chrome.test.sendMessage('getServiceFail', function (message) {
......
...@@ -2,7 +2,13 @@ ...@@ -2,7 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
var error;
function testGetService() { function testGetService() {
if (error !== undefined) {
chrome.test.sendMessage('fail');
chrome.test.fail(error);
}
chrome.test.assertTrue(service != null); chrome.test.assertTrue(service != null);
chrome.test.assertEq(serviceId, service.instanceId); chrome.test.assertEq(serviceId, service.instanceId);
...@@ -20,34 +26,48 @@ var badServiceId = 'service_id1'; ...@@ -20,34 +26,48 @@ var badServiceId = 'service_id1';
var service = null; var service = null;
function earlyError(message) {
error = message;
chrome.test.runTests([testGetService]);
}
function failOnError() { function failOnError() {
if (chrome.runtime.lastError) { if (chrome.runtime.lastError) {
chrome.test.fail(chrome.runtime.lastError.message); earlyError(chrome.runtime.lastError.message);
return true;
} }
return false;
} }
// 1. Unknown service instanceId. function failOnSuccess(result) {
chrome.bluetoothLowEnergy.getService(badServiceId, function(result) {
if (result || !chrome.runtime.lastError) { if (result || !chrome.runtime.lastError) {
chrome.test.fail('Unexpected service.'); earlyError('Unexpected service.');
return true;
} }
return false;
}
// 1. Unknown service instanceId.
chrome.bluetoothLowEnergy.getService(badServiceId, function(result) {
if (failOnSuccess(result))
return;
// 2. Known service instanceId, but the mapped device is unknown. // 2. Known service instanceId, but the mapped device is unknown.
chrome.bluetoothLowEnergy.getService(serviceId, function(result) { chrome.bluetoothLowEnergy.getService(serviceId, function(result) {
if (result || !chrome.runtime.lastError) { if (failOnSuccess(result))
chrome.test.fail('Unexpected service.'); return;
}
// 3. Known service instanceId, but the mapped device does not know about // 3. Known service instanceId, but the mapped device does not know about
// the service. // the service.
chrome.bluetoothLowEnergy.getService(serviceId, function(result) { chrome.bluetoothLowEnergy.getService(serviceId, function(result) {
if (result || !chrome.runtime.lastError) { if (failOnSuccess(result))
chrome.test.fail('Unexpected service.'); return;
}
// 4. Success. // 4. Success.
chrome.bluetoothLowEnergy.getService(serviceId, function(result) { chrome.bluetoothLowEnergy.getService(serviceId, function(result) {
failOnError(); if (failOnError())
return;
service = result; service = result;
chrome.test.sendMessage('ready', function(message) { chrome.test.sendMessage('ready', function(message) {
......
...@@ -2,7 +2,13 @@ ...@@ -2,7 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
var error;
function testGetServices() { function testGetServices() {
if (error !== undefined) {
chrome.test.sendMessage('fail');
chrome.test.fail(error);
}
chrome.test.assertEq(2, services.length); chrome.test.assertEq(2, services.length);
chrome.test.assertEq('service_id0', services[0].instanceId); chrome.test.assertEq('service_id0', services[0].instanceId);
...@@ -25,25 +31,38 @@ function testGetServices() { ...@@ -25,25 +31,38 @@ function testGetServices() {
var deviceAddress = '11:22:33:44:55:66'; var deviceAddress = '11:22:33:44:55:66';
var services = null; var services = null;
function earlyError(message) {
error = message;
chrome.test.runTests([testGetServices]);
}
function failOnError() { function failOnError() {
if (chrome.runtime.lastError) { if (chrome.runtime.lastError) {
chrome.test.fail(chrome.runtime.lastError.message); earlyError(chrome.runtime.lastError.message);
return true;
} }
return false;
} }
chrome.bluetoothLowEnergy.getServices(deviceAddress, function(result) { chrome.bluetoothLowEnergy.getServices(deviceAddress, function(result) {
if (result || !chrome.runtime.lastError) { if (result || !chrome.runtime.lastError) {
chrome.test.fail('Unexpected device.'); earlyError('Unexpected device.');
return;
} }
chrome.bluetoothLowEnergy.getServices(deviceAddress, function(result) { chrome.bluetoothLowEnergy.getServices(deviceAddress, function(result) {
failOnError(); if (failOnError())
return;
if (!result || result.length != 0) { if (!result || result.length != 0) {
chrome.test.fail('Services should be empty.'); earlyError('Services should be empty.');
return;
} }
chrome.bluetoothLowEnergy.getServices(deviceAddress, function(result) { chrome.bluetoothLowEnergy.getServices(deviceAddress, function(result) {
failOnError(); if (failOnError())
return;
services = result; services = result;
chrome.test.sendMessage('ready', function(message) { chrome.test.sendMessage('ready', function(message) {
......
...@@ -2,7 +2,13 @@ ...@@ -2,7 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
var error;
function testReadCharacteristicValue() { function testReadCharacteristicValue() {
if (error !== undefined) {
chrome.test.sendMessage('fail');
chrome.test.fail(error);
}
chrome.test.assertTrue(characteristic != null, '\'characteristic\' is null'); chrome.test.assertTrue(characteristic != null, '\'characteristic\' is null');
chrome.test.assertEq(charId, characteristic.instanceId); chrome.test.assertEq(charId, characteristic.instanceId);
...@@ -15,29 +21,39 @@ var badCharId = 'char_id1'; ...@@ -15,29 +21,39 @@ var badCharId = 'char_id1';
var characteristic = null; var characteristic = null;
function earlyError(message) {
error = message;
chrome.test.runTests([testReadCharacteristicValue]);
}
// 1. Unknown characteristic instanceId. // 1. Unknown characteristic instanceId.
readCharacteristicValue(badCharId, function (result) { readCharacteristicValue(badCharId, function (result) {
if (result || !chrome.runtime.lastError) { if (result || !chrome.runtime.lastError) {
chrome.test.fail('\'badCharId\' did not cause failure'); earlyError('\'badCharId\' did not cause failure');
return;
} }
// 2. Known characteristic instanceId, but call failure. // 2. Known characteristic instanceId, but call failure.
readCharacteristicValue(charId, function (result) { readCharacteristicValue(charId, function (result) {
if (result || !chrome.runtime.lastError) { if (result || !chrome.runtime.lastError) {
chrome.test.fail('readCharacteristicValue should have failed'); earlyError('readCharacteristicValue should have failed');
return;
} }
// 3. Call should succeed. // 3. Call should succeed.
readCharacteristicValue(charId, function (result) { readCharacteristicValue(charId, function (result) {
if (chrome.runtime.lastError) { if (chrome.runtime.lastError) {
chrome.test.fail(chrome.runtime.lastError.message); earlyError(chrome.runtime.lastError.message);
return;
} }
characteristic = result; characteristic = result;
chrome.test.sendMessage('ready', function (message) { chrome.test.sendMessage('ready', function (reply) {
chrome.test.runTests([testReadCharacteristicValue]); chrome.test.runTests([testReadCharacteristicValue]);
}); });
}); });
}); });
}); });
...@@ -2,7 +2,13 @@ ...@@ -2,7 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
var error;
function testReadDescriptorValue() { function testReadDescriptorValue() {
if (error !== undefined) {
chrome.test.sendMessage('fail');
chrome.test.fail(error);
}
chrome.test.assertTrue(descriptor != null, '\'descriptor\' is null'); chrome.test.assertTrue(descriptor != null, '\'descriptor\' is null');
chrome.test.assertEq(descId, descriptor.instanceId); chrome.test.assertEq(descId, descriptor.instanceId);
...@@ -15,22 +21,30 @@ var badDescId = 'desc_id1'; ...@@ -15,22 +21,30 @@ var badDescId = 'desc_id1';
var descriptor = null; var descriptor = null;
function earlyError(message) {
error = message;
chrome.test.runTests([testReadDescriptorValue]);
}
// 1. Unknown descriptor instanceId. // 1. Unknown descriptor instanceId.
readDescriptorValue(badDescId, function (result) { readDescriptorValue(badDescId, function (result) {
if (result || !chrome.runtime.lastError) { if (result || !chrome.runtime.lastError) {
chrome.test.fail('\'badDescId\' did not cause failure'); earlyError('\'badDescId\' did not cause failure');
return;
} }
// 2. Known descriptor instanceId, but call failure. // 2. Known descriptor instanceId, but call failure.
readDescriptorValue(descId, function (result) { readDescriptorValue(descId, function (result) {
if (result || !chrome.runtime.lastError) { if (result || !chrome.runtime.lastError) {
chrome.test.fail('readDescriptorValue should have failed'); earlyError('readDescriptorValue should have failed');
return;
} }
// 3. Call should succeed. // 3. Call should succeed.
readDescriptorValue(descId, function (result) { readDescriptorValue(descId, function (result) {
if (chrome.runtime.lastError) { if (chrome.runtime.lastError) {
chrome.test.fail(chrome.runtime.lastError.message); earlyError(chrome.runtime.lastError.message);
return;
} }
descriptor = result; descriptor = result;
......
...@@ -2,7 +2,13 @@ ...@@ -2,7 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
var error;
function testStartStopNotifications() { function testStartStopNotifications() {
if (error !== undefined) {
chrome.test.sendMessage('fail');
chrome.test.fail(error);
}
chrome.test.assertEq(1, Object.keys(changedChrcs).length); chrome.test.assertEq(1, Object.keys(changedChrcs).length);
chrome.test.assertEq(charId0, changedChrcs[charId0].instanceId); chrome.test.assertEq(charId0, changedChrcs[charId0].instanceId);
chrome.test.succeed(); chrome.test.succeed();
...@@ -23,33 +29,27 @@ var ble = chrome.bluetoothLowEnergy; ...@@ -23,33 +29,27 @@ var ble = chrome.bluetoothLowEnergy;
var start = ble.startCharacteristicNotifications; var start = ble.startCharacteristicNotifications;
var stop = ble.stopCharacteristicNotifications; var stop = ble.stopCharacteristicNotifications;
function sendReady(errorMessage) {
chrome.test.sendMessage('ready', function (message) {
if (errorMessage) {
chrome.test.fail(errorMessage);
return;
}
function earlyError(message) {
error = message;
chrome.test.runTests([testStartStopNotifications]); chrome.test.runTests([testStartStopNotifications]);
});
} }
function expectError(expectedMessage) { function expectError(expectedMessage) {
if (!chrome.runtime.lastError) { if (!chrome.runtime.lastError) {
sendReady('Expected error: ' + expectedMessage); earlyError('Expected error: ' + expectedMessage);
return; } else if (chrome.runtime.lastError.message != expectedMessage) {
} earlyError('Expected error: ' + expectedMessage + ', got error: ' +
if (chrome.runtime.lastError.message != expectedMessage) {
sendReady('Expected error: ' + expectedMessage + ', got error: ' +
expectedMessage); expectedMessage);
} }
return error !== undefined;
} }
function expectSuccess() { function expectSuccess() {
if (chrome.runtime.lastError) { if (chrome.runtime.lastError) {
sendReady('Unexpected error: ' + chrome.runtime.lastError.message); earlyError('Unexpected error: ' + chrome.runtime.lastError.message);
} }
return error !== undefined;
} }
ble.onCharacteristicValueChanged.addListener(function (chrc) { ble.onCharacteristicValueChanged.addListener(function (chrc) {
...@@ -57,26 +57,48 @@ ble.onCharacteristicValueChanged.addListener(function (chrc) { ...@@ -57,26 +57,48 @@ ble.onCharacteristicValueChanged.addListener(function (chrc) {
}); });
start('foo', function () { start('foo', function () {
expectError(errorNotFound); if (expectError(errorNotFound))
return;
start(charId2, function () { start(charId2, function () {
expectError(errorPermissionDenied); if (expectError(errorPermissionDenied))
return;
stop(charId0, function () { stop(charId0, function () {
expectError(errorNotNotifying); if (expectError(errorNotNotifying))
return;
start(charId0, function () { start(charId0, function () {
expectError(errorOperationFailed); if (expectError(errorOperationFailed))
return;
start(charId0, function () { start(charId0, function () {
expectSuccess(); if (expectSuccess())
return;
start(charId0, function () { start(charId0, function () {
expectError(errorAlreadyNotifying); if (expectError(errorAlreadyNotifying))
return;
start(charId1, function () { start(charId1, function () {
expectSuccess(); if (expectSuccess())
return;
stop(charId1, function () { stop(charId1, function () {
expectSuccess(); if (expectSuccess())
return;
stop(charId1, function () { stop(charId1, function () {
expectError(errorNotNotifying); if (expectError(errorNotNotifying))
return;
stop(charId2, function () { stop(charId2, function () {
expectError(errorNotNotifying); if (expectError(errorNotNotifying))
sendReady(undefined); return;
chrome.test.sendMessage('ready', function (message) {
chrome.test.runTests([testStartStopNotifications]);
});
}); });
}); });
}); });
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
function listener(data) { function listener(data) {
chrome.test.fail('Event should not have been received.'); chrome.test.sendMessage('Event should not have been received.');
} }
chrome.bluetoothLowEnergy.onServiceChanged.addListener(listener); chrome.bluetoothLowEnergy.onServiceChanged.addListener(listener);
......
...@@ -2,7 +2,13 @@ ...@@ -2,7 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
var error;
function testWriteCharacteristicValue() { function testWriteCharacteristicValue() {
if (error !== undefined) {
chrome.test.sendMessage('fail');
chrome.test.fail(error);
}
chrome.test.assertTrue(characteristic != null, '\'characteristic\' is null'); chrome.test.assertTrue(characteristic != null, '\'characteristic\' is null');
chrome.test.assertEq(charId, characteristic.instanceId); chrome.test.assertEq(charId, characteristic.instanceId);
...@@ -29,22 +35,30 @@ var writeValue = new ArrayBuffer(bytes.length); ...@@ -29,22 +35,30 @@ var writeValue = new ArrayBuffer(bytes.length);
var valueBytes = new Uint8Array(writeValue); var valueBytes = new Uint8Array(writeValue);
valueBytes.set(bytes); valueBytes.set(bytes);
function earlyError(message) {
error = message;
chrome.test.runTests([testWriteCharacteristicValue]);
}
// 1. Unknown characteristic instanceId. // 1. Unknown characteristic instanceId.
writeCharacteristicValue(badCharId, writeValue, function (result) { writeCharacteristicValue(badCharId, writeValue, function (result) {
if (result || !chrome.runtime.lastError) { if (result || !chrome.runtime.lastError) {
chrome.test.fail('\'badCharId\' did not cause failure'); earlyError('\'badCharId\' did not cause failure');
return;
} }
// 2. Known characteristic instanceId, but call failure. // 2. Known characteristic instanceId, but call failure.
writeCharacteristicValue(charId, writeValue, function (result) { writeCharacteristicValue(charId, writeValue, function (result) {
if (result || !chrome.runtime.lastError) { if (result || !chrome.runtime.lastError) {
chrome.test.fail('writeCharacteristicValue should have failed'); earlyError('writeCharacteristicValue should have failed');
return;
} }
// 3. Call should succeed. // 3. Call should succeed.
writeCharacteristicValue(charId, writeValue, function (result) { writeCharacteristicValue(charId, writeValue, function (result) {
if (chrome.runtime.lastError) { if (chrome.runtime.lastError) {
chrome.test.fail(chrome.runtime.lastError.message); earlyError(chrome.runtime.lastError.message);
return;
} }
chrome.bluetoothLowEnergy.getCharacteristic(charId, function (result) { chrome.bluetoothLowEnergy.getCharacteristic(charId, function (result) {
......
...@@ -2,7 +2,13 @@ ...@@ -2,7 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
var error;
function testWriteDescriptorValue() { function testWriteDescriptorValue() {
if (error !== undefined) {
chrome.test.sendMessage('fail');
chrome.test.fail(error);
}
chrome.test.assertTrue(descriptor != null, '\'descriptor\' is null'); chrome.test.assertTrue(descriptor != null, '\'descriptor\' is null');
chrome.test.assertEq(descId, descriptor.instanceId); chrome.test.assertEq(descId, descriptor.instanceId);
...@@ -16,6 +22,11 @@ function testWriteDescriptorValue() { ...@@ -16,6 +22,11 @@ function testWriteDescriptorValue() {
chrome.test.succeed(); chrome.test.succeed();
} }
function earlyError(message) {
error = message;
chrome.test.runTests([testWriteDescriptorValue]);
}
var writeDescriptorValue = chrome.bluetoothLowEnergy.writeDescriptorValue; var writeDescriptorValue = chrome.bluetoothLowEnergy.writeDescriptorValue;
var descId = 'desc_id0'; var descId = 'desc_id0';
var badDescId = 'desc_id1'; var badDescId = 'desc_id1';
...@@ -30,19 +41,22 @@ valueBytes.set(bytes); ...@@ -30,19 +41,22 @@ valueBytes.set(bytes);
// 1. Unknown descriptor instanceId. // 1. Unknown descriptor instanceId.
writeDescriptorValue(badDescId, writeValue, function (result) { writeDescriptorValue(badDescId, writeValue, function (result) {
if (result || !chrome.runtime.lastError) { if (result || !chrome.runtime.lastError) {
chrome.test.fail('\'badDescId\' did not cause failure'); earlyError('\'badDescId\' did not cause failure');
return;
} }
// 2. Known descriptor instanceId, but call failure. // 2. Known descriptor instanceId, but call failure.
writeDescriptorValue(descId, writeValue, function (result) { writeDescriptorValue(descId, writeValue, function (result) {
if (result || !chrome.runtime.lastError) { if (result || !chrome.runtime.lastError) {
chrome.test.fail('writeDescriptorValue should have failed'); earlyError('writeDescriptorValue should have failed');
return;
} }
// 3. Call should succeed. // 3. Call should succeed.
writeDescriptorValue(descId, writeValue, function (result) { writeDescriptorValue(descId, writeValue, function (result) {
if (chrome.runtime.lastError) { if (chrome.runtime.lastError) {
chrome.test.fail(chrome.runtime.lastError.message); earlyError(chrome.runtime.lastError.message);
return;
} }
chrome.bluetoothLowEnergy.getDescriptor(descId, function (result) { chrome.bluetoothLowEnergy.getDescriptor(descId, function (result) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment