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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: components/arc/bluetooth/arc_bluetooth_bridge_unittest.cc
diff --git a/components/arc/bluetooth/arc_bluetooth_bridge_unittest.cc b/components/arc/bluetooth/arc_bluetooth_bridge_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b4c9f80d82e5e5a4ab669c0d7515b5b460d465f6
--- /dev/null
+++ b/components/arc/bluetooth/arc_bluetooth_bridge_unittest.cc
@@ -0,0 +1,221 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/arc/bluetooth/arc_bluetooth_bridge.h"
+
+#include <string>
+#include <vector>
+
+#include "components/arc/bluetooth/bluetooth_type_converters.h"
+#include "components/arc/common/bluetooth.mojom.h"
+#include "components/arc/test/fake_arc_bridge_service.h"
+#include "components/arc/test/fake_bluetooth_instance.h"
+#include "device/bluetooth/test/mock_bluetooth_adapter.h"
+#include "device/bluetooth/test/mock_bluetooth_device.h"
+#include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h"
+#include "device/bluetooth/test/mock_bluetooth_gatt_descriptor.h"
+#include "device/bluetooth/test/mock_bluetooth_gatt_service.h"
+#include "mojo/public/cpp/bindings/array.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace arc {
+
+namespace {
+
+const char kAddressStr[] = "1A:2B:3C:4D:5E:6F";
rkc 2016/06/19 18:45:43 constexpr everywhere.
+const char kDeviceName[] = "DeviceName";
+const uint32_t kDeviceClass = 0x2580;
+const int16_t kDeviceRssi = -45;
+const char kServiceID[] = "/org/bluez/hci0/dev_1A_2B_3C_4D_5E_6F/service180f";
+const char kServiceUUID[] = "180f";
+const char kCharID[] =
+ "/org/bluez/hci0/dev_1A_2B_3C_4D_5E_6F/service180f/char2a19";
+const char kCharUUID[] = "2a19";
+const uint16_t kCharUUIDInt = 0x2a19;
+
+class FakeArcBluetoothBridge : public ArcBluetoothBridge {
+ public:
+ FakeArcBluetoothBridge(ArcBridgeService* bridge_service,
+ device::MockBluetoothAdapter* adapter)
+ : ArcBluetoothBridge(bridge_service, true /* test_flag */) {
+ SetAdapterForTest(adapter);
+ }
+
+ void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter) {}
+};
+
+} // namespace
+
+class ArcBluetoothBridgeTest : public testing::Test {
+ public:
+ ArcBluetoothBridgeTest() {}
+
+ protected:
+ FakeArcBridgeService* fake_arc_bridge_service_;
+ FakeArcBluetoothBridge* fake_arc_bluetooth_bridge_;
+ FakeBluetoothInstance* fake_bluetooth_instance;
+ device::MockBluetoothAdapter* fake_adapter_;
+ device::MockBluetoothDevice* fake_device_;
+ device::MockBluetoothGattService* fake_service_;
+ device::MockBluetoothGattCharacteristic* fake_char_;
+ std::vector<device::BluetoothUUID> device_uuids_;
+
+ mojom::BluetoothAddressPtr remote_addr;
+ mojom::BluetoothGattServiceIDPtr service_id;
+ mojom::BluetoothGattIDPtr char_id;
+
+ private:
+ void SetUp() override {
+ fake_arc_bridge_service_ = new FakeArcBridgeService();
+
+ fake_adapter_ = new testing::NiceMock<device::MockBluetoothAdapter>();
+ fake_arc_bluetooth_bridge_ =
+ new FakeArcBluetoothBridge(fake_arc_bridge_service_, fake_adapter_);
+
+ fake_device_ = new testing::NiceMock<device::MockBluetoothDevice>(
+ fake_adapter_, 0 /* bluetooth_class */, kDeviceName, kAddressStr,
+ false /* paired */, false /* connected */);
+ ON_CALL(*fake_device_, GetBluetoothClass())
+ .WillByDefault(testing::Return(kDeviceClass));
+ ON_CALL(*fake_device_, GetInquiryRSSI())
+ .WillByDefault(testing::Return(kDeviceRssi));
+
+ device_uuids_.emplace_back(device::BluetoothUUID(kServiceUUID));
+ device_uuids_.emplace_back(device::BluetoothUUID(kCharUUID));
+ ON_CALL(*fake_device_, GetUUIDs())
+ .WillByDefault(testing::Return(device_uuids_));
+
+ fake_adapter_->AddMockDevice(base::WrapUnique(fake_device_));
+ ON_CALL(*fake_adapter_, GetDevices())
+ .WillByDefault(testing::Return(fake_adapter_->GetConstMockDevices()));
+ ON_CALL(*fake_adapter_, GetDevice("1A:2B:3C:4D:5E:6F"))
+ .WillByDefault(testing::Return(fake_device_));
+
+ fake_service_ = new testing::NiceMock<device::MockBluetoothGattService>(
+ fake_device_, kServiceID, device::BluetoothUUID(kServiceUUID),
+ true /* is_primary */, true /* is_local */);
+ fake_device_->AddMockService(base::WrapUnique(fake_service_));
+ ON_CALL(*fake_device_, GetGattServices())
+ .WillByDefault(testing::Return(fake_device_->GetMockServices()));
+
+ fake_char_ = new testing::NiceMock<device::MockBluetoothGattCharacteristic>(
+ fake_service_, kCharID, device::BluetoothUUID(kCharUUID),
+ true /*is_local*/, device::BluetoothGattCharacteristic::PROPERTY_READ,
+ device::BluetoothGattCharacteristic::PERMISSION_READ);
+ fake_service_->AddMockCharacteristic(base::WrapUnique(fake_char_));
+ ON_CALL(*fake_service_, GetCharacteristics())
+ .WillByDefault(
+ testing::Return(fake_service_->GetMockCharacteristics()));
+
+ remote_addr = mojom::BluetoothAddress::From(std::string(kAddressStr));
+
+ service_id = mojom::BluetoothGattServiceID::New();
+ service_id->is_primary = true;
+ service_id->id = mojom::BluetoothGattID::New();
+ service_id->id->inst_id = 0;
+ service_id->id->uuid =
+ mojom::BluetoothUUID::From(device::BluetoothUUID(kServiceUUID));
+
+ char_id = mojom::BluetoothGattID::New();
+ char_id->inst_id = 0;
+ char_id->uuid =
+ mojom::BluetoothUUID::From(device::BluetoothUUID(kCharUUID));
+
+ mojom::BluetoothInstancePtr bluetooth_instance_ptr;
+ fake_bluetooth_instance = new FakeBluetoothInstance();
+ fake_arc_bluetooth_bridge_->SetBluetoothInstanceForTest(
+ fake_bluetooth_instance);
+ }
+
+ void TearDown() override {
+ fake_arc_bluetooth_bridge_->SetAdapterForTest(nullptr);
+ fake_arc_bluetooth_bridge_->SetBluetoothInstanceForTest(nullptr);
+ delete fake_arc_bridge_service_;
+ }
+};
+
+// Invoke OnDiscoveryStarted to send cached device to BT instance,
+// and check correctness of the device properties sent via arc bridge.
+TEST_F(ArcBluetoothBridgeTest, DeviceFound) {
+ fake_arc_bluetooth_bridge_->OnDiscoveryStarted(nullptr);
+
+ EXPECT_EQ((size_t)1, fake_bluetooth_instance->device_found_data().size());
+ const mojo::Array<mojom::BluetoothPropertyPtr>& prop =
+ fake_bluetooth_instance->device_found_data().back();
+
+ EXPECT_EQ((size_t)7, prop.size());
+ EXPECT_TRUE(prop[0]->is_bdname());
+ EXPECT_EQ(std::string(kDeviceName), prop[0]->get_bdname());
+ EXPECT_TRUE(prop[1]->is_bdaddr());
+ EXPECT_EQ(std::string(kAddressStr), prop[1]->get_bdaddr()->To<std::string>());
+ EXPECT_TRUE(prop[2]->is_uuids());
+ EXPECT_EQ(device_uuids_,
+ prop[2]->get_uuids().To<std::vector<device::BluetoothUUID>>());
+ EXPECT_TRUE(prop[3]->is_device_class());
+ EXPECT_EQ(kDeviceClass, prop[3]->get_device_class());
+ EXPECT_TRUE(prop[4]->is_device_type());
+ EXPECT_EQ(mojom::BluetoothDeviceType::DUAL, prop[4]->get_device_type());
+ EXPECT_TRUE(prop[5]->is_remote_friendly_name());
+ EXPECT_EQ(std::string(kDeviceName), prop[5]->get_remote_friendly_name());
+ EXPECT_TRUE(prop[6]->is_remote_rssi());
+ EXPECT_EQ(kDeviceRssi, prop[6]->get_remote_rssi());
+}
+
+// Invoke OnDiscoveryStarted to send cached device to BT instance,
+// and check correctness of the Advertising data sent via arc bridge.
+TEST_F(ArcBluetoothBridgeTest, LEDeviceFound) {
+ // Check that we check bluetooth_instance version correctly.
+ fake_arc_bluetooth_bridge_->OnDiscoveryStarted(nullptr);
+ EXPECT_EQ((size_t)0, fake_bluetooth_instance->le_device_found_data().size());
+
+ fake_arc_bluetooth_bridge_->SetBluetoothVersionForTest(1);
+ fake_arc_bluetooth_bridge_->OnDiscoveryStarted(nullptr);
+ EXPECT_EQ((size_t)1, fake_bluetooth_instance->le_device_found_data().size());
+
+ const mojom::BluetoothAddressPtr& addr =
+ fake_bluetooth_instance->le_device_found_data().back()->addr();
+ int32_t rssi = fake_bluetooth_instance->le_device_found_data().back()->rssi();
+ const mojo::Array<mojom::BluetoothAdvertisingDataPtr>& adv_data =
+ fake_bluetooth_instance->le_device_found_data().back()->adv_data();
+
+ EXPECT_EQ(std::string(kAddressStr), addr->To<std::string>());
+ EXPECT_EQ(kDeviceRssi, rssi);
+ EXPECT_EQ((size_t)1, adv_data.size());
+
+ EXPECT_TRUE(adv_data[0]->is_local_name());
+ EXPECT_EQ(std::string(kDeviceName),
+ adv_data[0]->get_local_name().To<std::string>());
+}
+
+// Invoke GetGattDB and check correctness of the GattDB sent via arc bridge.
+TEST_F(ArcBluetoothBridgeTest, GetGattDB) {
+ fake_arc_bluetooth_bridge_->GetGattDB(remote_addr.Clone());
+
+ EXPECT_EQ((size_t)1, fake_bluetooth_instance->gatt_db_result().size());
+
+ const mojom::BluetoothAddressPtr& addr =
+ fake_bluetooth_instance->gatt_db_result().back()->remote_addr();
+ const mojo::Array<mojom::BluetoothGattDBElementPtr>& db =
+ fake_bluetooth_instance->gatt_db_result().back()->db();
+
+ EXPECT_EQ(std::string(kAddressStr), addr->To<std::string>());
+ EXPECT_EQ((size_t)2, db.size());
+
+ EXPECT_EQ(device::BluetoothUUID(kServiceUUID),
+ db[0]->uuid.To<device::BluetoothUUID>());
+ EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_PRIMARY_SERVICE,
+ db[0]->type);
+ EXPECT_EQ(kCharUUIDInt, db[0]->start_handle);
+ EXPECT_EQ(kCharUUIDInt, db[0]->end_handle);
+
+ EXPECT_EQ(device::BluetoothUUID(kCharUUID),
+ db[1]->uuid.To<device::BluetoothUUID>());
+ EXPECT_EQ(mojom::BluetoothGattDBAttributeType::BTGATT_DB_CHARACTERISTIC,
+ db[1]->type);
+ EXPECT_EQ(device::BluetoothGattCharacteristic::PROPERTY_READ,
+ db[1]->properties);
+}
+
+} // namespace arc

Powered by Google App Engine
This is Rietveld 408576698