TextViewTests.cs 346 KB

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