blob: 1628a7b6445ac908d9026d37e0151c0a8c6f0857 [file] [log] [blame]
Arman Uguray50a31542015-11-10 18:03:36 -08001//
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
24using ::testing::_;
25using ::testing::Return;
26
27namespace bluetooth {
28namespace {
29
30class 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 Uguray3f6aa072015-11-30 14:58:11 -080038 MOCK_METHOD1(Scan, bt_status_t(bool));
Jakub Pawlowskice021dd2016-01-19 16:35:20 -080039 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 Uguray50a31542015-11-10 18:03:36 -080041
Arman Uguray50a31542015-11-10 18:03:36 -080042 private:
43 DISALLOW_COPY_AND_ASSIGN(MockGattHandler);
44};
45
46class 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 Pawlowski67d5a252016-07-13 11:55:16 -070057 nullptr,
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -070058 nullptr,
Arman Uguray50a31542015-11-10 18:03:36 -080059 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 Uguraybb18c412015-11-12 13:44:31 -080081TEST_F(GattClientTest, RegisterInstance) {
Arman Uguray50a31542015-11-10 18:03:36 -080082 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 Uguraybb18c412015-11-12 13:44:31 -080095 std::unique_ptr<BluetoothInstance> in_client) {
Arman Uguray50a31542015-11-10 18:03:36 -080096 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 Uguraybb18c412015-11-12 13:44:31 -0800106 EXPECT_FALSE(factory_->RegisterInstance(uuid0, callback));
Arman Uguray50a31542015-11-10 18:03:36 -0800107 EXPECT_EQ(0, callback_count);
108
109 // HAL returns success.
Arman Uguraybb18c412015-11-12 13:44:31 -0800110 EXPECT_TRUE(factory_->RegisterInstance(uuid0, callback));
Arman Uguray50a31542015-11-10 18:03:36 -0800111 EXPECT_EQ(0, callback_count);
112
113 // Calling twice with the same UUID should fail with no additional call into
114 // the stack.
Arman Uguraybb18c412015-11-12 13:44:31 -0800115 EXPECT_FALSE(factory_->RegisterInstance(uuid0, callback));
Arman Uguray50a31542015-11-10 18:03:36 -0800116
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 Uguraybb18c412015-11-12 13:44:31 -0800124 EXPECT_TRUE(factory_->RegisterInstance(uuid1, callback));
Arman Uguray50a31542015-11-10 18:03:36 -0800125
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 Uguraybb18c412015-11-12 13:44:31 -0800141 EXPECT_EQ(client_id0, client->GetInstanceId());
Arman Uguray50a31542015-11-10 18:03:36 -0800142 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 Uguray50a31542015-11-10 18:03:36 -0800164} // namespace
165} // namespace bluetooth