TextViewTests.cs 329 KB

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