device_notifier_unittest.cc 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2018 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 "services/audio/device_notifier.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/system/system_monitor.h"
  8. #include "base/test/task_environment.h"
  9. #include "mojo/public/cpp/bindings/pending_receiver.h"
  10. #include "mojo/public/cpp/bindings/receiver.h"
  11. #include "mojo/public/cpp/bindings/remote.h"
  12. #include "services/audio/public/mojom/device_notifications.mojom.h"
  13. #include "testing/gmock/include/gmock/gmock.h"
  14. #include "testing/gtest/include/gtest/gtest.h"
  15. namespace audio {
  16. namespace {
  17. class MockDeviceListener : public mojom::DeviceListener {
  18. public:
  19. explicit MockDeviceListener(
  20. mojo::PendingReceiver<audio::mojom::DeviceListener> receiver)
  21. : receiver_(this, std::move(receiver)) {}
  22. MockDeviceListener(const MockDeviceListener&) = delete;
  23. MockDeviceListener& operator=(const MockDeviceListener&) = delete;
  24. MOCK_METHOD0(DevicesChanged, void());
  25. private:
  26. mojo::Receiver<audio::mojom::DeviceListener> receiver_;
  27. };
  28. } // namespace
  29. class DeviceNotifierTest : public ::testing::Test {
  30. public:
  31. DeviceNotifierTest()
  32. : system_monitor_(std::make_unique<base::SystemMonitor>()) {}
  33. DeviceNotifierTest(const DeviceNotifierTest&) = delete;
  34. DeviceNotifierTest& operator=(const DeviceNotifierTest&) = delete;
  35. protected:
  36. void CreateDeviceNotifier() {
  37. device_notifier_ = std::make_unique<DeviceNotifier>();
  38. device_notifier_->Bind(
  39. remote_device_notifier_.BindNewPipeAndPassReceiver());
  40. }
  41. void DestroyDeviceNotifier() {
  42. remote_device_notifier_.reset();
  43. task_environment_.RunUntilIdle();
  44. }
  45. base::test::TaskEnvironment task_environment_;
  46. mojo::Remote<mojom::DeviceNotifier> remote_device_notifier_;
  47. private:
  48. std::unique_ptr<base::SystemMonitor> system_monitor_;
  49. std::unique_ptr<DeviceNotifier> device_notifier_;
  50. };
  51. TEST_F(DeviceNotifierTest, DeviceNotifierNotifies) {
  52. CreateDeviceNotifier();
  53. mojo::PendingRemote<mojom::DeviceListener> remote_device_listener;
  54. MockDeviceListener listener(
  55. remote_device_listener.InitWithNewPipeAndPassReceiver());
  56. // Simulate audio-device event, but no callback should be invoked before the
  57. // listener is registered.
  58. EXPECT_CALL(listener, DevicesChanged()).Times(0);
  59. base::SystemMonitor::Get()->ProcessDevicesChanged(
  60. base::SystemMonitor::DEVTYPE_AUDIO);
  61. task_environment_.RunUntilIdle();
  62. // Register the listener and simulate an audio-device event.
  63. remote_device_notifier_->RegisterListener(std::move(remote_device_listener));
  64. EXPECT_CALL(listener, DevicesChanged());
  65. base::SystemMonitor::Get()->ProcessDevicesChanged(
  66. base::SystemMonitor::DEVTYPE_AUDIO);
  67. task_environment_.RunUntilIdle();
  68. // Simulate a video-device event, which should be ignored.
  69. EXPECT_CALL(listener, DevicesChanged()).Times(0);
  70. base::SystemMonitor::Get()->ProcessDevicesChanged(
  71. base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE);
  72. task_environment_.RunUntilIdle();
  73. DestroyDeviceNotifier();
  74. }
  75. } // namespace audio