Removed 'anonymous' from namespace, added whitespace in thread_restrictions.cc
[chromium-blink-merge.git] / remoting / host / audio_silence_detector_unittest.cc
blob93a8309f0583d747d14d27efd9f2685dd700d1d7
1 // Copyright (c) 2012 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.
5 #include "remoting/host/audio_silence_detector.h"
7 #include "base/basictypes.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace remoting {
12 namespace {
14 const int kSamplingRate = 1000;
16 void TestSilenceDetector(AudioSilenceDetector* target,
17 const int16* samples, int samples_count,
18 bool silence_expected) {
19 target->Reset(kSamplingRate, 1);
20 bool silence_started = false;
21 int threshold_length = 0;
22 for (int i = 0; i < 3 * kSamplingRate / samples_count; ++i) {
23 bool result = target->IsSilence(samples, samples_count);
24 if (silence_started) {
25 ASSERT_TRUE(result);
26 } else if (result) {
27 silence_started = true;
28 threshold_length = i * samples_count;
32 // Check that the silence was detected if it was expected.
33 EXPECT_EQ(silence_expected, silence_started);
35 if (silence_expected) {
36 // Check that silence threshold is between 0.5 and 2 seconds.
37 EXPECT_GE(threshold_length, kSamplingRate / 2);
38 EXPECT_LE(threshold_length, kSamplingRate * 2);
42 } // namespace
44 TEST(AudioSilenceDetectorTest, Silence) {
45 const int16 kSamples[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
47 AudioSilenceDetector target(0);
48 TestSilenceDetector(&target, kSamples, arraysize(kSamples), true);
51 TEST(AudioSilenceDetectorTest, Sound) {
52 const int16 kSamples[] = {65, 73, 83, 89, 92, -1, 5, 9, 123, 0};
54 AudioSilenceDetector target(0);
55 TestSilenceDetector(&target, kSamples, arraysize(kSamples), false);
58 TEST(AudioSilenceDetectorTest, Threshold) {
59 const int16 kSamples[] = {0, 0, 0, 0, 1, 0, 0, -1, 0, 0};
61 AudioSilenceDetector target1(0);
62 TestSilenceDetector(&target1, kSamples, arraysize(kSamples), false);
64 AudioSilenceDetector target2(1);
65 TestSilenceDetector(&target2, kSamples, arraysize(kSamples), true);
68 } // namespace remoting