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 virtual void AddObserver(BluetoothAdapter::Observer
* observer
) OVERRIDE
{
23 virtual void RemoveObserver(BluetoothAdapter::Observer
* observer
) OVERRIDE
{
27 virtual std::string
GetAddress() const OVERRIDE
{
31 virtual std::string
GetName() const OVERRIDE
{
35 virtual void SetName(const std::string
& name
,
36 const base::Closure
& callback
,
37 const ErrorCallback
& error_callback
) OVERRIDE
{
40 virtual bool IsInitialized() const OVERRIDE
{
44 virtual bool IsPresent() const OVERRIDE
{
48 virtual bool IsPowered() const OVERRIDE
{
52 virtual void SetPowered(
54 const base::Closure
& callback
,
55 const ErrorCallback
& error_callback
) OVERRIDE
{
58 virtual bool IsDiscoverable() const OVERRIDE
{
62 virtual void SetDiscoverable(
64 const base::Closure
& callback
,
65 const ErrorCallback
& error_callback
) OVERRIDE
{
68 virtual bool IsDiscovering() const OVERRIDE
{
72 virtual void StartDiscoverySession(
73 const DiscoverySessionCallback
& callback
,
74 const ErrorCallback
& error_callback
) OVERRIDE
{
77 virtual void ReadLocalOutOfBandPairingData(
78 const BluetoothAdapter::BluetoothOutOfBandPairingDataCallback
& callback
,
79 const ErrorCallback
& error_callback
) OVERRIDE
{
82 virtual void CreateRfcommService(
83 const BluetoothUUID
& uuid
,
86 const CreateServiceCallback
& callback
,
87 const CreateServiceErrorCallback
& error_callback
) OVERRIDE
{
90 virtual void CreateL2capService(
91 const BluetoothUUID
& uuid
,
93 const CreateServiceCallback
& callback
,
94 const CreateServiceErrorCallback
& error_callback
) OVERRIDE
{
98 virtual ~TestBluetoothAdapter() {}
100 virtual void AddDiscoverySession(
101 const base::Closure
& callback
,
102 const ErrorCallback
& error_callback
) OVERRIDE
{
105 virtual void RemoveDiscoverySession(
106 const base::Closure
& callback
,
107 const ErrorCallback
& error_callback
) OVERRIDE
{
110 virtual void RemovePairingDelegateInternal(
111 BluetoothDevice::PairingDelegate
* pairing_delegate
) OVERRIDE
{
115 class TestPairingDelegate
: public BluetoothDevice::PairingDelegate
{
117 virtual void RequestPinCode(BluetoothDevice
* device
) OVERRIDE
{}
118 virtual void RequestPasskey(BluetoothDevice
* device
) OVERRIDE
{}
119 virtual void DisplayPinCode(BluetoothDevice
* device
,
120 const std::string
& pincode
) OVERRIDE
{}
121 virtual void DisplayPasskey(BluetoothDevice
* device
,
122 uint32 passkey
) OVERRIDE
{}
123 virtual void KeysEntered(BluetoothDevice
* device
,
124 uint32 entered
) OVERRIDE
{}
125 virtual void ConfirmPasskey(BluetoothDevice
* device
,
126 uint32 passkey
) OVERRIDE
{}
127 virtual void AuthorizePairing(BluetoothDevice
* device
) OVERRIDE
{}
131 TEST(BluetoothAdapterTest
, NoDefaultPairingDelegate
) {
132 scoped_refptr
<BluetoothAdapter
> adapter
= new TestBluetoothAdapter();
134 // Verify that when there is no registered pairing delegate, NULL is returned.
135 EXPECT_TRUE(adapter
->DefaultPairingDelegate() == NULL
);
138 TEST(BluetoothAdapterTest
, OneDefaultPairingDelegate
) {
139 scoped_refptr
<BluetoothAdapter
> adapter
= new TestBluetoothAdapter();
141 // Verify that when there is one registered pairing delegate, it is returned.
142 TestPairingDelegate delegate
;
144 adapter
->AddPairingDelegate(&delegate
,
145 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW
);
147 EXPECT_EQ(&delegate
, adapter
->DefaultPairingDelegate());
150 TEST(BluetoothAdapterTest
, SamePriorityDelegates
) {
151 scoped_refptr
<BluetoothAdapter
> adapter
= new TestBluetoothAdapter();
153 // Verify that when there are two registered pairing delegates of the same
154 // priority, the first one registered is returned.
155 TestPairingDelegate delegate1
, delegate2
;
157 adapter
->AddPairingDelegate(&delegate1
,
158 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW
);
159 adapter
->AddPairingDelegate(&delegate2
,
160 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW
);
162 EXPECT_EQ(&delegate1
, adapter
->DefaultPairingDelegate());
164 // After unregistering the first, the second can be returned.
165 adapter
->RemovePairingDelegate(&delegate1
);
167 EXPECT_EQ(&delegate2
, adapter
->DefaultPairingDelegate());
170 TEST(BluetoothAdapterTest
, HighestPriorityDelegate
) {
171 scoped_refptr
<BluetoothAdapter
> adapter
= new TestBluetoothAdapter();
173 // Verify that when there are two registered pairing delegates, the one with
174 // the highest priority is returned.
175 TestPairingDelegate delegate1
, delegate2
;
177 adapter
->AddPairingDelegate(&delegate1
,
178 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW
);
179 adapter
->AddPairingDelegate(&delegate2
,
180 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
182 EXPECT_EQ(&delegate2
, adapter
->DefaultPairingDelegate());
185 TEST(BluetoothAdapterTest
, UnregisterDelegate
) {
186 scoped_refptr
<BluetoothAdapter
> adapter
= new TestBluetoothAdapter();
188 // Verify that after unregistering a delegate, NULL is returned.
189 TestPairingDelegate delegate
;
191 adapter
->AddPairingDelegate(&delegate
,
192 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW
);
193 adapter
->RemovePairingDelegate(&delegate
);
195 EXPECT_TRUE(adapter
->DefaultPairingDelegate() == NULL
);
198 } // namespace device