forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device_notifier_unittest.cc
102 lines (79 loc) · 3.37 KB
/
device_notifier_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2018 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 "services/audio/device_notifier.h"
#include <memory>
#include <utility>
#include "base/system_monitor/system_monitor.h"
#include "base/test/scoped_task_environment.h"
#include "services/audio/public/mojom/device_notifications.mojom.h"
#include "services/audio/traced_service_ref.h"
#include "services/service_manager/public/cpp/service_context_ref.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace audio {
namespace {
class MockDeviceListener : public mojom::DeviceListener {
public:
explicit MockDeviceListener(audio::mojom::DeviceListenerRequest request)
: binding_(this, std::move(request)) {}
MOCK_METHOD0(DevicesChanged, void());
private:
mojo::Binding<audio::mojom::DeviceListener> binding_;
DISALLOW_COPY_AND_ASSIGN(MockDeviceListener);
};
} // namespace
class DeviceNotifierTest : public ::testing::Test {
public:
DeviceNotifierTest()
: system_monitor_(std::make_unique<base::SystemMonitor>()),
service_ref_factory_(
base::BindRepeating(&DeviceNotifierTest::OnNoServiceRefs,
base::Unretained(this))) {}
protected:
MOCK_METHOD0(OnNoServiceRefs, void());
void CreateDeviceNotifier() {
device_notifier_ = std::make_unique<DeviceNotifier>();
device_notifier_->Bind(mojo::MakeRequest(&device_notifier_ptr_),
TracedServiceRef(service_ref_factory_.CreateRef(),
"audio::DeviceNotifier Binding"));
EXPECT_FALSE(service_ref_factory_.HasNoRefs());
}
void DestroyDeviceNotifier() {
device_notifier_ptr_.reset();
scoped_task_environment_.RunUntilIdle();
EXPECT_TRUE(service_ref_factory_.HasNoRefs());
}
base::test::ScopedTaskEnvironment scoped_task_environment_;
mojom::DeviceNotifierPtr device_notifier_ptr_;
private:
std::unique_ptr<base::SystemMonitor> system_monitor_;
std::unique_ptr<DeviceNotifier> device_notifier_;
service_manager::ServiceContextRefFactory service_ref_factory_;
DISALLOW_COPY_AND_ASSIGN(DeviceNotifierTest);
};
TEST_F(DeviceNotifierTest, DeviceNotifierNotifies) {
EXPECT_CALL(*this, OnNoServiceRefs());
CreateDeviceNotifier();
mojom::DeviceListenerPtr device_listener_ptr;
MockDeviceListener listener(mojo::MakeRequest(&device_listener_ptr));
// Simulate audio-device event, but no callback should be invoked before the
// listener is registered.
EXPECT_CALL(listener, DevicesChanged()).Times(0);
base::SystemMonitor::Get()->ProcessDevicesChanged(
base::SystemMonitor::DEVTYPE_AUDIO);
scoped_task_environment_.RunUntilIdle();
// Register the listener and simulate an audio-device event.
device_notifier_ptr_->RegisterListener(std::move(device_listener_ptr));
EXPECT_CALL(listener, DevicesChanged());
base::SystemMonitor::Get()->ProcessDevicesChanged(
base::SystemMonitor::DEVTYPE_AUDIO);
scoped_task_environment_.RunUntilIdle();
// Simulate a video-device event, which should be ignored.
EXPECT_CALL(listener, DevicesChanged()).Times(0);
base::SystemMonitor::Get()->ProcessDevicesChanged(
base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE);
scoped_task_environment_.RunUntilIdle();
DestroyDeviceNotifier();
}
} // namespace audio