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>
9 test(function(t
) { assert_true(window
.testRunner
instanceof Object
); t
.done(); },
10 'window.testRunner is required for the following tests.');
13 testRunner
.setBluetoothMockDataSet('EmptyAdapter');
14 return assert_promise_rejects(requestDeviceWithKeyDown(),
16 }, 'Requires an argument.');
19 testRunner
.setBluetoothMockDataSet('EmptyAdapter');
20 return assert_promise_rejects(requestDeviceWithKeyDown({}),
22 }, 'RequestDeviceOptions requires a |filters| member.');
24 // TODO(jyasskin): Add a test that the chooser is informed of a failed discovery
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.');
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
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
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.');
63 testRunner
.setBluetoothMockDataSet('GenericAccessAdapter');
65 requestDeviceWithKeyDown({filters
: [{services
: [generic_access
.alias
]}]}),
66 requestDeviceWithKeyDown({filters
: [{services
: [generic_access
.name
]}]}),
67 requestDeviceWithKeyDown({filters
: [{services
: [generic_access
.uuid
]}]})
69 devices
.forEach(device
=> {
70 assert_equals(device
.constructor.name
, 'BluetoothDevice');
73 }, 'Mock will resolve.');
76 testRunner
.setBluetoothMockDataSet('GenericAccessAdapter');
77 return assert_promise_rejects(
78 navigator
.bluetooth
.requestDevice({filters
: [{services
: ['generic_access']}]}),
80 }, 'Requires a user gesture.');
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']}]});
88 first
.then(device
=> assert_equals(device
.constructor.name
, 'BluetoothDevice')),
89 assert_promise_rejects(second
, 'SecurityError'),
92 }, 'Consumes a user gesture.');
95 testRunner
.setBluetoothMockDataSet('GenericAccessAdapter');
97 requestDeviceWithKeyDown({filters
: [{services
: [generic_access
.alias
]}]}),
98 requestDeviceWithKeyDown({filters
: [{services
: [generic_access
.name
]}]}),
99 requestDeviceWithKeyDown({filters
: [{services
: [generic_access
.uuid
]}]})
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.');
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({
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.');
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.');
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.');
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.');
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
150 return assert_promise_rejects(requestDeviceWithKeyDown({
151 filters
: [{services
: ['generic_access', 'battery_service']}]
152 }), 'NotFoundError');
153 }, 'Too-strict filters do prevent matching.');