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.
4
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"
9
10using device::BluetoothAdapter;
11using device::BluetoothDevice;
12
13namespace device {
14
15class TestBluetoothAdapter : public BluetoothAdapter {
16 public:
17  TestBluetoothAdapter() {
18  }
19
20  virtual void AddObserver(BluetoothAdapter::Observer* observer) OVERRIDE {
21  }
22
23  virtual void RemoveObserver(BluetoothAdapter::Observer* observer) OVERRIDE {
24
25  }
26
27  virtual std::string GetAddress() const OVERRIDE {
28    return "";
29  }
30
31  virtual std::string GetName() const OVERRIDE {
32    return "";
33  }
34
35  virtual void SetName(const std::string& name,
36                       const base::Closure& callback,
37                       const ErrorCallback& error_callback) OVERRIDE {
38  }
39
40  virtual bool IsInitialized() const OVERRIDE {
41    return false;
42  }
43
44  virtual bool IsPresent() const OVERRIDE {
45    return false;
46  }
47
48  virtual bool IsPowered() const OVERRIDE {
49    return false;
50  }
51
52  virtual void SetPowered(
53      bool powered,
54      const base::Closure& callback,
55      const ErrorCallback& error_callback) OVERRIDE {
56  }
57
58  virtual bool IsDiscoverable() const OVERRIDE {
59    return false;
60  }
61
62  virtual void SetDiscoverable(
63      bool discoverable,
64      const base::Closure& callback,
65      const ErrorCallback& error_callback) OVERRIDE {
66  }
67
68  virtual bool IsDiscovering() const OVERRIDE {
69    return false;
70  }
71
72  virtual void StartDiscoverySession(
73      const DiscoverySessionCallback& callback,
74      const ErrorCallback& error_callback) OVERRIDE {
75  }
76
77  virtual void CreateRfcommService(
78      const BluetoothUUID& uuid,
79      const ServiceOptions& options,
80      const CreateServiceCallback& callback,
81      const CreateServiceErrorCallback& error_callback) OVERRIDE {
82  }
83
84  virtual void CreateL2capService(
85      const BluetoothUUID& uuid,
86      const ServiceOptions& options,
87      const CreateServiceCallback& callback,
88      const CreateServiceErrorCallback& error_callback) OVERRIDE {
89  }
90
91 protected:
92  virtual ~TestBluetoothAdapter() {}
93
94  virtual void AddDiscoverySession(
95      const base::Closure& callback,
96      const ErrorCallback& error_callback) OVERRIDE {
97  }
98
99  virtual void RemoveDiscoverySession(
100      const base::Closure& callback,
101      const ErrorCallback& error_callback) OVERRIDE {
102  }
103
104  virtual void RemovePairingDelegateInternal(
105      BluetoothDevice::PairingDelegate* pairing_delegate) OVERRIDE {
106  }
107};
108
109class TestPairingDelegate : public BluetoothDevice::PairingDelegate {
110  public:
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 {}
122};
123
124
125TEST(BluetoothAdapterTest, NoDefaultPairingDelegate) {
126  scoped_refptr<BluetoothAdapter> adapter = new TestBluetoothAdapter();
127
128  // Verify that when there is no registered pairing delegate, NULL is returned.
129  EXPECT_TRUE(adapter->DefaultPairingDelegate() == NULL);
130}
131
132TEST(BluetoothAdapterTest, OneDefaultPairingDelegate) {
133  scoped_refptr<BluetoothAdapter> adapter = new TestBluetoothAdapter();
134
135  // Verify that when there is one registered pairing delegate, it is returned.
136  TestPairingDelegate delegate;
137
138  adapter->AddPairingDelegate(&delegate,
139                              BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW);
140
141  EXPECT_EQ(&delegate, adapter->DefaultPairingDelegate());
142}
143
144TEST(BluetoothAdapterTest, SamePriorityDelegates) {
145  scoped_refptr<BluetoothAdapter> adapter = new TestBluetoothAdapter();
146
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;
150
151  adapter->AddPairingDelegate(&delegate1,
152                              BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW);
153  adapter->AddPairingDelegate(&delegate2,
154                              BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW);
155
156  EXPECT_EQ(&delegate1, adapter->DefaultPairingDelegate());
157
158  // After unregistering the first, the second can be returned.
159  adapter->RemovePairingDelegate(&delegate1);
160
161  EXPECT_EQ(&delegate2, adapter->DefaultPairingDelegate());
162}
163
164TEST(BluetoothAdapterTest, HighestPriorityDelegate) {
165  scoped_refptr<BluetoothAdapter> adapter = new TestBluetoothAdapter();
166
167  // Verify that when there are two registered pairing delegates, the one with
168  // the highest priority is returned.
169  TestPairingDelegate delegate1, delegate2;
170
171  adapter->AddPairingDelegate(&delegate1,
172                              BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW);
173  adapter->AddPairingDelegate(&delegate2,
174                              BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
175
176  EXPECT_EQ(&delegate2, adapter->DefaultPairingDelegate());
177}
178
179TEST(BluetoothAdapterTest, UnregisterDelegate) {
180  scoped_refptr<BluetoothAdapter> adapter = new TestBluetoothAdapter();
181
182  // Verify that after unregistering a delegate, NULL is returned.
183  TestPairingDelegate delegate;
184
185  adapter->AddPairingDelegate(&delegate,
186                              BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW);
187  adapter->RemovePairingDelegate(&delegate);
188
189  EXPECT_TRUE(adapter->DefaultPairingDelegate() == NULL);
190}
191
192}  // namespace device
193