Skip to content

Commit

Permalink
Refactoring the WebBluetooth html tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
zakorgy authored and fokinv committed Jul 28, 2016
1 parent 9c0e7b1 commit 5d3556a
Show file tree
Hide file tree
Showing 23 changed files with 1,112 additions and 119 deletions.
36 changes: 36 additions & 0 deletions tests/html/bluetooth/bluetooth_battery_level.html
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html>
<title>Battery Level</title>
<body>
<button type="button" onclick="onButtonClick()">Get Bluetooth Device's Battery Level</button>
<pre id="log"></pre>
<script src="bluetooth_functions.js"></script>
<script>
function onButtonClick() {
clear();
var options = {filters: [{services: ['battery_service']}], optionalServices: []};

try {
log('Requesting Bluetooth Device...');
var bluetooth = window.navigator.bluetooth;
var device = bluetooth.requestDevice(options);

log('Connecting to GATT Server on device...');
var server = device.gatt.connect();

log('Getting Battery Service...');
var service = server.getPrimaryService('battery_service');

log('Getting Battery Level Characteristic...');
var characteristic = service.getCharacteristic('battery_level');

log('Reading Battery Level...');
var value = asciiToDecimal(characteristic.readValue());
log('> Battery Level is ' + value + '%');
} catch(err) {
log(err);
}
}
</script>
</body>
</html>
@@ -1,23 +1,24 @@
<!DOCTYPE html>
<html>
<title>Battery Level</title>
<title>Battery Level with filters</title>
<body>
<input id="name" type="text" placeholder="Device Name">
<input id="namePrefix" type="text" placeholder="Device Name Prefix">
<button type="button" onclick="onButtonClick()">Get Bluetooth Device's Battery Level</button>
<pre id="log"></pre>
<script src="bluetooth_functions.js"></script>
<script>
function onButtonClick() {
clear();
var options = {filters: [{services: ['battery_service']}], optinalServices: []};
var options = {filters: [{services: ['battery_service']}], optionalServices: []};

var filterName = document.getElementById('name').value;
if (filterName)
options.filters.push({name: filterName});
options.filters[0].name = filterName;

var filterNamePrefix = document.getElementById('namePrefix').value;
if (filterNamePrefix)
options.filters.push({namePrefix: filterNamePrefix});
options.filters[0].namePrefix = filterNamePrefix;

try {
log('Requesting Bluetooth Device...');
Expand All @@ -34,28 +35,12 @@
var characteristic = service.getCharacteristic('battery_level');

log('Reading Battery Level...');
var value = AsciiToDecimal(characteristic.readValue());
var value = asciiToDecimal(characteristic.readValue());
log('> Battery Level is ' + value + '%');
} catch(err) {
log(err);
}
}

function clear() {
document.getElementById("log").textContent = "";
}

function log(line) {
document.getElementById("log").textContent += line + '\n';
}

function AsciiToDecimal(bytestr) {
var result = [];
for(i = 0; i < bytestr.length; i++) {
result[i] = bytestr[i].charCodeAt(0) ;
}
return result;
}
</script>
</body>
</html>
Expand Up @@ -6,14 +6,19 @@
<input id="characteristic" type="text" autofocus placeholder="Bluetooth Characteristic">
<button type="button" onclick="onButtonClick()">Get Characteristic Info</button>
<pre id="log"></pre>
<script src="bluetooth_functions.js"></script>
<script>
function onButtonClick() {
clear();
var serviceUuid = document.getElementById('service').value;
var characteristicUuid = document.getElementById('characteristic').value;

if (!serviceUuid || !characteristicUuid) {
return log('All input field must be filled!');
}
if (serviceUuid.startsWith('0x'))
serviceUuid = parseInt(serviceUuid, 16);

var characteristicUuid = document.getElementById('characteristic').value;
if (characteristicUuid.startsWith('0x'))
characteristicUuid = parseInt(characteristicUuid, 16);

Expand Down Expand Up @@ -43,27 +48,11 @@
log('> Queued Write: ' + characteristic.properties.reliableWrite);
log('> Writable Auxiliaries: ' + characteristic.properties.writableAuxiliaries);
characteristic.readValue();
log('> Characteristic value: ' + AsciiToDecimal(characteristic.value));
log('> Characteristic value: ' + asciiToDecimal(characteristic.value));
} catch(err) {
log(err);
}
}

