Skip to content

Commit

Permalink
bluetooth: Migrate server connection tests
Browse files Browse the repository at this point in the history
This change migrates the server/(dis)connect tests and the same object
test.

This change also modifies bluetooth-helpers.js to use the correct path
for health-thermometer-iframe.html for migrated tests and tests that
have not yet been migrated.

BUG=509038

Change-Id: I0485dd173898983841cea25a8ff3bdde198f9206
Reviewed-on: https://chromium-review.googlesource.com/836689
Commit-Queue: Ovidio Henriquez <[email protected]>
Reviewed-by: Conley Owens <[email protected]>
Reviewed-by: Vincent Scheib <[email protected]>
Cr-Commit-Position: refs/heads/master@{#528054}
  • Loading branch information
odejesush authored and chromium-wpt-export-bot committed Jan 9, 2018
1 parent 657ac6f commit ec972dc
Show file tree
Hide file tree
Showing 10 changed files with 254 additions and 1 deletion.
9 changes: 8 additions & 1 deletion bluetooth/resources/bluetooth-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,14 @@ function getHealthThermometerDeviceWithServicesDiscovered(options) {
code: HCI_SUCCESS,
}))
.then(() => new Promise(resolve => {
iframe.src = '../../../resources/bluetooth/health-thermometer-iframe.html';
let src = '/bluetooth/resources/health-thermometer-iframe.html';
// TODO(509038): Can be removed once LayoutTests/bluetooth/* that use
// health-thermometer-iframe.html have been moved to
// LayoutTests/external/wpt/bluetooth/*
if (window.location.pathname.includes('/LayoutTests/')) {
src = '../../../external/wpt/bluetooth/resources/health-thermometer-iframe.html';
}
iframe.src = src;
document.body.appendChild(iframe);
iframe.addEventListener('load', resolve);
}))
Expand Down
34 changes: 34 additions & 0 deletions bluetooth/resources/health-thermometer-iframe.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<script>
let device;

function requestDeviceWithOptionsAndConnect(options) {
return navigator.bluetooth.requestDevice(options)
.then(device => device.gatt.connect());
}

window.onmessage = messageEvent => {
switch (messageEvent.data.type) {
case 'RequestAndConnect':
requestDeviceWithOptionsAndConnect(messageEvent.data.options)
.then(gatt => {
device = gatt.device;
parent.postMessage('Connected', '*');
}).catch(err => {
parent.postMessage(`FAIL: ${err}`, '*');
});
break;
case 'DiscoverServices':
requestDeviceWithOptionsAndConnect(messageEvent.data.options)
.then(gatt => gatt.getPrimaryServices())
.then(() => parent.postMessage('DiscoveryComplete', '*'))
.catch(err => {
parent.postMessage(`FAIL: ${err}`, '*');
});
break;
default:
parent.postMessage(`FAIL: Bad message type: ${messageEvent.data.type}`,
'*');
}
};
</script>
17 changes: 17 additions & 0 deletions bluetooth/server/connect/connection-succeeds.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/bluetooth/resources/bluetooth-helpers.js"></script>
<script>
'use strict';
bluetooth_test(() => {
return getDiscoveredHealthThermometerDevice()
.then(({device, fake_peripheral}) => {
return fake_peripheral.setNextGATTConnectionResponse({code: HCI_SUCCESS})
.then(() => device.gatt.connect())
.then(gatt => assert_true(gatt.connected));
});
}, 'Device will connect');
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/bluetooth/resources/bluetooth-helpers.js"></script>
<script>
'use strict';
bluetooth_test(() => {
return getDiscoveredHealthThermometerDevice()
.then(({device, fake_peripheral}) => {
return fake_peripheral.setNextGATTConnectionResponse({code: HCI_SUCCESS})
.then(() => {
// Don't return the promise and let |device| go out of scope
// so that it gets garbage collected.
device.gatt.connect();
});
})
.then(runGarbageCollection)
}, 'Garbage Collection ran during a connect call that succeeds. ' +
'Should not crash.');
</script>
25 changes: 25 additions & 0 deletions bluetooth/server/connect/get-same-gatt-server.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/bluetooth/resources/bluetooth-helpers.js"></script>
<script>
'use strict';
bluetooth_test(() => {
return getDiscoveredHealthThermometerDevice()
.then(({device, fake_peripheral}) => {
return fake_peripheral
.setNextGATTConnectionResponse({code: HCI_SUCCESS})
.then(() => device.gatt.connect())
.then(gatt1 => {
// No second response is necessary because an ATT Bearer
// already exists from the first connection.
// See https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-connect
// step 5.1.
return device.gatt.connect().then(gatt2 => [gatt1, gatt2]);
});
})
.then(([gatt1, gatt2]) => assert_equals(gatt1, gatt2));
}, 'Multiple connects should return the same gatt object.');
</script>
19 changes: 19 additions & 0 deletions bluetooth/server/device-same-object.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/bluetooth/resources/bluetooth-helpers.js"></script>
<script>
'use strict';
bluetooth_test(() => {
return getDiscoveredHealthThermometerDevice()
.then(({device, fake_peripheral}) => {
return fake_peripheral.setNextGATTConnectionResponse({code: HCI_SUCCESS})
.then(() => device.gatt.connect());
})
.then(gatt => {
assert_equals(gatt.device, gatt.device);
});
}, "[SameObject] test for BluetoothRemoteGATTServer's device.");
</script>
29 changes: 29 additions & 0 deletions bluetooth/server/disconnect/connect-disconnect-twice.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/bluetooth/resources/bluetooth-helpers.js"></script>
<script>
'use strict';
const test_desc = 'Connect + Disconnect twice still results in ' +
'\'connected\' being false.';
let device, fake_peripheral;

