2 // Copyright 2015 Google, Inc.
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:
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <base/macros.h>
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
21 #include "service/gatt_client.h"
22 #include "service/hal/fake_bluetooth_gatt_interface.h"
25 using ::testing::Return;
31 : public hal::FakeBluetoothGattInterface::TestClientHandler {
33 MockGattHandler() = default;
34 ~MockGattHandler() override = default;
36 MOCK_METHOD1(RegisterClient, bt_status_t(const bluetooth::Uuid&));
37 MOCK_METHOD1(UnregisterClient, bt_status_t(int));
38 MOCK_METHOD1(Scan, bt_status_t(bool));
39 MOCK_METHOD4(Connect, bt_status_t(int, const RawAddress&, bool, int));
40 MOCK_METHOD3(Disconnect, bt_status_t(int, const RawAddress&, int));
43 DISALLOW_COPY_AND_ASSIGN(MockGattHandler);
46 class GattClientTest : public ::testing::Test {
48 GattClientTest() = default;
49 ~GattClientTest() override = default;
51 void SetUp() override {
52 // Only set |mock_handler_| if a previous test case hasn't set it.
53 if (!mock_handler_) mock_handler_.reset(new MockGattHandler());
55 fake_hal_gatt_iface_ = new hal::FakeBluetoothGattInterface(
57 std::static_pointer_cast<
58 hal::FakeBluetoothGattInterface::TestClientHandler>(mock_handler_),
60 hal::BluetoothGattInterface::InitializeForTesting(fake_hal_gatt_iface_);
62 factory_.reset(new GattClientFactory());
65 void TearDown() override {
67 hal::BluetoothGattInterface::CleanUp();
71 hal::FakeBluetoothGattInterface* fake_hal_gatt_iface_;
72 std::shared_ptr<MockGattHandler> mock_handler_;
73 std::unique_ptr<GattClientFactory> factory_;
76 DISALLOW_COPY_AND_ASSIGN(GattClientTest);
79 TEST_F(GattClientTest, RegisterInstance) {
80 EXPECT_CALL(*mock_handler_, RegisterClient(_))
82 .WillOnce(Return(BT_STATUS_FAIL))
83 .WillOnce(Return(BT_STATUS_SUCCESS));
85 // These will be asynchronously populated with a result when the callback
87 BLEStatus status = BLE_STATUS_SUCCESS;
89 std::unique_ptr<GattClient> client;
90 int callback_count = 0;
92 auto callback = [&](BLEStatus in_status, const Uuid& uuid,
93 std::unique_ptr<BluetoothInstance> in_client) {
96 client = std::unique_ptr<GattClient>(
97 static_cast<GattClient*>(in_client.release()));
101 Uuid uuid0 = Uuid::GetRandom();
103 // HAL returns failure.
104 EXPECT_FALSE(factory_->RegisterInstance(uuid0, callback));
105 EXPECT_EQ(0, callback_count);
107 // HAL returns success.
108 EXPECT_TRUE(factory_->RegisterInstance(uuid0, callback));
109 EXPECT_EQ(0, callback_count);
111 // Calling twice with the same Uuid should fail with no additional call into
113 EXPECT_FALSE(factory_->RegisterInstance(uuid0, callback));
115 testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
117 // Call with a different Uuid while one is pending.
118 Uuid uuid1 = Uuid::GetRandom();
119 EXPECT_CALL(*mock_handler_, RegisterClient(_))
121 .WillOnce(Return(BT_STATUS_SUCCESS));
122 EXPECT_TRUE(factory_->RegisterInstance(uuid1, callback));
124 // Trigger callback with an unknown Uuid. This should get ignored.
125 Uuid uuid2 = Uuid::GetRandom();
126 fake_hal_gatt_iface_->NotifyRegisterClientCallback(0, 0, uuid2);
127 EXPECT_EQ(0, callback_count);
130 int client_id0 = 2; // Pick something that's not 0.
131 fake_hal_gatt_iface_->NotifyRegisterClientCallback(BT_STATUS_SUCCESS,
134 EXPECT_EQ(1, callback_count);
135 ASSERT_TRUE(client.get() != nullptr); // Assert to terminate in case of error
136 EXPECT_EQ(BLE_STATUS_SUCCESS, status);
137 EXPECT_EQ(client_id0, client->GetInstanceId());
138 EXPECT_EQ(uuid0, client->GetAppIdentifier());
139 EXPECT_EQ(uuid0, cb_uuid);
141 // The client should unregister itself when deleted.
142 EXPECT_CALL(*mock_handler_, UnregisterClient(client_id0))
144 .WillOnce(Return(BT_STATUS_SUCCESS));
146 testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
150 fake_hal_gatt_iface_->NotifyRegisterClientCallback(BT_STATUS_FAIL, client_id1,
153 EXPECT_EQ(2, callback_count);
154 ASSERT_TRUE(client.get() == nullptr); // Assert to terminate in case of error
155 EXPECT_EQ(BLE_STATUS_FAILURE, status);
156 EXPECT_EQ(uuid1, cb_uuid);
160 } // namespace bluetooth