TextViewTests.cs 331 KB

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