TextViewTests.cs 333 KB

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