Files
Terminal.Gui/Tests/UnitTests/Views/TextFieldTests.cs
Copilot c5906c2dc1 #4329—Major Terminal.Gui v2 Architecture Modernization: Application Decoupling, Terminology Improvements, and Nullable Migration (#4338)
* Initial plan

* Add comprehensive terminology proposal for Application.Top/Toplevel renaming

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Add terminology proposal summary document

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Add before/after comparison document for terminology proposal

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Add index document for terminology proposal

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Add Mermaid diagrams visualizing the terminology proposal

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Move terminology docs to docfx/docs per CONTRIBUTING.md guidelines

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Update terminology proposal to reflect recent modernization (post-MainLoop removal)

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Update terminology proposal - validate against recent driver refactoring changes

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Update proposal after massive v2_develop modernization - TopLevels now public, RunState→SessionToken

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Update proposal: RunStack → SessionStack to align with SessionToken terminology

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Phase 1: Core API rename - Application.Top→Current, TopLevels→SessionStack

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Phase 2: Fix test compilation errors for renamed properties

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Phase 3: Update documentation files with new terminology

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Refactor generic type names and remove unused field

Renamed generic type parameters in `Dim` and `Pos` classes for clarity:
- `T` was renamed to `TDim` in `Dim.Has` method.
- `T` was renamed to `TPos` in `Pos.Has` method.
Updated type casting and pattern matching logic to reflect these changes.

Removed the unused `_stopAfterFirstIteration` field from the `ApplicationImpl` class to clean up the codebase.

* Increase minimum code coverage target to 75%

Updated the `codecov.yml` configuration file to raise the
`project.default.target` value from 70% to 75%, enforcing
stricter code coverage requirements for the overall project.

* Add comprehensive unit tests for ApplicationImpl Begin/End logic

Added ApplicationImplBeginEndTests with 16 tests covering:
- Begin/End argument validation
- SessionStack push/pop operations
- Current property management
- Balanced Begin/End enforcement
- Multiple nested Begin/End scenarios
- ResetState cleanup behavior
- Toplevel activation/deactivation
- SessionToken management

Tests validate the fragile state management logic in ApplicationImpl.Lifecycle.cs
and ApplicationImpl.Run.cs to catch regressions in Current/SessionStack handling.

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Refactor ApplicationImplBeginEndTests to work with ApplicationImpl instances directly

Tests now:
- Create ApplicationImpl instances directly instead of using global Application
- Use try-finally blocks to ensure Shutdown() is always called
- Work with app.Init() and app.Shutdown() properly
- 13 of 16 tests passing (3 tests need adjustment for ResetState behavior)

Tests validate fragile Begin/End state management logic without polluting global Application state.

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Fix ApplicationImplBeginEndTests: Remove explicit ResetState calls, rely on Shutdown

Per maintainer feedback:
- Tests should not call ResetState explicitly if they call Shutdown (Shutdown calls ResetState)
- Removed all explicit app.ResetState() calls from tests
- Tests now only call Shutdown in finally blocks for cleanup
- Ensured toplevels are disposed BEFORE Shutdown to satisfy DEBUG_IDISPOSABLE assertions
- Fixed test cleanup order to prevent ResetState assertion failures
- All 16 tests now pass successfully

ResetState is idempotent - the issue was tests calling it unnecessarily and with incorrect disposal ordering.

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Phase 1: Enable nullable by default, add directives to all files

- Added <Nullable>enable</Nullable> to Terminal.Gui.csproj
- Removed #nullable enable from 37 files (now redundant with project setting)
- Added #nullable disable to 170 files that don't have nullable enabled yet
- Build successful with 279 nullable warnings (expected)

Breakdown of files with #nullable disable:
- 27 in Views/ directory (will remain disabled per plan)
- 143 outside Views/ (need to be fixed in subsequent commits)

Next: Systematically fix nullable issues in non-View files.

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Phase 2: Remove nullable disable from all non-View files - COMPLETE!

- Removed #nullable disable from 142 non-View files
- All non-View code is now nullable-enabled
- Build successful with 0 errors, only 1 unrelated warning
- Created NULLABLE_VIEWS_REMAINING.md documenting the 121 View files still with nullable disable

Nullable cleanup summary:
 App/: 25 files - nullable enabled
 Configuration/: 24 files - nullable enabled
 ViewBase/: 30 files - nullable enabled
 Drivers/: 25 files - nullable enabled
 Drawing/: 18 files - nullable enabled
 FileServices/: 7 files - nullable enabled
 Input/: 6 files - nullable enabled
 Text/: 5 files - nullable enabled
 Resources/: 3 files - nullable enabled
⏸️  Views/: 121 files - remain disabled (as requested)

Total: 143 files nullable-enabled, 121 View files remain disabled.

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* WIP: fixing nullability issues.

* Fixed final nullability issues.

* Moved Arrangment tests

* Refactor and improve modularity across multiple classes

Refactored `FillRectangles` in `RegionScenario` and `Region` to accept an `IDriver` parameter, reducing reliance on global state. Updated `ApplicationImpl` to replace static method calls with instance methods for better encapsulation. Renamed `Toplevel` to `Current` in `IPopover` and related classes for clarity.

Simplified `LineCanvas` methods by removing unnecessary `IDriver` parameters. Added `Id` and `App` properties to `View` for better state management and unique identification. Streamlined the `Driver` property in `View` with a concise getter.

Improved formatting and consistency across files, including `Region` and `IntersectionRuneResolver`. Enhanced thread safety in `Region` and cleaned up redundant code. Updated tests to align with interface changes and ensure compatibility.

* Refactor to make IDriver dependency explicit

Updated `AnsiEscapeSequenceRequest.Send` to accept an `IDriver?` parameter, replacing reliance on `Application.Driver`. Refactored `AnsiRequestScheduler` methods (`SendOrSchedule`, `RunSchedule`, and private `Send`) to propagate the `IDriver?` parameter, ensuring explicit driver dependency.

Modified `DriverImpl.QueueAnsiRequest` to pass `this` to `SendOrSchedule`. Updated `AnsiRequestSchedulerTests` to reflect new method signatures, passing `null` for the driver parameter where applicable.

Added `<param>` documentation for new parameters to improve clarity. These changes enhance flexibility, maintainability, and testability by reducing reliance on global state and allowing driver substitution in tests.

* WIP: Started migrating to View.App

Refactored `ApplicationImpl` to ensure proper handling of the `App`
property for `Toplevel` instances, improving modularity. Replaced
direct references to `Application` with `App` in `Border`, `ShadowView`,
and other classes to enhance flexibility and maintainability.

Introduced `GetApp` in `View` to allow overrides for retrieving the
`App` instance. Updated `Adornment` to use this method. Moved mouse
event subscriptions in `Border` to `BeginInit` for proper lifecycle
management.

Updated unit tests in `ArrangementTests` to use `App.Mouse` instead of
`Application.Mouse`, ensuring alignment with the refactored design.
Added `BeginInit` and `EndInit` calls for proper initialization during
tests. Removed redundant code and improved test assertions.

* WIP: Next set of View.App changes

Updated `SetClipToScreen`, `SetClip`, and `GetClip` methods to accept an `IDriver` parameter, replacing reliance on the global `Application.Driver`. This improves modularity, testability, and reduces implicit global state usage.

- Updated `Driver` property in `View` to use `App?.Driver` as fallback.
- Refactored `DimAuto` to use `App?.Screen.Size` with a default for unit tests.
- Updated all test cases to align with the new method signatures.
- Performed general cleanup for consistency and readability.

* Adds View clip tests.

* Merged

* Merged

* wip

* Fixed test bug.

* Refactored Thickness.Draw to require driver.

* Made TextFormatter.Draw require driver.

* Code cleanup.

* Un did stoopid idea.

* Decouped Application.Navigation

* MASSIVE - Almost completely decoupled Application from View etc...

* Obsolete

* Missed some

* More cleanup and decoupling.

Refactor `ToString` and remove legacy code

