2
0

TextViewTests.cs 344 KB

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