Files
Terminal.Gui/UnitTests/Views/TextFieldTests.cs
Tig 4430fe2cc6 Fixes #3192. Improve correctness / clarity of existing View.AutoSize functionality/unit tests (#3202)
* Removed resharper settings from editorconfig

* Remove constructors with frame parameters from Button class.

* Remove constructors with frame parameters from CheckBox class.

* Cleanup code.

* Remove constructors with frame parameters from ComboBox class.

* @BDisp
Remove constructors with frame parameters from FrameView class.

* Remove constructors with frame parameters from Label class.

* Remove constructors with frame parameters from ListView class.

* Remove constructors with frame parameters from ScrollBarView class.

* Remove constructors with frame parameters from ScrollView class.

* Remove namespace braces.

* Cleanup code.

* Cleanup code.

* Cleanup code.

* Remove constructors with frame parameters from TextField class.

* Remove constructors with frame parameters from TimeField class.

* Fixes #3182. OnResizeNeeded returns int.MaxValue and int.MaxValue when Application.Top is null, should return Size.Empty.

* Remove constructors with frame parameters from Toplevel class.

* Remove constructors with frame parameters from Window class.

* Fix merge errors.

* Revert "Fixes #3182. OnResizeNeeded returns int.MaxValue and int.MaxValue when Application.Top is null, should return Size.Empty."

This reverts commit cf9c24b846.

* Revert unit test.

* Fixes #2882. TabView: 'Frame.DrawFrame(Rect, bool)' is obsolete: 'This method is obsolete in v2. Use use LineCanvas or Frame (#2980)

* Fixes #2882. TabView: 'Frame.DrawFrame(Rect, bool)' is obsolete: 'This method is obsolete in v2. Use use LineCanvas or Frame

* Trying fix this unit test that sometimes fail.

* Fixes #2983. View need a alternative DrawFrame for the v2.

* Use new DrawFrame method.

* Change _lines field to Lines property.

* Add TabWindow unit test.

* Add DrawIncompleteFrame method and unit tests.

* Add more unit tests to LineCanvas.

* Fix newline conflict errors.

* Revert "Change _lines field to Lines property."

This reverts commit ab6c5f3094.

* Add DrawIncompleteFrame method and unit tests.

* Add more unit tests to LineCanvas.

* Fix newline conflict errors.

* Force render immediately instead of join.

* I will never rely on zero-location-based unit test again.

* Fix TestTreeViewColor unit test fail.

* Using location of 3 to avoid be divisible by 2 and so avoiding bugs.

* Revert "Using location of 3 to avoid be divisible by 2 and so avoiding bugs."

This reverts commit dd3df135d8.

* Revert "I will never rely on zero-location-based unit test again."

This reverts commit 62adf6f285.

* Revert "Fix newline conflict errors."

This reverts commit 4acf72612d.

* Revert "Add more unit tests to LineCanvas."

This reverts commit 66bc6f514e.

* Revert "Add DrawIncompleteFrame method and unit tests."

This reverts commit 680ba264e1.

* Resolving merge conflicts.

* Revert "Use new DrawFrame method."

This reverts commit 69a7f17f19.

* Revert "Fixes #2983. View need a alternative DrawFrame for the v2."

This reverts commit dade9fd767.

* Reverting this changes to start a new one.

* Add horizontal and vertical support for combining glyphs.

* Fix text and auto size behavior.

* Add TabWidth property.

* Add unit test for WordWrap.

* Fixes #3017. View TextDirection returns incorrect size on a vertical direction instance with AutoSize as false.

* Using Frame to force read from the get method.

* Fix some issues with AutoSize and ForceValidatePosDim.

* Fixing broken unit tests.

* Restoring code I've broken.

* Removing forgotten code.

* Only LayoutStyle.Computed can change the Frame.

* DateField and TimeField depends on LayoutStyle.Computed.

* Fix unit tests related with LayoutStyle.

* Implements tabs, left and right arrows as View.

* Draws a minimum full border.

* Adds missing XML parameter.

* Adds assert tests for Frame.

* Removes duplicates InlineData.

* Adds more unit tests for minimum full border without Left and Right thickness.

* Trying to fix the TestTreeViewColor unit test fail.

* Prevents a user to set TextDirection to -1.

* Prevents any invalid TextDirection value.

* Removes (TextDirection)(-1).

* Removes unnecessary TextDirection initialization.

* Removes LayoutStyle.

* Fixing unit tests with border.

* Trying to fix TestTreeViewColor again.

* Revert "Trying to fix TestTreeViewColor again."

This reverts commit c2efa8e42e.

* Trying to fix TestTreeViewColor again.

* Fix merge errors.

* Fix merge errors.

* Restoring unit test.

* Restores the right XML comment.

* Fix Disposing unit tests that sometimes throws because some instances aren't cleared on others unit tests classes.

* Fix Disposing unit tests that sometimes throws because some instances aren't cleared on others unit tests classes.

* Only call OnResizeNeeded if it's LayoutStyle.Computed.

* Fix merge errors.

* Fix merge errors.

