2
0

TextViewTests.cs 332 KB

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