Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / bluetooth / requestDevice.html
blob834bc70756f228076fc087c42c09e290d99cef8e
1 <!DOCTYPE html>
2 <script src="../resources/testharness.js"></script>
3 <script src="../resources/testharness-helpers.js"></script>
4 <script src="../resources/testharnessreport.js"></script>
5 <script src="resources/bluetooth-helpers.js"></script>
6 <script>
7 'use strict';
9 test(function(t) { assert_true(window.testRunner instanceof Object); t.done(); },
10 'window.testRunner is required for the following tests.');
12 promise_test(() => {
13 testRunner.setBluetoothMockDataSet('EmptyAdapter');
14 return assert_promise_rejects(requestDeviceWithKeyDown(),
15 new TypeError());
16 }, 'Requires an argument.');
18 promise_test(() => {
19 testRunner.setBluetoothMockDataSet('EmptyAdapter');
20 return assert_promise_rejects(requestDeviceWithKeyDown({}),
21 new TypeError());
22 }, 'RequestDeviceOptions requires a |filters| member.');
24 // TODO(jyasskin): Add a test that the chooser is informed of a failed discovery
25 // session.
26 promise_test(() => {
27 testRunner.setBluetoothMockDataSet('FailStartDiscoveryAdapter');
28 return assert_promise_rejects_with_message(
29 requestDeviceWithKeyDown({filters: [{services: ['generic_access']}]}),
30 {name: 'NotFoundError', message: 'User cancelled the requestDevice() chooser.'},
31 'The adapter failed to start a discovery session.');
32 }, 'Discovery session fails to start.');
34 promise_test(() => {
35 testRunner.setBluetoothMockDataSet('NotPresentAdapter');
36 return assert_promise_rejects_with_message(
37 requestDeviceWithKeyDown({filters: [{services: ['generic_access']}]}),
38 {name: 'NotFoundError', message: 'Bluetooth adapter not available.'},
39 'Bluetooth adapter is not present.');
40 }, 'Reject with NotFoundError if the adapter is not present.');
42 // TODO(jyasskin): Add a test that the chooser is informed of a disabled
43 // Bluetooth adapter.
44 promise_test(() => {
45 testRunner.setBluetoothMockDataSet('NotPoweredAdapter');
46 return assert_promise_rejects_with_message(
47 requestDeviceWithKeyDown({filters: [{services: ['generic_access']}]}),
48 {name: 'NotFoundError', message: 'User cancelled the requestDevice() chooser.'},
49 'Bluetooth adapter is not powered.');
50 }, 'Reject with NotFoundError if the adapter is off.');
52 // TODO(jyasskin): Add a test that the chooser gets a full list of found
53 // devices.
54 promise_test(() => {
55 testRunner.setBluetoothMockDataSet('EmptyAdapter');
56 return assert_promise_rejects_with_message(
57 requestDeviceWithKeyDown({filters: [{services: ['generic_access']}]}),
58 {name: 'NotFoundError', message: 'User cancelled the requestDevice() chooser.'},
59 'No Bluetooth devices in range.');
60 }, 'Reject with NotFoundError if there are no devices around.');
62 promise_test(() => {
63 testRunner.setBluetoothMockDataSet('GenericAccessAdapter');
64 return Promise.all([
65 requestDeviceWithKeyDown({filters: [{services: [generic_access.alias]}]}),
66 requestDeviceWithKeyDown({filters: [{services: [generic_access.name]}]}),
67 requestDeviceWithKeyDown({filters: [{services: [generic_access.uuid]}]})
68 ]).then(devices => {
69 devices.forEach(device => {
70 assert_equals(device.constructor.name, 'BluetoothDevice');
71 });
72 });
73 }, 'Mock will resolve.');
75 promise_test(() => {
76 testRunner.setBluetoothMockDataSet('GenericAccessAdapter');
77 return assert_promise_rejects(
78 navigator.bluetooth.requestDevice({filters: [{services: ['generic_access']}]}),
79 'SecurityError');
80 }, 'Requires a user gesture.');
82 promise_test(() => {
83 testRunner.setBluetoothMockDataSet('GenericAccessAdapter');
84 return callWithKeyDown(() => {
85 var first = navigator.bluetooth.requestDevice({filters: [{services: ['generic_access']}]});
86 var second = navigator.bluetooth.requestDevice({filters: [{services: ['generic_access']}]});
87 return Promise.all([
88 first.then(device => assert_equals(device.constructor.name, 'BluetoothDevice')),
89 assert_promise_rejects(second, 'SecurityError'),
90 ]);
91 });
92 }, 'Consumes a user gesture.');
94 promise_test(() => {
95 testRunner.setBluetoothMockDataSet('GenericAccessAdapter');
96 return Promise.all([
97 requestDeviceWithKeyDown({filters: [{services: [generic_access.alias]}]}),
98 requestDeviceWithKeyDown({filters: [{services: [generic_access.name]}]}),
99 requestDeviceWithKeyDown({filters: [{services: [generic_access.uuid]}]})
100 ]).then(devices => {
101 // requestDevice should return the same object if it was created
102 // earlier. https://crbug.com/495270
103 // TODO(ortuno): Change to assert_equals.
104 assert_not_equals(devices[0], devices[1]);
105 assert_not_equals(devices[1], devices[2]);
107 }, 'Returned device should always be the same.');
109 promise_test(() => {
110 testRunner.setBluetoothMockDataSet('ScanFilterCheckingAdapter');
111 // The work of this test is done in the ScanFilterCheckingAdapter. It asserts
112 // that this requestDevice() call tells the platform to scan for only devices
113 // that include the Battery, Glucose, or Heart Rate services.
114 return requestDeviceWithKeyDown({
115 filters: [
116 {services: ['battery_service']},
117 {services: ['glucose', 'heart_rate']}
119 // The optionalServices shouldn't affect the platform's scan.
120 optionalServices: [generic_access.name]
122 }, 'Filters restrict the platform\'s Bluetooth scan.');
124 promise_test(() => {
125 testRunner.setBluetoothMockDataSet('GlucoseHeartRateAdapter');
126 return requestDeviceWithKeyDown({filters: [{services: ['glucose']}]})
127 .then(device => assert_equals(device.name, 'Glucose Device'));
128 }, 'Simple filter selects matching device.');
130 promise_test(() => {
131 testRunner.setBluetoothMockDataSet('GlucoseHeartRateAdapter');
132 return requestDeviceWithKeyDown({
133 filters: [{services: ['generic_access', 'heart_rate']}]
134 }).then(device => assert_equals(device.name, 'Heart Rate Device'));
135 }, 'Filter with 2 services returns a matching device.');
137 promise_test(() => {
138 testRunner.setBluetoothMockDataSet('GlucoseHeartRateAdapter');
139 return requestDeviceWithKeyDown({
140 filters: [{services: ['battery_service']},
141 {services: ['heart_rate']}]
142 }).then(device => assert_equals(device.name, 'Heart Rate Device'));
143 }, 'An extra filter doesn\'t prevent matching.');
145 promise_test(() => {
146 testRunner.setBluetoothMockDataSet('GlucoseHeartRateAdapter');
147 // Both devices support the Generic Access service, but things need to
148 // support both services to pass the filter, and neither has a Battery
149 // service.
150 return assert_promise_rejects(requestDeviceWithKeyDown({
151 filters: [{services: ['generic_access', 'battery_service']}]
152 }), 'NotFoundError');
153 }, 'Too-strict filters do prevent matching.');
154 </script>