Files
Terminal.Gui/UnitTests/FileServices/FileDialogTests.cs
Tig dcb3b359ad Fixes #2926 - Refactor KeyEvent and KeyEventEventArgs to simplify (#2927)
* Adds basic MainLoop unit tests

* Remove WinChange action from Curses

* Remove WinChange action from Curses

* Remove ProcessInput action from Windows MainLoop

* Simplified MainLoop/ConsoleDriver by making MainLoop internal and moving impt fns to Application

* Modernized Terminal resize events

* Modernized Terminal resize events

* Removed un used property

* for _isWindowsTerminal devenv->wininit; not sure what changed

* Modernized mouse/keyboard events (Action->EventHandler)

* Updated OnMouseEvent API docs

* Using WT_SESSION to detect WT

* removes hacky GetParentProcess

* Updates to fix #2634 (clear last line)

* removes hacky GetParentProcess2

* Addressed mac resize issue

* Addressed mac resize issue

* Removes ConsoleDriver.PrepareToRun, has Init return MainLoop

* Removes unneeded Attribute methods

* Removed GetProcesssName

* Removed GetProcesssName

* Refactored KeyEvent and KeyEventEventArgs into a single class

* Revert "Refactored KeyEvent and KeyEventEventArgs into a single class"

This reverts commit 88a00658db.

* Fixed key repeat issue; reverted stupidity on 1049/1047 confusion

* Updated CSI API Docs

* merge

* Rearranged Event.cs to Keyboard.cs and Mouse.cs

* Renamed KeyEventEventArgs KeyEventArgs

* temp renamed KeyEvent OldKeyEvent

* Merged KeyEvent into KeyEventArgs

* Renamed Application.ProcessKey members

* Renamed Application.ProcessKey members

* Renamed Application.ProcessKey members

* Added Responder.KeyPressed

* Removed unused references

* Fixed arg naming

* InvokeKeybindings->InvokeKeyBindings

* InvokeKeybindings->InvokeKeyBindings

* Fixed unit tests fail

* More progress on refactoring key input; still broken and probably wrong

* Moved OnKeyPressed out of Responder and made ProcessKeyPrssed non-virtual

* Updated API docs

* Moved key handling from Responder to View

* Updated API docs

* Updated HotKey API docs

* Updated shortcut API docs

* Fixed responder unit tests

* Removed Shortcut from View as it is not used

* Removed unneeded OnHotKey override from Button

* Fixed BackTab logic

* Button now uses Key Bindings exclusively

* Button now uses Key Bindings exclusively

* Updated keyboard.md docs

* Fixed unit tests to account for Toplevel handling default button

* Added View.InvokeCommand API

* Modernized RadioGroup

* Removed ColdKey

* Modernized (partially) StatusBar

* Worked around FileDialog issue with Ctrl-F

* Fixed driver unit test; view must be focused to reciev key pressed

* Application code cleanup

* Start on updaing menu

* Menu now mostly works

* Menu Select refinement

* Fixed known menu bugs!

* Enabled HotKey to cause focus- experimental

* Fixes #3022 & adds unit test to prove it

* Actually Fixes #3022 & adds unit test to prove it

* Working through hotkey issues

* Misc fixes

* removed hot/cold key stuff from Keys scenario

* Fixed scenarios

* Simplified shortcut string handling

* Modernized Checkbox

* Modernized TileView

* Updated API docs

* Updated API docs

* attempting to publish v2 docs

* Revert "attempting to publish v2 docs"

This reverts commit 59dcec111b.

* Playing with api docs

* Removed Key.BackTab

* Removed Caps/Scroll/Numlock

* Partial removal of keymodifiers - unit tests pass

* Partial removal of keymodifiers - broke netdriver somewhere

* WindowsDriver & added KeyEventArgsTests

* Fixing menu shortcut/hotkeys - broke Menu.cs into separate files

* Fixed MenuBar!

* Finished modernizing Menu/MenuBar

* Removed Key.a-z. Broke lots of stuff

* checkout@v4

* progress on key mapping and formatting

* VK tests are still failing

* Fixed some unit tests

* Added Hotkey and Keybinding unit tests

* fixed unit test

* All unit tests pass again...

* Fixed broken unit tests

* KeyEventArgs.KeyValue -> AsRune

* Fixed bugs. Still some broken

