Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(687)

Side by Side Diff: components/arc/bluetooth/arc_bluetooth_bridge_unittest.cc

Issue 2394683007: Reland "components/arc: implement multi advertising" (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/arc/bluetooth/arc_bluetooth_bridge.h" 5 #include "components/arc/bluetooth/arc_bluetooth_bridge.h"
6 6
7 #include <string> 7 #include <string>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h" 12 #include "base/run_loop.h"
13 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
14 #include "components/arc/bluetooth/bluetooth_type_converters.h" 14 #include "components/arc/bluetooth/bluetooth_type_converters.h"
15 #include "components/arc/common/bluetooth.mojom.h" 15 #include "components/arc/common/bluetooth.mojom.h"
16 #include "components/arc/test/fake_arc_bridge_service.h" 16 #include "components/arc/test/fake_arc_bridge_service.h"
17 #include "components/arc/test/fake_bluetooth_instance.h" 17 #include "components/arc/test/fake_bluetooth_instance.h"
18 #include "device/bluetooth/dbus/bluez_dbus_manager.h" 18 #include "device/bluetooth/dbus/bluez_dbus_manager.h"
19 #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h" 19 #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h"
20 #include "device/bluetooth/dbus/fake_bluetooth_device_client.h" 20 #include "device/bluetooth/dbus/fake_bluetooth_device_client.h"
21 #include "device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_client.h" 21 #include "device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_client.h"
22 #include "device/bluetooth/dbus/fake_bluetooth_gatt_descriptor_client.h" 22 #include "device/bluetooth/dbus/fake_bluetooth_gatt_descriptor_client.h"
23 #include "device/bluetooth/dbus/fake_bluetooth_gatt_service_client.h" 23 #include "device/bluetooth/dbus/fake_bluetooth_gatt_service_client.h"
24 #include "device/bluetooth/dbus/fake_bluetooth_le_advertising_manager_client.h"
24 #include "mojo/public/cpp/bindings/array.h" 25 #include "mojo/public/cpp/bindings/array.h"
25 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
26 27
27 namespace arc { 28 namespace arc {
28 29
30 constexpr int kFailureAdvHandle = -1;
31
29 class ArcBluetoothBridgeTest : public testing::Test { 32 class ArcBluetoothBridgeTest : public testing::Test {
30 protected: 33 protected:
31 void AddTestDevice() { 34 void AddTestDevice() {
32 bluez::BluezDBusManager* dbus_manager = bluez::BluezDBusManager::Get(); 35 bluez::BluezDBusManager* dbus_manager = bluez::BluezDBusManager::Get();
33 auto* fake_bluetooth_device_client = 36 auto* fake_bluetooth_device_client =
34 static_cast<bluez::FakeBluetoothDeviceClient*>( 37 static_cast<bluez::FakeBluetoothDeviceClient*>(
35 dbus_manager->GetBluetoothDeviceClient()); 38 dbus_manager->GetBluetoothDeviceClient());
36 auto* fake_bluetooth_gatt_service_client = 39 auto* fake_bluetooth_gatt_service_client =
37 static_cast<bluez::FakeBluetoothGattServiceClient*>( 40 static_cast<bluez::FakeBluetoothGattServiceClient*>(
38 dbus_manager->GetBluetoothGattServiceClient()); 41 dbus_manager->GetBluetoothGattServiceClient());
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 fake_bluetooth_instance_.get(), 2); 78 fake_bluetooth_instance_.get(), 2);
76 arc_bluetooth_bridge_.reset( 79 arc_bluetooth_bridge_.reset(
77 new ArcBluetoothBridge(fake_arc_bridge_service_.get())); 80 new ArcBluetoothBridge(fake_arc_bridge_service_.get()));
78 81
79 device::BluetoothAdapterFactory::GetAdapter(base::Bind( 82 device::BluetoothAdapterFactory::GetAdapter(base::Bind(
80 &ArcBluetoothBridgeTest::OnAdapterInitialized, base::Unretained(this))); 83 &ArcBluetoothBridgeTest::OnAdapterInitialized, base::Unretained(this)));
81 // We will quit the loop once we get the adapter. 84 // We will quit the loop once we get the adapter.
82 get_adapter_run_loop_.Run(); 85 get_adapter_run_loop_.Run();
83 } 86 }
84 87
88 // Helper methods for multi advertisement tests.
89 int32_t ReserveAdvertisementHandle() {
90 constexpr int kSentinelHandle = -2;
91 last_adv_handle_ = kSentinelHandle;
92 arc_bluetooth_bridge_->ReserveAdvertisementHandle(
93 base::Bind(&ArcBluetoothBridgeTest::ReserveAdvertisementHandleCallback,
94 base::Unretained(this)));
95
96 base::RunLoop().RunUntilIdle();
97 // Make sure the callback was called.
98 EXPECT_NE(kSentinelHandle, last_adv_handle_);
99 return last_adv_handle_;
100 }
101
102 mojom::BluetoothGattStatus BroadcastAdvertisement(
103 int adv_handle,
104 std::unique_ptr<device::BluetoothAdvertisement::Data> data) {
105 last_status_ = mojom::BluetoothGattStatus::GATT_REQUEST_NOT_SUPPORTED;
106 arc_bluetooth_bridge_->BroadcastAdvertisement(
107 adv_handle, std::move(data),
108 base::Bind(&ArcBluetoothBridgeTest::StatusSetterCallback,
109 base::Unretained(this)));
110
111 base::RunLoop().RunUntilIdle();
112 EXPECT_NE(mojom::BluetoothGattStatus::GATT_REQUEST_NOT_SUPPORTED,
113 last_status_);
114 return last_status_;
115 }
116
117 mojom::BluetoothGattStatus ReleaseAdvertisementHandle(int adv_handle) {
118 last_status_ = mojom::BluetoothGattStatus::GATT_REQUEST_NOT_SUPPORTED;
119 arc_bluetooth_bridge_->ReleaseAdvertisementHandle(
120 adv_handle, base::Bind(&ArcBluetoothBridgeTest::StatusSetterCallback,
121 base::Unretained(this)));
122
123 base::RunLoop().RunUntilIdle();
124 EXPECT_NE(mojom::BluetoothGattStatus::GATT_REQUEST_NOT_SUPPORTED,
125 last_status_);
126 return last_status_;
127 }
128
129 void ReserveAdvertisementHandleCallback(mojom::BluetoothGattStatus status,
130 int32_t adv_handle) {
131 if (status == mojom::BluetoothGattStatus::GATT_FAILURE)
132 last_adv_handle_ = kFailureAdvHandle;
133 else
134 last_adv_handle_ = adv_handle;
135 }
136
137 void StatusSetterCallback(mojom::BluetoothGattStatus status) {
138 last_status_ = status;
139 }
140
141 int NumActiveAdvertisements() {
142 bluez::FakeBluetoothLEAdvertisingManagerClient* adv_client =
143 static_cast<bluez::FakeBluetoothLEAdvertisingManagerClient*>(
144 bluez::BluezDBusManager::Get()
145 ->GetBluetoothLEAdvertisingManagerClient());
146 return adv_client->currently_registered();
147 }
148
149 int last_adv_handle_;
150 mojom::BluetoothGattStatus last_status_;
151
85 std::unique_ptr<FakeArcBridgeService> fake_arc_bridge_service_; 152 std::unique_ptr<FakeArcBridgeService> fake_arc_bridge_service_;
86 std::unique_ptr<FakeBluetoothInstance> fake_bluetooth_instance_; 153 std::unique_ptr<FakeBluetoothInstance> fake_bluetooth_instance_;
87 std::unique_ptr<ArcBluetoothBridge> arc_bluetooth_bridge_; 154 std::unique_ptr<ArcBluetoothBridge> arc_bluetooth_bridge_;
88 scoped_refptr<device::BluetoothAdapter> adapter_; 155 scoped_refptr<device::BluetoothAdapter> adapter_;
89 base::MessageLoop message_loop_; 156 base::MessageLoop message_loop_;
90 base::RunLoop get_adapter_run_loop_; 157 base::RunLoop get_adapter_run_loop_;
91 }; 158 };
92 159
93 // When we add device to bluez::FakeBluetoothDeviceClient, ArcBluetoothBridge 160 // When we add device to bluez::FakeBluetoothDeviceClient, ArcBluetoothBridge
94 // should send new device data to Android. This test will then check 161 // should send new device data to Android. This test will then check
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 271
205 EXPECT_EQ(device::BluetoothUUID(bluez::FakeBluetoothGattCharacteristicClient:: 272 EXPECT_EQ(device::BluetoothUUID(bluez::FakeBluetoothGattCharacteristicClient::
206 kHeartRateControlPointUUID), 273 kHeartRateControlPointUUID),
207 db[4]->uuid); 274 db[4]->uuid);
208 EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_CHARACTERISTIC, 275 EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_CHARACTERISTIC,
209 db[4]->type); 276 db[4]->type);
210 EXPECT_EQ(device::BluetoothGattCharacteristic::PROPERTY_WRITE, 277 EXPECT_EQ(device::BluetoothGattCharacteristic::PROPERTY_WRITE,
211 db[4]->properties); 278 db[4]->properties);
212 } 279 }
213 280
281 // Invoke multi advertisement methods and make sure they are going down to the
282 // D-Bus clients.
283 TEST_F(ArcBluetoothBridgeTest, SingleAdvertisement) {
284 int32_t handle = ReserveAdvertisementHandle();
285 EXPECT_NE(kFailureAdvHandle, handle);
286 EXPECT_EQ(0, NumActiveAdvertisements());
287
288 auto adv_data = base::MakeUnique<device::BluetoothAdvertisement::Data>(
289 device::BluetoothAdvertisement::ADVERTISEMENT_TYPE_BROADCAST);
290 mojom::BluetoothGattStatus status =
291 BroadcastAdvertisement(handle, std::move(adv_data));
292 EXPECT_EQ(mojom::BluetoothGattStatus::GATT_SUCCESS, status);
293 EXPECT_EQ(1, NumActiveAdvertisements());
294
295 status = ReleaseAdvertisementHandle(handle);
296 EXPECT_EQ(mojom::BluetoothGattStatus::GATT_SUCCESS, status);
297 EXPECT_EQ(0, NumActiveAdvertisements());
298 }
299
300 // Invoke multi advertisement methods and make sure they are going down to the
301 // D-Bus clients.
302 TEST_F(ArcBluetoothBridgeTest, MultiAdvertisement) {
303 int32_t handle = ReserveAdvertisementHandle();
304 EXPECT_NE(kFailureAdvHandle, handle);
305 EXPECT_EQ(0, NumActiveAdvertisements());
306
307 auto adv_data = base::MakeUnique<device::BluetoothAdvertisement::Data>(
308 device::BluetoothAdvertisement::ADVERTISEMENT_TYPE_BROADCAST);
309 mojom::BluetoothGattStatus status =
310 BroadcastAdvertisement(handle, std::move(adv_data));
311 EXPECT_EQ(mojom::BluetoothGattStatus::GATT_SUCCESS, status);
312 EXPECT_EQ(1, NumActiveAdvertisements());
313
314 int32_t handle2 = ReserveAdvertisementHandle();
315 EXPECT_NE(kFailureAdvHandle, handle2);
316 auto adv_data2 = base::MakeUnique<device::BluetoothAdvertisement::Data>(
317 device::BluetoothAdvertisement::ADVERTISEMENT_TYPE_PERIPHERAL);
318 status = BroadcastAdvertisement(handle2, std::move(adv_data2));
319 EXPECT_EQ(mojom::BluetoothGattStatus::GATT_SUCCESS, status);
320 EXPECT_EQ(2, NumActiveAdvertisements());
321
322 status = ReleaseAdvertisementHandle(handle);
323 EXPECT_EQ(mojom::BluetoothGattStatus::GATT_SUCCESS, status);
324 EXPECT_EQ(1, NumActiveAdvertisements());
325
326 status = ReleaseAdvertisementHandle(handle2);
327 EXPECT_EQ(mojom::BluetoothGattStatus::GATT_SUCCESS, status);
328 EXPECT_EQ(0, NumActiveAdvertisements());
329 }
330
331 // This tests that we support releasing reserved but unused handles.
332 // TODO(ejcaruso): When Chrome supports more handles, make sure we
333 // will stop reserving handles before we use all of Chrome's.
334 TEST_F(ArcBluetoothBridgeTest, ReleaseUnusedHandles) {
335 constexpr size_t kMaxBluezAdvertisements =
336 bluez::FakeBluetoothLEAdvertisingManagerClient::kMaxBluezAdvertisements;
337 std::vector<int32_t> reserved_handles;
338
339 for (size_t i = 0; i < kMaxBluezAdvertisements; i++) {
340 int32_t handle = ReserveAdvertisementHandle();
341 if (handle == kFailureAdvHandle)
342 break;
343 reserved_handles.push_back(handle);
344 }
345 EXPECT_GT(reserved_handles.size(), 1Ul);
346 EXPECT_LE(reserved_handles.size(), kMaxBluezAdvertisements);
347 EXPECT_EQ(0, NumActiveAdvertisements());
348
349 for (int32_t handle : reserved_handles) {
350 EXPECT_EQ(ReleaseAdvertisementHandle(handle),
351 mojom::BluetoothGattStatus::GATT_SUCCESS);
352 }
353 }
354
214 } // namespace arc 355 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698