FileDialogFluentTests.cs 15 KB

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