123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815 |
- using System.IO.Abstractions.TestingHelpers;
- using System.Runtime.InteropServices;
- using UnitTests;
- using Xunit.Abstractions;
- namespace Terminal.Gui.FileServicesTests;
- public class FileDialogTests ()
- {
- [Theory]
- [InlineData (true)]
- [InlineData (false)]
- [AutoInitShutdown]
- public void CancelSelection (bool cancel)
- {
- FileDialog dlg = GetInitializedFileDialog ();
- string openIn = Path.Combine (Environment.CurrentDirectory, "zz");
- Directory.CreateDirectory (openIn);
- dlg.Path = openIn + Path.DirectorySeparatorChar;
- dlg.FilesSelected += (s, e) => e.Cancel = cancel;
- //pressing enter will complete the current selection
- // unless the event cancels the confirm
- Send ('\n', ConsoleKey.Enter);
- Assert.Equal (cancel, dlg.Canceled);
- dlg.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void DirectTyping_Allowed ()
- {
- FileDialog dlg = GetInitializedFileDialog ();
- TextField tf = dlg.SubViews.OfType<TextField> ().First (t => t.HasFocus);
- tf.ClearAllSelection ();
- tf.CursorPosition = tf.Text.Length;
- Assert.True (tf.HasFocus);
- SendSlash ();
- Assert.Equal (
- new DirectoryInfo (Environment.CurrentDirectory + Path.DirectorySeparatorChar).FullName,
- new DirectoryInfo (dlg.Path + Path.DirectorySeparatorChar).FullName
- );
- // continue typing the rest of the path
- Send ("BOB");
- Send ('.', ConsoleKey.OemPeriod);
- Send ("CSV");
- Assert.True (dlg.Canceled);
- Send ('\n', ConsoleKey.Enter);
- Assert.False (dlg.Canceled);
- Assert.Equal ("bob.csv", Path.GetFileName (dlg.Path));
- dlg.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void DirectTyping_AutoComplete ()
- {
- FileDialog dlg = GetInitializedFileDialog ();
- string openIn = Path.Combine (Environment.CurrentDirectory, "zz");
- Directory.CreateDirectory (openIn);
- string expectedDest = Path.Combine (openIn, "xx");
- Directory.CreateDirectory (expectedDest);
- dlg.Path = openIn + Path.DirectorySeparatorChar;
- Send ("X");
- // nothing selected yet
- Assert.True (dlg.Canceled);
- Assert.Equal ("x", Path.GetFileName (dlg.Path));
- // complete auto typing
- Send ('\t', ConsoleKey.Tab);
- // but do not close dialog
- Assert.True (dlg.Canceled);
- Assert.EndsWith ("xx" + Path.DirectorySeparatorChar, dlg.Path);
- // press enter again to confirm the dialog
- Send ('\n', ConsoleKey.Enter);
- Assert.False (dlg.Canceled);
- Assert.EndsWith ("xx" + Path.DirectorySeparatorChar, dlg.Path);
- dlg.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void DoNotConfirmSelectionWhenFindFocused ()
- {
- FileDialog dlg = GetInitializedFileDialog ();
- string openIn = Path.Combine (Environment.CurrentDirectory, "zz");
- Directory.CreateDirectory (openIn);
- dlg.Path = openIn + Path.DirectorySeparatorChar;
- var tf = GetTextField (dlg, FileDialogPart.SearchField);
- tf.SetFocus ();
- Assert.IsType<TextField> (dlg.MostFocused);
- Assert.Same (tf, dlg.MostFocused);
- Assert.Equal ("Enter Search", tf.Caption);
- // Dialog has not yet been confirmed with a choice
- Assert.True (dlg.Canceled);
- //pressing enter while search focused should not confirm path
- Send ('\n', ConsoleKey.Enter);
- Assert.True (dlg.Canceled);
- // tabbing out of search
- Send ('\t', ConsoleKey.Tab);
- //should allow enter to confirm path
- Send ('\n', ConsoleKey.Enter);
- // Dialog has not yet been confirmed with a choice
- Assert.False (dlg.Canceled);
- dlg.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void DotDot_MovesToRoot_ThenPressBack ()
- {
- FileDialog dlg = GetDialog ();
- dlg.OpenMode = OpenMode.Directory;
- dlg.AllowsMultipleSelection = true;
- var selected = false;
- dlg.FilesSelected += (s, e) => { selected = true; };
- AssertIsTheStartingDirectory (dlg.Path);
- Assert.IsType<TextField> (dlg.MostFocused);
- Application.RaiseKeyDownEvent (Key.CursorDown);
- var tv = GetTableView(dlg);
- tv.SetFocus ();
- Assert.IsType<TableView> (dlg.MostFocused);
- // ".." should be the first thing selected
- // ".." should not mess with the displayed path
- AssertIsTheStartingDirectory (dlg.Path);
- // Accept navigation up a directory
- Application.RaiseKeyDownEvent (Key.Enter);
- AssertIsTheRootDirectory (dlg.Path);
- Assert.True (dlg.Canceled);
- Assert.False (selected);
- Assert.IsType<TableView> (dlg.MostFocused);
- // Now press Backspace (in table view)
- Application.RaiseKeyDownEvent (Key.Backspace);
- // Should move us back to the root
- AssertIsTheStartingDirectory (dlg.Path);
- Assert.True (dlg.Canceled);
- Assert.False (selected);
- dlg.Dispose ();
- }
- [Theory]
- [AutoInitShutdown]
- [InlineData (true)]
- [InlineData (false)]
- public void MultiSelectDirectory_CannotToggleDotDot (bool acceptWithEnter)
- {
- FileDialog dlg = GetDialog ();
- dlg.OpenMode = OpenMode.Directory;
- dlg.AllowsMultipleSelection = true;
- IReadOnlyCollection<string> eventMultiSelected = null;
- dlg.FilesSelected += (s, e) => { eventMultiSelected = e.Dialog.MultiSelected; };
- var tv = GetTableView (dlg);
- tv.SetFocus ();
- Assert.IsType<TableView> (dlg.MostFocused);
- // Try to toggle '..'
- Send (' ', ConsoleKey.Spacebar);
- Send ('v', ConsoleKey.DownArrow);
- // Toggle subfolder
- Send (' ', ConsoleKey.Spacebar);
- Assert.True (dlg.Canceled);
- if (acceptWithEnter)
- {
- Send ('\n', ConsoleKey.Enter);
- }
- else
- {
- Send ('O', ConsoleKey.O, false, true);
- }
- Assert.False (dlg.Canceled);
- Assert.Multiple (
- () =>
- {
- // Only the subfolder should be selected
- Assert.Single (dlg.MultiSelected);
- AssertIsTheSubfolder (dlg.Path);
- AssertIsTheSubfolder (dlg.MultiSelected.Single ());
- },
- () =>
- {
- // Event should also agree with the final state
- Assert.NotNull (eventMultiSelected);
- Assert.Single (eventMultiSelected);
- AssertIsTheSubfolder (eventMultiSelected.Single ());
- }
- );
- dlg.Dispose ();
- }
- [Theory]
- [AutoInitShutdown]
- [InlineData (true)]
- [InlineData (false)]
- public void MultiSelectDirectory_CanToggleThenAccept (bool acceptWithEnter)
- {
- FileDialog dlg = GetDialog ();
- dlg.OpenMode = OpenMode.Directory;
- dlg.AllowsMultipleSelection = true;
- IReadOnlyCollection<string> eventMultiSelected = null;
- dlg.FilesSelected += (s, e) => { eventMultiSelected = e.Dialog.MultiSelected; };
- var tv = GetTableView (dlg);
- tv.SetFocus ();
- Assert.IsType<TableView> (dlg.MostFocused);
- // Move selection to subfolder
- Send ('v', ConsoleKey.DownArrow);
- // Toggle subfolder
- Send (' ', ConsoleKey.Spacebar);
- Assert.True (dlg.Canceled);
- if (acceptWithEnter)
- {
- Send ('\n', ConsoleKey.Enter);
- }
- else
- {
- Send ('O', ConsoleKey.O, false, true);
- }
- Assert.False (dlg.Canceled);
- Assert.Multiple (
- () =>
- {
- // Only the subfolder should be selected
- Assert.Single (dlg.MultiSelected);
- AssertIsTheSubfolder (dlg.Path);
- AssertIsTheSubfolder (dlg.MultiSelected.Single ());
- },
- () =>
- {
- // Event should also agree with the final state
- Assert.NotNull (eventMultiSelected);
- Assert.Single (eventMultiSelected);
- AssertIsTheSubfolder (eventMultiSelected.Single ());
- }
- );
- dlg.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void MultiSelectDirectory_EnterOpensFolder ()
- {
- FileDialog dlg = GetDialog ();
- dlg.OpenMode = OpenMode.Directory;
- dlg.AllowsMultipleSelection = true;
- IReadOnlyCollection<string> eventMultiSelected = null;
- dlg.FilesSelected += (s, e) => { eventMultiSelected = e.Dialog.MultiSelected; };
- var tv = GetTableView (dlg);
- tv.SetFocus ();
- Assert.IsType<TableView> (dlg.MostFocused);
- // Move selection to subfolder
- Send ('v', ConsoleKey.DownArrow);
- Send ('\n', ConsoleKey.Enter);
- // Path should update to the newly opened folder
- AssertIsTheSubfolder (dlg.Path);
- // No selection will have been confirmed
- Assert.True (dlg.Canceled);
- Assert.Empty (dlg.MultiSelected);
- Assert.Null (eventMultiSelected);
- dlg.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void OnLoad_TextBoxIsFocused ()
- {
- FileDialog dlg = GetInitializedFileDialog ();
- View tf = dlg.SubViews.FirstOrDefault (t => t.HasFocus);
- Assert.NotNull (tf);
- Assert.IsType<TextField> (tf);
- dlg.Dispose ();
- }
- [Theory]
- [AutoInitShutdown]
- [InlineData (true, true)]
- [InlineData (true, false)]
- [InlineData (false, true)]
- [InlineData (false, false)]
- public void PickDirectory_ArrowNavigation (bool openModeMixed, bool multiple)
- {
- FileDialog dlg = GetDialog ();
- dlg.OpenMode = openModeMixed ? OpenMode.Mixed : OpenMode.Directory;
- dlg.AllowsMultipleSelection = multiple;
- var tv = GetTableView (dlg);
- tv.SetFocus ();
- Assert.IsType<TableView> (dlg.MostFocused);
- // Should be selecting ..
- Send ('v', ConsoleKey.DownArrow);
- // Down to the directory
- Assert.True (dlg.Canceled);
- // Alt+O to open (enter would just navigate into the child dir)
- Send ('O', ConsoleKey.O, false, true);
- Assert.False (dlg.Canceled);
- AssertIsTheSubfolder (dlg.Path);
- dlg.Dispose ();
- }
- [Theory]
- [AutoInitShutdown]
- [InlineData (true, true)]
- [InlineData (true, false)]
- [InlineData (false, true)]
- [InlineData (false, false)]
- public void PickDirectory_DirectTyping (bool openModeMixed, bool multiple)
- {
- FileDialog dlg = GetDialog ();
- dlg.OpenMode = openModeMixed ? OpenMode.Mixed : OpenMode.Directory;
- dlg.AllowsMultipleSelection = multiple;
- // whe first opening the text field will have select all on
- // so to add to current path user must press End or right
- Send ('>', ConsoleKey.LeftArrow);
- Send ('>', ConsoleKey.RightArrow);
- Send ("SUBFOLDER");
- // Dialog has not yet been confirmed with a choice
- Assert.True (dlg.Canceled);
- // Now it has
- Send ('\n', ConsoleKey.Enter);
- Assert.False (dlg.Canceled);
- AssertIsTheSubfolder (dlg.Path);
- dlg.Dispose ();
- }
- [Theory]
- [InlineData (".csv", null, false)]
- [InlineData (".csv", "", false)]
- [InlineData (".csv", "c:\\MyFile.csv", true)]
- [InlineData (".csv", "c:\\MyFile.CSV", true)]
- [InlineData (".csv", "c:\\MyFile.csv.bak", false)]
- public void TestAllowedType_Basic (string allowed, string candidate, bool expected)
- {
- Assert.Equal (expected, new AllowedType ("Test", allowed).IsAllowed (candidate));
- }
- [Theory]
- [InlineData (".Designer.cs", "c:\\MyView.Designer.cs", true)]
- [InlineData (".Designer.cs", "c:\\temp/MyView.Designer.cs", true)]
- [InlineData (".Designer.cs", "MyView.Designer.cs", true)]
- [InlineData (".Designer.cs", "c:\\MyView.DESIGNER.CS", true)]
- [InlineData (".Designer.cs", "MyView.cs", false)]
- public void TestAllowedType_DoubleBarreled (string allowed, string candidate, bool expected)
- {
- Assert.Equal (expected, new AllowedType ("Test", allowed).IsAllowed (candidate));
- }
- [Theory]
- [InlineData ("Dockerfile", "c:\\temp\\Dockerfile", true)]
- [InlineData ("Dockerfile", "Dockerfile", true)]
- [InlineData ("Dockerfile", "someimg.Dockerfile", true)]
- public void TestAllowedType_SpecificFile (string allowed, string candidate, bool expected)
- {
- Assert.Equal (expected, new AllowedType ("Test", allowed).IsAllowed (candidate));
- }
- [Fact]
- [AutoInitShutdown]
- public void TestDirectoryContents_Linux ()
- {
- if (IsWindows ())
- {
- return;
- }
- FileDialog fd = GetLinuxDialog ();
- fd.Title = string.Empty;
- fd.Style.Culture = new ("en-US");
- fd.Draw ();
- /*
- *
- *
- ┌─────────────────────────────────────────────────────────────────────────┐
- │/demo/ │
- │⟦▲⟧ │
- │┌────────────┬──────────┬──────────────────────────────┬────────────────┐│
- ││Filename (▲)│Size │Modified │Type ││
- │├────────────┼──────────┼──────────────────────────────┼────────────────┤│
- ││.. │ │ │<Directory> ││
- ││/subfolder │ │2002-01-01T22:42:10 │<Directory> ││
- ││image.gif │4.00 B │2002-01-01T22:42:10 │.gif ││
- ││jQuery.js │7.00 B │2001-01-01T11:44:42 │.js ││
- │ │
- │ │
- │ │
- │⟦ ►► ⟧ Enter Search ⟦► OK ◄⟧ ⟦ Cancel ⟧ │
- └─────────────────────────────────────────────────────────────────────────┘
- *
- */
- var path = GetTextField (fd, FileDialogPart.Path);
- Assert.Equal ("/demo/", path.Text);
- var tv = GetTableView (fd);
- // Asserting the headers
- Assert.Equal ("Filename (▲)", tv.Table.ColumnNames.ElementAt (0));
- Assert.Equal ("Size", tv.Table.ColumnNames.ElementAt (1));
- Assert.Equal ("Modified", tv.Table.ColumnNames.ElementAt (2));
- Assert.Equal ("Type", tv.Table.ColumnNames.ElementAt (3));
- // Asserting the table contents
- Assert.Equal ("..", tv.Style.GetOrCreateColumnStyle (0).GetRepresentation (tv.Table [0, 0]));
- Assert.Equal ("/subfolder", tv.Style.GetOrCreateColumnStyle (0).GetRepresentation (tv.Table [1, 0]));
- Assert.Equal ("image.gif", tv.Style.GetOrCreateColumnStyle (0).GetRepresentation (tv.Table [2, 0]));
- Assert.Equal ("jQuery.js", tv.Style.GetOrCreateColumnStyle (0).GetRepresentation (tv.Table [3, 0]));
- Assert.Equal ("", tv.Style.GetOrCreateColumnStyle (1).GetRepresentation (tv.Table [0, 1]));
- Assert.Equal ("", tv.Style.GetOrCreateColumnStyle (1).GetRepresentation (tv.Table [1, 1]));
- Assert.Equal ("4.00 B", tv.Style.GetOrCreateColumnStyle (1).GetRepresentation (tv.Table [2, 1]));
- Assert.Equal ("7.00 B", tv.Style.GetOrCreateColumnStyle (1).GetRepresentation (tv.Table [3, 1]));
- Assert.Equal ("", tv.Style.GetOrCreateColumnStyle (2).GetRepresentation (tv.Table [0, 2]));
- Assert.Equal ("2002-01-01T22:42:10", tv.Style.GetOrCreateColumnStyle (2).GetRepresentation (tv.Table [1, 2]));
- Assert.Equal ("2002-01-01T22:42:10", tv.Style.GetOrCreateColumnStyle (2).GetRepresentation (tv.Table [2, 2]));
- Assert.Equal ("2001-01-01T11:44:42", tv.Style.GetOrCreateColumnStyle (2).GetRepresentation (tv.Table [3, 2]));
- Assert.Equal ("<Directory>", tv.Style.GetOrCreateColumnStyle (3).GetRepresentation (tv.Table [0, 3]));
- Assert.Equal ("<Directory>", tv.Style.GetOrCreateColumnStyle (3).GetRepresentation (tv.Table [1, 3]));
- Assert.Equal (".gif", tv.Style.GetOrCreateColumnStyle (3).GetRepresentation (tv.Table [2, 3]));
- Assert.Equal (".js", tv.Style.GetOrCreateColumnStyle (3).GetRepresentation (tv.Table [3, 3]));
- fd.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void TestDirectoryContents_Windows ()
- {
- if (!IsWindows ())
- {
- return;
- }
- FileDialog fd = GetWindowsDialog ();
- fd.Title = string.Empty;
- fd.Style.Culture = new ("en-US");
- fd.Draw ();
- /*
- *
- *
- ┌─────────────────────────────────────────────────────────────────────────┐
- │c:\demo\ │
- │⟦▲⟧ │
- │┌────────────┬──────────┬──────────────────────────────┬────────────────┐│
- ││Filename (▲)│Size │Modified │Type ││
- │├────────────┼──────────┼──────────────────────────────┼────────────────┤│
- ││.. │ │ │<Directory> ││
- ││\subfolder │ │2002-01-01T22:42:10 │<Directory> ││
- ││image.gif │4.00 B │2002-01-01T22:42:10 │.gif ││
- ││jQuery.js │7.00 B │2001-01-01T11:44:42 │.js ││
- ││mybinary.exe│7.00 B │2001-01-01T11:44:42 │.exe ││
- │ │
- │ │
- │⟦ ►► ⟧ Enter Search ⟦► OK ◄⟧ ⟦ Cancel ⟧ │
- └─────────────────────────────────────────────────────────────────────────┘
-
- *
- */
- var path = GetTextField (fd, FileDialogPart.Path);
- Assert.Equal ("c:\\demo\\",path.Text);
- var tv = GetTableView (fd);
- // Asserting the headers
- Assert.Equal ("Filename (▲)", tv.Table.ColumnNames.ElementAt (0));
- Assert.Equal ("Size", tv.Table.ColumnNames.ElementAt (1));
- Assert.Equal ("Modified", tv.Table.ColumnNames.ElementAt (2));
- Assert.Equal ("Type", tv.Table.ColumnNames.ElementAt (3));
- // Asserting the table contents
- Assert.Equal ("..", tv.Style.GetOrCreateColumnStyle (0).GetRepresentation (tv.Table [0, 0]));
- Assert.Equal (@"\subfolder", tv.Style.GetOrCreateColumnStyle (0).GetRepresentation (tv.Table [1, 0]));
- Assert.Equal ("image.gif", tv.Style.GetOrCreateColumnStyle (0).GetRepresentation (tv.Table [2, 0]));
- Assert.Equal ("jQuery.js", tv.Style.GetOrCreateColumnStyle (0).GetRepresentation (tv.Table [3, 0]));
- Assert.Equal ("mybinary.exe", tv.Style.GetOrCreateColumnStyle (0).GetRepresentation (tv.Table [4, 0]));
- Assert.Equal ("", tv.Style.GetOrCreateColumnStyle (1).GetRepresentation (tv.Table [0, 1]));
- Assert.Equal ("", tv.Style.GetOrCreateColumnStyle (1).GetRepresentation (tv.Table [1, 1]));
- Assert.Equal ("4.00 B", tv.Style.GetOrCreateColumnStyle (1).GetRepresentation (tv.Table [2, 1]));
- Assert.Equal ("7.00 B", tv.Style.GetOrCreateColumnStyle (1).GetRepresentation (tv.Table [3, 1]));
- Assert.Equal ("7.00 B", tv.Style.GetOrCreateColumnStyle (1).GetRepresentation (tv.Table [4, 1]));
- Assert.Equal ("", tv.Style.GetOrCreateColumnStyle (2).GetRepresentation (tv.Table [0, 2]));
- Assert.Equal ("2002-01-01T22:42:10", tv.Style.GetOrCreateColumnStyle (2).GetRepresentation (tv.Table [1, 2]));
- Assert.Equal ("2002-01-01T22:42:10", tv.Style.GetOrCreateColumnStyle (2).GetRepresentation (tv.Table [2, 2]));
- Assert.Equal ("2001-01-01T11:44:42", tv.Style.GetOrCreateColumnStyle (2).GetRepresentation (tv.Table [3, 2]));
- Assert.Equal ("2001-01-01T11:44:42", tv.Style.GetOrCreateColumnStyle (2).GetRepresentation (tv.Table [4, 2]));
- Assert.Equal ("<Directory>", tv.Style.GetOrCreateColumnStyle (3).GetRepresentation (tv.Table [0, 3]));
- Assert.Equal ("<Directory>", tv.Style.GetOrCreateColumnStyle (3).GetRepresentation (tv.Table [1, 3]));
- Assert.Equal (".gif", tv.Style.GetOrCreateColumnStyle (3).GetRepresentation (tv.Table [2, 3]));
- Assert.Equal (".js", tv.Style.GetOrCreateColumnStyle (3).GetRepresentation (tv.Table [3, 3]));
- Assert.Equal (".exe", tv.Style.GetOrCreateColumnStyle (3).GetRepresentation (tv.Table [4, 3]));
- fd.Dispose ();
- }
- private void AssertIsTheRootDirectory (string path)
- {
- if (IsWindows ())
- {
- Assert.Equal (@"c:\", path);
- }
- else
- {
- Assert.Equal ("/", path);
- }
- }
- private void AssertIsTheStartingDirectory (string path)
- {
- if (IsWindows ())
- {
- Assert.Equal (@"c:\demo\", path);
- }
- else
- {
- Assert.Equal ("/demo/", path);
- }
- }
- private void AssertIsTheSubfolder (string path)
- {
- if (IsWindows ())
- {
- Assert.Equal (@"c:\demo\subfolder", path);
- }
- else
- {
- Assert.Equal ("/demo/subfolder", path);
- }
- }
- private void Begin (FileDialog dlg)
- {
- dlg.BeginInit ();
- dlg.EndInit ();
- Application.Begin (dlg);
- }
- private FileDialog GetDialog () { return IsWindows () ? GetWindowsDialog () : GetLinuxDialog (); }
- private FileDialog GetInitializedFileDialog ()
- {
- Window.DefaultBorderStyle = LineStyle.Single;
- Dialog.DefaultButtonAlignment = Alignment.Center;
- Dialog.DefaultBorderStyle = LineStyle.Single;
- Dialog.DefaultShadow = ShadowStyle.None;
- Button.DefaultShadow = ShadowStyle.None;
- var dlg = new FileDialog ();
- Begin (dlg);
- return dlg;
- }
- private FileDialog GetLinuxDialog ()
- {
- Window.DefaultBorderStyle = LineStyle.Single;
- Dialog.DefaultButtonAlignment = Alignment.Center;
- Dialog.DefaultBorderStyle = LineStyle.Single;
- Dialog.DefaultShadow = ShadowStyle.None;
- Button.DefaultShadow = ShadowStyle.None;
- // Arrange
- var fileSystem = new MockFileSystem (new Dictionary<string, MockFileData> (), "/");
- fileSystem.MockTime (() => new (2010, 01, 01, 11, 12, 43));
- fileSystem.AddFile (
- @"/myfile.txt",
- new ("Testing is meh.") { LastWriteTime = new DateTime (2001, 01, 01, 11, 12, 11) }
- );
- fileSystem.AddFile (
- @"/demo/jQuery.js",
- new ("some js") { LastWriteTime = new DateTime (2001, 01, 01, 11, 44, 42) }
- );
- fileSystem.AddFile (
- @"/demo/image.gif",
- new (new byte [] { 0x12, 0x34, 0x56, 0xd2 })
- {
- LastWriteTime = new DateTime (2002, 01, 01, 22, 42, 10)
- }
- );
- var m = (MockDirectoryInfo)fileSystem.DirectoryInfo.New (@"/demo/subfolder");
- m.Create ();
- m.LastWriteTime = new (2002, 01, 01, 22, 42, 10);
- fileSystem.AddFile (
- @"/demo/subfolder/image2.gif",
- new (new byte [] { 0x12, 0x34, 0x56, 0xd2 })
- {
- LastWriteTime = new DateTime (2002, 01, 01, 22, 42, 10)
- }
- );
- var fd = new FileDialog (fileSystem) { Height = 15, Width = 75 };
- fd.Path = @"/demo/";
- Begin (fd);
- return fd;
- }
- private FileDialog GetWindowsDialog ()
- {
- // Override CM
- Window.DefaultBorderStyle = LineStyle.Single;
- Dialog.DefaultButtonAlignment = Alignment.Center;
- Dialog.DefaultBorderStyle = LineStyle.Single;
- Dialog.DefaultShadow = ShadowStyle.None;
- Button.DefaultShadow = ShadowStyle.None;
- // Arrange
- var fileSystem = new MockFileSystem (new Dictionary<string, MockFileData> (), @"c:\");
- fileSystem.MockTime (() => new (2010, 01, 01, 11, 12, 43));
- fileSystem.AddFile (
- @"c:\myfile.txt",
- new ("Testing is meh.") { LastWriteTime = new DateTime (2001, 01, 01, 11, 12, 11) }
- );
- fileSystem.AddFile (
- @"c:\demo\jQuery.js",
- new ("some js") { LastWriteTime = new DateTime (2001, 01, 01, 11, 44, 42) }
- );
- fileSystem.AddFile (
- @"c:\demo\mybinary.exe",
- new ("some js") { LastWriteTime = new DateTime (2001, 01, 01, 11, 44, 42) }
- );
- fileSystem.AddFile (
- @"c:\demo\image.gif",
- new (new byte [] { 0x12, 0x34, 0x56, 0xd2 })
- {
- LastWriteTime = new DateTime (2002, 01, 01, 22, 42, 10)
- }
- );
- var m = (MockDirectoryInfo)fileSystem.DirectoryInfo.New (@"c:\demo\subfolder");
- m.Create ();
- m.LastWriteTime = new (2002, 01, 01, 22, 42, 10);
- fileSystem.AddFile (
- @"c:\demo\subfolder\image2.gif",
- new (new byte [] { 0x12, 0x34, 0x56, 0xd2 })
- {
- LastWriteTime = new DateTime (2002, 01, 01, 22, 42, 10)
- }
- );
- var fd = new FileDialog (fileSystem) { Height = 15, Width = 75 };
- fd.Path = @"c:\demo\";
- Begin (fd);
- return fd;
- }
- /*
- [Fact, AutoInitShutdown]
- public void Autocomplete_NoSuggestion_WhenTextMatchesExactly ()
- {
- var tb = new TextFieldWithAppendAutocomplete ();
- ForceFocus (tb);
- tb.Text = "/bob/fish";
- tb.CursorPosition = tb.Text.Length;
- tb.GenerateSuggestions (null, "fish", "fishes");
- // should not report success for autocompletion because we already have that exact
- // string
- Assert.False (tb.AcceptSelectionIfAny ());
- }
- [Fact, AutoInitShutdown]
- public void Autocomplete_AcceptSuggstion ()
- {
- var tb = new TextFieldWithAppendAutocomplete ();
- ForceFocus (tb);
- tb.Text = @"/bob/fi";
- tb.CursorPosition = tb.Text.Length;
- tb.GenerateSuggestions (null, "fish", "fishes");
- Assert.True (tb.AcceptSelectionIfAny ());
- Assert.Equal (@"/bob/fish", tb.Text);
- }*/
- private bool IsWindows () { return RuntimeInformation.IsOSPlatform (OSPlatform.Windows); }
- private void Send (char ch, ConsoleKey ck, bool shift = false, bool alt = false, bool control = false)
- {
- Application.Driver?.SendKeys (ch, ck, shift, alt, control);
- }
- private void Send (string chars)
- {
- foreach (char ch in chars)
- {
- Application.Driver?.SendKeys (ch, ConsoleKey.NoName, false, false, false);
- }
- }
- private void SendSlash ()
- {
- if (Path.DirectorySeparatorChar == '/')
- {
- Send ('/', ConsoleKey.Separator);
- }
- else
- {
- Send ('\\', ConsoleKey.Separator);
- }
- }
- private TextField GetTextField (FileDialog dlg, FileDialogPart part)
- {
- switch (part)
- {
- case FileDialogPart.Path:
- return dlg.SubViews.OfType<TextField> ().ElementAt (0);
- case FileDialogPart.SearchField:
- return dlg.SubViews.OfType<TextField> ().ElementAt (1);
- default:
- throw new ArgumentOutOfRangeException (nameof (part), part, null);
- }
- }
- private TableView GetTableView (FileDialog dlg)
- {
- var tile = dlg.SubViews.OfType<TileView> ().Single ();
- return (TableView)tile.Tiles.ElementAt (1).ContentView.SubViews.ElementAt(0);
- }
- private enum FileDialogPart
- {
- Path,
- SearchField,
- }
- }
|