FileDialogTests.cs 30 KB

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