OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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/throttled_bluetooth_connection_finder.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/test/test_simple_task_runner.h" |
| 9 #include "base/time/time.h" |
| 10 #include "components/proximity_auth/bluetooth_connection_finder.h" |
| 11 #include "components/proximity_auth/bluetooth_throttler.h" |
| 12 #include "components/proximity_auth/connection.h" |
| 13 #include "components/proximity_auth/remote_device.h" |
| 14 #include "device/bluetooth/bluetooth_uuid.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 using testing::NiceMock; |
| 19 using testing::Return; |
| 20 |
| 21 namespace proximity_auth { |
| 22 namespace { |
| 23 |
| 24 const int kPollingIntervalSeconds = 7; |
| 25 const char kUuid[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEF"; |
| 26 |
| 27 // A callback that stores a found |connection| into |out|. |
| 28 void SaveConnection(scoped_ptr<Connection>* out, |
| 29 scoped_ptr<Connection> connection) { |
| 30 *out = connection.Pass(); |
| 31 } |
| 32 |
| 33 class StubConnection : public Connection { |
| 34 public: |
| 35 StubConnection() : Connection(RemoteDevice()) {} |
| 36 ~StubConnection() override {} |
| 37 |
| 38 void Connect() override {} |
| 39 void Disconnect() override {} |
| 40 void SendMessageImpl(scoped_ptr<WireMessage> message) override {} |
| 41 |
| 42 private: |
| 43 DISALLOW_COPY_AND_ASSIGN(StubConnection); |
| 44 }; |
| 45 |
| 46 class MockBluetoothThrottler : public BluetoothThrottler { |
| 47 public: |
| 48 MockBluetoothThrottler() {} |
| 49 ~MockBluetoothThrottler() override {} |
| 50 |
| 51 MOCK_CONST_METHOD0(GetDelay, base::TimeDelta()); |
| 52 MOCK_METHOD0(OnConnectionClosed, void()); |
| 53 |
| 54 private: |
| 55 DISALLOW_COPY_AND_ASSIGN(MockBluetoothThrottler); |
| 56 }; |
| 57 |
| 58 class FakeBluetoothConnectionFinder : public BluetoothConnectionFinder { |
| 59 public: |
| 60 FakeBluetoothConnectionFinder() |
| 61 : BluetoothConnectionFinder( |
| 62 RemoteDevice(), |
| 63 device::BluetoothUUID(kUuid), |
| 64 base::TimeDelta::FromSeconds(kPollingIntervalSeconds)) {} |
| 65 ~FakeBluetoothConnectionFinder() override {} |
| 66 |
| 67 void Find(const ConnectionCallback& connection_callback) override { |
| 68 connection_callback.Run(make_scoped_ptr(new StubConnection)); |
| 69 } |
| 70 |
| 71 private: |
| 72 DISALLOW_COPY_AND_ASSIGN(FakeBluetoothConnectionFinder); |
| 73 }; |
| 74 |
| 75 } // namespace |
| 76 |
| 77 class ProximityAuthThrottledBluetoothConnectionFinderTest |
| 78 : public testing::Test { |
| 79 public: |
| 80 ProximityAuthThrottledBluetoothConnectionFinderTest() |
| 81 : task_runner_(new base::TestSimpleTaskRunner) {} |
| 82 |
| 83 protected: |
| 84 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 85 NiceMock<MockBluetoothThrottler> throttler_; |
| 86 }; |
| 87 |
| 88 TEST_F(ProximityAuthThrottledBluetoothConnectionFinderTest, |
| 89 Find_ExecutesImmediatelyWhenUnthrottled) { |
| 90 ON_CALL(throttler_, GetDelay()).WillByDefault(Return(base::TimeDelta())); |
| 91 |
| 92 ThrottledBluetoothConnectionFinder connection_finder( |
| 93 make_scoped_ptr(new FakeBluetoothConnectionFinder), task_runner_, |
| 94 &throttler_); |
| 95 scoped_ptr<Connection> connection; |
| 96 connection_finder.Find(base::Bind(&SaveConnection, &connection)); |
| 97 EXPECT_TRUE(connection); |
| 98 } |
| 99 |
| 100 TEST_F(ProximityAuthThrottledBluetoothConnectionFinderTest, |
| 101 Find_ExecutesAfterADelyaWhenThrottled) { |
| 102 ON_CALL(throttler_, GetDelay()) |
| 103 .WillByDefault(Return(base::TimeDelta::FromSeconds(1))); |
| 104 |
| 105 ThrottledBluetoothConnectionFinder connection_finder( |
| 106 make_scoped_ptr(new FakeBluetoothConnectionFinder), task_runner_, |
| 107 &throttler_); |
| 108 scoped_ptr<Connection> connection; |
| 109 connection_finder.Find(base::Bind(&SaveConnection, &connection)); |
| 110 EXPECT_FALSE(connection); |
| 111 |
| 112 // The connection should be found once the throttling period has elapsed. |
| 113 ON_CALL(throttler_, GetDelay()).WillByDefault(Return(base::TimeDelta())); |
| 114 task_runner_->RunUntilIdle(); |
| 115 EXPECT_TRUE(connection); |
| 116 } |
| 117 |
| 118 TEST_F(ProximityAuthThrottledBluetoothConnectionFinderTest, |
| 119 OnConnectionStatusChanged_NotifiesThrottlerWhenConnectionDisconnects) { |
| 120 ON_CALL(throttler_, GetDelay()).WillByDefault(Return(base::TimeDelta())); |
| 121 |
| 122 ThrottledBluetoothConnectionFinder connection_finder( |
| 123 make_scoped_ptr(new FakeBluetoothConnectionFinder), task_runner_, |
| 124 &throttler_); |
| 125 scoped_ptr<Connection> connection; |
| 126 connection_finder.Find(base::Bind(&SaveConnection, &connection)); |
| 127 ASSERT_TRUE(connection); |
| 128 |
| 129 // Simulate the connection disconnecting. |
| 130 EXPECT_CALL(throttler_, OnConnectionClosed()); |
| 131 static_cast<ConnectionObserver*>(&connection_finder) |
| 132 ->OnConnectionStatusChanged(*connection, Connection::CONNECTED, |
| 133 Connection::DISCONNECTED); |
| 134 } |
| 135 |
| 136 } // namespace proximity_auth |
OLD | NEW |