mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-26 15:57:56 +01:00
* WIP: Broken
* Got working. Mostly.
* Parllel tests pass
* More progres
* Fixed app tests.
* Mouse
* more progress.
* working on shortcut
* Shortcut accept on ENTER is broken.
* One left...
* More test progress.
* All unit tests pass. Still some issues though.
* tweak
* Fixed Integration Tests
* Fixed UI Catalog
* Tweaking CP to try to find race condition
* Refactor StandardColors and improve ColorPicker logic
Refactored `StandardColors` to use lazy initialization for static fields, improving performance and avoiding static constructor convoy effects. Introduced `NamesValueFactory` and `MapValueFactory` methods for encapsulated initialization logic.
Simplified `GetColorNames` to directly return `_names.Value`. Improved `TryParseColor` by clarifying default value usage and adopting object initializer syntax. Updated `TryNameColor` to use `_argbNameMap.Value`.
Refactored `GetArgb` for better readability. Replaced `MultiStandardColorNameResolver` with `StandardColorsNameResolver` in `ColorPicker`. Commented out `app.Init("Fake")` in `ColorPickerTests` for testing purposes.
Made minor formatting improvements, including updated comments and XML documentation for consistency.
* revert
* Throttle input loop to prevent CPU spinning
Introduce a 20ms delay in the input loop of `InputImpl<TInputRecord>`
to prevent excessive CPU usage when no input is available. Removed
the `DateTime dt = Now();` line and the `while (Peek())` block, which
previously enqueued input records.
This change improves resource management, especially in scenarios
where multiple `ApplicationImpl` instances are created in parallel
tests without calling `Shutdown()`. It prevents thread pool
exhaustion and ensures better performance in such cases.
* Refactor ApplicationImpl to use IDisposable pattern
Implemented the IDisposable pattern in ApplicationImpl to improve resource management. Added `Dispose` and `DisposeCore` methods, and marked the `Shutdown` method as obsolete, encouraging the use of `Dispose` or `using` statements instead. Updated the `IApplication` interface to inherit from IDisposable and added `GetResult` methods for retrieving run session results.
Refactored unit tests to adopt the new lifecycle management approach, replacing legacy `Shutdown` calls with `Dispose` or `using`. Removed fragile and obsolete tests, and re-enabled previously skipped tests after addressing underlying issues.
Updated `FakeApplicationLifecycle` and `SetupFakeApplicationAttribute` to align with the new disposal pattern. Improved documentation and examples to guide users toward modern usage patterns. Maintained backward compatibility for legacy singleton usage.
* Add IDisposable pattern with input loop throttling
- Add IDisposable to IApplication for proper resource cleanup
- Add 20ms throttle to input loop (prevents CPU spinning)
- Add Lazy<T> to StandardColors (eliminates convoy effect)
- Add MainLoopCoordinatorTests suite (5 new tests)
- Add Dispose() calls to all 16 ColorPickerTests
- Mark Application.Shutdown() as [Obsolete]
IApplication now requires Dispose() for cleanup
Performance: 100x CPU reduction, 15x faster disposal, tests complete in <5s
Fixes: Thread leaks, CPU saturation, test hangs in parallel execution
Docs: Updated application.md and newinv2.md with disposal patterns
* Refactor test for input loop throttling clarity
Updated `InputLoop_Throttle_Limits_Poll_Rate` test to improve clarity, reliability, and efficiency:
- Rewrote summary comment to clarify purpose and emphasize the 20ms throttle's role in preventing CPU spinning.
- Replaced `var` with explicit types for better readability.
- Reduced test duration from 1s to 500ms to improve test speed.
- Revised assertions:
- Replaced range-based assertion with upper-bound check to ensure poll count is below 500, avoiding timing sensitivity issues.
- Added assertion to verify the thread ran and was not immediately canceled.
- Added a 2-second timeout to `inputTask.Wait` and verified task completion.
- Improved comments to explain test behavior and reasoning behind changes.
* tweaks
* Fix nullabiltiy stuff.
* runnable fixes
* more nullabe
* More nullability
* warnings gone
* Fixed fluent test failure.
* Refactor ApplicationImpl and update Runnable layout logic
Refactored `ApplicationImpl.Run.cs` for improved readability and
atomicity:
- Combined `if (wasModal)` with `SessionStack?.TryPop` to streamline
logic.
- Simplified restoration of `previousRunnable` by reducing nesting.
- Updated comments for clarity and retained `SetIsModal` call.
Simplified focus-setting logic in `ApplicationImpl.Run.cs` using
pattern matching for `TopRunnableView`.
In `Runnable<TResult>`, added `SetNeedsLayout` after `IsModalChanged`
to ensure layout updates. Removed an unused empty line for cleanup.
Corrected namespace in `GetViewsUnderLocationForRootTests.cs` to
align with test structure.
* Update layout on modal state change
A call to `SetNeedsLayout()` was added to the `OnIsModalChanged`
method in the `Runnable` class. This ensures that the layout
is updated whenever the modal state changes.
* Increase test timeout for inputTask.Wait to 10 seconds
Extended the timeout duration for the `inputTask.Wait` method
from 4 seconds to 10 seconds in `MainLoopCoordinatorTests`.
This change ensures the test has a longer window to complete
under conditions of increased load or slower execution
environments, reducing the likelihood of false test failures.
* Refactor project files and simplify test logic
Removed `<LangVersion>` and `<ImplicitUsings>` properties from
`UnitTests.csproj` and `UnitTests.Parallelizable.csproj` to rely
on default SDK settings and disable implicit global usings.
Simplified the `SizeChanged_Event_Still_Fires_For_Compatibility`
test in `FakeDriverTests` by removing the `screenChangedFired`
variable, its associated event handler, and related assertions.
Also removed obsolete warning suppression directives as they
are no longer needed.
* Reduce UnitTestsParallelizable iterations from 10 to 3
Reduced the number of iterations for the UnitTestsParallelizable
test suite from 10 to 3 to save time and resources while still
exposing concurrency issues. Updated the loop and log messages
to reflect the new iteration count.
* disabled InputLoop_Throttle_Limits_Poll_Rate
* Refactor app lifecycle and improve Runnable API
Refactored `Program.cs` to simplify application lifecycle:
- Modularized app creation, initialization, and disposal.
- Improved result handling and ensured proper resource cleanup.
Re-implemented `Runnable<TResult>` with a cleaner design:
- Retained functionality while improving readability and structure.
- Added XML documentation and followed the Cancellable Work Pattern.
Re-implemented `RunnableWrapper<TView, TResult>`:
- Enabled wrapping any `View` to make it runnable with typed results.
- Added examples and remarks for better developer guidance.
Re-implemented `ViewRunnableExtensions`:
- Provided fluent API for making views runnable with or without results.
- Enhanced documentation with examples for common use cases.
General improvements:
- Enhanced code readability, maintainability, and error handling.
- Replaced redundant code with cleaner, more maintainable versions.
* Modernize codebase for Terminal.Gui and MVVM updates
Refactored `LoginView` to remove redundant `Application.LayoutAndDraw()`
call. Enhanced `LoginViewModel` with new observable properties for
automatic property change notifications. Updated `Message` class to use
nullable generics for improved type safety.
Replaced legacy `Application.Init()` and `Application.Run()` calls with
the modern `IApplication` API across `Program.cs`, `Example.cs`, and
`ReactiveExample`. Ensured proper disposal of `IApplication` instances
to prevent resource leaks.
Updated `TerminalScheduler` to integrate with `IApplication` for
invoking actions and managing timeouts. Added null checks and improved
timeout disposal logic for robustness.
Refactored `ExampleWindow` for better readability and alignment with
modern `Terminal.Gui` conventions. Cleaned up unused imports and
improved code clarity across the codebase.
Updated README.md to reflect the latest `Terminal.Gui` practices,
including examples of the `IApplication` API and automatic UI refresh
handling. Renamed `LoginAction` to `LoginActions` for consistency.
* Refactor: Transition to IRunnable-based architecture
Replaced `Toplevel` with `Window` as the primary top-level UI element. Introduced the `IRunnable` interface to modernize the architecture, enabling greater flexibility and testability. Deprecated the static `Application` class in favor of the instance-based `IApplication` model, which supports multiple application contexts.
Updated methods like `Application.Run()` and `Application.RequestStop()` to use `IRunnable`. Removed or replaced legacy `Modal` properties with `IsModal`. Enhanced the `IApplication` interface with a fluent API, including methods like `Run<TRunnable>()` and `GetResult<T>()`.
Refactored tests and examples to align with the new architecture. Updated documentation to reflect the instance-based model. Deprecated obsolete members and methods, including `Application.Current` and `Application.TopRunnable`.
Improved event handling by replacing the `Accept` event with `Accepting` and using `e.Handled` for event processing. Updated threading examples to use `App?.Invoke()` or `app.Invoke()` for UI updates. Cleaned up redundant code and redefined modal behavior for better consistency.
These changes modernize the `Terminal.Gui` library, improving clarity, usability, and maintainability while ensuring backward compatibility where possible.
* Refactor: Replace Toplevel with Runnable class
This commit introduces a major architectural update to the `Terminal.Gui` library, replacing the legacy `Toplevel` class with the new `Runnable` class. The changes span the entire codebase, including core functionality, tests, documentation, and configuration files.
- **Core Class Replacement**:
- Replaced `Toplevel` with `Runnable` as the base class for modal views and session management.
- Updated all references to `Toplevel` in the codebase, including constructors, methods, and properties.
- **Configuration Updates**:
- Updated `tui-config-schema.json` to reflect the new `Runnable` scheme.
- **New Classes**:
- Added `UICatalogRunnable` for managing the UI Catalog application.
- Introduced `Runnable<TResult>` as a generic base class for blocking sessions with result handling.
- **Documentation and Tests**:
- Updated documentation to emphasize `Runnable` and mark `Toplevel` as obsolete.
- Refactored test cases to use `Runnable` and ensure compatibility.
- **Behavioral Improvements**:
- Enhanced lifecycle management and alignment with the `IRunnable` interface.
- Improved clarity and consistency in naming conventions.
These changes modernize the library, improve flexibility, and provide a clearer architecture for developers.
* Refactor: Consolidate Runnable classes and decouple View from ApplicationImpl
- Made Runnable<TResult> inherit from Runnable (eliminating ~180 LOC duplication)
- Moved View init/layout/cursor logic from ApplicationImpl to Runnable lifecycle events
- ApplicationImpl.Begin now operates purely on IRunnable interface
Related to #4419
* Simplified the disposal logic in `ApplicationImpl.Run.cs` by replacing
the type-specific check for `View` with a more general check for
`IDisposable`. This ensures proper disposal of any `IDisposable`
object, improving robustness.
Removed the `FrameworkOwnedRunnable` property from the `ApplicationImpl`
class in `ApplicationImpl.cs` and the `IApplication` interface in
`IApplication.cs`. This eliminates the need to manage this property,
reducing complexity and improving maintainability.
Updated `application.md` to reflect the removal of the
`FrameworkOwnedRunnable` property, ensuring the documentation aligns
with the updated codebase.
* Replaces the legacy `Shutdown()` method with `Dispose()` to align
with the `IDisposable` pattern, ensuring proper resource cleanup
and simplifying the API. The `Dispose()` method is now the
recommended way to release resources, with `using` statements
encouraged for automatic disposal.
Key changes:
- Marked `Shutdown()` as obsolete; it now internally calls `Dispose()`.
- Updated the fluent API to remove `Shutdown()` from chaining.
- Enhanced session lifecycle management for thread safety.
- Updated tests to validate proper disposal and state reset.
- Improved `IRunnable` integration with automatic disposal for
framework-created runnables.
- Maintained backward compatibility for the legacy static
`Application` singleton.
- Refactored documentation and examples to reflect modern practices
and emphasize `Dispose()` usage.
These changes modernize the `Terminal.Gui` lifecycle, improve
testability, and encourage alignment with .NET conventions.
* Refactor runnable app context handling in ApplicationImpl
Refactor how the application context is set for `runnable` objects
by introducing a new `SetApp` method in the `IRunnable` interface.
This replaces the previous logic of directly setting the `App`
property for `View` objects, making the process more generic and
encapsulated within `IRunnable` implementations.
Simplify `Mouse.UngrabMouse()` by removing the conditional check
and calling it unconditionally.
Make a minor formatting adjustment in the generic constraint of
`Run<TRunnable>` in `ApplicationImpl`.
Add `SetApp(IApplication app)` to the `IRunnable` interface and
implement it in the `Runnable` class to set the `App` property
to the provided application instance.
* Improve docs, tests, and modularity across the codebase
Reorganized and updated `CONTRIBUTING.md`:
- Added **Key Architecture Concepts** section and reordered the table of contents.
- Updated testing requirements to discourage legacy patterns.
- Added instructions for replicating CI workflows locally.
- Clarified PR guidelines and coding style expectations.
Enhanced `README.md` with detailed CI/CD workflow documentation.
Refactored `ColorPicker.Prompt` to use `IApplication` for improved modularity and testability.
Introduced `IApplicationScreenChangedTests` for comprehensive testing of `ScreenChanged` events and `Screen` property.
Refactored `ApplicationScreenTests` and `TextView.PromptForColors` to align with modern patterns.
Updated `Terminal.sln` to include `.github/workflows/README.md`.
Performed general cleanup:
- Removed outdated documentation links.
- Improved XML documentation and coding consistency.
* readme tweaks
* Improve thread safety, layout, and test coverage
Refactored `OutputBufferImpl.cs` to enhance thread safety by locking shared resources and adding bounds checks for columns and rows. Improved handling of wide characters and removed outdated TODO comments.
Updated `Runnable.cs` to call `SetNeedsDraw()` on modal state changes, ensuring proper layout and drawing updates. Simplified layout handling in `ApplicationImpl.Run.cs` by replacing redundant comments with a `LayoutAndDraw()` call.
Added a check in `AllViewsTester.cs` to skip creating instances of `RunnableWrapper` types with unsatisfiable generic constraints, logging a warning when encountered.
Enhanced `ListViewTests.cs` by adding explicit `app.LayoutAndDraw()` calls to validate visual output and ensure tests reflect the updated application state.
These changes improve robustness, prevent race conditions, and ensure consistent behavior across the application.
* Refactor: Rename Toplevel to Runnable and update logic
Updated the `Border` class to use `Command.Quit` instead of
`Command.QuitToplevel` in the `CloseButton.Accept` handler.
Renamed test methods in `GetViewsAtLocationTests.cs` to replace
"Toplevel" with "Runnable" for consistency. Updated `Runnable<bool>`
instances to use "topRunnable" as the `Id` property.
These changes align the codebase with updated naming conventions
and improve clarity.
* Removed `ToplevelTests` and migrated relevant test cases to
`MouseDragTests` with improved structure and coverage. Updated
tests to use `Application.Create`, `app.Begin`, and `app.End`
for better resource management and lifecycle handling.
Replaced direct event handling with `app.Mouse.RaiseMouseEvent`
to align with the application's event-handling mechanism. Added
`Runnable` objects to ensure views are properly initialized and
disposed of within the application context.
Enhanced tests to include assertions for minimum width and
height constraints during resize operations. Removed redundant
tests and streamlined logic to reduce duplication and improve
maintainability.
* Reorged Unit Test namespaces.
* more
* Refactor tests and update namespaces for consistency
Updated namespaces in `ArrangementTests.cs` and `MouseDragTests.cs` for better organization. Enhanced `ArrangementTests.cs` with additional checks for arrangement flags. Reformatted and re-added `MouseDragTests.cs` and `SchemeTests.cs` with modern C# features like nullable annotations and object initializers. Ensured no functional changes while improving code clarity and consistency.
* Fix nullability warnings in MouseDragTests.cs
Updated `app.End` calls to use the null-forgiving operator (`!`)
on `app.SessionStack` to ensure it is treated as non-null.
This change addresses potential nullability warnings and
improves code safety and clarity. Applied consistently across
all relevant test cases in the `MouseDragTests` class.
1724 lines
63 KiB
C#
1724 lines
63 KiB
C#
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 ()
|
||
{
|
||
Runnable 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.TopRunnableView.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 ()));
|
||
Assert.Equal (10, caption.GetColumns ());
|
||
|
||
TextField tf = GetTextFieldsInView ();
|
||
|
||
tf.Title = caption;
|
||
Application.RaiseKeyDownEvent ('\t');
|
||
Assert.False (tf.HasFocus);
|
||
|
||
tf.Draw ();
|
||
DriverAssert.AssertDriverContentsAre ("Misérables", output);
|
||
Application.TopRunnableView.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.TopRunnableView.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.TopRunnableView.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.TopRunnableView.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.TopRunnableView.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.TopRunnableView.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 Runnable ();
|
||
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.", App = ApplicationImpl.Instance };
|
||
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 Runnable ();
|
||
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 Runnable ();
|
||
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 };
|
||
|
||
Runnable 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.TopRunnable.Scheme = Colors.Schemes ["Base"];
|
||
_textField = new ()
|
||
{
|
||
// 1 2 3
|
||
// 01234567890123456789012345678901=32 (Length)
|
||
Text = "TAB to jump between text fields.",
|
||
Width = 32,
|
||
App = ApplicationImpl.Instance
|
||
};
|
||
}
|
||
}
|
||
|
||
[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 ();
|
||
}
|
||
}
|