2
0

FileDialogTests.cs 30 KB

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