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. 4 5 #ifndef MEDIA_AUDIO_TEST_AUDIO_INPUT_CONTROLLER_FACTORY_H_ 6 #define MEDIA_AUDIO_TEST_AUDIO_INPUT_CONTROLLER_FACTORY_H_ 7 8 #include "base/bind.h" 9 #include "media/audio/audio_input_controller.h" 10 11 namespace media { 12 13 class UserInputMonitor; 14 class TestAudioInputControllerFactory; 15 16 // TestAudioInputController and TestAudioInputControllerFactory are used for 17 // testing consumers of AudioInputController. TestAudioInputControllerFactory 18 // is a AudioInputController::Factory that creates TestAudioInputControllers. 19 // 20 // TestAudioInputController::Record and Close are overriden to do nothing. It is 21 // expected that you'll grab the EventHandler from the TestAudioInputController 22 // and invoke the callback methods when appropriate. In this way it's easy to 23 // mock a AudioInputController. 24 // 25 // Typical usage: 26 // // Create and register factory. 27 // TestAudioInputControllerFactory factory; 28 // AudioInputController::set_factory_for_testing(&factory); 29 // 30 // // Do something that triggers creation of an AudioInputController. 31 // TestAudioInputController* controller = factory.last_controller(); 32 // DCHECK(controller); 33 // 34 // // Notify event handler with whatever data you want. 35 // controller->event_handler()->OnCreated(...); 36 // 37 // // Do something that triggers AudioInputController::Record to be called. 38 // controller->event_handler()->OnData(...); 39 // controller->event_handler()->OnError(...); 40 // 41 // // Make sure consumer of AudioInputController does the right thing. 42 // ... 43 // // Reset factory. 44 // AudioInputController::set_factory_for_testing(NULL); 45 46 class TestAudioInputController : public AudioInputController { 47 public: 48 class Delegate { 49 public: 50 virtual void TestAudioControllerOpened( 51 TestAudioInputController* controller) = 0; 52 virtual void TestAudioControllerClosed( 53 TestAudioInputController* controller) = 0; 54 }; 55 56 TestAudioInputController(TestAudioInputControllerFactory* factory, 57 AudioManager* audio_manager, 58 const AudioParameters& audio_parameters, 59 EventHandler* event_handler, 60 SyncWriter* sync_writer, 61 UserInputMonitor* user_input_monitor); 62 63 // Returns the event handler installed on the AudioInputController. 64 EventHandler* event_handler() const { return event_handler_; } 65 66 // Notifies the TestAudioControllerOpened() event to the delegate (if any). 67 virtual void Record() OVERRIDE; 68 69 // Ensure that the closure is run on the audio-manager thread. 70 virtual void Close(const base::Closure& closed_task) OVERRIDE; 71 72 const AudioParameters& audio_parameters() const { 73 return audio_parameters_; 74 } 75 76 protected: 77 virtual ~TestAudioInputController(); 78 79 private: 80 AudioParameters audio_parameters_; 81 82 // These are not owned by us and expected to be valid for this object's 83 // lifetime. 84 TestAudioInputControllerFactory* factory_; 85 EventHandler* event_handler_; 86 87 DISALLOW_COPY_AND_ASSIGN(TestAudioInputController); 88 }; 89 90 typedef TestAudioInputController::Delegate TestAudioInputControllerDelegate; 91 92 // Simple AudioInputController::Factory method that creates 93 // TestAudioInputControllers. 94 class TestAudioInputControllerFactory : public AudioInputController::Factory { 95 public: 96 TestAudioInputControllerFactory(); 97 virtual ~TestAudioInputControllerFactory(); 98 99 // AudioInputController::Factory methods. 100 virtual AudioInputController* Create( 101 AudioManager* audio_manager, 102 AudioInputController::EventHandler* event_handler, 103 AudioParameters params, 104 UserInputMonitor* user_input_monitor) OVERRIDE; 105 106 void set_delegate(TestAudioInputControllerDelegate* delegate) { 107 delegate_ = delegate; 108 } 109 110 TestAudioInputController* controller() const { return controller_; } 111 112 private: 113 friend class TestAudioInputController; 114 115 // Invoked by a TestAudioInputController when it gets destroyed. 116 void OnTestAudioInputControllerDestroyed( 117 TestAudioInputController* controller); 118 119 // The caller of Create owns this object. 120 TestAudioInputController* controller_; 121 122 // The delegate for tests for receiving audio controller events. 123 TestAudioInputControllerDelegate* delegate_; 124 125 DISALLOW_COPY_AND_ASSIGN(TestAudioInputControllerFactory); 126 }; 127 128 } // namespace media 129 130 #endif // MEDIA_AUDIO_TEST_AUDIO_INPUT_CONTROLLER_FACTORY_H_ 131