Files
Terminal.Gui/Tests/IntegrationTests/FluentTests/FileDialogFluentTests.cs
Tig 1eb5a4e1b9 Fixes #4305 - 'FileDialog` TreeView (#4306)
* Partial fix - probably breaks stuff

Refactored `FileDialog.cs` by replacing `treeViewContainer` with `_treeView`, adjusting UI component positions, and adding style properties to `_tableView`. Improved user interaction with new event handlers and key bindings. Rearranged `base.Add` calls to reflect the updated UI hierarchy.

* Tweaked Dialog and FileDialog attribute handling

Removed setting of _tableView.Style.InvertSelectedCellFirstCharacter = true;

Commented out custom attribute handling in Dialog.cs for VisualRole.Normal and VisualRole.Focus, reverting to base method. Removed InvertSelectedCellFirstCharacter setting in FileDialog.cs.

* Add tree view toggle to FileDialog

Introduced a new `_btnTreeToggle` button in `FileDialog.cs` to manage the visibility of a tree view within the file dialog interface. Added a new localized string `fdTree` in `Strings.Designer.cs` and `Strings.resx` for UI elements related to the tree view. Adjusted the layout and visibility of the tree and table views to accommodate the toggle functionality. Implemented methods `ToggleTreeVisibility`, `SetTreeVisible`, and `GetTreeToggleText` to handle tree view visibility logic. Cleaned up code and comments for clarity.

* Update localization and test logic for FileDialog

Updated `fdSearchCaption` localization from "Enter search string" to "Find" in `Strings.Designer.cs` and `Strings.resx`. Modified `FileDialogFluentTests.cs` to reflect UI changes by updating button text and removing skip conditions. Adjusted `FileDialogTests.cs` to change `TextField` caption and commented out certain test logic related to path confirmation.

* Moved Search view to be inside the table view container for better usability.

Refactor FileDialog to use nullable reference types

Updated the `FileDialog` class to adopt nullable reference types for improved null safety, marking fields, properties, and methods as nullable where appropriate. Simplified UI component initialization, including repositioning `_tbFind` and `_spinnerView` into `_tableViewContainer` and assigning `Id` properties to `_tableViewContainer` and `_tableView`.

Refactored methods like `TryAcceptMulti` and `GetFocusedFiles` to handle nullability. Simplified `Task.Run` calls and removed unused code, such as the `GetTextField` method and `FileDialogPart` enum, in `FileDialogTests.cs`. Updated tests to directly access subviews using `Id` properties. Minor layout and property adjustments were made to improve maintainability.

* Refactor Dialog class and improve null safety

- Enabled nullable reference types in `Dialog.cs` for better null safety.
- Removed and reintroduced static configuration properties with `[ConfigurationProperty]` attributes for configurability.
- Refactored `Dialog` constructor to use `base.ShadowStyle` and improved button management with alignment logic.
- Updated `Canceled` property with a private backing field and debug assertions.
- Added null-forgiving operators (`!`) across the codebase for nullable reference type compatibility.
- Introduced new test cases to verify `Dialog` behavior, including modal mouse capture and `Canceled` property access.
- Refactored and modernized existing test cases for consistency and readability.
- Removed redundant test cases and performed general code cleanup.
- Improved code comments and debug assertions for clarity and robustness.

* Refactor DialogTests to replace null with empty array

Updated the `BeginButtonTestDialog` method call in `DialogTests.cs`
to replace the `null!` argument with an empty array `[]`. This
improves type safety and ensures explicit handling of the argument.
2025-10-23 14:54:06 -06:00

376 lines
15 KiB
C#

