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

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

Issue 2256003002: components/arc: implement multi advertising (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@plumb-incoming-connections
Patch Set: setting 5 max advs to fix apps 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
29 class ArcBluetoothBridgeTest : public testing::Test { 30 class ArcBluetoothBridgeTest : public testing::Test {
30 protected: 31 protected:
31 void AddTestDevice() { 32 void AddTestDevice() {
32 bluez::BluezDBusManager* dbus_manager = bluez::BluezDBusManager::Get(); 33 bluez::BluezDBusManager* dbus_manager = bluez::BluezDBusManager::Get();
33 auto* fake_bluetooth_device_client = 34 auto* fake_bluetooth_device_client =
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 fake_bluetooth_instance_.get(), 2); 76 fake_bluetooth_instance_.get(), 2);
76 arc_bluetooth_bridge_.reset( 77 arc_bluetooth_bridge_.reset(
77 new ArcBluetoothBridge(fake_arc_bridge_service_.get())); 78 new ArcBluetoothBridge(fake_arc_bridge_service_.get()));
78 79
79 device::BluetoothAdapterFactory::GetAdapter(base::Bind( 80 device::BluetoothAdapterFactory::GetAdapter(base::Bind(
80 &ArcBluetoothBridgeTest::OnAdapterInitialized, base::Unretained(this))); 81 &ArcBluetoothBridgeTest::OnAdapterInitialized, base::Unretained(this)));
81 // We will quit the loop once we get the adapter. 82 // We will quit the loop once we get the adapter.
82 get_adapter_run_loop_.Run(); 83 get_adapter_run_loop_.Run();
83 } 84 }
84 85
86 // Helper methods for multi advertisement tests.
87 int32_t ReserveAdvertisementHandle() {
88 int32_t ret;
Rahul Chaturvedi 2016/09/29 22:49:18 The typical pattern is to have ret as a field. Tha
Eric Caruso 2016/09/29 22:57:34 I'd expect the callback would be done and called a
Rahul Chaturvedi 2016/09/30 19:51:08 Sure, but that expectation is only valid if the co
89 arc_bluetooth_bridge_->ReserveAdvertisementHandle(base::Bind(
90 &ArcBluetoothBridgeTest::ReserveAdvertisementHandleCallback, &ret));
91
92 base::RunLoop().RunUntilIdle();
93 return ret;
94 }
95
96 mojom::BluetoothGattStatus BroadcastAdvertisement(
97 int adv_handle,
98 std::unique_ptr<device::BluetoothAdvertisement::Data> data) {
99 mojom::BluetoothGattStatus ret;
100 arc_bluetooth_bridge_->BroadcastAdvertisement(
101 adv_handle, std::move(data),
102 base::Bind(&ArcBluetoothBridgeTest::StatusSetterCallback, &ret));
103
104 base::RunLoop().RunUntilIdle();
105 return ret;
106 }
107
108 mojom::BluetoothGattStatus ReleaseAdvertisementHandle(int adv_handle) {
109 mojom::BluetoothGattStatus ret;
110 arc_bluetooth_bridge_->ReleaseAdvertisementHandle(
111 adv_handle,
112 base::Bind(&ArcBluetoothBridgeTest::StatusSetterCallback, &ret));
113
114 base::RunLoop().RunUntilIdle();
115 return ret;
Rahul Chaturvedi 2016/09/29 22:49:18 Set ret to a canary value add an assert against it
116 }
117
118 static void ReserveAdvertisementHandleCallback(
119 int32_t* target_adv_handle,
120 mojom::BluetoothGattStatus status,
121 int32_t adv_handle) {
122 if (status == mojom::BluetoothGattStatus::GATT_FAILURE)
123 *target_adv_handle = -1;
124 else
125 *target_adv_handle = adv_handle;
126 }
127
128 static void StatusSetterCallback(mojom::BluetoothGattStatus* target_status,
129 mojom::BluetoothGattStatus status) {
130 *target_status = status;
131 }
132
133 bool CurrentlyAdvertising() {
Rahul Chaturvedi 2016/09/29 22:49:18 nit: IsCurrentlyAdvertising()
134 bluez::FakeBluetoothLEAdvertisingManagerClient* adv_client =
135 static_cast<bluez::FakeBluetoothLEAdvertisingManagerClient*>(
136 bluez::BluezDBusManager::Get()
137 ->GetBluetoothLEAdvertisingManagerClient());
138 return adv_client->currently_registered();
139 }
140
85 std::unique_ptr<FakeArcBridgeService> fake_arc_bridge_service_; 141 std::unique_ptr<FakeArcBridgeService> fake_arc_bridge_service_;
86 std::unique_ptr<FakeBluetoothInstance> fake_bluetooth_instance_; 142 std::unique_ptr<FakeBluetoothInstance> fake_bluetooth_instance_;
87 std::unique_ptr<ArcBluetoothBridge> arc_bluetooth_bridge_; 143 std::unique_ptr<ArcBluetoothBridge> arc_bluetooth_bridge_;
88 scoped_refptr<device::BluetoothAdapter> adapter_; 144 scoped_refptr<device::BluetoothAdapter> adapter_;
89 base::MessageLoop message_loop_; 145 base::MessageLoop message_loop_;
90 base::RunLoop get_adapter_run_loop_; 146 base::RunLoop get_adapter_run_loop_;
91 }; 147 };
92 148
93 // When we add device to bluez::FakeBluetoothDeviceClient, ArcBluetoothBridge 149 // When we add device to bluez::FakeBluetoothDeviceClient, ArcBluetoothBridge
94 // should send new device data to Android. This test will then check 150 // 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 260
205 EXPECT_EQ(device::BluetoothUUID(bluez::FakeBluetoothGattCharacteristicClient:: 261 EXPECT_EQ(device::BluetoothUUID(bluez::FakeBluetoothGattCharacteristicClient::
206 kHeartRateControlPointUUID), 262 kHeartRateControlPointUUID),
207 db[4]->uuid); 263 db[4]->uuid);
208 EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_CHARACTERISTIC, 264 EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_CHARACTERISTIC,
209 db[4]->type); 265 db[4]->type);
210 EXPECT_EQ(device::BluetoothGattCharacteristic::PROPERTY_WRITE, 266 EXPECT_EQ(device::BluetoothGattCharacteristic::PROPERTY_WRITE,
211 db[4]->properties); 267 db[4]->properties);
212 } 268 }
213 269
270 // Invoke multi advertisement methods and make sure they are going down to the
271 // D-Bus clients.
272 TEST_F(ArcBluetoothBridgeTest, MultiAdvertisement) {
Rahul Chaturvedi 2016/09/29 22:49:18 This is just SingleAdvertisement test :) You need
Eric Caruso 2016/09/29 22:57:34 Sure. I was using this as a simple test of whether
273 int32_t handle = ReserveAdvertisementHandle();
274 EXPECT_NE(handle, -1);
275 EXPECT_FALSE(CurrentlyAdvertising());
276
277 auto adv_data = base::MakeUnique<device::BluetoothAdvertisement::Data>(
278 device::BluetoothAdvertisement::ADVERTISEMENT_TYPE_BROADCAST);
279 mojom::BluetoothGattStatus status =
280 BroadcastAdvertisement(handle, std::move(adv_data));
281 EXPECT_EQ(status, mojom::BluetoothGattStatus::GATT_SUCCESS);
282 EXPECT_TRUE(CurrentlyAdvertising());
283
284 status = ReleaseAdvertisementHandle(handle);
285 EXPECT_EQ(status, mojom::BluetoothGattStatus::GATT_SUCCESS);
286 EXPECT_FALSE(CurrentlyAdvertising());
287 }
288
289 // Our stack currently lets us register 5 advertisements simultaneously. We
290 // want to make sure we can reserve several advertisement handles without using
291 // all of those 5 slots.
292 // This also tests that we support releasing reserved but unused handles.
293 TEST_F(ArcBluetoothBridgeTest, ReserveAndReleaseMaximumAdvertisementHandles) {
294 const size_t kChromeMaxAdvertisements = 5;
295 std::vector<int32_t> reserved_handles;
296
297 for (size_t i = 0; i < kChromeMaxAdvertisements; i++) {
298 int32_t handle = ReserveAdvertisementHandle();
299 if (handle == -1)
300 break;
301 reserved_handles.push_back(handle);
302 }
303 EXPECT_GT(reserved_handles.size(), 1UL);
304 EXPECT_LT(reserved_handles.size(), kChromeMaxAdvertisements);
305
306 for (int32_t handle : reserved_handles) {
307 EXPECT_EQ(ReleaseAdvertisementHandle(handle),
308 mojom::BluetoothGattStatus::GATT_SUCCESS);
309 }
310 }
311
214 } // namespace arc 312 } // namespace arc
OLDNEW
« no previous file with comments | « components/arc/bluetooth/arc_bluetooth_bridge.cc ('k') | components/arc/bluetooth/bluetooth_struct_traits.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698