* Fix unit tests fail.

* Reformat.

* Again.

* Rename to OnDrawAdornments.

* Fix failing unit tests.

* Reduces indentation and cleanup code.

* Cleanup code.

* Fix bug done when cleanup.

* Replace FrameHandledMouseEvent to AdornmentHandledMouseEvent.

* Removes Tab constructor parameters.

---------

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

* Fix merge errors.

* Remove constructors with parameters from Button.

* Remove parenthesis on objects initializers from Button.

* Remove constructors with parameters from CheckBox.

* Remove parenthesis on objects initializers from CheckBox.

* Remove constructors with parameters from ComboBox.

* Remove constructors with parameters from FrameView.

* Remove parenthesis on objects initializers from FrameView.

* Initial commit

* Renamed Direction enum for clarity in refactoring unit tests

* Moved nav tests to NavigationTests

* Moved view tests around

* Cleaning up TextFormatter and View.AutoSize code

* Fixed latent TextFormatter bug with \n

* removed Application dependency on some autosize unit tests

* Fixed Label tests to deal with auotsize overriding height/width

* Fixed more label tests. WIP

* Fixed all places where AutoSize = happend after setting Dims

* Started adding new primitive View.Text tests

* Code comments

* WIP: Enforce that it makes no sense to set Width/Height if AutoSize = true. Update Unit tests to match.

* WIP: Enforce that it makes no sense to set Width/Height if AutoSize = true. Update Unit tests to match.

* Remove frame set from the View constructor and prevent SetRelativeLayout running if not yet initialized.

* Changes needed for unit tests pass on remove parameters constructors from the Label class.

* Remove constructors with parameters from Label.

* Remove parenthesis on objects initializers from Label.

* Prefix private fields with underscore.

* Renamed to MaxLength.

* Remove constructors with parameters from ListView.

* MakeWrapper not needed anymore.

* Remove parenthesis on objects initializers from ListView.

* WIP: Enforce that it makes no sense to set Width/Height if AutoSize = true. Update Unit tests to match.

* Massive code cleanup - use parameterless constructors and ensure AutoSize is set properly. Code reformat.

* Massive code cleanup - use parameterless constructors and ensure AutoSize is set properly. Code reformat.

* Fixed messagebox

* Remove constructors with parameters from ScrollBarView and ScrollView.

* Remove parenthesis on objects initializers from ScrollBarView and ScrollView.

* Cleanup code.

* Fix merge errors.

* Add empty dotsettings for solution and projects.

* Set ReSharper language analysis level for projects to  C#12

* Make ReSharper consider itself the boss for style

* Add rule to enforce property backing fields above the property

* Disable auto-detection of naming rules so ReSharper doesn't change them by itself

* Don't let someone's VS settings override the indent settings

* Explicitly set tab width to 4 spaces and force spaces.

* Rules to keep various multi-line constructs aligned within themselves

* Curly brace rules (Using K&R style, per current project spec)

* Blank line rules

Mostly to add breaks in various situations, and also to enforce max of 1 blank line.

* Increase auto-wrap to 160 from default of 120

* Line break at end of all files, to make Unixy systems happy

* Keep attributes on their own lines except for methods and records that are themselves single-line

* Increase attribute auto-wrap to 60 from default of 38

* Wrap/chop rules for long method signatures and record declarations

Chop if either already multi-line or if over 8 parameters.
Chop AFTER the left paren and BEFORE the first parameter.

* Chop rules for generics

For generics with multi-line type parameters or multiple type parameter constraints, chop in a way that has consistent alignment.

* Always enforce enum members on their own lines

* One-line functions completely on one line

They're usually expression-bodied anyway, here, so this is almost irrelevant

* Keep control flow statements on their own lines

* Follow same chop rules for method invocation as method declaration

* Chop long or multi-line method chains or patterns

* Wrap rules for binary operators

Operator at beginning of new lines
Auto-chop if long or already multi-line

* Spaces between keywords and their opening parentheses

* Add file layout rules for organization of reorderable items like fields, properties, etc.

Ugly XML, so load it up in the UI to look at it
It's MOSTLY the ReSharper defaults, but with more aggressive sorting, generally by access modifier and name, within each grouping.

* Deconstructors use per-member types

* Use keywords for built-in types and increase severity of inspection for violations

Also apply to IntPtr and similar, which should now be nint and similar

* Apply syntax style on completion.

* Use var when evident for built-in and simple types, but not elsewhere

* Increase severity for some minor redundancy and clarity inspections

* Enforce braces always required for blocks, and treat as error

* Warn if a local function is not statement-bodied

* Increase various inspection severities

IF Condition => Severity:
Attributes not wrapped property => Suggestion
Constructors expression-bodied => Error
default doesn't have type when it isn't clear => suggestion
Namespaces not file-scoped => error
Methods not statement-bodied => hint
Null check pattern not the object property pattern => Error

* Adjust preference order of null check patterns to make object pattern highest

* XmlDoc rules to keep tags and contents aligned and wrapped

* Add a few custom profiles for code cleanup and make the full profile default

