blob: 38485a88bf14665ce6b15d9a149cefec6513751f [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"
aleloi10111bc2016-11-17 06:48:48 -080014#include "webrtc/modules/audio_mixer/audio_mixer_impl.h"
kwibergac9f8762016-09-30 22:29:43 -070015#include "webrtc/test/gtest.h"
solenberg566ef242015-11-06 15:34:49 -080016#include "webrtc/test/mock_voice_engine.h"
17
18namespace webrtc {
19namespace test {
20namespace {
21
22struct ConfigHelper {
23 ConfigHelper() {
24 EXPECT_CALL(voice_engine_,
25 RegisterVoiceEngineObserver(testing::_)).WillOnce(testing::Return(0));
26 EXPECT_CALL(voice_engine_,
27 DeRegisterVoiceEngineObserver()).WillOnce(testing::Return(0));
aleloidd310712016-11-17 06:28:59 -080028 EXPECT_CALL(voice_engine_, audio_device_module());
29 EXPECT_CALL(voice_engine_, audio_processing());
30 EXPECT_CALL(voice_engine_, audio_transport());
31
solenberg566ef242015-11-06 15:34:49 -080032 config_.voice_engine = &voice_engine_;
aleloi10111bc2016-11-17 06:48:48 -080033 config_.audio_mixer = AudioMixerImpl::Create();
solenberg566ef242015-11-06 15:34:49 -080034 }
35 AudioState::Config& config() { return config_; }
36 MockVoiceEngine& voice_engine() { return voice_engine_; }
37
38 private:
solenberg3a941542015-11-16 07:34:50 -080039 testing::StrictMock<MockVoiceEngine> voice_engine_;
solenberg566ef242015-11-06 15:34:49 -080040 AudioState::Config config_;
41};
42} // namespace
43
44TEST(AudioStateTest, Create) {
45 ConfigHelper helper;
46 rtc::scoped_refptr<AudioState> audio_state =
47 AudioState::Create(helper.config());
48 EXPECT_TRUE(audio_state.get());
49}
50
51TEST(AudioStateTest, ConstructDestruct) {
52 ConfigHelper helper;
kwibergfffa42b2016-02-23 10:46:32 -080053 std::unique_ptr<internal::AudioState> audio_state(
solenberg566ef242015-11-06 15:34:49 -080054 new internal::AudioState(helper.config()));
55}
56
57TEST(AudioStateTest, GetVoiceEngine) {
58 ConfigHelper helper;
kwibergfffa42b2016-02-23 10:46:32 -080059 std::unique_ptr<internal::AudioState> audio_state(
solenberg566ef242015-11-06 15:34:49 -080060 new internal::AudioState(helper.config()));
61 EXPECT_EQ(audio_state->voice_engine(), &helper.voice_engine());
62}
63
64TEST(AudioStateTest, TypingNoiseDetected) {
65 ConfigHelper helper;
kwibergfffa42b2016-02-23 10:46:32 -080066 std::unique_ptr<internal::AudioState> audio_state(
solenberg566ef242015-11-06 15:34:49 -080067 new internal::AudioState(helper.config()));
68 VoiceEngineObserver* voe_observer =
69 static_cast<VoiceEngineObserver*>(audio_state.get());
70 EXPECT_FALSE(audio_state->typing_noise_detected());
71
72 voe_observer->CallbackOnError(-1, VE_NOT_INITED);
73 EXPECT_FALSE(audio_state->typing_noise_detected());
74
75 voe_observer->CallbackOnError(-1, VE_TYPING_NOISE_WARNING);
76 EXPECT_TRUE(audio_state->typing_noise_detected());
77 voe_observer->CallbackOnError(-1, VE_NOT_INITED);
78 EXPECT_TRUE(audio_state->typing_noise_detected());
79
80 voe_observer->CallbackOnError(-1, VE_TYPING_NOISE_OFF_WARNING);
81 EXPECT_FALSE(audio_state->typing_noise_detected());
82 voe_observer->CallbackOnError(-1, VE_NOT_INITED);
83 EXPECT_FALSE(audio_state->typing_noise_detected());
84}
aleloidd310712016-11-17 06:28:59 -080085
86// Test that RecordedDataIsAvailable calls get to the original transport.
87TEST(AudioStateTest, RecordedAudioArrivesAtOriginalTransport) {
88 using testing::_;
89
90 ConfigHelper helper;
91 auto& voice_engine = helper.voice_engine();
92 auto device =
93 static_cast<MockAudioDeviceModule*>(voice_engine.audio_device_module());
94
95 AudioTransport* audio_transport_proxy = nullptr;
96 ON_CALL(*device, RegisterAudioCallback(_))
97 .WillByDefault(
98 testing::Invoke([&audio_transport_proxy](AudioTransport* transport) {
99 audio_transport_proxy = transport;
100 return 0;
101 }));
102
103 MockAudioTransport original_audio_transport;
104 ON_CALL(voice_engine, audio_transport())
105 .WillByDefault(testing::Return(&original_audio_transport));
106
107 EXPECT_CALL(voice_engine, audio_device_module());
108 std::unique_ptr<internal::AudioState> audio_state(
109 new internal::AudioState(helper.config()));
110
111 // Setup completed. Ensure call of old transport is forwarded to new.
112 uint32_t new_mic_level;
113 EXPECT_CALL(original_audio_transport,
114 RecordedDataIsAvailable(nullptr, 80, 2, 1, 8000, 0, 0, 0, false,
115 testing::Ref(new_mic_level)));
116
117 audio_transport_proxy->RecordedDataIsAvailable(nullptr, 80, 2, 1, 8000, 0, 0,
118 0, false, new_mic_level);
119}
solenberg566ef242015-11-06 15:34:49 -0800120} // namespace test
121} // namespace webrtc