UITest.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. using System.Text.RegularExpressions;
  17. using WindowsInput;
  18. using WindowsInput.Native;
  19. using System.Threading.Tasks;
  20. namespace WhiteTests
  21. {
  22. [TestClass]
  23. public class UITest
  24. {
  25. private TestStack.White.Application application;
  26. /// <summary>
  27. /// The directory of the input files, saved for repeated use
  28. /// </summary>
  29. private String input_file_dir = null;
  30. /// <summary>
  31. /// instance of TestContext to be able to access deployed files
  32. /// </summary>
  33. private TestContext testContextInstance;
  34. /// <summary>
  35. ///Gets or sets the test context which provides
  36. ///information about and functionality for the current test run.
  37. ///</summary>
  38. public TestContext TestContext
  39. {
  40. get
  41. {
  42. return testContextInstance;
  43. }
  44. set
  45. {
  46. testContextInstance = value;
  47. }
  48. }
  49. /// <summary>
  50. /// A function that returns the path to the test_input_files folder.
  51. /// Do with it what you want.
  52. /// </summary>
  53. /// <returns>the path to the test_input_files folder</returns>
  54. public String getSketchAssistantDirectory()
  55. {
  56. Regex rx = new Regex(@"^(.*\\SketchAssistant\\)");
  57. Match match = rx.Match(TestContext.DeploymentDirectory);
  58. String SketchAssistDir = match.Groups[1].Value;
  59. if (input_file_dir == null)
  60. {
  61. if (Directory.Exists(SketchAssistDir + @"WhiteTests\test_input_files\"))
  62. {
  63. input_file_dir = SketchAssistDir + @"WhiteTests\test_input_files\";
  64. }
  65. else if (Directory.Exists(SketchAssistDir + @"WhiteTests\bin\Debug\test_input_files\"))
  66. {
  67. input_file_dir = SketchAssistDir + @"WhiteTests\bin\Debug\test_input_files\";
  68. }
  69. else
  70. {
  71. Regex rx_0 = new Regex(@"^(.*\\projects\\)");
  72. Match match_0 = rx_0.Match(TestContext.DeploymentDirectory);
  73. String ProjectsDir = match_0.Groups[1].Value;
  74. var dirs = Directory.GetDirectories(ProjectsDir, "test_input_files", SearchOption.AllDirectories);
  75. input_file_dir = dirs[0];
  76. }
  77. }
  78. return input_file_dir;
  79. }
  80. public Window setupapp()
  81. {
  82. string outputDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  83. string editedDir = outputDir.Replace("WhiteTests", "SketchAssistantWPF");
  84. string app_path = editedDir + @"\SketchAssistantWPF.exe";
  85. ProcessStartInfo processStart = new ProcessStartInfo(app_path, "-debug");
  86. application = Application.Launch(processStart);
  87. return application.GetWindow("Sketch Assistant");
  88. }
  89. [TestMethod]
  90. [TestCategory("bla")]
  91. public void CreateCanvasTest()
  92. {
  93. Window mainWindow = setupapp();
  94. Thread.Sleep(20);
  95. Assert.AreEqual("none", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  96. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("CanvasButton")).Click();
  97. Thread.Sleep(20);
  98. Assert.AreEqual("Last Action: A new canvas was created.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  99. mainWindow.Close();
  100. }
  101. [TestMethod]
  102. [TestCategory("bla")]
  103. public void DrawLineTest()
  104. {
  105. Window mainWindow = setupapp();
  106. Thread.Sleep(20);
  107. Assert.AreEqual("none", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  108. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("CanvasButton")).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.Get<Menu>(SearchCriteria.ByAutomationId("EditMenuButton")).Click();
  112. Thread.Sleep(20);
  113. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugMode")).Click();
  114. Thread.Sleep(20);
  115. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugOne")).Click();
  116. Thread.Sleep(7000);
  117. Assert.AreEqual("Last Action: Line number 0 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  118. mainWindow.Close();
  119. }
  120. [TestMethod]
  121. public void DeleteLineTest()
  122. {
  123. Window mainWindow = setupapp();
  124. Thread.Sleep(20);
  125. Assert.AreEqual("none", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  126. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("CanvasButton")).Click();
  127. Thread.Sleep(20);
  128. Assert.AreEqual("Last Action: A new canvas was created.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  129. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("DrawButton")).Click();
  130. Thread.Sleep(20);
  131. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("EditMenuButton")).Click();
  132. Thread.Sleep(20);
  133. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugMode")).Click();
  134. Thread.Sleep(20);
  135. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugOne")).Click();
  136. Thread.Sleep(7000);
  137. Assert.AreEqual("Last Action: Line number 0 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  138. Thread.Sleep(20);
  139. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("DeleteButton")).Click();
  140. Thread.Sleep(20);
  141. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("EditMenuButton")).Click();
  142. Thread.Sleep(20);
  143. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugMode")).Click();
  144. Thread.Sleep(20);
  145. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugOne")).Click();
  146. Thread.Sleep(7000);
  147. Assert.AreEqual("Last Action: Line number 0 was deleted.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  148. mainWindow.Close();
  149. }
  150. [TestMethod]
  151. [TestCategory("bla")]
  152. public void UndoTest()
  153. {
  154. Window mainWindow = setupapp();
  155. Thread.Sleep(20);
  156. Assert.AreEqual("none", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  157. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("CanvasButton")).Click();
  158. Thread.Sleep(20);
  159. Assert.AreEqual("Last Action: A new canvas was created.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  160. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("EditMenuButton")).Click();
  161. Thread.Sleep(20);
  162. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugMode")).Click();
  163. Thread.Sleep(20);
  164. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugOne")).Click();
  165. Thread.Sleep(7000);
  166. Assert.AreEqual("Last Action: Line number 0 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  167. Thread.Sleep(20);
  168. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("UndoButton")).Click();
  169. Thread.Sleep(20);
  170. Assert.AreEqual("Last Action: A new canvas was created.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  171. mainWindow.Close();
  172. }
  173. [TestMethod]
  174. [TestCategory("bla")]
  175. public void RedoTest()
  176. {
  177. Window mainWindow = setupapp();
  178. Thread.Sleep(20);
  179. Assert.AreEqual("none", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  180. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("CanvasButton")).Click();
  181. Thread.Sleep(20);
  182. Assert.AreEqual("Last Action: A new canvas was created.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  183. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("EditMenuButton")).Click();
  184. Thread.Sleep(20);
  185. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugMode")).Click();
  186. Thread.Sleep(20);
  187. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugOne")).Click();
  188. Thread.Sleep(7000);
  189. Assert.AreEqual("Last Action: Line number 0 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  190. Thread.Sleep(20);
  191. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("UndoButton")).Click();
  192. Thread.Sleep(20);
  193. Assert.AreEqual("Last Action: A new canvas was created.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  194. Thread.Sleep(20);
  195. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("RedoButton")).Click();
  196. Thread.Sleep(20);
  197. Assert.AreEqual("Last Action: Line number 0 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  198. mainWindow.Close();
  199. }
  200. [TestMethod]
  201. public void DrawSeveralLines()
  202. {
  203. Window mainWindow = setupapp();
  204. Thread.Sleep(20);
  205. Assert.AreEqual("none", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  206. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("CanvasButton")).Click();
  207. Thread.Sleep(20);
  208. Assert.AreEqual("Last Action: A new canvas was created.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  209. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("EditMenuButton")).Click();
  210. Thread.Sleep(20);
  211. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugMode")).Click();
  212. Thread.Sleep(20);
  213. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugOne")).Click();
  214. Thread.Sleep(7000);
  215. Assert.AreEqual("Last Action: Line number 0 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  216. Thread.Sleep(20);
  217. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("EditMenuButton")).Click();
  218. Thread.Sleep(20);
  219. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugMode")).Click();
  220. Thread.Sleep(20);
  221. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugTwo")).Click();
  222. Thread.Sleep(30000);
  223. Assert.AreEqual("Last Action: Line number 1 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  224. Thread.Sleep(20);
  225. mainWindow.Close();
  226. }
  227. [TestMethod]
  228. public void DeleteSeveralLines()
  229. {
  230. Window mainWindow = setupapp();
  231. Thread.Sleep(20);
  232. Assert.AreEqual("none", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  233. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("CanvasButton")).Click();
  234. Thread.Sleep(20);
  235. Assert.AreEqual("Last Action: A new canvas was created.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  236. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("EditMenuButton")).Click();
  237. Thread.Sleep(20);
  238. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugMode")).Click();
  239. Thread.Sleep(20);
  240. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugOne")).Click();
  241. Thread.Sleep(7000);
  242. Assert.AreEqual("Last Action: Line number 0 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  243. Thread.Sleep(20);
  244. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("EditMenuButton")).Click();
  245. Thread.Sleep(20);
  246. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugMode")).Click();
  247. Thread.Sleep(20);
  248. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugTwo")).Click();
  249. Thread.Sleep(24000);
  250. Assert.AreEqual("Last Action: Line number 1 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  251. Thread.Sleep(20);
  252. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("DeleteButton")).Click();
  253. Thread.Sleep(20);
  254. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("EditMenuButton")).Click();
  255. Thread.Sleep(20);
  256. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugMode")).Click();
  257. Thread.Sleep(20);
  258. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugThree")).Click();
  259. Thread.Sleep(20);
  260. Assert.AreEqual("Last Action: Line number 1 was deleted.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  261. Thread.Sleep(10000);
  262. Assert.AreEqual("Last Action: Line number 0 was deleted.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  263. mainWindow.Close();
  264. }
  265. [TestMethod]
  266. public void UndoSeveralLines()
  267. {
  268. Window mainWindow = setupapp();
  269. Thread.Sleep(20);
  270. Assert.AreEqual("none", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  271. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("CanvasButton")).Click();
  272. Thread.Sleep(20);
  273. Assert.AreEqual("Last Action: A new canvas was created.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  274. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("EditMenuButton")).Click();
  275. Thread.Sleep(20);
  276. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugMode")).Click();
  277. Thread.Sleep(20);
  278. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugOne")).Click();
  279. Thread.Sleep(7000);
  280. Assert.AreEqual("Last Action: Line number 0 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  281. Thread.Sleep(20);
  282. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("EditMenuButton")).Click();
  283. Thread.Sleep(20);
  284. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugMode")).Click();
  285. Thread.Sleep(20);
  286. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugTwo")).Click();
  287. Thread.Sleep(30000);
  288. Assert.AreEqual("Last Action: Line number 1 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  289. Thread.Sleep(20);
  290. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("UndoButton")).Click();
  291. Thread.Sleep(20);
  292. Assert.AreEqual("Last Action: Line number 0 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  293. Thread.Sleep(20);
  294. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("UndoButton")).Click();
  295. Thread.Sleep(20);
  296. Assert.AreEqual("Last Action: A new canvas was created.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  297. Thread.Sleep(20);
  298. mainWindow.Close();
  299. }
  300. [TestMethod]
  301. public void RedoSeveralLines()
  302. {
  303. Window mainWindow = setupapp();
  304. Thread.Sleep(20);
  305. Assert.AreEqual("none", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  306. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("CanvasButton")).Click();
  307. Thread.Sleep(20);
  308. Assert.AreEqual("Last Action: A new canvas was created.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  309. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("EditMenuButton")).Click();
  310. Thread.Sleep(20);
  311. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugMode")).Click();
  312. Thread.Sleep(20);
  313. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugOne")).Click();
  314. Thread.Sleep(7000);
  315. Assert.AreEqual("Last Action: Line number 0 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  316. Thread.Sleep(20);
  317. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("EditMenuButton")).Click();
  318. Thread.Sleep(20);
  319. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugMode")).Click();
  320. Thread.Sleep(20);
  321. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugTwo")).Click();
  322. Thread.Sleep(24000);
  323. Assert.AreEqual("Last Action: Line number 1 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  324. Thread.Sleep(20);
  325. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("UndoButton")).Click();
  326. Thread.Sleep(20);
  327. Assert.AreEqual("Last Action: Line number 0 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  328. Thread.Sleep(20);
  329. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("UndoButton")).Click();
  330. Thread.Sleep(20);
  331. Assert.AreEqual("Last Action: A new canvas was created.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  332. Thread.Sleep(20);
  333. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("RedoButton")).Click();
  334. Thread.Sleep(20);
  335. Assert.AreEqual("Last Action: Line number 0 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  336. Thread.Sleep(20);
  337. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("RedoButton")).Click();
  338. Thread.Sleep(20);
  339. Assert.AreEqual("Last Action: Line number 1 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  340. Thread.Sleep(20);
  341. mainWindow.Close();
  342. }
  343. [TestMethod]
  344. public void UndoAndRedoTests()
  345. {
  346. Window mainWindow = setupapp();
  347. Thread.Sleep(20);
  348. Assert.AreEqual("none", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  349. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("CanvasButton")).Click();
  350. Thread.Sleep(20);
  351. Assert.AreEqual("Last Action: A new canvas was created.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  352. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("EditMenuButton")).Click();
  353. Thread.Sleep(20);
  354. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugMode")).Click();
  355. Thread.Sleep(20);
  356. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugOne")).Click();
  357. Thread.Sleep(7000);
  358. Assert.AreEqual("Last Action: Line number 0 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  359. Thread.Sleep(20);
  360. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("EditMenuButton")).Click();
  361. Thread.Sleep(20);
  362. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugMode")).Click();
  363. Thread.Sleep(20);
  364. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugTwo")).Click();
  365. Thread.Sleep(24000);
  366. Assert.AreEqual("Last Action: Line number 1 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  367. Thread.Sleep(20);
  368. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("UndoButton")).Click();
  369. Thread.Sleep(20);
  370. Assert.AreEqual("Last Action: Line number 0 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  371. Thread.Sleep(20);
  372. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("UndoButton")).Click();
  373. Thread.Sleep(20);
  374. Assert.AreEqual("Last Action: A new canvas was created.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  375. Thread.Sleep(20);
  376. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("RedoButton")).Click();
  377. Thread.Sleep(20);
  378. Assert.AreEqual("Last Action: Line number 0 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  379. Thread.Sleep(20);
  380. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("RedoButton")).Click();
  381. Thread.Sleep(20);
  382. Assert.AreEqual("Last Action: Line number 1 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  383. Thread.Sleep(20);
  384. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("DeleteButton")).Click();
  385. Thread.Sleep(20);
  386. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("EditMenuButton")).Click();
  387. Thread.Sleep(20);
  388. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugMode")).Click();
  389. Thread.Sleep(20);
  390. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugThree")).Click();
  391. Thread.Sleep(7000);
  392. Assert.AreEqual("Last Action: Line number 0 was deleted.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  393. Thread.Sleep(20);
  394. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("UndoButton")).Click();
  395. Thread.Sleep(20);
  396. Assert.AreEqual("Last Action: Line number 1 was deleted.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  397. Thread.Sleep(20);
  398. mainWindow.Close();
  399. }
  400. [TestMethod]
  401. public void PointDraw()
  402. {
  403. Window mainWindow = setupapp();
  404. Thread.Sleep(20);
  405. Assert.AreEqual("none", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  406. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("CanvasButton")).Click();
  407. Thread.Sleep(20);
  408. Assert.AreEqual("Last Action: A new canvas was created.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  409. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("EditMenuButton")).Click();
  410. Thread.Sleep(20);
  411. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugMode")).Click();
  412. Thread.Sleep(20);
  413. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugFour")).Click();
  414. Thread.Sleep(4000);
  415. Assert.AreEqual("Last Action: Line number 0 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  416. Thread.Sleep(20);
  417. mainWindow.Close();
  418. }
  419. [TestMethod]
  420. public void NewCanvasAfterDraw()
  421. {
  422. Window mainWindow = setupapp();
  423. Thread.Sleep(20);
  424. Assert.AreEqual("none", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  425. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("CanvasButton")).Click();
  426. Thread.Sleep(20);
  427. Assert.AreEqual("Last Action: A new canvas was created.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  428. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("EditMenuButton")).Click();
  429. Thread.Sleep(20);
  430. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugMode")).Click();
  431. Thread.Sleep(20);
  432. mainWindow.Get<Menu>(SearchCriteria.ByAutomationId("DebugThree")).Click();
  433. Thread.Sleep(4000);
  434. Assert.AreEqual("Last Action: Line number 0 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  435. Thread.Sleep(20);
  436. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("CanvasButton")).Click();
  437. Thread.Sleep(20);
  438. // Click on No button in warning
  439. Window messageBox0 = mainWindow.MessageBox("Warning");
  440. messageBox0.Get<Button>(SearchCriteria.ByText("No")).Click();
  441. Thread.Sleep(20);
  442. Assert.AreEqual("Last Action: Line number 0 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  443. Thread.Sleep(20);
  444. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("CanvasButton")).Click();
  445. Thread.Sleep(20);
  446. // close warning
  447. Window messageBox1 = mainWindow.MessageBox("Warning");
  448. messageBox1.Close();
  449. Thread.Sleep(20);
  450. Assert.AreEqual("Last Action: Line number 0 was drawn.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  451. Thread.Sleep(20);
  452. mainWindow.Get<Button>(SearchCriteria.ByAutomationId("CanvasButton")).Click();
  453. Thread.Sleep(20);
  454. // click yes button on warning
  455. Window messageBox2 = mainWindow.MessageBox("Warning");
  456. messageBox2.Get<Button>(SearchCriteria.ByText("Yes")).Click();
  457. Thread.Sleep(20);
  458. Assert.AreEqual("Last Action: A new canvas was created.", mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId("LastActionBox")).Text.ToString());
  459. Thread.Sleep(20);
  460. mainWindow.Close();
  461. }
  462. }
  463. [TestClass]
  464. //[DeploymentItem(@"WhiteTests\test_input_files\")]
  465. public class FileImporterTests
  466. {
  467. /// <summary>
  468. /// The directory of the input files, saved for repeated use
  469. /// </summary>
  470. private String input_file_dir = null;
  471. /// <summary>
  472. /// instance of TestContext to be able to access deployed files
  473. /// </summary>
  474. private TestContext testContextInstance;
  475. /// <summary>
  476. ///Gets or sets the test context which provides
  477. ///information about and functionality for the current test run.
  478. ///</summary>
  479. public TestContext TestContext
  480. {
  481. get
  482. {
  483. return testContextInstance;
  484. }
  485. set
  486. {
  487. testContextInstance = value;
  488. }
  489. }
  490. /// <summary>
  491. /// A function that returns the path to the test_input_files folder.
  492. /// Do with it what you want.
  493. /// </summary>
  494. /// <returns>the path to the test_input_files folder</returns>
  495. public String getSketchAssistantDirectory()
  496. {
  497. Regex rx = new Regex(@"^(.*\\SketchAssistant\\)");
  498. Match match = rx.Match(TestContext.DeploymentDirectory);
  499. String SketchAssistDir = match.Groups[1].Value;
  500. if(input_file_dir == null)
  501. {
  502. if (Directory.Exists(SketchAssistDir + @"\WhiteTests\test_input_files\"))
  503. {
  504. input_file_dir = SketchAssistDir + @"\WhiteTests\test_input_files\";
  505. }
  506. else if (Directory.Exists(SketchAssistDir + @"\WhiteTests\bin\Debug\test_input_files\"))
  507. {
  508. input_file_dir = SketchAssistDir + @"\WhiteTests\bin\Debug\test_input_files\";
  509. }
  510. else
  511. {
  512. Regex rx_0 = new Regex(@"^(.*\\projects\\)");
  513. Match match_0 = rx_0.Match(TestContext.DeploymentDirectory);
  514. String ProjectsDir = match_0.Groups[1].Value;
  515. var dirs = Directory.GetDirectories(ProjectsDir, "test_input_files", SearchOption.AllDirectories);
  516. input_file_dir = dirs[0];
  517. }
  518. }
  519. return input_file_dir;
  520. }
  521. /// <summary>
  522. /// 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
  523. /// </summary>
  524. /// <param name="xCoordinates">an array containing the x coordinates of the points that will be created (length divisible by 3)</param>
  525. /// <param name="yCoordinates">an array containing the y coordinates of the points that will be created (length divisible by 3)</param>
  526. [DataTestMethod]
  527. [TestCategory("bla")]
  528. [DataRow(new int[] { 54, 43, 57, 11, 145, 34, 113, 299, 0 }, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 })]
  529. [DataRow(new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 })]
  530. [DataRow(new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, new int[] { 54, 43, 57, 11, 145, 34, 113, 199, 0 })]
  531. public void ParseISADInputSuccessfulTest(int[] xCoordinates, int[] yCoordinates)
  532. {
  533. FileImporter uut = new FileImporter();
  534. List<String> file = new List<string>();
  535. file.Add("drawing");
  536. file.Add("300x200");
  537. for (int i = 0; i < xCoordinates.Length - 2; i += 3)
  538. {
  539. file.Add("line");
  540. file.Add(xCoordinates[i] + ";" + yCoordinates[i]);
  541. file.Add(xCoordinates[i + 1] + ";" + yCoordinates[i + 1]);
  542. file.Add(xCoordinates[i + 2] + ";" + yCoordinates[i + 2]);
  543. file.Add("endline");
  544. }
  545. file.Add("enddrawing");
  546. Tuple<int, int, List<InternalLine>> values = uut.ParseISADInputForTesting(file.ToArray());
  547. Assert.AreEqual(xCoordinates.Length / 3, values.Item3.Count);
  548. InternalLine[] lines = values.Item3.ToArray();
  549. for (int i = 0; i < xCoordinates.Length - 2; i += 3)
  550. {
  551. Point[] currentLine = lines[i / 3].GetPoints().ToArray();
  552. Assert.AreEqual(3, currentLine.Length);
  553. for (int j = 0; j < 3; j++)
  554. {
  555. Assert.IsTrue(currentLine[j].X == xCoordinates[i + j] && currentLine[j].Y == yCoordinates[i + j]);
  556. }
  557. }
  558. }
  559. /// <summary>
  560. /// parses teh given invalid .isad files and verifies that a FileImporterException is thrown, but no other exception
  561. /// </summary>
  562. /// <param name="file">the input file represented as an array of lines</param>
  563. [DataTestMethod]
  564. [TestCategory("bla")]
  565. [DataRow(new String[] {})]
  566. [DataRow(new String[] { "begindrawing", "300x300", "line", "50;50", "100;50", "endline", "enddrawing" })]
  567. [DataRow(new String[] { "drawing", "300;300", "line", "50;50", "100;50", "endline", "enddrawing" })]
  568. [DataRow(new String[] { "drawing", "30.5x300", "line", "50;50", "100;50", "endline", "enddrawing" })]
  569. [DataRow(new String[] { "drawing", "line", "50;50", "100;50", "endline", "enddrawing" })]
  570. [DataRow(new String[] { "drawing", "300x300", "beginline", "50;50", "100;50", "endline", "enddrawing" })]
  571. [DataRow(new String[] { "drawing", "300x300", "line", "500;50", "100;50", "endline", "enddrawing" })]
  572. [DataRow(new String[] { "drawing", "300x300", "line", "50x50", "100;50", "endline", "enddrawing" })]
  573. [DataRow(new String[] { "drawing", "300x300", "line", "50", "100", "endline", "enddrawing" })]
  574. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "line", "endline", "enddrawing" })]
  575. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "stopline", "enddrawing" })]
  576. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "enddrawing" })]
  577. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "endline", "endrawing" })]
  578. [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "endline" })]
  579. public void ParseISADInputExceptionTest(String[] file)
  580. {
  581. bool correctExceptionThrown = false;
  582. FileImporter uut = new FileImporter();
  583. try
  584. {
  585. //try to initialize the left image with an invalid isad drawing
  586. Tuple<int, int, List<InternalLine>> values1 = uut.ParseISADInputForTesting(file);
  587. }
  588. catch (FileImporterException)
  589. {
  590. //save the occurence of an exception
  591. correctExceptionThrown = true;
  592. }
  593. catch (Exception)
  594. {
  595. //don't set success flag
  596. }
  597. //check that an exception has been thrown
  598. Assert.IsTrue(correctExceptionThrown);
  599. }
  600. /// <summary>
  601. /// 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)
  602. /// </summary>
  603. [TestMethod]
  604. [TestCategory("FileIO")]
  605. public void ParseSVGInputNoErrorForWhitelistedFilesTest()
  606. {
  607. FileImporter uut = new FileImporter();
  608. string[] files = Directory.GetFiles(getSketchAssistantDirectory() + @"\whitelisted", "*.svg", SearchOption.AllDirectories);
  609. Assert.IsTrue(files.Length > 0);
  610. foreach (string s in files) //parse each of the whitelisted files
  611. {
  612. Console.WriteLine(s);
  613. bool noExceptionThrown = true;
  614. try
  615. {
  616. uut.ParseSVGInputFile(s, 10000, 10000);
  617. }
  618. catch (Exception)
  619. {
  620. noExceptionThrown = false;
  621. }
  622. Assert.IsTrue(noExceptionThrown);
  623. }
  624. }
  625. /// <summary>
  626. /// parses all blacklisted files and ensures an instance of FileIporterException is thrown for each file, but no other exceptions occur
  627. /// </summary>
  628. [TestMethod]
  629. [TestCategory("FileIO")]
  630. public void ParseSVGInputNoErrorForBlacklistedFilesTest()
  631. {
  632. FileImporter uut = new FileImporter();
  633. string[] files = Directory.GetFiles(getSketchAssistantDirectory() + @"\blacklisted", "*.svg", SearchOption.AllDirectories);
  634. Assert.IsTrue(files.Length > 0);
  635. foreach (string s in files) //parse each of the blacklisted files
  636. {
  637. bool correctExceptionThrown = false;
  638. try
  639. {
  640. uut.ParseSVGInputFile(s, 10000, 10000);
  641. }
  642. catch (FileImporterException)
  643. {
  644. correctExceptionThrown = true;
  645. }
  646. catch (Exception)
  647. {
  648. }
  649. Assert.IsTrue(correctExceptionThrown);
  650. }
  651. }
  652. }
  653. [TestClass]
  654. public class SimilarityCalculationTests
  655. {
  656. /// <summary>
  657. /// The debug data element used to generate random lines.
  658. /// </summary>
  659. private DebugData DebugData = new DebugData();
  660. /// <summary>
  661. /// Generates random lines and tests how similar they are.
  662. /// To test the similarity score always stays between 0 and 1.
  663. /// </summary>
  664. [TestMethod]
  665. public void StaysWithinParameters()
  666. {
  667. Parallel.For(1,100,
  668. i =>
  669. {
  670. InternalLine l0 = DebugData.GetRandomLine(1, (uint) i);
  671. InternalLine l1 = DebugData.GetRandomLine(1, (uint)i);
  672. var sim = GeometryCalculator.CalculateSimilarity(l0, l1);
  673. Assert.IsTrue((sim >= 0));
  674. Assert.IsTrue((sim <= 1));
  675. } );
  676. }
  677. [TestMethod]
  678. public void CorrectSimilarity()
  679. {
  680. Parallel.ForEach(DebugData.GetSimilarityTestData(),
  681. tup =>
  682. {
  683. InternalLine l0 = tup.Item1;
  684. InternalLine l1 = tup.Item2;
  685. var sim = GeometryCalculator.CalculateSimilarity(l0, l1);
  686. Assert.AreEqual(tup.Item3, sim, 0.00000001);
  687. });
  688. }
  689. }
  690. }