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