* Added KeyEventArgs.IsAlpha. Added KeyEventArgs.cast ops. Fixed bugs. Unit tests pass

* Fixed WindowsDriver

* Oops.

* Refactoring based on bdisp's help. Not complete!

* removed calling into subviews from OnKeyBindings

* removed calling into subviews from OnKeyBindings

* Improved View KeyEvent unit tests

* More hotkey unit tests

* BIg change - Got rid of KeyPress w/in Application/Drivers

* Unit tests now pass again

* Refreshed API docs

* Better HotKey logic. More progress. Getting close.

* Fixed handling of shifted chars like ö

* Minor code cleanup

* Minor code cleanup2

* Why is build Action failing?

* Why is build Action failing??

* upgraded to .net8 to try to fix weird CI/CD build errors

* upgraded to .net8 to try to fix weird CI/CD build errors2

* Disabling TextViewTests to diagnose build errors

* reenable TextViewTests to diagnose build errors

* Arrrrrrg

* Merged v2_develop

* Fixed uppercase accented keys in WindowsDriver

* Fixed key binding api docs

* Experimental impl of CommandScope.SubViews for MenuBar

* Removed dead code from application.cs

* Removed dead code from application.cs

* Removed dead code from ConsoleDriver.cs

* Cleaned up some key binding stuff

* Disabled Alt to activate menu for now

* Updated label commands

* Fixed menu bugs. Upgraded menu unit tests

* Fixed unit tests

* Working on NetDriver

* fixed netdriver

* Fixed issues called out by @bdisp CR

* fixed CursesDriver

* added todo to netdriver

* Cherry picked treeview test fix 1b415e5

* Fix NetDriver.

* CommandScope->KeyBindingScope

* Address some tznind feedback

* Refactored KeyBindings big time!

* Added key consts to KeyEventArgs and renamed Key to ConsoleDriverKey

* Fixed some API docs

* Moved ConsoleDriverKey to ConsoleDriver.cs

* Renamed Key->ConsoleDriverKey

* Renamed Key->ConsoleDriverKey

* Renamed Key->ConsoleDriverKey

* renamed file I forgot to rename before

* Updated name and API docs of KeyEventArgs.isAlpha

* Fixed issues with OnKeyUp not doing the right thing.

* Fixed MainLoop.Running never being used

* Fixed MainLoop.Running never being used - unit tests

* Claned up BUGBUG comments

* Disabled a unit test to see why ci/cd tests are failing

* Removed defunct commented code

* Removed more defunct commented code

* Re-eanbled unit test; jsut removing one test case...

* Disabled more...

* Renambed Global->Applicaton and updated scope API docs

* Disabled more unit tests...

* Removed dead code

* Disabled more unit tests...2

* Disabled more unit tests...3

* Renambed Global->Applicaton and updated scope API docs 2

* Added more KeyBinding scope tests

* Added more KeyBinding scope tests2

* ConsoleDriverKey too long. Key too ambiguous. Settled on KeyCode. (Partialy because eventually I want to intro a class named Key).

* KeyEventArgs improvements. cast to Rune must be explicit as it's lossy

* Fixed warnings

* Renamed KeyEventArgs to Key... progress on fixing broken stuff that resulted

* Fix ConsoleKeyMapping bugs.

* Fix NetDriver issue from converting a lower case to a upper case.

* Started migration to Key from KeyCode - e.g. made HotKeys all consistent.

* Fixed build warnings

* Added key defns to Key

* KeyBindings now uses Key vs. KeyCode

* Verified by tweaking UICatalog

* Fixed treeview test ... again

* Renamed ProcessKeyDown/Up to NewKeyDown/Up and OnKeyPressed to OnProcessKeyDown to make things more clear

* Added test AllViews_KeyDown_All_EventsFire unit tests and fixed a few Views that were wrong

* fixed stupid KeyUp event bug

* If key not handled, return false for datefield

* dotnet test --no-restore --verbosity diag

* dotnet test --blame

* run tests on windows

* Fix TestVKPacket unit test and move it to ConsoleKeyMappingTests.cs file.

* Remove unnecessary commented code.

* Tweaked unit tests and removed Key.BareKey

* Fixed little details and updated api docs

* updated api docs

* AddKeyBindingsForHotKey: KeyCode->Key

* Cleaned up more old KeyCode usages. Added TODOs

