TextViewTests.cs 343 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147
  1. using System.Reflection;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4. using UnitTests;
  5. using Xunit.Abstractions;
  6. namespace Terminal.Gui.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. [TextViewTestsAutoInitShutdown]
  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. [TextViewTestsAutoInitShutdown]
  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. [TextViewTestsAutoInitShutdown]
  53. public void BackTab_Test_Follow_By_Tab ()
  54. {
  55. var top = new Toplevel ();
  56. top.Add (_textView);
  57. Application.Iteration += (s, a) =>
  58. {
  59. int width = _textView.Viewport.Width - 1;
  60. Assert.Equal (30, width + 1);
  61. Assert.Equal (10, _textView.Height);
  62. _textView.Text = "";
  63. for (var i = 0; i < 100; i++)
  64. {
  65. _textView.Text += "\t";
  66. }
  67. var col = 100;
  68. int tabWidth = _textView.TabWidth;
  69. int leftCol = _textView.LeftColumn;
  70. _textView.MoveEnd ();
  71. Assert.Equal (new (col, 0), _textView.CursorPosition);
  72. leftCol = GetLeftCol (leftCol);
  73. Assert.Equal (leftCol, _textView.LeftColumn);
  74. while (col > 0)
  75. {
  76. col--;
  77. _textView.NewKeyDownEvent (Key.Tab.WithShift);
  78. Assert.Equal (new (col, 0), _textView.CursorPosition);
  79. leftCol = GetLeftCol (leftCol);
  80. Assert.Equal (leftCol, _textView.LeftColumn);
  81. }
  82. while (col < 100)
  83. {
  84. col++;
  85. _textView.NewKeyDownEvent (Key.Tab);
  86. Assert.Equal (new (col, 0), _textView.CursorPosition);
  87. leftCol = GetLeftCol (leftCol);
  88. Assert.Equal (leftCol, _textView.LeftColumn);
  89. }
  90. Application.Top.Remove (_textView);
  91. Application.RequestStop ();
  92. };
  93. Application.Run (top);
  94. top.Dispose ();
  95. }
  96. [Fact]
  97. [TextViewTestsAutoInitShutdown]
  98. public void CanFocus_False_Wont_Focus_With_Mouse ()
  99. {
  100. Toplevel top = new ();
  101. var tv = new TextView { Width = Dim.Fill (), CanFocus = false, ReadOnly = true, Text = "some text" };
  102. var fv = new FrameView
  103. {
  104. Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = false, Title = "I shouldn't get focus"
  105. };
  106. fv.Add (tv);
  107. top.Add (fv);
  108. Application.Begin (top);
  109. Assert.False (tv.CanFocus);
  110. Assert.False (tv.HasFocus);
  111. Assert.False (fv.CanFocus);
  112. Assert.False (fv.HasFocus);
  113. tv.NewMouseEvent (new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked });
  114. Assert.Empty (tv.SelectedText);
  115. Assert.False (tv.CanFocus);
  116. Assert.False (tv.HasFocus);
  117. Assert.False (fv.CanFocus);
  118. Assert.False (fv.HasFocus);
  119. fv.CanFocus = true;
  120. tv.CanFocus = true;
  121. tv.NewMouseEvent (new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked });
  122. Assert.Equal ("some ", tv.SelectedText);
  123. Assert.True (tv.CanFocus);
  124. Assert.True (tv.HasFocus);
  125. Assert.True (fv.CanFocus);
  126. Assert.True (fv.HasFocus);
  127. fv.CanFocus = false;
  128. tv.NewMouseEvent (new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked });
  129. Assert.Equal ("some ", tv.SelectedText); // Setting CanFocus to false don't change the SelectedText
  130. Assert.True (tv.CanFocus); // v2: CanFocus is not longer automatically changed
  131. Assert.False (tv.HasFocus);
  132. Assert.False (fv.CanFocus);
  133. Assert.False (fv.HasFocus);
  134. top.Dispose ();
  135. }
  136. [Fact]
  137. [TextViewTestsAutoInitShutdown]
  138. public void Changing_Selection_Or_CursorPosition_Update_SelectedLength_And_SelectedText ()
  139. {
  140. _textView.SelectionStartColumn = 2;
  141. _textView.SelectionStartRow = 0;
  142. Assert.Equal (0, _textView.CursorPosition.X);
  143. Assert.Equal (0, _textView.CursorPosition.Y);
  144. Assert.Equal (2, _textView.SelectedLength);
  145. Assert.Equal ("TA", _textView.SelectedText);
  146. _textView.CursorPosition = new (20, 0);
  147. Assert.Equal (2, _textView.SelectionStartColumn);
  148. Assert.Equal (0, _textView.SelectionStartRow);
  149. Assert.Equal (18, _textView.SelectedLength);
  150. Assert.Equal ("B to jump between ", _textView.SelectedText);
  151. }
  152. [Fact]
  153. public void CloseFile_Throws_If_FilePath_Is_Null ()
  154. {
  155. var tv = new TextView ();
  156. Assert.Throws<ArgumentNullException> (() => tv.CloseFile ());
  157. }
  158. [Fact]
  159. public void ContentsChanged_Event_Fires_ClearHistoryChanges ()
  160. {
  161. var eventcount = 0;
  162. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  163. var tv = new TextView { Width = 50, Height = 10, Text = text };
  164. tv.ContentsChanged += (s, e) => { eventcount++; };
  165. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  166. Assert.Equal (
  167. $"{Environment.NewLine}This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  168. tv.Text
  169. );
  170. Assert.Equal (4, tv.Lines);
  171. var expectedEventCount = 1; // for ENTER key
  172. Assert.Equal (expectedEventCount, eventcount);
  173. tv.ClearHistoryChanges ();
  174. expectedEventCount = 2;
  175. Assert.Equal (expectedEventCount, eventcount);
  176. }
  177. [Fact]
  178. public void ContentsChanged_Event_Fires_LoadStream_By_Calling_HistoryText_Clear ()
  179. {
  180. var eventcount = 0;
  181. var tv = new TextView { Width = 50, Height = 10 };
  182. tv.ContentsChanged += (s, e) => { eventcount++; };
  183. var text = "This is the first line.\r\nThis is the second line.\r\n";
  184. tv.Load (new MemoryStream (Encoding.ASCII.GetBytes (text)));
  185. Assert.Equal (
  186. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}",
  187. tv.Text
  188. );
  189. Assert.Equal (1, eventcount);
  190. }
  191. [Fact]
  192. [AutoInitShutdown]
  193. public void ContentsChanged_Event_Fires_On_Init ()
  194. {
  195. Application.Iteration += (s, a) => { Application.RequestStop (); };
  196. var expectedRow = 0;
  197. var expectedCol = 0;
  198. var eventcount = 0;
  199. var tv = new TextView { Width = 50, Height = 10 };
  200. tv.ContentsChanged += (s, e) =>
  201. {
  202. eventcount++;
  203. Assert.Equal (expectedRow, e.Row);
  204. Assert.Equal (expectedCol, e.Col);
  205. };
  206. var top = new Toplevel ();
  207. top.Add (tv);
  208. Application.Begin (top);
  209. Assert.Equal (1, eventcount);
  210. top.Dispose ();
  211. }
  212. [Fact]
  213. [AutoInitShutdown]
  214. public void ContentsChanged_Event_Fires_On_InsertText ()
  215. {
  216. var eventcount = 0;
  217. var tv = new TextView { Width = 50, Height = 10 };
  218. tv.CursorPosition = Point.Empty;
  219. tv.ContentsChanged += (s, e) => { eventcount++; };
  220. Assert.Equal (0, eventcount);
  221. tv.InsertText ("a");
  222. Assert.Equal (1, eventcount);
  223. tv.CursorPosition = Point.Empty;
  224. tv.InsertText ("bcd");
  225. Assert.Equal (4, eventcount);
  226. tv.InsertText ("e");
  227. Assert.Equal (5, eventcount);
  228. tv.InsertText ("\n");
  229. Assert.Equal (6, eventcount);
  230. tv.InsertText ("1234");
  231. Assert.Equal (10, eventcount);
  232. }
  233. [Fact]
  234. public void ContentsChanged_Event_Fires_On_LoadFile_By_Calling_HistoryText_Clear ()
  235. {
  236. var eventcount = 0;
  237. var tv = new TextView { Width = 50, Height = 10 };
  238. tv.BeginInit ();
  239. tv.EndInit ();
  240. tv.ContentsChanged += (s, e) => { eventcount++; };
  241. var fileName = "textview.txt";
  242. File.WriteAllText (fileName, "This is the first line.\r\nThis is the second line.\r\n");
  243. tv.Load (fileName);
  244. Assert.Equal (1, eventcount);
  245. Assert.Equal (
  246. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}",
  247. tv.Text
  248. );
  249. }
  250. [Fact]
  251. [AutoInitShutdown]
  252. public void ContentsChanged_Event_Fires_On_Set_Text ()
  253. {
  254. Application.Iteration += (s, a) => { Application.RequestStop (); };
  255. var eventcount = 0;
  256. var expectedRow = 0;
  257. var expectedCol = 0;
  258. var tv = new TextView
  259. {
  260. Width = 50,
  261. Height = 10,
  262. // you'd think col would be 3, but it's 0 because TextView sets
  263. // row/col = 0 when you set Text
  264. Text = "abc"
  265. };
  266. tv.ContentsChanged += (s, e) =>
  267. {
  268. eventcount++;
  269. Assert.Equal (expectedRow, e.Row);
  270. Assert.Equal (expectedCol, e.Col);
  271. };
  272. Assert.Equal ("abc", tv.Text);
  273. var top = new Toplevel ();
  274. top.Add (tv);
  275. RunState rs = Application.Begin (top);
  276. Assert.Equal (1, eventcount); // for Initialize
  277. expectedCol = 0;
  278. tv.Text = "defg";
  279. Assert.Equal (2, eventcount); // for set Text = "defg"
  280. top.Dispose ();
  281. }
  282. [Fact]
  283. [AutoInitShutdown]
  284. public void ContentsChanged_Event_Fires_On_Typing ()
  285. {
  286. Application.Iteration += (s, a) => { Application.RequestStop (); };
  287. var eventcount = 0;
  288. var expectedRow = 0;
  289. var expectedCol = 0;
  290. var tv = new TextView { Width = 50, Height = 10 };
  291. tv.ContentsChanged += (s, e) =>
  292. {
  293. eventcount++;
  294. Assert.Equal (expectedRow, e.Row);
  295. Assert.Equal (expectedCol, e.Col);
  296. };
  297. var top = new Toplevel ();
  298. top.Add (tv);
  299. RunState rs = Application.Begin (top);
  300. Assert.Equal (1, eventcount); // for Initialize
  301. expectedCol = 0;
  302. tv.Text = "ay";
  303. Assert.Equal (2, eventcount);
  304. expectedCol = 1;
  305. tv.NewKeyDownEvent (Key.Y.WithShift);
  306. Assert.Equal (3, eventcount);
  307. Assert.Equal ("Yay", tv.Text);
  308. top.Dispose ();
  309. }
  310. [Fact]
  311. [TextViewTestsAutoInitShutdown]
  312. public void ContentsChanged_Event_Fires_On_Undo_Redo ()
  313. {
  314. var eventcount = 0;
  315. var expectedEventCount = 0;
  316. _textView.ContentsChanged += (s, e) => { eventcount++; };
  317. expectedEventCount++;
  318. _textView.Text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  319. Assert.Equal (expectedEventCount, eventcount);
  320. expectedEventCount++;
  321. Assert.True (_textView.NewKeyDownEvent (Key.Enter));
  322. Assert.Equal (expectedEventCount, eventcount);
  323. // Undo
  324. expectedEventCount++;
  325. Assert.True (_textView.NewKeyDownEvent (Key.Z.WithCtrl));
  326. Assert.Equal (expectedEventCount, eventcount);
  327. // Redo
  328. expectedEventCount++;
  329. Assert.True (_textView.NewKeyDownEvent (Key.R.WithCtrl));
  330. Assert.Equal (expectedEventCount, eventcount);
  331. // Undo
  332. expectedEventCount++;
  333. Assert.True (_textView.NewKeyDownEvent (Key.Z.WithCtrl));
  334. Assert.Equal (expectedEventCount, eventcount);
  335. // Redo
  336. expectedEventCount++;
  337. Assert.True (_textView.NewKeyDownEvent (Key.R.WithCtrl));
  338. Assert.Equal (expectedEventCount, eventcount);
  339. }
  340. [Fact]
  341. [TextViewTestsAutoInitShutdown]
  342. public void ContentsChanged_Event_Fires_Using_Copy_Or_Cut_Tests ()
  343. {
  344. var eventcount = 0;
  345. _textView.ContentsChanged += (s, e) => { eventcount++; };
  346. var expectedEventCount = 1;
  347. // reset
  348. _textView.Text = TextViewTestsAutoInitShutdown.Txt;
  349. Assert.Equal (expectedEventCount, eventcount);
  350. expectedEventCount += 3;
  351. Copy_Or_Cut_And_Paste_With_No_Selection ();
  352. Assert.Equal (expectedEventCount, eventcount);
  353. // reset
  354. expectedEventCount += 1;
  355. _textView.Text = TextViewTestsAutoInitShutdown.Txt;
  356. Assert.Equal (expectedEventCount, eventcount);
  357. expectedEventCount += 3;
  358. Copy_Or_Cut_And_Paste_With_Selection ();
  359. Assert.Equal (expectedEventCount, eventcount);
  360. // reset
  361. expectedEventCount += 1;
  362. _textView.Text = TextViewTestsAutoInitShutdown.Txt;
  363. Assert.Equal (expectedEventCount, eventcount);
  364. expectedEventCount += 1;
  365. Copy_Or_Cut_Not_Null_If_Has_Selection ();
  366. Assert.Equal (expectedEventCount, eventcount);
  367. // reset
  368. expectedEventCount += 1;
  369. _textView.Text = TextViewTestsAutoInitShutdown.Txt;
  370. Assert.Equal (expectedEventCount, eventcount);
  371. expectedEventCount += 1;
  372. Copy_Or_Cut_Null_If_No_Selection ();
  373. Assert.Equal (expectedEventCount, eventcount);
  374. // reset
  375. expectedEventCount += 1;
  376. _textView.Text = TextViewTestsAutoInitShutdown.Txt;
  377. Assert.Equal (expectedEventCount, eventcount);
  378. expectedEventCount += 4;
  379. Copy_Without_Selection ();
  380. Assert.Equal (expectedEventCount, eventcount);
  381. // reset
  382. expectedEventCount += 1;
  383. _textView.Text = TextViewTestsAutoInitShutdown.Txt;
  384. Assert.Equal (expectedEventCount, eventcount);
  385. expectedEventCount += 4;
  386. Copy_Without_Selection ();
  387. Assert.Equal (expectedEventCount, eventcount);
  388. }
  389. [Fact]
  390. [TextViewTestsAutoInitShutdown]
  391. public void ContentsChanged_Event_Fires_Using_Kill_Delete_Tests ()
  392. {
  393. var eventcount = 0;
  394. _textView.ContentsChanged += (s, e) => { eventcount++; };
  395. var expectedEventCount = 1;
  396. Kill_Delete_WordForward ();
  397. Assert.Equal (expectedEventCount, eventcount); // for Initialize
  398. expectedEventCount += 1;
  399. Kill_Delete_WordBackward ();
  400. Assert.Equal (expectedEventCount, eventcount);
  401. expectedEventCount += 2;
  402. Kill_To_End_Delete_Forwards_Copy_To_The_Clipboard_And_Paste ();
  403. Assert.Equal (expectedEventCount, eventcount);
  404. expectedEventCount += 2;
  405. Kill_To_Start_Delete_Backwards_Copy_To_The_Clipboard_And_Paste ();
  406. Assert.Equal (expectedEventCount, eventcount);
  407. }
  408. [Fact]
  409. [AutoInitShutdown]
  410. public void ContentsChanged_Event_NoFires_On_CursorPosition ()
  411. {
  412. var eventcount = 0;
  413. var tv = new TextView { Width = 50, Height = 10 };
  414. tv.ContentsChanged += (s, e) => { eventcount++; };
  415. Assert.Equal (0, eventcount);
  416. tv.CursorPosition = Point.Empty;
  417. Assert.Equal (0, eventcount);
  418. }
  419. [Fact]
  420. [TextViewTestsAutoInitShutdown]
  421. public void Copy_Or_Cut_And_Paste_With_No_Selection ()
  422. {
  423. _textView.SelectionStartColumn = 20;
  424. _textView.SelectionStartRow = 0;
  425. _textView.CursorPosition = new (24, 0);
  426. _textView.NewKeyDownEvent (Key.C.WithCtrl); // Copy
  427. Assert.Equal ("text", _textView.SelectedText);
  428. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  429. _textView.SelectionStartColumn = 0;
  430. _textView.SelectionStartRow = 0;
  431. Assert.Equal (new (24, 0), _textView.CursorPosition);
  432. Assert.True (_textView.IsSelecting);
  433. _textView.IsSelecting = false;
  434. _textView.NewKeyDownEvent (Key.Y.WithCtrl); // Paste
  435. Assert.Equal (new (28, 0), _textView.CursorPosition);
  436. Assert.False (_textView.IsSelecting);
  437. Assert.Equal ("TAB to jump between texttext fields.", _textView.Text);
  438. _textView.SelectionStartColumn = 24;
  439. _textView.SelectionStartRow = 0;
  440. _textView.NewKeyDownEvent (Key.W.WithCtrl); // Cut
  441. Assert.Equal (new (24, 0), _textView.CursorPosition);
  442. Assert.False (_textView.IsSelecting);
  443. Assert.Equal ("", _textView.SelectedText);
  444. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  445. _textView.SelectionStartColumn = 0;
  446. _textView.SelectionStartRow = 0;
  447. _textView.IsSelecting = false;
  448. _textView.NewKeyDownEvent (Key.Y.WithCtrl); // Paste
  449. Assert.Equal (new (28, 0), _textView.CursorPosition);
  450. Assert.False (_textView.IsSelecting);
  451. Assert.Equal ("TAB to jump between texttext fields.", _textView.Text);
  452. }
  453. [Fact]
  454. [TextViewTestsAutoInitShutdown]
  455. public void Copy_Or_Cut_And_Paste_With_Selection ()
  456. {
  457. _textView.SelectionStartColumn = 20;
  458. _textView.SelectionStartRow = 0;
  459. _textView.CursorPosition = new (24, 0);
  460. _textView.NewKeyDownEvent (Key.C.WithCtrl); // Copy
  461. Assert.Equal ("text", _textView.SelectedText);
  462. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  463. _textView.NewKeyDownEvent (Key.Y.WithCtrl); // Paste
  464. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  465. _textView.SelectionStartColumn = 20;
  466. _textView.SelectionStartRow = 0;
  467. _textView.NewKeyDownEvent (Key.W.WithCtrl); // Cut
  468. _textView.NewKeyDownEvent (Key.Y.WithCtrl); // Paste
  469. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  470. }
  471. [Fact]
  472. [TextViewTestsAutoInitShutdown]
  473. public void Copy_Or_Cut_Not_Null_If_Has_Selection ()
  474. {
  475. _textView.SelectionStartColumn = 20;
  476. _textView.SelectionStartRow = 0;
  477. _textView.CursorPosition = new (24, 0);
  478. _textView.NewKeyDownEvent (Key.C.WithCtrl); // Copy
  479. Assert.Equal ("text", _textView.SelectedText);
  480. _textView.NewKeyDownEvent (Key.W.WithCtrl); // Cut
  481. Assert.Equal ("", _textView.SelectedText);
  482. }
  483. [Fact]
  484. [TextViewTestsAutoInitShutdown]
  485. public void Copy_Or_Cut_Null_If_No_Selection ()
  486. {
  487. _textView.SelectionStartColumn = 0;
  488. _textView.SelectionStartRow = 0;
  489. _textView.NewKeyDownEvent (Key.C.WithCtrl); // Copy
  490. Assert.Equal ("", _textView.SelectedText);
  491. _textView.NewKeyDownEvent (Key.W.WithCtrl); // Cut
  492. Assert.Equal ("", _textView.SelectedText);
  493. }
  494. [Fact]
  495. [TextViewTestsAutoInitShutdown]
  496. public void Copy_Paste_Surrogate_Pairs ()
  497. {
  498. _textView.Text = "TextView with some more test text. Unicode shouldn't 𝔹Aℝ𝔽!";
  499. _textView.SelectAll ();
  500. _textView.Cut ();
  501. Assert.Equal (
  502. "TextView with some more test text. Unicode shouldn't 𝔹Aℝ𝔽!",
  503. Application.Driver?.Clipboard.GetClipboardData ()
  504. );
  505. Assert.Equal (string.Empty, _textView.Text);
  506. _textView.Paste ();
  507. Assert.Equal ("TextView with some more test text. Unicode shouldn't 𝔹Aℝ𝔽!", _textView.Text);
  508. }
  509. [Fact]
  510. [TextViewTestsAutoInitShutdown]
  511. public void Copy_Without_Selection ()
  512. {
  513. _textView.Text = "This is the first line.\nThis is the second line.\n";
  514. _textView.CursorPosition = new (0, _textView.Lines - 1);
  515. _textView.NewKeyDownEvent (Key.C.WithCtrl); // Copy
  516. _textView.NewKeyDownEvent (Key.Y.WithCtrl); // Paste
  517. Assert.Equal (
  518. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}{Environment.NewLine}",
  519. _textView.Text
  520. );
  521. _textView.CursorPosition = new (3, 1);
  522. _textView.NewKeyDownEvent (Key.C.WithCtrl); // Copy
  523. _textView.NewKeyDownEvent (Key.Y.WithCtrl); // Paste
  524. Assert.Equal (
  525. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the second line.{Environment.NewLine}{Environment.NewLine}",
  526. _textView.Text
  527. );
  528. Assert.Equal (new (3, 2), _textView.CursorPosition);
  529. _textView.NewKeyDownEvent (Key.Y.WithCtrl); // Paste
  530. Assert.Equal (
  531. $"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}",
  532. _textView.Text
  533. );
  534. Assert.Equal (new (3, 3), _textView.CursorPosition);
  535. }
  536. [Fact]
  537. [TextViewTestsAutoInitShutdown]
  538. public void Cursor_Position_Multiline_False_Initialization ()
  539. {
  540. Assert.False (_textView.IsInitialized);
  541. Assert.True (_textView.Multiline);
  542. _textView.Multiline = false;
  543. Assert.Equal (32, _textView.CursorPosition.X);
  544. Assert.Equal (0, _textView.CursorPosition.Y);
  545. Assert.Equal (0, _textView.SelectedLength);
  546. Assert.Equal ("", _textView.SelectedText);
  547. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  548. }
  549. [Fact]
  550. [TextViewTestsAutoInitShutdown]
  551. public void CursorPosition_With_Value_Greater_Than_Text_Length_Changes_To_Text_Length ()
  552. {
  553. _textView.CursorPosition = new (33, 1);
  554. Assert.Equal (32, _textView.CursorPosition.X);
  555. Assert.Equal (0, _textView.CursorPosition.Y);
  556. Assert.Equal (0, _textView.SelectedLength);
  557. Assert.Equal ("", _textView.SelectedText);
  558. }
  559. [Fact]
  560. [TextViewTestsAutoInitShutdown]
  561. public void CursorPosition_With_Value_Less_Than_Zero_Changes_To_Zero ()
  562. {
  563. _textView.CursorPosition = new (-1, -1);
  564. Assert.Equal (0, _textView.CursorPosition.X);
  565. Assert.Equal (0, _textView.CursorPosition.Y);
  566. Assert.Equal (0, _textView.SelectedLength);
  567. Assert.Equal ("", _textView.SelectedText);
  568. }
  569. [Fact]
  570. [TextViewTestsAutoInitShutdown]
  571. public void Cut_Not_Allowed_If_ReadOnly_Is_True ()
  572. {
  573. _textView.ReadOnly = true;
  574. _textView.SelectionStartColumn = 20;
  575. _textView.SelectionStartRow = 0;
  576. _textView.CursorPosition = new (24, 0);
  577. _textView.NewKeyDownEvent (Key.C.WithCtrl); // Copy
  578. Assert.Equal ("text", _textView.SelectedText);
  579. _textView.NewKeyDownEvent (
  580. Key.W.WithCtrl
  581. ); // Selecting is set to false after Cut.
  582. Assert.Equal ("", _textView.SelectedText);
  583. _textView.ReadOnly = false;
  584. Assert.False (_textView.IsSelecting);
  585. _textView.IsSelecting = true; // Needed to set Selecting to true.
  586. _textView.NewKeyDownEvent (Key.C.WithCtrl); // Copy
  587. Assert.Equal ("text", _textView.SelectedText);
  588. _textView.NewKeyDownEvent (Key.W.WithCtrl); // Cut
  589. Assert.Equal ("", _textView.SelectedText);
  590. }
  591. [Fact]
  592. [AutoInitShutdown]
  593. public void DeleteTextBackwards_WordWrap_False_Return_Undo ()
  594. {
  595. const string text = "This is the first line.\nThis is the second line.\n";
  596. var tv = new TextView { Width = Dim.Fill (), Height = Dim.Fill (), Text = text };
  597. string envText = tv.Text;
  598. var top = new Toplevel ();
  599. top.Add (tv);
  600. RunState rs = Application.Begin (top);
  601. Application.RunIteration (ref rs);
  602. Assert.False (tv.WordWrap);
  603. Assert.Equal (Point.Empty, tv.CursorPosition);
  604. DriverAssert.AssertDriverContentsWithFrameAre (
  605. @"
  606. This is the first line.
  607. This is the second line.
  608. ",
  609. _output
  610. );
  611. tv.CursorPosition = new (3, 0);
  612. Assert.Equal (new (3, 0), tv.CursorPosition);
  613. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  614. Application.RunIteration (ref rs);
  615. Assert.Equal (new (2, 0), tv.CursorPosition);
  616. DriverAssert.AssertDriverContentsWithFrameAre (
  617. @"
  618. Ths is the first line.
  619. This is the second line.
  620. ",
  621. _output
  622. );
  623. tv.CursorPosition = new (0, 1);
  624. Assert.Equal (new (0, 1), tv.CursorPosition);
  625. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  626. Application.RunIteration (ref rs);
  627. Assert.Equal (new (22, 0), tv.CursorPosition);
  628. DriverAssert.AssertDriverContentsWithFrameAre (
  629. @"
  630. Ths is the first line.This is the second line.
  631. ",
  632. _output
  633. );
  634. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  635. Application.RunIteration (ref rs);
  636. Assert.Equal (new (0, 1), tv.CursorPosition);
  637. DriverAssert.AssertDriverContentsWithFrameAre (
  638. @"
  639. Ths is the first line.
  640. This is the second line.
  641. ",
  642. _output
  643. );
  644. while (tv.Text != envText)
  645. {
  646. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  647. }
  648. Application.RunIteration (ref rs);
  649. Assert.Equal (envText, tv.Text);
  650. Assert.Equal (new (3, 0), tv.CursorPosition);
  651. Assert.False (tv.IsDirty);
  652. top.Dispose ();
  653. }
  654. [Fact]
  655. [AutoInitShutdown]
  656. public void DeleteTextBackwards_WordWrap_True_Return_Undo ()
  657. {
  658. const string text = "This is the first line.\nThis is the second line.\n";
  659. var tv = new TextView { Width = Dim.Fill (), Height = Dim.Fill (), Text = text, WordWrap = true };
  660. string envText = tv.Text;
  661. var top = new Toplevel ();
  662. top.Add (tv);
  663. RunState rs = Application.Begin (top);
  664. Application.RunIteration (ref rs);
  665. Assert.True (tv.WordWrap);
  666. Assert.Equal (Point.Empty, tv.CursorPosition);
  667. DriverAssert.AssertDriverContentsWithFrameAre (
  668. @"
  669. This is the first line.
  670. This is the second line.
  671. ",
  672. _output
  673. );
  674. tv.CursorPosition = new (3, 0);
  675. Assert.Equal (new (3, 0), tv.CursorPosition);
  676. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  677. Application.RunIteration (ref rs);
  678. Assert.Equal (new (2, 0), tv.CursorPosition);
  679. DriverAssert.AssertDriverContentsWithFrameAre (
  680. @"
  681. Ths is the first line.
  682. This is the second line.
  683. ",
  684. _output
  685. );
  686. tv.CursorPosition = new (0, 1);
  687. Assert.Equal (new (0, 1), tv.CursorPosition);
  688. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  689. Application.RunIteration (ref rs);
  690. Assert.Equal (new (22, 0), tv.CursorPosition);
  691. DriverAssert.AssertDriverContentsWithFrameAre (
  692. @"
  693. Ths is the first line.This is the second line.
  694. ",
  695. _output
  696. );
  697. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  698. Application.RunIteration (ref rs);
  699. Assert.Equal (new (0, 1), tv.CursorPosition);
  700. DriverAssert.AssertDriverContentsWithFrameAre (
  701. @"
  702. Ths is the first line.
  703. This is the second line.
  704. ",
  705. _output
  706. );
  707. while (tv.Text != envText)
  708. {
  709. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  710. }
  711. Application.RunIteration (ref rs);
  712. Assert.Equal (envText, tv.Text);
  713. Assert.Equal (new (3, 0), tv.CursorPosition);
  714. Assert.False (tv.IsDirty);
  715. top.Dispose ();
  716. }
  717. [Fact]
  718. [AutoInitShutdown]
  719. public void DeleteTextForwards_WordWrap_False_Return_Undo ()
  720. {
  721. const string text = "This is the first line.\nThis is the second line.\n";
  722. var tv = new TextView { Width = Dim.Fill (), Height = Dim.Fill (), Text = text };
  723. string envText = tv.Text;
  724. var top = new Toplevel ();
  725. top.Add (tv);
  726. RunState rs = Application.Begin (top);
  727. Application.RunIteration (ref rs);
  728. Assert.False (tv.WordWrap);
  729. Assert.Equal (Point.Empty, tv.CursorPosition);
  730. DriverAssert.AssertDriverContentsWithFrameAre (
  731. @"
  732. This is the first line.
  733. This is the second line.
  734. ",
  735. _output
  736. );
  737. tv.CursorPosition = new (2, 0);
  738. Assert.Equal (new (2, 0), tv.CursorPosition);
  739. Assert.True (tv.NewKeyDownEvent (Key.Delete));
  740. Application.RunIteration (ref rs);
  741. Assert.Equal (new (2, 0), tv.CursorPosition);
  742. DriverAssert.AssertDriverContentsWithFrameAre (
  743. @"
  744. Ths is the first line.
  745. This is the second line.
  746. ",
  747. _output
  748. );
  749. tv.CursorPosition = new (22, 0);
  750. Assert.Equal (new (22, 0), tv.CursorPosition);
  751. Assert.True (tv.NewKeyDownEvent (Key.Delete));
  752. Application.RunIteration (ref rs);
  753. Assert.Equal (new (22, 0), tv.CursorPosition);
  754. DriverAssert.AssertDriverContentsWithFrameAre (
  755. @"
  756. Ths is the first line.This is the second line.
  757. ",
  758. _output
  759. );
  760. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  761. Application.RunIteration (ref rs);
  762. Assert.Equal (new (0, 1), tv.CursorPosition);
  763. DriverAssert.AssertDriverContentsWithFrameAre (
  764. @"
  765. Ths is the first line.
  766. This is the second line.
  767. ",
  768. _output
  769. );
  770. while (tv.Text != envText)
  771. {
  772. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  773. }
  774. Assert.Equal (envText, tv.Text);
  775. Assert.Equal (new (2, 0), tv.CursorPosition);
  776. Assert.False (tv.IsDirty);
  777. top.Dispose ();
  778. }
  779. [Fact]
  780. [AutoInitShutdown]
  781. public void DeleteTextForwards_WordWrap_True_Return_Undo ()
  782. {
  783. const string text = "This is the first line.\nThis is the second line.\n";
  784. var tv = new TextView { Width = Dim.Fill (), Height = Dim.Fill (), Text = text, WordWrap = true };
  785. string envText = tv.Text;
  786. var top = new Toplevel ();
  787. top.Add (tv);
  788. RunState rs = Application.Begin (top);
  789. Application.RunIteration (ref rs);
  790. Assert.True (tv.WordWrap);
  791. Assert.Equal (Point.Empty, tv.CursorPosition);
  792. DriverAssert.AssertDriverContentsWithFrameAre (
  793. @"
  794. This is the first line.
  795. This is the second line.
  796. ",
  797. _output
  798. );
  799. tv.CursorPosition = new (2, 0);
  800. Assert.Equal (new (2, 0), tv.CursorPosition);
  801. Assert.True (tv.NewKeyDownEvent (Key.Delete));
  802. Application.RunIteration (ref rs);
  803. Assert.Equal (new (2, 0), tv.CursorPosition);
  804. DriverAssert.AssertDriverContentsWithFrameAre (
  805. @"
  806. Ths is the first line.
  807. This is the second line.
  808. ",
  809. _output
  810. );
  811. tv.CursorPosition = new (22, 0);
  812. Assert.Equal (new (22, 0), tv.CursorPosition);
  813. Assert.True (tv.NewKeyDownEvent (Key.Delete));
  814. Application.RunIteration (ref rs);
  815. Assert.Equal (new (22, 0), tv.CursorPosition);
  816. DriverAssert.AssertDriverContentsWithFrameAre (
  817. @"
  818. Ths is the first line.This is the second line.
  819. ",
  820. _output
  821. );
  822. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  823. Application.RunIteration (ref rs);
  824. Assert.Equal (new (0, 1), tv.CursorPosition);
  825. DriverAssert.AssertDriverContentsWithFrameAre (
  826. @"
  827. Ths is the first line.
  828. This is the second line.
  829. ",
  830. _output
  831. );
  832. while (tv.Text != envText)
  833. {
  834. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  835. }
  836. Application.RunIteration (ref rs);
  837. Assert.Equal (envText, tv.Text);
  838. Assert.Equal (new (2, 0), tv.CursorPosition);
  839. Assert.False (tv.IsDirty);
  840. top.Dispose ();
  841. }
  842. [Fact]
  843. [TextViewTestsAutoInitShutdown]
  844. public void DesiredCursorVisibility_Horizontal_Navigation ()
  845. {
  846. var text = "";
  847. for (var i = 0; i < 12; i++)
  848. {
  849. text += $"{i.ToString () [^1]}";
  850. }
  851. var tv = new TextView { Width = 10, Height = 10 };
  852. tv.Text = text;
  853. var top = new Toplevel ();
  854. top.Add (tv);
  855. Application.Begin (top);
  856. Assert.Equal (0, tv.LeftColumn);
  857. Assert.Equal (Point.Empty, tv.CursorPosition);
  858. Application.PositionCursor ();
  859. Assert.Equal (CursorVisibility.Default, tv.CursorVisibility);
  860. for (var i = 0; i < 12; i++)
  861. {
  862. tv.NewMouseEvent (new () { Flags = MouseFlags.WheeledRight });
  863. Assert.Equal (Math.Min (i + 1, 11), tv.LeftColumn);
  864. Application.PositionCursor ();
  865. Application.Driver!.GetCursorVisibility (out CursorVisibility cursorVisibility);
  866. Assert.Equal (CursorVisibility.Invisible, cursorVisibility);
  867. }
  868. for (var i = 11; i > 0; i--)
  869. {
  870. tv.NewMouseEvent (new () { Flags = MouseFlags.WheeledLeft });
  871. Assert.Equal (i - 1, tv.LeftColumn);
  872. Application.PositionCursor ();
  873. Application.Driver!.GetCursorVisibility (out CursorVisibility cursorVisibility);
  874. if (i - 1 == 0)
  875. {
  876. Assert.Equal (CursorVisibility.Default, cursorVisibility);
  877. }
  878. else
  879. {
  880. Assert.Equal (CursorVisibility.Invisible, cursorVisibility);
  881. }
  882. }
  883. top.Dispose ();
  884. Application.Shutdown ();
  885. }
  886. [Fact]
  887. [TextViewTestsAutoInitShutdown]
  888. public void DesiredCursorVisibility_Vertical_Navigation ()
  889. {
  890. var text = "";
  891. for (var i = 0; i < 12; i++)
  892. {
  893. text += $"This is the line {i}\n";
  894. }
  895. var tv = new TextView { Width = 10, Height = 10 };
  896. tv.Text = text;
  897. var top = new Toplevel ();
  898. top.Add (tv);
  899. Application.Begin (top);
  900. Assert.Equal (0, tv.TopRow);
  901. Application.PositionCursor ();
  902. Assert.Equal (CursorVisibility.Default, tv.CursorVisibility);
  903. for (var i = 0; i < 12; i++)
  904. {
  905. tv.NewMouseEvent (new () { Flags = MouseFlags.WheeledDown });
  906. Application.PositionCursor ();
  907. Assert.Equal (i + 1, tv.TopRow);
  908. Application.Driver!.GetCursorVisibility (out CursorVisibility cursorVisibility);
  909. Assert.Equal (CursorVisibility.Invisible, cursorVisibility);
  910. }
  911. for (var i = 12; i > 0; i--)
  912. {
  913. tv.NewMouseEvent (new () { Flags = MouseFlags.WheeledUp });
  914. Application.PositionCursor ();
  915. Assert.Equal (i - 1, tv.TopRow);
  916. Application.PositionCursor ();
  917. Application.Driver!.GetCursorVisibility (out CursorVisibility cursorVisibility);
  918. if (i - 1 == 0)
  919. {
  920. Assert.Equal (CursorVisibility.Default, cursorVisibility);
  921. }
  922. else
  923. {
  924. Assert.Equal (CursorVisibility.Invisible, cursorVisibility);
  925. }
  926. }
  927. top.Dispose ();
  928. Application.Shutdown ();
  929. }
  930. [Fact]
  931. public void GetRegion_StringFromRunes_Environment_NewLine ()
  932. {
  933. var tv = new TextView { Text = $"1{Environment.NewLine}2" };
  934. Assert.Equal ($"1{Environment.NewLine}2", tv.Text);
  935. Assert.Equal ("", tv.SelectedText);
  936. tv.SelectAll ();
  937. Assert.Equal ($"1{Environment.NewLine}2", tv.Text);
  938. Assert.Equal ($"1{Environment.NewLine}2", tv.SelectedText);
  939. }
  940. [Fact]
  941. public void HistoryText_ClearHistoryChanges ()
  942. {
  943. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  944. var tv = new TextView { Text = text };
  945. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  946. Assert.Equal (
  947. $"{Environment.NewLine}This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  948. tv.Text
  949. );
  950. Assert.Equal (4, tv.Lines);
  951. Assert.Equal (new (0, 1), tv.CursorPosition);
  952. Assert.True (tv.IsDirty);
  953. Assert.True (tv.HasHistoryChanges);
  954. tv.ClearHistoryChanges ();
  955. Assert.Equal (
  956. $"{Environment.NewLine}This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  957. tv.Text
  958. );
  959. Assert.Equal (4, tv.Lines);
  960. Assert.Equal (new (0, 1), tv.CursorPosition);
  961. Assert.False (tv.IsDirty);
  962. Assert.False (tv.HasHistoryChanges);
  963. }
  964. [Fact]
  965. public void HistoryText_Exceptions ()
  966. {
  967. var ht = new HistoryText ();
  968. foreach (object ls in Enum.GetValues (typeof (TextEditingLineStatus)))
  969. {
  970. if ((TextEditingLineStatus)ls != TextEditingLineStatus.Original)
  971. {
  972. Assert.Throws<ArgumentException> (
  973. () => ht.Add (
  974. new List<List<Cell>> (),
  975. Point.Empty,
  976. (TextEditingLineStatus)ls
  977. )
  978. );
  979. }
  980. }
  981. Assert.Null (Record.Exception (() => ht.Add (new () { new () }, Point.Empty)));
  982. }
  983. [Fact]
  984. public void HistoryText_IsDirty_HasHistoryChanges ()
  985. {
  986. var tv = new TextView ();
  987. Assert.Equal ("", tv.Text);
  988. Assert.Equal (1, tv.Lines);
  989. Assert.Equal (Point.Empty, tv.CursorPosition);
  990. Assert.False (tv.IsDirty);
  991. Assert.False (tv.HasHistoryChanges);
  992. Assert.True (tv.NewKeyDownEvent (Key.D1));
  993. Assert.Equal ("1", tv.Text);
  994. Assert.Equal (1, tv.Lines);
  995. Assert.Equal (new (1, 0), tv.CursorPosition);
  996. Assert.True (tv.IsDirty);
  997. Assert.True (tv.HasHistoryChanges);
  998. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  999. Assert.Equal ($"1{Environment.NewLine}", tv.Text);
  1000. Assert.Equal (2, tv.Lines);
  1001. Assert.Equal (new (0, 1), tv.CursorPosition);
  1002. Assert.True (tv.IsDirty);
  1003. Assert.True (tv.HasHistoryChanges);
  1004. Assert.True (tv.NewKeyDownEvent (Key.D2));
  1005. Assert.Equal ($"1{Environment.NewLine}2", tv.Text);
  1006. Assert.Equal (2, tv.Lines);
  1007. Assert.Equal (new (1, 1), tv.CursorPosition);
  1008. Assert.True (tv.IsDirty);
  1009. Assert.True (tv.HasHistoryChanges);
  1010. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  1011. Assert.Equal ($"1{Environment.NewLine}", tv.Text);
  1012. Assert.Equal (2, tv.Lines);
  1013. Assert.Equal (new (0, 1), tv.CursorPosition);
  1014. Assert.True (tv.IsDirty);
  1015. Assert.True (tv.HasHistoryChanges);
  1016. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  1017. Assert.Equal ("1", tv.Text);
  1018. Assert.Equal (1, tv.Lines);
  1019. Assert.Equal (new (1, 0), tv.CursorPosition);
  1020. Assert.True (tv.IsDirty);
  1021. Assert.True (tv.HasHistoryChanges);
  1022. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  1023. Assert.Equal ("", tv.Text);
  1024. Assert.Equal (1, tv.Lines);
  1025. Assert.Equal (Point.Empty, tv.CursorPosition);
  1026. // IsDirty cannot be based on HasHistoryChanges because HasHistoryChanges is greater than 0
  1027. // The only way is comparing from the original text
  1028. Assert.False (tv.IsDirty);
  1029. // Still true because HasHistoryChanges is greater than 0
  1030. Assert.True (tv.HasHistoryChanges);
  1031. }
  1032. [Fact]
  1033. public void HistoryText_Undo_Redo_Changing_On_Middle_Clear_History_Forwards ()
  1034. {
  1035. var tv = new TextView ();
  1036. Assert.True (tv.NewKeyDownEvent (Key.D1));
  1037. Assert.Equal ("1", tv.Text);
  1038. Assert.Equal (1, tv.Lines);
  1039. Assert.Equal (new (1, 0), tv.CursorPosition);
  1040. Assert.True (tv.NewKeyDownEvent (Key.D2));
  1041. Assert.Equal ("12", tv.Text);
  1042. Assert.Equal (1, tv.Lines);
  1043. Assert.Equal (new (2, 0), tv.CursorPosition);
  1044. Assert.True (tv.NewKeyDownEvent (Key.D3));
  1045. Assert.Equal ("123", tv.Text);
  1046. Assert.Equal (1, tv.Lines);
  1047. Assert.Equal (new (3, 0), tv.CursorPosition);
  1048. // Undo
  1049. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1050. Assert.Equal ("12", tv.Text);
  1051. Assert.Equal (1, tv.Lines);
  1052. Assert.Equal (new (2, 0), tv.CursorPosition);
  1053. Assert.True (tv.NewKeyDownEvent (Key.D4));
  1054. Assert.Equal ("124", tv.Text);
  1055. Assert.Equal (1, tv.Lines);
  1056. Assert.Equal (new (3, 0), tv.CursorPosition);
  1057. // Redo
  1058. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1059. Assert.Equal ("124", tv.Text);
  1060. Assert.Equal (1, tv.Lines);
  1061. Assert.Equal (new (3, 0), tv.CursorPosition);
  1062. }
  1063. [Fact]
  1064. [AutoInitShutdown (useFakeClipboard: true)]
  1065. public void HistoryText_Undo_Redo_Copy_Without_Selection_Multi_Line_Paste ()
  1066. {
  1067. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  1068. var tv = new TextView { Text = text };
  1069. tv.CursorPosition = new (23, 0);
  1070. Assert.True (tv.NewKeyDownEvent (Key.C.WithCtrl));
  1071. Assert.Equal (
  1072. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1073. tv.Text
  1074. );
  1075. Assert.Equal ("", tv.SelectedText);
  1076. Assert.Equal ("This is the first line.", Clipboard.Contents);
  1077. Assert.Equal (3, tv.Lines);
  1078. Assert.Equal (new (23, 0), tv.CursorPosition);
  1079. Assert.True (tv.NewKeyDownEvent (Key.Y.WithCtrl));
  1080. Assert.Equal (
  1081. $"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.",
  1082. tv.Text
  1083. );
  1084. Assert.Equal (4, tv.Lines);
  1085. Assert.Equal (new (23, 1), tv.CursorPosition);
  1086. // Undo
  1087. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1088. Assert.Equal (
  1089. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1090. tv.Text
  1091. );
  1092. Assert.Equal (3, tv.Lines);
  1093. Assert.Equal (new (23, 0), tv.CursorPosition);
  1094. // Redo
  1095. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1096. Assert.Equal (
  1097. $"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.",
  1098. tv.Text
  1099. );
  1100. Assert.Equal (4, tv.Lines);
  1101. Assert.Equal (new (23, 1), tv.CursorPosition);
  1102. }
  1103. [Fact]
  1104. [AutoInitShutdown]
  1105. public void HistoryText_Undo_Redo_Cut_Multi_Line_Another_Selected_Paste ()
  1106. {
  1107. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  1108. var tv = new TextView { Text = text };
  1109. tv.SelectionStartColumn = 12;
  1110. tv.CursorPosition = new (17, 0);
  1111. Assert.True (tv.NewKeyDownEvent (Key.W.WithCtrl));
  1112. Assert.Equal (
  1113. $"This is the line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1114. tv.Text
  1115. );
  1116. Assert.Equal ("", tv.SelectedText);
  1117. Assert.Equal (3, tv.Lines);
  1118. Assert.Equal (new (12, 0), tv.CursorPosition);
  1119. tv.SelectionStartColumn = 12;
  1120. tv.SelectionStartRow = 1;
  1121. tv.CursorPosition = new (18, 1);
  1122. Assert.True (tv.NewKeyDownEvent (Key.Y.WithCtrl));
  1123. Assert.Equal (
  1124. $"This is the line.{Environment.NewLine}This is the first line.{Environment.NewLine}This is the third line.",
  1125. tv.Text
  1126. );
  1127. Assert.Equal (3, tv.Lines);
  1128. Assert.Equal (new (17, 1), tv.CursorPosition);
  1129. // Undo
  1130. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1131. Assert.Equal (
  1132. $"This is the line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1133. tv.Text
  1134. );
  1135. Assert.Equal (3, tv.Lines);
  1136. Assert.Equal (new (12, 1), tv.CursorPosition);
  1137. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1138. Assert.Equal (
  1139. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1140. tv.Text
  1141. );
  1142. Assert.Equal (3, tv.Lines);
  1143. Assert.Equal (new (12, 0), tv.CursorPosition);
  1144. // Redo
  1145. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1146. Assert.Equal (
  1147. $"This is the line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1148. tv.Text
  1149. );
  1150. Assert.Equal (3, tv.Lines);
  1151. Assert.Equal (new (12, 0), tv.CursorPosition);
  1152. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1153. Assert.Equal (
  1154. $"This is the line.{Environment.NewLine}This is the first line.{Environment.NewLine}This is the third line.",
  1155. tv.Text
  1156. );
  1157. Assert.Equal (3, tv.Lines);
  1158. Assert.Equal (new (17, 1), tv.CursorPosition);
  1159. }
  1160. [Fact]
  1161. [AutoInitShutdown]
  1162. public void HistoryText_Undo_Redo_Cut_Multi_Line_Selected_Paste ()
  1163. {
  1164. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  1165. var tv = new TextView { Text = text };
  1166. tv.SelectionStartColumn = 12;
  1167. tv.CursorPosition = new (17, 0);
  1168. Assert.True (tv.NewKeyDownEvent (Key.W.WithCtrl));
  1169. Assert.Equal (
  1170. $"This is the line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1171. tv.Text
  1172. );
  1173. Assert.Equal ("", tv.SelectedText);
  1174. Assert.Equal (3, tv.Lines);
  1175. Assert.Equal (new (12, 0), tv.CursorPosition);
  1176. tv.SelectionStartColumn = 12;
  1177. tv.CursorPosition = new (11, 1);
  1178. Assert.True (tv.NewKeyDownEvent (Key.Y.WithCtrl));
  1179. Assert.Equal ($"This is the first second line.{Environment.NewLine}This is the third line.", tv.Text);
  1180. Assert.Equal (2, tv.Lines);
  1181. Assert.Equal (new (17, 0), tv.CursorPosition);
  1182. // Undo
  1183. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1184. Assert.Equal (
  1185. $"This is the line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1186. tv.Text
  1187. );
  1188. Assert.Equal (3, tv.Lines);
  1189. Assert.Equal (new (12, 0), tv.CursorPosition);
  1190. // Redo
  1191. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1192. Assert.Equal ($"This is the first second line.{Environment.NewLine}This is the third line.", tv.Text);
  1193. Assert.Equal (2, tv.Lines);
  1194. Assert.Equal (new (17, 0), tv.CursorPosition);
  1195. }
  1196. [Fact]
  1197. [AutoInitShutdown]
  1198. public void HistoryText_Undo_Redo_Cut_Simple_Paste_Starting ()
  1199. {
  1200. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  1201. var tv = new TextView { Text = text };
  1202. tv.SelectionStartColumn = 12;
  1203. tv.CursorPosition = new (18, 1);
  1204. Assert.True (tv.NewKeyDownEvent (Key.W.WithCtrl));
  1205. Assert.Equal ($"This is the line.{Environment.NewLine}This is the third line.", tv.Text);
  1206. Assert.Equal ("", tv.SelectedText);
  1207. Assert.Equal (2, tv.Lines);
  1208. Assert.Equal (new (12, 0), tv.CursorPosition);
  1209. tv.IsSelecting = false;
  1210. Assert.True (tv.NewKeyDownEvent (Key.Y.WithCtrl));
  1211. Assert.Equal (
  1212. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1213. tv.Text
  1214. );
  1215. Assert.Equal (3, tv.Lines);
  1216. Assert.Equal (new (18, 1), tv.CursorPosition);
  1217. // Undo
  1218. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1219. Assert.Equal ($"This is the line.{Environment.NewLine}This is the third line.", tv.Text);
  1220. Assert.Equal (2, tv.Lines);
  1221. Assert.Equal (new (12, 0), tv.CursorPosition);
  1222. // Redo
  1223. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1224. Assert.Equal (
  1225. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1226. tv.Text
  1227. );
  1228. Assert.Equal (3, tv.Lines);
  1229. Assert.Equal (new (18, 1), tv.CursorPosition);
  1230. }
  1231. [Fact]
  1232. public void HistoryText_Undo_Redo_Disabled_On_WordWrap ()
  1233. {
  1234. var text = "This is the first line.\nThis is the second line.\nThis is the third line.\n";
  1235. var tv = new TextView { Width = 80, Height = 5, Text = text };
  1236. Assert.False (tv.WordWrap);
  1237. tv.WordWrap = true;
  1238. tv.SelectionStartColumn = 12;
  1239. tv.CursorPosition = new (12, 2);
  1240. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  1241. Assert.Equal ($"This is the {Environment.NewLine}third line.{Environment.NewLine}", tv.Text);
  1242. Assert.Equal (3, tv.Lines);
  1243. Assert.Equal (new (0, 1), tv.CursorPosition);
  1244. Assert.True (tv.NewKeyDownEvent (Key.A));
  1245. Assert.Equal ($"This is the {Environment.NewLine}athird line.{Environment.NewLine}", tv.Text);
  1246. Assert.Equal (3, tv.Lines);
  1247. Assert.Equal (new (1, 1), tv.CursorPosition);
  1248. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1249. Assert.Equal ($"This is the {Environment.NewLine}third line.{Environment.NewLine}", tv.Text);
  1250. Assert.Equal (3, tv.Lines);
  1251. Assert.Equal (new (0, 1), tv.CursorPosition);
  1252. Assert.True (tv.IsDirty);
  1253. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1254. Assert.Equal ($"This is the {Environment.NewLine}athird line.{Environment.NewLine}", tv.Text);
  1255. Assert.Equal (3, tv.Lines);
  1256. Assert.Equal (new (1, 1), tv.CursorPosition);
  1257. }
  1258. [Fact]
  1259. [AutoInitShutdown]
  1260. public void HistoryText_Undo_Redo_Empty_Copy_Without_Selection_Multi_Line_Selected_Paste ()
  1261. {
  1262. var text = "\nThis is the first line.\nThis is the second line.";
  1263. var tv = new TextView { Text = text };
  1264. Assert.True (tv.NewKeyDownEvent (Key.C.WithCtrl));
  1265. Assert.Equal (
  1266. $"{Environment.NewLine}This is the first line.{Environment.NewLine}This is the second line.",
  1267. tv.Text
  1268. );
  1269. Assert.Equal ("", tv.SelectedText);
  1270. Assert.Equal (3, tv.Lines);
  1271. Assert.Equal (Point.Empty, tv.CursorPosition);
  1272. Assert.True (tv.NewKeyDownEvent (Key.Y.WithCtrl));
  1273. Assert.Equal (
  1274. $"{Environment.NewLine}{Environment.NewLine}This is the first line.{Environment.NewLine}This is the second line.",
  1275. tv.Text
  1276. );
  1277. Assert.Equal (4, tv.Lines);
  1278. Assert.Equal (new (0, 1), tv.CursorPosition);
  1279. // Undo
  1280. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1281. Assert.Equal (
  1282. $"{Environment.NewLine}This is the first line.{Environment.NewLine}This is the second line.",
  1283. tv.Text
  1284. );
  1285. Assert.Equal (3, tv.Lines);
  1286. Assert.Equal (Point.Empty, tv.CursorPosition);
  1287. // Redo
  1288. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1289. Assert.Equal (
  1290. $"{Environment.NewLine}{Environment.NewLine}This is the first line.{Environment.NewLine}This is the second line.",
  1291. tv.Text
  1292. );
  1293. Assert.Equal (4, tv.Lines);
  1294. Assert.Equal (new (0, 1), tv.CursorPosition);
  1295. }
  1296. [Fact]
  1297. public void HistoryText_Undo_Redo_Ending_With_Newline_Multi_Line_Selected_Almost_All_Return_And_InsertText ()
  1298. {
  1299. var text = "This is the first line.\nThis is the second line.\nThis is the third line.\n";
  1300. var tv = new TextView { Text = text };
  1301. tv.SelectionStartColumn = 12;
  1302. tv.CursorPosition = new (12, 2);
  1303. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  1304. Assert.Equal ($"This is the {Environment.NewLine}third line.{Environment.NewLine}", tv.Text);
  1305. Assert.Equal (3, tv.Lines);
  1306. Assert.Equal (new (0, 1), tv.CursorPosition);
  1307. Assert.True (tv.NewKeyDownEvent (Key.A));
  1308. Assert.Equal ($"This is the {Environment.NewLine}athird line.{Environment.NewLine}", tv.Text);
  1309. Assert.Equal (3, tv.Lines);
  1310. Assert.Equal (new (1, 1), tv.CursorPosition);
  1311. // Undo
  1312. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1313. Assert.Equal ($"This is the {Environment.NewLine}third line.{Environment.NewLine}", tv.Text);
  1314. Assert.Equal (3, tv.Lines);
  1315. Assert.Equal (new (0, 1), tv.CursorPosition);
  1316. Assert.True (tv.IsDirty);
  1317. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1318. Assert.Equal (
  1319. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.{Environment.NewLine}",
  1320. tv.Text
  1321. );
  1322. Assert.Equal (4, tv.Lines);
  1323. Assert.Equal (new (12, 2), tv.CursorPosition);
  1324. Assert.False (tv.IsDirty);
  1325. // Redo
  1326. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1327. Assert.Equal ($"This is the {Environment.NewLine}third line.{Environment.NewLine}", tv.Text);
  1328. Assert.Equal (3, tv.Lines);
  1329. Assert.Equal (new (0, 1), tv.CursorPosition);
  1330. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1331. Assert.Equal ($"This is the {Environment.NewLine}athird line.{Environment.NewLine}", tv.Text);
  1332. Assert.Equal (3, tv.Lines);
  1333. Assert.Equal (new (1, 1), tv.CursorPosition);
  1334. // Undo
  1335. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1336. Assert.Equal ($"This is the {Environment.NewLine}third line.{Environment.NewLine}", tv.Text);
  1337. Assert.Equal (3, tv.Lines);
  1338. Assert.Equal (new (0, 1), tv.CursorPosition);
  1339. Assert.True (tv.IsDirty);
  1340. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1341. Assert.Equal (
  1342. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.{Environment.NewLine}",
  1343. tv.Text
  1344. );
  1345. Assert.Equal (4, tv.Lines);
  1346. Assert.Equal (new (12, 2), tv.CursorPosition);
  1347. Assert.False (tv.IsDirty);
  1348. // Redo
  1349. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1350. Assert.Equal ($"This is the {Environment.NewLine}third line.{Environment.NewLine}", tv.Text);
  1351. Assert.Equal (3, tv.Lines);
  1352. Assert.Equal (new (0, 1), tv.CursorPosition);
  1353. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1354. Assert.Equal ($"This is the {Environment.NewLine}athird line.{Environment.NewLine}", tv.Text);
  1355. Assert.Equal (3, tv.Lines);
  1356. Assert.Equal (new (1, 1), tv.CursorPosition);
  1357. }
  1358. [Fact]
  1359. public void HistoryText_Undo_Redo_First_Line_Selected_Return_And_InsertText ()
  1360. {
  1361. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  1362. var tv = new TextView { Text = text };
  1363. tv.SelectionStartColumn = 12;
  1364. tv.CursorPosition = new (17, 0);
  1365. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  1366. Assert.Equal (
  1367. $"This is the {Environment.NewLine} line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1368. tv.Text
  1369. );
  1370. Assert.Equal (4, tv.Lines);
  1371. Assert.Equal (new (0, 1), tv.CursorPosition);
  1372. Assert.True (tv.NewKeyDownEvent (Key.A));
  1373. Assert.Equal (
  1374. $"This is the {Environment.NewLine}a line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1375. tv.Text
  1376. );
  1377. Assert.Equal (4, tv.Lines);
  1378. Assert.Equal (new (1, 1), tv.CursorPosition);
  1379. // Undo
  1380. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1381. Assert.Equal (
  1382. $"This is the {Environment.NewLine} line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1383. tv.Text
  1384. );
  1385. Assert.Equal (4, tv.Lines);
  1386. Assert.Equal (new (0, 1), tv.CursorPosition);
  1387. Assert.True (tv.IsDirty);
  1388. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1389. Assert.Equal (
  1390. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1391. tv.Text
  1392. );
  1393. Assert.Equal (3, tv.Lines);
  1394. Assert.Equal (new (17, 0), tv.CursorPosition);
  1395. Assert.False (tv.IsDirty);
  1396. // Redo
  1397. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1398. Assert.Equal (
  1399. $"This is the {Environment.NewLine} line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1400. tv.Text
  1401. );
  1402. Assert.Equal (4, tv.Lines);
  1403. Assert.Equal (new (0, 1), tv.CursorPosition);
  1404. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1405. Assert.Equal (
  1406. $"This is the {Environment.NewLine}a line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1407. tv.Text
  1408. );
  1409. Assert.Equal (4, tv.Lines);
  1410. Assert.Equal (new (1, 1), tv.CursorPosition);
  1411. // Undo
  1412. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1413. Assert.Equal (
  1414. $"This is the {Environment.NewLine} line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1415. tv.Text
  1416. );
  1417. Assert.Equal (4, tv.Lines);
  1418. Assert.Equal (new (0, 1), tv.CursorPosition);
  1419. Assert.True (tv.IsDirty);
  1420. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1421. Assert.Equal (
  1422. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1423. tv.Text
  1424. );
  1425. Assert.Equal (3, tv.Lines);
  1426. Assert.Equal (new (17, 0), tv.CursorPosition);
  1427. Assert.False (tv.IsDirty);
  1428. // Redo
  1429. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1430. Assert.Equal (
  1431. $"This is the {Environment.NewLine} line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1432. tv.Text
  1433. );
  1434. Assert.Equal (4, tv.Lines);
  1435. Assert.Equal (new (0, 1), tv.CursorPosition);
  1436. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1437. Assert.Equal (
  1438. $"This is the {Environment.NewLine}a line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1439. tv.Text
  1440. );
  1441. Assert.Equal (4, tv.Lines);
  1442. Assert.Equal (new (1, 1), tv.CursorPosition);
  1443. }
  1444. [Fact]
  1445. [AutoInitShutdown]
  1446. public void HistoryText_Undo_Redo_KillToEndOfLine ()
  1447. {
  1448. var text = "First line.\nSecond line.";
  1449. var tv = new TextView { Text = text };
  1450. Assert.True (tv.NewKeyDownEvent (Key.K.WithCtrl));
  1451. Assert.Equal ($"{Environment.NewLine}Second line.", tv.Text);
  1452. Assert.Equal ("", tv.SelectedText);
  1453. Assert.Equal ("First line.", Clipboard.Contents);
  1454. Assert.Equal (2, tv.Lines);
  1455. Assert.Equal (Point.Empty, tv.CursorPosition);
  1456. Assert.True (tv.NewKeyDownEvent (Key.K.WithCtrl));
  1457. Assert.Equal ("Second line.", tv.Text);
  1458. Assert.Equal ("", tv.SelectedText);
  1459. Assert.Equal ($"First line.{Environment.NewLine}", Clipboard.Contents);
  1460. Assert.Equal (1, tv.Lines);
  1461. Assert.Equal (Point.Empty, tv.CursorPosition);
  1462. Assert.True (tv.NewKeyDownEvent (Key.K.WithCtrl));
  1463. Assert.Equal ("", tv.Text);
  1464. Assert.Equal ("", tv.SelectedText);
  1465. Assert.Equal ($"First line.{Environment.NewLine}Second line.", Clipboard.Contents);
  1466. Assert.Equal (1, tv.Lines);
  1467. Assert.Equal (Point.Empty, tv.CursorPosition);
  1468. // Undo
  1469. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1470. Assert.Equal ("Second line.", tv.Text);
  1471. Assert.Equal (1, tv.Lines);
  1472. Assert.Equal (Point.Empty, tv.CursorPosition);
  1473. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1474. Assert.Equal ($"{Environment.NewLine}Second line.", tv.Text);
  1475. Assert.Equal (2, tv.Lines);
  1476. Assert.Equal (Point.Empty, tv.CursorPosition);
  1477. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1478. Assert.Equal ($"First line.{Environment.NewLine}Second line.", tv.Text);
  1479. Assert.Equal (2, tv.Lines);
  1480. Assert.Equal (Point.Empty, tv.CursorPosition);
  1481. // Redo
  1482. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1483. Assert.Equal ($"{Environment.NewLine}Second line.", tv.Text);
  1484. Assert.Equal (2, tv.Lines);
  1485. Assert.Equal (Point.Empty, tv.CursorPosition);
  1486. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1487. Assert.Equal ("Second line.", tv.Text);
  1488. Assert.Equal (1, tv.Lines);
  1489. Assert.Equal (Point.Empty, tv.CursorPosition);
  1490. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1491. Assert.Equal ("", tv.Text);
  1492. Assert.Equal (1, tv.Lines);
  1493. Assert.Equal (Point.Empty, tv.CursorPosition);
  1494. }
  1495. [Fact]
  1496. [AutoInitShutdown]
  1497. public void HistoryText_Undo_Redo_KillToLeftStart ()
  1498. {
  1499. var text = "First line.\nSecond line.";
  1500. var tv = new TextView { Text = text };
  1501. Assert.True (tv.NewKeyDownEvent (Key.End.WithCtrl));
  1502. Assert.Equal ($"First line.{Environment.NewLine}Second line.", tv.Text);
  1503. Assert.Equal ("", tv.SelectedText);
  1504. Assert.Equal (2, tv.Lines);
  1505. Assert.Equal (new (12, 1), tv.CursorPosition);
  1506. Assert.True (tv.NewKeyDownEvent (Key.Backspace.WithCtrl.WithShift));
  1507. Assert.Equal ($"First line.{Environment.NewLine}", tv.Text);
  1508. Assert.Equal ("", tv.SelectedText);
  1509. Assert.Equal ("Second line.", Clipboard.Contents);
  1510. Assert.Equal (2, tv.Lines);
  1511. Assert.Equal (new (0, 1), tv.CursorPosition);
  1512. Assert.True (tv.NewKeyDownEvent (Key.Backspace.WithCtrl.WithShift));
  1513. Assert.Equal ("First line.", tv.Text);
  1514. Assert.Equal ("", tv.SelectedText);
  1515. Assert.Equal ($"Second line.{Environment.NewLine}", Clipboard.Contents);
  1516. Assert.Equal (1, tv.Lines);
  1517. Assert.Equal (new (11, 0), tv.CursorPosition);
  1518. Assert.True (tv.NewKeyDownEvent (Key.Backspace.WithCtrl.WithShift));
  1519. Assert.Equal ("", tv.Text);
  1520. Assert.Equal ("", tv.SelectedText);
  1521. Assert.Equal ($"Second line.{Environment.NewLine}First line.", Clipboard.Contents);
  1522. Assert.Equal (1, tv.Lines);
  1523. Assert.Equal (Point.Empty, tv.CursorPosition);
  1524. // Undo
  1525. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1526. Assert.Equal ("First line.", tv.Text);
  1527. Assert.Equal (1, tv.Lines);
  1528. Assert.Equal (new (11, 0), tv.CursorPosition);
  1529. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1530. Assert.Equal ($"First line.{Environment.NewLine}", tv.Text);
  1531. Assert.Equal (2, tv.Lines);
  1532. Assert.Equal (new (0, 1), tv.CursorPosition);
  1533. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1534. Assert.Equal ($"First line.{Environment.NewLine}Second line.", tv.Text);
  1535. Assert.Equal (2, tv.Lines);
  1536. Assert.Equal (new (12, 1), tv.CursorPosition);
  1537. // Redo
  1538. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1539. Assert.Equal ($"First line.{Environment.NewLine}", tv.Text);
  1540. Assert.Equal (2, tv.Lines);
  1541. Assert.Equal (new (0, 1), tv.CursorPosition);
  1542. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1543. Assert.Equal ("First line.", tv.Text);
  1544. Assert.Equal (1, tv.Lines);
  1545. Assert.Equal (new (11, 0), tv.CursorPosition);
  1546. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1547. Assert.Equal ("", tv.Text);
  1548. Assert.Equal (1, tv.Lines);
  1549. Assert.Equal (Point.Empty, tv.CursorPosition);
  1550. }
  1551. [Fact]
  1552. public void HistoryText_Undo_Redo_KillWordBackward ()
  1553. {
  1554. var text = "First line.\nSecond line.";
  1555. var tv = new TextView { Text = text };
  1556. Assert.True (tv.NewKeyDownEvent (Key.End.WithCtrl));
  1557. Assert.Equal ($"First line.{Environment.NewLine}Second line.", tv.Text);
  1558. Assert.Equal ("", tv.SelectedText);
  1559. Assert.Equal (2, tv.Lines);
  1560. Assert.Equal (new (12, 1), tv.CursorPosition);
  1561. Assert.True (tv.NewKeyDownEvent (Key.Backspace.WithCtrl));
  1562. Assert.Equal ($"First line.{Environment.NewLine}Second line", tv.Text);
  1563. Assert.Equal ("", tv.SelectedText);
  1564. Assert.Equal (2, tv.Lines);
  1565. Assert.Equal (new (11, 1), tv.CursorPosition);
  1566. Assert.True (tv.NewKeyDownEvent (Key.Backspace.WithCtrl));
  1567. Assert.Equal ($"First line.{Environment.NewLine}Second ", tv.Text);
  1568. Assert.Equal ("", tv.SelectedText);
  1569. Assert.Equal (2, tv.Lines);
  1570. Assert.Equal (new (7, 1), tv.CursorPosition);
  1571. Assert.True (tv.NewKeyDownEvent (Key.Backspace.WithCtrl));
  1572. Assert.Equal ($"First line.{Environment.NewLine}", tv.Text);
  1573. Assert.Equal ("", tv.SelectedText);
  1574. Assert.Equal (2, tv.Lines);
  1575. Assert.Equal (new (0, 1), tv.CursorPosition);
  1576. Assert.True (tv.NewKeyDownEvent (Key.Backspace.WithCtrl));
  1577. Assert.Equal ("First line.", tv.Text);
  1578. Assert.Equal ("", tv.SelectedText);
  1579. Assert.Equal (1, tv.Lines);
  1580. Assert.Equal (new (11, 0), tv.CursorPosition);
  1581. Assert.True (tv.NewKeyDownEvent (Key.Backspace.WithCtrl));
  1582. Assert.Equal ("First line", tv.Text);
  1583. Assert.Equal ("", tv.SelectedText);
  1584. Assert.Equal (1, tv.Lines);
  1585. Assert.Equal (new (10, 0), tv.CursorPosition);
  1586. Assert.True (tv.NewKeyDownEvent (Key.Backspace.WithCtrl));
  1587. Assert.Equal ("First ", tv.Text);
  1588. Assert.Equal ("", tv.SelectedText);
  1589. Assert.Equal (1, tv.Lines);
  1590. Assert.Equal (new (6, 0), tv.CursorPosition);
  1591. Assert.True (tv.NewKeyDownEvent (Key.Backspace.WithCtrl));
  1592. Assert.Equal ("", tv.Text);
  1593. Assert.Equal ("", tv.SelectedText);
  1594. Assert.Equal (1, tv.Lines);
  1595. Assert.Equal (Point.Empty, tv.CursorPosition);
  1596. // Undo
  1597. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1598. Assert.Equal ("First ", tv.Text);
  1599. Assert.Equal (1, tv.Lines);
  1600. Assert.Equal (new (6, 0), tv.CursorPosition);
  1601. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1602. Assert.Equal ("First line", tv.Text);
  1603. Assert.Equal (1, tv.Lines);
  1604. Assert.Equal (new (10, 0), tv.CursorPosition);
  1605. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1606. Assert.Equal ("First line.", tv.Text);
  1607. Assert.Equal (1, tv.Lines);
  1608. Assert.Equal (new (11, 0), tv.CursorPosition);
  1609. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1610. Assert.Equal ($"First line.{Environment.NewLine}", tv.Text);
  1611. Assert.Equal (2, tv.Lines);
  1612. Assert.Equal (new (0, 1), tv.CursorPosition);
  1613. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1614. Assert.Equal ($"First line.{Environment.NewLine}Second ", tv.Text);
  1615. Assert.Equal (2, tv.Lines);
  1616. Assert.Equal (new (7, 1), tv.CursorPosition);
  1617. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1618. Assert.Equal ($"First line.{Environment.NewLine}Second line", tv.Text);
  1619. Assert.Equal (2, tv.Lines);
  1620. Assert.Equal (new (11, 1), tv.CursorPosition);
  1621. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1622. Assert.Equal ($"First line.{Environment.NewLine}Second line.", tv.Text);
  1623. Assert.Equal (2, tv.Lines);
  1624. Assert.Equal (new (12, 1), tv.CursorPosition);
  1625. // Redo
  1626. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1627. Assert.Equal ($"First line.{Environment.NewLine}Second line", tv.Text);
  1628. Assert.Equal (2, tv.Lines);
  1629. Assert.Equal (new (11, 1), tv.CursorPosition);
  1630. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1631. Assert.Equal ($"First line.{Environment.NewLine}Second ", tv.Text);
  1632. Assert.Equal (2, tv.Lines);
  1633. Assert.Equal (new (7, 1), tv.CursorPosition);
  1634. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1635. Assert.Equal ($"First line.{Environment.NewLine}", tv.Text);
  1636. Assert.Equal (2, tv.Lines);
  1637. Assert.Equal (new (0, 1), tv.CursorPosition);
  1638. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1639. Assert.Equal ("First line.", tv.Text);
  1640. Assert.Equal (1, tv.Lines);
  1641. Assert.Equal (new (11, 0), tv.CursorPosition);
  1642. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1643. Assert.Equal ("First line", tv.Text);
  1644. Assert.Equal (1, tv.Lines);
  1645. Assert.Equal (new (10, 0), tv.CursorPosition);
  1646. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1647. Assert.Equal ("First ", tv.Text);
  1648. Assert.Equal (1, tv.Lines);
  1649. Assert.Equal (new (6, 0), tv.CursorPosition);
  1650. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1651. Assert.Equal ("", tv.Text);
  1652. Assert.Equal (1, tv.Lines);
  1653. Assert.Equal (Point.Empty, tv.CursorPosition);
  1654. }
  1655. [Fact]
  1656. public void HistoryText_Undo_Redo_KillWordForward ()
  1657. {
  1658. var text = "First line.\nSecond line.";
  1659. var tv = new TextView { Text = text };
  1660. Assert.True (tv.NewKeyDownEvent (Key.Delete.WithCtrl));
  1661. Assert.Equal ($"line.{Environment.NewLine}Second line.", tv.Text);
  1662. Assert.Equal ("", tv.SelectedText);
  1663. Assert.Equal (2, tv.Lines);
  1664. Assert.Equal (Point.Empty, tv.CursorPosition);
  1665. Assert.True (tv.NewKeyDownEvent (Key.Delete.WithCtrl));
  1666. Assert.Equal ($"{Environment.NewLine}Second line.", tv.Text);
  1667. Assert.Equal (2, tv.Lines);
  1668. Assert.Equal (Point.Empty, tv.CursorPosition);
  1669. Assert.True (tv.NewKeyDownEvent (Key.Delete.WithCtrl));
  1670. Assert.Equal ("Second line.", tv.Text);
  1671. Assert.Equal (1, tv.Lines);
  1672. Assert.Equal (Point.Empty, tv.CursorPosition);
  1673. Assert.True (tv.NewKeyDownEvent (Key.Delete.WithCtrl));
  1674. Assert.Equal ("line.", tv.Text);
  1675. Assert.Equal (1, tv.Lines);
  1676. Assert.Equal (Point.Empty, tv.CursorPosition);
  1677. Assert.True (tv.NewKeyDownEvent (Key.Delete.WithCtrl));
  1678. Assert.Equal ("", tv.Text);
  1679. Assert.Equal (1, tv.Lines);
  1680. Assert.Equal (Point.Empty, tv.CursorPosition);
  1681. // Undo
  1682. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1683. Assert.Equal ("line.", tv.Text);
  1684. Assert.Equal (1, tv.Lines);
  1685. Assert.Equal (Point.Empty, tv.CursorPosition);
  1686. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1687. Assert.Equal ("Second line.", tv.Text);
  1688. Assert.Equal (1, tv.Lines);
  1689. Assert.Equal (Point.Empty, tv.CursorPosition);
  1690. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1691. Assert.Equal ($"{Environment.NewLine}Second line.", tv.Text);
  1692. Assert.Equal (2, tv.Lines);
  1693. Assert.Equal (Point.Empty, tv.CursorPosition);
  1694. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1695. Assert.Equal ($"line.{Environment.NewLine}Second line.", tv.Text);
  1696. Assert.Equal (2, tv.Lines);
  1697. Assert.Equal (Point.Empty, tv.CursorPosition);
  1698. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1699. Assert.Equal ($"First line.{Environment.NewLine}Second line.", tv.Text);
  1700. Assert.Equal (2, tv.Lines);
  1701. Assert.Equal (Point.Empty, tv.CursorPosition);
  1702. // Redo
  1703. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1704. Assert.Equal ($"line.{Environment.NewLine}Second line.", tv.Text);
  1705. Assert.Equal (2, tv.Lines);
  1706. Assert.Equal (Point.Empty, tv.CursorPosition);
  1707. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1708. Assert.Equal ($"{Environment.NewLine}Second line.", tv.Text);
  1709. Assert.Equal (2, tv.Lines);
  1710. Assert.Equal (Point.Empty, tv.CursorPosition);
  1711. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1712. Assert.Equal ("Second line.", tv.Text);
  1713. Assert.Equal (1, tv.Lines);
  1714. Assert.Equal (Point.Empty, tv.CursorPosition);
  1715. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1716. Assert.Equal ("line.", tv.Text);
  1717. Assert.Equal (1, tv.Lines);
  1718. Assert.Equal (Point.Empty, tv.CursorPosition);
  1719. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1720. Assert.Equal ("", tv.Text);
  1721. Assert.Equal (1, tv.Lines);
  1722. Assert.Equal (Point.Empty, tv.CursorPosition);
  1723. }
  1724. [Fact]
  1725. [AutoInitShutdown]
  1726. public void HistoryText_Undo_Redo_Multi_Line_DeleteCharLeft ()
  1727. {
  1728. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  1729. var tv = new TextView { Width = 10, Height = 2, Text = text };
  1730. Toplevel top = new ();
  1731. top.Add (tv);
  1732. Application.Begin (top);
  1733. Assert.Equal (
  1734. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1735. tv.Text
  1736. );
  1737. Assert.Equal (3, tv.Lines);
  1738. Assert.Equal (Point.Empty, tv.CursorPosition);
  1739. var ntimes = 3;
  1740. tv.CursorPosition = new (7, 1);
  1741. for (var i = 0; i < ntimes; i++)
  1742. {
  1743. tv.DeleteCharLeft ();
  1744. }
  1745. Assert.Equal (
  1746. $"This is the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  1747. tv.Text
  1748. );
  1749. Assert.Equal (3, tv.Lines);
  1750. Assert.Equal (new (4, 1), tv.CursorPosition);
  1751. tv.CursorPosition = new (7, 0);
  1752. for (var i = 0; i < ntimes; i++)
  1753. {
  1754. tv.DeleteCharLeft ();
  1755. }
  1756. Assert.Equal (
  1757. $"This the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  1758. tv.Text
  1759. );
  1760. Assert.Equal (3, tv.Lines);
  1761. Assert.Equal (new (4, 0), tv.CursorPosition);
  1762. tv.CursorPosition = new (7, 2);
  1763. for (var i = 0; i < ntimes; i++)
  1764. {
  1765. tv.DeleteCharLeft ();
  1766. }
  1767. Assert.Equal (
  1768. $"This the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This the third line.",
  1769. tv.Text
  1770. );
  1771. Assert.Equal (3, tv.Lines);
  1772. Assert.Equal (new (4, 2), tv.CursorPosition);
  1773. for (var i = 0; i < ntimes; i++)
  1774. {
  1775. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1776. switch (i)
  1777. {
  1778. case 0:
  1779. Assert.Equal (
  1780. $"This the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This the third line.",
  1781. tv.Text
  1782. );
  1783. Assert.Equal (new (5, 2), tv.CursorPosition);
  1784. break;
  1785. case 1:
  1786. Assert.Equal (
  1787. $"This the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This i the third line.",
  1788. tv.Text
  1789. );
  1790. Assert.Equal (new (6, 2), tv.CursorPosition);
  1791. break;
  1792. case 2:
  1793. Assert.Equal (
  1794. $"This the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  1795. tv.Text
  1796. );
  1797. Assert.Equal (new (7, 2), tv.CursorPosition);
  1798. break;
  1799. }
  1800. }
  1801. Assert.Equal (
  1802. $"This the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  1803. tv.Text
  1804. );
  1805. Assert.Equal (3, tv.Lines);
  1806. Assert.Equal (new (7, 2), tv.CursorPosition);
  1807. for (var i = 0; i < ntimes; i++)
  1808. {
  1809. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1810. switch (i)
  1811. {
  1812. case 0:
  1813. Assert.Equal (
  1814. $"This the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  1815. tv.Text
  1816. );
  1817. Assert.Equal (new (5, 0), tv.CursorPosition);
  1818. break;
  1819. case 1:
  1820. Assert.Equal (
  1821. $"This i the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  1822. tv.Text
  1823. );
  1824. Assert.Equal (new (6, 0), tv.CursorPosition);
  1825. break;
  1826. case 2:
  1827. Assert.Equal (
  1828. $"This is the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  1829. tv.Text
  1830. );
  1831. Assert.Equal (new (7, 0), tv.CursorPosition);
  1832. break;
  1833. }
  1834. }
  1835. Assert.Equal (
  1836. $"This is the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  1837. tv.Text
  1838. );
  1839. Assert.Equal (3, tv.Lines);
  1840. Assert.Equal (new (7, 0), tv.CursorPosition);
  1841. for (var i = 0; i < ntimes; i++)
  1842. {
  1843. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1844. switch (i)
  1845. {
  1846. case 0:
  1847. Assert.Equal (
  1848. $"This is the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  1849. tv.Text
  1850. );
  1851. Assert.Equal (new (5, 1), tv.CursorPosition);
  1852. break;
  1853. case 1:
  1854. Assert.Equal (
  1855. $"This is the first line.{Environment.NewLine}This i the second line.{Environment.NewLine}This is the third line.",
  1856. tv.Text
  1857. );
  1858. Assert.Equal (new (6, 1), tv.CursorPosition);
  1859. break;
  1860. case 2:
  1861. Assert.Equal (
  1862. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1863. tv.Text
  1864. );
  1865. Assert.Equal (new (7, 1), tv.CursorPosition);
  1866. break;
  1867. }
  1868. }
  1869. Assert.Equal (
  1870. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1871. tv.Text
  1872. );
  1873. Assert.Equal (3, tv.Lines);
  1874. Assert.Equal (new (7, 1), tv.CursorPosition);
  1875. for (var i = 0; i < ntimes; i++)
  1876. {
  1877. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1878. }
  1879. Assert.Equal (
  1880. $"This is the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  1881. tv.Text
  1882. );
  1883. Assert.Equal (3, tv.Lines);
  1884. Assert.Equal (new (4, 1), tv.CursorPosition);
  1885. for (var i = 0; i < ntimes; i++)
  1886. {
  1887. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1888. }
  1889. Assert.Equal (
  1890. $"This the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  1891. tv.Text
  1892. );
  1893. Assert.Equal (3, tv.Lines);
  1894. Assert.Equal (new (4, 0), tv.CursorPosition);
  1895. for (var i = 0; i < ntimes; i++)
  1896. {
  1897. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1898. }
  1899. Assert.Equal (
  1900. $"This the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This the third line.",
  1901. tv.Text
  1902. );
  1903. Assert.Equal (3, tv.Lines);
  1904. Assert.Equal (new (4, 2), tv.CursorPosition);
  1905. top.Dispose ();
  1906. }
  1907. [Fact]
  1908. [AutoInitShutdown]
  1909. public void HistoryText_Undo_Redo_Multi_Line_DeleteCharRight ()
  1910. {
  1911. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  1912. var tv = new TextView { Width = 10, Height = 2, Text = text };
  1913. Toplevel top = new ();
  1914. top.Add (tv);
  1915. Application.Begin (top);
  1916. Assert.Equal (
  1917. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1918. tv.Text
  1919. );
  1920. Assert.Equal (3, tv.Lines);
  1921. Assert.Equal (Point.Empty, tv.CursorPosition);
  1922. var ntimes = 3;
  1923. tv.CursorPosition = new (7, 1);
  1924. for (var i = 0; i < ntimes; i++)
  1925. {
  1926. tv.DeleteCharRight ();
  1927. }
  1928. Assert.Equal (
  1929. $"This is the first line.{Environment.NewLine}This ise second line.{Environment.NewLine}This is the third line.",
  1930. tv.Text
  1931. );
  1932. Assert.Equal (3, tv.Lines);
  1933. Assert.Equal (new (7, 1), tv.CursorPosition);
  1934. tv.CursorPosition = new (7, 0);
  1935. for (var i = 0; i < ntimes; i++)
  1936. {
  1937. tv.DeleteCharRight ();
  1938. }
  1939. Assert.Equal (
  1940. $"This ise first line.{Environment.NewLine}This ise second line.{Environment.NewLine}This is the third line.",
  1941. tv.Text
  1942. );
  1943. Assert.Equal (3, tv.Lines);
  1944. Assert.Equal (new (7, 0), tv.CursorPosition);
  1945. tv.CursorPosition = new (7, 2);
  1946. for (var i = 0; i < ntimes; i++)
  1947. {
  1948. tv.DeleteCharRight ();
  1949. }
  1950. Assert.Equal (
  1951. $"This ise first line.{Environment.NewLine}This ise second line.{Environment.NewLine}This ise third line.",
  1952. tv.Text
  1953. );
  1954. Assert.Equal (3, tv.Lines);
  1955. Assert.Equal (new (7, 2), tv.CursorPosition);
  1956. for (var i = 0; i < ntimes; i++)
  1957. {
  1958. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1959. }
  1960. Assert.Equal (
  1961. $"This ise first line.{Environment.NewLine}This ise second line.{Environment.NewLine}This is the third line.",
  1962. tv.Text
  1963. );
  1964. Assert.Equal (3, tv.Lines);
  1965. Assert.Equal (new (7, 2), tv.CursorPosition);
  1966. for (var i = 0; i < ntimes; i++)
  1967. {
  1968. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1969. }
  1970. Assert.Equal (
  1971. $"This is the first line.{Environment.NewLine}This ise second line.{Environment.NewLine}This is the third line.",
  1972. tv.Text
  1973. );
  1974. Assert.Equal (3, tv.Lines);
  1975. Assert.Equal (new (7, 0), tv.CursorPosition);
  1976. for (var i = 0; i < ntimes; i++)
  1977. {
  1978. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  1979. }
  1980. Assert.Equal (
  1981. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  1982. tv.Text
  1983. );
  1984. Assert.Equal (3, tv.Lines);
  1985. Assert.Equal (new (7, 1), tv.CursorPosition);
  1986. for (var i = 0; i < ntimes; i++)
  1987. {
  1988. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1989. }
  1990. Assert.Equal (
  1991. $"This is the first line.{Environment.NewLine}This ise second line.{Environment.NewLine}This is the third line.",
  1992. tv.Text
  1993. );
  1994. Assert.Equal (3, tv.Lines);
  1995. Assert.Equal (new (7, 1), tv.CursorPosition);
  1996. for (var i = 0; i < ntimes; i++)
  1997. {
  1998. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  1999. }
  2000. Assert.Equal (
  2001. $"This ise first line.{Environment.NewLine}This ise second line.{Environment.NewLine}This is the third line.",
  2002. tv.Text
  2003. );
  2004. Assert.Equal (3, tv.Lines);
  2005. Assert.Equal (new (7, 0), tv.CursorPosition);
  2006. for (var i = 0; i < ntimes; i++)
  2007. {
  2008. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2009. }
  2010. Assert.Equal (
  2011. $"This ise first line.{Environment.NewLine}This ise second line.{Environment.NewLine}This ise third line.",
  2012. tv.Text
  2013. );
  2014. Assert.Equal (3, tv.Lines);
  2015. Assert.Equal (new (7, 2), tv.CursorPosition);
  2016. top.Dispose ();
  2017. }
  2018. [Fact]
  2019. [AutoInitShutdown]
  2020. public void HistoryText_Undo_Redo_Multi_Line_InsertText ()
  2021. {
  2022. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  2023. var tv = new TextView { Width = 10, Height = 2, Text = text };
  2024. Toplevel top = new ();
  2025. top.Add (tv);
  2026. Application.Begin (top);
  2027. Assert.Equal (
  2028. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2029. tv.Text
  2030. );
  2031. Assert.Equal (3, tv.Lines);
  2032. Assert.Equal (Point.Empty, tv.CursorPosition);
  2033. var messy = " messy";
  2034. tv.CursorPosition = new (7, 1);
  2035. tv.InsertText (messy);
  2036. Assert.Equal (
  2037. $"This is the first line.{Environment.NewLine}This is messy the second line.{Environment.NewLine}This is the third line.",
  2038. tv.Text
  2039. );
  2040. Assert.Equal (3, tv.Lines);
  2041. Assert.Equal (new (13, 1), tv.CursorPosition);
  2042. tv.CursorPosition = new (7, 0);
  2043. tv.InsertText (messy);
  2044. Assert.Equal (
  2045. $"This is messy the first line.{Environment.NewLine}This is messy the second line.{Environment.NewLine}This is the third line.",
  2046. tv.Text
  2047. );
  2048. Assert.Equal (3, tv.Lines);
  2049. Assert.Equal (new (13, 0), tv.CursorPosition);
  2050. tv.CursorPosition = new (7, 2);
  2051. tv.InsertText (messy);
  2052. Assert.Equal (
  2053. $"This is messy the first line.{Environment.NewLine}This is messy the second line.{Environment.NewLine}This is messy the third line.",
  2054. tv.Text
  2055. );
  2056. Assert.Equal (3, tv.Lines);
  2057. Assert.Equal (new (13, 2), tv.CursorPosition);
  2058. for (var i = 0; i < messy.Length; i++)
  2059. {
  2060. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2061. }
  2062. Assert.Equal (
  2063. $"This is messy the first line.{Environment.NewLine}This is messy the second line.{Environment.NewLine}This is the third line.",
  2064. tv.Text
  2065. );
  2066. Assert.Equal (3, tv.Lines);
  2067. Assert.Equal (new (7, 2), tv.CursorPosition);
  2068. for (var i = 0; i < messy.Length; i++)
  2069. {
  2070. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2071. }
  2072. Assert.Equal (
  2073. $"This is the first line.{Environment.NewLine}This is messy the second line.{Environment.NewLine}This is the third line.",
  2074. tv.Text
  2075. );
  2076. Assert.Equal (3, tv.Lines);
  2077. Assert.Equal (new (7, 0), tv.CursorPosition);
  2078. for (var i = 0; i < messy.Length; i++)
  2079. {
  2080. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2081. }
  2082. Assert.Equal (
  2083. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2084. tv.Text
  2085. );
  2086. Assert.Equal (3, tv.Lines);
  2087. Assert.Equal (new (7, 1), tv.CursorPosition);
  2088. for (var i = 0; i < messy.Length; i++)
  2089. {
  2090. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2091. }
  2092. Assert.Equal (
  2093. $"This is the first line.{Environment.NewLine}This is messy the second line.{Environment.NewLine}This is the third line.",
  2094. tv.Text
  2095. );
  2096. Assert.Equal (3, tv.Lines);
  2097. Assert.Equal (new (13, 1), tv.CursorPosition);
  2098. for (var i = 0; i < messy.Length; i++)
  2099. {
  2100. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2101. }
  2102. Assert.Equal (
  2103. $"This is messy the first line.{Environment.NewLine}This is messy the second line.{Environment.NewLine}This is the third line.",
  2104. tv.Text
  2105. );
  2106. Assert.Equal (3, tv.Lines);
  2107. Assert.Equal (new (13, 0), tv.CursorPosition);
  2108. for (var i = 0; i < messy.Length; i++)
  2109. {
  2110. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2111. }
  2112. Assert.Equal (
  2113. $"This is messy the first line.{Environment.NewLine}This is messy the second line.{Environment.NewLine}This is messy the third line.",
  2114. tv.Text
  2115. );
  2116. Assert.Equal (3, tv.Lines);
  2117. Assert.Equal (new (13, 2), tv.CursorPosition);
  2118. top.Dispose ();
  2119. }
  2120. [Fact]
  2121. public void HistoryText_Undo_Redo_Multi_Line_Selected_All_Return_And_InsertText ()
  2122. {
  2123. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  2124. var tv = new TextView { Text = text };
  2125. Assert.True (tv.NewKeyDownEvent (Key.End.WithCtrl.WithShift));
  2126. Assert.Equal (
  2127. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2128. tv.Text
  2129. );
  2130. Assert.Equal (3, tv.Lines);
  2131. Assert.Equal (new (23, 2), tv.CursorPosition);
  2132. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  2133. Assert.Equal ($"{Environment.NewLine}", tv.Text);
  2134. Assert.Equal (2, tv.Lines);
  2135. Assert.Equal (new (0, 1), tv.CursorPosition);
  2136. Assert.True (tv.NewKeyDownEvent (Key.A));
  2137. Assert.Equal ($"{Environment.NewLine}a", tv.Text);
  2138. Assert.Equal (2, tv.Lines);
  2139. Assert.Equal (new (1, 1), tv.CursorPosition);
  2140. // Undo
  2141. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2142. Assert.Equal ($"{Environment.NewLine}", tv.Text);
  2143. Assert.Equal (2, tv.Lines);
  2144. Assert.Equal (new (0, 1), tv.CursorPosition);
  2145. Assert.True (tv.IsDirty);
  2146. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2147. Assert.Equal (
  2148. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2149. tv.Text
  2150. );
  2151. Assert.Equal (3, tv.Lines);
  2152. Assert.Equal (new (23, 2), tv.CursorPosition);
  2153. Assert.False (tv.IsDirty);
  2154. // Redo
  2155. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2156. Assert.Equal ($"{Environment.NewLine}", tv.Text);
  2157. Assert.Equal (2, tv.Lines);
  2158. Assert.Equal (new (0, 1), tv.CursorPosition);
  2159. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2160. Assert.Equal ($"{Environment.NewLine}a", tv.Text);
  2161. Assert.Equal (2, tv.Lines);
  2162. Assert.Equal (new (1, 1), tv.CursorPosition);
  2163. // Undo
  2164. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2165. Assert.Equal ($"{Environment.NewLine}", tv.Text);
  2166. Assert.Equal (2, tv.Lines);
  2167. Assert.Equal (new (0, 1), tv.CursorPosition);
  2168. Assert.True (tv.IsDirty);
  2169. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2170. Assert.Equal (
  2171. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2172. tv.Text
  2173. );
  2174. Assert.Equal (3, tv.Lines);
  2175. Assert.Equal (new (23, 2), tv.CursorPosition);
  2176. Assert.False (tv.IsDirty);
  2177. // Redo
  2178. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2179. Assert.Equal ($"{Environment.NewLine}", tv.Text);
  2180. Assert.Equal (2, tv.Lines);
  2181. Assert.Equal (new (0, 1), tv.CursorPosition);
  2182. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2183. Assert.Equal ($"{Environment.NewLine}a", tv.Text);
  2184. Assert.Equal (2, tv.Lines);
  2185. Assert.Equal (new (1, 1), tv.CursorPosition);
  2186. }
  2187. [Fact]
  2188. [AutoInitShutdown]
  2189. public void HistoryText_Undo_Redo_Multi_Line_Selected_Copy_Simple_Paste_Starting_On_Letter ()
  2190. {
  2191. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  2192. var tv = new TextView { Text = text };
  2193. tv.SelectionStartColumn = 12;
  2194. tv.CursorPosition = new (18, 1);
  2195. Assert.True (tv.NewKeyDownEvent (Key.C.WithCtrl));
  2196. Assert.Equal (
  2197. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2198. tv.Text
  2199. );
  2200. Assert.Equal ($"first line.{Environment.NewLine}This is the second", tv.SelectedText);
  2201. Assert.Equal (3, tv.Lines);
  2202. Assert.Equal (new (18, 1), tv.CursorPosition);
  2203. tv.IsSelecting = false;
  2204. tv.CursorPosition = new (17, 1);
  2205. Assert.True (tv.NewKeyDownEvent (Key.Y.WithCtrl));
  2206. Assert.Equal (
  2207. $"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.",
  2208. tv.Text
  2209. );
  2210. Assert.Equal (4, tv.Lines);
  2211. Assert.Equal (new (18, 2), tv.CursorPosition);
  2212. // Undo
  2213. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2214. Assert.Equal (
  2215. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2216. tv.Text
  2217. );
  2218. Assert.Equal (3, tv.Lines);
  2219. Assert.Equal (new (17, 1), tv.CursorPosition);
  2220. // Redo
  2221. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2222. Assert.Equal (
  2223. $"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.",
  2224. tv.Text
  2225. );
  2226. Assert.Equal (4, tv.Lines);
  2227. Assert.Equal (new (18, 2), tv.CursorPosition);
  2228. }
  2229. [Fact]
  2230. [AutoInitShutdown (useFakeClipboard: true)]
  2231. public void HistoryText_Undo_Redo_Multi_Line_Selected_Copy_Simple_Paste_Starting_On_Space ()
  2232. {
  2233. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  2234. var tv = new TextView { Text = text };
  2235. tv.SelectionStartColumn = 12;
  2236. tv.CursorPosition = new (18, 1);
  2237. Assert.True (tv.NewKeyDownEvent (Key.C.WithCtrl));
  2238. Assert.Equal (
  2239. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2240. tv.Text
  2241. );
  2242. Assert.Equal ($"first line.{Environment.NewLine}This is the second", tv.SelectedText);
  2243. Assert.Equal (3, tv.Lines);
  2244. Assert.Equal (new (18, 1), tv.CursorPosition);
  2245. tv.IsSelecting = false;
  2246. Assert.True (tv.NewKeyDownEvent (Key.Y.WithCtrl));
  2247. Assert.Equal (
  2248. $"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.",
  2249. tv.Text
  2250. );
  2251. Assert.Equal (4, tv.Lines);
  2252. Assert.Equal (new (18, 2), tv.CursorPosition);
  2253. // Undo
  2254. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2255. Assert.Equal (
  2256. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2257. tv.Text
  2258. );
  2259. Assert.Equal (3, tv.Lines);
  2260. Assert.Equal (new (18, 1), tv.CursorPosition);
  2261. // Redo
  2262. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2263. Assert.Equal (
  2264. $"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.",
  2265. tv.Text
  2266. );
  2267. Assert.Equal (4, tv.Lines);
  2268. Assert.Equal (new (18, 2), tv.CursorPosition);
  2269. }
  2270. [Fact]
  2271. public void HistoryText_Undo_Redo_Multi_Line_Selected_DeleteCharLeft_All ()
  2272. {
  2273. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  2274. var tv = new TextView { Text = text };
  2275. Assert.Equal (
  2276. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2277. tv.Text
  2278. );
  2279. Assert.Equal (3, tv.Lines);
  2280. Assert.Equal (Point.Empty, tv.CursorPosition);
  2281. Assert.False (tv.IsDirty);
  2282. Assert.False (tv.HasHistoryChanges);
  2283. Assert.True (tv.NewKeyDownEvent (Key.End.WithCtrl.WithShift));
  2284. Assert.Equal (
  2285. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2286. tv.Text
  2287. );
  2288. Assert.Equal (
  2289. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2290. tv.SelectedText
  2291. );
  2292. Assert.Equal (3, tv.Lines);
  2293. Assert.Equal (new (23, 2), tv.CursorPosition);
  2294. Assert.Equal (70 + Environment.NewLine.Length * 2, tv.SelectedLength);
  2295. Assert.False (tv.IsDirty);
  2296. Assert.False (tv.HasHistoryChanges);
  2297. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  2298. Assert.Equal ("", tv.Text);
  2299. Assert.Equal ("", tv.SelectedText);
  2300. Assert.Equal (1, tv.Lines);
  2301. Assert.Equal (Point.Empty, tv.CursorPosition);
  2302. Assert.Equal (0, tv.SelectedLength);
  2303. Assert.True (tv.IsDirty);
  2304. Assert.True (tv.HasHistoryChanges);
  2305. // Undo
  2306. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2307. Assert.Equal (
  2308. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2309. tv.Text
  2310. );
  2311. Assert.Equal ("", tv.SelectedText);
  2312. Assert.Equal (3, tv.Lines);
  2313. Assert.Equal (new (23, 2), tv.CursorPosition);
  2314. Assert.Equal (0, tv.SelectedLength);
  2315. Assert.False (tv.IsDirty);
  2316. Assert.True (tv.HasHistoryChanges);
  2317. // Redo
  2318. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2319. Assert.Equal ("", tv.Text);
  2320. Assert.Equal ("", tv.SelectedText);
  2321. Assert.Equal (1, tv.Lines);
  2322. Assert.Equal (Point.Empty, tv.CursorPosition);
  2323. Assert.Equal (0, tv.SelectedLength);
  2324. Assert.True (tv.IsDirty);
  2325. Assert.True (tv.HasHistoryChanges);
  2326. }
  2327. [Fact]
  2328. public void HistoryText_Undo_Redo_Multi_Line_Selected_DeleteCharRight_All ()
  2329. {
  2330. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  2331. var tv = new TextView { Text = text };
  2332. Assert.Equal (
  2333. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2334. tv.Text
  2335. );
  2336. Assert.Equal (3, tv.Lines);
  2337. Assert.Equal (Point.Empty, tv.CursorPosition);
  2338. Assert.False (tv.IsDirty);
  2339. Assert.False (tv.HasHistoryChanges);
  2340. Assert.True (tv.NewKeyDownEvent (Key.End.WithCtrl.WithShift));
  2341. Assert.Equal (
  2342. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2343. tv.Text
  2344. );
  2345. Assert.Equal (
  2346. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2347. tv.SelectedText
  2348. );
  2349. Assert.Equal (3, tv.Lines);
  2350. Assert.Equal (new (23, 2), tv.CursorPosition);
  2351. Assert.Equal (70 + Environment.NewLine.Length * 2, tv.SelectedLength);
  2352. Assert.False (tv.IsDirty);
  2353. Assert.False (tv.HasHistoryChanges);
  2354. Assert.True (tv.NewKeyDownEvent (Key.Delete));
  2355. Assert.Equal ("", tv.Text);
  2356. Assert.Equal ("", tv.SelectedText);
  2357. Assert.Equal (1, tv.Lines);
  2358. Assert.Equal (Point.Empty, tv.CursorPosition);
  2359. Assert.Equal (0, tv.SelectedLength);
  2360. Assert.True (tv.IsDirty);
  2361. Assert.True (tv.HasHistoryChanges);
  2362. // Undo
  2363. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2364. Assert.Equal (
  2365. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2366. tv.Text
  2367. );
  2368. Assert.Equal ("", tv.SelectedText);
  2369. Assert.Equal (3, tv.Lines);
  2370. Assert.Equal (new (23, 2), tv.CursorPosition);
  2371. Assert.Equal (0, tv.SelectedLength);
  2372. Assert.False (tv.IsDirty);
  2373. Assert.True (tv.HasHistoryChanges);
  2374. // Redo
  2375. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2376. Assert.Equal ("", tv.Text);
  2377. Assert.Equal ("", tv.SelectedText);
  2378. Assert.Equal (1, tv.Lines);
  2379. Assert.Equal (Point.Empty, tv.CursorPosition);
  2380. Assert.Equal (0, tv.SelectedLength);
  2381. Assert.True (tv.IsDirty);
  2382. Assert.True (tv.HasHistoryChanges);
  2383. }
  2384. [Fact]
  2385. [AutoInitShutdown]
  2386. public void HistoryText_Undo_Redo_Multi_Line_Selected_InsertText ()
  2387. {
  2388. var text =
  2389. $"This is the first line.{Environment.NewLine}This is the second line.\nThis is the third line.";
  2390. var tv = new TextView { Width = 10, Height = 2, Text = text };
  2391. Toplevel top = new ();
  2392. top.Add (tv);
  2393. Application.Begin (top);
  2394. Assert.Equal (
  2395. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2396. tv.Text
  2397. );
  2398. Assert.Equal (3, tv.Lines);
  2399. Assert.Equal (Point.Empty, tv.CursorPosition);
  2400. var messy = " messy";
  2401. tv.CursorPosition = new (7, 0);
  2402. tv.SelectionStartColumn = 11;
  2403. tv.SelectionStartRow = 2;
  2404. Assert.Equal (51 + Environment.NewLine.Length * 2, tv.SelectedLength);
  2405. for (var i = 0; i < messy.Length; i++)
  2406. {
  2407. tv.InsertText (messy [i].ToString ());
  2408. switch (i)
  2409. {
  2410. case 0:
  2411. Assert.Equal ("This is third line.", tv.Text);
  2412. Assert.Equal (new (8, 0), tv.CursorPosition);
  2413. break;
  2414. case 1:
  2415. Assert.Equal ("This is m third line.", tv.Text);
  2416. Assert.Equal (new (9, 0), tv.CursorPosition);
  2417. break;
  2418. case 2:
  2419. Assert.Equal ("This is me third line.", tv.Text);
  2420. Assert.Equal (new (10, 0), tv.CursorPosition);
  2421. break;
  2422. case 3:
  2423. Assert.Equal ("This is mes third line.", tv.Text);
  2424. Assert.Equal (new (11, 0), tv.CursorPosition);
  2425. break;
  2426. case 4:
  2427. Assert.Equal ("This is mess third line.", tv.Text);
  2428. Assert.Equal (new (12, 0), tv.CursorPosition);
  2429. break;
  2430. case 5:
  2431. Assert.Equal ("This is messy third line.", tv.Text);
  2432. Assert.Equal (new (13, 0), tv.CursorPosition);
  2433. break;
  2434. }
  2435. }
  2436. Assert.Equal ("This is messy third line.", tv.Text);
  2437. Assert.Equal (1, tv.Lines);
  2438. Assert.Equal (new (13, 0), tv.CursorPosition);
  2439. Assert.Equal (11, tv.SelectionStartColumn);
  2440. Assert.Equal (2, tv.SelectionStartRow);
  2441. Assert.Equal (0, tv.SelectedLength);
  2442. for (var i = 0; i < messy.Length; i++)
  2443. {
  2444. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2445. switch (i)
  2446. {
  2447. case 0:
  2448. Assert.Equal ("This is mess third line.", tv.Text);
  2449. Assert.Equal (new (12, 0), tv.CursorPosition);
  2450. break;
  2451. case 1:
  2452. Assert.Equal ("This is mes third line.", tv.Text);
  2453. Assert.Equal (new (11, 0), tv.CursorPosition);
  2454. break;
  2455. case 2:
  2456. Assert.Equal ("This is me third line.", tv.Text);
  2457. Assert.Equal (new (10, 0), tv.CursorPosition);
  2458. break;
  2459. case 3:
  2460. Assert.Equal ("This is m third line.", tv.Text);
  2461. Assert.Equal (new (9, 0), tv.CursorPosition);
  2462. break;
  2463. case 4:
  2464. Assert.Equal ("This is third line.", tv.Text);
  2465. Assert.Equal (new (8, 0), tv.CursorPosition);
  2466. break;
  2467. case 5:
  2468. Assert.Equal (
  2469. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2470. tv.Text
  2471. );
  2472. Assert.Equal (new (7, 0), tv.CursorPosition);
  2473. break;
  2474. }
  2475. }
  2476. Assert.Equal (
  2477. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  2478. tv.Text
  2479. );
  2480. Assert.Equal (3, tv.Lines);
  2481. Assert.Equal (new (7, 0), tv.CursorPosition);
  2482. Assert.Equal (11, tv.SelectionStartColumn);
  2483. Assert.Equal (2, tv.SelectionStartRow);
  2484. Assert.Equal (0, tv.SelectedLength);
  2485. for (var i = 0; i < messy.Length; i++)
  2486. {
  2487. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2488. switch (i)
  2489. {
  2490. case 0:
  2491. Assert.Equal ("This is third line.", tv.Text);
  2492. Assert.Equal (new (8, 0), tv.CursorPosition);
  2493. break;
  2494. case 1:
  2495. Assert.Equal ("This is m third line.", tv.Text);
  2496. Assert.Equal (new (9, 0), tv.CursorPosition);
  2497. break;
  2498. case 2:
  2499. Assert.Equal ("This is me third line.", tv.Text);
  2500. Assert.Equal (new (10, 0), tv.CursorPosition);
  2501. break;
  2502. case 3:
  2503. Assert.Equal ("This is mes third line.", tv.Text);
  2504. Assert.Equal (new (11, 0), tv.CursorPosition);
  2505. break;
  2506. case 4:
  2507. Assert.Equal ("This is mess third line.", tv.Text);
  2508. Assert.Equal (new (12, 0), tv.CursorPosition);
  2509. break;
  2510. case 5:
  2511. Assert.Equal ("This is messy third line.", tv.Text);
  2512. Assert.Equal (new (13, 0), tv.CursorPosition);
  2513. break;
  2514. }
  2515. }
  2516. Assert.Equal ("This is messy third line.", tv.Text);
  2517. Assert.Equal (1, tv.Lines);
  2518. Assert.Equal (new (13, 0), tv.CursorPosition);
  2519. Assert.Equal (11, tv.SelectionStartColumn);
  2520. Assert.Equal (2, tv.SelectionStartRow);
  2521. Assert.Equal (0, tv.SelectedLength);
  2522. top.Dispose ();
  2523. }
  2524. [Fact]
  2525. [AutoInitShutdown]
  2526. public void HistoryText_Undo_Redo_Multi_Line_Selected_InsertText_Twice_On_Same_Line ()
  2527. {
  2528. var text = "One\nTwo\nThree";
  2529. var tv = new TextView { Width = 10, Height = 2, Text = text };
  2530. Toplevel top = new ();
  2531. top.Add (tv);
  2532. Application.Begin (top);
  2533. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three", tv.Text);
  2534. Assert.Equal (3, tv.Lines);
  2535. Assert.Equal (Point.Empty, tv.CursorPosition);
  2536. Assert.False (tv.IsDirty);
  2537. tv.SelectionStartColumn = 0;
  2538. tv.SelectionStartRow = 0;
  2539. tv.CursorPosition = new (0, 1);
  2540. Assert.Equal (3 + Environment.NewLine.Length, tv.SelectedLength);
  2541. Assert.True (tv.NewKeyDownEvent (Key.D1));
  2542. Assert.Equal ($"1Two{Environment.NewLine}Three", tv.Text);
  2543. Assert.Equal (new (1, 0), tv.CursorPosition);
  2544. Assert.Equal (0, tv.SelectedLength);
  2545. Assert.True (tv.IsDirty);
  2546. tv.SelectionStartColumn = 1;
  2547. tv.SelectionStartRow = 0;
  2548. tv.CursorPosition = new (1, 1);
  2549. Assert.Equal (4 + Environment.NewLine.Length, tv.SelectedLength);
  2550. Assert.True (tv.NewKeyDownEvent (Key.D2));
  2551. Assert.Equal ("12hree", tv.Text);
  2552. Assert.Equal (new (2, 0), tv.CursorPosition);
  2553. Assert.Equal (0, tv.SelectedLength);
  2554. Assert.True (tv.IsDirty);
  2555. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2556. Assert.Equal ($"1Two{Environment.NewLine}Three", tv.Text);
  2557. Assert.Equal (new (1, 1), tv.CursorPosition);
  2558. Assert.Equal (0, tv.SelectedLength);
  2559. Assert.True (tv.IsDirty);
  2560. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2561. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three", tv.Text);
  2562. Assert.Equal (3, tv.Lines);
  2563. Assert.Equal (new (0, 1), tv.CursorPosition);
  2564. Assert.Equal (0, tv.SelectedLength);
  2565. Assert.False (tv.IsDirty);
  2566. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2567. Assert.Equal ($"1Two{Environment.NewLine}Three", tv.Text);
  2568. Assert.Equal (new (1, 0), tv.CursorPosition);
  2569. Assert.Equal (0, tv.SelectedLength);
  2570. Assert.True (tv.IsDirty);
  2571. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2572. Assert.Equal ("12hree", tv.Text);
  2573. Assert.Equal (new (2, 0), tv.CursorPosition);
  2574. Assert.Equal (0, tv.SelectedLength);
  2575. Assert.True (tv.IsDirty);
  2576. top.Dispose ();
  2577. }
  2578. [Fact]
  2579. [AutoInitShutdown]
  2580. public void HistoryText_Undo_Redo_Multi_Line_Selected_InsertText_Twice_On_Same_Line_With_End_Line ()
  2581. {
  2582. var text = "One\nTwo\nThree\n";
  2583. var tv = new TextView { Width = 10, Height = 2, Text = text };
  2584. Toplevel top = new ();
  2585. top.Add (tv);
  2586. Application.Begin (top);
  2587. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  2588. Assert.Equal (4, tv.Lines);
  2589. Assert.Equal (Point.Empty, tv.CursorPosition);
  2590. Assert.False (tv.IsDirty);
  2591. tv.SelectionStartColumn = 0;
  2592. tv.SelectionStartRow = 0;
  2593. tv.CursorPosition = new (0, 1);
  2594. Assert.Equal (3 + Environment.NewLine.Length, tv.SelectedLength);
  2595. Assert.True (tv.NewKeyDownEvent (Key.D1));
  2596. Assert.Equal ($"1Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  2597. Assert.Equal (new (1, 0), tv.CursorPosition);
  2598. Assert.Equal (0, tv.SelectedLength);
  2599. Assert.True (tv.IsDirty);
  2600. tv.SelectionStartColumn = 1;
  2601. tv.SelectionStartRow = 0;
  2602. tv.CursorPosition = new (1, 1);
  2603. Assert.Equal (4 + Environment.NewLine.Length, tv.SelectedLength);
  2604. Assert.True (tv.NewKeyDownEvent (Key.D2));
  2605. Assert.Equal ($"12hree{Environment.NewLine}", tv.Text);
  2606. Assert.Equal (new (2, 0), tv.CursorPosition);
  2607. Assert.Equal (0, tv.SelectedLength);
  2608. Assert.True (tv.IsDirty);
  2609. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2610. Assert.Equal ($"1Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  2611. Assert.Equal (new (1, 1), tv.CursorPosition);
  2612. Assert.Equal (0, tv.SelectedLength);
  2613. Assert.True (tv.IsDirty);
  2614. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2615. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  2616. Assert.Equal (4, tv.Lines);
  2617. Assert.Equal (new (0, 1), tv.CursorPosition);
  2618. Assert.Equal (0, tv.SelectedLength);
  2619. Assert.False (tv.IsDirty);
  2620. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2621. Assert.Equal ($"1Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  2622. Assert.Equal (new (1, 0), tv.CursorPosition);
  2623. Assert.Equal (0, tv.SelectedLength);
  2624. Assert.True (tv.IsDirty);
  2625. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2626. Assert.Equal ($"12hree{Environment.NewLine}", tv.Text);
  2627. Assert.Equal (new (2, 0), tv.CursorPosition);
  2628. Assert.Equal (0, tv.SelectedLength);
  2629. Assert.True (tv.IsDirty);
  2630. top.Dispose ();
  2631. }
  2632. [Fact]
  2633. [AutoInitShutdown]
  2634. public void HistoryText_Undo_Redo_Multi_Line_Selected_With_Empty_Text ()
  2635. {
  2636. var tv = new TextView { Width = 10, Height = 2 };
  2637. Toplevel top = new ();
  2638. top.Add (tv);
  2639. Application.Begin (top);
  2640. Assert.Equal ("", tv.Text);
  2641. Assert.Equal (1, tv.Lines);
  2642. Assert.Equal (Point.Empty, tv.CursorPosition);
  2643. Assert.False (tv.IsDirty);
  2644. Assert.True (tv.NewKeyDownEvent (Key.O.WithShift));
  2645. Assert.Equal ("O", tv.Text);
  2646. Assert.Equal (1, tv.Lines);
  2647. Assert.Equal (new (1, 0), tv.CursorPosition);
  2648. Assert.True (tv.IsDirty);
  2649. Assert.True (tv.NewKeyDownEvent (Key.N));
  2650. Assert.Equal ("On", tv.Text);
  2651. Assert.Equal (1, tv.Lines);
  2652. Assert.Equal (new (2, 0), tv.CursorPosition);
  2653. Assert.True (tv.IsDirty);
  2654. Assert.True (tv.NewKeyDownEvent (Key.E));
  2655. Assert.Equal ("One", tv.Text);
  2656. Assert.Equal (1, tv.Lines);
  2657. Assert.Equal (new (3, 0), tv.CursorPosition);
  2658. Assert.True (tv.IsDirty);
  2659. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  2660. Assert.Equal ($"One{Environment.NewLine}", tv.Text);
  2661. Assert.Equal (2, tv.Lines);
  2662. Assert.Equal (new (0, 1), tv.CursorPosition);
  2663. Assert.True (tv.IsDirty);
  2664. Assert.True (tv.NewKeyDownEvent (Key.T.WithShift));
  2665. Assert.Equal ($"One{Environment.NewLine}T", tv.Text);
  2666. Assert.Equal (2, tv.Lines);
  2667. Assert.Equal (new (1, 1), tv.CursorPosition);
  2668. Assert.True (tv.IsDirty);
  2669. Assert.True (tv.NewKeyDownEvent (Key.W));
  2670. Assert.Equal ($"One{Environment.NewLine}Tw", tv.Text);
  2671. Assert.Equal (2, tv.Lines);
  2672. Assert.Equal (new (2, 1), tv.CursorPosition);
  2673. Assert.True (tv.IsDirty);
  2674. Assert.True (tv.NewKeyDownEvent (Key.O));
  2675. Assert.Equal ($"One{Environment.NewLine}Two", tv.Text);
  2676. Assert.Equal (2, tv.Lines);
  2677. Assert.Equal (new (3, 1), tv.CursorPosition);
  2678. Assert.True (tv.IsDirty);
  2679. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  2680. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}", tv.Text);
  2681. Assert.Equal (3, tv.Lines);
  2682. Assert.Equal (new (0, 2), tv.CursorPosition);
  2683. Assert.True (tv.IsDirty);
  2684. Assert.True (tv.NewKeyDownEvent (Key.T.WithShift));
  2685. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}T", tv.Text);
  2686. Assert.Equal (3, tv.Lines);
  2687. Assert.Equal (new (1, 2), tv.CursorPosition);
  2688. Assert.True (tv.IsDirty);
  2689. Assert.True (tv.NewKeyDownEvent (Key.H));
  2690. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Th", tv.Text);
  2691. Assert.Equal (3, tv.Lines);
  2692. Assert.Equal (new (2, 2), tv.CursorPosition);
  2693. Assert.True (tv.IsDirty);
  2694. Assert.True (tv.NewKeyDownEvent (Key.R));
  2695. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Thr", tv.Text);
  2696. Assert.Equal (3, tv.Lines);
  2697. Assert.Equal (new (3, 2), tv.CursorPosition);
  2698. Assert.True (tv.IsDirty);
  2699. Assert.True (tv.NewKeyDownEvent (Key.E));
  2700. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Thre", tv.Text);
  2701. Assert.Equal (3, tv.Lines);
  2702. Assert.Equal (new (4, 2), tv.CursorPosition);
  2703. Assert.True (tv.IsDirty);
  2704. Assert.True (tv.NewKeyDownEvent (Key.E));
  2705. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three", tv.Text);
  2706. Assert.Equal (3, tv.Lines);
  2707. Assert.Equal (new (5, 2), tv.CursorPosition);
  2708. Assert.True (tv.IsDirty);
  2709. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  2710. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  2711. Assert.Equal (4, tv.Lines);
  2712. Assert.Equal (new (0, 3), tv.CursorPosition);
  2713. Assert.True (tv.IsDirty);
  2714. tv.SelectionStartColumn = 0;
  2715. tv.SelectionStartRow = 0;
  2716. tv.CursorPosition = new (0, 1);
  2717. Assert.Equal (3 + Environment.NewLine.Length, tv.SelectedLength);
  2718. Assert.True (tv.NewKeyDownEvent (Key.D1));
  2719. Assert.Equal ($"1Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  2720. Assert.Equal (3, tv.Lines);
  2721. Assert.Equal (new (1, 0), tv.CursorPosition);
  2722. Assert.Equal (0, tv.SelectedLength);
  2723. Assert.True (tv.IsDirty);
  2724. tv.SelectionStartColumn = 1;
  2725. tv.SelectionStartRow = 0;
  2726. tv.CursorPosition = new (1, 1);
  2727. Assert.Equal (4 + Environment.NewLine.Length, tv.SelectedLength);
  2728. Assert.True (tv.NewKeyDownEvent (Key.D2));
  2729. Assert.Equal ($"12hree{Environment.NewLine}", tv.Text);
  2730. Assert.Equal (2, tv.Lines);
  2731. Assert.Equal (new (2, 0), tv.CursorPosition);
  2732. Assert.Equal (0, tv.SelectedLength);
  2733. Assert.True (tv.IsDirty);
  2734. // Undoing
  2735. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2736. Assert.Equal ($"1Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  2737. Assert.Equal (3, tv.Lines);
  2738. Assert.Equal (new (1, 1), tv.CursorPosition);
  2739. Assert.Equal (0, tv.SelectedLength);
  2740. Assert.True (tv.IsDirty);
  2741. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2742. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  2743. Assert.Equal (4, tv.Lines);
  2744. Assert.Equal (new (0, 1), tv.CursorPosition);
  2745. Assert.Equal (0, tv.SelectedLength);
  2746. Assert.True (tv.IsDirty);
  2747. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2748. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three", tv.Text);
  2749. Assert.Equal (3, tv.Lines);
  2750. Assert.Equal (new (5, 2), tv.CursorPosition);
  2751. Assert.Equal (0, tv.SelectedLength);
  2752. Assert.True (tv.IsDirty);
  2753. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2754. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Thre", tv.Text);
  2755. Assert.Equal (3, tv.Lines);
  2756. Assert.Equal (new (4, 2), tv.CursorPosition);
  2757. Assert.True (tv.IsDirty);
  2758. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2759. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Thr", tv.Text);
  2760. Assert.Equal (3, tv.Lines);
  2761. Assert.Equal (new (3, 2), tv.CursorPosition);
  2762. Assert.Equal (0, tv.SelectedLength);
  2763. Assert.True (tv.IsDirty);
  2764. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2765. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Th", tv.Text);
  2766. Assert.Equal (3, tv.Lines);
  2767. Assert.Equal (new (2, 2), tv.CursorPosition);
  2768. Assert.Equal (0, tv.SelectedLength);
  2769. Assert.True (tv.IsDirty);
  2770. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2771. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}T", tv.Text);
  2772. Assert.Equal (3, tv.Lines);
  2773. Assert.Equal (new (1, 2), tv.CursorPosition);
  2774. Assert.Equal (0, tv.SelectedLength);
  2775. Assert.True (tv.IsDirty);
  2776. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2777. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}", tv.Text);
  2778. Assert.Equal (3, tv.Lines);
  2779. Assert.Equal (new (0, 2), tv.CursorPosition);
  2780. Assert.Equal (0, tv.SelectedLength);
  2781. Assert.True (tv.IsDirty);
  2782. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2783. Assert.Equal ($"One{Environment.NewLine}Two", tv.Text);
  2784. Assert.Equal (2, tv.Lines);
  2785. Assert.Equal (new (3, 1), tv.CursorPosition);
  2786. Assert.Equal (0, tv.SelectedLength);
  2787. Assert.True (tv.IsDirty);
  2788. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2789. Assert.Equal ($"One{Environment.NewLine}Tw", tv.Text);
  2790. Assert.Equal (2, tv.Lines);
  2791. Assert.Equal (new (2, 1), tv.CursorPosition);
  2792. Assert.Equal (0, tv.SelectedLength);
  2793. Assert.True (tv.IsDirty);
  2794. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2795. Assert.Equal ($"One{Environment.NewLine}T", tv.Text);
  2796. Assert.Equal (2, tv.Lines);
  2797. Assert.Equal (new (1, 1), tv.CursorPosition);
  2798. Assert.Equal (0, tv.SelectedLength);
  2799. Assert.True (tv.IsDirty);
  2800. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2801. Assert.Equal ($"One{Environment.NewLine}", tv.Text);
  2802. Assert.Equal (2, tv.Lines);
  2803. Assert.Equal (new (0, 1), tv.CursorPosition);
  2804. Assert.Equal (0, tv.SelectedLength);
  2805. Assert.True (tv.IsDirty);
  2806. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2807. Assert.Equal ("One", tv.Text);
  2808. Assert.Equal (1, tv.Lines);
  2809. Assert.Equal (new (3, 0), tv.CursorPosition);
  2810. Assert.Equal (0, tv.SelectedLength);
  2811. Assert.True (tv.IsDirty);
  2812. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2813. Assert.Equal ("On", tv.Text);
  2814. Assert.Equal (1, tv.Lines);
  2815. Assert.Equal (new (2, 0), tv.CursorPosition);
  2816. Assert.Equal (0, tv.SelectedLength);
  2817. Assert.True (tv.IsDirty);
  2818. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2819. Assert.Equal ("O", tv.Text);
  2820. Assert.Equal (1, tv.Lines);
  2821. Assert.Equal (new (1, 0), tv.CursorPosition);
  2822. Assert.Equal (0, tv.SelectedLength);
  2823. Assert.True (tv.IsDirty);
  2824. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2825. Assert.Equal ("", tv.Text);
  2826. Assert.Equal (1, tv.Lines);
  2827. Assert.Equal (Point.Empty, tv.CursorPosition);
  2828. Assert.Equal (0, tv.SelectedLength);
  2829. Assert.False (tv.IsDirty);
  2830. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  2831. Assert.Equal ("", tv.Text);
  2832. Assert.Equal (1, tv.Lines);
  2833. Assert.Equal (Point.Empty, tv.CursorPosition);
  2834. Assert.Equal (0, tv.SelectedLength);
  2835. Assert.False (tv.IsDirty);
  2836. // Redoing
  2837. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2838. Assert.Equal ("O", tv.Text);
  2839. Assert.Equal (1, tv.Lines);
  2840. Assert.Equal (new (1, 0), tv.CursorPosition);
  2841. Assert.Equal (0, tv.SelectedLength);
  2842. Assert.True (tv.IsDirty);
  2843. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2844. Assert.Equal ("On", tv.Text);
  2845. Assert.Equal (1, tv.Lines);
  2846. Assert.Equal (new (2, 0), tv.CursorPosition);
  2847. Assert.Equal (0, tv.SelectedLength);
  2848. Assert.True (tv.IsDirty);
  2849. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2850. Assert.Equal ("One", tv.Text);
  2851. Assert.Equal (1, tv.Lines);
  2852. Assert.Equal (new (3, 0), tv.CursorPosition);
  2853. Assert.Equal (0, tv.SelectedLength);
  2854. Assert.True (tv.IsDirty);
  2855. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2856. Assert.Equal ($"One{Environment.NewLine}", tv.Text);
  2857. Assert.Equal (2, tv.Lines);
  2858. Assert.Equal (new (0, 1), tv.CursorPosition);
  2859. Assert.Equal (0, tv.SelectedLength);
  2860. Assert.True (tv.IsDirty);
  2861. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2862. Assert.Equal ($"One{Environment.NewLine}T", tv.Text);
  2863. Assert.Equal (2, tv.Lines);
  2864. Assert.Equal (new (1, 1), tv.CursorPosition);
  2865. Assert.Equal (0, tv.SelectedLength);
  2866. Assert.True (tv.IsDirty);
  2867. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2868. Assert.Equal ($"One{Environment.NewLine}Tw", tv.Text);
  2869. Assert.Equal (2, tv.Lines);
  2870. Assert.Equal (new (2, 1), tv.CursorPosition);
  2871. Assert.Equal (0, tv.SelectedLength);
  2872. Assert.True (tv.IsDirty);
  2873. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2874. Assert.Equal ($"One{Environment.NewLine}Two", tv.Text);
  2875. Assert.Equal (2, tv.Lines);
  2876. Assert.Equal (new (3, 1), tv.CursorPosition);
  2877. Assert.Equal (0, tv.SelectedLength);
  2878. Assert.True (tv.IsDirty);
  2879. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2880. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}", tv.Text);
  2881. Assert.Equal (3, tv.Lines);
  2882. Assert.Equal (new (0, 2), tv.CursorPosition);
  2883. Assert.Equal (0, tv.SelectedLength);
  2884. Assert.True (tv.IsDirty);
  2885. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2886. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}T", tv.Text);
  2887. Assert.Equal (3, tv.Lines);
  2888. Assert.Equal (new (1, 2), tv.CursorPosition);
  2889. Assert.Equal (0, tv.SelectedLength);
  2890. Assert.True (tv.IsDirty);
  2891. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2892. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Th", tv.Text);
  2893. Assert.Equal (3, tv.Lines);
  2894. Assert.Equal (new (2, 2), tv.CursorPosition);
  2895. Assert.Equal (0, tv.SelectedLength);
  2896. Assert.True (tv.IsDirty);
  2897. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2898. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Thr", tv.Text);
  2899. Assert.Equal (3, tv.Lines);
  2900. Assert.Equal (new (3, 2), tv.CursorPosition);
  2901. Assert.Equal (0, tv.SelectedLength);
  2902. Assert.True (tv.IsDirty);
  2903. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2904. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Thre", tv.Text);
  2905. Assert.Equal (3, tv.Lines);
  2906. Assert.Equal (new (4, 2), tv.CursorPosition);
  2907. Assert.Equal (0, tv.SelectedLength);
  2908. Assert.True (tv.IsDirty);
  2909. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2910. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three", tv.Text);
  2911. Assert.Equal (3, tv.Lines);
  2912. Assert.Equal (new (5, 2), tv.CursorPosition);
  2913. Assert.Equal (0, tv.SelectedLength);
  2914. Assert.True (tv.IsDirty);
  2915. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2916. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  2917. Assert.Equal (4, tv.Lines);
  2918. Assert.Equal (new (0, 3), tv.CursorPosition);
  2919. Assert.Equal (0, tv.SelectedLength);
  2920. Assert.True (tv.IsDirty);
  2921. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2922. Assert.Equal ($"1Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  2923. Assert.Equal (3, tv.Lines);
  2924. Assert.Equal (new (1, 0), tv.CursorPosition);
  2925. Assert.Equal (0, tv.SelectedLength);
  2926. Assert.True (tv.IsDirty);
  2927. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  2928. Assert.Equal ($"12hree{Environment.NewLine}", tv.Text);
  2929. Assert.Equal (2, tv.Lines);
  2930. Assert.Equal (new (2, 0), tv.CursorPosition);
  2931. Assert.Equal (0, tv.SelectedLength);
  2932. Assert.True (tv.IsDirty);
  2933. top.Dispose ();
  2934. }
  2935. [Fact]
  2936. [AutoInitShutdown]
  2937. public void HistoryText_Undo_Redo_Multi_Line_With_Empty_Text ()
  2938. {
  2939. var tv = new TextView { Width = 10, Height = 2 };
  2940. Toplevel top = new ();
  2941. top.Add (tv);
  2942. Application.Begin (top);
  2943. Assert.Equal ("", tv.Text);
  2944. Assert.Equal (1, tv.Lines);
  2945. Assert.Equal (Point.Empty, tv.CursorPosition);
  2946. Assert.False (tv.IsDirty);
  2947. Assert.True (tv.NewKeyDownEvent (Key.O.WithShift));
  2948. Assert.Equal ("O", tv.Text);
  2949. Assert.Equal (1, tv.Lines);
  2950. Assert.Equal (new (1, 0), tv.CursorPosition);
  2951. Assert.True (tv.IsDirty);
  2952. Assert.True (tv.NewKeyDownEvent (Key.N));
  2953. Assert.Equal ("On", tv.Text);
  2954. Assert.Equal (1, tv.Lines);
  2955. Assert.Equal (new (2, 0), tv.CursorPosition);
  2956. Assert.True (tv.IsDirty);
  2957. Assert.True (tv.NewKeyDownEvent (Key.E));
  2958. Assert.Equal ("One", tv.Text);
  2959. Assert.Equal (1, tv.Lines);
  2960. Assert.Equal (new (3, 0), tv.CursorPosition);
  2961. Assert.True (tv.IsDirty);
  2962. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  2963. Assert.Equal ($"One{Environment.NewLine}", tv.Text);
  2964. Assert.Equal (2, tv.Lines);
  2965. Assert.Equal (new (0, 1), tv.CursorPosition);
  2966. Assert.True (tv.IsDirty);
  2967. Assert.True (tv.NewKeyDownEvent (Key.T.WithShift));
  2968. Assert.Equal ($"One{Environment.NewLine}T", tv.Text);
  2969. Assert.Equal (2, tv.Lines);
  2970. Assert.Equal (new (1, 1), tv.CursorPosition);
  2971. Assert.True (tv.IsDirty);
  2972. Assert.True (tv.NewKeyDownEvent (Key.W));
  2973. Assert.Equal ($"One{Environment.NewLine}Tw", tv.Text);
  2974. Assert.Equal (2, tv.Lines);
  2975. Assert.Equal (new (2, 1), tv.CursorPosition);
  2976. Assert.True (tv.IsDirty);
  2977. Assert.True (tv.NewKeyDownEvent (Key.O));
  2978. Assert.Equal ($"One{Environment.NewLine}Two", tv.Text);
  2979. Assert.Equal (2, tv.Lines);
  2980. Assert.Equal (new (3, 1), tv.CursorPosition);
  2981. Assert.True (tv.IsDirty);
  2982. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  2983. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}", tv.Text);
  2984. Assert.Equal (3, tv.Lines);
  2985. Assert.Equal (new (0, 2), tv.CursorPosition);
  2986. Assert.True (tv.IsDirty);
  2987. Assert.True (tv.NewKeyDownEvent (Key.T.WithShift));
  2988. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}T", tv.Text);
  2989. Assert.Equal (3, tv.Lines);
  2990. Assert.Equal (new (1, 2), tv.CursorPosition);
  2991. Assert.True (tv.IsDirty);
  2992. Assert.True (tv.NewKeyDownEvent (Key.H));
  2993. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Th", tv.Text);
  2994. Assert.Equal (3, tv.Lines);
  2995. Assert.Equal (new (2, 2), tv.CursorPosition);
  2996. Assert.True (tv.IsDirty);
  2997. Assert.True (tv.NewKeyDownEvent (Key.R));
  2998. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Thr", tv.Text);
  2999. Assert.Equal (3, tv.Lines);
  3000. Assert.Equal (new (3, 2), tv.CursorPosition);
  3001. Assert.True (tv.IsDirty);
  3002. Assert.True (tv.NewKeyDownEvent (Key.E));
  3003. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Thre", tv.Text);
  3004. Assert.Equal (3, tv.Lines);
  3005. Assert.Equal (new (4, 2), tv.CursorPosition);
  3006. Assert.True (tv.IsDirty);
  3007. Assert.True (tv.NewKeyDownEvent (Key.E));
  3008. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three", tv.Text);
  3009. Assert.Equal (3, tv.Lines);
  3010. Assert.Equal (new (5, 2), tv.CursorPosition);
  3011. Assert.True (tv.IsDirty);
  3012. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  3013. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  3014. Assert.Equal (4, tv.Lines);
  3015. Assert.Equal (new (0, 3), tv.CursorPosition);
  3016. Assert.True (tv.IsDirty);
  3017. // Undoing
  3018. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3019. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three", tv.Text);
  3020. Assert.Equal (3, tv.Lines);
  3021. Assert.Equal (new (5, 2), tv.CursorPosition);
  3022. Assert.True (tv.IsDirty);
  3023. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3024. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Thre", tv.Text);
  3025. Assert.Equal (3, tv.Lines);
  3026. Assert.Equal (new (4, 2), tv.CursorPosition);
  3027. Assert.True (tv.IsDirty);
  3028. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3029. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Thr", tv.Text);
  3030. Assert.Equal (3, tv.Lines);
  3031. Assert.Equal (new (3, 2), tv.CursorPosition);
  3032. Assert.True (tv.IsDirty);
  3033. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3034. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Th", tv.Text);
  3035. Assert.Equal (3, tv.Lines);
  3036. Assert.Equal (new (2, 2), tv.CursorPosition);
  3037. Assert.True (tv.IsDirty);
  3038. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3039. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}T", tv.Text);
  3040. Assert.Equal (3, tv.Lines);
  3041. Assert.Equal (new (1, 2), tv.CursorPosition);
  3042. Assert.True (tv.IsDirty);
  3043. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3044. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}", tv.Text);
  3045. Assert.Equal (3, tv.Lines);
  3046. Assert.Equal (new (0, 2), tv.CursorPosition);
  3047. Assert.True (tv.IsDirty);
  3048. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3049. Assert.Equal ($"One{Environment.NewLine}Two", tv.Text);
  3050. Assert.Equal (2, tv.Lines);
  3051. Assert.Equal (new (3, 1), tv.CursorPosition);
  3052. Assert.True (tv.IsDirty);
  3053. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3054. Assert.Equal ($"One{Environment.NewLine}Tw", tv.Text);
  3055. Assert.Equal (2, tv.Lines);
  3056. Assert.Equal (new (2, 1), tv.CursorPosition);
  3057. Assert.True (tv.IsDirty);
  3058. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3059. Assert.Equal ($"One{Environment.NewLine}T", tv.Text);
  3060. Assert.Equal (2, tv.Lines);
  3061. Assert.Equal (new (1, 1), tv.CursorPosition);
  3062. Assert.True (tv.IsDirty);
  3063. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3064. Assert.Equal ($"One{Environment.NewLine}", tv.Text);
  3065. Assert.Equal (2, tv.Lines);
  3066. Assert.Equal (new (0, 1), tv.CursorPosition);
  3067. Assert.True (tv.IsDirty);
  3068. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3069. Assert.Equal ("One", tv.Text);
  3070. Assert.Equal (1, tv.Lines);
  3071. Assert.Equal (new (3, 0), tv.CursorPosition);
  3072. Assert.True (tv.IsDirty);
  3073. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3074. Assert.Equal ("On", tv.Text);
  3075. Assert.Equal (1, tv.Lines);
  3076. Assert.Equal (new (2, 0), tv.CursorPosition);
  3077. Assert.True (tv.IsDirty);
  3078. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3079. Assert.Equal ("O", tv.Text);
  3080. Assert.Equal (1, tv.Lines);
  3081. Assert.Equal (new (1, 0), tv.CursorPosition);
  3082. Assert.True (tv.IsDirty);
  3083. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3084. Assert.Equal ("", tv.Text);
  3085. Assert.Equal (1, tv.Lines);
  3086. Assert.Equal (Point.Empty, tv.CursorPosition);
  3087. Assert.False (tv.IsDirty);
  3088. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3089. Assert.Equal ("", tv.Text);
  3090. Assert.Equal (1, tv.Lines);
  3091. Assert.Equal (Point.Empty, tv.CursorPosition);
  3092. Assert.False (tv.IsDirty);
  3093. // Redoing
  3094. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3095. Assert.Equal ("O", tv.Text);
  3096. Assert.Equal (1, tv.Lines);
  3097. Assert.Equal (new (1, 0), tv.CursorPosition);
  3098. Assert.True (tv.IsDirty);
  3099. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3100. Assert.Equal ("On", tv.Text);
  3101. Assert.Equal (1, tv.Lines);
  3102. Assert.Equal (new (2, 0), tv.CursorPosition);
  3103. Assert.True (tv.IsDirty);
  3104. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3105. Assert.Equal ("One", tv.Text);
  3106. Assert.Equal (1, tv.Lines);
  3107. Assert.Equal (new (3, 0), tv.CursorPosition);
  3108. Assert.True (tv.IsDirty);
  3109. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3110. Assert.Equal ($"One{Environment.NewLine}", tv.Text);
  3111. Assert.Equal (2, tv.Lines);
  3112. Assert.Equal (new (0, 1), tv.CursorPosition);
  3113. Assert.True (tv.IsDirty);
  3114. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3115. Assert.Equal ($"One{Environment.NewLine}T", tv.Text);
  3116. Assert.Equal (2, tv.Lines);
  3117. Assert.Equal (new (1, 1), tv.CursorPosition);
  3118. Assert.True (tv.IsDirty);
  3119. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3120. Assert.Equal ($"One{Environment.NewLine}Tw", tv.Text);
  3121. Assert.Equal (2, tv.Lines);
  3122. Assert.Equal (new (2, 1), tv.CursorPosition);
  3123. Assert.True (tv.IsDirty);
  3124. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3125. Assert.Equal ($"One{Environment.NewLine}Two", tv.Text);
  3126. Assert.Equal (2, tv.Lines);
  3127. Assert.Equal (new (3, 1), tv.CursorPosition);
  3128. Assert.True (tv.IsDirty);
  3129. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3130. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}", tv.Text);
  3131. Assert.Equal (3, tv.Lines);
  3132. Assert.Equal (new (0, 2), tv.CursorPosition);
  3133. Assert.True (tv.IsDirty);
  3134. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3135. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}T", tv.Text);
  3136. Assert.Equal (3, tv.Lines);
  3137. Assert.Equal (new (1, 2), tv.CursorPosition);
  3138. Assert.True (tv.IsDirty);
  3139. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3140. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Th", tv.Text);
  3141. Assert.Equal (3, tv.Lines);
  3142. Assert.Equal (new (2, 2), tv.CursorPosition);
  3143. Assert.True (tv.IsDirty);
  3144. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3145. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Thr", tv.Text);
  3146. Assert.Equal (3, tv.Lines);
  3147. Assert.Equal (new (3, 2), tv.CursorPosition);
  3148. Assert.True (tv.IsDirty);
  3149. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3150. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Thre", tv.Text);
  3151. Assert.Equal (3, tv.Lines);
  3152. Assert.Equal (new (4, 2), tv.CursorPosition);
  3153. Assert.True (tv.IsDirty);
  3154. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3155. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three", tv.Text);
  3156. Assert.Equal (3, tv.Lines);
  3157. Assert.Equal (new (5, 2), tv.CursorPosition);
  3158. Assert.True (tv.IsDirty);
  3159. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3160. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  3161. Assert.Equal (4, tv.Lines);
  3162. Assert.Equal (new (0, 3), tv.CursorPosition);
  3163. Assert.True (tv.IsDirty);
  3164. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3165. Assert.Equal ($"One{Environment.NewLine}Two{Environment.NewLine}Three{Environment.NewLine}", tv.Text);
  3166. Assert.Equal (4, tv.Lines);
  3167. Assert.Equal (new (0, 3), tv.CursorPosition);
  3168. Assert.True (tv.IsDirty);
  3169. top.Dispose ();
  3170. }
  3171. [Fact]
  3172. public void HistoryText_Undo_Redo_Multiline_Selected_Tab_BackTab ()
  3173. {
  3174. var text = "First line.\nSecond line.\nThird line.";
  3175. var tv = new TextView { Width = 80, Height = 5, Text = text };
  3176. tv.SelectionStartColumn = 6;
  3177. tv.CursorPosition = new (6, 2);
  3178. Assert.True (tv.NewKeyDownEvent (Key.Tab));
  3179. Assert.Equal ("First \tline.", tv.Text);
  3180. Assert.Equal (1, tv.Lines);
  3181. Assert.Equal (new (7, 0), tv.CursorPosition);
  3182. Assert.True (tv.NewKeyDownEvent (Key.Tab.WithShift));
  3183. Assert.Equal ("First line.", tv.Text);
  3184. Assert.Equal (1, tv.Lines);
  3185. Assert.Equal (new (6, 0), tv.CursorPosition);
  3186. // Undo
  3187. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3188. Assert.Equal ("First \tline.", tv.Text);
  3189. Assert.Equal (1, tv.Lines);
  3190. Assert.Equal (new (7, 0), tv.CursorPosition);
  3191. Assert.True (tv.IsDirty);
  3192. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3193. Assert.Equal ($"First line.{Environment.NewLine}Second line.{Environment.NewLine}Third line.", tv.Text);
  3194. Assert.Equal (3, tv.Lines);
  3195. Assert.Equal (new (6, 2), tv.CursorPosition);
  3196. Assert.False (tv.IsDirty);
  3197. // Redo
  3198. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3199. Assert.Equal ("First \tline.", tv.Text);
  3200. Assert.Equal (1, tv.Lines);
  3201. Assert.Equal (new (7, 0), tv.CursorPosition);
  3202. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3203. Assert.Equal ("First line.", tv.Text);
  3204. Assert.Equal (1, tv.Lines);
  3205. Assert.Equal (new (6, 0), tv.CursorPosition);
  3206. }
  3207. [Fact]
  3208. public void HistoryText_Undo_Redo_Multiline_Simples_Tab_BackTab ()
  3209. {
  3210. var text = "First line.\nSecond line.\nThird line.";
  3211. var tv = new TextView { Width = 80, Height = 5, Text = text };
  3212. Assert.True (tv.NewKeyDownEvent (Key.Tab));
  3213. Assert.Equal (
  3214. $"\tFirst line.{Environment.NewLine}Second line.{Environment.NewLine}Third line.",
  3215. tv.Text
  3216. );
  3217. Assert.Equal (3, tv.Lines);
  3218. Assert.Equal (new (1, 0), tv.CursorPosition);
  3219. Assert.True (tv.NewKeyDownEvent (Key.Tab.WithShift));
  3220. Assert.Equal ($"First line.{Environment.NewLine}Second line.{Environment.NewLine}Third line.", tv.Text);
  3221. Assert.Equal (3, tv.Lines);
  3222. Assert.Equal (Point.Empty, tv.CursorPosition);
  3223. // Undo
  3224. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3225. Assert.Equal (
  3226. $"\tFirst line.{Environment.NewLine}Second line.{Environment.NewLine}Third line.",
  3227. tv.Text
  3228. );
  3229. Assert.Equal (3, tv.Lines);
  3230. Assert.Equal (new (1, 0), tv.CursorPosition);
  3231. Assert.True (tv.IsDirty);
  3232. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3233. Assert.Equal ($"First line.{Environment.NewLine}Second line.{Environment.NewLine}Third line.", tv.Text);
  3234. Assert.Equal (3, tv.Lines);
  3235. Assert.Equal (Point.Empty, tv.CursorPosition);
  3236. Assert.False (tv.IsDirty);
  3237. // Redo
  3238. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3239. Assert.Equal (
  3240. $"\tFirst line.{Environment.NewLine}Second line.{Environment.NewLine}Third line.",
  3241. tv.Text
  3242. );
  3243. Assert.Equal (3, tv.Lines);
  3244. Assert.Equal (new (1, 0), tv.CursorPosition);
  3245. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3246. Assert.Equal ($"First line.{Environment.NewLine}Second line.{Environment.NewLine}Third line.", tv.Text);
  3247. Assert.Equal (3, tv.Lines);
  3248. Assert.Equal (Point.Empty, tv.CursorPosition);
  3249. }
  3250. [Fact]
  3251. [AutoInitShutdown]
  3252. public void HistoryText_Undo_Redo_Setting_Clipboard_Multi_Line_Selected_Paste ()
  3253. {
  3254. var text = "This is the first line.\nThis is the second line.";
  3255. var tv = new TextView { Text = text };
  3256. Clipboard.Contents = "Inserted\nNewLine";
  3257. Assert.Equal ($"This is the first line.{Environment.NewLine}This is the second line.", tv.Text);
  3258. Assert.Equal ("", tv.SelectedText);
  3259. Assert.Equal (2, tv.Lines);
  3260. Assert.Equal (Point.Empty, tv.CursorPosition);
  3261. Assert.True (tv.NewKeyDownEvent (Key.Y.WithCtrl));
  3262. Assert.Equal (
  3263. $"Inserted{Environment.NewLine}NewLineThis is the first line.{Environment.NewLine}This is the second line.",
  3264. tv.Text
  3265. );
  3266. Assert.Equal (3, tv.Lines);
  3267. Assert.Equal (new (7, 1), tv.CursorPosition);
  3268. // Undo
  3269. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3270. Assert.Equal ($"This is the first line.{Environment.NewLine}This is the second line.", tv.Text);
  3271. Assert.Equal (2, tv.Lines);
  3272. Assert.Equal (Point.Empty, tv.CursorPosition);
  3273. // Redo
  3274. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3275. Assert.Equal (
  3276. $"Inserted{Environment.NewLine}NewLineThis is the first line.{Environment.NewLine}This is the second line.",
  3277. tv.Text
  3278. );
  3279. Assert.Equal (3, tv.Lines);
  3280. Assert.Equal (new (7, 1), tv.CursorPosition);
  3281. }
  3282. [Fact]
  3283. [AutoInitShutdown (useFakeClipboard: true)]
  3284. public void HistoryText_Undo_Redo_Simple_Copy_Multi_Line_Selected_Paste ()
  3285. {
  3286. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  3287. var tv = new TextView { Text = text };
  3288. tv.SelectionStartColumn = 12;
  3289. tv.CursorPosition = new (17, 0);
  3290. Assert.True (tv.NewKeyDownEvent (Key.C.WithCtrl));
  3291. Assert.Equal (
  3292. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3293. tv.Text
  3294. );
  3295. Assert.Equal ("first", tv.SelectedText);
  3296. Assert.Equal (3, tv.Lines);
  3297. Assert.Equal (new (17, 0), tv.CursorPosition);
  3298. tv.SelectionStartColumn = 12;
  3299. tv.CursorPosition = new (11, 1);
  3300. Assert.True (tv.NewKeyDownEvent (Key.Y.WithCtrl));
  3301. Assert.Equal ($"This is the first second line.{Environment.NewLine}This is the third line.", tv.Text);
  3302. Assert.Equal (2, tv.Lines);
  3303. Assert.Equal (new (17, 0), tv.CursorPosition);
  3304. // Undo
  3305. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3306. Assert.Equal (
  3307. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3308. tv.Text
  3309. );
  3310. Assert.Equal (3, tv.Lines);
  3311. Assert.Equal (new (12, 0), tv.CursorPosition);
  3312. // Redo
  3313. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3314. Assert.Equal ($"This is the first second line.{Environment.NewLine}This is the third line.", tv.Text);
  3315. Assert.Equal (2, tv.Lines);
  3316. Assert.Equal (new (17, 0), tv.CursorPosition);
  3317. }
  3318. [Fact]
  3319. [AutoInitShutdown]
  3320. public void HistoryText_Undo_Redo_Single_Line_DeleteCharLeft ()
  3321. {
  3322. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  3323. var tv = new TextView { Width = 10, Height = 2, Text = text };
  3324. Toplevel top = new ();
  3325. top.Add (tv);
  3326. Application.Begin (top);
  3327. Assert.Equal (
  3328. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3329. tv.Text
  3330. );
  3331. Assert.Equal (3, tv.Lines);
  3332. Assert.Equal (Point.Empty, tv.CursorPosition);
  3333. var ntimes = 3;
  3334. tv.CursorPosition = new (7, 1);
  3335. for (var i = 0; i < ntimes; i++)
  3336. {
  3337. tv.DeleteCharLeft ();
  3338. }
  3339. Assert.Equal (
  3340. $"This is the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  3341. tv.Text
  3342. );
  3343. Assert.Equal (3, tv.Lines);
  3344. Assert.Equal (new (4, 1), tv.CursorPosition);
  3345. for (var i = 0; i < ntimes; i++)
  3346. {
  3347. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3348. }
  3349. Assert.Equal (
  3350. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3351. tv.Text
  3352. );
  3353. Assert.Equal (3, tv.Lines);
  3354. Assert.Equal (new (7, 1), tv.CursorPosition);
  3355. for (var i = 0; i < ntimes; i++)
  3356. {
  3357. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3358. }
  3359. Assert.Equal (
  3360. $"This is the first line.{Environment.NewLine}This the second line.{Environment.NewLine}This is the third line.",
  3361. tv.Text
  3362. );
  3363. Assert.Equal (3, tv.Lines);
  3364. Assert.Equal (new (4, 1), tv.CursorPosition);
  3365. top.Dispose ();
  3366. }
  3367. [Fact]
  3368. [AutoInitShutdown]
  3369. public void HistoryText_Undo_Redo_Single_Line_DeleteCharRight ()
  3370. {
  3371. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  3372. var tv = new TextView { Width = 10, Height = 2, Text = text };
  3373. Toplevel top = new ();
  3374. top.Add (tv);
  3375. Application.Begin (top);
  3376. Assert.Equal (
  3377. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3378. tv.Text
  3379. );
  3380. Assert.Equal (3, tv.Lines);
  3381. Assert.Equal (Point.Empty, tv.CursorPosition);
  3382. var ntimes = 3;
  3383. tv.CursorPosition = new (7, 1);
  3384. for (var i = 0; i < ntimes; i++)
  3385. {
  3386. tv.DeleteCharRight ();
  3387. }
  3388. Assert.Equal (
  3389. $"This is the first line.{Environment.NewLine}This ise second line.{Environment.NewLine}This is the third line.",
  3390. tv.Text
  3391. );
  3392. Assert.Equal (3, tv.Lines);
  3393. Assert.Equal (new (7, 1), tv.CursorPosition);
  3394. for (var i = 0; i < ntimes; i++)
  3395. {
  3396. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3397. }
  3398. Assert.Equal (
  3399. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3400. tv.Text
  3401. );
  3402. Assert.Equal (3, tv.Lines);
  3403. Assert.Equal (new (7, 1), tv.CursorPosition);
  3404. for (var i = 0; i < ntimes; i++)
  3405. {
  3406. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3407. }
  3408. Assert.Equal (
  3409. $"This is the first line.{Environment.NewLine}This ise second line.{Environment.NewLine}This is the third line.",
  3410. tv.Text
  3411. );
  3412. Assert.Equal (3, tv.Lines);
  3413. Assert.Equal (new (7, 1), tv.CursorPosition);
  3414. top.Dispose ();
  3415. }
  3416. [Fact]
  3417. [AutoInitShutdown]
  3418. public void HistoryText_Undo_Redo_Single_Line_InsertText ()
  3419. {
  3420. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  3421. var tv = new TextView { Width = 10, Height = 2, Text = text };
  3422. Toplevel top = new ();
  3423. top.Add (tv);
  3424. Application.Begin (top);
  3425. Assert.Equal (
  3426. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3427. tv.Text
  3428. );
  3429. Assert.Equal (3, tv.Lines);
  3430. Assert.Equal (Point.Empty, tv.CursorPosition);
  3431. var messy = " messy";
  3432. tv.CursorPosition = new (7, 1);
  3433. tv.InsertText (messy);
  3434. Assert.Equal (
  3435. $"This is the first line.{Environment.NewLine}This is messy the second line.{Environment.NewLine}This is the third line.",
  3436. tv.Text
  3437. );
  3438. Assert.Equal (3, tv.Lines);
  3439. Assert.Equal (new (13, 1), tv.CursorPosition);
  3440. for (var i = 0; i < messy.Length; i++)
  3441. {
  3442. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3443. }
  3444. Assert.Equal (
  3445. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3446. tv.Text
  3447. );
  3448. Assert.Equal (3, tv.Lines);
  3449. Assert.Equal (new (7, 1), tv.CursorPosition);
  3450. for (var i = 0; i < messy.Length; i++)
  3451. {
  3452. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3453. }
  3454. Assert.Equal (
  3455. $"This is the first line.{Environment.NewLine}This is messy the second line.{Environment.NewLine}This is the third line.",
  3456. tv.Text
  3457. );
  3458. Assert.Equal (3, tv.Lines);
  3459. Assert.Equal (new (13, 1), tv.CursorPosition);
  3460. top.Dispose ();
  3461. }
  3462. [Fact]
  3463. [AutoInitShutdown]
  3464. public void HistoryText_Undo_Redo_Single_Line_Selected_DeleteCharLeft ()
  3465. {
  3466. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  3467. var tv = new TextView { Width = 10, Height = 2, Text = text };
  3468. Toplevel top = new ();
  3469. top.Add (tv);
  3470. Application.Begin (top);
  3471. Assert.Equal (
  3472. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3473. tv.Text
  3474. );
  3475. Assert.Equal (3, tv.Lines);
  3476. Assert.Equal (Point.Empty, tv.CursorPosition);
  3477. var ntimes = 3;
  3478. tv.CursorPosition = new (7, 1);
  3479. tv.SelectionStartColumn = 11;
  3480. tv.SelectionStartRow = 1;
  3481. Assert.Equal (4, tv.SelectedLength);
  3482. for (var i = 0; i < ntimes; i++)
  3483. {
  3484. tv.DeleteCharLeft ();
  3485. }
  3486. Assert.Equal (
  3487. $"This is the first line.{Environment.NewLine}This second line.{Environment.NewLine}This is the third line.",
  3488. tv.Text
  3489. );
  3490. Assert.Equal (3, tv.Lines);
  3491. Assert.Equal (new (5, 1), tv.CursorPosition);
  3492. Assert.Equal (11, tv.SelectionStartColumn);
  3493. Assert.Equal (1, tv.SelectionStartRow);
  3494. Assert.Equal (0, tv.SelectedLength);
  3495. for (var i = 0; i < ntimes; i++)
  3496. {
  3497. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3498. }
  3499. Assert.Equal (
  3500. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3501. tv.Text
  3502. );
  3503. Assert.Equal (3, tv.Lines);
  3504. Assert.Equal (new (7, 1), tv.CursorPosition);
  3505. Assert.Equal (11, tv.SelectionStartColumn);
  3506. Assert.Equal (1, tv.SelectionStartRow);
  3507. Assert.Equal (0, tv.SelectedLength);
  3508. for (var i = 0; i < ntimes; i++)
  3509. {
  3510. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3511. }
  3512. Assert.Equal (
  3513. $"This is the first line.{Environment.NewLine}This second line.{Environment.NewLine}This is the third line.",
  3514. tv.Text
  3515. );
  3516. Assert.Equal (3, tv.Lines);
  3517. Assert.Equal (new (5, 1), tv.CursorPosition);
  3518. Assert.Equal (11, tv.SelectionStartColumn);
  3519. Assert.Equal (1, tv.SelectionStartRow);
  3520. Assert.Equal (0, tv.SelectedLength);
  3521. top.Dispose ();
  3522. }
  3523. [Fact]
  3524. [AutoInitShutdown]
  3525. public void HistoryText_Undo_Redo_Single_Line_Selected_DeleteCharRight ()
  3526. {
  3527. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  3528. var tv = new TextView { Width = 10, Height = 2, Text = text };
  3529. Toplevel top = new ();
  3530. top.Add (tv);
  3531. Application.Begin (top);
  3532. Assert.Equal (
  3533. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3534. tv.Text
  3535. );
  3536. Assert.Equal (3, tv.Lines);
  3537. Assert.Equal (Point.Empty, tv.CursorPosition);
  3538. var ntimes = 3;
  3539. tv.CursorPosition = new (7, 1);
  3540. tv.SelectionStartColumn = 11;
  3541. tv.SelectionStartRow = 1;
  3542. Assert.Equal (4, tv.SelectedLength);
  3543. for (var i = 0; i < ntimes; i++)
  3544. {
  3545. tv.DeleteCharRight ();
  3546. }
  3547. Assert.Equal (
  3548. $"This is the first line.{Environment.NewLine}This isecond line.{Environment.NewLine}This is the third line.",
  3549. tv.Text
  3550. );
  3551. Assert.Equal (3, tv.Lines);
  3552. Assert.Equal (new (7, 1), tv.CursorPosition);
  3553. Assert.Equal (11, tv.SelectionStartColumn);
  3554. Assert.Equal (1, tv.SelectionStartRow);
  3555. Assert.Equal (0, tv.SelectedLength);
  3556. for (var i = 0; i < ntimes; i++)
  3557. {
  3558. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3559. }
  3560. Assert.Equal (
  3561. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3562. tv.Text
  3563. );
  3564. Assert.Equal (3, tv.Lines);
  3565. Assert.Equal (new (7, 1), tv.CursorPosition);
  3566. Assert.Equal (11, tv.SelectionStartColumn);
  3567. Assert.Equal (1, tv.SelectionStartRow);
  3568. Assert.Equal (0, tv.SelectedLength);
  3569. for (var i = 0; i < ntimes; i++)
  3570. {
  3571. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3572. }
  3573. Assert.Equal (
  3574. $"This is the first line.{Environment.NewLine}This isecond line.{Environment.NewLine}This is the third line.",
  3575. tv.Text
  3576. );
  3577. Assert.Equal (3, tv.Lines);
  3578. Assert.Equal (new (7, 1), tv.CursorPosition);
  3579. Assert.Equal (11, tv.SelectionStartColumn);
  3580. Assert.Equal (1, tv.SelectionStartRow);
  3581. Assert.Equal (0, tv.SelectedLength);
  3582. top.Dispose ();
  3583. }
  3584. [Fact]
  3585. [AutoInitShutdown]
  3586. public void HistoryText_Undo_Redo_Single_Line_Selected_InsertText ()
  3587. {
  3588. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  3589. var tv = new TextView { Width = 10, Height = 2, Text = text };
  3590. Toplevel top = new ();
  3591. top.Add (tv);
  3592. Application.Begin (top);
  3593. Assert.Equal (
  3594. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3595. tv.Text
  3596. );
  3597. Assert.Equal (3, tv.Lines);
  3598. Assert.Equal (Point.Empty, tv.CursorPosition);
  3599. var messy = " messy";
  3600. tv.CursorPosition = new (7, 1);
  3601. tv.SelectionStartColumn = 11;
  3602. tv.SelectionStartRow = 1;
  3603. Assert.Equal (4, tv.SelectedLength);
  3604. tv.InsertText (messy);
  3605. Assert.Equal (
  3606. $"This is the first line.{Environment.NewLine}This is messy second line.{Environment.NewLine}This is the third line.",
  3607. tv.Text
  3608. );
  3609. Assert.Equal (3, tv.Lines);
  3610. Assert.Equal (new (13, 1), tv.CursorPosition);
  3611. Assert.Equal (11, tv.SelectionStartColumn);
  3612. Assert.Equal (1, tv.SelectionStartRow);
  3613. Assert.Equal (0, tv.SelectedLength);
  3614. for (var i = 0; i < messy.Length; i++)
  3615. {
  3616. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3617. }
  3618. Assert.Equal (
  3619. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3620. tv.Text
  3621. );
  3622. Assert.Equal (3, tv.Lines);
  3623. Assert.Equal (new (7, 1), tv.CursorPosition);
  3624. Assert.Equal (11, tv.SelectionStartColumn);
  3625. Assert.Equal (1, tv.SelectionStartRow);
  3626. Assert.Equal (0, tv.SelectedLength);
  3627. for (var i = 0; i < messy.Length; i++)
  3628. {
  3629. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3630. }
  3631. Assert.Equal (
  3632. $"This is the first line.{Environment.NewLine}This is messy second line.{Environment.NewLine}This is the third line.",
  3633. tv.Text
  3634. );
  3635. Assert.Equal (3, tv.Lines);
  3636. Assert.Equal (new (13, 1), tv.CursorPosition);
  3637. Assert.Equal (11, tv.SelectionStartColumn);
  3638. Assert.Equal (1, tv.SelectionStartRow);
  3639. Assert.Equal (0, tv.SelectedLength);
  3640. top.Dispose ();
  3641. }
  3642. [Fact]
  3643. public void HistoryText_Undo_Redo_Single_Line_Selected_Return ()
  3644. {
  3645. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  3646. var tv = new TextView { Text = text };
  3647. tv.SelectionStartColumn = 12;
  3648. tv.CursorPosition = new (17, 0);
  3649. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  3650. Assert.Equal (
  3651. $"This is the {Environment.NewLine} line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3652. tv.Text
  3653. );
  3654. Assert.Equal (4, tv.Lines);
  3655. Assert.Equal (new (0, 1), tv.CursorPosition);
  3656. // Undo
  3657. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3658. Assert.Equal (
  3659. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3660. tv.Text
  3661. );
  3662. Assert.Equal (3, tv.Lines);
  3663. Assert.Equal (new (17, 0), tv.CursorPosition);
  3664. Assert.False (tv.IsDirty);
  3665. // Redo
  3666. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3667. Assert.Equal (
  3668. $"This is the {Environment.NewLine} line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3669. tv.Text
  3670. );
  3671. Assert.Equal (4, tv.Lines);
  3672. Assert.Equal (new (0, 1), tv.CursorPosition);
  3673. // Undo
  3674. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3675. Assert.Equal (
  3676. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3677. tv.Text
  3678. );
  3679. Assert.Equal (3, tv.Lines);
  3680. Assert.Equal (new (17, 0), tv.CursorPosition);
  3681. Assert.False (tv.IsDirty);
  3682. // Redo
  3683. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3684. Assert.Equal (
  3685. $"This is the {Environment.NewLine} line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3686. tv.Text
  3687. );
  3688. Assert.Equal (4, tv.Lines);
  3689. Assert.Equal (new (0, 1), tv.CursorPosition);
  3690. }
  3691. [Fact]
  3692. public void HistoryText_Undo_Redo_Single_Second_Line_Selected_Return ()
  3693. {
  3694. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  3695. var tv = new TextView { Text = text };
  3696. tv.SelectionStartColumn = 12;
  3697. tv.SelectionStartRow = 1;
  3698. tv.CursorPosition = new (18, 1);
  3699. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  3700. Assert.Equal (
  3701. $"This is the first line.{Environment.NewLine}This is the {Environment.NewLine} line.{Environment.NewLine}This is the third line.",
  3702. tv.Text
  3703. );
  3704. Assert.Equal (4, tv.Lines);
  3705. Assert.Equal (new (0, 2), tv.CursorPosition);
  3706. // Undo
  3707. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3708. Assert.Equal (
  3709. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3710. tv.Text
  3711. );
  3712. Assert.Equal (3, tv.Lines);
  3713. Assert.Equal (new (18, 1), tv.CursorPosition);
  3714. Assert.False (tv.IsDirty);
  3715. // Redo
  3716. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3717. Assert.Equal (
  3718. $"This is the first line.{Environment.NewLine}This is the {Environment.NewLine} line.{Environment.NewLine}This is the third line.",
  3719. tv.Text
  3720. );
  3721. Assert.Equal (4, tv.Lines);
  3722. Assert.Equal (new (0, 2), tv.CursorPosition);
  3723. // Undo
  3724. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3725. Assert.Equal (
  3726. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3727. tv.Text
  3728. );
  3729. Assert.Equal (3, tv.Lines);
  3730. Assert.Equal (new (18, 1), tv.CursorPosition);
  3731. Assert.False (tv.IsDirty);
  3732. // Redo
  3733. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3734. Assert.Equal (
  3735. $"This is the first line.{Environment.NewLine}This is the {Environment.NewLine} line.{Environment.NewLine}This is the third line.",
  3736. tv.Text
  3737. );
  3738. Assert.Equal (4, tv.Lines);
  3739. Assert.Equal (new (0, 2), tv.CursorPosition);
  3740. }
  3741. [Fact]
  3742. public void HistoryText_Undo_Redo_Single_Second_Line_Selected_Return_And_InsertText ()
  3743. {
  3744. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  3745. var tv = new TextView { Text = text };
  3746. tv.SelectionStartColumn = 12;
  3747. tv.SelectionStartRow = 1;
  3748. tv.CursorPosition = new (18, 1);
  3749. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  3750. Assert.Equal (
  3751. $"This is the first line.{Environment.NewLine}This is the {Environment.NewLine} line.{Environment.NewLine}This is the third line.",
  3752. tv.Text
  3753. );
  3754. Assert.Equal (4, tv.Lines);
  3755. Assert.Equal (new (0, 2), tv.CursorPosition);
  3756. Assert.True (tv.NewKeyDownEvent (Key.A));
  3757. Assert.Equal (
  3758. $"This is the first line.{Environment.NewLine}This is the {Environment.NewLine}a line.{Environment.NewLine}This is the third line.",
  3759. tv.Text
  3760. );
  3761. Assert.Equal (4, tv.Lines);
  3762. Assert.Equal (new (1, 2), tv.CursorPosition);
  3763. // Undo
  3764. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3765. Assert.Equal (
  3766. $"This is the first line.{Environment.NewLine}This is the {Environment.NewLine} line.{Environment.NewLine}This is the third line.",
  3767. tv.Text
  3768. );
  3769. Assert.Equal (4, tv.Lines);
  3770. Assert.Equal (new (0, 2), tv.CursorPosition);
  3771. Assert.True (tv.IsDirty);
  3772. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3773. Assert.Equal (
  3774. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3775. tv.Text
  3776. );
  3777. Assert.Equal (3, tv.Lines);
  3778. Assert.Equal (new (18, 1), tv.CursorPosition);
  3779. Assert.False (tv.IsDirty);
  3780. // Redo
  3781. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3782. Assert.Equal (
  3783. $"This is the first line.{Environment.NewLine}This is the {Environment.NewLine} line.{Environment.NewLine}This is the third line.",
  3784. tv.Text
  3785. );
  3786. Assert.Equal (4, tv.Lines);
  3787. Assert.Equal (new (0, 2), tv.CursorPosition);
  3788. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3789. Assert.Equal (
  3790. $"This is the first line.{Environment.NewLine}This is the {Environment.NewLine}a line.{Environment.NewLine}This is the third line.",
  3791. tv.Text
  3792. );
  3793. Assert.Equal (4, tv.Lines);
  3794. Assert.Equal (new (1, 2), tv.CursorPosition);
  3795. // Undo
  3796. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3797. Assert.Equal (
  3798. $"This is the first line.{Environment.NewLine}This is the {Environment.NewLine} line.{Environment.NewLine}This is the third line.",
  3799. tv.Text
  3800. );
  3801. Assert.Equal (4, tv.Lines);
  3802. Assert.Equal (new (0, 2), tv.CursorPosition);
  3803. Assert.True (tv.IsDirty);
  3804. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3805. Assert.Equal (
  3806. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3807. tv.Text
  3808. );
  3809. Assert.Equal (3, tv.Lines);
  3810. Assert.Equal (new (18, 1), tv.CursorPosition);
  3811. Assert.False (tv.IsDirty);
  3812. // Redo
  3813. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3814. Assert.Equal (
  3815. $"This is the first line.{Environment.NewLine}This is the {Environment.NewLine} line.{Environment.NewLine}This is the third line.",
  3816. tv.Text
  3817. );
  3818. Assert.Equal (4, tv.Lines);
  3819. Assert.Equal (new (0, 2), tv.CursorPosition);
  3820. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3821. Assert.Equal (
  3822. $"This is the first line.{Environment.NewLine}This is the {Environment.NewLine}a line.{Environment.NewLine}This is the third line.",
  3823. tv.Text
  3824. );
  3825. Assert.Equal (4, tv.Lines);
  3826. Assert.Equal (new (1, 2), tv.CursorPosition);
  3827. }
  3828. [Fact]
  3829. public void HistoryText_Undo_Redo_Three_Line_Selected_Return ()
  3830. {
  3831. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  3832. var tv = new TextView { Text = text };
  3833. tv.SelectionStartColumn = 12;
  3834. tv.CursorPosition = new (17, 2);
  3835. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  3836. Assert.Equal ($"This is the {Environment.NewLine} line.", tv.Text);
  3837. Assert.Equal (2, tv.Lines);
  3838. Assert.Equal (new (0, 1), tv.CursorPosition);
  3839. // Undo
  3840. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3841. Assert.Equal (
  3842. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3843. tv.Text
  3844. );
  3845. Assert.Equal (3, tv.Lines);
  3846. Assert.Equal (new (17, 2), tv.CursorPosition);
  3847. Assert.False (tv.IsDirty);
  3848. // Redo
  3849. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3850. Assert.Equal ($"This is the {Environment.NewLine} line.", tv.Text);
  3851. Assert.Equal (2, tv.Lines);
  3852. Assert.Equal (new (0, 1), tv.CursorPosition);
  3853. // Undo
  3854. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3855. Assert.Equal (
  3856. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3857. tv.Text
  3858. );
  3859. Assert.Equal (3, tv.Lines);
  3860. Assert.Equal (new (17, 2), tv.CursorPosition);
  3861. Assert.False (tv.IsDirty);
  3862. // Redo
  3863. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3864. Assert.Equal ($"This is the {Environment.NewLine} line.", tv.Text);
  3865. Assert.Equal (2, tv.Lines);
  3866. Assert.Equal (new (0, 1), tv.CursorPosition);
  3867. }
  3868. [Fact]
  3869. public void HistoryText_Undo_Redo_Two_Line_Selected_Return ()
  3870. {
  3871. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  3872. var tv = new TextView { Text = text };
  3873. tv.SelectionStartColumn = 12;
  3874. tv.CursorPosition = new (18, 1);
  3875. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  3876. Assert.Equal (
  3877. $"This is the {Environment.NewLine} line.{Environment.NewLine}This is the third line.",
  3878. tv.Text
  3879. );
  3880. Assert.Equal (3, tv.Lines);
  3881. Assert.Equal (new (0, 1), tv.CursorPosition);
  3882. // Undo
  3883. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3884. Assert.Equal (
  3885. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3886. tv.Text
  3887. );
  3888. Assert.Equal (3, tv.Lines);
  3889. Assert.Equal (new (18, 1), tv.CursorPosition);
  3890. Assert.False (tv.IsDirty);
  3891. // Redo
  3892. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3893. Assert.Equal (
  3894. $"This is the {Environment.NewLine} line.{Environment.NewLine}This is the third line.",
  3895. tv.Text
  3896. );
  3897. Assert.Equal (3, tv.Lines);
  3898. Assert.Equal (new (0, 1), tv.CursorPosition);
  3899. // Undo
  3900. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3901. Assert.Equal (
  3902. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  3903. tv.Text
  3904. );
  3905. Assert.Equal (3, tv.Lines);
  3906. Assert.Equal (new (18, 1), tv.CursorPosition);
  3907. Assert.False (tv.IsDirty);
  3908. // Redo
  3909. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3910. Assert.Equal (
  3911. $"This is the {Environment.NewLine} line.{Environment.NewLine}This is the third line.",
  3912. tv.Text
  3913. );
  3914. Assert.Equal (3, tv.Lines);
  3915. Assert.Equal (new (0, 1), tv.CursorPosition);
  3916. }
  3917. [Fact]
  3918. public void HistoryText_Undo_Redo_ApplyCellsAttribute ()
  3919. {
  3920. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  3921. var tv = new TextView { Text = text };
  3922. tv.SelectionStartColumn = 12;
  3923. tv.CursorPosition = new (18, 1);
  3924. if (Environment.NewLine.Length == 2)
  3925. {
  3926. Assert.Equal (31, tv.SelectedLength);
  3927. }
  3928. else
  3929. {
  3930. Assert.Equal (30, tv.SelectedLength);
  3931. }
  3932. Assert.Equal ($"first line.{Environment.NewLine}This is the second", tv.SelectedText);
  3933. Assert.Equal ($"first line.{Environment.NewLine}This is the second", Cell.ToString (tv.SelectedCellsList));
  3934. Assert.Equal (new (18, 1), tv.CursorPosition);
  3935. Assert.False (tv.IsDirty);
  3936. AssertNullAttribute ();
  3937. tv.ApplyCellsAttribute (new (Color.Red, Color.Green));
  3938. AssertRedGreenAttribute ();
  3939. Assert.Equal (0, tv.SelectedLength);
  3940. Assert.Equal ("", tv.SelectedText);
  3941. Assert.Equal ($"first line.{Environment.NewLine}This is the second", Cell.ToString (tv.SelectedCellsList));
  3942. Assert.Equal (new (18, 1), tv.CursorPosition);
  3943. Assert.True (tv.IsDirty);
  3944. // Undo
  3945. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  3946. AssertNullAttribute ();
  3947. Assert.Equal (12, tv.SelectionStartColumn);
  3948. Assert.Equal (0, tv.SelectionStartRow);
  3949. Assert.Equal (0, tv.SelectedLength);
  3950. Assert.Equal ("", tv.SelectedText);
  3951. Assert.Empty (tv.SelectedCellsList);
  3952. Assert.Equal (new (12, 0), tv.CursorPosition);
  3953. Assert.False (tv.IsDirty);
  3954. // Redo
  3955. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  3956. AssertRedGreenAttribute ();
  3957. Assert.Equal (12, tv.SelectionStartColumn);
  3958. Assert.Equal (0, tv.SelectionStartRow);
  3959. Assert.Equal (0, tv.SelectedLength);
  3960. Assert.Equal ("", tv.SelectedText);
  3961. Assert.Empty (tv.SelectedCellsList);
  3962. Assert.Equal (new (12, 0), tv.CursorPosition);
  3963. Assert.True (tv.IsDirty);
  3964. void AssertNullAttribute ()
  3965. {
  3966. tv.GetRegion (out List<List<Cell>> region, 0, 12, 1, 18);
  3967. foreach (List<Cell> cells in region)
  3968. {
  3969. foreach (Cell cell in cells)
  3970. {
  3971. Assert.Null (cell.Attribute);
  3972. }
  3973. }
  3974. }
  3975. void AssertRedGreenAttribute ()
  3976. {
  3977. tv.GetRegion (out List<List<Cell>> region, 0, 12, 1, 18);
  3978. foreach (List<Cell> cells in region)
  3979. {
  3980. foreach (Cell cell in cells)
  3981. {
  3982. Assert.Equal ("[Red,Green,None]", cell.Attribute.ToString ());
  3983. }
  3984. }
  3985. }
  3986. }
  3987. [Fact]
  3988. public void Internal_Tests ()
  3989. {
  3990. var txt = "This is a text.";
  3991. List<Cell> txtRunes = Cell.StringToCells (txt);
  3992. Assert.Equal (txt.Length, txtRunes.Count);
  3993. Assert.Equal ('T', txtRunes [0].Rune.Value);
  3994. Assert.Equal ('h', txtRunes [1].Rune.Value);
  3995. Assert.Equal ('i', txtRunes [2].Rune.Value);
  3996. Assert.Equal ('s', txtRunes [3].Rune.Value);
  3997. Assert.Equal (' ', txtRunes [4].Rune.Value);
  3998. Assert.Equal ('i', txtRunes [5].Rune.Value);
  3999. Assert.Equal ('s', txtRunes [6].Rune.Value);
  4000. Assert.Equal (' ', txtRunes [7].Rune.Value);
  4001. Assert.Equal ('a', txtRunes [8].Rune.Value);
  4002. Assert.Equal (' ', txtRunes [9].Rune.Value);
  4003. Assert.Equal ('t', txtRunes [10].Rune.Value);
  4004. Assert.Equal ('e', txtRunes [11].Rune.Value);
  4005. Assert.Equal ('x', txtRunes [12].Rune.Value);
  4006. Assert.Equal ('t', txtRunes [13].Rune.Value);
  4007. Assert.Equal ('.', txtRunes [^1].Rune.Value);
  4008. var col = 0;
  4009. Assert.True (TextModel.SetCol (ref col, 80, 79));
  4010. Assert.False (TextModel.SetCol (ref col, 80, 80));
  4011. Assert.Equal (79, col);
  4012. var start = 0;
  4013. var x = 8;
  4014. Assert.Equal (8, TextModel.GetColFromX (txtRunes, start, x));
  4015. Assert.Equal ('a', txtRunes [start + x].Rune.Value);
  4016. start = 1;
  4017. x = 7;
  4018. Assert.Equal (7, TextModel.GetColFromX (txtRunes, start, x));
  4019. Assert.Equal ('a', txtRunes [start + x].Rune.Value);
  4020. Assert.Equal ((15, 15), TextModel.DisplaySize (txtRunes));
  4021. Assert.Equal ((6, 6), TextModel.DisplaySize (txtRunes, 1, 7));
  4022. Assert.Equal (0, TextModel.CalculateLeftColumn (txtRunes, 0, 7, 8));
  4023. Assert.Equal (1, TextModel.CalculateLeftColumn (txtRunes, 0, 8, 8));
  4024. Assert.Equal (2, TextModel.CalculateLeftColumn (txtRunes, 0, 9, 8));
  4025. var tm = new TextModel ();
  4026. tm.AddLine (0, Cell.StringToCells ("This is first line."));
  4027. tm.AddLine (1, Cell.StringToCells ("This is last line."));
  4028. Assert.Equal ((new Point (2, 0), true), tm.FindNextText ("is", out bool gaveFullTurn));
  4029. Assert.False (gaveFullTurn);
  4030. Assert.Equal ((new Point (5, 0), true), tm.FindNextText ("is", out gaveFullTurn));
  4031. Assert.False (gaveFullTurn);
  4032. Assert.Equal ((new Point (2, 1), true), tm.FindNextText ("is", out gaveFullTurn));
  4033. Assert.False (gaveFullTurn);
  4034. Assert.Equal ((new Point (5, 1), true), tm.FindNextText ("is", out gaveFullTurn));
  4035. Assert.False (gaveFullTurn);
  4036. Assert.Equal ((new Point (2, 0), true), tm.FindNextText ("is", out gaveFullTurn));
  4037. Assert.True (gaveFullTurn);
  4038. tm.ResetContinuousFind (Point.Empty);
  4039. Assert.Equal ((new Point (5, 1), true), tm.FindPreviousText ("is", out gaveFullTurn));
  4040. Assert.False (gaveFullTurn);
  4041. Assert.Equal ((new Point (2, 1), true), tm.FindPreviousText ("is", out gaveFullTurn));
  4042. Assert.False (gaveFullTurn);
  4043. Assert.Equal ((new Point (5, 0), true), tm.FindPreviousText ("is", out gaveFullTurn));
  4044. Assert.False (gaveFullTurn);
  4045. Assert.Equal ((new Point (2, 0), true), tm.FindPreviousText ("is", out gaveFullTurn));
  4046. Assert.False (gaveFullTurn);
  4047. Assert.Equal ((new Point (5, 1), true), tm.FindPreviousText ("is", out gaveFullTurn));
  4048. Assert.True (gaveFullTurn);
  4049. Assert.Equal ((new Point (9, 1), true), tm.ReplaceAllText ("is", false, false, "really"));
  4050. Assert.Equal (Cell.StringToCells ("Threally really first line."), tm.GetLine (0));
  4051. Assert.Equal (Cell.StringToCells ("Threally really last line."), tm.GetLine (1));
  4052. tm = new ();
  4053. tm.AddLine (0, Cell.StringToCells ("This is first line."));
  4054. tm.AddLine (1, Cell.StringToCells ("This is last line."));
  4055. Assert.Equal ((new Point (5, 1), true), tm.ReplaceAllText ("is", false, true, "really"));
  4056. Assert.Equal (Cell.StringToCells ("This really first line."), tm.GetLine (0));
  4057. Assert.Equal (Cell.StringToCells ("This really last line."), tm.GetLine (1));
  4058. }
  4059. [Fact]
  4060. [AutoInitShutdown (useFakeClipboard: true)]
  4061. public void KeyBindings_Command ()
  4062. {
  4063. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  4064. var tv = new TextView { Width = 10, Height = 2, Text = text };
  4065. Toplevel top = new ();
  4066. top.Add (tv);
  4067. Application.Begin (top);
  4068. Assert.Equal (
  4069. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  4070. tv.Text
  4071. );
  4072. Assert.Equal (3, tv.Lines);
  4073. Assert.Equal (Point.Empty, tv.CursorPosition);
  4074. Assert.False (tv.ReadOnly);
  4075. Assert.True (tv.CanFocus);
  4076. Assert.False (tv.IsSelecting);
  4077. var g = (SingleWordSuggestionGenerator)tv.Autocomplete.SuggestionGenerator;
  4078. tv.CanFocus = false;
  4079. Assert.True (tv.NewKeyDownEvent (Key.CursorLeft));
  4080. Assert.False (tv.IsSelecting);
  4081. tv.CanFocus = true;
  4082. Assert.False (tv.NewKeyDownEvent (Key.CursorLeft));
  4083. Assert.False (tv.IsSelecting);
  4084. Assert.True (tv.NewKeyDownEvent (Key.CursorRight));
  4085. Assert.Equal (new (1, 0), tv.CursorPosition);
  4086. Assert.False (tv.IsSelecting);
  4087. Assert.True (tv.NewKeyDownEvent (Key.End.WithCtrl));
  4088. Assert.Equal (2, tv.CurrentRow);
  4089. Assert.Equal (23, tv.CurrentColumn);
  4090. Assert.Equal (tv.CurrentColumn, tv.GetCurrentLine ().Count);
  4091. Assert.Equal (new (23, 2), tv.CursorPosition);
  4092. Assert.False (tv.IsSelecting);
  4093. Assert.False (tv.NewKeyDownEvent (Key.CursorRight));
  4094. Assert.NotNull (tv.Autocomplete);
  4095. Assert.Empty (g.AllSuggestions);
  4096. Assert.False (tv.IsSelecting);
  4097. Assert.True (tv.NewKeyDownEvent (Key.F.WithShift));
  4098. tv.Draw ();
  4099. Assert.Equal (
  4100. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.F",
  4101. tv.Text
  4102. );
  4103. Assert.Equal (new (24, 2), tv.CursorPosition);
  4104. Assert.Empty (tv.Autocomplete.Suggestions);
  4105. Assert.False (tv.IsSelecting);
  4106. Assert.True (tv.NewKeyDownEvent (Key.Z.WithCtrl));
  4107. tv.Draw ();
  4108. Assert.Equal (
  4109. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  4110. tv.Text
  4111. );
  4112. Assert.Equal (new (23, 2), tv.CursorPosition);
  4113. Assert.Empty (tv.Autocomplete.Suggestions);
  4114. Assert.False (tv.IsSelecting);
  4115. Assert.True (tv.NewKeyDownEvent (Key.R.WithCtrl));
  4116. tv.Draw ();
  4117. Assert.Equal (
  4118. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.F",
  4119. tv.Text
  4120. );
  4121. Assert.Equal (new (24, 2), tv.CursorPosition);
  4122. Assert.Empty (tv.Autocomplete.Suggestions);
  4123. Assert.False (tv.IsSelecting);
  4124. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  4125. Assert.Equal (
  4126. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  4127. tv.Text
  4128. );
  4129. Assert.Equal (new (23, 2), tv.CursorPosition);
  4130. g.AllSuggestions = Regex.Matches (tv.Text, "\\w+")
  4131. .Select (s => s.Value)
  4132. .Distinct ()
  4133. .ToList ();
  4134. Assert.Equal (7, g.AllSuggestions.Count);
  4135. Assert.Equal ("This", g.AllSuggestions [0]);
  4136. Assert.Equal ("is", g.AllSuggestions [1]);
  4137. Assert.Equal ("the", g.AllSuggestions [2]);
  4138. Assert.Equal ("first", g.AllSuggestions [3]);
  4139. Assert.Equal ("line", g.AllSuggestions [4]);
  4140. Assert.Equal ("second", g.AllSuggestions [5]);
  4141. Assert.Equal ("third", g.AllSuggestions [^1]);
  4142. Assert.False (tv.IsSelecting);
  4143. Assert.True (tv.NewKeyDownEvent (Key.F.WithShift));
  4144. tv.Draw ();
  4145. Assert.Equal (
  4146. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.F",
  4147. tv.Text
  4148. );
  4149. Assert.Equal (new (24, 2), tv.CursorPosition);
  4150. Assert.Single (tv.Autocomplete.Suggestions);
  4151. Assert.Equal ("first", tv.Autocomplete.Suggestions [0].Replacement);
  4152. Assert.False (tv.IsSelecting);
  4153. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  4154. Assert.Equal (
  4155. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4156. tv.Text
  4157. );
  4158. Assert.Equal (new (28, 2), tv.CursorPosition);
  4159. Assert.Empty (tv.Autocomplete.Suggestions);
  4160. Assert.False (tv.Autocomplete.Visible);
  4161. g.AllSuggestions = new ();
  4162. tv.Autocomplete.ClearSuggestions ();
  4163. Assert.Empty (g.AllSuggestions);
  4164. Assert.Empty (tv.Autocomplete.Suggestions);
  4165. Assert.False (tv.IsSelecting);
  4166. Assert.True (tv.NewKeyDownEvent (Key.PageUp));
  4167. Assert.Equal (24, tv.GetCurrentLine ().Count);
  4168. Assert.Equal (new (24, 1), tv.CursorPosition);
  4169. Assert.False (tv.IsSelecting);
  4170. Assert.True (tv.NewKeyDownEvent (new (Key.PageUp)));
  4171. Assert.Equal (23, tv.GetCurrentLine ().Count);
  4172. Assert.Equal (new (23, 0), tv.CursorPosition);
  4173. Assert.False (tv.IsSelecting);
  4174. Assert.True (tv.NewKeyDownEvent (Key.PageDown));
  4175. Assert.Equal (24, tv.GetCurrentLine ().Count);
  4176. Assert.Equal (new (23, 1), tv.CursorPosition); // gets the previous length
  4177. Assert.False (tv.IsSelecting);
  4178. Assert.True (tv.NewKeyDownEvent (Key.V.WithCtrl));
  4179. Assert.Equal (28, tv.GetCurrentLine ().Count);
  4180. Assert.Equal (new (23, 2), tv.CursorPosition); // gets the previous length
  4181. Assert.Equal (0, tv.SelectedLength);
  4182. Assert.Equal ("", tv.SelectedText);
  4183. Assert.False (tv.IsSelecting);
  4184. Assert.True (tv.NewKeyDownEvent (Key.PageUp.WithShift));
  4185. Assert.Equal (24, tv.GetCurrentLine ().Count);
  4186. Assert.Equal (new (23, 1), tv.CursorPosition); // gets the previous length
  4187. Assert.Equal (24 + Environment.NewLine.Length, tv.SelectedLength);
  4188. Assert.Equal ($".{Environment.NewLine}This is the third line.", tv.SelectedText);
  4189. Assert.True (tv.IsSelecting);
  4190. Assert.True (tv.NewKeyDownEvent (Key.PageDown.WithShift));
  4191. Assert.Equal (28, tv.GetCurrentLine ().Count);
  4192. Assert.Equal (new (23, 2), tv.CursorPosition); // gets the previous length
  4193. Assert.Equal (0, tv.SelectedLength);
  4194. Assert.Equal ("", tv.SelectedText);
  4195. Assert.True (tv.IsSelecting);
  4196. Assert.True (tv.NewKeyDownEvent (Key.Home.WithCtrl));
  4197. Assert.Equal (Point.Empty, tv.CursorPosition);
  4198. Assert.False (tv.IsSelecting);
  4199. Assert.True (tv.NewKeyDownEvent (Key.N.WithCtrl));
  4200. Assert.Equal (new (0, 1), tv.CursorPosition);
  4201. Assert.Equal (0, tv.SelectedLength);
  4202. Assert.Equal ("", tv.SelectedText);
  4203. Assert.False (tv.IsSelecting);
  4204. Assert.True (tv.NewKeyDownEvent (Key.P.WithCtrl));
  4205. Assert.Equal (Point.Empty, tv.CursorPosition);
  4206. Assert.Equal (0, tv.SelectedLength);
  4207. Assert.Equal ("", tv.SelectedText);
  4208. Assert.False (tv.IsSelecting);
  4209. Assert.True (tv.NewKeyDownEvent (Key.CursorDown));
  4210. Assert.Equal (new (0, 1), tv.CursorPosition);
  4211. Assert.Equal (0, tv.SelectedLength);
  4212. Assert.Equal ("", tv.SelectedText);
  4213. Assert.False (tv.IsSelecting);
  4214. Assert.True (tv.NewKeyDownEvent (Key.CursorUp));
  4215. Assert.Equal (Point.Empty, tv.CursorPosition);
  4216. Assert.Equal (0, tv.SelectedLength);
  4217. Assert.Equal ("", tv.SelectedText);
  4218. Assert.False (tv.IsSelecting);
  4219. Assert.True (tv.NewKeyDownEvent (Key.CursorDown.WithShift));
  4220. Assert.Equal (new (0, 1), tv.CursorPosition);
  4221. Assert.Equal (23 + Environment.NewLine.Length, tv.SelectedLength);
  4222. Assert.Equal ($"This is the first line.{Environment.NewLine}", tv.SelectedText);
  4223. Assert.True (tv.IsSelecting);
  4224. Assert.True (tv.NewKeyDownEvent (Key.CursorUp.WithShift));
  4225. Assert.Equal (Point.Empty, tv.CursorPosition);
  4226. Assert.Equal (0, tv.SelectedLength);
  4227. Assert.Equal ("", tv.SelectedText);
  4228. Assert.True (tv.IsSelecting);
  4229. Assert.True (tv.NewKeyDownEvent (Key.F.WithCtrl));
  4230. Assert.Equal (new (1, 0), tv.CursorPosition);
  4231. Assert.Equal (0, tv.SelectedLength);
  4232. Assert.Equal ("", tv.SelectedText);
  4233. Assert.False (tv.IsSelecting);
  4234. Assert.True (tv.NewKeyDownEvent (Key.B.WithCtrl));
  4235. Assert.Equal (Point.Empty, tv.CursorPosition);
  4236. Assert.Equal (0, tv.SelectedLength);
  4237. Assert.Equal ("", tv.SelectedText);
  4238. Assert.False (tv.IsSelecting);
  4239. Assert.True (tv.NewKeyDownEvent (Key.CursorRight));
  4240. Assert.Equal (new (1, 0), tv.CursorPosition);
  4241. Assert.Equal (0, tv.SelectedLength);
  4242. Assert.Equal ("", tv.SelectedText);
  4243. Assert.False (tv.IsSelecting);
  4244. Assert.True (tv.NewKeyDownEvent (Key.CursorLeft));
  4245. Assert.Equal (Point.Empty, tv.CursorPosition);
  4246. Assert.Equal (0, tv.SelectedLength);
  4247. Assert.Equal ("", tv.SelectedText);
  4248. Assert.False (tv.IsSelecting);
  4249. Assert.True (tv.NewKeyDownEvent (Key.CursorRight.WithShift));
  4250. Assert.Equal (new (1, 0), tv.CursorPosition);
  4251. Assert.Equal (1, tv.SelectedLength);
  4252. Assert.Equal ("T", tv.SelectedText);
  4253. Assert.True (tv.IsSelecting);
  4254. Assert.True (tv.NewKeyDownEvent (Key.CursorLeft.WithShift));
  4255. Assert.Equal (Point.Empty, tv.CursorPosition);
  4256. Assert.Equal (0, tv.SelectedLength);
  4257. Assert.Equal ("", tv.SelectedText);
  4258. Assert.True (tv.IsSelecting);
  4259. Assert.True (tv.NewKeyDownEvent (Key.Delete));
  4260. Assert.Equal (
  4261. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4262. tv.Text
  4263. );
  4264. Assert.Equal (Point.Empty, tv.CursorPosition);
  4265. Assert.Equal (0, tv.SelectedLength);
  4266. Assert.Equal ("", tv.SelectedText);
  4267. Assert.False (tv.IsSelecting);
  4268. Assert.True (tv.NewKeyDownEvent (Key.Delete));
  4269. Assert.Equal (
  4270. $"his is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4271. tv.Text
  4272. );
  4273. Assert.Equal (Point.Empty, tv.CursorPosition);
  4274. Assert.Equal (0, tv.SelectedLength);
  4275. Assert.Equal ("", tv.SelectedText);
  4276. Assert.False (tv.IsSelecting);
  4277. Assert.True (tv.NewKeyDownEvent (Key.D.WithCtrl));
  4278. Assert.Equal (
  4279. $"is is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4280. tv.Text
  4281. );
  4282. Assert.Equal (Point.Empty, tv.CursorPosition);
  4283. Assert.True (tv.NewKeyDownEvent (Key.End));
  4284. Assert.Equal (
  4285. $"is is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4286. tv.Text
  4287. );
  4288. Assert.Equal (new (21, 0), tv.CursorPosition);
  4289. Assert.False (tv.IsSelecting);
  4290. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  4291. Assert.Equal (
  4292. $"is is the first line{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4293. tv.Text
  4294. );
  4295. Assert.Equal (new (20, 0), tv.CursorPosition);
  4296. Assert.False (tv.IsSelecting);
  4297. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  4298. Assert.Equal (
  4299. $"is is the first lin{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4300. tv.Text
  4301. );
  4302. Assert.Equal (new (19, 0), tv.CursorPosition);
  4303. Assert.False (tv.IsSelecting);
  4304. Assert.True (tv.NewKeyDownEvent (Key.Home));
  4305. Assert.Equal (Point.Empty, tv.CursorPosition);
  4306. Assert.Equal (0, tv.SelectedLength);
  4307. Assert.Equal ("", tv.SelectedText);
  4308. Assert.False (tv.IsSelecting);
  4309. Assert.True (tv.NewKeyDownEvent (Key.End.WithShift));
  4310. Assert.Equal (new (19, 0), tv.CursorPosition);
  4311. Assert.Equal (19, tv.SelectedLength);
  4312. Assert.Equal ("is is the first lin", tv.SelectedText);
  4313. Assert.True (tv.IsSelecting);
  4314. Assert.True (tv.NewKeyDownEvent (Key.Home.WithShift));
  4315. Assert.Equal (Point.Empty, tv.CursorPosition);
  4316. Assert.Equal (0, tv.SelectedLength);
  4317. Assert.Equal ("", tv.SelectedText);
  4318. Assert.True (tv.IsSelecting);
  4319. Assert.True (tv.NewKeyDownEvent (Key.E.WithCtrl));
  4320. Assert.Equal (new (19, 0), tv.CursorPosition);
  4321. Assert.Equal (0, tv.SelectedLength);
  4322. Assert.Equal ("", tv.SelectedText);
  4323. Assert.False (tv.IsSelecting);
  4324. Assert.True (tv.NewKeyDownEvent (Key.Home));
  4325. Assert.Equal (Point.Empty, tv.CursorPosition);
  4326. Assert.Equal (0, tv.SelectedLength);
  4327. Assert.Equal ("", tv.SelectedText);
  4328. Assert.False (tv.IsSelecting);
  4329. Assert.True (tv.NewKeyDownEvent (Key.K.WithCtrl));
  4330. Assert.Equal (
  4331. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4332. tv.Text
  4333. );
  4334. Assert.Equal (Point.Empty, tv.CursorPosition);
  4335. Assert.Equal (0, tv.SelectedLength);
  4336. Assert.Equal ("", tv.SelectedText);
  4337. Assert.False (tv.IsSelecting);
  4338. Assert.Equal ("is is the first lin", Clipboard.Contents);
  4339. Assert.True (tv.NewKeyDownEvent (Key.Y.WithCtrl));
  4340. Assert.Equal (
  4341. $"is is the first lin{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4342. tv.Text
  4343. );
  4344. Assert.Equal (new (19, 0), tv.CursorPosition);
  4345. Assert.Equal (0, tv.SelectedLength);
  4346. Assert.Equal ("", tv.SelectedText);
  4347. Assert.False (tv.IsSelecting);
  4348. Assert.Equal ("is is the first lin", Clipboard.Contents);
  4349. tv.CursorPosition = Point.Empty;
  4350. Assert.True (tv.NewKeyDownEvent (Key.Delete.WithCtrl.WithShift));
  4351. Assert.Equal (
  4352. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4353. tv.Text
  4354. );
  4355. Assert.Equal (Point.Empty, tv.CursorPosition);
  4356. Assert.Equal (0, tv.SelectedLength);
  4357. Assert.Equal ("", tv.SelectedText);
  4358. Assert.False (tv.IsSelecting);
  4359. Assert.Equal ("is is the first lin", Clipboard.Contents);
  4360. Assert.True (tv.NewKeyDownEvent (Key.Y.WithCtrl));
  4361. Assert.Equal (
  4362. $"is is the first lin{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4363. tv.Text
  4364. );
  4365. Assert.Equal (new (19, 0), tv.CursorPosition);
  4366. Assert.Equal (0, tv.SelectedLength);
  4367. Assert.Equal ("", tv.SelectedText);
  4368. Assert.False (tv.IsSelecting);
  4369. Assert.Equal ("is is the first lin", Clipboard.Contents);
  4370. Assert.True (tv.NewKeyDownEvent (Key.Backspace.WithCtrl.WithShift));
  4371. Assert.Equal (
  4372. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4373. tv.Text
  4374. );
  4375. Assert.Equal (Point.Empty, tv.CursorPosition);
  4376. Assert.Equal (0, tv.SelectedLength);
  4377. Assert.Equal ("", tv.SelectedText);
  4378. Assert.False (tv.IsSelecting);
  4379. tv.ReadOnly = true;
  4380. Assert.True (tv.NewKeyDownEvent (Key.Y.WithCtrl));
  4381. Assert.Equal (
  4382. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4383. tv.Text
  4384. );
  4385. Assert.Equal (Point.Empty, tv.CursorPosition);
  4386. Assert.Equal (0, tv.SelectedLength);
  4387. Assert.Equal ("", tv.SelectedText);
  4388. Assert.False (tv.IsSelecting);
  4389. tv.ReadOnly = false;
  4390. Assert.True (tv.NewKeyDownEvent (Key.Y.WithCtrl));
  4391. Assert.Equal (
  4392. $"is is the first lin{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4393. tv.Text
  4394. );
  4395. Assert.Equal (new (19, 0), tv.CursorPosition);
  4396. Assert.Equal (0, tv.SelectedLength);
  4397. Assert.Equal ("", tv.SelectedText);
  4398. Assert.False (tv.IsSelecting);
  4399. Assert.Equal (0, tv.SelectionStartColumn);
  4400. Assert.Equal (0, tv.SelectionStartRow);
  4401. Assert.True (tv.NewKeyDownEvent (Key.Space.WithCtrl));
  4402. Assert.Equal (
  4403. $"is is the first lin{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4404. tv.Text
  4405. );
  4406. Assert.Equal (new (19, 0), tv.CursorPosition);
  4407. Assert.Equal (0, tv.SelectedLength);
  4408. Assert.Equal ("", tv.SelectedText);
  4409. Assert.True (tv.IsSelecting);
  4410. Assert.Equal (19, tv.SelectionStartColumn);
  4411. Assert.Equal (0, tv.SelectionStartRow);
  4412. Assert.True (tv.NewKeyDownEvent (Key.Space.WithCtrl));
  4413. Assert.Equal (
  4414. $"is is the first lin{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4415. tv.Text
  4416. );
  4417. Assert.Equal (new (19, 0), tv.CursorPosition);
  4418. Assert.Equal (0, tv.SelectedLength);
  4419. Assert.Equal ("", tv.SelectedText);
  4420. Assert.False (tv.IsSelecting);
  4421. Assert.Equal (19, tv.SelectionStartColumn);
  4422. Assert.Equal (0, tv.SelectionStartRow);
  4423. tv.SelectionStartColumn = 0;
  4424. Assert.True (tv.NewKeyDownEvent (Key.C.WithCtrl));
  4425. Assert.Equal (
  4426. $"is is the first lin{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4427. tv.Text
  4428. );
  4429. Assert.Equal (new (19, 0), tv.CursorPosition);
  4430. Assert.Equal (19, tv.SelectedLength);
  4431. Assert.Equal ("is is the first lin", tv.SelectedText);
  4432. Assert.True (tv.IsSelecting);
  4433. Assert.Equal (0, tv.SelectionStartColumn);
  4434. Assert.Equal (0, tv.SelectionStartRow);
  4435. Assert.True (tv.NewKeyDownEvent (Key.C.WithCtrl));
  4436. Assert.Equal (
  4437. $"is is the first lin{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4438. tv.Text
  4439. );
  4440. Assert.Equal (new (19, 0), tv.CursorPosition);
  4441. Assert.Equal (19, tv.SelectedLength);
  4442. Assert.Equal ("is is the first lin", tv.SelectedText);
  4443. Assert.True (tv.IsSelecting);
  4444. Assert.Equal (0, tv.SelectionStartColumn);
  4445. Assert.Equal (0, tv.SelectionStartRow);
  4446. Assert.True (tv.NewKeyDownEvent (Key.X.WithCtrl));
  4447. Assert.Equal (
  4448. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4449. tv.Text
  4450. );
  4451. Assert.Equal (Point.Empty, tv.CursorPosition);
  4452. Assert.Equal (0, tv.SelectedLength);
  4453. Assert.Equal ("", tv.SelectedText);
  4454. Assert.False (tv.IsSelecting);
  4455. Assert.Equal (0, tv.SelectionStartColumn);
  4456. Assert.Equal (0, tv.SelectionStartRow);
  4457. Assert.Equal ("is is the first lin", Clipboard.Contents);
  4458. Assert.True (tv.NewKeyDownEvent (Key.W.WithCtrl));
  4459. Assert.Equal (
  4460. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4461. tv.Text
  4462. );
  4463. Assert.Equal (Point.Empty, tv.CursorPosition);
  4464. Assert.Equal (0, tv.SelectedLength);
  4465. Assert.Equal ("", tv.SelectedText);
  4466. Assert.False (tv.IsSelecting);
  4467. Assert.Equal (0, tv.SelectionStartColumn);
  4468. Assert.Equal (0, tv.SelectionStartRow);
  4469. Assert.Equal ("", Clipboard.Contents);
  4470. Assert.True (tv.NewKeyDownEvent (Key.X.WithCtrl));
  4471. Assert.Equal (
  4472. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4473. tv.Text
  4474. );
  4475. Assert.Equal (Point.Empty, tv.CursorPosition);
  4476. Assert.Equal (0, tv.SelectedLength);
  4477. Assert.Equal ("", tv.SelectedText);
  4478. Assert.False (tv.IsSelecting);
  4479. Assert.Equal (0, tv.SelectionStartColumn);
  4480. Assert.Equal (0, tv.SelectionStartRow);
  4481. Assert.Equal ("", Clipboard.Contents);
  4482. Assert.True (tv.NewKeyDownEvent (Key.End.WithCtrl));
  4483. Assert.Equal (
  4484. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4485. tv.Text
  4486. );
  4487. Assert.Equal (new (28, 2), tv.CursorPosition);
  4488. Assert.Equal (0, tv.SelectedLength);
  4489. Assert.Equal ("", tv.SelectedText);
  4490. Assert.False (tv.IsSelecting);
  4491. Assert.True (tv.NewKeyDownEvent (Key.CursorLeft.WithCtrl));
  4492. Assert.Equal (
  4493. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4494. tv.Text
  4495. );
  4496. Assert.Equal (new (18, 2), tv.CursorPosition);
  4497. Assert.Equal (0, tv.SelectedLength);
  4498. Assert.Equal ("", tv.SelectedText);
  4499. Assert.False (tv.IsSelecting);
  4500. Assert.True (tv.NewKeyDownEvent (Key.CursorLeft.WithShift.WithCtrl));
  4501. Assert.Equal (
  4502. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4503. tv.Text
  4504. );
  4505. Assert.Equal (new (12, 2), tv.CursorPosition);
  4506. Assert.Equal (6, tv.SelectedLength);
  4507. Assert.Equal ("third ", tv.SelectedText);
  4508. Assert.True (tv.IsSelecting);
  4509. Assert.True (tv.NewKeyDownEvent (Key.CursorLeft.WithCtrl));
  4510. Assert.Equal (
  4511. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4512. tv.Text
  4513. );
  4514. Assert.Equal (new (8, 2), tv.CursorPosition);
  4515. Assert.Equal (0, tv.SelectedLength);
  4516. Assert.Equal ("", tv.SelectedText);
  4517. Assert.False (tv.IsSelecting);
  4518. Assert.True (tv.NewKeyDownEvent (Key.CursorRight.WithCtrl));
  4519. Assert.Equal (
  4520. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4521. tv.Text
  4522. );
  4523. Assert.Equal (new (12, 2), tv.CursorPosition);
  4524. Assert.Equal (0, tv.SelectedLength);
  4525. Assert.Equal ("", tv.SelectedText);
  4526. Assert.False (tv.IsSelecting);
  4527. Assert.True (tv.NewKeyDownEvent (Key.CursorRight.WithShift.WithCtrl));
  4528. Assert.Equal (
  4529. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4530. tv.Text
  4531. );
  4532. Assert.Equal (new (18, 2), tv.CursorPosition);
  4533. Assert.Equal (6, tv.SelectedLength);
  4534. Assert.Equal ("third ", tv.SelectedText);
  4535. Assert.True (tv.IsSelecting);
  4536. Assert.True (tv.NewKeyDownEvent (Key.CursorRight.WithCtrl));
  4537. Assert.Equal (
  4538. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4539. tv.Text
  4540. );
  4541. Assert.Equal (new (22, 2), tv.CursorPosition);
  4542. Assert.Equal (0, tv.SelectedLength);
  4543. Assert.Equal ("", tv.SelectedText);
  4544. Assert.False (tv.IsSelecting);
  4545. Assert.True (tv.NewKeyDownEvent (Key.CursorRight.WithCtrl));
  4546. Assert.Equal (
  4547. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4548. tv.Text
  4549. );
  4550. Assert.Equal (new (23, 2), tv.CursorPosition);
  4551. Assert.Equal (0, tv.SelectedLength);
  4552. Assert.Equal ("", tv.SelectedText);
  4553. Assert.False (tv.IsSelecting);
  4554. Assert.True (tv.NewKeyDownEvent (Key.CursorRight.WithCtrl));
  4555. Assert.Equal (
  4556. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4557. tv.Text
  4558. );
  4559. Assert.Equal (new (28, 2), tv.CursorPosition);
  4560. Assert.Equal (0, tv.SelectedLength);
  4561. Assert.Equal ("", tv.SelectedText);
  4562. Assert.False (tv.IsSelecting);
  4563. Assert.True (tv.NewKeyDownEvent (Key.Home.WithCtrl));
  4564. Assert.Equal (
  4565. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.first",
  4566. tv.Text
  4567. );
  4568. Assert.Equal (Point.Empty, tv.CursorPosition);
  4569. Assert.Equal (0, tv.SelectedLength);
  4570. Assert.Equal ("", tv.SelectedText);
  4571. Assert.False (tv.IsSelecting);
  4572. Assert.True (tv.NewKeyDownEvent (Key.Delete.WithCtrl));
  4573. Assert.Equal ($"This is the second line.{Environment.NewLine}This is the third line.first", tv.Text);
  4574. Assert.Equal (Point.Empty, tv.CursorPosition);
  4575. Assert.Equal (0, tv.SelectedLength);
  4576. Assert.Equal ("", tv.SelectedText);
  4577. Assert.False (tv.IsSelecting);
  4578. Assert.True (tv.NewKeyDownEvent (Key.End.WithCtrl));
  4579. Assert.Equal ($"This is the second line.{Environment.NewLine}This is the third line.first", tv.Text);
  4580. Assert.Equal (new (28, 1), tv.CursorPosition);
  4581. Assert.Equal (0, tv.SelectedLength);
  4582. Assert.Equal ("", tv.SelectedText);
  4583. Assert.False (tv.IsSelecting);
  4584. Assert.True (tv.NewKeyDownEvent (Key.Backspace.WithCtrl));
  4585. Assert.Equal ($"This is the second line.{Environment.NewLine}This is the third ", tv.Text);
  4586. Assert.Equal (new (18, 1), tv.CursorPosition);
  4587. Assert.Equal (0, tv.SelectedLength);
  4588. Assert.Equal ("", tv.SelectedText);
  4589. Assert.False (tv.IsSelecting);
  4590. Assert.True (tv.AllowsReturn);
  4591. tv.AllowsReturn = false;
  4592. Assert.Equal (Point.Empty, tv.CursorPosition);
  4593. Assert.False (tv.IsSelecting);
  4594. Assert.False (tv.NewKeyDownEvent (Key.Enter)); // Accepted event not handled
  4595. Assert.Equal ($"This is the second line.{Environment.NewLine}This is the third ", tv.Text);
  4596. Assert.Equal (Point.Empty, tv.CursorPosition);
  4597. Assert.Equal (0, tv.SelectedLength);
  4598. Assert.Equal ("", tv.SelectedText);
  4599. Assert.False (tv.IsSelecting);
  4600. Assert.False (tv.AllowsReturn);
  4601. tv.AllowsReturn = true;
  4602. Assert.Equal (Point.Empty, tv.CursorPosition);
  4603. Assert.True (tv.NewKeyDownEvent (Key.Enter));
  4604. Assert.Equal (
  4605. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third ",
  4606. tv.Text
  4607. );
  4608. Assert.Equal (new (0, 1), tv.CursorPosition);
  4609. Assert.Equal (0, tv.SelectedLength);
  4610. Assert.Equal ("", tv.SelectedText);
  4611. Assert.False (tv.IsSelecting);
  4612. Assert.True (tv.AllowsReturn);
  4613. Assert.True (tv.NewKeyDownEvent (Key.End.WithShift.WithCtrl));
  4614. Assert.Equal (
  4615. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third ",
  4616. tv.Text
  4617. );
  4618. Assert.Equal (new (18, 2), tv.CursorPosition);
  4619. Assert.Equal (42 + Environment.NewLine.Length, tv.SelectedLength);
  4620. Assert.Equal ($"This is the second line.{Environment.NewLine}This is the third ", tv.SelectedText);
  4621. Assert.True (tv.IsSelecting);
  4622. Assert.True (tv.NewKeyDownEvent (Key.Home.WithShift.WithCtrl));
  4623. Assert.Equal (
  4624. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third ",
  4625. tv.Text
  4626. );
  4627. Assert.Equal (Point.Empty, tv.CursorPosition);
  4628. Assert.Equal (Environment.NewLine.Length, tv.SelectedLength);
  4629. Assert.Equal ($"{Environment.NewLine}", tv.SelectedText);
  4630. Assert.True (tv.IsSelecting);
  4631. Assert.True (tv.NewKeyDownEvent (Key.A.WithCtrl));
  4632. Assert.Equal (
  4633. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third ",
  4634. tv.Text
  4635. );
  4636. Assert.Equal (new (18, 2), tv.CursorPosition);
  4637. Assert.Equal (42 + Environment.NewLine.Length * 2, tv.SelectedLength);
  4638. Assert.Equal (
  4639. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third ",
  4640. tv.SelectedText
  4641. );
  4642. Assert.True (tv.IsSelecting);
  4643. Assert.True (tv.Used);
  4644. Assert.True (tv.NewKeyDownEvent (Key.InsertChar));
  4645. Assert.False (tv.Used);
  4646. Assert.True (tv.AllowsTab);
  4647. Assert.Equal (new (18, 2), tv.CursorPosition);
  4648. Assert.True (tv.IsSelecting);
  4649. tv.AllowsTab = false;
  4650. Assert.False (tv.NewKeyDownEvent (Key.Tab));
  4651. Assert.Equal (
  4652. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third ",
  4653. tv.Text
  4654. );
  4655. Assert.False (tv.AllowsTab);
  4656. tv.AllowsTab = true;
  4657. Assert.Equal (new (18, 2), tv.CursorPosition);
  4658. Assert.True (tv.IsSelecting);
  4659. tv.IsSelecting = false;
  4660. Assert.True (tv.NewKeyDownEvent (Key.Tab));
  4661. Assert.Equal (
  4662. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third \t",
  4663. tv.Text
  4664. );
  4665. Assert.False (tv.IsSelecting);
  4666. Assert.True (tv.AllowsTab);
  4667. tv.AllowsTab = false;
  4668. Assert.False (tv.NewKeyDownEvent (Key.Tab.WithShift));
  4669. Assert.Equal (
  4670. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third \t",
  4671. tv.Text
  4672. );
  4673. Assert.False (tv.IsSelecting);
  4674. Assert.False (tv.AllowsTab);
  4675. tv.AllowsTab = true;
  4676. Assert.True (tv.NewKeyDownEvent (Key.Tab.WithShift));
  4677. Assert.Equal (
  4678. $"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third ",
  4679. tv.Text
  4680. );
  4681. Assert.False (tv.IsSelecting);
  4682. Assert.True (tv.AllowsTab);
  4683. Assert.False (tv.NewKeyDownEvent (Key.F6));
  4684. Assert.False (tv.NewKeyDownEvent (Application.NextTabGroupKey));
  4685. Assert.False (tv.NewKeyDownEvent (Key.F6.WithShift));
  4686. Assert.False (tv.NewKeyDownEvent (Application.PrevTabGroupKey));
  4687. Assert.True (tv.NewKeyDownEvent (PopoverMenu.DefaultKey));
  4688. Assert.True (tv.ContextMenu != null && tv.ContextMenu.Visible);
  4689. Assert.False (tv.IsSelecting);
  4690. top.Dispose ();
  4691. }
  4692. [Fact]
  4693. [TextViewTestsAutoInitShutdown]
  4694. public void Kill_Delete_WordBackward ()
  4695. {
  4696. _textView.Text = "This is the first line.";
  4697. _textView.MoveEnd ();
  4698. var iteration = 0;
  4699. var iterationsFinished = false;
  4700. while (!iterationsFinished)
  4701. {
  4702. _textView.NewKeyDownEvent (Key.Backspace.WithCtrl);
  4703. switch (iteration)
  4704. {
  4705. case 0:
  4706. Assert.Equal (22, _textView.CursorPosition.X);
  4707. Assert.Equal (0, _textView.CursorPosition.Y);
  4708. Assert.Equal ("This is the first line", _textView.Text);
  4709. break;
  4710. case 1:
  4711. Assert.Equal (18, _textView.CursorPosition.X);
  4712. Assert.Equal (0, _textView.CursorPosition.Y);
  4713. Assert.Equal ("This is the first ", _textView.Text);
  4714. break;
  4715. case 2:
  4716. Assert.Equal (12, _textView.CursorPosition.X);
  4717. Assert.Equal (0, _textView.CursorPosition.Y);
  4718. Assert.Equal ("This is the ", _textView.Text);
  4719. break;
  4720. case 3:
  4721. Assert.Equal (8, _textView.CursorPosition.X);
  4722. Assert.Equal (0, _textView.CursorPosition.Y);
  4723. Assert.Equal ("This is ", _textView.Text);
  4724. break;
  4725. case 4:
  4726. Assert.Equal (5, _textView.CursorPosition.X);
  4727. Assert.Equal (0, _textView.CursorPosition.Y);
  4728. Assert.Equal ("This ", _textView.Text);
  4729. break;
  4730. case 5:
  4731. Assert.Equal (0, _textView.CursorPosition.X);
  4732. Assert.Equal (0, _textView.CursorPosition.Y);
  4733. Assert.Equal ("", _textView.Text);
  4734. break;
  4735. default:
  4736. iterationsFinished = true;
  4737. break;
  4738. }
  4739. iteration++;
  4740. }
  4741. }
  4742. [Fact]
  4743. [TextViewTestsAutoInitShutdown]
  4744. public void Kill_Delete_WordBackward_Multiline ()
  4745. {
  4746. _textView.Text = "This is the first line.\nThis is the second line.";
  4747. _textView.Width = 4;
  4748. _textView.MoveEnd ();
  4749. var iteration = 0;
  4750. var iterationsFinished = false;
  4751. while (!iterationsFinished)
  4752. {
  4753. _textView.NewKeyDownEvent (Key.Backspace.WithCtrl);
  4754. switch (iteration)
  4755. {
  4756. case 0:
  4757. Assert.Equal (23, _textView.CursorPosition.X);
  4758. Assert.Equal (1, _textView.CursorPosition.Y);
  4759. Assert.Equal (
  4760. "This is the first line."
  4761. + Environment.NewLine
  4762. + "This is the second line",
  4763. _textView.Text
  4764. );
  4765. break;
  4766. case 1:
  4767. Assert.Equal (19, _textView.CursorPosition.X);
  4768. Assert.Equal (1, _textView.CursorPosition.Y);
  4769. Assert.Equal (
  4770. "This is the first line."
  4771. + Environment.NewLine
  4772. + "This is the second ",
  4773. _textView.Text
  4774. );
  4775. break;
  4776. case 2:
  4777. Assert.Equal (12, _textView.CursorPosition.X);
  4778. Assert.Equal (1, _textView.CursorPosition.Y);
  4779. Assert.Equal (
  4780. "This is the first line."
  4781. + Environment.NewLine
  4782. + "This is the ",
  4783. _textView.Text
  4784. );
  4785. break;
  4786. case 3:
  4787. Assert.Equal (8, _textView.CursorPosition.X);
  4788. Assert.Equal (1, _textView.CursorPosition.Y);
  4789. Assert.Equal (
  4790. "This is the first line."
  4791. + Environment.NewLine
  4792. + "This is ",
  4793. _textView.Text
  4794. );
  4795. break;
  4796. case 4:
  4797. Assert.Equal (5, _textView.CursorPosition.X);
  4798. Assert.Equal (1, _textView.CursorPosition.Y);
  4799. Assert.Equal (
  4800. "This is the first line."
  4801. + Environment.NewLine
  4802. + "This ",
  4803. _textView.Text
  4804. );
  4805. break;
  4806. case 5:
  4807. Assert.Equal (0, _textView.CursorPosition.X);
  4808. Assert.Equal (1, _textView.CursorPosition.Y);
  4809. Assert.Equal ("This is the first line." + Environment.NewLine, _textView.Text);
  4810. break;
  4811. case 6:
  4812. Assert.Equal (23, _textView.CursorPosition.X);
  4813. Assert.Equal (0, _textView.CursorPosition.Y);
  4814. Assert.Equal ("This is the first line.", _textView.Text);
  4815. break;
  4816. case 7:
  4817. Assert.Equal (22, _textView.CursorPosition.X);
  4818. Assert.Equal (0, _textView.CursorPosition.Y);
  4819. Assert.Equal ("This is the first line", _textView.Text);
  4820. break;
  4821. case 8:
  4822. Assert.Equal (18, _textView.CursorPosition.X);
  4823. Assert.Equal (0, _textView.CursorPosition.Y);
  4824. Assert.Equal ("This is the first ", _textView.Text);
  4825. break;
  4826. case 9:
  4827. Assert.Equal (12, _textView.CursorPosition.X);
  4828. Assert.Equal (0, _textView.CursorPosition.Y);
  4829. Assert.Equal ("This is the ", _textView.Text);
  4830. break;
  4831. case 10:
  4832. Assert.Equal (8, _textView.CursorPosition.X);
  4833. Assert.Equal (0, _textView.CursorPosition.Y);
  4834. Assert.Equal ("This is ", _textView.Text);
  4835. break;
  4836. case 11:
  4837. Assert.Equal (5, _textView.CursorPosition.X);
  4838. Assert.Equal (0, _textView.CursorPosition.Y);
  4839. Assert.Equal ("This ", _textView.Text);
  4840. break;
  4841. case 12:
  4842. Assert.Equal (0, _textView.CursorPosition.X);
  4843. Assert.Equal (0, _textView.CursorPosition.Y);
  4844. Assert.Equal ("", _textView.Text);
  4845. break;
  4846. default:
  4847. iterationsFinished = true;
  4848. break;
  4849. }
  4850. iteration++;
  4851. }
  4852. }
  4853. [Fact]
  4854. [TextViewTestsAutoInitShutdown]
  4855. public void Kill_Delete_WordForward ()
  4856. {
  4857. _textView.Text = "This is the first line.";
  4858. var iteration = 0;
  4859. var iterationsFinished = false;
  4860. while (!iterationsFinished)
  4861. {
  4862. _textView.NewKeyDownEvent (Key.Delete.WithCtrl);
  4863. switch (iteration)
  4864. {
  4865. case 0:
  4866. Assert.Equal (0, _textView.CursorPosition.X);
  4867. Assert.Equal (0, _textView.CursorPosition.Y);
  4868. Assert.Equal ("is the first line.", _textView.Text);
  4869. break;
  4870. case 1:
  4871. Assert.Equal (0, _textView.CursorPosition.X);
  4872. Assert.Equal (0, _textView.CursorPosition.Y);
  4873. Assert.Equal ("the first line.", _textView.Text);
  4874. break;
  4875. case 2:
  4876. Assert.Equal (0, _textView.CursorPosition.X);
  4877. Assert.Equal (0, _textView.CursorPosition.Y);
  4878. Assert.Equal ("first line.", _textView.Text);
  4879. break;
  4880. case 3:
  4881. Assert.Equal (0, _textView.CursorPosition.X);
  4882. Assert.Equal (0, _textView.CursorPosition.Y);
  4883. Assert.Equal ("line.", _textView.Text);
  4884. break;
  4885. case 4:
  4886. Assert.Equal (0, _textView.CursorPosition.X);
  4887. Assert.Equal (0, _textView.CursorPosition.Y);
  4888. Assert.Equal ("", _textView.Text);
  4889. break;
  4890. default:
  4891. iterationsFinished = true;
  4892. break;
  4893. }
  4894. iteration++;
  4895. }
  4896. }
  4897. [Fact]
  4898. [TextViewTestsAutoInitShutdown]
  4899. public void Kill_Delete_WordForward_Multiline ()
  4900. {
  4901. _textView.Text = "This is the first line.\nThis is the second line.";
  4902. _textView.Width = 4;
  4903. var iteration = 0;
  4904. var iterationsFinished = false;
  4905. while (!iterationsFinished)
  4906. {
  4907. _textView.NewKeyDownEvent (Key.Delete.WithCtrl);
  4908. switch (iteration)
  4909. {
  4910. case 0:
  4911. Assert.Equal (0, _textView.CursorPosition.X);
  4912. Assert.Equal (0, _textView.CursorPosition.Y);
  4913. Assert.Equal (
  4914. "is the first line."
  4915. + Environment.NewLine
  4916. + "This is the second line.",
  4917. _textView.Text
  4918. );
  4919. break;
  4920. case 1:
  4921. Assert.Equal (0, _textView.CursorPosition.X);
  4922. Assert.Equal (0, _textView.CursorPosition.Y);
  4923. Assert.Equal (
  4924. "the first line."
  4925. + Environment.NewLine
  4926. + "This is the second line.",
  4927. _textView.Text
  4928. );
  4929. break;
  4930. case 2:
  4931. Assert.Equal (0, _textView.CursorPosition.X);
  4932. Assert.Equal (0, _textView.CursorPosition.Y);
  4933. Assert.Equal (
  4934. "first line."
  4935. + Environment.NewLine
  4936. + "This is the second line.",
  4937. _textView.Text
  4938. );
  4939. break;
  4940. case 3:
  4941. Assert.Equal (0, _textView.CursorPosition.X);
  4942. Assert.Equal (0, _textView.CursorPosition.Y);
  4943. Assert.Equal (
  4944. "line."
  4945. + Environment.NewLine
  4946. + "This is the second line.",
  4947. _textView.Text
  4948. );
  4949. break;
  4950. case 4:
  4951. Assert.Equal (0, _textView.CursorPosition.X);
  4952. Assert.Equal (0, _textView.CursorPosition.Y);
  4953. Assert.Equal (
  4954. ""
  4955. + Environment.NewLine
  4956. + "This is the second line.",
  4957. _textView.Text
  4958. );
  4959. break;
  4960. case 5:
  4961. Assert.Equal (0, _textView.CursorPosition.X);
  4962. Assert.Equal (0, _textView.CursorPosition.Y);
  4963. Assert.Equal ("This is the second line.", _textView.Text);
  4964. break;
  4965. case 6:
  4966. Assert.Equal (0, _textView.CursorPosition.X);
  4967. Assert.Equal (0, _textView.CursorPosition.Y);
  4968. Assert.Equal ("is the second line.", _textView.Text);
  4969. break;
  4970. case 7:
  4971. Assert.Equal (0, _textView.CursorPosition.X);
  4972. Assert.Equal (0, _textView.CursorPosition.Y);
  4973. Assert.Equal ("the second line.", _textView.Text);
  4974. break;
  4975. case 8:
  4976. Assert.Equal (0, _textView.CursorPosition.X);
  4977. Assert.Equal (0, _textView.CursorPosition.Y);
  4978. Assert.Equal ("second line.", _textView.Text);
  4979. break;
  4980. case 9:
  4981. Assert.Equal (0, _textView.CursorPosition.X);
  4982. Assert.Equal (0, _textView.CursorPosition.Y);
  4983. Assert.Equal ("line.", _textView.Text);
  4984. break;
  4985. case 10:
  4986. Assert.Equal (0, _textView.CursorPosition.X);
  4987. Assert.Equal (0, _textView.CursorPosition.Y);
  4988. Assert.Equal ("", _textView.Text);
  4989. break;
  4990. default:
  4991. iterationsFinished = true;
  4992. break;
  4993. }
  4994. iteration++;
  4995. }
  4996. }
  4997. [Fact]
  4998. [TextViewTestsAutoInitShutdown]
  4999. public void Kill_To_End_Delete_Forwards_Copy_To_The_Clipboard_And_Paste ()
  5000. {
  5001. _textView.Text = "This is the first line.\nThis is the second line.";
  5002. var iteration = 0;
  5003. var iterationsFinished = false;
  5004. while (!iterationsFinished)
  5005. {
  5006. switch (iteration)
  5007. {
  5008. case 0:
  5009. _textView.NewKeyDownEvent (Key.K.WithCtrl);
  5010. Assert.Equal (0, _textView.CursorPosition.X);
  5011. Assert.Equal (0, _textView.CursorPosition.Y);
  5012. Assert.Equal ($"{Environment.NewLine}This is the second line.", _textView.Text);
  5013. Assert.Equal ("This is the first line.", Clipboard.Contents);
  5014. break;
  5015. case 1:
  5016. _textView.NewKeyDownEvent (
  5017. new (
  5018. KeyCode.Delete | KeyCode.CtrlMask | KeyCode.ShiftMask
  5019. )
  5020. );
  5021. Assert.Equal (0, _textView.CursorPosition.X);
  5022. Assert.Equal (0, _textView.CursorPosition.Y);
  5023. Assert.Equal ("This is the second line.", _textView.Text);
  5024. Assert.Equal ($"This is the first line.{Environment.NewLine}", Clipboard.Contents);
  5025. break;
  5026. case 2:
  5027. _textView.NewKeyDownEvent (Key.K.WithCtrl);
  5028. Assert.Equal (0, _textView.CursorPosition.X);
  5029. Assert.Equal (0, _textView.CursorPosition.Y);
  5030. Assert.Equal ("", _textView.Text);
  5031. Assert.Equal (
  5032. $"This is the first line.{Environment.NewLine}This is the second line.",
  5033. Clipboard.Contents
  5034. );
  5035. // Paste
  5036. _textView.NewKeyDownEvent (Key.Y.WithCtrl);
  5037. Assert.Equal (
  5038. $"This is the first line.{Environment.NewLine}This is the second line.",
  5039. _textView.Text
  5040. );
  5041. break;
  5042. default:
  5043. iterationsFinished = true;
  5044. break;
  5045. }
  5046. iteration++;
  5047. }
  5048. }
  5049. [Fact]
  5050. [TextViewTestsAutoInitShutdown]
  5051. public void Kill_To_Start_Delete_Backwards_Copy_To_The_Clipboard_And_Paste ()
  5052. {
  5053. _textView.Text = "This is the first line.\nThis is the second line.";
  5054. _textView.MoveEnd ();
  5055. var iteration = 0;
  5056. var iterationsFinished = false;
  5057. while (!iterationsFinished)
  5058. {
  5059. switch (iteration)
  5060. {
  5061. case 0:
  5062. _textView.NewKeyDownEvent (Key.Backspace.WithCtrl.WithShift);
  5063. Assert.Equal (0, _textView.CursorPosition.X);
  5064. Assert.Equal (1, _textView.CursorPosition.Y);
  5065. Assert.Equal ($"This is the first line.{Environment.NewLine}", _textView.Text);
  5066. Assert.Equal ("This is the second line.", Clipboard.Contents);
  5067. break;
  5068. case 1:
  5069. _textView.NewKeyDownEvent (
  5070. new (
  5071. KeyCode.Backspace | KeyCode.CtrlMask | KeyCode.ShiftMask
  5072. )
  5073. );
  5074. Assert.Equal (23, _textView.CursorPosition.X);
  5075. Assert.Equal (0, _textView.CursorPosition.Y);
  5076. Assert.Equal ("This is the first line.", _textView.Text);
  5077. Assert.Equal ($"This is the second line.{Environment.NewLine}", Clipboard.Contents);
  5078. break;
  5079. case 2:
  5080. _textView.NewKeyDownEvent (Key.Backspace.WithCtrl.WithShift);
  5081. Assert.Equal (0, _textView.CursorPosition.X);
  5082. Assert.Equal (0, _textView.CursorPosition.Y);
  5083. Assert.Equal ("", _textView.Text);
  5084. Assert.Equal (
  5085. $"This is the second line.{Environment.NewLine}This is the first line.",
  5086. Clipboard.Contents
  5087. );
  5088. // Paste inverted
  5089. _textView.NewKeyDownEvent (Key.Y.WithCtrl);
  5090. Assert.Equal (
  5091. $"This is the second line.{Environment.NewLine}This is the first line.",
  5092. _textView.Text
  5093. );
  5094. break;
  5095. default:
  5096. iterationsFinished = true;
  5097. break;
  5098. }
  5099. iteration++;
  5100. }
  5101. }
  5102. [Fact]
  5103. public void LeftColumn_Add_One_If_Text_Length_Is_Equal_To_Width ()
  5104. {
  5105. var tv = new TextView { Width = 10, Text = "1234567890" };
  5106. Assert.Equal (Point.Empty, tv.CursorPosition);
  5107. Assert.Equal (0, tv.LeftColumn);
  5108. tv.CursorPosition = new (9, 0);
  5109. Assert.Equal (new (9, 0), tv.CursorPosition);
  5110. Assert.Equal (0, tv.LeftColumn);
  5111. Assert.True (tv.NewKeyDownEvent (Key.CursorRight));
  5112. tv.CursorPosition = new (10, 0);
  5113. Assert.Equal (new (10, 0), tv.CursorPosition);
  5114. Assert.Equal (1, tv.LeftColumn);
  5115. }
  5116. [Fact]
  5117. public void LoadFile_Throws_If_File_Is_Empty ()
  5118. {
  5119. var result = false;
  5120. var tv = new TextView ();
  5121. Assert.Throws<ArgumentException> (() => result = tv.Load (""));
  5122. Assert.False (result);
  5123. }
  5124. [Fact]
  5125. public void LoadFile_Throws_If_File_Is_Null ()
  5126. {
  5127. var result = false;
  5128. var tv = new TextView ();
  5129. Assert.Throws<ArgumentNullException> (() => result = tv.Load ((string)null));
  5130. Assert.False (result);
  5131. }
  5132. [Fact]
  5133. public void LoadFile_Throws_If_File_Not_Exist ()
  5134. {
  5135. var result = false;
  5136. var tv = new TextView ();
  5137. Assert.Throws<FileNotFoundException> (() => result = tv.Load ("blabla"));
  5138. Assert.False (result);
  5139. }
  5140. [Fact]
  5141. public void LoadStream_CRLF ()
  5142. {
  5143. var text = "This is the first line.\r\nThis is the second line.\r\n";
  5144. var tv = new TextView ();
  5145. tv.Load (new MemoryStream (Encoding.ASCII.GetBytes (text)));
  5146. Assert.Equal (
  5147. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}",
  5148. tv.Text
  5149. );
  5150. }
  5151. [Fact]
  5152. public void LoadStream_IsDirty ()
  5153. {
  5154. var text = "Testing";
  5155. using (var stream = new MemoryStream ())
  5156. {
  5157. var writer = new StreamWriter (stream);
  5158. writer.Write (text);
  5159. writer.Flush ();
  5160. stream.Position = 0;
  5161. var tv = new TextView ();
  5162. tv.Load (stream);
  5163. Assert.Equal (7, text.Length);
  5164. Assert.Equal (text.Length, tv.Text.Length);
  5165. Assert.Equal (text, tv.Text);
  5166. Assert.False (tv.IsDirty);
  5167. }
  5168. }
  5169. [Fact]
  5170. public void LoadStream_IsDirty_With_Null_On_The_Text ()
  5171. {
  5172. var text = "Test\0ing";
  5173. using (var stream = new MemoryStream ())
  5174. {
  5175. var writer = new StreamWriter (stream);
  5176. writer.Write (text);
  5177. writer.Flush ();
  5178. stream.Position = 0;
  5179. var tv = new TextView ();
  5180. tv.Load (stream);
  5181. Assert.Equal (8, text.Length);
  5182. Assert.Equal (text.Length, tv.Text.Length);
  5183. Assert.Equal (8, text.Length);
  5184. Assert.Equal (8, tv.Text.Length);
  5185. Assert.Equal (text, tv.Text);
  5186. Assert.False (tv.IsDirty);
  5187. Assert.Equal ((Rune)'\u2400', ((Rune)tv.Text [4]).MakePrintable ());
  5188. }
  5189. }
  5190. [Fact]
  5191. public void LoadStream_LF ()
  5192. {
  5193. var text = "This is the first line.\nThis is the second line.\n";
  5194. var tv = new TextView ();
  5195. tv.Load (new MemoryStream (Encoding.ASCII.GetBytes (text)));
  5196. Assert.Equal (
  5197. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}",
  5198. tv.Text
  5199. );
  5200. }
  5201. [Fact]
  5202. public void LoadStream_Stream_Is_Empty ()
  5203. {
  5204. var tv = new TextView ();
  5205. tv.Load (new MemoryStream ());
  5206. Assert.Equal ("", tv.Text);
  5207. }
  5208. [Fact]
  5209. public void LoadStream_Throws_If_Stream_Is_Null ()
  5210. {
  5211. var tv = new TextView ();
  5212. Assert.Throws<ArgumentNullException> (() => tv.Load ((Stream)null));
  5213. }
  5214. [Fact]
  5215. [TextViewTestsAutoInitShutdown]
  5216. public void Mouse_Button_Shift_Preserves_Selection ()
  5217. {
  5218. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  5219. Assert.True (
  5220. _textView.NewMouseEvent (
  5221. new () { Position = new (12, 0), Flags = MouseFlags.Button1Pressed | MouseFlags.ButtonShift }
  5222. )
  5223. );
  5224. Assert.Equal (0, _textView.SelectionStartColumn);
  5225. Assert.Equal (0, _textView.SelectionStartRow);
  5226. Assert.Equal (new (12, 0), _textView.CursorPosition);
  5227. Assert.True (_textView.IsSelecting);
  5228. Assert.Equal ("TAB to jump ", _textView.SelectedText);
  5229. Assert.True (_textView.NewMouseEvent (new () { Position = new (12, 0), Flags = MouseFlags.Button1Clicked }));
  5230. Assert.Equal (0, _textView.SelectionStartRow);
  5231. Assert.Equal (0, _textView.SelectionStartRow);
  5232. Assert.Equal (new (12, 0), _textView.CursorPosition);
  5233. Assert.True (_textView.IsSelecting);
  5234. Assert.Equal ("TAB to jump ", _textView.SelectedText);
  5235. Assert.True (
  5236. _textView.NewMouseEvent (
  5237. new () { Position = new (19, 0), Flags = MouseFlags.Button1Pressed | MouseFlags.ButtonShift }
  5238. )
  5239. );
  5240. Assert.Equal (0, _textView.SelectionStartRow);
  5241. Assert.Equal (0, _textView.SelectionStartRow);
  5242. Assert.Equal (new (19, 0), _textView.CursorPosition);
  5243. Assert.True (_textView.IsSelecting);
  5244. Assert.Equal ("TAB to jump between", _textView.SelectedText);
  5245. Assert.True (_textView.NewMouseEvent (new () { Position = new (19, 0), Flags = MouseFlags.Button1Clicked }));
  5246. Assert.Equal (0, _textView.SelectionStartRow);
  5247. Assert.Equal (0, _textView.SelectionStartRow);
  5248. Assert.Equal (new (19, 0), _textView.CursorPosition);
  5249. Assert.True (_textView.IsSelecting);
  5250. Assert.Equal ("TAB to jump between", _textView.SelectedText);
  5251. Assert.True (
  5252. _textView.NewMouseEvent (
  5253. new () { Position = new (24, 0), Flags = MouseFlags.Button1Pressed | MouseFlags.ButtonShift }
  5254. )
  5255. );
  5256. Assert.Equal (0, _textView.SelectionStartRow);
  5257. Assert.Equal (0, _textView.SelectionStartRow);
  5258. Assert.Equal (new (24, 0), _textView.CursorPosition);
  5259. Assert.True (_textView.IsSelecting);
  5260. Assert.Equal ("TAB to jump between text", _textView.SelectedText);
  5261. Assert.True (_textView.NewMouseEvent (new () { Position = new (24, 0), Flags = MouseFlags.Button1Clicked }));
  5262. Assert.Equal (0, _textView.SelectionStartRow);
  5263. Assert.Equal (0, _textView.SelectionStartRow);
  5264. Assert.Equal (new (24, 0), _textView.CursorPosition);
  5265. Assert.True (_textView.IsSelecting);
  5266. Assert.Equal ("TAB to jump between text", _textView.SelectedText);
  5267. Assert.True (_textView.NewMouseEvent (new () { Position = new (24, 0), Flags = MouseFlags.Button1Pressed }));
  5268. Assert.Equal (0, _textView.SelectionStartRow);
  5269. Assert.Equal (0, _textView.SelectionStartRow);
  5270. Assert.Equal (new (24, 0), _textView.CursorPosition);
  5271. Assert.True (_textView.IsSelecting);
  5272. Assert.Equal ("", _textView.SelectedText);
  5273. }
  5274. [Fact]
  5275. [AutoInitShutdown]
  5276. public void MoveDown_By_Setting_CursorPosition ()
  5277. {
  5278. var tv = new TextView { Width = 10, Height = 5 };
  5279. // add 100 lines of wide text to view
  5280. for (var i = 0; i < 100; i++)
  5281. {
  5282. tv.Text += new string ('x', 100) + (i == 99 ? "" : Environment.NewLine);
  5283. }
  5284. Assert.Equal (Point.Empty, tv.CursorPosition);
  5285. tv.CursorPosition = new (5, 50);
  5286. Assert.Equal (new (5, 50), tv.CursorPosition);
  5287. tv.CursorPosition = new (200, 200);
  5288. Assert.Equal (new (100, 99), tv.CursorPosition);
  5289. }
  5290. [Fact]
  5291. [TextViewTestsAutoInitShutdown]
  5292. public void Multiline_Setting_Changes_AllowsReturn_AllowsTab_Height_WordWrap ()
  5293. {
  5294. Assert.True (_textView.Multiline);
  5295. Assert.True (_textView.AllowsReturn);
  5296. Assert.Equal (4, _textView.TabWidth);
  5297. Assert.True (_textView.AllowsTab);
  5298. Assert.False (_textView.WordWrap);
  5299. _textView.WordWrap = true;
  5300. Assert.True (_textView.WordWrap);
  5301. _textView.Multiline = false;
  5302. Assert.False (_textView.Multiline);
  5303. Assert.False (_textView.AllowsReturn);
  5304. Assert.Equal (0, _textView.TabWidth);
  5305. Assert.False (_textView.AllowsTab);
  5306. Assert.False (_textView.WordWrap);
  5307. _textView.WordWrap = true;
  5308. Assert.False (_textView.WordWrap);
  5309. _textView.Multiline = true;
  5310. Assert.True (_textView.Multiline);
  5311. Assert.True (_textView.AllowsReturn);
  5312. Assert.Equal (4, _textView.TabWidth);
  5313. Assert.True (_textView.AllowsTab);
  5314. Assert.False (_textView.WordWrap);
  5315. }
  5316. [Fact]
  5317. [TextViewTestsAutoInitShutdown]
  5318. public void Paste_Always_Clear_The_SelectedText ()
  5319. {
  5320. _textView.SelectionStartColumn = 20;
  5321. _textView.SelectionStartRow = 0;
  5322. _textView.CursorPosition = new (24, 0);
  5323. _textView.NewKeyDownEvent (Key.C.WithCtrl); // Copy
  5324. Assert.Equal ("text", _textView.SelectedText);
  5325. _textView.NewKeyDownEvent (Key.Y.WithCtrl); // Paste
  5326. Assert.Equal ("", _textView.SelectedText);
  5327. }
  5328. [Fact]
  5329. public void ReplaceAllText_Does_Not_Throw_Exception ()
  5330. {
  5331. var textToFind = "hello! hello!";
  5332. var textToReplace = "hello!";
  5333. var tv = new TextView { Width = 20, Height = 3, Text = textToFind };
  5334. Exception exception = Record.Exception (() => tv.ReplaceAllText (textToFind, false, false, textToReplace));
  5335. Assert.Null (exception);
  5336. Assert.Equal (textToReplace, tv.Text);
  5337. }
  5338. [Fact]
  5339. [AutoInitShutdown]
  5340. public void ScrollDownTillCaretOffscreen_ThenType ()
  5341. {
  5342. var tv = new TextView { Width = 10, Height = 5 };
  5343. // add 100 lines of wide text to view
  5344. for (var i = 0; i < 100; i++)
  5345. {
  5346. tv.Text += new string ('x', 100) + Environment.NewLine;
  5347. }
  5348. Assert.Equal (0, tv.CursorPosition.Y);
  5349. tv.ScrollTo (50);
  5350. Assert.Equal (0, tv.CursorPosition.Y);
  5351. tv.NewKeyDownEvent (Key.P);
  5352. }
  5353. [Fact]
  5354. [AutoInitShutdown]
  5355. public void ScrollTo_CursorPosition ()
  5356. {
  5357. var tv = new TextView { Width = 10, Height = 5 };
  5358. // add 100 lines of wide text to view
  5359. for (var i = 0; i < 100; i++)
  5360. {
  5361. tv.Text += new string ('x', 100) + (i == 99 ? "" : Environment.NewLine);
  5362. }
  5363. Assert.Equal (Point.Empty, tv.CursorPosition);
  5364. tv.ScrollTo (50);
  5365. Assert.Equal (Point.Empty, tv.CursorPosition);
  5366. tv.CursorPosition = new (tv.LeftColumn, tv.TopRow);
  5367. Assert.Equal (new (0, 50), tv.CursorPosition);
  5368. }
  5369. [Fact]
  5370. [TextViewTestsAutoInitShutdown]
  5371. public void Selected_Text_Shows ()
  5372. {
  5373. // Proves #3022 is fixed (TextField selected text does not show in v2)
  5374. var top = new Toplevel ();
  5375. top.Add (_textView);
  5376. RunState rs = Application.Begin (top);
  5377. _textView.CursorPosition = Point.Empty;
  5378. _textView.SelectionStartColumn = 0;
  5379. _textView.SelectionStartRow = 0;
  5380. Attribute [] attributes =
  5381. {
  5382. _textView.GetScheme ().Focus,
  5383. new (_textView.GetScheme ().Focus.Background, _textView.GetScheme ().Focus.Foreground)
  5384. };
  5385. // TAB to jump between text fields.
  5386. DriverAssert.AssertDriverAttributesAre ("0000000", _output, Application.Driver, attributes);
  5387. Assert.Empty (_textView.SelectedCellsList);
  5388. _textView.NewKeyDownEvent (Key.CursorRight.WithCtrl.WithShift);
  5389. Application.RunIteration (ref rs, true);
  5390. Assert.Equal (new (4, 0), _textView.CursorPosition);
  5391. // TAB to jump between text fields.
  5392. DriverAssert.AssertDriverAttributesAre ("1111000", _output, Application.Driver, attributes);
  5393. Assert.Equal ("TAB ", Cell.ToString (_textView.SelectedCellsList [^1]));
  5394. top.Dispose ();
  5395. }
  5396. [Fact]
  5397. [TextViewTestsAutoInitShutdown]
  5398. public void Selection_And_CursorPosition_With_Value_Greater_Than_Text_Length_Changes_Both_To_Text_Length ()
  5399. {
  5400. _textView.CursorPosition = new (33, 2);
  5401. _textView.SelectionStartColumn = 33;
  5402. _textView.SelectionStartRow = 33;
  5403. Assert.Equal (32, _textView.CursorPosition.X);
  5404. Assert.Equal (0, _textView.CursorPosition.Y);
  5405. Assert.Equal (32, _textView.SelectionStartColumn);
  5406. Assert.Equal (0, _textView.SelectionStartRow);
  5407. Assert.Equal (0, _textView.SelectedLength);
  5408. Assert.Equal ("", _textView.SelectedText);
  5409. }
  5410. [Fact]
  5411. [TextViewTestsAutoInitShutdown]
  5412. public void Selection_With_Empty_Text ()
  5413. {
  5414. _textView = new ();
  5415. _textView.CursorPosition = new (2, 0);
  5416. _textView.SelectionStartColumn = 33;
  5417. _textView.SelectionStartRow = 1;
  5418. Assert.Equal (0, _textView.SelectionStartColumn);
  5419. Assert.Equal (0, _textView.SelectionStartRow);
  5420. Assert.Equal (0, _textView.SelectedLength);
  5421. Assert.Equal ("", _textView.SelectedText);
  5422. }
  5423. [Fact]
  5424. [TextViewTestsAutoInitShutdown]
  5425. public void Selection_With_Value_Greater_Than_Text_Length_Changes_To_Text_Length ()
  5426. {
  5427. _textView.CursorPosition = new (2, 0);
  5428. _textView.SelectionStartColumn = 33;
  5429. _textView.SelectionStartRow = 1;
  5430. Assert.Equal (32, _textView.SelectionStartColumn);
  5431. Assert.Equal (0, _textView.SelectionStartRow);
  5432. Assert.Equal (30, _textView.SelectedLength);
  5433. Assert.Equal ("B to jump between text fields.", _textView.SelectedText);
  5434. }
  5435. [Fact]
  5436. [TextViewTestsAutoInitShutdown]
  5437. public void Selection_With_Value_Less_Than_Zero_Changes_To_Zero ()
  5438. {
  5439. _textView.SelectionStartColumn = -2;
  5440. _textView.SelectionStartRow = -2;
  5441. Assert.Equal (0, _textView.SelectionStartColumn);
  5442. Assert.Equal (0, _textView.SelectionStartRow);
  5443. Assert.Equal (0, _textView.SelectedLength);
  5444. Assert.Equal ("", _textView.SelectedText);
  5445. }
  5446. [Fact]
  5447. public void StringToRunes_Slipts_CRLF ()
  5448. {
  5449. var text = "This is the first line.\r\nThis is the second line.\r\n";
  5450. var tv = new TextView ();
  5451. tv.Text = text;
  5452. Assert.Equal (
  5453. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}",
  5454. tv.Text
  5455. );
  5456. }
  5457. [Fact]
  5458. public void StringToRunes_Slipts_LF ()
  5459. {
  5460. var text = "This is the first line.\nThis is the second line.\n";
  5461. var tv = new TextView ();
  5462. tv.Text = text;
  5463. Assert.Equal (
  5464. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}",
  5465. tv.Text
  5466. );
  5467. }
  5468. [Fact]
  5469. [TextViewTestsAutoInitShutdown]
  5470. public void Tab_Test_Follow_By_BackTab ()
  5471. {
  5472. var top = new Toplevel ();
  5473. top.Add (_textView);
  5474. Application.Iteration += (s, a) =>
  5475. {
  5476. int width = _textView.Viewport.Width - 1;
  5477. Assert.Equal (30, width + 1);
  5478. Assert.Equal (10, _textView.Height);
  5479. _textView.Text = "";
  5480. var col = 0;
  5481. var leftCol = 0;
  5482. int tabWidth = _textView.TabWidth;
  5483. while (col < 100)
  5484. {
  5485. col++;
  5486. _textView.NewKeyDownEvent (Key.Tab);
  5487. Assert.Equal (new (col, 0), _textView.CursorPosition);
  5488. leftCol = GetLeftCol (leftCol);
  5489. Assert.Equal (leftCol, _textView.LeftColumn);
  5490. }
  5491. while (col > 0)
  5492. {
  5493. col--;
  5494. _textView.NewKeyDownEvent (Key.Tab.WithShift);
  5495. Assert.Equal (new (col, 0), _textView.CursorPosition);
  5496. leftCol = GetLeftCol (leftCol);
  5497. Assert.Equal (leftCol, _textView.LeftColumn);
  5498. }
  5499. top.Remove (_textView);
  5500. Application.RequestStop ();
  5501. };
  5502. Application.Run (top);
  5503. top.Dispose ();
  5504. }
  5505. [Fact]
  5506. [TextViewTestsAutoInitShutdown]
  5507. public void Tab_Test_Follow_By_BackTab_With_Text ()
  5508. {
  5509. var top = new Toplevel ();
  5510. top.Add (_textView);
  5511. Application.Iteration += (s, a) =>
  5512. {
  5513. int width = _textView.Viewport.Width - 1;
  5514. Assert.Equal (30, width + 1);
  5515. Assert.Equal (10, _textView.Height);
  5516. var col = 0;
  5517. var leftCol = 0;
  5518. Assert.Equal (new (col, 0), _textView.CursorPosition);
  5519. Assert.Equal (leftCol, _textView.LeftColumn);
  5520. while (col < 100)
  5521. {
  5522. col++;
  5523. _textView.NewKeyDownEvent (Key.Tab);
  5524. Assert.Equal (new (col, 0), _textView.CursorPosition);
  5525. leftCol = GetLeftCol (leftCol);
  5526. Assert.Equal (leftCol, _textView.LeftColumn);
  5527. }
  5528. while (col > 0)
  5529. {
  5530. col--;
  5531. _textView.NewKeyDownEvent (Key.Tab.WithShift);
  5532. Assert.Equal (new (col, 0), _textView.CursorPosition);
  5533. leftCol = GetLeftCol (leftCol);
  5534. Assert.Equal (leftCol, _textView.LeftColumn);
  5535. }
  5536. top.Remove (_textView);
  5537. Application.RequestStop ();
  5538. };
  5539. Application.Run (top);
  5540. top.Dispose ();
  5541. }
  5542. [Fact]
  5543. [TextViewTestsAutoInitShutdown]
  5544. public void Tab_Test_Follow_By_CursorLeft_And_Then_Follow_By_CursorRight ()
  5545. {
  5546. var top = new Toplevel ();
  5547. top.Add (_textView);
  5548. Application.Iteration += (s, a) =>
  5549. {
  5550. int width = _textView.Viewport.Width - 1;
  5551. Assert.Equal (30, width + 1);
  5552. Assert.Equal (10, _textView.Height);
  5553. _textView.Text = "";
  5554. var col = 0;
  5555. var leftCol = 0;
  5556. int tabWidth = _textView.TabWidth;
  5557. while (col < 100)
  5558. {
  5559. col++;
  5560. _textView.NewKeyDownEvent (Key.Tab);
  5561. Assert.Equal (new (col, 0), _textView.CursorPosition);
  5562. leftCol = GetLeftCol (leftCol);
  5563. Assert.Equal (leftCol, _textView.LeftColumn);
  5564. }
  5565. while (col > 0)
  5566. {
  5567. col--;
  5568. _textView.NewKeyDownEvent (Key.CursorLeft);
  5569. Assert.Equal (new (col, 0), _textView.CursorPosition);
  5570. leftCol = GetLeftCol (leftCol);
  5571. Assert.Equal (leftCol, _textView.LeftColumn);
  5572. }
  5573. while (col < 100)
  5574. {
  5575. col++;
  5576. _textView.NewKeyDownEvent (Key.CursorRight);
  5577. Assert.Equal (new (col, 0), _textView.CursorPosition);
  5578. leftCol = GetLeftCol (leftCol);
  5579. Assert.Equal (leftCol, _textView.LeftColumn);
  5580. }
  5581. top.Remove (_textView);
  5582. Application.RequestStop ();
  5583. };
  5584. Application.Run (top);
  5585. top.Dispose ();
  5586. }
  5587. [Fact]
  5588. [TextViewTestsAutoInitShutdown]
  5589. public void Tab_Test_Follow_By_CursorLeft_And_Then_Follow_By_CursorRight_With_Text ()
  5590. {
  5591. var top = new Toplevel ();
  5592. top.Add (_textView);
  5593. Application.Iteration += (s, a) =>
  5594. {
  5595. int width = _textView.Viewport.Width - 1;
  5596. Assert.Equal (30, width + 1);
  5597. Assert.Equal (10, _textView.Height);
  5598. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  5599. var col = 0;
  5600. var leftCol = 0;
  5601. int tabWidth = _textView.TabWidth;
  5602. while (col < 100)
  5603. {
  5604. col++;
  5605. _textView.NewKeyDownEvent (Key.Tab);
  5606. Assert.Equal (new (col, 0), _textView.CursorPosition);
  5607. leftCol = GetLeftCol (leftCol);
  5608. Assert.Equal (leftCol, _textView.LeftColumn);
  5609. }
  5610. Assert.Equal (132, _textView.Text.Length);
  5611. while (col > 0)
  5612. {
  5613. col--;
  5614. _textView.NewKeyDownEvent (Key.CursorLeft);
  5615. Assert.Equal (new (col, 0), _textView.CursorPosition);
  5616. leftCol = GetLeftCol (leftCol);
  5617. Assert.Equal (leftCol, _textView.LeftColumn);
  5618. }
  5619. while (col < 100)
  5620. {
  5621. col++;
  5622. _textView.NewKeyDownEvent (Key.CursorRight);
  5623. Assert.Equal (new (col, 0), _textView.CursorPosition);
  5624. leftCol = GetLeftCol (leftCol);
  5625. Assert.Equal (leftCol, _textView.LeftColumn);
  5626. }
  5627. top.Remove (_textView);
  5628. Application.RequestStop ();
  5629. };
  5630. Application.Run (top);
  5631. top.Dispose ();
  5632. }
  5633. [Fact]
  5634. [TextViewTestsAutoInitShutdown]
  5635. public void Tab_Test_Follow_By_Home_And_Then_Follow_By_End_And_Then_Follow_By_BackTab_With_Text ()
  5636. {
  5637. var top = new Toplevel ();
  5638. top.Add (_textView);
  5639. Application.Iteration += (s, a) =>
  5640. {
  5641. int width = _textView.Viewport.Width - 1;
  5642. Assert.Equal (30, width + 1);
  5643. Assert.Equal (10, _textView.Height);
  5644. var col = 0;
  5645. var leftCol = 0;
  5646. Assert.Equal (new (col, 0), _textView.CursorPosition);
  5647. Assert.Equal (leftCol, _textView.LeftColumn);
  5648. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  5649. Assert.Equal (32, _textView.Text.Length);
  5650. while (col < 100)
  5651. {
  5652. col++;
  5653. _textView.NewKeyDownEvent (Key.Tab);
  5654. Assert.Equal (new (col, 0), _textView.CursorPosition);
  5655. leftCol = GetLeftCol (leftCol);
  5656. Assert.Equal (leftCol, _textView.LeftColumn);
  5657. }
  5658. _textView.NewKeyDownEvent (Key.Home);
  5659. col = 0;
  5660. Assert.Equal (new (col, 0), _textView.CursorPosition);
  5661. leftCol = 0;
  5662. Assert.Equal (leftCol, _textView.LeftColumn);
  5663. _textView.NewKeyDownEvent (Key.End);
  5664. col = _textView.Text.Length;
  5665. Assert.Equal (132, _textView.Text.Length);
  5666. Assert.Equal (new (col, 0), _textView.CursorPosition);
  5667. leftCol = GetLeftCol (leftCol);
  5668. Assert.Equal (leftCol, _textView.LeftColumn);
  5669. string txt = _textView.Text;
  5670. while (col - 1 > 0 && txt [col - 1] != '\t')
  5671. {
  5672. col--;
  5673. }
  5674. _textView.CursorPosition = new (col, 0);
  5675. leftCol = GetLeftCol (leftCol);
  5676. while (col > 0)
  5677. {
  5678. col--;
  5679. _textView.NewKeyDownEvent (Key.Tab.WithShift);
  5680. Assert.Equal (new (col, 0), _textView.CursorPosition);
  5681. leftCol = GetLeftCol (leftCol);
  5682. Assert.Equal (leftCol, _textView.LeftColumn);
  5683. }
  5684. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  5685. Assert.Equal (32, _textView.Text.Length);
  5686. top.Remove (_textView);
  5687. Application.RequestStop ();
  5688. };
  5689. Application.Run (top);
  5690. top.Dispose ();
  5691. }
  5692. [Fact]
  5693. [TextViewTestsAutoInitShutdown]
  5694. public void TabWidth_Setting_To_Zero_Keeps_AllowsTab ()
  5695. {
  5696. var top = new Toplevel ();
  5697. top.Add (_textView);
  5698. Application.Begin (top);
  5699. Assert.Equal (4, _textView.TabWidth);
  5700. Assert.True (_textView.AllowsTab);
  5701. Assert.True (_textView.AllowsReturn);
  5702. Assert.True (_textView.Multiline);
  5703. _textView.TabWidth = -1;
  5704. Assert.Equal (0, _textView.TabWidth);
  5705. Assert.True (_textView.AllowsTab);
  5706. Assert.True (_textView.AllowsReturn);
  5707. Assert.True (_textView.Multiline);
  5708. _textView.NewKeyDownEvent (Key.Tab);
  5709. Assert.Equal ("\tTAB to jump between text fields.", _textView.Text);
  5710. Application.LayoutAndDraw ();
  5711. DriverAssert.AssertDriverContentsWithFrameAre (
  5712. @"
  5713. TAB to jump between text field",
  5714. _output
  5715. );
  5716. _textView.TabWidth = 4;
  5717. Application.LayoutAndDraw ();
  5718. DriverAssert.AssertDriverContentsWithFrameAre (
  5719. @"
  5720. TAB to jump between text f",
  5721. _output
  5722. );
  5723. _textView.NewKeyDownEvent (Key.Tab.WithShift);
  5724. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  5725. Assert.True (_textView.NeedsDraw);
  5726. Application.LayoutAndDraw ();
  5727. DriverAssert.AssertDriverContentsWithFrameAre (
  5728. @"
  5729. TAB to jump between text field",
  5730. _output
  5731. );
  5732. top.Dispose ();
  5733. }
  5734. [Fact]
  5735. [TextViewTestsAutoInitShutdown]
  5736. public void TextChanged_Event ()
  5737. {
  5738. _textView.TextChanged += (s, e) =>
  5739. {
  5740. if (_textView.Text == "changing")
  5741. {
  5742. Assert.Equal ("changing", _textView.Text);
  5743. _textView.Text = "changed";
  5744. }
  5745. };
  5746. _textView.Text = "changing";
  5747. Assert.Equal ("changed", _textView.Text);
  5748. }
  5749. [Fact]
  5750. [TextViewTestsAutoInitShutdown]
  5751. public void TextChanged_Event_NoFires_OnTyping ()
  5752. {
  5753. var eventcount = 0;
  5754. _textView.TextChanged += (s, e) => { eventcount++; };
  5755. _textView.Text = "ay";
  5756. Assert.Equal (1, eventcount);
  5757. _textView.NewKeyDownEvent (Key.Y.WithShift);
  5758. Assert.Equal (1, eventcount);
  5759. Assert.Equal ("Yay", _textView.Text);
  5760. }
  5761. [Fact]
  5762. [TextViewTestsAutoInitShutdown]
  5763. public void TextView_InsertText_Newline_CRLF ()
  5764. {
  5765. var tv = new TextView { Width = 10, Height = 10 };
  5766. tv.InsertText ("\r\naaa\r\nbbb");
  5767. PlatformID p = Environment.OSVersion.Platform;
  5768. if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows)
  5769. {
  5770. Assert.Equal ("\r\naaa\r\nbbb", tv.Text);
  5771. }
  5772. else
  5773. {
  5774. Assert.Equal ("\naaa\nbbb", tv.Text);
  5775. }
  5776. Assert.Equal ($"{Environment.NewLine}aaa{Environment.NewLine}bbb", tv.Text);
  5777. var win = new Window ();
  5778. win.Add (tv);
  5779. var top = new Toplevel ();
  5780. top.Add (win);
  5781. Application.Begin (top);
  5782. ((FakeDriver)Application.Driver!).SetBufferSize (15, 15);
  5783. Application.LayoutAndDraw ();
  5784. //this passes
  5785. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (
  5786. @"
  5787. ┌─────────────┐
  5788. │ │
  5789. │aaa │
  5790. │bbb │
  5791. │ │
  5792. │ │
  5793. │ │
  5794. │ │
  5795. │ │
  5796. │ │
  5797. │ │
  5798. │ │
  5799. │ │
  5800. │ │
  5801. └─────────────┘",
  5802. _output
  5803. );
  5804. Assert.Equal (new (0, 0, 15, 15), pos);
  5805. Assert.True (tv.Used);
  5806. tv.Used = false;
  5807. tv.CursorPosition = Point.Empty;
  5808. tv.InsertText ("\r\naaa\r\nbbb");
  5809. Application.LayoutAndDraw ();
  5810. DriverAssert.AssertDriverContentsWithFrameAre (
  5811. @"
  5812. ┌─────────────┐
  5813. │ │
  5814. │aaa │
  5815. │bbb │
  5816. │aaa │
  5817. │bbb │
  5818. │ │
  5819. │ │
  5820. │ │
  5821. │ │
  5822. │ │
  5823. │ │
  5824. │ │
  5825. │ │
  5826. └─────────────┘",
  5827. _output
  5828. );
  5829. top.Dispose ();
  5830. }
  5831. [Fact]
  5832. [TextViewTestsAutoInitShutdown]
  5833. public void TextView_InsertText_Newline_LF ()
  5834. {
  5835. var tv = new TextView { Width = 10, Height = 10 };
  5836. tv.InsertText ("\naaa\nbbb");
  5837. PlatformID p = Environment.OSVersion.Platform;
  5838. if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows)
  5839. {
  5840. Assert.Equal ("\r\naaa\r\nbbb", tv.Text);
  5841. }
  5842. else
  5843. {
  5844. Assert.Equal ("\naaa\nbbb", tv.Text);
  5845. }
  5846. Assert.Equal ($"{Environment.NewLine}aaa{Environment.NewLine}bbb", tv.Text);
  5847. var win = new Window ();
  5848. win.Add (tv);
  5849. var top = new Toplevel ();
  5850. top.Add (win);
  5851. Application.Begin (top);
  5852. ((FakeDriver)Application.Driver!).SetBufferSize (15, 15);
  5853. Application.LayoutAndDraw ();
  5854. //this passes
  5855. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (
  5856. @"
  5857. ┌─────────────┐
  5858. │ │
  5859. │aaa │
  5860. │bbb │
  5861. │ │
  5862. │ │
  5863. │ │
  5864. │ │
  5865. │ │
  5866. │ │
  5867. │ │
  5868. │ │
  5869. │ │
  5870. │ │
  5871. └─────────────┘",
  5872. _output
  5873. );
  5874. Assert.Equal (new (0, 0, 15, 15), pos);
  5875. Assert.True (tv.Used);
  5876. tv.Used = false;
  5877. tv.CursorPosition = Point.Empty;
  5878. tv.InsertText ("\naaa\nbbb");
  5879. Application.LayoutAndDraw ();
  5880. DriverAssert.AssertDriverContentsWithFrameAre (
  5881. @"
  5882. ┌─────────────┐
  5883. │ │
  5884. │aaa │
  5885. │bbb │
  5886. │aaa │
  5887. │bbb │
  5888. │ │
  5889. │ │
  5890. │ │
  5891. │ │
  5892. │ │
  5893. │ │
  5894. │ │
  5895. │ │
  5896. └─────────────┘",
  5897. _output
  5898. );
  5899. top.Dispose ();
  5900. }
  5901. [Fact]
  5902. public void TextView_MultiLine_But_Without_Tabs ()
  5903. {
  5904. var view = new TextView ();
  5905. // the default for TextView
  5906. Assert.True (view.Multiline);
  5907. view.AllowsTab = false;
  5908. Assert.False (view.AllowsTab);
  5909. Assert.True (view.Multiline);
  5910. }
  5911. [Fact]
  5912. [TextViewTestsAutoInitShutdown]
  5913. public void TextView_SpaceHandling ()
  5914. {
  5915. var tv = new TextView { Width = 10, Text = " " };
  5916. var ev = new MouseEventArgs { Position = new (0, 0), Flags = MouseFlags.Button1DoubleClicked };
  5917. tv.NewMouseEvent (ev);
  5918. Assert.Equal (1, tv.SelectedLength);
  5919. ev = new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked };
  5920. tv.NewMouseEvent (ev);
  5921. Assert.Equal (1, tv.SelectedLength);
  5922. }
  5923. [Fact]
  5924. [AutoInitShutdown]
  5925. public void UnwrappedCursorPosition_Event ()
  5926. {
  5927. var cp = Point.Empty;
  5928. var tv = new TextView
  5929. {
  5930. Width = Dim.Fill (), Height = Dim.Fill (), Text = "This is the first line.\nThis is the second line.\n"
  5931. };
  5932. tv.UnwrappedCursorPosition += (s, e) => { cp = e; };
  5933. var top = new Toplevel ();
  5934. top.Add (tv);
  5935. Application.Begin (top);
  5936. Application.LayoutAndDraw ();
  5937. Assert.False (tv.WordWrap);
  5938. Assert.Equal (Point.Empty, tv.CursorPosition);
  5939. Assert.Equal (Point.Empty, cp);
  5940. DriverAssert.AssertDriverContentsWithFrameAre (
  5941. @"
  5942. This is the first line.
  5943. This is the second line.
  5944. ",
  5945. _output
  5946. );
  5947. tv.WordWrap = true;
  5948. tv.CursorPosition = new (12, 0);
  5949. tv.Draw ();
  5950. Assert.Equal (new (12, 0), tv.CursorPosition);
  5951. Assert.Equal (new (12, 0), cp);
  5952. DriverAssert.AssertDriverContentsWithFrameAre (
  5953. @"
  5954. This is the first line.
  5955. This is the second line.
  5956. ",
  5957. _output
  5958. );
  5959. ((FakeDriver)Application.Driver).SetBufferSize (6, 25);
  5960. tv.SetRelativeLayout (Application.Screen.Size);
  5961. tv.Draw ();
  5962. Assert.Equal (new (4, 2), tv.CursorPosition);
  5963. Assert.Equal (new (12, 0), cp);
  5964. DriverAssert.AssertDriverContentsWithFrameAre (
  5965. @"
  5966. This
  5967. is
  5968. the
  5969. first
  5970. line.
  5971. This
  5972. is
  5973. the
  5974. secon
  5975. d
  5976. line.
  5977. ",
  5978. _output
  5979. );
  5980. Assert.True (tv.NewKeyDownEvent (Key.CursorRight));
  5981. tv.Draw ();
  5982. Assert.Equal (new (0, 3), tv.CursorPosition);
  5983. Assert.Equal (new (12, 0), cp);
  5984. DriverAssert.AssertDriverContentsWithFrameAre (
  5985. @"
  5986. This
  5987. is
  5988. the
  5989. first
  5990. line.
  5991. This
  5992. is
  5993. the
  5994. secon
  5995. d
  5996. line.
  5997. ",
  5998. _output
  5999. );
  6000. Assert.True (tv.NewKeyDownEvent (Key.CursorRight));
  6001. tv.Draw ();
  6002. Assert.Equal (new (1, 3), tv.CursorPosition);
  6003. Assert.Equal (new (13, 0), cp);
  6004. DriverAssert.AssertDriverContentsWithFrameAre (
  6005. @"
  6006. This
  6007. is
  6008. the
  6009. first
  6010. line.
  6011. This
  6012. is
  6013. the
  6014. secon
  6015. d
  6016. line.
  6017. ",
  6018. _output
  6019. );
  6020. Assert.True (tv.NewMouseEvent (new () { Position = new (0, 3), Flags = MouseFlags.Button1Pressed }));
  6021. tv.Draw ();
  6022. Assert.Equal (new (0, 3), tv.CursorPosition);
  6023. Assert.Equal (new (12, 0), cp);
  6024. DriverAssert.AssertDriverContentsWithFrameAre (
  6025. @"
  6026. This
  6027. is
  6028. the
  6029. first
  6030. line.
  6031. This
  6032. is
  6033. the
  6034. secon
  6035. d
  6036. line.
  6037. ",
  6038. _output
  6039. );
  6040. top.Dispose ();
  6041. }
  6042. [Fact]
  6043. [TextViewTestsAutoInitShutdown]
  6044. public void Used_Is_False ()
  6045. {
  6046. _textView.Used = false;
  6047. _textView.CursorPosition = new (10, 0);
  6048. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  6049. _textView.NewKeyDownEvent (Key.U); // u
  6050. Assert.Equal ("TAB to jumu between text fields.", _textView.Text);
  6051. _textView.NewKeyDownEvent (Key.S); // s
  6052. Assert.Equal ("TAB to jumusbetween text fields.", _textView.Text);
  6053. _textView.NewKeyDownEvent (Key.E); // e
  6054. Assert.Equal ("TAB to jumuseetween text fields.", _textView.Text);
  6055. _textView.NewKeyDownEvent (Key.D); // d
  6056. Assert.Equal ("TAB to jumusedtween text fields.", _textView.Text);
  6057. }
  6058. [Fact]
  6059. [TextViewTestsAutoInitShutdown]
  6060. public void Used_Is_True_By_Default ()
  6061. {
  6062. _textView.CursorPosition = new (10, 0);
  6063. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  6064. _textView.NewKeyDownEvent (Key.U); // u
  6065. Assert.Equal ("TAB to jumup between text fields.", _textView.Text);
  6066. _textView.NewKeyDownEvent (Key.S); // s
  6067. Assert.Equal ("TAB to jumusp between text fields.", _textView.Text);
  6068. _textView.NewKeyDownEvent (Key.E); // e
  6069. Assert.Equal ("TAB to jumusep between text fields.", _textView.Text);
  6070. _textView.NewKeyDownEvent (Key.D); // d
  6071. Assert.Equal ("TAB to jumusedp between text fields.", _textView.Text);
  6072. }
  6073. [Fact]
  6074. [TextViewTestsAutoInitShutdown]
  6075. public void WordBackward_Multiline_With_Selection ()
  6076. {
  6077. // 4 3 2 1
  6078. // 87654321098765432109876 54321098765432109876543210-Length
  6079. // 1 2 1 2
  6080. // 01234567890123456789012 0123456789012345678901234
  6081. _textView.Text = "This is the first line.\nThis is the second line.";
  6082. _textView.MoveEnd ();
  6083. _textView.SelectionStartColumn = _textView.CurrentColumn;
  6084. _textView.SelectionStartRow = _textView.CurrentRow;
  6085. var iteration = 0;
  6086. var iterationsFinished = false;
  6087. while (!iterationsFinished)
  6088. {
  6089. _textView.NewKeyDownEvent (Key.CursorLeft.WithCtrl.WithShift);
  6090. switch (iteration)
  6091. {
  6092. case 0:
  6093. Assert.Equal (23, _textView.CursorPosition.X);
  6094. Assert.Equal (1, _textView.CursorPosition.Y);
  6095. Assert.Equal (24, _textView.SelectionStartColumn);
  6096. Assert.Equal (1, _textView.SelectionStartRow);
  6097. Assert.Equal (1, _textView.SelectedLength);
  6098. Assert.Equal (".", _textView.SelectedText);
  6099. break;
  6100. case 1:
  6101. Assert.Equal (19, _textView.CursorPosition.X);
  6102. Assert.Equal (1, _textView.CursorPosition.Y);
  6103. Assert.Equal (24, _textView.SelectionStartColumn);
  6104. Assert.Equal (1, _textView.SelectionStartRow);
  6105. Assert.Equal (5, _textView.SelectedLength);
  6106. Assert.Equal ("line.", _textView.SelectedText);
  6107. break;
  6108. case 2:
  6109. Assert.Equal (12, _textView.CursorPosition.X);
  6110. Assert.Equal (1, _textView.CursorPosition.Y);
  6111. Assert.Equal (24, _textView.SelectionStartColumn);
  6112. Assert.Equal (1, _textView.SelectionStartRow);
  6113. Assert.Equal (12, _textView.SelectedLength);
  6114. Assert.Equal ("second line.", _textView.SelectedText);
  6115. break;
  6116. case 3:
  6117. Assert.Equal (8, _textView.CursorPosition.X);
  6118. Assert.Equal (1, _textView.CursorPosition.Y);
  6119. Assert.Equal (24, _textView.SelectionStartColumn);
  6120. Assert.Equal (1, _textView.SelectionStartRow);
  6121. Assert.Equal (16, _textView.SelectedLength);
  6122. Assert.Equal ("the second line.", _textView.SelectedText);
  6123. break;
  6124. case 4:
  6125. Assert.Equal (5, _textView.CursorPosition.X);
  6126. Assert.Equal (1, _textView.CursorPosition.Y);
  6127. Assert.Equal (24, _textView.SelectionStartColumn);
  6128. Assert.Equal (1, _textView.SelectionStartRow);
  6129. Assert.Equal (19, _textView.SelectedLength);
  6130. Assert.Equal ("is the second line.", _textView.SelectedText);
  6131. break;
  6132. case 5:
  6133. Assert.Equal (0, _textView.CursorPosition.X);
  6134. Assert.Equal (1, _textView.CursorPosition.Y);
  6135. Assert.Equal (24, _textView.SelectionStartColumn);
  6136. Assert.Equal (1, _textView.SelectionStartRow);
  6137. Assert.Equal (24, _textView.SelectedLength);
  6138. Assert.Equal ("This is the second line.", _textView.SelectedText);
  6139. break;
  6140. case 6:
  6141. Assert.Equal (23, _textView.CursorPosition.X);
  6142. Assert.Equal (0, _textView.CursorPosition.Y);
  6143. Assert.Equal (24, _textView.SelectionStartColumn);
  6144. Assert.Equal (1, _textView.SelectionStartRow);
  6145. Assert.Equal (24 + Environment.NewLine.Length, _textView.SelectedLength);
  6146. Assert.Equal ($"{Environment.NewLine}This is the second line.", _textView.SelectedText);
  6147. break;
  6148. case 7:
  6149. Assert.Equal (22, _textView.CursorPosition.X);
  6150. Assert.Equal (0, _textView.CursorPosition.Y);
  6151. Assert.Equal (24, _textView.SelectionStartColumn);
  6152. Assert.Equal (1, _textView.SelectionStartRow);
  6153. Assert.Equal (25 + Environment.NewLine.Length, _textView.SelectedLength);
  6154. Assert.Equal (
  6155. $".{Environment.NewLine}This is the second line.",
  6156. _textView.SelectedText
  6157. );
  6158. break;
  6159. case 8:
  6160. Assert.Equal (18, _textView.CursorPosition.X);
  6161. Assert.Equal (0, _textView.CursorPosition.Y);
  6162. Assert.Equal (24, _textView.SelectionStartColumn);
  6163. Assert.Equal (1, _textView.SelectionStartRow);
  6164. Assert.Equal (29 + Environment.NewLine.Length, _textView.SelectedLength);
  6165. Assert.Equal (
  6166. $"line.{Environment.NewLine}This is the second line.",
  6167. _textView.SelectedText
  6168. );
  6169. break;
  6170. case 9:
  6171. Assert.Equal (12, _textView.CursorPosition.X);
  6172. Assert.Equal (0, _textView.CursorPosition.Y);
  6173. Assert.Equal (24, _textView.SelectionStartColumn);
  6174. Assert.Equal (1, _textView.SelectionStartRow);
  6175. Assert.Equal (35 + Environment.NewLine.Length, _textView.SelectedLength);
  6176. Assert.Equal (
  6177. $"first line.{Environment.NewLine}This is the second line.",
  6178. _textView.SelectedText
  6179. );
  6180. break;
  6181. case 10:
  6182. Assert.Equal (8, _textView.CursorPosition.X);
  6183. Assert.Equal (0, _textView.CursorPosition.Y);
  6184. Assert.Equal (24, _textView.SelectionStartColumn);
  6185. Assert.Equal (1, _textView.SelectionStartRow);
  6186. Assert.Equal (39 + Environment.NewLine.Length, _textView.SelectedLength);
  6187. Assert.Equal (
  6188. $"the first line.{Environment.NewLine}This is the second line.",
  6189. _textView.SelectedText
  6190. );
  6191. break;
  6192. case 11:
  6193. Assert.Equal (5, _textView.CursorPosition.X);
  6194. Assert.Equal (0, _textView.CursorPosition.Y);
  6195. Assert.Equal (24, _textView.SelectionStartColumn);
  6196. Assert.Equal (1, _textView.SelectionStartRow);
  6197. Assert.Equal (42 + Environment.NewLine.Length, _textView.SelectedLength);
  6198. Assert.Equal (
  6199. $"is the first line.{Environment.NewLine}This is the second line.",
  6200. _textView.SelectedText
  6201. );
  6202. break;
  6203. case 12:
  6204. Assert.Equal (0, _textView.CursorPosition.X);
  6205. Assert.Equal (0, _textView.CursorPosition.Y);
  6206. Assert.Equal (24, _textView.SelectionStartColumn);
  6207. Assert.Equal (1, _textView.SelectionStartRow);
  6208. Assert.Equal (47 + Environment.NewLine.Length, _textView.SelectedLength);
  6209. Assert.Equal (
  6210. $"This is the first line.{Environment.NewLine}This is the second line.",
  6211. _textView.SelectedText
  6212. );
  6213. break;
  6214. default:
  6215. iterationsFinished = true;
  6216. break;
  6217. }
  6218. iteration++;
  6219. }
  6220. }
  6221. [Fact]
  6222. [TextViewTestsAutoInitShutdown]
  6223. public void WordBackward_With_No_Selection ()
  6224. {
  6225. _textView.CursorPosition = new (_textView.Text.Length, 0);
  6226. var iteration = 0;
  6227. while (_textView.CursorPosition.X > 0)
  6228. {
  6229. _textView.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  6230. switch (iteration)
  6231. {
  6232. case 0:
  6233. Assert.Equal (31, _textView.CursorPosition.X);
  6234. Assert.Equal (0, _textView.CursorPosition.Y);
  6235. Assert.Equal (0, _textView.SelectionStartColumn);
  6236. Assert.Equal (0, _textView.SelectionStartRow);
  6237. Assert.Equal (0, _textView.SelectedLength);
  6238. Assert.Equal ("", _textView.SelectedText);
  6239. break;
  6240. case 1:
  6241. Assert.Equal (25, _textView.CursorPosition.X);
  6242. Assert.Equal (0, _textView.CursorPosition.Y);
  6243. Assert.Equal (0, _textView.SelectionStartColumn);
  6244. Assert.Equal (0, _textView.SelectionStartRow);
  6245. Assert.Equal (0, _textView.SelectedLength);
  6246. Assert.Equal ("", _textView.SelectedText);
  6247. break;
  6248. case 2:
  6249. Assert.Equal (20, _textView.CursorPosition.X);
  6250. Assert.Equal (0, _textView.CursorPosition.Y);
  6251. Assert.Equal (0, _textView.SelectionStartColumn);
  6252. Assert.Equal (0, _textView.SelectionStartRow);
  6253. Assert.Equal (0, _textView.SelectedLength);
  6254. Assert.Equal ("", _textView.SelectedText);
  6255. break;
  6256. case 3:
  6257. Assert.Equal (12, _textView.CursorPosition.X);
  6258. Assert.Equal (0, _textView.CursorPosition.Y);
  6259. Assert.Equal (0, _textView.SelectionStartColumn);
  6260. Assert.Equal (0, _textView.SelectionStartRow);
  6261. Assert.Equal (0, _textView.SelectedLength);
  6262. Assert.Equal ("", _textView.SelectedText);
  6263. break;
  6264. case 4:
  6265. Assert.Equal (7, _textView.CursorPosition.X);
  6266. Assert.Equal (0, _textView.CursorPosition.Y);
  6267. Assert.Equal (0, _textView.SelectionStartColumn);
  6268. Assert.Equal (0, _textView.SelectionStartRow);
  6269. Assert.Equal (0, _textView.SelectedLength);
  6270. Assert.Equal ("", _textView.SelectedText);
  6271. break;
  6272. case 5:
  6273. Assert.Equal (4, _textView.CursorPosition.X);
  6274. Assert.Equal (0, _textView.CursorPosition.Y);
  6275. Assert.Equal (0, _textView.SelectionStartColumn);
  6276. Assert.Equal (0, _textView.SelectionStartRow);
  6277. Assert.Equal (0, _textView.SelectedLength);
  6278. Assert.Equal ("", _textView.SelectedText);
  6279. break;
  6280. case 6:
  6281. Assert.Equal (0, _textView.CursorPosition.X);
  6282. Assert.Equal (0, _textView.CursorPosition.Y);
  6283. Assert.Equal (0, _textView.SelectionStartColumn);
  6284. Assert.Equal (0, _textView.SelectionStartRow);
  6285. Assert.Equal (0, _textView.SelectedLength);
  6286. Assert.Equal ("", _textView.SelectedText);
  6287. break;
  6288. }
  6289. iteration++;
  6290. }
  6291. }
  6292. [Fact]
  6293. [TextViewTestsAutoInitShutdown]
  6294. public void WordBackward_With_No_Selection_And_With_More_Than_Only_One_Whitespace_And_With_Only_One_Letter ()
  6295. {
  6296. // 1 2 3 4 5
  6297. // 0123456789012345678901234567890123456789012345678901234=55 (Length)
  6298. _textView.Text = "TAB t o jump b etween t ext f ields .";
  6299. _textView.CursorPosition = new (_textView.Text.Length, 0);
  6300. var iteration = 0;
  6301. while (_textView.CursorPosition.X > 0)
  6302. {
  6303. _textView.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  6304. switch (iteration)
  6305. {
  6306. case 0:
  6307. Assert.Equal (54, _textView.CursorPosition.X);
  6308. Assert.Equal (0, _textView.CursorPosition.Y);
  6309. Assert.Equal (0, _textView.SelectionStartColumn);
  6310. Assert.Equal (0, _textView.SelectionStartRow);
  6311. Assert.Equal (0, _textView.SelectedLength);
  6312. Assert.Equal ("", _textView.SelectedText);
  6313. break;
  6314. case 1:
  6315. Assert.Equal (48, _textView.CursorPosition.X);
  6316. Assert.Equal (0, _textView.CursorPosition.Y);
  6317. Assert.Equal (0, _textView.SelectionStartColumn);
  6318. Assert.Equal (0, _textView.SelectionStartRow);
  6319. Assert.Equal (0, _textView.SelectedLength);
  6320. Assert.Equal ("", _textView.SelectedText);
  6321. break;
  6322. case 2:
  6323. Assert.Equal (46, _textView.CursorPosition.X);
  6324. Assert.Equal (0, _textView.CursorPosition.Y);
  6325. Assert.Equal (0, _textView.SelectionStartColumn);
  6326. Assert.Equal (0, _textView.SelectionStartRow);
  6327. Assert.Equal (0, _textView.SelectedLength);
  6328. Assert.Equal ("", _textView.SelectedText);
  6329. break;
  6330. case 3:
  6331. Assert.Equal (40, _textView.CursorPosition.X);
  6332. Assert.Equal (0, _textView.CursorPosition.Y);
  6333. Assert.Equal (0, _textView.SelectionStartColumn);
  6334. Assert.Equal (0, _textView.SelectionStartRow);
  6335. Assert.Equal (0, _textView.SelectedLength);
  6336. Assert.Equal ("", _textView.SelectedText);
  6337. break;
  6338. case 4:
  6339. Assert.Equal (38, _textView.CursorPosition.X);
  6340. Assert.Equal (0, _textView.CursorPosition.Y);
  6341. Assert.Equal (0, _textView.SelectionStartColumn);
  6342. Assert.Equal (0, _textView.SelectionStartRow);
  6343. Assert.Equal (0, _textView.SelectedLength);
  6344. Assert.Equal ("", _textView.SelectedText);
  6345. break;
  6346. case 5:
  6347. Assert.Equal (28, _textView.CursorPosition.X);
  6348. Assert.Equal (0, _textView.CursorPosition.Y);
  6349. Assert.Equal (0, _textView.SelectionStartColumn);
  6350. Assert.Equal (0, _textView.SelectionStartRow);
  6351. Assert.Equal (0, _textView.SelectedLength);
  6352. Assert.Equal ("", _textView.SelectedText);
  6353. break;
  6354. case 6:
  6355. Assert.Equal (25, _textView.CursorPosition.X);
  6356. Assert.Equal (0, _textView.CursorPosition.Y);
  6357. Assert.Equal (0, _textView.SelectionStartColumn);
  6358. Assert.Equal (0, _textView.SelectionStartRow);
  6359. Assert.Equal (0, _textView.SelectedLength);
  6360. Assert.Equal ("", _textView.SelectedText);
  6361. break;
  6362. case 7:
  6363. Assert.Equal (12, _textView.CursorPosition.X);
  6364. Assert.Equal (0, _textView.CursorPosition.Y);
  6365. Assert.Equal (0, _textView.SelectionStartColumn);
  6366. Assert.Equal (0, _textView.SelectionStartRow);
  6367. Assert.Equal (0, _textView.SelectedLength);
  6368. Assert.Equal ("", _textView.SelectedText);
  6369. break;
  6370. case 8:
  6371. Assert.Equal (9, _textView.CursorPosition.X);
  6372. Assert.Equal (0, _textView.CursorPosition.Y);
  6373. Assert.Equal (0, _textView.SelectionStartColumn);
  6374. Assert.Equal (0, _textView.SelectionStartRow);
  6375. Assert.Equal (0, _textView.SelectedLength);
  6376. Assert.Equal ("", _textView.SelectedText);
  6377. break;
  6378. case 9:
  6379. Assert.Equal (6, _textView.CursorPosition.X);
  6380. Assert.Equal (0, _textView.CursorPosition.Y);
  6381. Assert.Equal (0, _textView.SelectionStartColumn);
  6382. Assert.Equal (0, _textView.SelectionStartRow);
  6383. Assert.Equal (0, _textView.SelectedLength);
  6384. Assert.Equal ("", _textView.SelectedText);
  6385. break;
  6386. case 10:
  6387. Assert.Equal (0, _textView.CursorPosition.X);
  6388. Assert.Equal (0, _textView.CursorPosition.Y);
  6389. Assert.Equal (0, _textView.SelectionStartColumn);
  6390. Assert.Equal (0, _textView.SelectionStartRow);
  6391. Assert.Equal (0, _textView.SelectedLength);
  6392. Assert.Equal ("", _textView.SelectedText);
  6393. break;
  6394. }
  6395. iteration++;
  6396. }
  6397. }
  6398. [Fact]
  6399. [TextViewTestsAutoInitShutdown]
  6400. public void WordBackward_With_Selection ()
  6401. {
  6402. _textView.CursorPosition = new (_textView.Text.Length, 0);
  6403. _textView.SelectionStartColumn = _textView.Text.Length;
  6404. _textView.SelectionStartRow = 0;
  6405. var iteration = 0;
  6406. while (_textView.CursorPosition.X > 0)
  6407. {
  6408. _textView.NewKeyDownEvent (Key.CursorLeft.WithCtrl.WithShift);
  6409. switch (iteration)
  6410. {
  6411. case 0:
  6412. Assert.Equal (31, _textView.CursorPosition.X);
  6413. Assert.Equal (0, _textView.CursorPosition.Y);
  6414. Assert.Equal (32, _textView.SelectionStartColumn);
  6415. Assert.Equal (0, _textView.SelectionStartRow);
  6416. Assert.Equal (1, _textView.SelectedLength);
  6417. Assert.Equal (".", _textView.SelectedText);
  6418. break;
  6419. case 1:
  6420. Assert.Equal (25, _textView.CursorPosition.X);
  6421. Assert.Equal (0, _textView.CursorPosition.Y);
  6422. Assert.Equal (32, _textView.SelectionStartColumn);
  6423. Assert.Equal (0, _textView.SelectionStartRow);
  6424. Assert.Equal (7, _textView.SelectedLength);
  6425. Assert.Equal ("fields.", _textView.SelectedText);
  6426. break;
  6427. case 2:
  6428. Assert.Equal (20, _textView.CursorPosition.X);
  6429. Assert.Equal (0, _textView.CursorPosition.Y);
  6430. Assert.Equal (32, _textView.SelectionStartColumn);
  6431. Assert.Equal (0, _textView.SelectionStartRow);
  6432. Assert.Equal (12, _textView.SelectedLength);
  6433. Assert.Equal ("text fields.", _textView.SelectedText);
  6434. break;
  6435. case 3:
  6436. Assert.Equal (12, _textView.CursorPosition.X);
  6437. Assert.Equal (0, _textView.CursorPosition.Y);
  6438. Assert.Equal (32, _textView.SelectionStartColumn);
  6439. Assert.Equal (0, _textView.SelectionStartRow);
  6440. Assert.Equal (20, _textView.SelectedLength);
  6441. Assert.Equal ("between text fields.", _textView.SelectedText);
  6442. break;
  6443. case 4:
  6444. Assert.Equal (7, _textView.CursorPosition.X);
  6445. Assert.Equal (0, _textView.CursorPosition.Y);
  6446. Assert.Equal (32, _textView.SelectionStartColumn);
  6447. Assert.Equal (0, _textView.SelectionStartRow);
  6448. Assert.Equal (25, _textView.SelectedLength);
  6449. Assert.Equal ("jump between text fields.", _textView.SelectedText);
  6450. break;
  6451. case 5:
  6452. Assert.Equal (4, _textView.CursorPosition.X);
  6453. Assert.Equal (0, _textView.CursorPosition.Y);
  6454. Assert.Equal (32, _textView.SelectionStartColumn);
  6455. Assert.Equal (0, _textView.SelectionStartRow);
  6456. Assert.Equal (28, _textView.SelectedLength);
  6457. Assert.Equal ("to jump between text fields.", _textView.SelectedText);
  6458. break;
  6459. case 6:
  6460. Assert.Equal (0, _textView.CursorPosition.X);
  6461. Assert.Equal (0, _textView.CursorPosition.Y);
  6462. Assert.Equal (32, _textView.SelectionStartColumn);
  6463. Assert.Equal (0, _textView.SelectionStartRow);
  6464. Assert.Equal (32, _textView.SelectedLength);
  6465. Assert.Equal ("TAB to jump between text fields.", _textView.SelectedText);
  6466. break;
  6467. }
  6468. iteration++;
  6469. }
  6470. }
  6471. [Fact]
  6472. [TextViewTestsAutoInitShutdown]
  6473. public void
  6474. WordBackward_With_The_Same_Values_For_SelectedStart_And_CursorPosition_And_Not_Starting_At_Beginning_Of_The_Text ()
  6475. {
  6476. _textView.CursorPosition = new (10, 0);
  6477. _textView.SelectionStartColumn = 10;
  6478. _textView.SelectionStartRow = 0;
  6479. var iteration = 0;
  6480. while (_textView.CursorPosition.X > 0)
  6481. {
  6482. _textView.NewKeyDownEvent (Key.CursorLeft.WithCtrl.WithShift);
  6483. switch (iteration)
  6484. {
  6485. case 0:
  6486. Assert.Equal (7, _textView.CursorPosition.X);
  6487. Assert.Equal (0, _textView.CursorPosition.Y);
  6488. Assert.Equal (10, _textView.SelectionStartColumn);
  6489. Assert.Equal (0, _textView.SelectionStartRow);
  6490. Assert.Equal (3, _textView.SelectedLength);
  6491. Assert.Equal ("jum", _textView.SelectedText);
  6492. break;
  6493. case 1:
  6494. Assert.Equal (4, _textView.CursorPosition.X);
  6495. Assert.Equal (0, _textView.CursorPosition.Y);
  6496. Assert.Equal (10, _textView.SelectionStartColumn);
  6497. Assert.Equal (0, _textView.SelectionStartRow);
  6498. Assert.Equal (6, _textView.SelectedLength);
  6499. Assert.Equal ("to jum", _textView.SelectedText);
  6500. break;
  6501. case 2:
  6502. Assert.Equal (0, _textView.CursorPosition.X);
  6503. Assert.Equal (0, _textView.CursorPosition.Y);
  6504. Assert.Equal (10, _textView.SelectionStartColumn);
  6505. Assert.Equal (0, _textView.SelectionStartRow);
  6506. Assert.Equal (10, _textView.SelectedLength);
  6507. Assert.Equal ("TAB to jum", _textView.SelectedText);
  6508. break;
  6509. }
  6510. iteration++;
  6511. }
  6512. }
  6513. [Fact]
  6514. public void WordBackward_WordForward_Limits_Return_Null ()
  6515. {
  6516. var model = new TextModel ();
  6517. model.LoadString ("Test");
  6518. (int col, int row)? newPos = model.WordBackward (0, 0);
  6519. Assert.Null (newPos);
  6520. newPos = model.WordForward (4, 0);
  6521. Assert.Null (newPos);
  6522. }
  6523. [Fact]
  6524. [TextViewTestsAutoInitShutdown]
  6525. public void WordForward_Multiline_With_Selection ()
  6526. {
  6527. // 1 2 3 4
  6528. // 01234567890123456789012 34567890123456789012345678-Length
  6529. // 1 2 1 2
  6530. // 01234567890123456789012 0123456789012345678901234
  6531. _textView.Text = "This is the first line.\nThis is the second line.";
  6532. _textView.SelectionStartColumn = _textView.CurrentColumn;
  6533. _textView.SelectionStartRow = _textView.CurrentRow;
  6534. var iteration = 0;
  6535. var iterationsFinished = false;
  6536. while (!iterationsFinished)
  6537. {
  6538. _textView.NewKeyDownEvent (
  6539. new (
  6540. KeyCode.CursorRight | KeyCode.CtrlMask | KeyCode.ShiftMask
  6541. )
  6542. );
  6543. switch (iteration)
  6544. {
  6545. case 0:
  6546. Assert.Equal (5, _textView.CursorPosition.X);
  6547. Assert.Equal (0, _textView.CursorPosition.Y);
  6548. Assert.Equal (0, _textView.SelectionStartColumn);
  6549. Assert.Equal (0, _textView.SelectionStartRow);
  6550. Assert.Equal (5, _textView.SelectedLength);
  6551. Assert.Equal ("This ", _textView.SelectedText);
  6552. break;
  6553. case 1:
  6554. Assert.Equal (8, _textView.CursorPosition.X);
  6555. Assert.Equal (0, _textView.CursorPosition.Y);
  6556. Assert.Equal (0, _textView.SelectionStartColumn);
  6557. Assert.Equal (0, _textView.SelectionStartRow);
  6558. Assert.Equal (8, _textView.SelectedLength);
  6559. Assert.Equal ("This is ", _textView.SelectedText);
  6560. break;
  6561. case 2:
  6562. Assert.Equal (12, _textView.CursorPosition.X);
  6563. Assert.Equal (0, _textView.CursorPosition.Y);
  6564. Assert.Equal (0, _textView.SelectionStartColumn);
  6565. Assert.Equal (0, _textView.SelectionStartRow);
  6566. Assert.Equal (12, _textView.SelectedLength);
  6567. Assert.Equal ("This is the ", _textView.SelectedText);
  6568. break;
  6569. case 3:
  6570. Assert.Equal (18, _textView.CursorPosition.X);
  6571. Assert.Equal (0, _textView.CursorPosition.Y);
  6572. Assert.Equal (0, _textView.SelectionStartColumn);
  6573. Assert.Equal (0, _textView.SelectionStartRow);
  6574. Assert.Equal (18, _textView.SelectedLength);
  6575. Assert.Equal ("This is the first ", _textView.SelectedText);
  6576. break;
  6577. case 4:
  6578. Assert.Equal (23, _textView.CursorPosition.X);
  6579. Assert.Equal (0, _textView.CursorPosition.Y);
  6580. Assert.Equal (0, _textView.SelectionStartColumn);
  6581. Assert.Equal (0, _textView.SelectionStartRow);
  6582. Assert.Equal (23, _textView.SelectedLength);
  6583. Assert.Equal ("This is the first line.", _textView.SelectedText);
  6584. break;
  6585. case 5:
  6586. Assert.Equal (0, _textView.CursorPosition.X);
  6587. Assert.Equal (1, _textView.CursorPosition.Y);
  6588. Assert.Equal (0, _textView.SelectionStartColumn);
  6589. Assert.Equal (0, _textView.SelectionStartRow);
  6590. Assert.Equal (23 + Environment.NewLine.Length, _textView.SelectedLength);
  6591. Assert.Equal ($"This is the first line.{Environment.NewLine}", _textView.SelectedText);
  6592. break;
  6593. case 6:
  6594. Assert.Equal (5, _textView.CursorPosition.X);
  6595. Assert.Equal (1, _textView.CursorPosition.Y);
  6596. Assert.Equal (0, _textView.SelectionStartColumn);
  6597. Assert.Equal (0, _textView.SelectionStartRow);
  6598. Assert.Equal (28 + Environment.NewLine.Length, _textView.SelectedLength);
  6599. Assert.Equal (
  6600. $"This is the first line.{Environment.NewLine}This ",
  6601. _textView.SelectedText
  6602. );
  6603. break;
  6604. case 7:
  6605. Assert.Equal (8, _textView.CursorPosition.X);
  6606. Assert.Equal (1, _textView.CursorPosition.Y);
  6607. Assert.Equal (0, _textView.SelectionStartColumn);
  6608. Assert.Equal (0, _textView.SelectionStartRow);
  6609. Assert.Equal (31 + Environment.NewLine.Length, _textView.SelectedLength);
  6610. Assert.Equal (
  6611. $"This is the first line.{Environment.NewLine}This is ",
  6612. _textView.SelectedText
  6613. );
  6614. break;
  6615. case 8:
  6616. Assert.Equal (12, _textView.CursorPosition.X);
  6617. Assert.Equal (1, _textView.CursorPosition.Y);
  6618. Assert.Equal (0, _textView.SelectionStartColumn);
  6619. Assert.Equal (0, _textView.SelectionStartRow);
  6620. Assert.Equal (35 + Environment.NewLine.Length, _textView.SelectedLength);
  6621. Assert.Equal (
  6622. $"This is the first line.{Environment.NewLine}This is the ",
  6623. _textView.SelectedText
  6624. );
  6625. break;
  6626. case 9:
  6627. Assert.Equal (19, _textView.CursorPosition.X);
  6628. Assert.Equal (1, _textView.CursorPosition.Y);
  6629. Assert.Equal (0, _textView.SelectionStartColumn);
  6630. Assert.Equal (0, _textView.SelectionStartRow);
  6631. Assert.Equal (42 + Environment.NewLine.Length, _textView.SelectedLength);
  6632. Assert.Equal (
  6633. $"This is the first line.{Environment.NewLine}This is the second ",
  6634. _textView.SelectedText
  6635. );
  6636. break;
  6637. case 10:
  6638. Assert.Equal (24, _textView.CursorPosition.X);
  6639. Assert.Equal (1, _textView.CursorPosition.Y);
  6640. Assert.Equal (0, _textView.SelectionStartColumn);
  6641. Assert.Equal (0, _textView.SelectionStartRow);
  6642. Assert.Equal (47 + Environment.NewLine.Length, _textView.SelectedLength);
  6643. Assert.Equal (
  6644. $"This is the first line.{Environment.NewLine}This is the second line.",
  6645. _textView.SelectedText
  6646. );
  6647. break;
  6648. default:
  6649. iterationsFinished = true;
  6650. break;
  6651. }
  6652. iteration++;
  6653. }
  6654. }
  6655. [Fact]
  6656. [TextViewTestsAutoInitShutdown]
  6657. public void WordForward_With_No_Selection ()
  6658. {
  6659. _textView.CursorPosition = Point.Empty;
  6660. var iteration = 0;
  6661. while (_textView.CursorPosition.X < _textView.Text.Length)
  6662. {
  6663. _textView.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  6664. switch (iteration)
  6665. {
  6666. case 0:
  6667. Assert.Equal (4, _textView.CursorPosition.X);
  6668. Assert.Equal (0, _textView.CursorPosition.Y);
  6669. Assert.Equal (0, _textView.SelectionStartColumn);
  6670. Assert.Equal (0, _textView.SelectionStartRow);
  6671. Assert.Equal (0, _textView.SelectedLength);
  6672. Assert.Equal ("", _textView.SelectedText);
  6673. break;
  6674. case 1:
  6675. Assert.Equal (7, _textView.CursorPosition.X);
  6676. Assert.Equal (0, _textView.CursorPosition.Y);
  6677. Assert.Equal (0, _textView.SelectionStartColumn);
  6678. Assert.Equal (0, _textView.SelectionStartRow);
  6679. Assert.Equal (0, _textView.SelectedLength);
  6680. Assert.Equal ("", _textView.SelectedText);
  6681. break;
  6682. case 2:
  6683. Assert.Equal (12, _textView.CursorPosition.X);
  6684. Assert.Equal (0, _textView.CursorPosition.Y);
  6685. Assert.Equal (0, _textView.SelectionStartColumn);
  6686. Assert.Equal (0, _textView.SelectionStartRow);
  6687. Assert.Equal (0, _textView.SelectedLength);
  6688. Assert.Equal ("", _textView.SelectedText);
  6689. break;
  6690. case 3:
  6691. Assert.Equal (20, _textView.CursorPosition.X);
  6692. Assert.Equal (0, _textView.CursorPosition.Y);
  6693. Assert.Equal (0, _textView.SelectionStartColumn);
  6694. Assert.Equal (0, _textView.SelectionStartRow);
  6695. Assert.Equal (0, _textView.SelectedLength);
  6696. Assert.Equal ("", _textView.SelectedText);
  6697. break;
  6698. case 4:
  6699. Assert.Equal (25, _textView.CursorPosition.X);
  6700. Assert.Equal (0, _textView.CursorPosition.Y);
  6701. Assert.Equal (0, _textView.SelectionStartColumn);
  6702. Assert.Equal (0, _textView.SelectionStartRow);
  6703. Assert.Equal (0, _textView.SelectedLength);
  6704. Assert.Equal ("", _textView.SelectedText);
  6705. break;
  6706. case 5:
  6707. Assert.Equal (32, _textView.CursorPosition.X);
  6708. Assert.Equal (0, _textView.CursorPosition.Y);
  6709. Assert.Equal (0, _textView.SelectionStartColumn);
  6710. Assert.Equal (0, _textView.SelectionStartRow);
  6711. Assert.Equal (0, _textView.SelectedLength);
  6712. Assert.Equal ("", _textView.SelectedText);
  6713. break;
  6714. }
  6715. iteration++;
  6716. }
  6717. }
  6718. [Fact]
  6719. [TextViewTestsAutoInitShutdown]
  6720. public void WordForward_With_No_Selection_And_With_More_Than_Only_One_Whitespace_And_With_Only_One_Letter ()
  6721. {
  6722. // 1 2 3 4 5
  6723. // 0123456789012345678901234567890123456789012345678901234=55 (Length)
  6724. _textView.Text = "TAB t o jump b etween t ext f ields .";
  6725. _textView.CursorPosition = Point.Empty;
  6726. var iteration = 0;
  6727. while (_textView.CursorPosition.X < _textView.Text.Length)
  6728. {
  6729. _textView.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  6730. switch (iteration)
  6731. {
  6732. case 0:
  6733. Assert.Equal (6, _textView.CursorPosition.X);
  6734. Assert.Equal (0, _textView.CursorPosition.Y);
  6735. Assert.Equal (0, _textView.SelectionStartColumn);
  6736. Assert.Equal (0, _textView.SelectionStartRow);
  6737. Assert.Equal (0, _textView.SelectedLength);
  6738. Assert.Equal ("", _textView.SelectedText);
  6739. break;
  6740. case 1:
  6741. Assert.Equal (9, _textView.CursorPosition.X);
  6742. Assert.Equal (0, _textView.CursorPosition.Y);
  6743. Assert.Equal (0, _textView.SelectionStartColumn);
  6744. Assert.Equal (0, _textView.SelectionStartRow);
  6745. Assert.Equal (0, _textView.SelectedLength);
  6746. Assert.Equal ("", _textView.SelectedText);
  6747. break;
  6748. case 2:
  6749. Assert.Equal (12, _textView.CursorPosition.X);
  6750. Assert.Equal (0, _textView.CursorPosition.Y);
  6751. Assert.Equal (0, _textView.SelectionStartColumn);
  6752. Assert.Equal (0, _textView.SelectionStartRow);
  6753. Assert.Equal (0, _textView.SelectedLength);
  6754. Assert.Equal ("", _textView.SelectedText);
  6755. break;
  6756. case 3:
  6757. Assert.Equal (25, _textView.CursorPosition.X);
  6758. Assert.Equal (0, _textView.CursorPosition.Y);
  6759. Assert.Equal (0, _textView.SelectionStartColumn);
  6760. Assert.Equal (0, _textView.SelectionStartRow);
  6761. Assert.Equal (0, _textView.SelectedLength);
  6762. Assert.Equal ("", _textView.SelectedText);
  6763. break;
  6764. case 4:
  6765. Assert.Equal (28, _textView.CursorPosition.X);
  6766. Assert.Equal (0, _textView.CursorPosition.Y);
  6767. Assert.Equal (0, _textView.SelectionStartColumn);
  6768. Assert.Equal (0, _textView.SelectionStartRow);
  6769. Assert.Equal (0, _textView.SelectedLength);
  6770. Assert.Equal ("", _textView.SelectedText);
  6771. break;
  6772. case 5:
  6773. Assert.Equal (38, _textView.CursorPosition.X);
  6774. Assert.Equal (0, _textView.CursorPosition.Y);
  6775. Assert.Equal (0, _textView.SelectionStartColumn);
  6776. Assert.Equal (0, _textView.SelectionStartRow);
  6777. Assert.Equal (0, _textView.SelectedLength);
  6778. Assert.Equal ("", _textView.SelectedText);
  6779. break;
  6780. case 6:
  6781. Assert.Equal (40, _textView.CursorPosition.X);
  6782. Assert.Equal (0, _textView.CursorPosition.Y);
  6783. Assert.Equal (0, _textView.SelectionStartColumn);
  6784. Assert.Equal (0, _textView.SelectionStartRow);
  6785. Assert.Equal (0, _textView.SelectedLength);
  6786. Assert.Equal ("", _textView.SelectedText);
  6787. break;
  6788. case 7:
  6789. Assert.Equal (46, _textView.CursorPosition.X);
  6790. Assert.Equal (0, _textView.CursorPosition.Y);
  6791. Assert.Equal (0, _textView.SelectionStartColumn);
  6792. Assert.Equal (0, _textView.SelectionStartRow);
  6793. Assert.Equal (0, _textView.SelectedLength);
  6794. Assert.Equal ("", _textView.SelectedText);
  6795. break;
  6796. case 8:
  6797. Assert.Equal (48, _textView.CursorPosition.X);
  6798. Assert.Equal (0, _textView.CursorPosition.Y);
  6799. Assert.Equal (0, _textView.SelectionStartColumn);
  6800. Assert.Equal (0, _textView.SelectionStartRow);
  6801. Assert.Equal (0, _textView.SelectedLength);
  6802. Assert.Equal ("", _textView.SelectedText);
  6803. break;
  6804. case 9:
  6805. Assert.Equal (54, _textView.CursorPosition.X);
  6806. Assert.Equal (0, _textView.CursorPosition.Y);
  6807. Assert.Equal (0, _textView.SelectionStartColumn);
  6808. Assert.Equal (0, _textView.SelectionStartRow);
  6809. Assert.Equal (0, _textView.SelectedLength);
  6810. Assert.Equal ("", _textView.SelectedText);
  6811. break;
  6812. case 10:
  6813. Assert.Equal (55, _textView.CursorPosition.X);
  6814. Assert.Equal (0, _textView.CursorPosition.Y);
  6815. Assert.Equal (0, _textView.SelectionStartColumn);
  6816. Assert.Equal (0, _textView.SelectionStartRow);
  6817. Assert.Equal (0, _textView.SelectedLength);
  6818. Assert.Equal ("", _textView.SelectedText);
  6819. break;
  6820. }
  6821. iteration++;
  6822. }
  6823. }
  6824. [Fact]
  6825. [TextViewTestsAutoInitShutdown]
  6826. public void WordForward_With_Selection ()
  6827. {
  6828. _textView.CursorPosition = Point.Empty;
  6829. _textView.SelectionStartColumn = 0;
  6830. _textView.SelectionStartRow = 0;
  6831. var iteration = 0;
  6832. while (_textView.CursorPosition.X < _textView.Text.Length)
  6833. {
  6834. _textView.NewKeyDownEvent (
  6835. new (
  6836. KeyCode.CursorRight | KeyCode.CtrlMask | KeyCode.ShiftMask
  6837. )
  6838. );
  6839. switch (iteration)
  6840. {
  6841. case 0:
  6842. Assert.Equal (4, _textView.CursorPosition.X);
  6843. Assert.Equal (0, _textView.CursorPosition.Y);
  6844. Assert.Equal (0, _textView.SelectionStartColumn);
  6845. Assert.Equal (0, _textView.SelectionStartRow);
  6846. Assert.Equal (4, _textView.SelectedLength);
  6847. Assert.Equal ("TAB ", _textView.SelectedText);
  6848. break;
  6849. case 1:
  6850. Assert.Equal (7, _textView.CursorPosition.X);
  6851. Assert.Equal (0, _textView.CursorPosition.Y);
  6852. Assert.Equal (0, _textView.SelectionStartColumn);
  6853. Assert.Equal (0, _textView.SelectionStartRow);
  6854. Assert.Equal (7, _textView.SelectedLength);
  6855. Assert.Equal ("TAB to ", _textView.SelectedText);
  6856. break;
  6857. case 2:
  6858. Assert.Equal (12, _textView.CursorPosition.X);
  6859. Assert.Equal (0, _textView.CursorPosition.Y);
  6860. Assert.Equal (0, _textView.SelectionStartColumn);
  6861. Assert.Equal (0, _textView.SelectionStartRow);
  6862. Assert.Equal (12, _textView.SelectedLength);
  6863. Assert.Equal ("TAB to jump ", _textView.SelectedText);
  6864. break;
  6865. case 3:
  6866. Assert.Equal (20, _textView.CursorPosition.X);
  6867. Assert.Equal (0, _textView.CursorPosition.Y);
  6868. Assert.Equal (0, _textView.SelectionStartColumn);
  6869. Assert.Equal (0, _textView.SelectionStartRow);
  6870. Assert.Equal (20, _textView.SelectedLength);
  6871. Assert.Equal ("TAB to jump between ", _textView.SelectedText);
  6872. break;
  6873. case 4:
  6874. Assert.Equal (25, _textView.CursorPosition.X);
  6875. Assert.Equal (0, _textView.CursorPosition.Y);
  6876. Assert.Equal (0, _textView.SelectionStartColumn);
  6877. Assert.Equal (0, _textView.SelectionStartRow);
  6878. Assert.Equal (25, _textView.SelectedLength);
  6879. Assert.Equal ("TAB to jump between text ", _textView.SelectedText);
  6880. break;
  6881. case 5:
  6882. Assert.Equal (32, _textView.CursorPosition.X);
  6883. Assert.Equal (0, _textView.CursorPosition.Y);
  6884. Assert.Equal (0, _textView.SelectionStartColumn);
  6885. Assert.Equal (0, _textView.SelectionStartRow);
  6886. Assert.Equal (32, _textView.SelectedLength);
  6887. Assert.Equal ("TAB to jump between text fields.", _textView.SelectedText);
  6888. break;
  6889. }
  6890. iteration++;
  6891. }
  6892. }
  6893. [Fact]
  6894. [TextViewTestsAutoInitShutdown]
  6895. public void
  6896. WordForward_With_The_Same_Values_For_SelectedStart_And_CursorPosition_And_Not_Starting_At_Beginning_Of_The_Text ()
  6897. {
  6898. _textView.CursorPosition = new (10, 0);
  6899. _textView.SelectionStartColumn = 10;
  6900. _textView.SelectionStartRow = 0;
  6901. var iteration = 0;
  6902. while (_textView.CursorPosition.X < _textView.Text.Length)
  6903. {
  6904. _textView.NewKeyDownEvent (
  6905. new (
  6906. KeyCode.CursorRight | KeyCode.CtrlMask | KeyCode.ShiftMask
  6907. )
  6908. );
  6909. switch (iteration)
  6910. {
  6911. case 0:
  6912. Assert.Equal (12, _textView.CursorPosition.X);
  6913. Assert.Equal (0, _textView.CursorPosition.Y);
  6914. Assert.Equal (10, _textView.SelectionStartColumn);
  6915. Assert.Equal (0, _textView.SelectionStartRow);
  6916. Assert.Equal (2, _textView.SelectedLength);
  6917. Assert.Equal ("p ", _textView.SelectedText);
  6918. break;
  6919. case 1:
  6920. Assert.Equal (20, _textView.CursorPosition.X);
  6921. Assert.Equal (0, _textView.CursorPosition.Y);
  6922. Assert.Equal (10, _textView.SelectionStartColumn);
  6923. Assert.Equal (0, _textView.SelectionStartRow);
  6924. Assert.Equal (10, _textView.SelectedLength);
  6925. Assert.Equal ("p between ", _textView.SelectedText);
  6926. break;
  6927. case 2:
  6928. Assert.Equal (25, _textView.CursorPosition.X);
  6929. Assert.Equal (0, _textView.CursorPosition.Y);
  6930. Assert.Equal (10, _textView.SelectionStartColumn);
  6931. Assert.Equal (0, _textView.SelectionStartRow);
  6932. Assert.Equal (15, _textView.SelectedLength);
  6933. Assert.Equal ("p between text ", _textView.SelectedText);
  6934. break;
  6935. case 3:
  6936. Assert.Equal (32, _textView.CursorPosition.X);
  6937. Assert.Equal (0, _textView.CursorPosition.Y);
  6938. Assert.Equal (10, _textView.SelectionStartColumn);
  6939. Assert.Equal (0, _textView.SelectionStartRow);
  6940. Assert.Equal (22, _textView.SelectedLength);
  6941. Assert.Equal ("p between text fields.", _textView.SelectedText);
  6942. break;
  6943. }
  6944. iteration++;
  6945. }
  6946. }
  6947. [Fact]
  6948. [AutoInitShutdown]
  6949. public void WordWrap_Deleting_Backwards ()
  6950. {
  6951. var tv = new TextView { Width = 5, Height = 2, WordWrap = true, Text = "aaaa" };
  6952. var top = new Toplevel ();
  6953. top.Add (tv);
  6954. Application.Begin (top);
  6955. Application.LayoutAndDraw ();
  6956. Assert.Equal (Point.Empty, tv.CursorPosition);
  6957. Assert.Equal (0, tv.LeftColumn);
  6958. DriverAssert.AssertDriverContentsAre (
  6959. @"
  6960. aaaa
  6961. ",
  6962. _output
  6963. );
  6964. tv.CursorPosition = new (5, 0);
  6965. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  6966. Application.LayoutAndDraw ();
  6967. Assert.Equal (0, tv.LeftColumn);
  6968. DriverAssert.AssertDriverContentsAre (
  6969. @"
  6970. aaa
  6971. ",
  6972. _output
  6973. );
  6974. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  6975. Application.LayoutAndDraw ();
  6976. Assert.Equal (0, tv.LeftColumn);
  6977. DriverAssert.AssertDriverContentsAre (
  6978. @"
  6979. aa
  6980. ",
  6981. _output
  6982. );
  6983. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  6984. Application.LayoutAndDraw ();
  6985. Assert.Equal (0, tv.LeftColumn);
  6986. DriverAssert.AssertDriverContentsAre (
  6987. @"
  6988. a
  6989. ",
  6990. _output
  6991. );
  6992. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  6993. Application.LayoutAndDraw ();
  6994. Assert.Equal (0, tv.LeftColumn);
  6995. DriverAssert.AssertDriverContentsAre (
  6996. @"
  6997. ",
  6998. _output
  6999. );
  7000. Assert.True (tv.NewKeyDownEvent (Key.Backspace));
  7001. Application.LayoutAndDraw ();
  7002. Assert.Equal (0, tv.LeftColumn);
  7003. DriverAssert.AssertDriverContentsAre (
  7004. @"
  7005. ",
  7006. _output
  7007. );
  7008. top.Dispose ();
  7009. }
  7010. [Theory]
  7011. [TextViewTestsAutoInitShutdown]
  7012. [InlineData (KeyCode.Delete)]
  7013. public void WordWrap_Draw_Typed_Keys_After_Text_Is_Deleted (KeyCode del)
  7014. {
  7015. var top = new Toplevel ();
  7016. top.Add (_textView);
  7017. _textView.Text = "Line 1.\nLine 2.";
  7018. _textView.WordWrap = true;
  7019. Application.Begin (top);
  7020. Application.LayoutAndDraw ();
  7021. Assert.True (_textView.WordWrap);
  7022. DriverAssert.AssertDriverContentsWithFrameAre (
  7023. @"
  7024. Line 1.
  7025. Line 2.",
  7026. _output
  7027. );
  7028. Assert.True (_textView.NewKeyDownEvent (Key.End.WithShift));
  7029. Assert.Equal ("Line 1.", _textView.SelectedText);
  7030. Assert.True (_textView.NewKeyDownEvent (new (del)));
  7031. Application.LayoutAndDraw ();
  7032. DriverAssert.AssertDriverContentsWithFrameAre ("Line 2.", _output);
  7033. Assert.True (_textView.NewKeyDownEvent (Key.H.WithShift));
  7034. Assert.NotEqual (Rectangle.Empty, _textView.NeedsDrawRect);
  7035. Application.LayoutAndDraw ();
  7036. DriverAssert.AssertDriverContentsWithFrameAre (
  7037. @"
  7038. H
  7039. Line 2.",
  7040. _output
  7041. );
  7042. top.Dispose ();
  7043. }
  7044. [Fact]
  7045. public void WordWrap_Gets_Sets ()
  7046. {
  7047. var tv = new TextView { WordWrap = true };
  7048. Assert.True (tv.WordWrap);
  7049. tv.WordWrap = false;
  7050. Assert.False (tv.WordWrap);
  7051. }
  7052. [Fact]
  7053. [AutoInitShutdown]
  7054. public void WordWrap_Not_Throw_If_Width_Is_Less_Than_Zero ()
  7055. {
  7056. Exception exception = Record.Exception (
  7057. () =>
  7058. {
  7059. var tv = new TextView
  7060. {
  7061. Width = Dim.Fill (),
  7062. Height = Dim.Fill (),
  7063. WordWrap = true,
  7064. Text = "これは、左右のクリップ境界をテストするための非常に長いテキストです。"
  7065. };
  7066. }
  7067. );
  7068. Assert.Null (exception);
  7069. }
  7070. [Fact]
  7071. [AutoInitShutdown]
  7072. public void WordWrap_ReadOnly_CursorPosition_SelectedText_Copy ()
  7073. {
  7074. // 0123456789
  7075. var text = "This is the first line.\nThis is the second line.\n";
  7076. var tv = new TextView { Width = 11, Height = 9 };
  7077. tv.Text = text;
  7078. Assert.Equal (
  7079. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}",
  7080. tv.Text
  7081. );
  7082. tv.WordWrap = true;
  7083. var top = new Toplevel ();
  7084. top.Add (tv);
  7085. top.Layout ();
  7086. tv.Draw ();
  7087. DriverAssert.AssertDriverContentsWithFrameAre (
  7088. @"
  7089. This is
  7090. the first
  7091. line.
  7092. This is
  7093. the
  7094. second
  7095. line.
  7096. ",
  7097. _output
  7098. );
  7099. tv.ReadOnly = true;
  7100. tv.CursorPosition = new (6, 2);
  7101. Assert.Equal (new (5, 2), tv.CursorPosition);
  7102. top.LayoutSubViews ();
  7103. View.SetClipToScreen ();
  7104. tv.Draw ();
  7105. DriverAssert.AssertDriverContentsWithFrameAre (
  7106. @"
  7107. This is
  7108. the first
  7109. line.
  7110. This is
  7111. the second
  7112. line.
  7113. ",
  7114. _output
  7115. );
  7116. tv.SelectionStartRow = 0;
  7117. tv.SelectionStartColumn = 0;
  7118. Assert.Equal ("This is the first line.", tv.SelectedText);
  7119. tv.Copy ();
  7120. Assert.Equal ("This is the first line.", Clipboard.Contents);
  7121. }
  7122. [Fact]
  7123. [TextViewTestsAutoInitShutdown]
  7124. public void WordWrap_True_LoadStream_New_Text ()
  7125. {
  7126. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  7127. _textView.WordWrap = true;
  7128. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  7129. var text = "This is the first line.\nThis is the second line.\n";
  7130. using (var stream = new MemoryStream ())
  7131. {
  7132. var writer = new StreamWriter (stream);
  7133. writer.Write (text);
  7134. writer.Flush ();
  7135. stream.Position = 0;
  7136. _textView.Load (stream);
  7137. Assert.Equal (
  7138. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}",
  7139. _textView.Text
  7140. );
  7141. Assert.True (_textView.WordWrap);
  7142. }
  7143. }
  7144. [Fact]
  7145. public void WordWrap_True_Text_Always_Returns_Unwrapped ()
  7146. {
  7147. var text = "This is the first line.\nThis is the second line.\n";
  7148. var tv = new TextView { Width = 10 };
  7149. tv.Text = text;
  7150. Assert.Equal (
  7151. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}",
  7152. tv.Text
  7153. );
  7154. tv.WordWrap = true;
  7155. Assert.Equal (
  7156. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}",
  7157. tv.Text
  7158. );
  7159. }
  7160. [Fact]
  7161. [TextViewTestsAutoInitShutdown]
  7162. public void WordWrap_WrapModel_Output ()
  7163. {
  7164. // 0123456789
  7165. var text = "This is the first line.\nThis is the second line.\n";
  7166. var tv = new TextView { Width = 10, Height = 10 };
  7167. tv.Text = text;
  7168. Assert.Equal (
  7169. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}",
  7170. tv.Text
  7171. );
  7172. tv.WordWrap = true;
  7173. var top = new Toplevel ();
  7174. top.Add (tv);
  7175. top.Layout ();
  7176. tv.Draw ();
  7177. DriverAssert.AssertDriverContentsWithFrameAre (
  7178. @"
  7179. This is
  7180. the
  7181. first
  7182. line.
  7183. This is
  7184. the
  7185. second
  7186. line.
  7187. ",
  7188. _output
  7189. );
  7190. }
  7191. private int GetLeftCol (int start)
  7192. {
  7193. string [] lines = _textView.Text.Split (Environment.NewLine);
  7194. if (lines == null || lines.Length == 0)
  7195. {
  7196. return 0;
  7197. }
  7198. if (start == _textView.LeftColumn)
  7199. {
  7200. return start;
  7201. }
  7202. if (_textView.LeftColumn == _textView.CurrentColumn)
  7203. {
  7204. return _textView.CurrentColumn;
  7205. }
  7206. int cCol = _textView.CurrentColumn;
  7207. string line = lines [_textView.CurrentRow];
  7208. int lCount = cCol > line.Length - 1 ? line.Length - 1 : cCol;
  7209. int width = _textView.Frame.Width;
  7210. int tabWidth = _textView.TabWidth;
  7211. var sumLength = 0;
  7212. var col = 0;
  7213. for (int i = lCount; i >= 0; i--)
  7214. {
  7215. char r = line [i];
  7216. sumLength += ((Rune)r).GetColumns ();
  7217. if (r == '\t')
  7218. {
  7219. sumLength += tabWidth + 1;
  7220. }
  7221. if (sumLength > width)
  7222. {
  7223. if (col + width == cCol)
  7224. {
  7225. col++;
  7226. }
  7227. break;
  7228. }
  7229. if ((cCol < line.Length && col > 0 && start < cCol && col == start) || cCol - col == width - 1)
  7230. {
  7231. break;
  7232. }
  7233. col = i;
  7234. }
  7235. return col;
  7236. }
  7237. // This class enables test functions annotated with the [InitShutdown] attribute
  7238. // to have a function called before the test function is called and after.
  7239. //
  7240. // This is necessary because a) Application is a singleton and Init/Shutdown must be called
  7241. // as a pair, and b) all unit test functions should be atomic.
  7242. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]
  7243. public class TextViewTestsAutoInitShutdown : AutoInitShutdownAttribute
  7244. {
  7245. public static string Txt = "TAB to jump between text fields.";
  7246. public TextViewTestsAutoInitShutdown () : base () { }
  7247. public override void After (MethodInfo methodUnderTest)
  7248. {
  7249. _textView = null;
  7250. base.After (methodUnderTest);
  7251. }
  7252. public override void Before (MethodInfo methodUnderTest)
  7253. {
  7254. FakeDriver.FakeBehaviors.UseFakeClipboard = true;
  7255. base.Before (methodUnderTest);
  7256. // 1 2 3
  7257. // 01234567890123456789012345678901=32 (Length)
  7258. byte [] buff = Encoding.Unicode.GetBytes (Txt);
  7259. byte [] ms = new MemoryStream (buff).ToArray ();
  7260. _textView = new () { Width = 30, Height = 10, SchemeName = "Base" };
  7261. _textView.Text = Encoding.Unicode.GetString (ms);
  7262. }
  7263. }
  7264. [Fact]
  7265. public void HotKey_Command_SetsFocus ()
  7266. {
  7267. var view = new TextView ();
  7268. view.CanFocus = true;
  7269. Assert.False (view.HasFocus);
  7270. view.InvokeCommand (Command.HotKey);
  7271. Assert.True (view.HasFocus);
  7272. }
  7273. [Fact]
  7274. public void HotKey_Command_Does_Not_Accept ()
  7275. {
  7276. var view = new TextView ();
  7277. var accepted = false;
  7278. view.Accepting += OnAccept;
  7279. view.InvokeCommand (Command.HotKey);
  7280. Assert.False (accepted);
  7281. return;
  7282. void OnAccept (object sender, CommandEventArgs e) { accepted = true; }
  7283. }
  7284. [Theory]
  7285. [InlineData (false, 1)]
  7286. [InlineData (true, 1)]
  7287. public void Accepted_Command_Raises_Accepted_Regardles_Of_AllowsReturn (bool allowsReturn, int expectedAcceptEvents)
  7288. {
  7289. var view = new TextView
  7290. {
  7291. AllowsReturn = allowsReturn
  7292. };
  7293. var acceptedEvents = 0;
  7294. view.Accepting += Accept;
  7295. view.InvokeCommand (Command.Accept);
  7296. Assert.Equal (expectedAcceptEvents, acceptedEvents);
  7297. return;
  7298. void Accept (object sender, CommandEventArgs e) { acceptedEvents++; }
  7299. }
  7300. [Theory]
  7301. [InlineData (false, 1)]
  7302. [InlineData (true, 0)]
  7303. public void Enter_Key_Fires_Accepted_BasedOn_AllowsReturn (bool allowsReturn, int expectedAccepts)
  7304. {
  7305. var view = new TextView
  7306. {
  7307. Multiline = allowsReturn
  7308. };
  7309. var accepted = 0;
  7310. view.Accepting += Accept;
  7311. view.NewKeyDownEvent (Key.Enter);
  7312. Assert.Equal (expectedAccepts, accepted);
  7313. return;
  7314. void Accept (object sender, CommandEventArgs e) { accepted++; }
  7315. }
  7316. [Theory]
  7317. [InlineData (false, 1)]
  7318. [InlineData (true, 0)]
  7319. public void Enter_Key_Fires_Accepted_BasedOn_Multiline (bool multiline, int expectedAccepts)
  7320. {
  7321. var view = new TextView
  7322. {
  7323. Multiline = multiline
  7324. };
  7325. var accepted = 0;
  7326. view.Accepting += Accept;
  7327. view.NewKeyDownEvent (Key.Enter);
  7328. Assert.Equal (expectedAccepts, accepted);
  7329. return;
  7330. void Accept (object sender, CommandEventArgs e) { accepted++; }
  7331. }
  7332. [Fact]
  7333. public void Space_Key_Types_Space ()
  7334. {
  7335. var view = new TextView ();
  7336. view.NewKeyDownEvent (Key.Space);
  7337. Assert.Equal (" ", view.Text);
  7338. }
  7339. [Theory]
  7340. [InlineData (false, false, 1, 1)]
  7341. [InlineData (false, true, 1, 0)]
  7342. [InlineData (true, false, 0, 0)]
  7343. [InlineData (true, true, 0, 0)]
  7344. public void Accepted_Event_Handled_Prevents_Default_Button_Accept (bool multiline, bool handleAccept, int expectedAccepts, int expectedButtonAccepts)
  7345. {
  7346. var superView = new Window ();
  7347. var tv = new TextView
  7348. {
  7349. Multiline = multiline
  7350. };
  7351. var button = new Button
  7352. {
  7353. IsDefault = true
  7354. };
  7355. superView.Add (tv, button);
  7356. var buttonAccept = 0;
  7357. button.Accepting += ButtonAccept;
  7358. var textViewAccept = 0;
  7359. tv.Accepting += TextViewAccept;
  7360. tv.SetFocus ();
  7361. Assert.True (tv.HasFocus);
  7362. superView.NewKeyDownEvent (Key.Enter);
  7363. Assert.Equal (expectedAccepts, textViewAccept);
  7364. Assert.Equal (expectedButtonAccepts, buttonAccept);
  7365. button.SetFocus ();
  7366. superView.NewKeyDownEvent (Key.Enter);
  7367. Assert.Equal (expectedAccepts, textViewAccept);
  7368. Assert.Equal (expectedButtonAccepts + 1, buttonAccept);
  7369. return;
  7370. void TextViewAccept (object sender, CommandEventArgs e)
  7371. {
  7372. textViewAccept++;
  7373. e.Handled = handleAccept;
  7374. }
  7375. void ButtonAccept (object sender, CommandEventArgs e) { buttonAccept++; }
  7376. }
  7377. [Theory]
  7378. [InlineData (true, 0)]
  7379. [InlineData (false, 1)]
  7380. public void Accepted_No_Handler_Enables_Default_Button_Accept (bool multiline, int expectedButtonAccept)
  7381. {
  7382. var superView = new Window ();
  7383. var tv = new TextView
  7384. {
  7385. Multiline = multiline
  7386. };
  7387. var button = new Button
  7388. {
  7389. IsDefault = true
  7390. };
  7391. superView.Add (tv, button);
  7392. var buttonAccept = 0;
  7393. button.Accepting += ButtonAccept;
  7394. tv.SetFocus ();
  7395. Assert.True (tv.HasFocus);
  7396. superView.NewKeyDownEvent (Key.Enter);
  7397. Assert.Equal (expectedButtonAccept, buttonAccept);
  7398. button.SetFocus ();
  7399. superView.NewKeyDownEvent (Key.Enter);
  7400. Assert.Equal (expectedButtonAccept + 1, buttonAccept);
  7401. return;
  7402. void ButtonAccept (object sender, CommandEventArgs e) { buttonAccept++; }
  7403. }
  7404. [Fact]
  7405. public void Autocomplete_Popup_Added_To_SuperView_On_Init ()
  7406. {
  7407. View superView = new ()
  7408. {
  7409. CanFocus = true
  7410. };
  7411. TextView t = new ();
  7412. superView.Add (t);
  7413. Assert.Single (superView.SubViews);
  7414. superView.BeginInit ();
  7415. superView.EndInit ();
  7416. Assert.Equal (2, superView.SubViews.Count);
  7417. }
  7418. [Fact]
  7419. public void Autocomplete__Added_To_SuperView_On_Add ()
  7420. {
  7421. View superView = new ()
  7422. {
  7423. CanFocus = true,
  7424. Id = "superView"
  7425. };
  7426. superView.BeginInit ();
  7427. superView.EndInit ();
  7428. Assert.Empty (superView.SubViews);
  7429. TextView t = new ()
  7430. {
  7431. Id = "t"
  7432. };
  7433. superView.Add (t);
  7434. Assert.Equal (2, superView.SubViews.Count);
  7435. }
  7436. [Fact]
  7437. public void Autocomplete_Visible_False_By_Default ()
  7438. {
  7439. View superView = new ()
  7440. {
  7441. CanFocus = true
  7442. };
  7443. TextView t = new ();
  7444. superView.Add (t);
  7445. superView.BeginInit ();
  7446. superView.EndInit ();
  7447. Assert.Equal (2, superView.SubViews.Count);
  7448. Assert.True (t.Visible);
  7449. Assert.False (t.Autocomplete.Visible);
  7450. }
  7451. [Fact]
  7452. public void Right_CursorAtEnd_WithSelection_ShouldClearSelection ()
  7453. {
  7454. var tv = new TextView
  7455. {
  7456. Text = "Hello"
  7457. };
  7458. tv.SetFocus ();
  7459. tv.NewKeyDownEvent (Key.End.WithShift);
  7460. Assert.Equal (5, tv.CursorPosition.X);
  7461. // When there is selected text and the cursor is at the end of the text field
  7462. Assert.Equal ("Hello", tv.SelectedText);
  7463. // Pressing right should not move focus, instead it should clear selection
  7464. Assert.True (tv.NewKeyDownEvent (Key.CursorRight));
  7465. Assert.Empty (tv.SelectedText);
  7466. // Now that the selection is cleared another right keypress should move focus
  7467. Assert.False (tv.NewKeyDownEvent (Key.CursorRight));
  7468. }
  7469. [Fact]
  7470. public void Left_CursorAtStart_WithSelection_ShouldClearSelection ()
  7471. {
  7472. var tv = new TextView
  7473. {
  7474. Text = "Hello"
  7475. };
  7476. tv.SetFocus ();
  7477. tv.NewKeyDownEvent (Key.CursorRight);
  7478. tv.NewKeyDownEvent (Key.CursorRight);
  7479. Assert.Equal (2, tv.CursorPosition.X);
  7480. Assert.True (tv.NewKeyDownEvent (Key.CursorLeft.WithShift));
  7481. Assert.True (tv.NewKeyDownEvent (Key.CursorLeft.WithShift));
  7482. // When there is selected text and the cursor is at the start of the text field
  7483. Assert.Equal ("He", tv.SelectedText);
  7484. // Pressing left should not move focus, instead it should clear selection
  7485. Assert.True (tv.NewKeyDownEvent (Key.CursorLeft));
  7486. Assert.Empty (tv.SelectedText);
  7487. // When clearing selected text with left the cursor should be at the start of the selection
  7488. Assert.Equal (0, tv.CursorPosition.X);
  7489. // Now that the selection is cleared another left keypress should move focus
  7490. Assert.False (tv.NewKeyDownEvent (Key.CursorLeft));
  7491. }
  7492. [Fact]
  7493. [AutoInitShutdown]
  7494. public void Draw_Esc_Rune ()
  7495. {
  7496. var tv = new TextView { Width = 5, Height = 1, Text = "\u001b" };
  7497. tv.BeginInit ();
  7498. tv.EndInit ();
  7499. tv.Draw ();
  7500. DriverAssert.AssertDriverContentsWithFrameAre ("\u241b", _output);
  7501. tv.Dispose ();
  7502. }
  7503. [Fact]
  7504. public void Equals_True ()
  7505. {
  7506. var c1 = new Cell ();
  7507. var c2 = new Cell ();
  7508. Assert.True (c1.Equals (c2));
  7509. Assert.True (c2.Equals (c1));
  7510. c1.Rune = new ('a');
  7511. c1.Attribute = new ();
  7512. c2.Rune = new ('a');
  7513. c2.Attribute = new ();
  7514. Assert.True (c1.Equals (c2));
  7515. Assert.True (c2.Equals (c1));
  7516. }
  7517. [Fact]
  7518. [AutoInitShutdown]
  7519. public void CellEventArgs_WordWrap_True ()
  7520. {
  7521. var eventCount = 0;
  7522. List<List<Cell>> text =
  7523. [
  7524. Cell.ToCells (
  7525. "This is the first line.".ToRunes ()
  7526. ),
  7527. Cell.ToCells (
  7528. "This is the second line.".ToRunes ()
  7529. )
  7530. ];
  7531. TextView tv = CreateTextView ();
  7532. tv.DrawNormalColor += TextView_DrawColor;
  7533. tv.DrawReadOnlyColor += TextView_DrawColor;
  7534. tv.DrawSelectionColor += TextView_DrawColor;
  7535. tv.DrawUsedColor += TextView_DrawColor;
  7536. void TextView_DrawColor (object sender, CellEventArgs e)
  7537. {
  7538. Assert.Equal (e.Line [e.Col], text [e.UnwrappedPosition.Row] [e.UnwrappedPosition.Col]);
  7539. eventCount++;
  7540. }
  7541. tv.Text = $"{Cell.ToString (text [0])}\n{Cell.ToString (text [1])}\n";
  7542. Assert.False (tv.WordWrap);
  7543. var top = new Toplevel ();
  7544. top.Add (tv);
  7545. Application.Begin (top);
  7546. Application.LayoutAndDraw ();
  7547. DriverAssert.AssertDriverContentsWithFrameAre (
  7548. @"
  7549. This is the first line.
  7550. This is the second line.",
  7551. _output
  7552. );
  7553. tv.Width = 10;
  7554. tv.Height = 25;
  7555. tv.WordWrap = true;
  7556. Application.LayoutAndDraw ();
  7557. DriverAssert.AssertDriverContentsWithFrameAre (
  7558. @"
  7559. This is
  7560. the
  7561. first
  7562. line.
  7563. This is
  7564. the
  7565. second
  7566. line. ",
  7567. _output
  7568. );
  7569. Assert.Equal (eventCount, (text [0].Count + text [1].Count) * 2);
  7570. top.Dispose ();
  7571. }
  7572. // BUGBUG: This test depends on the order of the schemes in SchemeManager.Schemes.
  7573. // BUGBUG: Breaks on Mac/Linux?
  7574. [Fact (Skip = "This test depends on the order of the schemes in SchemeManager.Schemes.")]
  7575. [AutoInitShutdown]
  7576. public void Cell_LoadCells_InheritsPreviousAttribute ()
  7577. {
  7578. List<Cell> cells = [];
  7579. foreach (KeyValuePair<string, Scheme> color in SchemeManager.GetSchemes ())
  7580. {
  7581. string csName = color.Key;
  7582. foreach (Rune rune in csName.EnumerateRunes ())
  7583. {
  7584. cells.Add (new () { Rune = rune, Attribute = color.Value.Normal });
  7585. }
  7586. cells.Add (new () { Rune = (Rune)'\n', Attribute = color.Value.Focus });
  7587. }
  7588. TextView tv = CreateTextView ();
  7589. tv.Load (cells);
  7590. var top = new Toplevel ();
  7591. top.Add (tv);
  7592. RunState rs = Application.Begin (top);
  7593. Application.LayoutAndDraw ();
  7594. Assert.True (tv.InheritsPreviousAttribute);
  7595. var expectedText = @"
  7596. TopLevel
  7597. Base
  7598. Dialog
  7599. Menu
  7600. Error ";
  7601. DriverAssert.AssertDriverContentsWithFrameAre (expectedText, _output);
  7602. Attribute [] attributes =
  7603. {
  7604. // 0
  7605. SchemeManager.GetSchemes () ["TopLevel"].Normal,
  7606. // 1
  7607. SchemeManager.GetSchemes () ["Base"].Normal,
  7608. // 2
  7609. SchemeManager.GetSchemes () ["Dialog"].Normal,
  7610. // 3
  7611. SchemeManager.GetSchemes () ["Menu"].Normal,
  7612. // 4
  7613. SchemeManager.GetSchemes () ["Error"].Normal,
  7614. // 5
  7615. tv.GetScheme ()!.Focus
  7616. };
  7617. var expectedColor = @"
  7618. 0000000055
  7619. 1111555555
  7620. 2222225555
  7621. 3333555555
  7622. 4444455555";
  7623. DriverAssert.AssertDriverAttributesAre (expectedColor, _output, Application.Driver, attributes);
  7624. tv.WordWrap = true;
  7625. Application.LayoutAndDraw ();
  7626. DriverAssert.AssertDriverContentsWithFrameAre (expectedText, _output);
  7627. DriverAssert.AssertDriverAttributesAre (expectedColor, _output, Application.Driver, attributes);
  7628. tv.CursorPosition = new (6, 2);
  7629. tv.SelectionStartColumn = 0;
  7630. tv.SelectionStartRow = 0;
  7631. Assert.Equal ($"TopLevel{Environment.NewLine}Base{Environment.NewLine}Dialog", tv.SelectedText);
  7632. tv.Copy ();
  7633. tv.IsSelecting = false;
  7634. tv.CursorPosition = new (2, 4);
  7635. tv.Paste ();
  7636. Application.LayoutAndDraw ();
  7637. expectedText = @"
  7638. TopLevel
  7639. Base
  7640. Dialog
  7641. Menu
  7642. ErTopLevel
  7643. Base
  7644. Dialogror ";
  7645. DriverAssert.AssertDriverContentsWithFrameAre (expectedText, _output);
  7646. expectedColor = @"
  7647. 0000000055
  7648. 1111555555
  7649. 2222225555
  7650. 3333555555
  7651. 4400000000
  7652. 1111555555
  7653. 2222224445";
  7654. DriverAssert.AssertDriverAttributesAre (expectedColor, _output, Application.Driver, attributes);
  7655. tv.Undo ();
  7656. tv.CursorPosition = new (0, 3);
  7657. tv.SelectionStartColumn = 0;
  7658. tv.SelectionStartRow = 0;
  7659. Assert.Equal (
  7660. $"TopLevel{Environment.NewLine}Base{Environment.NewLine}Dialog{Environment.NewLine}",
  7661. tv.SelectedText
  7662. );
  7663. tv.Copy ();
  7664. tv.IsSelecting = false;
  7665. tv.CursorPosition = new (2, 4);
  7666. tv.Paste ();
  7667. Application.LayoutAndDraw ();
  7668. expectedText = @"
  7669. TopLevel
  7670. Base
  7671. Dialog
  7672. Menu
  7673. ErTopLevel
  7674. Base
  7675. Dialog
  7676. ror ";
  7677. DriverAssert.AssertDriverContentsWithFrameAre (expectedText, _output);
  7678. expectedColor = @"
  7679. 0000000055
  7680. 1111555555
  7681. 2222225555
  7682. 3333555555
  7683. 4400000000
  7684. 1111555555
  7685. 2222225555
  7686. 4445555555";
  7687. DriverAssert.AssertDriverAttributesAre (expectedColor, _output, Application.Driver, attributes);
  7688. Application.End (rs);
  7689. top.Dispose ();
  7690. }
  7691. [Fact]
  7692. public void Cell_LoadCells_Without_Scheme_Is_Never_Null ()
  7693. {
  7694. List<Cell> cells = new ()
  7695. {
  7696. new () { Rune = new ('T') },
  7697. new () { Rune = new ('e') },
  7698. new () { Rune = new ('s') },
  7699. new () { Rune = new ('t') }
  7700. };
  7701. TextView tv = CreateTextView ();
  7702. var top = new Toplevel ();
  7703. top.Add (tv);
  7704. tv.Load (cells);
  7705. for (var i = 0; i < tv.Lines; i++)
  7706. {
  7707. List<Cell> line = tv.GetLine (i);
  7708. foreach (Cell c in line)
  7709. {
  7710. Assert.NotNull (c.Attribute);
  7711. }
  7712. }
  7713. }
  7714. private TextView CreateTextView () { return new () { Width = 30, Height = 10 }; }
  7715. }