FileDialogFluentTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. using System.Globalization;
  2. using System.IO.Abstractions;
  3. using System.IO.Abstractions.TestingHelpers;
  4. using System.Runtime.InteropServices;
  5. using TerminalGuiFluentTesting;
  6. using TerminalGuiFluentTestingXunit;
  7. using Xunit.Abstractions;
  8. namespace IntegrationTests.FluentTests;
  9. public class FileDialogFluentTests
  10. {
  11. private readonly TextWriter _out;
  12. public FileDialogFluentTests (ITestOutputHelper outputHelper)
  13. {
  14. CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
  15. _out = new TestOutputWriter (outputHelper);
  16. }
  17. private MockFileSystem CreateExampleFileSystem ()
  18. {
  19. // Optional: use Ordinal to simulate Linux-style case sensitivity
  20. var mockFileSystem = new MockFileSystem (new Dictionary<string, MockFileData> ());
  21. string testDir = mockFileSystem.Path.Combine ("test-dir");
  22. string subDir = mockFileSystem.Path.Combine (testDir, "sub-dir");
  23. string logsDir = "logs";
  24. string emptyDir = "empty-dir";
  25. // Add files
  26. mockFileSystem.AddFile (mockFileSystem.Path.Combine (testDir, "file1.txt"), new MockFileData ("Hello, this is file 1."));
  27. mockFileSystem.AddFile (mockFileSystem.Path.Combine (testDir, "file2.txt"), new MockFileData ("Hello, this is file 2."));
  28. mockFileSystem.AddFile (mockFileSystem.Path.Combine (subDir, "nested-file.txt"), new MockFileData ("This is a nested file."));
  29. mockFileSystem.AddFile (mockFileSystem.Path.Combine (logsDir, "log1.log"), new MockFileData ("Log entry 1"));
  30. mockFileSystem.AddFile (mockFileSystem.Path.Combine (logsDir, "log2.log"), new MockFileData ("Log entry 2"));
  31. // Create an empty directory
  32. mockFileSystem.AddDirectory (emptyDir);
  33. return mockFileSystem;
  34. }
  35. private Toplevel NewSaveDialog (out SaveDialog sd, bool modal = true)
  36. {
  37. return NewSaveDialog (out sd, out _, modal);
  38. }
  39. private Toplevel NewSaveDialog (out SaveDialog sd, out MockFileSystem fs,bool modal = true)
  40. {
  41. fs = CreateExampleFileSystem ();
  42. sd = new SaveDialog (fs) { Modal = modal };
  43. return sd;
  44. }
  45. [Theory]
  46. [ClassData (typeof (V2TestDrivers))]
  47. public void CancelFileDialog_UsingEscape (V2TestDriver d)
  48. {
  49. SaveDialog? sd = null;
  50. using var c = With.A (()=>NewSaveDialog(out sd), 100, 20, d)
  51. .ScreenShot ("Save dialog", _out)
  52. .Escape ()
  53. .AssertTrue (sd!.Canceled)
  54. .Stop ();
  55. }
  56. [Theory]
  57. [ClassData (typeof (V2TestDrivers))]
  58. public void CancelFileDialog_UsingCancelButton_TabThenEnter (V2TestDriver d)
  59. {
  60. SaveDialog? sd = null;
  61. using var c = With.A (() => NewSaveDialog (out sd,modal:false), 100, 20, d)
  62. .ScreenShot ("Save dialog", _out)
  63. .Focus<Button> (b => b.Text == "_Cancel")
  64. .AssertTrue (sd.Canceled)
  65. .Enter ()
  66. .Stop ();
  67. }
  68. [Theory]
  69. [ClassData (typeof (V2TestDrivers))]
  70. public void CancelFileDialog_UsingCancelButton_LeftClickButton (V2TestDriver d)
  71. {
  72. SaveDialog? sd = null;
  73. using var c = With.A (() => NewSaveDialog (out sd), 100, 20, d)
  74. .ScreenShot ("Save dialog", _out)
  75. .LeftClick<Button> (b => b.Text == "_Cancel")
  76. .WriteOutLogs (_out)
  77. .AssertTrue (sd.Canceled)
  78. .Stop ();
  79. }
  80. [Theory]
  81. [ClassData (typeof (V2TestDrivers))]
  82. public void CancelFileDialog_UsingCancelButton_AltC (V2TestDriver d)
  83. {
  84. SaveDialog? sd = null;
  85. using var c = With.A (() => NewSaveDialog (out sd), 100, 20, d)
  86. .ScreenShot ("Save dialog", _out)
  87. .Send (Key.C.WithAlt)
  88. .WriteOutLogs (_out)
  89. .AssertTrue (sd.Canceled)
  90. .Stop ();
  91. }
  92. [Theory]
  93. [ClassData (typeof (V2TestDrivers))]
  94. public void SaveFileDialog_UsingOkButton_Enter (V2TestDriver d)
  95. {
  96. SaveDialog? sd = null;
  97. MockFileSystem? fs = null;
  98. using var c = With.A (() => NewSaveDialog (out sd,out fs), 100, 20, d)
  99. .ScreenShot ("Save dialog", _out)
  100. .LeftClick<Button> (b => b.Text == "_Save")
  101. .WaitIteration ()
  102. .WriteOutLogs (_out)
  103. .AssertFalse(sd.Canceled)
  104. .AssertEqual (GetFileSystemRoot (fs), sd.FileName)
  105. .Stop ();
  106. }
  107. [Theory]
  108. [ClassData (typeof (V2TestDrivers))]
  109. public void SaveFileDialog_UsingOkButton_AltS (V2TestDriver d)
  110. {
  111. SaveDialog? sd = null;
  112. MockFileSystem? fs = null;
  113. using var c = With.A (() => NewSaveDialog (out sd, out fs), 100, 20, d)
  114. .ScreenShot ("Save dialog", _out)
  115. .Send (Key.S.WithAlt)
  116. .WriteOutLogs (_out)
  117. .AssertFalse (sd.Canceled)
  118. .AssertEqual (GetFileSystemRoot (fs), sd.FileName)
  119. .Stop ();
  120. }
  121. [Theory]
  122. [ClassData (typeof (V2TestDrivers))]
  123. public void SaveFileDialog_UsingOkButton_TabEnter (V2TestDriver d)
  124. {
  125. SaveDialog? sd = null;
  126. MockFileSystem? fs = null;
  127. using var c = With.A (() => NewSaveDialog (out sd, out fs,modal:false), 100, 20, d)
  128. .ScreenShot ("Save dialog", _out)
  129. .Focus<Button> (b => b.Text == "_Save")
  130. .Enter ()
  131. .WriteOutLogs (_out)
  132. .AssertFalse(sd.Canceled)
  133. .AssertEqual (GetFileSystemRoot(fs), sd.FileName)
  134. .Stop ();
  135. }
  136. private string GetFileSystemRoot (IFileSystem fs)
  137. {
  138. return RuntimeInformation.IsOSPlatform (OSPlatform.Windows) ?
  139. $@"C:{fs.Path.DirectorySeparatorChar}" :
  140. "/";
  141. }
  142. [Theory]
  143. [ClassData (typeof (V2TestDrivers))]
  144. public void SaveFileDialog_PressingPopTree_ShouldNotChangeCancel (V2TestDriver d)
  145. {
  146. SaveDialog? sd = null;
  147. MockFileSystem? fs = null;
  148. using var c = With.A (() => NewSaveDialog (out sd, out fs,modal:false), 100, 20, d)
  149. .ScreenShot ("Save dialog", _out)
  150. .AssertTrue (sd.Canceled)
  151. .Focus<Button> (b => b.Text == "►►")
  152. .Enter ()
  153. .ScreenShot ("After pop tree", _out)
  154. .WriteOutLogs (_out)
  155. .AssertTrue (sd.Canceled)
  156. .Stop ();
  157. }
  158. [Theory]
  159. [ClassData (typeof (V2TestDrivers))]
  160. public void SaveFileDialog_PopTree_AndNavigate (V2TestDriver d)
  161. {
  162. SaveDialog? sd = null;
  163. MockFileSystem? fs = null;
  164. using var c = With.A (() => NewSaveDialog (out sd, out fs, modal: false), 100, 20, d)
  165. .ScreenShot ("Save dialog", _out)
  166. .AssertTrue (sd.Canceled)
  167. .LeftClick<Button> (b => b.Text == "►►")
  168. .ScreenShot ("After pop tree", _out)
  169. .Focus<TreeView<IFileSystemInfo>> (_ => true)
  170. .Right ()
  171. .ScreenShot ("After expand tree", _out)
  172. .Down ()
  173. .ScreenShot ("After navigate down in tree", _out)
  174. .Enter ()
  175. .WaitIteration ()
  176. .AssertFalse (sd.Canceled)
  177. .AssertContains ("empty-dir", sd.FileName)
  178. .WriteOutLogs (_out)
  179. .Stop ();
  180. }
  181. [Theory]
  182. [ClassData (typeof (V2TestDrivers))]
  183. public void SaveFileDialog_PopTree_AndNavigate_PreserveFilenameOnDirectoryChanges_True (V2TestDriver d)
  184. {
  185. SaveDialog? sd = null;
  186. MockFileSystem? fs = null;
  187. using var c = With.A (() => NewSaveDialog (out sd, out fs, modal: false), 100, 20, d)
  188. .Then (()=>sd.Style.PreserveFilenameOnDirectoryChanges=true)
  189. .ScreenShot ("Save dialog", _out)
  190. .AssertTrue (sd.Canceled)
  191. .Focus<TextField> (_=>true)
  192. // Clear selection by pressing right in 'file path' text box
  193. .RaiseKeyDownEvent (Key.CursorRight)
  194. .AssertIsType <TextField>(sd.Focused)
  195. // Type a filename into the dialog
  196. .RaiseKeyDownEvent (Key.H)
  197. .RaiseKeyDownEvent (Key.E)
  198. .RaiseKeyDownEvent (Key.L)
  199. .RaiseKeyDownEvent (Key.L)
  200. .RaiseKeyDownEvent (Key.O)
  201. .WaitIteration ()
  202. .ScreenShot ("After typing filename 'hello'", _out)
  203. .AssertEndsWith ("hello", sd.Path)
  204. .LeftClick<Button> (b => b.Text == "►►")
  205. .ScreenShot ("After pop tree", _out)
  206. .Focus<TreeView<IFileSystemInfo>> (_ => true)
  207. .Right ()
  208. .ScreenShot ("After expand tree", _out)
  209. // Because of PreserveFilenameOnDirectoryChanges we should select the new dir but keep the filename
  210. .AssertEndsWith ("hello", sd.Path)
  211. .Down ()
  212. .ScreenShot ("After navigate down in tree", _out)
  213. // Because of PreserveFilenameOnDirectoryChanges we should select the new dir but keep the filename
  214. .AssertContains ("empty-dir",sd.Path)
  215. .AssertEndsWith ("hello", sd.Path)
  216. .Enter ()
  217. .WaitIteration ()
  218. .AssertFalse (sd.Canceled)
  219. .AssertContains ("empty-dir", sd.FileName)
  220. .WriteOutLogs (_out)
  221. .Stop ();
  222. }
  223. [Theory]
  224. [ClassData (typeof (V2TestDrivers))]
  225. public void SaveFileDialog_PopTree_AndNavigate_PreserveFilenameOnDirectoryChanges_False (V2TestDriver d)
  226. {
  227. SaveDialog? sd = null;
  228. MockFileSystem? fs = null;
  229. using var c = With.A (() => NewSaveDialog (out sd, out fs, modal: false), 100, 20, d)
  230. .Then (()=> sd.Style.PreserveFilenameOnDirectoryChanges = false)
  231. .ScreenShot ("Save dialog", _out)
  232. .AssertTrue (sd.Canceled)
  233. .Focus<TextField> (_ => true)
  234. // Clear selection by pressing right in 'file path' text box
  235. .RaiseKeyDownEvent (Key.CursorRight)
  236. .AssertIsType<TextField> (sd.Focused)
  237. // Type a filename into the dialog
  238. .RaiseKeyDownEvent (Key.H)
  239. .RaiseKeyDownEvent (Key.E)
  240. .RaiseKeyDownEvent (Key.L)
  241. .RaiseKeyDownEvent (Key.L)
  242. .RaiseKeyDownEvent (Key.O)
  243. .WaitIteration ()
  244. .ScreenShot ("After typing filename 'hello'", _out)
  245. .AssertEndsWith ("hello", sd.Path)
  246. .LeftClick<Button> (b => b.Text == "►►")
  247. .ScreenShot ("After pop tree", _out)
  248. .Focus<TreeView<IFileSystemInfo>> (_ => true)
  249. .Right ()
  250. .ScreenShot ("After expand tree", _out)
  251. .Down ()
  252. .ScreenShot ("After navigate down in tree", _out)
  253. // PreserveFilenameOnDirectoryChanges is false so just select new path
  254. .AssertEndsWith ("empty-dir", sd.Path)
  255. .AssertDoesNotContain ("hello", sd.Path)
  256. .Enter ()
  257. .WaitIteration ()
  258. .AssertFalse (sd.Canceled)
  259. .AssertContains ("empty-dir", sd.FileName)
  260. .WriteOutLogs (_out)
  261. .Stop ();
  262. }
  263. [Theory]
  264. [ClassData (typeof (V2TestDrivers_WithTrueFalseParameter))]
  265. public void SaveFileDialog_TableView_UpDown_PreserveFilenameOnDirectoryChanges_True (V2TestDriver d, bool preserve)
  266. {
  267. SaveDialog? sd = null;
  268. MockFileSystem? fs = null;
  269. using var c = With.A (() => NewSaveDialog (out sd, out fs, modal: false), 100, 20, d)
  270. .Then (() => sd.Style.PreserveFilenameOnDirectoryChanges = preserve)
  271. .ScreenShot ("Save dialog", _out)
  272. .AssertTrue (sd.Canceled)
  273. .Focus<TextField> (_ => true)
  274. // Clear selection by pressing right in 'file path' text box
  275. .RaiseKeyDownEvent (Key.CursorRight)
  276. .AssertIsType<TextField> (sd.Focused)
  277. // Type a filename into the dialog
  278. .RaiseKeyDownEvent (Key.H)
  279. .RaiseKeyDownEvent (Key.E)
  280. .RaiseKeyDownEvent (Key.L)
  281. .RaiseKeyDownEvent (Key.L)
  282. .RaiseKeyDownEvent (Key.O)
  283. .WaitIteration ()
  284. .ScreenShot ("After typing filename 'hello'", _out)
  285. .AssertEndsWith ("hello", sd.Path)
  286. .Focus<TableView> (_ => true)
  287. .ScreenShot ("After focus table", _out)
  288. .Down ()
  289. .ScreenShot ("After down in table", _out);
  290. if (preserve)
  291. {
  292. c.AssertContains ("logs", sd.Path)
  293. .AssertEndsWith ("hello", sd.Path);
  294. }
  295. else
  296. {
  297. c.AssertContains ("logs", sd.Path)
  298. .AssertDoesNotContain ("hello", sd.Path);
  299. }
  300. c.Up ()
  301. .ScreenShot ("After up in table", _out);
  302. if (preserve)
  303. {
  304. c.AssertContains ("empty-dir", sd.Path)
  305. .AssertEndsWith ("hello", sd.Path);
  306. }
  307. else
  308. {
  309. c.AssertContains ("empty-dir", sd.Path)
  310. .AssertDoesNotContain ("hello", sd.Path);
  311. }
  312. c.Enter ()
  313. .ScreenShot ("After enter in table", _out); ;
  314. if (preserve)
  315. {
  316. c.AssertContains ("empty-dir", sd.Path)
  317. .AssertEndsWith ("hello", sd.Path);
  318. }
  319. else
  320. {
  321. c.AssertContains ("empty-dir", sd.Path)
  322. .AssertDoesNotContain ("hello", sd.Path);
  323. }
  324. c.LeftClick<Button> (b => b.Text == "_Save");
  325. c.WaitIteration ();
  326. c.AssertFalse (sd.Canceled);
  327. if (preserve)
  328. {
  329. c.AssertContains ("empty-dir", sd.Path)
  330. .AssertEndsWith ("hello", sd.Path);
  331. }
  332. else
  333. {
  334. c.AssertContains ("empty-dir", sd.Path)
  335. .AssertDoesNotContain ("hello", sd.Path);
  336. }
  337. c.WriteOutLogs (_out);
  338. c.WaitIteration ();
  339. c.Stop ();
  340. }
  341. }