* Remove parts for languages not used in this solution

* copied in v2_develop changes

* Merged v2_develop

* Added Begin/Init unit tests. Removed Application dependencey from AutoSizeFalse tests

* TextFormatter.Lines -> GetLines ()

* Let ReSharper know we intend to localize things

* TextFormatter code cleanup

* copied in v2_develop changes

* Merged v2_develop

* Spaces aren't wanted here either.

* Fix merge errors.

* Fixes ContentBottomRightCorner related with https://github.com/gui-cs/Terminal.Gui/issues/3211#issue-2098878820

* Remove constructors with parameters from ContextMenu.

* Remove commented code.

* Remove constructors with parameters from OpenDialog.

* Remove constructors with parameters from SaveDialog.

* Remove constructors with parameters from TextField.

* Remove constructors with parameters from TimeField.

* Fix unit test.

* Remove unnecessary SetInitialProperties method.

* Remove unnecessary SetInitialProperties method.

* Remove parenthesis on objects initializers from Toplevel and Window.

* Remove constructors with parameters from RadioGroup.

* Remove constructors with parameters from TextView.

* Remove constructors with parameters from MenuBar.

* TEMPORARY: Turn everything that was set to error down to warning or lower

* Fixes #3219. MenuBar is opened by call OpenMenu even it's disabled.

* Remove constructors with parameters from Menu.

* Remove constructors with parameters from View.

* Change constructor to internal because is mainly useful for testing.

* ReSharper Cleanup Code.

* Added format only r# config

* Fixes #3224. TextFormatter.Lines should return a single string.Empty list even with Width or Height equal to zero.

* Remove constructors with parameters from Dialog.

* Remove constructors with parameters from TextValidateField.

* Fixes https://github.com/gui-cs/Terminal.Gui/issues/3224#issuecomment-1924096038

* Fixes #3225. Press CursorDown on TabView doesn't move to the next view.

* Fixes #3229. TextFormatter should have a FillRemaining property.

* Testing formatting merge in bdisp

* Testing formatting merge

* Testing formatting merge bdisp

* Testing formatting merge 2

* xmldoc format

* R# Full Code Cleanup

* R# Full Code Cleanup2

* R# Full Code Cleanup2

* Merged! But broke tests

* Refixing...

* Refixed DrawTests

* Refixed ViewTests

* Refixed Text Tests

* Refixed more unit tests

* Refixed scenarios

* Refixed rest of scenarios

* Refixed ViewsTests

* Refixed rest of tests. All unit tests pass again!

* Fixed warnings

* Updated R# version. Added new code cleanup settings

* Applied latest code cleanup to solution

* Another code cleanup pass

* Tweaked r# settings. .editorconfig now matches.

* r# else on separate line

* r# - update

* r# - full solution

* test commit

* test commit

* test commit

* Removed extra profiles

* Full cleanup following cleaning up profiles

* Null checking pattern rules/inspections

* Tabs and extra whitespace are evil

* Attributes on their own lines

* Code layout rules to put fields first and to put backing fields with their properties

* Merged v2_develop

* Full cleanup following dodexahedron's PR

---------

Co-authored-by: BDisp <bd.bdisp@gmail.com>
Co-authored-by: Brandon Thetford <github@snapsinazfs.com>
2024-02-11 15:54:03 -07:00

