FileDialogTests.cs 25 KB

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