Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 "components/proximity_auth/bluetooth_connection_finder.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/memory/ref_counted.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 #include "base/run_loop.h" | |
| 12 #include "base/time/time.h" | |
| 13 #include "components/proximity_auth/remote_device.h" | |
| 14 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
| 15 #include "device/bluetooth/bluetooth_uuid.h" | |
| 16 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | |
| 17 #include "testing/gmock/include/gmock/gmock.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 using testing::_; | |
| 21 using testing::NiceMock; | |
| 22 using testing::Return; | |
| 23 using testing::StrictMock; | |
| 24 | |
| 25 namespace proximity_auth { | |
| 26 namespace { | |
| 27 | |
| 28 const char kDeviceName[] = "Device name"; | |
| 29 const char kBluetoothAddress[] = "11:22:33:44:55:66"; | |
| 30 const RemoteDevice kRemoteDevice = {kDeviceName, kBluetoothAddress}; | |
| 31 | |
| 32 const char kUuid[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEF"; | |
| 33 | |
| 34 class MockConnection : public Connection { | |
| 35 public: | |
| 36 MockConnection() : Connection(kRemoteDevice) {} | |
| 37 virtual ~MockConnection() {} | |
| 38 | |
| 39 MOCK_METHOD0(Connect, void()); | |
| 40 | |
| 41 using Connection::SetStatus; | |
| 42 | |
| 43 private: | |
| 44 void Disconnect() {} | |
| 45 void SendMessageImpl(scoped_ptr<WireMessage> message) {} | |
| 46 | |
| 47 DISALLOW_COPY_AND_ASSIGN(MockConnection); | |
| 48 }; | |
| 49 | |
| 50 class MockBluetoothConnectionFinder : public BluetoothConnectionFinder { | |
| 51 public: | |
| 52 MockBluetoothConnectionFinder() | |
| 53 : BluetoothConnectionFinder(kRemoteDevice, | |
| 54 device::BluetoothUUID(kUuid), | |
| 55 base::TimeDelta()) {} | |
| 56 virtual ~MockBluetoothConnectionFinder() {} | |
| 57 | |
| 58 MOCK_METHOD0(CreateConnectionProxy, Connection*()); | |
| 59 MOCK_METHOD1(OnConnectionFoundProxy, void(Connection* connection)); | |
| 60 | |
| 61 void OnConnectionFound(scoped_ptr<Connection> connection) { | |
|
Tim Song
2014/10/10 18:44:39
If you move this to the test fixture, you can have
Ilya Sherman
2014/10/11 00:31:34
Done.
| |
| 62 OnConnectionFoundProxy(connection.get()); | |
| 63 found_connection_ = connection.Pass(); | |
| 64 } | |
| 65 | |
| 66 // Creates a mock connection and sets an expectation that the mock connection | |
| 67 // finder's CreateConnection() method will be called and will return the | |
| 68 // created connection. Returns a reference to the created connection. | |
| 69 // NOTE: The returned connection's lifetime is managed by the connection | |
| 70 // finder. | |
| 71 MockConnection* ExpectCreateConnection() { | |
| 72 scoped_ptr<MockConnection> connection(new NiceMock<MockConnection>()); | |
| 73 MockConnection* connection_alias = connection.get(); | |
| 74 EXPECT_CALL(*this, CreateConnectionProxy()) | |
| 75 .WillOnce(Return(connection.release())); | |
| 76 return connection_alias; | |
| 77 } | |
| 78 | |
| 79 using BluetoothConnectionFinder::AdapterPresentChanged; | |
| 80 using BluetoothConnectionFinder::AdapterPoweredChanged; | |
| 81 using BluetoothConnectionFinder::OnScreenLockStateChanged; | |
| 82 | |
| 83 protected: | |
| 84 virtual scoped_ptr<Connection> CreateConnection() override { | |
| 85 return make_scoped_ptr(CreateConnectionProxy()); | |
| 86 } | |
| 87 | |
| 88 private: | |
| 89 // Save a pointer to the last found connection, to extend its lifetime. | |
| 90 scoped_ptr<Connection> found_connection_; | |
| 91 | |
| 92 DISALLOW_COPY_AND_ASSIGN(MockBluetoothConnectionFinder); | |
| 93 }; | |
| 94 | |
| 95 } // namespace | |
| 96 | |
| 97 class ProximityAuthBluetoothConnectionFinderTest : public testing::Test { | |
| 98 protected: | |
| 99 ProximityAuthBluetoothConnectionFinderTest() | |
| 100 : adapter_(new NiceMock<device::MockBluetoothAdapter>) { | |
| 101 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); | |
| 102 | |
| 103 // By default, configure the environment to allow polling. Individual tests | |
| 104 // can override this as needed. | |
| 105 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); | |
| 106 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); | |
| 107 // TODO(isherman): Also set a default screen locked status. | |
| 108 } | |
| 109 | |
| 110 scoped_refptr<device::MockBluetoothAdapter> adapter_; | |
| 111 | |
| 112 private: | |
| 113 base::MessageLoop message_loop_; | |
| 114 }; | |
| 115 | |
| 116 TEST_F(ProximityAuthBluetoothConnectionFinderTest, | |
| 117 ConstructAndDestroyDoesntCrash) { | |
| 118 // Destroying a BluetoothConnectionFinder for which Find() has not been called | |
| 119 // should not crash. | |
| 120 BluetoothConnectionFinder connection_finder( | |
| 121 kRemoteDevice, | |
| 122 device::BluetoothUUID(kUuid), | |
| 123 base::TimeDelta::FromMilliseconds(1)); | |
| 124 } | |
| 125 | |
| 126 TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_NoBluetoothAdapter) { | |
| 127 // Some platforms do not support Bluetooth. This test is only meaningful on | |
| 128 // those platforms. | |
| 129 adapter_ = NULL; | |
| 130 if (device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) | |
| 131 return; | |
| 132 | |
| 133 // The StrictMock will verify that no connection is created. | |
| 134 StrictMock<MockBluetoothConnectionFinder> connection_finder; | |
| 135 connection_finder.Find( | |
| 136 base::Bind(&MockBluetoothConnectionFinder::OnConnectionFound, | |
| 137 base::Unretained(&connection_finder))); | |
| 138 } | |
| 139 | |
| 140 TEST_F(ProximityAuthBluetoothConnectionFinderTest, | |
| 141 Find_BluetoothAdapterNotPresent) { | |
| 142 // The StrictMock will verify that no connection is created. | |
| 143 StrictMock<MockBluetoothConnectionFinder> connection_finder; | |
| 144 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(false)); | |
| 145 connection_finder.Find( | |
| 146 base::Bind(&MockBluetoothConnectionFinder::OnConnectionFound, | |
| 147 base::Unretained(&connection_finder))); | |
| 148 } | |
| 149 | |
| 150 TEST_F(ProximityAuthBluetoothConnectionFinderTest, | |
| 151 Find_BluetoothAdapterNotPowered) { | |
| 152 // The StrictMock will verify that no connection is created. | |
| 153 StrictMock<MockBluetoothConnectionFinder> connection_finder; | |
| 154 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(false)); | |
| 155 connection_finder.Find( | |
| 156 base::Bind(&MockBluetoothConnectionFinder::OnConnectionFound, | |
| 157 base::Unretained(&connection_finder))); | |
| 158 } | |
| 159 | |
| 160 TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_ConnectionSucceeds) { | |
| 161 StrictMock<MockBluetoothConnectionFinder> connection_finder; | |
| 162 | |
| 163 MockConnection* connection = connection_finder.ExpectCreateConnection(); | |
| 164 connection_finder.Find( | |
| 165 base::Bind(&MockBluetoothConnectionFinder::OnConnectionFound, | |
| 166 base::Unretained(&connection_finder))); | |
| 167 | |
| 168 connection->SetStatus(Connection::IN_PROGRESS); | |
| 169 | |
| 170 EXPECT_CALL(connection_finder, OnConnectionFoundProxy(_)); | |
| 171 connection->SetStatus(Connection::CONNECTED); | |
| 172 } | |
| 173 | |
| 174 TEST_F(ProximityAuthBluetoothConnectionFinderTest, | |
| 175 Find_ConnectionSucceeds_UnregistersAsObserver) { | |
| 176 StrictMock<MockBluetoothConnectionFinder> connection_finder; | |
| 177 | |
| 178 MockConnection* connection = connection_finder.ExpectCreateConnection(); | |
| 179 connection_finder.Find( | |
| 180 base::Bind(&MockBluetoothConnectionFinder::OnConnectionFound, | |
| 181 base::Unretained(&connection_finder))); | |
| 182 | |
| 183 connection->SetStatus(Connection::IN_PROGRESS); | |
| 184 | |
| 185 EXPECT_CALL(connection_finder, OnConnectionFoundProxy(_)); | |
| 186 EXPECT_CALL(*adapter_, RemoveObserver(&connection_finder)); | |
| 187 connection->SetStatus(Connection::CONNECTED); | |
| 188 | |
| 189 // If for some reason the connection sends more status updates, they should be | |
| 190 // ignored. | |
| 191 EXPECT_CALL(connection_finder, OnConnectionFoundProxy(_)).Times(0); | |
| 192 connection->SetStatus(Connection::IN_PROGRESS); | |
| 193 connection->SetStatus(Connection::CONNECTED); | |
| 194 } | |
| 195 | |
| 196 TEST_F(ProximityAuthBluetoothConnectionFinderTest, | |
| 197 Find_ConnectionFails_PostsTaskToPollAgain) { | |
| 198 StrictMock<MockBluetoothConnectionFinder> connection_finder; | |
| 199 | |
| 200 MockConnection* connection = connection_finder.ExpectCreateConnection(); | |
| 201 connection_finder.Find( | |
| 202 base::Bind(&MockBluetoothConnectionFinder::OnConnectionFound, | |
| 203 base::Unretained(&connection_finder))); | |
| 204 | |
| 205 // Simulate a connection that fails to connect. | |
| 206 connection->SetStatus(Connection::IN_PROGRESS); | |
| 207 connection->SetStatus(Connection::DISCONNECTED); | |
| 208 | |
| 209 // A task should have been posted to poll again. | |
| 210 base::RunLoop run_loop; | |
| 211 connection_finder.ExpectCreateConnection(); | |
| 212 run_loop.RunUntilIdle(); | |
| 213 } | |
| 214 | |
| 215 TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_PollsOnAdapterPresent) { | |
| 216 StrictMock<MockBluetoothConnectionFinder> connection_finder; | |
| 217 | |
| 218 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(false)); | |
| 219 EXPECT_CALL(connection_finder, CreateConnectionProxy()).Times(0); | |
| 220 connection_finder.Find( | |
| 221 base::Bind(&MockBluetoothConnectionFinder::OnConnectionFound, | |
| 222 base::Unretained(&connection_finder))); | |
| 223 | |
| 224 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); | |
| 225 connection_finder.ExpectCreateConnection(); | |
| 226 connection_finder.AdapterPresentChanged(adapter_.get(), true); | |
| 227 } | |
| 228 | |
| 229 TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_PollsOnAdapterPowered) { | |
| 230 StrictMock<MockBluetoothConnectionFinder> connection_finder; | |
| 231 | |
| 232 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(false)); | |
| 233 EXPECT_CALL(connection_finder, CreateConnectionProxy()).Times(0); | |
| 234 connection_finder.Find( | |
| 235 base::Bind(&MockBluetoothConnectionFinder::OnConnectionFound, | |
| 236 base::Unretained(&connection_finder))); | |
| 237 | |
| 238 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); | |
| 239 connection_finder.ExpectCreateConnection(); | |
| 240 connection_finder.AdapterPoweredChanged(adapter_.get(), true); | |
| 241 } | |
| 242 | |
| 243 TEST_F(ProximityAuthBluetoothConnectionFinderTest, | |
| 244 Find_DoesNotPollIfConnectionPending) { | |
| 245 StrictMock<MockBluetoothConnectionFinder> connection_finder; | |
| 246 | |
| 247 MockConnection* connection = connection_finder.ExpectCreateConnection(); | |
| 248 connection_finder.Find( | |
| 249 base::Bind(&MockBluetoothConnectionFinder::OnConnectionFound, | |
| 250 base::Unretained(&connection_finder))); | |
| 251 | |
| 252 connection->SetStatus(Connection::IN_PROGRESS); | |
| 253 | |
| 254 // At this point, there is a pending connection in progress. Hence, an event | |
| 255 // that would normally trigger a new polling iteration should not do so now, | |
| 256 // because the delay interval between successive polling attempts has not yet | |
| 257 // expired. | |
| 258 EXPECT_CALL(connection_finder, CreateConnectionProxy()).Times(0); | |
| 259 connection_finder.AdapterPresentChanged(adapter_.get(), true); | |
| 260 } | |
| 261 | |
| 262 TEST_F(ProximityAuthBluetoothConnectionFinderTest, | |
| 263 Find_ConnectionFails_PostsTaskToPollAgain_PollWaitsForTask) { | |
| 264 StrictMock<MockBluetoothConnectionFinder> connection_finder; | |
| 265 | |
| 266 MockConnection* connection = connection_finder.ExpectCreateConnection(); | |
| 267 connection_finder.Find( | |
| 268 base::Bind(&MockBluetoothConnectionFinder::OnConnectionFound, | |
| 269 base::Unretained(&connection_finder))); | |
| 270 | |
| 271 connection->SetStatus(Connection::IN_PROGRESS); | |
| 272 connection->SetStatus(Connection::DISCONNECTED); | |
| 273 | |
| 274 // At this point, there is a pending poll scheduled. Hence, an event that | |
| 275 // would normally trigger a new polling iteration should not do so now, | |
| 276 // because the delay interval between successive polling attempts has not yet | |
| 277 // expired. | |
| 278 EXPECT_CALL(connection_finder, CreateConnectionProxy()).Times(0); | |
| 279 connection_finder.AdapterPresentChanged(adapter_.get(), true); | |
| 280 | |
| 281 // Now, allow the pending task to run, but fail early, so that no new task is | |
| 282 // posted. | |
| 283 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(false)); | |
| 284 base::RunLoop run_loop; | |
| 285 run_loop.RunUntilIdle(); | |
| 286 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); | |
| 287 | |
| 288 // Now that there is no pending task, events should once again trigger new | |
| 289 // polling iterations. | |
| 290 connection_finder.ExpectCreateConnection(); | |
| 291 connection_finder.AdapterPresentChanged(adapter_.get(), true); | |
| 292 } | |
| 293 | |
| 294 // TODO(isherman): Test screen lock state: | |
| 295 // * Find() should not poll if the screen is not locked. | |
| 296 // * If the screen becomes locked, we should poll. | |
| 297 | |
| 298 } // namespace proximity_auth | |
| OLD | NEW |