1811 lines
68 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 Xunit.Abstractions;
namespace Terminal.Gui.ViewsTests;
public class TextFieldTests
{
private static TextField _textField;
private readonly ITestOutputHelper _output;
public TextFieldTests (ITestOutputHelper output) { _output = output; }
[Fact]
[AutoInitShutdown]
public void Accented_Letter_With_Three_Combining_Unicode_Chars ()
{
var tf = new TextField { Width = 3, Text = "ắ" };
Toplevel top = Application.Top;
top.Add (tf);
Application.Begin (top);
TestHelpers.AssertDriverContentsWithFrameAre (
@"
ắ",
_output
);
tf.Text = "\u1eaf";
Application.Refresh ();
TestHelpers.AssertDriverContentsWithFrameAre (
@"
ắ",
_output
);
tf.Text = "\u0103\u0301";
Application.Refresh ();
TestHelpers.AssertDriverContentsWithFrameAre (
@"
ắ",
_output
);
tf.Text = "\u0061\u0306\u0301";
Application.Refresh ();
TestHelpers.AssertDriverContentsWithFrameAre (
@"
ắ",
_output
);
}
[Fact]
[AutoInitShutdown]
public void Adjust_First ()
{
var tf = new TextField { Width = Dim.Fill (), Text = "This is a test." };
Application.Top.Add (tf);
Application.Begin (Application.Top);
Assert.Equal ("This is a test. ", GetContents ());
string GetContents ()
{
var item = "";
for (var i = 0; i < 16; i++)
{
item += Application.Driver.Contents [0, i].Rune;
}
return item;
}
}
[Fact]
public void Cancel_TextChanging_ThenBackspace ()
{
var tf = new TextField ();
tf.EnsureFocus ();
tf.NewKeyDownEvent (new Key (KeyCode.A | KeyCode.ShiftMask));
Assert.Equal ("A", tf.Text);
// cancel the next keystroke
tf.TextChanging += (s, e) => e.Cancel = e.NewText == "AB";
tf.NewKeyDownEvent (new Key (KeyCode.B | KeyCode.ShiftMask));
// B was canceled so should just be A
Assert.Equal ("A", tf.Text);
// now delete the A
tf.NewKeyDownEvent (new Key (KeyCode.Backspace));
Assert.Equal ("", tf.Text);
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void CanFocus_False_Wont_Focus_With_Mouse ()
{
Toplevel top = Application.Top;
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.MouseEvent (
new MouseEvent { X = 1, Y = 0, Flags = MouseFlags.Button1DoubleClicked }
);
Assert.Null (tf.SelectedText);
Assert.False (tf.CanFocus);
Assert.False (tf.HasFocus);
Assert.False (fv.CanFocus);
Assert.False (fv.HasFocus);
Assert.Throws<InvalidOperationException> (() => tf.CanFocus = true);
fv.CanFocus = true;
tf.CanFocus = true;
tf.MouseEvent (
new MouseEvent { X = 1, Y = 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.MouseEvent (
new MouseEvent { X = 1, Y = 0, Flags = MouseFlags.Button1DoubleClicked }
);
Assert.Equal ("some ", tf.SelectedText); // Setting CanFocus to false don't change the SelectedText
Assert.False (tf.CanFocus);
Assert.False (tf.HasFocus);
Assert.False (fv.CanFocus);
Assert.False (fv.HasFocus);
}
[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.Caption = caption;
Application.Driver.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
Assert.False (tf.HasFocus);
tf.Draw ();
TestHelpers.AssertDriverContentsAre (expectedRender, _output);
}
[Fact]
[AutoInitShutdown]
public void CaptionedTextField_DoesNotOverspillBounds_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.Caption = caption;
Application.Driver.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
Assert.False (tf.HasFocus);
tf.Draw ();
TestHelpers.AssertDriverContentsAre ("Misérables", _output);
}
[Theory]
[AutoInitShutdown]
[InlineData ("blah")]
[InlineData (" ")]
public void CaptionedTextField_DoNotRenderCaption_WhenTextPresent (string content)
{
TextField tf = GetTextFieldsInView ();
tf.Draw ();
TestHelpers.AssertDriverContentsAre ("", _output);
tf.Caption = "Enter txt";
Application.Driver.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
// Caption should appear when not focused and no text
Assert.False (tf.HasFocus);
tf.Draw ();
TestHelpers.AssertDriverContentsAre ("Enter txt", _output);
// but disapear when text is added
tf.Text = content;
tf.Draw ();
TestHelpers.AssertDriverContentsAre (content, _output);
}
[Fact]
[AutoInitShutdown]
public void CaptionedTextField_RendersCaption_WhenNotFocused ()
{
TextField tf = GetTextFieldsInView ();
tf.Draw ();
TestHelpers.AssertDriverContentsAre ("", _output);
// Caption has no effect when focused
tf.Caption = "Enter txt";
Assert.True (tf.HasFocus);
tf.Draw ();
TestHelpers.AssertDriverContentsAre ("", _output);
Application.Driver.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
Assert.False (tf.HasFocus);
tf.Draw ();
TestHelpers.AssertDriverContentsAre ("Enter txt", _output);
}
[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 += _textField_TextChanging;
void _textField_TextChanging (object sender, TextChangingEventArgs e)
{
if (e.NewText.GetRuneCount () > 11)
{
e.NewText = e.NewText [..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 -= _textField_TextChanging;
}
[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.NewText;
tf.TextChanged += (s, e) => oldText = e.OldValue;
Application.Top.Add (tf);
Application.Begin (Application.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 (new Key (KeyCode.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 (new Key (KeyCode.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 (new Key (KeyCode.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 (new Key (KeyCode.X | KeyCode.CtrlMask)));
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.MouseEvent (
new MouseEvent { X = 7, Y = 1, Flags = MouseFlags.Button1DoubleClicked, View = tf }
)
);
Assert.Equal ("Misérables ", tf.SelectedText);
Assert.Equal (11, tf.SelectedLength);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Delete)));
Assert.Equal ("Les movie.", newText);
Assert.Equal ("Les Misérables movie.", oldText);
Assert.Equal ("Les movie.", tf.Text);
}
[Fact]
public void HistoryText_IsDirty_ClearHistoryChanges ()
{
var text = "Testing";
var tf = new TextField { Text = text };
tf.BeginInit ();
tf.EndInit ();
Assert.Equal (text, tf.Text);
tf.ClearHistoryChanges ();
Assert.False (tf.IsDirty);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.A | KeyCode.ShiftMask)));
Assert.Equal ($"{text}A", tf.Text);
Assert.True (tf.IsDirty);
}
[Fact]
[AutoInitShutdown (useFakeClipboard: true)]
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 (new Key (KeyCode.Delete)));
Assert.Equal ("This is a test.", tf.Text);
tf.CursorPosition = 0;
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Delete)));
Assert.Equal ("his is a test.", tf.Text);
tf.ReadOnly = true;
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.D | KeyCode.CtrlMask)));
Assert.Equal ("his is a test.", tf.Text);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Delete)));
Assert.Equal ("his is a test.", tf.Text);
tf.ReadOnly = false;
tf.CursorPosition = 1;
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Backspace)));
Assert.Equal ("is is a test.", tf.Text);
tf.CursorPosition = 5;
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Home | KeyCode.ShiftMask)));
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 (new Key (KeyCode.Home | KeyCode.ShiftMask | KeyCode.CtrlMask)));
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 (new Key (KeyCode.A | KeyCode.ShiftMask | KeyCode.CtrlMask)));
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 (new Key (KeyCode.End | KeyCode.ShiftMask)));
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 (new Key (KeyCode.End | KeyCode.ShiftMask | KeyCode.CtrlMask)));
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 (new Key (KeyCode.E | KeyCode.ShiftMask | KeyCode.CtrlMask)));
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 (new Key (KeyCode.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 (new Key (KeyCode.Home | KeyCode.CtrlMask)));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal (0, tf.CursorPosition);
tf.CursorPosition = 5;
Assert.Null (tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.A | KeyCode.CtrlMask)));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal (0, tf.CursorPosition);
tf.CursorPosition = 5;
tf.SelectedStart = -1;
Assert.Null (tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorLeft | KeyCode.ShiftMask)));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal ("s", tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorUp | KeyCode.ShiftMask)));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal ("is", tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorRight | KeyCode.ShiftMask)));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal ("s", tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorDown | KeyCode.ShiftMask)));
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 (new Key (KeyCode.CursorLeft | KeyCode.ShiftMask | KeyCode.CtrlMask)));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal ("a", tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorUp | KeyCode.ShiftMask | KeyCode.CtrlMask)));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal ("is a", tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (new Key (('B' + KeyCode.ShiftMask) | KeyCode.AltMask)));
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 (new Key (KeyCode.CursorRight | KeyCode.ShiftMask | KeyCode.CtrlMask)));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal ("is ", tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorDown | KeyCode.ShiftMask | KeyCode.CtrlMask)));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal ("is a ", tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (new Key (('F' + KeyCode.ShiftMask) | KeyCode.AltMask)));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal ("is a test.", tf.SelectedText);
Assert.Equal (13, tf.CursorPosition);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorLeft)));
Assert.Equal ("is is a test.", tf.Text);
Assert.Null (tf.SelectedText);
Assert.Equal (12, tf.CursorPosition);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorLeft)));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal (11, tf.CursorPosition);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.End)));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal (13, tf.CursorPosition);
tf.CursorPosition = 0;
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.End | KeyCode.CtrlMask)));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal (13, tf.CursorPosition);
tf.CursorPosition = 0;
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.E | KeyCode.CtrlMask)));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal (13, tf.CursorPosition);
tf.CursorPosition = 0;
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorRight)));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal (1, tf.CursorPosition);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.F | KeyCode.CtrlMask)));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal (2, tf.CursorPosition);
tf.CursorPosition = 9;
tf.ReadOnly = true;
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.K | KeyCode.CtrlMask)));
Assert.Equal ("is is a test.", tf.Text);
tf.ReadOnly = false;
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.K | KeyCode.CtrlMask)));
Assert.Equal ("is is a t", tf.Text);
Assert.Equal ("est.", Clipboard.Contents);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Z | KeyCode.CtrlMask)));
Assert.Equal ("is is a test.", tf.Text);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Y | KeyCode.CtrlMask)));
Assert.Equal ("is is a t", tf.Text);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Backspace | KeyCode.AltMask)));
Assert.Equal ("is is a test.", tf.Text);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Y | KeyCode.CtrlMask)));
Assert.Equal ("is is a t", tf.Text);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorLeft | KeyCode.CtrlMask)));
Assert.Equal ("is is a t", tf.Text);
Assert.Equal (8, tf.CursorPosition);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorUp | KeyCode.CtrlMask)));
Assert.Equal ("is is a t", tf.Text);
Assert.Equal (6, tf.CursorPosition);
Assert.True (tf.NewKeyDownEvent (new Key ('B' + KeyCode.AltMask)));
Assert.Equal ("is is a t", tf.Text);
Assert.Equal (3, tf.CursorPosition);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorRight | KeyCode.CtrlMask)));
Assert.Equal ("is is a t", tf.Text);
Assert.Equal (6, tf.CursorPosition);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorDown | KeyCode.CtrlMask)));
Assert.Equal ("is is a t", tf.Text);
Assert.Equal (8, tf.CursorPosition);
Assert.True (tf.NewKeyDownEvent (new Key ('F' + KeyCode.AltMask)));
Assert.Equal ("is is a t", tf.Text);
Assert.Equal (9, tf.CursorPosition);
Assert.True (tf.Used);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Insert)));
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 (new Key (KeyCode.C | KeyCode.CtrlMask)));
Assert.Equal ("is is a t", tf.Text);
Assert.Equal ("is a", Clipboard.Contents);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.X | KeyCode.CtrlMask)));
Assert.Equal ("is t", tf.Text);
Assert.Equal ("is a", Clipboard.Contents);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.V | KeyCode.CtrlMask)));
Assert.Equal ("is is a t", tf.Text);
Assert.Equal ("is a", Clipboard.Contents);
Assert.Equal (7, tf.CursorPosition);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.K | KeyCode.AltMask)));
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 (new Key (KeyCode.Delete | KeyCode.CtrlMask)));
Assert.Equal ("to jump between text fields.", tf.Text);
tf.CursorPosition = tf.Text.Length;
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Backspace | KeyCode.CtrlMask)));
Assert.Equal ("to jump between text fields", tf.Text);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.T | KeyCode.CtrlMask)));
Assert.Equal ("to jump between text fields", tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.D | KeyCode.CtrlMask | KeyCode.ShiftMask)));
Assert.Equal ("", tf.Text);
}
[Fact]
public void OnEnter_Does_Not_Throw_If_Not_IsInitialized_SetCursorVisibility ()
{
var top = new Toplevel ();
var tf = new TextField { Width = 10 };
top.Add (tf);
Exception exception = Record.Exception (tf.SetFocus);
Assert.Null (exception);
}
[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]
public void ProcessKey_Backspace_From_End ()
{
var tf = new TextField { Text = "ABC" };
tf.EnsureFocus ();
Assert.Equal ("ABC", tf.Text);
tf.BeginInit ();
tf.EndInit ();
Assert.Equal (3, tf.CursorPosition);
// now delete the C
tf.NewKeyDownEvent (new Key (KeyCode.Backspace));
Assert.Equal ("AB", tf.Text);
Assert.Equal (2, tf.CursorPosition);
// then delete the B
tf.NewKeyDownEvent (new Key (KeyCode.Backspace));
Assert.Equal ("A", tf.Text);
Assert.Equal (1, tf.CursorPosition);
// then delete the A
tf.NewKeyDownEvent (new Key (KeyCode.Backspace));
Assert.Equal ("", tf.Text);
Assert.Equal (0, tf.CursorPosition);
}
[Fact]
public void ProcessKey_Backspace_From_Middle ()
{
var tf = new TextField { Text = "ABC" };
tf.EnsureFocus ();
tf.CursorPosition = 2;
Assert.Equal ("ABC", tf.Text);
// now delete the B
tf.NewKeyDownEvent (new Key (KeyCode.Backspace));
Assert.Equal ("AC", tf.Text);
// then delete the A
tf.NewKeyDownEvent (new Key (KeyCode.Backspace));
Assert.Equal ("C", tf.Text);
// then delete nothing
tf.NewKeyDownEvent (new Key (KeyCode.Backspace));
Assert.Equal ("C", tf.Text);
// now delete the C
tf.CursorPosition = 1;
tf.NewKeyDownEvent (new Key (KeyCode.Backspace));
Assert.Equal ("", tf.Text);
}
[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);
Application.Top.Add (tf);
Application.Begin (Application.Top);
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;
Application.Top.Add (_textField);
RunState rs = Application.Begin (Application.Top);
Attribute [] attributes =
{
_textField.ColorScheme.Focus,
new (
_textField.ColorScheme.Focus.Background,
_textField.ColorScheme.Focus.Foreground
)
};
// TAB to jump between text fields.
TestHelpers.AssertDriverAttributesAre ("0000000", Application.Driver, attributes);
// Cursor is at the end
Assert.Equal (32, _textField.CursorPosition);
_textField.CursorPosition = 0;
_textField.NewKeyDownEvent (new Key (KeyCode.CursorRight | KeyCode.CtrlMask | KeyCode.ShiftMask));
var first = true;
Application.RunIteration (ref rs, ref first);
Assert.Equal (4, _textField.CursorPosition);
// TAB to jump between text fields.
TestHelpers.AssertDriverAttributesAre ("1111000", Application.Driver, attributes);
}
[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 (new Key ((KeyCode)0x75)); // 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 Test_RootKeyEvent_Cancel ()
{
Application.KeyDown += SuppressKey;
var tf = new TextField ();
Application.Top.Add (tf);
Application.Begin (Application.Top);
Application.Driver.SendKeys ('a', ConsoleKey.A, false, false, false);
Assert.Equal ("a", tf.Text);
// SuppressKey suppresses the 'j' key
Application.Driver.SendKeys ('j', ConsoleKey.J, false, false, false);
Assert.Equal ("a", tf.Text);
Application.KeyDown -= SuppressKey;
// Now that the delegate has been removed we can type j again
Application.Driver.SendKeys ('j', ConsoleKey.J, false, false, false);
Assert.Equal ("aj", tf.Text);
}
[Fact]
[AutoInitShutdown]
public void Test_RootMouseKeyEvent_Cancel ()
{
Application.MouseEvent += SuppressRightClick;
var tf = new TextField { Width = 10 };
var clickCounter = 0;
tf.MouseClick += (s, m) => { clickCounter++; };
Application.Top.Add (tf);
Application.Begin (Application.Top);
var mouseEvent = new MouseEvent { Flags = MouseFlags.Button1Clicked, View = tf };
Application.OnMouseEvent (new MouseEventEventArgs (mouseEvent));
Assert.Equal (1, clickCounter);
// Get a fresh instance that represents a right click.
// Should be ignored because of SuppressRightClick callback
mouseEvent = new MouseEvent { Flags = MouseFlags.Button3Clicked, View = tf };
Application.OnMouseEvent (new MouseEventEventArgs (mouseEvent));
Assert.Equal (1, clickCounter);
Application.MouseEvent -= SuppressRightClick;
// Get a fresh instance that represents a right click.
// Should no longer be ignored as the callback was removed
mouseEvent = new MouseEvent { 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.OnMouseEvent (new MouseEventEventArgs (mouseEvent));
Assert.Equal (1, clickCounter);
}
[InlineData ("a")] // Lower than selection
[InlineData ("aaaaaaaaaaa")] // Greater than selection
[InlineData ("aaaa")] // Equal than selection
[Theory]
public void TestSetTextAndMoveCursorToEnd_WhenExistingSelection (string newText)
{
var tf = new TextField ();
tf.Text = "fish";
tf.CursorPosition = tf.Text.Length;
tf.NewKeyDownEvent (new Key (KeyCode.CursorLeft));
tf.NewKeyDownEvent (new Key (KeyCode.CursorLeft | KeyCode.ShiftMask));
tf.NewKeyDownEvent (new Key (KeyCode.CursorLeft | KeyCode.ShiftMask));
Assert.Equal (1, tf.CursorPosition);
Assert.Equal (2, tf.SelectedLength);
Assert.Equal ("is", tf.SelectedText);
tf.Text = newText;
tf.CursorPosition = tf.Text.Length;
Assert.Equal (newText.Length, tf.CursorPosition);
Assert.Equal (0, tf.SelectedLength);
Assert.Null (tf.SelectedText);
}
[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 ()
{
_textField.TextChanged += (s, e) => { Assert.Equal ("TAB to jump between text fields.", e.OldValue); };
_textField.Text = "changed";
Assert.Equal ("changed", _textField.Text);
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void TextChanging_Event ()
{
var cancel = true;
_textField.TextChanging += (s, e) =>
{
Assert.Equal ("changing", e.NewText);
if (cancel)
{
e.Cancel = 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 TextField_SpaceHandling ()
{
var tf = new TextField { Width = 10, Text = " " };
var ev = new MouseEvent { X = 0, Y = 0, Flags = MouseFlags.Button1DoubleClicked };
tf.MouseEvent (ev);
Assert.Equal (1, tf.SelectedLength);
ev = new MouseEvent { X = 1, Y = 0, Flags = MouseFlags.Button1DoubleClicked };
tf.MouseEvent (ev);
Assert.Equal (1, tf.SelectedLength);
}
[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 (new Key ((KeyCode)0x75)); // u
Assert.Equal ("TAB to jumu between text fields.", _textField.Text);
_textField.NewKeyDownEvent (new Key ((KeyCode)0x73)); // s
Assert.Equal ("TAB to jumusbetween text fields.", _textField.Text);
_textField.NewKeyDownEvent (new Key ((KeyCode)0x65)); // e
Assert.Equal ("TAB to jumuseetween text fields.", _textField.Text);
_textField.NewKeyDownEvent (new Key ((KeyCode)0x64)); // 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 (new Key ((KeyCode)0x75)); // u
Assert.Equal ("TAB to jumup between text fields.", _textField.Text);
_textField.NewKeyDownEvent (new Key ((KeyCode)0x73)); // s
Assert.Equal ("TAB to jumusp between text fields.", _textField.Text);
_textField.NewKeyDownEvent (new Key ((KeyCode)0x65)); // e
Assert.Equal ("TAB to jumusep between text fields.", _textField.Text);
_textField.NewKeyDownEvent (new Key ((KeyCode)0x64)); // 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 (new Key (KeyCode.CursorLeft | KeyCode.CtrlMask));
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 (new Key (KeyCode.CursorLeft | KeyCode.CtrlMask));
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 Key (
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 Key (
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]
public void WordBackward_WordForward_Mixed ()
{
var tf = new TextField { Width = 30, Text = "Test with0. and!.?;-@+" };
tf.BeginInit ();
tf.EndInit ();
tf.NewKeyDownEvent (new Key (KeyCode.CtrlMask | KeyCode.CursorLeft));
Assert.Equal (15, tf.CursorPosition);
tf.NewKeyDownEvent (new Key (KeyCode.CtrlMask | KeyCode.CursorLeft));
Assert.Equal (12, tf.CursorPosition);
tf.NewKeyDownEvent (new Key (KeyCode.CtrlMask | KeyCode.CursorLeft));
Assert.Equal (10, tf.CursorPosition);
tf.NewKeyDownEvent (new Key (KeyCode.CtrlMask | KeyCode.CursorLeft));
Assert.Equal (5, tf.CursorPosition);
tf.NewKeyDownEvent (new Key (KeyCode.CtrlMask | KeyCode.CursorLeft));
Assert.Equal (0, tf.CursorPosition);
tf.NewKeyDownEvent (new Key (KeyCode.CtrlMask | KeyCode.CursorRight));
Assert.Equal (5, tf.CursorPosition);
tf.NewKeyDownEvent (new Key (KeyCode.CtrlMask | KeyCode.CursorRight));
Assert.Equal (10, tf.CursorPosition);
tf.NewKeyDownEvent (new Key (KeyCode.CtrlMask | KeyCode.CursorRight));
Assert.Equal (12, tf.CursorPosition);
tf.NewKeyDownEvent (new Key (KeyCode.CtrlMask | KeyCode.CursorRight));
Assert.Equal (15, tf.CursorPosition);
tf.NewKeyDownEvent (new Key (KeyCode.CtrlMask | KeyCode.CursorRight));
Assert.Equal (22, tf.CursorPosition);
}
[Fact]
public void WordBackward_WordForward_SelectedText_With_Accent ()
{
var text = "Les Misérables movie.";
var tf = new TextField { Width = 30, Text = text };
Assert.Equal (21, text.Length);
Assert.Equal (21, tf.Text.GetRuneCount ());
Assert.Equal (21, tf.Text.GetColumns ());
List<Rune> runes = tf.Text.ToRuneList ();
Assert.Equal (21, runes.Count);
Assert.Equal (21, tf.Text.Length);
for (var i = 0; i < runes.Count; i++)
{
char cs = text [i];
var cus = (char)runes [i].Value;
Assert.Equal (cs, cus);
}
var idx = 15;
Assert.Equal ('m', text [idx]);
Assert.Equal ('m', (char)runes [idx].Value);
Assert.Equal ("m", runes [idx].ToString ());
Assert.True (
tf.MouseEvent (
new MouseEvent { X = idx, Y = 1, Flags = MouseFlags.Button1DoubleClicked, View = tf }
)
);
Assert.Equal ("movie.", tf.SelectedText);
Assert.True (
tf.MouseEvent (
new MouseEvent { X = idx + 1, Y = 1, Flags = MouseFlags.Button1DoubleClicked, View = tf }
)
);
Assert.Equal ("movie.", tf.SelectedText);
}
[Fact]
[TextFieldTestsAutoInitShutdown]
public void WordForward_With_No_Selection ()
{
_textField.CursorPosition = 0;
var iteration = 0;
while (_textField.CursorPosition < _textField.Text.Length)
{
_textField.NewKeyDownEvent (new Key (KeyCode.CursorRight | KeyCode.CtrlMask));
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 (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 (new Key (KeyCode.CursorRight | KeyCode.CtrlMask));
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 (
new Key (KeyCode.CursorRight | KeyCode.CtrlMask | KeyCode.ShiftMask)
);
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 (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 (
new Key (KeyCode.CursorRight | KeyCode.CtrlMask | KeyCode.ShiftMask)
);
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 (32, _textField.CursorPosition);
Assert.Equal (10, _textField.SelectedStart);
Assert.Equal (22, _textField.SelectedLength);
Assert.Equal ("p between text fields.", _textField.SelectedText);
break;
}
iteration++;
}
}
[Fact]
[AutoInitShutdown]
public void Words_With_Accents_Incorrect_Order_Will_Result_With_Wrong_Accent_Place ()
{
var tf = new TextField { Width = 30, Text = "Les Misérables" };
Toplevel top = Application.Top;
top.Add (tf);
Application.Begin (top);
TestHelpers.AssertDriverContentsWithFrameAre (
@"
Les Misérables",
_output
);
tf.Text = "Les Mise" + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber)) + "rables";
Application.Refresh ();
TestHelpers.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";
Application.Refresh ();
TestHelpers.AssertDriverContentsWithFrameAre (
@"
Les Miśerables",
_output
);
}
private TextField GetTextFieldsInView ()
{
var tf = new TextField { Width = 10 };
var tf2 = new TextField { Y = 1, Width = 10 };
Toplevel top = Application.Top;
top.Add (tf);
top.Add (tf2);
Application.Begin (top);
Assert.Same (tf, top.Focused);
return tf;
}
private void SuppressKey (object s, Key arg)
{
if (arg.AsRune == new Rune ('j'))
{
arg.Handled = true;
}
}
private void SuppressRightClick (object sender, MouseEventEventArgs arg)
{
if (arg.MouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked))
{
arg.Handled = true;
}
}
// 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.Top.ColorScheme = Colors.ColorSchemes ["Base"];
_textField = new TextField
{
ColorScheme = new ColorScheme (Colors.ColorSchemes ["Base"]),
// 1 2 3
// 01234567890123456789012345678901=32 (Length)
Text = "TAB to jump between text fields.",
Width = 32
};
}
}
}