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

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

Issue 2046283003: Add unit test for ArcBluetoothBridge and TypeConverter (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bt
Patch Set: rebase Created 4 years, 6 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/arc/bluetooth/arc_bluetooth_bridge.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "components/arc/bluetooth/bluetooth_type_converters.h"
11 #include "components/arc/common/bluetooth.mojom.h"
12 #include "components/arc/test/fake_arc_bridge_service.h"
13 #include "components/arc/test/fake_bluetooth_instance.h"
14 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
15 #include "device/bluetooth/test/mock_bluetooth_device.h"
16 #include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h"
17 #include "device/bluetooth/test/mock_bluetooth_gatt_descriptor.h"
18 #include "device/bluetooth/test/mock_bluetooth_gatt_service.h"
19 #include "mojo/public/cpp/bindings/array.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22
23 namespace arc {
24
25 namespace {
26
27 const char kAddressStr[] = "1A:2B:3C:4D:5E:6F";
rkc 2016/06/19 18:45:43 constexpr everywhere.
28 const char kDeviceName[] = "DeviceName";
29 const uint32_t kDeviceClass = 0x2580;
30 const int16_t kDeviceRssi = -45;
31 const char kServiceID[] = "/org/bluez/hci0/dev_1A_2B_3C_4D_5E_6F/service180f";
32 const char kServiceUUID[] = "180f";
33 const char kCharID[] =
34 "/org/bluez/hci0/dev_1A_2B_3C_4D_5E_6F/service180f/char2a19";
35 const char kCharUUID[] = "2a19";
36 const uint16_t kCharUUIDInt = 0x2a19;
37
38 class FakeArcBluetoothBridge : public ArcBluetoothBridge {
39 public:
40 FakeArcBluetoothBridge(ArcBridgeService* bridge_service,
41 device::MockBluetoothAdapter* adapter)
42 : ArcBluetoothBridge(bridge_service, true /* test_flag */) {
43 SetAdapterForTest(adapter);
44 }
45
46 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter) {}
47 };
48
49 } // namespace
50
51 class ArcBluetoothBridgeTest : public testing::Test {
52 public:
53 ArcBluetoothBridgeTest() {}
54
55 protected:
56 FakeArcBridgeService* fake_arc_bridge_service_;
57 FakeArcBluetoothBridge* fake_arc_bluetooth_bridge_;
58 FakeBluetoothInstance* fake_bluetooth_instance;
59 device::MockBluetoothAdapter* fake_adapter_;
60 device::MockBluetoothDevice* fake_device_;
61 device::MockBluetoothGattService* fake_service_;
62 device::MockBluetoothGattCharacteristic* fake_char_;
63 std::vector<device::BluetoothUUID> device_uuids_;
64
65 mojom::BluetoothAddressPtr remote_addr;
66 mojom::BluetoothGattServiceIDPtr service_id;
67 mojom::BluetoothGattIDPtr char_id;
68
69 private:
70 void SetUp() override {
71 fake_arc_bridge_service_ = new FakeArcBridgeService();
72
73 fake_adapter_ = new testing::NiceMock<device::MockBluetoothAdapter>();
74 fake_arc_bluetooth_bridge_ =
75 new FakeArcBluetoothBridge(fake_arc_bridge_service_, fake_adapter_);
76
77 fake_device_ = new testing::NiceMock<device::MockBluetoothDevice>(
78 fake_adapter_, 0 /* bluetooth_class */, kDeviceName, kAddressStr,
79 false /* paired */, false /* connected */);
80 ON_CALL(*fake_device_, GetBluetoothClass())
81 .WillByDefault(testing::Return(kDeviceClass));
82 ON_CALL(*fake_device_, GetInquiryRSSI())
83 .WillByDefault(testing::Return(kDeviceRssi));
84
85 device_uuids_.emplace_back(device::BluetoothUUID(kServiceUUID));
86 device_uuids_.emplace_back(device::BluetoothUUID(kCharUUID));
87 ON_CALL(*fake_device_, GetUUIDs())
88 .WillByDefault(testing::Return(device_uuids_));
89
90 fake_adapter_->AddMockDevice(base::WrapUnique(fake_device_));
91 ON_CALL(*fake_adapter_, GetDevices())
92 .WillByDefault(testing::Return(fake_adapter_->GetConstMockDevices()));
93 ON_CALL(*fake_adapter_, GetDevice("1A:2B:3C:4D:5E:6F"))
94 .WillByDefault(testing::Return(fake_device_));
95
96 fake_service_ = new testing::NiceMock<device::MockBluetoothGattService>(
97 fake_device_, kServiceID, device::BluetoothUUID(kServiceUUID),
98 true /* is_primary */, true /* is_local */);
99 fake_device_->AddMockService(base::WrapUnique(fake_service_));
100 ON_CALL(*fake_device_, GetGattServices())
101 .WillByDefault(testing::Return(fake_device_->GetMockServices()));
102
103 fake_char_ = new testing::NiceMock<device::MockBluetoothGattCharacteristic>(
104 fake_service_, kCharID, device::BluetoothUUID(kCharUUID),
105 true /*is_local*/, device::BluetoothGattCharacteristic::PROPERTY_READ,
106 device::BluetoothGattCharacteristic::PERMISSION_READ);
107 fake_service_->AddMockCharacteristic(base::WrapUnique(fake_char_));
108 ON_CALL(*fake_service_, GetCharacteristics())
109 .WillByDefault(
110 testing::Return(fake_service_->GetMockCharacteristics()));
111
112 remote_addr = mojom::BluetoothAddress::From(std::string(kAddressStr));
113
114 service_id = mojom::BluetoothGattServiceID::New();
115 service_id->is_primary = true;
116 service_id->id = mojom::BluetoothGattID::New();
117 service_id->id->inst_id = 0;
118 service_id->id->uuid =
119 mojom::BluetoothUUID::From(device::BluetoothUUID(kServiceUUID));
120
121 char_id = mojom::BluetoothGattID::New();
122 char_id->inst_id = 0;
123 char_id->uuid =
124 mojom::BluetoothUUID::From(device::BluetoothUUID(kCharUUID));
125
126 mojom::BluetoothInstancePtr bluetooth_instance_ptr;
127 fake_bluetooth_instance = new FakeBluetoothInstance();
128 fake_arc_bluetooth_bridge_->SetBluetoothInstanceForTest(
129 fake_bluetooth_instance);
130 }
131
132 void TearDown() override {
133 fake_arc_bluetooth_bridge_->SetAdapterForTest(nullptr);
134 fake_arc_bluetooth_bridge_->SetBluetoothInstanceForTest(nullptr);
135 delete fake_arc_bridge_service_;
136 }
137 };
138
139 // Invoke OnDiscoveryStarted to send cached device to BT instance,
140 // and check correctness of the device properties sent via arc bridge.
141 TEST_F(ArcBluetoothBridgeTest, DeviceFound) {
142 fake_arc_bluetooth_bridge_->OnDiscoveryStarted(nullptr);
143
144 EXPECT_EQ((size_t)1, fake_bluetooth_instance->device_found_data().size());
145 const mojo::Array<mojom::BluetoothPropertyPtr>& prop =
146 fake_bluetooth_instance->device_found_data().back();
147
148 EXPECT_EQ((size_t)7, prop.size());
149 EXPECT_TRUE(prop[0]->is_bdname());
150 EXPECT_EQ(std::string(kDeviceName), prop[0]->get_bdname());
151 EXPECT_TRUE(prop[1]->is_bdaddr());
152 EXPECT_EQ(std::string(kAddressStr), prop[1]->get_bdaddr()->To<std::string>());
153 EXPECT_TRUE(prop[2]->is_uuids());
154 EXPECT_EQ(device_uuids_,
155 prop[2]->get_uuids().To<std::vector<device::BluetoothUUID>>());
156 EXPECT_TRUE(prop[3]->is_device_class());
157 EXPECT_EQ(kDeviceClass, prop[3]->get_device_class());
158 EXPECT_TRUE(prop[4]->is_device_type());
159 EXPECT_EQ(mojom::BluetoothDeviceType::DUAL, prop[4]->get_device_type());
160 EXPECT_TRUE(prop[5]->is_remote_friendly_name());
161 EXPECT_EQ(std::string(kDeviceName), prop[5]->get_remote_friendly_name());
162 EXPECT_TRUE(prop[6]->is_remote_rssi());
163 EXPECT_EQ(kDeviceRssi, prop[6]->get_remote_rssi());
164 }
165
166 // Invoke OnDiscoveryStarted to send cached device to BT instance,
167 // and check correctness of the Advertising data sent via arc bridge.
168 TEST_F(ArcBluetoothBridgeTest, LEDeviceFound) {
169 // Check that we check bluetooth_instance version correctly.
170 fake_arc_bluetooth_bridge_->OnDiscoveryStarted(nullptr);
171 EXPECT_EQ((size_t)0, fake_bluetooth_instance->le_device_found_data().size());
172
173 fake_arc_bluetooth_bridge_->SetBluetoothVersionForTest(1);
174 fake_arc_bluetooth_bridge_->OnDiscoveryStarted(nullptr);
175 EXPECT_EQ((size_t)1, fake_bluetooth_instance->le_device_found_data().size());
176
177 const mojom::BluetoothAddressPtr& addr =
178 fake_bluetooth_instance->le_device_found_data().back()->addr();
179 int32_t rssi = fake_bluetooth_instance->le_device_found_data().back()->rssi();
180 const mojo::Array<mojom::BluetoothAdvertisingDataPtr>& adv_data =
181 fake_bluetooth_instance->le_device_found_data().back()->adv_data();
182
183 EXPECT_EQ(std::string(kAddressStr), addr->To<std::string>());
184 EXPECT_EQ(kDeviceRssi, rssi);
185 EXPECT_EQ((size_t)1, adv_data.size());
186
187 EXPECT_TRUE(adv_data[0]->is_local_name());
188 EXPECT_EQ(std::string(kDeviceName),
189 adv_data[0]->get_local_name().To<std::string>());
190 }
191
192 // Invoke GetGattDB and check correctness of the GattDB sent via arc bridge.
193 TEST_F(ArcBluetoothBridgeTest, GetGattDB) {
194 fake_arc_bluetooth_bridge_->GetGattDB(remote_addr.Clone());
195
196 EXPECT_EQ((size_t)1, fake_bluetooth_instance->gatt_db_result().size());
197
198 const mojom::BluetoothAddressPtr& addr =
199 fake_bluetooth_instance->gatt_db_result().back()->remote_addr();
200 const mojo::Array<mojom::BluetoothGattDBElementPtr>& db =
201 fake_bluetooth_instance->gatt_db_result().back()->db();
202
203 EXPECT_EQ(std::string(kAddressStr), addr->To<std::string>());
204 EXPECT_EQ((size_t)2, db.size());
205
206 EXPECT_EQ(device::BluetoothUUID(kServiceUUID),
207 db[0]->uuid.To<device::BluetoothUUID>());
208 EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_PRIMARY_SERVICE,
209 db[0]->type);
210 EXPECT_EQ(kCharUUIDInt, db[0]->start_handle);
211 EXPECT_EQ(kCharUUIDInt, db[0]->end_handle);
212
213 EXPECT_EQ(device::BluetoothUUID(kCharUUID),
214 db[1]->uuid.To<device::BluetoothUUID>());
215 EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_CHARACTERISTIC,
216 db[1]->type);
217 EXPECT_EQ(device::BluetoothGattCharacteristic::PROPERTY_READ,
218 db[1]->properties);
219 }
220
221 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698