WizardTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Terminal.Gui;
  7. using Xunit;
  8. using System.Globalization;
  9. using Xunit.Abstractions;
  10. using NStack;
  11. namespace Terminal.Gui.Views {
  12. public class WizardTests {
  13. readonly ITestOutputHelper output;
  14. public WizardTests (ITestOutputHelper output)
  15. {
  16. this.output = output;
  17. }
  18. private void RunButtonTestWizard (string title, int width, int height)
  19. {
  20. var wizard = new Wizard (title) { Width = width, Height = height };
  21. Application.End (Application.Begin (wizard));
  22. }
  23. // =========== WizardStep Tests
  24. [Fact, AutoInitShutdown]
  25. public void WizardStep_Title ()
  26. {
  27. // Verify default title
  28. // Verify set actually changes property
  29. // Verify set changes Wizard title (TODO: NOT YET IMPLEMENTED)
  30. }
  31. [Fact, AutoInitShutdown]
  32. public void WizardStep_ButtonText ()
  33. {
  34. // Verify default button text
  35. // Verify set actually changes property
  36. // Verify set actually changes buttons for the current step
  37. }
  38. // =========== Wizard Tests
  39. [Fact, AutoInitShutdown]
  40. public void DefaultConstructor_SizedProperly ()
  41. {
  42. var d = ((FakeDriver)Application.Driver);
  43. var wizard = new Wizard ();
  44. Assert.NotEqual (0, wizard.Width);
  45. Assert.NotEqual (0, wizard.Height);
  46. }
  47. [Fact, AutoInitShutdown]
  48. // Verify a zero-step wizard doesn't crash and shows a blank wizard
  49. // and that the title is correct
  50. public void ZeroStepWizard_Shows ()
  51. {
  52. var d = ((FakeDriver)Application.Driver);
  53. var title = "1234";
  54. var stepTitle = "";
  55. int width = 30;
  56. int height = 6;
  57. d.SetBufferSize (width, height);
  58. var btnBackText = "Back";
  59. var btnBack = $"{d.LeftBracket} {btnBackText} {d.RightBracket}";
  60. var btnNextText = "Finish";
  61. var btnNext = $"{d.LeftBracket}{d.LeftDefaultIndicator} {btnNextText} {d.RightDefaultIndicator}{d.RightBracket}";
  62. var topRow = $"{d.ULDCorner} {title}{stepTitle} {new String (d.HDLine.ToString () [0], width - title.Length - stepTitle.Length - 4)}{d.URDCorner}";
  63. var row2 = $"{d.VDLine}{new String (' ', width - 2)}{d.VDLine}";
  64. var row3 = row2;
  65. var separatorRow = $"{d.VDLine}{new String (d.HLine.ToString () [0], width - 2)}{d.VDLine}";
  66. var buttonRow = $"{d.VDLine}{btnBack}{new String (' ', width - btnBack.Length - btnNext.Length - 2)}{btnNext}{d.VDLine}";
  67. var bottomRow = $"{d.LLDCorner}{new String (d.HDLine.ToString () [0], width - 2)}{d.LRDCorner}";
  68. var wizard = new Wizard (title) { Width = width, Height = height };
  69. Application.End (Application.Begin (wizard));
  70. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{row2}\n{row3}\n{separatorRow}\n{buttonRow}\n{bottomRow}", output);
  71. }
  72. [Fact, AutoInitShutdown]
  73. // This test verifies that a single step wizard shows the correct buttons
  74. // and that the title is correct
  75. public void OneStepWizard_Shows ()
  76. {
  77. var d = ((FakeDriver)Application.Driver);
  78. var title = "1234";
  79. var stepTitle = "ABCD";
  80. int width = 30;
  81. int height = 7;
  82. d.SetBufferSize (width, height);
  83. var btnBackText = "Back";
  84. var btnBack = string.Empty; // $"{d.LeftBracket} {btnBackText} {d.RightBracket}";
  85. var btnNextText = "Finish"; // "Next";
  86. var btnNext = $"{d.LeftBracket}{d.LeftDefaultIndicator} {btnNextText} {d.RightDefaultIndicator}{d.RightBracket}";
  87. var topRow = $"{d.ULDCorner} {title} - {stepTitle} {new String (d.HDLine.ToString () [0], width - title.Length - stepTitle.Length - 7)}{d.URDCorner}";
  88. var row2 = $"{d.VDLine}{new String (' ', width - 2)}{d.VDLine}";
  89. var row3 = row2;
  90. var row4 = row3;
  91. var separatorRow = $"{d.VDLine}{new String (d.HLine.ToString () [0], width - 2)}{d.VDLine}";
  92. var buttonRow = $"{d.VDLine}{btnBack}{new String (' ', width - btnBack.Length - btnNext.Length - 2)}{btnNext}{d.VDLine}";
  93. var bottomRow = $"{d.LLDCorner}{new String (d.HDLine.ToString () [0], width - 2)}{d.LRDCorner}";
  94. var wizard = new Wizard (title) { Width = width, Height = height };
  95. wizard.AddStep (new Wizard.WizardStep (stepTitle));
  96. //wizard.LayoutSubviews ();
  97. var firstIteration = false;
  98. var runstate = Application.Begin (wizard);
  99. Application.RunMainLoopIteration (ref runstate, true, ref firstIteration);
  100. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{row2}\n{row3}\n{row4}\n{separatorRow}\n{buttonRow}\n{bottomRow}", output);
  101. Application.End (runstate);
  102. }
  103. [Fact, AutoInitShutdown]
  104. // This test verifies that the 2nd step in a wizard with 2 steps
  105. // shows the correct buttons on both steps
  106. // and that the title is correct
  107. public void TwoStepWizard_Next_Shows_SecondStep ()
  108. {
  109. // verify step one
  110. // Next
  111. // verify step two
  112. // Back
  113. // verify step one again
  114. }
  115. [Fact, AutoInitShutdown]
  116. // This test verifies that the 2nd step in a wizard with more than 2 steps
  117. // shows the correct buttons on all steps
  118. // and that the title is correct
  119. public void ThreeStepWizard_Next_Shows_Steps ()
  120. {
  121. // verify step one
  122. // Next
  123. // verify step two
  124. // Back
  125. // verify step one again
  126. }
  127. [Fact, AutoInitShutdown]
  128. // this test is needed because Wizard overrides Dialog's title behavior ("Title - StepTitle")
  129. public void Setting_Title_Works ()
  130. {
  131. var d = ((FakeDriver)Application.Driver);
  132. var title = "1234";
  133. var stepTitle = " - ABCD";
  134. int width = 40;
  135. int height = 4;
  136. d.SetBufferSize (width, height);
  137. var btnNextText = "Finish";
  138. var btnNext = $"{d.LeftBracket}{d.LeftDefaultIndicator} {btnNextText} {d.RightDefaultIndicator}{d.RightBracket}";
  139. var topRow = $"{d.ULDCorner} {title}{stepTitle} {new String (d.HDLine.ToString () [0], width - title.Length - stepTitle.Length - 4)}{d.URDCorner}";
  140. var separatorRow = $"{d.VDLine}{new String (d.HLine.ToString () [0], width - 2)}{d.VDLine}";
  141. // Once this is fixed, revert to commented out line: https://github.com/migueldeicaza/gui.cs/issues/1791
  142. var buttonRow = $"{d.VDLine}{new String (' ', width - btnNext.Length - 2)}{btnNext}{d.VDLine}";
  143. //var buttonRow = $"{d.VDLine}{new String (' ', width - btnNext.Length - 2)}{btnNext}{d.VDLine}";
  144. var bottomRow = $"{d.LLDCorner}{new String (d.HDLine.ToString () [0], width - 2)}{d.LRDCorner}";
  145. var wizard = new Wizard (title) { Width = width, Height = height };
  146. wizard.AddStep (new Wizard.WizardStep ("ABCD"));
  147. Application.End (Application.Begin (wizard));
  148. GraphViewTests.AssertDriverContentsWithFrameAre ($"{topRow}\n{separatorRow}\n{buttonRow}\n{bottomRow}", output);
  149. }
  150. [Fact, AutoInitShutdown]
  151. public void Navigate_GetPreviousStep_Correct ()
  152. {
  153. var wizard = new Wizard ();
  154. // If no steps should be null
  155. Assert.Null (wizard.GetPreviousStep ());
  156. var step1 = new Wizard.WizardStep ("step1");
  157. wizard.AddStep (step1);
  158. // If no current step, should be last step
  159. Assert.Equal (step1.Title.ToString(), wizard.GetPreviousStep ().Title.ToString());
  160. wizard.CurrentStep = step1;
  161. // If there is 1 step it's current step should be null
  162. Assert.Null (wizard.GetPreviousStep ());
  163. // If one disabled step should be null
  164. step1.Enabled = false;
  165. Assert.Null (wizard.GetPreviousStep ());
  166. // If two steps and at 2 and step 1 is `Enabled = true`should be step1
  167. var step2 = new Wizard.WizardStep ("step2");
  168. wizard.AddStep (step2);
  169. wizard.CurrentStep = step2;
  170. step1.Enabled = true;
  171. Assert.Equal (step1.Title.ToString(), wizard.GetPreviousStep ().Title.ToString());
  172. // If two steps and at 2 and step 1 is `Enabled = false` should be null
  173. step1.Enabled = false;
  174. Assert.Null (wizard.GetPreviousStep ());
  175. // If three steps with Step2.Enabled = true
  176. // At step 1 should be null
  177. // At step 2 should be step 1
  178. // At step 3 should be step 2
  179. var step3 = new Wizard.WizardStep ("step3");
  180. wizard.AddStep (step3);
  181. step1.Enabled = true;
  182. wizard.CurrentStep = step1;
  183. step2.Enabled = true;
  184. step3.Enabled = true;
  185. Assert.Null (wizard.GetPreviousStep ());
  186. wizard.CurrentStep = step2;
  187. Assert.Equal (step1.Title.ToString(), wizard.GetPreviousStep ().Title.ToString());
  188. wizard.CurrentStep = step3;
  189. Assert.Equal (step2.Title.ToString(), wizard.GetPreviousStep ().Title.ToString());
  190. // If three steps with Step2.Enabled = false
  191. // At step 1 should be null
  192. // At step 3 should be step1
  193. step1.Enabled = true;
  194. step2.Enabled = false;
  195. step3.Enabled = true;
  196. wizard.CurrentStep = step1;
  197. Assert.Null (wizard.GetPreviousStep ());
  198. wizard.CurrentStep = step3;
  199. Assert.Equal (step1.Title.ToString(), wizard.GetPreviousStep ().Title.ToString());
  200. // If three steps with Step1.Enabled = false & Step2.Enabled = false
  201. // At step 3 should be null
  202. // If no current step, GetPreviousStep provides equivalent to GetLastStep
  203. wizard.CurrentStep = null;
  204. step1.Enabled = true;
  205. step2.Enabled = true;
  206. step3.Enabled = true;
  207. Assert.Equal (step3.Title.ToString(), wizard.GetPreviousStep ().Title.ToString());
  208. step1.Enabled = false;
  209. step2.Enabled = true;
  210. step3.Enabled = true;
  211. Assert.Equal (step3.Title.ToString(), wizard.GetPreviousStep ().Title.ToString());
  212. step1.Enabled = false;
  213. step2.Enabled = false;
  214. step3.Enabled = true;
  215. Assert.Equal (step3.Title.ToString(), wizard.GetPreviousStep ().Title.ToString());
  216. step1.Enabled = false;
  217. step2.Enabled = true;
  218. step3.Enabled = false;
  219. Assert.Equal (step2.Title.ToString(), wizard.GetPreviousStep ().Title.ToString());
  220. step1.Enabled = true;
  221. step2.Enabled = false;
  222. step3.Enabled = false;
  223. Assert.Equal (step1.Title.ToString(), wizard.GetPreviousStep ().Title.ToString());
  224. }
  225. [Fact, AutoInitShutdown]
  226. public void Navigate_GetNextStep_Correct ()
  227. {
  228. var wizard = new Wizard ();
  229. // If no steps should be null
  230. Assert.Null (wizard.GetNextStep ());
  231. var step1 = new Wizard.WizardStep ("step1");
  232. wizard.AddStep (step1);
  233. // If no current step, should be first step
  234. Assert.Equal (step1.Title.ToString(), wizard.GetNextStep ().Title.ToString());
  235. wizard.CurrentStep = step1;
  236. // If there is 1 step it's current step should be null
  237. Assert.Null (wizard.GetNextStep ());
  238. // If one disabled step should be null
  239. step1.Enabled = false;
  240. Assert.Null (wizard.GetNextStep ());
  241. // If two steps and at 1 and step 2 is `Enabled = true`should be step 2
  242. var step2 = new Wizard.WizardStep ("step2");
  243. wizard.AddStep (step2);
  244. Assert.Equal (step2.Title.ToString(), wizard.GetNextStep ().Title.ToString());
  245. // If two steps and at 1 and step 2 is `Enabled = false` should be null
  246. step1.Enabled = true;
  247. wizard.CurrentStep = step1;
  248. step2.Enabled = false;
  249. Assert.Null (wizard.GetNextStep ());
  250. // If three steps with Step2.Enabled = true
  251. // At step 1 should be step 2
  252. // At step 2 should be step 3
  253. // At step 3 should be null
  254. var step3 = new Wizard.WizardStep ("step3");
  255. wizard.AddStep (step3);
  256. step1.Enabled = true;
  257. wizard.CurrentStep = step1;
  258. step2.Enabled = true;
  259. step3.Enabled = true;
  260. Assert.Equal (step2.Title.ToString(), wizard.GetNextStep ().Title.ToString());
  261. wizard.CurrentStep = step2;
  262. Assert.Equal (step3.Title.ToString(), wizard.GetNextStep ().Title.ToString());
  263. wizard.CurrentStep = step3;
  264. Assert.Null (wizard.GetNextStep ());
  265. // If three steps with Step2.Enabled = false
  266. // At step 1 should be step 3
  267. // At step 3 should be null
  268. step1.Enabled = true;
  269. wizard.CurrentStep = step1;
  270. step2.Enabled = false;
  271. step3.Enabled = true;
  272. Assert.Equal (step3.Title.ToString(), wizard.GetNextStep ().Title.ToString());
  273. wizard.CurrentStep = step3;
  274. Assert.Null (wizard.GetNextStep ());
  275. // If three steps with Step2.Enabled = false & Step3.Enabled = false
  276. // At step 1 should be null
  277. step1.Enabled = true;
  278. wizard.CurrentStep = step1;
  279. step2.Enabled = false;
  280. step3.Enabled = false;
  281. Assert.Null (wizard.GetNextStep ());
  282. // If no current step, GetNextStep provides equivalent to GetFirstStep
  283. wizard.CurrentStep = null;
  284. step1.Enabled = true;
  285. step2.Enabled = true;
  286. step3.Enabled = true;
  287. Assert.Equal (step1.Title.ToString(), wizard.GetNextStep ().Title.ToString());
  288. step1.Enabled = false;
  289. step2.Enabled = true;
  290. step3.Enabled = true;
  291. Assert.Equal (step2.Title.ToString(), wizard.GetNextStep ().Title.ToString());
  292. step1.Enabled = false;
  293. step2.Enabled = false;
  294. step3.Enabled = true;
  295. Assert.Equal (step3.Title.ToString(), wizard.GetNextStep ().Title.ToString());
  296. step1.Enabled = false;
  297. step2.Enabled = true;
  298. step3.Enabled = false;
  299. Assert.Equal (step2.Title.ToString(), wizard.GetNextStep ().Title.ToString());
  300. step1.Enabled = true;
  301. step2.Enabled = false;
  302. step3.Enabled = false;
  303. Assert.Equal (step1.Title.ToString(), wizard.GetNextStep ().Title.ToString());
  304. }
  305. [Fact, AutoInitShutdown]
  306. public void Navigate_GoNext_Works ()
  307. {
  308. // If zero steps do nothing
  309. // If one step do nothing (enabled or disabled)
  310. // If two steps
  311. // If current is 1
  312. // If 2 is enabled 2 becomes current
  313. // If 2 is disabled 1 stays current
  314. // If current is 2 does nothing
  315. }
  316. [Fact, AutoInitShutdown]
  317. public void Navigate_GoBack_Works ()
  318. {
  319. // If zero steps do nothing
  320. // If one step do nothing (enabled or disabled)
  321. // If two steps
  322. // If current is 1 does nothing
  323. // If current is 2 does nothing
  324. // If 1 is enabled 2 becomes current
  325. // If 1 is disabled 1 stays current
  326. }
  327. }
  328. }