| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/test/test_message_loop.h" | 5 #include "base/memory/scoped_ptr.h" |
| 6 #include "base/thread_task_runner_handle.h" | |
| 7 #include "media/audio/audio_manager.h" | 6 #include "media/audio/audio_manager.h" |
| 8 #include "media/audio/audio_manager_factory.h" | 7 #include "media/audio/audio_manager_factory.h" |
| 8 #include "media/audio/fake_audio_log_factory.h" |
| 9 #include "media/audio/fake_audio_manager.h" | 9 #include "media/audio/fake_audio_manager.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace media { | 12 namespace media { |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 class FakeAudioManagerFactory : public AudioManagerFactory { | 15 class FakeAudioManagerFactory : public AudioManagerFactory { |
| 16 public: | 16 public: |
| 17 FakeAudioManagerFactory() {} | 17 FakeAudioManagerFactory() {} |
| 18 ~FakeAudioManagerFactory() override {} | 18 ~FakeAudioManagerFactory() override {} |
| 19 | 19 |
| 20 ScopedAudioManagerPtr CreateInstance( | 20 AudioManager* CreateInstance(AudioLogFactory* audio_log_factory) override { |
| 21 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
| 22 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, | |
| 23 AudioLogFactory* audio_log_factory) override { | |
| 24 ScopedAudioManagerPtr instance( | |
| 25 new FakeAudioManager(std::move(task_runner), | |
| 26 std::move(worker_task_runner), audio_log_factory)); | |
| 27 // |created_instance_| is used for verifying. Ownership is transferred to | 21 // |created_instance_| is used for verifying. Ownership is transferred to |
| 28 // caller. | 22 // caller. |
| 29 created_instance_ = instance.get(); | 23 created_instance_ = new FakeAudioManager(audio_log_factory); |
| 30 return instance; | 24 return created_instance_; |
| 31 } | 25 } |
| 32 | 26 |
| 33 AudioManager* created_instance() { return created_instance_; } | 27 AudioManager* created_instance() { return created_instance_; } |
| 34 | 28 |
| 35 private: | 29 private: |
| 36 AudioManager* created_instance_; | 30 AudioManager* created_instance_; |
| 37 }; | 31 }; |
| 38 | 32 |
| 39 } // namespace | 33 } // namespace |
| 40 | 34 |
| 41 // Verifies that SetFactory has the intended effect. | 35 // Verifies that SetFactory has the intended effect. |
| 42 TEST(AudioManagerFactoryTest, CreateInstance) { | 36 TEST(AudioManagerFactoryTest, CreateInstance) { |
| 43 { | 37 // Create an audio manager and verify that it is not null. |
| 44 base::TestMessageLoop message_loop; | 38 scoped_ptr<AudioManager> manager(AudioManager::CreateForTesting()); |
| 45 // Create an audio manager and verify that it is not null. | 39 ASSERT_NE(nullptr, manager.get()); |
| 46 ScopedAudioManagerPtr manager = | 40 manager.reset(); |
| 47 AudioManager::CreateForTesting(base::ThreadTaskRunnerHandle::Get()); | |
| 48 ASSERT_NE(nullptr, manager.get()); | |
| 49 } | |
| 50 | 41 |
| 51 // Set the factory. Note that ownership of |factory| is transferred to | 42 // Set the factory. Note that ownership of |factory| is transferred to |
| 52 // AudioManager. | 43 // AudioManager. |
| 53 FakeAudioManagerFactory* factory = new FakeAudioManagerFactory(); | 44 FakeAudioManagerFactory* factory = new FakeAudioManagerFactory(); |
| 54 AudioManager::SetFactory(factory); | 45 AudioManager::SetFactory(factory); |
| 55 { | 46 |
| 56 base::TestMessageLoop message_loop; | 47 // Create the AudioManager instance. Verify that it matches the instance |
| 57 // Create the AudioManager instance. Verify that it matches the instance | 48 // provided by the factory. |
| 58 // provided by the factory. | 49 manager.reset(AudioManager::CreateForTesting()); |
| 59 ScopedAudioManagerPtr manager = | 50 ASSERT_NE(nullptr, manager.get()); |
| 60 AudioManager::CreateForTesting(base::ThreadTaskRunnerHandle::Get()); | 51 ASSERT_EQ(factory->created_instance(), manager.get()); |
| 61 ASSERT_NE(nullptr, manager.get()); | 52 |
| 62 ASSERT_EQ(factory->created_instance(), manager.get()); | |
| 63 } | |
| 64 // Reset AudioManagerFactory to prevent factory from persisting to other | 53 // Reset AudioManagerFactory to prevent factory from persisting to other |
| 65 // tests on the same process. | 54 // tests on the same process. |manager| will reset when scope exits. |
| 66 AudioManager::ResetFactoryForTesting(); | 55 AudioManager::ResetFactoryForTesting(); |
| 67 } | 56 } |
| 68 | 57 |
| 69 } // namespace media | 58 } // namespace media |
| OLD | NEW |