xref: /aosp_15_r20/external/webrtc/test/testsupport/copy_to_file_audio_capturer_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2019 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 
11 #include "test/testsupport/copy_to_file_audio_capturer.h"
12 
13 #include <memory>
14 #include <utility>
15 
16 #include "modules/audio_device/include/test_audio_device.h"
17 #include "test/gtest.h"
18 #include "test/testsupport/file_utils.h"
19 
20 namespace webrtc {
21 namespace test {
22 
23 class CopyToFileAudioCapturerTest : public ::testing::Test {
24  protected:
SetUp()25   void SetUp() override {
26     temp_filename_ = webrtc::test::TempFilename(
27         webrtc::test::OutputPath(), "copy_to_file_audio_capturer_unittest");
28     std::unique_ptr<TestAudioDeviceModule::Capturer> delegate =
29         TestAudioDeviceModule::CreatePulsedNoiseCapturer(32000, 48000);
30     capturer_ = std::make_unique<CopyToFileAudioCapturer>(std::move(delegate),
31                                                           temp_filename_);
32   }
33 
TearDown()34   void TearDown() override { ASSERT_EQ(remove(temp_filename_.c_str()), 0); }
35 
36   std::unique_ptr<CopyToFileAudioCapturer> capturer_;
37   std::string temp_filename_;
38 };
39 
TEST_F(CopyToFileAudioCapturerTest,Capture)40 TEST_F(CopyToFileAudioCapturerTest, Capture) {
41   rtc::BufferT<int16_t> expected_buffer;
42   ASSERT_TRUE(capturer_->Capture(&expected_buffer));
43   ASSERT_TRUE(!expected_buffer.empty());
44   // Destruct capturer to close wav file.
45   capturer_.reset(nullptr);
46 
47   // Read resulted file content with `wav_file_capture` and compare with
48   // what was captured.
49   std::unique_ptr<TestAudioDeviceModule::Capturer> wav_file_capturer =
50       TestAudioDeviceModule::CreateWavFileReader(temp_filename_, 48000);
51   rtc::BufferT<int16_t> actual_buffer;
52   wav_file_capturer->Capture(&actual_buffer);
53   ASSERT_EQ(actual_buffer, expected_buffer);
54 }
55 
56 }  // namespace test
57 }  // namespace webrtc
58