123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- // Copyright 2021 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 "ash/quick_pair/keyed_service/battery_update_message_handler.h"
- #include <memory>
- #include "ash/quick_pair/common/constants.h"
- #include "ash/quick_pair/common/device.h"
- #include "ash/quick_pair/common/logging.h"
- #include "ash/quick_pair/common/protocol.h"
- #include "ash/quick_pair/message_stream/fake_bluetooth_socket.h"
- #include "ash/quick_pair/message_stream/fake_message_stream_lookup.h"
- #include "ash/quick_pair/message_stream/message_stream.h"
- #include "ash/quick_pair/message_stream/message_stream_lookup.h"
- #include "ash/quick_pair/pairing/mock_pairer_broker.h"
- #include "ash/quick_pair/pairing/pairer_broker.h"
- #include "ash/services/quick_pair/fast_pair_data_parser.h"
- #include "ash/services/quick_pair/mock_quick_pair_process_manager.h"
- #include "ash/services/quick_pair/quick_pair_process.h"
- #include "ash/services/quick_pair/quick_pair_process_manager.h"
- #include "ash/services/quick_pair/quick_pair_process_manager_impl.h"
- #include "base/memory/scoped_refptr.h"
- #include "base/run_loop.h"
- #include "base/test/task_environment.h"
- #include "device/bluetooth/bluetooth_adapter_factory.h"
- #include "device/bluetooth/test/mock_bluetooth_adapter.h"
- #include "device/bluetooth/test/mock_bluetooth_device.h"
- #include "mojo/public/cpp/bindings/shared_remote.h"
- #include "testing/gtest/include/gtest/gtest.h"
- #include "third_party/abseil-cpp/absl/types/optional.h"
- namespace {
- constexpr char kTestDeviceAddress[] = "11:12:13:14:15:16";
- constexpr char kTestBleDeviceName[] = "Test Device Name";
- std::vector<uint8_t> kBatteryUpdateBytes1 = {/*mesage_group=*/0x03,
- /*mesage_code=*/0x03,
- /*additional_data_length=*/0x00,
- 0x03,
- /*additional_data=*/0x57,
- 0x41,
- 0x7F};
- std::vector<uint8_t> kBatteryUpdateBytes2 = {/*mesage_group=*/0x03,
- /*mesage_code=*/0x03,
- /*additional_data_length=*/0x00,
- 0x03,
- /*additional_data=*/0x51,
- 0x38,
- 0x38};
- const std::vector<uint8_t> kModelIdBytes = {
- /*message_group=*/0x03,
- /*message_code=*/0x01,
- /*additional_data_length=*/0x00, 0x03,
- /*additional_data=*/0xAA, 0xBB, 0xCC};
- std::unique_ptr<testing::NiceMock<device::MockBluetoothDevice>>
- CreateTestBluetoothDevice(std::string address,
- device::MockBluetoothAdapter* adapter) {
- return std::make_unique<testing::NiceMock<device::MockBluetoothDevice>>(
- /*adapter=*/adapter, /*bluetooth_class=*/0, kTestBleDeviceName, address,
- /*paired=*/true, /*connected=*/false);
- }
- } // namespace
- namespace ash {
- namespace quick_pair {
- class BatteryUpdateMessageFakeBluetoothAdapter
- : public testing::NiceMock<device::MockBluetoothAdapter> {
- public:
- device::BluetoothDevice* GetDevice(const std::string& address) override {
- for (const auto& it : mock_devices_) {
- if (it->GetAddress() == address)
- return it.get();
- }
- return nullptr;
- }
- private:
- ~BatteryUpdateMessageFakeBluetoothAdapter() = default;
- };
- class BatteryUpdateMessageHandlerTest : public testing::Test {
- public:
- void SetUp() override {
- adapter_ = base::MakeRefCounted<BatteryUpdateMessageFakeBluetoothAdapter>();
- std::unique_ptr<testing::NiceMock<device::MockBluetoothDevice>>
- bluetooth_device =
- CreateTestBluetoothDevice(kTestDeviceAddress, adapter_.get());
- device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_);
- bluetooth_device_ = bluetooth_device.get();
- adapter_->AddMockDevice(std::move(bluetooth_device));
- message_stream_lookup_ = std::make_unique<FakeMessageStreamLookup>();
- fake_message_stream_lookup_ =
- static_cast<FakeMessageStreamLookup*>(message_stream_lookup_.get());
- message_stream_ =
- std::make_unique<MessageStream>(kTestDeviceAddress, fake_socket_.get());
- process_manager_ = std::make_unique<MockQuickPairProcessManager>();
- quick_pair_process::SetProcessManager(process_manager_.get());
- data_parser_ = std::make_unique<FastPairDataParser>(
- fast_pair_data_parser_.InitWithNewPipeAndPassReceiver());
- data_parser_remote_.Bind(std::move(fast_pair_data_parser_),
- task_environment_.GetMainThreadTaskRunner());
- EXPECT_CALL(*mock_process_manager(), GetProcessReference)
- .WillRepeatedly([&](QuickPairProcessManager::ProcessStoppedCallback) {
- return std::make_unique<
- QuickPairProcessManagerImpl::ProcessReferenceImpl>(
- data_parser_remote_, base::DoNothing());
- });
- battery_update_message_handler_ =
- std::make_unique<BatteryUpdateMessageHandler>(
- message_stream_lookup_.get());
- }
- void TearDown() override {
- fake_message_stream_lookup_->RemoveMessageStream(kTestDeviceAddress);
- battery_update_message_handler_.reset();
- }
- MockQuickPairProcessManager* mock_process_manager() {
- return static_cast<MockQuickPairProcessManager*>(process_manager_.get());
- }
- void SetMessageStream(const std::vector<uint8_t>& message_bytes) {
- fake_socket_->SetIOBufferFromBytes(message_bytes);
- message_stream_ =
- std::make_unique<MessageStream>(kTestDeviceAddress, fake_socket_.get());
- }
- void AddMessageStream(const std::vector<uint8_t>& message_bytes) {
- fake_socket_->SetIOBufferFromBytes(message_bytes);
- message_stream_ =
- std::make_unique<MessageStream>(kTestDeviceAddress, fake_socket_.get());
- fake_message_stream_lookup_->AddMessageStream(kTestDeviceAddress,
- message_stream_.get());
- }
- void NotifyMessageStreamConnected(std::string device_address) {
- fake_message_stream_lookup_->NotifyMessageStreamConnected(
- device_address, message_stream_.get());
- }
- protected:
- base::test::TaskEnvironment task_environment_{
- base::test::TaskEnvironment::TimeSource::MOCK_TIME};
- scoped_refptr<BatteryUpdateMessageFakeBluetoothAdapter> adapter_;
- scoped_refptr<FakeBluetoothSocket> fake_socket_ =
- base::MakeRefCounted<FakeBluetoothSocket>();
- std::unique_ptr<MessageStream> message_stream_;
- std::unique_ptr<MessageStreamLookup> message_stream_lookup_;
- FakeMessageStreamLookup* fake_message_stream_lookup_ = nullptr;
- mojo::SharedRemote<mojom::FastPairDataParser> data_parser_remote_;
- mojo::PendingRemote<mojom::FastPairDataParser> fast_pair_data_parser_;
- std::unique_ptr<FastPairDataParser> data_parser_;
- std::unique_ptr<QuickPairProcessManager> process_manager_;
- device::BluetoothDevice* bluetooth_device_ = nullptr;
- std::unique_ptr<BatteryUpdateMessageHandler> battery_update_message_handler_;
- };
- TEST_F(BatteryUpdateMessageHandlerTest, BatteryUpdate_GetMessages) {
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kCaseTrueWireless));
- SetMessageStream(kBatteryUpdateBytes1);
- fake_socket_->TriggerReceiveCallback();
- base::RunLoop().RunUntilIdle();
- NotifyMessageStreamConnected(kTestDeviceAddress);
- base::RunLoop().RunUntilIdle();
- EXPECT_NE(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
- EXPECT_NE(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
- EXPECT_NE(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kCaseTrueWireless));
- }
- TEST_F(BatteryUpdateMessageHandlerTest, BatteryUpdate_Observation) {
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kCaseTrueWireless));
- fake_socket_->SetIOBufferFromBytes(kBatteryUpdateBytes1);
- NotifyMessageStreamConnected(kTestDeviceAddress);
- base::RunLoop().RunUntilIdle();
- fake_socket_->TriggerReceiveCallback();
- base::RunLoop().RunUntilIdle();
- EXPECT_NE(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
- EXPECT_NE(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
- EXPECT_NE(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kCaseTrueWireless));
- }
- TEST_F(BatteryUpdateMessageHandlerTest, BatteryUpdate_MultipleMessages) {
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kCaseTrueWireless));
- SetMessageStream(kBatteryUpdateBytes1);
- fake_socket_->TriggerReceiveCallback();
- base::RunLoop().RunUntilIdle();
- NotifyMessageStreamConnected(kTestDeviceAddress);
- base::RunLoop().RunUntilIdle();
- EXPECT_TRUE(
- bluetooth_device_
- ->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kLeftBudTrueWireless)
- ->percentage);
- EXPECT_EQ(87,
- bluetooth_device_
- ->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kLeftBudTrueWireless)
- ->percentage.value());
- EXPECT_TRUE(
- bluetooth_device_
- ->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kRightBudTrueWireless)
- ->percentage);
- EXPECT_EQ(65,
- bluetooth_device_
- ->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kRightBudTrueWireless)
- ->percentage.value());
- EXPECT_FALSE(bluetooth_device_
- ->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kCaseTrueWireless)
- ->percentage);
- fake_socket_->SetIOBufferFromBytes(kBatteryUpdateBytes2);
- fake_socket_->TriggerReceiveCallback();
- base::RunLoop().RunUntilIdle();
- EXPECT_TRUE(
- bluetooth_device_
- ->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kLeftBudTrueWireless)
- ->percentage);
- EXPECT_EQ(81,
- bluetooth_device_
- ->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kLeftBudTrueWireless)
- ->percentage.value());
- EXPECT_TRUE(
- bluetooth_device_
- ->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kRightBudTrueWireless)
- ->percentage);
- EXPECT_EQ(56,
- bluetooth_device_
- ->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kRightBudTrueWireless)
- ->percentage.value());
- EXPECT_TRUE(bluetooth_device_
- ->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kCaseTrueWireless)
- ->percentage);
- EXPECT_EQ(56, bluetooth_device_
- ->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kCaseTrueWireless)
- ->percentage.value());
- }
- TEST_F(BatteryUpdateMessageHandlerTest, NoBatteryUpdate_GetMessages) {
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kCaseTrueWireless));
- SetMessageStream(kModelIdBytes);
- fake_socket_->TriggerReceiveCallback();
- base::RunLoop().RunUntilIdle();
- NotifyMessageStreamConnected(kTestDeviceAddress);
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kCaseTrueWireless));
- }
- TEST_F(BatteryUpdateMessageHandlerTest, NoBatteryUpdate_Observation) {
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kCaseTrueWireless));
- fake_socket_->SetIOBufferFromBytes(kModelIdBytes);
- NotifyMessageStreamConnected(kTestDeviceAddress);
- base::RunLoop().RunUntilIdle();
- fake_socket_->TriggerReceiveCallback();
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kCaseTrueWireless));
- }
- TEST_F(BatteryUpdateMessageHandlerTest, MessageStreamRemovedOnDestroyed) {
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kCaseTrueWireless));
- SetMessageStream(kBatteryUpdateBytes1);
- NotifyMessageStreamConnected(kTestDeviceAddress);
- base::RunLoop().RunUntilIdle();
- message_stream_.reset();
- fake_socket_->TriggerReceiveCallback();
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kCaseTrueWireless));
- }
- TEST_F(BatteryUpdateMessageHandlerTest, MessageStreamRemovedOnDisconnect) {
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kCaseTrueWireless));
- fake_socket_->SetErrorReason(
- device::BluetoothSocket::ErrorReason::kDisconnected);
- message_stream_ =
- std::make_unique<MessageStream>(kTestDeviceAddress, fake_socket_.get());
- NotifyMessageStreamConnected(kTestDeviceAddress);
- base::RunLoop().RunUntilIdle();
- fake_socket_->TriggerReceiveCallback();
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kCaseTrueWireless));
- }
- TEST_F(BatteryUpdateMessageHandlerTest,
- MessageStreamRemovedOnDisconnect_MessageStreamDestroted) {
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kCaseTrueWireless));
- fake_socket_->SetErrorReason(
- device::BluetoothSocket::ErrorReason::kDisconnected);
- message_stream_ =
- std::make_unique<MessageStream>(kTestDeviceAddress, fake_socket_.get());
- NotifyMessageStreamConnected(kTestDeviceAddress);
- base::RunLoop().RunUntilIdle();
- fake_socket_->TriggerReceiveCallback();
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kCaseTrueWireless));
- SetMessageStream(kBatteryUpdateBytes1);
- NotifyMessageStreamConnected(kTestDeviceAddress);
- base::RunLoop().RunUntilIdle();
- message_stream_.reset();
- fake_socket_->TriggerReceiveCallback();
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kCaseTrueWireless));
- }
- TEST_F(BatteryUpdateMessageHandlerTest, DeviceLost) {
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device_->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kCaseTrueWireless));
- SetMessageStream(kBatteryUpdateBytes1);
- fake_socket_->TriggerReceiveCallback();
- base::RunLoop().RunUntilIdle();
- auto bluetooth_device = adapter_->RemoveMockDevice(kTestDeviceAddress);
- NotifyMessageStreamConnected(kTestDeviceAddress);
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(absl::nullopt,
- bluetooth_device->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
- EXPECT_EQ(absl::nullopt,
- bluetooth_device->GetBatteryInfo(
- device::BluetoothDevice::BatteryType::kCaseTrueWireless));
- }
- } // namespace quick_pair
- } // namespace ash
|