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 CreateRfcommService(
78 const BluetoothUUID
& uuid
,
79 const ServiceOptions
& options
,
80 const CreateServiceCallback
& callback
,
81 const CreateServiceErrorCallback
& error_callback
) OVERRIDE
{
84 virtual void CreateL2capService(
85 const BluetoothUUID
& uuid
,
86 const ServiceOptions
& options
,
87 const CreateServiceCallback
& callback
,
88 const CreateServiceErrorCallback
& error_callback
) OVERRIDE
{
92 virtual ~TestBluetoothAdapter() {}
94 virtual void AddDiscoverySession(
95 const base::Closure
& callback
,
96 const ErrorCallback
& error_callback
) OVERRIDE
{
99 virtual void RemoveDiscoverySession(
100 const base::Closure
& callback
,
101 const ErrorCallback
& error_callback
) OVERRIDE
{
104 virtual void RemovePairingDelegateInternal(
105 BluetoothDevice::PairingDelegate
* pairing_delegate
) OVERRIDE
{
109 class TestPairingDelegate
: public BluetoothDevice::PairingDelegate
{
111 virtual void RequestPinCode(BluetoothDevice
* device
) OVERRIDE
{}
112 virtual void RequestPasskey(BluetoothDevice
* device
) OVERRIDE
{}
113 virtual void DisplayPinCode(BluetoothDevice
* device
,
114 const std::string
& pincode
) OVERRIDE
{}
115 virtual void DisplayPasskey(BluetoothDevice
* device
,
116 uint32 passkey
) OVERRIDE
{}
117 virtual void KeysEntered(BluetoothDevice
* device
,
118 uint32 entered
) OVERRIDE
{}
119 virtual void ConfirmPasskey(BluetoothDevice
* device
,
120 uint32 passkey
) OVERRIDE
{}
121 virtual void AuthorizePairing(BluetoothDevice
* device
) OVERRIDE
{}
125 TEST(BluetoothAdapterTest
, NoDefaultPairingDelegate
) {
126 scoped_refptr
<BluetoothAdapter
> adapter
= new TestBluetoothAdapter();
128 // Verify that when there is no registered pairing delegate, NULL is returned.
129 EXPECT_TRUE(adapter
->DefaultPairingDelegate() == NULL
);
132 TEST(BluetoothAdapterTest
, OneDefaultPairingDelegate
) {
133 scoped_refptr
<BluetoothAdapter
> adapter
= new TestBluetoothAdapter();
135 // Verify that when there is one registered pairing delegate, it is returned.
136 TestPairingDelegate delegate
;
138 adapter
->AddPairingDelegate(&delegate
,
139 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW
);
141 EXPECT_EQ(&delegate
, adapter
->DefaultPairingDelegate());
144 TEST(BluetoothAdapterTest
, SamePriorityDelegates
) {
145 scoped_refptr
<BluetoothAdapter
> adapter
= new TestBluetoothAdapter();
147 // Verify that when there are two registered pairing delegates of the same
148 // priority, the first one registered is returned.
149 TestPairingDelegate delegate1
, delegate2
;
151 adapter
->AddPairingDelegate(&delegate1
,
152 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW
);
153 adapter
->AddPairingDelegate(&delegate2
,
154 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW
);
156 EXPECT_EQ(&delegate1
, adapter
->DefaultPairingDelegate());
158 // After unregistering the first, the second can be returned.
159 adapter
->RemovePairingDelegate(&delegate1
);
161 EXPECT_EQ(&delegate2
, adapter
->DefaultPairingDelegate());
164 TEST(BluetoothAdapterTest
, HighestPriorityDelegate
) {
165 scoped_refptr
<BluetoothAdapter
> adapter
= new TestBluetoothAdapter();
167 // Verify that when there are two registered pairing delegates, the one with
168 // the highest priority is returned.
169 TestPairingDelegate delegate1
, delegate2
;
171 adapter
->AddPairingDelegate(&delegate1
,
172 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW
);
173 adapter
->AddPairingDelegate(&delegate2
,
174 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
176 EXPECT_EQ(&delegate2
, adapter
->DefaultPairingDelegate());
179 TEST(BluetoothAdapterTest
, UnregisterDelegate
) {
180 scoped_refptr
<BluetoothAdapter
> adapter
= new TestBluetoothAdapter();
182 // Verify that after unregistering a delegate, NULL is returned.
183 TestPairingDelegate delegate
;
185 adapter
->AddPairingDelegate(&delegate
,
186 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW
);
187 adapter
->RemovePairingDelegate(&delegate
);
189 EXPECT_TRUE(adapter
->DefaultPairingDelegate() == NULL
);
192 } // namespace device