IMMDeviceEnumeratorTest.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // -----------------------------------------
  2. // SoundScribe (TM) and related software.
  3. //
  4. // Copyright (C) 2007-2011 Vannatech
  5. // http://www.vannatech.com
  6. // All rights reserved.
  7. //
  8. // This source code is subject to the MIT License.
  9. // http://www.opensource.org/licenses/mit-license.php
  10. //
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. // THE SOFTWARE.
  18. // -----------------------------------------
  19. using System;
  20. using CoreAudioTests.Common;
  21. using Microsoft.VisualStudio.TestTools.UnitTesting;
  22. using Vannatech.CoreAudio.Constants;
  23. using Vannatech.CoreAudio.Enumerations;
  24. using Vannatech.CoreAudio.Interfaces;
  25. namespace CoreAudioTests.MMDeviceApi
  26. {
  27. /// <summary>
  28. /// Tests all methods of the IMMDeviceEnumerator interface.
  29. /// </summary>
  30. [TestClass]
  31. public class IMMDeviceEnumeratorTest
  32. {
  33. /// <summary>
  34. /// This test method does nothing. Testing of the EnumAudioEndpoints method is implicit by testing other aspects of the IMMDevice API.
  35. /// </summary>
  36. [TestMethod]
  37. public void IMMDeviceEnumerator_EnumAudioEndpoints()
  38. {
  39. // This method is thouroughly tested through various other unit tests.
  40. // The entry point for most other tests starts with calling EnumAudioEndpoints.
  41. // TODO: Add specific test for this.
  42. }
  43. /// <summary>
  44. /// Tests that the default audio endpoint for all combinations of data flow and roles can be created with S_OK HRESULT and that each device is not null.
  45. /// </summary>
  46. [TestMethod]
  47. public void IMMDeviceEnumerator_GetDefaultAudioEndpoint()
  48. {
  49. int result = 0;
  50. IMMDevice device = null;
  51. var enumerator = TestUtilities.CreateIMMDeviceEnumerator();
  52. // data flow - eAll (this should always produce HRESULT of E_INVALIDARG, which is 0x80070057)
  53. result = enumerator.GetDefaultAudioEndpoint(EDataFlow.eAll, ERole.eCommunications, out device);
  54. Assert.AreEqual(0x80070057, (uint)result);
  55. result = enumerator.GetDefaultAudioEndpoint(EDataFlow.eAll, ERole.eConsole, out device);
  56. Assert.AreEqual(0x80070057, (uint)result);
  57. result = enumerator.GetDefaultAudioEndpoint(EDataFlow.eAll, ERole.eMultimedia, out device);
  58. Assert.AreEqual(0x80070057, (uint)result);
  59. // data flow - eCapture
  60. result = enumerator.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications, out device);
  61. AssertCoreAudio.IsHResultOk(result);
  62. Assert.IsNotNull(device);
  63. result = enumerator.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eConsole, out device);
  64. AssertCoreAudio.IsHResultOk(result);
  65. Assert.IsNotNull(device);
  66. result = enumerator.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia, out device);
  67. AssertCoreAudio.IsHResultOk(result);
  68. Assert.IsNotNull(device);
  69. // data flow - eRender
  70. result = enumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications, out device);
  71. AssertCoreAudio.IsHResultOk(result);
  72. Assert.IsNotNull(device);
  73. result = enumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eConsole, out device);
  74. AssertCoreAudio.IsHResultOk(result);
  75. Assert.IsNotNull(device);
  76. result = enumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out device);
  77. AssertCoreAudio.IsHResultOk(result);
  78. Assert.IsNotNull(device);
  79. }
  80. /// <summary>
  81. /// Tests that the GetDevice method can get each audio device individually, by ID.
  82. /// </summary>
  83. [TestMethod]
  84. public void IMMDeviceEnumerator_GetDevice()
  85. {
  86. int result = 0;
  87. var enumerator = TestUtilities.CreateIMMDeviceEnumerator();
  88. var allDevices = TestUtilities.CreateIMMDeviceCollection(EDataFlow.eAll, DEVICE_STATE_XXX.DEVICE_STATEMASK_ALL);
  89. foreach (var device in allDevices)
  90. {
  91. // Get the device ID.
  92. string deviceId = null;
  93. result = device.GetId(out deviceId);
  94. AssertCoreAudio.IsHResultOk(result);
  95. Assert.IsNotNull(deviceId, "The device string is null.");
  96. // Get the IMMDevice directly from the ID.
  97. IMMDevice deviceFromId = null;
  98. result = enumerator.GetDevice(deviceId, out deviceFromId);
  99. AssertCoreAudio.IsHResultOk(result);
  100. Assert.IsNotNull(deviceFromId, "The IMMDevice object is null.");
  101. // Ensure the IDs of each device match.
  102. string deviceId2 = null;
  103. result = deviceFromId.GetId(out deviceId2);
  104. AssertCoreAudio.IsHResultOk(result);
  105. Assert.IsNotNull(deviceId2, "The device string is null.");
  106. Assert.AreEqual(deviceId, deviceId2, "The device IDs are not equal.");
  107. }
  108. }
  109. /// <summary>
  110. /// Tests that a valid client can be registered and an HRESULT of S_OK is returned.
  111. /// </summary>
  112. [TestMethod]
  113. public void IMMDeviceEnumerator_RegisterEndpointNotificationCallback()
  114. {
  115. int result = 0;
  116. var enumerator = TestUtilities.CreateIMMDeviceEnumerator();
  117. var client = new MMDeviceNotifyClient();
  118. result = enumerator.RegisterEndpointNotificationCallback(client);
  119. AssertCoreAudio.IsHResultOk(result);
  120. }
  121. /// <summary>
  122. /// Tests that a previously registered client can be unregistered with HRESULT of S_OK. Also tests that unregistration of an invalid client will fail.
  123. /// </summary>
  124. [TestMethod]
  125. public void IMMDeviceEnumerator_UnregisterEndpointNotificationCallback()
  126. {
  127. int result = 0;
  128. var enumerator = TestUtilities.CreateIMMDeviceEnumerator();
  129. // Test for unregistering a valid client.
  130. var client = new MMDeviceNotifyClient();
  131. result = enumerator.RegisterEndpointNotificationCallback(client);
  132. AssertCoreAudio.IsHResultOk(result);
  133. result = enumerator.UnregisterEndpointNotificationCallback(client);
  134. AssertCoreAudio.IsHResultOk(result);
  135. // Test for unregistering a non-registered client (should fail with HRESULT of ELEMENT_NOT_FOUND).
  136. result = enumerator.UnregisterEndpointNotificationCallback(new MMDeviceNotifyClient());
  137. Assert.AreEqual(TestUtilities.HRESULTS.E_NOTFOUND, (uint)result);
  138. }
  139. }
  140. }