File: audio_pipe_reader_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (121 lines) | stat: -rw-r--r-- 3,787 bytes parent folder | download | duplicates (5)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


#include "remoting/host/linux/audio_pipe_reader.h"

#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <memory>

#include "base/containers/span.h"
#include "base/files/file.h"
#include "base/files/scoped_temp_dir.h"
#include "base/message_loop/message_pump_type.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace remoting {

class AudioPipeReaderTest : public testing::Test,
                            public AudioPipeReader::StreamObserver {
 public:
  AudioPipeReaderTest() : stop_at_position_(-1) {}

  AudioPipeReaderTest(const AudioPipeReaderTest&) = delete;
  AudioPipeReaderTest& operator=(const AudioPipeReaderTest&) = delete;

  void SetUp() override {
    ASSERT_TRUE(test_dir_.CreateUniqueTempDir());
    pipe_path_ = test_dir_.GetPath().AppendASCII("test_pipe");
    audio_thread_.reset(new base::Thread("TestAudioThread"));
    audio_thread_->StartWithOptions(
        base::Thread::Options(base::MessagePumpType::IO, 0));
    reader_ = AudioPipeReader::Create(audio_thread_->task_runner(), pipe_path_);
    reader_->AddObserver(this);
  }

  // AudioPipeReader::StreamObserver interface.
  void OnDataRead(scoped_refptr<base::RefCountedString> data) override {
    read_data_ += data->as_string();
    if (stop_at_position_ > 0 &&
        static_cast<int>(read_data_.size()) >= stop_at_position_) {
      stop_at_position_ = -1;
      run_loop_->Quit();
    }
  }

  void CreatePipe() {
    ASSERT_EQ(0, mkfifo(pipe_path_.value().c_str(), 0600));
    output_.reset(new base::File(
        pipe_path_, base::File::FLAG_OPEN | base::File::FLAG_WRITE));
    ASSERT_TRUE(output_->IsValid());
  }

  void DeletePipe() {
    output_.reset();
    ASSERT_EQ(0, unlink(pipe_path_.value().c_str()));
  }

  void WaitForInput(int num_bytes) {
    run_loop_.reset(new base::RunLoop());
    stop_at_position_ = read_data_.size() + num_bytes;
    run_loop_->Run();
  }

  void WriteAndWait(const std::string& data) {
    ASSERT_TRUE(output_->WriteAtCurrentPosAndCheck(base::as_byte_span(data)));
    WaitForInput(data.size());
  }

 protected:
  base::test::SingleThreadTaskEnvironment task_environment_;
  std::unique_ptr<base::RunLoop> run_loop_;
  std::unique_ptr<base::Thread> audio_thread_;
  base::ScopedTempDir test_dir_;
  base::FilePath pipe_path_;
  std::unique_ptr<base::File> output_;

  scoped_refptr<AudioPipeReader> reader_;

  std::string read_data_;
  int stop_at_position_;
};

// Verify that the reader can detect when the pipe is created and destroyed.
TEST_F(AudioPipeReaderTest, CreateAndDestroyPipe) {
  ASSERT_NO_FATAL_FAILURE(CreatePipe());
  ASSERT_NO_FATAL_FAILURE(WriteAndWait("ABCD"));
  ASSERT_NO_FATAL_FAILURE(DeletePipe());

  ASSERT_NO_FATAL_FAILURE(CreatePipe());
  ASSERT_NO_FATAL_FAILURE(WriteAndWait("abcd"));
  ASSERT_NO_FATAL_FAILURE(DeletePipe());

  EXPECT_EQ("ABCDabcd", read_data_);
}

// Verifies that the reader reads at the right speed.
TEST_F(AudioPipeReaderTest, Pacing) {
  int test_data_size = int{AudioPipeReader::kSamplingRate} *
                       AudioPipeReader::kChannels *
                       AudioPipeReader::kBytesPerSample / 2;
  std::string test_data(test_data_size, '\0');

  ASSERT_NO_FATAL_FAILURE(CreatePipe());

  base::TimeTicks start_time = base::TimeTicks::Now();
  ASSERT_NO_FATAL_FAILURE(WriteAndWait(test_data));
  base::TimeDelta time_passed = base::TimeTicks::Now() - start_time;

  EXPECT_EQ(test_data, read_data_);
  EXPECT_GE(time_passed, base::Milliseconds(500));
}

}  // namespace remoting