Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2015 Google, Inc. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at: |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | #include <base/macros.h> |
| 18 | #include <gmock/gmock.h> |
| 19 | #include <gtest/gtest.h> |
| 20 | |
| 21 | #include "service/gatt_client.h" |
| 22 | #include "service/hal/fake_bluetooth_gatt_interface.h" |
| 23 | |
| 24 | using ::testing::_; |
| 25 | using ::testing::Return; |
| 26 | |
| 27 | namespace bluetooth { |
| 28 | namespace { |
| 29 | |
| 30 | class MockGattHandler |
| 31 | : public hal::FakeBluetoothGattInterface::TestClientHandler { |
| 32 | public: |
| 33 | MockGattHandler() = default; |
| 34 | ~MockGattHandler() override = default; |
| 35 | |
Jakub Pawlowski | 96ac0a3 | 2017-06-21 00:00:18 -0700 | [diff] [blame] | 36 | MOCK_METHOD1(RegisterClient, bt_status_t(const bt_uuid_t&)); |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 37 | MOCK_METHOD1(UnregisterClient, bt_status_t(int)); |
Arman Uguray | 3f6aa07 | 2015-11-30 14:58:11 -0800 | [diff] [blame] | 38 | MOCK_METHOD1(Scan, bt_status_t(bool)); |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame] | 39 | MOCK_METHOD4(Connect, bt_status_t(int, const RawAddress&, bool, int)); |
| 40 | MOCK_METHOD3(Disconnect, bt_status_t(int, const RawAddress&, int)); |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 41 | |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 42 | private: |
| 43 | DISALLOW_COPY_AND_ASSIGN(MockGattHandler); |
| 44 | }; |
| 45 | |
| 46 | class GattClientTest : public ::testing::Test { |
| 47 | public: |
| 48 | GattClientTest() = default; |
| 49 | ~GattClientTest() override = default; |
| 50 | |
| 51 | void SetUp() override { |
| 52 | // Only set |mock_handler_| if a previous test case hasn't set it. |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 53 | if (!mock_handler_) mock_handler_.reset(new MockGattHandler()); |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 54 | |
| 55 | fake_hal_gatt_iface_ = new hal::FakeBluetoothGattInterface( |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 56 | nullptr, nullptr, |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 57 | std::static_pointer_cast< |
| 58 | hal::FakeBluetoothGattInterface::TestClientHandler>(mock_handler_), |
| 59 | nullptr); |
| 60 | hal::BluetoothGattInterface::InitializeForTesting(fake_hal_gatt_iface_); |
| 61 | |
| 62 | factory_.reset(new GattClientFactory()); |
| 63 | } |
| 64 | |
| 65 | void TearDown() override { |
| 66 | factory_.reset(); |
| 67 | hal::BluetoothGattInterface::CleanUp(); |
| 68 | } |
| 69 | |
| 70 | protected: |
| 71 | hal::FakeBluetoothGattInterface* fake_hal_gatt_iface_; |
| 72 | std::shared_ptr<MockGattHandler> mock_handler_; |
| 73 | std::unique_ptr<GattClientFactory> factory_; |
| 74 | |
| 75 | private: |
| 76 | DISALLOW_COPY_AND_ASSIGN(GattClientTest); |
| 77 | }; |
| 78 | |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 79 | TEST_F(GattClientTest, RegisterInstance) { |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 80 | EXPECT_CALL(*mock_handler_, RegisterClient(_)) |
| 81 | .Times(2) |
| 82 | .WillOnce(Return(BT_STATUS_FAIL)) |
| 83 | .WillOnce(Return(BT_STATUS_SUCCESS)); |
| 84 | |
| 85 | // These will be asynchronously populated with a result when the callback |
| 86 | // executes. |
| 87 | BLEStatus status = BLE_STATUS_SUCCESS; |
| 88 | UUID cb_uuid; |
| 89 | std::unique_ptr<GattClient> client; |
| 90 | int callback_count = 0; |
| 91 | |
| 92 | auto callback = [&](BLEStatus in_status, const UUID& uuid, |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 93 | std::unique_ptr<BluetoothInstance> in_client) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 94 | status = in_status; |
| 95 | cb_uuid = uuid; |
| 96 | client = std::unique_ptr<GattClient>( |
| 97 | static_cast<GattClient*>(in_client.release())); |
| 98 | callback_count++; |
| 99 | }; |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 100 | |
| 101 | UUID uuid0 = UUID::GetRandom(); |
| 102 | |
| 103 | // HAL returns failure. |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 104 | EXPECT_FALSE(factory_->RegisterInstance(uuid0, callback)); |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 105 | EXPECT_EQ(0, callback_count); |
| 106 | |
| 107 | // HAL returns success. |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 108 | EXPECT_TRUE(factory_->RegisterInstance(uuid0, callback)); |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 109 | EXPECT_EQ(0, callback_count); |
| 110 | |
| 111 | // Calling twice with the same UUID should fail with no additional call into |
| 112 | // the stack. |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 113 | EXPECT_FALSE(factory_->RegisterInstance(uuid0, callback)); |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 114 | |
| 115 | testing::Mock::VerifyAndClearExpectations(mock_handler_.get()); |
| 116 | |
| 117 | // Call with a different UUID while one is pending. |
| 118 | UUID uuid1 = UUID::GetRandom(); |
| 119 | EXPECT_CALL(*mock_handler_, RegisterClient(_)) |
| 120 | .Times(1) |
| 121 | .WillOnce(Return(BT_STATUS_SUCCESS)); |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 122 | EXPECT_TRUE(factory_->RegisterInstance(uuid1, callback)); |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 123 | |
| 124 | // Trigger callback with an unknown UUID. This should get ignored. |
| 125 | UUID uuid2 = UUID::GetRandom(); |
| 126 | bt_uuid_t hal_uuid = uuid2.GetBlueDroid(); |
| 127 | fake_hal_gatt_iface_->NotifyRegisterClientCallback(0, 0, hal_uuid); |
| 128 | EXPECT_EQ(0, callback_count); |
| 129 | |
| 130 | // |uuid0| succeeds. |
| 131 | int client_id0 = 2; // Pick something that's not 0. |
| 132 | hal_uuid = uuid0.GetBlueDroid(); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 133 | fake_hal_gatt_iface_->NotifyRegisterClientCallback(BT_STATUS_SUCCESS, |
| 134 | client_id0, hal_uuid); |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 135 | |
| 136 | EXPECT_EQ(1, callback_count); |
| 137 | ASSERT_TRUE(client.get() != nullptr); // Assert to terminate in case of error |
| 138 | EXPECT_EQ(BLE_STATUS_SUCCESS, status); |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 139 | EXPECT_EQ(client_id0, client->GetInstanceId()); |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 140 | EXPECT_EQ(uuid0, client->GetAppIdentifier()); |
| 141 | EXPECT_EQ(uuid0, cb_uuid); |
| 142 | |
| 143 | // The client should unregister itself when deleted. |
| 144 | EXPECT_CALL(*mock_handler_, UnregisterClient(client_id0)) |
| 145 | .Times(1) |
| 146 | .WillOnce(Return(BT_STATUS_SUCCESS)); |
| 147 | client.reset(); |
| 148 | testing::Mock::VerifyAndClearExpectations(mock_handler_.get()); |
| 149 | |
| 150 | // |uuid1| fails. |
| 151 | int client_id1 = 3; |
| 152 | hal_uuid = uuid1.GetBlueDroid(); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 153 | fake_hal_gatt_iface_->NotifyRegisterClientCallback(BT_STATUS_FAIL, client_id1, |
| 154 | hal_uuid); |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 155 | |
| 156 | EXPECT_EQ(2, callback_count); |
| 157 | ASSERT_TRUE(client.get() == nullptr); // Assert to terminate in case of error |
| 158 | EXPECT_EQ(BLE_STATUS_FAILURE, status); |
| 159 | EXPECT_EQ(uuid1, cb_uuid); |
| 160 | } |
| 161 | |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 162 | } // namespace |
| 163 | } // namespace bluetooth |