2
0

TextViewTests.cs 341 KB

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