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 | |
| 36 | MOCK_METHOD1(RegisterClient, bt_status_t(bt_uuid_t*)); |
| 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 | ce021dd | 2016-01-19 16:35:20 -0800 | [diff] [blame] | 39 | MOCK_METHOD4(Connect, bt_status_t(int , const bt_bdaddr_t *, bool, int)); |
| 40 | MOCK_METHOD3(Disconnect, bt_status_t(int , const bt_bdaddr_t *, 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. |
| 53 | if (!mock_handler_) |
| 54 | mock_handler_.reset(new MockGattHandler()); |
| 55 | |
| 56 | fake_hal_gatt_iface_ = new hal::FakeBluetoothGattInterface( |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 57 | nullptr, |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame^] | 58 | nullptr, |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 59 | std::static_pointer_cast< |
| 60 | hal::FakeBluetoothGattInterface::TestClientHandler>(mock_handler_), |
| 61 | nullptr); |
| 62 | hal::BluetoothGattInterface::InitializeForTesting(fake_hal_gatt_iface_); |
| 63 | |
| 64 | factory_.reset(new GattClientFactory()); |
| 65 | } |
| 66 | |
| 67 | void TearDown() override { |
| 68 | factory_.reset(); |
| 69 | hal::BluetoothGattInterface::CleanUp(); |
| 70 | } |
| 71 | |
| 72 | protected: |
| 73 | hal::FakeBluetoothGattInterface* fake_hal_gatt_iface_; |
| 74 | std::shared_ptr<MockGattHandler> mock_handler_; |
| 75 | std::unique_ptr<GattClientFactory> factory_; |
| 76 | |
| 77 | private: |
| 78 | DISALLOW_COPY_AND_ASSIGN(GattClientTest); |
| 79 | }; |
| 80 | |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 81 | TEST_F(GattClientTest, RegisterInstance) { |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 82 | EXPECT_CALL(*mock_handler_, RegisterClient(_)) |
| 83 | .Times(2) |
| 84 | .WillOnce(Return(BT_STATUS_FAIL)) |
| 85 | .WillOnce(Return(BT_STATUS_SUCCESS)); |
| 86 | |
| 87 | // These will be asynchronously populated with a result when the callback |
| 88 | // executes. |
| 89 | BLEStatus status = BLE_STATUS_SUCCESS; |
| 90 | UUID cb_uuid; |
| 91 | std::unique_ptr<GattClient> client; |
| 92 | int callback_count = 0; |
| 93 | |
| 94 | auto callback = [&](BLEStatus in_status, const UUID& uuid, |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 95 | std::unique_ptr<BluetoothInstance> in_client) { |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 96 | status = in_status; |
| 97 | cb_uuid = uuid; |
| 98 | client = std::unique_ptr<GattClient>( |
| 99 | static_cast<GattClient*>(in_client.release())); |
| 100 | callback_count++; |
| 101 | }; |
| 102 | |
| 103 | UUID uuid0 = UUID::GetRandom(); |
| 104 | |
| 105 | // HAL returns failure. |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 106 | EXPECT_FALSE(factory_->RegisterInstance(uuid0, callback)); |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 107 | EXPECT_EQ(0, callback_count); |
| 108 | |
| 109 | // HAL returns success. |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 110 | EXPECT_TRUE(factory_->RegisterInstance(uuid0, callback)); |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 111 | EXPECT_EQ(0, callback_count); |
| 112 | |
| 113 | // Calling twice with the same UUID should fail with no additional call into |
| 114 | // the stack. |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 115 | EXPECT_FALSE(factory_->RegisterInstance(uuid0, callback)); |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 116 | |
| 117 | testing::Mock::VerifyAndClearExpectations(mock_handler_.get()); |
| 118 | |
| 119 | // Call with a different UUID while one is pending. |
| 120 | UUID uuid1 = UUID::GetRandom(); |
| 121 | EXPECT_CALL(*mock_handler_, RegisterClient(_)) |
| 122 | .Times(1) |
| 123 | .WillOnce(Return(BT_STATUS_SUCCESS)); |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 124 | EXPECT_TRUE(factory_->RegisterInstance(uuid1, callback)); |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 125 | |
| 126 | // Trigger callback with an unknown UUID. This should get ignored. |
| 127 | UUID uuid2 = UUID::GetRandom(); |
| 128 | bt_uuid_t hal_uuid = uuid2.GetBlueDroid(); |
| 129 | fake_hal_gatt_iface_->NotifyRegisterClientCallback(0, 0, hal_uuid); |
| 130 | EXPECT_EQ(0, callback_count); |
| 131 | |
| 132 | // |uuid0| succeeds. |
| 133 | int client_id0 = 2; // Pick something that's not 0. |
| 134 | hal_uuid = uuid0.GetBlueDroid(); |
| 135 | fake_hal_gatt_iface_->NotifyRegisterClientCallback( |
| 136 | BT_STATUS_SUCCESS, client_id0, hal_uuid); |
| 137 | |
| 138 | EXPECT_EQ(1, callback_count); |
| 139 | ASSERT_TRUE(client.get() != nullptr); // Assert to terminate in case of error |
| 140 | EXPECT_EQ(BLE_STATUS_SUCCESS, status); |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 141 | EXPECT_EQ(client_id0, client->GetInstanceId()); |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 142 | EXPECT_EQ(uuid0, client->GetAppIdentifier()); |
| 143 | EXPECT_EQ(uuid0, cb_uuid); |
| 144 | |
| 145 | // The client should unregister itself when deleted. |
| 146 | EXPECT_CALL(*mock_handler_, UnregisterClient(client_id0)) |
| 147 | .Times(1) |
| 148 | .WillOnce(Return(BT_STATUS_SUCCESS)); |
| 149 | client.reset(); |
| 150 | testing::Mock::VerifyAndClearExpectations(mock_handler_.get()); |
| 151 | |
| 152 | // |uuid1| fails. |
| 153 | int client_id1 = 3; |
| 154 | hal_uuid = uuid1.GetBlueDroid(); |
| 155 | fake_hal_gatt_iface_->NotifyRegisterClientCallback( |
| 156 | BT_STATUS_FAIL, client_id1, hal_uuid); |
| 157 | |
| 158 | EXPECT_EQ(2, callback_count); |
| 159 | ASSERT_TRUE(client.get() == nullptr); // Assert to terminate in case of error |
| 160 | EXPECT_EQ(BLE_STATUS_FAILURE, status); |
| 161 | EXPECT_EQ(uuid1, cb_uuid); |
| 162 | } |
| 163 | |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 164 | } // namespace |
| 165 | } // namespace bluetooth |