IAudioEndpointVolumeCallbackTest.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.Interfaces;
  23. using Vannatech.CoreAudio.Constants;
  24. using Vannatech.CoreAudio.Structures;
  25. namespace CoreAudioTests.EndpointVolumeApi
  26. {
  27. /// <summary>
  28. /// Tests all methods of the IAudioEndpointVolumeCallback interface.
  29. /// </summary>
  30. [TestClass]
  31. public class IAudioEndpointVolumeCallbackTest : TestClass<IAudioEndpointVolume>
  32. {
  33. /// <summary>
  34. ///
  35. /// </summary>
  36. [TestMethod]
  37. public void IAudioEndpointVolumeCallback_OnNotify()
  38. {
  39. ExecuteDeviceActivationTest(activation =>
  40. {
  41. var context = Guid.NewGuid();
  42. var client = new AudioEndpointVolumeCallback();
  43. activation.RegisterControlChangeNotify(client);
  44. client.SetExpected("MuteState", new AUDIO_VOLUME_NOTIFICATION_DATA
  45. {
  46. EventContext = context,
  47. IsMuted = true
  48. });
  49. System.Threading.Thread.Sleep(100);
  50. activation.SetMute(true, context);
  51. var loopCount = 0;
  52. while (client.Result == -1 && loopCount < 20)
  53. {
  54. System.Threading.Thread.Sleep(100);
  55. loopCount++;
  56. }
  57. if (client.Result == -1)
  58. Assert.Inconclusive("The result was not received.");
  59. else if (client.Result == 1)
  60. Assert.Fail("The client did not receive the correct notification.");
  61. client.SetExpected("MasterVolume", new AUDIO_VOLUME_NOTIFICATION_DATA
  62. {
  63. EventContext = context,
  64. MasterVolume = -10.0f
  65. });
  66. System.Threading.Thread.Sleep(100);
  67. activation.SetMasterVolumeLevel(-10.0f, context);
  68. loopCount = 0;
  69. while (client.Result == -1 && loopCount < 20)
  70. {
  71. System.Threading.Thread.Sleep(100);
  72. loopCount++;
  73. }
  74. if (client.Result == -1)
  75. Assert.Inconclusive("The result was not received.");
  76. else if (client.Result == 1)
  77. Assert.Fail("The client did not receive the correct notification.");
  78. activation.UnregisterControlChangeNotify(client);
  79. });
  80. }
  81. }
  82. /// <summary>
  83. /// Local class used to test methods that require an object implementing IAudioEndpointVolumeCallback
  84. /// </summary>
  85. internal class AudioEndpointVolumeCallback : IAudioEndpointVolumeCallback
  86. {
  87. private bool _tested;
  88. private bool _passed;
  89. private string _type;
  90. private AUDIO_VOLUME_NOTIFICATION_DATA _expected;
  91. public int OnNotify(IntPtr dataPtr)
  92. {
  93. if(String.IsNullOrEmpty(_type)) return 0;
  94. var localType = _type;
  95. _type = null;
  96. _tested = true;
  97. AUDIO_VOLUME_NOTIFICATION_DATA notificationData = (AUDIO_VOLUME_NOTIFICATION_DATA)System.Runtime.InteropServices.Marshal.PtrToStructure(dataPtr, typeof(AUDIO_VOLUME_NOTIFICATION_DATA));
  98. Assert.AreEqual(_expected.EventContext, notificationData.EventContext);
  99. switch (localType)
  100. {
  101. case "MuteState":
  102. Assert.AreEqual(_expected.IsMuted, notificationData.IsMuted);
  103. break;
  104. case "MasterVolume":
  105. Assert.AreEqual(_expected.MasterVolume, notificationData.MasterVolume);
  106. break;
  107. }
  108. _passed = true;
  109. return 0;
  110. }
  111. public void SetExpected(string type, AUDIO_VOLUME_NOTIFICATION_DATA expected)
  112. {
  113. _type = type;
  114. _expected = expected;
  115. _tested = false;
  116. _passed = false;
  117. }
  118. public int Result
  119. {
  120. get
  121. {
  122. if (!_tested) return -1;
  123. else if (_tested && _passed) return 0;
  124. else return 1;
  125. }
  126. }
  127. }
  128. }