IPerChannelDbLevelTest.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. namespace CoreAudioTests.DeviceTopologyApi
  25. {
  26. /// <summary>
  27. /// Provides test methods for each interface that derives from IPerChannelDbLevel
  28. /// </summary>
  29. public class IPerChannelDbLevelTest<T> : TestClass<T> where T : IPerChannelDbLevel
  30. {
  31. // No methods are called directly against this interface.
  32. // It only serves as a base for the following:
  33. // * IAudioBass
  34. // * IAudioMidrange
  35. // * IAudioTreble
  36. // * IAudioVolumeLevel
  37. // Methods below server as common tests for each of the derived interfaces.
  38. internal void ExecuteGetChannelCountTest()
  39. {
  40. ExecutePartActivationTest(activation =>
  41. {
  42. var count = UInt32.MaxValue;
  43. var result = activation.GetChannelCount(out count);
  44. AssertCoreAudio.IsHResultOk(result);
  45. Assert.AreNotEqual(UInt32.MaxValue, count, "The channel count was not received.");
  46. });
  47. }
  48. internal void ExecuteGetLevelTest()
  49. {
  50. var tested = false;
  51. ExecutePartActivationTest(activation =>
  52. {
  53. var count = UInt32.MaxValue;
  54. activation.GetChannelCount(out count);
  55. for (uint i = 0; i < count; i++)
  56. {
  57. var level = 123.456f;
  58. var result = activation.GetLevel(i, out level);
  59. AssertCoreAudio.IsHResultOk(result);
  60. Assert.AreNotEqual(123.456f, level, "The level was not received.");
  61. tested = true;
  62. }
  63. });
  64. if (!tested) Assert.Inconclusive("No channels were available to test against.");
  65. }
  66. internal void ExecuteGetLevelRangeTest()
  67. {
  68. var tested = false;
  69. ExecutePartActivationTest(activation =>
  70. {
  71. var count = UInt32.MaxValue;
  72. activation.GetChannelCount(out count);
  73. for (uint i = 0; i < count; i++)
  74. {
  75. float volMin = 123.456f, volMax = 123.456f, volStep = 123.456f;
  76. var result = activation.GetLevelRange(i, out volMin, out volMax, out volStep);
  77. AssertCoreAudio.IsHResultOk(result);
  78. Assert.AreNotEqual(123.456f, volMin, "The minimum volume was not received.");
  79. Assert.AreNotEqual(123.456f, volMax, "The maximum volume was not received.");
  80. Assert.AreNotEqual(123.456f, volStep, "The volume increment was not received.");
  81. tested = true;
  82. }
  83. });
  84. if (!tested) Assert.Inconclusive("No channels were available to test against.");
  85. }
  86. internal void ExecuteSetLevelTest()
  87. {
  88. var tested = false;
  89. ExecutePartActivationTest(activation =>
  90. {
  91. var count = UInt32.MaxValue;
  92. activation.GetChannelCount(out count);
  93. for (uint i = 0; i < count; i++)
  94. {
  95. float levelOrig, levelNew;
  96. activation.GetLevel(i, out levelOrig);
  97. float volMin, volMax, volStep;
  98. var result = activation.GetLevelRange(i, out volMin, out volMax, out volStep);
  99. var context = Guid.NewGuid();
  100. result = activation.SetLevel(i, volMin + volStep, context);
  101. activation.GetLevel(i, out levelNew);
  102. AssertCoreAudio.IsHResultOk(result);
  103. Assert.AreEqual(volMin + volStep, levelNew, volStep, "The channel volume was not set properly.");
  104. result = activation.SetLevel(i, volMax - volStep, context);
  105. activation.GetLevel(i, out levelNew);
  106. AssertCoreAudio.IsHResultOk(result);
  107. Assert.AreEqual(volMax - volStep, levelNew, volStep, "The channel volume was not set properly.");
  108. activation.SetLevel(i, levelOrig, context);
  109. tested = true;
  110. }
  111. });
  112. if (!tested) Assert.Inconclusive("No channels were available to test against.");
  113. }
  114. internal void ExecuteSetLevelAllChannelsTest()
  115. {
  116. var tested = false;
  117. ExecutePartActivationTest(activation =>
  118. {
  119. var count = UInt32.MaxValue;
  120. activation.GetChannelCount(out count);
  121. var levelsOrig = new float[count];
  122. var levelsLow = new float[count];
  123. var levelsHigh = new float[count];
  124. float volStep = 0f;
  125. for (uint i = 0; i < count; i++)
  126. {
  127. activation.GetLevel(i, out levelsOrig[i]);
  128. float volMin, volMax;
  129. activation.GetLevelRange(i, out volMin, out volMax, out volStep);
  130. levelsLow[i] = volMin + volStep;
  131. levelsHigh[i] = volMax - volStep;
  132. }
  133. var context = Guid.NewGuid();
  134. var result = activation.SetLevelAllChannels(levelsLow, count, context);
  135. AssertCoreAudio.IsHResultOk(result);
  136. for (uint i = 0; i < count; i++)
  137. {
  138. float levelNew;
  139. activation.GetLevel(i, out levelNew);
  140. Assert.AreEqual(levelsLow[i], levelNew, volStep, "The channel volume was not set properly.");
  141. }
  142. result = activation.SetLevelAllChannels(levelsHigh, count, context);
  143. AssertCoreAudio.IsHResultOk(result);
  144. for (uint i = 0; i < count; i++)
  145. {
  146. float levelNew;
  147. activation.GetLevel(i, out levelNew);
  148. Assert.AreEqual(levelsHigh[i], levelNew, volStep, "The channel volume was not set properly.");
  149. }
  150. // return to original levels
  151. for (uint i = 0; i < count; i++)
  152. {
  153. activation.SetLevel(i, levelsOrig[i], context);
  154. tested = true;
  155. }
  156. });
  157. if (!tested) Assert.Inconclusive("No channels were available to test against.");
  158. }
  159. internal void ExecuteSetLevelUniformTest()
  160. {
  161. var tested = false;
  162. ExecutePartActivationTest(activation =>
  163. {
  164. UInt32 count;
  165. activation.GetChannelCount(out count);
  166. var levelsOrig = new float[count];
  167. float volLow = float.MinValue, volHigh = float.MaxValue, volStep = 0f;
  168. // Get original values.
  169. for (uint i = 0; i < count; i++)
  170. {
  171. activation.GetLevel(i, out levelsOrig[i]);
  172. float volMin, volMax;
  173. activation.GetLevelRange(i, out volMin, out volMax, out volStep);
  174. if (volMin > volLow)
  175. volLow = volMin;
  176. if (volMax < volHigh)
  177. volHigh = volMax;
  178. }
  179. volLow += volStep;
  180. volHigh -= volStep;
  181. if (volLow > volHigh) Assert.Inconclusive("The minimum volume is less than the maximum. Thist will not result in a valid test.");
  182. // Verify setting low value.
  183. var context = Guid.NewGuid();
  184. var result = activation.SetLevelUniform(volLow, context);
  185. AssertCoreAudio.IsHResultOk(result);
  186. for (uint i = 0; i < count; i++)
  187. {
  188. float levelNew;
  189. activation.GetLevel(i, out levelNew);
  190. Assert.AreEqual(volLow, levelNew, volStep, "The channel volume was not set properly.");
  191. }
  192. // Verify setting high value.
  193. result = activation.SetLevelUniform(volHigh, context);
  194. AssertCoreAudio.IsHResultOk(result);
  195. for (uint i = 0; i < count; i++)
  196. {
  197. float levelNew;
  198. activation.GetLevel(i, out levelNew);
  199. Assert.AreEqual(volHigh, levelNew, volStep, "The channel volume was not set properly.");
  200. }
  201. // return to original levels
  202. for (uint i = 0; i < count; i++)
  203. {
  204. activation.SetLevel(i, levelsOrig[i], context);
  205. tested = true;
  206. }
  207. });
  208. if (!tested) Assert.Inconclusive("No channels were available to test against.");
  209. }
  210. }
  211. }