Refactored `ToString` implementations across `Application`, `DriverImpl`, and `IDriver` to improve consistency and maintainability. Removed the legacy `ToString(IDriver? driver)` method and its associated references. Simplified `ToString` in `DriverImpl` to generate a string representation of the `Contents` buffer.

Replaced redundant XML documentation with `<inheritdoc/>` tags to reduce duplication. Cleaned up unused `global using` directives and removed deprecated methods and properties, including `Screen`, `SetCursorVisibility`, and `IsRuneSupported`.

Updated test cases in `GuiTestContext` and `DriverAssert` to use the new `ToString` implementation. Improved error messages for better debugging output. Streamlined LINQ queries and removed redundant checks for better readability and performance.

Enhanced maintainability by decluttering the codebase, aligning namespaces, and consolidating related changes.

* Changes before error encountered

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Update docfx/docs to document View.App architecture and instance-based patterns

Updated 16 documentation files to reflect the major architectural changes:

NEW FILES:
- application.md: Comprehensive deep dive on decoupled Application architecture

UPDATED FILES:
- View.md: Documents View.App property, GetApp(), and instance-based patterns
- navigation.md: Shows View.App usage instead of static Application
- drivers.md: Documents View.Driver and GetDriver() patterns
- keyboard.md: Event handling through View.App
- mouse.md: Mouse event handling via View.App
- arrangement.md: Updated code examples to use View.App
- drawing.md: Rendering examples with instance-based API
- cursor.md: Cursor management through View.App
- multitasking.md: SessionStack and session management via View.App
- Popovers.md: Popover patterns with View.App
- cancellable-work-pattern.md: Updated examples
- command.md: Command pattern with View.App context
- config.md: Configuration access through View.App
- migratingfromv1.md: Migration guide for static→instance patterns
- newinv2.md: Documents new instance-based architecture

All code examples now demonstrate the instance-based API (view.App.Current)
instead of obsolete static Application references. Documentation accurately
reflects the massive architectural decoupling achieved in this PR.

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Add `ToAnsi` support for ANSI escape sequence generation

Introduced `ToAnsi` in `IDriver` and `IOutput` interfaces to generate
ANSI escape sequences representing the terminal's current state. This
enables serialization of terminal content for debugging, testing, and
exporting.

Implemented `ToAnsi` in `DriverImpl` and `FakeOutput`, supporting both
16-color and RGB modes. Refactored `OutputBase` with helper methods
`BuildAnsiForRegion` and `AppendCellAnsi` for efficient ANSI generation.

Enhanced `GuiTestContext` with `AnsiScreenShot` for capturing terminal
state during tests. Added `ToAnsiTests` for comprehensive validation,
including edge cases, performance, and wide/Unicode character handling.

Updated documentation to reflect `ToAnsi` functionality and modernized
driver architecture. Improved testability, modularity, and performance
while removing legacy driver references.

* Improve null safety and cleanup in GuiTestContext

Enhanced null safety across `GuiTestContext` and `GuiTestContextTests`:
- Replaced `a` with `app` for better readability in tests.
- Added null checks (`!`, `?.`) to prevent potential null reference exceptions.
- Removed redundant `WaitIteration` and duplicate `ScreenShot` calls.

Improved error handling and robustness:
- Updated shutdown logic to use null-safe calls for `RequestStop` and `Shutdown`.
- Applied null-safe invocation for `_applicationImpl.Invoke`.

General cleanup:
- Removed redundant method calls and improved naming consistency.
- Ensured better maintainability and adherence to best practices.

* Refactor docs: remove deprecated files, update architecture

Removed outdated documentation files related to the terminology
proposal (`terminology-before-after.md`, `terminology-diagrams.md`,
`terminology-index.md`, `terminology-proposal-summary.md`,
`terminology-proposal.md`) from the `Docs` project. These files
were either deprecated or consolidated into other documentation.

Updated `application.md`:
- Added a "View Hierarchy and Run Stack" section with a Mermaid
  diagram to illustrate the relationship between the view hierarchy
  and the application session stack.
- Added a "Usage Example Flow" section with a sequence diagram
  to demonstrate the flow of running and stopping views.

These changes improve clarity, streamline documentation, and
align with the finalized terminology updates for the
`Application.Current` and `Application.SessionStack` APIs.

* Refactor Init/Run methods to simplify driver handling

The `Init` method in `Application` and `IApplication` now accepts only an optional `driverName` parameter, removing the `IDriver` parameter. This simplifies initialization by relying on driver names to determine the appropriate driver.

The `Run` methods have been updated to use `driverName` instead of `driver`, ensuring consistency with the updated `Init` method.

Replaced redundant inline documentation with `<inheritdoc>` tags to improve maintainability and consistency. Legacy `Application` methods (`Init`, `Shutdown`, `Run`) have been marked as `[Obsolete]` to signal their eventual deprecation.

Test cases have been refactored to align with the updated `Init` method signature, removing unused `driver` parameters. Documentation files have also been updated to reflect these API changes.

These changes improve clarity, reduce complexity, and ensure a more consistent API design.

* Refactor: Introduce Application.Create() factory method

Introduced a new static method `Application.Create()` to create
instances of `IApplication`, replacing direct instantiation of
`ApplicationImpl`. This enforces a cleaner, recommended pattern
for creating application instances.

Made the `ApplicationImpl` constructor `internal` to ensure
`Application.Create()` is used for instance creation.

Refactored test cases across multiple files to use
`Application.Create()` instead of directly instantiating
`ApplicationImpl`. Simplified object initialization in tests
using target-typed `new()` expressions.

Updated documentation and examples in `application.md` to
reflect the new instance-based architecture and highlight its
benefits, such as supporting multiple applications with
different drivers.

Improved code readability, formatting, and consistency in
tests and documentation. Aligned `ApplicationImplBeginEndTests`
to use `IApplication` directly, adhering to the new architecture.

* Added `Application.StopAll` and fixed coupling issues.

Refactored `ApplicationImpl` to use an instance-based approach, replacing the static singleton pattern and Lazy<T>. Introduced `SetInstance` for configuring the singleton instance and updated tests to use `ApplicationImpl.Instance` or explicitly set the `Driver` property.

Enabled nullable reference types across the codebase, updating fields and variables to nullable types where applicable. Added null checks to improve safety and prevent runtime errors.

Refactored timeout management by introducing tokens for `Application.AddTimeout` and adding a `StopAll` method to `TimedEvents` for cleanup. Updated tests to use `System.Threading.Timer` for independent watchdog timers.

Removed legacy code, improved logging for error cases, and updated view initialization to explicitly set `App` or `Driver` in tests. Enhanced test coverage and restructured `ScrollSliderTests` for better readability.

Performed general code cleanup, including formatting changes, removal of unused imports, and improved naming consistency.

* Refactor: Transition to IApplication interface

Refactored the codebase to replace the static `Application` class with the `IApplication` interface, improving modularity, testability, and maintainability. Updated methods like `Application.Run`, `RequestStop`, and `Init` to use the new interface.

Marked static members `SessionStack` and `Current` as `[Obsolete]` and delegated their functionality to `ApplicationImpl.Instance`. Updated XML documentation to reflect these changes.

Simplified code by removing redundant comments, unused code, and converting methods like `GetMarginThickness` to single-line expressions. Improved null safety with null-conditional operators in `ToplevelTransitionManager`.

Enhanced consistency with formatting updates, logging improvements, and better error handling. Updated `Shortcut` and other classes to align with the new interface-based design.

Made breaking changes, including the removal of the `helpText` parameter in the `Shortcut` constructor. Updated `Wizard`, `Dialog`, and `GraphView` to use `IApplication` methods. Adjusted `ViewportSettings` and `HighlightStates` for better behavior.

* Enhance null-safety and simplify codebase

Improved null-safety by adopting nullable reference types and adding null-forgiving operators (`!`) where appropriate. Replaced direct method calls with null-safe calls using the null-conditional operator (`?.`) to prevent potential `NullReferenceException`.

Removed default parameter values in test methods to enforce explicit parameter passing. Refactored test classes to remove unnecessary dependencies on `ITestOutputHelper`.

