TextViewTests.cs 275 KB

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