FileDialogTests.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. using System.IO.Abstractions.TestingHelpers;
  2. using System.Runtime.InteropServices;
  3. using Xunit.Abstractions;
  4. namespace Terminal.Gui.FileServicesTests;
  5. public class FileDialogTests (ITestOutputHelper output)
  6. {
  7. [Theory]
  8. [InlineData (true)]
  9. [InlineData (false)]
  10. [AutoInitShutdown]
  11. public void CancelSelection (bool cancel)
  12. {
  13. FileDialog dlg = GetInitializedFileDialog ();
  14. string openIn = Path.Combine (Environment.CurrentDirectory, "zz");
  15. Directory.CreateDirectory (openIn);
  16. dlg.Path = openIn + Path.DirectorySeparatorChar;
  17. dlg.FilesSelected += (s, e) => e.Cancel = cancel;
  18. //pressing enter will complete the current selection
  19. // unless the event cancels the confirm
  20. Send ('\n', ConsoleKey.Enter);
  21. Assert.Equal (cancel, dlg.Canceled);
  22. dlg.Dispose ();
  23. }
  24. [Fact]
  25. [AutoInitShutdown]
  26. public void DirectTyping_Allowed ()
  27. {
  28. FileDialog dlg = GetInitializedFileDialog ();
  29. TextField tf = dlg.Subviews.OfType<TextField> ().First (t => t.HasFocus);
  30. tf.ClearAllSelection ();
  31. tf.CursorPosition = tf.Text.Length;
  32. Assert.True (tf.HasFocus);
  33. SendSlash ();
  34. Assert.Equal (
  35. new DirectoryInfo (Environment.CurrentDirectory + Path.DirectorySeparatorChar).FullName,
  36. new DirectoryInfo (dlg.Path + Path.DirectorySeparatorChar).FullName
  37. );
  38. // continue typing the rest of the path
  39. Send ("BOB");
  40. Send ('.', ConsoleKey.OemPeriod);
  41. Send ("CSV");
  42. Assert.True (dlg.Canceled);
  43. Send ('\n', ConsoleKey.Enter);
  44. Assert.False (dlg.Canceled);
  45. Assert.Equal ("bob.csv", Path.GetFileName (dlg.Path));
  46. dlg.Dispose ();
  47. }
  48. [Fact]
  49. [AutoInitShutdown]
  50. public void DirectTyping_AutoComplete ()
  51. {
  52. FileDialog dlg = GetInitializedFileDialog ();
  53. string openIn = Path.Combine (Environment.CurrentDirectory, "zz");
  54. Directory.CreateDirectory (openIn);
  55. string expectedDest = Path.Combine (openIn, "xx");
  56. Directory.CreateDirectory (expectedDest);
  57. dlg.Path = openIn + Path.DirectorySeparatorChar;
  58. Send ("X");
  59. // nothing selected yet
  60. Assert.True (dlg.Canceled);
  61. Assert.Equal ("x", Path.GetFileName (dlg.Path));
  62. // complete auto typing
  63. Send ('\t', ConsoleKey.Tab);
  64. // but do not close dialog
  65. Assert.True (dlg.Canceled);
  66. Assert.EndsWith ("xx" + Path.DirectorySeparatorChar, dlg.Path);
  67. // press enter again to confirm the dialog
  68. Send ('\n', ConsoleKey.Enter);
  69. Assert.False (dlg.Canceled);
  70. Assert.EndsWith ("xx" + Path.DirectorySeparatorChar, dlg.Path);
  71. dlg.Dispose ();
  72. }
  73. [Fact]
  74. [AutoInitShutdown]
  75. public void DoNotConfirmSelectionWhenFindFocused ()
  76. {
  77. FileDialog dlg = GetInitializedFileDialog ();
  78. string openIn = Path.Combine (Environment.CurrentDirectory, "zz");
  79. Directory.CreateDirectory (openIn);
  80. dlg.Path = openIn + Path.DirectorySeparatorChar;
  81. Application.OnKeyDown (Key.Tab);
  82. Application.OnKeyDown (Key.Tab);
  83. Application.OnKeyDown (Key.Tab);
  84. Assert.IsType<TextField> (dlg.MostFocused);
  85. var tf = (TextField)dlg.MostFocused;
  86. Assert.Equal ("Enter Search", tf.Caption);
  87. // Dialog has not yet been confirmed with a choice
  88. Assert.True (dlg.Canceled);
  89. //pressing enter while search focused should not confirm path
  90. Send ('\n', ConsoleKey.Enter);
  91. Assert.True (dlg.Canceled);
  92. // tabbing out of search
  93. Send ('\t', ConsoleKey.Tab);
  94. //should allow enter to confirm path
  95. Send ('\n', ConsoleKey.Enter);
  96. // Dialog has not yet been confirmed with a choice
  97. Assert.False (dlg.Canceled);
  98. dlg.Dispose ();
  99. }
  100. [Fact]
  101. [AutoInitShutdown]
  102. public void DotDot_MovesToRoot_ThenPressBack ()
  103. {
  104. FileDialog dlg = GetDialog ();
  105. dlg.OpenMode = OpenMode.Directory;
  106. dlg.AllowsMultipleSelection = true;
  107. var selected = false;
  108. dlg.FilesSelected += (s, e) => { selected = true; };
  109. AssertIsTheStartingDirectory (dlg.Path);
  110. Assert.IsType<TextField> (dlg.MostFocused);
  111. Send ('v', ConsoleKey.DownArrow);
  112. Assert.IsType<TableView> (dlg.MostFocused);
  113. // ".." should be the first thing selected
  114. // ".." should not mess with the displayed path
  115. AssertIsTheStartingDirectory (dlg.Path);
  116. // Accept navigation up a directory
  117. Send ('\n', ConsoleKey.Enter);
  118. AssertIsTheRootDirectory (dlg.Path);
  119. Assert.True (dlg.Canceled);
  120. Assert.False (selected);
  121. // Now press the back button (in table view)
  122. Send ('<', ConsoleKey.Backspace);
  123. // Should move us back to the root
  124. AssertIsTheStartingDirectory (dlg.Path);
  125. Assert.True (dlg.Canceled);
  126. Assert.False (selected);
  127. dlg.Dispose ();
  128. }
  129. [Theory]
  130. [AutoInitShutdown]
  131. [InlineData (true)]
  132. [InlineData (false)]
  133. public void MultiSelectDirectory_CannotToggleDotDot (bool acceptWithEnter)
  134. {
  135. FileDialog dlg = GetDialog ();
  136. dlg.OpenMode = OpenMode.Directory;
  137. dlg.AllowsMultipleSelection = true;
  138. IReadOnlyCollection<string> eventMultiSelected = null;
  139. dlg.FilesSelected += (s, e) => { eventMultiSelected = e.Dialog.MultiSelected; };
  140. Assert.IsType<TextField> (dlg.MostFocused);
  141. Send ('v', ConsoleKey.DownArrow);
  142. Assert.IsType<TableView> (dlg.MostFocused);
  143. // Try to toggle '..'
  144. Send (' ', ConsoleKey.Spacebar);
  145. Send ('v', ConsoleKey.DownArrow);
  146. // Toggle subfolder
  147. Send (' ', ConsoleKey.Spacebar);
  148. Assert.True (dlg.Canceled);
  149. if (acceptWithEnter)
  150. {
  151. Send ('\n', ConsoleKey.Enter);
  152. }
  153. else
  154. {
  155. Send ('O', ConsoleKey.O, false, true);
  156. }
  157. Assert.False (dlg.Canceled);
  158. Assert.Multiple (
  159. () =>
  160. {
  161. // Only the subfolder should be selected
  162. Assert.Single (dlg.MultiSelected);
  163. AssertIsTheSubfolder (dlg.Path);
  164. AssertIsTheSubfolder (dlg.MultiSelected.Single ());
  165. },
  166. () =>
  167. {
  168. // Event should also agree with the final state
  169. Assert.NotNull (eventMultiSelected);
  170. Assert.Single (eventMultiSelected);
  171. AssertIsTheSubfolder (eventMultiSelected.Single ());
  172. }
  173. );
  174. dlg.Dispose ();
  175. }
  176. [Theory]
  177. [AutoInitShutdown]
  178. [InlineData (true)]
  179. [InlineData (false)]
  180. public void MultiSelectDirectory_CanToggleThenAccept (bool acceptWithEnter)
  181. {
  182. FileDialog dlg = GetDialog ();
  183. dlg.OpenMode = OpenMode.Directory;
  184. dlg.AllowsMultipleSelection = true;
  185. IReadOnlyCollection<string> eventMultiSelected = null;
  186. dlg.FilesSelected += (s, e) => { eventMultiSelected = e.Dialog.MultiSelected; };
  187. Assert.IsType<TextField> (dlg.MostFocused);
  188. Send ('v', ConsoleKey.DownArrow);
  189. Assert.IsType<TableView> (dlg.MostFocused);
  190. // Move selection to subfolder
  191. Send ('v', ConsoleKey.DownArrow);
  192. // Toggle subfolder
  193. Send (' ', ConsoleKey.Spacebar);
  194. Assert.True (dlg.Canceled);
  195. if (acceptWithEnter)
  196. {
  197. Send ('\n', ConsoleKey.Enter);
  198. }
  199. else
  200. {
  201. Send ('O', ConsoleKey.O, false, true);
  202. }
  203. Assert.False (dlg.Canceled);
  204. Assert.Multiple (
  205. () =>
  206. {
  207. // Only the subfolder should be selected
  208. Assert.Single (dlg.MultiSelected);
  209. AssertIsTheSubfolder (dlg.Path);
  210. AssertIsTheSubfolder (dlg.MultiSelected.Single ());
  211. },
  212. () =>
  213. {
  214. // Event should also agree with the final state
  215. Assert.NotNull (eventMultiSelected);
  216. Assert.Single (eventMultiSelected);
  217. AssertIsTheSubfolder (eventMultiSelected.Single ());
  218. }
  219. );
  220. dlg.Dispose ();
  221. }
  222. [Fact]
  223. [AutoInitShutdown]
  224. public void MultiSelectDirectory_EnterOpensFolder ()
  225. {
  226. FileDialog dlg = GetDialog ();
  227. dlg.OpenMode = OpenMode.Directory;
  228. dlg.AllowsMultipleSelection = true;
  229. IReadOnlyCollection<string> eventMultiSelected = null;
  230. dlg.FilesSelected += (s, e) => { eventMultiSelected = e.Dialog.MultiSelected; };
  231. Assert.IsType<TextField> (dlg.MostFocused);
  232. Send ('v', ConsoleKey.DownArrow);
  233. Assert.IsType<TableView> (dlg.MostFocused);
  234. // Move selection to subfolder
  235. Send ('v', ConsoleKey.DownArrow);
  236. Send ('\n', ConsoleKey.Enter);
  237. // Path should update to the newly opened folder
  238. AssertIsTheSubfolder (dlg.Path);
  239. // No selection will have been confirmed
  240. Assert.True (dlg.Canceled);
  241. Assert.Empty (dlg.MultiSelected);
  242. Assert.Null (eventMultiSelected);
  243. dlg.Dispose ();
  244. }
  245. [Fact]
  246. [AutoInitShutdown]
  247. public void OnLoad_TextBoxIsFocused ()
  248. {
  249. FileDialog dlg = GetInitializedFileDialog ();
  250. View tf = dlg.Subviews.FirstOrDefault (t => t.HasFocus);
  251. Assert.NotNull (tf);
  252. Assert.IsType<TextField> (tf);
  253. dlg.Dispose ();
  254. }
  255. [Theory]
  256. [AutoInitShutdown]
  257. [InlineData (true, true)]
  258. [InlineData (true, false)]
  259. [InlineData (false, true)]
  260. [InlineData (false, false)]
  261. public void PickDirectory_ArrowNavigation (bool openModeMixed, bool multiple)
  262. {
  263. FileDialog dlg = GetDialog ();
  264. dlg.OpenMode = openModeMixed ? OpenMode.Mixed : OpenMode.Directory;
  265. dlg.AllowsMultipleSelection = multiple;
  266. Assert.IsType<TextField> (dlg.MostFocused);
  267. Send ('v', ConsoleKey.DownArrow);
  268. Assert.IsType<TableView> (dlg.MostFocused);
  269. // Should be selecting ..
  270. Send ('v', ConsoleKey.DownArrow);
  271. // Down to the directory
  272. Assert.True (dlg.Canceled);
  273. // Alt+O to open (enter would just navigate into the child dir)
  274. Send ('O', ConsoleKey.O, false, true);
  275. Assert.False (dlg.Canceled);
  276. AssertIsTheSubfolder (dlg.Path);
  277. dlg.Dispose ();
  278. }
  279. [Theory]
  280. [AutoInitShutdown]
  281. [InlineData (true, true)]
  282. [InlineData (true, false)]
  283. [InlineData (false, true)]
  284. [InlineData (false, false)]
  285. public void PickDirectory_DirectTyping (bool openModeMixed, bool multiple)
  286. {
  287. FileDialog dlg = GetDialog ();
  288. dlg.OpenMode = openModeMixed ? OpenMode.Mixed : OpenMode.Directory;
  289. dlg.AllowsMultipleSelection = multiple;
  290. // whe first opening the text field will have select all on
  291. // so to add to current path user must press End or right
  292. Send ('>', ConsoleKey.LeftArrow);
  293. Send ('>', ConsoleKey.RightArrow);
  294. Send ("SUBFOLDER");
  295. // Dialog has not yet been confirmed with a choice
  296. Assert.True (dlg.Canceled);
  297. // Now it has
  298. Send ('\n', ConsoleKey.Enter);
  299. Assert.False (dlg.Canceled);
  300. AssertIsTheSubfolder (dlg.Path);
  301. dlg.Dispose ();
  302. }
  303. [Theory]
  304. [InlineData (".csv", null, false)]
  305. [InlineData (".csv", "", false)]
  306. [InlineData (".csv", "c:\\MyFile.csv", true)]
  307. [InlineData (".csv", "c:\\MyFile.CSV", true)]
  308. [InlineData (".csv", "c:\\MyFile.csv.bak", false)]
  309. public void TestAllowedType_Basic (string allowed, string candidate, bool expected)
  310. {
  311. Assert.Equal (expected, new AllowedType ("Test", allowed).IsAllowed (candidate));
  312. }
  313. [Theory]
  314. [InlineData (".Designer.cs", "c:\\MyView.Designer.cs", true)]
  315. [InlineData (".Designer.cs", "c:\\temp/MyView.Designer.cs", true)]
  316. [InlineData (".Designer.cs", "MyView.Designer.cs", true)]
  317. [InlineData (".Designer.cs", "c:\\MyView.DESIGNER.CS", true)]
  318. [InlineData (".Designer.cs", "MyView.cs", false)]
  319. public void TestAllowedType_DoubleBarreled (string allowed, string candidate, bool expected)
  320. {
  321. Assert.Equal (expected, new AllowedType ("Test", allowed).IsAllowed (candidate));
  322. }
  323. [Theory]
  324. [InlineData ("Dockerfile", "c:\\temp\\Dockerfile", true)]
  325. [InlineData ("Dockerfile", "Dockerfile", true)]
  326. [InlineData ("Dockerfile", "someimg.Dockerfile", true)]
  327. public void TestAllowedType_SpecificFile (string allowed, string candidate, bool expected)
  328. {
  329. Assert.Equal (expected, new AllowedType ("Test", allowed).IsAllowed (candidate));
  330. }
  331. [Fact]
  332. [AutoInitShutdown]
  333. public void TestDirectoryContents_Linux ()
  334. {
  335. if (IsWindows ())
  336. {
  337. return;
  338. }
  339. FileDialog fd = GetLinuxDialog ();
  340. fd.Title = string.Empty;
  341. fd.Style.Culture = new ("en-US");
  342. fd.Draw ();
  343. var expected =
  344. @$"
  345. ┌─────────────────────────────────────────────────────────────────────────┐
  346. │/demo/ │
  347. │{
  348. CM.Glyphs.LeftBracket
  349. }▲{
  350. CM.Glyphs.RightBracket
  351. } │
  352. │┌────────────┬──────────┬──────────────────────────────┬────────────────┐│
  353. ││Filename (▲)│Size │Modified │Type ││
  354. │├────────────┼──────────┼──────────────────────────────┼────────────────┤│
  355. ││.. │ │ │<Directory> ││
  356. ││/subfolder │ │2002-01-01T22:42:10 │<Directory> ││
  357. ││image.gif │4.00 B │2002-01-01T22:42:10 │.gif ││
  358. ││jQuery.js │7.00 B │2001-01-01T11:44:42 │.js ││
  359. │ │
  360. │ │
  361. │ │
  362. │{
  363. CM.Glyphs.LeftBracket
  364. } ►► {
  365. CM.Glyphs.RightBracket
  366. } Enter Search {
  367. CM.Glyphs.LeftBracket
  368. }{
  369. CM.Glyphs.LeftDefaultIndicator
  370. } OK {
  371. CM.Glyphs.RightDefaultIndicator
  372. }{
  373. CM.Glyphs.RightBracket
  374. } {
  375. CM.Glyphs.LeftBracket
  376. } Cancel {
  377. CM.Glyphs.RightBracket
  378. } │
  379. └─────────────────────────────────────────────────────────────────────────┘
  380. ";
  381. TestHelpers.AssertDriverContentsAre (expected, output, ignoreLeadingWhitespace: true);
  382. fd.Dispose ();
  383. }
  384. [Fact]
  385. [AutoInitShutdown]
  386. public void TestDirectoryContents_Windows ()
  387. {
  388. if (!IsWindows ())
  389. {
  390. return;
  391. }
  392. FileDialog fd = GetWindowsDialog ();
  393. fd.Title = string.Empty;
  394. fd.Style.Culture = new ("en-US");
  395. fd.Draw ();
  396. var expected =
  397. @$"
  398. ┌─────────────────────────────────────────────────────────────────────────┐
  399. │c:\demo\ │
  400. │{
  401. CM.Glyphs.LeftBracket
  402. }▲{
  403. CM.Glyphs.RightBracket
  404. } │
  405. │┌────────────┬──────────┬──────────────────────────────┬────────────────┐│
  406. ││Filename (▲)│Size │Modified │Type ││
  407. │├────────────┼──────────┼──────────────────────────────┼────────────────┤│
  408. ││.. │ │ │<Directory> ││
  409. ││\subfolder │ │2002-01-01T22:42:10 │<Directory> ││
  410. ││image.gif │4.00 B │2002-01-01T22:42:10 │.gif ││
  411. ││jQuery.js │7.00 B │2001-01-01T11:44:42 │.js ││
  412. ││mybinary.exe│7.00 B │2001-01-01T11:44:42 │.exe ││
  413. │ │
  414. │ │
  415. │{
  416. CM.Glyphs.LeftBracket
  417. } ►► {
  418. CM.Glyphs.RightBracket
  419. } Enter Search {
  420. CM.Glyphs.LeftBracket
  421. }{
  422. CM.Glyphs.LeftDefaultIndicator
  423. } OK {
  424. CM.Glyphs.RightDefaultIndicator
  425. }{
  426. CM.Glyphs.RightBracket
  427. } {
  428. CM.Glyphs.LeftBracket
  429. } Cancel {
  430. CM.Glyphs.RightBracket
  431. } │
  432. └─────────────────────────────────────────────────────────────────────────┘
  433. ";
  434. TestHelpers.AssertDriverContentsAre (expected, output, ignoreLeadingWhitespace: true);
  435. fd.Dispose ();
  436. }
  437. private void AssertIsTheRootDirectory (string path)
  438. {
  439. if (IsWindows ())
  440. {
  441. Assert.Equal (@"c:\", path);
  442. }
  443. else
  444. {
  445. Assert.Equal ("/", path);
  446. }
  447. }
  448. private void AssertIsTheStartingDirectory (string path)
  449. {
  450. if (IsWindows ())
  451. {
  452. Assert.Equal (@"c:\demo\", path);
  453. }
  454. else
  455. {
  456. Assert.Equal ("/demo/", path);
  457. }
  458. }
  459. private void AssertIsTheSubfolder (string path)
  460. {
  461. if (IsWindows ())
  462. {
  463. Assert.Equal (@"c:\demo\subfolder", path);
  464. }
  465. else
  466. {
  467. Assert.Equal ("/demo/subfolder", path);
  468. }
  469. }
  470. private void Begin (FileDialog dlg)
  471. {
  472. dlg.BeginInit ();
  473. dlg.EndInit ();
  474. Application.Begin (dlg);
  475. }
  476. private FileDialog GetDialog () { return IsWindows () ? GetWindowsDialog () : GetLinuxDialog (); }
  477. private FileDialog GetInitializedFileDialog ()
  478. {
  479. Window.DefaultBorderStyle = LineStyle.Single;
  480. Dialog.DefaultButtonAlignment = Alignment.Center;
  481. Dialog.DefaultBorderStyle = LineStyle.Single;
  482. var dlg = new FileDialog ();
  483. Begin (dlg);
  484. return dlg;
  485. }
  486. private FileDialog GetLinuxDialog ()
  487. {
  488. Window.DefaultBorderStyle = LineStyle.Single;
  489. Dialog.DefaultButtonAlignment = Alignment.Center;
  490. Dialog.DefaultBorderStyle = LineStyle.Single;
  491. // Arrange
  492. var fileSystem = new MockFileSystem (new Dictionary<string, MockFileData> (), "/");
  493. fileSystem.MockTime (() => new (2010, 01, 01, 11, 12, 43));
  494. fileSystem.AddFile (
  495. @"/myfile.txt",
  496. new ("Testing is meh.") { LastWriteTime = new DateTime (2001, 01, 01, 11, 12, 11) }
  497. );
  498. fileSystem.AddFile (
  499. @"/demo/jQuery.js",
  500. new ("some js") { LastWriteTime = new DateTime (2001, 01, 01, 11, 44, 42) }
  501. );
  502. fileSystem.AddFile (
  503. @"/demo/image.gif",
  504. new (new byte [] { 0x12, 0x34, 0x56, 0xd2 })
  505. {
  506. LastWriteTime = new DateTime (2002, 01, 01, 22, 42, 10)
  507. }
  508. );
  509. var m = (MockDirectoryInfo)fileSystem.DirectoryInfo.New (@"/demo/subfolder");
  510. m.Create ();
  511. m.LastWriteTime = new (2002, 01, 01, 22, 42, 10);
  512. fileSystem.AddFile (
  513. @"/demo/subfolder/image2.gif",
  514. new (new byte [] { 0x12, 0x34, 0x56, 0xd2 })
  515. {
  516. LastWriteTime = new DateTime (2002, 01, 01, 22, 42, 10)
  517. }
  518. );
  519. var fd = new FileDialog (fileSystem) { Height = 15, Width = 75 };
  520. fd.Path = @"/demo/";
  521. Begin (fd);
  522. return fd;
  523. }
  524. private FileDialog GetWindowsDialog ()
  525. {
  526. // Override CM
  527. Window.DefaultBorderStyle = LineStyle.Single;
  528. Dialog.DefaultButtonAlignment = Alignment.Center;
  529. Dialog.DefaultBorderStyle = LineStyle.Single;
  530. // Arrange
  531. var fileSystem = new MockFileSystem (new Dictionary<string, MockFileData> (), @"c:\");
  532. fileSystem.MockTime (() => new (2010, 01, 01, 11, 12, 43));
  533. fileSystem.AddFile (
  534. @"c:\myfile.txt",
  535. new ("Testing is meh.") { LastWriteTime = new DateTime (2001, 01, 01, 11, 12, 11) }
  536. );
  537. fileSystem.AddFile (
  538. @"c:\demo\jQuery.js",
  539. new ("some js") { LastWriteTime = new DateTime (2001, 01, 01, 11, 44, 42) }
  540. );
  541. fileSystem.AddFile (
  542. @"c:\demo\mybinary.exe",
  543. new ("some js") { LastWriteTime = new DateTime (2001, 01, 01, 11, 44, 42) }
  544. );
  545. fileSystem.AddFile (
  546. @"c:\demo\image.gif",
  547. new (new byte [] { 0x12, 0x34, 0x56, 0xd2 })
  548. {
  549. LastWriteTime = new DateTime (2002, 01, 01, 22, 42, 10)
  550. }
  551. );
  552. var m = (MockDirectoryInfo)fileSystem.DirectoryInfo.New (@"c:\demo\subfolder");
  553. m.Create ();
  554. m.LastWriteTime = new (2002, 01, 01, 22, 42, 10);
  555. fileSystem.AddFile (
  556. @"c:\demo\subfolder\image2.gif",
  557. new (new byte [] { 0x12, 0x34, 0x56, 0xd2 })
  558. {
  559. LastWriteTime = new DateTime (2002, 01, 01, 22, 42, 10)
  560. }
  561. );
  562. var fd = new FileDialog (fileSystem) { Height = 15, Width = 75 };
  563. fd.Path = @"c:\demo\";
  564. Begin (fd);
  565. return fd;
  566. }
  567. /*
  568. [Fact, AutoInitShutdown]
  569. public void Autocomplete_NoSuggestion_WhenTextMatchesExactly ()
  570. {
  571. var tb = new TextFieldWithAppendAutocomplete ();
  572. ForceFocus (tb);
  573. tb.Text = "/bob/fish";
  574. tb.CursorPosition = tb.Text.Length;
  575. tb.GenerateSuggestions (null, "fish", "fishes");
  576. // should not report success for autocompletion because we already have that exact
  577. // string
  578. Assert.False (tb.AcceptSelectionIfAny ());
  579. }
  580. [Fact, AutoInitShutdown]
  581. public void Autocomplete_AcceptSuggstion ()
  582. {
  583. var tb = new TextFieldWithAppendAutocomplete ();
  584. ForceFocus (tb);
  585. tb.Text = @"/bob/fi";
  586. tb.CursorPosition = tb.Text.Length;
  587. tb.GenerateSuggestions (null, "fish", "fishes");
  588. Assert.True (tb.AcceptSelectionIfAny ());
  589. Assert.Equal (@"/bob/fish", tb.Text);
  590. }*/
  591. private bool IsWindows () { return RuntimeInformation.IsOSPlatform (OSPlatform.Windows); }
  592. private void Send (char ch, ConsoleKey ck, bool shift = false, bool alt = false, bool control = false)
  593. {
  594. Application.Driver?.SendKeys (ch, ck, shift, alt, control);
  595. }
  596. private void Send (string chars)
  597. {
  598. foreach (char ch in chars)
  599. {
  600. Application.Driver?.SendKeys (ch, ConsoleKey.NoName, false, false, false);
  601. }
  602. }
  603. private void SendSlash ()
  604. {
  605. if (Path.DirectorySeparatorChar == '/')
  606. {
  607. Send ('/', ConsoleKey.Separator);
  608. }
  609. else
  610. {
  611. Send ('\\', ConsoleKey.Separator);
  612. }
  613. }
  614. }