function clear() {
document.getElementById("log").textContent = "";
}

function log(line) {
document.getElementById("log").textContent += line + '\n';
}

function AsciiToDecimal(bytestr) {
var result = [];
for(i = 0; i < bytestr.length; i++) {
result[i] = bytestr[i].charCodeAt(0) ;
}
return result;
}
</script>
</body>
</html>
@@ -0,0 +1,66 @@
<!DOCTYPE html>
<html>
<title>Characterstic's ReadValue Test Cases</title>
<body>
<div id="buttons"></div>
<pre id="log"></pre>
<script src="bluetooth_functions.js"></script>
<script>
var testCases = [];
//Test 1
testCases.push({characteristic: 'body_sensor_location', mustDisconnect: true});
//Test 2
testCases.push({characteristic: 'gap.reconnection_address', mustDisconnect: false});
//Test 3
testCases.push({characteristic: 'serial_number_string', mustDisconnect: false});
//Test 4
testCases.push({characteristic: 0x00002a03, mustDisconnect: false});
//Test 5
testCases.push({characteristic: 0x00002a25, mustDisconnect: false});
//Test 6
testCases.push({characteristic: '00002a03-0000-1000-8000-00805f9b34fb', mustDisconnect: false});
//Test 7
testCases.push({characteristic: '00002a25-0000-1000-8000-00805f9b34fb', mustDisconnect: false});
//Test 8
testCases.push({characteristic: 'body_sensor_location', mustDisconnect: false});
//Test 9
testCases.push({characteristic: 0x00002a38, mustDisconnect: false});
//Test 10
testCases.push({characteristic: '00002a38-0000-1000-8000-00805f9b34fb', mustDisconnect: false});
//Test 11
testCases.push({characteristic: 'heart_rate_control_point', mustDisconnect: false});

function onButtonClick(testNumber) {
clear();
try {
log('Requesting Bluetooth Device...');
var device = window.navigator.bluetooth.requestDevice({filters: [{services: ['heart_rate']}]});

log('Connecting to GATTserver on device...');
var server = device.gatt.connect();

log('Getting Primary Service "heart_rate"...');
var primaryService = server.getPrimaryService('heart_rate');

log('Getting Characteristic "' + testCases[testNumber].characteristic + '"...');
var characteristic = primaryService.getCharacteristic(testCases[testNumber].characteristic);

log('Characteristic found!');

if (testCases[testNumber].mustDisconnect) {
log('Disconnecting from server...');
device.gatt.disconnect();
}

log('Reading the value of the Characteristic...');
characteristic.readValue();
log('> Characteristic value: ' + asciiToDecimal(characteristic.value));
} catch(err) {
log(err);
}
}

populate(testCases);
</script>
</body>
</html>
@@ -0,0 +1,80 @@
<!DOCTYPE html>
<html>
<title>Characterstic's WriteValue Test Cases</title>
<body>
<div id="buttons"></div>
<pre id="log"></pre>
<script src="bluetooth_functions.js"></script>
<script>
var testCases = [];
//Test 1
testCases.push({characteristic: 0x2345, valueToWrite: [11], mustDisconnect: true});
//Test 2
testCases.push({characteristic: 0x2345, valueToWrite: new Array(513), mustDisconnect: false});
//Test 3
testCases.push({characteristic: 'gap.reconnection_address', valueToWrite: [1], mustDisconnect: false});
//Test 4
testCases.push({characteristic: 'serial_number_string', valueToWrite: [2], mustDisconnect: false});
//Test 5
testCases.push({characteristic: 0x00002a02, valueToWrite: [3], mustDisconnect: false});
//Test 6
testCases.push({characteristic: 0x00002a03, valueToWrite: [3], mustDisconnect: false});
//Test 7
testCases.push({characteristic: 0x00002a25, valueToWrite: [4], mustDisconnect: false});
//Test 8
testCases.push({characteristic: '00002a02-0000-1000-8000-00805f9b34fb', valueToWrite: [6], mustDisconnect: false});
//Test 9
testCases.push({characteristic: '00002a03-0000-1000-8000-00805f9b34fb', valueToWrite: [5], mustDisconnect: false});
//Test 10
testCases.push({characteristic: '00002a25-0000-1000-8000-00805f9b34fb', valueToWrite: [6], mustDisconnect: false});
//Test 11
testCases.push({characteristic: 0x2345, valueToWrite: [11]});
//Test 12
testCases.push({characteristic: '00002345-0000-1000-8000-00805f9b34fb', valueToWrite: [22], mustDisconnect: false});

