TextViewTests.cs 277 KB

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