OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/proximity_auth/bluetooth_connection_finder.h" | 5 #include "components/proximity_auth/bluetooth_connection_finder.h" |
6 | 6 |
| 7 #include <memory> |
7 #include <utility> | 8 #include <utility> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" |
11 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
14 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
15 #include "base/time/time.h" | 16 #include "base/time/time.h" |
16 #include "components/proximity_auth/proximity_auth_test_util.h" | 17 #include "components/proximity_auth/proximity_auth_test_util.h" |
17 #include "components/proximity_auth/remote_device.h" | 18 #include "components/proximity_auth/remote_device.h" |
18 #include "components/proximity_auth/wire_message.h" | 19 #include "components/proximity_auth/wire_message.h" |
19 #include "device/bluetooth/bluetooth_adapter_factory.h" | 20 #include "device/bluetooth/bluetooth_adapter_factory.h" |
20 #include "device/bluetooth/bluetooth_uuid.h" | 21 #include "device/bluetooth/bluetooth_uuid.h" |
21 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 22 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
22 #include "device/bluetooth/test/mock_bluetooth_device.h" | 23 #include "device/bluetooth/test/mock_bluetooth_device.h" |
(...skipping 22 matching lines...) Expand all Loading... |
45 void SetStatus(Connection::Status status) { | 46 void SetStatus(Connection::Status status) { |
46 // This object should not be destroyed after setting the status and calling | 47 // This object should not be destroyed after setting the status and calling |
47 // observers. | 48 // observers. |
48 do_not_destroy_ = true; | 49 do_not_destroy_ = true; |
49 Connection::SetStatus(status); | 50 Connection::SetStatus(status); |
50 do_not_destroy_ = false; | 51 do_not_destroy_ = false; |
51 } | 52 } |
52 | 53 |
53 private: | 54 private: |
54 void Disconnect() override {} | 55 void Disconnect() override {} |
55 void SendMessageImpl(scoped_ptr<WireMessage> message) override {} | 56 void SendMessageImpl(std::unique_ptr<WireMessage> message) override {} |
56 | 57 |
57 // If true, we do not expect |this| object to be destroyed until this value is | 58 // If true, we do not expect |this| object to be destroyed until this value is |
58 // toggled back to false. | 59 // toggled back to false. |
59 bool do_not_destroy_; | 60 bool do_not_destroy_; |
60 | 61 |
61 DISALLOW_COPY_AND_ASSIGN(MockConnection); | 62 DISALLOW_COPY_AND_ASSIGN(MockConnection); |
62 }; | 63 }; |
63 | 64 |
64 class MockBluetoothConnectionFinder : public BluetoothConnectionFinder { | 65 class MockBluetoothConnectionFinder : public BluetoothConnectionFinder { |
65 public: | 66 public: |
66 MockBluetoothConnectionFinder() | 67 MockBluetoothConnectionFinder() |
67 : BluetoothConnectionFinder(CreateClassicRemoteDeviceForTest(), | 68 : BluetoothConnectionFinder(CreateClassicRemoteDeviceForTest(), |
68 device::BluetoothUUID(kUuid), | 69 device::BluetoothUUID(kUuid), |
69 base::TimeDelta()) {} | 70 base::TimeDelta()) {} |
70 ~MockBluetoothConnectionFinder() override {} | 71 ~MockBluetoothConnectionFinder() override {} |
71 | 72 |
72 MOCK_METHOD0(CreateConnectionProxy, Connection*()); | 73 MOCK_METHOD0(CreateConnectionProxy, Connection*()); |
73 | 74 |
74 // Creates a mock connection and sets an expectation that the mock connection | 75 // Creates a mock connection and sets an expectation that the mock connection |
75 // finder's CreateConnection() method will be called and will return the | 76 // finder's CreateConnection() method will be called and will return the |
76 // created connection. Returns a reference to the created connection. | 77 // created connection. Returns a reference to the created connection. |
77 // NOTE: The returned connection's lifetime is managed by the connection | 78 // NOTE: The returned connection's lifetime is managed by the connection |
78 // finder. | 79 // finder. |
79 MockConnection* ExpectCreateConnection() { | 80 MockConnection* ExpectCreateConnection() { |
80 scoped_ptr<MockConnection> connection(new NiceMock<MockConnection>()); | 81 std::unique_ptr<MockConnection> connection(new NiceMock<MockConnection>()); |
81 MockConnection* connection_alias = connection.get(); | 82 MockConnection* connection_alias = connection.get(); |
82 EXPECT_CALL(*this, CreateConnectionProxy()) | 83 EXPECT_CALL(*this, CreateConnectionProxy()) |
83 .WillOnce(Return(connection.release())); | 84 .WillOnce(Return(connection.release())); |
84 return connection_alias; | 85 return connection_alias; |
85 } | 86 } |
86 | 87 |
87 using BluetoothConnectionFinder::AdapterPresentChanged; | 88 using BluetoothConnectionFinder::AdapterPresentChanged; |
88 using BluetoothConnectionFinder::AdapterPoweredChanged; | 89 using BluetoothConnectionFinder::AdapterPoweredChanged; |
89 | 90 |
90 void ClearSeekCallbacks() { | 91 void ClearSeekCallbacks() { |
91 seek_callback_ = base::Closure(); | 92 seek_callback_ = base::Closure(); |
92 seek_error_callback_ = bluetooth_util::ErrorCallback(); | 93 seek_error_callback_ = bluetooth_util::ErrorCallback(); |
93 } | 94 } |
94 | 95 |
95 const base::Closure& seek_callback() { return seek_callback_; } | 96 const base::Closure& seek_callback() { return seek_callback_; } |
96 const bluetooth_util::ErrorCallback& seek_error_callback() { | 97 const bluetooth_util::ErrorCallback& seek_error_callback() { |
97 return seek_error_callback_; | 98 return seek_error_callback_; |
98 } | 99 } |
99 | 100 |
100 protected: | 101 protected: |
101 // BluetoothConnectionFinder: | 102 // BluetoothConnectionFinder: |
102 scoped_ptr<Connection> CreateConnection() override { | 103 std::unique_ptr<Connection> CreateConnection() override { |
103 return make_scoped_ptr(CreateConnectionProxy()); | 104 return base::WrapUnique(CreateConnectionProxy()); |
104 } | 105 } |
105 | 106 |
106 void SeekDeviceByAddress( | 107 void SeekDeviceByAddress( |
107 const std::string& bluetooth_address, | 108 const std::string& bluetooth_address, |
108 const base::Closure& callback, | 109 const base::Closure& callback, |
109 const bluetooth_util::ErrorCallback& error_callback) override { | 110 const bluetooth_util::ErrorCallback& error_callback) override { |
110 EXPECT_EQ(kTestRemoteDeviceBluetoothAddress, bluetooth_address); | 111 EXPECT_EQ(kTestRemoteDeviceBluetoothAddress, bluetooth_address); |
111 seek_callback_ = callback; | 112 seek_callback_ = callback; |
112 seek_error_callback_ = error_callback; | 113 seek_error_callback_ = error_callback; |
113 } | 114 } |
(...skipping 28 matching lines...) Expand all Loading... |
142 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); | 143 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); |
143 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); | 144 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); |
144 | 145 |
145 // By default, the remote device is known to |adapter_| so | 146 // By default, the remote device is known to |adapter_| so |
146 // |SeekDeviceByAddress()| will not be called. | 147 // |SeekDeviceByAddress()| will not be called. |
147 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress)) | 148 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress)) |
148 .WillByDefault(Return(bluetooth_device_.get())); | 149 .WillByDefault(Return(bluetooth_device_.get())); |
149 } | 150 } |
150 | 151 |
151 MOCK_METHOD1(OnConnectionFoundProxy, void(Connection* connection)); | 152 MOCK_METHOD1(OnConnectionFoundProxy, void(Connection* connection)); |
152 void OnConnectionFound(scoped_ptr<Connection> connection) { | 153 void OnConnectionFound(std::unique_ptr<Connection> connection) { |
153 OnConnectionFoundProxy(connection.get()); | 154 OnConnectionFoundProxy(connection.get()); |
154 last_found_connection_ = std::move(connection); | 155 last_found_connection_ = std::move(connection); |
155 } | 156 } |
156 | 157 |
157 // Starts |connection_finder_|. If |expect_connection| is true, then we set an | 158 // Starts |connection_finder_|. If |expect_connection| is true, then we set an |
158 // expectation that an in-progress connection will be created and returned. | 159 // expectation that an in-progress connection will be created and returned. |
159 MockConnection* StartConnectionFinder(bool expect_connection) { | 160 MockConnection* StartConnectionFinder(bool expect_connection) { |
160 MockConnection* connection = nullptr; | 161 MockConnection* connection = nullptr; |
161 if (expect_connection) | 162 if (expect_connection) |
162 connection = connection_finder_.ExpectCreateConnection(); | 163 connection = connection_finder_.ExpectCreateConnection(); |
163 connection_finder_.Find(connection_callback_); | 164 connection_finder_.Find(connection_callback_); |
164 return connection; | 165 return connection; |
165 } | 166 } |
166 | 167 |
167 // Given an in-progress |connection| returned by |StartConnectionFinder()|, | 168 // Given an in-progress |connection| returned by |StartConnectionFinder()|, |
168 // simulate it transitioning to the CONNECTED state. | 169 // simulate it transitioning to the CONNECTED state. |
169 void SimulateDeviceConnection(MockConnection* connection) { | 170 void SimulateDeviceConnection(MockConnection* connection) { |
170 connection->SetStatus(Connection::IN_PROGRESS); | 171 connection->SetStatus(Connection::IN_PROGRESS); |
171 base::RunLoop run_loop; | 172 base::RunLoop run_loop; |
172 EXPECT_CALL(*this, OnConnectionFoundProxy(_)); | 173 EXPECT_CALL(*this, OnConnectionFoundProxy(_)); |
173 connection->SetStatus(Connection::CONNECTED); | 174 connection->SetStatus(Connection::CONNECTED); |
174 run_loop.RunUntilIdle(); | 175 run_loop.RunUntilIdle(); |
175 } | 176 } |
176 | 177 |
177 scoped_refptr<device::MockBluetoothAdapter> adapter_; | 178 scoped_refptr<device::MockBluetoothAdapter> adapter_; |
178 StrictMock<MockBluetoothConnectionFinder> connection_finder_; | 179 StrictMock<MockBluetoothConnectionFinder> connection_finder_; |
179 scoped_ptr<device::MockBluetoothDevice> bluetooth_device_; | 180 std::unique_ptr<device::MockBluetoothDevice> bluetooth_device_; |
180 ConnectionFinder::ConnectionCallback connection_callback_; | 181 ConnectionFinder::ConnectionCallback connection_callback_; |
181 | 182 |
182 private: | 183 private: |
183 // Save a pointer to the last found connection, to extend its lifetime. | 184 // Save a pointer to the last found connection, to extend its lifetime. |
184 scoped_ptr<Connection> last_found_connection_; | 185 std::unique_ptr<Connection> last_found_connection_; |
185 | 186 |
186 base::MessageLoop message_loop_; | 187 base::MessageLoop message_loop_; |
187 }; | 188 }; |
188 | 189 |
189 TEST_F(ProximityAuthBluetoothConnectionFinderTest, | 190 TEST_F(ProximityAuthBluetoothConnectionFinderTest, |
190 ConstructAndDestroyDoesntCrash) { | 191 ConstructAndDestroyDoesntCrash) { |
191 // Destroying a BluetoothConnectionFinder for which Find() has not been called | 192 // Destroying a BluetoothConnectionFinder for which Find() has not been called |
192 // should not crash. | 193 // should not crash. |
193 BluetoothConnectionFinder connection_finder( | 194 BluetoothConnectionFinder connection_finder( |
194 CreateClassicRemoteDeviceForTest(), device::BluetoothUUID(kUuid), | 195 CreateClassicRemoteDeviceForTest(), device::BluetoothUUID(kUuid), |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 | 358 |
358 // Successfully connect to the Bluetooth device. | 359 // Successfully connect to the Bluetooth device. |
359 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress)) | 360 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress)) |
360 .WillByDefault(Return(bluetooth_device_.get())); | 361 .WillByDefault(Return(bluetooth_device_.get())); |
361 MockConnection* connection = connection_finder_.ExpectCreateConnection(); | 362 MockConnection* connection = connection_finder_.ExpectCreateConnection(); |
362 connection_finder_.seek_callback().Run(); | 363 connection_finder_.seek_callback().Run(); |
363 SimulateDeviceConnection(connection); | 364 SimulateDeviceConnection(connection); |
364 } | 365 } |
365 | 366 |
366 } // namespace proximity_auth | 367 } // namespace proximity_auth |
OLD | NEW |