Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / device / bluetooth / bluetooth_adapter_unittest.cc
blobc028b972218a7fb8ca99476f3674ffc8dc06e391
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/bind.h"
6 #include "base/memory/ref_counted.h"
7 #include "device/bluetooth/bluetooth_adapter.h"
8 #include "device/bluetooth/bluetooth_device.h"
9 #include "device/bluetooth/bluetooth_discovery_session.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 using device::BluetoothAdapter;
13 using device::BluetoothDevice;
15 namespace device {
17 class TestBluetoothAdapter : public BluetoothAdapter {
18 public:
19 TestBluetoothAdapter() {
22 std::string GetAddress() const override { return ""; }
24 std::string GetName() const override { return ""; }
26 void SetName(const std::string& name,
27 const base::Closure& callback,
28 const ErrorCallback& error_callback) override {}
30 bool IsInitialized() const override { return false; }
32 bool IsPresent() const override { return false; }
34 bool IsPowered() const override { return false; }
36 void SetPowered(bool powered,
37 const base::Closure& callback,
38 const ErrorCallback& error_callback) override {}
40 bool IsDiscoverable() const override { return false; }
42 void SetDiscoverable(bool discoverable,
43 const base::Closure& callback,
44 const ErrorCallback& error_callback) override {}
46 bool IsDiscovering() const override { return false; }
48 void StartDiscoverySessionWithFilter(
49 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter,
50 const DiscoverySessionCallback& callback,
51 const ErrorCallback& error_callback) override {
52 OnStartDiscoverySession(discovery_filter.Pass(), callback);
55 void StartDiscoverySession(const DiscoverySessionCallback& callback,
56 const ErrorCallback& error_callback) override {}
57 void CreateRfcommService(
58 const BluetoothUUID& uuid,
59 const ServiceOptions& options,
60 const CreateServiceCallback& callback,
61 const CreateServiceErrorCallback& error_callback) override {}
63 void CreateL2capService(
64 const BluetoothUUID& uuid,
65 const ServiceOptions& options,
66 const CreateServiceCallback& callback,
67 const CreateServiceErrorCallback& error_callback) override {}
69 void RegisterAudioSink(
70 const BluetoothAudioSink::Options& options,
71 const AcquiredCallback& callback,
72 const BluetoothAudioSink::ErrorCallback& error_callback) override {}
74 void TestErrorCallback() {}
76 ScopedVector<BluetoothDiscoverySession> discovery_sessions_;
78 void TestOnStartDiscoverySession(
79 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) {
80 discovery_sessions_.push_back(discovery_session.Pass());
83 void CleanupSessions() { discovery_sessions_.clear(); }
85 void InjectFilteredSession(
86 scoped_ptr<device::BluetoothDiscoveryFilter> discovery_filter) {
87 StartDiscoverySessionWithFilter(
88 discovery_filter.Pass(),
89 base::Bind(&TestBluetoothAdapter::TestOnStartDiscoverySession,
90 base::Unretained(this)),
91 base::Bind(&TestBluetoothAdapter::TestErrorCallback,
92 base::Unretained(this)));
95 protected:
96 ~TestBluetoothAdapter() override {}
98 void AddDiscoverySession(BluetoothDiscoveryFilter* discovery_filter,
99 const base::Closure& callback,
100 const ErrorCallback& error_callback) override {}
102 void RemoveDiscoverySession(BluetoothDiscoveryFilter* discovery_filter,
103 const base::Closure& callback,
104 const ErrorCallback& error_callback) override {}
106 void SetDiscoveryFilter(scoped_ptr<BluetoothDiscoveryFilter> discovery_filter,
107 const base::Closure& callback,
108 const ErrorCallback& error_callback) override {}
110 void RemovePairingDelegateInternal(
111 BluetoothDevice::PairingDelegate* pairing_delegate) override {}
114 class TestPairingDelegate : public BluetoothDevice::PairingDelegate {
115 public:
116 void RequestPinCode(BluetoothDevice* device) override {}
117 void RequestPasskey(BluetoothDevice* device) override {}
118 void DisplayPinCode(BluetoothDevice* device,
119 const std::string& pincode) override {}
120 void DisplayPasskey(BluetoothDevice* device, uint32 passkey) override {}
121 void KeysEntered(BluetoothDevice* device, uint32 entered) override {}
122 void ConfirmPasskey(BluetoothDevice* device, uint32 passkey) override {}
123 void AuthorizePairing(BluetoothDevice* device) override {}
127 TEST(BluetoothAdapterTest, NoDefaultPairingDelegate) {
128 scoped_refptr<BluetoothAdapter> adapter = new TestBluetoothAdapter();
130 // Verify that when there is no registered pairing delegate, NULL is returned.
131 EXPECT_TRUE(adapter->DefaultPairingDelegate() == NULL);
134 TEST(BluetoothAdapterTest, OneDefaultPairingDelegate) {
135 scoped_refptr<BluetoothAdapter> adapter = new TestBluetoothAdapter();
137 // Verify that when there is one registered pairing delegate, it is returned.
138 TestPairingDelegate delegate;
140 adapter->AddPairingDelegate(&delegate,
141 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW);
143 EXPECT_EQ(&delegate, adapter->DefaultPairingDelegate());
146 TEST(BluetoothAdapterTest, SamePriorityDelegates) {
147 scoped_refptr<BluetoothAdapter> adapter = new TestBluetoothAdapter();
149 // Verify that when there are two registered pairing delegates of the same
150 // priority, the first one registered is returned.
151 TestPairingDelegate delegate1, delegate2;
153 adapter->AddPairingDelegate(&delegate1,
154 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW);
155 adapter->AddPairingDelegate(&delegate2,
156 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW);
158 EXPECT_EQ(&delegate1, adapter->DefaultPairingDelegate());
160 // After unregistering the first, the second can be returned.
161 adapter->RemovePairingDelegate(&delegate1);
163 EXPECT_EQ(&delegate2, adapter->DefaultPairingDelegate());
166 TEST(BluetoothAdapterTest, HighestPriorityDelegate) {
167 scoped_refptr<BluetoothAdapter> adapter = new TestBluetoothAdapter();
169 // Verify that when there are two registered pairing delegates, the one with
170 // the highest priority is returned.
171 TestPairingDelegate delegate1, delegate2;
173 adapter->AddPairingDelegate(&delegate1,
174 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW);
175 adapter->AddPairingDelegate(&delegate2,
176 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
178 EXPECT_EQ(&delegate2, adapter->DefaultPairingDelegate());
181 TEST(BluetoothAdapterTest, UnregisterDelegate) {
182 scoped_refptr<BluetoothAdapter> adapter = new TestBluetoothAdapter();
184 // Verify that after unregistering a delegate, NULL is returned.
185 TestPairingDelegate delegate;
187 adapter->AddPairingDelegate(&delegate,
188 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW);
189 adapter->RemovePairingDelegate(&delegate);
191 EXPECT_TRUE(adapter->DefaultPairingDelegate() == NULL);
194 TEST(BluetoothAdapterTest, GetMergedDiscoveryFilterEmpty) {
195 scoped_refptr<BluetoothAdapter> adapter = new TestBluetoothAdapter();
196 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter;
198 discovery_filter = adapter->GetMergedDiscoveryFilter();
199 EXPECT_TRUE(discovery_filter.get() == nullptr);
201 discovery_filter = adapter->GetMergedDiscoveryFilterMasked(nullptr);
202 EXPECT_TRUE(discovery_filter.get() == nullptr);
205 TEST(BluetoothAdapterTest, GetMergedDiscoveryFilterRegular) {
206 scoped_refptr<TestBluetoothAdapter> adapter = new TestBluetoothAdapter();
207 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter;
209 // make sure adapter have one session wihout filtering.
210 adapter->InjectFilteredSession(discovery_filter.Pass());
212 // having one reglar session should result in no filter
213 scoped_ptr<BluetoothDiscoveryFilter> resulting_filter =
214 adapter->GetMergedDiscoveryFilter();
215 EXPECT_TRUE(resulting_filter.get() == nullptr);
217 // omiting no filter when having one reglar session should result in no filter
218 resulting_filter = adapter->GetMergedDiscoveryFilterMasked(nullptr);
219 EXPECT_TRUE(resulting_filter.get() == nullptr);
221 adapter->CleanupSessions();
224 TEST(BluetoothAdapterTest, GetMergedDiscoveryFilterRssi) {
225 scoped_refptr<TestBluetoothAdapter> adapter = new TestBluetoothAdapter();
226 int16_t resulting_rssi;
227 uint16_t resulting_pathloss;
228 scoped_ptr<BluetoothDiscoveryFilter> resulting_filter;
230 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
231 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
232 df->SetRSSI(-30);
233 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
235 BluetoothDiscoveryFilter* df2 = new BluetoothDiscoveryFilter(
236 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
237 df2->SetRSSI(-65);
238 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter2(df2);
240 // make sure adapter have one session wihout filtering.
241 adapter->InjectFilteredSession(discovery_filter.Pass());
243 // DO_NOTHING should have no impact
244 resulting_filter = adapter->GetMergedDiscoveryFilter();
245 resulting_filter->GetRSSI(&resulting_rssi);
246 EXPECT_EQ(-30, resulting_rssi);
248 // should not use df2 at all, as it's not associated with adapter yet
249 resulting_filter = adapter->GetMergedDiscoveryFilterMasked(df2);
250 resulting_filter->GetRSSI(&resulting_rssi);
251 EXPECT_EQ(-30, resulting_rssi);
253 adapter->InjectFilteredSession(discovery_filter2.Pass());
255 // result of merging two rssi values should be lower one
256 resulting_filter = adapter->GetMergedDiscoveryFilter();
257 resulting_filter->GetRSSI(&resulting_rssi);
258 EXPECT_EQ(-65, resulting_rssi);
260 // ommit bigger value, result should stay same
261 resulting_filter = adapter->GetMergedDiscoveryFilterMasked(df);
262 resulting_filter->GetRSSI(&resulting_rssi);
263 EXPECT_EQ(-65, resulting_rssi);
265 // ommit lower value, result should change
266 resulting_filter = adapter->GetMergedDiscoveryFilterMasked(df2);
267 resulting_filter->GetRSSI(&resulting_rssi);
268 EXPECT_EQ(-30, resulting_rssi);
270 BluetoothDiscoveryFilter* df3 = new BluetoothDiscoveryFilter(
271 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
272 df3->SetPathloss(60);
273 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter3(df3);
275 // when rssi and pathloss are merged, both should be cleared, becuase there is
276 // no way to tell which filter will be more generic
277 adapter->InjectFilteredSession(discovery_filter3.Pass());
278 resulting_filter = adapter->GetMergedDiscoveryFilter();
279 EXPECT_FALSE(resulting_filter->GetRSSI(&resulting_rssi));
280 EXPECT_FALSE(resulting_filter->GetPathloss(&resulting_pathloss));
282 adapter->CleanupSessions();
285 TEST(BluetoothAdapterTest, GetMergedDiscoveryFilterTransport) {
286 scoped_refptr<TestBluetoothAdapter> adapter = new TestBluetoothAdapter();
287 scoped_ptr<BluetoothDiscoveryFilter> resulting_filter;
289 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
290 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC);
291 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
293 BluetoothDiscoveryFilter* df2 = new BluetoothDiscoveryFilter(
294 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
295 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter2(df2);
297 adapter->InjectFilteredSession(discovery_filter.Pass());
299 // Just one filter, make sure transport was properly rewritten
300 resulting_filter = adapter->GetMergedDiscoveryFilter();
301 EXPECT_EQ(BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC,
302 resulting_filter->GetTransport());
304 adapter->InjectFilteredSession(discovery_filter2.Pass());
306 // Two filters, should have OR of both transport's
307 resulting_filter = adapter->GetMergedDiscoveryFilter();
308 EXPECT_EQ(BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL,
309 resulting_filter->GetTransport());
311 // When 1st filter is masked, 2nd filter transport should be returned.
312 resulting_filter = adapter->GetMergedDiscoveryFilterMasked(df);
313 EXPECT_EQ(BluetoothDiscoveryFilter::Transport::TRANSPORT_LE,
314 resulting_filter->GetTransport());
316 // When 2nd filter is masked, 1st filter transport should be returned.
317 resulting_filter = adapter->GetMergedDiscoveryFilterMasked(df2);
318 EXPECT_EQ(BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC,
319 resulting_filter->GetTransport());
321 BluetoothDiscoveryFilter* df3 = new BluetoothDiscoveryFilter(
322 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
323 df3->CopyFrom(BluetoothDiscoveryFilter(
324 BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL));
325 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter3(df3);
327 // Merging empty filter in should result in empty filter
328 adapter->InjectFilteredSession(discovery_filter3.Pass());
329 resulting_filter = adapter->GetMergedDiscoveryFilter();
330 EXPECT_TRUE(resulting_filter->IsDefault());
332 adapter->CleanupSessions();
335 TEST(BluetoothAdapterTest, GetMergedDiscoveryFilterAllFields) {
336 scoped_refptr<TestBluetoothAdapter> adapter = new TestBluetoothAdapter();
337 int16_t resulting_rssi;
338 std::set<device::BluetoothUUID> resulting_uuids;
340 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
341 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
342 df->SetRSSI(-60);
343 df->AddUUID(device::BluetoothUUID("1000"));
344 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
346 BluetoothDiscoveryFilter* df2 = new BluetoothDiscoveryFilter(
347 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
348 df2->SetRSSI(-85);
349 df2->SetTransport(BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
350 df2->AddUUID(device::BluetoothUUID("1020"));
351 df2->AddUUID(device::BluetoothUUID("1001"));
352 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter2(df2);
354 BluetoothDiscoveryFilter* df3 = new BluetoothDiscoveryFilter(
355 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
356 df3->SetRSSI(-65);
357 df3->SetTransport(BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC);
358 df3->AddUUID(device::BluetoothUUID("1020"));
359 df3->AddUUID(device::BluetoothUUID("1003"));
360 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter3(df3);
362 // make sure adapter have one session wihout filtering.
363 adapter->InjectFilteredSession(discovery_filter.Pass());
364 adapter->InjectFilteredSession(discovery_filter2.Pass());
365 adapter->InjectFilteredSession(discovery_filter3.Pass());
367 scoped_ptr<BluetoothDiscoveryFilter> resulting_filter =
368 adapter->GetMergedDiscoveryFilter();
369 resulting_filter->GetRSSI(&resulting_rssi);
370 resulting_filter->GetUUIDs(resulting_uuids);
371 EXPECT_TRUE(resulting_filter->GetTransport());
372 EXPECT_EQ(BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL,
373 resulting_filter->GetTransport());
374 EXPECT_EQ(-85, resulting_rssi);
375 EXPECT_EQ(4UL, resulting_uuids.size());
376 EXPECT_TRUE(resulting_uuids.find(device::BluetoothUUID("1000")) !=
377 resulting_uuids.end());
378 EXPECT_TRUE(resulting_uuids.find(device::BluetoothUUID("1001")) !=
379 resulting_uuids.end());
380 EXPECT_TRUE(resulting_uuids.find(device::BluetoothUUID("1003")) !=
381 resulting_uuids.end());
382 EXPECT_TRUE(resulting_uuids.find(device::BluetoothUUID("1020")) !=
383 resulting_uuids.end());
385 resulting_filter = adapter->GetMergedDiscoveryFilterMasked(df);
386 EXPECT_EQ(BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL,
387 resulting_filter->GetTransport());
389 adapter->CleanupSessions();
392 } // namespace device