UITest.cs 15 KB

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