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