Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: media/audio/audio_manager_unittest.cc

Issue 2784433002: Ensures that audio tasks cannot run after AudioManager is deleted. (Closed)
Patch Set: cleanup Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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_manager.h" 5 #include "media/audio/audio_manager.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/environment.h" 11 #include "base/environment.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "base/run_loop.h" 14 #include "base/run_loop.h"
15 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
16 #include "base/synchronization/waitable_event.h" 16 #include "base/synchronization/waitable_event.h"
17 #include "base/test/test_message_loop.h" 17 #include "base/test/test_message_loop.h"
18 #include "base/threading/thread_task_runner_handle.h" 18 #include "base/threading/thread_task_runner_handle.h"
19 #include "build/build_config.h" 19 #include "build/build_config.h"
20 #include "media/audio/audio_device_description.h" 20 #include "media/audio/audio_device_description.h"
21 #include "media/audio/audio_device_name.h" 21 #include "media/audio/audio_device_name.h"
22 #include "media/audio/audio_output_proxy.h" 22 #include "media/audio/audio_output_proxy.h"
23 #include "media/audio/audio_unittest_util.h" 23 #include "media/audio/audio_unittest_util.h"
24 #include "media/audio/fake_audio_log_factory.h" 24 #include "media/audio/fake_audio_log_factory.h"
25 #include "media/audio/fake_audio_manager.h" 25 #include "media/audio/fake_audio_manager.h"
26 #include "media/audio/test_audio_thread.h"
26 #include "testing/gmock/include/gmock/gmock.h" 27 #include "testing/gmock/include/gmock/gmock.h"
27 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
28 29
29 #if defined(USE_ALSA) 30 #if defined(USE_ALSA)
30 #include "media/audio/alsa/audio_manager_alsa.h" 31 #include "media/audio/alsa/audio_manager_alsa.h"
31 #endif // defined(USE_ALSA) 32 #endif // defined(USE_ALSA)
32 33
33 #if defined(OS_WIN) 34 #if defined(OS_WIN)
34 #include "base/win/scoped_com_initializer.h" 35 #include "base/win/scoped_com_initializer.h"
35 #include "media/audio/win/audio_manager_win.h" 36 #include "media/audio/win/audio_manager_win.h"
36 #endif 37 #endif
37 38
38 #if defined(USE_PULSEAUDIO) 39 #if defined(USE_PULSEAUDIO)
39 #include "media/audio/pulse/audio_manager_pulse.h" 40 #include "media/audio/pulse/audio_manager_pulse.h"
41 #include "media/audio/pulse/pulse_util.h"
40 #endif // defined(USE_PULSEAUDIO) 42 #endif // defined(USE_PULSEAUDIO)
41 43
42 #if defined(USE_CRAS) 44 #if defined(USE_CRAS)
43 #include "chromeos/audio/audio_devices_pref_handler_stub.h" 45 #include "chromeos/audio/audio_devices_pref_handler_stub.h"
44 #include "chromeos/audio/cras_audio_handler.h" 46 #include "chromeos/audio/cras_audio_handler.h"
45 #include "chromeos/dbus/dbus_thread_manager.h" 47 #include "chromeos/dbus/dbus_thread_manager.h"
46 #include "chromeos/dbus/fake_cras_audio_client.h" 48 #include "chromeos/dbus/fake_cras_audio_client.h"
47 #include "media/audio/cras/audio_manager_cras.h" 49 #include "media/audio/cras/audio_manager_cras.h"
48 #endif // defined(USE_CRAS) 50 #endif // defined(USE_CRAS)
49 51
50 namespace media { 52 namespace media {
51 53
52 namespace { 54 namespace {
53 55
54 template <typename T> 56 template <typename T>
55 struct TestAudioManagerFactory { 57 struct TestAudioManagerFactory {
56 static ScopedAudioManagerPtr Create(AudioLogFactory* audio_log_factory) { 58 static std::unique_ptr<AudioManager> Create(
57 return ScopedAudioManagerPtr(new T(base::ThreadTaskRunnerHandle::Get(), 59 AudioLogFactory* audio_log_factory) {
58 base::ThreadTaskRunnerHandle::Get(), 60 return base::MakeUnique<T>(base::MakeUnique<TestAudioThread>(),
59 audio_log_factory)); 61 audio_log_factory);
60 } 62 }
61 }; 63 };
62 64
63 #if defined(USE_PULSEAUDIO) 65 #if defined(USE_PULSEAUDIO)
64 template <> 66 template <>
65 struct TestAudioManagerFactory<AudioManagerPulse> { 67 struct TestAudioManagerFactory<AudioManagerPulse> {
66 static ScopedAudioManagerPtr Create(AudioLogFactory* audio_log_factory) { 68 static std::unique_ptr<AudioManager> Create(
67 std::unique_ptr<AudioManagerPulse, AudioManagerDeleter> manager( 69 AudioLogFactory* audio_log_factory) {
68 new AudioManagerPulse(base::ThreadTaskRunnerHandle::Get(), 70 pa_threaded_mainloop* pa_mainloop = nullptr;
69 base::ThreadTaskRunnerHandle::Get(), 71 pa_context* pa_context = nullptr;
70 audio_log_factory)); 72 if (!pulse::InitPulse(&pa_mainloop, &pa_context))
71 if (!manager->Init()) 73 return nullptr;
72 manager.reset(); 74 return base::MakeUnique<AudioManagerPulse>(
73 return std::move(manager); 75 base::MakeUnique<TestAudioThread>(), audio_log_factory, pa_mainloop,
76 pa_context);
74 } 77 }
75 }; 78 };
76 #endif // defined(USE_PULSEAUDIO) 79 #endif // defined(USE_PULSEAUDIO)
77 80
78 template <> 81 template <>
79 struct TestAudioManagerFactory<std::nullptr_t> { 82 struct TestAudioManagerFactory<std::nullptr_t> {
80 static ScopedAudioManagerPtr Create(AudioLogFactory* audio_log_factory) { 83 static std::unique_ptr<AudioManager> Create(
81 return AudioManager::CreateForTesting(base::ThreadTaskRunnerHandle::Get()); 84 AudioLogFactory* audio_log_factory) {
85 return AudioManager::CreateForTesting(base::MakeUnique<TestAudioThread>());
82 } 86 }
83 }; 87 };
84 88
85 #if defined(USE_CRAS) 89 #if defined(USE_CRAS)
86 using chromeos::AudioNode; 90 using chromeos::AudioNode;
87 using chromeos::AudioNodeList; 91 using chromeos::AudioNodeList;
88 92
89 const uint64_t kJabraSpeaker1Id = 30001; 93 const uint64_t kJabraSpeaker1Id = 30001;
90 const uint64_t kJabraSpeaker1StableDeviceId = 80001; 94 const uint64_t kJabraSpeaker1StableDeviceId = 80001;
91 const uint64_t kJabraSpeaker2Id = 30002; 95 const uint64_t kJabraSpeaker2Id = 30002;
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 audio_client_->SetAudioNodesForTesting(audio_nodes); 230 audio_client_->SetAudioNodesForTesting(audio_nodes);
227 audio_pref_handler_ = new chromeos::AudioDevicesPrefHandlerStub(); 231 audio_pref_handler_ = new chromeos::AudioDevicesPrefHandlerStub();
228 chromeos::CrasAudioHandler::Initialize(audio_pref_handler_); 232 chromeos::CrasAudioHandler::Initialize(audio_pref_handler_);
229 cras_audio_handler_ = chromeos::CrasAudioHandler::Get(); 233 cras_audio_handler_ = chromeos::CrasAudioHandler::Get();
230 base::RunLoop().RunUntilIdle(); 234 base::RunLoop().RunUntilIdle();
231 } 235 }
232 #endif // defined(USE_CRAS) 236 #endif // defined(USE_CRAS)
233 237
234 protected: 238 protected:
235 AudioManagerTest() { CreateAudioManagerForTesting(); } 239 AudioManagerTest() { CreateAudioManagerForTesting(); }
236 ~AudioManagerTest() override {} 240 ~AudioManagerTest() override { audio_manager_->Shutdown(); }
237 241
238 // Helper method which verifies that the device list starts with a valid 242 // Helper method which verifies that the device list starts with a valid
239 // default record followed by non-default device names. 243 // default record followed by non-default device names.
240 static void CheckDeviceDescriptions( 244 static void CheckDeviceDescriptions(
241 const AudioDeviceDescriptions& device_descriptions) { 245 const AudioDeviceDescriptions& device_descriptions) {
242 DVLOG(2) << "Got " << device_descriptions.size() << " audio devices."; 246 DVLOG(2) << "Got " << device_descriptions.size() << " audio devices.";
243 if (!device_descriptions.empty()) { 247 if (!device_descriptions.empty()) {
244 AudioDeviceDescriptions::const_iterator it = device_descriptions.begin(); 248 AudioDeviceDescriptions::const_iterator it = device_descriptions.begin();
245 249
246 // The first device in the list should always be the default device. 250 // The first device in the list should always be the default device.
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 } 327 }
324 bool OutputDevicesAvailable() { 328 bool OutputDevicesAvailable() {
325 return audio_manager_->HasAudioOutputDevices(); 329 return audio_manager_->HasAudioOutputDevices();
326 } 330 }
327 331
328 template <typename T = std::nullptr_t> 332 template <typename T = std::nullptr_t>
329 void CreateAudioManagerForTesting() { 333 void CreateAudioManagerForTesting() {
330 // Only one AudioManager may exist at a time, so destroy the one we're 334 // Only one AudioManager may exist at a time, so destroy the one we're
331 // currently holding before creating a new one. 335 // currently holding before creating a new one.
332 // Flush the message loop to run any shutdown tasks posted by AudioManager. 336 // Flush the message loop to run any shutdown tasks posted by AudioManager.
333 audio_manager_.reset(); 337 if (audio_manager_) {
334 base::RunLoop().RunUntilIdle(); 338 audio_manager_->Shutdown();
339 audio_manager_.reset();
340 }
335 341
336 audio_manager_ = 342 audio_manager_ =
337 TestAudioManagerFactory<T>::Create(&fake_audio_log_factory_); 343 TestAudioManagerFactory<T>::Create(&fake_audio_log_factory_);
338 // A few AudioManager implementations post initialization tasks to 344 // A few AudioManager implementations post initialization tasks to
339 // audio thread. Flush the thread to ensure that |audio_manager_| is 345 // audio thread. Flush the thread to ensure that |audio_manager_| is
340 // initialized and ready to use before returning from this function. 346 // initialized and ready to use before returning from this function.
341 // TODO(alokp): We should perhaps do this in AudioManager::Create(). 347 // TODO(alokp): We should perhaps do this in AudioManager::Create().
342 base::RunLoop().RunUntilIdle(); 348 base::RunLoop().RunUntilIdle();
343 } 349 }
344 350
345 base::TestMessageLoop message_loop_; 351 base::TestMessageLoop message_loop_;
346 FakeAudioLogFactory fake_audio_log_factory_; 352 FakeAudioLogFactory fake_audio_log_factory_;
347 ScopedAudioManagerPtr audio_manager_; 353 std::unique_ptr<AudioManager> audio_manager_;
348 354
349 #if defined(USE_CRAS) 355 #if defined(USE_CRAS)
350 chromeos::CrasAudioHandler* cras_audio_handler_ = nullptr; // Not owned. 356 chromeos::CrasAudioHandler* cras_audio_handler_ = nullptr; // Not owned.
351 chromeos::FakeCrasAudioClient* audio_client_ = nullptr; // Not owned. 357 chromeos::FakeCrasAudioClient* audio_client_ = nullptr; // Not owned.
352 scoped_refptr<chromeos::AudioDevicesPrefHandlerStub> audio_pref_handler_; 358 scoped_refptr<chromeos::AudioDevicesPrefHandlerStub> audio_pref_handler_;
353 #endif // defined(USE_CRAS) 359 #endif // defined(USE_CRAS)
354 }; 360 };
355 361
356 #if defined(USE_CRAS) 362 #if defined(USE_CRAS)
357 TEST_F(AudioManagerTest, EnumerateInputDevicesCras) { 363 TEST_F(AudioManagerTest, EnumerateInputDevicesCras) {
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
570 private: 576 private:
571 DISALLOW_COPY_AND_ASSIGN(MockAudioDebugRecordingManager); 577 DISALLOW_COPY_AND_ASSIGN(MockAudioDebugRecordingManager);
572 }; 578 };
573 579
574 class TestAudioManager : public FakeAudioManager { 580 class TestAudioManager : public FakeAudioManager {
575 // For testing the default implementation of GetGroupId(Input|Output) 581 // For testing the default implementation of GetGroupId(Input|Output)
576 // input$i is associated to output$i, if both exist. 582 // input$i is associated to output$i, if both exist.
577 // Default input is input1. 583 // Default input is input1.
578 // Default output is output2. 584 // Default output is output2.
579 public: 585 public:
580 TestAudioManager( 586 TestAudioManager(std::unique_ptr<AudioThread> audio_thread,
581 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 587 AudioLogFactory* audio_log_factory)
582 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, 588 : FakeAudioManager(std::move(audio_thread), audio_log_factory) {}
583 AudioLogFactory* audio_log_factory)
584 : FakeAudioManager(task_runner, worker_task_runner, audio_log_factory) {}
585 589
586 std::string GetDefaultOutputDeviceID() override { return "output4"; } 590 std::string GetDefaultOutputDeviceID() override { return "output4"; }
587 591
588 std::string GetAssociatedOutputDeviceID( 592 std::string GetAssociatedOutputDeviceID(
589 const std::string& input_id) override { 593 const std::string& input_id) override {
590 if (input_id == "input1") 594 if (input_id == "input1")
591 return "output1"; 595 return "output1";
592 if (input_id == "input2") 596 if (input_id == "input2")
593 return "output2"; 597 return "output2";
594 if (input_id == "default") 598 if (input_id == "default")
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
664 668
665 base::FilePath file_path(FILE_PATH_LITERAL("path")); 669 base::FilePath file_path(FILE_PATH_LITERAL("path"));
666 EXPECT_CALL(*mock_debug_recording_manager, EnableDebugRecording(file_path)); 670 EXPECT_CALL(*mock_debug_recording_manager, EnableDebugRecording(file_path));
667 audio_manager_->EnableOutputDebugRecording(file_path); 671 audio_manager_->EnableOutputDebugRecording(file_path);
668 672
669 EXPECT_CALL(*mock_debug_recording_manager, DisableDebugRecording()); 673 EXPECT_CALL(*mock_debug_recording_manager, DisableDebugRecording());
670 audio_manager_->DisableOutputDebugRecording(); 674 audio_manager_->DisableOutputDebugRecording();
671 } 675 }
672 676
673 } // namespace media 677 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698