| OLD | NEW | 
|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "media/audio/audio_debug_recording_helper.h" | 5 #include "media/audio/audio_debug_recording_helper.h" | 
| 6 | 6 | 
| 7 #include "base/bind.h" | 7 #include "base/bind.h" | 
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" | 
| 9 #include "base/logging.h" | 9 #include "base/logging.h" | 
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" | 
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" | 
| 12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" | 
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" | 
|  | 14 #include "base/test/scoped_task_environment.h" | 
| 14 #include "base/test/test_message_loop.h" | 15 #include "base/test/test_message_loop.h" | 
| 15 #include "media/base/audio_bus.h" | 16 #include "media/base/audio_bus.h" | 
| 16 #include "media/base/audio_sample_types.h" | 17 #include "media/base/audio_sample_types.h" | 
| 17 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" | 
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" | 
| 19 | 20 | 
| 20 using testing::_; | 21 using testing::_; | 
| 21 using testing::Return; | 22 using testing::Return; | 
| 22 | 23 | 
| 23 #if defined(OS_WIN) | 24 #if defined(OS_WIN) | 
| (...skipping 10 matching lines...) Expand all  Loading... | 
| 34 const base::FilePath::CharType kBaseFilePath[] = FILE_PATH_LITERAL("file_path"); | 35 const base::FilePath::CharType kBaseFilePath[] = FILE_PATH_LITERAL("file_path"); | 
| 35 | 36 | 
| 36 // The filename extension the mock should return in GetFileNameExtension(). | 37 // The filename extension the mock should return in GetFileNameExtension(). | 
| 37 const base::FilePath::CharType kFileNameExtension[] = FILE_PATH_LITERAL("wav"); | 38 const base::FilePath::CharType kFileNameExtension[] = FILE_PATH_LITERAL("wav"); | 
| 38 | 39 | 
| 39 }  // namespace | 40 }  // namespace | 
| 40 | 41 | 
| 41 // Mock class for the audio file writer that the helper wraps. | 42 // Mock class for the audio file writer that the helper wraps. | 
| 42 class MockAudioDebugFileWriter : public AudioDebugFileWriter { | 43 class MockAudioDebugFileWriter : public AudioDebugFileWriter { | 
| 43  public: | 44  public: | 
| 44   MockAudioDebugFileWriter( | 45   MockAudioDebugFileWriter(const AudioParameters& params) | 
| 45       const AudioParameters& params, | 46       : AudioDebugFileWriter(params), reference_data_(nullptr) {} | 
| 46       scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) |  | 
| 47       : AudioDebugFileWriter(params, std::move(file_task_runner)), |  | 
| 48         reference_data_(nullptr) {} |  | 
| 49   ~MockAudioDebugFileWriter() override {} | 47   ~MockAudioDebugFileWriter() override {} | 
| 50 | 48 | 
| 51   MOCK_METHOD1(Start, void(const base::FilePath&)); | 49   MOCK_METHOD1(Start, void(const base::FilePath&)); | 
| 52   MOCK_METHOD0(Stop, void()); | 50   MOCK_METHOD0(Stop, void()); | 
| 53 | 51 | 
| 54   // Functions with move-only types as arguments can't be mocked directly, so | 52   // Functions with move-only types as arguments can't be mocked directly, so | 
| 55   // we pass on to DoWrite(). Also, we can verify the data this way. | 53   // we pass on to DoWrite(). Also, we can verify the data this way. | 
| 56   MOCK_METHOD1(DoWrite, void(AudioBus*)); | 54   MOCK_METHOD1(DoWrite, void(AudioBus*)); | 
| 57   void Write(std::unique_ptr<AudioBus> data) override { | 55   void Write(std::unique_ptr<AudioBus> data) override { | 
| 58     CHECK(reference_data_); | 56     CHECK(reference_data_); | 
| (...skipping 25 matching lines...) Expand all  Loading... | 
| 84   DISALLOW_COPY_AND_ASSIGN(MockAudioDebugFileWriter); | 82   DISALLOW_COPY_AND_ASSIGN(MockAudioDebugFileWriter); | 
| 85 }; | 83 }; | 
| 86 | 84 | 
| 87 // Sub-class of the helper that overrides the CreateAudioDebugFileWriter | 85 // Sub-class of the helper that overrides the CreateAudioDebugFileWriter | 
| 88 // function to create the above mock instead. | 86 // function to create the above mock instead. | 
| 89 class AudioDebugRecordingHelperUnderTest : public AudioDebugRecordingHelper { | 87 class AudioDebugRecordingHelperUnderTest : public AudioDebugRecordingHelper { | 
| 90  public: | 88  public: | 
| 91   AudioDebugRecordingHelperUnderTest( | 89   AudioDebugRecordingHelperUnderTest( | 
| 92       const AudioParameters& params, | 90       const AudioParameters& params, | 
| 93       scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 91       scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 
| 94       scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |  | 
| 95       base::OnceClosure on_destruction_closure) | 92       base::OnceClosure on_destruction_closure) | 
| 96       : AudioDebugRecordingHelper(params, | 93       : AudioDebugRecordingHelper(params, | 
| 97                                   std::move(task_runner), | 94                                   std::move(task_runner), | 
| 98                                   std::move(file_task_runner), |  | 
| 99                                   std::move(on_destruction_closure)) {} | 95                                   std::move(on_destruction_closure)) {} | 
| 100   ~AudioDebugRecordingHelperUnderTest() override {} | 96   ~AudioDebugRecordingHelperUnderTest() override {} | 
| 101 | 97 | 
| 102  private: | 98  private: | 
| 103   // Creates the mock writer. After the mock writer is returned, we always | 99   // Creates the mock writer. After the mock writer is returned, we always | 
| 104   // expect GetFileNameExtension() and Start() to be called on it by the helper. | 100   // expect GetFileNameExtension() and Start() to be called on it by the helper. | 
| 105   std::unique_ptr<AudioDebugFileWriter> CreateAudioDebugFileWriter( | 101   std::unique_ptr<AudioDebugFileWriter> CreateAudioDebugFileWriter( | 
| 106       const AudioParameters& params, | 102       const AudioParameters& params) override { | 
| 107       scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) override { | 103     MockAudioDebugFileWriter* writer = new MockAudioDebugFileWriter(params); | 
| 108     MockAudioDebugFileWriter* writer = |  | 
| 109         new MockAudioDebugFileWriter(params, std::move(file_task_runner)); |  | 
| 110     EXPECT_CALL(*writer, GetFileNameExtension()) | 104     EXPECT_CALL(*writer, GetFileNameExtension()) | 
| 111         .WillOnce(Return(kFileNameExtension)); | 105         .WillOnce(Return(kFileNameExtension)); | 
| 112     base::FilePath expected_file_path = | 106     base::FilePath expected_file_path = | 
| 113         base::FilePath(kBaseFilePath).AddExtension(kFileNameExtension); | 107         base::FilePath(kBaseFilePath).AddExtension(kFileNameExtension); | 
| 114     EXPECT_CALL(*writer, Start(expected_file_path)); | 108     EXPECT_CALL(*writer, Start(expected_file_path)); | 
| 115     return base::WrapUnique<AudioDebugFileWriter>(writer); | 109     return base::WrapUnique<AudioDebugFileWriter>(writer); | 
| 116   } | 110   } | 
| 117 | 111 | 
| 118   DISALLOW_COPY_AND_ASSIGN(AudioDebugRecordingHelperUnderTest); | 112   DISALLOW_COPY_AND_ASSIGN(AudioDebugRecordingHelperUnderTest); | 
| 119 }; | 113 }; | 
| 120 | 114 | 
| 121 // The test fixture. | 115 // The test fixture. | 
| 122 class AudioDebugRecordingHelperTest : public ::testing::Test { | 116 class AudioDebugRecordingHelperTest : public ::testing::Test { | 
| 123  public: | 117  public: | 
| 124   AudioDebugRecordingHelperTest() : file_path_(kBaseFilePath) {} | 118   AudioDebugRecordingHelperTest() : file_path_(kBaseFilePath) {} | 
| 125 | 119 | 
| 126   ~AudioDebugRecordingHelperTest() override {} | 120   ~AudioDebugRecordingHelperTest() override {} | 
| 127 | 121 | 
| 128   // Helper function that creates a recording helper. | 122   // Helper function that creates a recording helper. | 
| 129   std::unique_ptr<AudioDebugRecordingHelper> CreateRecordingHelper( | 123   std::unique_ptr<AudioDebugRecordingHelper> CreateRecordingHelper( | 
| 130       const AudioParameters& params, | 124       const AudioParameters& params, | 
| 131       base::OnceClosure on_destruction_closure) { | 125       base::OnceClosure on_destruction_closure) { | 
| 132     return base::MakeUnique<AudioDebugRecordingHelperUnderTest>( | 126     return base::MakeUnique<AudioDebugRecordingHelperUnderTest>( | 
| 133         params, message_loop_.task_runner(), message_loop_.task_runner(), | 127         params, scoped_task_environment_.GetMainThreadTaskRunner(), | 
| 134         std::move(on_destruction_closure)); | 128         std::move(on_destruction_closure)); | 
| 135   } | 129   } | 
| 136 | 130 | 
| 137   // Helper function that unsets the mock writer pointer after disabling. | 131   // Helper function that unsets the mock writer pointer after disabling. | 
| 138   void DisableDebugRecording(AudioDebugRecordingHelper* recording_helper) { | 132   void DisableDebugRecording(AudioDebugRecordingHelper* recording_helper) { | 
| 139     recording_helper->DisableDebugRecording(); | 133     recording_helper->DisableDebugRecording(); | 
| 140   } | 134   } | 
| 141 | 135 | 
| 142   MOCK_METHOD0(OnAudioDebugRecordingHelperDestruction, void()); | 136   MOCK_METHOD0(OnAudioDebugRecordingHelperDestruction, void()); | 
| 143 | 137 | 
| 144  protected: | 138  protected: | 
| 145   base::FilePath file_path_; | 139   base::FilePath file_path_; | 
| 146 | 140 | 
| 147   base::TestMessageLoop message_loop_; | 141   // The test task environment. | 
|  | 142   base::test::ScopedTaskEnvironment scoped_task_environment_; | 
| 148 | 143 | 
| 149  private: | 144  private: | 
| 150   DISALLOW_COPY_AND_ASSIGN(AudioDebugRecordingHelperTest); | 145   DISALLOW_COPY_AND_ASSIGN(AudioDebugRecordingHelperTest); | 
| 151 }; | 146 }; | 
| 152 | 147 | 
| 153 // Creates a helper with an on destrcution closure, and verifies that it's | 148 // Creates a helper with an on destrcution closure, and verifies that it's | 
| 154 // run. | 149 // run. | 
| 155 TEST_F(AudioDebugRecordingHelperTest, TestDestructionClosure) { | 150 TEST_F(AudioDebugRecordingHelperTest, TestDestructionClosure) { | 
| 156   const AudioParameters params; | 151   const AudioParameters params; | 
| 157   std::unique_ptr<AudioDebugRecordingHelper> recording_helper = | 152   std::unique_ptr<AudioDebugRecordingHelper> recording_helper = | 
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 250 | 245 | 
| 251   EXPECT_CALL(*mock_audio_file_writer, Stop()); | 246   EXPECT_CALL(*mock_audio_file_writer, Stop()); | 
| 252   DisableDebugRecording(recording_helper.get()); | 247   DisableDebugRecording(recording_helper.get()); | 
| 253 | 248 | 
| 254   // This call should not yield a DoWrite() call on the mock either. | 249   // This call should not yield a DoWrite() call on the mock either. | 
| 255   recording_helper->OnData(audio_bus.get()); | 250   recording_helper->OnData(audio_bus.get()); | 
| 256   base::RunLoop().RunUntilIdle(); | 251   base::RunLoop().RunUntilIdle(); | 
| 257 } | 252 } | 
| 258 | 253 | 
| 259 }  // namespace media | 254 }  // namespace media | 
| OLD | NEW | 
|---|