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/memory/ref_counted.h"
6 #include "device/bluetooth/bluetooth_adapter.h"
7 #include "device/bluetooth/bluetooth_device.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 using device::BluetoothAdapter
;
11 using device::BluetoothDevice
;
15 class TestBluetoothAdapter
: public BluetoothAdapter
{
17 TestBluetoothAdapter() {
20 void AddObserver(BluetoothAdapter::Observer
* observer
) override
{}
22 void RemoveObserver(BluetoothAdapter::Observer
* observer
) override
{}
24 std::string
GetAddress() const override
{ return ""; }
26 std::string
GetName() const override
{ return ""; }
28 void SetName(const std::string
& name
,
29 const base::Closure
& callback
,
30 const ErrorCallback
& error_callback
) override
{}
32 bool IsInitialized() const override
{ return false; }
34 bool IsPresent() const override
{ return false; }
36 bool IsPowered() const override
{ return false; }
38 void SetPowered(bool powered
,
39 const base::Closure
& callback
,
40 const ErrorCallback
& error_callback
) override
{}
42 bool IsDiscoverable() const override
{ return false; }
44 void SetDiscoverable(bool discoverable
,
45 const base::Closure
& callback
,
46 const ErrorCallback
& error_callback
) override
{}
48 bool IsDiscovering() const override
{ return false; }
50 void DeleteOnCorrectThread() const override
{ delete this; }
52 void StartDiscoverySession(const DiscoverySessionCallback
& callback
,
53 const ErrorCallback
& error_callback
) override
{}
55 void CreateRfcommService(
56 const BluetoothUUID
& uuid
,
57 const ServiceOptions
& options
,
58 const CreateServiceCallback
& callback
,
59 const CreateServiceErrorCallback
& error_callback
) override
{}
61 void CreateL2capService(
62 const BluetoothUUID
& uuid
,
63 const ServiceOptions
& options
,
64 const CreateServiceCallback
& callback
,
65 const CreateServiceErrorCallback
& error_callback
) override
{}
67 void RegisterAudioSink(
68 const BluetoothAudioSink::Options
& options
,
69 const AcquiredCallback
& callback
,
70 const BluetoothAudioSink::ErrorCallback
& error_callback
) override
{}
73 ~TestBluetoothAdapter() override
{}
75 void AddDiscoverySession(const base::Closure
& callback
,
76 const ErrorCallback
& error_callback
) override
{}
78 void RemoveDiscoverySession(const base::Closure
& callback
,
79 const ErrorCallback
& error_callback
) override
{}
81 void RemovePairingDelegateInternal(
82 BluetoothDevice::PairingDelegate
* pairing_delegate
) override
{}
85 class TestPairingDelegate
: public BluetoothDevice::PairingDelegate
{
87 void RequestPinCode(BluetoothDevice
* device
) override
{}
88 void RequestPasskey(BluetoothDevice
* device
) override
{}
89 void DisplayPinCode(BluetoothDevice
* device
,
90 const std::string
& pincode
) override
{}
91 void DisplayPasskey(BluetoothDevice
* device
, uint32 passkey
) override
{}
92 void KeysEntered(BluetoothDevice
* device
, uint32 entered
) override
{}
93 void ConfirmPasskey(BluetoothDevice
* device
, uint32 passkey
) override
{}
94 void AuthorizePairing(BluetoothDevice
* device
) override
{}
98 TEST(BluetoothAdapterTest
, NoDefaultPairingDelegate
) {
99 scoped_refptr
<BluetoothAdapter
> adapter
= new TestBluetoothAdapter();
101 // Verify that when there is no registered pairing delegate, NULL is returned.
102 EXPECT_TRUE(adapter
->DefaultPairingDelegate() == NULL
);
105 TEST(BluetoothAdapterTest
, OneDefaultPairingDelegate
) {
106 scoped_refptr
<BluetoothAdapter
> adapter
= new TestBluetoothAdapter();
108 // Verify that when there is one registered pairing delegate, it is returned.
109 TestPairingDelegate delegate
;
111 adapter
->AddPairingDelegate(&delegate
,
112 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW
);
114 EXPECT_EQ(&delegate
, adapter
->DefaultPairingDelegate());
117 TEST(BluetoothAdapterTest
, SamePriorityDelegates
) {
118 scoped_refptr
<BluetoothAdapter
> adapter
= new TestBluetoothAdapter();
120 // Verify that when there are two registered pairing delegates of the same
121 // priority, the first one registered is returned.
122 TestPairingDelegate delegate1
, delegate2
;
124 adapter
->AddPairingDelegate(&delegate1
,
125 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW
);
126 adapter
->AddPairingDelegate(&delegate2
,
127 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW
);
129 EXPECT_EQ(&delegate1
, adapter
->DefaultPairingDelegate());
131 // After unregistering the first, the second can be returned.
132 adapter
->RemovePairingDelegate(&delegate1
);
134 EXPECT_EQ(&delegate2
, adapter
->DefaultPairingDelegate());
137 TEST(BluetoothAdapterTest
, HighestPriorityDelegate
) {
138 scoped_refptr
<BluetoothAdapter
> adapter
= new TestBluetoothAdapter();
140 // Verify that when there are two registered pairing delegates, the one with
141 // the highest priority is returned.
142 TestPairingDelegate delegate1
, delegate2
;
144 adapter
->AddPairingDelegate(&delegate1
,
145 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW
);
146 adapter
->AddPairingDelegate(&delegate2
,
147 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
149 EXPECT_EQ(&delegate2
, adapter
->DefaultPairingDelegate());
152 TEST(BluetoothAdapterTest
, UnregisterDelegate
) {
153 scoped_refptr
<BluetoothAdapter
> adapter
= new TestBluetoothAdapter();
155 // Verify that after unregistering a delegate, NULL is returned.
156 TestPairingDelegate delegate
;
158 adapter
->AddPairingDelegate(&delegate
,
159 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW
);
160 adapter
->RemovePairingDelegate(&delegate
);
162 EXPECT_TRUE(adapter
->DefaultPairingDelegate() == NULL
);
165 } // namespace device