---------

Co-authored-by: BDisp <bd.bdisp@gmail.com>
2023-12-16 12:04:23 -07:00

613 lines
21 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using Terminal.Gui;
using Xunit;
using Xunit.Abstractions;
namespace Terminal.Gui.FileServicesTests {
public class FileDialogTests {
readonly ITestOutputHelper output;
public FileDialogTests (ITestOutputHelper output)
{
this.output = output;
}
[Fact, AutoInitShutdown]
public void OnLoad_TextBoxIsFocused ()
{
var dlg = GetInitializedFileDialog ();
var tf = dlg.Subviews.FirstOrDefault (t => t.HasFocus);
Assert.NotNull (tf);
Assert.IsType<TextField> (tf);
}
[Fact, AutoInitShutdown]
public void DirectTyping_Allowed ()
{
var dlg = GetInitializedFileDialog ();
var 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, false);
Send ("csv");
Assert.True (dlg.Canceled);
Send ('\n', ConsoleKey.Enter, false);
Assert.False (dlg.Canceled);
Assert.Equal ("bob.csv", Path.GetFileName (dlg.Path));
}
private void SendSlash ()
{
if (Path.DirectorySeparatorChar == '/') {
Send ('/', ConsoleKey.Separator, false);
} else {
Send ('\\', ConsoleKey.Separator, false);
}
}
[Fact, AutoInitShutdown]
public void DirectTyping_AutoComplete ()
{
var dlg = GetInitializedFileDialog ();
var openIn = Path.Combine (Environment.CurrentDirectory, "zz");
Directory.CreateDirectory (openIn);
var 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, false);
// 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, false);
Assert.False (dlg.Canceled);
Assert.EndsWith ("xx" + Path.DirectorySeparatorChar, dlg.Path);
}
[Fact, AutoInitShutdown]
public void DoNotConfirmSelectionWhenFindFocused ()
{
var dlg = GetInitializedFileDialog ();
var openIn = Path.Combine (Environment.CurrentDirectory, "zz");
Directory.CreateDirectory (openIn);
dlg.Path = openIn + Path.DirectorySeparatorChar;
#if BROKE_IN_2927
Send ('f', ConsoleKey.F, false, true, false);
#else
Application.OnKeyDown (new Key (KeyCode.Tab));
Application.OnKeyDown (new Key (KeyCode.Tab));
Application.OnKeyDown (new Key (KeyCode.Tab));
#endif
Assert.IsType<TextField> (dlg.MostFocused);
var tf = (TextField)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, false);
Assert.True (dlg.Canceled);
// tabbing out of search
Send ('\t', ConsoleKey.Tab, false);
//should allow enter to confirm path
Send ('\n', ConsoleKey.Enter, false);
// Dialog has not yet been confirmed with a choice
Assert.False (dlg.Canceled);
}
[Theory, AutoInitShutdown]
[InlineData (true, true)]
[InlineData (true, false)]
[InlineData (false, true)]
[InlineData (false, false)]
public void PickDirectory_DirectTyping (bool openModeMixed, bool multiple)
{
var 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.RightArrow, false);
Send ("subfolder");
// Dialog has not yet been confirmed with a choice
Assert.True (dlg.Canceled);
// Now it has
Send ('\n', ConsoleKey.Enter, false);
Assert.False (dlg.Canceled);
AssertIsTheSubfolder (dlg.Path);
}
[Theory, AutoInitShutdown]
[InlineData (true, true)]
[InlineData (true, false)]
[InlineData (false, true)]
[InlineData (false, false)]
public void PickDirectory_ArrowNavigation (bool openModeMixed, bool multiple)
{
var dlg = GetDialog ();
dlg.OpenMode = openModeMixed ? OpenMode.Mixed : OpenMode.Directory;
dlg.AllowsMultipleSelection = multiple;
Assert.IsType<TextField> (dlg.MostFocused);
Send ('v', ConsoleKey.DownArrow, false);
Assert.IsType<TableView> (dlg.MostFocused);
// Should be selecting ..
Send ('v', ConsoleKey.DownArrow, false);
// 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);
}
[Theory, AutoInitShutdown]
[InlineData (true)]
[InlineData (false)]
public void MultiSelectDirectory_CannotToggleDotDot (bool acceptWithEnter)
{
var dlg = GetDialog ();
dlg.OpenMode = OpenMode.Directory;
dlg.AllowsMultipleSelection = true;
IReadOnlyCollection<string> eventMultiSelected = null;
dlg.FilesSelected += (s, e) => {
eventMultiSelected = e.Dialog.MultiSelected;
};
Assert.IsType<TextField> (dlg.MostFocused);
Send ('v', ConsoleKey.DownArrow, false);
Assert.IsType<TableView> (dlg.MostFocused);
// Try to toggle '..'
Send (' ', ConsoleKey.Spacebar, false);
Send ('v', ConsoleKey.DownArrow, false);
// Toggle subfolder
Send (' ', ConsoleKey.Spacebar, false);
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 ());
}
);
}
[Fact, AutoInitShutdown]
public void DotDot_MovesToRoot_ThenPressBack ()
{
var dlg = GetDialog ();
dlg.OpenMode = OpenMode.Directory;
dlg.AllowsMultipleSelection = true;
bool selected = false;
dlg.FilesSelected += (s, e) => {
selected = true;
};
AssertIsTheStartingDirectory (dlg.Path);
Assert.IsType<TextField> (dlg.MostFocused);
Send ('v', ConsoleKey.DownArrow, false);
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
Send ('\n', ConsoleKey.Enter);
AssertIsTheRootDirectory (dlg.Path);
Assert.True (dlg.Canceled);
Assert.False (selected);
// Now press the back button (in table view)
Send ('<', ConsoleKey.Backspace);
// Should move us back to the root
AssertIsTheStartingDirectory (dlg.Path);
Assert.True (dlg.Canceled);
Assert.False (selected);
}
[Fact, AutoInitShutdown]
public void MultiSelectDirectory_EnterOpensFolder ()
{
var dlg = GetDialog ();
dlg.OpenMode = OpenMode.Directory;
dlg.AllowsMultipleSelection = true;
IReadOnlyCollection<string> eventMultiSelected = null;
dlg.FilesSelected += (s, e) => {
eventMultiSelected = e.Dialog.MultiSelected;
};
Assert.IsType<TextField> (dlg.MostFocused);
Send ('v', ConsoleKey.DownArrow, false);
Assert.IsType<TableView> (dlg.MostFocused);
// Move selection to subfolder
Send ('v', ConsoleKey.DownArrow, false);
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);
}
[Theory, AutoInitShutdown]
[InlineData (true)]
[InlineData (false)]
public void MultiSelectDirectory_CanToggleThenAccept (bool acceptWithEnter)
{
var dlg = GetDialog ();
dlg.OpenMode = OpenMode.Directory;
dlg.AllowsMultipleSelection = true;
IReadOnlyCollection<string> eventMultiSelected = null;
dlg.FilesSelected += (s, e) => {
eventMultiSelected = e.Dialog.MultiSelected;
};
Assert.IsType<TextField> (dlg.MostFocused);
Send ('v', ConsoleKey.DownArrow, false);
Assert.IsType<TableView> (dlg.MostFocused);
// Move selection to subfolder
Send ('v', ConsoleKey.DownArrow, false);
// Toggle subfolder
Send (' ', ConsoleKey.Spacebar, false);
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 ());
}
);
}
private void AssertIsTheStartingDirectory (string path)
{
if (IsWindows ()) {
Assert.Equal (@"c:\demo\", path);
} else {
Assert.Equal ("/demo/", path);
}
}
private void AssertIsTheRootDirectory (string path)
{
if (IsWindows ()) {
Assert.Equal (@"c:\", path);
} else {
Assert.Equal ("/", path);
}
}
private void AssertIsTheSubfolder (string path)
{
if (IsWindows ()) {
Assert.Equal (@"c:\demo\subfolder", path);
} else {
Assert.Equal ("/demo/subfolder", path);
}
}
[Fact, AutoInitShutdown]
public void TestDirectoryContents_Linux ()
{
if (IsWindows ()) {
return;
}
var fd = GetLinuxDialog ();
fd.Title = string.Empty;
fd.Style.Culture = new CultureInfo ("en-US");
fd.Draw ();
string expected =
@$"
┌─────────────────────────────────────────────────────────────────────────┐
│/demo/ │
│{CM.Glyphs.LeftBracket}▲{CM.Glyphs.RightBracket} │
│┌────────────┬──────────┬──────────────────────────────┬────────────────┐│
││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 ││
│ │
│ │
│ │
│{CM.Glyphs.LeftBracket} ►► {CM.Glyphs.RightBracket} Enter Search {CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} OK {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket} {CM.Glyphs.LeftBracket} Cancel {CM.Glyphs.RightBracket} │
└─────────────────────────────────────────────────────────────────────────┘
";
TestHelpers.AssertDriverContentsAre (expected, output, ignoreLeadingWhitespace: true);
}
[Fact, AutoInitShutdown]
public void TestDirectoryContents_Windows ()
{
if (!IsWindows ()) {
return;
}
var fd = GetWindowsDialog ();
fd.Title = string.Empty;
fd.Style.Culture = new CultureInfo ("en-US");
fd.Draw ();
string expected =
@$"
┌─────────────────────────────────────────────────────────────────────────┐
│c:\demo\ │
│{CM.Glyphs.LeftBracket}▲{CM.Glyphs.RightBracket} │
│┌────────────┬──────────┬──────────────────────────────┬────────────────┐│
││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 ││
│ │
│ │
│{CM.Glyphs.LeftBracket} ►► {CM.Glyphs.RightBracket} Enter Search {CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} OK {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket} {CM.Glyphs.LeftBracket} Cancel {CM.Glyphs.RightBracket} │
└─────────────────────────────────────────────────────────────────────────┘
";
TestHelpers.AssertDriverContentsAre (expected, output, ignoreLeadingWhitespace: true);
}
[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 ("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));
}
[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, AutoInitShutdown]
[InlineData (true)]
[InlineData (false)]
public void CancelSelection (bool cancel)
{
var dlg = GetInitializedFileDialog ();
var 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, false);
Assert.Equal (cancel, dlg.Canceled);
}
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 (var ch in chars) {
Application.Driver.SendKeys (ch, ConsoleKey.NoName, false, false, false);
}
}
/*
[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 System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform (System.Runtime.InteropServices.OSPlatform.Windows);
}
private FileDialog GetDialog ()
{
return IsWindows () ? GetWindowsDialog () : GetLinuxDialog ();
}
private FileDialog GetWindowsDialog ()
{
// Arrange
var fileSystem = new MockFileSystem (new Dictionary<string, MockFileData> (), @"c:\");
fileSystem.MockTime (() => new DateTime (2010, 01, 01, 11, 12, 43));
fileSystem.AddFile (@"c:\myfile.txt", new MockFileData ("Testing is meh.") { LastWriteTime = new DateTime (2001, 01, 01, 11, 12, 11) });
fileSystem.AddFile (@"c:\demo\jQuery.js", new MockFileData ("some js") { LastWriteTime = new DateTime (2001, 01, 01, 11, 44, 42) });
fileSystem.AddFile (@"c:\demo\mybinary.exe", new MockFileData ("some js") { LastWriteTime = new DateTime (2001, 01, 01, 11, 44, 42) });
fileSystem.AddFile (@"c:\demo\image.gif", new MockFileData (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 DateTime (2002, 01, 01, 22, 42, 10);
fileSystem.AddFile (@"c:\demo\subfolder\image2.gif", new MockFileData (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;
}
private FileDialog GetLinuxDialog ()
{
// Arrange
var fileSystem = new MockFileSystem (new Dictionary<string, MockFileData> (), "/");
fileSystem.MockTime (() => new DateTime (2010, 01, 01, 11, 12, 43));
fileSystem.AddFile (@"/myfile.txt", new MockFileData ("Testing is meh.") { LastWriteTime = new DateTime (2001, 01, 01, 11, 12, 11) });
fileSystem.AddFile (@"/demo/jQuery.js", new MockFileData ("some js") { LastWriteTime = new DateTime (2001, 01, 01, 11, 44, 42) });
fileSystem.AddFile (@"/demo/image.gif", new MockFileData (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 DateTime (2002, 01, 01, 22, 42, 10);
fileSystem.AddFile (@"/demo/subfolder/image2.gif", new MockFileData (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 GetInitializedFileDialog ()
{
var dlg = new FileDialog ();
Begin (dlg);
return dlg;
}
private void Begin (FileDialog dlg)
{
dlg.BeginInit ();
dlg.EndInit ();
Application.Begin (dlg);
}
}
}