TextViewTests.cs 345 KB

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