UITest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using TestStack.White;
  6. using TestStack.White.UIItems;
  7. using TestStack.White.UIItems.WindowItems;
  8. using TestStack.White.UIItems.Finders;
  9. using System.Threading;
  10. using SketchAssistantWPF;
  11. using System.Windows;
  12. using System.Diagnostics;
  13. using TestStack.White.UIItems.WindowStripControls;
  14. using TestStack.White.UIItems.MenuItems;
  15. using System.Collections.Generic;
  16. namespace WhiteTests
  17. {
  18. [TestClass]
  19. public class UITest
  20. {
  21. private TestStack.White.Application application;
  22. public Window setupapp()
  23. {
  24. string outputDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  25. string editedDir = outputDir.Replace("WhiteTests", "SketchAssistantWPF");
  26. string app_path = editedDir + @"\SketchAssistantWPF.exe";
  27. ProcessStartInfo processStart = new ProcessStartInfo(app_path, "-debug");
  28. application = Application.Launch(processStart);
  29. return application.GetWindow("Sketch Assistant");
  30. }
  31. [TestMethod]
  32. public void CreateCanvasTest()
  33. {
  34. Window mainWindow = setupapp();
  35. Thread.Sleep(20);
  36. Assert.AreEqual("none", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  37. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("CanvasButton")).Click();
  38. Thread.Sleep(20);
  39. Assert.AreEqual("Last Action: A new canvas was created.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  40. mainWindow.Close();
  41. }
  42. [TestMethod]
  43. public void DrawLineTest()
  44. {
  45. Window mainWindow = setupapp();
  46. Thread.Sleep(20);
  47. Assert.AreEqual("none", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  48. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("CanvasButton")).Click();
  49. Thread.Sleep(20);
  50. Assert.AreEqual("Last Action: A new canvas was created.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  51. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("EditMenuButton")).Click();
  52. Thread.Sleep(20);
  53. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugMode")).Click();
  54. Thread.Sleep(20);
  55. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugOne")).Click();
  56. Thread.Sleep(7000);
  57. Assert.AreEqual("Last Action: Line number 0 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  58. mainWindow.Close();
  59. }
  60. /*[TestMethod]
  61. public void DeleteLineTest()
  62. {
  63. Window mainWindow = setupapp();
  64. Thread.Sleep(20);
  65. Assert.AreEqual("none", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  66. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("CanvasButton")).Click();
  67. Thread.Sleep(20);
  68. Assert.AreEqual("Last Action: A new canvas was created.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  69. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("EditMenuButton")).Click();
  70. Thread.Sleep(20);
  71. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugMode")).Click();
  72. Thread.Sleep(20);
  73. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugOne")).Click();
  74. Thread.Sleep(7000);
  75. Assert.AreEqual("Last Action: Line number 0 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  76. Thread.Sleep(20);
  77. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("DeleteButton")).Click();
  78. Thread.Sleep(20);
  79. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("EditMenuButton")).Click();
  80. Thread.Sleep(20);
  81. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugMode")).Click();
  82. Thread.Sleep(20);
  83. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugThree")).Click();
  84. Thread.Sleep(24000);
  85. Assert.AreEqual("Last Action: Line number 0 was deleted", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  86. mainWindow.Close();
  87. }*/
  88. [TestMethod]
  89. public void UndoTest()
  90. {
  91. Window mainWindow = setupapp();
  92. Thread.Sleep(20);
  93. Assert.AreEqual("none", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  94. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("CanvasButton")).Click();
  95. Thread.Sleep(20);
  96. Assert.AreEqual("Last Action: A new canvas was created.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  97. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("EditMenuButton")).Click();
  98. Thread.Sleep(20);
  99. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugMode")).Click();
  100. Thread.Sleep(20);
  101. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugOne")).Click();
  102. Thread.Sleep(7000);
  103. Assert.AreEqual("Last Action: Line number 0 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  104. Thread.Sleep(20);
  105. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("UndoButton")).Click();
  106. Thread.Sleep(20);
  107. Assert.AreEqual("Last Action: A new canvas was created.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  108. mainWindow.Close();
  109. }
  110. [TestMethod]
  111. public void RedoTest()
  112. {
  113. Window mainWindow = setupapp();
  114. Thread.Sleep(20);
  115. Assert.AreEqual("none", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  116. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("CanvasButton")).Click();
  117. Thread.Sleep(20);
  118. Assert.AreEqual("Last Action: A new canvas was created.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  119. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("EditMenuButton")).Click();
  120. Thread.Sleep(20);
  121. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugMode")).Click();
  122. Thread.Sleep(20);
  123. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugOne")).Click();
  124. Thread.Sleep(7000);
  125. Assert.AreEqual("Last Action: Line number 0 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  126. Thread.Sleep(20);
  127. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("UndoButton")).Click();
  128. Thread.Sleep(20);
  129. Assert.AreEqual("Last Action: A new canvas was created.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  130. Thread.Sleep(20);
  131. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("RedoButton")).Click();
  132. Thread.Sleep(20);
  133. Assert.AreEqual("Last Action: Line number 0 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  134. mainWindow.Close();
  135. }
  136. }
  137. [TestClass]
  138. [DeploymentItem(@"WhiteTests\test_input_files\")]
  139. public class FileImporterTests
  140. {
  141. /// <summary>
  142. /// instance of TestContext to be able to access deployed files
  143. /// </summary>
  144. private TestContext testContextInstance;
  145. /// <summary>
  146. ///Gets or sets the test context which provides
  147. ///information about and functionality for the current test run.
  148. ///</summary>
  149. public TestContext TestContext
  150. {
  151. get
  152. {
  153. return testContextInstance;
  154. }
  155. set
  156. {
  157. testContextInstance = value;
  158. }
  159. }
  160. /// <summary>
  161. /// creates a valid .isad file from the given sets of coordinates (number divisible by 3) by creating a line for every three consecutive points, parses the file and verifies that all lines and their points have been parsed correctly
  162. /// </summary>
  163. /// <param name="xCoordinates">an array containing the x coordinates of the points that will be created (length divisible by 3)</param>
  164. /// <param name="yCoordinates">an array containing the y coordinates of the points that will be created (length divisible by 3)</param>
  165. [DataTestMethod]
  166. [DataRow(new int[] { 54, 43, 57, 11, 145, 34, 113, 299, 0 }, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 })]
  167. [DataRow(new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 })]
  168. [DataRow(new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, new int[] { 54, 43, 57, 11, 145, 34, 113, 199, 0 })]
  169. public void ParseISADInputSuccessfulTest(int[] xCoordinates, int[] yCoordinates)
  170. {
  171. FileImporter uut = new FileImporter();
  172. List<String> file = new List<string>();
  173. file.Add("drawing");
  174. file.Add("300x200");
  175. for (int i = 0; i < xCoordinates.Length - 2; i += 3)
  176. {
  177. file.Add("line");
  178. file.Add(xCoordinates[i] + ";" + yCoordinates[i]);
  179. file.Add(xCoordinates[i + 1] + ";" + yCoordinates[i + 1]);
  180. file.Add(xCoordinates[i + 2] + ";" + yCoordinates[i + 2]);
  181. file.Add("endline");
  182. }
  183. file.Add("enddrawing");
  184. Tuple<int, int, List<InternalLine>> values = uut.ParseISADInputForTesting(file.ToArray());
  185. Assert.AreEqual(xCoordinates.Length / 3, values.Item3.Count);
  186. InternalLine[] lines = values.Item3.ToArray();
  187. for (int i = 0; i < xCoordinates.Length - 2; i += 3)
  188. {
  189. Point[] currentLine = lines[i / 3].GetPoints().ToArray();
  190. Assert.AreEqual(3, currentLine.Length);
  191. for (int j = 0; j < 3; j++)
  192. {
  193. Assert.IsTrue(currentLine[j].X == xCoordinates[i + j] && currentLine[j].Y == yCoordinates[i + j]);
  194. }
  195. }
  196. }
  197. /// <summary>
  198. /// parses teh given invalid .isad files and verifies that a FileImporterException is thrown, but no other exception
  199. /// </summary>
  200. /// <param name="file">the input file represented as an array of lines</param>
  201. [DataTestMethod]
  202. [DataRow(new String[] {})]
  203. [DataRow(new String[] { "begindrawing", "300x300", "line", "50;50", "100;50", "endline", "enddrawing" })]
  204. [DataRow(new String[] { "drawing", "300;300", "line", "50;50", "100;50", "endline", "enddrawing" })]
  205. [DataRow(new String[] { "drawing", "30.5x300", "line", "50;50", "100;50", "endline", "enddrawing" })]
  206. [DataRow(new String[] { "drawing", "line", "50;50", "100;50", "endline", "enddrawing" })]
  207. [DataRow(new String[] { "drawing", "300x300", "beginline", "50;50", "100;50", "endline", "enddrawing" })]
  208. [DataRow(new String[] { "drawing", "300x300", "line", "500;50", "100;50", "endline", "enddrawing" })]
  209. [DataRow(new String[] { "drawing", "300x300", "line", "50x50", "100;50", "endline", "enddrawing" })]
  210. [DataRow(new String[] { "drawing", "300x300", "line", "50", "100", "endline", "enddrawing" })]
  211. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "line", "endline", "enddrawing" })]
  212. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "stopline", "enddrawing" })]
  213. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "enddrawing" })]
  214. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "endline", "endrawing" })]
  215. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "endline" })]
  216. public void ParseISADInputExceptionTest(String[] file)
  217. {
  218. bool correctExceptionThrown = false;
  219. FileImporter uut = new FileImporter();
  220. try
  221. {
  222. //try to initialize the left image with an invalid isad drawing
  223. Tuple<int, int, List<InternalLine>> values1 = uut.ParseISADInputForTesting(file);
  224. }
  225. catch (FileImporterException)
  226. {
  227. //save the occurence of an exception
  228. correctExceptionThrown = true;
  229. }
  230. catch (Exception e)
  231. {
  232. //don't set success flag
  233. }
  234. //check that an exception has been thrown
  235. Assert.IsTrue(correctExceptionThrown);
  236. }
  237. /// <summary>
  238. /// parses all whitelisted files and ensures no exceptions are thrown (parsing abortion, e.g. due to corrupted input files, are realized by throwing a FileImporterException)
  239. /// </summary>
  240. [TestMethod]
  241. [TestCategory("FileIO")]
  242. public void ParseSVGInputNoErrorForWhitelistedFilesTest()
  243. {
  244. FileImporter uut = new FileImporter();
  245. string[] files = Directory.GetFiles(TestContext.DeploymentDirectory + @"\test_input_files\whitelisted", "*.svg", SearchOption.AllDirectories);
  246. Assert.IsTrue(files.Length > 0);
  247. foreach (string s in files) //parse each of the whitelisted files
  248. {
  249. Console.WriteLine(s);
  250. bool noExceptionThrown = true;
  251. try
  252. {
  253. uut.ParseSVGInputFile(s, 10000, 10000);
  254. }
  255. catch (Exception e)
  256. {
  257. noExceptionThrown = false;
  258. }
  259. Assert.IsTrue(noExceptionThrown);
  260. }
  261. }
  262. /// <summary>
  263. /// parses all blacklisted files and ensures an instance of FileIporterException is thrown for each file, but no other exceptions occur
  264. /// </summary>
  265. [TestMethod]
  266. [TestCategory("FileIO")]
  267. public void ParseSVGInputNoErrorForBlacklistedFilesTest()
  268. {
  269. FileImporter uut = new FileImporter();
  270. string[] files = Directory.GetFiles(TestContext.DeploymentDirectory + @"\test_input_files\blacklisted", "*.svg", SearchOption.AllDirectories);
  271. Assert.IsTrue(files.Length > 0);
  272. foreach (string s in files) //parse each of the blacklisted files
  273. {
  274. bool correctExceptionThrown = false;
  275. try
  276. {
  277. uut.ParseSVGInputFile(s, 10000, 10000);
  278. }
  279. catch (FileImporterException e)
  280. {
  281. correctExceptionThrown = true;
  282. }
  283. catch (Exception e)
  284. {
  285. }
  286. Assert.IsTrue(correctExceptionThrown);
  287. }
  288. }
  289. }
  290. }