IAudioEndpointVolumeTest.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 IAudioEndpointVolume interface.
  29. /// </summary>
  30. [TestClass]
  31. public class IAudioEndpointVolumeTest : TestClass<IAudioEndpointVolume>
  32. {
  33. /// <summary>
  34. /// Tests that the channel count can be received for each available endpoint, and an HRESULT of S_OK is returned.
  35. /// </summary>
  36. [TestMethod]
  37. public void IAudioEndpointVolume_GetChannelCount()
  38. {
  39. ExecuteDeviceActivationTest(activation =>
  40. {
  41. var count = UInt32.MaxValue;
  42. var result = activation.GetChannelCount(out count);
  43. AssertCoreAudio.IsHResultOk(result);
  44. Assert.AreNotEqual(uint.MaxValue, count, "The channel count value was not received.");
  45. });
  46. }
  47. /// <summary>
  48. /// Tests that the volume level can be obtained for each available endpoint channel.
  49. /// </summary>
  50. [TestMethod]
  51. public void IAudioEndpointVolume_GetChannelVolumeLevel()
  52. {
  53. var tested = false;
  54. ExecuteDeviceActivationTest(activation =>
  55. {
  56. var count = UInt32.MaxValue;
  57. activation.GetChannelCount(out count);
  58. for (uint i = 0; i < count; i++)
  59. {
  60. float level = 123.456f;
  61. var result = activation.GetChannelVolumeLevel(i, out level);
  62. AssertCoreAudio.IsHResultOk(result);
  63. Assert.AreNotEqual(123.456f, level, "The level value was not received.");
  64. tested = true;
  65. }
  66. });
  67. if (!tested) Assert.Inconclusive("No channels were available to test against.");
  68. }
  69. /// <summary>
  70. /// Tests that the scalar volume level can be obtained for each available endpoint channel.
  71. /// </summary>
  72. [TestMethod]
  73. public void IAudioEndpointVolume_GetChannelVolumeLevelScalar()
  74. {
  75. var tested = false;
  76. ExecuteDeviceActivationTest(activation =>
  77. {
  78. var count = UInt32.MaxValue;
  79. activation.GetChannelCount(out count);
  80. for (uint i = 0; i < count; i++)
  81. {
  82. float level = 123.456f;
  83. var result = activation.GetChannelVolumeLevelScalar(i, out level);
  84. AssertCoreAudio.IsHResultOk(result);
  85. Assert.AreNotEqual(123.456f, level, "The level value was not received.");
  86. tested = true;
  87. }
  88. });
  89. if (!tested) Assert.Inconclusive("No channels were available to test against.");
  90. }
  91. /// <summary>
  92. /// Tests that the master volume level can be obtained for each available endpoint.
  93. /// </summary>
  94. [TestMethod]
  95. public void IAudioEndpointVolume_GetMasterVolumeLevel()
  96. {
  97. ExecuteDeviceActivationTest(activation =>
  98. {
  99. float level = 123.456f;
  100. var result = activation.GetMasterVolumeLevel(out level);
  101. AssertCoreAudio.IsHResultOk(result);
  102. Assert.AreNotEqual(123.456f, level, "The level value was not received.");
  103. });
  104. }
  105. /// <summary>
  106. /// Tests that the master scalar volume level can be obtained for each available endpoint.
  107. /// </summary>
  108. [TestMethod]
  109. public void IAudioEndpointVolume_GetMasterVolumeLevelScalar()
  110. {
  111. ExecuteDeviceActivationTest(activation =>
  112. {
  113. float level = 123.456f;
  114. var result = activation.GetMasterVolumeLevelScalar(out level);
  115. AssertCoreAudio.IsHResultOk(result);
  116. Assert.AreNotEqual(123.456f, level, "The level value was not received.");
  117. });
  118. }
  119. /// <summary>
  120. /// Tests that the muting state can be received for each available endpoint, with an HRESULT of S_OK returned.
  121. /// </summary>
  122. [TestMethod]
  123. public void IAudioEndpointVolume_GetMute()
  124. {
  125. ExecuteDeviceActivationTest(activation =>
  126. {
  127. bool valOne = true, valTwo = false;
  128. var result = activation.GetMute(out valOne);
  129. AssertCoreAudio.IsHResultOk(result);
  130. result = activation.GetMute(out valTwo);
  131. AssertCoreAudio.IsHResultOk(result);
  132. Assert.AreEqual(valOne, valTwo, "The mute state was not received.");
  133. });
  134. }
  135. /// <summary>
  136. /// Tests that the volume ranges for each available endpoint can be received.
  137. /// </summary>
  138. [TestMethod]
  139. public void IAudioEndpointVolume_GetVolumeRange()
  140. {
  141. ExecuteDeviceActivationTest(activation =>
  142. {
  143. float volumeMin = 123.456f, volumeMax = 123.456f, volumeStep = 123.456f;
  144. var result = activation.GetVolumeRange(out volumeMin, out volumeMax, out volumeStep);
  145. AssertCoreAudio.IsHResultOk(result);
  146. Assert.AreNotEqual(123.456f, volumeMin, "The min volume value was not received.");
  147. Assert.AreNotEqual(123.456f, volumeMax, "The max volume value was not received.");
  148. Assert.AreNotEqual(123.456f, volumeStep, "The volume step value was not received.");
  149. });
  150. }
  151. /// <summary>
  152. /// Tests that the volume step info for each available endpoint can be received.
  153. /// </summary>
  154. [TestMethod]
  155. public void IAudioEndpointVolume_GetVolumeStepInfo()
  156. {
  157. ExecuteDeviceActivationTest(activation =>
  158. {
  159. var step = UInt32.MaxValue;
  160. var stepCount = UInt32.MaxValue;
  161. var result = activation.GetVolumeStepInfo(out step, out stepCount);
  162. AssertCoreAudio.IsHResultOk(result);
  163. Assert.AreNotEqual(UInt32.MaxValue, step, "The step value was not received.");
  164. Assert.AreNotEqual(UInt32.MaxValue, stepCount, "The step count value was not received.");
  165. });
  166. }
  167. /// <summary>
  168. /// Tests that the hardware support flags for each available endpoint can be received, and are within a valid range.
  169. /// </summary>
  170. [TestMethod]
  171. public void IAudioEndpointVolume_QueryHardwareSupport()
  172. {
  173. ExecuteDeviceActivationTest(activation =>
  174. {
  175. var mask = UInt32.MaxValue;
  176. var result = activation.QueryHardwareSupport(out mask);
  177. AssertCoreAudio.IsHResultOk(result);
  178. Assert.IsTrue((mask >= 0) && (mask <= 7), "The hardware mask is not in the valid range.");
  179. });
  180. }
  181. /// <summary>
  182. ///
  183. /// </summary>
  184. [TestMethod]
  185. public void IAudioEndpointVolume_RegisterControlChangeNotify()
  186. {
  187. ExecuteDeviceActivationTest(activation =>
  188. {
  189. var client = new AudioEndpointVolumeCallback();
  190. var result = activation.RegisterControlChangeNotify(client);
  191. AssertCoreAudio.IsHResultOk(result);
  192. });
  193. }
  194. /// <summary>
  195. /// Tests that the volume level can be set for each channel on each available endpoint.
  196. /// </summary>
  197. [TestMethod]
  198. public void IAudioEndpointVolume_SetChannelVolumeLevel()
  199. {
  200. var tested = false;
  201. ExecuteDeviceActivationTest(activation =>
  202. {
  203. UInt32 count;
  204. activation.GetChannelCount(out count);
  205. for (uint i = 0; i < count; i++)
  206. {
  207. // determine valid range.
  208. float volumeMin, volumeMax, volumeStep;
  209. activation.GetVolumeRange(out volumeMin, out volumeMax, out volumeStep);
  210. // get the original level.
  211. float levelOrig, levelNew, levelCheck;
  212. activation.GetChannelVolumeLevel(i, out levelOrig);
  213. // create a random valid level
  214. var rand = new Random();
  215. levelNew = (float)rand.Next((int)volumeMin, (int)volumeMax);
  216. // set the new level.
  217. Guid context = Guid.NewGuid();
  218. var result = activation.SetChannelVolumeLevel(i, levelNew, context);
  219. AssertCoreAudio.IsHResultOk(result);
  220. // check that the level was set.
  221. activation.GetChannelVolumeLevel(i, out levelCheck);
  222. Assert.AreEqual(levelNew, levelCheck, volumeStep, "The new volume level was not set properly.");
  223. // reset the level to the original.
  224. result = activation.SetChannelVolumeLevel(i, levelOrig, context);
  225. AssertCoreAudio.IsHResultOk(result);
  226. tested = true;
  227. }
  228. });
  229. if(!tested) Assert.Inconclusive("No channels were available to test against.");
  230. }
  231. /// <summary>
  232. /// Tests that the scalar volume level can be set for each channel on each available endpoint.
  233. /// </summary>
  234. [TestMethod]
  235. public void IAudioEndpointVolume_SetChannelVolumeLevelScalar()
  236. {
  237. var tested = false;
  238. ExecuteDeviceActivationTest(activation =>
  239. {
  240. UInt32 count;
  241. activation.GetChannelCount(out count);
  242. for (uint i = 0; i < count; i++)
  243. {
  244. // get the original level.
  245. float levelOrig, levelNew, levelCheck;
  246. activation.GetChannelVolumeLevelScalar(i, out levelOrig);
  247. // create a random valid level
  248. var rand = new Random();
  249. levelNew = (float)rand.NextDouble();
  250. // set the new level.
  251. Guid context = Guid.NewGuid();
  252. var result = activation.SetChannelVolumeLevelScalar(i, levelNew, context);
  253. AssertCoreAudio.IsHResultOk(result);
  254. // check that the level was set.
  255. activation.GetChannelVolumeLevelScalar(i, out levelCheck);
  256. Assert.AreEqual(levelNew, levelCheck, 0.001f, "The new volume level was not set properly.");
  257. // reset the level to the original.
  258. result = activation.SetChannelVolumeLevelScalar(i, levelOrig, context);
  259. AssertCoreAudio.IsHResultOk(result);
  260. tested = true;
  261. }
  262. });
  263. if (!tested) Assert.Inconclusive("No channels were available to test against.");
  264. }
  265. /// <summary>
  266. /// Tests that the master volume level can be set for each available endpoint.
  267. /// </summary>
  268. [TestMethod]
  269. public void IAudioEndpointVolume_SetMasterVolumeLevel()
  270. {
  271. ExecuteDeviceActivationTest(activation =>
  272. {
  273. // determine valid range.
  274. float volumeMin, volumeMax, volumeStep;
  275. activation.GetVolumeRange(out volumeMin, out volumeMax, out volumeStep);
  276. // get the original level.
  277. float levelOrig, levelNew, levelCheck;
  278. activation.GetMasterVolumeLevel(out levelOrig);
  279. // create a random valid level
  280. var rand = new Random();
  281. levelNew = (float)rand.Next((int)volumeMin, (int)volumeMax);
  282. // set the new level.
  283. Guid context = Guid.NewGuid();
  284. var result = activation.SetMasterVolumeLevel(levelNew, context);
  285. AssertCoreAudio.IsHResultOk(result);
  286. // check that the level was set.
  287. activation.GetMasterVolumeLevel(out levelCheck);
  288. Assert.AreEqual(levelNew, levelCheck, volumeStep, "The new volume level was not set properly.");
  289. // reset the level to the original.
  290. result = activation.SetMasterVolumeLevel(levelOrig, context);
  291. });
  292. }
  293. /// <summary>
  294. /// Tests that the master scalar volume level can be set for each available endpoint.
  295. /// </summary>
  296. [TestMethod]
  297. public void IAudioEndpointVolume_SetMasterVolumeLevelScalar()
  298. {
  299. ExecuteDeviceActivationTest(activation =>
  300. {
  301. // get the original level.
  302. float levelOrig, levelNew, levelCheck;
  303. activation.GetMasterVolumeLevelScalar(out levelOrig);
  304. // create a random valid level
  305. var rand = new Random();
  306. levelNew = (float)rand.NextDouble();
  307. // set the new level.
  308. Guid context = Guid.NewGuid();
  309. var result = activation.SetMasterVolumeLevelScalar(levelNew, context);
  310. AssertCoreAudio.IsHResultOk(result);
  311. // check that the level was set.
  312. activation.GetMasterVolumeLevelScalar(out levelCheck);
  313. Assert.AreEqual(levelNew, levelCheck, 0.001f, "The new volume level was not set properly.");
  314. // reset the level to the original.
  315. result = activation.SetMasterVolumeLevelScalar(levelOrig, context);
  316. });
  317. }
  318. /// <summary>
  319. /// Tests that the muting state can be set for each available endpoint.
  320. /// </summary>
  321. [TestMethod]
  322. public void IAudioEndpointVolume_SetMute()
  323. {
  324. ExecuteDeviceActivationTest(activation =>
  325. {
  326. // get the original mute state.
  327. bool muteOrig, muteNew, muteCheck;
  328. activation.GetMute(out muteOrig);
  329. muteNew = !muteOrig;
  330. // change it to the opposite.
  331. Guid context = Guid.NewGuid();
  332. var result = activation.SetMute(muteNew, context);
  333. AssertCoreAudio.IsHResultOk(result);
  334. // check that the value changed.
  335. activation.GetMute(out muteCheck);
  336. Assert.AreEqual(muteNew, muteCheck, "The muting state was not set properly.");
  337. // return to original state.
  338. result = activation.SetMute(muteOrig, context);
  339. AssertCoreAudio.IsHResultOk(result);
  340. });
  341. }
  342. /// <summary>
  343. ///
  344. /// </summary>
  345. [TestMethod]
  346. public void IAudioEndpointVolume_UnregisterControlChangeNotify()
  347. {
  348. ExecuteDeviceActivationTest(activation =>
  349. {
  350. var client = new AudioEndpointVolumeCallback();
  351. activation.RegisterControlChangeNotify(client);
  352. var result = activation.UnregisterControlChangeNotify(client);
  353. AssertCoreAudio.IsHResultOk(result);
  354. });
  355. }
  356. /// <summary>
  357. /// Tests that the volume step down decrements the step by one, for each available endpoint.
  358. /// </summary>
  359. [TestMethod]
  360. public void IAudioEndpointVolume_VolumeStepDown()
  361. {
  362. ExecuteDeviceActivationTest(activation =>
  363. {
  364. // get the original value.
  365. float levelOrig;
  366. activation.GetMasterVolumeLevelScalar(out levelOrig);
  367. // set to a volume in the middle.
  368. Guid context = Guid.NewGuid();
  369. activation.SetMasterVolumeLevelScalar(0.5f, context);
  370. // get the initial step info
  371. UInt32 step, stepCount;
  372. activation.GetVolumeStepInfo(out step, out stepCount);
  373. // determine valid number of steps to take.
  374. uint stepsToTake = (uint)Math.Floor(stepCount / 2.0f);
  375. while (stepsToTake > 0)
  376. {
  377. // step down the volume.
  378. var result = activation.VolumeStepDown(context);
  379. AssertCoreAudio.IsHResultOk(result);
  380. // check that the volume step decremented one.
  381. var previousStep = step;
  382. activation.GetVolumeStepInfo(out step, out stepCount);
  383. Assert.AreEqual(previousStep - 1, step, "The volume step did not decrement.");
  384. stepsToTake--;
  385. }
  386. // reset the volume level to original value.
  387. activation.SetMasterVolumeLevelScalar(levelOrig, context);
  388. });
  389. }
  390. /// <summary>
  391. /// Tests that the volume step up increments the step by one, for each available endpoint.
  392. /// </summary>
  393. [TestMethod]
  394. public void IAudioEndpointVolume_VolumeStepUp()
  395. {
  396. ExecuteDeviceActivationTest(activation =>
  397. {
  398. // get the original value.
  399. float levelOrig;
  400. activation.GetMasterVolumeLevelScalar(out levelOrig);
  401. // set to a volume in the middle.
  402. Guid context = Guid.NewGuid();
  403. activation.SetMasterVolumeLevelScalar(0.5f, context);
  404. // get the initial step info
  405. UInt32 step, stepCount;
  406. activation.GetVolumeStepInfo(out step, out stepCount);
  407. // determine valid number of steps to take.
  408. uint stepsToTake = (uint)Math.Floor(stepCount / 2.0f);
  409. while (stepsToTake > 0)
  410. {
  411. // step up the volume.
  412. var result = activation.VolumeStepUp(context);
  413. AssertCoreAudio.IsHResultOk(result);
  414. // check that the volume step decremented one.
  415. uint previousStep = step;
  416. activation.GetVolumeStepInfo(out step, out stepCount);
  417. Assert.AreEqual(previousStep + 1, step, "The volume step did not increment.");
  418. stepsToTake--;
  419. }
  420. // reset the volume level to original value.
  421. activation.SetMasterVolumeLevelScalar(levelOrig, context);
  422. });
  423. }
  424. }
  425. }