TextViewTests.cs 225 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011
  1. using System.Reflection;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4. using UnitTests;
  5. using Xunit.Abstractions;
  6. namespace UnitTests.ViewsTests;
  7. public class TextViewTests
  8. {
  9. private static TextView _textView;
  10. private readonly ITestOutputHelper _output;
  11. public TextViewTests (ITestOutputHelper output) { _output = output; }
  12. [Fact]
  13. [TextViewTestsSetupFakeApplication]
  14. public void AllowsReturn_Setting_To_True_Changes_Multiline_To_True_If_It_Is_False ()
  15. {
  16. Assert.True (_textView.AllowsReturn);
  17. Assert.True (_textView.Multiline);
  18. Assert.Equal (4, _textView.TabWidth);
  19. Assert.True (_textView.AllowsTab);
  20. _textView.NewKeyDownEvent (Key.Enter);
  21. Assert.Equal (
  22. Environment.NewLine + "TAB to jump between text fields.",
  23. _textView.Text
  24. );
  25. _textView.AllowsReturn = false;
  26. Assert.False (_textView.AllowsReturn);
  27. Assert.False (_textView.Multiline);
  28. Assert.Equal (0, _textView.TabWidth);
  29. Assert.False (_textView.AllowsTab);
  30. _textView.NewKeyDownEvent (Key.Enter);
  31. Assert.Equal (
  32. Environment.NewLine + "TAB to jump between text fields.",
  33. _textView.Text
  34. );
  35. }
  36. [Fact]
  37. [TextViewTestsSetupFakeApplication]
  38. public void AllowsTab_Setting_To_True_Changes_TabWidth_To_Default_If_It_Is_Zero ()
  39. {
  40. _textView.TabWidth = 0;
  41. Assert.Equal (0, _textView.TabWidth);
  42. Assert.True (_textView.AllowsTab);
  43. Assert.True (_textView.AllowsReturn);
  44. Assert.True (_textView.Multiline);
  45. _textView.AllowsTab = true;
  46. Assert.True (_textView.AllowsTab);
  47. Assert.Equal (4, _textView.TabWidth);
  48. Assert.True (_textView.AllowsReturn);
  49. Assert.True (_textView.Multiline);
  50. }
  51. [Fact]
  52. [TextViewTestsSetupFakeApplication]
  53. public void CanFocus_False_Wont_Focus_With_Mouse ()
  54. {
  55. Toplevel top = new ();
  56. var tv = new TextView { Width = Dim.Fill (), CanFocus = false, ReadOnly = true, Text = "some text" };
  57. var fv = new FrameView
  58. {
  59. Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = false, Title = "I shouldn't get focus"
  60. };
  61. fv.Add (tv);
  62. top.Add (fv);
  63. Application.Begin (top);
  64. Assert.False (tv.CanFocus);
  65. Assert.False (tv.HasFocus);
  66. Assert.False (fv.CanFocus);
  67. Assert.False (fv.HasFocus);
  68. tv.NewMouseEvent (new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked });
  69. Assert.Empty (tv.SelectedText);
  70. Assert.False (tv.CanFocus);
  71. Assert.False (tv.HasFocus);
  72. Assert.False (fv.CanFocus);
  73. Assert.False (fv.HasFocus);
  74. fv.CanFocus = true;
  75. tv.CanFocus = true;
  76. tv.NewMouseEvent (new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked });
  77. Assert.Equal ("some ", tv.SelectedText);
  78. Assert.True (tv.CanFocus);
  79. Assert.True (tv.HasFocus);
  80. Assert.True (fv.CanFocus);
  81. Assert.True (fv.HasFocus);
  82. fv.CanFocus = false;
  83. tv.NewMouseEvent (new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked });
  84. Assert.Equal ("some ", tv.SelectedText); // Setting CanFocus to false don't change the SelectedText
  85. Assert.True (tv.CanFocus); // v2: CanFocus is not longer automatically changed
  86. Assert.False (tv.HasFocus);
  87. Assert.False (fv.CanFocus);
  88. Assert.False (fv.HasFocus);
  89. top.Dispose ();
  90. }
  91. [Fact]
  92. [TextViewTestsSetupFakeApplication]
  93. public void Changing_Selection_Or_CursorPosition_Update_SelectedLength_And_SelectedText ()
  94. {
  95. _textView.SelectionStartColumn = 2;
  96. _textView.SelectionStartRow = 0;
  97. Assert.Equal (0, _textView.CursorPosition.X);
  98. Assert.Equal (0, _textView.CursorPosition.Y);
  99. Assert.Equal (2, _textView.SelectedLength);
  100. Assert.Equal ("TA", _textView.SelectedText);
  101. _textView.CursorPosition = new (20, 0);
  102. Assert.Equal (2, _textView.SelectionStartColumn);
  103. Assert.Equal (0, _textView.SelectionStartRow);
  104. Assert.Equal (18, _textView.SelectedLength);
  105. Assert.Equal ("B to jump between ", _textView.SelectedText);
  106. }
  107. [Fact]
  108. [SetupFakeApplication]
  109. public void ContentsChanged_Event_Fires_On_Init ()
  110. {
  111. Application.StopAfterFirstIteration = true;
  112. var expectedRow = 0;
  113. var expectedCol = 0;
  114. var eventcount = 0;
  115. var tv = new TextView { Width = 50, Height = 10 };
  116. tv.ContentsChanged += (s, e) =>
  117. {
  118. eventcount++;
  119. Assert.Equal (expectedRow, e.Row);
  120. Assert.Equal (expectedCol, e.Col);
  121. };
  122. var top = new Toplevel ();
  123. top.Add (tv);
  124. Application.Begin (top);
  125. Assert.Equal (1, eventcount);
  126. top.Dispose ();
  127. }
  128. [Fact]
  129. [SetupFakeApplication]
  130. public void ContentsChanged_Event_Fires_On_InsertText ()
  131. {
  132. var eventcount = 0;
  133. var tv = new TextView { Width = 50, Height = 10 };
  134. tv.CursorPosition = Point.Empty;
  135. tv.ContentsChanged += (s, e) => { eventcount++; };
  136. Assert.Equal (0, eventcount);
  137. tv.InsertText ("a");
  138. Assert.Equal (1, eventcount);
  139. tv.CursorPosition = Point.Empty;
  140. tv.InsertText ("bcd");
  141. Assert.Equal (4, eventcount);
  142. tv.InsertText ("e");
  143. Assert.Equal (5, eventcount);
  144. tv.InsertText ("\n");
  145. Assert.Equal (6, eventcount);
  146. tv.InsertText ("1234");
  147. Assert.Equal (10, eventcount);
  148. }
  149. [Fact]
  150. [SetupFakeApplication]
  151. public void ContentsChanged_Event_Fires_On_Set_Text ()
  152. {
  153. Application.StopAfterFirstIteration = true;
  154. var eventcount = 0;
  155. var expectedRow = 0;
  156. var expectedCol = 0;
  157. var tv = new TextView
  158. {
  159. Width = 50,
  160. Height = 10,
  161. // you'd think col would be 3, but it's 0 because TextView sets
  162. // row/col = 0 when you set Text
  163. Text = "abc"
  164. };
  165. tv.ContentsChanged += (s, e) =>
  166. {
  167. eventcount++;
  168. Assert.Equal (expectedRow, e.Row);
  169. Assert.Equal (expectedCol, e.Col);
  170. };
  171. Assert.Equal ("abc", tv.Text);
  172. var top = new Toplevel ();
  173. top.Add (tv);
  174. SessionToken rs = Application.Begin (top);
  175. Assert.Equal (1, eventcount); // for Initialize
  176. expectedCol = 0;
  177. tv.Text = "defg";
  178. Assert.Equal (2, eventcount); // for set Text = "defg"
  179. top.Dispose ();
  180. }
  181. [Fact]
  182. [SetupFakeApplication]
  183. public void ContentsChanged_Event_Fires_On_Typing ()
  184. {
  185. Application.StopAfterFirstIteration = true;
  186. var eventcount = 0;
  187. var expectedRow = 0;
  188. var expectedCol = 0;
  189. var tv = new TextView { Width = 50, Height = 10 };
  190. tv.ContentsChanged += (s, e) =>
  191. {
  192. eventcount++;
  193. Assert.Equal (expectedRow, e.Row);
  194. Assert.Equal (expectedCol, e.Col);
  195. };
  196. var top = new Toplevel ();
  197. top.Add (tv);
  198. SessionToken rs = Application.Begin (top);
  199. Assert.Equal (1, eventcount); // for Initialize
  200. expectedCol = 0;
  201. tv.Text = "ay";
  202. Assert.Equal (2, eventcount);
  203. expectedCol = 1;
  204. tv.NewKeyDownEvent (Key.Y.WithShift);
  205. Assert.Equal (3, eventcount);
  206. Assert.Equal ("Yay", tv.Text);
  207. top.Dispose ();
  208. }
  209. [Fact]
  210. [TextViewTestsSetupFakeApplication]
  211. public void ContentsChanged_Event_Fires_On_Undo_Redo ()
  212. {
  213. var eventcount = 0;
  214. var expectedEventCount = 0;
  215. _textView.ContentsChanged += (s, e) => { eventcount++; };
  216. expectedEventCount++;
  217. _textView.Text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  218. Assert.Equal (expectedEventCount, eventcount);
  219. expectedEventCount++;
  220. Assert.True (_textView.NewKeyDownEvent (Key.Enter));
  221. Assert.Equal (expectedEventCount, eventcount);
  222. // Undo
  223. expectedEventCount++;
  224. Assert.True (_textView.NewKeyDownEvent (Key.Z.WithCtrl));
  225. Assert.Equal (expectedEventCount, eventcount);
  226. // Redo
  227. expectedEventCount++;
  228. Assert.True (_textView.NewKeyDownEvent (Key.R.WithCtrl));
  229. Assert.Equal (expectedEventCount, eventcount);
  230. // Undo
  231. expectedEventCount++;
  232. Assert.True (_textView.NewKeyDownEvent (Key.Z.WithCtrl));
  233. Assert.Equal (expectedEventCount, eventcount);
  234. // Redo
  235. expectedEventCount++;
  236. Assert.True (_textView.NewKeyDownEvent (Key.R.WithCtrl));
  237. Assert.Equal (expectedEventCount, eventcount);
  238. }
  239. [Fact]
  240. [TextViewTestsSetupFakeApplication]
  241. public void ContentsChanged_Event_Fires_Using_Copy_Or_Cut_Tests ()
  242. {
  243. var eventcount = 0;
  244. _textView.ContentsChanged += (s, e) => { eventcount++; };
  245. var expectedEventCount = 1;
  246. // reset
  247. _textView.Text = TextViewTestsSetupFakeApplication.Txt;
  248. Assert.Equal (expectedEventCount, eventcount);
  249. expectedEventCount += 3;
  250. Copy_Or_Cut_And_Paste_With_No_Selection ();
  251. Assert.Equal (expectedEventCount, eventcount);
  252. // reset
  253. expectedEventCount += 1;
  254. _textView.Text = TextViewTestsSetupFakeApplication.Txt;
  255. Assert.Equal (expectedEventCount, eventcount);
  256. expectedEventCount += 3;
  257. Copy_Or_Cut_And_Paste_With_Selection ();
  258. Assert.Equal (expectedEventCount, eventcount);
  259. // reset
  260. expectedEventCount += 1;
  261. _textView.Text = TextViewTestsSetupFakeApplication.Txt;
  262. Assert.Equal (expectedEventCount, eventcount);
  263. expectedEventCount += 1;
  264. Copy_Or_Cut_Not_Null_If_Has_Selection ();
  265. Assert.Equal (expectedEventCount, eventcount);
  266. // reset
  267. expectedEventCount += 1;
  268. _textView.Text = TextViewTestsSetupFakeApplication.Txt;
  269. Assert.Equal (expectedEventCount, eventcount);
  270. expectedEventCount += 1;
  271. Copy_Or_Cut_Null_If_No_Selection ();
  272. Assert.Equal (expectedEventCount, eventcount);
  273. // reset
  274. expectedEventCount += 1;
  275. _textView.Text = TextViewTestsSetupFakeApplication.Txt;
  276. Assert.Equal (expectedEventCount, eventcount);
  277. expectedEventCount += 4;
  278. Copy_Without_Selection ();
  279. Assert.Equal (expectedEventCount, eventcount);
  280. // reset
  281. expectedEventCount += 1;
  282. _textView.Text = TextViewTestsSetupFakeApplication.Txt;
  283. Assert.Equal (expectedEventCount, eventcount);
  284. expectedEventCount += 4;
  285. Copy_Without_Selection ();
  286. Assert.Equal (expectedEventCount, eventcount);
  287. }
  288. [Fact]
  289. [TextViewTestsSetupFakeApplication]
  290. public void ContentsChanged_Event_Fires_Using_Kill_Delete_Tests ()
  291. {
  292. var eventcount = 0;
  293. _textView.ContentsChanged += (s, e) => { eventcount++; };
  294. var expectedEventCount = 1;
  295. Kill_Delete_WordForward ();
  296. Assert.Equal (expectedEventCount, eventcount); // for Initialize
  297. expectedEventCount += 1;
  298. Kill_Delete_WordBackward ();
  299. Assert.Equal (expectedEventCount, eventcount);
  300. expectedEventCount += 2;
  301. Kill_To_End_Delete_Forwards_Copy_To_The_Clipboard_And_Paste ();
  302. Assert.Equal (expectedEventCount, eventcount);
  303. expectedEventCount += 2;
  304. Kill_To_Start_Delete_Backwards_Copy_To_The_Clipboard_And_Paste ();
  305. Assert.Equal (expectedEventCount, eventcount);
  306. }
  307. [Fact]
  308. [SetupFakeApplication]
  309. public void ContentsChanged_Event_NoFires_On_CursorPosition ()
  310. {
  311. var eventcount = 0;
  312. var tv = new TextView { Width = 50, Height = 10 };
  313. tv.ContentsChanged += (s, e) => { eventcount++; };
  314. Assert.Equal (0, eventcount);
  315. tv.CursorPosition = Point.Empty;
  316. Assert.Equal (0, eventcount);
  317. }
  318. [Fact]
  319. [TextViewTestsSetupFakeApplication]
  320. public void Copy_Or_Cut_And_Paste_With_No_Selection ()
  321. {
  322. _textView.SelectionStartColumn = 20;
  323. _textView.SelectionStartRow = 0;
  324. _textView.CursorPosition = new (24, 0);
  325. _textView.NewKeyDownEvent (Key.C.WithCtrl); // Copy
  326. Assert.Equal ("text", _textView.SelectedText);
  327. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  328. _textView.SelectionStartColumn = 0;
  329. _textView.SelectionStartRow = 0;
  330. Assert.Equal (new (24, 0), _textView.CursorPosition);
  331. Assert.True (_textView.IsSelecting);
  332. _textView.IsSelecting = false;
  333. _textView.NewKeyDownEvent (Key.Y.WithCtrl); // Paste
  334. Assert.Equal (new (28, 0), _textView.CursorPosition);
  335. Assert.False (_textView.IsSelecting);
  336. Assert.Equal ("TAB to jump between texttext fields.", _textView.Text);
  337. _textView.SelectionStartColumn = 24;
  338. _textView.SelectionStartRow = 0;
  339. _textView.NewKeyDownEvent (Key.W.WithCtrl); // Cut
  340. Assert.Equal (new (24, 0), _textView.CursorPosition);
  341. Assert.False (_textView.IsSelecting);
  342. Assert.Equal ("", _textView.SelectedText);
  343. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  344. _textView.SelectionStartColumn = 0;
  345. _textView.SelectionStartRow = 0;
  346. _textView.IsSelecting = false;
  347. _textView.NewKeyDownEvent (Key.Y.WithCtrl); // Paste
  348. Assert.Equal (new (28, 0), _textView.CursorPosition);
  349. Assert.False (_textView.IsSelecting);
  350. Assert.Equal ("TAB to jump between texttext fields.", _textView.Text);
  351. }
  352. [Fact]
  353. [TextViewTestsSetupFakeApplication]
  354. public void Copy_Or_Cut_And_Paste_With_Selection ()
  355. {
  356. _textView.SelectionStartColumn = 20;
  357. _textView.SelectionStartRow = 0;
  358. _textView.CursorPosition = new (24, 0);
  359. _textView.NewKeyDownEvent (Key.C.WithCtrl); // Copy
  360. Assert.Equal ("text", _textView.SelectedText);
  361. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  362. _textView.NewKeyDownEvent (Key.Y.WithCtrl); // Paste
  363. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  364. _textView.SelectionStartColumn = 20;
  365. _textView.SelectionStartRow = 0;
  366. _textView.NewKeyDownEvent (Key.W.WithCtrl); // Cut
  367. _textView.NewKeyDownEvent (Key.Y.WithCtrl); // Paste
  368. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  369. }
  370. [Fact]
  371. [TextViewTestsSetupFakeApplication]
  372. public void Copy_Or_Cut_Not_Null_If_Has_Selection ()
  373. {
  374. _textView.SelectionStartColumn = 20;
  375. _textView.SelectionStartRow = 0;
  376. _textView.CursorPosition = new (24, 0);
  377. _textView.NewKeyDownEvent (Key.C.WithCtrl); // Copy
  378. Assert.Equal ("text", _textView.SelectedText);
  379. _textView.NewKeyDownEvent (Key.W.WithCtrl); // Cut
  380. Assert.Equal ("", _textView.SelectedText);
  381. }
  382. [Fact]
  383. [TextViewTestsSetupFakeApplication]
  384. public void Copy_Or_Cut_Null_If_No_Selection ()
  385. {
  386. _textView.SelectionStartColumn = 0;
  387. _textView.SelectionStartRow = 0;
  388. _textView.NewKeyDownEvent (Key.C.WithCtrl); // Copy
  389. Assert.Equal ("", _textView.SelectedText);
  390. _textView.NewKeyDownEvent (Key.W.WithCtrl); // Cut
  391. Assert.Equal ("", _textView.SelectedText);
  392. }
  393. [Fact]
  394. [TextViewTestsSetupFakeApplication]
  395. public void Copy_Paste_Surrogate_Pairs ()
  396. {
  397. _textView.Text = "TextView with some more test text. Unicode shouldn't 𝔹Aℝ𝔽!";
  398. _textView.SelectAll ();
  399. _textView.Cut ();
  400. Assert.Equal (
  401. "TextView with some more test text. Unicode shouldn't 𝔹Aℝ𝔽!",
  402. Application.Driver?.Clipboard.GetClipboardData ()
  403. );
  404. Assert.Equal (string.Empty, _textView.Text);
  405. _textView.Paste ();
  406. Assert.Equal ("TextView with some more test text. Unicode shouldn't 𝔹Aℝ𝔽!", _textView.Text);
  407. }
  408. [Fact]
  409. [TextViewTestsSetupFakeApplication]
  410. public void Copy_Without_Selection ()
  411. {
  412. _textView.Text = "This is the first line.\nThis is the second line.\n";
  413. _textView.CursorPosition = new (0, _textView.Lines - 1);
  414. _textView.NewKeyDownEvent (Key.C.WithCtrl); // Copy
  415. _textView.NewKeyDownEvent (Key.Y.WithCtrl); // Paste
  416. Assert.Equal (
  417. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}{Environment.NewLine}",
  418. _textView.Text
  419. );
  420. _textView.CursorPosition = new (3, 1);
  421. _textView.NewKeyDownEvent (Key.C.WithCtrl); // Copy
  422. _textView.NewKeyDownEvent (Key.Y.WithCtrl); // Paste
  423. Assert.Equal (
  424. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the second line.{Environment.NewLine}{Environment.NewLine}",
  425. _textView.Text
  426. );
  427. Assert.Equal (new (3, 2), _textView.CursorPosition);
  428. _textView.NewKeyDownEvent (Key.Y.WithCtrl); // Paste
  429. Assert.Equal (
  430. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the second line.{Environment.NewLine}{Environment.NewLine}",
  431. _textView.Text
  432. );
  433. Assert.Equal (new (3, 3), _textView.CursorPosition);
  434. }
  435. [Fact]
  436. [TextViewTestsSetupFakeApplication]
  437. public void Cursor_Position_Multiline_False_Initialization ()
  438. {
  439. Assert.False (_textView.IsInitialized);
  440. Assert.True (_textView.Multiline);
  441. _textView.Multiline = false;
  442. Assert.Equal (32, _textView.CursorPosition.X);
  443. Assert.Equal (0, _textView.CursorPosition.Y);
  444. Assert.Equal (0, _textView.SelectedLength);
  445. Assert.Equal ("", _textView.SelectedText);
  446. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  447. }
  448. [Fact]
  449. [TextViewTestsSetupFakeApplication]
  450. public void CursorPosition_With_Value_Greater_Than_Text_Length_Changes_To_Text_Length ()
  451. {
  452. _textView.CursorPosition = new (33, 1);
  453. Assert.Equal (32, _textView.CursorPosition.X);
  454. Assert.Equal (0, _textView.CursorPosition.Y);
  455. Assert.Equal (0, _textView.SelectedLength);
  456. Assert.Equal ("", _textView.SelectedText);
  457. }
  458. [Fact]
  459. [TextViewTestsSetupFakeApplication]
  460. public void CursorPosition_With_Value_Less_Than_Zero_Changes_To_Zero ()
  461. {
  462. _textView.CursorPosition = new (-1, -1);
  463. Assert.Equal (0, _textView.CursorPosition.X);
  464. Assert.Equal (0, _textView.CursorPosition.Y);
  465. Assert.Equal (0, _textView.SelectedLength);
  466. Assert.Equal ("", _textView.SelectedText);
  467. }
  468. [Fact]
  469. [TextViewTestsSetupFakeApplication]
  470. public void Cut_Not_Allowed_If_ReadOnly_Is_True ()
  471. {
  472. _textView.ReadOnly = true;
  473. _textView.SelectionStartColumn = 20;
  474. _textView.SelectionStartRow = 0;
  475. _textView.CursorPosition = new (24, 0);
  476. _textView.NewKeyDownEvent (Key.C.WithCtrl); // Copy
  477. Assert.Equal ("text", _textView.SelectedText);
  478. _textView.NewKeyDownEvent (
  479. Key.W.WithCtrl
  480. ); // Selecting is set to false after Cut.
  481. Assert.Equal ("", _textView.SelectedText);
  482. _textView.ReadOnly = false;
  483. Assert.False (_textView.IsSelecting);
  484. _textView.IsSelecting = true; // Needed to set Selecting to true.
  485. _textView.NewKeyDownEvent (Key.C.WithCtrl); // Copy
  486. Assert.Equal ("text", _textView.SelectedText);
  487. _textView.NewKeyDownEvent (Key.W.WithCtrl); // Cut
  488. Assert.Equal ("", _textView.SelectedText);
  489. }
  490. [Fact]
  491. [SetupFakeApplication]
  492. public void DeleteTextBackwards_WordWrap_False_Return_Undo ()
  493. {
  494. const string text = "This is the first line.\nThis is the second line.\n";
  495. var tv = new TextView { Width = Dim.Fill (), Height = Dim.Fill (), Text = text };
  496. string envText = tv.Text;
  497. var top = new Toplevel ();
  498. top.Add (tv);
  499. SessionToken rs = Application.Begin (top);
  500. SetupFakeApplicationAttribute.RunIteration ();
  501. Assert.False (tv.WordWrap);
  502. Assert.Equal (Point.Empty, tv.CursorPosition);
  503. DriverAssert.AssertDriverContentsWithFrameAre (
  504. @"
  505. This is the first line.
  506. This is the second line.
  507. ",
  508. _output
  509. );
  510. tv.CursorPosition = new (3, 0);
  511. Assert.Equal (new (3, 0), tv.CursorPosition);
  512. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  513. SetupFakeApplicationAttribute.RunIteration ();
  514. Assert.Equal (new (2, 0), tv.CursorPosition);
  515. DriverAssert.AssertDriverContentsWithFrameAre (
  516. @"
  517. Ths is the first line.
  518. This is the second line.
  519. ",
  520. _output
  521. );
  522. tv.CursorPosition = new (0, 1);
  523. Assert.Equal (new (0, 1), tv.CursorPosition);
  524. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  525. SetupFakeApplicationAttribute.RunIteration ();
  526. Assert.Equal (new (22, 0), tv.CursorPosition);
  527. DriverAssert.AssertDriverContentsWithFrameAre (
  528. @"
  529. Ths is the first line.This is the second line.
  530. ",
  531. _output
  532. );
  533. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  534. SetupFakeApplicationAttribute.RunIteration ();
  535. Assert.Equal (new (0, 1), tv.CursorPosition);
  536. DriverAssert.AssertDriverContentsWithFrameAre (
  537. @"
  538. Ths is the first line.
  539. This is the second line.
  540. ",
  541. _output
  542. );
  543. while (tv.Text != envText)
  544. {
  545. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  546. }
  547. SetupFakeApplicationAttribute.RunIteration ();
  548. Assert.Equal (envText, tv.Text);
  549. Assert.Equal (new (3, 0), tv.CursorPosition);
  550. Assert.False (tv.IsDirty);
  551. top.Dispose ();
  552. }
  553. [Fact]
  554. [SetupFakeApplication]
  555. public void DeleteTextBackwards_WordWrap_True_Return_Undo ()
  556. {
  557. const string text = "This is the first line.\nThis is the second line.\n";
  558. var tv = new TextView { Width = Dim.Fill (), Height = Dim.Fill (), Text = text, WordWrap = true };
  559. string envText = tv.Text;
  560. var top = new Toplevel ();
  561. top.Add (tv);
  562. SessionToken rs = Application.Begin (top);
  563. SetupFakeApplicationAttribute.RunIteration ();
  564. Assert.True (tv.WordWrap);
  565. Assert.Equal (Point.Empty, tv.CursorPosition);
  566. DriverAssert.AssertDriverContentsWithFrameAre (
  567. @"
  568. This is the first line.
  569. This is the second line.
  570. ",
  571. _output
  572. );
  573. tv.CursorPosition = new (3, 0);
  574. Assert.Equal (new (3, 0), tv.CursorPosition);
  575. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  576. SetupFakeApplicationAttribute.RunIteration ();
  577. Assert.Equal (new (2, 0), tv.CursorPosition);
  578. DriverAssert.AssertDriverContentsWithFrameAre (
  579. @"
  580. Ths is the first line.
  581. This is the second line.
  582. ",
  583. _output
  584. );
  585. tv.CursorPosition = new (0, 1);
  586. Assert.Equal (new (0, 1), tv.CursorPosition);
  587. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  588. SetupFakeApplicationAttribute.RunIteration ();
  589. Assert.Equal (new (22, 0), tv.CursorPosition);
  590. DriverAssert.AssertDriverContentsWithFrameAre (
  591. @"
  592. Ths is the first line.This is the second line.
  593. ",
  594. _output
  595. );
  596. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  597. SetupFakeApplicationAttribute.RunIteration ();
  598. Assert.Equal (new (0, 1), tv.CursorPosition);
  599. DriverAssert.AssertDriverContentsWithFrameAre (
  600. @"
  601. Ths is the first line.
  602. This is the second line.
  603. ",
  604. _output
  605. );
  606. while (tv.Text != envText)
  607. {
  608. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  609. }
  610. SetupFakeApplicationAttribute.RunIteration ();
  611. Assert.Equal (envText, tv.Text);
  612. Assert.Equal (new (3, 0), tv.CursorPosition);
  613. Assert.False (tv.IsDirty);
  614. top.Dispose ();
  615. }
  616. [Fact]
  617. [SetupFakeApplication]
  618. public void DeleteTextForwards_WordWrap_False_Return_Undo ()
  619. {
  620. const string text = "This is the first line.\nThis is the second line.\n";
  621. var tv = new TextView { Width = Dim.Fill (), Height = Dim.Fill (), Text = text };
  622. string envText = tv.Text;
  623. var top = new Toplevel ();
  624. top.Add (tv);
  625. SessionToken rs = Application.Begin (top);
  626. SetupFakeApplicationAttribute.RunIteration ();
  627. Assert.False (tv.WordWrap);
  628. Assert.Equal (Point.Empty, tv.CursorPosition);
  629. DriverAssert.AssertDriverContentsWithFrameAre (
  630. @"
  631. This is the first line.
  632. This is the second line.
  633. ",
  634. _output
  635. );
  636. tv.CursorPosition = new (2, 0);
  637. Assert.Equal (new (2, 0), tv.CursorPosition);
  638. Assert.True (tv.NewKeyDownEvent (Key.Delete));
  639. SetupFakeApplicationAttribute.RunIteration ();
  640. Assert.Equal (new (2, 0), tv.CursorPosition);
  641. DriverAssert.AssertDriverContentsWithFrameAre (
  642. @"
  643. Ths is the first line.
  644. This is the second line.
  645. ",
  646. _output
  647. );
  648. tv.CursorPosition = new (22, 0);
  649. Assert.Equal (new (22, 0), tv.CursorPosition);
  650. Assert.True (tv.NewKeyDownEvent (Key.Delete));
  651. SetupFakeApplicationAttribute.RunIteration ();
  652. Assert.Equal (new (22, 0), tv.CursorPosition);
  653. DriverAssert.AssertDriverContentsWithFrameAre (
  654. @"
  655. Ths is the first line.This is the second line.
  656. ",
  657. _output
  658. );
  659. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  660. SetupFakeApplicationAttribute.RunIteration ();
  661. Assert.Equal (new (0, 1), tv.CursorPosition);
  662. DriverAssert.AssertDriverContentsWithFrameAre (
  663. @"
  664. Ths is the first line.
  665. This is the second line.
  666. ",
  667. _output
  668. );
  669. while (tv.Text != envText)
  670. {
  671. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  672. }
  673. Assert.Equal (envText, tv.Text);
  674. Assert.Equal (new (2, 0), tv.CursorPosition);
  675. Assert.False (tv.IsDirty);
  676. top.Dispose ();
  677. }
  678. [Fact]
  679. [SetupFakeApplication]
  680. public void DeleteTextForwards_WordWrap_True_Return_Undo ()
  681. {
  682. const string text = "This is the first line.\nThis is the second line.\n";
  683. var tv = new TextView { Width = Dim.Fill (), Height = Dim.Fill (), Text = text, WordWrap = true };
  684. string envText = tv.Text;
  685. var top = new Toplevel ();
  686. top.Add (tv);
  687. SessionToken rs = Application.Begin (top);
  688. SetupFakeApplicationAttribute.RunIteration ();
  689. Assert.True (tv.WordWrap);
  690. Assert.Equal (Point.Empty, tv.CursorPosition);
  691. DriverAssert.AssertDriverContentsWithFrameAre (
  692. @"
  693. This is the first line.
  694. This is the second line.
  695. ",
  696. _output
  697. );
  698. tv.CursorPosition = new (2, 0);
  699. Assert.Equal (new (2, 0), tv.CursorPosition);
  700. Assert.True (tv.NewKeyDownEvent (Key.Delete));
  701. SetupFakeApplicationAttribute.RunIteration ();
  702. Assert.Equal (new (2, 0), tv.CursorPosition);
  703. DriverAssert.AssertDriverContentsWithFrameAre (
  704. @"
  705. Ths is the first line.
  706. This is the second line.
  707. ",
  708. _output
  709. );
  710. tv.CursorPosition = new (22, 0);
  711. Assert.Equal (new (22, 0), tv.CursorPosition);
  712. Assert.True (tv.NewKeyDownEvent (Key.Delete));
  713. SetupFakeApplicationAttribute.RunIteration ();
  714. Assert.Equal (new (22, 0), tv.CursorPosition);
  715. DriverAssert.AssertDriverContentsWithFrameAre (
  716. @"
  717. Ths is the first line.This is the second line.
  718. ",
  719. _output
  720. );
  721. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  722. SetupFakeApplicationAttribute.RunIteration ();
  723. Assert.Equal (new (0, 1), tv.CursorPosition);
  724. DriverAssert.AssertDriverContentsWithFrameAre (
  725. @"
  726. Ths is the first line.
  727. This is the second line.
  728. ",
  729. _output
  730. );
  731. while (tv.Text != envText)
  732. {
  733. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  734. }
  735. SetupFakeApplicationAttribute.RunIteration ();
  736. Assert.Equal (envText, tv.Text);
  737. Assert.Equal (new (2, 0), tv.CursorPosition);
  738. Assert.False (tv.IsDirty);
  739. top.Dispose ();
  740. }
  741. [Fact]
  742. [TextViewTestsSetupFakeApplication]
  743. public void DesiredCursorVisibility_Horizontal_Navigation ()
  744. {
  745. var text = "";
  746. for (var i = 0; i < 12; i++)
  747. {
  748. text += $"{i.ToString () [^1]}";
  749. }
  750. var tv = new TextView { Width = 10, Height = 10 };
  751. tv.Text = text;
  752. var top = new Toplevel ();
  753. top.Add (tv);
  754. Application.Begin (top);
  755. Assert.Equal (0, tv.LeftColumn);
  756. Assert.Equal (Point.Empty, tv.CursorPosition);
  757. Application.PositionCursor ();
  758. Assert.Equal (CursorVisibility.Default, tv.CursorVisibility);
  759. for (var i = 0; i < 12; i++)
  760. {
  761. tv.NewMouseEvent (new () { Flags = MouseFlags.WheeledRight });
  762. Assert.Equal (Math.Min (i + 1, 11), tv.LeftColumn);
  763. Application.PositionCursor ();
  764. Application.Driver!.GetCursorVisibility (out CursorVisibility cursorVisibility);
  765. Assert.Equal (CursorVisibility.Invisible, cursorVisibility);
  766. }
  767. for (var i = 11; i > 0; i--)
  768. {
  769. tv.NewMouseEvent (new () { Flags = MouseFlags.WheeledLeft });
  770. Assert.Equal (i - 1, tv.LeftColumn);
  771. Application.PositionCursor ();
  772. Application.Driver!.GetCursorVisibility (out CursorVisibility cursorVisibility);
  773. if (i - 1 == 0)
  774. {
  775. Assert.Equal (CursorVisibility.Default, cursorVisibility);
  776. }
  777. else
  778. {
  779. Assert.Equal (CursorVisibility.Invisible, cursorVisibility);
  780. }
  781. }
  782. top.Dispose ();
  783. Application.Shutdown ();
  784. }
  785. [Fact]
  786. [TextViewTestsSetupFakeApplication]
  787. public void DesiredCursorVisibility_Vertical_Navigation ()
  788. {
  789. var text = "";
  790. for (var i = 0; i < 12; i++)
  791. {
  792. text += $"This is the line {i}\n";
  793. }
  794. var tv = new TextView { Width = 10, Height = 10 };
  795. tv.Text = text;
  796. var top = new Toplevel ();
  797. top.Add (tv);
  798. Application.Begin (top);
  799. Assert.Equal (0, tv.TopRow);
  800. Application.PositionCursor ();
  801. Assert.Equal (CursorVisibility.Default, tv.CursorVisibility);
  802. for (var i = 0; i < 12; i++)
  803. {
  804. tv.NewMouseEvent (new () { Flags = MouseFlags.WheeledDown });
  805. Application.PositionCursor ();
  806. Assert.Equal (i + 1, tv.TopRow);
  807. Application.Driver!.GetCursorVisibility (out CursorVisibility cursorVisibility);
  808. Assert.Equal (CursorVisibility.Invisible, cursorVisibility);
  809. }
  810. for (var i = 12; i > 0; i--)
  811. {
  812. tv.NewMouseEvent (new () { Flags = MouseFlags.WheeledUp });
  813. Application.PositionCursor ();
  814. Assert.Equal (i - 1, tv.TopRow);
  815. Application.PositionCursor ();
  816. Application.Driver!.GetCursorVisibility (out CursorVisibility cursorVisibility);
  817. if (i - 1 == 0)
  818. {
  819. Assert.Equal (CursorVisibility.Default, cursorVisibility);
  820. }
  821. else
  822. {
  823. Assert.Equal (CursorVisibility.Invisible, cursorVisibility);
  824. }
  825. }
  826. top.Dispose ();
  827. Application.Shutdown ();
  828. }
  829. [Fact]
  830. [AutoInitShutdown ()]
  831. public void HistoryText_Undo_Redo_Copy_Without_Selection_Multi_Line_Paste ()
  832. {
  833. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  834. var tv = new TextView { Text = text };
  835. tv.CursorPosition = new (23, 0);
  836. Assert.True (tv.NewKeyDownEvent (Key.C.WithCtrl));
  837. Assert.Equal (
  838. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  839. tv.Text
  840. );
  841. Assert.Equal ("", tv.SelectedText);
  842. Assert.Equal ("This is the first line.", Clipboard.Contents);
  843. Assert.Equal (3, tv.Lines);
  844. Assert.Equal (new (23, 0), tv.CursorPosition);
  845. Assert.True (tv.NewKeyDownEvent (Key.Y.WithCtrl));
  846. Assert.Equal (
  847. $"This is the first line.{Environment.NewLine}This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  848. tv.Text
  849. );
  850. Assert.Equal (4, tv.Lines);
  851. Assert.Equal (new (23, 1), tv.CursorPosition);
  852. // Undo
  853. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  854. Assert.Equal (
  855. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  856. tv.Text
  857. );
  858. Assert.Equal (3, tv.Lines);
  859. Assert.Equal (new (23, 0), tv.CursorPosition);
  860. // Redo
  861. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  862. Assert.Equal (
  863. $"This is the first line.{Environment.NewLine}This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  864. tv.Text
  865. );
  866. Assert.Equal (4, tv.Lines);
  867. Assert.Equal (new (23, 1), tv.CursorPosition);
  868. }
  869. [Fact]
  870. [SetupFakeApplication]
  871. public void HistoryText_Undo_Redo_Cut_Multi_Line_Another_Selected_Paste ()
  872. {
  873. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  874. var tv = new TextView { Text = text };
  875. tv.SelectionStartColumn = 12;
  876. tv.CursorPosition = new (17, 0);
  877. Assert.True (tv.NewKeyDownEvent (Key.W.WithCtrl));
  878. Assert.Equal (
  879. $"This is the line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  880. tv.Text
  881. );
  882. Assert.Equal ("", tv.SelectedText);
  883. Assert.Equal (3, tv.Lines);
  884. Assert.Equal (new (12, 0), tv.CursorPosition);
  885. tv.SelectionStartColumn = 12;
  886. tv.SelectionStartRow = 1;
  887. tv.CursorPosition = new (18, 1);
  888. Assert.True (tv.NewKeyDownEvent (Key.Y.WithCtrl));
  889. Assert.Equal (
  890. $"This is the line.{Environment.NewLine}This is the first line.{Environment.NewLine}This is the third line.",
  891. tv.Text
  892. );
  893. Assert.Equal (3, tv.Lines);
  894. Assert.Equal (new (17, 1), tv.CursorPosition);
  895. // Undo
  896. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  897. Assert.Equal (
  898. $"This is the line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  899. tv.Text
  900. );
  901. Assert.Equal (3, tv.Lines);
  902. Assert.Equal (new (12, 1), tv.CursorPosition);
  903. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  904. Assert.Equal (
  905. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  906. tv.Text
  907. );
  908. Assert.Equal (3, tv.Lines);
  909. Assert.Equal (new (12, 0), tv.CursorPosition);
  910. // Redo
  911. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  912. Assert.Equal (
  913. $"This is the line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  914. tv.Text
  915. );
  916. Assert.Equal (3, tv.Lines);
  917. Assert.Equal (new (12, 0), tv.CursorPosition);
  918. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  919. Assert.Equal (
  920. $"This is the line.{Environment.NewLine}This is the first line.{Environment.NewLine}This is the third line.",
  921. tv.Text
  922. );
  923. Assert.Equal (3, tv.Lines);
  924. Assert.Equal (new (17, 1), tv.CursorPosition);
  925. }
  926. [Fact]
  927. [SetupFakeApplication]
  928. public void HistoryText_Undo_Redo_Cut_Multi_Line_Selected_Paste ()
  929. {
  930. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  931. var tv = new TextView { Text = text };
  932. tv.SelectionStartColumn = 12;
  933. tv.CursorPosition = new (17, 0);
  934. Assert.True (tv.NewKeyDownEvent (Key.W.WithCtrl));
  935. Assert.Equal (
  936. $"This is the line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  937. tv.Text
  938. );
  939. Assert.Equal ("", tv.SelectedText);
  940. Assert.Equal (3, tv.Lines);
  941. Assert.Equal (new (12, 0), tv.CursorPosition);
  942. tv.SelectionStartColumn = 12;
  943. tv.CursorPosition = new (11, 1);
  944. Assert.True (tv.NewKeyDownEvent (Key.Y.WithCtrl));
  945. Assert.Equal ($"This is the first second line.{Environment.NewLine}This is the third line.", tv.Text);
  946. Assert.Equal (2, tv.Lines);
  947. Assert.Equal (new (17, 0), tv.CursorPosition);
  948. // Undo
  949. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  950. Assert.Equal (
  951. $"This is the line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  952. tv.Text
  953. );
  954. Assert.Equal (3, tv.Lines);
  955. Assert.Equal (new (12, 0), tv.CursorPosition);
  956. // Redo
  957. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  958. Assert.Equal ($"This is the first second line.{Environment.NewLine}This is the third line.", tv.Text);
  959. Assert.Equal (2, tv.Lines);
  960. Assert.Equal (new (17, 0), tv.CursorPosition);
  961. }
  962. [Fact]
  963. [SetupFakeApplication]
  964. public void HistoryText_Undo_Redo_Cut_Simple_Paste_Starting ()
  965. {
  966. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  967. var tv = new TextView { Text = text };
  968. tv.SelectionStartColumn = 12;
  969. tv.CursorPosition = new (18, 1);
  970. Assert.True (tv.NewKeyDownEvent (Key.W.WithCtrl));
  971. Assert.Equal ($"This is the line.{Environment.NewLine}This is the third line.", tv.Text);
  972. Assert.Equal ("", tv.SelectedText);
  973. Assert.Equal (2, tv.Lines);
  974. Assert.Equal (new (12, 0), tv.CursorPosition);
  975. tv.IsSelecting = false;
  976. Assert.True (tv.NewKeyDownEvent (Key.Y.WithCtrl));
  977. Assert.Equal (
  978. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  979. tv.Text
  980. );
  981. Assert.Equal (3, tv.Lines);
  982. Assert.Equal (new (18, 1), tv.CursorPosition);
  983. // Undo
  984. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  985. Assert.Equal ($"This is the line.{Environment.NewLine}This is the third line.", tv.Text);
  986. Assert.Equal (2, tv.Lines);
  987. Assert.Equal (new (12, 0), tv.CursorPosition);
  988. // Redo
  989. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  990. Assert.Equal (
  991. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  992. tv.Text
  993. );
  994. Assert.Equal (3, tv.Lines);
  995. Assert.Equal (new (18, 1), tv.CursorPosition);
  996. }
  997. [Fact]
  998. [SetupFakeApplication]
  999. public void HistoryText_Undo_Redo_Empty_Copy_Without_Selection_Multi_Line_Selected_Paste ()
  1000. {
  1001. var text = "\nThis is the first line.\nThis is the second line.";
  1002. var tv = new TextView { Text = text };
  1003. Assert.True (tv.NewKeyDownEvent (Key.C.WithCtrl));
  1004. Assert.Equal (
  1005. $"{Environment.NewLine}This is the first line.{Environment.NewLine}This is the second line.",
  1006. tv.Text
  1007. );
  1008. Assert.Equal ("", tv.SelectedText);
  1009. Assert.Equal (3, tv.Lines);
  1010. Assert.Equal (Point.Empty, tv.CursorPosition);
  1011. Assert.True (tv.NewKeyDownEvent (Key.Y.WithCtrl));
  1012. Assert.Equal (
  1013. $"{Environment.NewLine}{Environment.NewLine}This is the first line.{Environment.NewLine}This is the second line.",
  1014. tv.Text
  1015. );
  1016. Assert.Equal (4, tv.Lines);
  1017. Assert.Equal (new (0, 1), tv.CursorPosition);
  1018. // Undo
  1019. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1020. Assert.Equal (
  1021. $"{Environment.NewLine}This is the first line.{Environment.NewLine}This is the second line.",
  1022. tv.Text
  1023. );
  1024. Assert.Equal (3, tv.Lines);
  1025. Assert.Equal (Point.Empty, tv.CursorPosition);
  1026. // Redo
  1027. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1028. Assert.Equal (
  1029. $"{Environment.NewLine}{Environment.NewLine}This is the first line.{Environment.NewLine}This is the second line.",
  1030. tv.Text
  1031. );
  1032. Assert.Equal (4, tv.Lines);
  1033. Assert.Equal (new (0, 1), tv.CursorPosition);
  1034. }
  1035. [Fact]
  1036. [SetupFakeApplication]
  1037. public void HistoryText_Undo_Redo_KillToEndOfLine ()
  1038. {
  1039. var text = "First line.\nSecond line.";
  1040. var tv = new TextView { Text = text };
  1041. Assert.True (tv.NewKeyDownEvent (Key.K.WithCtrl));
  1042. Assert.Equal ($"{Environment.NewLine}Second line.", tv.Text);
  1043. Assert.Equal ("", tv.SelectedText);
  1044. Assert.Equal ("First line.", Clipboard.Contents);
  1045. Assert.Equal (2, tv.Lines);
  1046. Assert.Equal (Point.Empty, tv.CursorPosition);
  1047. Assert.True (tv.NewKeyDownEvent (Key.K.WithCtrl));
  1048. Assert.Equal ("Second line.", tv.Text);
  1049. Assert.Equal ("", tv.SelectedText);
  1050. Assert.Equal ($"First line.{Environment.NewLine}", Clipboard.Contents);
  1051. Assert.Equal (1, tv.Lines);
  1052. Assert.Equal (Point.Empty, tv.CursorPosition);
  1053. Assert.True (tv.NewKeyDownEvent (Key.K.WithCtrl));
  1054. Assert.Equal ("", tv.Text);
  1055. Assert.Equal ("", tv.SelectedText);
  1056. Assert.Equal ($"First line.{Environment.NewLine}Second line.", Clipboard.Contents);
  1057. Assert.Equal (1, tv.Lines);
  1058. Assert.Equal (Point.Empty, tv.CursorPosition);
  1059. // Undo
  1060. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1061. Assert.Equal ("Second line.", tv.Text);
  1062. Assert.Equal (1, tv.Lines);
  1063. Assert.Equal (Point.Empty, tv.CursorPosition);
  1064. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1065. Assert.Equal ($"{Environment.NewLine}Second line.", tv.Text);
  1066. Assert.Equal (2, tv.Lines);
  1067. Assert.Equal (Point.Empty, tv.CursorPosition);
  1068. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1069. Assert.Equal ($"First line.{Environment.NewLine}Second line.", tv.Text);
  1070. Assert.Equal (2, tv.Lines);
  1071. Assert.Equal (Point.Empty, tv.CursorPosition);
  1072. // Redo
  1073. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1074. Assert.Equal ($"{Environment.NewLine}Second line.", tv.Text);
  1075. Assert.Equal (2, tv.Lines);
  1076. Assert.Equal (Point.Empty, tv.CursorPosition);
  1077. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1078. Assert.Equal ("Second line.", tv.Text);
  1079. Assert.Equal (1, tv.Lines);
  1080. Assert.Equal (Point.Empty, tv.CursorPosition);
  1081. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1082. Assert.Equal ("", tv.Text);
  1083. Assert.Equal (1, tv.Lines);
  1084. Assert.Equal (Point.Empty, tv.CursorPosition);
  1085. }
  1086. [Fact]
  1087. [SetupFakeApplication]
  1088. public void HistoryText_Undo_Redo_KillToLeftStart ()
  1089. {
  1090. var text = "First line.\nSecond line.";
  1091. var tv = new TextView { Text = text };
  1092. Assert.True (tv.NewKeyDownEvent (Key.End.WithCtrl));
  1093. Assert.Equal ($"First line.{Environment.NewLine}Second line.", tv.Text);
  1094. Assert.Equal ("", tv.SelectedText);
  1095. Assert.Equal (2, tv.Lines);
  1096. Assert.Equal (new (12, 1), tv.CursorPosition);
  1097. Assert.True (tv.NewKeyDownEvent (Key.Backspace.WithCtrl.WithShift));
  1098. Assert.Equal ($"First line.{Environment.NewLine}", tv.Text);
  1099. Assert.Equal ("", tv.SelectedText);
  1100. Assert.Equal ("Second line.", Clipboard.Contents);
  1101. Assert.Equal (2, tv.Lines);
  1102. Assert.Equal (new (0, 1), tv.CursorPosition);
  1103. Assert.True (tv.NewKeyDownEvent (Key.Backspace.WithCtrl.WithShift));
  1104. Assert.Equal ("First line.", tv.Text);
  1105. Assert.Equal ("", tv.SelectedText);
  1106. Assert.Equal ($"Second line.{Environment.NewLine}", Clipboard.Contents);
  1107. Assert.Equal (1, tv.Lines);
  1108. Assert.Equal (new (11, 0), tv.CursorPosition);
  1109. Assert.True (tv.NewKeyDownEvent (Key.Backspace.WithCtrl.WithShift));
  1110. Assert.Equal ("", tv.Text);
  1111. Assert.Equal ("", tv.SelectedText);
  1112. Assert.Equal ($"Second line.{Environment.NewLine}First line.", Clipboard.Contents);
  1113. Assert.Equal (1, tv.Lines);
  1114. Assert.Equal (Point.Empty, tv.CursorPosition);
  1115. // Undo
  1116. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1117. Assert.Equal ("First line.", tv.Text);
  1118. Assert.Equal (1, tv.Lines);
  1119. Assert.Equal (new (11, 0), tv.CursorPosition);
  1120. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1121. Assert.Equal ($"First line.{Environment.NewLine}", tv.Text);
  1122. Assert.Equal (2, tv.Lines);
  1123. Assert.Equal (new (0, 1), tv.CursorPosition);
  1124. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1125. Assert.Equal ($"First line.{Environment.NewLine}Second line.", tv.Text);
  1126. Assert.Equal (2, tv.Lines);
  1127. Assert.Equal (new (12, 1), tv.CursorPosition);
  1128. // Redo
  1129. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1130. Assert.Equal ($"First line.{Environment.NewLine}", tv.Text);
  1131. Assert.Equal (2, tv.Lines);
  1132. Assert.Equal (new (0, 1), tv.CursorPosition);
  1133. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1134. Assert.Equal ("First line.", tv.Text);
  1135. Assert.Equal (1, tv.Lines);
  1136. Assert.Equal (new (11, 0), tv.CursorPosition);
  1137. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1138. Assert.Equal ("", tv.Text);
  1139. Assert.Equal (1, tv.Lines);
  1140. Assert.Equal (Point.Empty, tv.CursorPosition);
  1141. }
  1142. [Fact]
  1143. [SetupFakeApplication]
  1144. public void HistoryText_Undo_Redo_Multi_Line_DeleteCharLeft ()
  1145. {
  1146. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  1147. var tv = new TextView { Width = 10, Height = 2, Text = text };
  1148. Toplevel top = new ();
  1149. top.Add (tv);
  1150. Application.Begin (top);
  1151. Assert.Equal (
  1152. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1153. tv.Text
  1154. );
  1155. Assert.Equal (3, tv.Lines);
  1156. Assert.Equal (Point.Empty, tv.CursorPosition);
  1157. var ntimes = 3;
  1158. tv.CursorPosition = new (7, 1);
  1159. for (var i = 0; i < ntimes; i++)
  1160. {
  1161. tv.DeleteCharLeft ();
  1162. }
  1163. Assert.Equal (
  1164. $"This is the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  1165. tv.Text
  1166. );
  1167. Assert.Equal (3, tv.Lines);
  1168. Assert.Equal (new (4, 1), tv.CursorPosition);
  1169. tv.CursorPosition = new (7, 0);
  1170. for (var i = 0; i < ntimes; i++)
  1171. {
  1172. tv.DeleteCharLeft ();
  1173. }
  1174. Assert.Equal (
  1175. $"This the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  1176. tv.Text
  1177. );
  1178. Assert.Equal (3, tv.Lines);
  1179. Assert.Equal (new (4, 0), tv.CursorPosition);
  1180. tv.CursorPosition = new (7, 2);
  1181. for (var i = 0; i < ntimes; i++)
  1182. {
  1183. tv.DeleteCharLeft ();
  1184. }
  1185. Assert.Equal (
  1186. $"This the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This the third line.",
  1187. tv.Text
  1188. );
  1189. Assert.Equal (3, tv.Lines);
  1190. Assert.Equal (new (4, 2), tv.CursorPosition);
  1191. for (var i = 0; i < ntimes; i++)
  1192. {
  1193. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1194. switch (i)
  1195. {
  1196. case 0:
  1197. Assert.Equal (
  1198. $"This the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This the third line.",
  1199. tv.Text
  1200. );
  1201. Assert.Equal (new (5, 2), tv.CursorPosition);
  1202. break;
  1203. case 1:
  1204. Assert.Equal (
  1205. $"This the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This i the third line.",
  1206. tv.Text
  1207. );
  1208. Assert.Equal (new (6, 2), tv.CursorPosition);
  1209. break;
  1210. case 2:
  1211. Assert.Equal (
  1212. $"This the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  1213. tv.Text
  1214. );
  1215. Assert.Equal (new (7, 2), tv.CursorPosition);
  1216. break;
  1217. }
  1218. }
  1219. Assert.Equal (
  1220. $"This the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  1221. tv.Text
  1222. );
  1223. Assert.Equal (3, tv.Lines);
  1224. Assert.Equal (new (7, 2), tv.CursorPosition);
  1225. for (var i = 0; i < ntimes; i++)
  1226. {
  1227. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1228. switch (i)
  1229. {
  1230. case 0:
  1231. Assert.Equal (
  1232. $"This the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  1233. tv.Text
  1234. );
  1235. Assert.Equal (new (5, 0), tv.CursorPosition);
  1236. break;
  1237. case 1:
  1238. Assert.Equal (
  1239. $"This i the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  1240. tv.Text
  1241. );
  1242. Assert.Equal (new (6, 0), tv.CursorPosition);
  1243. break;
  1244. case 2:
  1245. Assert.Equal (
  1246. $"This is the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  1247. tv.Text
  1248. );
  1249. Assert.Equal (new (7, 0), tv.CursorPosition);
  1250. break;
  1251. }
  1252. }
  1253. Assert.Equal (
  1254. $"This is the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  1255. tv.Text
  1256. );
  1257. Assert.Equal (3, tv.Lines);
  1258. Assert.Equal (new (7, 0), tv.CursorPosition);
  1259. for (var i = 0; i < ntimes; i++)
  1260. {
  1261. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1262. switch (i)
  1263. {
  1264. case 0:
  1265. Assert.Equal (
  1266. $"This is the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  1267. tv.Text
  1268. );
  1269. Assert.Equal (new (5, 1), tv.CursorPosition);
  1270. break;
  1271. case 1:
  1272. Assert.Equal (
  1273. $"This is the first line.{Environment.NewLine}This i the second line.{Environment.NewLine}This is the third line.",
  1274. tv.Text
  1275. );
  1276. Assert.Equal (new (6, 1), tv.CursorPosition);
  1277. break;
  1278. case 2:
  1279. Assert.Equal (
  1280. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1281. tv.Text
  1282. );
  1283. Assert.Equal (new (7, 1), tv.CursorPosition);
  1284. break;
  1285. }
  1286. }
  1287. Assert.Equal (
  1288. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1289. tv.Text
  1290. );
  1291. Assert.Equal (3, tv.Lines);
  1292. Assert.Equal (new (7, 1), tv.CursorPosition);
  1293. for (var i = 0; i < ntimes; i++)
  1294. {
  1295. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1296. }
  1297. Assert.Equal (
  1298. $"This is the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  1299. tv.Text
  1300. );
  1301. Assert.Equal (3, tv.Lines);
  1302. Assert.Equal (new (4, 1), tv.CursorPosition);
  1303. for (var i = 0; i < ntimes; i++)
  1304. {
  1305. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1306. }
  1307. Assert.Equal (
  1308. $"This the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  1309. tv.Text
  1310. );
  1311. Assert.Equal (3, tv.Lines);
  1312. Assert.Equal (new (4, 0), tv.CursorPosition);
  1313. for (var i = 0; i < ntimes; i++)
  1314. {
  1315. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1316. }
  1317. Assert.Equal (
  1318. $"This the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This the third line.",
  1319. tv.Text
  1320. );
  1321. Assert.Equal (3, tv.Lines);
  1322. Assert.Equal (new (4, 2), tv.CursorPosition);
  1323. top.Dispose ();
  1324. }
  1325. [Fact]
  1326. [SetupFakeApplication]
  1327. public void HistoryText_Undo_Redo_Multi_Line_DeleteCharRight ()
  1328. {
  1329. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  1330. var tv = new TextView { Width = 10, Height = 2, Text = text };
  1331. Toplevel top = new ();
  1332. top.Add (tv);
  1333. Application.Begin (top);
  1334. Assert.Equal (
  1335. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1336. tv.Text
  1337. );
  1338. Assert.Equal (3, tv.Lines);
  1339. Assert.Equal (Point.Empty, tv.CursorPosition);
  1340. var ntimes = 3;
  1341. tv.CursorPosition = new (7, 1);
  1342. for (var i = 0; i < ntimes; i++)
  1343. {
  1344. tv.DeleteCharRight ();
  1345. }
  1346. Assert.Equal (
  1347. $"This is the first line.{Environment.NewLine}This ise second line.{Environment.NewLine}This is the third line.",
  1348. tv.Text
  1349. );
  1350. Assert.Equal (3, tv.Lines);
  1351. Assert.Equal (new (7, 1), tv.CursorPosition);
  1352. tv.CursorPosition = new (7, 0);
  1353. for (var i = 0; i < ntimes; i++)
  1354. {
  1355. tv.DeleteCharRight ();
  1356. }
  1357. Assert.Equal (
  1358. $"This ise first line.{Environment.NewLine}This ise second line.{Environment.NewLine}This is the third line.",
  1359. tv.Text
  1360. );
  1361. Assert.Equal (3, tv.Lines);
  1362. Assert.Equal (new (7, 0), tv.CursorPosition);
  1363. tv.CursorPosition = new (7, 2);
  1364. for (var i = 0; i < ntimes; i++)
  1365. {
  1366. tv.DeleteCharRight ();
  1367. }
  1368. Assert.Equal (
  1369. $"This ise first line.{Environment.NewLine}This ise second line.{Environment.NewLine}This ise third line.",
  1370. tv.Text
  1371. );
  1372. Assert.Equal (3, tv.Lines);
  1373. Assert.Equal (new (7, 2), tv.CursorPosition);
  1374. for (var i = 0; i < ntimes; i++)
  1375. {
  1376. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1377. }
  1378. Assert.Equal (
  1379. $"This ise first line.{Environment.NewLine}This ise second line.{Environment.NewLine}This is the third line.",
  1380. tv.Text
  1381. );
  1382. Assert.Equal (3, tv.Lines);
  1383. Assert.Equal (new (7, 2), tv.CursorPosition);
  1384. for (var i = 0; i < ntimes; i++)
  1385. {
  1386. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1387. }
  1388. Assert.Equal (
  1389. $"This is the first line.{Environment.NewLine}This ise second line.{Environment.NewLine}This is the third line.",
  1390. tv.Text
  1391. );
  1392. Assert.Equal (3, tv.Lines);
  1393. Assert.Equal (new (7, 0), tv.CursorPosition);
  1394. for (var i = 0; i < ntimes; i++)
  1395. {
  1396. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1397. }
  1398. Assert.Equal (
  1399. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1400. tv.Text
  1401. );
  1402. Assert.Equal (3, tv.Lines);
  1403. Assert.Equal (new (7, 1), tv.CursorPosition);
  1404. for (var i = 0; i < ntimes; i++)
  1405. {
  1406. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1407. }
  1408. Assert.Equal (
  1409. $"This is the first line.{Environment.NewLine}This ise second line.{Environment.NewLine}This is the third line.",
  1410. tv.Text
  1411. );
  1412. Assert.Equal (3, tv.Lines);
  1413. Assert.Equal (new (7, 1), tv.CursorPosition);
  1414. for (var i = 0; i < ntimes; i++)
  1415. {
  1416. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1417. }
  1418. Assert.Equal (
  1419. $"This ise first line.{Environment.NewLine}This ise second line.{Environment.NewLine}This is the third line.",
  1420. tv.Text
  1421. );
  1422. Assert.Equal (3, tv.Lines);
  1423. Assert.Equal (new (7, 0), tv.CursorPosition);
  1424. for (var i = 0; i < ntimes; i++)
  1425. {
  1426. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1427. }
  1428. Assert.Equal (
  1429. $"This ise first line.{Environment.NewLine}This ise second line.{Environment.NewLine}This ise third line.",
  1430. tv.Text
  1431. );
  1432. Assert.Equal (3, tv.Lines);
  1433. Assert.Equal (new (7, 2), tv.CursorPosition);
  1434. top.Dispose ();
  1435. }
  1436. [Fact]
  1437. [SetupFakeApplication]
  1438. public void HistoryText_Undo_Redo_Multi_Line_InsertText ()
  1439. {
  1440. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  1441. var tv = new TextView { Width = 10, Height = 2, Text = text };
  1442. Toplevel top = new ();
  1443. top.Add (tv);
  1444. Application.Begin (top);
  1445. Assert.Equal (
  1446. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1447. tv.Text
  1448. );
  1449. Assert.Equal (3, tv.Lines);
  1450. Assert.Equal (Point.Empty, tv.CursorPosition);
  1451. var messy = " messy";
  1452. tv.CursorPosition = new (7, 1);
  1453. tv.InsertText (messy);
  1454. Assert.Equal (
  1455. $"This is the first line.{Environment.NewLine}This is messy the second line.{Environment.NewLine}This is the third line.",
  1456. tv.Text
  1457. );
  1458. Assert.Equal (3, tv.Lines);
  1459. Assert.Equal (new (13, 1), tv.CursorPosition);
  1460. tv.CursorPosition = new (7, 0);
  1461. tv.InsertText (messy);
  1462. Assert.Equal (
  1463. $"This is messy the first line.{Environment.NewLine}This is messy the second line.{Environment.NewLine}This is the third line.",
  1464. tv.Text
  1465. );
  1466. Assert.Equal (3, tv.Lines);
  1467. Assert.Equal (new (13, 0), tv.CursorPosition);
  1468. tv.CursorPosition = new (7, 2);
  1469. tv.InsertText (messy);
  1470. Assert.Equal (
  1471. $"This is messy the first line.{Environment.NewLine}This is messy the second line.{Environment.NewLine}This is messy the third line.",
  1472. tv.Text
  1473. );
  1474. Assert.Equal (3, tv.Lines);
  1475. Assert.Equal (new (13, 2), tv.CursorPosition);
  1476. for (var i = 0; i < messy.Length; i++)
  1477. {
  1478. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1479. }
  1480. Assert.Equal (
  1481. $"This is messy the first line.{Environment.NewLine}This is messy the second line.{Environment.NewLine}This is the third line.",
  1482. tv.Text
  1483. );
  1484. Assert.Equal (3, tv.Lines);
  1485. Assert.Equal (new (7, 2), tv.CursorPosition);
  1486. for (var i = 0; i < messy.Length; i++)
  1487. {
  1488. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1489. }
  1490. Assert.Equal (
  1491. $"This is the first line.{Environment.NewLine}This is messy the second line.{Environment.NewLine}This is the third line.",
  1492. tv.Text
  1493. );
  1494. Assert.Equal (3, tv.Lines);
  1495. Assert.Equal (new (7, 0), tv.CursorPosition);
  1496. for (var i = 0; i < messy.Length; i++)
  1497. {
  1498. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1499. }
  1500. Assert.Equal (
  1501. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1502. tv.Text
  1503. );
  1504. Assert.Equal (3, tv.Lines);
  1505. Assert.Equal (new (7, 1), tv.CursorPosition);
  1506. for (var i = 0; i < messy.Length; i++)
  1507. {
  1508. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1509. }
  1510. Assert.Equal (
  1511. $"This is the first line.{Environment.NewLine}This is messy the second line.{Environment.NewLine}This is the third line.",
  1512. tv.Text
  1513. );
  1514. Assert.Equal (3, tv.Lines);
  1515. Assert.Equal (new (13, 1), tv.CursorPosition);
  1516. for (var i = 0; i < messy.Length; i++)
  1517. {
  1518. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1519. }
  1520. Assert.Equal (
  1521. $"This is messy the first line.{Environment.NewLine}This is messy the second line.{Environment.NewLine}This is the third line.",
  1522. tv.Text
  1523. );
  1524. Assert.Equal (3, tv.Lines);
  1525. Assert.Equal (new (13, 0), tv.CursorPosition);
  1526. for (var i = 0; i < messy.Length; i++)
  1527. {
  1528. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1529. }
  1530. Assert.Equal (
  1531. $"This is messy the first line.{Environment.NewLine}This is messy the second line.{Environment.NewLine}This is messy the third line.",
  1532. tv.Text
  1533. );
  1534. Assert.Equal (3, tv.Lines);
  1535. Assert.Equal (new (13, 2), tv.CursorPosition);
  1536. top.Dispose ();
  1537. }
  1538. [Fact]
  1539. [SetupFakeApplication]
  1540. public void HistoryText_Undo_Redo_Multi_Line_Selected_Copy_Simple_Paste_Starting_On_Letter ()
  1541. {
  1542. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  1543. var tv = new TextView { Text = text };
  1544. tv.SelectionStartColumn = 12;
  1545. tv.CursorPosition = new (18, 1);
  1546. Assert.True (tv.NewKeyDownEvent (Key.C.WithCtrl));
  1547. Assert.Equal (
  1548. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1549. tv.Text
  1550. );
  1551. Assert.Equal ($"first line.{Environment.NewLine}This is the second", tv.SelectedText);
  1552. Assert.Equal (3, tv.Lines);
  1553. Assert.Equal (new (18, 1), tv.CursorPosition);
  1554. tv.IsSelecting = false;
  1555. tv.CursorPosition = new (17, 1);
  1556. Assert.True (tv.NewKeyDownEvent (Key.Y.WithCtrl));
  1557. Assert.Equal (
  1558. $"This is the first line.{Environment.NewLine}This is the seconfirst line.{Environment.NewLine}This is the secondd line.{Environment.NewLine}This is the third line.",
  1559. tv.Text
  1560. );
  1561. Assert.Equal (4, tv.Lines);
  1562. Assert.Equal (new (18, 2), tv.CursorPosition);
  1563. // Undo
  1564. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1565. Assert.Equal (
  1566. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1567. tv.Text
  1568. );
  1569. Assert.Equal (3, tv.Lines);
  1570. Assert.Equal (new (17, 1), tv.CursorPosition);
  1571. // Redo
  1572. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1573. Assert.Equal (
  1574. $"This is the first line.{Environment.NewLine}This is the seconfirst line.{Environment.NewLine}This is the secondd line.{Environment.NewLine}This is the third line.",
  1575. tv.Text
  1576. );
  1577. Assert.Equal (4, tv.Lines);
  1578. Assert.Equal (new (18, 2), tv.CursorPosition);
  1579. }
  1580. [Fact]
  1581. [SetupFakeApplication]
  1582. public void HistoryText_Undo_Redo_Multi_Line_Selected_Copy_Simple_Paste_Starting_On_Space ()
  1583. {
  1584. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  1585. var tv = new TextView { Text = text };
  1586. tv.SelectionStartColumn = 12;
  1587. tv.CursorPosition = new (18, 1);
  1588. Assert.True (tv.NewKeyDownEvent (Key.C.WithCtrl));
  1589. Assert.Equal (
  1590. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1591. tv.Text
  1592. );
  1593. Assert.Equal ($"first line.{Environment.NewLine}This is the second", tv.SelectedText);
  1594. Assert.Equal (3, tv.Lines);
  1595. Assert.Equal (new (18, 1), tv.CursorPosition);
  1596. tv.IsSelecting = false;
  1597. Assert.True (tv.NewKeyDownEvent (Key.Y.WithCtrl));
  1598. Assert.Equal (
  1599. $"This is the first line.{Environment.NewLine}This is the secondfirst line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1600. tv.Text
  1601. );
  1602. Assert.Equal (4, tv.Lines);
  1603. Assert.Equal (new (18, 2), tv.CursorPosition);
  1604. // Undo
  1605. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1606. Assert.Equal (
  1607. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1608. tv.Text
  1609. );
  1610. Assert.Equal (3, tv.Lines);
  1611. Assert.Equal (new (18, 1), tv.CursorPosition);
  1612. // Redo
  1613. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1614. Assert.Equal (
  1615. $"This is the first line.{Environment.NewLine}This is the secondfirst line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1616. tv.Text
  1617. );
  1618. Assert.Equal (4, tv.Lines);
  1619. Assert.Equal (new (18, 2), tv.CursorPosition);
  1620. }
  1621. [Fact]
  1622. [SetupFakeApplication]
  1623. public void HistoryText_Undo_Redo_Multi_Line_Selected_InsertText ()
  1624. {
  1625. var text =
  1626. $"This is the first line.{Environment.NewLine}This is the second line.\nThis is the third line.";
  1627. var tv = new TextView { Width = 10, Height = 2, Text = text };
  1628. Toplevel top = new ();
  1629. top.Add (tv);
  1630. Application.Begin (top);
  1631. Assert.Equal (
  1632. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1633. tv.Text
  1634. );
  1635. Assert.Equal (3, tv.Lines);
  1636. Assert.Equal (Point.Empty, tv.CursorPosition);
  1637. var messy = " messy";
  1638. tv.CursorPosition = new (7, 0);
  1639. tv.SelectionStartColumn = 11;
  1640. tv.SelectionStartRow = 2;
  1641. Assert.Equal (51 + Environment.NewLine.Length * 2, tv.SelectedLength);
  1642. for (var i = 0; i < messy.Length; i++)
  1643. {
  1644. tv.InsertText (messy [i].ToString ());
  1645. switch (i)
  1646. {
  1647. case 0:
  1648. Assert.Equal ("This is third line.", tv.Text);
  1649. Assert.Equal (new (8, 0), tv.CursorPosition);
  1650. break;
  1651. case 1:
  1652. Assert.Equal ("This is m third line.", tv.Text);
  1653. Assert.Equal (new (9, 0), tv.CursorPosition);
  1654. break;
  1655. case 2:
  1656. Assert.Equal ("This is me third line.", tv.Text);
  1657. Assert.Equal (new (10, 0), tv.CursorPosition);
  1658. break;
  1659. case 3:
  1660. Assert.Equal ("This is mes third line.", tv.Text);
  1661. Assert.Equal (new (11, 0), tv.CursorPosition);
  1662. break;
  1663. case 4:
  1664. Assert.Equal ("This is mess third line.", tv.Text);
  1665. Assert.Equal (new (12, 0), tv.CursorPosition);
  1666. break;
  1667. case 5:
  1668. Assert.Equal ("This is messy third line.", tv.Text);
  1669. Assert.Equal (new (13, 0), tv.CursorPosition);
  1670. break;
  1671. }
  1672. }
  1673. Assert.Equal ("This is messy third line.", tv.Text);
  1674. Assert.Equal (1, tv.Lines);
  1675. Assert.Equal (new (13, 0), tv.CursorPosition);
  1676. Assert.Equal (11, tv.SelectionStartColumn);
  1677. Assert.Equal (2, tv.SelectionStartRow);
  1678. Assert.Equal (0, tv.SelectedLength);
  1679. for (var i = 0; i < messy.Length; i++)
  1680. {
  1681. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1682. switch (i)
  1683. {
  1684. case 0:
  1685. Assert.Equal ("This is mess third line.", tv.Text);
  1686. Assert.Equal (new (12, 0), tv.CursorPosition);
  1687. break;
  1688. case 1:
  1689. Assert.Equal ("This is mes third line.", tv.Text);
  1690. Assert.Equal (new (11, 0), tv.CursorPosition);
  1691. break;
  1692. case 2:
  1693. Assert.Equal ("This is me third line.", tv.Text);
  1694. Assert.Equal (new (10, 0), tv.CursorPosition);
  1695. break;
  1696. case 3:
  1697. Assert.Equal ("This is m third line.", tv.Text);
  1698. Assert.Equal (new (9, 0), tv.CursorPosition);
  1699. break;
  1700. case 4:
  1701. Assert.Equal ("This is third line.", tv.Text);
  1702. Assert.Equal (new (8, 0), tv.CursorPosition);
  1703. break;
  1704. case 5:
  1705. Assert.Equal (
  1706. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1707. tv.Text
  1708. );
  1709. Assert.Equal (new (7, 0), tv.CursorPosition);
  1710. break;
  1711. }
  1712. }
  1713. Assert.Equal (
  1714. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1715. tv.Text
  1716. );
  1717. Assert.Equal (3, tv.Lines);
  1718. Assert.Equal (new (7, 0), tv.CursorPosition);
  1719. Assert.Equal (11, tv.SelectionStartColumn);
  1720. Assert.Equal (2, tv.SelectionStartRow);
  1721. Assert.Equal (0, tv.SelectedLength);
  1722. for (var i = 0; i < messy.Length; i++)
  1723. {
  1724. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1725. switch (i)
  1726. {
  1727. case 0:
  1728. Assert.Equal ("This is third line.", tv.Text);
  1729. Assert.Equal (new (8, 0), tv.CursorPosition);
  1730. break;
  1731. case 1:
  1732. Assert.Equal ("This is m third line.", tv.Text);
  1733. Assert.Equal (new (9, 0), tv.CursorPosition);
  1734. break;
  1735. case 2:
  1736. Assert.Equal ("This is me third line.", tv.Text);
  1737. Assert.Equal (new (10, 0), tv.CursorPosition);
  1738. break;
  1739. case 3:
  1740. Assert.Equal ("This is mes third line.", tv.Text);
  1741. Assert.Equal (new (11, 0), tv.CursorPosition);
  1742. break;
  1743. case 4:
  1744. Assert.Equal ("This is mess third line.", tv.Text);
  1745. Assert.Equal (new (12, 0), tv.CursorPosition);
  1746. break;
  1747. case 5:
  1748. Assert.Equal ("This is messy third line.", tv.Text);
  1749. Assert.Equal (new (13, 0), tv.CursorPosition);
  1750. break;
  1751. }
  1752. }
  1753. Assert.Equal ("This is messy third line.", tv.Text);
  1754. Assert.Equal (1, tv.Lines);
  1755. Assert.Equal (new (13, 0), tv.CursorPosition);
  1756. Assert.Equal (11, tv.SelectionStartColumn);
  1757. Assert.Equal (2, tv.SelectionStartRow);
  1758. Assert.Equal (0, tv.SelectedLength);
  1759. top.Dispose ();
  1760. }
  1761. [Fact]
  1762. [SetupFakeApplication]
  1763. public void HistoryText_Undo_Redo_Multi_Line_Selected_InsertText_Twice_On_Same_Line ()
  1764. {
  1765. var text = "One\nTwo\nThree";
  1766. var tv = new TextView { Width = 10, Height = 2, Text = text };
  1767. Toplevel top = new ();
  1768. top.Add (tv);
  1769. Application.Begin (top);
  1770. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three", tv.Text);
  1771. Assert.Equal (3, tv.Lines);
  1772. Assert.Equal (Point.Empty, tv.CursorPosition);
  1773. Assert.False (tv.IsDirty);
  1774. tv.SelectionStartColumn = 0;
  1775. tv.SelectionStartRow = 0;
  1776. tv.CursorPosition = new (0, 1);
  1777. Assert.Equal (3 + Environment.NewLine.Length, tv.SelectedLength);
  1778. Assert.True (tv.NewKeyDownEvent (Key.D1));
  1779. Assert.Equal ($"1Two{Environment.NewLine}Three", tv.Text);
  1780. Assert.Equal (new (1, 0), tv.CursorPosition);
  1781. Assert.Equal (0, tv.SelectedLength);
  1782. Assert.True (tv.IsDirty);
  1783. tv.SelectionStartColumn = 1;
  1784. tv.SelectionStartRow = 0;
  1785. tv.CursorPosition = new (1, 1);
  1786. Assert.Equal (4 + Environment.NewLine.Length, tv.SelectedLength);
  1787. Assert.True (tv.NewKeyDownEvent (Key.D2));
  1788. Assert.Equal ("12hree", tv.Text);
  1789. Assert.Equal (new (2, 0), tv.CursorPosition);
  1790. Assert.Equal (0, tv.SelectedLength);
  1791. Assert.True (tv.IsDirty);
  1792. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1793. Assert.Equal ($"1Two{Environment.NewLine}Three", tv.Text);
  1794. Assert.Equal (new (1, 1), tv.CursorPosition);
  1795. Assert.Equal (0, tv.SelectedLength);
  1796. Assert.True (tv.IsDirty);
  1797. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1798. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three", tv.Text);
  1799. Assert.Equal (3, tv.Lines);
  1800. Assert.Equal (new (0, 1), tv.CursorPosition);
  1801. Assert.Equal (0, tv.SelectedLength);
  1802. Assert.False (tv.IsDirty);
  1803. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1804. Assert.Equal ($"1Two{Environment.NewLine}Three", tv.Text);
  1805. Assert.Equal (new (1, 0), tv.CursorPosition);
  1806. Assert.Equal (0, tv.SelectedLength);
  1807. Assert.True (tv.IsDirty);
  1808. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1809. Assert.Equal ("12hree", tv.Text);
  1810. Assert.Equal (new (2, 0), tv.CursorPosition);
  1811. Assert.Equal (0, tv.SelectedLength);
  1812. Assert.True (tv.IsDirty);
  1813. top.Dispose ();
  1814. }
  1815. [Fact]
  1816. [SetupFakeApplication]
  1817. public void HistoryText_Undo_Redo_Multi_Line_Selected_InsertText_Twice_On_Same_Line_With_End_Line ()
  1818. {
  1819. var text = "One\nTwo\nThree\n";
  1820. var tv = new TextView { Width = 10, Height = 2, Text = text };
  1821. Toplevel top = new ();
  1822. top.Add (tv);
  1823. Application.Begin (top);
  1824. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  1825. Assert.Equal (4, tv.Lines);
  1826. Assert.Equal (Point.Empty, tv.CursorPosition);
  1827. Assert.False (tv.IsDirty);
  1828. tv.SelectionStartColumn = 0;
  1829. tv.SelectionStartRow = 0;
  1830. tv.CursorPosition = new (0, 1);
  1831. Assert.Equal (3 + Environment.NewLine.Length, tv.SelectedLength);
  1832. Assert.True (tv.NewKeyDownEvent (Key.D1));
  1833. Assert.Equal ($"1Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  1834. Assert.Equal (new (1, 0), tv.CursorPosition);
  1835. Assert.Equal (0, tv.SelectedLength);
  1836. Assert.True (tv.IsDirty);
  1837. tv.SelectionStartColumn = 1;
  1838. tv.SelectionStartRow = 0;
  1839. tv.CursorPosition = new (1, 1);
  1840. Assert.Equal (4 + Environment.NewLine.Length, tv.SelectedLength);
  1841. Assert.True (tv.NewKeyDownEvent (Key.D2));
  1842. Assert.Equal ($"12hree{Environment.NewLine}", tv.Text);
  1843. Assert.Equal (new (2, 0), tv.CursorPosition);
  1844. Assert.Equal (0, tv.SelectedLength);
  1845. Assert.True (tv.IsDirty);
  1846. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1847. Assert.Equal ($"1Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  1848. Assert.Equal (new (1, 1), tv.CursorPosition);
  1849. Assert.Equal (0, tv.SelectedLength);
  1850. Assert.True (tv.IsDirty);
  1851. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1852. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  1853. Assert.Equal (4, tv.Lines);
  1854. Assert.Equal (new (0, 1), tv.CursorPosition);
  1855. Assert.Equal (0, tv.SelectedLength);
  1856. Assert.False (tv.IsDirty);
  1857. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1858. Assert.Equal ($"1Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  1859. Assert.Equal (new (1, 0), tv.CursorPosition);
  1860. Assert.Equal (0, tv.SelectedLength);
  1861. Assert.True (tv.IsDirty);
  1862. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1863. Assert.Equal ($"12hree{Environment.NewLine}", tv.Text);
  1864. Assert.Equal (new (2, 0), tv.CursorPosition);
  1865. Assert.Equal (0, tv.SelectedLength);
  1866. Assert.True (tv.IsDirty);
  1867. top.Dispose ();
  1868. }
  1869. [Fact]
  1870. [SetupFakeApplication]
  1871. public void HistoryText_Undo_Redo_Multi_Line_Selected_With_Empty_Text ()
  1872. {
  1873. var tv = new TextView { Width = 10, Height = 2 };
  1874. Toplevel top = new ();
  1875. top.Add (tv);
  1876. Application.Begin (top);
  1877. Assert.Equal ("", tv.Text);
  1878. Assert.Equal (1, tv.Lines);
  1879. Assert.Equal (Point.Empty, tv.CursorPosition);
  1880. Assert.False (tv.IsDirty);
  1881. Assert.True (tv.NewKeyDownEvent (Key.O.WithShift));
  1882. Assert.Equal ("O", tv.Text);
  1883. Assert.Equal (1, tv.Lines);
  1884. Assert.Equal (new (1, 0), tv.CursorPosition);
  1885. Assert.True (tv.IsDirty);
  1886. Assert.True (tv.NewKeyDownEvent (Key.N));
  1887. Assert.Equal ("On", tv.Text);
  1888. Assert.Equal (1, tv.Lines);
  1889. Assert.Equal (new (2, 0), tv.CursorPosition);
  1890. Assert.True (tv.IsDirty);
  1891. Assert.True (tv.NewKeyDownEvent (Key.E));
  1892. Assert.Equal ("One", tv.Text);
  1893. Assert.Equal (1, tv.Lines);
  1894. Assert.Equal (new (3, 0), tv.CursorPosition);
  1895. Assert.True (tv.IsDirty);
  1896. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  1897. Assert.Equal ($"One{Environment.NewLine}", tv.Text);
  1898. Assert.Equal (2, tv.Lines);
  1899. Assert.Equal (new (0, 1), tv.CursorPosition);
  1900. Assert.True (tv.IsDirty);
  1901. Assert.True (tv.NewKeyDownEvent (Key.T.WithShift));
  1902. Assert.Equal ($"One{Environment.NewLine}T", tv.Text);
  1903. Assert.Equal (2, tv.Lines);
  1904. Assert.Equal (new (1, 1), tv.CursorPosition);
  1905. Assert.True (tv.IsDirty);
  1906. Assert.True (tv.NewKeyDownEvent (Key.W));
  1907. Assert.Equal ($"One{Environment.NewLine}Tw", tv.Text);
  1908. Assert.Equal (2, tv.Lines);
  1909. Assert.Equal (new (2, 1), tv.CursorPosition);
  1910. Assert.True (tv.IsDirty);
  1911. Assert.True (tv.NewKeyDownEvent (Key.O));
  1912. Assert.Equal ($"One{Environment.NewLine}Two", tv.Text);
  1913. Assert.Equal (2, tv.Lines);
  1914. Assert.Equal (new (3, 1), tv.CursorPosition);
  1915. Assert.True (tv.IsDirty);
  1916. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  1917. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}", tv.Text);
  1918. Assert.Equal (3, tv.Lines);
  1919. Assert.Equal (new (0, 2), tv.CursorPosition);
  1920. Assert.True (tv.IsDirty);
  1921. Assert.True (tv.NewKeyDownEvent (Key.T.WithShift));
  1922. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}T", tv.Text);
  1923. Assert.Equal (3, tv.Lines);
  1924. Assert.Equal (new (1, 2), tv.CursorPosition);
  1925. Assert.True (tv.IsDirty);
  1926. Assert.True (tv.NewKeyDownEvent (Key.H));
  1927. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Th", tv.Text);
  1928. Assert.Equal (3, tv.Lines);
  1929. Assert.Equal (new (2, 2), tv.CursorPosition);
  1930. Assert.True (tv.IsDirty);
  1931. Assert.True (tv.NewKeyDownEvent (Key.R));
  1932. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Thr", tv.Text);
  1933. Assert.Equal (3, tv.Lines);
  1934. Assert.Equal (new (3, 2), tv.CursorPosition);
  1935. Assert.True (tv.IsDirty);
  1936. Assert.True (tv.NewKeyDownEvent (Key.E));
  1937. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Thre", tv.Text);
  1938. Assert.Equal (3, tv.Lines);
  1939. Assert.Equal (new (4, 2), tv.CursorPosition);
  1940. Assert.True (tv.IsDirty);
  1941. Assert.True (tv.NewKeyDownEvent (Key.E));
  1942. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three", tv.Text);
  1943. Assert.Equal (3, tv.Lines);
  1944. Assert.Equal (new (5, 2), tv.CursorPosition);
  1945. Assert.True (tv.IsDirty);
  1946. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  1947. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  1948. Assert.Equal (4, tv.Lines);
  1949. Assert.Equal (new (0, 3), tv.CursorPosition);
  1950. Assert.True (tv.IsDirty);
  1951. tv.SelectionStartColumn = 0;
  1952. tv.SelectionStartRow = 0;
  1953. tv.CursorPosition = new (0, 1);
  1954. Assert.Equal (3 + Environment.NewLine.Length, tv.SelectedLength);
  1955. Assert.True (tv.NewKeyDownEvent (Key.D1));
  1956. Assert.Equal ($"1Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  1957. Assert.Equal (3, tv.Lines);
  1958. Assert.Equal (new (1, 0), tv.CursorPosition);
  1959. Assert.Equal (0, tv.SelectedLength);
  1960. Assert.True (tv.IsDirty);
  1961. tv.SelectionStartColumn = 1;
  1962. tv.SelectionStartRow = 0;
  1963. tv.CursorPosition = new (1, 1);
  1964. Assert.Equal (4 + Environment.NewLine.Length, tv.SelectedLength);
  1965. Assert.True (tv.NewKeyDownEvent (Key.D2));
  1966. Assert.Equal ($"12hree{Environment.NewLine}", tv.Text);
  1967. Assert.Equal (2, tv.Lines);
  1968. Assert.Equal (new (2, 0), tv.CursorPosition);
  1969. Assert.Equal (0, tv.SelectedLength);
  1970. Assert.True (tv.IsDirty);
  1971. // Undoing
  1972. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1973. Assert.Equal ($"1Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  1974. Assert.Equal (3, tv.Lines);
  1975. Assert.Equal (new (1, 1), tv.CursorPosition);
  1976. Assert.Equal (0, tv.SelectedLength);
  1977. Assert.True (tv.IsDirty);
  1978. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1979. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  1980. Assert.Equal (4, tv.Lines);
  1981. Assert.Equal (new (0, 1), tv.CursorPosition);
  1982. Assert.Equal (0, tv.SelectedLength);
  1983. Assert.True (tv.IsDirty);
  1984. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1985. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three", tv.Text);
  1986. Assert.Equal (3, tv.Lines);
  1987. Assert.Equal (new (5, 2), tv.CursorPosition);
  1988. Assert.Equal (0, tv.SelectedLength);
  1989. Assert.True (tv.IsDirty);
  1990. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1991. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Thre", tv.Text);
  1992. Assert.Equal (3, tv.Lines);
  1993. Assert.Equal (new (4, 2), tv.CursorPosition);
  1994. Assert.True (tv.IsDirty);
  1995. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1996. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Thr", tv.Text);
  1997. Assert.Equal (3, tv.Lines);
  1998. Assert.Equal (new (3, 2), tv.CursorPosition);
  1999. Assert.Equal (0, tv.SelectedLength);
  2000. Assert.True (tv.IsDirty);
  2001. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2002. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Th", tv.Text);
  2003. Assert.Equal (3, tv.Lines);
  2004. Assert.Equal (new (2, 2), tv.CursorPosition);
  2005. Assert.Equal (0, tv.SelectedLength);
  2006. Assert.True (tv.IsDirty);
  2007. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2008. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}T", tv.Text);
  2009. Assert.Equal (3, tv.Lines);
  2010. Assert.Equal (new (1, 2), tv.CursorPosition);
  2011. Assert.Equal (0, tv.SelectedLength);
  2012. Assert.True (tv.IsDirty);
  2013. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2014. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}", tv.Text);
  2015. Assert.Equal (3, tv.Lines);
  2016. Assert.Equal (new (0, 2), tv.CursorPosition);
  2017. Assert.Equal (0, tv.SelectedLength);
  2018. Assert.True (tv.IsDirty);
  2019. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2020. Assert.Equal ($"One{Environment.NewLine}Two", tv.Text);
  2021. Assert.Equal (2, tv.Lines);
  2022. Assert.Equal (new (3, 1), tv.CursorPosition);
  2023. Assert.Equal (0, tv.SelectedLength);
  2024. Assert.True (tv.IsDirty);
  2025. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2026. Assert.Equal ($"One{Environment.NewLine}Tw", tv.Text);
  2027. Assert.Equal (2, tv.Lines);
  2028. Assert.Equal (new (2, 1), tv.CursorPosition);
  2029. Assert.Equal (0, tv.SelectedLength);
  2030. Assert.True (tv.IsDirty);
  2031. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2032. Assert.Equal ($"One{Environment.NewLine}T", tv.Text);
  2033. Assert.Equal (2, tv.Lines);
  2034. Assert.Equal (new (1, 1), tv.CursorPosition);
  2035. Assert.Equal (0, tv.SelectedLength);
  2036. Assert.True (tv.IsDirty);
  2037. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2038. Assert.Equal ($"One{Environment.NewLine}", tv.Text);
  2039. Assert.Equal (2, tv.Lines);
  2040. Assert.Equal (new (0, 1), tv.CursorPosition);
  2041. Assert.Equal (0, tv.SelectedLength);
  2042. Assert.True (tv.IsDirty);
  2043. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2044. Assert.Equal ("One", tv.Text);
  2045. Assert.Equal (1, tv.Lines);
  2046. Assert.Equal (new (3, 0), tv.CursorPosition);
  2047. Assert.Equal (0, tv.SelectedLength);
  2048. Assert.True (tv.IsDirty);
  2049. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2050. Assert.Equal ("On", tv.Text);
  2051. Assert.Equal (1, tv.Lines);
  2052. Assert.Equal (new (2, 0), tv.CursorPosition);
  2053. Assert.Equal (0, tv.SelectedLength);
  2054. Assert.True (tv.IsDirty);
  2055. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2056. Assert.Equal ("O", tv.Text);
  2057. Assert.Equal (1, tv.Lines);
  2058. Assert.Equal (new (1, 0), tv.CursorPosition);
  2059. Assert.Equal (0, tv.SelectedLength);
  2060. Assert.True (tv.IsDirty);
  2061. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2062. Assert.Equal ("", tv.Text);
  2063. Assert.Equal (1, tv.Lines);
  2064. Assert.Equal (Point.Empty, tv.CursorPosition);
  2065. Assert.Equal (0, tv.SelectedLength);
  2066. Assert.False (tv.IsDirty);
  2067. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2068. Assert.Equal ("", tv.Text);
  2069. Assert.Equal (1, tv.Lines);
  2070. Assert.Equal (Point.Empty, tv.CursorPosition);
  2071. Assert.Equal (0, tv.SelectedLength);
  2072. Assert.False (tv.IsDirty);
  2073. // Redoing
  2074. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2075. Assert.Equal ("O", tv.Text);
  2076. Assert.Equal (1, tv.Lines);
  2077. Assert.Equal (new (1, 0), tv.CursorPosition);
  2078. Assert.Equal (0, tv.SelectedLength);
  2079. Assert.True (tv.IsDirty);
  2080. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2081. Assert.Equal ("On", tv.Text);
  2082. Assert.Equal (1, tv.Lines);
  2083. Assert.Equal (new (2, 0), tv.CursorPosition);
  2084. Assert.Equal (0, tv.SelectedLength);
  2085. Assert.True (tv.IsDirty);
  2086. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2087. Assert.Equal ("One", tv.Text);
  2088. Assert.Equal (1, tv.Lines);
  2089. Assert.Equal (new (3, 0), tv.CursorPosition);
  2090. Assert.Equal (0, tv.SelectedLength);
  2091. Assert.True (tv.IsDirty);
  2092. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2093. Assert.Equal ($"One{Environment.NewLine}", tv.Text);
  2094. Assert.Equal (2, tv.Lines);
  2095. Assert.Equal (new (0, 1), tv.CursorPosition);
  2096. Assert.Equal (0, tv.SelectedLength);
  2097. Assert.True (tv.IsDirty);
  2098. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2099. Assert.Equal ($"One{Environment.NewLine}T", tv.Text);
  2100. Assert.Equal (2, tv.Lines);
  2101. Assert.Equal (new (1, 1), tv.CursorPosition);
  2102. Assert.Equal (0, tv.SelectedLength);
  2103. Assert.True (tv.IsDirty);
  2104. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2105. Assert.Equal ($"One{Environment.NewLine}Tw", tv.Text);
  2106. Assert.Equal (2, tv.Lines);
  2107. Assert.Equal (new (2, 1), tv.CursorPosition);
  2108. Assert.Equal (0, tv.SelectedLength);
  2109. Assert.True (tv.IsDirty);
  2110. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2111. Assert.Equal ($"One{Environment.NewLine}Two", tv.Text);
  2112. Assert.Equal (2, tv.Lines);
  2113. Assert.Equal (new (3, 1), tv.CursorPosition);
  2114. Assert.Equal (0, tv.SelectedLength);
  2115. Assert.True (tv.IsDirty);
  2116. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2117. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}", tv.Text);
  2118. Assert.Equal (3, tv.Lines);
  2119. Assert.Equal (new (0, 2), tv.CursorPosition);
  2120. Assert.Equal (0, tv.SelectedLength);
  2121. Assert.True (tv.IsDirty);
  2122. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2123. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}T", tv.Text);
  2124. Assert.Equal (3, tv.Lines);
  2125. Assert.Equal (new (1, 2), tv.CursorPosition);
  2126. Assert.Equal (0, tv.SelectedLength);
  2127. Assert.True (tv.IsDirty);
  2128. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2129. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Th", tv.Text);
  2130. Assert.Equal (3, tv.Lines);
  2131. Assert.Equal (new (2, 2), tv.CursorPosition);
  2132. Assert.Equal (0, tv.SelectedLength);
  2133. Assert.True (tv.IsDirty);
  2134. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2135. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Thr", tv.Text);
  2136. Assert.Equal (3, tv.Lines);
  2137. Assert.Equal (new (3, 2), tv.CursorPosition);
  2138. Assert.Equal (0, tv.SelectedLength);
  2139. Assert.True (tv.IsDirty);
  2140. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2141. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Thre", tv.Text);
  2142. Assert.Equal (3, tv.Lines);
  2143. Assert.Equal (new (4, 2), tv.CursorPosition);
  2144. Assert.Equal (0, tv.SelectedLength);
  2145. Assert.True (tv.IsDirty);
  2146. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2147. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three", tv.Text);
  2148. Assert.Equal (3, tv.Lines);
  2149. Assert.Equal (new (5, 2), tv.CursorPosition);
  2150. Assert.Equal (0, tv.SelectedLength);
  2151. Assert.True (tv.IsDirty);
  2152. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2153. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  2154. Assert.Equal (4, tv.Lines);
  2155. Assert.Equal (new (0, 3), tv.CursorPosition);
  2156. Assert.Equal (0, tv.SelectedLength);
  2157. Assert.True (tv.IsDirty);
  2158. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2159. Assert.Equal ($"1Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  2160. Assert.Equal (3, tv.Lines);
  2161. Assert.Equal (new (1, 0), tv.CursorPosition);
  2162. Assert.Equal (0, tv.SelectedLength);
  2163. Assert.True (tv.IsDirty);
  2164. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2165. Assert.Equal ($"12hree{Environment.NewLine}", tv.Text);
  2166. Assert.Equal (2, tv.Lines);
  2167. Assert.Equal (new (2, 0), tv.CursorPosition);
  2168. Assert.Equal (0, tv.SelectedLength);
  2169. Assert.True (tv.IsDirty);
  2170. top.Dispose ();
  2171. }
  2172. [Fact]
  2173. [SetupFakeApplication]
  2174. public void HistoryText_Undo_Redo_Multi_Line_With_Empty_Text ()
  2175. {
  2176. var tv = new TextView { Width = 10, Height = 2 };
  2177. Toplevel top = new ();
  2178. top.Add (tv);
  2179. Application.Begin (top);
  2180. Assert.Equal ("", tv.Text);
  2181. Assert.Equal (1, tv.Lines);
  2182. Assert.Equal (Point.Empty, tv.CursorPosition);
  2183. Assert.False (tv.IsDirty);
  2184. Assert.True (tv.NewKeyDownEvent (Key.O.WithShift));
  2185. Assert.Equal ("O", tv.Text);
  2186. Assert.Equal (1, tv.Lines);
  2187. Assert.Equal (new (1, 0), tv.CursorPosition);
  2188. Assert.True (tv.IsDirty);
  2189. Assert.True (tv.NewKeyDownEvent (Key.N));
  2190. Assert.Equal ("On", tv.Text);
  2191. Assert.Equal (1, tv.Lines);
  2192. Assert.Equal (new (2, 0), tv.CursorPosition);
  2193. Assert.True (tv.IsDirty);
  2194. Assert.True (tv.NewKeyDownEvent (Key.E));
  2195. Assert.Equal ("One", tv.Text);
  2196. Assert.Equal (1, tv.Lines);
  2197. Assert.Equal (new (3, 0), tv.CursorPosition);
  2198. Assert.True (tv.IsDirty);
  2199. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  2200. Assert.Equal ($"One{Environment.NewLine}", tv.Text);
  2201. Assert.Equal (2, tv.Lines);
  2202. Assert.Equal (new (0, 1), tv.CursorPosition);
  2203. Assert.True (tv.IsDirty);
  2204. Assert.True (tv.NewKeyDownEvent (Key.T.WithShift));
  2205. Assert.Equal ($"One{Environment.NewLine}T", tv.Text);
  2206. Assert.Equal (2, tv.Lines);
  2207. Assert.Equal (new (1, 1), tv.CursorPosition);
  2208. Assert.True (tv.IsDirty);
  2209. Assert.True (tv.NewKeyDownEvent (Key.W));
  2210. Assert.Equal ($"One{Environment.NewLine}Tw", tv.Text);
  2211. Assert.Equal (2, tv.Lines);
  2212. Assert.Equal (new (2, 1), tv.CursorPosition);
  2213. Assert.True (tv.IsDirty);
  2214. Assert.True (tv.NewKeyDownEvent (Key.O));
  2215. Assert.Equal ($"One{Environment.NewLine}Two", tv.Text);
  2216. Assert.Equal (2, tv.Lines);
  2217. Assert.Equal (new (3, 1), tv.CursorPosition);
  2218. Assert.True (tv.IsDirty);
  2219. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  2220. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}", tv.Text);
  2221. Assert.Equal (3, tv.Lines);
  2222. Assert.Equal (new (0, 2), tv.CursorPosition);
  2223. Assert.True (tv.IsDirty);
  2224. Assert.True (tv.NewKeyDownEvent (Key.T.WithShift));
  2225. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}T", tv.Text);
  2226. Assert.Equal (3, tv.Lines);
  2227. Assert.Equal (new (1, 2), tv.CursorPosition);
  2228. Assert.True (tv.IsDirty);
  2229. Assert.True (tv.NewKeyDownEvent (Key.H));
  2230. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Th", tv.Text);
  2231. Assert.Equal (3, tv.Lines);
  2232. Assert.Equal (new (2, 2), tv.CursorPosition);
  2233. Assert.True (tv.IsDirty);
  2234. Assert.True (tv.NewKeyDownEvent (Key.R));
  2235. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Thr", tv.Text);
  2236. Assert.Equal (3, tv.Lines);
  2237. Assert.Equal (new (3, 2), tv.CursorPosition);
  2238. Assert.True (tv.IsDirty);
  2239. Assert.True (tv.NewKeyDownEvent (Key.E));
  2240. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Thre", tv.Text);
  2241. Assert.Equal (3, tv.Lines);
  2242. Assert.Equal (new (4, 2), tv.CursorPosition);
  2243. Assert.True (tv.IsDirty);
  2244. Assert.True (tv.NewKeyDownEvent (Key.E));
  2245. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three", tv.Text);
  2246. Assert.Equal (3, tv.Lines);
  2247. Assert.Equal (new (5, 2), tv.CursorPosition);
  2248. Assert.True (tv.IsDirty);
  2249. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  2250. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  2251. Assert.Equal (4, tv.Lines);
  2252. Assert.Equal (new (0, 3), tv.CursorPosition);
  2253. Assert.True (tv.IsDirty);
  2254. // Undoing
  2255. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2256. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three", tv.Text);
  2257. Assert.Equal (3, tv.Lines);
  2258. Assert.Equal (new (5, 2), tv.CursorPosition);
  2259. Assert.True (tv.IsDirty);
  2260. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2261. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Thre", tv.Text);
  2262. Assert.Equal (3, tv.Lines);
  2263. Assert.Equal (new (4, 2), tv.CursorPosition);
  2264. Assert.True (tv.IsDirty);
  2265. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2266. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Thr", tv.Text);
  2267. Assert.Equal (3, tv.Lines);
  2268. Assert.Equal (new (3, 2), tv.CursorPosition);
  2269. Assert.True (tv.IsDirty);
  2270. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2271. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Th", tv.Text);
  2272. Assert.Equal (3, tv.Lines);
  2273. Assert.Equal (new (2, 2), tv.CursorPosition);
  2274. Assert.True (tv.IsDirty);
  2275. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2276. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}T", tv.Text);
  2277. Assert.Equal (3, tv.Lines);
  2278. Assert.Equal (new (1, 2), tv.CursorPosition);
  2279. Assert.True (tv.IsDirty);
  2280. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2281. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}", tv.Text);
  2282. Assert.Equal (3, tv.Lines);
  2283. Assert.Equal (new (0, 2), tv.CursorPosition);
  2284. Assert.True (tv.IsDirty);
  2285. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2286. Assert.Equal ($"One{Environment.NewLine}Two", tv.Text);
  2287. Assert.Equal (2, tv.Lines);
  2288. Assert.Equal (new (3, 1), tv.CursorPosition);
  2289. Assert.True (tv.IsDirty);
  2290. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2291. Assert.Equal ($"One{Environment.NewLine}Tw", tv.Text);
  2292. Assert.Equal (2, tv.Lines);
  2293. Assert.Equal (new (2, 1), tv.CursorPosition);
  2294. Assert.True (tv.IsDirty);
  2295. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2296. Assert.Equal ($"One{Environment.NewLine}T", tv.Text);
  2297. Assert.Equal (2, tv.Lines);
  2298. Assert.Equal (new (1, 1), tv.CursorPosition);
  2299. Assert.True (tv.IsDirty);
  2300. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2301. Assert.Equal ($"One{Environment.NewLine}", tv.Text);
  2302. Assert.Equal (2, tv.Lines);
  2303. Assert.Equal (new (0, 1), tv.CursorPosition);
  2304. Assert.True (tv.IsDirty);
  2305. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2306. Assert.Equal ("One", tv.Text);
  2307. Assert.Equal (1, tv.Lines);
  2308. Assert.Equal (new (3, 0), tv.CursorPosition);
  2309. Assert.True (tv.IsDirty);
  2310. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2311. Assert.Equal ("On", tv.Text);
  2312. Assert.Equal (1, tv.Lines);
  2313. Assert.Equal (new (2, 0), tv.CursorPosition);
  2314. Assert.True (tv.IsDirty);
  2315. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2316. Assert.Equal ("O", tv.Text);
  2317. Assert.Equal (1, tv.Lines);
  2318. Assert.Equal (new (1, 0), tv.CursorPosition);
  2319. Assert.True (tv.IsDirty);
  2320. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2321. Assert.Equal ("", tv.Text);
  2322. Assert.Equal (1, tv.Lines);
  2323. Assert.Equal (Point.Empty, tv.CursorPosition);
  2324. Assert.False (tv.IsDirty);
  2325. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2326. Assert.Equal ("", tv.Text);
  2327. Assert.Equal (1, tv.Lines);
  2328. Assert.Equal (Point.Empty, tv.CursorPosition);
  2329. Assert.False (tv.IsDirty);
  2330. // Redoing
  2331. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2332. Assert.Equal ("O", tv.Text);
  2333. Assert.Equal (1, tv.Lines);
  2334. Assert.Equal (new (1, 0), tv.CursorPosition);
  2335. Assert.True (tv.IsDirty);
  2336. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2337. Assert.Equal ("On", tv.Text);
  2338. Assert.Equal (1, tv.Lines);
  2339. Assert.Equal (new (2, 0), tv.CursorPosition);
  2340. Assert.True (tv.IsDirty);
  2341. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2342. Assert.Equal ("One", tv.Text);
  2343. Assert.Equal (1, tv.Lines);
  2344. Assert.Equal (new (3, 0), tv.CursorPosition);
  2345. Assert.True (tv.IsDirty);
  2346. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2347. Assert.Equal ($"One{Environment.NewLine}", tv.Text);
  2348. Assert.Equal (2, tv.Lines);
  2349. Assert.Equal (new (0, 1), tv.CursorPosition);
  2350. Assert.True (tv.IsDirty);
  2351. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2352. Assert.Equal ($"One{Environment.NewLine}T", tv.Text);
  2353. Assert.Equal (2, tv.Lines);
  2354. Assert.Equal (new (1, 1), tv.CursorPosition);
  2355. Assert.True (tv.IsDirty);
  2356. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2357. Assert.Equal ($"One{Environment.NewLine}Tw", tv.Text);
  2358. Assert.Equal (2, tv.Lines);
  2359. Assert.Equal (new (2, 1), tv.CursorPosition);
  2360. Assert.True (tv.IsDirty);
  2361. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2362. Assert.Equal ($"One{Environment.NewLine}Two", tv.Text);
  2363. Assert.Equal (2, tv.Lines);
  2364. Assert.Equal (new (3, 1), tv.CursorPosition);
  2365. Assert.True (tv.IsDirty);
  2366. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2367. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}", tv.Text);
  2368. Assert.Equal (3, tv.Lines);
  2369. Assert.Equal (new (0, 2), tv.CursorPosition);
  2370. Assert.True (tv.IsDirty);
  2371. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2372. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}T", tv.Text);
  2373. Assert.Equal (3, tv.Lines);
  2374. Assert.Equal (new (1, 2), tv.CursorPosition);
  2375. Assert.True (tv.IsDirty);
  2376. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2377. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Th", tv.Text);
  2378. Assert.Equal (3, tv.Lines);
  2379. Assert.Equal (new (2, 2), tv.CursorPosition);
  2380. Assert.True (tv.IsDirty);
  2381. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2382. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Thr", tv.Text);
  2383. Assert.Equal (3, tv.Lines);
  2384. Assert.Equal (new (3, 2), tv.CursorPosition);
  2385. Assert.True (tv.IsDirty);
  2386. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2387. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Thre", tv.Text);
  2388. Assert.Equal (3, tv.Lines);
  2389. Assert.Equal (new (4, 2), tv.CursorPosition);
  2390. Assert.True (tv.IsDirty);
  2391. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2392. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three", tv.Text);
  2393. Assert.Equal (3, tv.Lines);
  2394. Assert.Equal (new (5, 2), tv.CursorPosition);
  2395. Assert.True (tv.IsDirty);
  2396. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2397. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  2398. Assert.Equal (4, tv.Lines);
  2399. Assert.Equal (new (0, 3), tv.CursorPosition);
  2400. Assert.True (tv.IsDirty);
  2401. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2402. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  2403. Assert.Equal (4, tv.Lines);
  2404. Assert.Equal (new (0, 3), tv.CursorPosition);
  2405. Assert.True (tv.IsDirty);
  2406. top.Dispose ();
  2407. }
  2408. [Fact]
  2409. [SetupFakeApplication]
  2410. public void HistoryText_Undo_Redo_Setting_Clipboard_Multi_Line_Selected_Paste ()
  2411. {
  2412. var text = "This is the first line.\nThis is the second line.";
  2413. var tv = new TextView { Text = text };
  2414. Clipboard.Contents = "Inserted\nNewLine";
  2415. Assert.Equal ($"This is the first line.{Environment.NewLine}This is the second line.", tv.Text);
  2416. Assert.Equal ("", tv.SelectedText);
  2417. Assert.Equal (2, tv.Lines);
  2418. Assert.Equal (Point.Empty, tv.CursorPosition);
  2419. Assert.True (tv.NewKeyDownEvent (Key.Y.WithCtrl));
  2420. Assert.Equal (
  2421. $"Inserted{Environment.NewLine}NewLineThis is the first line.{Environment.NewLine}This is the second line.",
  2422. tv.Text
  2423. );
  2424. Assert.Equal (3, tv.Lines);
  2425. Assert.Equal (new (7, 1), tv.CursorPosition);
  2426. // Undo
  2427. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2428. Assert.Equal ($"This is the first line.{Environment.NewLine}This is the second line.", tv.Text);
  2429. Assert.Equal (2, tv.Lines);
  2430. Assert.Equal (Point.Empty, tv.CursorPosition);
  2431. // Redo
  2432. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2433. Assert.Equal (
  2434. $"Inserted{Environment.NewLine}NewLineThis is the first line.{Environment.NewLine}This is the second line.",
  2435. tv.Text
  2436. );
  2437. Assert.Equal (3, tv.Lines);
  2438. Assert.Equal (new (7, 1), tv.CursorPosition);
  2439. }
  2440. [Fact]
  2441. [AutoInitShutdown ()]
  2442. public void HistoryText_Undo_Redo_Simple_Copy_Multi_Line_Selected_Paste ()
  2443. {
  2444. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  2445. var tv = new TextView { Text = text };
  2446. tv.SelectionStartColumn = 12;
  2447. tv.CursorPosition = new (17, 0);
  2448. Assert.True (tv.NewKeyDownEvent (Key.C.WithCtrl));
  2449. Assert.Equal (
  2450. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2451. tv.Text
  2452. );
  2453. Assert.Equal ("first", tv.SelectedText);
  2454. Assert.Equal (3, tv.Lines);
  2455. Assert.Equal (new (17, 0), tv.CursorPosition);
  2456. tv.SelectionStartColumn = 12;
  2457. tv.CursorPosition = new (11, 1);
  2458. Assert.True (tv.NewKeyDownEvent (Key.Y.WithCtrl));
  2459. Assert.Equal ($"This is the first second line.{Environment.NewLine}This is the third line.", tv.Text);
  2460. Assert.Equal (2, tv.Lines);
  2461. Assert.Equal (new (17, 0), tv.CursorPosition);
  2462. // Undo
  2463. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2464. Assert.Equal (
  2465. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2466. tv.Text
  2467. );
  2468. Assert.Equal (3, tv.Lines);
  2469. Assert.Equal (new (12, 0), tv.CursorPosition);
  2470. // Redo
  2471. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2472. Assert.Equal ($"This is the first second line.{Environment.NewLine}This is the third line.", tv.Text);
  2473. Assert.Equal (2, tv.Lines);
  2474. Assert.Equal (new (17, 0), tv.CursorPosition);
  2475. }
  2476. [Fact]
  2477. [SetupFakeApplication]
  2478. public void HistoryText_Undo_Redo_Single_Line_DeleteCharLeft ()
  2479. {
  2480. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  2481. var tv = new TextView { Width = 10, Height = 2, Text = text };
  2482. Toplevel top = new ();
  2483. top.Add (tv);
  2484. Application.Begin (top);
  2485. Assert.Equal (
  2486. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2487. tv.Text
  2488. );
  2489. Assert.Equal (3, tv.Lines);
  2490. Assert.Equal (Point.Empty, tv.CursorPosition);
  2491. var ntimes = 3;
  2492. tv.CursorPosition = new (7, 1);
  2493. for (var i = 0; i < ntimes; i++)
  2494. {
  2495. tv.DeleteCharLeft ();
  2496. }
  2497. Assert.Equal (
  2498. $"This is the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  2499. tv.Text
  2500. );
  2501. Assert.Equal (3, tv.Lines);
  2502. Assert.Equal (new (4, 1), tv.CursorPosition);
  2503. for (var i = 0; i < ntimes; i++)
  2504. {
  2505. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2506. }
  2507. Assert.Equal (
  2508. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2509. tv.Text
  2510. );
  2511. Assert.Equal (3, tv.Lines);
  2512. Assert.Equal (new (7, 1), tv.CursorPosition);
  2513. for (var i = 0; i < ntimes; i++)
  2514. {
  2515. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2516. }
  2517. Assert.Equal (
  2518. $"This is the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  2519. tv.Text
  2520. );
  2521. Assert.Equal (3, tv.Lines);
  2522. Assert.Equal (new (4, 1), tv.CursorPosition);
  2523. top.Dispose ();
  2524. }
  2525. [Fact]
  2526. [SetupFakeApplication]
  2527. public void HistoryText_Undo_Redo_Single_Line_DeleteCharRight ()
  2528. {
  2529. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  2530. var tv = new TextView { Width = 10, Height = 2, Text = text };
  2531. Toplevel top = new ();
  2532. top.Add (tv);
  2533. Application.Begin (top);
  2534. Assert.Equal (
  2535. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2536. tv.Text
  2537. );
  2538. Assert.Equal (3, tv.Lines);
  2539. Assert.Equal (Point.Empty, tv.CursorPosition);
  2540. var ntimes = 3;
  2541. tv.CursorPosition = new (7, 1);
  2542. for (var i = 0; i < ntimes; i++)
  2543. {
  2544. tv.DeleteCharRight ();
  2545. }
  2546. Assert.Equal (
  2547. $"This is the first line.{Environment.NewLine}This ise second line.{Environment.NewLine}This is the third line.",
  2548. tv.Text
  2549. );
  2550. Assert.Equal (3, tv.Lines);
  2551. Assert.Equal (new (7, 1), tv.CursorPosition);
  2552. for (var i = 0; i < ntimes; i++)
  2553. {
  2554. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2555. }
  2556. Assert.Equal (
  2557. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2558. tv.Text
  2559. );
  2560. Assert.Equal (3, tv.Lines);
  2561. Assert.Equal (new (7, 1), tv.CursorPosition);
  2562. for (var i = 0; i < ntimes; i++)
  2563. {
  2564. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2565. }
  2566. Assert.Equal (
  2567. $"This is the first line.{Environment.NewLine}This ise second line.{Environment.NewLine}This is the third line.",
  2568. tv.Text
  2569. );
  2570. Assert.Equal (3, tv.Lines);
  2571. Assert.Equal (new (7, 1), tv.CursorPosition);
  2572. top.Dispose ();
  2573. }
  2574. [Fact]
  2575. [SetupFakeApplication]
  2576. public void HistoryText_Undo_Redo_Single_Line_InsertText ()
  2577. {
  2578. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  2579. var tv = new TextView { Width = 10, Height = 2, Text = text };
  2580. Toplevel top = new ();
  2581. top.Add (tv);
  2582. Application.Begin (top);
  2583. Assert.Equal (
  2584. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2585. tv.Text
  2586. );
  2587. Assert.Equal (3, tv.Lines);
  2588. Assert.Equal (Point.Empty, tv.CursorPosition);
  2589. var messy = " messy";
  2590. tv.CursorPosition = new (7, 1);
  2591. tv.InsertText (messy);
  2592. Assert.Equal (
  2593. $"This is the first line.{Environment.NewLine}This is messy the second line.{Environment.NewLine}This is the third line.",
  2594. tv.Text
  2595. );
  2596. Assert.Equal (3, tv.Lines);
  2597. Assert.Equal (new (13, 1), tv.CursorPosition);
  2598. for (var i = 0; i < messy.Length; i++)
  2599. {
  2600. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2601. }
  2602. Assert.Equal (
  2603. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2604. tv.Text
  2605. );
  2606. Assert.Equal (3, tv.Lines);
  2607. Assert.Equal (new (7, 1), tv.CursorPosition);
  2608. for (var i = 0; i < messy.Length; i++)
  2609. {
  2610. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2611. }
  2612. Assert.Equal (
  2613. $"This is the first line.{Environment.NewLine}This is messy the second line.{Environment.NewLine}This is the third line.",
  2614. tv.Text
  2615. );
  2616. Assert.Equal (3, tv.Lines);
  2617. Assert.Equal (new (13, 1), tv.CursorPosition);
  2618. top.Dispose ();
  2619. }
  2620. [Fact]
  2621. [SetupFakeApplication]
  2622. public void HistoryText_Undo_Redo_Single_Line_Selected_DeleteCharLeft ()
  2623. {
  2624. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  2625. var tv = new TextView { Width = 10, Height = 2, Text = text };
  2626. Toplevel top = new ();
  2627. top.Add (tv);
  2628. Application.Begin (top);
  2629. Assert.Equal (
  2630. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2631. tv.Text
  2632. );
  2633. Assert.Equal (3, tv.Lines);
  2634. Assert.Equal (Point.Empty, tv.CursorPosition);
  2635. var ntimes = 3;
  2636. tv.CursorPosition = new (7, 1);
  2637. tv.SelectionStartColumn = 11;
  2638. tv.SelectionStartRow = 1;
  2639. Assert.Equal (4, tv.SelectedLength);
  2640. for (var i = 0; i < ntimes; i++)
  2641. {
  2642. tv.DeleteCharLeft ();
  2643. }
  2644. Assert.Equal (
  2645. $"This is the first line.{Environment.NewLine}This second line.{Environment.NewLine}This is the third line.",
  2646. tv.Text
  2647. );
  2648. Assert.Equal (3, tv.Lines);
  2649. Assert.Equal (new (5, 1), tv.CursorPosition);
  2650. Assert.Equal (11, tv.SelectionStartColumn);
  2651. Assert.Equal (1, tv.SelectionStartRow);
  2652. Assert.Equal (0, tv.SelectedLength);
  2653. for (var i = 0; i < ntimes; i++)
  2654. {
  2655. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2656. }
  2657. Assert.Equal (
  2658. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2659. tv.Text
  2660. );
  2661. Assert.Equal (3, tv.Lines);
  2662. Assert.Equal (new (7, 1), tv.CursorPosition);
  2663. Assert.Equal (11, tv.SelectionStartColumn);
  2664. Assert.Equal (1, tv.SelectionStartRow);
  2665. Assert.Equal (0, tv.SelectedLength);
  2666. for (var i = 0; i < ntimes; i++)
  2667. {
  2668. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2669. }
  2670. Assert.Equal (
  2671. $"This is the first line.{Environment.NewLine}This second line.{Environment.NewLine}This is the third line.",
  2672. tv.Text
  2673. );
  2674. Assert.Equal (3, tv.Lines);
  2675. Assert.Equal (new (5, 1), tv.CursorPosition);
  2676. Assert.Equal (11, tv.SelectionStartColumn);
  2677. Assert.Equal (1, tv.SelectionStartRow);
  2678. Assert.Equal (0, tv.SelectedLength);
  2679. top.Dispose ();
  2680. }
  2681. [Fact]
  2682. [SetupFakeApplication]
  2683. public void HistoryText_Undo_Redo_Single_Line_Selected_DeleteCharRight ()
  2684. {
  2685. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  2686. var tv = new TextView { Width = 10, Height = 2, Text = text };
  2687. Toplevel top = new ();
  2688. top.Add (tv);
  2689. Application.Begin (top);
  2690. Assert.Equal (
  2691. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2692. tv.Text
  2693. );
  2694. Assert.Equal (3, tv.Lines);
  2695. Assert.Equal (Point.Empty, tv.CursorPosition);
  2696. var ntimes = 3;
  2697. tv.CursorPosition = new (7, 1);
  2698. tv.SelectionStartColumn = 11;
  2699. tv.SelectionStartRow = 1;
  2700. Assert.Equal (4, tv.SelectedLength);
  2701. for (var i = 0; i < ntimes; i++)
  2702. {
  2703. tv.DeleteCharRight ();
  2704. }
  2705. Assert.Equal (
  2706. $"This is the first line.{Environment.NewLine}This isecond line.{Environment.NewLine}This is the third line.",
  2707. tv.Text
  2708. );
  2709. Assert.Equal (3, tv.Lines);
  2710. Assert.Equal (new (7, 1), tv.CursorPosition);
  2711. Assert.Equal (11, tv.SelectionStartColumn);
  2712. Assert.Equal (1, tv.SelectionStartRow);
  2713. Assert.Equal (0, tv.SelectedLength);
  2714. for (var i = 0; i < ntimes; i++)
  2715. {
  2716. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2717. }
  2718. Assert.Equal (
  2719. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2720. tv.Text
  2721. );
  2722. Assert.Equal (3, tv.Lines);
  2723. Assert.Equal (new (7, 1), tv.CursorPosition);
  2724. Assert.Equal (11, tv.SelectionStartColumn);
  2725. Assert.Equal (1, tv.SelectionStartRow);
  2726. Assert.Equal (0, tv.SelectedLength);
  2727. for (var i = 0; i < ntimes; i++)
  2728. {
  2729. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2730. }
  2731. Assert.Equal (
  2732. $"This is the first line.{Environment.NewLine}This isecond line.{Environment.NewLine}This is the third line.",
  2733. tv.Text
  2734. );
  2735. Assert.Equal (3, tv.Lines);
  2736. Assert.Equal (new (7, 1), tv.CursorPosition);
  2737. Assert.Equal (11, tv.SelectionStartColumn);
  2738. Assert.Equal (1, tv.SelectionStartRow);
  2739. Assert.Equal (0, tv.SelectedLength);
  2740. top.Dispose ();
  2741. }
  2742. [Fact]
  2743. [SetupFakeApplication]
  2744. public void HistoryText_Undo_Redo_Single_Line_Selected_InsertText ()
  2745. {
  2746. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  2747. var tv = new TextView { Width = 10, Height = 2, Text = text };
  2748. Toplevel top = new ();
  2749. top.Add (tv);
  2750. Application.Begin (top);
  2751. Assert.Equal (
  2752. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2753. tv.Text
  2754. );
  2755. Assert.Equal (3, tv.Lines);
  2756. Assert.Equal (Point.Empty, tv.CursorPosition);
  2757. var messy = " messy";
  2758. tv.CursorPosition = new (7, 1);
  2759. tv.SelectionStartColumn = 11;
  2760. tv.SelectionStartRow = 1;
  2761. Assert.Equal (4, tv.SelectedLength);
  2762. tv.InsertText (messy);
  2763. Assert.Equal (
  2764. $"This is the first line.{Environment.NewLine}This is messy second line.{Environment.NewLine}This is the third line.",
  2765. tv.Text
  2766. );
  2767. Assert.Equal (3, tv.Lines);
  2768. Assert.Equal (new (13, 1), tv.CursorPosition);
  2769. Assert.Equal (11, tv.SelectionStartColumn);
  2770. Assert.Equal (1, tv.SelectionStartRow);
  2771. Assert.Equal (0, tv.SelectedLength);
  2772. for (var i = 0; i < messy.Length; i++)
  2773. {
  2774. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2775. }
  2776. Assert.Equal (
  2777. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2778. tv.Text
  2779. );
  2780. Assert.Equal (3, tv.Lines);
  2781. Assert.Equal (new (7, 1), tv.CursorPosition);
  2782. Assert.Equal (11, tv.SelectionStartColumn);
  2783. Assert.Equal (1, tv.SelectionStartRow);
  2784. Assert.Equal (0, tv.SelectedLength);
  2785. for (var i = 0; i < messy.Length; i++)
  2786. {
  2787. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2788. }
  2789. Assert.Equal (
  2790. $"This is the first line.{Environment.NewLine}This is messy second line.{Environment.NewLine}This is the third line.",
  2791. tv.Text
  2792. );
  2793. Assert.Equal (3, tv.Lines);
  2794. Assert.Equal (new (13, 1), tv.CursorPosition);
  2795. Assert.Equal (11, tv.SelectionStartColumn);
  2796. Assert.Equal (1, tv.SelectionStartRow);
  2797. Assert.Equal (0, tv.SelectedLength);
  2798. top.Dispose ();
  2799. }
  2800. [Fact]
  2801. [TextViewTestsSetupFakeApplication]
  2802. public void Kill_Delete_WordBackward ()
  2803. {
  2804. _textView.Text = "This is the first line.";
  2805. _textView.MoveEnd ();
  2806. var iteration = 0;
  2807. var iterationsFinished = false;
  2808. while (!iterationsFinished)
  2809. {
  2810. _textView.NewKeyDownEvent (Key.Backspace.WithCtrl);
  2811. switch (iteration)
  2812. {
  2813. case 0:
  2814. Assert.Equal (22, _textView.CursorPosition.X);
  2815. Assert.Equal (0, _textView.CursorPosition.Y);
  2816. Assert.Equal ("This is the first line", _textView.Text);
  2817. break;
  2818. case 1:
  2819. Assert.Equal (18, _textView.CursorPosition.X);
  2820. Assert.Equal (0, _textView.CursorPosition.Y);
  2821. Assert.Equal ("This is the first ", _textView.Text);
  2822. break;
  2823. case 2:
  2824. Assert.Equal (12, _textView.CursorPosition.X);
  2825. Assert.Equal (0, _textView.CursorPosition.Y);
  2826. Assert.Equal ("This is the ", _textView.Text);
  2827. break;
  2828. case 3:
  2829. Assert.Equal (8, _textView.CursorPosition.X);
  2830. Assert.Equal (0, _textView.CursorPosition.Y);
  2831. Assert.Equal ("This is ", _textView.Text);
  2832. break;
  2833. case 4:
  2834. Assert.Equal (5, _textView.CursorPosition.X);
  2835. Assert.Equal (0, _textView.CursorPosition.Y);
  2836. Assert.Equal ("This ", _textView.Text);
  2837. break;
  2838. case 5:
  2839. Assert.Equal (0, _textView.CursorPosition.X);
  2840. Assert.Equal (0, _textView.CursorPosition.Y);
  2841. Assert.Equal ("", _textView.Text);
  2842. break;
  2843. default:
  2844. iterationsFinished = true;
  2845. break;
  2846. }
  2847. iteration++;
  2848. }
  2849. }
  2850. [Fact]
  2851. [TextViewTestsSetupFakeApplication]
  2852. public void Kill_Delete_WordBackward_Multiline ()
  2853. {
  2854. _textView.Text = "This is the first line.\nThis is the second line.";
  2855. _textView.Width = 4;
  2856. _textView.MoveEnd ();
  2857. var iteration = 0;
  2858. var iterationsFinished = false;
  2859. while (!iterationsFinished)
  2860. {
  2861. _textView.NewKeyDownEvent (Key.Backspace.WithCtrl);
  2862. switch (iteration)
  2863. {
  2864. case 0:
  2865. Assert.Equal (23, _textView.CursorPosition.X);
  2866. Assert.Equal (1, _textView.CursorPosition.Y);
  2867. Assert.Equal (
  2868. "This is the first line."
  2869. + Environment.NewLine
  2870. + "This is the second line",
  2871. _textView.Text
  2872. );
  2873. break;
  2874. case 1:
  2875. Assert.Equal (19, _textView.CursorPosition.X);
  2876. Assert.Equal (1, _textView.CursorPosition.Y);
  2877. Assert.Equal (
  2878. "This is the first line."
  2879. + Environment.NewLine
  2880. + "This is the second ",
  2881. _textView.Text
  2882. );
  2883. break;
  2884. case 2:
  2885. Assert.Equal (12, _textView.CursorPosition.X);
  2886. Assert.Equal (1, _textView.CursorPosition.Y);
  2887. Assert.Equal (
  2888. "This is the first line."
  2889. + Environment.NewLine
  2890. + "This is the ",
  2891. _textView.Text
  2892. );
  2893. break;
  2894. case 3:
  2895. Assert.Equal (8, _textView.CursorPosition.X);
  2896. Assert.Equal (1, _textView.CursorPosition.Y);
  2897. Assert.Equal (
  2898. "This is the first line."
  2899. + Environment.NewLine
  2900. + "This is ",
  2901. _textView.Text
  2902. );
  2903. break;
  2904. case 4:
  2905. Assert.Equal (5, _textView.CursorPosition.X);
  2906. Assert.Equal (1, _textView.CursorPosition.Y);
  2907. Assert.Equal (
  2908. "This is the first line."
  2909. + Environment.NewLine
  2910. + "This ",
  2911. _textView.Text
  2912. );
  2913. break;
  2914. case 5:
  2915. Assert.Equal (0, _textView.CursorPosition.X);
  2916. Assert.Equal (1, _textView.CursorPosition.Y);
  2917. Assert.Equal ("This is the first line." + Environment.NewLine, _textView.Text);
  2918. break;
  2919. case 6:
  2920. Assert.Equal (23, _textView.CursorPosition.X);
  2921. Assert.Equal (0, _textView.CursorPosition.Y);
  2922. Assert.Equal ("This is the first line.", _textView.Text);
  2923. break;
  2924. case 7:
  2925. Assert.Equal (22, _textView.CursorPosition.X);
  2926. Assert.Equal (0, _textView.CursorPosition.Y);
  2927. Assert.Equal ("This is the first line", _textView.Text);
  2928. break;
  2929. case 8:
  2930. Assert.Equal (18, _textView.CursorPosition.X);
  2931. Assert.Equal (0, _textView.CursorPosition.Y);
  2932. Assert.Equal ("This is the first ", _textView.Text);
  2933. break;
  2934. case 9:
  2935. Assert.Equal (12, _textView.CursorPosition.X);
  2936. Assert.Equal (0, _textView.CursorPosition.Y);
  2937. Assert.Equal ("This is the ", _textView.Text);
  2938. break;
  2939. case 10:
  2940. Assert.Equal (8, _textView.CursorPosition.X);
  2941. Assert.Equal (0, _textView.CursorPosition.Y);
  2942. Assert.Equal ("This is ", _textView.Text);
  2943. break;
  2944. case 11:
  2945. Assert.Equal (5, _textView.CursorPosition.X);
  2946. Assert.Equal (0, _textView.CursorPosition.Y);
  2947. Assert.Equal ("This ", _textView.Text);
  2948. break;
  2949. case 12:
  2950. Assert.Equal (0, _textView.CursorPosition.X);
  2951. Assert.Equal (0, _textView.CursorPosition.Y);
  2952. Assert.Equal ("", _textView.Text);
  2953. break;
  2954. default:
  2955. iterationsFinished = true;
  2956. break;
  2957. }
  2958. iteration++;
  2959. }
  2960. }
  2961. [Fact]
  2962. [TextViewTestsSetupFakeApplication]
  2963. public void Kill_Delete_WordForward ()
  2964. {
  2965. _textView.Text = "This is the first line.";
  2966. var iteration = 0;
  2967. var iterationsFinished = false;
  2968. while (!iterationsFinished)
  2969. {
  2970. _textView.NewKeyDownEvent (Key.Delete.WithCtrl);
  2971. switch (iteration)
  2972. {
  2973. case 0:
  2974. Assert.Equal (0, _textView.CursorPosition.X);
  2975. Assert.Equal (0, _textView.CursorPosition.Y);
  2976. Assert.Equal ("is the first line.", _textView.Text);
  2977. break;
  2978. case 1:
  2979. Assert.Equal (0, _textView.CursorPosition.X);
  2980. Assert.Equal (0, _textView.CursorPosition.Y);
  2981. Assert.Equal ("the first line.", _textView.Text);
  2982. break;
  2983. case 2:
  2984. Assert.Equal (0, _textView.CursorPosition.X);
  2985. Assert.Equal (0, _textView.CursorPosition.Y);
  2986. Assert.Equal ("first line.", _textView.Text);
  2987. break;
  2988. case 3:
  2989. Assert.Equal (0, _textView.CursorPosition.X);
  2990. Assert.Equal (0, _textView.CursorPosition.Y);
  2991. Assert.Equal ("line.", _textView.Text);
  2992. break;
  2993. case 4:
  2994. Assert.Equal (0, _textView.CursorPosition.X);
  2995. Assert.Equal (0, _textView.CursorPosition.Y);
  2996. Assert.Equal (".", _textView.Text);
  2997. break;
  2998. case 5:
  2999. Assert.Equal (0, _textView.CursorPosition.X);
  3000. Assert.Equal (0, _textView.CursorPosition.Y);
  3001. Assert.Equal ("", _textView.Text);
  3002. break;
  3003. default:
  3004. iterationsFinished = true;
  3005. break;
  3006. }
  3007. iteration++;
  3008. }
  3009. }
  3010. [Fact]
  3011. [TextViewTestsSetupFakeApplication]
  3012. public void Kill_Delete_WordForward_Multiline ()
  3013. {
  3014. _textView.Text = "This is the first line.\nThis is the second line.";
  3015. _textView.Width = 4;
  3016. var iteration = 0;
  3017. var iterationsFinished = false;
  3018. while (!iterationsFinished)
  3019. {
  3020. _textView.NewKeyDownEvent (Key.Delete.WithCtrl);
  3021. switch (iteration)
  3022. {
  3023. case 0:
  3024. Assert.Equal (0, _textView.CursorPosition.X);
  3025. Assert.Equal (0, _textView.CursorPosition.Y);
  3026. Assert.Equal (
  3027. "is the first line."
  3028. + Environment.NewLine
  3029. + "This is the second line.",
  3030. _textView.Text
  3031. );
  3032. break;
  3033. case 1:
  3034. Assert.Equal (0, _textView.CursorPosition.X);
  3035. Assert.Equal (0, _textView.CursorPosition.Y);
  3036. Assert.Equal (
  3037. "the first line."
  3038. + Environment.NewLine
  3039. + "This is the second line.",
  3040. _textView.Text
  3041. );
  3042. break;
  3043. case 2:
  3044. Assert.Equal (0, _textView.CursorPosition.X);
  3045. Assert.Equal (0, _textView.CursorPosition.Y);
  3046. Assert.Equal (
  3047. "first line."
  3048. + Environment.NewLine
  3049. + "This is the second line.",
  3050. _textView.Text
  3051. );
  3052. break;
  3053. case 3:
  3054. Assert.Equal (0, _textView.CursorPosition.X);
  3055. Assert.Equal (0, _textView.CursorPosition.Y);
  3056. Assert.Equal (
  3057. "line."
  3058. + Environment.NewLine
  3059. + "This is the second line.",
  3060. _textView.Text
  3061. );
  3062. break;
  3063. case 4:
  3064. Assert.Equal (0, _textView.CursorPosition.X);
  3065. Assert.Equal (0, _textView.CursorPosition.Y);
  3066. Assert.Equal (
  3067. "."
  3068. + Environment.NewLine
  3069. + "This is the second line.",
  3070. _textView.Text
  3071. );
  3072. break;
  3073. case 5:
  3074. Assert.Equal (0, _textView.CursorPosition.X);
  3075. Assert.Equal (0, _textView.CursorPosition.Y);
  3076. Assert.Equal (
  3077. ""
  3078. + Environment.NewLine
  3079. + "This is the second line.",
  3080. _textView.Text
  3081. );
  3082. break;
  3083. case 6:
  3084. Assert.Equal (0, _textView.CursorPosition.X);
  3085. Assert.Equal (0, _textView.CursorPosition.Y);
  3086. Assert.Equal ("This is the second line.", _textView.Text);
  3087. break;
  3088. case 7:
  3089. Assert.Equal (0, _textView.CursorPosition.X);
  3090. Assert.Equal (0, _textView.CursorPosition.Y);
  3091. Assert.Equal ("is the second line.", _textView.Text);
  3092. break;
  3093. case 8:
  3094. Assert.Equal (0, _textView.CursorPosition.X);
  3095. Assert.Equal (0, _textView.CursorPosition.Y);
  3096. Assert.Equal ("the second line.", _textView.Text);
  3097. break;
  3098. case 9:
  3099. Assert.Equal (0, _textView.CursorPosition.X);
  3100. Assert.Equal (0, _textView.CursorPosition.Y);
  3101. Assert.Equal ("second line.", _textView.Text);
  3102. break;
  3103. case 10:
  3104. Assert.Equal (0, _textView.CursorPosition.X);
  3105. Assert.Equal (0, _textView.CursorPosition.Y);
  3106. Assert.Equal ("line.", _textView.Text);
  3107. break;
  3108. case 11:
  3109. Assert.Equal (0, _textView.CursorPosition.X);
  3110. Assert.Equal (0, _textView.CursorPosition.Y);
  3111. Assert.Equal (".", _textView.Text);
  3112. break;
  3113. case 12:
  3114. Assert.Equal (0, _textView.CursorPosition.X);
  3115. Assert.Equal (0, _textView.CursorPosition.Y);
  3116. Assert.Equal ("", _textView.Text);
  3117. break;
  3118. default:
  3119. iterationsFinished = true;
  3120. break;
  3121. }
  3122. iteration++;
  3123. }
  3124. }
  3125. [Fact (Skip = "Fake Clipboard is broken")]
  3126. [TextViewTestsSetupFakeApplication]
  3127. public void Kill_To_End_Delete_Forwards_Copy_To_The_Clipboard_And_Paste ()
  3128. {
  3129. _textView.Text = "This is the first line.\nThis is the second line.";
  3130. var iteration = 0;
  3131. var iterationsFinished = false;
  3132. while (!iterationsFinished)
  3133. {
  3134. switch (iteration)
  3135. {
  3136. case 0:
  3137. _textView.NewKeyDownEvent (Key.K.WithCtrl);
  3138. Assert.Equal (0, _textView.CursorPosition.X);
  3139. Assert.Equal (0, _textView.CursorPosition.Y);
  3140. Assert.Equal ($"{Environment.NewLine}This is the second line.", _textView.Text);
  3141. Assert.Equal ("This is the first line.", Clipboard.Contents);
  3142. break;
  3143. case 1:
  3144. _textView.NewKeyDownEvent (
  3145. new (
  3146. KeyCode.Delete | KeyCode.CtrlMask | KeyCode.ShiftMask
  3147. )
  3148. );
  3149. Assert.Equal (0, _textView.CursorPosition.X);
  3150. Assert.Equal (0, _textView.CursorPosition.Y);
  3151. Assert.Equal ("This is the second line.", _textView.Text);
  3152. Assert.Equal ($"This is the first line.{Environment.NewLine}", Clipboard.Contents);
  3153. break;
  3154. case 2:
  3155. _textView.NewKeyDownEvent (Key.K.WithCtrl);
  3156. Assert.Equal (0, _textView.CursorPosition.X);
  3157. Assert.Equal (0, _textView.CursorPosition.Y);
  3158. Assert.Equal ("", _textView.Text);
  3159. Assert.Equal (
  3160. $"This is the first line.{Environment.NewLine}This is the second line.",
  3161. Clipboard.Contents
  3162. );
  3163. // Paste
  3164. _textView.NewKeyDownEvent (Key.Y.WithCtrl);
  3165. Assert.Equal (
  3166. $"This is the first line.{Environment.NewLine}This is the second line.",
  3167. _textView.Text
  3168. );
  3169. break;
  3170. default:
  3171. iterationsFinished = true;
  3172. break;
  3173. }
  3174. iteration++;
  3175. }
  3176. }
  3177. [Fact (Skip = "FakeClipboard is broken in some way, causing this unit test to fail intermittently.")]
  3178. [TextViewTestsSetupFakeApplication]
  3179. public void Kill_To_Start_Delete_Backwards_Copy_To_The_Clipboard_And_Paste ()
  3180. {
  3181. _textView.Text = "This is the first line.\nThis is the second line.";
  3182. _textView.MoveEnd ();
  3183. var iteration = 0;
  3184. var iterationsFinished = false;
  3185. while (!iterationsFinished)
  3186. {
  3187. switch (iteration)
  3188. {
  3189. case 0:
  3190. _textView.NewKeyDownEvent (Key.Backspace.WithCtrl.WithShift);
  3191. Assert.Equal (0, _textView.CursorPosition.X);
  3192. Assert.Equal (1, _textView.CursorPosition.Y);
  3193. Assert.Equal ($"This is the first line.{Environment.NewLine}", _textView.Text);
  3194. Assert.Equal ("This is the second line.", Clipboard.Contents);
  3195. break;
  3196. case 1:
  3197. _textView.NewKeyDownEvent (
  3198. new (
  3199. KeyCode.Backspace | KeyCode.CtrlMask | KeyCode.ShiftMask
  3200. )
  3201. );
  3202. Assert.Equal (23, _textView.CursorPosition.X);
  3203. Assert.Equal (0, _textView.CursorPosition.Y);
  3204. Assert.Equal ("This is the first line.", _textView.Text);
  3205. Assert.Equal ($"This is the second line.{Environment.NewLine}", Clipboard.Contents);
  3206. break;
  3207. case 2:
  3208. _textView.NewKeyDownEvent (Key.Backspace.WithCtrl.WithShift);
  3209. Assert.Equal (0, _textView.CursorPosition.X);
  3210. Assert.Equal (0, _textView.CursorPosition.Y);
  3211. Assert.Equal ("", _textView.Text);
  3212. Assert.Equal (
  3213. $"This is the second line.{Environment.NewLine}This is the first line.",
  3214. Clipboard.Contents
  3215. );
  3216. // Paste inverted
  3217. _textView.NewKeyDownEvent (Key.Y.WithCtrl);
  3218. Assert.Equal (
  3219. $"This is the second line.{Environment.NewLine}This is the first line.",
  3220. _textView.Text
  3221. );
  3222. break;
  3223. default:
  3224. iterationsFinished = true;
  3225. break;
  3226. }
  3227. iteration++;
  3228. }
  3229. }
  3230. [Fact]
  3231. [TextViewTestsSetupFakeApplication]
  3232. public void Mouse_Button_Shift_Preserves_Selection ()
  3233. {
  3234. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  3235. Assert.True (
  3236. _textView.NewMouseEvent (
  3237. new () { Position = new (12, 0), Flags = MouseFlags.Button1Pressed | MouseFlags.ButtonShift }
  3238. )
  3239. );
  3240. Assert.Equal (0, _textView.SelectionStartColumn);
  3241. Assert.Equal (0, _textView.SelectionStartRow);
  3242. Assert.Equal (new (12, 0), _textView.CursorPosition);
  3243. Assert.True (_textView.IsSelecting);
  3244. Assert.Equal ("TAB to jump ", _textView.SelectedText);
  3245. Assert.True (_textView.NewMouseEvent (new () { Position = new (12, 0), Flags = MouseFlags.Button1Clicked }));
  3246. Assert.Equal (0, _textView.SelectionStartRow);
  3247. Assert.Equal (0, _textView.SelectionStartRow);
  3248. Assert.Equal (new (12, 0), _textView.CursorPosition);
  3249. Assert.True (_textView.IsSelecting);
  3250. Assert.Equal ("TAB to jump ", _textView.SelectedText);
  3251. Assert.True (
  3252. _textView.NewMouseEvent (
  3253. new () { Position = new (19, 0), Flags = MouseFlags.Button1Pressed | MouseFlags.ButtonShift }
  3254. )
  3255. );
  3256. Assert.Equal (0, _textView.SelectionStartRow);
  3257. Assert.Equal (0, _textView.SelectionStartRow);
  3258. Assert.Equal (new (19, 0), _textView.CursorPosition);
  3259. Assert.True (_textView.IsSelecting);
  3260. Assert.Equal ("TAB to jump between", _textView.SelectedText);
  3261. Assert.True (_textView.NewMouseEvent (new () { Position = new (19, 0), Flags = MouseFlags.Button1Clicked }));
  3262. Assert.Equal (0, _textView.SelectionStartRow);
  3263. Assert.Equal (0, _textView.SelectionStartRow);
  3264. Assert.Equal (new (19, 0), _textView.CursorPosition);
  3265. Assert.True (_textView.IsSelecting);
  3266. Assert.Equal ("TAB to jump between", _textView.SelectedText);
  3267. Assert.True (
  3268. _textView.NewMouseEvent (
  3269. new () { Position = new (24, 0), Flags = MouseFlags.Button1Pressed | MouseFlags.ButtonShift }
  3270. )
  3271. );
  3272. Assert.Equal (0, _textView.SelectionStartRow);
  3273. Assert.Equal (0, _textView.SelectionStartRow);
  3274. Assert.Equal (new (24, 0), _textView.CursorPosition);
  3275. Assert.True (_textView.IsSelecting);
  3276. Assert.Equal ("TAB to jump between text", _textView.SelectedText);
  3277. Assert.True (_textView.NewMouseEvent (new () { Position = new (24, 0), Flags = MouseFlags.Button1Clicked }));
  3278. Assert.Equal (0, _textView.SelectionStartRow);
  3279. Assert.Equal (0, _textView.SelectionStartRow);
  3280. Assert.Equal (new (24, 0), _textView.CursorPosition);
  3281. Assert.True (_textView.IsSelecting);
  3282. Assert.Equal ("TAB to jump between text", _textView.SelectedText);
  3283. Assert.True (_textView.NewMouseEvent (new () { Position = new (24, 0), Flags = MouseFlags.Button1Pressed }));
  3284. Assert.Equal (0, _textView.SelectionStartRow);
  3285. Assert.Equal (0, _textView.SelectionStartRow);
  3286. Assert.Equal (new (24, 0), _textView.CursorPosition);
  3287. Assert.True (_textView.IsSelecting);
  3288. Assert.Equal ("", _textView.SelectedText);
  3289. }
  3290. [Fact]
  3291. [SetupFakeApplication]
  3292. public void MoveDown_By_Setting_CursorPosition ()
  3293. {
  3294. var tv = new TextView { Width = 10, Height = 5 };
  3295. // add 100 lines of wide text to view
  3296. for (var i = 0; i < 100; i++)
  3297. {
  3298. tv.Text += new string ('x', 100) + (i == 99 ? "" : Environment.NewLine);
  3299. }
  3300. Assert.Equal (Point.Empty, tv.CursorPosition);
  3301. tv.CursorPosition = new (5, 50);
  3302. Assert.Equal (new (5, 50), tv.CursorPosition);
  3303. tv.CursorPosition = new (200, 200);
  3304. Assert.Equal (new (100, 99), tv.CursorPosition);
  3305. }
  3306. [Fact]
  3307. [TextViewTestsSetupFakeApplication]
  3308. public void Multiline_Setting_Changes_AllowsReturn_AllowsTab_Height_WordWrap ()
  3309. {
  3310. Assert.True (_textView.Multiline);
  3311. Assert.True (_textView.AllowsReturn);
  3312. Assert.Equal (4, _textView.TabWidth);
  3313. Assert.True (_textView.AllowsTab);
  3314. Assert.False (_textView.WordWrap);
  3315. _textView.WordWrap = true;
  3316. Assert.True (_textView.WordWrap);
  3317. _textView.Multiline = false;
  3318. Assert.False (_textView.Multiline);
  3319. Assert.False (_textView.AllowsReturn);
  3320. Assert.Equal (0, _textView.TabWidth);
  3321. Assert.False (_textView.AllowsTab);
  3322. Assert.False (_textView.WordWrap);
  3323. _textView.WordWrap = true;
  3324. Assert.False (_textView.WordWrap);
  3325. _textView.Multiline = true;
  3326. Assert.True (_textView.Multiline);
  3327. Assert.True (_textView.AllowsReturn);
  3328. Assert.Equal (4, _textView.TabWidth);
  3329. Assert.True (_textView.AllowsTab);
  3330. Assert.False (_textView.WordWrap);
  3331. }
  3332. [Fact]
  3333. [TextViewTestsSetupFakeApplication]
  3334. public void Paste_Always_Clear_The_SelectedText ()
  3335. {
  3336. _textView.SelectionStartColumn = 20;
  3337. _textView.SelectionStartRow = 0;
  3338. _textView.CursorPosition = new (24, 0);
  3339. _textView.NewKeyDownEvent (Key.C.WithCtrl); // Copy
  3340. Assert.Equal ("text", _textView.SelectedText);
  3341. _textView.NewKeyDownEvent (Key.Y.WithCtrl); // Paste
  3342. Assert.Equal ("", _textView.SelectedText);
  3343. }
  3344. [Fact]
  3345. [SetupFakeApplication]
  3346. public void ScrollDownTillCaretOffscreen_ThenType ()
  3347. {
  3348. var tv = new TextView { Width = 10, Height = 5 };
  3349. // add 100 lines of wide text to view
  3350. for (var i = 0; i < 100; i++)
  3351. {
  3352. tv.Text += new string ('x', 100) + Environment.NewLine;
  3353. }
  3354. Assert.Equal (0, tv.CursorPosition.Y);
  3355. tv.ScrollTo (50);
  3356. Assert.Equal (0, tv.CursorPosition.Y);
  3357. tv.NewKeyDownEvent (Key.P);
  3358. }
  3359. [Fact]
  3360. [SetupFakeApplication]
  3361. public void ScrollTo_CursorPosition ()
  3362. {
  3363. var tv = new TextView { Width = 10, Height = 5 };
  3364. // add 100 lines of wide text to view
  3365. for (var i = 0; i < 100; i++)
  3366. {
  3367. tv.Text += new string ('x', 100) + (i == 99 ? "" : Environment.NewLine);
  3368. }
  3369. Assert.Equal (Point.Empty, tv.CursorPosition);
  3370. tv.ScrollTo (50);
  3371. Assert.Equal (Point.Empty, tv.CursorPosition);
  3372. tv.CursorPosition = new (tv.LeftColumn, tv.TopRow);
  3373. Assert.Equal (new (0, 50), tv.CursorPosition);
  3374. }
  3375. [Fact]
  3376. [TextViewTestsSetupFakeApplication]
  3377. public void Selected_Text_Shows ()
  3378. {
  3379. // Proves #3022 is fixed (TextField selected text does not show in v2)
  3380. Toplevel top = new ();
  3381. top.Add (_textView);
  3382. SessionToken rs = Application.Begin (top);
  3383. _textView.CursorPosition = Point.Empty;
  3384. _textView.SelectionStartColumn = 0;
  3385. _textView.SelectionStartRow = 0;
  3386. Attribute [] attributes =
  3387. {
  3388. _textView.GetScheme ().Focus,
  3389. new (_textView.GetScheme ().Focus.Background, _textView.GetScheme ().Focus.Foreground)
  3390. };
  3391. // TAB to jump between text fields.
  3392. DriverAssert.AssertDriverAttributesAre ("0000000", _output, Application.Driver, attributes);
  3393. Assert.Empty (_textView.SelectedCellsList);
  3394. _textView.NewKeyDownEvent (Key.CursorRight.WithCtrl.WithShift);
  3395. SetupFakeApplicationAttribute.RunIteration ();
  3396. Assert.Equal (new (4, 0), _textView.CursorPosition);
  3397. // TAB to jump between text fields.
  3398. DriverAssert.AssertDriverAttributesAre ("1111000", _output, Application.Driver, attributes);
  3399. Assert.Equal ("TAB ", Cell.ToString (_textView.SelectedCellsList [^1]));
  3400. top.Dispose ();
  3401. }
  3402. [Fact]
  3403. [TextViewTestsSetupFakeApplication]
  3404. public void Selection_And_CursorPosition_With_Value_Greater_Than_Text_Length_Changes_Both_To_Text_Length ()
  3405. {
  3406. _textView.CursorPosition = new (33, 2);
  3407. _textView.SelectionStartColumn = 33;
  3408. _textView.SelectionStartRow = 33;
  3409. Assert.Equal (32, _textView.CursorPosition.X);
  3410. Assert.Equal (0, _textView.CursorPosition.Y);
  3411. Assert.Equal (32, _textView.SelectionStartColumn);
  3412. Assert.Equal (0, _textView.SelectionStartRow);
  3413. Assert.Equal (0, _textView.SelectedLength);
  3414. Assert.Equal ("", _textView.SelectedText);
  3415. }
  3416. [Fact]
  3417. [TextViewTestsSetupFakeApplication]
  3418. public void Selection_With_Empty_Text ()
  3419. {
  3420. _textView = new ();
  3421. _textView.CursorPosition = new (2, 0);
  3422. _textView.SelectionStartColumn = 33;
  3423. _textView.SelectionStartRow = 1;
  3424. Assert.Equal (0, _textView.SelectionStartColumn);
  3425. Assert.Equal (0, _textView.SelectionStartRow);
  3426. Assert.Equal (0, _textView.SelectedLength);
  3427. Assert.Equal ("", _textView.SelectedText);
  3428. }
  3429. [Fact]
  3430. [TextViewTestsSetupFakeApplication]
  3431. public void Selection_With_Value_Greater_Than_Text_Length_Changes_To_Text_Length ()
  3432. {
  3433. _textView.CursorPosition = new (2, 0);
  3434. _textView.SelectionStartColumn = 33;
  3435. _textView.SelectionStartRow = 1;
  3436. Assert.Equal (32, _textView.SelectionStartColumn);
  3437. Assert.Equal (0, _textView.SelectionStartRow);
  3438. Assert.Equal (30, _textView.SelectedLength);
  3439. Assert.Equal ("B to jump between text fields.", _textView.SelectedText);
  3440. }
  3441. [Fact]
  3442. [TextViewTestsSetupFakeApplication]
  3443. public void Selection_With_Value_Less_Than_Zero_Changes_To_Zero ()
  3444. {
  3445. _textView.SelectionStartColumn = -2;
  3446. _textView.SelectionStartRow = -2;
  3447. Assert.Equal (0, _textView.SelectionStartColumn);
  3448. Assert.Equal (0, _textView.SelectionStartRow);
  3449. Assert.Equal (0, _textView.SelectedLength);
  3450. Assert.Equal ("", _textView.SelectedText);
  3451. }
  3452. [Fact]
  3453. [TextViewTestsSetupFakeApplication]
  3454. public void TextChanged_Event ()
  3455. {
  3456. _textView.TextChanged += (s, e) =>
  3457. {
  3458. if (_textView.Text == "changing")
  3459. {
  3460. Assert.Equal ("changing", _textView.Text);
  3461. _textView.Text = "changed";
  3462. }
  3463. };
  3464. _textView.Text = "changing";
  3465. Assert.Equal ("changed", _textView.Text);
  3466. }
  3467. [Fact]
  3468. [TextViewTestsSetupFakeApplication]
  3469. public void TextChanged_Event_NoFires_OnTyping ()
  3470. {
  3471. var eventcount = 0;
  3472. _textView.TextChanged += (s, e) => { eventcount++; };
  3473. _textView.Text = "ay";
  3474. Assert.Equal (1, eventcount);
  3475. _textView.NewKeyDownEvent (Key.Y.WithShift);
  3476. Assert.Equal (1, eventcount);
  3477. Assert.Equal ("Yay", _textView.Text);
  3478. }
  3479. [Fact]
  3480. [TextViewTestsSetupFakeApplication]
  3481. public void TextView_InsertText_Newline_CRLF ()
  3482. {
  3483. var tv = new TextView { Width = 10, Height = 10 };
  3484. tv.InsertText ("\r\naaa\r\nbbb");
  3485. PlatformID p = Environment.OSVersion.Platform;
  3486. if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows)
  3487. {
  3488. Assert.Equal ("\r\naaa\r\nbbb", tv.Text);
  3489. }
  3490. else
  3491. {
  3492. Assert.Equal ("\naaa\nbbb", tv.Text);
  3493. }
  3494. Assert.Equal ($"{Environment.NewLine}aaa{Environment.NewLine}bbb", tv.Text);
  3495. var win = new Window ();
  3496. win.Add (tv);
  3497. var top = new Toplevel ();
  3498. top.Add (win);
  3499. Application.Begin (top);
  3500. Application.Driver!.SetScreenSize (15, 15);
  3501. SetupFakeApplicationAttribute.RunIteration ();
  3502. //this passes
  3503. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (
  3504. @"
  3505. ┌─────────────┐
  3506. │ │
  3507. │aaa │
  3508. │bbb │
  3509. │ │
  3510. │ │
  3511. │ │
  3512. │ │
  3513. │ │
  3514. │ │
  3515. │ │
  3516. │ │
  3517. │ │
  3518. │ │
  3519. └─────────────┘",
  3520. _output
  3521. );
  3522. Assert.Equal (new (0, 0, 15, 15), pos);
  3523. Assert.True (tv.Used);
  3524. tv.Used = false;
  3525. tv.CursorPosition = Point.Empty;
  3526. tv.InsertText ("\r\naaa\r\nbbb");
  3527. SetupFakeApplicationAttribute.RunIteration ();
  3528. DriverAssert.AssertDriverContentsWithFrameAre (
  3529. @"
  3530. ┌─────────────┐
  3531. │ │
  3532. │aaa │
  3533. │bbb │
  3534. │aaa │
  3535. │bbb │
  3536. │ │
  3537. │ │
  3538. │ │
  3539. │ │
  3540. │ │
  3541. │ │
  3542. │ │
  3543. │ │
  3544. └─────────────┘",
  3545. _output
  3546. );
  3547. top.Dispose ();
  3548. }
  3549. [Fact]
  3550. [TextViewTestsSetupFakeApplication]
  3551. public void TextView_InsertText_Newline_LF ()
  3552. {
  3553. var tv = new TextView { Width = 10, Height = 10 };
  3554. tv.InsertText ("\naaa\nbbb");
  3555. PlatformID p = Environment.OSVersion.Platform;
  3556. if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows)
  3557. {
  3558. Assert.Equal ("\r\naaa\r\nbbb", tv.Text);
  3559. }
  3560. else
  3561. {
  3562. Assert.Equal ("\naaa\nbbb", tv.Text);
  3563. }
  3564. Assert.Equal ($"{Environment.NewLine}aaa{Environment.NewLine}bbb", tv.Text);
  3565. var win = new Window ();
  3566. win.Add (tv);
  3567. var top = new Toplevel ();
  3568. top.Add (win);
  3569. Application.Begin (top);
  3570. Application.Driver!.SetScreenSize (15, 15);
  3571. SetupFakeApplicationAttribute.RunIteration ();
  3572. //this passes
  3573. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (
  3574. @"
  3575. ┌─────────────┐
  3576. │ │
  3577. │aaa │
  3578. │bbb │
  3579. │ │
  3580. │ │
  3581. │ │
  3582. │ │
  3583. │ │
  3584. │ │
  3585. │ │
  3586. │ │
  3587. │ │
  3588. │ │
  3589. └─────────────┘",
  3590. _output
  3591. );
  3592. Assert.Equal (new (0, 0, 15, 15), pos);
  3593. Assert.True (tv.Used);
  3594. tv.Used = false;
  3595. tv.CursorPosition = Point.Empty;
  3596. tv.InsertText ("\naaa\nbbb");
  3597. SetupFakeApplicationAttribute.RunIteration ();
  3598. DriverAssert.AssertDriverContentsWithFrameAre (
  3599. @"
  3600. ┌─────────────┐
  3601. │ │
  3602. │aaa │
  3603. │bbb │
  3604. │aaa │
  3605. │bbb │
  3606. │ │
  3607. │ │
  3608. │ │
  3609. │ │
  3610. │ │
  3611. │ │
  3612. │ │
  3613. │ │
  3614. └─────────────┘",
  3615. _output
  3616. );
  3617. top.Dispose ();
  3618. }
  3619. [Fact]
  3620. [TextViewTestsSetupFakeApplication]
  3621. public void TextView_SpaceHandling ()
  3622. {
  3623. var tv = new TextView { Width = 10, Text = " " };
  3624. var ev = new MouseEventArgs { Position = new (0, 0), Flags = MouseFlags.Button1DoubleClicked };
  3625. tv.NewMouseEvent (ev);
  3626. Assert.Equal (1, tv.SelectedLength);
  3627. ev = new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked };
  3628. tv.NewMouseEvent (ev);
  3629. Assert.Equal (1, tv.SelectedLength);
  3630. }
  3631. [Fact]
  3632. [TextViewTestsSetupFakeApplication]
  3633. public void Used_Is_False ()
  3634. {
  3635. _textView.Used = false;
  3636. _textView.CursorPosition = new (10, 0);
  3637. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  3638. _textView.NewKeyDownEvent (Key.U); // u
  3639. Assert.Equal ("TAB to jumu between text fields.", _textView.Text);
  3640. _textView.NewKeyDownEvent (Key.S); // s
  3641. Assert.Equal ("TAB to jumusbetween text fields.", _textView.Text);
  3642. _textView.NewKeyDownEvent (Key.E); // e
  3643. Assert.Equal ("TAB to jumuseetween text fields.", _textView.Text);
  3644. _textView.NewKeyDownEvent (Key.D); // d
  3645. Assert.Equal ("TAB to jumusedtween text fields.", _textView.Text);
  3646. }
  3647. [Fact]
  3648. [TextViewTestsSetupFakeApplication]
  3649. public void Used_Is_True_By_Default ()
  3650. {
  3651. _textView.CursorPosition = new (10, 0);
  3652. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  3653. _textView.NewKeyDownEvent (Key.U); // u
  3654. Assert.Equal ("TAB to jumup between text fields.", _textView.Text);
  3655. _textView.NewKeyDownEvent (Key.S); // s
  3656. Assert.Equal ("TAB to jumusp between text fields.", _textView.Text);
  3657. _textView.NewKeyDownEvent (Key.E); // e
  3658. Assert.Equal ("TAB to jumusep between text fields.", _textView.Text);
  3659. _textView.NewKeyDownEvent (Key.D); // d
  3660. Assert.Equal ("TAB to jumusedp between text fields.", _textView.Text);
  3661. }
  3662. [Fact]
  3663. [TextViewTestsSetupFakeApplication]
  3664. public void WordBackward_Multiline_With_Selection ()
  3665. {
  3666. // 4 3 2 1
  3667. // 87654321098765432109876 54321098765432109876543210-Length
  3668. // 1 2 1 2
  3669. // 01234567890123456789012 0123456789012345678901234
  3670. _textView.Text = "This is the first line.\nThis is the second line.";
  3671. _textView.MoveEnd ();
  3672. _textView.SelectionStartColumn = _textView.CurrentColumn;
  3673. _textView.SelectionStartRow = _textView.CurrentRow;
  3674. var iteration = 0;
  3675. var iterationsFinished = false;
  3676. while (!iterationsFinished)
  3677. {
  3678. _textView.NewKeyDownEvent (Key.CursorLeft.WithCtrl.WithShift);
  3679. switch (iteration)
  3680. {
  3681. case 0:
  3682. Assert.Equal (23, _textView.CursorPosition.X);
  3683. Assert.Equal (1, _textView.CursorPosition.Y);
  3684. Assert.Equal (24, _textView.SelectionStartColumn);
  3685. Assert.Equal (1, _textView.SelectionStartRow);
  3686. Assert.Equal (1, _textView.SelectedLength);
  3687. Assert.Equal (".", _textView.SelectedText);
  3688. break;
  3689. case 1:
  3690. Assert.Equal (19, _textView.CursorPosition.X);
  3691. Assert.Equal (1, _textView.CursorPosition.Y);
  3692. Assert.Equal (24, _textView.SelectionStartColumn);
  3693. Assert.Equal (1, _textView.SelectionStartRow);
  3694. Assert.Equal (5, _textView.SelectedLength);
  3695. Assert.Equal ("line.", _textView.SelectedText);
  3696. break;
  3697. case 2:
  3698. Assert.Equal (12, _textView.CursorPosition.X);
  3699. Assert.Equal (1, _textView.CursorPosition.Y);
  3700. Assert.Equal (24, _textView.SelectionStartColumn);
  3701. Assert.Equal (1, _textView.SelectionStartRow);
  3702. Assert.Equal (12, _textView.SelectedLength);
  3703. Assert.Equal ("second line.", _textView.SelectedText);
  3704. break;
  3705. case 3:
  3706. Assert.Equal (8, _textView.CursorPosition.X);
  3707. Assert.Equal (1, _textView.CursorPosition.Y);
  3708. Assert.Equal (24, _textView.SelectionStartColumn);
  3709. Assert.Equal (1, _textView.SelectionStartRow);
  3710. Assert.Equal (16, _textView.SelectedLength);
  3711. Assert.Equal ("the second line.", _textView.SelectedText);
  3712. break;
  3713. case 4:
  3714. Assert.Equal (5, _textView.CursorPosition.X);
  3715. Assert.Equal (1, _textView.CursorPosition.Y);
  3716. Assert.Equal (24, _textView.SelectionStartColumn);
  3717. Assert.Equal (1, _textView.SelectionStartRow);
  3718. Assert.Equal (19, _textView.SelectedLength);
  3719. Assert.Equal ("is the second line.", _textView.SelectedText);
  3720. break;
  3721. case 5:
  3722. Assert.Equal (0, _textView.CursorPosition.X);
  3723. Assert.Equal (1, _textView.CursorPosition.Y);
  3724. Assert.Equal (24, _textView.SelectionStartColumn);
  3725. Assert.Equal (1, _textView.SelectionStartRow);
  3726. Assert.Equal (24, _textView.SelectedLength);
  3727. Assert.Equal ("This is the second line.", _textView.SelectedText);
  3728. break;
  3729. case 6:
  3730. Assert.Equal (23, _textView.CursorPosition.X);
  3731. Assert.Equal (0, _textView.CursorPosition.Y);
  3732. Assert.Equal (24, _textView.SelectionStartColumn);
  3733. Assert.Equal (1, _textView.SelectionStartRow);
  3734. Assert.Equal (24 + Environment.NewLine.Length, _textView.SelectedLength);
  3735. Assert.Equal ($"{Environment.NewLine}This is the second line.", _textView.SelectedText);
  3736. break;
  3737. case 7:
  3738. Assert.Equal (22, _textView.CursorPosition.X);
  3739. Assert.Equal (0, _textView.CursorPosition.Y);
  3740. Assert.Equal (24, _textView.SelectionStartColumn);
  3741. Assert.Equal (1, _textView.SelectionStartRow);
  3742. Assert.Equal (25 + Environment.NewLine.Length, _textView.SelectedLength);
  3743. Assert.Equal (
  3744. $".{Environment.NewLine}This is the second line.",
  3745. _textView.SelectedText
  3746. );
  3747. break;
  3748. case 8:
  3749. Assert.Equal (18, _textView.CursorPosition.X);
  3750. Assert.Equal (0, _textView.CursorPosition.Y);
  3751. Assert.Equal (24, _textView.SelectionStartColumn);
  3752. Assert.Equal (1, _textView.SelectionStartRow);
  3753. Assert.Equal (29 + Environment.NewLine.Length, _textView.SelectedLength);
  3754. Assert.Equal (
  3755. $"line.{Environment.NewLine}This is the second line.",
  3756. _textView.SelectedText
  3757. );
  3758. break;
  3759. case 9:
  3760. Assert.Equal (12, _textView.CursorPosition.X);
  3761. Assert.Equal (0, _textView.CursorPosition.Y);
  3762. Assert.Equal (24, _textView.SelectionStartColumn);
  3763. Assert.Equal (1, _textView.SelectionStartRow);
  3764. Assert.Equal (35 + Environment.NewLine.Length, _textView.SelectedLength);
  3765. Assert.Equal (
  3766. $"first line.{Environment.NewLine}This is the second line.",
  3767. _textView.SelectedText
  3768. );
  3769. break;
  3770. case 10:
  3771. Assert.Equal (8, _textView.CursorPosition.X);
  3772. Assert.Equal (0, _textView.CursorPosition.Y);
  3773. Assert.Equal (24, _textView.SelectionStartColumn);
  3774. Assert.Equal (1, _textView.SelectionStartRow);
  3775. Assert.Equal (39 + Environment.NewLine.Length, _textView.SelectedLength);
  3776. Assert.Equal (
  3777. $"the first line.{Environment.NewLine}This is the second line.",
  3778. _textView.SelectedText
  3779. );
  3780. break;
  3781. case 11:
  3782. Assert.Equal (5, _textView.CursorPosition.X);
  3783. Assert.Equal (0, _textView.CursorPosition.Y);
  3784. Assert.Equal (24, _textView.SelectionStartColumn);
  3785. Assert.Equal (1, _textView.SelectionStartRow);
  3786. Assert.Equal (42 + Environment.NewLine.Length, _textView.SelectedLength);
  3787. Assert.Equal (
  3788. $"is the first line.{Environment.NewLine}This is the second line.",
  3789. _textView.SelectedText
  3790. );
  3791. break;
  3792. case 12:
  3793. Assert.Equal (0, _textView.CursorPosition.X);
  3794. Assert.Equal (0, _textView.CursorPosition.Y);
  3795. Assert.Equal (24, _textView.SelectionStartColumn);
  3796. Assert.Equal (1, _textView.SelectionStartRow);
  3797. Assert.Equal (47 + Environment.NewLine.Length, _textView.SelectedLength);
  3798. Assert.Equal (
  3799. $"This is the first line.{Environment.NewLine}This is the second line.",
  3800. _textView.SelectedText
  3801. );
  3802. break;
  3803. default:
  3804. iterationsFinished = true;
  3805. break;
  3806. }
  3807. iteration++;
  3808. }
  3809. }
  3810. [Fact]
  3811. [TextViewTestsSetupFakeApplication]
  3812. public void WordBackward_With_No_Selection ()
  3813. {
  3814. _textView.CursorPosition = new (_textView.Text.Length, 0);
  3815. var iteration = 0;
  3816. while (_textView.CursorPosition.X > 0)
  3817. {
  3818. _textView.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  3819. switch (iteration)
  3820. {
  3821. case 0:
  3822. Assert.Equal (31, _textView.CursorPosition.X);
  3823. Assert.Equal (0, _textView.CursorPosition.Y);
  3824. Assert.Equal (0, _textView.SelectionStartColumn);
  3825. Assert.Equal (0, _textView.SelectionStartRow);
  3826. Assert.Equal (0, _textView.SelectedLength);
  3827. Assert.Equal ("", _textView.SelectedText);
  3828. break;
  3829. case 1:
  3830. Assert.Equal (25, _textView.CursorPosition.X);
  3831. Assert.Equal (0, _textView.CursorPosition.Y);
  3832. Assert.Equal (0, _textView.SelectionStartColumn);
  3833. Assert.Equal (0, _textView.SelectionStartRow);
  3834. Assert.Equal (0, _textView.SelectedLength);
  3835. Assert.Equal ("", _textView.SelectedText);
  3836. break;
  3837. case 2:
  3838. Assert.Equal (20, _textView.CursorPosition.X);
  3839. Assert.Equal (0, _textView.CursorPosition.Y);
  3840. Assert.Equal (0, _textView.SelectionStartColumn);
  3841. Assert.Equal (0, _textView.SelectionStartRow);
  3842. Assert.Equal (0, _textView.SelectedLength);
  3843. Assert.Equal ("", _textView.SelectedText);
  3844. break;
  3845. case 3:
  3846. Assert.Equal (12, _textView.CursorPosition.X);
  3847. Assert.Equal (0, _textView.CursorPosition.Y);
  3848. Assert.Equal (0, _textView.SelectionStartColumn);
  3849. Assert.Equal (0, _textView.SelectionStartRow);
  3850. Assert.Equal (0, _textView.SelectedLength);
  3851. Assert.Equal ("", _textView.SelectedText);
  3852. break;
  3853. case 4:
  3854. Assert.Equal (7, _textView.CursorPosition.X);
  3855. Assert.Equal (0, _textView.CursorPosition.Y);
  3856. Assert.Equal (0, _textView.SelectionStartColumn);
  3857. Assert.Equal (0, _textView.SelectionStartRow);
  3858. Assert.Equal (0, _textView.SelectedLength);
  3859. Assert.Equal ("", _textView.SelectedText);
  3860. break;
  3861. case 5:
  3862. Assert.Equal (4, _textView.CursorPosition.X);
  3863. Assert.Equal (0, _textView.CursorPosition.Y);
  3864. Assert.Equal (0, _textView.SelectionStartColumn);
  3865. Assert.Equal (0, _textView.SelectionStartRow);
  3866. Assert.Equal (0, _textView.SelectedLength);
  3867. Assert.Equal ("", _textView.SelectedText);
  3868. break;
  3869. case 6:
  3870. Assert.Equal (0, _textView.CursorPosition.X);
  3871. Assert.Equal (0, _textView.CursorPosition.Y);
  3872. Assert.Equal (0, _textView.SelectionStartColumn);
  3873. Assert.Equal (0, _textView.SelectionStartRow);
  3874. Assert.Equal (0, _textView.SelectedLength);
  3875. Assert.Equal ("", _textView.SelectedText);
  3876. break;
  3877. }
  3878. iteration++;
  3879. }
  3880. }
  3881. [Fact]
  3882. [TextViewTestsSetupFakeApplication]
  3883. public void WordBackward_With_No_Selection_And_With_More_Than_Only_One_Whitespace_And_With_Only_One_Letter ()
  3884. {
  3885. // 1 2 3 4 5
  3886. // 0123456789012345678901234567890123456789012345678901234=55 (Length)
  3887. _textView.Text = "TAB t o jump b etween t ext f ields .";
  3888. _textView.CursorPosition = new (_textView.Text.Length, 0);
  3889. var iteration = 0;
  3890. while (_textView.CursorPosition.X > 0)
  3891. {
  3892. _textView.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  3893. switch (iteration)
  3894. {
  3895. case 0:
  3896. Assert.Equal (54, _textView.CursorPosition.X);
  3897. Assert.Equal (0, _textView.CursorPosition.Y);
  3898. Assert.Equal (0, _textView.SelectionStartColumn);
  3899. Assert.Equal (0, _textView.SelectionStartRow);
  3900. Assert.Equal (0, _textView.SelectedLength);
  3901. Assert.Equal ("", _textView.SelectedText);
  3902. break;
  3903. case 1:
  3904. Assert.Equal (48, _textView.CursorPosition.X);
  3905. Assert.Equal (0, _textView.CursorPosition.Y);
  3906. Assert.Equal (0, _textView.SelectionStartColumn);
  3907. Assert.Equal (0, _textView.SelectionStartRow);
  3908. Assert.Equal (0, _textView.SelectedLength);
  3909. Assert.Equal ("", _textView.SelectedText);
  3910. break;
  3911. case 2:
  3912. Assert.Equal (46, _textView.CursorPosition.X);
  3913. Assert.Equal (0, _textView.CursorPosition.Y);
  3914. Assert.Equal (0, _textView.SelectionStartColumn);
  3915. Assert.Equal (0, _textView.SelectionStartRow);
  3916. Assert.Equal (0, _textView.SelectedLength);
  3917. Assert.Equal ("", _textView.SelectedText);
  3918. break;
  3919. case 3:
  3920. Assert.Equal (40, _textView.CursorPosition.X);
  3921. Assert.Equal (0, _textView.CursorPosition.Y);
  3922. Assert.Equal (0, _textView.SelectionStartColumn);
  3923. Assert.Equal (0, _textView.SelectionStartRow);
  3924. Assert.Equal (0, _textView.SelectedLength);
  3925. Assert.Equal ("", _textView.SelectedText);
  3926. break;
  3927. case 4:
  3928. Assert.Equal (38, _textView.CursorPosition.X);
  3929. Assert.Equal (0, _textView.CursorPosition.Y);
  3930. Assert.Equal (0, _textView.SelectionStartColumn);
  3931. Assert.Equal (0, _textView.SelectionStartRow);
  3932. Assert.Equal (0, _textView.SelectedLength);
  3933. Assert.Equal ("", _textView.SelectedText);
  3934. break;
  3935. case 5:
  3936. Assert.Equal (28, _textView.CursorPosition.X);
  3937. Assert.Equal (0, _textView.CursorPosition.Y);
  3938. Assert.Equal (0, _textView.SelectionStartColumn);
  3939. Assert.Equal (0, _textView.SelectionStartRow);
  3940. Assert.Equal (0, _textView.SelectedLength);
  3941. Assert.Equal ("", _textView.SelectedText);
  3942. break;
  3943. case 6:
  3944. Assert.Equal (25, _textView.CursorPosition.X);
  3945. Assert.Equal (0, _textView.CursorPosition.Y);
  3946. Assert.Equal (0, _textView.SelectionStartColumn);
  3947. Assert.Equal (0, _textView.SelectionStartRow);
  3948. Assert.Equal (0, _textView.SelectedLength);
  3949. Assert.Equal ("", _textView.SelectedText);
  3950. break;
  3951. case 7:
  3952. Assert.Equal (12, _textView.CursorPosition.X);
  3953. Assert.Equal (0, _textView.CursorPosition.Y);
  3954. Assert.Equal (0, _textView.SelectionStartColumn);
  3955. Assert.Equal (0, _textView.SelectionStartRow);
  3956. Assert.Equal (0, _textView.SelectedLength);
  3957. Assert.Equal ("", _textView.SelectedText);
  3958. break;
  3959. case 8:
  3960. Assert.Equal (9, _textView.CursorPosition.X);
  3961. Assert.Equal (0, _textView.CursorPosition.Y);
  3962. Assert.Equal (0, _textView.SelectionStartColumn);
  3963. Assert.Equal (0, _textView.SelectionStartRow);
  3964. Assert.Equal (0, _textView.SelectedLength);
  3965. Assert.Equal ("", _textView.SelectedText);
  3966. break;
  3967. case 9:
  3968. Assert.Equal (6, _textView.CursorPosition.X);
  3969. Assert.Equal (0, _textView.CursorPosition.Y);
  3970. Assert.Equal (0, _textView.SelectionStartColumn);
  3971. Assert.Equal (0, _textView.SelectionStartRow);
  3972. Assert.Equal (0, _textView.SelectedLength);
  3973. Assert.Equal ("", _textView.SelectedText);
  3974. break;
  3975. case 10:
  3976. Assert.Equal (0, _textView.CursorPosition.X);
  3977. Assert.Equal (0, _textView.CursorPosition.Y);
  3978. Assert.Equal (0, _textView.SelectionStartColumn);
  3979. Assert.Equal (0, _textView.SelectionStartRow);
  3980. Assert.Equal (0, _textView.SelectedLength);
  3981. Assert.Equal ("", _textView.SelectedText);
  3982. break;
  3983. }
  3984. iteration++;
  3985. }
  3986. }
  3987. [Fact]
  3988. [TextViewTestsSetupFakeApplication]
  3989. public void WordBackward_With_Selection ()
  3990. {
  3991. _textView.CursorPosition = new (_textView.Text.Length, 0);
  3992. _textView.SelectionStartColumn = _textView.Text.Length;
  3993. _textView.SelectionStartRow = 0;
  3994. var iteration = 0;
  3995. while (_textView.CursorPosition.X > 0)
  3996. {
  3997. _textView.NewKeyDownEvent (Key.CursorLeft.WithCtrl.WithShift);
  3998. switch (iteration)
  3999. {
  4000. case 0:
  4001. Assert.Equal (31, _textView.CursorPosition.X);
  4002. Assert.Equal (0, _textView.CursorPosition.Y);
  4003. Assert.Equal (32, _textView.SelectionStartColumn);
  4004. Assert.Equal (0, _textView.SelectionStartRow);
  4005. Assert.Equal (1, _textView.SelectedLength);
  4006. Assert.Equal (".", _textView.SelectedText);
  4007. break;
  4008. case 1:
  4009. Assert.Equal (25, _textView.CursorPosition.X);
  4010. Assert.Equal (0, _textView.CursorPosition.Y);
  4011. Assert.Equal (32, _textView.SelectionStartColumn);
  4012. Assert.Equal (0, _textView.SelectionStartRow);
  4013. Assert.Equal (7, _textView.SelectedLength);
  4014. Assert.Equal ("fields.", _textView.SelectedText);
  4015. break;
  4016. case 2:
  4017. Assert.Equal (20, _textView.CursorPosition.X);
  4018. Assert.Equal (0, _textView.CursorPosition.Y);
  4019. Assert.Equal (32, _textView.SelectionStartColumn);
  4020. Assert.Equal (0, _textView.SelectionStartRow);
  4021. Assert.Equal (12, _textView.SelectedLength);
  4022. Assert.Equal ("text fields.", _textView.SelectedText);
  4023. break;
  4024. case 3:
  4025. Assert.Equal (12, _textView.CursorPosition.X);
  4026. Assert.Equal (0, _textView.CursorPosition.Y);
  4027. Assert.Equal (32, _textView.SelectionStartColumn);
  4028. Assert.Equal (0, _textView.SelectionStartRow);
  4029. Assert.Equal (20, _textView.SelectedLength);
  4030. Assert.Equal ("between text fields.", _textView.SelectedText);
  4031. break;
  4032. case 4:
  4033. Assert.Equal (7, _textView.CursorPosition.X);
  4034. Assert.Equal (0, _textView.CursorPosition.Y);
  4035. Assert.Equal (32, _textView.SelectionStartColumn);
  4036. Assert.Equal (0, _textView.SelectionStartRow);
  4037. Assert.Equal (25, _textView.SelectedLength);
  4038. Assert.Equal ("jump between text fields.", _textView.SelectedText);
  4039. break;
  4040. case 5:
  4041. Assert.Equal (4, _textView.CursorPosition.X);
  4042. Assert.Equal (0, _textView.CursorPosition.Y);
  4043. Assert.Equal (32, _textView.SelectionStartColumn);
  4044. Assert.Equal (0, _textView.SelectionStartRow);
  4045. Assert.Equal (28, _textView.SelectedLength);
  4046. Assert.Equal ("to jump between text fields.", _textView.SelectedText);
  4047. break;
  4048. case 6:
  4049. Assert.Equal (0, _textView.CursorPosition.X);
  4050. Assert.Equal (0, _textView.CursorPosition.Y);
  4051. Assert.Equal (32, _textView.SelectionStartColumn);
  4052. Assert.Equal (0, _textView.SelectionStartRow);
  4053. Assert.Equal (32, _textView.SelectedLength);
  4054. Assert.Equal ("TAB to jump between text fields.", _textView.SelectedText);
  4055. break;
  4056. }
  4057. iteration++;
  4058. }
  4059. }
  4060. [Fact]
  4061. [TextViewTestsSetupFakeApplication]
  4062. public void
  4063. WordBackward_With_The_Same_Values_For_SelectedStart_And_CursorPosition_And_Not_Starting_At_Beginning_Of_The_Text ()
  4064. {
  4065. _textView.CursorPosition = new (10, 0);
  4066. _textView.SelectionStartColumn = 10;
  4067. _textView.SelectionStartRow = 0;
  4068. var iteration = 0;
  4069. while (_textView.CursorPosition.X > 0)
  4070. {
  4071. _textView.NewKeyDownEvent (Key.CursorLeft.WithCtrl.WithShift);
  4072. switch (iteration)
  4073. {
  4074. case 0:
  4075. Assert.Equal (7, _textView.CursorPosition.X);
  4076. Assert.Equal (0, _textView.CursorPosition.Y);
  4077. Assert.Equal (10, _textView.SelectionStartColumn);
  4078. Assert.Equal (0, _textView.SelectionStartRow);
  4079. Assert.Equal (3, _textView.SelectedLength);
  4080. Assert.Equal ("jum", _textView.SelectedText);
  4081. break;
  4082. case 1:
  4083. Assert.Equal (4, _textView.CursorPosition.X);
  4084. Assert.Equal (0, _textView.CursorPosition.Y);
  4085. Assert.Equal (10, _textView.SelectionStartColumn);
  4086. Assert.Equal (0, _textView.SelectionStartRow);
  4087. Assert.Equal (6, _textView.SelectedLength);
  4088. Assert.Equal ("to jum", _textView.SelectedText);
  4089. break;
  4090. case 2:
  4091. Assert.Equal (0, _textView.CursorPosition.X);
  4092. Assert.Equal (0, _textView.CursorPosition.Y);
  4093. Assert.Equal (10, _textView.SelectionStartColumn);
  4094. Assert.Equal (0, _textView.SelectionStartRow);
  4095. Assert.Equal (10, _textView.SelectedLength);
  4096. Assert.Equal ("TAB to jum", _textView.SelectedText);
  4097. break;
  4098. }
  4099. iteration++;
  4100. }
  4101. }
  4102. [Fact]
  4103. [TextViewTestsSetupFakeApplication]
  4104. public void WordForward_Multiline_With_Selection ()
  4105. {
  4106. // 1 2 3 4
  4107. // 01234567890123456789012 34567890123456789012345678-Length
  4108. // 1 2 1 2
  4109. // 01234567890123456789012 0123456789012345678901234
  4110. _textView.Text = "This is the first line.\nThis is the second line.";
  4111. _textView.SelectionStartColumn = _textView.CurrentColumn;
  4112. _textView.SelectionStartRow = _textView.CurrentRow;
  4113. var iteration = 0;
  4114. var iterationsFinished = false;
  4115. while (!iterationsFinished)
  4116. {
  4117. _textView.NewKeyDownEvent (
  4118. Key.CursorRight.WithCtrl.WithShift
  4119. );
  4120. switch (iteration)
  4121. {
  4122. case 0:
  4123. Assert.Equal (5, _textView.CursorPosition.X);
  4124. Assert.Equal (0, _textView.CursorPosition.Y);
  4125. Assert.Equal (0, _textView.SelectionStartColumn);
  4126. Assert.Equal (0, _textView.SelectionStartRow);
  4127. Assert.Equal (5, _textView.SelectedLength);
  4128. Assert.Equal ("This ", _textView.SelectedText);
  4129. break;
  4130. case 1:
  4131. Assert.Equal (8, _textView.CursorPosition.X);
  4132. Assert.Equal (0, _textView.CursorPosition.Y);
  4133. Assert.Equal (0, _textView.SelectionStartColumn);
  4134. Assert.Equal (0, _textView.SelectionStartRow);
  4135. Assert.Equal (8, _textView.SelectedLength);
  4136. Assert.Equal ("This is ", _textView.SelectedText);
  4137. break;
  4138. case 2:
  4139. Assert.Equal (12, _textView.CursorPosition.X);
  4140. Assert.Equal (0, _textView.CursorPosition.Y);
  4141. Assert.Equal (0, _textView.SelectionStartColumn);
  4142. Assert.Equal (0, _textView.SelectionStartRow);
  4143. Assert.Equal (12, _textView.SelectedLength);
  4144. Assert.Equal ("This is the ", _textView.SelectedText);
  4145. break;
  4146. case 3:
  4147. Assert.Equal (18, _textView.CursorPosition.X);
  4148. Assert.Equal (0, _textView.CursorPosition.Y);
  4149. Assert.Equal (0, _textView.SelectionStartColumn);
  4150. Assert.Equal (0, _textView.SelectionStartRow);
  4151. Assert.Equal (18, _textView.SelectedLength);
  4152. Assert.Equal ("This is the first ", _textView.SelectedText);
  4153. break;
  4154. case 4:
  4155. Assert.Equal (22, _textView.CursorPosition.X);
  4156. Assert.Equal (0, _textView.CursorPosition.Y);
  4157. Assert.Equal (0, _textView.SelectionStartColumn);
  4158. Assert.Equal (0, _textView.SelectionStartRow);
  4159. Assert.Equal (22, _textView.SelectedLength);
  4160. Assert.Equal ("This is the first line", _textView.SelectedText);
  4161. break;
  4162. case 5:
  4163. Assert.Equal (23, _textView.CursorPosition.X);
  4164. Assert.Equal (0, _textView.CursorPosition.Y);
  4165. Assert.Equal (0, _textView.SelectionStartColumn);
  4166. Assert.Equal (0, _textView.SelectionStartRow);
  4167. Assert.Equal (23, _textView.SelectedLength);
  4168. Assert.Equal ("This is the first line.", _textView.SelectedText);
  4169. break;
  4170. case 6:
  4171. Assert.Equal (0, _textView.CursorPosition.X);
  4172. Assert.Equal (1, _textView.CursorPosition.Y);
  4173. Assert.Equal (0, _textView.SelectionStartColumn);
  4174. Assert.Equal (0, _textView.SelectionStartRow);
  4175. Assert.Equal (23 + Environment.NewLine.Length, _textView.SelectedLength);
  4176. Assert.Equal ($"This is the first line.{Environment.NewLine}", _textView.SelectedText);
  4177. break;
  4178. case 7:
  4179. Assert.Equal (5, _textView.CursorPosition.X);
  4180. Assert.Equal (1, _textView.CursorPosition.Y);
  4181. Assert.Equal (0, _textView.SelectionStartColumn);
  4182. Assert.Equal (0, _textView.SelectionStartRow);
  4183. Assert.Equal (28 + Environment.NewLine.Length, _textView.SelectedLength);
  4184. Assert.Equal (
  4185. $"This is the first line.{Environment.NewLine}This ",
  4186. _textView.SelectedText
  4187. );
  4188. break;
  4189. case 8:
  4190. Assert.Equal (8, _textView.CursorPosition.X);
  4191. Assert.Equal (1, _textView.CursorPosition.Y);
  4192. Assert.Equal (0, _textView.SelectionStartColumn);
  4193. Assert.Equal (0, _textView.SelectionStartRow);
  4194. Assert.Equal (31 + Environment.NewLine.Length, _textView.SelectedLength);
  4195. Assert.Equal (
  4196. $"This is the first line.{Environment.NewLine}This is ",
  4197. _textView.SelectedText
  4198. );
  4199. break;
  4200. case 9:
  4201. Assert.Equal (12, _textView.CursorPosition.X);
  4202. Assert.Equal (1, _textView.CursorPosition.Y);
  4203. Assert.Equal (0, _textView.SelectionStartColumn);
  4204. Assert.Equal (0, _textView.SelectionStartRow);
  4205. Assert.Equal (35 + Environment.NewLine.Length, _textView.SelectedLength);
  4206. Assert.Equal (
  4207. $"This is the first line.{Environment.NewLine}This is the ",
  4208. _textView.SelectedText
  4209. );
  4210. break;
  4211. case 10:
  4212. Assert.Equal (19, _textView.CursorPosition.X);
  4213. Assert.Equal (1, _textView.CursorPosition.Y);
  4214. Assert.Equal (0, _textView.SelectionStartColumn);
  4215. Assert.Equal (0, _textView.SelectionStartRow);
  4216. Assert.Equal (42 + Environment.NewLine.Length, _textView.SelectedLength);
  4217. Assert.Equal (
  4218. $"This is the first line.{Environment.NewLine}This is the second ",
  4219. _textView.SelectedText
  4220. );
  4221. break;
  4222. case 11:
  4223. Assert.Equal (23, _textView.CursorPosition.X);
  4224. Assert.Equal (1, _textView.CursorPosition.Y);
  4225. Assert.Equal (0, _textView.SelectionStartColumn);
  4226. Assert.Equal (0, _textView.SelectionStartRow);
  4227. Assert.Equal (46 + Environment.NewLine.Length, _textView.SelectedLength);
  4228. Assert.Equal (
  4229. $"This is the first line.{Environment.NewLine}This is the second line",
  4230. _textView.SelectedText
  4231. );
  4232. break;
  4233. case 12:
  4234. Assert.Equal (24, _textView.CursorPosition.X);
  4235. Assert.Equal (1, _textView.CursorPosition.Y);
  4236. Assert.Equal (0, _textView.SelectionStartColumn);
  4237. Assert.Equal (0, _textView.SelectionStartRow);
  4238. Assert.Equal (47 + Environment.NewLine.Length, _textView.SelectedLength);
  4239. Assert.Equal (
  4240. $"This is the first line.{Environment.NewLine}This is the second line.",
  4241. _textView.SelectedText
  4242. );
  4243. break;
  4244. default:
  4245. iterationsFinished = true;
  4246. break;
  4247. }
  4248. iteration++;
  4249. }
  4250. }
  4251. [Fact]
  4252. [TextViewTestsSetupFakeApplication]
  4253. public void WordForward_With_No_Selection ()
  4254. {
  4255. _textView.CursorPosition = Point.Empty;
  4256. var iteration = 0;
  4257. while (_textView.CursorPosition.X < _textView.Text.Length)
  4258. {
  4259. _textView.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  4260. switch (iteration)
  4261. {
  4262. case 0:
  4263. Assert.Equal (4, _textView.CursorPosition.X);
  4264. Assert.Equal (0, _textView.CursorPosition.Y);
  4265. Assert.Equal (0, _textView.SelectionStartColumn);
  4266. Assert.Equal (0, _textView.SelectionStartRow);
  4267. Assert.Equal (0, _textView.SelectedLength);
  4268. Assert.Equal ("", _textView.SelectedText);
  4269. break;
  4270. case 1:
  4271. Assert.Equal (7, _textView.CursorPosition.X);
  4272. Assert.Equal (0, _textView.CursorPosition.Y);
  4273. Assert.Equal (0, _textView.SelectionStartColumn);
  4274. Assert.Equal (0, _textView.SelectionStartRow);
  4275. Assert.Equal (0, _textView.SelectedLength);
  4276. Assert.Equal ("", _textView.SelectedText);
  4277. break;
  4278. case 2:
  4279. Assert.Equal (12, _textView.CursorPosition.X);
  4280. Assert.Equal (0, _textView.CursorPosition.Y);
  4281. Assert.Equal (0, _textView.SelectionStartColumn);
  4282. Assert.Equal (0, _textView.SelectionStartRow);
  4283. Assert.Equal (0, _textView.SelectedLength);
  4284. Assert.Equal ("", _textView.SelectedText);
  4285. break;
  4286. case 3:
  4287. Assert.Equal (20, _textView.CursorPosition.X);
  4288. Assert.Equal (0, _textView.CursorPosition.Y);
  4289. Assert.Equal (0, _textView.SelectionStartColumn);
  4290. Assert.Equal (0, _textView.SelectionStartRow);
  4291. Assert.Equal (0, _textView.SelectedLength);
  4292. Assert.Equal ("", _textView.SelectedText);
  4293. break;
  4294. case 4:
  4295. Assert.Equal (25, _textView.CursorPosition.X);
  4296. Assert.Equal (0, _textView.CursorPosition.Y);
  4297. Assert.Equal (0, _textView.SelectionStartColumn);
  4298. Assert.Equal (0, _textView.SelectionStartRow);
  4299. Assert.Equal (0, _textView.SelectedLength);
  4300. Assert.Equal ("", _textView.SelectedText);
  4301. break;
  4302. case 5:
  4303. Assert.Equal (31, _textView.CursorPosition.X);
  4304. Assert.Equal (0, _textView.CursorPosition.Y);
  4305. Assert.Equal (0, _textView.SelectionStartColumn);
  4306. Assert.Equal (0, _textView.SelectionStartRow);
  4307. Assert.Equal (0, _textView.SelectedLength);
  4308. Assert.Equal ("", _textView.SelectedText);
  4309. break;
  4310. case 6:
  4311. Assert.Equal (32, _textView.CursorPosition.X);
  4312. Assert.Equal (0, _textView.CursorPosition.Y);
  4313. Assert.Equal (0, _textView.SelectionStartColumn);
  4314. Assert.Equal (0, _textView.SelectionStartRow);
  4315. Assert.Equal (0, _textView.SelectedLength);
  4316. Assert.Equal ("", _textView.SelectedText);
  4317. break;
  4318. }
  4319. iteration++;
  4320. }
  4321. }
  4322. [Fact]
  4323. [TextViewTestsSetupFakeApplication]
  4324. public void WordForward_With_No_Selection_And_With_More_Than_Only_One_Whitespace_And_With_Only_One_Letter ()
  4325. {
  4326. // 1 2 3 4 5
  4327. // 0123456789012345678901234567890123456789012345678901234=55 (Length)
  4328. _textView.Text = "TAB t o jump b etween t ext f ields .";
  4329. _textView.CursorPosition = Point.Empty;
  4330. var iteration = 0;
  4331. while (_textView.CursorPosition.X < _textView.Text.Length)
  4332. {
  4333. _textView.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  4334. switch (iteration)
  4335. {
  4336. case 0:
  4337. Assert.Equal (6, _textView.CursorPosition.X);
  4338. Assert.Equal (0, _textView.CursorPosition.Y);
  4339. Assert.Equal (0, _textView.SelectionStartColumn);
  4340. Assert.Equal (0, _textView.SelectionStartRow);
  4341. Assert.Equal (0, _textView.SelectedLength);
  4342. Assert.Equal ("", _textView.SelectedText);
  4343. break;
  4344. case 1:
  4345. Assert.Equal (9, _textView.CursorPosition.X);
  4346. Assert.Equal (0, _textView.CursorPosition.Y);
  4347. Assert.Equal (0, _textView.SelectionStartColumn);
  4348. Assert.Equal (0, _textView.SelectionStartRow);
  4349. Assert.Equal (0, _textView.SelectedLength);
  4350. Assert.Equal ("", _textView.SelectedText);
  4351. break;
  4352. case 2:
  4353. Assert.Equal (12, _textView.CursorPosition.X);
  4354. Assert.Equal (0, _textView.CursorPosition.Y);
  4355. Assert.Equal (0, _textView.SelectionStartColumn);
  4356. Assert.Equal (0, _textView.SelectionStartRow);
  4357. Assert.Equal (0, _textView.SelectedLength);
  4358. Assert.Equal ("", _textView.SelectedText);
  4359. break;
  4360. case 3:
  4361. Assert.Equal (25, _textView.CursorPosition.X);
  4362. Assert.Equal (0, _textView.CursorPosition.Y);
  4363. Assert.Equal (0, _textView.SelectionStartColumn);
  4364. Assert.Equal (0, _textView.SelectionStartRow);
  4365. Assert.Equal (0, _textView.SelectedLength);
  4366. Assert.Equal ("", _textView.SelectedText);
  4367. break;
  4368. case 4:
  4369. Assert.Equal (28, _textView.CursorPosition.X);
  4370. Assert.Equal (0, _textView.CursorPosition.Y);
  4371. Assert.Equal (0, _textView.SelectionStartColumn);
  4372. Assert.Equal (0, _textView.SelectionStartRow);
  4373. Assert.Equal (0, _textView.SelectedLength);
  4374. Assert.Equal ("", _textView.SelectedText);
  4375. break;
  4376. case 5:
  4377. Assert.Equal (38, _textView.CursorPosition.X);
  4378. Assert.Equal (0, _textView.CursorPosition.Y);
  4379. Assert.Equal (0, _textView.SelectionStartColumn);
  4380. Assert.Equal (0, _textView.SelectionStartRow);
  4381. Assert.Equal (0, _textView.SelectedLength);
  4382. Assert.Equal ("", _textView.SelectedText);
  4383. break;
  4384. case 6:
  4385. Assert.Equal (40, _textView.CursorPosition.X);
  4386. Assert.Equal (0, _textView.CursorPosition.Y);
  4387. Assert.Equal (0, _textView.SelectionStartColumn);
  4388. Assert.Equal (0, _textView.SelectionStartRow);
  4389. Assert.Equal (0, _textView.SelectedLength);
  4390. Assert.Equal ("", _textView.SelectedText);
  4391. break;
  4392. case 7:
  4393. Assert.Equal (46, _textView.CursorPosition.X);
  4394. Assert.Equal (0, _textView.CursorPosition.Y);
  4395. Assert.Equal (0, _textView.SelectionStartColumn);
  4396. Assert.Equal (0, _textView.SelectionStartRow);
  4397. Assert.Equal (0, _textView.SelectedLength);
  4398. Assert.Equal ("", _textView.SelectedText);
  4399. break;
  4400. case 8:
  4401. Assert.Equal (48, _textView.CursorPosition.X);
  4402. Assert.Equal (0, _textView.CursorPosition.Y);
  4403. Assert.Equal (0, _textView.SelectionStartColumn);
  4404. Assert.Equal (0, _textView.SelectionStartRow);
  4405. Assert.Equal (0, _textView.SelectedLength);
  4406. Assert.Equal ("", _textView.SelectedText);
  4407. break;
  4408. case 9:
  4409. Assert.Equal (54, _textView.CursorPosition.X);
  4410. Assert.Equal (0, _textView.CursorPosition.Y);
  4411. Assert.Equal (0, _textView.SelectionStartColumn);
  4412. Assert.Equal (0, _textView.SelectionStartRow);
  4413. Assert.Equal (0, _textView.SelectedLength);
  4414. Assert.Equal ("", _textView.SelectedText);
  4415. break;
  4416. case 10:
  4417. Assert.Equal (55, _textView.CursorPosition.X);
  4418. Assert.Equal (0, _textView.CursorPosition.Y);
  4419. Assert.Equal (0, _textView.SelectionStartColumn);
  4420. Assert.Equal (0, _textView.SelectionStartRow);
  4421. Assert.Equal (0, _textView.SelectedLength);
  4422. Assert.Equal ("", _textView.SelectedText);
  4423. break;
  4424. }
  4425. iteration++;
  4426. }
  4427. }
  4428. [Fact]
  4429. [TextViewTestsSetupFakeApplication]
  4430. public void WordForward_With_Selection ()
  4431. {
  4432. _textView.CursorPosition = Point.Empty;
  4433. _textView.SelectionStartColumn = 0;
  4434. _textView.SelectionStartRow = 0;
  4435. var iteration = 0;
  4436. while (_textView.CursorPosition.X < _textView.Text.Length)
  4437. {
  4438. _textView.NewKeyDownEvent (
  4439. Key.CursorRight.WithCtrl.WithShift
  4440. );
  4441. switch (iteration)
  4442. {
  4443. case 0:
  4444. Assert.Equal (4, _textView.CursorPosition.X);
  4445. Assert.Equal (0, _textView.CursorPosition.Y);
  4446. Assert.Equal (0, _textView.SelectionStartColumn);
  4447. Assert.Equal (0, _textView.SelectionStartRow);
  4448. Assert.Equal (4, _textView.SelectedLength);
  4449. Assert.Equal ("TAB ", _textView.SelectedText);
  4450. break;
  4451. case 1:
  4452. Assert.Equal (7, _textView.CursorPosition.X);
  4453. Assert.Equal (0, _textView.CursorPosition.Y);
  4454. Assert.Equal (0, _textView.SelectionStartColumn);
  4455. Assert.Equal (0, _textView.SelectionStartRow);
  4456. Assert.Equal (7, _textView.SelectedLength);
  4457. Assert.Equal ("TAB to ", _textView.SelectedText);
  4458. break;
  4459. case 2:
  4460. Assert.Equal (12, _textView.CursorPosition.X);
  4461. Assert.Equal (0, _textView.CursorPosition.Y);
  4462. Assert.Equal (0, _textView.SelectionStartColumn);
  4463. Assert.Equal (0, _textView.SelectionStartRow);
  4464. Assert.Equal (12, _textView.SelectedLength);
  4465. Assert.Equal ("TAB to jump ", _textView.SelectedText);
  4466. break;
  4467. case 3:
  4468. Assert.Equal (20, _textView.CursorPosition.X);
  4469. Assert.Equal (0, _textView.CursorPosition.Y);
  4470. Assert.Equal (0, _textView.SelectionStartColumn);
  4471. Assert.Equal (0, _textView.SelectionStartRow);
  4472. Assert.Equal (20, _textView.SelectedLength);
  4473. Assert.Equal ("TAB to jump between ", _textView.SelectedText);
  4474. break;
  4475. case 4:
  4476. Assert.Equal (25, _textView.CursorPosition.X);
  4477. Assert.Equal (0, _textView.CursorPosition.Y);
  4478. Assert.Equal (0, _textView.SelectionStartColumn);
  4479. Assert.Equal (0, _textView.SelectionStartRow);
  4480. Assert.Equal (25, _textView.SelectedLength);
  4481. Assert.Equal ("TAB to jump between text ", _textView.SelectedText);
  4482. break;
  4483. case 5:
  4484. Assert.Equal (31, _textView.CursorPosition.X);
  4485. Assert.Equal (0, _textView.CursorPosition.Y);
  4486. Assert.Equal (0, _textView.SelectionStartColumn);
  4487. Assert.Equal (0, _textView.SelectionStartRow);
  4488. Assert.Equal (31, _textView.SelectedLength);
  4489. Assert.Equal ("TAB to jump between text fields", _textView.SelectedText);
  4490. break;
  4491. case 6:
  4492. Assert.Equal (32, _textView.CursorPosition.X);
  4493. Assert.Equal (0, _textView.CursorPosition.Y);
  4494. Assert.Equal (0, _textView.SelectionStartColumn);
  4495. Assert.Equal (0, _textView.SelectionStartRow);
  4496. Assert.Equal (32, _textView.SelectedLength);
  4497. Assert.Equal ("TAB to jump between text fields.", _textView.SelectedText);
  4498. break;
  4499. }
  4500. iteration++;
  4501. }
  4502. }
  4503. [Fact]
  4504. [TextViewTestsSetupFakeApplication]
  4505. public void
  4506. WordForward_With_The_Same_Values_For_SelectedStart_And_CursorPosition_And_Not_Starting_At_Beginning_Of_The_Text ()
  4507. {
  4508. _textView.CursorPosition = new (10, 0);
  4509. _textView.SelectionStartColumn = 10;
  4510. _textView.SelectionStartRow = 0;
  4511. var iteration = 0;
  4512. while (_textView.CursorPosition.X < _textView.Text.Length)
  4513. {
  4514. _textView.NewKeyDownEvent (
  4515. Key.CursorRight.WithCtrl.WithShift
  4516. );
  4517. switch (iteration)
  4518. {
  4519. case 0:
  4520. Assert.Equal (12, _textView.CursorPosition.X);
  4521. Assert.Equal (0, _textView.CursorPosition.Y);
  4522. Assert.Equal (10, _textView.SelectionStartColumn);
  4523. Assert.Equal (0, _textView.SelectionStartRow);
  4524. Assert.Equal (2, _textView.SelectedLength);
  4525. Assert.Equal ("p ", _textView.SelectedText);
  4526. break;
  4527. case 1:
  4528. Assert.Equal (20, _textView.CursorPosition.X);
  4529. Assert.Equal (0, _textView.CursorPosition.Y);
  4530. Assert.Equal (10, _textView.SelectionStartColumn);
  4531. Assert.Equal (0, _textView.SelectionStartRow);
  4532. Assert.Equal (10, _textView.SelectedLength);
  4533. Assert.Equal ("p between ", _textView.SelectedText);
  4534. break;
  4535. case 2:
  4536. Assert.Equal (25, _textView.CursorPosition.X);
  4537. Assert.Equal (0, _textView.CursorPosition.Y);
  4538. Assert.Equal (10, _textView.SelectionStartColumn);
  4539. Assert.Equal (0, _textView.SelectionStartRow);
  4540. Assert.Equal (15, _textView.SelectedLength);
  4541. Assert.Equal ("p between text ", _textView.SelectedText);
  4542. break;
  4543. case 3:
  4544. Assert.Equal (31, _textView.CursorPosition.X);
  4545. Assert.Equal (0, _textView.CursorPosition.Y);
  4546. Assert.Equal (10, _textView.SelectionStartColumn);
  4547. Assert.Equal (0, _textView.SelectionStartRow);
  4548. Assert.Equal (21, _textView.SelectedLength);
  4549. Assert.Equal ("p between text fields", _textView.SelectedText);
  4550. break;
  4551. case 4:
  4552. Assert.Equal (32, _textView.CursorPosition.X);
  4553. Assert.Equal (0, _textView.CursorPosition.Y);
  4554. Assert.Equal (10, _textView.SelectionStartColumn);
  4555. Assert.Equal (0, _textView.SelectionStartRow);
  4556. Assert.Equal (22, _textView.SelectedLength);
  4557. Assert.Equal ("p between text fields.", _textView.SelectedText);
  4558. break;
  4559. }
  4560. iteration++;
  4561. }
  4562. }
  4563. [Fact]
  4564. [SetupFakeApplication]
  4565. public void WordWrap_Deleting_Backwards ()
  4566. {
  4567. var tv = new TextView { Width = 5, Height = 2, WordWrap = true, Text = "aaaa" };
  4568. var top = new Toplevel ();
  4569. top.Add (tv);
  4570. Application.Begin (top);
  4571. SetupFakeApplicationAttribute.RunIteration ();
  4572. Assert.Equal (Point.Empty, tv.CursorPosition);
  4573. Assert.Equal (0, tv.LeftColumn);
  4574. DriverAssert.AssertDriverContentsAre (
  4575. @"
  4576. aaaa
  4577. ",
  4578. _output
  4579. );
  4580. tv.CursorPosition = new (5, 0);
  4581. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  4582. SetupFakeApplicationAttribute.RunIteration ();
  4583. Assert.Equal (0, tv.LeftColumn);
  4584. DriverAssert.AssertDriverContentsAre (
  4585. @"
  4586. aaa
  4587. ",
  4588. _output
  4589. );
  4590. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  4591. SetupFakeApplicationAttribute.RunIteration ();
  4592. Assert.Equal (0, tv.LeftColumn);
  4593. DriverAssert.AssertDriverContentsAre (
  4594. @"
  4595. aa
  4596. ",
  4597. _output
  4598. );
  4599. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  4600. SetupFakeApplicationAttribute.RunIteration ();
  4601. Assert.Equal (0, tv.LeftColumn);
  4602. DriverAssert.AssertDriverContentsAre (
  4603. @"
  4604. a
  4605. ",
  4606. _output
  4607. );
  4608. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  4609. SetupFakeApplicationAttribute.RunIteration ();
  4610. Assert.Equal (0, tv.LeftColumn);
  4611. DriverAssert.AssertDriverContentsAre (
  4612. @"
  4613. ",
  4614. _output
  4615. );
  4616. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  4617. SetupFakeApplicationAttribute.RunIteration ();
  4618. Assert.Equal (0, tv.LeftColumn);
  4619. DriverAssert.AssertDriverContentsAre (
  4620. @"
  4621. ",
  4622. _output
  4623. );
  4624. top.Dispose ();
  4625. }
  4626. [Theory]
  4627. [TextViewTestsSetupFakeApplication]
  4628. [InlineData (KeyCode.Delete)]
  4629. public void WordWrap_Draw_Typed_Keys_After_Text_Is_Deleted (KeyCode del)
  4630. {
  4631. var top = new Toplevel ();
  4632. top.Add (_textView);
  4633. _textView.Text = "Line 1.\nLine 2.";
  4634. _textView.WordWrap = true;
  4635. Application.Begin (top);
  4636. SetupFakeApplicationAttribute.RunIteration ();
  4637. Assert.True (_textView.WordWrap);
  4638. DriverAssert.AssertDriverContentsWithFrameAre (
  4639. @"
  4640. Line 1.
  4641. Line 2.",
  4642. _output
  4643. );
  4644. Assert.True (_textView.NewKeyDownEvent (Key.End.WithShift));
  4645. Assert.Equal ("Line 1.", _textView.SelectedText);
  4646. Assert.True (_textView.NewKeyDownEvent (new (del)));
  4647. SetupFakeApplicationAttribute.RunIteration ();
  4648. DriverAssert.AssertDriverContentsWithFrameAre ("Line 2.", _output);
  4649. Assert.True (_textView.NewKeyDownEvent (Key.H.WithShift));
  4650. Assert.NotEqual (Rectangle.Empty, _textView.NeedsDrawRect);
  4651. SetupFakeApplicationAttribute.RunIteration ();
  4652. DriverAssert.AssertDriverContentsWithFrameAre (
  4653. @"
  4654. H
  4655. Line 2.",
  4656. _output
  4657. );
  4658. top.Dispose ();
  4659. }
  4660. [Fact]
  4661. [SetupFakeApplication]
  4662. public void WordWrap_Not_Throw_If_Width_Is_Less_Than_Zero ()
  4663. {
  4664. Exception exception = Record.Exception (
  4665. () =>
  4666. {
  4667. var tv = new TextView
  4668. {
  4669. Width = Dim.Fill (),
  4670. Height = Dim.Fill (),
  4671. WordWrap = true,
  4672. Text = "これは、左右のクリップ境界をテストするための非常に長いテキストです。"
  4673. };
  4674. }
  4675. );
  4676. Assert.Null (exception);
  4677. }
  4678. [Fact]
  4679. [SetupFakeApplication]
  4680. public void WordWrap_ReadOnly_CursorPosition_SelectedText_Copy ()
  4681. {
  4682. // 0123456789
  4683. var text = "This is the first line.\nThis is the second line.\n";
  4684. var tv = new TextView { Width = 11, Height = 9 };
  4685. tv.Text = text;
  4686. Assert.Equal (
  4687. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}",
  4688. tv.Text
  4689. );
  4690. tv.WordWrap = true;
  4691. var top = new Toplevel ()
  4692. {
  4693. Driver = ApplicationImpl.Instance.Driver,
  4694. };
  4695. top.Add (tv);
  4696. top.Layout ();
  4697. tv.Draw ();
  4698. DriverAssert.AssertDriverContentsWithFrameAre (
  4699. @"
  4700. This is
  4701. the first
  4702. line.
  4703. This is
  4704. the
  4705. second
  4706. line.
  4707. ",
  4708. _output
  4709. );
  4710. tv.ReadOnly = true;
  4711. tv.CursorPosition = new (6, 2);
  4712. Assert.Equal (new (5, 2), tv.CursorPosition);
  4713. top.LayoutSubViews ();
  4714. top.SetClipToScreen ();
  4715. tv.Draw ();
  4716. DriverAssert.AssertDriverContentsWithFrameAre (
  4717. @"
  4718. This is
  4719. the first
  4720. line.
  4721. This is
  4722. the second
  4723. line.
  4724. ",
  4725. _output
  4726. );
  4727. tv.SelectionStartRow = 0;
  4728. tv.SelectionStartColumn = 0;
  4729. Assert.Equal ("This is the first line.", tv.SelectedText);
  4730. tv.Copy ();
  4731. Assert.Equal ("This is the first line.", Clipboard.Contents);
  4732. }
  4733. [Fact]
  4734. [TextViewTestsSetupFakeApplication]
  4735. public void WordWrap_True_LoadStream_New_Text ()
  4736. {
  4737. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  4738. _textView.WordWrap = true;
  4739. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  4740. var text = "This is the first line.\nThis is the second line.\n";
  4741. using (var stream = new MemoryStream ())
  4742. {
  4743. var writer = new StreamWriter (stream);
  4744. writer.Write (text);
  4745. writer.Flush ();
  4746. stream.Position = 0;
  4747. _textView.Load (stream);
  4748. Assert.Equal (
  4749. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}",
  4750. _textView.Text
  4751. );
  4752. Assert.True (_textView.WordWrap);
  4753. }
  4754. }
  4755. [Fact]
  4756. [TextViewTestsSetupFakeApplication]
  4757. public void WordWrap_WrapModel_Output ()
  4758. {
  4759. // 0123456789
  4760. var text = "This is the first line.\nThis is the second line.\n";
  4761. var tv = new TextView { Width = 10, Height = 10 };
  4762. tv.Text = text;
  4763. Assert.Equal (
  4764. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}",
  4765. tv.Text
  4766. );
  4767. tv.WordWrap = true;
  4768. var top = new Toplevel ()
  4769. {
  4770. Driver = ApplicationImpl.Instance.Driver,
  4771. };
  4772. top.Add (tv);
  4773. top.Layout ();
  4774. tv.Draw ();
  4775. DriverAssert.AssertDriverContentsWithFrameAre (
  4776. @"
  4777. This is
  4778. the
  4779. first
  4780. line.
  4781. This is
  4782. the
  4783. second
  4784. line.
  4785. ",
  4786. _output
  4787. );
  4788. }
  4789. private int GetLeftCol (int start)
  4790. {
  4791. string [] lines = _textView.Text.Split (Environment.NewLine);
  4792. if (lines is { Length: 0 })
  4793. {
  4794. return 0;
  4795. }
  4796. if (start == _textView.LeftColumn)
  4797. {
  4798. return start;
  4799. }
  4800. if (_textView.LeftColumn == _textView.CurrentColumn)
  4801. {
  4802. return _textView.CurrentColumn;
  4803. }
  4804. int cCol = _textView.CurrentColumn;
  4805. string line = lines [_textView.CurrentRow];
  4806. int lCount = cCol > line.Length - 1 ? line.Length - 1 : cCol;
  4807. int width = _textView.Frame.Width;
  4808. int tabWidth = _textView.TabWidth;
  4809. var sumLength = 0;
  4810. var col = 0;
  4811. for (int i = lCount; i >= 0; i--)
  4812. {
  4813. char r = line [i];
  4814. sumLength += ((Rune)r).GetColumns ();
  4815. if (r == '\t')
  4816. {
  4817. sumLength += tabWidth + 1;
  4818. }
  4819. if (sumLength > width)
  4820. {
  4821. if (col + width == cCol)
  4822. {
  4823. col++;
  4824. }
  4825. break;
  4826. }
  4827. if ((cCol < line.Length && col > 0 && start < cCol && col == start) || cCol - col == width - 1)
  4828. {
  4829. break;
  4830. }
  4831. col = i;
  4832. }
  4833. return col;
  4834. }
  4835. // This class enables test functions annotated with the [InitShutdown] attribute
  4836. // to have a function called before the test function is called and after.
  4837. //
  4838. // This is necessary because a) Application is a singleton and Init/Shutdown must be called
  4839. // as a pair, and b) all unit test functions should be atomic.
  4840. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]
  4841. public class TextViewTestsSetupFakeApplication : SetupFakeApplicationAttribute
  4842. {
  4843. public static string Txt = "TAB to jump between text fields.";
  4844. public TextViewTestsSetupFakeApplication () : base () { }
  4845. public override void After (MethodInfo methodUnderTest)
  4846. {
  4847. _textView = null;
  4848. base.After (methodUnderTest);
  4849. }
  4850. public override void Before (MethodInfo methodUnderTest)
  4851. {
  4852. base.Before (methodUnderTest);
  4853. // 1 2 3
  4854. // 01234567890123456789012345678901=32 (Length)
  4855. byte [] buff = Encoding.Unicode.GetBytes (Txt);
  4856. byte [] ms = new MemoryStream (buff).ToArray ();
  4857. _textView = new () { Width = 30, Height = 10, SchemeName = "Base" };
  4858. _textView.Text = Encoding.Unicode.GetString (ms);
  4859. }
  4860. }
  4861. [Fact]
  4862. [SetupFakeApplication]
  4863. public void Draw_Esc_Rune ()
  4864. {
  4865. var tv = new TextView
  4866. {
  4867. Driver = ApplicationImpl.Instance.Driver,
  4868. Width = 5, Height = 1, Text = "\u001b"
  4869. };
  4870. tv.BeginInit ();
  4871. tv.EndInit ();
  4872. tv.Draw ();
  4873. DriverAssert.AssertDriverContentsWithFrameAre ("\u241b", _output);
  4874. tv.Dispose ();
  4875. }
  4876. [Fact (Skip = "v2 fake driver broke. TextView still works; disabling tests.")]
  4877. [SetupFakeApplication]
  4878. public void CellEventArgs_WordWrap_True ()
  4879. {
  4880. var eventCount = 0;
  4881. List<List<Cell>> text =
  4882. [
  4883. Cell.ToCells (
  4884. "This is the first line.".ToStringList ()
  4885. ),
  4886. Cell.ToCells (
  4887. "This is the second line.".ToStringList ()
  4888. )
  4889. ];
  4890. TextView tv = CreateTextView ();
  4891. tv.DrawNormalColor += TextView_DrawColor;
  4892. tv.DrawReadOnlyColor += TextView_DrawColor;
  4893. tv.DrawSelectionColor += TextView_DrawColor;
  4894. tv.DrawUsedColor += TextView_DrawColor;
  4895. void TextView_DrawColor (object sender, CellEventArgs e)
  4896. {
  4897. Assert.Equal (e.Line [e.Col], text [e.UnwrappedPosition.Row] [e.UnwrappedPosition.Col]);
  4898. eventCount++;
  4899. }
  4900. tv.Text = $"{Cell.ToString (text [0])}\n{Cell.ToString (text [1])}\n";
  4901. Assert.False (tv.WordWrap);
  4902. var top = new Toplevel ();
  4903. top.Add (tv);
  4904. Application.Begin (top);
  4905. SetupFakeApplicationAttribute.RunIteration ();
  4906. DriverAssert.AssertDriverContentsWithFrameAre (
  4907. @"
  4908. This is the first line.
  4909. This is the second line.",
  4910. _output
  4911. );
  4912. tv.Width = 10;
  4913. tv.Height = 25;
  4914. tv.WordWrap = true;
  4915. SetupFakeApplicationAttribute.RunIteration ();
  4916. DriverAssert.AssertDriverContentsWithFrameAre (
  4917. @"
  4918. This is
  4919. the
  4920. first
  4921. line.
  4922. This is
  4923. the
  4924. second
  4925. line. ",
  4926. _output
  4927. );
  4928. Assert.Equal (eventCount, (text [0].Count + text [1].Count) * 2);
  4929. top.Dispose ();
  4930. }
  4931. // BUGBUG: This test depends on the order of the schemes in SchemeManager.Schemes.
  4932. // BUGBUG: Breaks on Mac/Linux?
  4933. [Fact (Skip = "This test depends on the order of the schemes in SchemeManager.Schemes.")]
  4934. [SetupFakeApplication]
  4935. public void Cell_LoadCells_InheritsPreviousAttribute ()
  4936. {
  4937. List<Cell> cells = [];
  4938. foreach (KeyValuePair<string, Scheme> color in SchemeManager.GetSchemes ())
  4939. {
  4940. string csName = color.Key;
  4941. cells.AddRange (Cell.ToCellList (csName, color.Value.Normal));
  4942. cells.Add (new () { Grapheme = "\n", Attribute = color.Value.Focus });
  4943. }
  4944. TextView tv = CreateTextView ();
  4945. tv.Load (cells);
  4946. var top = new Toplevel ();
  4947. top.Add (tv);
  4948. SessionToken rs = Application.Begin (top);
  4949. SetupFakeApplicationAttribute.RunIteration ();
  4950. Assert.True (tv.InheritsPreviousAttribute);
  4951. var expectedText = @"
  4952. TopLevel
  4953. Base
  4954. Dialog
  4955. Menu
  4956. Error ";
  4957. DriverAssert.AssertDriverContentsWithFrameAre (expectedText, _output);
  4958. Attribute [] attributes =
  4959. {
  4960. // 0
  4961. SchemeManager.GetSchemes () ["TopLevel"].Normal,
  4962. // 1
  4963. SchemeManager.GetSchemes () ["Base"].Normal,
  4964. // 2
  4965. SchemeManager.GetSchemes () ["Dialog"].Normal,
  4966. // 3
  4967. SchemeManager.GetSchemes () ["Menu"].Normal,
  4968. // 4
  4969. SchemeManager.GetSchemes () ["Error"].Normal,
  4970. // 5
  4971. tv.GetScheme ()!.Focus
  4972. };
  4973. var expectedColor = @"
  4974. 0000000055
  4975. 1111555555
  4976. 2222225555
  4977. 3333555555
  4978. 4444455555";
  4979. DriverAssert.AssertDriverAttributesAre (expectedColor, _output, Application.Driver, attributes);
  4980. tv.WordWrap = true;
  4981. SetupFakeApplicationAttribute.RunIteration ();
  4982. DriverAssert.AssertDriverContentsWithFrameAre (expectedText, _output);
  4983. DriverAssert.AssertDriverAttributesAre (expectedColor, _output, Application.Driver, attributes);
  4984. tv.CursorPosition = new (6, 2);
  4985. tv.SelectionStartColumn = 0;
  4986. tv.SelectionStartRow = 0;
  4987. Assert.Equal ($"TopLevel{Environment.NewLine}Base{Environment.NewLine}Dialog", tv.SelectedText);
  4988. tv.Copy ();
  4989. tv.IsSelecting = false;
  4990. tv.CursorPosition = new (2, 4);
  4991. tv.Paste ();
  4992. SetupFakeApplicationAttribute.RunIteration ();
  4993. expectedText = @"
  4994. TopLevel
  4995. Base
  4996. Dialog
  4997. Menu
  4998. ErTopLevel
  4999. Base
  5000. Dialogror ";
  5001. DriverAssert.AssertDriverContentsWithFrameAre (expectedText, _output);
  5002. expectedColor = @"
  5003. 0000000055
  5004. 1111555555
  5005. 2222225555
  5006. 3333555555
  5007. 4400000000
  5008. 1111555555
  5009. 2222224445";
  5010. DriverAssert.AssertDriverAttributesAre (expectedColor, _output, Application.Driver, attributes);
  5011. tv.Undo ();
  5012. tv.CursorPosition = new (0, 3);
  5013. tv.SelectionStartColumn = 0;
  5014. tv.SelectionStartRow = 0;
  5015. Assert.Equal (
  5016. $"TopLevel{Environment.NewLine}Base{Environment.NewLine}Dialog{Environment.NewLine}",
  5017. tv.SelectedText
  5018. );
  5019. tv.Copy ();
  5020. tv.IsSelecting = false;
  5021. tv.CursorPosition = new (2, 4);
  5022. tv.Paste ();
  5023. SetupFakeApplicationAttribute.RunIteration ();
  5024. expectedText = @"
  5025. TopLevel
  5026. Base
  5027. Dialog
  5028. Menu
  5029. ErTopLevel
  5030. Base
  5031. Dialog
  5032. ror ";
  5033. DriverAssert.AssertDriverContentsWithFrameAre (expectedText, _output);
  5034. expectedColor = @"
  5035. 0000000055
  5036. 1111555555
  5037. 2222225555
  5038. 3333555555
  5039. 4400000000
  5040. 1111555555
  5041. 2222225555
  5042. 4445555555";
  5043. DriverAssert.AssertDriverAttributesAre (expectedColor, _output, Application.Driver, attributes);
  5044. Application.End (rs);
  5045. top.Dispose ();
  5046. }
  5047. [Fact]
  5048. [TextViewTestsSetupFakeApplication]
  5049. public void IsSelecting_False_If_SelectedLength_Is_Zero_On_Mouse_Click ()
  5050. {
  5051. _textView.Text = "This is the first line.";
  5052. var top = new Toplevel ();
  5053. top.Add (_textView);
  5054. Application.Begin (top);
  5055. Application.RaiseMouseEvent (new () { ScreenPosition = new (22, 0), Flags = MouseFlags.Button1Pressed });
  5056. Assert.Equal (22, _textView.CursorPosition.X);
  5057. Assert.Equal (0, _textView.CursorPosition.Y);
  5058. Assert.Equal (0, _textView.SelectedLength);
  5059. Assert.True (_textView.IsSelecting);
  5060. Application.RaiseMouseEvent (new () { ScreenPosition = new (22, 0), Flags = MouseFlags.Button1Released });
  5061. Assert.Equal (22, _textView.CursorPosition.X);
  5062. Assert.Equal (0, _textView.CursorPosition.Y);
  5063. Assert.Equal (0, _textView.SelectedLength);
  5064. Assert.True (_textView.IsSelecting);
  5065. Application.RaiseMouseEvent (new () { ScreenPosition = new (22, 0), Flags = MouseFlags.Button1Clicked });
  5066. Assert.Equal (22, _textView.CursorPosition.X);
  5067. Assert.Equal (0, _textView.CursorPosition.Y);
  5068. Assert.Equal (0, _textView.SelectedLength);
  5069. Assert.False (_textView.IsSelecting);
  5070. top.Dispose ();
  5071. Application.Shutdown ();
  5072. }
  5073. private TextView CreateTextView () { return new () { Width = 30, Height = 10 }; }
  5074. }