123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172 |
- using System.Globalization;
- using System.Reflection;
- using System.Text;
- using Xunit.Abstractions;
- namespace Terminal.Gui.ViewsTests;
- public class TextFieldTests (ITestOutputHelper output)
- {
- private static TextField _textField;
- [Fact]
- [SetupFakeDriver]
- public void Accented_Letter_With_Three_Combining_Unicode_Chars ()
- {
- var tf = new TextField { Width = 3, Text = "ắ" };
- tf.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ắ",
- output
- );
- tf.Text = "\u1eaf";
- tf.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ắ",
- output
- );
- tf.Text = "\u0103\u0301";
- tf.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ắ",
- output
- );
- tf.Text = "\u0061\u0306\u0301";
- tf.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ắ",
- output
- );
- }
- [Fact]
- [SetupFakeDriver]
- public void Adjust_First ()
- {
- var tf = new TextField { Width = Dim.Fill (), Text = "This is a test." };
- tf.SetRelativeLayout (new (20, 20));
- tf.Draw ();
- 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.SetFocus ();
- 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 () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked }
- );
- Assert.Null (tf.SelectedText);
- Assert.False (tf.CanFocus);
- Assert.False (tf.HasFocus);
- Assert.False (fv.CanFocus);
- Assert.False (fv.HasFocus);
- fv.CanFocus = true;
- tf.CanFocus = true;
- tf.NewMouseEvent (
- new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked }
- );
- Assert.Equal ("some ", tf.SelectedText);
- Assert.True (tf.CanFocus);
- Assert.True (tf.HasFocus);
- Assert.True (fv.CanFocus);
- Assert.True (fv.HasFocus);
- fv.CanFocus = false;
- tf.NewMouseEvent (
- new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked }
- );
- Assert.Equal ("some ", tf.SelectedText); // Setting CanFocus to false don't change the SelectedText
- Assert.True (tf.CanFocus); // v2: CanFocus is not longer automatically changed
- Assert.False (tf.HasFocus);
- Assert.False (fv.CanFocus);
- Assert.False (fv.HasFocus);
- top.Dispose ();
- }
- [Theory]
- [AutoInitShutdown]
- [InlineData ("0123456789", "0123456789")]
- [InlineData ("01234567890", "0123456789")]
- public void CaptionedTextField_DoesNotOverspillBounds (string caption, string expectedRender)
- {
- TextField tf = GetTextFieldsInView ();
- // Caption has no effect when focused
- tf.Caption = caption;
- Application.Driver?.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
- Assert.False (tf.HasFocus);
- tf.Draw ();
- TestHelpers.AssertDriverContentsAre (expectedRender, output);
- Application.Top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void CaptionedTextField_DoesNotOverspillViewport_Unicode ()
- {
- string caption = "Mise" + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber)) + "rables";
- Assert.Equal (11, caption.Length);
- Assert.Equal (10, caption.EnumerateRunes ().Sum (c => c.GetColumns ()));
- TextField tf = GetTextFieldsInView ();
- tf.Caption = caption;
- Application.Driver?.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
- Assert.False (tf.HasFocus);
- tf.Draw ();
- TestHelpers.AssertDriverContentsAre ("Misérables", output);
- Application.Top.Dispose ();
- }
- [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);
- Application.Top.Dispose ();
- }
- [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);
- Application.Top.Dispose ();
- }
- [Fact]
- [TextFieldTestsAutoInitShutdown]
- public void Changing_SelectedStart_Or_CursorPosition_Update_SelectedLength_And_SelectedText ()
- {
- _textField.BeginInit ();
- _textField.EndInit ();
- _textField.SelectedStart = 2;
- Assert.Equal (32, _textField.CursorPosition);
- Assert.Equal (30, _textField.SelectedLength);
- Assert.Equal ("B to jump between text fields.", _textField.SelectedText);
- _textField.CursorPosition = 20;
- Assert.Equal (2, _textField.SelectedStart);
- Assert.Equal (18, _textField.SelectedLength);
- Assert.Equal ("B to jump between ", _textField.SelectedText);
- }
- [Fact]
- [TextFieldTestsAutoInitShutdown]
- public void Copy_Or_Cut__Not_Allowed_If_Secret_Is_True ()
- {
- _textField.Secret = true;
- _textField.SelectedStart = 20;
- _textField.CursorPosition = 24;
- _textField.Copy ();
- Assert.Null (_textField.SelectedText);
- _textField.Cut ();
- Assert.Null (_textField.SelectedText);
- _textField.Secret = false;
- _textField.Copy ();
- Assert.Equal ("text", _textField.SelectedText);
- _textField.Cut ();
- Assert.Null (_textField.SelectedText);
- }
- [Fact]
- [TextFieldTestsAutoInitShutdown]
- public void Copy_Or_Cut_And_Paste_With_No_Selection ()
- {
- _textField.SelectedStart = 20;
- _textField.CursorPosition = 24;
- _textField.Copy ();
- Assert.Equal ("text", _textField.SelectedText);
- Assert.Equal ("TAB to jump between text fields.", _textField.Text);
- _textField.SelectedStart = -1;
- _textField.Paste ();
- Assert.Equal ("TAB to jump between texttext fields.", _textField.Text);
- _textField.SelectedStart = 24;
- _textField.Cut ();
- Assert.Null (_textField.SelectedText);
- Assert.Equal ("TAB to jump between text fields.", _textField.Text);
- _textField.SelectedStart = -1;
- _textField.Paste ();
- Assert.Equal ("TAB to jump between texttext fields.", _textField.Text);
- }
- [Fact]
- [TextFieldTestsAutoInitShutdown]
- public void Copy_Or_Cut_And_Paste_With_Selection ()
- {
- _textField.SelectedStart = 20;
- _textField.CursorPosition = 24;
- _textField.Copy ();
- Assert.Equal ("text", _textField.SelectedText);
- Assert.Equal ("TAB to jump between text fields.", _textField.Text);
- _textField.Paste ();
- Assert.Equal ("TAB to jump between text fields.", _textField.Text);
- _textField.SelectedStart = 20;
- _textField.Cut ();
- _textField.Paste ();
- Assert.Equal ("TAB to jump between text fields.", _textField.Text);
- }
- [Fact]
- [TextFieldTestsAutoInitShutdown]
- public void Copy_Or_Cut_Not_Null_If_Has_Selection ()
- {
- _textField.SelectedStart = 20;
- _textField.CursorPosition = 24;
- _textField.Copy ();
- Assert.Equal ("text", _textField.SelectedText);
- _textField.Cut ();
- Assert.Null (_textField.SelectedText);
- }
- [Fact]
- [TextFieldTestsAutoInitShutdown]
- public void Copy_Or_Cut_Null_If_No_Selection ()
- {
- _textField.SelectedStart = -1;
- _textField.Copy ();
- Assert.Null (_textField.SelectedText);
- _textField.Cut ();
- Assert.Null (_textField.SelectedText);
- }
- [Fact]
- [TextFieldTestsAutoInitShutdown]
- public void Copy_Paste_Surrogate_Pairs ()
- {
- _textField.Text = "TextField with some more test text. Unicode shouldn't 𝔹Aℝ𝔽!";
- _textField.SelectAll ();
- _textField.Cut ();
- Assert.Equal (
- "TextField with some more test text. Unicode shouldn't 𝔹Aℝ𝔽!",
- Application.Driver?.Clipboard.GetClipboardData ()
- );
- Assert.Equal (string.Empty, _textField.Text);
- _textField.Paste ();
- Assert.Equal ("TextField with some more test text. Unicode shouldn't 𝔹Aℝ𝔽!", _textField.Text);
- }
- [Fact]
- [TextFieldTestsAutoInitShutdown]
- public void Copy_Paste_Text_Changing_Updates_Cursor_Position ()
- {
- _textField.BeginInit ();
- _textField.EndInit ();
- _textField.TextChanging += _textField_TextChanging;
- void _textField_TextChanging (object sender, CancelEventArgs<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;
- oldText = e.CurrentValue;
- };
- var top = new Toplevel ();
- top.Add (tf);
- Application.Begin (top);
- Assert.Equal ("-1", tf.Text);
- // InsertText
- tf.SelectedStart = 1;
- tf.CursorPosition = 2;
- Assert.Equal (1, tf.SelectedLength);
- Assert.Equal ("1", tf.SelectedText);
- Assert.True (tf.NewKeyDownEvent (Key.D2));
- Assert.Equal ("-2", newText);
- Assert.Equal ("-1", oldText);
- Assert.Equal ("-2", tf.Text);
- // DeleteCharLeft
- tf.SelectedStart = 1;
- tf.CursorPosition = 2;
- Assert.Equal (1, tf.SelectedLength);
- Assert.Equal ("2", tf.SelectedText);
- Assert.True (tf.NewKeyDownEvent (Key.Backspace));
- Assert.Equal ("-", newText);
- Assert.Equal ("-2", oldText);
- Assert.Equal ("-", tf.Text);
- // DeleteCharRight
- tf.Text = "-1";
- tf.SelectedStart = 1;
- tf.CursorPosition = 2;
- Assert.Equal (1, tf.SelectedLength);
- Assert.Equal ("1", tf.SelectedText);
- Assert.True (tf.NewKeyDownEvent (Key.Delete));
- Assert.Equal ("-", newText);
- Assert.Equal ("-1", oldText);
- Assert.Equal ("-", tf.Text);
- // Cut
- tf.Text = "-1";
- tf.SelectedStart = 1;
- tf.CursorPosition = 2;
- Assert.Equal (1, tf.SelectedLength);
- Assert.Equal ("1", tf.SelectedText);
- Assert.True (tf.NewKeyDownEvent (Key.X.WithCtrl));
- Assert.Equal ("-", newText);
- Assert.Equal ("-1", oldText);
- Assert.Equal ("-", tf.Text);
- // Delete word with accented char
- tf.Text = "Les Misérables movie.";
- Assert.True (
- tf.NewMouseEvent (
- new () { Position = new (7, 1), Flags = MouseFlags.Button1DoubleClicked, View = tf }
- )
- );
- Assert.Equal ("Misérables ", tf.SelectedText);
- Assert.Equal (11, tf.SelectedLength);
- Assert.True (tf.NewKeyDownEvent (Key.Delete));
- Assert.Equal ("Les movie.", newText);
- Assert.Equal ("Les Misérables movie.", oldText);
- Assert.Equal ("Les movie.", tf.Text);
- top.Dispose ();
- }
- [Fact]
- 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]
- public void Space_Does_Not_Raise_Selected ()
- {
- TextField tf = new ();
- tf.Selecting += (sender, args) => Assert.Fail ("Selected should not be raied.");
- Application.Top = new ();
- Application.Top.Add (tf);
- tf.SetFocus ();
- Application.RaiseKeyDownEvent (Key.Space);
- Application.Top.Dispose ();
- Application.ResetState (true);
- }
- [Fact]
- public void Enter_Does_Not_Raise_Selected ()
- {
- TextField tf = new ();
- var selectingCount = 0;
- tf.Selecting += (sender, args) => selectingCount++;
- Application.Top = new ();
- Application.Top.Add (tf);
- tf.SetFocus ();
- Application.RaiseKeyDownEvent (Key.Enter);
- Assert.Equal (0, selectingCount);
- Application.Top.Dispose ();
- Application.ResetState (true);
- }
- [Fact]
- public void Enter_Raises_Accepted ()
- {
- TextField tf = new ();
- var acceptedCount = 0;
- tf.Accepting += (sender, args) => acceptedCount++;
- Application.Top = new ();
- Application.Top.Add (tf);
- tf.SetFocus ();
- Application.RaiseKeyDownEvent (Key.Enter);
- Assert.Equal (1, acceptedCount);
- Application.Top.Dispose ();
- Application.ResetState (true);
- }
- [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.Accepting += OnAccept;
- view.InvokeCommand (Command.HotKey);
- Assert.False (accepted);
- return;
- void OnAccept (object sender, CommandEventArgs e) { accepted = true; }
- }
- [Fact]
- public void Accepted_Command_Fires_Accept ()
- {
- var view = new TextField ();
- var accepted = false;
- view.Accepting += Accept;
- view.InvokeCommand (Command.Accept);
- Assert.True (accepted);
- return;
- void Accept (object sender, CommandEventArgs e) { accepted = true; }
- }
- [Theory]
- [InlineData (false, 1)]
- [InlineData (true, 0)]
- public void Accepted_Handler_Handled_Prevents_Default_Button_Accept (bool handleAccept, int expectedButtonAccepts)
- {
- var superView = new Window
- {
- Id = "superView"
- };
- var tf = new TextField
- {
- Id = "tf"
- };
- var button = new Button
- {
- Id = "button",
- IsDefault = true
- };
- superView.Add (tf, button);
- var buttonAccept = 0;
- button.Accepting += ButtonAccept;
- var textFieldAccept = 0;
- tf.Accepting += TextFieldAccept;
- tf.SetFocus ();
- Assert.True (tf.HasFocus);
- superView.NewKeyDownEvent (Key.Enter);
- Assert.Equal (1, textFieldAccept);
- Assert.Equal (expectedButtonAccepts, buttonAccept);
- button.SetFocus ();
- superView.NewKeyDownEvent (Key.Enter);
- Assert.Equal (1, textFieldAccept);
- Assert.Equal (expectedButtonAccepts + 1, buttonAccept);
- return;
- void TextFieldAccept (object sender, CommandEventArgs e)
- {
- textFieldAccept++;
- e.Cancel = handleAccept;
- }
- void ButtonAccept (object sender, CommandEventArgs e) { buttonAccept++; }
- }
- [Fact]
- public void Accepted_No_Handler_Enables_Default_Button_Accept ()
- {
- var superView = new Window
- {
- Id = "superView"
- };
- var tf = new TextField
- {
- Id = "tf"
- };
- var button = new Button
- {
- Id = "button",
- IsDefault = true
- };
- superView.Add (tf, button);
- var buttonAccept = 0;
- button.Accepting += ButtonAccept;
- tf.SetFocus ();
- Assert.True (tf.HasFocus);
- superView.NewKeyDownEvent (Key.Enter);
- Assert.Equal (1, buttonAccept);
- button.SetFocus ();
- superView.NewKeyDownEvent (Key.Enter);
- Assert.Equal (2, buttonAccept);
- return;
- void ButtonAccept (object sender, CommandEventArgs e) { buttonAccept++; }
- }
- [Fact]
- public void Accepted_Cancel_Event_HandlesCommand ()
- {
- //var super = new View ();
- var view = new TextField ();
- //super.Add (view);
- //var superAcceptedInvoked = false;
- var tfAcceptedInvoked = false;
- var handle = false;
- view.Accepting += TextViewAccept;
- Assert.False (view.InvokeCommand (Command.Accept));
- Assert.True (tfAcceptedInvoked);
- tfAcceptedInvoked = false;
- handle = true;
- view.Accepting += TextViewAccept;
- Assert.True (view.InvokeCommand (Command.Accept));
- Assert.True (tfAcceptedInvoked);
- return;
- void TextViewAccept (object sender, CommandEventArgs e)
- {
- tfAcceptedInvoked = true;
- e.Cancel = handle;
- }
- }
- [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.SetFocus ();
- 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.SetFocus ();
- 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);
- }
- [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);
- top.Dispose ();
- }
- [Fact]
- [TextFieldTestsAutoInitShutdown]
- public void SelectedStart_And_CursorPosition_With_Value_Greater_Than_Text_Length_Changes_Both_To_Text_Length ()
- {
- _textField.CursorPosition = 33;
- _textField.SelectedStart = 33;
- Assert.Equal (32, _textField.CursorPosition);
- Assert.Equal (32, _textField.SelectedStart);
- Assert.Equal (0, _textField.SelectedLength);
- Assert.Null (_textField.SelectedText);
- }
- [Fact]
- [TextFieldTestsAutoInitShutdown]
- public void SelectedStart_Greater_Than_CursorPosition_All_Selection_Is_Overwritten_On_Typing ()
- {
- _textField.SelectedStart = 19;
- _textField.CursorPosition = 12;
- Assert.Equal ("TAB to jump between text fields.", _textField.Text);
- _textField.NewKeyDownEvent (Key.U); // u
- Assert.Equal ("TAB to jump u text fields.", _textField.Text);
- }
- [Fact]
- [TextFieldTestsAutoInitShutdown]
- public void SelectedStart_With_Value_Greater_Than_Text_Length_Changes_To_Text_Length ()
- {
- _textField.CursorPosition = 2;
- _textField.SelectedStart = 33;
- Assert.Equal (32, _textField.SelectedStart);
- Assert.Equal (30, _textField.SelectedLength);
- Assert.Equal ("B to jump between text fields.", _textField.SelectedText);
- }
- [Fact]
- [TextFieldTestsAutoInitShutdown]
- public void SelectedStart_With_Value_Less_Than_Minus_One_Changes_To_Minus_One ()
- {
- _textField.SelectedStart = -2;
- Assert.Equal (-1, _textField.SelectedStart);
- Assert.Equal (0, _textField.SelectedLength);
- Assert.Null (_textField.SelectedText);
- }
- [Fact]
- 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.RaiseMouseEvent (mouseEvent);
- Assert.Equal (1, clickCounter);
- // Get a fresh instance that represents a right click.
- // Should be ignored because of SuppressRightClick callback
- mouseEvent = new () { Flags = MouseFlags.Button3Clicked, View = tf };
- Application.RaiseMouseEvent (mouseEvent);
- Assert.Equal (1, clickCounter);
- Application.MouseEvent -= HandleRightClick;
- // Get a fresh instance that represents a right click.
- // Should no longer be ignored as the callback was removed
- mouseEvent = new () { Flags = MouseFlags.Button3Clicked, View = tf };
- // In #3183 OnMouseClicked is no longer called before MouseEvent().
- // This call causes the context menu to pop, and MouseEvent() returns true.
- // Thus, the clickCounter is NOT incremented.
- // Which is correct, because the user did NOT click with the left mouse button.
- Application.RaiseMouseEvent (mouseEvent);
- Assert.Equal (1, clickCounter);
- top.Dispose ();
- return;
- void HandleRightClick (object sender, 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 ()
- {
- var eventFired = false;
- _textField.TextChanged += (s, e) => eventFired = true;
- _textField.Text = "changed";
- Assert.True (eventFired);
- Assert.Equal ("changed", _textField.Text);
- }
- [Fact]
- [TextFieldTestsAutoInitShutdown]
- public void TextChanging_Event ()
- {
- var cancel = true;
- _textField.TextChanging += (s, e) =>
- {
- Assert.Equal ("changing", e.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]
- 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 () { 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 (
- KeyCode.CursorLeft | KeyCode.CtrlMask | KeyCode.ShiftMask
- )
- );
- switch (iteration)
- {
- case 0:
- Assert.Equal (31, _textField.CursorPosition);
- Assert.Equal (32, _textField.SelectedStart);
- Assert.Equal (1, _textField.SelectedLength);
- Assert.Equal (".", _textField.SelectedText);
- break;
- case 1:
- Assert.Equal (25, _textField.CursorPosition);
- Assert.Equal (32, _textField.SelectedStart);
- Assert.Equal (7, _textField.SelectedLength);
- Assert.Equal ("fields.", _textField.SelectedText);
- break;
- case 2:
- Assert.Equal (20, _textField.CursorPosition);
- Assert.Equal (32, _textField.SelectedStart);
- Assert.Equal (12, _textField.SelectedLength);
- Assert.Equal ("text fields.", _textField.SelectedText);
- break;
- case 3:
- Assert.Equal (12, _textField.CursorPosition);
- Assert.Equal (32, _textField.SelectedStart);
- Assert.Equal (20, _textField.SelectedLength);
- Assert.Equal ("between text fields.", _textField.SelectedText);
- break;
- case 4:
- Assert.Equal (7, _textField.CursorPosition);
- Assert.Equal (32, _textField.SelectedStart);
- Assert.Equal (25, _textField.SelectedLength);
- Assert.Equal ("jump between text fields.", _textField.SelectedText);
- break;
- case 5:
- Assert.Equal (4, _textField.CursorPosition);
- Assert.Equal (32, _textField.SelectedStart);
- Assert.Equal (28, _textField.SelectedLength);
- Assert.Equal ("to jump between text fields.", _textField.SelectedText);
- break;
- case 6:
- Assert.Equal (0, _textField.CursorPosition);
- Assert.Equal (32, _textField.SelectedStart);
- Assert.Equal (32, _textField.SelectedLength);
- Assert.Equal ("TAB to jump between text fields.", _textField.SelectedText);
- break;
- }
- iteration++;
- }
- }
- [Fact]
- [TextFieldTestsAutoInitShutdown]
- public void
- WordBackward_With_The_Same_Values_For_SelectedStart_And_CursorPosition_And_Not_Starting_At_Beginning_Of_The_Text ()
- {
- _textField.CursorPosition = 10;
- _textField.SelectedStart = 10;
- var iteration = 0;
- while (_textField.CursorPosition > 0)
- {
- _textField.NewKeyDownEvent (
- new (
- KeyCode.CursorLeft | KeyCode.CtrlMask | KeyCode.ShiftMask
- )
- );
- switch (iteration)
- {
- case 0:
- Assert.Equal (7, _textField.CursorPosition);
- Assert.Equal (10, _textField.SelectedStart);
- Assert.Equal (3, _textField.SelectedLength);
- Assert.Equal ("jum", _textField.SelectedText);
- break;
- case 1:
- Assert.Equal (4, _textField.CursorPosition);
- Assert.Equal (10, _textField.SelectedStart);
- Assert.Equal (6, _textField.SelectedLength);
- Assert.Equal ("to jum", _textField.SelectedText);
- break;
- case 2:
- Assert.Equal (0, _textField.CursorPosition);
- Assert.Equal (10, _textField.SelectedStart);
- Assert.Equal (10, _textField.SelectedLength);
- Assert.Equal ("TAB to jum", _textField.SelectedText);
- break;
- }
- iteration++;
- }
- }
- [Fact]
- 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 () { Position = new (idx, 1), Flags = MouseFlags.Button1DoubleClicked, View = tf }
- )
- );
- Assert.Equal ("movie.", tf.SelectedText);
- Assert.True (
- tf.NewMouseEvent (
- new () { 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]
- [SetupFakeDriver]
- public void Words_With_Accents_Incorrect_Order_Will_Result_With_Wrong_Accent_Place ()
- {
- var tf = new TextField { Width = 30, Text = "Les Misérables" };
- tf.SetRelativeLayout (new (100, 100));
- tf.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- Les Misérables",
- output
- );
- tf.Text = "Les Mise" + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber)) + "rables";
- tf.Draw ();
- 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";
- tf.Draw ();
- 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 ()
- {
- ColorScheme = new (Colors.ColorSchemes ["Base"]),
- // 1 2 3
- // 01234567890123456789012345678901=32 (Length)
- Text = "TAB to jump between text fields.",
- Width = 32
- };
- }
- }
- [Fact]
- public void Autocomplete_Popup_Added_To_SuperView_On_Init ()
- {
- View superView = new ()
- {
- CanFocus = true
- };
- TextField t = new ();
- superView.Add (t);
- Assert.Single (superView.Subviews);
- superView.BeginInit ();
- superView.EndInit ();
- Assert.Equal (2, superView.Subviews.Count);
- }
- [Fact]
- public void Autocomplete__Added_To_SuperView_On_Add ()
- {
- View superView = new ()
- {
- CanFocus = true,
- Id = "superView"
- };
- superView.BeginInit ();
- superView.EndInit ();
- Assert.Empty (superView.Subviews);
- TextField t = new ()
- {
- Id = "t"
- };
- superView.Add (t);
- Assert.Equal (2, superView.Subviews.Count);
- }
- [Fact]
- public void Right_CursorAtEnd_WithSelection_ShouldClearSelection ()
- {
- var tf = new TextField
- {
- Text = "Hello",
- };
- tf.SetFocus ();
- tf.SelectAll ();
- tf.CursorPosition = 5;
- // When there is selected text and the cursor is at the end of the text field
- Assert.Equal ("Hello",tf.SelectedText);
- // Pressing right should not move focus, instead it should clear selection
- Assert.True(tf.NewKeyDownEvent (Key.CursorRight));
- Assert.Null (tf.SelectedText);
- // Now that the selection is cleared another right keypress should move focus
- Assert.False (tf.NewKeyDownEvent (Key.CursorRight));
- }
- [Fact]
- public void Left_CursorAtStart_WithSelection_ShouldClearSelection ()
- {
- var tf = new TextField
- {
- Text = "Hello",
- };
- tf.SetFocus ();
- tf.CursorPosition = 2;
- Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithShift));
- Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithShift));
- // When there is selected text and the cursor is at the start of the text field
- Assert.Equal ("He", tf.SelectedText);
- // Pressing left should not move focus, instead it should clear selection
- Assert.True (tf.NewKeyDownEvent (Key.CursorLeft));
- Assert.Null (tf.SelectedText);
- // When clearing selected text with left the cursor should be at the start of the selection
- Assert.Equal (0,tf.CursorPosition);
- // Now that the selection is cleared another left keypress should move focus
- Assert.False (tf.NewKeyDownEvent (Key.CursorLeft));
- }
- [Fact]
- public void Autocomplete_Visible_False_By_Default ()
- {
- View superView = new ()
- {
- CanFocus = true
- };
- TextField t = new ();
- superView.Add (t);
- superView.BeginInit ();
- superView.EndInit ();
- Assert.Equal (2, superView.Subviews.Count);
- Assert.True (t.Visible);
- Assert.False (t.Autocomplete.Visible);
- }
- [Fact]
- [AutoInitShutdown]
- public void Draw_Esc_Rune ()
- {
- var tf = new TextField { Width = 5, Text = "\u001b" };
- tf.BeginInit ();
- tf.EndInit ();
- tf.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre ("\u241b", output);
- tf.Dispose ();
- }
- }
|