TextViewTests.cs 274 KB

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