12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907 |
- using System.ComponentModel;
- using System.Globalization;
- using System.Reflection;
- using System.Text;
- using CsvHelper;
- 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 = new ();
- 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." };
- var top = new Toplevel ();
- top.Add (tf);
- Application.Begin (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 (Key.A.WithShift);
- Assert.Equal ("A", tf.Text);
- // cancel the next keystroke
- tf.TextChanging += (s, e) => e.Cancel = e.NewValue == "AB";
- tf.NewKeyDownEvent (Key.B.WithShift);
- // B was canceled so should just be A
- Assert.Equal ("A", tf.Text);
- // now delete the A
- tf.NewKeyDownEvent (Key.Backspace);
- Assert.Equal ("", tf.Text);
- }
- [Fact]
- [TextFieldTestsAutoInitShutdown]
- public void CanFocus_False_Wont_Focus_With_Mouse ()
- {
- Toplevel top = new ();
- var tf = new TextField { Width = Dim.Fill (), CanFocus = false, ReadOnly = true, Text = "some text" };
- var fv = new FrameView
- {
- Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = false, Title = "I shouldn't get focus"
- };
- fv.Add (tf);
- top.Add (fv);
- Application.Begin (top);
- Assert.False (tf.CanFocus);
- Assert.False (tf.HasFocus);
- Assert.False (fv.CanFocus);
- Assert.False (fv.HasFocus);
- tf.NewMouseEvent (
- new MouseEvent { 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);
- Assert.Throws<InvalidOperationException> (() => tf.CanFocus = true);
- fv.CanFocus = true;
- tf.CanFocus = true;
- tf.NewMouseEvent (
- new MouseEvent { 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 MouseEvent { Position = new (1, 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_DoesNotOverspillViewport_Unicode ()
- {
- string caption = "Mise" + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber)) + "rables";
- Assert.Equal (11, caption.Length);
- Assert.Equal (10, caption.EnumerateRunes ().Sum (c => c.GetColumns ()));
- TextField tf = GetTextFieldsInView ();
- tf.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, StateEventArgs<string> e)
- {
- if (e.NewValue.GetRuneCount () > 11)
- {
- e.NewValue = e.NewValue [..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.NewValue;
- tf.TextChanged += (s, e) => oldText = e.OldValue;
- var top = new Toplevel ();
- top.Add (tf);
- Application.Begin (top);
- Assert.Equal ("-1", tf.Text);
- // InsertText
- tf.SelectedStart = 1;
- tf.CursorPosition = 2;
- Assert.Equal (1, tf.SelectedLength);
- Assert.Equal ("1", tf.SelectedText);
- Assert.True (tf.NewKeyDownEvent (Key.D2));
- Assert.Equal ("-2", newText);
- Assert.Equal ("-1", oldText);
- Assert.Equal ("-2", tf.Text);
- // DeleteCharLeft
- tf.SelectedStart = 1;
- tf.CursorPosition = 2;
- Assert.Equal (1, tf.SelectedLength);
- Assert.Equal ("2", tf.SelectedText);
- Assert.True (tf.NewKeyDownEvent (Key.Backspace));
- Assert.Equal ("-", newText);
- Assert.Equal ("-2", oldText);
- Assert.Equal ("-", tf.Text);
- // DeleteCharRight
- tf.Text = "-1";
- tf.SelectedStart = 1;
- tf.CursorPosition = 2;
- Assert.Equal (1, tf.SelectedLength);
- Assert.Equal ("1", tf.SelectedText);
- Assert.True (tf.NewKeyDownEvent (Key.Delete));
- Assert.Equal ("-", newText);
- Assert.Equal ("-1", oldText);
- Assert.Equal ("-", tf.Text);
- // Cut
- tf.Text = "-1";
- tf.SelectedStart = 1;
- tf.CursorPosition = 2;
- Assert.Equal (1, tf.SelectedLength);
- Assert.Equal ("1", tf.SelectedText);
- Assert.True (tf.NewKeyDownEvent (Key.X.WithCtrl));
- Assert.Equal ("-", newText);
- Assert.Equal ("-1", oldText);
- Assert.Equal ("-", tf.Text);
- // Delete word with accented char
- tf.Text = "Les Misérables movie.";
- Assert.True (
- tf.NewMouseEvent (
- new MouseEvent { 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);
- }
- [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 (Key.A.WithShift));
- 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 (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 (0, 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.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.T.WithCtrl));
- Assert.Equal ("to jump between text fields", tf.SelectedText);
- Assert.True (tf.NewKeyDownEvent (Key.D.WithCtrl.WithShift));
- Assert.Equal ("", tf.Text);
- }
- [Fact]
- public void HotKey_Command_SetsFocus ()
- {
- var view = new TextField ();
- view.CanFocus = true;
- Assert.False (view.HasFocus);
- view.InvokeCommand (Command.HotKey);
- Assert.True (view.HasFocus);
- }
- [Fact]
- public void HotKey_Command_Does_Not_Accept ()
- {
- var view = new TextField ();
- var accepted = false;
- view.Accept += OnAccept;
- view.InvokeCommand (Command.HotKey);
- Assert.False (accepted);
- return;
- void OnAccept (object sender, CancelEventArgs e) { accepted = true; }
- }
- [Fact]
- public void Accept_Command_Fires_Accept ()
- {
- var view = new TextField ();
- var accepted = false;
- view.Accept += Accept;
- view.InvokeCommand (Command.Accept);
- Assert.True (accepted);
- return;
- void Accept (object sender, CancelEventArgs e) { accepted = true; }
- }
- [Fact]
- public void Accept_Cancel_Event_HandlesCommand ()
- {
- //var super = new View ();
- var view = new TextField ();
- //super.Add (view);
- //var superAcceptedInvoked = false;
- var tfAcceptedInvoked = false;
- var cancel = false;
- view.Accept += TextViewAccept;
- Assert.True (view.InvokeCommand (Command.Accept));
- Assert.True (tfAcceptedInvoked);
- tfAcceptedInvoked = false;
- cancel = true;
- view.Accept += TextViewAccept;
- Assert.False (view.InvokeCommand (Command.Accept));
- Assert.True (tfAcceptedInvoked);
- return;
- void TextViewAccept (object sender, CancelEventArgs e)
- {
- tfAcceptedInvoked = true;
- e.Cancel = cancel;
- }
- }
- [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 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 (Key.Backspace);
- Assert.Equal ("AB", tf.Text);
- Assert.Equal (2, tf.CursorPosition);
- // then delete the B
- tf.NewKeyDownEvent (Key.Backspace);
- Assert.Equal ("A", tf.Text);
- Assert.Equal (1, tf.CursorPosition);
- // then delete the A
- tf.NewKeyDownEvent (Key.Backspace);
- Assert.Equal ("", tf.Text);
- Assert.Equal (0, tf.CursorPosition);
- }
- [Fact]
- public void 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 (Key.Backspace);
- Assert.Equal ("AC", tf.Text);
- // then delete the A
- tf.NewKeyDownEvent (Key.Backspace);
- Assert.Equal ("C", tf.Text);
- // then delete nothing
- tf.NewKeyDownEvent (Key.Backspace);
- Assert.Equal ("C", tf.Text);
- // now delete the C
- tf.CursorPosition = 1;
- tf.NewKeyDownEvent (Key.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);
- var top = new Toplevel ();
- top.Add (tf);
- Application.Begin (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;
- var top = new Toplevel ();
- top.Add (_textField);
- RunState rs = Application.Begin (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 (Key.CursorRight.WithCtrl.WithShift);
- 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 (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]
- public void KeyDown_Handled_Prevents_Input ()
- {
- var tf = new TextField ();
- tf.KeyDown += HandleJKey;
- tf.NewKeyDownEvent (Key.A);
- Assert.Equal ("a", tf.Text);
- // SuppressKey suppresses the 'j' key
- tf.NewKeyDownEvent (Key.J);
- Assert.Equal ("a", tf.Text);
- tf.KeyDown -= HandleJKey;
- // Now that the delegate has been removed we can type j again
- tf.NewKeyDownEvent (Key.J);
- Assert.Equal ("aj", tf.Text);
- return;
- void HandleJKey (object s, Key arg)
- {
- if (arg.AsRune == new Rune ('j'))
- {
- arg.Handled = true;
- }
- }
- }
- [Fact]
- [AutoInitShutdown]
- public void MouseEvent_Handled_Prevents_RightClick ()
- {
- Application.MouseEvent += HandleRightClick;
- var tf = new TextField { Width = 10 };
- var clickCounter = 0;
- tf.MouseClick += (s, m) => { clickCounter++; };
- var top = new Toplevel ();
- top.Add (tf);
- Application.Begin (top);
- var mouseEvent = new MouseEvent { Flags = MouseFlags.Button1Clicked, View = tf };
- Application.OnMouseEvent (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 (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 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 (mouseEvent);
- Assert.Equal (1, clickCounter);
- return;
- void HandleRightClick (object sender, MouseEvent arg)
- {
- if (arg.Flags.HasFlag (MouseFlags.Button3Clicked))
- {
- arg.Handled = true;
- }
- }
- }
- [InlineData ("a")] // Lower than selection
- [InlineData ("aaaaaaaaaaa")] // Greater than selection
- [InlineData ("aaaa")] // Equal than selection
- [Theory]
- public void SetTextAndMoveCursorToEnd_WhenExistingSelection (string newText)
- {
- var tf = new TextField ();
- tf.Text = "fish";
- tf.CursorPosition = tf.Text.Length;
- tf.NewKeyDownEvent (Key.CursorLeft);
- tf.NewKeyDownEvent (Key.CursorLeft.WithShift);
- tf.NewKeyDownEvent (Key.CursorLeft.WithShift);
- 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.NewValue);
- 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 SpaceHandling ()
- {
- var tf = new TextField { Width = 10, Text = " " };
- var ev = new MouseEvent { Position = new (0, 0), Flags = MouseFlags.Button1DoubleClicked };
- tf.NewMouseEvent (ev);
- Assert.Equal (1, tf.SelectedLength);
- ev = new MouseEvent { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked };
- tf.NewMouseEvent (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 (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 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 (Key.CursorLeft.WithCtrl);
- Assert.Equal (15, tf.CursorPosition);
- tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
- Assert.Equal (12, tf.CursorPosition);
- tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
- Assert.Equal (10, tf.CursorPosition);
- tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
- Assert.Equal (5, tf.CursorPosition);
- tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
- Assert.Equal (0, tf.CursorPosition);
- tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
- Assert.Equal (5, tf.CursorPosition);
- tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
- Assert.Equal (10, tf.CursorPosition);
- tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
- Assert.Equal (12, tf.CursorPosition);
- tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
- Assert.Equal (15, tf.CursorPosition);
- tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
- 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.NewMouseEvent (
- new MouseEvent { Position = new (idx, 1), Flags = MouseFlags.Button1DoubleClicked, View = tf }
- )
- );
- Assert.Equal ("movie.", tf.SelectedText);
- Assert.True (
- tf.NewMouseEvent (
- new MouseEvent { Position = new (idx + 1, 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 (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 (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 (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 (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 = new ();
- 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 = 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.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
- };
- }
- }
- }
|