TextViewTests.cs 323 KB

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