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
{
83 virtual ~TestBluetoothAdapter() {}
85 virtual void AddDiscoverySession(
86 const base::Closure
& callback
,
87 const ErrorCallback
& error_callback
) OVERRIDE
{
90 virtual void RemoveDiscoverySession(
91 const base::Closure
& callback
,
92 const ErrorCallback
& error_callback
) OVERRIDE
{
95 virtual void RemovePairingDelegateInternal(
96 BluetoothDevice::PairingDelegate
* pairing_delegate
) OVERRIDE
{
100 class TestPairingDelegate
: public BluetoothDevice::PairingDelegate
{
102 virtual void RequestPinCode(BluetoothDevice
* device
) OVERRIDE
{}
103 virtual void RequestPasskey(BluetoothDevice
* device
) OVERRIDE
{}
104 virtual void DisplayPinCode(BluetoothDevice
* device
,
105 const std::string
& pincode
) OVERRIDE
{}
106 virtual void DisplayPasskey(BluetoothDevice
* device
,
107 uint32 passkey
) OVERRIDE
{}
108 virtual void KeysEntered(BluetoothDevice
* device
,
109 uint32 entered
) OVERRIDE
{}
110 virtual void ConfirmPasskey(BluetoothDevice
* device
,
111 uint32 passkey
) OVERRIDE
{}
112 virtual void AuthorizePairing(BluetoothDevice
* device
) OVERRIDE
{}
116 TEST(BluetoothAdapterTest
, NoDefaultPairingDelegate
) {
117 scoped_refptr
<BluetoothAdapter
> adapter
= new TestBluetoothAdapter();
119 // Verify that when there is no registered pairing delegate, NULL is returned.
120 EXPECT_TRUE(adapter
->DefaultPairingDelegate() == NULL
);
123 TEST(BluetoothAdapterTest
, OneDefaultPairingDelegate
) {
124 scoped_refptr
<BluetoothAdapter
> adapter
= new TestBluetoothAdapter();
126 // Verify that when there is one registered pairing delegate, it is returned.
127 TestPairingDelegate delegate
;
129 adapter
->AddPairingDelegate(&delegate
,
130 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW
);
132 EXPECT_EQ(&delegate
, adapter
->DefaultPairingDelegate());
135 TEST(BluetoothAdapterTest
, SamePriorityDelegates
) {
136 scoped_refptr
<BluetoothAdapter
> adapter
= new TestBluetoothAdapter();
138 // Verify that when there are two registered pairing delegates of the same
139 // priority, the first one registered is returned.
140 TestPairingDelegate delegate1
, delegate2
;
142 adapter
->AddPairingDelegate(&delegate1
,
143 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW
);
144 adapter
->AddPairingDelegate(&delegate2
,
145 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW
);
147 EXPECT_EQ(&delegate1
, adapter
->DefaultPairingDelegate());
149 // After unregistering the first, the second can be returned.
150 adapter
->RemovePairingDelegate(&delegate1
);
152 EXPECT_EQ(&delegate2
, adapter
->DefaultPairingDelegate());
155 TEST(BluetoothAdapterTest
, HighestPriorityDelegate
) {
156 scoped_refptr
<BluetoothAdapter
> adapter
= new TestBluetoothAdapter();
158 // Verify that when there are two registered pairing delegates, the one with
159 // the highest priority is returned.
160 TestPairingDelegate delegate1
, delegate2
;
162 adapter
->AddPairingDelegate(&delegate1
,
163 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW
);
164 adapter
->AddPairingDelegate(&delegate2
,
165 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
167 EXPECT_EQ(&delegate2
, adapter
->DefaultPairingDelegate());
170 TEST(BluetoothAdapterTest
, UnregisterDelegate
) {
171 scoped_refptr
<BluetoothAdapter
> adapter
= new TestBluetoothAdapter();
173 // Verify that after unregistering a delegate, NULL is returned.
174 TestPairingDelegate delegate
;
176 adapter
->AddPairingDelegate(&delegate
,
177 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW
);
178 adapter
->RemovePairingDelegate(&delegate
);
180 EXPECT_TRUE(adapter
->DefaultPairingDelegate() == NULL
);
183 } // namespace device