TextViewTests.cs 344 KB

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