Chromium Code Reviews| Index: media/audio/win/core_audio_util_win_unittest.cc |
| diff --git a/media/audio/win/core_audio_util_win_unittest.cc b/media/audio/win/core_audio_util_win_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a0bfd63440e3158dd7c6189bb58a001a52f16200 |
| --- /dev/null |
| +++ b/media/audio/win/core_audio_util_win_unittest.cc |
| @@ -0,0 +1,226 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/synchronization/waitable_event.h" |
| +#include "base/win/scoped_com_initializer.h" |
| +#include "media/audio/win/core_audio_util_win.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using base::win::ScopedCOMInitializer; |
| + |
| +namespace media { |
| + |
| +class CoreAudioUtilWinTest : public ::testing::Test { |
| + protected: |
| + // The test runs on a COM thread in a multithread apartment (MTA). |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
nit: s/a multithread/the multithreaded
(there is a
henrika (OOO until Aug 14)
2012/10/25 15:14:08
Done.
|
| + // If we don't initialize the COM library on a thread before using COM, |
| + // all function calls will return CO_E_NOTINITIALIZED. |
| + CoreAudioUtilWinTest() |
| + : com_init_(ScopedCOMInitializer::kMTA) {} |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
maybe DCHECK(com_init_.succeeded())?
henrika (OOO until Aug 14)
2012/10/25 15:14:08
Done.
|
| + virtual ~CoreAudioUtilWinTest() {} |
| + |
| + bool CanRunAudioTest() { |
| + bool core_audio = CoreAudioIsSupported(); |
| + int capture_devices = CoreAudioNumberOfActiveDevices(eCapture); |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
shouldn't you only call these methods if core_audi
henrika (OOO until Aug 14)
2012/10/25 15:14:08
Thanks.
|
| + int render_devices = CoreAudioNumberOfActiveDevices(eRender); |
| + return (core_audio && (capture_devices > 0) && (render_devices > 0)); |
| + } |
| + |
| + ScopedCOMInitializer com_init_; |
| +}; |
| + |
| +// --- MMdevice |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
remove?
henrika (OOO until Aug 14)
2012/10/25 15:14:08
Done.
|
| + |
| +TEST_F(CoreAudioUtilWinTest, NumberOfActiveDevices) { |
| + if (!CanRunAudioTest()) |
| + return; |
| + |
| + int render_devices = CoreAudioNumberOfActiveDevices(eRender); |
| + EXPECT_GT(render_devices, 0); |
| + int capture_devices = CoreAudioNumberOfActiveDevices(eCapture); |
| + EXPECT_GT(capture_devices, 0); |
| + int total_devices = CoreAudioNumberOfActiveDevices(eAll); |
| + EXPECT_EQ(total_devices, render_devices + capture_devices); |
| +} |
| + |
| +TEST_F(CoreAudioUtilWinTest, CreateDeviceEnumerator) { |
| + if (!CanRunAudioTest()) |
| + return; |
| + |
| + ScopedComPtr<IMMDeviceEnumerator> enumerator = |
| + CoreAudioCreateDeviceEnumerator(); |
| + EXPECT_TRUE(enumerator); |
| + enumerator = CoreAudioCreateDeviceEnumerator(); |
| + EXPECT_TRUE(enumerator); |
| + enumerator.Release(); |
| + EXPECT_FALSE(enumerator); |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
this isn't needed since you're not testing ScopedC
henrika (OOO until Aug 14)
2012/10/25 15:14:08
Good point.
|
| + enumerator = CoreAudioCreateDeviceEnumerator(); |
| + EXPECT_TRUE(enumerator); |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
I must be missing something - is the intention of
henrika (OOO until Aug 14)
2012/10/25 15:14:08
Agree. I was trying to test different use cases bu
|
| +} |
| + |
| +TEST_F(CoreAudioUtilWinTest, CreateDefaultDevice) { |
| + if (!CanRunAudioTest()) |
| + return; |
| + |
| + // Verify all device roles for playback endpoints. |
| + ScopedComPtr<IMMDevice> audio_device = |
| + CoreAudioCreateDefaultDevice(eRender, eConsole); |
| + EXPECT_TRUE(audio_device); |
| + EXPECT_EQ(eRender, CoreAudioGetDataFlow(audio_device)); |
| + CoreAudioCreateDefaultDevice(eRender, eCommunications); |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
Forgot to assign to audio_device. All the checks
henrika (OOO until Aug 14)
2012/10/25 15:14:08
LOL. Will fix.
|
| + EXPECT_TRUE(audio_device); |
| + CoreAudioCreateDefaultDevice(eRender, eMultimedia); |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
forgot to assign.
henrika (OOO until Aug 14)
2012/10/25 15:14:08
dito
|
| + EXPECT_TRUE(audio_device); |
| + CoreAudioCreateDefaultDevice(eRender, eCommunications); |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
forgot to assign... but then again, you've already
henrika (OOO until Aug 14)
2012/10/25 15:14:08
see comment above about being stupid ;-)
|
| + |
| + // Verify all device roles for recording endpoints. |
| + audio_device = CoreAudioCreateDefaultDevice(eCapture, eConsole); |
| + EXPECT_TRUE(audio_device); |
| + EXPECT_EQ(eCapture, CoreAudioGetDataFlow(audio_device)); |
| + audio_device = CoreAudioCreateDefaultDevice(eCapture, eCommunications); |
| + EXPECT_TRUE(audio_device); |
| + EXPECT_EQ(eCapture, CoreAudioGetDataFlow(audio_device)); |
| + audio_device = CoreAudioCreateDefaultDevice(eCapture, eMultimedia); |
| + EXPECT_TRUE(audio_device); |
| + EXPECT_EQ(eCapture, CoreAudioGetDataFlow(audio_device)); |
| + |
| + // Only eRender and eCapture are allowed as flow parameter. |
| + audio_device = CoreAudioCreateDefaultDevice(eAll, eConsole); |
| + EXPECT_FALSE(audio_device); |
| + EXPECT_EQ(eAll, CoreAudioGetDataFlow(audio_device)); |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
As a suggestion for writing code like this, I pers
henrika (OOO until Aug 14)
2012/10/25 15:14:08
Cool ;-)
Will fix last test as well.
Something t
|
| +} |
| + |
| +TEST_F(CoreAudioUtilWinTest, CreateDevice) { |
| + if (!CanRunAudioTest()) |
| + return; |
| + |
| + // Get name and ID of default device used for playback. |
| + ScopedComPtr<IMMDevice> default_render_device = |
| + CoreAudioCreateDefaultDevice(eRender, eConsole); |
| + AudioDeviceName default_render_name; |
| + HRESULT hr = CoreAudioGetDeviceName(default_render_device, |
| + &default_render_name); |
| + EXPECT_TRUE(SUCCEEDED(hr)); |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
nit: just pass the entire function call to EXPECT_
henrika (OOO until Aug 14)
2012/10/25 15:14:08
Nice.
|
| + |
| + // Use the uniqe ID as input to CoreAudioCreateDevice() and create a |
| + // corresponding IMMDevice. |
| + ScopedComPtr<IMMDevice> audio_device = |
| + CoreAudioCreateDevice(default_render_name.unique_id); |
| + EXPECT_TRUE(audio_device); |
| + |
| + // Verify that the two IMMDevice interfaces represents the same endpoint |
| + // by comparing their unique IDs. |
| + AudioDeviceName device_name; |
| + hr = CoreAudioGetDeviceName(audio_device, &device_name); |
| + EXPECT_TRUE(SUCCEEDED(hr)); |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
same here and elsewhere.
henrika (OOO until Aug 14)
2012/10/25 15:14:08
Done.
|
| + EXPECT_STREQ(default_render_name.unique_id.c_str(), |
| + device_name.unique_id.c_str()); |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
doesn't this work?
EXPECT_EQ(default_render_name.u
henrika (OOO until Aug 14)
2012/10/25 15:14:08
Yes. In fact it does ;-)
|
| +} |
| + |
| +TEST_F(CoreAudioUtilWinTest, GetDefaultDeviceName) { |
| + if (!CanRunAudioTest()) |
| + return; |
| + |
| + // Get name and ID of default device used for playback. |
| + ScopedComPtr<IMMDevice> audio_device = |
| + CoreAudioCreateDefaultDevice(eRender, eConsole); |
| + AudioDeviceName device_name; |
| + HRESULT hr = CoreAudioGetDeviceName(audio_device, &device_name); |
| + EXPECT_TRUE(SUCCEEDED(hr)); |
| + EXPECT_FALSE(device_name.device_name.empty()); |
| + EXPECT_FALSE(device_name.unique_id.empty()); |
| + |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
remove
henrika (OOO until Aug 14)
2012/10/25 15:14:08
Done.
|
| + |
| + // Get name and ID of default communication device used for playback. |
| + audio_device = CoreAudioCreateDefaultDevice(eRender, eCommunications); |
| + hr = CoreAudioGetDeviceName(audio_device, &device_name); |
| + EXPECT_TRUE(SUCCEEDED(hr)); |
| + EXPECT_FALSE(device_name.device_name.empty()); |
| + EXPECT_FALSE(device_name.unique_id.empty()); |
| + |
| + // Get name and ID of default device used for recording. |
| + audio_device = CoreAudioCreateDefaultDevice(eCapture, eConsole); |
| + hr = CoreAudioGetDeviceName(audio_device, &device_name); |
| + EXPECT_TRUE(SUCCEEDED(hr)); |
| + EXPECT_FALSE(device_name.device_name.empty()); |
| + EXPECT_FALSE(device_name.unique_id.empty()); |
| + |
| + // Get name and ID of default communication device used for recording. |
| + audio_device = CoreAudioCreateDefaultDevice(eCapture, eCommunications); |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
you could use the same data driven approach in thi
henrika (OOO until Aug 14)
2012/10/25 15:14:08
Done.
|
| + hr = CoreAudioGetDeviceName(audio_device, &device_name); |
| + EXPECT_TRUE(SUCCEEDED(hr)); |
| + EXPECT_FALSE(device_name.device_name.empty()); |
| + EXPECT_FALSE(device_name.unique_id.empty()); |
| +} |
| + |
| +TEST_F(CoreAudioUtilWinTest, GetFriendlyName) { |
| + if (!CanRunAudioTest()) |
| + return; |
| + |
| + // Get name and ID of default device used for recording. |
| + ScopedComPtr<IMMDevice> audio_device = |
| + CoreAudioCreateDefaultDevice(eCapture, eConsole); |
| + AudioDeviceName device_name; |
| + HRESULT hr = CoreAudioGetDeviceName(audio_device, &device_name); |
| + EXPECT_TRUE(SUCCEEDED(hr)); |
| + |
| + // Use unique ID as input to CoreAudioGetFriendlyName() and compare the |
| + // result with the already obtained friendly name for the default capture |
| + // device. |
| + std::string friendly_name = CoreAudioGetFriendlyName(device_name.unique_id); |
| + EXPECT_STREQ(friendly_name.c_str(), device_name.device_name.c_str()); |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
EXPECT_EQ should work for std::string (and no .c_s
henrika (OOO until Aug 14)
2012/10/25 15:14:08
Done.
|
| + |
| + // Same test as above but for playback. |
| + audio_device = CoreAudioCreateDefaultDevice(eRender, eConsole); |
| + hr = CoreAudioGetDeviceName(audio_device, &device_name); |
| + EXPECT_TRUE(SUCCEEDED(hr)); |
| + friendly_name = CoreAudioGetFriendlyName(device_name.unique_id); |
| + EXPECT_STREQ(friendly_name.c_str(), device_name.device_name.c_str()); |
| +} |
| + |
| +TEST_F(CoreAudioUtilWinTest, DeviceIsDefault) { |
| + if (!CanRunAudioTest()) |
| + return; |
| + |
| + // Verify that the default render device is correctly identified as a |
| + // default device. |
| + ScopedComPtr<IMMDevice> audio_device = |
| + CoreAudioCreateDefaultDevice(eRender, eConsole); |
| + AudioDeviceName name; |
| + HRESULT hr = CoreAudioGetDeviceName(audio_device, &name); |
| + EXPECT_TRUE(SUCCEEDED(hr)); |
| + const std::string id = name.unique_id; |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
nit: const std::string&
(assuming you're only addi
henrika (OOO until Aug 14)
2012/10/25 15:14:08
less line breaking as well
|
| + EXPECT_TRUE(CoreAudioDeviceIsDefault(eRender, eConsole, id)); |
| + EXPECT_FALSE(CoreAudioDeviceIsDefault(eCapture, eConsole, id)); |
| + |
| + // If more than one active render device exists, one must be default and |
| + // the other default communication. |
| + if (CoreAudioNumberOfActiveDevices(eRender) > 1) { |
| + EXPECT_FALSE(CoreAudioDeviceIsDefault(eRender, eCommunications, id)); |
| + } |
| +} |
| + |
| +// --- WASAPI |
| + |
| +TEST_F(CoreAudioUtilWinTest, CreateClient) { |
| + if (!CanRunAudioTest()) |
| + return; |
| + |
| + // Create an audio client for the default render device. |
| + ScopedComPtr<IMMDevice> device = |
| + CoreAudioCreateDefaultDevice(eRender, eConsole); |
| + EXPECT_EQ(eRender, CoreAudioGetDataFlow(device)); |
| + ScopedComPtr<IAudioClient> client = CoreAudioCreateClient(device); |
| + EXPECT_TRUE(client); |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
nit: EXPECT_TRUE(CoreAudioCreateClient(device));
|
| + |
| + // Create an audio client for the default capture device. |
| + device = CoreAudioCreateDefaultDevice(eCapture, eConsole); |
|
tommi (sloooow) - chröme
2012/10/25 08:42:11
this looks like a copy/paste of the block above, s
henrika (OOO until Aug 14)
2012/10/25 15:14:08
OK ;-)
|
| + EXPECT_EQ(eCapture, CoreAudioGetDataFlow(device)); |
| + client = CoreAudioCreateClient(device); |
| + EXPECT_TRUE(client); |
| +} |
| + |
| +} // namespace media |