using System.Globalization;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using System.Runtime.InteropServices;
using TerminalGuiFluentTesting;
using TerminalGuiFluentTestingXunit;
using Xunit.Abstractions;
namespace IntegrationTests.FluentTests;
public class FileDialogFluentTests
{
private readonly TextWriter _out;
public FileDialogFluentTests (ITestOutputHelper outputHelper)
{
CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
_out = new TestOutputWriter (outputHelper);
}
private MockFileSystem CreateExampleFileSystem ()
{
// Optional: use Ordinal to simulate Linux-style case sensitivity
var mockFileSystem = new MockFileSystem (new Dictionary<string, MockFileData> ());
string testDir = mockFileSystem.Path.Combine ("test-dir");
string subDir = mockFileSystem.Path.Combine (testDir, "sub-dir");
string logsDir = "logs";
string emptyDir = "empty-dir";
// Add files
mockFileSystem.AddFile (mockFileSystem.Path.Combine (testDir, "file1.txt"), new MockFileData ("Hello, this is file 1."));
mockFileSystem.AddFile (mockFileSystem.Path.Combine (testDir, "file2.txt"), new MockFileData ("Hello, this is file 2."));
mockFileSystem.AddFile (mockFileSystem.Path.Combine (subDir, "nested-file.txt"), new MockFileData ("This is a nested file."));
mockFileSystem.AddFile (mockFileSystem.Path.Combine (logsDir, "log1.log"), new MockFileData ("Log entry 1"));
mockFileSystem.AddFile (mockFileSystem.Path.Combine (logsDir, "log2.log"), new MockFileData ("Log entry 2"));
// Create an empty directory
mockFileSystem.AddDirectory (emptyDir);
return mockFileSystem;
}
private Toplevel NewSaveDialog (out SaveDialog sd, bool modal = true)
{
return NewSaveDialog (out sd, out _, modal);
}
private Toplevel NewSaveDialog (out SaveDialog sd, out MockFileSystem fs,bool modal = true)
{
fs = CreateExampleFileSystem ();
sd = new SaveDialog (fs) { Modal = modal };
return sd;
}
[Theory]
[ClassData (typeof (TestDrivers))]
public void CancelFileDialog_UsingEscape (TestDriver d)
{
SaveDialog? sd = null;
using var c = With.A (()=>NewSaveDialog(out sd), 100, 20, d)
.ScreenShot ("Save dialog", _out)
.Escape ()
.AssertTrue (sd!.Canceled)
.Stop ();
}
[Theory]
[ClassData (typeof (TestDrivers))]
public void CancelFileDialog_UsingCancelButton_TabThenEnter (TestDriver d)
{
SaveDialog? sd = null;
using var c = With.A (() => NewSaveDialog (out sd,modal:false), 100, 20, d)
.ScreenShot ("Save dialog", _out)
.Focus<Button> (b => b.Text == "_Cancel")
.AssertTrue (sd!.Canceled)
.Enter ()
.Stop ();
}
[Theory]
[ClassData (typeof (TestDrivers))]
public void CancelFileDialog_UsingCancelButton_LeftClickButton (TestDriver d)
{
SaveDialog? sd = null;
using var c = With.A (() => NewSaveDialog (out sd), 100, 20, d)
.ScreenShot ("Save dialog", _out)
.LeftClick<Button> (b => b.Text == "_Cancel")
.WriteOutLogs (_out)
.AssertTrue (sd!.Canceled)
.Stop ();
}
[Theory]
[ClassData (typeof (TestDrivers))]
public void CancelFileDialog_UsingCancelButton_AltC (TestDriver d)
{
SaveDialog? sd = null;
using var c = With.A (() => NewSaveDialog (out sd), 100, 20, d)
.ScreenShot ("Save dialog", _out)
.Send (Key.C.WithAlt)
.WriteOutLogs (_out)
.AssertTrue (sd!.Canceled)
.Stop ();
}
[Theory]
[ClassData (typeof (TestDrivers))]
public void SaveFileDialog_UsingOkButton_Enter (TestDriver d)
{
SaveDialog? sd = null;
MockFileSystem? fs = null;
using var c = With.A (() => NewSaveDialog (out sd,out fs), 100, 20, d)
.ScreenShot ("Save dialog", _out)
.LeftClick<Button> (b => b.Text == "_Save")
.WaitIteration ()
.WriteOutLogs (_out)
.AssertFalse(sd!.Canceled)
.AssertEqual (GetFileSystemRoot (fs!), sd!.FileName)
.Stop ();
}
[Theory]
[ClassData (typeof (TestDrivers))]
public void SaveFileDialog_UsingOkButton_AltS (TestDriver d)
{
SaveDialog? sd = null;
MockFileSystem? fs = null;
using var c = With.A (() => NewSaveDialog (out sd, out fs), 100, 20, d)
.ScreenShot ("Save dialog", _out)
.Send (Key.S.WithAlt)
.WriteOutLogs (_out)
.AssertFalse (sd!.Canceled)
.AssertEqual (GetFileSystemRoot (fs!), sd!.FileName)
.Stop ();
}
[Theory]
[ClassData (typeof (TestDrivers))]
public void SaveFileDialog_UsingOkButton_TabEnter (TestDriver d)
{
SaveDialog? sd = null;
MockFileSystem? fs = null;
using var c = With.A (() => NewSaveDialog (out sd, out fs,modal:false), 100, 20, d)
.ScreenShot ("Save dialog", _out)
.Focus<Button> (b => b.Text == "_Save")
.Enter ()
.WriteOutLogs (_out)
.AssertFalse(sd!.Canceled)
.AssertEqual (GetFileSystemRoot(fs!), sd!.FileName)
.Stop ();
}
private string GetFileSystemRoot (IFileSystem fs)
{
return RuntimeInformation.IsOSPlatform (OSPlatform.Windows) ?
$@"C:{fs.Path.DirectorySeparatorChar}" :
"/";
}
[Theory]
[ClassData (typeof (TestDrivers))]
public void SaveFileDialog_PressingPopTree_ShouldNotChangeCancel (TestDriver d)
{
SaveDialog? sd = null;
MockFileSystem? fs = null;
using var c = With.A (() => NewSaveDialog (out sd, out fs,modal:false), 100, 20, d)
.ScreenShot ("Save dialog", _out)
.AssertTrue (sd!.Canceled)
.Focus<Button> (b => b.Text == "►_Tree")
.Enter ()
.ScreenShot ("After pop tree", _out)
.WriteOutLogs (_out)
.AssertTrue (sd!.Canceled)
.Stop ();
}
[Theory]
[ClassData (typeof (TestDrivers))]
public void SaveFileDialog_PopTree_AndNavigate (TestDriver d)
{
SaveDialog? sd = null;
MockFileSystem? fs = null;
using var c = With.A (() => NewSaveDialog (out sd, out fs, modal: false), 100, 20, d)
.ScreenShot ("Save dialog", _out)
.AssertTrue (sd!.Canceled)
.LeftClick<Button> (b => b.Text == "►_Tree")
.ScreenShot ("After pop tree", _out)
.Focus<TreeView<IFileSystemInfo>> (_ => true)
.Right ()
.ScreenShot ("After expand tree", _out)
.Down ()
.ScreenShot ("After navigate down in tree", _out)
.Enter ()
.WaitIteration ()
.AssertFalse (sd!.Canceled)
.AssertContains ("empty-dir", sd!.FileName)
.WriteOutLogs (_out)
.Stop ();
}
[Theory]
[ClassData (typeof (TestDrivers))]
public void SaveFileDialog_PopTree_AndNavigate_PreserveFilenameOnDirectoryChanges_True (TestDriver d)
{
SaveDialog? sd = null;
MockFileSystem? fs = null;
using var c = With.A (() => NewSaveDialog (out sd, out fs, modal: false), 100, 20, d)
.Then (()=>sd!.Style.PreserveFilenameOnDirectoryChanges=true)
.ScreenShot ("Save dialog", _out)
.AssertTrue (sd!.Canceled)
.Focus<TextField> (_=>true)
// Clear selection by pressing right in 'file path' text box
.RaiseKeyDownEvent (Key.CursorRight)
.AssertIsType <TextField>(sd!.Focused)
// Type a filename into the dialog
.RaiseKeyDownEvent (Key.H)
.RaiseKeyDownEvent (Key.E)
.RaiseKeyDownEvent (Key.L)
.RaiseKeyDownEvent (Key.L)
.RaiseKeyDownEvent (Key.O)
.WaitIteration ()
.ScreenShot ("After typing filename 'hello'", _out)
.AssertEndsWith ("hello", sd!.Path)
.LeftClick<Button> (b => b.Text == "►_Tree")
.ScreenShot ("After pop tree", _out)
.Focus<TreeView<IFileSystemInfo>> (_ => true)
.Right ()
.ScreenShot ("After expand tree", _out)
// Because of PreserveFilenameOnDirectoryChanges we should select the new dir but keep the filename
.AssertEndsWith ("hello", sd!.Path)
.Down ()
.ScreenShot ("After navigate down in tree", _out)
// Because of PreserveFilenameOnDirectoryChanges we should select the new dir but keep the filename
.AssertContains ("empty-dir",sd!.Path)
.AssertEndsWith ("hello", sd!.Path)
.Enter ()
.WaitIteration ()
.AssertFalse (sd!.Canceled)
.AssertContains ("empty-dir", sd!.FileName)
.WriteOutLogs (_out)
.Stop ();
}
[Theory]
[ClassData (typeof (TestDrivers))]
public void SaveFileDialog_PopTree_AndNavigate_PreserveFilenameOnDirectoryChanges_False (TestDriver d)
{
SaveDialog? sd = null;
MockFileSystem? fs = null;
using var c = With.A (() => NewSaveDialog (out sd, out fs, modal: false), 100, 20, d)
.Then (()=> sd!.Style.PreserveFilenameOnDirectoryChanges = false)
.ScreenShot ("Save dialog", _out)
.AssertTrue (sd!.Canceled)
.Focus<TextField> (_ => true)
// Clear selection by pressing right in 'file path' text box
.RaiseKeyDownEvent (Key.CursorRight)
.AssertIsType<TextField> (sd!.Focused)
// Type a filename into the dialog
.RaiseKeyDownEvent (Key.H)
.RaiseKeyDownEvent (Key.E)
.RaiseKeyDownEvent (Key.L)
.RaiseKeyDownEvent (Key.L)
.RaiseKeyDownEvent (Key.O)
.WaitIteration ()
.ScreenShot ("After typing filename 'hello'", _out)
.AssertEndsWith ("hello", sd!.Path)
.LeftClick<Button> (b => b.Text == "►_Tree")
.ScreenShot ("After pop tree", _out)
.Focus<TreeView<IFileSystemInfo>> (_ => true)
.Right ()
.ScreenShot ("After expand tree", _out)
.Down ()
.ScreenShot ("After navigate down in tree", _out)
// PreserveFilenameOnDirectoryChanges is false so just select new path
.AssertEndsWith ("empty-dir", sd!.Path)
.AssertDoesNotContain ("hello", sd!.Path)
.Enter ()
.WaitIteration ()
.AssertFalse (sd!.Canceled)
.AssertContains ("empty-dir", sd!.FileName)
.WriteOutLogs (_out)
.Stop ();
}
[Theory]
[ClassData (typeof (TestDrivers_WithTrueFalseParameter))]
public void SaveFileDialog_TableView_UpDown_PreserveFilenameOnDirectoryChanges_True (TestDriver d, bool preserve)
{
SaveDialog? sd = null;
MockFileSystem? fs = null;
using var c = With.A (() => NewSaveDialog (out sd, out fs, modal: false), 100, 20, d)
.Then (() => sd!.Style.PreserveFilenameOnDirectoryChanges = preserve)
.ScreenShot ("Save dialog", _out)
.AssertTrue (sd!.Canceled)
.Focus<TextField> (_ => true)
// Clear selection by pressing right in 'file path' text box
.RaiseKeyDownEvent (Key.CursorRight)
.AssertIsType<TextField> (sd!.Focused)
// Type a filename into the dialog
.RaiseKeyDownEvent (Key.H)
.RaiseKeyDownEvent (Key.E)
.RaiseKeyDownEvent (Key.L)
.RaiseKeyDownEvent (Key.L)
.RaiseKeyDownEvent (Key.O)
.WaitIteration ()
.ScreenShot ("After typing filename 'hello'", _out)
.AssertEndsWith ("hello", sd!.Path)
.Focus<TableView> (_ => true)
.ScreenShot ("After focus table", _out)
.Down ()
.ScreenShot ("After down in table", _out);
if (preserve)
{
c.AssertContains ("logs", sd!.Path)
.AssertEndsWith ("hello", sd!.Path);
}
else
{
c.AssertContains ("logs", sd!.Path)
.AssertDoesNotContain ("hello", sd!.Path);
}
c.Up ()
.ScreenShot ("After up in table", _out);
if (preserve)
{
c.AssertContains ("empty-dir", sd!.Path)
.AssertEndsWith ("hello", sd!.Path);
}
else
{
c.AssertContains ("empty-dir", sd!.Path)
.AssertDoesNotContain ("hello", sd!.Path);
}
c.Enter ()
.ScreenShot ("After enter in table", _out); ;
if (preserve)
{
c.AssertContains ("empty-dir", sd!.Path)
.AssertEndsWith ("hello", sd!.Path);
}
else
{
c.AssertContains ("empty-dir", sd!.Path)
.AssertDoesNotContain ("hello", sd!.Path);
}
c.LeftClick<Button> (b => b.Text == "_Save");
c.WaitIteration ();
c.AssertFalse (sd!.Canceled);
if (preserve)
{
c.AssertContains ("empty-dir", sd!.Path)
.AssertEndsWith ("hello", sd!.Path);
}
else
{
c.AssertContains ("empty-dir", sd!.Path)
.AssertDoesNotContain ("hello", sd!.Path);
}
c.WriteOutLogs (_out);
c.WaitIteration ();
c.Stop ();
}
}