Fixed a bug in `WindowsOutput.cs` by setting `_force16Colors` to `false` to avoid reliance on a problematic driver property. Updated `SessionTokenTests` to use null-forgiving operators for clarity in intentional null usage.

Simplified graph and UI updates by ensuring safe access to properties and methods. Cleaned up namespaces and removed unused `using` directives for better readability.

Updated `Dispose` methods to use null-safe calls and replaced nullable driver initialization with non-nullable initialization in `ScrollSliderTests` to ensure proper instantiation.

* Refactor test code to use nullable `App` property

Replaced direct `Application` references with `App` property across test classes to improve encapsulation and robustness. Updated `GuiTestContext` to use a nullable `App` property, replacing `_applicationImpl` for consistency.

Refactored key event handling to use `App.Driver` and revised `InitializeApplication` and `CleanupApplication` methods to ensure safe usage of the nullable `App` property. Updated `Then` callbacks to explicitly pass `App` for clarity.

Replaced `Application.QuitKey` with `context.App?.Keyboard.RaiseKeyDownEvent` to ensure context-specific event handling. Refactored `EnableForDesign` logic in `MenuBarv2Tests` and `PopoverMenuTests` to operate on the correct application instance.

Improved null safety in test assertions and revised `RequestStop` and `Shutdown` calls to use `App?.RequestStop` and `App?.Shutdown`. Updated navigation logic to use `Terminal.Gui.App.Application` for namespace consistency.

Enhanced exception handling in the `Invoke` method and performed general cleanup to align with modern C# practices, improving maintainability and readability.

* Commented out exception handling in Application.Shutdown

The `try-catch` block around `Application.Shutdown` was commented out, disabling the logging of exceptions thrown after a test exited. This change removes the `catch` block that used `Debug.WriteLine` for logging.

The `finally` block remains intact, ensuring cleanup operations such as clearing `View.Instances` and resetting the application state are still executed.

* Fixes #4394 - Changing Theme at Runtime does not Update Some Properties

* Tweaks to config format.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Tig <tig@users.noreply.github.com>
Co-authored-by: tig <585482+tig@users.noreply.github.com>
2025-11-19 16:23:35 -05:00

