FileDialogFluentTests.cs 15 KB

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