function onButtonClick(testNumber) {
clear();
try {
log('Requesting Bluetooth Device...');
var device = window.navigator.bluetooth.requestDevice({filters: [{services: [0x1234]}]});

log('Connecting to GATTserver on device...');
var server = device.gatt.connect();

log('Getting Primary Service "Test Service"...');
var primaryService = server.getPrimaryService(0x1234);

log('Getting Characteristic "' + testCases[testNumber].characteristic + '"...');
var characteristic = primaryService.getCharacteristic(testCases[testNumber].characteristic);

log('Characteristic found!');

log('Reading the old value of the Characteristic...');
characteristic.readValue();
log('> Characteristic value: ' + asciiToDecimal(characteristic.value));

if (testCases[testNumber].mustDisconnect) {
log('Disconnecting from server...');
device.gatt.disconnect();
}

if (testNumber !== 1) {
log('Writing the value of the Characteristic with: ' + testCases[testNumber].valueToWrite + '...');
} else {
log('Writing the value of the Characteristic with a 513 long array...');
}

characteristic.writeValue(testCases[testNumber].valueToWrite);

log('Reading the new value of the Characteristic...');
characteristic.readValue();
log('> Characteristic value: ' + asciiToDecimal(characteristic.value));
} catch(err) {
log(err);
}
}

populate(testCases);
</script>
</body>
</html>
Expand Up @@ -7,18 +7,23 @@
<input id="descriptor" type="text" autofocus placeholder="Bluetooth Descriptor">
<button type="button" onclick="onButtonClick()">Get Descriptor Info</button>
<pre id="log"></pre>
<script src="bluetooth_functions.js"></script>
<script>
function onButtonClick() {
clear();
var serviceUuid = document.getElementById('service').value;
var characteristicUuid = document.getElementById('characteristic').value;
var descriptorUuid = document.getElementById('descriptor').value;

if (!serviceUuid || !characteristicUuid || !descriptorUuid) {
return log('All input field must be filled!');
}
if (serviceUuid.startsWith('0x'))
serviceUuid = parseInt(serviceUuid, 16);

var characteristicUuid = document.getElementById('characteristic').value;
if (characteristicUuid.startsWith('0x'))
characteristicUuid = parseInt(characteristicUuid, 16);

var descriptorUuid = document.getElementById('descriptor').value;
if (descriptorUuid.startsWith('0x'))
descriptorUuid = parseInt(descriptorUuid, 16);

Expand All @@ -42,27 +47,11 @@
log('> Descriptor characteristic: ' + descriptor.characteristic.uuid);
log('> Descriptor UUID: ' + descriptor.uuid);
descriptor.readValue();
log('> Descriptor value: ' + AsciiToDecimal(descriptor.value));
log('> Descriptor value: ' + asciiToDecimal(descriptor.value));
} catch(err) {
log(err);
}
}

function clear() {
document.getElementById("log").textContent = "";
}

function log(line) {
document.getElementById("log").textContent += line + '\n';
}

function AsciiToDecimal(bytestr) {
var result = [];
for(i = 0; i < bytestr.length; i++) {
result[i] = bytestr[i].charCodeAt(0) ;
}
return result;
}
</script>
</body>
</html>

0 comments on commit 5d3556a

Please sign in to comment.