1722 lines
63 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Globalization;
using System.Reflection;
using System.Text;
using UnitTests;
using Xunit.Abstractions;
namespace UnitTests.ViewsTests;
public class TextFieldTests (ITestOutputHelper output)
{
private static TextField _textField;
[Fact]
[TextFieldTestsAutoInitShutdown]
public void CanFocus_False_Wont_Focus_With_Mouse ()
{
Toplevel top = new ();
var tf = new TextField { Width = Dim.Fill (), CanFocus = false, ReadOnly = true, Text = "some text" };
var fv = new FrameView
{
Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = false, Title = "I shouldn't get focus"
};
fv.Add (tf);
top.Add (fv);
Application.Begin (top);
Assert.False (tf.CanFocus);
Assert.False (tf.HasFocus);
Assert.False (fv.CanFocus);
Assert.False (fv.HasFocus);
tf.NewMouseEvent (
new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked }
);
Assert.Null (tf.SelectedText);
Assert.False (tf.CanFocus);
Assert.False (tf.HasFocus);
Assert.False (fv.CanFocus);
Assert.False (fv.HasFocus);
fv.CanFocus = true;
tf.CanFocus = true;
tf.NewMouseEvent (
new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked }
);
Assert.Equal ("some ", tf.SelectedText);
Assert.True (tf.CanFocus);
Assert.True (tf.HasFocus);
Assert.True (fv.CanFocus);
Assert.True (fv.HasFocus);
fv.CanFocus = false;
tf.NewMouseEvent (
new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked }
);
Assert.Equal ("some ", tf.SelectedText); // Setting CanFocus to false don't change the SelectedText
Assert.True (tf.CanFocus); // v2: CanFocus is not longer automatically changed
Assert.False (tf.HasFocus);
Assert.False (fv.CanFocus);
Assert.False (fv.HasFocus);
top.Dispose ();
}
[Theory]
[AutoInitShutdown]
[InlineData ("0123456789", "0123456789")]
[InlineData ("01234567890", "0123456789")]
public void CaptionedTextField_DoesNotOverspillBounds (string caption, string expectedRender)
{
TextField tf = GetTextFieldsInView ();
// Caption has no effect when focused
tf.Title = caption;
Application.RaiseKeyDownEvent ('\t');
Assert.False (tf.HasFocus);
tf.Draw ();
DriverAssert.AssertDriverContentsAre (expectedRender, output);
Application.Current.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void CaptionedTextField_DoesNotOverspillViewport_Unicode ()
{
string caption = "Mise" + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber)) + "rables";
Assert.Equal (11, caption.Length);
Assert.Equal (10, caption.EnumerateRunes ().Sum (c => c.GetColumns ()));
TextField tf = GetTextFieldsInView ();
tf.Title = caption;
Application.RaiseKeyDownEvent ('\t');
Assert.False (tf.HasFocus);
tf.Draw ();
DriverAssert.AssertDriverContentsAre ("Misérables", output);
Application.Current.Dispose ();
}
[Theory (Skip = "Broke with ContextMenuv2")]
[AutoInitShutdown]
[InlineData ("blah")]
[InlineData (" ")]
public void CaptionedTextField_DoNotRenderCaption_WhenTextPresent (string content)
{
TextField tf = GetTextFieldsInView ();
tf.Draw ();
DriverAssert.AssertDriverContentsAre ("", output);
tf.Title = "Enter txt";
Application.RaiseKeyDownEvent ('\t');
// Caption should appear when not focused and no text
Assert.False (tf.HasFocus);
tf.SetClipToScreen ();
tf.Draw ();
DriverAssert.AssertDriverContentsAre ("Enter txt", output);
// but disapear when text is added
tf.Text = content;
tf.SetClipToScreen ();
tf.Draw ();
DriverAssert.AssertDriverContentsAre (content, output);
Application.Current.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void CaptionedTextField_RendersCaption_WhenNotFocused ()
{
TextField tf = GetTextFieldsInView ();
tf.Draw ();
DriverAssert.AssertDriverContentsAre ("", output);
// Caption has no effect when focused
tf.Title = "Enter txt";
Assert.True (tf.HasFocus);
tf.SetClipToScreen ();
tf.Draw ();
DriverAssert.AssertDriverContentsAre ("", output);
Application.RaiseKeyDownEvent ('\t');
Assert.False (tf.HasFocus);
tf.SetClipToScreen ();
tf.Draw ();
DriverAssert.AssertDriverContentsAre ("Enter txt", output);
Application.Current.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void Title_RendersAsCaption_WithCorrectAttributes ()
{
TextField tf = GetTextFieldsInView ();
// Set a title (caption)
tf.Title = "Enter text";
// Remove focus so caption appears
Application.RaiseKeyDownEvent ('\t');
Assert.False (tf.HasFocus);
tf.SetClipToScreen ();
tf.Draw ();
// Verify the caption text is rendered
DriverAssert.AssertDriverContentsAre ("Enter text", output);
// Verify the caption uses dimmed color attribute
Attribute captionAttr = new Attribute (
tf.GetAttributeForRole (VisualRole.Editable).Foreground.GetDimColor (),
tf.GetAttributeForRole (VisualRole.Editable).Background);
// All characters in "Enter text" should have the caption attribute
DriverAssert.AssertDriverAttributesAre ("0000000000", output, Application.Driver, captionAttr);
Application.Current.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void Title_WithHotkey_RendersUnderlined ()
{
TextField tf = GetTextFieldsInView ();
// Title with hotkey should be rendered with the hotkey underlined when not focused
tf.Title = "_Find";
// Remove focus so caption appears
Application.RaiseKeyDownEvent ('\t');
Assert.False (tf.HasFocus);
tf.SetClipToScreen ();
tf.Draw ();
// The hotkey character 'F' should be rendered (without the underscore in the actual text)
DriverAssert.AssertDriverContentsAre ("Find", output);
// Verify the hotkey character 'F' has underline style
Attribute captionAttr = new Attribute (
tf.GetAttributeForRole (VisualRole.Editable).Foreground.GetDimColor (),
tf.GetAttributeForRole (VisualRole.Editable).Background);
Attribute hotkeyAttr = new Attribute (
tf.GetAttributeForRole (VisualRole.Editable).Foreground.GetDimColor (),
tf.GetAttributeForRole (VisualRole.Editable).Background,
tf.GetAttributeForRole (VisualRole.Editable).Style | TextStyle.Underline);
// F is underlined (index 1), remaining characters use normal caption attribute (index 0)
DriverAssert.AssertDriverAttributesAre ("1000", output, Application.Driver, captionAttr, hotkeyAttr);
Application.Current.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void Title_WithHotkey_MiddleCharacter_RendersUnderlined ()
{
TextField tf = GetTextFieldsInView ();
// Title with hotkey in middle of text
tf.Title = "Enter _Text";
// Remove focus so caption appears
Application.RaiseKeyDownEvent ('\t');
Assert.False (tf.HasFocus);
tf.SetClipToScreen ();
tf.Draw ();
// The underscore should not be rendered, 'T' should be underlined
DriverAssert.AssertDriverContentsAre ("Enter Text", output);
// Verify the hotkey character 'T' has underline style
Attribute captionAttr = new Attribute (
tf.GetAttributeForRole (VisualRole.Editable).Foreground.GetDimColor (),
tf.GetAttributeForRole (VisualRole.Editable).Background);
Attribute hotkeyAttr = new Attribute (
tf.GetAttributeForRole (VisualRole.Editable).Foreground.GetDimColor (),
tf.GetAttributeForRole (VisualRole.Editable).Background,
tf.GetAttributeForRole (VisualRole.Editable).Style | TextStyle.Underline);
// "Enter " (6 chars) + "T" (underlined) + "ext" (3 chars)
DriverAssert.AssertDriverAttributesAre ("0000001000", output, Application.Driver, captionAttr, hotkeyAttr);
Application.Current.Dispose ();
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void Changing_SelectedStart_Or_CursorPosition_Update_SelectedLength_And_SelectedText ()
{
_textField.BeginInit ();
_textField.EndInit ();
_textField.SelectedStart = 2;
Assert.Equal (32, _textField.CursorPosition);
Assert.Equal (30, _textField.SelectedLength);
Assert.Equal ("B to jump between text fields.", _textField.SelectedText);
_textField.CursorPosition = 20;
Assert.Equal (2, _textField.SelectedStart);
Assert.Equal (18, _textField.SelectedLength);
Assert.Equal ("B to jump between ", _textField.SelectedText);
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void Copy_Or_Cut__Not_Allowed_If_Secret_Is_True ()
{
_textField.Secret = true;
_textField.SelectedStart = 20;
_textField.CursorPosition = 24;
_textField.Copy ();
Assert.Null (_textField.SelectedText);
_textField.Cut ();
Assert.Null (_textField.SelectedText);
_textField.Secret = false;
_textField.Copy ();
Assert.Equal ("text", _textField.SelectedText);
_textField.Cut ();
Assert.Null (_textField.SelectedText);
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void Copy_Or_Cut_And_Paste_With_No_Selection ()
{
_textField.SelectedStart = 20;
_textField.CursorPosition = 24;
_textField.Copy ();
Assert.Equal ("text", _textField.SelectedText);
Assert.Equal ("TAB to jump between text fields.", _textField.Text);
_textField.SelectedStart = -1;
_textField.Paste ();
Assert.Equal ("TAB to jump between texttext fields.", _textField.Text);
_textField.SelectedStart = 24;
_textField.Cut ();
Assert.Null (_textField.SelectedText);
Assert.Equal ("TAB to jump between text fields.", _textField.Text);
_textField.SelectedStart = -1;
_textField.Paste ();
Assert.Equal ("TAB to jump between texttext fields.", _textField.Text);
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void Copy_Or_Cut_And_Paste_With_Selection ()
{
_textField.SelectedStart = 20;
_textField.CursorPosition = 24;
_textField.Copy ();
Assert.Equal ("text", _textField.SelectedText);
Assert.Equal ("TAB to jump between text fields.", _textField.Text);
_textField.Paste ();
Assert.Equal ("TAB to jump between text fields.", _textField.Text);
_textField.SelectedStart = 20;
_textField.Cut ();
_textField.Paste ();
Assert.Equal ("TAB to jump between text fields.", _textField.Text);
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void Copy_Or_Cut_Not_Null_If_Has_Selection ()
{
_textField.SelectedStart = 20;
_textField.CursorPosition = 24;
_textField.Copy ();
Assert.Equal ("text", _textField.SelectedText);
_textField.Cut ();
Assert.Null (_textField.SelectedText);
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void Copy_Or_Cut_Null_If_No_Selection ()
{
_textField.SelectedStart = -1;
_textField.Copy ();
Assert.Null (_textField.SelectedText);
_textField.Cut ();
Assert.Null (_textField.SelectedText);
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void Copy_Paste_Surrogate_Pairs ()
{
_textField.Text = "TextField with some more test text. Unicode shouldn't 𝔹A𝔽!";
_textField.SelectAll ();
_textField.Cut ();
Assert.Equal (
"TextField with some more test text. Unicode shouldn't 𝔹A𝔽!",
Application.Driver?.Clipboard!.GetClipboardData ()
);
Assert.Equal (string.Empty, _textField.Text);
_textField.Paste ();
Assert.Equal ("TextField with some more test text. Unicode shouldn't 𝔹A𝔽!", _textField.Text);
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void Copy_Paste_Text_Changing_Updates_Cursor_Position ()
{
_textField.BeginInit ();
_textField.EndInit ();
_textField.TextChanging += TextFieldTextChanging;
void TextFieldTextChanging (object sender, ResultEventArgs<string> e)
{
if (e.Result.GetRuneCount () > 11)
{
e.Result = e.Result [..11];
}
}
Assert.Equal (32, _textField.CursorPosition);
_textField.SelectAll ();
_textField.Cut ();
Assert.Equal ("TAB to jump between text fields.", Application.Driver?.Clipboard!.GetClipboardData ());
Assert.Equal (string.Empty, _textField.Text);
Assert.Equal (0, _textField.CursorPosition);
_textField.Paste ();
Assert.Equal ("TAB to jump", _textField.Text);
Assert.Equal (11, _textField.CursorPosition);
_textField.TextChanging -= TextFieldTextChanging;
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void Cursor_Position_Initialization ()
{
Assert.False (_textField.IsInitialized);
// BUGBUG: IsInitialized is false and
// CursorPosition wasn't calculated yet
Assert.Equal (0, _textField.CursorPosition);
_textField.BeginInit ();
_textField.EndInit ();
Assert.Equal (32, _textField.CursorPosition);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
Assert.Equal ("TAB to jump between text fields.", _textField.Text);
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void CursorPosition_With_Value_Greater_Than_Text_Length_Changes_To_Text_Length ()
{
_textField.CursorPosition = 33;
Assert.Equal (32, _textField.CursorPosition);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void CursorPosition_With_Value_Less_Than_Zero_Changes_To_Zero ()
{
_textField.CursorPosition = -1;
Assert.Equal (0, _textField.CursorPosition);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
}
[Fact]
[AutoInitShutdown]
public void DeleteSelectedText_InsertText_DeleteCharLeft_DeleteCharRight_Cut ()
{
var newText = "";
var oldText = "";
var tf = new TextField { Width = 10, Text = "-1" };
tf.TextChanging += (s, e) =>
{
newText = e.Result;
oldText = tf.Text;
};
var top = new Toplevel ();
top.Add (tf);
Application.Begin (top);
Assert.Equal ("-1", tf.Text);
// InsertText
tf.SelectedStart = 1;
tf.CursorPosition = 2;
Assert.Equal (1, tf.SelectedLength);
Assert.Equal ("1", tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (Key.D2));
Assert.Equal ("-2", newText);
Assert.Equal ("-1", oldText);
Assert.Equal ("-2", tf.Text);
// DeleteCharLeft
tf.SelectedStart = 1;
tf.CursorPosition = 2;
Assert.Equal (1, tf.SelectedLength);
Assert.Equal ("2", tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (Key.Backspace));
Assert.Equal ("-", newText);
Assert.Equal ("-2", oldText);
Assert.Equal ("-", tf.Text);
// DeleteCharRight
tf.Text = "-1";
tf.SelectedStart = 1;
tf.CursorPosition = 2;
Assert.Equal (1, tf.SelectedLength);
Assert.Equal ("1", tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (Key.Delete));
Assert.Equal ("-", newText);
Assert.Equal ("-1", oldText);
Assert.Equal ("-", tf.Text);
// Cut
tf.Text = "-1";
tf.SelectedStart = 1;
tf.CursorPosition = 2;
Assert.Equal (1, tf.SelectedLength);
Assert.Equal ("1", tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (Key.X.WithCtrl));
Assert.Equal ("-", newText);
Assert.Equal ("-1", oldText);
Assert.Equal ("-", tf.Text);
// Delete word with accented char
tf.Text = "Les Misérables movie.";
Assert.True (
tf.NewMouseEvent (
new () { Position = new (7, 1), Flags = MouseFlags.Button1DoubleClicked, View = tf }
)
);
Assert.Equal ("Misérables ", tf.SelectedText);
Assert.Equal (11, tf.SelectedLength);
Assert.True (tf.NewKeyDownEvent (Key.Delete));
Assert.Equal ("Les movie.", newText);
Assert.Equal ("Les Misérables movie.", oldText);
Assert.Equal ("Les movie.", tf.Text);
top.Dispose ();
}
[Fact]
[SetupFakeApplication]
public void KeyBindings_Command ()
{
var tf = new TextField { Width = 20, Text = "This is a test." };
tf.BeginInit ();
tf.EndInit ();
Assert.Equal (15, tf.Text.Length);
Assert.Equal (15, tf.CursorPosition);
Assert.False (tf.ReadOnly);
Assert.True (tf.NewKeyDownEvent (Key.Delete));
Assert.Equal ("This is a test.", tf.Text);
tf.CursorPosition = 0;
Assert.True (tf.NewKeyDownEvent (Key.Delete));
Assert.Equal ("his is a test.", tf.Text);
tf.ReadOnly = true;
Assert.True (tf.NewKeyDownEvent (Key.D.WithCtrl));
Assert.Equal ("his is a test.", tf.Text);
Assert.True (tf.NewKeyDownEvent (Key.Delete));
Assert.Equal ("his is a test.", tf.Text);
tf.ReadOnly = false;
tf.CursorPosition = 1;
Assert.True (tf.NewKeyDownEvent (Key.Backspace));
Assert.Equal ("is is a test.", tf.Text);
tf.CursorPosition = 5;
Assert.True (tf.NewKeyDownEvent (Key.Home.WithShift));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal ("is is", tf.SelectedText);
tf.CursorPosition = 5;
tf.SelectedStart = -1;
Assert.Null (tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (Key.Home.WithShift.WithCtrl));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal ("is is", tf.SelectedText);
tf.CursorPosition = 5;
tf.SelectedStart = -1;
Assert.Null (tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (Key.A.WithShift.WithCtrl));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal ("is is", tf.SelectedText);
tf.CursorPosition = 5;
tf.SelectedStart = -1;
Assert.Null (tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (Key.End.WithShift));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal (" a test.", tf.SelectedText);
tf.CursorPosition = 5;
tf.SelectedStart = -1;
Assert.Null (tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (Key.End.WithShift.WithCtrl));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal (" a test.", tf.SelectedText);
tf.CursorPosition = 5;
tf.SelectedStart = -1;
Assert.Null (tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (Key.E.WithShift.WithCtrl));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal (" a test.", tf.SelectedText);
tf.CursorPosition = 5;
tf.SelectedStart = -1;
Assert.Null (tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (Key.Home));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal (0, tf.CursorPosition);
tf.CursorPosition = 5;
Assert.Null (tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (Key.Home.WithCtrl));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal (0, tf.CursorPosition);
tf.CursorPosition = 5;
Assert.Null (tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (Key.A.WithCtrl));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal (tf.Text.Length, tf.CursorPosition);
tf.CursorPosition = 5;
tf.SelectedStart = -1;
Assert.Null (tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithShift));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal ("s", tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (Key.CursorUp.WithShift));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal ("is", tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithShift));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal ("s", tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (Key.CursorDown.WithShift));
Assert.Equal ("is is a test.", tf.Text);
Assert.Null (tf.SelectedText);
tf.CursorPosition = 7;
tf.SelectedStart = -1;
Assert.Null (tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithShift.WithCtrl));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal ("a", tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (Key.CursorUp.WithShift.WithCtrl));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal ("is a", tf.SelectedText);
#if UNIX_KEY_BINDINGS
Assert.True (tf.NewKeyDownEvent (Key.B.WithShift.WithAlt));
#else
Assert.True (tf.NewKeyDownEvent (Key.CursorUp.WithShift.WithCtrl));
#endif
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal ("is is a", tf.SelectedText);
tf.CursorPosition = 3;
tf.SelectedStart = -1;
Assert.Null (tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithShift.WithCtrl));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal ("is ", tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (Key.CursorDown.WithShift.WithCtrl));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal ("is a ", tf.SelectedText);
#if UNIX_KEY_BINDINGS
Assert.True (tf.NewKeyDownEvent (Key.F.WithShift.WithAlt));
#else
Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithShift.WithCtrl));
#endif
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal ("is a test", tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithShift.WithCtrl));
Assert.Equal ("is a test.", tf.SelectedText);
Assert.Equal (13, tf.CursorPosition);
Assert.True (tf.NewKeyDownEvent (Key.CursorLeft));
Assert.Equal ("is is a test.", tf.Text);
Assert.Null (tf.SelectedText);
Assert.Equal (12, tf.CursorPosition);
Assert.True (tf.NewKeyDownEvent (Key.CursorLeft));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal (11, tf.CursorPosition);
Assert.True (tf.NewKeyDownEvent (Key.End));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal (13, tf.CursorPosition);
tf.CursorPosition = 0;
Assert.True (tf.NewKeyDownEvent (Key.End.WithCtrl));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal (13, tf.CursorPosition);
tf.CursorPosition = 0;
Assert.True (tf.NewKeyDownEvent (Key.E.WithCtrl));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal (13, tf.CursorPosition);
tf.CursorPosition = 0;
Assert.True (tf.NewKeyDownEvent (Key.CursorRight));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal (1, tf.CursorPosition);
Assert.True (tf.NewKeyDownEvent (Key.F.WithCtrl));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal (2, tf.CursorPosition);
tf.CursorPosition = 9;
tf.ReadOnly = true;
Assert.True (tf.NewKeyDownEvent (Key.K.WithCtrl));
Assert.Equal ("is is a test.", tf.Text);
tf.ReadOnly = false;
Assert.True (tf.NewKeyDownEvent (Key.K.WithCtrl));
Assert.Equal ("is is a t", tf.Text);
Assert.Equal ("est.", Clipboard.Contents);
Assert.True (tf.NewKeyDownEvent (Key.Z.WithCtrl));
Assert.Equal ("is is a test.", tf.Text);
Assert.True (tf.NewKeyDownEvent (Key.Y.WithCtrl));
Assert.Equal ("is is a t", tf.Text);
#if UNIX_KEY_BINDINGS
Assert.True (tf.NewKeyDownEvent (Key.Backspace.WithAlt));
#else
Assert.True (tf.NewKeyDownEvent (Key.Z.WithCtrl));
#endif
Assert.Equal ("is is a test.", tf.Text);
Assert.True (tf.NewKeyDownEvent (Key.Y.WithCtrl));
Assert.Equal ("is is a t", tf.Text);
Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl));
Assert.Equal ("is is a t", tf.Text);
Assert.Equal (8, tf.CursorPosition);
Assert.True (tf.NewKeyDownEvent (Key.CursorUp.WithCtrl));
Assert.Equal ("is is a t", tf.Text);
Assert.Equal (6, tf.CursorPosition);
#if UNIX_KEY_BINDINGS
Assert.True (tf.NewKeyDownEvent (Key.B.WithAlt));
#else
Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl));
#endif
Assert.Equal ("is is a t", tf.Text);
Assert.Equal (3, tf.CursorPosition);
Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithCtrl));
Assert.Equal ("is is a t", tf.Text);
Assert.Equal (6, tf.CursorPosition);
Assert.True (tf.NewKeyDownEvent (Key.CursorDown.WithCtrl));
Assert.Equal ("is is a t", tf.Text);
Assert.Equal (8, tf.CursorPosition);
#if UNIX_KEY_BINDINGS
Assert.True (tf.NewKeyDownEvent (Key.F.WithAlt));
#else
Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithCtrl));
#endif
Assert.Equal ("is is a t", tf.Text);
Assert.Equal (9, tf.CursorPosition);
Assert.True (tf.Used);
Assert.True (tf.NewKeyDownEvent (Key.InsertChar));
Assert.Equal ("is is a t", tf.Text);
Assert.Equal (9, tf.CursorPosition);
Assert.False (tf.Used);
tf.SelectedStart = 3;
tf.CursorPosition = 7;
Assert.Equal ("is a", tf.SelectedText);
Assert.Equal ("est.", Clipboard.Contents);
Assert.True (tf.NewKeyDownEvent (Key.C.WithCtrl));
Assert.Equal ("is is a t", tf.Text);
Assert.Equal ("is a", Clipboard.Contents);
Assert.True (tf.NewKeyDownEvent (Key.X.WithCtrl));
Assert.Equal ("is t", tf.Text);
Assert.Equal ("is a", Clipboard.Contents);
Assert.True (tf.NewKeyDownEvent (Key.V.WithCtrl));
Assert.Equal ("is is a t", tf.Text);
Assert.Equal ("is a", Clipboard.Contents);
Assert.Equal (7, tf.CursorPosition);
#if UNIX_KEY_BINDINGS
Assert.True (tf.NewKeyDownEvent (Key.K.WithAlt));
#else
Assert.True (tf.NewKeyDownEvent (Key.K.WithCtrl.WithShift));
#endif
Assert.Equal (" t", tf.Text);
Assert.Equal ("is is a", Clipboard.Contents);
tf.Text = "TAB to jump between text fields.";
Assert.Equal (0, tf.CursorPosition);
Assert.True (tf.NewKeyDownEvent (Key.Delete.WithCtrl));
Assert.Equal ("to jump between text fields.", tf.Text);
tf.CursorPosition = tf.Text.Length;
Assert.True (tf.NewKeyDownEvent (Key.Backspace.WithCtrl));
Assert.Equal ("to jump between text fields", tf.Text);
Assert.True (tf.NewKeyDownEvent (Key.A.WithCtrl));
Assert.Equal ("to jump between text fields", tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (Key.D.WithCtrl.WithShift));
Assert.Equal ("", tf.Text);
}
[Theory]
[InlineData (false, 1)]
[InlineData (true, 0)]
public void Accepted_Handler_Handled_Prevents_Default_Button_Accept (bool handleAccept, int expectedButtonAccepts)
{
var superView = new Window
{
Id = "superView"
};
var tf = new TextField
{
Id = "tf"
};
var button = new Button
{
Id = "button",
IsDefault = true
};
superView.Add (tf, button);
var buttonAccept = 0;
button.Accepting += ButtonAccept;
var textFieldAccept = 0;
tf.Accepting += TextFieldAccept;
tf.SetFocus ();
Assert.True (tf.HasFocus);
superView.NewKeyDownEvent (Key.Enter);
Assert.Equal (1, textFieldAccept);
Assert.Equal (expectedButtonAccepts, buttonAccept);
button.SetFocus ();
superView.NewKeyDownEvent (Key.Enter);
Assert.Equal (1, textFieldAccept);
Assert.Equal (expectedButtonAccepts + 1, buttonAccept);
return;
void TextFieldAccept (object sender, CommandEventArgs e)
{
textFieldAccept++;
e.Handled = handleAccept;
}
void ButtonAccept (object sender, CommandEventArgs e) { buttonAccept++; }
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void Paste_Always_Clear_The_SelectedText ()
{
_textField.SelectedStart = 20;
_textField.CursorPosition = 24;
_textField.Copy ();
Assert.Equal ("text", _textField.SelectedText);
_textField.Paste ();
Assert.Null (_textField.SelectedText);
}
[Fact]
[AutoInitShutdown]
public void ScrollOffset_Initialize ()
{
var tf = new TextField { X = 1, Y = 1, Width = 20, Text = "Testing Scrolls." };
tf.BeginInit ();
tf.EndInit ();
Assert.Equal (0, tf.ScrollOffset);
Assert.Equal (16, tf.CursorPosition);
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void Selected_Text_Shows ()
{
// Proves #3022 is fixed (TextField selected text does not show in v2)
_textField.CursorPosition = 0;
var top = new Toplevel ();
top.Add (_textField);
SessionToken rs = Application.Begin (top);
Attribute [] attributes =
{
_textField.GetAttributeForRole (VisualRole.Focus),
new (
_textField.GetAttributeForRole (VisualRole.Focus).Background,
_textField.GetAttributeForRole (VisualRole.Focus).Foreground
)
};
// TAB to jump between text fields.
DriverAssert.AssertDriverAttributesAre ("0000000", output, Application.Driver, attributes);
// Cursor is at the end
Assert.Equal (32, _textField.CursorPosition);
_textField.CursorPosition = 0;
_textField.NewKeyDownEvent (Key.CursorRight.WithCtrl.WithShift);
AutoInitShutdownAttribute.RunIteration ();
Assert.Equal (4, _textField.CursorPosition);
// TAB to jump between text fields.
DriverAssert.AssertDriverAttributesAre ("1111000", output, Application.Driver, attributes);
top.Dispose ();
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void SelectedStart_And_CursorPosition_With_Value_Greater_Than_Text_Length_Changes_Both_To_Text_Length ()
{
_textField.CursorPosition = 33;
_textField.SelectedStart = 33;
Assert.Equal (32, _textField.CursorPosition);
Assert.Equal (32, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void SelectedStart_Greater_Than_CursorPosition_All_Selection_Is_Overwritten_On_Typing ()
{
_textField.SelectedStart = 19;
_textField.CursorPosition = 12;
Assert.Equal ("TAB to jump between text fields.", _textField.Text);
_textField.NewKeyDownEvent (Key.U); // u
Assert.Equal ("TAB to jump u text fields.", _textField.Text);
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void SelectedStart_With_Value_Greater_Than_Text_Length_Changes_To_Text_Length ()
{
_textField.CursorPosition = 2;
_textField.SelectedStart = 33;
Assert.Equal (32, _textField.SelectedStart);
Assert.Equal (30, _textField.SelectedLength);
Assert.Equal ("B to jump between text fields.", _textField.SelectedText);
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void SelectedStart_With_Value_Less_Than_Minus_One_Changes_To_Minus_One ()
{
_textField.SelectedStart = -2;
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
}
[Fact]
[AutoInitShutdown]
public void MouseEvent_Handled_Prevents_RightClick ()
{
Application.MouseEvent += HandleRightClick;
var tf = new TextField { Width = 10 };
var clickCounter = 0;
tf.MouseClick += (s, m) => { clickCounter++; };
var top = new Toplevel ();
top.Add (tf);
Application.Begin (top);
var mouseEvent = new MouseEventArgs { Flags = MouseFlags.Button1Clicked, View = tf };
Application.RaiseMouseEvent (mouseEvent);
Assert.Equal (1, clickCounter);
// Get a fresh instance that represents a right click.
// Should be ignored because of SuppressRightClick callback
mouseEvent = new () { Flags = MouseFlags.Button3Clicked, View = tf };
Application.RaiseMouseEvent (mouseEvent);
Assert.Equal (1, clickCounter);
Application.MouseEvent -= HandleRightClick;
// Get a fresh instance that represents a right click.
// Should no longer be ignored as the callback was removed
mouseEvent = new () { Flags = MouseFlags.Button3Clicked, View = tf };
// In #3183 OnMouseClicked is no longer called before MouseEvent().
// This call causes the context menu to pop, and MouseEvent() returns true.
// Thus, the clickCounter is NOT incremented.
// Which is correct, because the user did NOT click with the left mouse button.
Application.RaiseMouseEvent (mouseEvent);
Assert.Equal (1, clickCounter);
top.Dispose ();
return;
void HandleRightClick (object sender, MouseEventArgs arg)
{
if (arg.Flags.HasFlag (MouseFlags.Button3Clicked))
{
arg.Handled = true;
}
}
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void Text_Replaces_Tabs_With_Empty_String ()
{
_textField.Text = "\t\tTAB to jump between text fields.";
Assert.Equal ("TAB to jump between text fields.", _textField.Text);
_textField.Text = "";
Clipboard.Contents = "\t\tTAB to jump between text fields.";
_textField.Paste ();
Assert.Equal ("TAB to jump between text fields.", _textField.Text);
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void TextChanged_Event ()
{
var eventFired = false;
_textField.TextChanged += (s, e) => eventFired = true;
_textField.Text = "changed";
Assert.True (eventFired);
Assert.Equal ("changed", _textField.Text);
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void TextChanging_Event ()
{
var cancel = true;
_textField.TextChanging += (s, e) =>
{
Assert.Equal ("changing", e.Result);
if (cancel)
{
e.Handled = true;
}
};
_textField.Text = "changing";
Assert.Equal ("TAB to jump between text fields.", _textField.Text);
cancel = false;
_textField.Text = "changing";
Assert.Equal ("changing", _textField.Text);
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void Used_Is_False ()
{
_textField.Used = false;
_textField.CursorPosition = 10;
Assert.Equal ("TAB to jump between text fields.", _textField.Text);
_textField.NewKeyDownEvent (Key.U); // u
Assert.Equal ("TAB to jumu between text fields.", _textField.Text);
_textField.NewKeyDownEvent (Key.S); // s
Assert.Equal ("TAB to jumusbetween text fields.", _textField.Text);
_textField.NewKeyDownEvent (Key.E); // e
Assert.Equal ("TAB to jumuseetween text fields.", _textField.Text);
_textField.NewKeyDownEvent (Key.D); // d
Assert.Equal ("TAB to jumusedtween text fields.", _textField.Text);
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void Used_Is_True_By_Default ()
{
_textField.CursorPosition = 10;
Assert.Equal ("TAB to jump between text fields.", _textField.Text);
_textField.NewKeyDownEvent (Key.U); // u
Assert.Equal ("TAB to jumup between text fields.", _textField.Text);
_textField.NewKeyDownEvent (Key.S); // s
Assert.Equal ("TAB to jumusp between text fields.", _textField.Text);
_textField.NewKeyDownEvent (Key.E); // e
Assert.Equal ("TAB to jumusep between text fields.", _textField.Text);
_textField.NewKeyDownEvent (Key.D); // d
Assert.Equal ("TAB to jumusedp between text fields.", _textField.Text);
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void WordBackward_With_No_Selection ()
{
_textField.CursorPosition = _textField.Text.Length;
var iteration = 0;
while (_textField.CursorPosition > 0)
{
_textField.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
switch (iteration)
{
case 0:
Assert.Equal (31, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 1:
Assert.Equal (25, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 2:
Assert.Equal (20, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 3:
Assert.Equal (12, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 4:
Assert.Equal (7, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 5:
Assert.Equal (4, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 6:
Assert.Equal (0, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
}
iteration++;
}
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void WordBackward_With_No_Selection_And_With_More_Than_Only_One_Whitespace_And_With_Only_One_Letter ()
{
// 1 2 3 4 5
// 0123456789012345678901234567890123456789012345678901234=55 (Length)
_textField.Text = "TAB t o jump b etween t ext f ields .";
_textField.CursorPosition = _textField.Text.Length;
var iteration = 0;
while (_textField.CursorPosition > 0)
{
_textField.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
switch (iteration)
{
case 0:
Assert.Equal (54, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 1:
Assert.Equal (48, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 2:
Assert.Equal (46, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 3:
Assert.Equal (40, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 4:
Assert.Equal (38, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 5:
Assert.Equal (28, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 6:
Assert.Equal (25, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 7:
Assert.Equal (12, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 8:
Assert.Equal (9, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 9:
Assert.Equal (6, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 10:
Assert.Equal (0, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
}
iteration++;
}
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void WordBackward_With_Selection ()
{
_textField.CursorPosition = _textField.Text.Length;
_textField.SelectedStart = _textField.Text.Length;
var iteration = 0;
while (_textField.CursorPosition > 0)
{
_textField.NewKeyDownEvent (
new (
KeyCode.CursorLeft | KeyCode.CtrlMask | KeyCode.ShiftMask
)
);
switch (iteration)
{
case 0:
Assert.Equal (31, _textField.CursorPosition);
Assert.Equal (32, _textField.SelectedStart);
Assert.Equal (1, _textField.SelectedLength);
Assert.Equal (".", _textField.SelectedText);
break;
case 1:
Assert.Equal (25, _textField.CursorPosition);
Assert.Equal (32, _textField.SelectedStart);
Assert.Equal (7, _textField.SelectedLength);
Assert.Equal ("fields.", _textField.SelectedText);
break;
case 2:
Assert.Equal (20, _textField.CursorPosition);
Assert.Equal (32, _textField.SelectedStart);
Assert.Equal (12, _textField.SelectedLength);
Assert.Equal ("text fields.", _textField.SelectedText);
break;
case 3:
Assert.Equal (12, _textField.CursorPosition);
Assert.Equal (32, _textField.SelectedStart);
Assert.Equal (20, _textField.SelectedLength);
Assert.Equal ("between text fields.", _textField.SelectedText);
break;
case 4:
Assert.Equal (7, _textField.CursorPosition);
Assert.Equal (32, _textField.SelectedStart);
Assert.Equal (25, _textField.SelectedLength);
Assert.Equal ("jump between text fields.", _textField.SelectedText);
break;
case 5:
Assert.Equal (4, _textField.CursorPosition);
Assert.Equal (32, _textField.SelectedStart);
Assert.Equal (28, _textField.SelectedLength);
Assert.Equal ("to jump between text fields.", _textField.SelectedText);
break;
case 6:
Assert.Equal (0, _textField.CursorPosition);
Assert.Equal (32, _textField.SelectedStart);
Assert.Equal (32, _textField.SelectedLength);
Assert.Equal ("TAB to jump between text fields.", _textField.SelectedText);
break;
}
iteration++;
}
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void
WordBackward_With_The_Same_Values_For_SelectedStart_And_CursorPosition_And_Not_Starting_At_Beginning_Of_The_Text ()
{
_textField.CursorPosition = 10;
_textField.SelectedStart = 10;
var iteration = 0;
while (_textField.CursorPosition > 0)
{
_textField.NewKeyDownEvent (
new (
KeyCode.CursorLeft | KeyCode.CtrlMask | KeyCode.ShiftMask
)
);
switch (iteration)
{
case 0:
Assert.Equal (7, _textField.CursorPosition);
Assert.Equal (10, _textField.SelectedStart);
Assert.Equal (3, _textField.SelectedLength);
Assert.Equal ("jum", _textField.SelectedText);
break;
case 1:
Assert.Equal (4, _textField.CursorPosition);
Assert.Equal (10, _textField.SelectedStart);
Assert.Equal (6, _textField.SelectedLength);
Assert.Equal ("to jum", _textField.SelectedText);
break;
case 2:
Assert.Equal (0, _textField.CursorPosition);
Assert.Equal (10, _textField.SelectedStart);
Assert.Equal (10, _textField.SelectedLength);
Assert.Equal ("TAB to jum", _textField.SelectedText);
break;
}
iteration++;
}
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void WordForward_With_No_Selection ()
{
_textField.CursorPosition = 0;
var iteration = 0;
while (_textField.CursorPosition < _textField.Text.Length)
{
_textField.NewKeyDownEvent (Key.CursorRight.WithCtrl);
switch (iteration)
{
case 0:
Assert.Equal (4, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 1:
Assert.Equal (7, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 2:
Assert.Equal (12, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 3:
Assert.Equal (20, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 4:
Assert.Equal (25, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 5:
Assert.Equal (31, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 6:
Assert.Equal (32, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
}
iteration++;
}
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void WordForward_With_No_Selection_And_With_More_Than_Only_One_Whitespace_And_With_Only_One_Letter ()
{
// 1 2 3 4 5
// 0123456789012345678901234567890123456789012345678901234=55 (Length)
_textField.Text = "TAB t o jump b etween t ext f ields .";
_textField.CursorPosition = 0;
var iteration = 0;
while (_textField.CursorPosition < _textField.Text.Length)
{
_textField.NewKeyDownEvent (Key.CursorRight.WithCtrl);
switch (iteration)
{
case 0:
Assert.Equal (6, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 1:
Assert.Equal (9, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 2:
Assert.Equal (12, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 3:
Assert.Equal (25, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 4:
Assert.Equal (28, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 5:
Assert.Equal (38, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 6:
Assert.Equal (40, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 7:
Assert.Equal (46, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 8:
Assert.Equal (48, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 9:
Assert.Equal (54, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
case 10:
Assert.Equal (55, _textField.CursorPosition);
Assert.Equal (-1, _textField.SelectedStart);
Assert.Equal (0, _textField.SelectedLength);
Assert.Null (_textField.SelectedText);
break;
}
iteration++;
}
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void WordForward_With_Selection ()
{
_textField.CursorPosition = 0;
_textField.SelectedStart = 0;
var iteration = 0;
while (_textField.CursorPosition < _textField.Text.Length)
{
_textField.NewKeyDownEvent (Key.CursorRight.WithCtrl.WithShift);
switch (iteration)
{
case 0:
Assert.Equal (4, _textField.CursorPosition);
Assert.Equal (0, _textField.SelectedStart);
Assert.Equal (4, _textField.SelectedLength);
Assert.Equal ("TAB ", _textField.SelectedText);
break;
case 1:
Assert.Equal (7, _textField.CursorPosition);
Assert.Equal (0, _textField.SelectedStart);
Assert.Equal (7, _textField.SelectedLength);
Assert.Equal ("TAB to ", _textField.SelectedText);
break;
case 2:
Assert.Equal (12, _textField.CursorPosition);
Assert.Equal (0, _textField.SelectedStart);
Assert.Equal (12, _textField.SelectedLength);
Assert.Equal ("TAB to jump ", _textField.SelectedText);
break;
case 3:
Assert.Equal (20, _textField.CursorPosition);
Assert.Equal (0, _textField.SelectedStart);
Assert.Equal (20, _textField.SelectedLength);
Assert.Equal ("TAB to jump between ", _textField.SelectedText);
break;
case 4:
Assert.Equal (25, _textField.CursorPosition);
Assert.Equal (0, _textField.SelectedStart);
Assert.Equal (25, _textField.SelectedLength);
Assert.Equal ("TAB to jump between text ", _textField.SelectedText);
break;
case 5:
Assert.Equal (31, _textField.CursorPosition);
Assert.Equal (0, _textField.SelectedStart);
Assert.Equal (31, _textField.SelectedLength);
Assert.Equal ("TAB to jump between text fields", _textField.SelectedText);
break;
case 6:
Assert.Equal (32, _textField.CursorPosition);
Assert.Equal (0, _textField.SelectedStart);
Assert.Equal (32, _textField.SelectedLength);
Assert.Equal ("TAB to jump between text fields.", _textField.SelectedText);
break;
}
iteration++;
}
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void
WordForward_With_The_Same_Values_For_SelectedStart_And_CursorPosition_And_Not_Starting_At_Beginning_Of_The_Text ()
{
_textField.CursorPosition = 10;
_textField.SelectedStart = 10;
var iteration = 0;
while (_textField.CursorPosition < _textField.Text.Length)
{
_textField.NewKeyDownEvent (Key.CursorRight.WithCtrl.WithShift);
switch (iteration)
{
case 0:
Assert.Equal (12, _textField.CursorPosition);
Assert.Equal (10, _textField.SelectedStart);
Assert.Equal (2, _textField.SelectedLength);
Assert.Equal ("p ", _textField.SelectedText);
break;
case 1:
Assert.Equal (20, _textField.CursorPosition);
Assert.Equal (10, _textField.SelectedStart);
Assert.Equal (10, _textField.SelectedLength);
Assert.Equal ("p between ", _textField.SelectedText);
break;
case 2:
Assert.Equal (25, _textField.CursorPosition);
Assert.Equal (10, _textField.SelectedStart);
Assert.Equal (15, _textField.SelectedLength);
Assert.Equal ("p between text ", _textField.SelectedText);
break;
case 3:
Assert.Equal (31, _textField.CursorPosition);
Assert.Equal (10, _textField.SelectedStart);
Assert.Equal (21, _textField.SelectedLength);
Assert.Equal ("p between text fields", _textField.SelectedText);
break;
case 4:
Assert.Equal (32, _textField.CursorPosition);
Assert.Equal (10, _textField.SelectedStart);
Assert.Equal (22, _textField.SelectedLength);
Assert.Equal ("p between text fields.", _textField.SelectedText);
break;
}
iteration++;
}
}
[Fact]
[SetupFakeApplication]
public void Words_With_Accents_Incorrect_Order_Will_Result_With_Wrong_Accent_Place ()
{
var tf = new TextField
{
Driver = ApplicationImpl.Instance.Driver,
Width = 30, Text = "Les Misérables"
};
tf.SetRelativeLayout (new (100, 100));
tf.Draw ();
DriverAssert.AssertDriverContentsWithFrameAre (
@"
Les Misérables",
output
);
tf.Text = "Les Mise" + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber)) + "rables";
tf.Draw ();
DriverAssert.AssertDriverContentsWithFrameAre (
@"
Les Misérables",
output
);
// incorrect order will result with a wrong accent place
tf.Text = "Les Mis" + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber)) + "erables";
tf.SetClipToScreen ();
tf.Draw ();
DriverAssert.AssertDriverContentsWithFrameAre (
@"
Les Miśerables",
output
);
}
private TextField GetTextFieldsInView ()
{
var tf = new TextField { Width = 10 };
var tf2 = new TextField { Y = 1, Width = 10 };
Toplevel top = new ();
top.Add (tf);
top.Add (tf2);
Application.Begin (top);
Assert.Same (tf, top.Focused);
return tf;
}
// This class enables test functions annotated with the [InitShutdown] attribute
// to have a function called before the test function is called and after.
//
// This is necessary because a) Application is a singleton and Init/Shutdown must be called
// as a pair, and b) all unit test functions should be atomic.
[AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]
public class TextFieldTestsAutoInitShutdown : AutoInitShutdownAttribute
{
public override void After (MethodInfo methodUnderTest)
{
_textField.Dispose ();
_textField = null;
base.After (methodUnderTest);
}
public override void Before (MethodInfo methodUnderTest)
{
base.Before (methodUnderTest);
//Application.Current.Scheme = Colors.Schemes ["Base"];
_textField = new ()
{
// 1 2 3
// 01234567890123456789012345678901=32 (Length)
Text = "TAB to jump between text fields.",
Width = 32
};
}
}
[Fact]
[AutoInitShutdown]
public void Draw_Esc_Rune ()
{
var tf = new TextField
{
Driver = ApplicationImpl.Instance.Driver,
Width = 5, Text = "\u001b"
};
tf.BeginInit ();
tf.EndInit ();
tf.Draw ();
DriverAssert.AssertDriverContentsWithFrameAre ("\u241b", output);
tf.Dispose ();
}
}