TextViewTests.cs 345 KB

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