battery_update_message_handler_unittest.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. // Copyright 2021 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. #include "ash/quick_pair/keyed_service/battery_update_message_handler.h"
  5. #include <memory>
  6. #include "ash/quick_pair/common/constants.h"
  7. #include "ash/quick_pair/common/device.h"
  8. #include "ash/quick_pair/common/logging.h"
  9. #include "ash/quick_pair/common/protocol.h"
  10. #include "ash/quick_pair/message_stream/fake_bluetooth_socket.h"
  11. #include "ash/quick_pair/message_stream/fake_message_stream_lookup.h"
  12. #include "ash/quick_pair/message_stream/message_stream.h"
  13. #include "ash/quick_pair/message_stream/message_stream_lookup.h"
  14. #include "ash/quick_pair/pairing/mock_pairer_broker.h"
  15. #include "ash/quick_pair/pairing/pairer_broker.h"
  16. #include "ash/services/quick_pair/fast_pair_data_parser.h"
  17. #include "ash/services/quick_pair/mock_quick_pair_process_manager.h"
  18. #include "ash/services/quick_pair/quick_pair_process.h"
  19. #include "ash/services/quick_pair/quick_pair_process_manager.h"
  20. #include "ash/services/quick_pair/quick_pair_process_manager_impl.h"
  21. #include "base/memory/scoped_refptr.h"
  22. #include "base/run_loop.h"
  23. #include "base/test/task_environment.h"
  24. #include "device/bluetooth/bluetooth_adapter_factory.h"
  25. #include "device/bluetooth/test/mock_bluetooth_adapter.h"
  26. #include "device/bluetooth/test/mock_bluetooth_device.h"
  27. #include "mojo/public/cpp/bindings/shared_remote.h"
  28. #include "testing/gtest/include/gtest/gtest.h"
  29. #include "third_party/abseil-cpp/absl/types/optional.h"
  30. namespace {
  31. constexpr char kTestDeviceAddress[] = "11:12:13:14:15:16";
  32. constexpr char kTestBleDeviceName[] = "Test Device Name";
  33. std::vector<uint8_t> kBatteryUpdateBytes1 = {/*mesage_group=*/0x03,
  34. /*mesage_code=*/0x03,
  35. /*additional_data_length=*/0x00,
  36. 0x03,
  37. /*additional_data=*/0x57,
  38. 0x41,
  39. 0x7F};
  40. std::vector<uint8_t> kBatteryUpdateBytes2 = {/*mesage_group=*/0x03,
  41. /*mesage_code=*/0x03,
  42. /*additional_data_length=*/0x00,
  43. 0x03,
  44. /*additional_data=*/0x51,
  45. 0x38,
  46. 0x38};
  47. const std::vector<uint8_t> kModelIdBytes = {
  48. /*message_group=*/0x03,
  49. /*message_code=*/0x01,
  50. /*additional_data_length=*/0x00, 0x03,
  51. /*additional_data=*/0xAA, 0xBB, 0xCC};
  52. std::unique_ptr<testing::NiceMock<device::MockBluetoothDevice>>
  53. CreateTestBluetoothDevice(std::string address,
  54. device::MockBluetoothAdapter* adapter) {
  55. return std::make_unique<testing::NiceMock<device::MockBluetoothDevice>>(
  56. /*adapter=*/adapter, /*bluetooth_class=*/0, kTestBleDeviceName, address,
  57. /*paired=*/true, /*connected=*/false);
  58. }
  59. } // namespace
  60. namespace ash {
  61. namespace quick_pair {
  62. class BatteryUpdateMessageFakeBluetoothAdapter
  63. : public testing::NiceMock<device::MockBluetoothAdapter> {
  64. public:
  65. device::BluetoothDevice* GetDevice(const std::string& address) override {
  66. for (const auto& it : mock_devices_) {
  67. if (it->GetAddress() == address)
  68. return it.get();
  69. }
  70. return nullptr;
  71. }
  72. private:
  73. ~BatteryUpdateMessageFakeBluetoothAdapter() = default;
  74. };
  75. class BatteryUpdateMessageHandlerTest : public testing::Test {
  76. public:
  77. void SetUp() override {
  78. adapter_ = base::MakeRefCounted<BatteryUpdateMessageFakeBluetoothAdapter>();
  79. std::unique_ptr<testing::NiceMock<device::MockBluetoothDevice>>
  80. bluetooth_device =
  81. CreateTestBluetoothDevice(kTestDeviceAddress, adapter_.get());
  82. device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_);
  83. bluetooth_device_ = bluetooth_device.get();
  84. adapter_->AddMockDevice(std::move(bluetooth_device));
  85. message_stream_lookup_ = std::make_unique<FakeMessageStreamLookup>();
  86. fake_message_stream_lookup_ =
  87. static_cast<FakeMessageStreamLookup*>(message_stream_lookup_.get());
  88. message_stream_ =
  89. std::make_unique<MessageStream>(kTestDeviceAddress, fake_socket_.get());
  90. process_manager_ = std::make_unique<MockQuickPairProcessManager>();
  91. quick_pair_process::SetProcessManager(process_manager_.get());
  92. data_parser_ = std::make_unique<FastPairDataParser>(
  93. fast_pair_data_parser_.InitWithNewPipeAndPassReceiver());
  94. data_parser_remote_.Bind(std::move(fast_pair_data_parser_),
  95. task_environment_.GetMainThreadTaskRunner());
  96. EXPECT_CALL(*mock_process_manager(), GetProcessReference)
  97. .WillRepeatedly([&](QuickPairProcessManager::ProcessStoppedCallback) {
  98. return std::make_unique<
  99. QuickPairProcessManagerImpl::ProcessReferenceImpl>(
  100. data_parser_remote_, base::DoNothing());
  101. });
  102. battery_update_message_handler_ =
  103. std::make_unique<BatteryUpdateMessageHandler>(
  104. message_stream_lookup_.get());
  105. }
  106. void TearDown() override {
  107. fake_message_stream_lookup_->RemoveMessageStream(kTestDeviceAddress);
  108. battery_update_message_handler_.reset();
  109. }
  110. MockQuickPairProcessManager* mock_process_manager() {
  111. return static_cast<MockQuickPairProcessManager*>(process_manager_.get());
  112. }
  113. void SetMessageStream(const std::vector<uint8_t>& message_bytes) {
  114. fake_socket_->SetIOBufferFromBytes(message_bytes);
  115. message_stream_ =
  116. std::make_unique<MessageStream>(kTestDeviceAddress, fake_socket_.get());
  117. }
  118. void AddMessageStream(const std::vector<uint8_t>& message_bytes) {
  119. fake_socket_->SetIOBufferFromBytes(message_bytes);
  120. message_stream_ =
  121. std::make_unique<MessageStream>(kTestDeviceAddress, fake_socket_.get());
  122. fake_message_stream_lookup_->AddMessageStream(kTestDeviceAddress,
  123. message_stream_.get());
  124. }
  125. void NotifyMessageStreamConnected(std::string device_address) {
  126. fake_message_stream_lookup_->NotifyMessageStreamConnected(
  127. device_address, message_stream_.get());
  128. }
  129. protected:
  130. base::test::TaskEnvironment task_environment_{
  131. base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  132. scoped_refptr<BatteryUpdateMessageFakeBluetoothAdapter> adapter_;
  133. scoped_refptr<FakeBluetoothSocket> fake_socket_ =
  134. base::MakeRefCounted<FakeBluetoothSocket>();
  135. std::unique_ptr<MessageStream> message_stream_;
  136. std::unique_ptr<MessageStreamLookup> message_stream_lookup_;
  137. FakeMessageStreamLookup* fake_message_stream_lookup_ = nullptr;
  138. mojo::SharedRemote<mojom::FastPairDataParser> data_parser_remote_;
  139. mojo::PendingRemote<mojom::FastPairDataParser> fast_pair_data_parser_;
  140. std::unique_ptr<FastPairDataParser> data_parser_;
  141. std::unique_ptr<QuickPairProcessManager> process_manager_;
  142. device::BluetoothDevice* bluetooth_device_ = nullptr;
  143. std::unique_ptr<BatteryUpdateMessageHandler> battery_update_message_handler_;
  144. };
  145. TEST_F(BatteryUpdateMessageHandlerTest, BatteryUpdate_GetMessages) {
  146. EXPECT_EQ(absl::nullopt,
  147. bluetooth_device_->GetBatteryInfo(
  148. device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
  149. EXPECT_EQ(absl::nullopt,
  150. bluetooth_device_->GetBatteryInfo(
  151. device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
  152. EXPECT_EQ(absl::nullopt,
  153. bluetooth_device_->GetBatteryInfo(
  154. device::BluetoothDevice::BatteryType::kCaseTrueWireless));
  155. SetMessageStream(kBatteryUpdateBytes1);
  156. fake_socket_->TriggerReceiveCallback();
  157. base::RunLoop().RunUntilIdle();
  158. NotifyMessageStreamConnected(kTestDeviceAddress);
  159. base::RunLoop().RunUntilIdle();
  160. EXPECT_NE(absl::nullopt,
  161. bluetooth_device_->GetBatteryInfo(
  162. device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
  163. EXPECT_NE(absl::nullopt,
  164. bluetooth_device_->GetBatteryInfo(
  165. device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
  166. EXPECT_NE(absl::nullopt,
  167. bluetooth_device_->GetBatteryInfo(
  168. device::BluetoothDevice::BatteryType::kCaseTrueWireless));
  169. }
  170. TEST_F(BatteryUpdateMessageHandlerTest, BatteryUpdate_Observation) {
  171. EXPECT_EQ(absl::nullopt,
  172. bluetooth_device_->GetBatteryInfo(
  173. device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
  174. EXPECT_EQ(absl::nullopt,
  175. bluetooth_device_->GetBatteryInfo(
  176. device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
  177. EXPECT_EQ(absl::nullopt,
  178. bluetooth_device_->GetBatteryInfo(
  179. device::BluetoothDevice::BatteryType::kCaseTrueWireless));
  180. fake_socket_->SetIOBufferFromBytes(kBatteryUpdateBytes1);
  181. NotifyMessageStreamConnected(kTestDeviceAddress);
  182. base::RunLoop().RunUntilIdle();
  183. fake_socket_->TriggerReceiveCallback();
  184. base::RunLoop().RunUntilIdle();
  185. EXPECT_NE(absl::nullopt,
  186. bluetooth_device_->GetBatteryInfo(
  187. device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
  188. EXPECT_NE(absl::nullopt,
  189. bluetooth_device_->GetBatteryInfo(
  190. device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
  191. EXPECT_NE(absl::nullopt,
  192. bluetooth_device_->GetBatteryInfo(
  193. device::BluetoothDevice::BatteryType::kCaseTrueWireless));
  194. }
  195. TEST_F(BatteryUpdateMessageHandlerTest, BatteryUpdate_MultipleMessages) {
  196. EXPECT_EQ(absl::nullopt,
  197. bluetooth_device_->GetBatteryInfo(
  198. device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
  199. EXPECT_EQ(absl::nullopt,
  200. bluetooth_device_->GetBatteryInfo(
  201. device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
  202. EXPECT_EQ(absl::nullopt,
  203. bluetooth_device_->GetBatteryInfo(
  204. device::BluetoothDevice::BatteryType::kCaseTrueWireless));
  205. SetMessageStream(kBatteryUpdateBytes1);
  206. fake_socket_->TriggerReceiveCallback();
  207. base::RunLoop().RunUntilIdle();
  208. NotifyMessageStreamConnected(kTestDeviceAddress);
  209. base::RunLoop().RunUntilIdle();
  210. EXPECT_TRUE(
  211. bluetooth_device_
  212. ->GetBatteryInfo(
  213. device::BluetoothDevice::BatteryType::kLeftBudTrueWireless)
  214. ->percentage);
  215. EXPECT_EQ(87,
  216. bluetooth_device_
  217. ->GetBatteryInfo(
  218. device::BluetoothDevice::BatteryType::kLeftBudTrueWireless)
  219. ->percentage.value());
  220. EXPECT_TRUE(
  221. bluetooth_device_
  222. ->GetBatteryInfo(
  223. device::BluetoothDevice::BatteryType::kRightBudTrueWireless)
  224. ->percentage);
  225. EXPECT_EQ(65,
  226. bluetooth_device_
  227. ->GetBatteryInfo(
  228. device::BluetoothDevice::BatteryType::kRightBudTrueWireless)
  229. ->percentage.value());
  230. EXPECT_FALSE(bluetooth_device_
  231. ->GetBatteryInfo(
  232. device::BluetoothDevice::BatteryType::kCaseTrueWireless)
  233. ->percentage);
  234. fake_socket_->SetIOBufferFromBytes(kBatteryUpdateBytes2);
  235. fake_socket_->TriggerReceiveCallback();
  236. base::RunLoop().RunUntilIdle();
  237. EXPECT_TRUE(
  238. bluetooth_device_
  239. ->GetBatteryInfo(
  240. device::BluetoothDevice::BatteryType::kLeftBudTrueWireless)
  241. ->percentage);
  242. EXPECT_EQ(81,
  243. bluetooth_device_
  244. ->GetBatteryInfo(
  245. device::BluetoothDevice::BatteryType::kLeftBudTrueWireless)
  246. ->percentage.value());
  247. EXPECT_TRUE(
  248. bluetooth_device_
  249. ->GetBatteryInfo(
  250. device::BluetoothDevice::BatteryType::kRightBudTrueWireless)
  251. ->percentage);
  252. EXPECT_EQ(56,
  253. bluetooth_device_
  254. ->GetBatteryInfo(
  255. device::BluetoothDevice::BatteryType::kRightBudTrueWireless)
  256. ->percentage.value());
  257. EXPECT_TRUE(bluetooth_device_
  258. ->GetBatteryInfo(
  259. device::BluetoothDevice::BatteryType::kCaseTrueWireless)
  260. ->percentage);
  261. EXPECT_EQ(56, bluetooth_device_
  262. ->GetBatteryInfo(
  263. device::BluetoothDevice::BatteryType::kCaseTrueWireless)
  264. ->percentage.value());
  265. }
  266. TEST_F(BatteryUpdateMessageHandlerTest, NoBatteryUpdate_GetMessages) {
  267. EXPECT_EQ(absl::nullopt,
  268. bluetooth_device_->GetBatteryInfo(
  269. device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
  270. EXPECT_EQ(absl::nullopt,
  271. bluetooth_device_->GetBatteryInfo(
  272. device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
  273. EXPECT_EQ(absl::nullopt,
  274. bluetooth_device_->GetBatteryInfo(
  275. device::BluetoothDevice::BatteryType::kCaseTrueWireless));
  276. SetMessageStream(kModelIdBytes);
  277. fake_socket_->TriggerReceiveCallback();
  278. base::RunLoop().RunUntilIdle();
  279. NotifyMessageStreamConnected(kTestDeviceAddress);
  280. base::RunLoop().RunUntilIdle();
  281. EXPECT_EQ(absl::nullopt,
  282. bluetooth_device_->GetBatteryInfo(
  283. device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
  284. EXPECT_EQ(absl::nullopt,
  285. bluetooth_device_->GetBatteryInfo(
  286. device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
  287. EXPECT_EQ(absl::nullopt,
  288. bluetooth_device_->GetBatteryInfo(
  289. device::BluetoothDevice::BatteryType::kCaseTrueWireless));
  290. }
  291. TEST_F(BatteryUpdateMessageHandlerTest, NoBatteryUpdate_Observation) {
  292. EXPECT_EQ(absl::nullopt,
  293. bluetooth_device_->GetBatteryInfo(
  294. device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
  295. EXPECT_EQ(absl::nullopt,
  296. bluetooth_device_->GetBatteryInfo(
  297. device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
  298. EXPECT_EQ(absl::nullopt,
  299. bluetooth_device_->GetBatteryInfo(
  300. device::BluetoothDevice::BatteryType::kCaseTrueWireless));
  301. fake_socket_->SetIOBufferFromBytes(kModelIdBytes);
  302. NotifyMessageStreamConnected(kTestDeviceAddress);
  303. base::RunLoop().RunUntilIdle();
  304. fake_socket_->TriggerReceiveCallback();
  305. base::RunLoop().RunUntilIdle();
  306. EXPECT_EQ(absl::nullopt,
  307. bluetooth_device_->GetBatteryInfo(
  308. device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
  309. EXPECT_EQ(absl::nullopt,
  310. bluetooth_device_->GetBatteryInfo(
  311. device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
  312. EXPECT_EQ(absl::nullopt,
  313. bluetooth_device_->GetBatteryInfo(
  314. device::BluetoothDevice::BatteryType::kCaseTrueWireless));
  315. }
  316. TEST_F(BatteryUpdateMessageHandlerTest, MessageStreamRemovedOnDestroyed) {
  317. EXPECT_EQ(absl::nullopt,
  318. bluetooth_device_->GetBatteryInfo(
  319. device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
  320. EXPECT_EQ(absl::nullopt,
  321. bluetooth_device_->GetBatteryInfo(
  322. device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
  323. EXPECT_EQ(absl::nullopt,
  324. bluetooth_device_->GetBatteryInfo(
  325. device::BluetoothDevice::BatteryType::kCaseTrueWireless));
  326. SetMessageStream(kBatteryUpdateBytes1);
  327. NotifyMessageStreamConnected(kTestDeviceAddress);
  328. base::RunLoop().RunUntilIdle();
  329. message_stream_.reset();
  330. fake_socket_->TriggerReceiveCallback();
  331. base::RunLoop().RunUntilIdle();
  332. EXPECT_EQ(absl::nullopt,
  333. bluetooth_device_->GetBatteryInfo(
  334. device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
  335. EXPECT_EQ(absl::nullopt,
  336. bluetooth_device_->GetBatteryInfo(
  337. device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
  338. EXPECT_EQ(absl::nullopt,
  339. bluetooth_device_->GetBatteryInfo(
  340. device::BluetoothDevice::BatteryType::kCaseTrueWireless));
  341. }
  342. TEST_F(BatteryUpdateMessageHandlerTest, MessageStreamRemovedOnDisconnect) {
  343. EXPECT_EQ(absl::nullopt,
  344. bluetooth_device_->GetBatteryInfo(
  345. device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
  346. EXPECT_EQ(absl::nullopt,
  347. bluetooth_device_->GetBatteryInfo(
  348. device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
  349. EXPECT_EQ(absl::nullopt,
  350. bluetooth_device_->GetBatteryInfo(
  351. device::BluetoothDevice::BatteryType::kCaseTrueWireless));
  352. fake_socket_->SetErrorReason(
  353. device::BluetoothSocket::ErrorReason::kDisconnected);
  354. message_stream_ =
  355. std::make_unique<MessageStream>(kTestDeviceAddress, fake_socket_.get());
  356. NotifyMessageStreamConnected(kTestDeviceAddress);
  357. base::RunLoop().RunUntilIdle();
  358. fake_socket_->TriggerReceiveCallback();
  359. base::RunLoop().RunUntilIdle();
  360. EXPECT_EQ(absl::nullopt,
  361. bluetooth_device_->GetBatteryInfo(
  362. device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
  363. EXPECT_EQ(absl::nullopt,
  364. bluetooth_device_->GetBatteryInfo(
  365. device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
  366. EXPECT_EQ(absl::nullopt,
  367. bluetooth_device_->GetBatteryInfo(
  368. device::BluetoothDevice::BatteryType::kCaseTrueWireless));
  369. }
  370. TEST_F(BatteryUpdateMessageHandlerTest,
  371. MessageStreamRemovedOnDisconnect_MessageStreamDestroted) {
  372. EXPECT_EQ(absl::nullopt,
  373. bluetooth_device_->GetBatteryInfo(
  374. device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
  375. EXPECT_EQ(absl::nullopt,
  376. bluetooth_device_->GetBatteryInfo(
  377. device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
  378. EXPECT_EQ(absl::nullopt,
  379. bluetooth_device_->GetBatteryInfo(
  380. device::BluetoothDevice::BatteryType::kCaseTrueWireless));
  381. fake_socket_->SetErrorReason(
  382. device::BluetoothSocket::ErrorReason::kDisconnected);
  383. message_stream_ =
  384. std::make_unique<MessageStream>(kTestDeviceAddress, fake_socket_.get());
  385. NotifyMessageStreamConnected(kTestDeviceAddress);
  386. base::RunLoop().RunUntilIdle();
  387. fake_socket_->TriggerReceiveCallback();
  388. base::RunLoop().RunUntilIdle();
  389. EXPECT_EQ(absl::nullopt,
  390. bluetooth_device_->GetBatteryInfo(
  391. device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
  392. EXPECT_EQ(absl::nullopt,
  393. bluetooth_device_->GetBatteryInfo(
  394. device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
  395. EXPECT_EQ(absl::nullopt,
  396. bluetooth_device_->GetBatteryInfo(
  397. device::BluetoothDevice::BatteryType::kCaseTrueWireless));
  398. SetMessageStream(kBatteryUpdateBytes1);
  399. NotifyMessageStreamConnected(kTestDeviceAddress);
  400. base::RunLoop().RunUntilIdle();
  401. message_stream_.reset();
  402. fake_socket_->TriggerReceiveCallback();
  403. base::RunLoop().RunUntilIdle();
  404. EXPECT_EQ(absl::nullopt,
  405. bluetooth_device_->GetBatteryInfo(
  406. device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
  407. EXPECT_EQ(absl::nullopt,
  408. bluetooth_device_->GetBatteryInfo(
  409. device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
  410. EXPECT_EQ(absl::nullopt,
  411. bluetooth_device_->GetBatteryInfo(
  412. device::BluetoothDevice::BatteryType::kCaseTrueWireless));
  413. }
  414. TEST_F(BatteryUpdateMessageHandlerTest, DeviceLost) {
  415. EXPECT_EQ(absl::nullopt,
  416. bluetooth_device_->GetBatteryInfo(
  417. device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
  418. EXPECT_EQ(absl::nullopt,
  419. bluetooth_device_->GetBatteryInfo(
  420. device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
  421. EXPECT_EQ(absl::nullopt,
  422. bluetooth_device_->GetBatteryInfo(
  423. device::BluetoothDevice::BatteryType::kCaseTrueWireless));
  424. SetMessageStream(kBatteryUpdateBytes1);
  425. fake_socket_->TriggerReceiveCallback();
  426. base::RunLoop().RunUntilIdle();
  427. auto bluetooth_device = adapter_->RemoveMockDevice(kTestDeviceAddress);
  428. NotifyMessageStreamConnected(kTestDeviceAddress);
  429. base::RunLoop().RunUntilIdle();
  430. EXPECT_EQ(absl::nullopt,
  431. bluetooth_device->GetBatteryInfo(
  432. device::BluetoothDevice::BatteryType::kLeftBudTrueWireless));
  433. EXPECT_EQ(absl::nullopt,
  434. bluetooth_device->GetBatteryInfo(
  435. device::BluetoothDevice::BatteryType::kRightBudTrueWireless));
  436. EXPECT_EQ(absl::nullopt,
  437. bluetooth_device->GetBatteryInfo(
  438. device::BluetoothDevice::BatteryType::kCaseTrueWireless));
  439. }
  440. } // namespace quick_pair
  441. } // namespace ash