blob: bd39baac1b2a6c68fa47c0da7d2e81a4425db7ff [file] [log] [blame]
solenberg566ef242015-11-06 15:34:49 -08001/*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
kwibergfffa42b2016-02-23 10:46:32 -080011#include <memory>
12
solenberg566ef242015-11-06 15:34:49 -080013#include "webrtc/audio/audio_state.h"
kwibergac9f8762016-09-30 22:29:43 -070014#include "webrtc/test/gtest.h"
solenberg566ef242015-11-06 15:34:49 -080015#include "webrtc/test/mock_voice_engine.h"
16
17namespace webrtc {
18namespace test {
19namespace {
20
21struct ConfigHelper {
22 ConfigHelper() {
23 EXPECT_CALL(voice_engine_,
24 RegisterVoiceEngineObserver(testing::_)).WillOnce(testing::Return(0));
25 EXPECT_CALL(voice_engine_,
26 DeRegisterVoiceEngineObserver()).WillOnce(testing::Return(0));
aleloidd310712016-11-17 06:28:59 -080027 EXPECT_CALL(voice_engine_, audio_device_module());
28 EXPECT_CALL(voice_engine_, audio_processing());
29 EXPECT_CALL(voice_engine_, audio_transport());
30
solenberg566ef242015-11-06 15:34:49 -080031 config_.voice_engine = &voice_engine_;
32 }
33 AudioState::Config& config() { return config_; }
34 MockVoiceEngine& voice_engine() { return voice_engine_; }
35
36 private:
solenberg3a941542015-11-16 07:34:50 -080037 testing::StrictMock<MockVoiceEngine> voice_engine_;
solenberg566ef242015-11-06 15:34:49 -080038 AudioState::Config config_;
39};
40} // namespace
41
42TEST(AudioStateTest, Create) {
43 ConfigHelper helper;
44 rtc::scoped_refptr<AudioState> audio_state =
45 AudioState::Create(helper.config());
46 EXPECT_TRUE(audio_state.get());
47}
48
49TEST(AudioStateTest, ConstructDestruct) {
50 ConfigHelper helper;
kwibergfffa42b2016-02-23 10:46:32 -080051 std::unique_ptr<internal::AudioState> audio_state(
solenberg566ef242015-11-06 15:34:49 -080052 new internal::AudioState(helper.config()));
53}
54
55TEST(AudioStateTest, GetVoiceEngine) {
56 ConfigHelper helper;
kwibergfffa42b2016-02-23 10:46:32 -080057 std::unique_ptr<internal::AudioState> audio_state(
solenberg566ef242015-11-06 15:34:49 -080058 new internal::AudioState(helper.config()));
59 EXPECT_EQ(audio_state->voice_engine(), &helper.voice_engine());
60}
61
62TEST(AudioStateTest, TypingNoiseDetected) {
63 ConfigHelper helper;
kwibergfffa42b2016-02-23 10:46:32 -080064 std::unique_ptr<internal::AudioState> audio_state(
solenberg566ef242015-11-06 15:34:49 -080065 new internal::AudioState(helper.config()));
66 VoiceEngineObserver* voe_observer =
67 static_cast<VoiceEngineObserver*>(audio_state.get());
68 EXPECT_FALSE(audio_state->typing_noise_detected());
69
70 voe_observer->CallbackOnError(-1, VE_NOT_INITED);
71 EXPECT_FALSE(audio_state->typing_noise_detected());
72
73 voe_observer->CallbackOnError(-1, VE_TYPING_NOISE_WARNING);
74 EXPECT_TRUE(audio_state->typing_noise_detected());
75 voe_observer->CallbackOnError(-1, VE_NOT_INITED);
76 EXPECT_TRUE(audio_state->typing_noise_detected());
77
78 voe_observer->CallbackOnError(-1, VE_TYPING_NOISE_OFF_WARNING);
79 EXPECT_FALSE(audio_state->typing_noise_detected());
80 voe_observer->CallbackOnError(-1, VE_NOT_INITED);
81 EXPECT_FALSE(audio_state->typing_noise_detected());
82}
aleloidd310712016-11-17 06:28:59 -080083
84// Test that RecordedDataIsAvailable calls get to the original transport.
85TEST(AudioStateTest, RecordedAudioArrivesAtOriginalTransport) {
86 using testing::_;
87
88 ConfigHelper helper;
89 auto& voice_engine = helper.voice_engine();
90 auto device =
91 static_cast<MockAudioDeviceModule*>(voice_engine.audio_device_module());
92
93 AudioTransport* audio_transport_proxy = nullptr;
94 ON_CALL(*device, RegisterAudioCallback(_))
95 .WillByDefault(
96 testing::Invoke([&audio_transport_proxy](AudioTransport* transport) {
97 audio_transport_proxy = transport;
98 return 0;
99 }));
100
101 MockAudioTransport original_audio_transport;
102 ON_CALL(voice_engine, audio_transport())
103 .WillByDefault(testing::Return(&original_audio_transport));
104
105 EXPECT_CALL(voice_engine, audio_device_module());
106 std::unique_ptr<internal::AudioState> audio_state(
107 new internal::AudioState(helper.config()));
108
109 // Setup completed. Ensure call of old transport is forwarded to new.
110 uint32_t new_mic_level;
111 EXPECT_CALL(original_audio_transport,
112 RecordedDataIsAvailable(nullptr, 80, 2, 1, 8000, 0, 0, 0, false,
113 testing::Ref(new_mic_level)));
114
115 audio_transport_proxy->RecordedDataIsAvailable(nullptr, 80, 2, 1, 8000, 0, 0,
116 0, false, new_mic_level);
117}
solenberg566ef242015-11-06 15:34:49 -0800118} // namespace test
119} // namespace webrtc