bluetooth_device_status_ui_handler_unittest.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/system/bluetooth/bluetooth_device_status_ui_handler.h"
  5. #include "ash/constants/ash_features.h"
  6. #include "ash/public/cpp/system/toast_manager.h"
  7. #include "ash/strings/grit/ash_strings.h"
  8. #include "ash/test/ash_test_base.h"
  9. #include "ash/test/ash_test_helper.h"
  10. #include "base/test/scoped_feature_list.h"
  11. #include "chromeos/services/bluetooth_config/fake_bluetooth_device_status_notifier.h"
  12. #include "chromeos/services/bluetooth_config/public/mojom/cros_bluetooth_config.mojom.h"
  13. #include "testing/gmock/include/gmock/gmock.h"
  14. using chromeos::bluetooth_config::mojom::BatteryProperties;
  15. using chromeos::bluetooth_config::mojom::BluetoothDeviceProperties;
  16. using chromeos::bluetooth_config::mojom::DeviceBatteryInfo;
  17. using chromeos::bluetooth_config::mojom::DeviceBatteryInfoPtr;
  18. using chromeos::bluetooth_config::mojom::DeviceConnectionState;
  19. using chromeos::bluetooth_config::mojom::PairedBluetoothDeviceProperties;
  20. using chromeos::bluetooth_config::mojom::PairedBluetoothDevicePropertiesPtr;
  21. using testing::NiceMock;
  22. namespace ash {
  23. namespace {
  24. class MockBluetoothDeviceStatusUiHandler
  25. : public BluetoothDeviceStatusUiHandler {
  26. public:
  27. MOCK_METHOD(void, ShowToast, (const ash::ToastData& toast_data), (override));
  28. };
  29. } // namespace
  30. class BluetoothDeviceStatusUiHandlerTest : public AshTestBase {
  31. public:
  32. void SetUp() override {
  33. AshTestBase::SetUp();
  34. feature_list_.InitAndEnableFeature(features::kBluetoothRevamp);
  35. device_status_ui_handler_ =
  36. std::make_unique<NiceMock<MockBluetoothDeviceStatusUiHandler>>();
  37. base::RunLoop().RunUntilIdle();
  38. }
  39. MockBluetoothDeviceStatusUiHandler& device_status_ui_handler() {
  40. return *device_status_ui_handler_;
  41. }
  42. void SetPairedDevice(PairedBluetoothDevicePropertiesPtr paired_device) {
  43. fake_device_status_notifier()->SetNewlyPairedDevice(
  44. std::move(paired_device));
  45. base::RunLoop().RunUntilIdle();
  46. }
  47. void SetConnectedDevice(PairedBluetoothDevicePropertiesPtr paired_device) {
  48. fake_device_status_notifier()->SetConnectedDevice(std::move(paired_device));
  49. base::RunLoop().RunUntilIdle();
  50. }
  51. void SetDisconnectedDevice(PairedBluetoothDevicePropertiesPtr paired_device) {
  52. fake_device_status_notifier()->SetDisconnectedDevice(
  53. std::move(paired_device));
  54. base::RunLoop().RunUntilIdle();
  55. }
  56. PairedBluetoothDevicePropertiesPtr GetPairedDevice() {
  57. auto paired_device = PairedBluetoothDeviceProperties::New();
  58. paired_device->nickname = "Beats X";
  59. paired_device->device_properties = BluetoothDeviceProperties::New();
  60. return paired_device;
  61. }
  62. private:
  63. chromeos::bluetooth_config::FakeBluetoothDeviceStatusNotifier*
  64. fake_device_status_notifier() {
  65. return ash_test_helper()
  66. ->bluetooth_config_test_helper()
  67. ->fake_bluetooth_device_status_notifier();
  68. }
  69. std::unique_ptr<MockBluetoothDeviceStatusUiHandler> device_status_ui_handler_;
  70. base::test::ScopedFeatureList feature_list_;
  71. };
  72. TEST_F(BluetoothDeviceStatusUiHandlerTest, PairedDevice) {
  73. EXPECT_CALL(device_status_ui_handler(), ShowToast);
  74. SetPairedDevice(GetPairedDevice());
  75. }
  76. TEST_F(BluetoothDeviceStatusUiHandlerTest, ConnectedDevice) {
  77. EXPECT_CALL(device_status_ui_handler(), ShowToast);
  78. SetConnectedDevice(GetPairedDevice());
  79. }
  80. TEST_F(BluetoothDeviceStatusUiHandlerTest, DisconnectedDevice) {
  81. EXPECT_CALL(device_status_ui_handler(), ShowToast);
  82. SetDisconnectedDevice(GetPairedDevice());
  83. }
  84. } // namespace ash