IConnectorTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 System.Linq;
  21. using CoreAudioTests.Common;
  22. using Microsoft.VisualStudio.TestTools.UnitTesting;
  23. using Vannatech.CoreAudio.Interfaces;
  24. using Vannatech.CoreAudio.Constants;
  25. using System.Collections.Generic;
  26. using Vannatech.CoreAudio.Enumerations;
  27. namespace CoreAudioTests.DeviceTopologyApi
  28. {
  29. // TODO: Create a custom test manager for IConnector tests, and properly dispose of COM interfaces.
  30. /// <summary>
  31. /// Tests all methods of the IConnector interface.
  32. /// </summary>
  33. [TestClass]
  34. public class IConnectorTest
  35. {
  36. /// <summary>
  37. /// Tests that each available connector in the system may be connectors to other connectors.
  38. /// </summary>
  39. [TestMethod]
  40. public void IConnector_ConnectTo()
  41. {
  42. int result = 0;
  43. IEnumerable<IConnector> inputConnectors, outputConnectors;
  44. ToConnectors(TestUtilities.CreateIPartCollection(), out inputConnectors, out outputConnectors);
  45. foreach (var cIn in inputConnectors)
  46. {
  47. bool isConnected;
  48. IConnector inOrig = null;
  49. cIn.IsConnected(out isConnected);
  50. if (isConnected)
  51. cIn.GetConnectedTo(out inOrig);
  52. // New try connection to each output connector
  53. foreach (var cOut in outputConnectors)
  54. {
  55. IConnector outOrig;
  56. cOut.GetConnectedTo(out outOrig);
  57. // Start by disconnecting the connectors
  58. cIn.Disconnect();
  59. cOut.Disconnect();
  60. // Connect the input and output
  61. result = cIn.ConnectTo(cOut);
  62. AssertCoreAudio.IsHResultOk(result);
  63. // Verify the connection was made.
  64. string cInId, idVerify;
  65. ((IPart)cIn).GetGlobalId(out cInId);
  66. cOut.GetConnectorIdConnectedTo(out idVerify);
  67. Assert.AreEqual(cInId, idVerify, "The connector IDs do not match.");
  68. // Return the output connector to original state.
  69. cOut.Disconnect();
  70. cOut.ConnectTo(outOrig);
  71. }
  72. // Return the input connector to original state.
  73. cIn.Disconnect();
  74. if (inOrig != null)
  75. cIn.ConnectTo(inOrig);
  76. }
  77. }
  78. /// <summary>
  79. /// Tests that each available connector in the system may be disconnected.
  80. /// </summary>
  81. [TestMethod]
  82. public void IConnector_Disconnect()
  83. {
  84. int result = 0;
  85. IEnumerable<IConnector> allConnectors;
  86. ToConnectors(TestUtilities.CreateIPartCollection(), out allConnectors);
  87. foreach (var con in allConnectors)
  88. {
  89. bool isConnected;
  90. IConnector conOrig = null;
  91. con.IsConnected(out isConnected);
  92. if (isConnected)
  93. con.GetConnectedTo(out conOrig);
  94. result = con.Disconnect();
  95. // HRESULT should either be S_OK, or E_NOTFOUND
  96. // E_NOTFOUND is returned when Disconnect method is called on a connector that is already disconnected.
  97. if (isConnected) AssertCoreAudio.IsHResultOk(result);
  98. else Assert.AreEqual(TestUtilities.HRESULTS.E_NOTFOUND, (uint)result, "A disconnected connector did not return E_NOTFOUND HRESULT.");
  99. // Verify connected state is now false.
  100. con.IsConnected(out isConnected);
  101. Assert.IsFalse(isConnected, "The connector was not properly disconnected.");
  102. // Return connector to original state.
  103. if (conOrig != null)
  104. con.ConnectTo(conOrig);
  105. }
  106. }
  107. /// <summary>
  108. /// Tests that the global ID may be received, for each available connector in the system.
  109. /// </summary>
  110. [TestMethod]
  111. public void IConnector_GetConnectorIdConnectedTo()
  112. {
  113. int result = 0;
  114. IEnumerable<IConnector> allConnectors;
  115. ToConnectors(TestUtilities.CreateIPartCollection(), out allConnectors);
  116. foreach (var con in allConnectors)
  117. {
  118. bool isConnected;
  119. con.IsConnected(out isConnected);
  120. if (isConnected)
  121. {
  122. IConnector cTo;
  123. con.GetConnectedTo(out cTo);
  124. string expectedId, idConTo;
  125. ((IPart)cTo).GetGlobalId(out expectedId);
  126. result = con.GetConnectorIdConnectedTo(out idConTo);
  127. AssertCoreAudio.IsHResultOk(result);
  128. Assert.AreEqual(expectedId, idConTo, "The connected to ID does not match the actual ID of the part.");
  129. }
  130. }
  131. }
  132. /// <summary>
  133. /// Tests that the other connector may be received, for each applicable connector in the system.
  134. /// </summary>
  135. [TestMethod]
  136. public void IConnector_GetConnectedTo()
  137. {
  138. int result = 0;
  139. IEnumerable<IConnector> allConnectors;
  140. ToConnectors(TestUtilities.CreateIPartCollection(), out allConnectors);
  141. foreach (var con in allConnectors)
  142. {
  143. IConnector cTo;
  144. result = con.GetConnectedTo(out cTo);
  145. // A result of E_NOTFOUND is valid in this context.
  146. if ((uint)result == TestUtilities.HRESULTS.E_NOTFOUND) continue;
  147. // Otherwise the result should be S_OK
  148. AssertCoreAudio.IsHResultOk(result);
  149. }
  150. }
  151. /// <summary>
  152. /// Tests that the data flow may be received, for each available connector in the system.
  153. /// </summary>
  154. [TestMethod]
  155. public void IConnector_GetDataFlow()
  156. {
  157. int result = 0;
  158. IEnumerable<IConnector> allConnectors;
  159. ToConnectors(TestUtilities.CreateIPartCollection(), out allConnectors);
  160. foreach (var con in allConnectors)
  161. {
  162. var valOne = DataFlow.In;
  163. var valTwo = DataFlow.Out;
  164. result = con.GetDataFlow(out valOne);
  165. AssertCoreAudio.IsHResultOk(result);
  166. result = con.GetDataFlow(out valTwo);
  167. AssertCoreAudio.IsHResultOk(result);
  168. Assert.AreEqual(valOne, valTwo, "The data flow was not received.");
  169. }
  170. }
  171. /// <summary>
  172. /// Tests that the local device ID may be received, for each available connector in the system.
  173. /// </summary>
  174. [TestMethod]
  175. public void IConnector_GetDeviceIdConnectedTo()
  176. {
  177. int result = 0;
  178. IEnumerable<IConnector> allConnectors;
  179. ToConnectors(TestUtilities.CreateIPartCollection(), out allConnectors);
  180. foreach (var con in allConnectors)
  181. {
  182. bool isConnected;
  183. con.IsConnected(out isConnected);
  184. if (isConnected)
  185. {
  186. IConnector cTo;
  187. con.GetConnectedTo(out cTo);
  188. IDeviceTopology topology;
  189. ((IPart)cTo).GetTopologyObject(out topology);
  190. string expectedId, devIdConTo;
  191. topology.GetDeviceId(out expectedId);
  192. result = con.GetDeviceIdConnectedTo(out devIdConTo);
  193. AssertCoreAudio.IsHResultOk(result);
  194. Assert.AreEqual(expectedId, devIdConTo, "The connected to device ID does not match the actual device ID.");
  195. }
  196. }
  197. }
  198. /// <summary>
  199. /// Tests that the connector type may be received, for each available connector in the system.
  200. /// </summary>
  201. [TestMethod]
  202. public void IConnector_GetType()
  203. {
  204. int result = 0;
  205. IEnumerable<IConnector> allConnectors;
  206. ToConnectors(TestUtilities.CreateIPartCollection(), out allConnectors);
  207. foreach (var con in allConnectors)
  208. {
  209. // Try confiming that the type is changed from unknown.
  210. var cType = ConnectorType.Unknown_Connector;
  211. result = con.GetType(out cType);
  212. AssertCoreAudio.IsHResultOk(result);
  213. if (cType == ConnectorType.Unknown_Connector)
  214. {
  215. // If it's still unknown, verify that this is correct.
  216. var cType2 = ConnectorType.Network;
  217. con.GetType(out cType2);
  218. AssertCoreAudio.IsHResultOk(result);
  219. Assert.AreEqual(ConnectorType.Unknown_Connector, cType2, "The connector type was not received.");
  220. }
  221. else
  222. {
  223. // The value being changed is enough to verify correctness of the method.
  224. }
  225. }
  226. }
  227. /// <summary>
  228. /// Tests that the connected state may be received, for each available connector in the system.
  229. /// </summary>
  230. [TestMethod]
  231. public void IConnector_IsConnected()
  232. {
  233. int result = 0;
  234. IEnumerable<IConnector> allConnectors;
  235. ToConnectors(TestUtilities.CreateIPartCollection(), out allConnectors);
  236. foreach (var con in allConnectors)
  237. {
  238. var valOne = true;
  239. var valTwo = false;
  240. result = con.IsConnected(out valOne);
  241. AssertCoreAudio.IsHResultOk(result);
  242. result = con.IsConnected(out valTwo);
  243. AssertCoreAudio.IsHResultOk(result);
  244. Assert.AreEqual(valOne, valTwo, "The connected state was not received.");
  245. }
  246. }
  247. #region Helper Methods
  248. /// <summary>
  249. /// Takes a collection of IPart objects and returns the subset of the collection that are IConnector objects.
  250. /// </summary>
  251. /// <param name="allParts">A collection that may contain both subunits and connectors.</param>
  252. /// <param name="allConnectors">A collection of connectors contained within the part collection.</param>
  253. private static void ToConnectors(IEnumerable<IPart> allParts, out IEnumerable<IConnector> allConnectors)
  254. {
  255. var cList = new List<IConnector>();
  256. IEnumerable<IConnector> cIns, cOuts;
  257. ToConnectors(allParts, out cIns, out cOuts);
  258. cList.AddRange(cIns);
  259. cList.AddRange(cIns);
  260. allConnectors = cList;
  261. }
  262. /// <summary>
  263. /// Takes a collection of IPart objects and returns the subset of the collection that are separated input and output IConnector objects.
  264. /// </summary>
  265. /// <param name="allParts">A collection that may contain both subunits and connectors.</param>
  266. /// <param name="inputConnectors">A collection of input connectors contained within the part collection.</param>
  267. /// <param name="outputConnectors">A collection of output connectors contained within the part collection.</param>
  268. private static void ToConnectors(IEnumerable<IPart> allParts, out IEnumerable<IConnector> inputConnectors, out IEnumerable<IConnector> outputConnectors)
  269. {
  270. var inList = new List<IConnector>();
  271. var outList = new List<IConnector>();
  272. foreach (IPart part in allParts)
  273. {
  274. PartType pType;
  275. part.GetPartType(out pType);
  276. if (pType == PartType.Connector)
  277. {
  278. DataFlow flow;
  279. var connector = (IConnector)part;
  280. connector.GetDataFlow(out flow);
  281. switch (flow)
  282. {
  283. case DataFlow.In:
  284. inList.Add(connector);
  285. break;
  286. case DataFlow.Out:
  287. outList.Add(connector);
  288. break;
  289. }
  290. }
  291. }
  292. if (!inList.Any()) Assert.Inconclusive("The test cannot be run properly. No connectors were found.");
  293. if (!outList.Any()) Assert.Inconclusive("The test cannot be run properly. No connectors were found.");
  294. inputConnectors = inList;
  295. outputConnectors = outList;
  296. }
  297. #endregion
  298. }
  299. }