// TODO(569716): Test that the disconnect signal was sent to the device.
bluetooth_test(() => getDiscoveredHealthThermometerDevice()
.then(_ => ({device, fake_peripheral} = _))
.then(() => fake_peripheral.setNextGATTConnectionResponse({
code: HCI_SUCCESS,
}))
.then(() => device.gatt.connect()
.then(gattServer => {
gattServer.disconnect();
assert_false(gattServer.connected);
})
.then(() => device.gatt.connect())
.then(gattServer => {
gattServer.disconnect();
assert_false(gattServer.connected);
})), test_desc);
</script>
36 changes: 36 additions & 0 deletions bluetooth/server/disconnect/detach-gc.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/bluetooth/resources/bluetooth-helpers.js"></script>
<body>
<script>
'use strict';
const test_desc = 'Detach frame then garbage collect. We shouldn\'t crash.';
let iframe = document.createElement('iframe');

bluetooth_test(() => setUpConnectableHealthThermometerDevice()
// 1. Load the iframe.
.then(() => new Promise(resolve => {
iframe.src = '/bluetooth/resources/health-thermometer-iframe.html';
document.body.appendChild(iframe);
iframe.addEventListener('load', resolve);
}))
// 2. Connect device, detach the iframe, and run garbage collection.
.then(() => new Promise(resolve => {
callWithTrustedClick(() => {
iframe.contentWindow.postMessage({
type: 'RequestAndConnect',
options: {filters: [{services: ['health_thermometer']}]}
}, '*');
});

window.onmessage = messageEvent => {
assert_equals(messageEvent.data, 'Connected');
iframe.remove();
runGarbageCollection().then(resolve);
}
})), test_desc)
</script>
</body>
26 changes: 26 additions & 0 deletions bluetooth/server/disconnect/disconnect-twice-in-a-row.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/bluetooth/resources/bluetooth-helpers.js"></script>
<script>
'use strict';
const test_desc = 'Calling disconnect twice in a row still results in ' +
'\'connected\' being false.';
let device, fake_peripheral;

// TODO(569716): Test that the disconnect signal was sent to the device.
bluetooth_test(() => getDiscoveredHealthThermometerDevice()
.then(_ => ({device, fake_peripheral} = _))
.then(() => fake_peripheral.setNextGATTConnectionResponse({
code: HCI_SUCCESS,
}))
.then(() => device.gatt.connect())
.then(gattServer => {
gattServer.disconnect();
assert_false(gattServer.connected);
gattServer.disconnect();
assert_false(gattServer.connected);
}), test_desc);
</script>
38 changes: 38 additions & 0 deletions bluetooth/server/disconnect/gc-detach.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/bluetooth/resources/bluetooth-helpers.js"></script>
<body>
<script>
'use strict';
const test_desc = 'Garbage collect then detach frame. We shouldn\'t crash.';
let iframe = document.createElement('iframe');

bluetooth_test(() => setUpConnectableHealthThermometerDevice()
// 1. Load the iframe.
.then((f) => new Promise(resolve => {
iframe.src = '/bluetooth/resources/health-thermometer-iframe.html';
document.body.appendChild(iframe);
iframe.addEventListener('load', resolve);
}))
// 2. Connect device, run garbage collection, and detach iframe.
.then(() => new Promise(resolve => {
callWithTrustedClick(() => {
iframe.contentWindow.postMessage({
type: 'RequestAndConnect',
options: {filters: [{services: ['health_thermometer']}]}
}, '*');
});

window.onmessage = messageEvent => {
assert_equals(messageEvent.data, 'Connected');
runGarbageCollection().then(() => {
iframe.remove();
resolve();
});
}
})), test_desc)
</script>
</body>

0 comments on commit ec972dc

Please sign in to comment.