TextViewTests.cs 274 KB

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