| OLD | NEW |
| 1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
| 2 <script src="../resources/testharness.js"></script> | 2 <script src="../resources/testharness.js"></script> |
| 3 <script src="../resources/testharnessreport.js"></script> | 3 <script src="../resources/testharnessreport.js"></script> |
| 4 <script src="resources/bluetooth-helpers.js"></script> | 4 <script src="resources/bluetooth-helpers.js"></script> |
| 5 <script> | 5 <script> |
| 6 'use strict'; | 6 'use strict'; |
| 7 | 7 |
| 8 test(t => { assert_exists(window, 'testRunner'); t.done(); }, | 8 test(t => { assert_exists(window, 'testRunner'); t.done(); }, |
| 9 'window.testRunner is required for the following tests.'); | 9 'window.testRunner is required for the following tests.'); |
| 10 | 10 |
| 11 promise_test(() => { | 11 promise_test(() => { |
| 12 testRunner.setBluetoothMockDataSet('HeartRateAdapter'); | 12 return setBluetoothFakeAdapter('HeartRateAdapter') |
| 13 return requestDeviceWithKeyDown({filters: [{services: ['heart_rate']}], | 13 .then(() => requestDeviceWithKeyDown({ |
| 14 optionalServices: ['generic_access']}) | 14 filters: [{services: ['heart_rate']}], |
| 15 optionalServices: ['generic_access']})) |
| 15 .then(device => device.gatt.connect()) | 16 .then(device => device.gatt.connect()) |
| 16 .then(gattServer => gattServer.getPrimaryService('generic_access')) | 17 .then(gattServer => gattServer.getPrimaryService('generic_access')) |
| 17 .then(service => service.getCharacteristic('gap.device_name')) | 18 .then(service => service.getCharacteristic('gap.device_name')) |
| 18 .then(characteristic => { | 19 .then(characteristic => { |
| 19 testRunner.setBluetoothMockDataSet('EmptyAdapter'); | 20 return setBluetoothFakeAdapter('EmptyAdapter') |
| 20 return assert_promise_rejects_with_message( | 21 .then(() => assert_promise_rejects_with_message( |
| 21 characteristic.writeValue(new ArrayBuffer(1 /* length */)), | 22 characteristic.writeValue(new ArrayBuffer(1 /* length */)), |
| 22 new DOMException( | 23 new DOMException( |
| 23 'Bluetooth Device is no longer in range.', 'NetworkError'), | 24 'Bluetooth Device is no longer in range.', 'NetworkError'), |
| 24 'Device went out of range.'); | 25 'Device went out of range.')); |
| 25 }); | 26 }); |
| 26 }, 'Device goes out of range. Reject with NetworkError.'); | 27 }, 'Device goes out of range. Reject with NetworkError.'); |
| 27 | 28 |
| 28 promise_test(() => { | 29 promise_test(() => { |
| 29 testRunner.setBluetoothMockDataSet('HeartRateAdapter'); | 30 return setBluetoothFakeAdapter('HeartRateAdapter') |
| 30 return requestDeviceWithKeyDown({filters: [{services: ['heart_rate']}], | 31 .then(() => requestDeviceWithKeyDown({ |
| 31 optionalServices: ['generic_access']}) | 32 filters: [{services: ['heart_rate']}], |
| 33 optionalServices: ['generic_access']})) |
| 32 .then(device => device.gatt.connect()) | 34 .then(device => device.gatt.connect()) |
| 33 .then(gattServer => gattServer.getPrimaryService('generic_access')) | 35 .then(gattServer => gattServer.getPrimaryService('generic_access')) |
| 34 .then(service => service.getCharacteristic('gap.device_name')) | 36 .then(service => service.getCharacteristic('gap.device_name')) |
| 35 .then(characteristic => { | 37 .then(characteristic => { |
| 36 testRunner.setBluetoothMockDataSet('MissingServiceHeartRateAdapter'); | 38 return setBluetoothFakeAdapter('MissingServiceHeartRateAdapter') |
| 37 return assert_promise_rejects_with_message( | 39 .then(() => assert_promise_rejects_with_message( |
| 38 characteristic.writeValue(new ArrayBuffer(1 /* length */)), | 40 characteristic.writeValue(new ArrayBuffer(1 /* length */)), |
| 39 new DOMException('GATT Service no longer exists.', | 41 new DOMException('GATT Service no longer exists.', |
| 40 'InvalidStateError'), | 42 'InvalidStateError'), |
| 41 'Service got removed.'); | 43 'Service got removed.')); |
| 42 }); | 44 }); |
| 43 }, 'Service gets removed. Reject with InvalidStateError.'); | 45 }, 'Service gets removed. Reject with InvalidStateError.'); |
| 44 | 46 |
| 45 promise_test(() => { | 47 promise_test(() => { |
| 46 testRunner.setBluetoothMockDataSet('HeartRateAdapter'); | 48 return setBluetoothFakeAdapter('HeartRateAdapter') |
| 47 return requestDeviceWithKeyDown({filters: [{services: ['heart_rate']}], | 49 .then(() => requestDeviceWithKeyDown({ |
| 48 optionalServices: ['generic_access']}) | 50 filters: [{services: ['heart_rate']}], |
| 51 optionalServices: ['generic_access']})) |
| 49 .then(device => device.gatt.connect()) | 52 .then(device => device.gatt.connect()) |
| 50 .then(gattServer => gattServer.getPrimaryService('generic_access')) | 53 .then(gattServer => gattServer.getPrimaryService('generic_access')) |
| 51 .then(service => service.getCharacteristic('gap.device_name')) | 54 .then(service => service.getCharacteristic('gap.device_name')) |
| 52 .then(characteristic => { | 55 .then(characteristic => { |
| 53 testRunner.setBluetoothMockDataSet('MissingCharacteristicHeartRateAdapter'
); | 56 return setBluetoothFakeAdapter('MissingCharacteristicHeartRateAdapter') |
| 54 return assert_promise_rejects_with_message( | 57 .then(() => assert_promise_rejects_with_message( |
| 55 characteristic.writeValue(new ArrayBuffer(1 /* length */)), | 58 characteristic.writeValue(new ArrayBuffer(1 /* length */)), |
| 56 new DOMException( | 59 new DOMException( |
| 57 'GATT Characteristic no longer exists.', 'InvalidStateError'), | 60 'GATT Characteristic no longer exists.', 'InvalidStateError'), |
| 58 'Characteristic got removed.'); | 61 'Characteristic got removed.')); |
| 59 }); | 62 }); |
| 60 }, 'Characteristic gets removed. Reject with InvalidStateError.'); | 63 }, 'Characteristic gets removed. Reject with InvalidStateError.'); |
| 61 | 64 |
| 62 gatt_errors_tests.forEach(testSpec => { | 65 gatt_errors_tests.forEach(testSpec => { |
| 63 promise_test(() => { | 66 promise_test(() => { |
| 64 testRunner.setBluetoothMockDataSet('FailingGATTOperationsAdapter'); | 67 return setBluetoothFakeAdapter('FailingGATTOperationsAdapter') |
| 65 return requestDeviceWithKeyDown({filters: [{services: [errorUUID(0xA0)]}]}) | 68 .then(() => requestDeviceWithKeyDown({ |
| 69 filters: [{services: [errorUUID(0xA0)]}]})) |
| 66 .then(device => device.gatt.connect()) | 70 .then(device => device.gatt.connect()) |
| 67 .then(gattServer => gattServer.getPrimaryService(errorUUID(0xA0))) | 71 .then(gattServer => gattServer.getPrimaryService(errorUUID(0xA0))) |
| 68 .then(service => service.getCharacteristic(testSpec.uuid)) | 72 .then(service => service.getCharacteristic(testSpec.uuid)) |
| 69 .then(characteristic => { | 73 .then(characteristic => { |
| 70 return assert_promise_rejects_with_message( | 74 return assert_promise_rejects_with_message( |
| 71 characteristic.writeValue(new Uint8Array([1])), | 75 characteristic.writeValue(new Uint8Array([1])), |
| 72 testSpec.error, | 76 testSpec.error, |
| 73 'Trying to write to a characteristic failed.'); | 77 'Trying to write to a characteristic failed.'); |
| 74 }); | 78 }); |
| 75 }, testSpec.testName); | 79 }, testSpec.testName); |
| 76 }); | 80 }); |
| 77 | 81 |
| 78 promise_test(() => { | 82 promise_test(() => { |
| 79 testRunner.setBluetoothMockDataSet('HeartRateAdapter'); | 83 return setBluetoothFakeAdapter('HeartRateAdapter') |
| 80 return requestDeviceWithKeyDown({filters: [{services: ['heart_rate']}], | 84 .then(() => requestDeviceWithKeyDown({ |
| 81 optionalServices: ['generic_access']}) | 85 filters: [{services: ['heart_rate']}], |
| 86 optionalServices: ['generic_access']})) |
| 82 .then(device => device.gatt.connect()) | 87 .then(device => device.gatt.connect()) |
| 83 .then(gattServer => gattServer.getPrimaryService('generic_access')) | 88 .then(gattServer => gattServer.getPrimaryService('generic_access')) |
| 84 .then(service => service.getCharacteristic('gap.device_name')) | 89 .then(service => service.getCharacteristic('gap.device_name')) |
| 85 .then(characteristic => { | 90 .then(characteristic => { |
| 86 return assert_promise_rejects_with_message( | 91 return assert_promise_rejects_with_message( |
| 87 characteristic.writeValue(new Uint8Array(513 /* length */)), | 92 characteristic.writeValue(new Uint8Array(513 /* length */)), |
| 88 new DOMException( | 93 new DOMException( |
| 89 'Value can\'t exceed 512 bytes.', 'InvalidModificationError'), | 94 'Value can\'t exceed 512 bytes.', 'InvalidModificationError'), |
| 90 'Value passed was too long.'); | 95 'Value passed was too long.'); |
| 91 }); | 96 }); |
| 92 }, 'Trying to write more than 512 bytes should return an error.'); | 97 }, 'Trying to write more than 512 bytes should return an error.'); |
| 93 | 98 |
| 94 promise_test(() => { | 99 promise_test(() => { |
| 95 testRunner.setBluetoothMockDataSet('HeartRateAdapter'); | 100 return setBluetoothFakeAdapter('HeartRateAdapter') |
| 96 return requestDeviceWithKeyDown({filters: [{services: ['heart_rate']}], | 101 .then(() => requestDeviceWithKeyDown({ |
| 97 optionalServices: ['generic_access']}) | 102 filters: [{services: ['heart_rate']}], |
| 103 optionalServices: ['generic_access']})) |
| 98 .then(device => device.gatt.connect()) | 104 .then(device => device.gatt.connect()) |
| 99 .then(gattServer => gattServer.getPrimaryService('generic_access')) | 105 .then(gattServer => gattServer.getPrimaryService('generic_access')) |
| 100 .then(service => service.getCharacteristic('gap.device_name')) | 106 .then(service => service.getCharacteristic('gap.device_name')) |
| 101 .then(characteristic => Promise.all( | 107 .then(characteristic => Promise.all( |
| 102 [characteristic.writeValue(new Uint8Array(1 /* length */)), | 108 [characteristic.writeValue(new Uint8Array(1 /* length */)), |
| 103 characteristic.writeValue(new ArrayBuffer(1 /* length */)), | 109 characteristic.writeValue(new ArrayBuffer(1 /* length */)), |
| 104 characteristic.writeValue(new DataView(new ArrayBuffer(1 /* length */)))]
)); | 110 characteristic.writeValue(new DataView(new ArrayBuffer(1 /* length */)))]
)); |
| 105 }, 'A regular write request to a writable characteristic should succeed.'); | 111 }, 'A regular write request to a writable characteristic should succeed.'); |
| 106 | 112 |
| 107 promise_test(() => { | 113 promise_test(() => { |
| 108 testRunner.setBluetoothMockDataSet('HeartRateAdapter'); | 114 return setBluetoothFakeAdapter('HeartRateAdapter') |
| 109 return requestDeviceWithKeyDown({filters: [{services: ['heart_rate']}], | 115 .then(() => requestDeviceWithKeyDown({ |
| 110 optionalServices: ['generic_access']}) | 116 filters: [{services: ['heart_rate']}], |
| 117 optionalServices: ['generic_access']})) |
| 111 .then(device => device.gatt.connect()) | 118 .then(device => device.gatt.connect()) |
| 112 .then(gattServer => gattServer.getPrimaryService('generic_access')) | 119 .then(gattServer => gattServer.getPrimaryService('generic_access')) |
| 113 .then(service => service.getCharacteristic('gap.device_name')) | 120 .then(service => service.getCharacteristic('gap.device_name')) |
| 114 .then(characteristic => { | 121 .then(characteristic => { |
| 115 assert_equals(characteristic.value, null); | 122 assert_equals(characteristic.value, null); |
| 116 let textEncoder = new TextEncoder(); | 123 let textEncoder = new TextEncoder(); |
| 117 let newValue = textEncoder.encode('foo'); | 124 let newValue = textEncoder.encode('foo'); |
| 118 return characteristic.writeValue(newValue).then(() => { | 125 return characteristic.writeValue(newValue).then(() => { |
| 119 assert_array_equals(characteristic.value.buffer, newValue.buffer); | 126 assert_array_equals(characteristic.value.buffer, newValue.buffer); |
| 120 }); | 127 }); |
| 121 }); | 128 }); |
| 122 }, 'A regular write request to a writable characteristic should update value.'); | 129 }, 'A regular write request to a writable characteristic should update value.'); |
| 123 </script> | 130 </script> |
| OLD | NEW |