test_text_edit.h 163 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335
  1. /**************************************************************************/
  2. /* test_text_edit.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_TEXT_EDIT_H
  31. #define TEST_TEXT_EDIT_H
  32. #include "scene/gui/text_edit.h"
  33. #include "tests/test_macros.h"
  34. namespace TestTextEdit {
  35. TEST_CASE("[SceneTree][TextEdit] text entry") {
  36. TextEdit *text_edit = memnew(TextEdit);
  37. SceneTree::get_singleton()->get_root()->add_child(text_edit);
  38. text_edit->grab_focus();
  39. Array empty_signal_args;
  40. empty_signal_args.push_back(Array());
  41. SUBCASE("[TextEdit] text entry") {
  42. SIGNAL_WATCH(text_edit, "text_set");
  43. SIGNAL_WATCH(text_edit, "text_changed");
  44. SIGNAL_WATCH(text_edit, "lines_edited_from");
  45. SIGNAL_WATCH(text_edit, "caret_changed");
  46. Array args1;
  47. args1.push_back(0);
  48. args1.push_back(0);
  49. Array lines_edited_args;
  50. lines_edited_args.push_back(args1);
  51. lines_edited_args.push_back(args1.duplicate());
  52. SUBCASE("[TextEdit] clear and set text") {
  53. // "text_changed" should not be emitted on clear / set.
  54. text_edit->clear();
  55. MessageQueue::get_singleton()->flush();
  56. CHECK(text_edit->get_text() == "");
  57. CHECK(text_edit->get_caret_column() == 0);
  58. CHECK(text_edit->get_line_count() == 1);
  59. SIGNAL_CHECK("text_set", empty_signal_args);
  60. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  61. SIGNAL_CHECK_FALSE("caret_changed");
  62. SIGNAL_CHECK_FALSE("text_changed");
  63. text_edit->set_text("test text");
  64. MessageQueue::get_singleton()->flush();
  65. CHECK(text_edit->get_text() == "test text");
  66. CHECK(text_edit->get_caret_column() == 0);
  67. CHECK(text_edit->get_line_count() == 1);
  68. SIGNAL_CHECK("text_set", empty_signal_args);
  69. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  70. SIGNAL_CHECK("caret_changed", empty_signal_args);
  71. SIGNAL_CHECK_FALSE("text_changed");
  72. text_edit->clear();
  73. MessageQueue::get_singleton()->flush();
  74. CHECK(text_edit->get_text() == "");
  75. CHECK(text_edit->get_caret_column() == 0);
  76. SIGNAL_CHECK("text_set", empty_signal_args);
  77. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  78. SIGNAL_CHECK_FALSE("caret_changed");
  79. SIGNAL_CHECK_FALSE("text_changed");
  80. // Can undo / redo words when editable.
  81. text_edit->undo();
  82. MessageQueue::get_singleton()->flush();
  83. CHECK(text_edit->get_text() == "test text");
  84. CHECK(text_edit->get_caret_column() == 0);
  85. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  86. SIGNAL_CHECK("text_changed", empty_signal_args);
  87. SIGNAL_CHECK_FALSE("caret_changed");
  88. SIGNAL_CHECK_FALSE("text_set");
  89. text_edit->redo();
  90. MessageQueue::get_singleton()->flush();
  91. CHECK(text_edit->get_text() == "");
  92. CHECK(text_edit->get_caret_column() == 0);
  93. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  94. SIGNAL_CHECK("text_changed", empty_signal_args);
  95. SIGNAL_CHECK_FALSE("caret_changed");
  96. SIGNAL_CHECK_FALSE("text_set");
  97. // Cannot undo when not-editable but should still clear.
  98. text_edit->undo();
  99. MessageQueue::get_singleton()->flush();
  100. CHECK(text_edit->get_text() == "test text");
  101. CHECK(text_edit->get_caret_column() == 0);
  102. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  103. SIGNAL_CHECK("text_changed", empty_signal_args);
  104. SIGNAL_CHECK_FALSE("caret_changed");
  105. SIGNAL_CHECK_FALSE("text_set");
  106. // Clear.
  107. text_edit->set_editable(false);
  108. Array lines_edited_clear_args;
  109. Array new_args = args1.duplicate();
  110. new_args[0] = 1;
  111. lines_edited_clear_args.push_back(new_args);
  112. text_edit->clear();
  113. MessageQueue::get_singleton()->flush();
  114. CHECK(text_edit->get_text() == "");
  115. CHECK(text_edit->get_caret_column() == 0);
  116. SIGNAL_CHECK("text_set", empty_signal_args);
  117. SIGNAL_CHECK("lines_edited_from", lines_edited_clear_args);
  118. SIGNAL_CHECK_FALSE("caret_changed");
  119. SIGNAL_CHECK_FALSE("text_changed");
  120. text_edit->set_editable(true);
  121. text_edit->undo();
  122. MessageQueue::get_singleton()->flush();
  123. CHECK(text_edit->get_text() == "");
  124. CHECK(text_edit->get_caret_column() == 0);
  125. SIGNAL_CHECK_FALSE("text_set");
  126. SIGNAL_CHECK_FALSE("lines_edited_from");
  127. SIGNAL_CHECK_FALSE("text_changed");
  128. SIGNAL_CHECK_FALSE("caret_changed");
  129. // Can still undo set_text.
  130. text_edit->set_editable(false);
  131. text_edit->set_text("test text");
  132. MessageQueue::get_singleton()->flush();
  133. CHECK(text_edit->get_text() == "test text");
  134. CHECK(text_edit->get_caret_column() == 0);
  135. SIGNAL_CHECK("text_set", empty_signal_args);
  136. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  137. SIGNAL_CHECK("caret_changed", empty_signal_args);
  138. SIGNAL_CHECK_FALSE("text_changed");
  139. text_edit->set_editable(true);
  140. text_edit->undo();
  141. MessageQueue::get_singleton()->flush();
  142. CHECK(text_edit->get_text() == "");
  143. CHECK(text_edit->get_caret_column() == 0);
  144. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  145. SIGNAL_CHECK("text_changed", empty_signal_args);
  146. SIGNAL_CHECK_FALSE("caret_changed");
  147. SIGNAL_CHECK_FALSE("text_set");
  148. // Any selections are removed.
  149. text_edit->set_text("test text");
  150. MessageQueue::get_singleton()->flush();
  151. text_edit->select_all();
  152. SIGNAL_CHECK("caret_changed", empty_signal_args);
  153. MessageQueue::get_singleton()->flush();
  154. CHECK(text_edit->get_text() == "test text");
  155. CHECK(text_edit->get_caret_column() == 9);
  156. CHECK(text_edit->has_selection());
  157. SIGNAL_CHECK("text_set", empty_signal_args);
  158. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  159. SIGNAL_CHECK("caret_changed", empty_signal_args);
  160. SIGNAL_CHECK_FALSE("text_changed");
  161. text_edit->set_text("test");
  162. MessageQueue::get_singleton()->flush();
  163. CHECK(text_edit->get_text() == "test");
  164. CHECK(text_edit->get_caret_column() == 0);
  165. CHECK_FALSE(text_edit->has_selection());
  166. SIGNAL_CHECK("text_set", empty_signal_args);
  167. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  168. SIGNAL_CHECK("caret_changed", empty_signal_args);
  169. SIGNAL_CHECK_FALSE("text_changed");
  170. text_edit->select_all();
  171. MessageQueue::get_singleton()->flush();
  172. SIGNAL_CHECK("caret_changed", empty_signal_args);
  173. CHECK(text_edit->has_selection());
  174. text_edit->clear();
  175. MessageQueue::get_singleton()->flush();
  176. CHECK(text_edit->get_text() == "");
  177. CHECK(text_edit->get_caret_column() == 0);
  178. CHECK_FALSE(text_edit->has_selection());
  179. SIGNAL_CHECK("text_set", empty_signal_args);
  180. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  181. SIGNAL_CHECK("caret_changed", empty_signal_args);
  182. SIGNAL_CHECK_FALSE("text_changed");
  183. }
  184. SUBCASE("[TextEdit] set and get line") {
  185. // Set / Get line is 0 indexed.
  186. text_edit->set_line(1, "test");
  187. MessageQueue::get_singleton()->flush();
  188. CHECK(text_edit->get_text() == "");
  189. SIGNAL_CHECK_FALSE("lines_edited_from");
  190. SIGNAL_CHECK_FALSE("text_set");
  191. SIGNAL_CHECK_FALSE("text_changed");
  192. SIGNAL_CHECK_FALSE("caret_changed");
  193. text_edit->set_line(0, "test");
  194. MessageQueue::get_singleton()->flush();
  195. CHECK(text_edit->get_text() == "test");
  196. CHECK(text_edit->get_line(0) == "test");
  197. CHECK(text_edit->get_line(1) == "");
  198. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  199. SIGNAL_CHECK("text_changed", empty_signal_args);
  200. SIGNAL_CHECK_FALSE("text_set");
  201. SIGNAL_CHECK_FALSE("caret_changed");
  202. // Setting to a longer line, caret and selections should be preserved.
  203. text_edit->select_all();
  204. MessageQueue::get_singleton()->flush();
  205. CHECK(text_edit->has_selection());
  206. SIGNAL_CHECK("caret_changed", empty_signal_args);
  207. text_edit->set_line(0, "test text");
  208. MessageQueue::get_singleton()->flush();
  209. CHECK(text_edit->get_line(0) == "test text");
  210. CHECK(text_edit->has_selection());
  211. CHECK(text_edit->get_selected_text() == "test");
  212. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  213. SIGNAL_CHECK("text_changed", empty_signal_args);
  214. SIGNAL_CHECK_FALSE("caret_changed");
  215. SIGNAL_CHECK_FALSE("text_set");
  216. // Setting to a shorter line, selection and caret should be adjusted. Also works if not editable.
  217. text_edit->set_editable(false);
  218. text_edit->set_line(0, "te");
  219. MessageQueue::get_singleton()->flush();
  220. CHECK(text_edit->get_line(0) == "te");
  221. CHECK(text_edit->has_selection());
  222. CHECK(text_edit->get_selected_text() == "te");
  223. CHECK(text_edit->get_caret_column() == 2);
  224. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  225. SIGNAL_CHECK("caret_changed", empty_signal_args);
  226. SIGNAL_CHECK("text_changed", empty_signal_args);
  227. SIGNAL_CHECK_FALSE("text_set");
  228. text_edit->set_editable(true);
  229. // Undo / redo should work.
  230. text_edit->undo();
  231. MessageQueue::get_singleton()->flush();
  232. CHECK(text_edit->get_line(0) == "test text");
  233. CHECK(text_edit->has_selection());
  234. CHECK(text_edit->get_selected_text() == "test");
  235. CHECK(text_edit->get_caret_column() == 4);
  236. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  237. SIGNAL_CHECK("caret_changed", empty_signal_args);
  238. SIGNAL_CHECK("text_changed", empty_signal_args);
  239. SIGNAL_CHECK_FALSE("text_set");
  240. text_edit->redo();
  241. MessageQueue::get_singleton()->flush();
  242. CHECK(text_edit->get_line(0) == "te");
  243. CHECK(text_edit->has_selection());
  244. CHECK(text_edit->get_caret_column() == 2);
  245. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  246. SIGNAL_CHECK("caret_changed", empty_signal_args);
  247. SIGNAL_CHECK("text_changed", empty_signal_args);
  248. SIGNAL_CHECK_FALSE("text_set");
  249. // Out of range.
  250. ERR_PRINT_OFF;
  251. text_edit->set_line(-1, "test");
  252. MessageQueue::get_singleton()->flush();
  253. CHECK(text_edit->get_line(0) == "te");
  254. SIGNAL_CHECK_FALSE("lines_edited_from");
  255. SIGNAL_CHECK_FALSE("caret_changed");
  256. SIGNAL_CHECK_FALSE("text_changed");
  257. SIGNAL_CHECK_FALSE("text_set");
  258. text_edit->set_line(1, "test");
  259. MessageQueue::get_singleton()->flush();
  260. CHECK(text_edit->get_line(0) == "te");
  261. SIGNAL_CHECK_FALSE("lines_edited_from");
  262. SIGNAL_CHECK_FALSE("caret_changed");
  263. SIGNAL_CHECK_FALSE("text_changed");
  264. SIGNAL_CHECK_FALSE("text_set");
  265. ERR_PRINT_ON;
  266. }
  267. SUBCASE("[TextEdit] swap lines") {
  268. ((Array)lines_edited_args[1])[1] = 1;
  269. text_edit->set_text("testing\nswap");
  270. MessageQueue::get_singleton()->flush();
  271. CHECK(text_edit->get_text() == "testing\nswap");
  272. SIGNAL_CHECK("text_set", empty_signal_args);
  273. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  274. SIGNAL_CHECK("caret_changed", empty_signal_args);
  275. SIGNAL_CHECK_FALSE("text_changed");
  276. text_edit->set_caret_column(text_edit->get_line(0).length());
  277. MessageQueue::get_singleton()->flush();
  278. SIGNAL_CHECK("caret_changed", empty_signal_args);
  279. ((Array)lines_edited_args[1])[1] = 0;
  280. Array swap_args;
  281. swap_args.push_back(1);
  282. swap_args.push_back(1);
  283. lines_edited_args.push_back(swap_args);
  284. lines_edited_args.push_back(swap_args);
  285. // Order does not matter. Should also work if not editable.
  286. text_edit->set_editable(false);
  287. text_edit->swap_lines(1, 0);
  288. MessageQueue::get_singleton()->flush();
  289. CHECK(text_edit->get_text() == "swap\ntesting");
  290. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  291. SIGNAL_CHECK("caret_changed", empty_signal_args);
  292. SIGNAL_CHECK("text_changed", empty_signal_args);
  293. SIGNAL_CHECK_FALSE("text_set");
  294. text_edit->set_editable(true);
  295. lines_edited_args.reverse();
  296. // Single undo/redo action
  297. text_edit->undo();
  298. MessageQueue::get_singleton()->flush();
  299. CHECK(text_edit->get_text() == "testing\nswap");
  300. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  301. SIGNAL_CHECK("caret_changed", empty_signal_args);
  302. SIGNAL_CHECK("text_changed", empty_signal_args);
  303. SIGNAL_CHECK_FALSE("text_set");
  304. lines_edited_args.reverse();
  305. text_edit->redo();
  306. MessageQueue::get_singleton()->flush();
  307. CHECK(text_edit->get_text() == "swap\ntesting");
  308. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  309. SIGNAL_CHECK("caret_changed", empty_signal_args);
  310. SIGNAL_CHECK("text_changed", empty_signal_args);
  311. SIGNAL_CHECK_FALSE("text_set");
  312. // Out of range.
  313. ERR_PRINT_OFF;
  314. text_edit->swap_lines(-1, 0);
  315. CHECK(text_edit->get_text() == "swap\ntesting");
  316. SIGNAL_CHECK_FALSE("lines_edited_from");
  317. SIGNAL_CHECK_FALSE("caret_changed");
  318. SIGNAL_CHECK_FALSE("text_changed");
  319. SIGNAL_CHECK_FALSE("text_set");
  320. text_edit->swap_lines(0, -1);
  321. CHECK(text_edit->get_text() == "swap\ntesting");
  322. SIGNAL_CHECK_FALSE("lines_edited_from");
  323. SIGNAL_CHECK_FALSE("caret_changed");
  324. SIGNAL_CHECK_FALSE("text_changed");
  325. SIGNAL_CHECK_FALSE("text_set");
  326. text_edit->swap_lines(2, 0);
  327. CHECK(text_edit->get_text() == "swap\ntesting");
  328. SIGNAL_CHECK_FALSE("lines_edited_from");
  329. SIGNAL_CHECK_FALSE("caret_changed");
  330. SIGNAL_CHECK_FALSE("text_changed");
  331. SIGNAL_CHECK_FALSE("text_set");
  332. text_edit->swap_lines(0, 2);
  333. CHECK(text_edit->get_text() == "swap\ntesting");
  334. SIGNAL_CHECK_FALSE("lines_edited_from");
  335. SIGNAL_CHECK_FALSE("caret_changed");
  336. SIGNAL_CHECK_FALSE("text_changed");
  337. SIGNAL_CHECK_FALSE("text_set");
  338. ERR_PRINT_ON;
  339. }
  340. SUBCASE("[TextEdit] insert line at") {
  341. ((Array)lines_edited_args[1])[1] = 1;
  342. text_edit->set_text("testing\nswap");
  343. MessageQueue::get_singleton()->flush();
  344. CHECK(text_edit->get_text() == "testing\nswap");
  345. SIGNAL_CHECK("text_set", empty_signal_args);
  346. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  347. SIGNAL_CHECK("caret_changed", empty_signal_args);
  348. SIGNAL_CHECK_FALSE("text_changed");
  349. text_edit->select_all();
  350. MessageQueue::get_singleton()->flush();
  351. CHECK(text_edit->has_selection());
  352. CHECK(text_edit->get_selection_from_line() == 0);
  353. CHECK(text_edit->get_selection_to_line() == 1);
  354. SIGNAL_CHECK("caret_changed", empty_signal_args);
  355. // insert before should move caret and selecion, and works when not editable.
  356. text_edit->set_editable(false);
  357. lines_edited_args.remove_at(0);
  358. text_edit->insert_line_at(0, "new");
  359. MessageQueue::get_singleton()->flush();
  360. CHECK(text_edit->get_text() == "new\ntesting\nswap");
  361. CHECK(text_edit->get_caret_line() == 2);
  362. CHECK(text_edit->get_caret_column() == text_edit->get_line(2).size() - 1);
  363. CHECK(text_edit->has_selection());
  364. CHECK(text_edit->get_selection_from_line() == 1);
  365. CHECK(text_edit->get_selection_to_line() == 2);
  366. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  367. SIGNAL_CHECK("caret_changed", empty_signal_args);
  368. SIGNAL_CHECK("text_changed", empty_signal_args);
  369. SIGNAL_CHECK_FALSE("text_set");
  370. text_edit->set_editable(true);
  371. // can undo/redo as single action
  372. ((Array)lines_edited_args[0])[0] = 1;
  373. ((Array)lines_edited_args[0])[1] = 0;
  374. text_edit->undo();
  375. MessageQueue::get_singleton()->flush();
  376. CHECK(text_edit->get_text() == "testing\nswap");
  377. CHECK(text_edit->has_selection());
  378. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  379. SIGNAL_CHECK("caret_changed", empty_signal_args);
  380. SIGNAL_CHECK("text_changed", empty_signal_args);
  381. SIGNAL_CHECK_FALSE("text_set");
  382. ((Array)lines_edited_args[0])[0] = 0;
  383. ((Array)lines_edited_args[0])[1] = 1;
  384. text_edit->redo();
  385. MessageQueue::get_singleton()->flush();
  386. CHECK(text_edit->get_text() == "new\ntesting\nswap");
  387. CHECK(text_edit->has_selection());
  388. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  389. SIGNAL_CHECK("caret_changed", empty_signal_args);
  390. SIGNAL_CHECK("text_changed", empty_signal_args);
  391. SIGNAL_CHECK_FALSE("text_set");
  392. // Adding inside selection extends selection.
  393. text_edit->select_all();
  394. MessageQueue::get_singleton()->flush();
  395. CHECK(text_edit->has_selection());
  396. CHECK(text_edit->get_selection_from_line() == 0);
  397. CHECK(text_edit->get_selection_to_line() == 2);
  398. SIGNAL_CHECK_FALSE("caret_changed");
  399. ((Array)lines_edited_args[0])[0] = 2;
  400. ((Array)lines_edited_args[0])[1] = 3;
  401. text_edit->insert_line_at(2, "after");
  402. MessageQueue::get_singleton()->flush();
  403. CHECK(text_edit->get_text() == "new\ntesting\nafter\nswap");
  404. CHECK(text_edit->get_caret_line() == 3);
  405. CHECK(text_edit->get_caret_column() == text_edit->get_line(3).size() - 1);
  406. CHECK(text_edit->has_selection());
  407. CHECK(text_edit->get_selection_from_line() == 0);
  408. CHECK(text_edit->get_selection_to_line() == 3);
  409. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  410. SIGNAL_CHECK("caret_changed", empty_signal_args);
  411. SIGNAL_CHECK("text_changed", empty_signal_args);
  412. SIGNAL_CHECK_FALSE("text_set");
  413. // Out of range.
  414. ERR_PRINT_OFF;
  415. text_edit->insert_line_at(-1, "after");
  416. CHECK(text_edit->get_text() == "new\ntesting\nafter\nswap");
  417. SIGNAL_CHECK_FALSE("lines_edited_from");
  418. SIGNAL_CHECK_FALSE("caret_changed");
  419. SIGNAL_CHECK_FALSE("text_changed");
  420. SIGNAL_CHECK_FALSE("text_set");
  421. text_edit->insert_line_at(4, "after");
  422. CHECK(text_edit->get_text() == "new\ntesting\nafter\nswap");
  423. SIGNAL_CHECK_FALSE("lines_edited_from");
  424. SIGNAL_CHECK_FALSE("caret_changed");
  425. SIGNAL_CHECK_FALSE("text_changed");
  426. SIGNAL_CHECK_FALSE("text_set");
  427. ERR_PRINT_ON;
  428. }
  429. SUBCASE("[TextEdit] insert line at caret") {
  430. lines_edited_args.pop_back();
  431. ((Array)lines_edited_args[0])[1] = 1;
  432. text_edit->insert_text_at_caret("testing\nswap");
  433. MessageQueue::get_singleton()->flush();
  434. CHECK(text_edit->get_text() == "testing\nswap");
  435. CHECK(text_edit->get_caret_line() == 1);
  436. CHECK(text_edit->get_caret_column() == text_edit->get_line(1).size() - 1);
  437. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  438. SIGNAL_CHECK("text_changed", empty_signal_args);
  439. SIGNAL_CHECK("caret_changed", empty_signal_args);
  440. SIGNAL_CHECK_FALSE("text_set");
  441. text_edit->set_caret_line(0, false);
  442. text_edit->set_caret_column(2);
  443. SIGNAL_DISCARD("caret_changed");
  444. ((Array)lines_edited_args[0])[1] = 0;
  445. text_edit->insert_text_at_caret("mid");
  446. MessageQueue::get_singleton()->flush();
  447. CHECK(text_edit->get_text() == "temidsting\nswap");
  448. CHECK(text_edit->get_caret_line() == 0);
  449. CHECK(text_edit->get_caret_column() == 5);
  450. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  451. SIGNAL_CHECK("text_changed", empty_signal_args);
  452. SIGNAL_CHECK("caret_changed", empty_signal_args);
  453. SIGNAL_CHECK_FALSE("text_set");
  454. text_edit->select(0, 0, 0, text_edit->get_line(0).length());
  455. CHECK(text_edit->has_selection());
  456. lines_edited_args.push_back(args1.duplicate());
  457. text_edit->set_editable(false);
  458. text_edit->insert_text_at_caret("new line");
  459. MessageQueue::get_singleton()->flush();
  460. CHECK(text_edit->get_text() == "new line\nswap");
  461. CHECK(text_edit->get_caret_line() == 0);
  462. CHECK(text_edit->get_caret_column() == text_edit->get_line(0).size() - 1);
  463. CHECK_FALSE(text_edit->has_selection());
  464. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  465. SIGNAL_CHECK("text_changed", empty_signal_args);
  466. SIGNAL_CHECK("caret_changed", empty_signal_args);
  467. SIGNAL_CHECK_FALSE("text_set");
  468. text_edit->set_editable(true);
  469. text_edit->undo();
  470. MessageQueue::get_singleton()->flush();
  471. CHECK(text_edit->get_text() == "temidsting\nswap");
  472. CHECK(text_edit->get_caret_line() == 0);
  473. CHECK(text_edit->get_caret_column() == 5);
  474. CHECK(text_edit->has_selection());
  475. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  476. SIGNAL_CHECK("text_changed", empty_signal_args);
  477. SIGNAL_CHECK("caret_changed", empty_signal_args);
  478. SIGNAL_CHECK_FALSE("text_set");
  479. text_edit->redo();
  480. MessageQueue::get_singleton()->flush();
  481. CHECK(text_edit->get_text() == "new line\nswap");
  482. CHECK(text_edit->get_caret_line() == 0);
  483. CHECK(text_edit->get_caret_column() == 8);
  484. CHECK_FALSE(text_edit->has_selection());
  485. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  486. SIGNAL_CHECK("text_changed", empty_signal_args);
  487. SIGNAL_CHECK("caret_changed", empty_signal_args);
  488. SIGNAL_CHECK_FALSE("text_set");
  489. }
  490. SIGNAL_UNWATCH(text_edit, "text_set");
  491. SIGNAL_UNWATCH(text_edit, "text_changed");
  492. SIGNAL_UNWATCH(text_edit, "lines_edited_from");
  493. SIGNAL_UNWATCH(text_edit, "caret_changed");
  494. }
  495. SUBCASE("[TextEdit] indent level") {
  496. CHECK(text_edit->get_indent_level(0) == 0);
  497. CHECK(text_edit->get_first_non_whitespace_column(0) == 0);
  498. text_edit->set_line(0, "a");
  499. CHECK(text_edit->get_indent_level(0) == 0);
  500. CHECK(text_edit->get_first_non_whitespace_column(0) == 0);
  501. text_edit->set_line(0, "\t");
  502. CHECK(text_edit->get_indent_level(0) == 4);
  503. CHECK(text_edit->get_first_non_whitespace_column(0) == 1);
  504. text_edit->set_tab_size(8);
  505. CHECK(text_edit->get_indent_level(0) == 8);
  506. text_edit->set_line(0, "\t a");
  507. CHECK(text_edit->get_first_non_whitespace_column(0) == 2);
  508. CHECK(text_edit->get_indent_level(0) == 9);
  509. }
  510. SUBCASE("[TextEdit] selection") {
  511. SIGNAL_WATCH(text_edit, "text_set");
  512. SIGNAL_WATCH(text_edit, "text_changed");
  513. SIGNAL_WATCH(text_edit, "lines_edited_from");
  514. SIGNAL_WATCH(text_edit, "caret_changed");
  515. Array args1;
  516. args1.push_back(0);
  517. args1.push_back(0);
  518. Array lines_edited_args;
  519. lines_edited_args.push_back(args1);
  520. lines_edited_args.push_back(args1.duplicate());
  521. SUBCASE("[TextEdit] select all") {
  522. text_edit->select_all();
  523. CHECK_FALSE(text_edit->has_selection());
  524. ERR_PRINT_OFF;
  525. CHECK(text_edit->get_selection_from_line() == -1);
  526. CHECK(text_edit->get_selection_from_column() == -1);
  527. CHECK(text_edit->get_selection_to_line() == -1);
  528. CHECK(text_edit->get_selection_to_column() == -1);
  529. CHECK(text_edit->get_selected_text() == "");
  530. ERR_PRINT_ON;
  531. text_edit->set_text("test\nselection");
  532. SEND_GUI_ACTION("ui_text_select_all");
  533. CHECK(text_edit->get_viewport()->is_input_handled());
  534. MessageQueue::get_singleton()->flush();
  535. CHECK(text_edit->get_selected_text() == "test\nselection");
  536. CHECK(text_edit->has_selection());
  537. CHECK(text_edit->get_selection_from_line() == 0);
  538. CHECK(text_edit->get_selection_from_column() == 0);
  539. CHECK(text_edit->get_selection_to_line() == 1);
  540. CHECK(text_edit->get_selection_to_column() == 9);
  541. CHECK(text_edit->get_selection_mode() == TextEdit::SelectionMode::SELECTION_MODE_SHIFT);
  542. CHECK(text_edit->get_caret_line() == 1);
  543. CHECK(text_edit->get_caret_column() == 9);
  544. SIGNAL_CHECK("caret_changed", empty_signal_args);
  545. text_edit->set_caret_line(0);
  546. text_edit->set_caret_column(0);
  547. text_edit->set_selecting_enabled(false);
  548. CHECK_FALSE(text_edit->has_selection());
  549. CHECK(text_edit->get_selected_text() == "");
  550. CHECK(text_edit->get_caret_line() == 0);
  551. CHECK(text_edit->get_caret_column() == 0);
  552. text_edit->select_all();
  553. CHECK_FALSE(text_edit->has_selection());
  554. CHECK(text_edit->get_selected_text() == "");
  555. CHECK(text_edit->get_caret_line() == 0);
  556. CHECK(text_edit->get_caret_column() == 0);
  557. }
  558. SUBCASE("[TextEdit] select word under caret") {
  559. text_edit->set_text("\ntest test\ntest test");
  560. text_edit->set_caret_column(0);
  561. text_edit->set_caret_line(1);
  562. text_edit->add_caret(2, 0);
  563. text_edit->add_caret(2, 2);
  564. CHECK(text_edit->get_caret_count() == 3);
  565. MessageQueue::get_singleton()->flush();
  566. SIGNAL_DISCARD("text_set");
  567. SIGNAL_DISCARD("text_changed");
  568. SIGNAL_DISCARD("lines_edited_from");
  569. SIGNAL_DISCARD("caret_changed");
  570. text_edit->select_word_under_caret();
  571. CHECK(text_edit->has_selection(0));
  572. CHECK(text_edit->get_selected_text(0) == "test");
  573. CHECK(text_edit->get_selection_from_line(0) == 1);
  574. CHECK(text_edit->get_selection_from_column(0) == 0);
  575. CHECK(text_edit->get_selection_to_line(0) == 1);
  576. CHECK(text_edit->get_selection_to_column(0) == 4);
  577. CHECK(text_edit->get_caret_line(0) == 1);
  578. CHECK(text_edit->get_caret_column(0) == 4);
  579. CHECK(text_edit->has_selection(1));
  580. CHECK(text_edit->get_selected_text(1) == "test");
  581. CHECK(text_edit->get_selection_from_line(1) == 2);
  582. CHECK(text_edit->get_selection_from_column(1) == 0);
  583. CHECK(text_edit->get_selection_to_line(1) == 2);
  584. CHECK(text_edit->get_selection_to_column(1) == 4);
  585. CHECK(text_edit->get_caret_line(1) == 2);
  586. CHECK(text_edit->get_caret_column(1) == 4);
  587. CHECK(text_edit->get_caret_count() == 2);
  588. text_edit->select_word_under_caret();
  589. CHECK_FALSE(text_edit->has_selection());
  590. CHECK(text_edit->get_selected_text() == "");
  591. SEND_GUI_ACTION("ui_text_select_word_under_caret");
  592. CHECK(text_edit->get_viewport()->is_input_handled());
  593. MessageQueue::get_singleton()->flush();
  594. CHECK(text_edit->has_selection(0));
  595. CHECK(text_edit->get_selected_text(0) == "test");
  596. CHECK(text_edit->get_selection_from_line(0) == 1);
  597. CHECK(text_edit->get_selection_from_column(0) == 0);
  598. CHECK(text_edit->get_selection_to_line(0) == 1);
  599. CHECK(text_edit->get_selection_to_column(0) == 4);
  600. CHECK(text_edit->get_caret_line(0) == 1);
  601. CHECK(text_edit->get_caret_column(0) == 4);
  602. CHECK(text_edit->has_selection(1));
  603. CHECK(text_edit->get_selected_text(1) == "test");
  604. CHECK(text_edit->get_selection_from_line(1) == 2);
  605. CHECK(text_edit->get_selection_from_column(1) == 0);
  606. CHECK(text_edit->get_selection_to_line(1) == 2);
  607. CHECK(text_edit->get_selection_to_column(1) == 4);
  608. CHECK(text_edit->get_caret_line(1) == 2);
  609. CHECK(text_edit->get_caret_column(1) == 4);
  610. CHECK(text_edit->get_selected_text() == "test\ntest");
  611. SIGNAL_CHECK("caret_changed", empty_signal_args);
  612. text_edit->set_selecting_enabled(false);
  613. text_edit->select_word_under_caret();
  614. CHECK_FALSE(text_edit->has_selection());
  615. CHECK(text_edit->get_selected_text() == "");
  616. CHECK(text_edit->get_caret_line(0) == 1);
  617. CHECK(text_edit->get_caret_column(0) == 4);
  618. CHECK(text_edit->get_caret_line(1) == 2);
  619. CHECK(text_edit->get_caret_column(1) == 4);
  620. SIGNAL_CHECK_FALSE("caret_changed");
  621. text_edit->set_selecting_enabled(true);
  622. text_edit->set_caret_line(1, false, true, 0, 0);
  623. text_edit->set_caret_column(5, false, 0);
  624. text_edit->set_caret_line(2, false, true, 0, 1);
  625. text_edit->set_caret_column(5, false, 1);
  626. text_edit->select_word_under_caret();
  627. CHECK_FALSE(text_edit->has_selection());
  628. CHECK(text_edit->get_selected_text() == "");
  629. text_edit->select_word_under_caret();
  630. CHECK_FALSE(text_edit->has_selection());
  631. CHECK(text_edit->get_selected_text() == "");
  632. CHECK(text_edit->get_caret_line(0) == 1);
  633. CHECK(text_edit->get_caret_column(0) == 5);
  634. CHECK(text_edit->get_caret_line(1) == 2);
  635. CHECK(text_edit->get_caret_column(1) == 5);
  636. SIGNAL_CHECK_FALSE("caret_changed");
  637. }
  638. SUBCASE("[TextEdit] add selection for next occurrence") {
  639. text_edit->set_text("\ntest other_test\nrandom test\nword test word nonrandom");
  640. text_edit->set_caret_column(0);
  641. text_edit->set_caret_line(1);
  642. // First selection made by the implicit select_word_under_caret call
  643. text_edit->add_selection_for_next_occurrence();
  644. CHECK(text_edit->get_caret_count() == 1);
  645. CHECK(text_edit->get_selected_text(0) == "test");
  646. CHECK(text_edit->get_selection_from_line(0) == 1);
  647. CHECK(text_edit->get_selection_from_column(0) == 0);
  648. CHECK(text_edit->get_selection_to_line(0) == 1);
  649. CHECK(text_edit->get_selection_to_column(0) == 4);
  650. CHECK(text_edit->get_caret_line(0) == 1);
  651. CHECK(text_edit->get_caret_column(0) == 4);
  652. text_edit->add_selection_for_next_occurrence();
  653. CHECK(text_edit->get_caret_count() == 2);
  654. CHECK(text_edit->get_selected_text(1) == "test");
  655. CHECK(text_edit->get_selection_from_line(1) == 1);
  656. CHECK(text_edit->get_selection_from_column(1) == 13);
  657. CHECK(text_edit->get_selection_to_line(1) == 1);
  658. CHECK(text_edit->get_selection_to_column(1) == 17);
  659. CHECK(text_edit->get_caret_line(1) == 1);
  660. CHECK(text_edit->get_caret_column(1) == 17);
  661. text_edit->add_selection_for_next_occurrence();
  662. CHECK(text_edit->get_caret_count() == 3);
  663. CHECK(text_edit->get_selected_text(2) == "test");
  664. CHECK(text_edit->get_selection_from_line(2) == 2);
  665. CHECK(text_edit->get_selection_from_column(2) == 9);
  666. CHECK(text_edit->get_selection_to_line(2) == 2);
  667. CHECK(text_edit->get_selection_to_column(2) == 13);
  668. CHECK(text_edit->get_caret_line(2) == 2);
  669. CHECK(text_edit->get_caret_column(2) == 13);
  670. text_edit->add_selection_for_next_occurrence();
  671. CHECK(text_edit->get_caret_count() == 4);
  672. CHECK(text_edit->get_selected_text(3) == "test");
  673. CHECK(text_edit->get_selection_from_line(3) == 3);
  674. CHECK(text_edit->get_selection_from_column(3) == 5);
  675. CHECK(text_edit->get_selection_to_line(3) == 3);
  676. CHECK(text_edit->get_selection_to_column(3) == 9);
  677. CHECK(text_edit->get_caret_line(3) == 3);
  678. CHECK(text_edit->get_caret_column(3) == 9);
  679. // A different word with a new manually added caret
  680. text_edit->add_caret(2, 1);
  681. text_edit->select(2, 0, 2, 4, 4);
  682. CHECK(text_edit->get_selected_text(4) == "rand");
  683. text_edit->add_selection_for_next_occurrence();
  684. CHECK(text_edit->get_caret_count() == 6);
  685. CHECK(text_edit->get_selected_text(5) == "rand");
  686. CHECK(text_edit->get_selection_from_line(5) == 3);
  687. CHECK(text_edit->get_selection_from_column(5) == 18);
  688. CHECK(text_edit->get_selection_to_line(5) == 3);
  689. CHECK(text_edit->get_selection_to_column(5) == 22);
  690. CHECK(text_edit->get_caret_line(5) == 3);
  691. CHECK(text_edit->get_caret_column(5) == 22);
  692. // Make sure the previous selections are still active
  693. CHECK(text_edit->get_selected_text(0) == "test");
  694. CHECK(text_edit->get_selected_text(1) == "test");
  695. CHECK(text_edit->get_selected_text(2) == "test");
  696. CHECK(text_edit->get_selected_text(3) == "test");
  697. }
  698. SUBCASE("[TextEdit] deselect on focus loss") {
  699. text_edit->set_text("test");
  700. text_edit->set_deselect_on_focus_loss_enabled(true);
  701. CHECK(text_edit->is_deselect_on_focus_loss_enabled());
  702. text_edit->grab_focus();
  703. text_edit->select_all();
  704. CHECK(text_edit->has_focus());
  705. CHECK(text_edit->has_selection());
  706. text_edit->release_focus();
  707. CHECK_FALSE(text_edit->has_focus());
  708. CHECK_FALSE(text_edit->has_selection());
  709. text_edit->set_deselect_on_focus_loss_enabled(false);
  710. CHECK_FALSE(text_edit->is_deselect_on_focus_loss_enabled());
  711. text_edit->grab_focus();
  712. text_edit->select_all();
  713. CHECK(text_edit->has_focus());
  714. CHECK(text_edit->has_selection());
  715. text_edit->release_focus();
  716. CHECK_FALSE(text_edit->has_focus());
  717. CHECK(text_edit->has_selection());
  718. text_edit->set_deselect_on_focus_loss_enabled(true);
  719. CHECK_FALSE(text_edit->has_selection());
  720. }
  721. SUBCASE("[TextEdit] key select") {
  722. text_edit->set_text("test");
  723. text_edit->grab_focus();
  724. SEND_GUI_KEY_EVENT(Key::RIGHT | KeyModifierMask::SHIFT)
  725. CHECK(text_edit->has_selection());
  726. CHECK(text_edit->get_selected_text() == "t");
  727. #ifdef MACOS_ENABLED
  728. SEND_GUI_KEY_EVENT(Key::RIGHT | KeyModifierMask::SHIFT | KeyModifierMask::ALT)
  729. #else
  730. SEND_GUI_KEY_EVENT(Key::RIGHT | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL)
  731. #endif
  732. CHECK(text_edit->has_selection());
  733. CHECK(text_edit->get_selected_text() == "test");
  734. SEND_GUI_KEY_EVENT(Key::LEFT | KeyModifierMask::SHIFT)
  735. CHECK(text_edit->has_selection());
  736. CHECK(text_edit->get_selected_text() == "tes");
  737. #ifdef MACOS_ENABLED
  738. SEND_GUI_KEY_EVENT(Key::LEFT | KeyModifierMask::SHIFT | KeyModifierMask::ALT)
  739. #else
  740. SEND_GUI_KEY_EVENT(Key::LEFT | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL)
  741. #endif
  742. CHECK_FALSE(text_edit->has_selection());
  743. CHECK(text_edit->get_selected_text() == "");
  744. SEND_GUI_KEY_EVENT(Key::RIGHT | KeyModifierMask::SHIFT)
  745. CHECK(text_edit->has_selection());
  746. CHECK(text_edit->get_selected_text() == "t");
  747. SEND_GUI_KEY_EVENT(Key::RIGHT)
  748. CHECK_FALSE(text_edit->has_selection());
  749. CHECK(text_edit->get_selected_text() == "");
  750. SEND_GUI_KEY_EVENT(Key::LEFT | KeyModifierMask::SHIFT)
  751. CHECK(text_edit->has_selection());
  752. CHECK(text_edit->get_selected_text() == "t");
  753. SEND_GUI_KEY_EVENT(Key::LEFT)
  754. CHECK_FALSE(text_edit->has_selection());
  755. CHECK(text_edit->get_selected_text() == "");
  756. text_edit->set_selecting_enabled(false);
  757. SEND_GUI_KEY_EVENT(Key::RIGHT | KeyModifierMask::SHIFT)
  758. CHECK_FALSE(text_edit->has_selection());
  759. CHECK(text_edit->get_selected_text() == "");
  760. text_edit->set_selecting_enabled(true);
  761. }
  762. SUBCASE("[TextEdit] mouse drag select") {
  763. /* Set size for mouse input. */
  764. text_edit->set_size(Size2(200, 200));
  765. text_edit->set_text("this is some text\nfor selection");
  766. text_edit->grab_focus();
  767. MessageQueue::get_singleton()->flush();
  768. SEND_GUI_MOUSE_BUTTON_EVENT(text_edit->get_pos_at_line_column(0, 1), MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  769. SEND_GUI_MOUSE_MOTION_EVENT(text_edit->get_pos_at_line_column(0, 7), MouseButtonMask::LEFT, Key::NONE);
  770. CHECK(text_edit->has_selection());
  771. CHECK(text_edit->get_selected_text() == "for s");
  772. CHECK(text_edit->get_selection_mode() == TextEdit::SELECTION_MODE_POINTER);
  773. CHECK(text_edit->get_selection_from_line() == 1);
  774. CHECK(text_edit->get_selection_from_column() == 0);
  775. CHECK(text_edit->get_selection_to_line() == 1);
  776. CHECK(text_edit->get_selection_to_column() == 5);
  777. CHECK(text_edit->get_caret_line() == 1);
  778. CHECK(text_edit->get_caret_column() == 5);
  779. SEND_GUI_MOUSE_BUTTON_EVENT(text_edit->get_pos_at_line_column(0, 9), MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  780. CHECK_FALSE(text_edit->has_selection());
  781. text_edit->set_selecting_enabled(false);
  782. SEND_GUI_MOUSE_BUTTON_EVENT(text_edit->get_pos_at_line_column(0, 1), MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  783. SEND_GUI_MOUSE_MOTION_EVENT(text_edit->get_pos_at_line_column(0, 7), MouseButtonMask::LEFT, Key::NONE);
  784. CHECK_FALSE(text_edit->has_selection());
  785. CHECK(text_edit->get_caret_line() == 1);
  786. CHECK(text_edit->get_caret_column() == 5);
  787. text_edit->set_selecting_enabled(true);
  788. }
  789. SUBCASE("[TextEdit] mouse word select") {
  790. /* Set size for mouse input. */
  791. text_edit->set_size(Size2(200, 200));
  792. text_edit->set_text("this is some text\nfor selection");
  793. MessageQueue::get_singleton()->flush();
  794. SIGNAL_DISCARD("caret_changed");
  795. SEND_GUI_DOUBLE_CLICK(text_edit->get_pos_at_line_column(0, 2), Key::NONE);
  796. CHECK(text_edit->has_selection());
  797. CHECK(text_edit->get_selected_text() == "for");
  798. CHECK(text_edit->get_selection_mode() == TextEdit::SELECTION_MODE_WORD);
  799. CHECK(text_edit->get_selection_from_line() == 1);
  800. CHECK(text_edit->get_selection_from_column() == 0);
  801. CHECK(text_edit->get_selection_to_line() == 1);
  802. CHECK(text_edit->get_selection_to_column() == 3);
  803. CHECK(text_edit->get_caret_line() == 1);
  804. CHECK(text_edit->get_caret_column() == 3);
  805. SIGNAL_CHECK("caret_changed", empty_signal_args);
  806. SEND_GUI_MOUSE_MOTION_EVENT(text_edit->get_pos_at_line_column(0, 7), MouseButtonMask::LEFT, Key::NONE);
  807. CHECK(text_edit->has_selection());
  808. CHECK(text_edit->get_selected_text() == "for selection");
  809. CHECK(text_edit->get_selection_mode() == TextEdit::SELECTION_MODE_WORD);
  810. CHECK(text_edit->get_selection_from_line() == 1);
  811. CHECK(text_edit->get_selection_from_column() == 0);
  812. CHECK(text_edit->get_selection_to_line() == 1);
  813. CHECK(text_edit->get_selection_to_column() == 13);
  814. CHECK(text_edit->get_caret_line() == 1);
  815. CHECK(text_edit->get_caret_column() == 13);
  816. SIGNAL_CHECK("caret_changed", empty_signal_args);
  817. Point2i line_0 = text_edit->get_pos_at_line_column(0, 0);
  818. line_0.y /= 2;
  819. SEND_GUI_MOUSE_BUTTON_EVENT(line_0, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  820. CHECK_FALSE(text_edit->has_selection());
  821. text_edit->set_selecting_enabled(false);
  822. SEND_GUI_DOUBLE_CLICK(text_edit->get_pos_at_line_column(0, 2), Key::NONE);
  823. CHECK_FALSE(text_edit->has_selection());
  824. CHECK(text_edit->get_caret_line() == 1);
  825. CHECK(text_edit->get_caret_column() == 3);
  826. text_edit->set_selecting_enabled(true);
  827. }
  828. SUBCASE("[TextEdit] mouse line select") {
  829. /* Set size for mouse input. */
  830. text_edit->set_size(Size2(200, 200));
  831. text_edit->set_text("this is some text\nfor selection");
  832. MessageQueue::get_singleton()->flush();
  833. SEND_GUI_DOUBLE_CLICK(text_edit->get_pos_at_line_column(0, 2), Key::NONE);
  834. SEND_GUI_MOUSE_BUTTON_EVENT(text_edit->get_pos_at_line_column(0, 2), MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  835. CHECK(text_edit->has_selection());
  836. CHECK(text_edit->get_selected_text() == "for selection");
  837. CHECK(text_edit->get_selection_mode() == TextEdit::SELECTION_MODE_LINE);
  838. CHECK(text_edit->get_selection_from_line() == 1);
  839. CHECK(text_edit->get_selection_from_column() == 0);
  840. CHECK(text_edit->get_selection_to_line() == 1);
  841. CHECK(text_edit->get_selection_to_column() == 13);
  842. CHECK(text_edit->get_caret_line() == 1);
  843. CHECK(text_edit->get_caret_column() == 0);
  844. Point2i line_0 = text_edit->get_pos_at_line_column(0, 0);
  845. line_0.y /= 2;
  846. SEND_GUI_MOUSE_BUTTON_EVENT(line_0, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  847. CHECK_FALSE(text_edit->has_selection());
  848. text_edit->set_selecting_enabled(false);
  849. SEND_GUI_DOUBLE_CLICK(text_edit->get_pos_at_line_column(0, 2), Key::NONE);
  850. SEND_GUI_MOUSE_BUTTON_EVENT(text_edit->get_pos_at_line_column(0, 2), MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  851. CHECK_FALSE(text_edit->has_selection());
  852. CHECK(text_edit->get_caret_line() == 1);
  853. CHECK(text_edit->get_caret_column() == 0);
  854. text_edit->set_selecting_enabled(true);
  855. }
  856. SUBCASE("[TextEdit] mouse shift click select") {
  857. /* Set size for mouse input. */
  858. text_edit->set_size(Size2(200, 200));
  859. text_edit->set_text("this is some text\nfor selection");
  860. MessageQueue::get_singleton()->flush();
  861. SEND_GUI_MOUSE_BUTTON_EVENT(text_edit->get_pos_at_line_column(0, 0), MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  862. SEND_GUI_MOUSE_BUTTON_EVENT(text_edit->get_pos_at_line_column(0, 7), MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE | KeyModifierMask::SHIFT);
  863. CHECK(text_edit->has_selection());
  864. CHECK(text_edit->get_selected_text() == "for s");
  865. CHECK(text_edit->get_selection_mode() == TextEdit::SELECTION_MODE_POINTER);
  866. CHECK(text_edit->get_selection_from_line() == 1);
  867. CHECK(text_edit->get_selection_from_column() == 0);
  868. CHECK(text_edit->get_selection_to_line() == 1);
  869. CHECK(text_edit->get_selection_to_column() == 5);
  870. CHECK(text_edit->get_caret_line() == 1);
  871. CHECK(text_edit->get_caret_column() == 5);
  872. SEND_GUI_MOUSE_BUTTON_EVENT(text_edit->get_pos_at_line_column(0, 9), MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  873. CHECK_FALSE(text_edit->has_selection());
  874. text_edit->set_selecting_enabled(false);
  875. SEND_GUI_MOUSE_BUTTON_EVENT(text_edit->get_pos_at_line_column(0, 0), MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  876. SEND_GUI_MOUSE_BUTTON_EVENT(text_edit->get_pos_at_line_column(0, 7), MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE | KeyModifierMask::SHIFT);
  877. CHECK_FALSE(text_edit->has_selection());
  878. CHECK(text_edit->get_caret_line() == 1);
  879. CHECK(text_edit->get_caret_column() == 5);
  880. text_edit->set_selecting_enabled(true);
  881. }
  882. SUBCASE("[TextEdit] select and deselect") {
  883. text_edit->set_text("this is some text\nfor selection");
  884. MessageQueue::get_singleton()->flush();
  885. text_edit->select(-1, -1, 500, 500);
  886. CHECK(text_edit->has_selection());
  887. CHECK(text_edit->get_selected_text() == "this is some text\nfor selection");
  888. text_edit->deselect();
  889. CHECK_FALSE(text_edit->has_selection());
  890. text_edit->select(500, 500, -1, -1);
  891. CHECK(text_edit->has_selection());
  892. CHECK(text_edit->get_selected_text() == "this is some text\nfor selection");
  893. text_edit->deselect();
  894. CHECK_FALSE(text_edit->has_selection());
  895. text_edit->select(0, 4, 0, 8);
  896. CHECK(text_edit->has_selection());
  897. CHECK(text_edit->get_selected_text() == " is ");
  898. text_edit->deselect();
  899. CHECK_FALSE(text_edit->has_selection());
  900. text_edit->select(0, 8, 0, 4);
  901. CHECK(text_edit->has_selection());
  902. CHECK(text_edit->get_selected_text() == " is ");
  903. text_edit->set_selecting_enabled(false);
  904. CHECK_FALSE(text_edit->has_selection());
  905. text_edit->select(0, 8, 0, 4);
  906. CHECK_FALSE(text_edit->has_selection());
  907. text_edit->set_selecting_enabled(true);
  908. text_edit->select(0, 8, 0, 4);
  909. CHECK(text_edit->has_selection());
  910. SEND_GUI_ACTION("ui_text_caret_right");
  911. CHECK_FALSE(text_edit->has_selection());
  912. text_edit->delete_selection();
  913. CHECK(text_edit->get_text() == "this is some text\nfor selection");
  914. CHECK(text_edit->get_caret_line() == 0);
  915. CHECK(text_edit->get_caret_column() == 8);
  916. text_edit->select(0, 8, 0, 4);
  917. CHECK(text_edit->has_selection());
  918. SEND_GUI_ACTION("ui_text_backspace");
  919. CHECK(text_edit->get_text() == "thissome text\nfor selection");
  920. CHECK(text_edit->get_caret_line() == 0);
  921. CHECK(text_edit->get_caret_column() == 4);
  922. text_edit->undo();
  923. CHECK(text_edit->has_selection());
  924. CHECK(text_edit->get_text() == "this is some text\nfor selection");
  925. CHECK(text_edit->get_caret_line() == 0);
  926. CHECK(text_edit->get_caret_column() == 8);
  927. text_edit->redo();
  928. CHECK_FALSE(text_edit->has_selection());
  929. CHECK(text_edit->get_text() == "thissome text\nfor selection");
  930. CHECK(text_edit->get_caret_line() == 0);
  931. CHECK(text_edit->get_caret_column() == 4);
  932. text_edit->undo();
  933. CHECK(text_edit->has_selection());
  934. CHECK(text_edit->get_text() == "this is some text\nfor selection");
  935. CHECK(text_edit->get_caret_line() == 0);
  936. CHECK(text_edit->get_caret_column() == 8);
  937. text_edit->select(0, 8, 0, 4);
  938. CHECK(text_edit->has_selection());
  939. text_edit->delete_selection();
  940. CHECK_FALSE(text_edit->has_selection());
  941. CHECK(text_edit->get_text() == "thissome text\nfor selection");
  942. text_edit->undo();
  943. CHECK(text_edit->has_selection());
  944. CHECK(text_edit->get_text() == "this is some text\nfor selection");
  945. CHECK(text_edit->get_caret_line() == 0);
  946. CHECK(text_edit->get_caret_column() == 8);
  947. text_edit->redo();
  948. CHECK_FALSE(text_edit->has_selection());
  949. CHECK(text_edit->get_text() == "thissome text\nfor selection");
  950. CHECK(text_edit->get_caret_line() == 0);
  951. CHECK(text_edit->get_caret_column() == 4);
  952. text_edit->undo();
  953. CHECK(text_edit->has_selection());
  954. CHECK(text_edit->get_text() == "this is some text\nfor selection");
  955. CHECK(text_edit->get_caret_line() == 0);
  956. CHECK(text_edit->get_caret_column() == 8);
  957. text_edit->set_editable(false);
  958. text_edit->delete_selection();
  959. text_edit->set_editable(false);
  960. CHECK_FALSE(text_edit->has_selection());
  961. CHECK(text_edit->get_text() == "thissome text\nfor selection");
  962. text_edit->undo();
  963. CHECK_FALSE(text_edit->has_selection());
  964. CHECK(text_edit->get_text() == "thissome text\nfor selection");
  965. }
  966. // Add readonly test?
  967. SUBCASE("[TextEdit] text drag") {
  968. TextEdit *target_text_edit = memnew(TextEdit);
  969. SceneTree::get_singleton()->get_root()->add_child(target_text_edit);
  970. target_text_edit->set_size(Size2(200, 200));
  971. target_text_edit->set_position(Point2(400, 0));
  972. text_edit->set_size(Size2(200, 200));
  973. CHECK_FALSE(text_edit->is_mouse_over_selection());
  974. text_edit->set_text("drag me");
  975. text_edit->select_all();
  976. text_edit->grab_click_focus();
  977. MessageQueue::get_singleton()->flush();
  978. Point2i line_0 = text_edit->get_pos_at_line_column(0, 0);
  979. line_0.y /= 2;
  980. SEND_GUI_MOUSE_BUTTON_EVENT(line_0, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  981. CHECK(text_edit->is_mouse_over_selection());
  982. SEND_GUI_MOUSE_MOTION_EVENT(text_edit->get_pos_at_line_column(0, 7), MouseButtonMask::LEFT, Key::NONE);
  983. CHECK(text_edit->get_viewport()->gui_is_dragging());
  984. CHECK(text_edit->get_viewport()->gui_get_drag_data() == "drag me");
  985. line_0 = target_text_edit->get_pos_at_line_column(0, 0);
  986. line_0.y /= 2;
  987. line_0.x += 401; // As empty add one.
  988. SEND_GUI_MOUSE_MOTION_EVENT(line_0, MouseButtonMask::LEFT, Key::NONE);
  989. CHECK(text_edit->get_viewport()->gui_is_dragging());
  990. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(line_0, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  991. CHECK_FALSE(text_edit->get_viewport()->gui_is_dragging());
  992. CHECK(text_edit->get_text() == "");
  993. CHECK(target_text_edit->get_text() == "drag me");
  994. memdelete(target_text_edit);
  995. }
  996. SIGNAL_UNWATCH(text_edit, "text_set");
  997. SIGNAL_UNWATCH(text_edit, "text_changed");
  998. SIGNAL_UNWATCH(text_edit, "lines_edited_from");
  999. SIGNAL_UNWATCH(text_edit, "caret_changed");
  1000. }
  1001. SUBCASE("[TextEdit] overridable actions") {
  1002. SIGNAL_WATCH(text_edit, "text_set");
  1003. SIGNAL_WATCH(text_edit, "text_changed");
  1004. SIGNAL_WATCH(text_edit, "lines_edited_from");
  1005. SIGNAL_WATCH(text_edit, "caret_changed");
  1006. Array args1;
  1007. args1.push_back(0);
  1008. args1.push_back(0);
  1009. Array lines_edited_args;
  1010. lines_edited_args.push_back(args1);
  1011. SUBCASE("[TextEdit] backspace") {
  1012. text_edit->set_text("this is\nsome\n");
  1013. text_edit->set_caret_line(0);
  1014. text_edit->set_caret_column(0);
  1015. MessageQueue::get_singleton()->flush();
  1016. SIGNAL_DISCARD("text_set");
  1017. SIGNAL_DISCARD("text_changed");
  1018. SIGNAL_DISCARD("lines_edited_from");
  1019. SIGNAL_DISCARD("caret_changed");
  1020. text_edit->backspace();
  1021. MessageQueue::get_singleton()->flush();
  1022. SIGNAL_CHECK_FALSE("text_changed");
  1023. SIGNAL_CHECK_FALSE("caret_changed");
  1024. SIGNAL_CHECK_FALSE("lines_edited_from");
  1025. text_edit->set_caret_line(2);
  1026. text_edit->set_caret_column(0);
  1027. MessageQueue::get_singleton()->flush();
  1028. SIGNAL_DISCARD("caret_changed");
  1029. ((Array)lines_edited_args[0])[0] = 2;
  1030. ((Array)lines_edited_args[0])[1] = 1;
  1031. text_edit->backspace();
  1032. MessageQueue::get_singleton()->flush();
  1033. CHECK(text_edit->get_text() == "this is\nsome");
  1034. CHECK(text_edit->get_caret_line() == 1);
  1035. CHECK(text_edit->get_caret_column() == 4);
  1036. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1037. SIGNAL_CHECK("text_changed", empty_signal_args);
  1038. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1039. ((Array)lines_edited_args[0])[0] = 1;
  1040. text_edit->backspace();
  1041. MessageQueue::get_singleton()->flush();
  1042. CHECK(text_edit->get_text() == "this is\nsom");
  1043. CHECK(text_edit->get_caret_line() == 1);
  1044. CHECK(text_edit->get_caret_column() == 3);
  1045. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1046. SIGNAL_CHECK("text_changed", empty_signal_args);
  1047. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1048. text_edit->end_complex_operation();
  1049. text_edit->select(1, 0, 1, 3);
  1050. text_edit->backspace();
  1051. MessageQueue::get_singleton()->flush();
  1052. CHECK(text_edit->get_text() == "this is\n");
  1053. CHECK(text_edit->get_caret_line() == 1);
  1054. CHECK(text_edit->get_caret_column() == 0);
  1055. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1056. SIGNAL_CHECK("text_changed", empty_signal_args);
  1057. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1058. text_edit->set_editable(false);
  1059. text_edit->backspace();
  1060. text_edit->set_editable(true);
  1061. MessageQueue::get_singleton()->flush();
  1062. CHECK(text_edit->get_text() == "this is\n");
  1063. CHECK(text_edit->get_caret_line() == 1);
  1064. CHECK(text_edit->get_caret_column() == 0);
  1065. SIGNAL_CHECK_FALSE("text_changed");
  1066. SIGNAL_CHECK_FALSE("caret_changed");
  1067. SIGNAL_CHECK_FALSE("lines_edited_from");
  1068. text_edit->undo();
  1069. MessageQueue::get_singleton()->flush();
  1070. CHECK(text_edit->get_text() == "this is\nsom");
  1071. CHECK(text_edit->get_caret_line() == 1);
  1072. CHECK(text_edit->get_caret_column() == 3);
  1073. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1074. SIGNAL_CHECK("text_changed", empty_signal_args);
  1075. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1076. }
  1077. SUBCASE("[TextEdit] cut") {
  1078. text_edit->set_text("this is\nsome\n");
  1079. text_edit->set_caret_line(0);
  1080. text_edit->set_caret_column(6);
  1081. MessageQueue::get_singleton()->flush();
  1082. SIGNAL_DISCARD("text_set");
  1083. SIGNAL_DISCARD("text_changed");
  1084. SIGNAL_DISCARD("lines_edited_from");
  1085. SIGNAL_DISCARD("caret_changed");
  1086. ERR_PRINT_OFF;
  1087. text_edit->cut();
  1088. MessageQueue::get_singleton()->flush();
  1089. ERR_PRINT_ON; // Can't check display server content.
  1090. ((Array)lines_edited_args[0])[0] = 1;
  1091. CHECK(text_edit->get_text() == "some\n");
  1092. CHECK(text_edit->get_caret_line() == 0);
  1093. CHECK(text_edit->get_caret_column() == 4);
  1094. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1095. SIGNAL_CHECK("text_changed", empty_signal_args);
  1096. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1097. ((Array)lines_edited_args[0])[0] = 0;
  1098. ((Array)lines_edited_args[0])[1] = 1;
  1099. text_edit->undo();
  1100. MessageQueue::get_singleton()->flush();
  1101. CHECK(text_edit->get_text() == "this is\nsome\n");
  1102. CHECK(text_edit->get_caret_line() == 0);
  1103. CHECK(text_edit->get_caret_column() == 6);
  1104. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1105. SIGNAL_CHECK("text_changed", empty_signal_args);
  1106. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1107. ((Array)lines_edited_args[0])[0] = 1;
  1108. ((Array)lines_edited_args[0])[1] = 0;
  1109. text_edit->redo();
  1110. MessageQueue::get_singleton()->flush();
  1111. CHECK(text_edit->get_text() == "some\n");
  1112. CHECK(text_edit->get_caret_line() == 0);
  1113. CHECK(text_edit->get_caret_column() == 4);
  1114. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1115. SIGNAL_CHECK("text_changed", empty_signal_args);
  1116. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1117. text_edit->set_text("this is\nsome\n");
  1118. MessageQueue::get_singleton()->flush();
  1119. SIGNAL_DISCARD("text_set");
  1120. SIGNAL_DISCARD("text_changed");
  1121. SIGNAL_DISCARD("lines_edited_from");
  1122. SIGNAL_DISCARD("caret_changed");
  1123. ((Array)lines_edited_args[0])[0] = 0;
  1124. text_edit->select(0, 5, 0, 7);
  1125. ERR_PRINT_OFF;
  1126. SEND_GUI_ACTION("ui_cut");
  1127. CHECK(text_edit->get_viewport()->is_input_handled());
  1128. MessageQueue::get_singleton()->flush();
  1129. ERR_PRINT_ON; // Can't check display server content.
  1130. CHECK(text_edit->get_text() == "this \nsome\n");
  1131. CHECK(text_edit->get_caret_line() == 0);
  1132. CHECK(text_edit->get_caret_column() == 5);
  1133. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1134. SIGNAL_CHECK("text_changed", empty_signal_args);
  1135. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1136. text_edit->set_editable(false);
  1137. text_edit->cut();
  1138. MessageQueue::get_singleton()->flush();
  1139. text_edit->set_editable(true);
  1140. CHECK(text_edit->get_text() == "this \nsome\n");
  1141. CHECK(text_edit->get_caret_line() == 0);
  1142. CHECK(text_edit->get_caret_column() == 5);
  1143. SIGNAL_CHECK_FALSE("caret_changed");
  1144. SIGNAL_CHECK_FALSE("text_changed");
  1145. SIGNAL_CHECK_FALSE("lines_edited_from");
  1146. }
  1147. SUBCASE("[TextEdit] copy") {
  1148. // TODO: Cannot test need display server support.
  1149. }
  1150. SUBCASE("[TextEdit] paste") {
  1151. // TODO: Cannot test need display server support.
  1152. }
  1153. SUBCASE("[TextEdit] paste primary") {
  1154. // TODO: Cannot test need display server support.
  1155. }
  1156. SIGNAL_UNWATCH(text_edit, "text_set");
  1157. SIGNAL_UNWATCH(text_edit, "text_changed");
  1158. SIGNAL_UNWATCH(text_edit, "lines_edited_from");
  1159. SIGNAL_UNWATCH(text_edit, "caret_changed");
  1160. }
  1161. // Add undo / redo tests?
  1162. SUBCASE("[TextEdit] input") {
  1163. SIGNAL_WATCH(text_edit, "text_set");
  1164. SIGNAL_WATCH(text_edit, "text_changed");
  1165. SIGNAL_WATCH(text_edit, "lines_edited_from");
  1166. SIGNAL_WATCH(text_edit, "caret_changed");
  1167. Array args1;
  1168. args1.push_back(0);
  1169. args1.push_back(0);
  1170. Array lines_edited_args;
  1171. lines_edited_args.push_back(args1);
  1172. SUBCASE("[TextEdit] ui_text_newline_above") {
  1173. text_edit->set_text("this is some test text.\nthis is some test text.");
  1174. text_edit->select(0, 0, 0, 4);
  1175. text_edit->set_caret_column(4);
  1176. text_edit->add_caret(1, 4);
  1177. text_edit->select(1, 0, 1, 4, 1);
  1178. CHECK(text_edit->get_caret_count() == 2);
  1179. MessageQueue::get_singleton()->flush();
  1180. SIGNAL_DISCARD("text_set");
  1181. SIGNAL_DISCARD("text_changed");
  1182. SIGNAL_DISCARD("lines_edited_from");
  1183. SIGNAL_DISCARD("caret_changed");
  1184. // For the second caret.
  1185. Array args2;
  1186. args2.push_back(0);
  1187. args2.push_back(1);
  1188. lines_edited_args.push_front(args2);
  1189. ((Array)lines_edited_args[1])[1] = 1;
  1190. SEND_GUI_ACTION("ui_text_newline_above");
  1191. CHECK(text_edit->get_viewport()->is_input_handled());
  1192. CHECK(text_edit->get_text() == "\nthis is some test text.\n\nthis is some test text.");
  1193. CHECK(text_edit->get_caret_line() == 0);
  1194. CHECK(text_edit->get_caret_column() == 0);
  1195. CHECK_FALSE(text_edit->has_selection(0));
  1196. CHECK(text_edit->get_caret_line(1) == 2);
  1197. CHECK(text_edit->get_caret_column(1) == 0);
  1198. CHECK_FALSE(text_edit->has_selection(1));
  1199. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1200. SIGNAL_CHECK("text_changed", empty_signal_args);
  1201. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1202. text_edit->set_caret_line(1);
  1203. text_edit->set_caret_column(4);
  1204. text_edit->set_caret_line(3, false, true, 0, 1);
  1205. text_edit->set_caret_column(4, false, 1);
  1206. MessageQueue::get_singleton()->flush();
  1207. SIGNAL_DISCARD("caret_changed");
  1208. text_edit->set_editable(false);
  1209. SEND_GUI_ACTION("ui_text_newline_above");
  1210. CHECK(text_edit->get_viewport()->is_input_handled());
  1211. CHECK(text_edit->get_text() == "\nthis is some test text.\n\nthis is some test text.");
  1212. CHECK(text_edit->get_caret_line() == 1);
  1213. CHECK(text_edit->get_caret_column() == 4);
  1214. CHECK_FALSE(text_edit->has_selection(0));
  1215. CHECK(text_edit->get_caret_line(1) == 3);
  1216. CHECK(text_edit->get_caret_column(1) == 4);
  1217. CHECK_FALSE(text_edit->has_selection(1));
  1218. SIGNAL_CHECK_FALSE("caret_changed");
  1219. SIGNAL_CHECK_FALSE("text_changed");
  1220. SIGNAL_CHECK_FALSE("lines_edited_from");
  1221. text_edit->set_editable(true);
  1222. ((Array)lines_edited_args[0])[0] = 2;
  1223. ((Array)lines_edited_args[0])[1] = 3;
  1224. SEND_GUI_ACTION("ui_text_newline_above");
  1225. CHECK(text_edit->get_viewport()->is_input_handled());
  1226. CHECK(text_edit->get_text() == "\n\nthis is some test text.\n\n\nthis is some test text.");
  1227. CHECK(text_edit->get_caret_line() == 1);
  1228. CHECK(text_edit->get_caret_column() == 0);
  1229. CHECK_FALSE(text_edit->has_selection(0));
  1230. CHECK(text_edit->get_caret_line(1) == 4);
  1231. CHECK(text_edit->get_caret_column(1) == 0);
  1232. CHECK_FALSE(text_edit->has_selection(1));
  1233. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1234. SIGNAL_CHECK("text_changed", empty_signal_args);
  1235. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1236. }
  1237. SUBCASE("[TextEdit] ui_text_newline_blank") {
  1238. text_edit->set_text("this is some test text.\nthis is some test text.");
  1239. text_edit->select(0, 0, 0, 4);
  1240. text_edit->set_caret_column(4);
  1241. text_edit->add_caret(1, 4);
  1242. text_edit->select(1, 0, 1, 4, 1);
  1243. CHECK(text_edit->get_caret_count() == 2);
  1244. MessageQueue::get_singleton()->flush();
  1245. SIGNAL_DISCARD("text_set");
  1246. SIGNAL_DISCARD("text_changed");
  1247. SIGNAL_DISCARD("lines_edited_from");
  1248. SIGNAL_DISCARD("caret_changed");
  1249. // For the second caret.
  1250. Array args2;
  1251. args2.push_back(1);
  1252. args2.push_back(2);
  1253. lines_edited_args.push_front(args2);
  1254. ((Array)lines_edited_args[1])[1] = 1;
  1255. SEND_GUI_ACTION("ui_text_newline_blank");
  1256. CHECK(text_edit->get_viewport()->is_input_handled());
  1257. CHECK(text_edit->get_text() == "this is some test text.\n\nthis is some test text.\n");
  1258. CHECK(text_edit->get_caret_line() == 1);
  1259. CHECK(text_edit->get_caret_column() == 0);
  1260. CHECK_FALSE(text_edit->has_selection(0));
  1261. CHECK(text_edit->get_caret_line(1) == 3);
  1262. CHECK(text_edit->get_caret_column(1) == 0);
  1263. CHECK_FALSE(text_edit->has_selection(1));
  1264. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1265. SIGNAL_CHECK("text_changed", empty_signal_args);
  1266. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1267. text_edit->set_editable(false);
  1268. SEND_GUI_ACTION("ui_text_newline_blank");
  1269. CHECK(text_edit->get_viewport()->is_input_handled());
  1270. CHECK(text_edit->get_text() == "this is some test text.\n\nthis is some test text.\n");
  1271. CHECK(text_edit->get_caret_line() == 1);
  1272. CHECK(text_edit->get_caret_column() == 0);
  1273. CHECK_FALSE(text_edit->has_selection(0));
  1274. CHECK(text_edit->get_caret_line(1) == 3);
  1275. CHECK(text_edit->get_caret_column(1) == 0);
  1276. CHECK_FALSE(text_edit->has_selection(1));
  1277. SIGNAL_CHECK_FALSE("caret_changed");
  1278. SIGNAL_CHECK_FALSE("text_changed");
  1279. SIGNAL_CHECK_FALSE("lines_edited_from");
  1280. text_edit->set_editable(true);
  1281. }
  1282. SUBCASE("[TextEdit] ui_text_newline") {
  1283. text_edit->set_text("this is some test text.\nthis is some test text.");
  1284. text_edit->select(0, 0, 0, 4);
  1285. text_edit->set_caret_column(4);
  1286. text_edit->add_caret(1, 4);
  1287. text_edit->select(1, 0, 1, 4, 1);
  1288. CHECK(text_edit->get_caret_count() == 2);
  1289. MessageQueue::get_singleton()->flush();
  1290. SIGNAL_DISCARD("text_set");
  1291. SIGNAL_DISCARD("text_changed");
  1292. SIGNAL_DISCARD("lines_edited_from");
  1293. SIGNAL_DISCARD("caret_changed");
  1294. // For the second caret.
  1295. Array args2;
  1296. args2.push_back(1);
  1297. args2.push_back(1);
  1298. lines_edited_args.push_front(args2);
  1299. lines_edited_args.push_front(args2.duplicate());
  1300. ((Array)lines_edited_args[1])[1] = 2;
  1301. lines_edited_args.push_back(lines_edited_args[2].duplicate());
  1302. ((Array)lines_edited_args[3])[1] = 1;
  1303. SEND_GUI_ACTION("ui_text_newline");
  1304. CHECK(text_edit->get_viewport()->is_input_handled());
  1305. CHECK(text_edit->get_text() == "\n is some test text.\n\n is some test text.");
  1306. CHECK(text_edit->get_caret_line() == 1);
  1307. CHECK(text_edit->get_caret_column() == 0);
  1308. CHECK_FALSE(text_edit->has_selection(0));
  1309. CHECK(text_edit->get_caret_line(1) == 3);
  1310. CHECK(text_edit->get_caret_column(1) == 0);
  1311. CHECK_FALSE(text_edit->has_selection(1));
  1312. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1313. SIGNAL_CHECK("text_changed", empty_signal_args);
  1314. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1315. text_edit->set_editable(false);
  1316. SEND_GUI_ACTION("ui_text_newline");
  1317. CHECK(text_edit->get_viewport()->is_input_handled());
  1318. CHECK(text_edit->get_text() == "\n is some test text.\n\n is some test text.");
  1319. CHECK(text_edit->get_caret_line() == 1);
  1320. CHECK(text_edit->get_caret_column() == 0);
  1321. CHECK_FALSE(text_edit->has_selection(0));
  1322. CHECK(text_edit->get_caret_line(1) == 3);
  1323. CHECK(text_edit->get_caret_column(1) == 0);
  1324. CHECK_FALSE(text_edit->has_selection(1));
  1325. SIGNAL_CHECK_FALSE("caret_changed");
  1326. SIGNAL_CHECK_FALSE("text_changed");
  1327. SIGNAL_CHECK_FALSE("lines_edited_from");
  1328. text_edit->set_editable(true);
  1329. }
  1330. SUBCASE("[TextEdit] ui_text_backspace_all_to_left") {
  1331. text_edit->set_text("\nthis is some test text.\n\nthis is some test text.");
  1332. text_edit->select(1, 0, 1, 4);
  1333. text_edit->set_caret_line(1);
  1334. text_edit->set_caret_column(4);
  1335. text_edit->add_caret(3, 4);
  1336. text_edit->select(3, 0, 3, 4, 1);
  1337. CHECK(text_edit->get_caret_count() == 2);
  1338. MessageQueue::get_singleton()->flush();
  1339. Ref<InputEvent> tmpevent = InputEventKey::create_reference(Key::BACKSPACE | KeyModifierMask::ALT | KeyModifierMask::CMD_OR_CTRL);
  1340. InputMap::get_singleton()->action_add_event("ui_text_backspace_all_to_left", tmpevent);
  1341. SIGNAL_DISCARD("text_set");
  1342. SIGNAL_DISCARD("text_changed");
  1343. SIGNAL_DISCARD("lines_edited_from");
  1344. SIGNAL_DISCARD("caret_changed");
  1345. // For the second caret.
  1346. Array args2;
  1347. args2.push_back(3);
  1348. args2.push_back(3);
  1349. lines_edited_args.push_front(args2);
  1350. // With selection should be a normal backspace.
  1351. ((Array)lines_edited_args[1])[0] = 1;
  1352. ((Array)lines_edited_args[1])[1] = 1;
  1353. SEND_GUI_ACTION("ui_text_backspace_all_to_left");
  1354. CHECK(text_edit->get_viewport()->is_input_handled());
  1355. CHECK(text_edit->get_text() == "\n is some test text.\n\n is some test text.");
  1356. CHECK(text_edit->get_caret_line() == 1);
  1357. CHECK(text_edit->get_caret_column() == 0);
  1358. CHECK_FALSE(text_edit->has_selection(0));
  1359. CHECK(text_edit->get_caret_line(1) == 3);
  1360. CHECK(text_edit->get_caret_column(1) == 0);
  1361. CHECK_FALSE(text_edit->has_selection(1));
  1362. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1363. SIGNAL_CHECK("text_changed", empty_signal_args);
  1364. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1365. ((Array)lines_edited_args[0])[1] = 2;
  1366. ((Array)lines_edited_args[1])[1] = 0;
  1367. // Start of line should also be a normal backspace.
  1368. SEND_GUI_ACTION("ui_text_backspace_all_to_left");
  1369. CHECK(text_edit->get_viewport()->is_input_handled());
  1370. CHECK(text_edit->get_text() == " is some test text.\n is some test text.");
  1371. CHECK(text_edit->get_caret_line() == 0);
  1372. CHECK(text_edit->get_caret_column() == 0);
  1373. CHECK_FALSE(text_edit->has_selection(0));
  1374. CHECK(text_edit->get_caret_line(1) == 1);
  1375. CHECK(text_edit->get_caret_column(1) == 0);
  1376. CHECK_FALSE(text_edit->has_selection(1));
  1377. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1378. SIGNAL_CHECK("text_changed", empty_signal_args);
  1379. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1380. text_edit->set_caret_column(text_edit->get_line(0).length());
  1381. text_edit->set_caret_column(text_edit->get_line(1).length(), false, 1);
  1382. MessageQueue::get_singleton()->flush();
  1383. SIGNAL_DISCARD("text_set");
  1384. SIGNAL_DISCARD("text_changed");
  1385. SIGNAL_DISCARD("lines_edited_from");
  1386. SIGNAL_DISCARD("caret_changed");
  1387. text_edit->set_editable(false);
  1388. SEND_GUI_ACTION("ui_text_backspace_all_to_left");
  1389. CHECK(text_edit->get_viewport()->is_input_handled());
  1390. CHECK(text_edit->get_text() == " is some test text.\n is some test text.");
  1391. CHECK(text_edit->get_caret_line() == 0);
  1392. CHECK(text_edit->get_caret_column() == text_edit->get_line(0).length());
  1393. CHECK_FALSE(text_edit->has_selection(0));
  1394. CHECK(text_edit->get_caret_line(1) == 1);
  1395. CHECK(text_edit->get_caret_column(1) == text_edit->get_line(1).length());
  1396. CHECK_FALSE(text_edit->has_selection(1));
  1397. SIGNAL_CHECK_FALSE("caret_changed");
  1398. SIGNAL_CHECK_FALSE("text_changed");
  1399. SIGNAL_CHECK_FALSE("lines_edited_from");
  1400. text_edit->set_editable(true);
  1401. ((Array)lines_edited_args[0])[0] = 1;
  1402. ((Array)lines_edited_args[0])[1] = 1;
  1403. ((Array)lines_edited_args[1])[0] = 0;
  1404. SEND_GUI_ACTION("ui_text_backspace_all_to_left");
  1405. CHECK(text_edit->get_viewport()->is_input_handled());
  1406. CHECK(text_edit->get_text() == "\n");
  1407. CHECK(text_edit->get_caret_line() == 0);
  1408. CHECK(text_edit->get_caret_column() == 0);
  1409. CHECK_FALSE(text_edit->has_selection(0));
  1410. CHECK(text_edit->get_caret_line(1) == 1);
  1411. CHECK(text_edit->get_caret_column(1) == 0);
  1412. CHECK_FALSE(text_edit->has_selection(1));
  1413. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1414. SIGNAL_CHECK("text_changed", empty_signal_args);
  1415. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1416. InputMap::get_singleton()->action_erase_event("ui_text_backspace_all_to_left", tmpevent);
  1417. }
  1418. SUBCASE("[TextEdit] ui_text_backspace_word") {
  1419. text_edit->set_text("\nthis is some test text.\n\nthis is some test text.");
  1420. text_edit->select(1, 0, 1, 4);
  1421. text_edit->set_caret_line(1);
  1422. text_edit->set_caret_column(4);
  1423. text_edit->add_caret(3, 4);
  1424. text_edit->select(3, 0, 3, 4, 1);
  1425. CHECK(text_edit->get_caret_count() == 2);
  1426. MessageQueue::get_singleton()->flush();
  1427. SIGNAL_DISCARD("text_set");
  1428. SIGNAL_DISCARD("text_changed");
  1429. SIGNAL_DISCARD("lines_edited_from");
  1430. SIGNAL_DISCARD("caret_changed");
  1431. // For the second caret.
  1432. Array args2;
  1433. args2.push_back(3);
  1434. args2.push_back(3);
  1435. lines_edited_args.push_front(args2);
  1436. // With selection should be a normal backspace.
  1437. ((Array)lines_edited_args[1])[0] = 1;
  1438. ((Array)lines_edited_args[1])[1] = 1;
  1439. SEND_GUI_ACTION("ui_text_backspace_word");
  1440. CHECK(text_edit->get_viewport()->is_input_handled());
  1441. CHECK(text_edit->get_text() == "\n is some test text.\n\n is some test text.");
  1442. CHECK(text_edit->get_caret_line() == 1);
  1443. CHECK(text_edit->get_caret_column() == 0);
  1444. CHECK_FALSE(text_edit->has_selection(0));
  1445. CHECK(text_edit->get_caret_line(1) == 3);
  1446. CHECK(text_edit->get_caret_column(1) == 0);
  1447. CHECK_FALSE(text_edit->has_selection(1));
  1448. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1449. SIGNAL_CHECK("text_changed", empty_signal_args);
  1450. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1451. text_edit->end_complex_operation();
  1452. ((Array)lines_edited_args[0])[1] = 2;
  1453. ((Array)lines_edited_args[1])[1] = 0;
  1454. // Start of line should also be a normal backspace.
  1455. SEND_GUI_ACTION("ui_text_backspace_word");
  1456. CHECK(text_edit->get_viewport()->is_input_handled());
  1457. CHECK(text_edit->get_text() == " is some test text.\n is some test text.");
  1458. CHECK(text_edit->get_caret_line() == 0);
  1459. CHECK(text_edit->get_caret_column() == 0);
  1460. CHECK_FALSE(text_edit->has_selection(0));
  1461. CHECK(text_edit->get_caret_line(1) == 1);
  1462. CHECK(text_edit->get_caret_column(1) == 0);
  1463. CHECK_FALSE(text_edit->has_selection(1));
  1464. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1465. SIGNAL_CHECK("text_changed", empty_signal_args);
  1466. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1467. text_edit->set_editable(false);
  1468. SEND_GUI_ACTION("ui_text_backspace_word");
  1469. CHECK(text_edit->get_viewport()->is_input_handled());
  1470. CHECK(text_edit->get_text() == " is some test text.\n is some test text.");
  1471. CHECK(text_edit->get_caret_line() == 0);
  1472. CHECK(text_edit->get_caret_column() == 0);
  1473. CHECK_FALSE(text_edit->has_selection(0));
  1474. CHECK(text_edit->get_caret_line(1) == 1);
  1475. CHECK(text_edit->get_caret_column(1) == 0);
  1476. CHECK_FALSE(text_edit->has_selection(1));
  1477. SIGNAL_CHECK_FALSE("caret_changed");
  1478. SIGNAL_CHECK_FALSE("text_changed");
  1479. SIGNAL_CHECK_FALSE("lines_edited_from");
  1480. text_edit->set_editable(true);
  1481. text_edit->set_caret_column(text_edit->get_line(0).length());
  1482. text_edit->set_caret_column(text_edit->get_line(1).length(), false, 1);
  1483. MessageQueue::get_singleton()->flush();
  1484. SIGNAL_DISCARD("text_set");
  1485. SIGNAL_DISCARD("text_changed");
  1486. SIGNAL_DISCARD("lines_edited_from");
  1487. SIGNAL_DISCARD("caret_changed");
  1488. ((Array)lines_edited_args[0])[0] = 1;
  1489. ((Array)lines_edited_args[0])[1] = 1;
  1490. ((Array)lines_edited_args[1])[0] = 0;
  1491. SEND_GUI_ACTION("ui_text_backspace_word");
  1492. CHECK(text_edit->get_viewport()->is_input_handled());
  1493. CHECK(text_edit->get_text() == " is some test \n is some test ");
  1494. CHECK(text_edit->get_caret_line() == 0);
  1495. CHECK(text_edit->get_caret_column() == 14);
  1496. CHECK_FALSE(text_edit->has_selection(0));
  1497. CHECK(text_edit->get_caret_line(1) == 1);
  1498. CHECK(text_edit->get_caret_column(1) == 14);
  1499. CHECK_FALSE(text_edit->has_selection(1));
  1500. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1501. SIGNAL_CHECK("text_changed", empty_signal_args);
  1502. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1503. }
  1504. SUBCASE("[TextEdit] ui_text_backspace_word same line") {
  1505. text_edit->set_text("test test test");
  1506. text_edit->set_caret_column(4);
  1507. text_edit->add_caret(0, 9);
  1508. text_edit->add_caret(0, 15);
  1509. // For the second caret.
  1510. Array args2;
  1511. args2.push_back(0);
  1512. lines_edited_args.push_front(args2);
  1513. // For the third caret.
  1514. Array args3;
  1515. args2.push_back(0);
  1516. lines_edited_args.push_front(args2);
  1517. CHECK(text_edit->get_caret_count() == 3);
  1518. MessageQueue::get_singleton()->flush();
  1519. SIGNAL_DISCARD("text_set");
  1520. SIGNAL_DISCARD("text_changed");
  1521. SIGNAL_DISCARD("lines_edited_from");
  1522. SIGNAL_DISCARD("caret_changed");
  1523. SEND_GUI_ACTION("ui_text_backspace_word");
  1524. CHECK(text_edit->get_viewport()->is_input_handled());
  1525. CHECK(text_edit->get_text() == " ");
  1526. CHECK(text_edit->get_caret_line() == 0);
  1527. CHECK(text_edit->get_caret_column() == 0);
  1528. CHECK_FALSE(text_edit->has_selection(0));
  1529. CHECK(text_edit->get_caret_line(1) == 0);
  1530. CHECK(text_edit->get_caret_column(1) == 1);
  1531. CHECK_FALSE(text_edit->has_selection(1));
  1532. CHECK(text_edit->get_caret_line(2) == 0);
  1533. CHECK(text_edit->get_caret_column(2) == 2);
  1534. CHECK_FALSE(text_edit->has_selection(1));
  1535. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1536. SIGNAL_CHECK("text_changed", empty_signal_args);
  1537. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1538. }
  1539. SUBCASE("[TextEdit] ui_text_backspace") {
  1540. text_edit->set_text("\nthis is some test text.\n\nthis is some test text.");
  1541. text_edit->select(1, 0, 1, 4);
  1542. text_edit->set_caret_line(1);
  1543. text_edit->set_caret_column(4);
  1544. text_edit->add_caret(3, 4);
  1545. text_edit->select(3, 0, 3, 4, 1);
  1546. CHECK(text_edit->get_caret_count() == 2);
  1547. MessageQueue::get_singleton()->flush();
  1548. SIGNAL_DISCARD("text_set");
  1549. SIGNAL_DISCARD("text_changed");
  1550. SIGNAL_DISCARD("lines_edited_from");
  1551. SIGNAL_DISCARD("caret_changed");
  1552. // For the second caret.
  1553. Array args2;
  1554. args2.push_back(3);
  1555. args2.push_back(3);
  1556. lines_edited_args.push_front(args2);
  1557. // With selection should be a normal backspace.
  1558. ((Array)lines_edited_args[1])[0] = 1;
  1559. ((Array)lines_edited_args[1])[1] = 1;
  1560. SEND_GUI_ACTION("ui_text_backspace");
  1561. CHECK(text_edit->get_viewport()->is_input_handled());
  1562. CHECK(text_edit->get_text() == "\n is some test text.\n\n is some test text.");
  1563. CHECK(text_edit->get_caret_line() == 1);
  1564. CHECK(text_edit->get_caret_column() == 0);
  1565. CHECK_FALSE(text_edit->has_selection(0));
  1566. CHECK(text_edit->get_caret_line(1) == 3);
  1567. CHECK(text_edit->get_caret_column(1) == 0);
  1568. CHECK_FALSE(text_edit->has_selection(1));
  1569. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1570. SIGNAL_CHECK("text_changed", empty_signal_args);
  1571. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1572. ((Array)lines_edited_args[0])[1] = 2;
  1573. ((Array)lines_edited_args[1])[1] = 0;
  1574. // Start of line should also be a normal backspace.
  1575. SEND_GUI_ACTION("ui_text_backspace");
  1576. CHECK(text_edit->get_viewport()->is_input_handled());
  1577. CHECK(text_edit->get_text() == " is some test text.\n is some test text.");
  1578. CHECK(text_edit->get_caret_line() == 0);
  1579. CHECK(text_edit->get_caret_column() == 0);
  1580. CHECK_FALSE(text_edit->has_selection(0));
  1581. CHECK(text_edit->get_caret_line(1) == 1);
  1582. CHECK(text_edit->get_caret_column(1) == 0);
  1583. CHECK_FALSE(text_edit->has_selection(1));
  1584. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1585. SIGNAL_CHECK("text_changed", empty_signal_args);
  1586. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1587. text_edit->set_caret_column(text_edit->get_line(0).length());
  1588. text_edit->set_caret_column(text_edit->get_line(1).length(), false, 1);
  1589. MessageQueue::get_singleton()->flush();
  1590. SIGNAL_DISCARD("text_set");
  1591. SIGNAL_DISCARD("text_changed");
  1592. SIGNAL_DISCARD("lines_edited_from");
  1593. SIGNAL_DISCARD("caret_changed");
  1594. text_edit->set_editable(false);
  1595. SEND_GUI_ACTION("ui_text_backspace");
  1596. CHECK(text_edit->get_viewport()->is_input_handled());
  1597. CHECK(text_edit->get_text() == " is some test text.\n is some test text.");
  1598. CHECK(text_edit->get_caret_line() == 0);
  1599. CHECK(text_edit->get_caret_column() == text_edit->get_line(0).length());
  1600. CHECK_FALSE(text_edit->has_selection(0));
  1601. CHECK(text_edit->get_caret_line(1) == 1);
  1602. CHECK(text_edit->get_caret_column(1) == text_edit->get_line(1).length());
  1603. CHECK_FALSE(text_edit->has_selection(1));
  1604. SIGNAL_CHECK_FALSE("caret_changed");
  1605. SIGNAL_CHECK_FALSE("text_changed");
  1606. SIGNAL_CHECK_FALSE("lines_edited_from");
  1607. text_edit->set_editable(true);
  1608. ((Array)lines_edited_args[0])[0] = 1;
  1609. ((Array)lines_edited_args[0])[1] = 1;
  1610. ((Array)lines_edited_args[1])[0] = 0;
  1611. SEND_GUI_ACTION("ui_text_backspace");
  1612. CHECK(text_edit->get_viewport()->is_input_handled());
  1613. CHECK(text_edit->get_text() == " is some test text\n is some test text");
  1614. CHECK(text_edit->get_caret_line() == 0);
  1615. CHECK(text_edit->get_caret_column() == 18);
  1616. CHECK_FALSE(text_edit->has_selection(0));
  1617. CHECK(text_edit->get_caret_line(1) == 1);
  1618. CHECK(text_edit->get_caret_column(1) == 18);
  1619. CHECK_FALSE(text_edit->has_selection(1));
  1620. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1621. SIGNAL_CHECK("text_changed", empty_signal_args);
  1622. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1623. // Select the entire text, from right to left
  1624. text_edit->select(0, 18, 0, 0);
  1625. text_edit->set_caret_line(0);
  1626. text_edit->set_caret_column(0);
  1627. text_edit->select(1, 18, 1, 0, 1);
  1628. text_edit->set_caret_line(1, false, true, 0, 1);
  1629. text_edit->set_caret_column(0, false, 1);
  1630. MessageQueue::get_singleton()->flush();
  1631. SIGNAL_DISCARD("text_set");
  1632. SIGNAL_DISCARD("text_changed");
  1633. SIGNAL_DISCARD("lines_edited_from");
  1634. SIGNAL_DISCARD("caret_changed");
  1635. SEND_GUI_ACTION("ui_text_backspace");
  1636. CHECK(text_edit->get_text() == "\n");
  1637. CHECK(text_edit->get_caret_line() == 0);
  1638. CHECK(text_edit->get_caret_column() == 0);
  1639. CHECK(text_edit->get_caret_line(1) == 1);
  1640. CHECK(text_edit->get_caret_column(1) == 0);
  1641. SIGNAL_CHECK_FALSE("caret_changed");
  1642. SIGNAL_CHECK("text_changed", empty_signal_args);
  1643. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1644. }
  1645. SUBCASE("[TextEdit] ui_text_delete_all_to_right") {
  1646. Ref<InputEvent> tmpevent = InputEventKey::create_reference(Key::BACKSPACE | KeyModifierMask::ALT | KeyModifierMask::CMD_OR_CTRL);
  1647. InputMap::get_singleton()->action_add_event("ui_text_delete_all_to_right", tmpevent);
  1648. text_edit->set_text("this is some test text.\nthis is some test text.\n");
  1649. text_edit->select(0, 0, 0, 4);
  1650. text_edit->set_caret_line(0);
  1651. text_edit->set_caret_column(4);
  1652. text_edit->add_caret(1, 4);
  1653. text_edit->select(1, 0, 1, 4, 1);
  1654. CHECK(text_edit->get_caret_count() == 2);
  1655. MessageQueue::get_singleton()->flush();
  1656. SIGNAL_DISCARD("text_set");
  1657. SIGNAL_DISCARD("text_changed");
  1658. SIGNAL_DISCARD("lines_edited_from");
  1659. SIGNAL_DISCARD("caret_changed");
  1660. // For the second caret.
  1661. Array args2;
  1662. args2.push_back(1);
  1663. args2.push_back(1);
  1664. lines_edited_args.push_front(args2);
  1665. // With selection should be a normal delete.
  1666. SEND_GUI_ACTION("ui_text_delete_all_to_right");
  1667. CHECK(text_edit->get_viewport()->is_input_handled());
  1668. CHECK(text_edit->get_text() == " is some test text.\n is some test text.\n");
  1669. CHECK(text_edit->get_caret_line() == 0);
  1670. CHECK(text_edit->get_caret_column() == 0);
  1671. CHECK_FALSE(text_edit->has_selection(0));
  1672. CHECK(text_edit->get_caret_line(1) == 1);
  1673. CHECK(text_edit->get_caret_column(1) == 0);
  1674. CHECK_FALSE(text_edit->has_selection(1));
  1675. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1676. SIGNAL_CHECK("text_changed", empty_signal_args);
  1677. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1678. // End of line should not do anything.
  1679. text_edit->set_caret_column(text_edit->get_line(0).length());
  1680. text_edit->set_caret_column(text_edit->get_line(1).length(), false, 1);
  1681. MessageQueue::get_singleton()->flush();
  1682. SIGNAL_DISCARD("text_set");
  1683. SIGNAL_DISCARD("text_changed");
  1684. SIGNAL_DISCARD("lines_edited_from");
  1685. SIGNAL_DISCARD("caret_changed");
  1686. SEND_GUI_ACTION("ui_text_delete_all_to_right");
  1687. CHECK(text_edit->get_viewport()->is_input_handled());
  1688. CHECK(text_edit->get_text() == " is some test text.\n is some test text.\n");
  1689. CHECK(text_edit->get_caret_line() == 0);
  1690. CHECK(text_edit->get_caret_column() == text_edit->get_line(0).length());
  1691. CHECK_FALSE(text_edit->has_selection(0));
  1692. CHECK(text_edit->get_caret_line(1) == 1);
  1693. CHECK(text_edit->get_caret_column(1) == text_edit->get_line(1).length());
  1694. CHECK_FALSE(text_edit->has_selection(1));
  1695. SIGNAL_CHECK_FALSE("caret_changed");
  1696. SIGNAL_CHECK_FALSE("text_changed");
  1697. SIGNAL_CHECK_FALSE("lines_edited_from");
  1698. text_edit->set_caret_column(0);
  1699. text_edit->set_caret_column(0, false, 1);
  1700. MessageQueue::get_singleton()->flush();
  1701. SIGNAL_DISCARD("text_set");
  1702. SIGNAL_DISCARD("text_changed");
  1703. SIGNAL_DISCARD("lines_edited_from");
  1704. SIGNAL_DISCARD("caret_changed");
  1705. text_edit->set_editable(false);
  1706. SEND_GUI_ACTION("ui_text_delete_all_to_right");
  1707. CHECK(text_edit->get_viewport()->is_input_handled());
  1708. CHECK(text_edit->get_text() == " is some test text.\n is some test text.\n");
  1709. CHECK(text_edit->get_caret_line() == 0);
  1710. CHECK(text_edit->get_caret_column() == 0);
  1711. CHECK_FALSE(text_edit->has_selection(0));
  1712. CHECK(text_edit->get_caret_line(1) == 1);
  1713. CHECK(text_edit->get_caret_column(1) == 0);
  1714. CHECK_FALSE(text_edit->has_selection(1));
  1715. SIGNAL_CHECK_FALSE("caret_changed");
  1716. SIGNAL_CHECK_FALSE("text_changed");
  1717. SIGNAL_CHECK_FALSE("lines_edited_from");
  1718. text_edit->set_editable(true);
  1719. SEND_GUI_ACTION("ui_text_delete_all_to_right");
  1720. CHECK(text_edit->get_viewport()->is_input_handled());
  1721. CHECK(text_edit->get_text() == "\n\n");
  1722. CHECK(text_edit->get_caret_line() == 0);
  1723. CHECK(text_edit->get_caret_column() == 0);
  1724. CHECK_FALSE(text_edit->has_selection(0));
  1725. CHECK(text_edit->get_caret_line(1) == 1);
  1726. CHECK(text_edit->get_caret_column(1) == 0);
  1727. CHECK_FALSE(text_edit->has_selection(1));
  1728. SIGNAL_CHECK_FALSE("caret_changed");
  1729. SIGNAL_CHECK("text_changed", empty_signal_args);
  1730. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1731. InputMap::get_singleton()->action_erase_event("ui_text_delete_all_to_right", tmpevent);
  1732. }
  1733. SUBCASE("[TextEdit] ui_text_delete_word") {
  1734. text_edit->set_caret_mid_grapheme_enabled(true);
  1735. CHECK(text_edit->is_caret_mid_grapheme_enabled());
  1736. text_edit->set_text("this ffi some test text.\n\nthis ffi some test text.\n");
  1737. text_edit->select(0, 0, 0, 4);
  1738. text_edit->set_caret_line(0);
  1739. text_edit->set_caret_column(4);
  1740. text_edit->add_caret(2, 4);
  1741. text_edit->select(2, 0, 2, 4, 1);
  1742. CHECK(text_edit->get_caret_count() == 2);
  1743. MessageQueue::get_singleton()->flush();
  1744. SIGNAL_DISCARD("text_set");
  1745. SIGNAL_DISCARD("text_changed");
  1746. SIGNAL_DISCARD("lines_edited_from");
  1747. SIGNAL_DISCARD("caret_changed");
  1748. // For the second caret.
  1749. Array args2;
  1750. args2.push_back(2);
  1751. args2.push_back(2);
  1752. lines_edited_args.push_front(args2);
  1753. // With selection should be a normal delete.
  1754. SEND_GUI_ACTION("ui_text_delete_word");
  1755. CHECK(text_edit->get_viewport()->is_input_handled());
  1756. CHECK(text_edit->get_text() == " ffi some test text.\n\n ffi some test text.\n");
  1757. CHECK(text_edit->get_caret_line() == 0);
  1758. CHECK(text_edit->get_caret_column() == 0);
  1759. CHECK_FALSE(text_edit->has_selection(0));
  1760. CHECK(text_edit->get_caret_line(1) == 2);
  1761. CHECK(text_edit->get_caret_column(1) == 0);
  1762. CHECK_FALSE(text_edit->has_selection(1));
  1763. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1764. SIGNAL_CHECK("text_changed", empty_signal_args);
  1765. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1766. // With selection should be a normal delete.
  1767. ((Array)lines_edited_args[0])[0] = 3;
  1768. ((Array)lines_edited_args[1])[0] = 1;
  1769. text_edit->set_caret_column(text_edit->get_line(0).length());
  1770. text_edit->set_caret_column(text_edit->get_line(2).length(), false, 1);
  1771. MessageQueue::get_singleton()->flush();
  1772. SIGNAL_DISCARD("text_set");
  1773. SIGNAL_DISCARD("text_changed");
  1774. SIGNAL_DISCARD("lines_edited_from");
  1775. SIGNAL_DISCARD("caret_changed");
  1776. SEND_GUI_ACTION("ui_text_delete_word");
  1777. CHECK(text_edit->get_viewport()->is_input_handled());
  1778. CHECK(text_edit->get_text() == " ffi some test text.\n ffi some test text.");
  1779. CHECK(text_edit->get_caret_line() == 0);
  1780. CHECK(text_edit->get_caret_column() == text_edit->get_line(0).length());
  1781. CHECK_FALSE(text_edit->has_selection());
  1782. CHECK(text_edit->get_caret_line(1) == 1);
  1783. CHECK(text_edit->get_caret_column(1) == text_edit->get_line(1).length());
  1784. CHECK_FALSE(text_edit->has_selection(0));
  1785. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1786. SIGNAL_CHECK("text_changed", empty_signal_args);
  1787. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1788. ((Array)lines_edited_args[1])[0] = 0;
  1789. ((Array)lines_edited_args[0])[0] = 1;
  1790. ((Array)lines_edited_args[0])[1] = 1;
  1791. text_edit->set_caret_column(0);
  1792. text_edit->set_caret_column(0, false, 1);
  1793. MessageQueue::get_singleton()->flush();
  1794. SIGNAL_DISCARD("text_set");
  1795. SIGNAL_DISCARD("text_changed");
  1796. SIGNAL_DISCARD("lines_edited_from");
  1797. SIGNAL_DISCARD("caret_changed");
  1798. text_edit->set_editable(false);
  1799. SEND_GUI_ACTION("ui_text_delete_word");
  1800. CHECK(text_edit->get_viewport()->is_input_handled());
  1801. CHECK(text_edit->get_text() == " ffi some test text.\n ffi some test text.");
  1802. CHECK(text_edit->get_caret_line() == 0);
  1803. CHECK(text_edit->get_caret_column() == 0);
  1804. CHECK_FALSE(text_edit->has_selection(0));
  1805. CHECK(text_edit->get_caret_line(1) == 1);
  1806. CHECK(text_edit->get_caret_column(1) == 0);
  1807. CHECK_FALSE(text_edit->has_selection(1));
  1808. SIGNAL_CHECK_FALSE("caret_changed");
  1809. SIGNAL_CHECK_FALSE("text_changed");
  1810. SIGNAL_CHECK_FALSE("lines_edited_from");
  1811. text_edit->set_editable(true);
  1812. SEND_GUI_ACTION("ui_text_delete_word");
  1813. CHECK(text_edit->get_viewport()->is_input_handled());
  1814. CHECK(text_edit->get_text() == " some test text.\n some test text.");
  1815. CHECK(text_edit->get_caret_line() == 0);
  1816. CHECK(text_edit->get_caret_column() == 0);
  1817. CHECK_FALSE(text_edit->has_selection(0));
  1818. CHECK(text_edit->get_caret_line(1) == 1);
  1819. CHECK(text_edit->get_caret_column(1) == 0);
  1820. CHECK_FALSE(text_edit->has_selection(1));
  1821. SIGNAL_CHECK_FALSE("caret_changed");
  1822. SIGNAL_CHECK("text_changed", empty_signal_args);
  1823. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1824. }
  1825. SUBCASE("[TextEdit] ui_text_delete") {
  1826. text_edit->set_caret_mid_grapheme_enabled(true);
  1827. CHECK(text_edit->is_caret_mid_grapheme_enabled());
  1828. text_edit->set_text("this ffi some test text.\nthis ffi some test text.");
  1829. text_edit->select(0, 0, 0, 4);
  1830. text_edit->set_caret_line(0);
  1831. text_edit->set_caret_column(4);
  1832. text_edit->add_caret(1, 4);
  1833. text_edit->select(1, 0, 1, 4, 1);
  1834. CHECK(text_edit->get_caret_count() == 2);
  1835. MessageQueue::get_singleton()->flush();
  1836. SIGNAL_DISCARD("text_set");
  1837. SIGNAL_DISCARD("text_changed");
  1838. SIGNAL_DISCARD("lines_edited_from");
  1839. SIGNAL_DISCARD("caret_changed");
  1840. // For the second caret.
  1841. Array args2;
  1842. args2.push_back(1);
  1843. args2.push_back(1);
  1844. lines_edited_args.push_front(args2);
  1845. // With selection should be a normal delete.
  1846. SEND_GUI_ACTION("ui_text_delete");
  1847. CHECK(text_edit->get_viewport()->is_input_handled());
  1848. CHECK(text_edit->get_text() == " ffi some test text.\n ffi some test text.");
  1849. CHECK(text_edit->get_caret_line() == 0);
  1850. CHECK(text_edit->get_caret_column() == 0);
  1851. CHECK_FALSE(text_edit->has_selection(0));
  1852. CHECK(text_edit->get_caret_line(1) == 1);
  1853. CHECK(text_edit->get_caret_column(1) == 0);
  1854. CHECK_FALSE(text_edit->has_selection(1));
  1855. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1856. SIGNAL_CHECK("text_changed", empty_signal_args);
  1857. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1858. // With selection should be a normal delete.
  1859. lines_edited_args.remove_at(0);
  1860. ((Array)lines_edited_args[0])[0] = 1;
  1861. text_edit->set_caret_column(text_edit->get_line(1).length(), false, 1);
  1862. text_edit->set_caret_column(text_edit->get_line(0).length());
  1863. MessageQueue::get_singleton()->flush();
  1864. SIGNAL_DISCARD("text_set");
  1865. SIGNAL_DISCARD("text_changed");
  1866. SIGNAL_DISCARD("lines_edited_from");
  1867. SIGNAL_DISCARD("caret_changed");
  1868. SEND_GUI_ACTION("ui_text_delete");
  1869. CHECK(text_edit->get_viewport()->is_input_handled());
  1870. CHECK(text_edit->get_text() == " ffi some test text. ffi some test text.");
  1871. CHECK(text_edit->get_caret_line() == 0);
  1872. CHECK(text_edit->get_caret_column() == 20);
  1873. CHECK_FALSE(text_edit->has_selection(0));
  1874. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1875. SIGNAL_CHECK("text_changed", empty_signal_args);
  1876. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1877. // Caret should be removed due to column preservation.
  1878. CHECK(text_edit->get_caret_count() == 1);
  1879. // Lets add it back.
  1880. text_edit->set_caret_column(0);
  1881. text_edit->add_caret(0, 20);
  1882. ((Array)lines_edited_args[0])[0] = 0;
  1883. lines_edited_args.push_back(args2);
  1884. ((Array)lines_edited_args[1])[0] = 0;
  1885. ((Array)lines_edited_args[1])[1] = 0;
  1886. MessageQueue::get_singleton()->flush();
  1887. SIGNAL_DISCARD("text_set");
  1888. SIGNAL_DISCARD("text_changed");
  1889. SIGNAL_DISCARD("lines_edited_from");
  1890. SIGNAL_DISCARD("caret_changed");
  1891. text_edit->set_editable(false);
  1892. SEND_GUI_ACTION("ui_text_delete");
  1893. CHECK(text_edit->get_viewport()->is_input_handled());
  1894. CHECK(text_edit->get_text() == " ffi some test text. ffi some test text.");
  1895. CHECK(text_edit->get_caret_line() == 0);
  1896. CHECK(text_edit->get_caret_column() == 0);
  1897. CHECK_FALSE(text_edit->has_selection(0));
  1898. CHECK(text_edit->get_caret_line(1) == 0);
  1899. CHECK(text_edit->get_caret_column(1) == 20);
  1900. CHECK_FALSE(text_edit->has_selection(1));
  1901. SIGNAL_CHECK_FALSE("caret_changed");
  1902. SIGNAL_CHECK_FALSE("text_changed");
  1903. SIGNAL_CHECK_FALSE("lines_edited_from");
  1904. text_edit->set_editable(true);
  1905. text_edit->start_action(TextEdit::EditAction::ACTION_NONE);
  1906. SEND_GUI_ACTION("ui_text_delete");
  1907. CHECK(text_edit->get_viewport()->is_input_handled());
  1908. CHECK(text_edit->get_text() == "ffi some test text.ffi some test text.");
  1909. CHECK(text_edit->get_caret_line() == 0);
  1910. CHECK(text_edit->get_caret_column() == 0);
  1911. CHECK_FALSE(text_edit->has_selection(0));
  1912. CHECK(text_edit->get_caret_line(1) == 0);
  1913. CHECK(text_edit->get_caret_column(1) == 19);
  1914. CHECK_FALSE(text_edit->has_selection(0));
  1915. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1916. SIGNAL_CHECK("text_changed", empty_signal_args);
  1917. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1918. text_edit->start_action(TextEdit::EditAction::ACTION_NONE);
  1919. SEND_GUI_ACTION("ui_text_delete");
  1920. CHECK(text_edit->get_viewport()->is_input_handled());
  1921. CHECK(text_edit->get_text() == "fi some test text.fi some test text.");
  1922. CHECK(text_edit->get_caret_line() == 0);
  1923. CHECK(text_edit->get_caret_column() == 0);
  1924. CHECK_FALSE(text_edit->has_selection(0));
  1925. CHECK(text_edit->get_caret_line(1) == 0);
  1926. CHECK(text_edit->get_caret_column(1) == 18);
  1927. CHECK_FALSE(text_edit->has_selection(1));
  1928. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1929. SIGNAL_CHECK("text_changed", empty_signal_args);
  1930. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1931. text_edit->set_caret_mid_grapheme_enabled(false);
  1932. CHECK_FALSE(text_edit->is_caret_mid_grapheme_enabled());
  1933. text_edit->start_action(TextEdit::EditAction::ACTION_NONE);
  1934. text_edit->undo();
  1935. MessageQueue::get_singleton()->flush();
  1936. CHECK(text_edit->get_text() == "ffi some test text.ffi some test text.");
  1937. SIGNAL_DISCARD("text_set");
  1938. SIGNAL_DISCARD("text_changed");
  1939. SIGNAL_DISCARD("lines_edited_from");
  1940. SIGNAL_DISCARD("caret_changed");
  1941. SEND_GUI_ACTION("ui_text_delete");
  1942. CHECK(text_edit->get_viewport()->is_input_handled());
  1943. CHECK(text_edit->get_text() == " some test text. some test text.");
  1944. CHECK(text_edit->get_caret_line() == 0);
  1945. CHECK(text_edit->get_caret_column() == 0);
  1946. CHECK_FALSE(text_edit->has_selection(0));
  1947. CHECK(text_edit->get_caret_line(1) == 0);
  1948. CHECK(text_edit->get_caret_column(1) == 16);
  1949. CHECK_FALSE(text_edit->has_selection(1));
  1950. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1951. SIGNAL_CHECK("text_changed", empty_signal_args);
  1952. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  1953. }
  1954. SUBCASE("[TextEdit] ui_text_caret_word_left") {
  1955. text_edit->set_text("\nthis is some test text.\nthis is some test text.");
  1956. text_edit->set_caret_line(1);
  1957. text_edit->set_caret_column(7);
  1958. text_edit->add_caret(2, 7);
  1959. CHECK(text_edit->get_caret_count() == 2);
  1960. MessageQueue::get_singleton()->flush();
  1961. SIGNAL_DISCARD("text_set");
  1962. SIGNAL_DISCARD("text_changed");
  1963. SIGNAL_DISCARD("lines_edited_from");
  1964. SIGNAL_DISCARD("caret_changed");
  1965. // Shift should select.
  1966. #ifdef MACOS_ENABLED
  1967. SEND_GUI_KEY_EVENT(Key::LEFT | KeyModifierMask::ALT | KeyModifierMask::SHIFT);
  1968. #else
  1969. SEND_GUI_KEY_EVENT(Key::LEFT | KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT);
  1970. #endif
  1971. CHECK(text_edit->get_viewport()->is_input_handled());
  1972. CHECK(text_edit->get_caret_line() == 1);
  1973. CHECK(text_edit->get_caret_column() == 5);
  1974. CHECK(text_edit->get_selected_text(0) == "is");
  1975. CHECK(text_edit->has_selection(0));
  1976. CHECK(text_edit->get_caret_line(1) == 2);
  1977. CHECK(text_edit->get_caret_column(1) == 5);
  1978. CHECK(text_edit->get_selected_text(1) == "is");
  1979. CHECK(text_edit->has_selection(1));
  1980. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1981. SIGNAL_CHECK_FALSE("text_changed");
  1982. SIGNAL_CHECK_FALSE("lines_edited_from");
  1983. // Should still move caret with selection.
  1984. SEND_GUI_ACTION("ui_text_caret_word_left");
  1985. CHECK(text_edit->get_viewport()->is_input_handled());
  1986. CHECK(text_edit->get_caret_line() == 1);
  1987. CHECK(text_edit->get_caret_column() == 0);
  1988. CHECK_FALSE(text_edit->has_selection(0));
  1989. CHECK(text_edit->get_caret_line(1) == 2);
  1990. CHECK(text_edit->get_caret_column(1) == 0);
  1991. CHECK_FALSE(text_edit->has_selection(1));
  1992. SIGNAL_CHECK("caret_changed", empty_signal_args);
  1993. SIGNAL_CHECK_FALSE("text_changed");
  1994. SIGNAL_CHECK_FALSE("lines_edited_from");
  1995. // Normal word left.
  1996. SEND_GUI_ACTION("ui_text_caret_word_left");
  1997. CHECK(text_edit->get_viewport()->is_input_handled());
  1998. CHECK(text_edit->get_caret_line() == 0);
  1999. CHECK(text_edit->get_caret_column() == 0);
  2000. CHECK_FALSE(text_edit->has_selection(0));
  2001. CHECK(text_edit->get_caret_line(1) == 1);
  2002. CHECK(text_edit->get_caret_column(1) == 23);
  2003. CHECK_FALSE(text_edit->has_selection(1));
  2004. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2005. SIGNAL_CHECK_FALSE("text_changed");
  2006. SIGNAL_CHECK_FALSE("lines_edited_from");
  2007. }
  2008. SUBCASE("[TextEdit] ui_text_caret_left") {
  2009. text_edit->set_text("\nthis is some test text.\nthis is some test text.");
  2010. text_edit->set_caret_line(1);
  2011. text_edit->set_caret_column(7);
  2012. text_edit->select(1, 2, 1, 7);
  2013. text_edit->add_caret(2, 7);
  2014. text_edit->select(2, 2, 2, 7, 1);
  2015. CHECK(text_edit->get_caret_count() == 2);
  2016. MessageQueue::get_singleton()->flush();
  2017. SIGNAL_DISCARD("text_set");
  2018. SIGNAL_DISCARD("text_changed");
  2019. SIGNAL_DISCARD("lines_edited_from");
  2020. SIGNAL_DISCARD("caret_changed");
  2021. // Normal left should deselect and place at selection start.
  2022. SEND_GUI_ACTION("ui_text_caret_left");
  2023. CHECK(text_edit->get_viewport()->is_input_handled());
  2024. CHECK(text_edit->get_caret_line() == 1);
  2025. CHECK(text_edit->get_caret_column() == 2);
  2026. CHECK_FALSE(text_edit->has_selection(0));
  2027. CHECK(text_edit->get_caret_line(1) == 2);
  2028. CHECK(text_edit->get_caret_column(1) == 2);
  2029. CHECK_FALSE(text_edit->has_selection(1));
  2030. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2031. SIGNAL_CHECK_FALSE("text_changed");
  2032. SIGNAL_CHECK_FALSE("lines_edited_from");
  2033. // With shift should select.
  2034. SEND_GUI_KEY_EVENT(Key::LEFT | KeyModifierMask::SHIFT);
  2035. CHECK(text_edit->get_viewport()->is_input_handled());
  2036. CHECK(text_edit->get_caret_line() == 1);
  2037. CHECK(text_edit->get_caret_column() == 1);
  2038. CHECK(text_edit->get_selected_text(0) == "h");
  2039. CHECK(text_edit->has_selection(0));
  2040. CHECK(text_edit->get_caret_line(1) == 2);
  2041. CHECK(text_edit->get_caret_column(1) == 1);
  2042. CHECK(text_edit->get_selected_text(1) == "h");
  2043. CHECK(text_edit->has_selection(1));
  2044. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2045. SIGNAL_CHECK_FALSE("text_changed");
  2046. SIGNAL_CHECK_FALSE("lines_edited_from");
  2047. // All ready at select left, should only deselect.
  2048. SEND_GUI_ACTION("ui_text_caret_left");
  2049. CHECK(text_edit->get_viewport()->is_input_handled());
  2050. CHECK(text_edit->get_caret_line() == 1);
  2051. CHECK(text_edit->get_caret_column() == 1);
  2052. CHECK_FALSE(text_edit->has_selection(0));
  2053. CHECK(text_edit->get_caret_line(1) == 2);
  2054. CHECK(text_edit->get_caret_column(1) == 1);
  2055. CHECK_FALSE(text_edit->has_selection(1));
  2056. SIGNAL_CHECK_FALSE("caret_changed");
  2057. SIGNAL_CHECK_FALSE("text_changed");
  2058. SIGNAL_CHECK_FALSE("lines_edited_from");
  2059. // Normal left.
  2060. SEND_GUI_ACTION("ui_text_caret_left");
  2061. CHECK(text_edit->get_viewport()->is_input_handled());
  2062. CHECK(text_edit->get_caret_line() == 1);
  2063. CHECK(text_edit->get_caret_column() == 0);
  2064. CHECK_FALSE(text_edit->has_selection(0));
  2065. CHECK(text_edit->get_caret_line(1) == 2);
  2066. CHECK(text_edit->get_caret_column(1) == 0);
  2067. CHECK_FALSE(text_edit->has_selection());
  2068. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2069. SIGNAL_CHECK_FALSE("text_changed");
  2070. SIGNAL_CHECK_FALSE("lines_edited_from");
  2071. // Left at col 0 should go up a line.
  2072. SEND_GUI_ACTION("ui_text_caret_left");
  2073. CHECK(text_edit->get_viewport()->is_input_handled());
  2074. CHECK(text_edit->get_caret_line() == 0);
  2075. CHECK(text_edit->get_caret_column() == 0);
  2076. CHECK_FALSE(text_edit->has_selection(0));
  2077. CHECK(text_edit->get_caret_line(1) == 1);
  2078. CHECK(text_edit->get_caret_column(1) == 23);
  2079. CHECK_FALSE(text_edit->has_selection(1));
  2080. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2081. SIGNAL_CHECK_FALSE("text_changed");
  2082. SIGNAL_CHECK_FALSE("lines_edited_from");
  2083. }
  2084. SUBCASE("[TextEdit] ui_text_caret_word_right") {
  2085. text_edit->set_text("this is some test text\n\nthis is some test text\n");
  2086. text_edit->set_caret_line(0);
  2087. text_edit->set_caret_column(13);
  2088. text_edit->add_caret(2, 13);
  2089. CHECK(text_edit->get_caret_count() == 2);
  2090. MessageQueue::get_singleton()->flush();
  2091. SIGNAL_DISCARD("text_set");
  2092. SIGNAL_DISCARD("text_changed");
  2093. SIGNAL_DISCARD("lines_edited_from");
  2094. SIGNAL_DISCARD("caret_changed");
  2095. // Shift should select.
  2096. #ifdef MACOS_ENABLED
  2097. SEND_GUI_KEY_EVENT(Key::RIGHT | KeyModifierMask::ALT | KeyModifierMask::SHIFT);
  2098. #else
  2099. SEND_GUI_KEY_EVENT(Key::RIGHT | KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT);
  2100. #endif
  2101. CHECK(text_edit->get_viewport()->is_input_handled());
  2102. CHECK(text_edit->get_caret_line() == 0);
  2103. CHECK(text_edit->get_caret_column() == 17);
  2104. CHECK(text_edit->get_selected_text(0) == "test");
  2105. CHECK(text_edit->has_selection(0));
  2106. CHECK(text_edit->get_caret_line(1) == 2);
  2107. CHECK(text_edit->get_caret_column(1) == 17);
  2108. CHECK(text_edit->get_selected_text(1) == "test");
  2109. CHECK(text_edit->has_selection(1));
  2110. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2111. SIGNAL_CHECK_FALSE("text_changed");
  2112. SIGNAL_CHECK_FALSE("lines_edited_from");
  2113. // Should still move caret with selection.
  2114. SEND_GUI_ACTION("ui_text_caret_word_right");
  2115. CHECK(text_edit->get_viewport()->is_input_handled());
  2116. CHECK(text_edit->get_caret_line() == 0);
  2117. CHECK(text_edit->get_caret_column() == 22);
  2118. CHECK_FALSE(text_edit->has_selection(0));
  2119. CHECK(text_edit->get_caret_line(1) == 2);
  2120. CHECK(text_edit->get_caret_column(1) == 22);
  2121. CHECK_FALSE(text_edit->has_selection(1));
  2122. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2123. SIGNAL_CHECK_FALSE("text_changed");
  2124. SIGNAL_CHECK_FALSE("lines_edited_from");
  2125. // Normal word right.
  2126. SEND_GUI_ACTION("ui_text_caret_word_right");
  2127. CHECK(text_edit->get_viewport()->is_input_handled());
  2128. CHECK(text_edit->get_caret_line() == 1);
  2129. CHECK(text_edit->get_caret_column() == 0);
  2130. CHECK_FALSE(text_edit->has_selection(0));
  2131. CHECK(text_edit->get_caret_line(1) == 3);
  2132. CHECK(text_edit->get_caret_column(1) == 0);
  2133. CHECK_FALSE(text_edit->has_selection(1));
  2134. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2135. SIGNAL_CHECK_FALSE("text_changed");
  2136. SIGNAL_CHECK_FALSE("lines_edited_from");
  2137. }
  2138. SUBCASE("[TextEdit] ui_text_caret_right") {
  2139. text_edit->set_text("this is some test text\n\nthis is some test text\n");
  2140. text_edit->set_caret_line(0);
  2141. text_edit->set_caret_column(16);
  2142. text_edit->select(0, 16, 0, 20);
  2143. text_edit->add_caret(2, 16);
  2144. text_edit->select(2, 16, 2, 20, 1);
  2145. CHECK(text_edit->get_caret_count() == 2);
  2146. MessageQueue::get_singleton()->flush();
  2147. SIGNAL_DISCARD("text_set");
  2148. SIGNAL_DISCARD("text_changed");
  2149. SIGNAL_DISCARD("lines_edited_from");
  2150. SIGNAL_DISCARD("caret_changed");
  2151. // Normal right should deselect and place at selection start.
  2152. SEND_GUI_ACTION("ui_text_caret_right");
  2153. CHECK(text_edit->get_viewport()->is_input_handled());
  2154. CHECK(text_edit->get_caret_line() == 0);
  2155. CHECK(text_edit->get_caret_column() == 20);
  2156. CHECK_FALSE(text_edit->has_selection(0));
  2157. CHECK(text_edit->get_caret_line(1) == 2);
  2158. CHECK(text_edit->get_caret_column(1) == 20);
  2159. CHECK_FALSE(text_edit->has_selection(1));
  2160. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2161. SIGNAL_CHECK_FALSE("text_changed");
  2162. SIGNAL_CHECK_FALSE("lines_edited_from");
  2163. // With shift should select.
  2164. SEND_GUI_KEY_EVENT(Key::RIGHT | KeyModifierMask::SHIFT);
  2165. CHECK(text_edit->get_viewport()->is_input_handled());
  2166. CHECK(text_edit->get_caret_line() == 0);
  2167. CHECK(text_edit->get_caret_column() == 21);
  2168. CHECK(text_edit->get_selected_text(0) == "x");
  2169. CHECK(text_edit->has_selection(0));
  2170. CHECK(text_edit->get_caret_line(1) == 2);
  2171. CHECK(text_edit->get_caret_column(1) == 21);
  2172. CHECK(text_edit->get_selected_text(1) == "x");
  2173. CHECK(text_edit->has_selection(1));
  2174. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2175. SIGNAL_CHECK_FALSE("text_changed");
  2176. SIGNAL_CHECK_FALSE("lines_edited_from");
  2177. // All ready at select right, should only deselect.
  2178. SEND_GUI_ACTION("ui_text_caret_right");
  2179. CHECK(text_edit->get_viewport()->is_input_handled());
  2180. CHECK(text_edit->get_caret_line() == 0);
  2181. CHECK(text_edit->get_caret_column() == 21);
  2182. CHECK_FALSE(text_edit->has_selection(0));
  2183. CHECK(text_edit->get_caret_line(1) == 2);
  2184. CHECK(text_edit->get_caret_column(1) == 21);
  2185. CHECK_FALSE(text_edit->has_selection(1));
  2186. SIGNAL_CHECK_FALSE("caret_changed");
  2187. SIGNAL_CHECK_FALSE("text_changed");
  2188. SIGNAL_CHECK_FALSE("lines_edited_from");
  2189. // Normal right.
  2190. SEND_GUI_ACTION("ui_text_caret_right");
  2191. CHECK(text_edit->get_viewport()->is_input_handled());
  2192. CHECK(text_edit->get_caret_line() == 0);
  2193. CHECK(text_edit->get_caret_column() == 22);
  2194. CHECK_FALSE(text_edit->has_selection(0));
  2195. CHECK(text_edit->get_caret_line(1) == 2);
  2196. CHECK(text_edit->get_caret_column(1) == 22);
  2197. CHECK_FALSE(text_edit->has_selection(1));
  2198. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2199. SIGNAL_CHECK_FALSE("text_changed");
  2200. SIGNAL_CHECK_FALSE("lines_edited_from");
  2201. // Right at end col should go down a line.
  2202. SEND_GUI_ACTION("ui_text_caret_right");
  2203. CHECK(text_edit->get_viewport()->is_input_handled());
  2204. CHECK(text_edit->get_caret_line() == 1);
  2205. CHECK(text_edit->get_caret_column() == 0);
  2206. CHECK_FALSE(text_edit->has_selection(0));
  2207. CHECK(text_edit->get_caret_line(1) == 3);
  2208. CHECK(text_edit->get_caret_column(1) == 0);
  2209. CHECK_FALSE(text_edit->has_selection(1));
  2210. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2211. SIGNAL_CHECK_FALSE("text_changed");
  2212. SIGNAL_CHECK_FALSE("lines_edited_from");
  2213. }
  2214. SUBCASE("[TextEdit] ui_text_caret_up") {
  2215. text_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY);
  2216. text_edit->set_size(Size2(110, 100));
  2217. text_edit->set_text("this is some\nother test\nlines\ngo here\nthis is some\nother test\nlines\ngo here");
  2218. text_edit->set_caret_line(3);
  2219. text_edit->set_caret_column(7);
  2220. text_edit->add_caret(7, 7);
  2221. CHECK(text_edit->get_caret_count() == 2);
  2222. MessageQueue::get_singleton()->flush();
  2223. CHECK(text_edit->is_line_wrapped(0));
  2224. SIGNAL_DISCARD("text_set");
  2225. SIGNAL_DISCARD("text_changed");
  2226. SIGNAL_DISCARD("lines_edited_from");
  2227. SIGNAL_DISCARD("caret_changed");
  2228. // Select + up should select everything to the left on that line.
  2229. SEND_GUI_KEY_EVENT(Key::UP | KeyModifierMask::SHIFT);
  2230. CHECK(text_edit->get_viewport()->is_input_handled());
  2231. CHECK(text_edit->get_caret_line() == 2);
  2232. CHECK(text_edit->get_caret_column() == 5);
  2233. CHECK(text_edit->get_selected_text(0) == "\ngo here");
  2234. CHECK(text_edit->has_selection(0));
  2235. CHECK(text_edit->get_caret_line(1) == 6);
  2236. CHECK(text_edit->get_caret_column(1) == 5);
  2237. CHECK(text_edit->get_selected_text(1) == "\ngo here");
  2238. CHECK(text_edit->has_selection(1));
  2239. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2240. SIGNAL_CHECK_FALSE("text_changed");
  2241. SIGNAL_CHECK_FALSE("lines_edited_from");
  2242. // Should deselect and move up.
  2243. SEND_GUI_ACTION("ui_text_caret_up");
  2244. CHECK(text_edit->get_viewport()->is_input_handled());
  2245. CHECK(text_edit->get_caret_line() == 1);
  2246. CHECK(text_edit->get_caret_column() == 8);
  2247. CHECK_FALSE(text_edit->has_selection(0));
  2248. CHECK(text_edit->get_caret_line(1) == 5);
  2249. CHECK(text_edit->get_caret_column(1) == 8);
  2250. CHECK_FALSE(text_edit->has_selection(1));
  2251. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2252. SIGNAL_CHECK_FALSE("text_changed");
  2253. SIGNAL_CHECK_FALSE("lines_edited_from");
  2254. // Normal up over wrapped line.
  2255. SEND_GUI_ACTION("ui_text_caret_up");
  2256. CHECK(text_edit->get_viewport()->is_input_handled());
  2257. CHECK(text_edit->get_caret_line() == 0);
  2258. CHECK(text_edit->get_caret_column() == 12);
  2259. CHECK_FALSE(text_edit->has_selection(0));
  2260. CHECK(text_edit->get_caret_line(1) == 4);
  2261. CHECK(text_edit->get_caret_column(1) == 12);
  2262. CHECK_FALSE(text_edit->has_selection(1));
  2263. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2264. SIGNAL_CHECK_FALSE("text_changed");
  2265. SIGNAL_CHECK_FALSE("lines_edited_from");
  2266. text_edit->set_caret_column(12, false);
  2267. // Normal up over wrapped line to line 0.
  2268. SEND_GUI_ACTION("ui_text_caret_up");
  2269. CHECK(text_edit->get_viewport()->is_input_handled());
  2270. CHECK(text_edit->get_caret_line() == 0);
  2271. CHECK(text_edit->get_caret_column() == 7);
  2272. CHECK_FALSE(text_edit->has_selection(0));
  2273. CHECK(text_edit->get_caret_line(1) == 4);
  2274. CHECK(text_edit->get_caret_column(1) == 7);
  2275. CHECK_FALSE(text_edit->has_selection(1));
  2276. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2277. SIGNAL_CHECK_FALSE("text_changed");
  2278. SIGNAL_CHECK_FALSE("lines_edited_from");
  2279. }
  2280. SUBCASE("[TextEdit] ui_text_caret_down") {
  2281. text_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY);
  2282. text_edit->set_size(Size2(110, 100));
  2283. text_edit->set_text("go here\nlines\nother test\nthis is some\ngo here\nlines\nother test\nthis is some");
  2284. text_edit->set_caret_line(0);
  2285. text_edit->set_caret_column(7);
  2286. text_edit->add_caret(4, 7);
  2287. CHECK(text_edit->get_caret_count() == 2);
  2288. MessageQueue::get_singleton()->flush();
  2289. CHECK(text_edit->is_line_wrapped(3));
  2290. SIGNAL_DISCARD("text_set");
  2291. SIGNAL_DISCARD("text_changed");
  2292. SIGNAL_DISCARD("lines_edited_from");
  2293. SIGNAL_DISCARD("caret_changed");
  2294. // Select + down should select everything to the right on that line.
  2295. SEND_GUI_KEY_EVENT(Key::DOWN | KeyModifierMask::SHIFT);
  2296. CHECK(text_edit->get_viewport()->is_input_handled());
  2297. CHECK(text_edit->get_caret_line() == 1);
  2298. CHECK(text_edit->get_caret_column() == 5);
  2299. CHECK(text_edit->get_selected_text(0) == "\nlines");
  2300. CHECK(text_edit->has_selection(0));
  2301. CHECK(text_edit->get_caret_line(1) == 5);
  2302. CHECK(text_edit->get_caret_column(1) == 5);
  2303. CHECK(text_edit->get_selected_text(1) == "\nlines");
  2304. CHECK(text_edit->has_selection(1));
  2305. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2306. SIGNAL_CHECK_FALSE("text_changed");
  2307. SIGNAL_CHECK_FALSE("lines_edited_from");
  2308. // Should deselect and move down.
  2309. SEND_GUI_ACTION("ui_text_caret_down");
  2310. CHECK(text_edit->get_viewport()->is_input_handled());
  2311. CHECK(text_edit->get_caret_line() == 2);
  2312. CHECK(text_edit->get_caret_column() == 8);
  2313. CHECK_FALSE(text_edit->has_selection(0));
  2314. CHECK(text_edit->get_caret_line(1) == 6);
  2315. CHECK(text_edit->get_caret_column(1) == 8);
  2316. CHECK_FALSE(text_edit->has_selection(1));
  2317. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2318. SIGNAL_CHECK_FALSE("text_changed");
  2319. SIGNAL_CHECK_FALSE("lines_edited_from");
  2320. // Normal down over wrapped line.
  2321. SEND_GUI_ACTION("ui_text_caret_down");
  2322. CHECK(text_edit->get_viewport()->is_input_handled());
  2323. CHECK(text_edit->get_caret_line() == 3);
  2324. CHECK(text_edit->get_caret_column() == 7);
  2325. CHECK_FALSE(text_edit->has_selection(0));
  2326. CHECK(text_edit->get_caret_line(1) == 7);
  2327. CHECK(text_edit->get_caret_column(1) == 7);
  2328. CHECK_FALSE(text_edit->has_selection(1));
  2329. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2330. SIGNAL_CHECK_FALSE("text_changed");
  2331. SIGNAL_CHECK_FALSE("lines_edited_from");
  2332. text_edit->set_caret_column(7, false);
  2333. // Normal down over wrapped line to last wrapped line.
  2334. SEND_GUI_ACTION("ui_text_caret_down");
  2335. CHECK(text_edit->get_viewport()->is_input_handled());
  2336. CHECK(text_edit->get_caret_line() == 3);
  2337. CHECK(text_edit->get_caret_column() == 12);
  2338. CHECK_FALSE(text_edit->has_selection(0));
  2339. CHECK(text_edit->get_caret_line(1) == 7);
  2340. CHECK(text_edit->get_caret_column(1) == 12);
  2341. CHECK_FALSE(text_edit->has_selection(1));
  2342. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2343. SIGNAL_CHECK_FALSE("text_changed");
  2344. SIGNAL_CHECK_FALSE("lines_edited_from");
  2345. }
  2346. SUBCASE("[TextEdit] ui_text_caret_document_start") {
  2347. text_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY);
  2348. text_edit->set_size(Size2(110, 100));
  2349. text_edit->set_text("this is some\nother test\nlines\ngo here");
  2350. text_edit->set_caret_line(4);
  2351. text_edit->set_caret_column(7);
  2352. text_edit->add_caret(3, 2);
  2353. CHECK(text_edit->get_caret_count() == 2);
  2354. MessageQueue::get_singleton()->flush();
  2355. CHECK(text_edit->is_line_wrapped(0));
  2356. SIGNAL_DISCARD("text_set");
  2357. SIGNAL_DISCARD("text_changed");
  2358. SIGNAL_DISCARD("lines_edited_from");
  2359. SIGNAL_DISCARD("caret_changed");
  2360. #ifdef MACOS_ENABLED
  2361. SEND_GUI_KEY_EVENT(Key::UP | KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT);
  2362. #else
  2363. SEND_GUI_KEY_EVENT(Key::HOME | KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT);
  2364. #endif
  2365. CHECK(text_edit->get_viewport()->is_input_handled());
  2366. CHECK(text_edit->get_text() == "this is some\nother test\nlines\ngo here");
  2367. CHECK(text_edit->get_caret_line() == 0);
  2368. CHECK(text_edit->get_caret_column() == 0);
  2369. CHECK(text_edit->get_selected_text() == "this is some\nother test\nlines\ngo here");
  2370. CHECK(text_edit->has_selection());
  2371. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2372. SIGNAL_CHECK_FALSE("text_changed");
  2373. SIGNAL_CHECK_FALSE("lines_edited_from");
  2374. CHECK(text_edit->get_caret_count() == 1);
  2375. SEND_GUI_ACTION("ui_text_caret_document_start");
  2376. CHECK(text_edit->get_viewport()->is_input_handled());
  2377. CHECK(text_edit->get_text() == "this is some\nother test\nlines\ngo here");
  2378. CHECK(text_edit->get_caret_line() == 0);
  2379. CHECK(text_edit->get_caret_column() == 0);
  2380. CHECK_FALSE(text_edit->has_selection());
  2381. SIGNAL_CHECK_FALSE("caret_changed");
  2382. SIGNAL_CHECK_FALSE("text_changed");
  2383. SIGNAL_CHECK_FALSE("lines_edited_from");
  2384. }
  2385. SUBCASE("[TextEdit] ui_text_caret_document_end") {
  2386. text_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY);
  2387. text_edit->set_size(Size2(110, 100));
  2388. text_edit->set_text("go here\nlines\nother test\nthis is some");
  2389. text_edit->set_caret_line(0);
  2390. text_edit->set_caret_column(0);
  2391. text_edit->add_caret(1, 0);
  2392. CHECK(text_edit->get_caret_count() == 2);
  2393. MessageQueue::get_singleton()->flush();
  2394. CHECK(text_edit->is_line_wrapped(3));
  2395. SIGNAL_DISCARD("text_set");
  2396. SIGNAL_DISCARD("text_changed");
  2397. SIGNAL_DISCARD("lines_edited_from");
  2398. SIGNAL_DISCARD("caret_changed");
  2399. #ifdef MACOS_ENABLED
  2400. SEND_GUI_KEY_EVENT(Key::DOWN | KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT);
  2401. #else
  2402. SEND_GUI_KEY_EVENT(Key::END | KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT);
  2403. #endif
  2404. CHECK(text_edit->get_viewport()->is_input_handled());
  2405. CHECK(text_edit->get_text() == "go here\nlines\nother test\nthis is some");
  2406. CHECK(text_edit->get_caret_line() == 3);
  2407. CHECK(text_edit->get_caret_column() == 12);
  2408. CHECK(text_edit->get_selected_text() == "go here\nlines\nother test\nthis is some");
  2409. CHECK(text_edit->has_selection());
  2410. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2411. SIGNAL_CHECK_FALSE("text_changed");
  2412. SIGNAL_CHECK_FALSE("lines_edited_from");
  2413. CHECK(text_edit->get_caret_count() == 1);
  2414. SEND_GUI_ACTION("ui_text_caret_document_end");
  2415. CHECK(text_edit->get_viewport()->is_input_handled());
  2416. CHECK(text_edit->get_text() == "go here\nlines\nother test\nthis is some");
  2417. CHECK(text_edit->get_caret_line() == 3);
  2418. CHECK(text_edit->get_caret_column() == 12);
  2419. CHECK_FALSE(text_edit->has_selection());
  2420. SIGNAL_CHECK_FALSE("caret_changed");
  2421. SIGNAL_CHECK_FALSE("text_changed");
  2422. SIGNAL_CHECK_FALSE("lines_edited_from");
  2423. }
  2424. SUBCASE("[TextEdit] ui_text_caret_line_start") {
  2425. text_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY);
  2426. text_edit->set_size(Size2(110, 100));
  2427. text_edit->set_text(" this is some\n this is some");
  2428. text_edit->set_caret_line(0);
  2429. text_edit->set_caret_column(text_edit->get_line(0).length());
  2430. text_edit->add_caret(1, text_edit->get_line(1).length());
  2431. CHECK(text_edit->get_caret_count() == 2);
  2432. MessageQueue::get_singleton()->flush();
  2433. CHECK(text_edit->is_line_wrapped(0));
  2434. SIGNAL_DISCARD("text_set");
  2435. SIGNAL_DISCARD("text_changed");
  2436. SIGNAL_DISCARD("lines_edited_from");
  2437. SIGNAL_DISCARD("caret_changed");
  2438. #ifdef MACOS_ENABLED
  2439. SEND_GUI_KEY_EVENT(Key::LEFT | KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT);
  2440. #else
  2441. SEND_GUI_KEY_EVENT(Key::HOME | KeyModifierMask::SHIFT);
  2442. #endif
  2443. CHECK(text_edit->get_viewport()->is_input_handled());
  2444. CHECK(text_edit->get_caret_line() == 0);
  2445. CHECK(text_edit->get_caret_column() == 10);
  2446. CHECK(text_edit->has_selection(0));
  2447. CHECK(text_edit->get_selected_text(0) == "some");
  2448. CHECK(text_edit->get_caret_line(1) == 1);
  2449. CHECK(text_edit->get_caret_column(1) == 10);
  2450. CHECK(text_edit->has_selection(1));
  2451. CHECK(text_edit->get_selected_text(1) == "some");
  2452. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2453. SIGNAL_CHECK_FALSE("text_changed");
  2454. SIGNAL_CHECK_FALSE("lines_edited_from");
  2455. SEND_GUI_ACTION("ui_text_caret_line_start");
  2456. CHECK(text_edit->get_viewport()->is_input_handled());
  2457. CHECK(text_edit->get_caret_line() == 0);
  2458. CHECK(text_edit->get_caret_column() == 2);
  2459. CHECK_FALSE(text_edit->has_selection(0));
  2460. CHECK(text_edit->get_caret_line(1) == 1);
  2461. CHECK(text_edit->get_caret_column(1) == 2);
  2462. CHECK_FALSE(text_edit->has_selection(1));
  2463. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2464. SIGNAL_CHECK_FALSE("text_changed");
  2465. SIGNAL_CHECK_FALSE("lines_edited_from");
  2466. SEND_GUI_ACTION("ui_text_caret_line_start");
  2467. CHECK(text_edit->get_viewport()->is_input_handled());
  2468. CHECK(text_edit->get_caret_line() == 0);
  2469. CHECK(text_edit->get_caret_column() == 0);
  2470. CHECK_FALSE(text_edit->has_selection(0));
  2471. CHECK(text_edit->get_caret_line(1) == 1);
  2472. CHECK(text_edit->get_caret_column(1) == 0);
  2473. CHECK_FALSE(text_edit->has_selection(1));
  2474. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2475. SIGNAL_CHECK_FALSE("text_changed");
  2476. SIGNAL_CHECK_FALSE("lines_edited_from");
  2477. SEND_GUI_ACTION("ui_text_caret_line_start");
  2478. CHECK(text_edit->get_viewport()->is_input_handled());
  2479. CHECK(text_edit->get_caret_line() == 0);
  2480. CHECK(text_edit->get_caret_column() == 2);
  2481. CHECK_FALSE(text_edit->has_selection(0));
  2482. CHECK(text_edit->get_caret_line(1) == 1);
  2483. CHECK(text_edit->get_caret_column(1) == 2);
  2484. CHECK_FALSE(text_edit->has_selection(1));
  2485. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2486. SIGNAL_CHECK_FALSE("text_changed");
  2487. SIGNAL_CHECK_FALSE("lines_edited_from");
  2488. }
  2489. SUBCASE("[TextEdit] ui_text_caret_line_end") {
  2490. text_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY);
  2491. text_edit->set_size(Size2(110, 100));
  2492. text_edit->set_text(" this is some\n this is some");
  2493. text_edit->set_caret_line(0);
  2494. text_edit->set_caret_column(0);
  2495. text_edit->add_caret(1, 0);
  2496. CHECK(text_edit->get_caret_count() == 2);
  2497. MessageQueue::get_singleton()->flush();
  2498. CHECK(text_edit->is_line_wrapped(0));
  2499. SIGNAL_DISCARD("text_set");
  2500. SIGNAL_DISCARD("text_changed");
  2501. SIGNAL_DISCARD("lines_edited_from");
  2502. SIGNAL_DISCARD("caret_changed");
  2503. #ifdef MACOS_ENABLED
  2504. SEND_GUI_KEY_EVENT(Key::RIGHT | KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT);
  2505. #else
  2506. SEND_GUI_KEY_EVENT(Key::END | KeyModifierMask::SHIFT);
  2507. #endif
  2508. CHECK(text_edit->get_viewport()->is_input_handled());
  2509. CHECK(text_edit->get_caret_line() == 0);
  2510. CHECK(text_edit->get_caret_column() == 9);
  2511. CHECK(text_edit->has_selection(0));
  2512. CHECK(text_edit->get_selected_text(0) == " this is");
  2513. CHECK(text_edit->get_caret_line(1) == 1);
  2514. CHECK(text_edit->get_caret_column(1) == 9);
  2515. CHECK(text_edit->has_selection(1));
  2516. CHECK(text_edit->get_selected_text(1) == " this is");
  2517. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2518. SIGNAL_CHECK_FALSE("text_changed");
  2519. SIGNAL_CHECK_FALSE("lines_edited_from");
  2520. SEND_GUI_ACTION("ui_text_caret_line_end");
  2521. CHECK(text_edit->get_viewport()->is_input_handled());
  2522. CHECK(text_edit->get_caret_line() == 0);
  2523. CHECK(text_edit->get_caret_column() == text_edit->get_line(0).length());
  2524. CHECK_FALSE(text_edit->has_selection(0));
  2525. CHECK(text_edit->get_viewport()->is_input_handled());
  2526. CHECK(text_edit->get_caret_line(1) == 1);
  2527. CHECK(text_edit->get_caret_column(1) == text_edit->get_line(1).length());
  2528. CHECK_FALSE(text_edit->has_selection(1));
  2529. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2530. SIGNAL_CHECK_FALSE("text_changed");
  2531. SIGNAL_CHECK_FALSE("lines_edited_from");
  2532. }
  2533. SUBCASE("[TextEdit] unicode") {
  2534. text_edit->set_text("\n");
  2535. text_edit->set_caret_line(0);
  2536. text_edit->set_caret_column(0);
  2537. text_edit->add_caret(1, 0);
  2538. CHECK(text_edit->get_caret_count() == 2);
  2539. text_edit->insert_text_at_caret("a");
  2540. MessageQueue::get_singleton()->flush();
  2541. SIGNAL_DISCARD("text_set");
  2542. SIGNAL_DISCARD("text_changed");
  2543. SIGNAL_DISCARD("lines_edited_from");
  2544. SIGNAL_DISCARD("caret_changed");
  2545. // For the second caret.
  2546. Array args2;
  2547. args2.push_back(1);
  2548. args2.push_back(1);
  2549. lines_edited_args.push_front(args2);
  2550. SEND_GUI_KEY_EVENT(Key::A);
  2551. CHECK(text_edit->get_viewport()->is_input_handled());
  2552. CHECK(text_edit->get_text() == "aA\naA");
  2553. CHECK(text_edit->get_caret_column() == 2);
  2554. CHECK(text_edit->get_caret_column(1) == 2);
  2555. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2556. SIGNAL_CHECK("text_changed", empty_signal_args);
  2557. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  2558. text_edit->set_editable(false);
  2559. SEND_GUI_KEY_EVENT(Key::A);
  2560. CHECK_FALSE(text_edit->get_viewport()->is_input_handled()); // Should this be handled?
  2561. CHECK(text_edit->get_text() == "aA\naA");
  2562. CHECK(text_edit->get_caret_column() == 2);
  2563. CHECK(text_edit->get_caret_column(1) == 2);
  2564. SIGNAL_CHECK_FALSE("caret_changed");
  2565. SIGNAL_CHECK_FALSE("text_changed");
  2566. SIGNAL_CHECK_FALSE("lines_edited_from");
  2567. text_edit->set_editable(true);
  2568. lines_edited_args.push_back(lines_edited_args[1].duplicate());
  2569. lines_edited_args.push_front(args2.duplicate());
  2570. text_edit->select(0, 0, 0, 1);
  2571. text_edit->select(1, 0, 1, 1, 1);
  2572. SEND_GUI_KEY_EVENT(Key::B);
  2573. CHECK(text_edit->get_viewport()->is_input_handled());
  2574. CHECK(text_edit->get_text() == "BA\nBA");
  2575. CHECK(text_edit->get_caret_column() == 1);
  2576. CHECK(text_edit->get_caret_column(1) == 1);
  2577. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2578. SIGNAL_CHECK("text_changed", empty_signal_args);
  2579. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  2580. SEND_GUI_ACTION("ui_text_toggle_insert_mode");
  2581. CHECK(text_edit->is_overtype_mode_enabled());
  2582. SEND_GUI_KEY_EVENT(Key::B);
  2583. CHECK(text_edit->get_viewport()->is_input_handled());
  2584. CHECK(text_edit->get_text() == "BB\nBB");
  2585. CHECK(text_edit->get_caret_column() == 2);
  2586. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2587. SIGNAL_CHECK("text_changed", empty_signal_args);
  2588. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  2589. text_edit->select(0, 0, 0, 1);
  2590. text_edit->select(1, 0, 1, 1, 1);
  2591. SEND_GUI_KEY_EVENT(Key::A);
  2592. CHECK(text_edit->get_viewport()->is_input_handled());
  2593. CHECK(text_edit->get_text() == "AB\nAB");
  2594. CHECK(text_edit->get_caret_column() == 1);
  2595. CHECK(text_edit->get_caret_column(1) == 1);
  2596. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2597. SIGNAL_CHECK("text_changed", empty_signal_args);
  2598. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  2599. text_edit->set_overtype_mode_enabled(false);
  2600. CHECK_FALSE(text_edit->is_overtype_mode_enabled());
  2601. lines_edited_args.remove_at(0);
  2602. lines_edited_args.remove_at(1);
  2603. SEND_GUI_KEY_EVENT(Key::TAB);
  2604. CHECK(text_edit->get_viewport()->is_input_handled());
  2605. CHECK(text_edit->get_text() == "A\tB\nA\tB");
  2606. CHECK(text_edit->get_caret_column() == 2);
  2607. CHECK(text_edit->get_caret_column(1) == 2);
  2608. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2609. SIGNAL_CHECK("text_changed", empty_signal_args);
  2610. SIGNAL_CHECK("lines_edited_from", lines_edited_args);
  2611. }
  2612. SIGNAL_UNWATCH(text_edit, "text_set");
  2613. SIGNAL_UNWATCH(text_edit, "text_changed");
  2614. SIGNAL_UNWATCH(text_edit, "lines_edited_from");
  2615. SIGNAL_UNWATCH(text_edit, "caret_changed");
  2616. }
  2617. memdelete(text_edit);
  2618. }
  2619. TEST_CASE("[SceneTree][TextEdit] context menu") {
  2620. TextEdit *text_edit = memnew(TextEdit);
  2621. SceneTree::get_singleton()->get_root()->add_child(text_edit);
  2622. text_edit->set_size(Size2(800, 200));
  2623. text_edit->set_line(0, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vasius mattis leo, sed porta ex lacinia bibendum. Nunc bibendum pellentesque.");
  2624. MessageQueue::get_singleton()->flush();
  2625. text_edit->set_context_menu_enabled(false);
  2626. CHECK_FALSE(text_edit->is_context_menu_enabled());
  2627. CHECK_FALSE(text_edit->is_menu_visible());
  2628. SEND_GUI_MOUSE_BUTTON_EVENT(Point2i(600, 10), MouseButton::RIGHT, MouseButtonMask::RIGHT, Key::NONE);
  2629. CHECK_FALSE(text_edit->is_menu_visible());
  2630. text_edit->set_context_menu_enabled(true);
  2631. CHECK(text_edit->is_context_menu_enabled());
  2632. CHECK_FALSE(text_edit->is_menu_visible());
  2633. SEND_GUI_MOUSE_BUTTON_EVENT(Point2i(700, 10), MouseButton::RIGHT, MouseButtonMask::RIGHT, Key::NONE);
  2634. CHECK(text_edit->is_menu_visible());
  2635. memdelete(text_edit);
  2636. }
  2637. TEST_CASE("[SceneTree][TextEdit] versioning") {
  2638. TextEdit *text_edit = memnew(TextEdit);
  2639. SceneTree::get_singleton()->get_root()->add_child(text_edit);
  2640. // Action undo / redo states are tested in the action test e.g selection_delete.
  2641. CHECK_FALSE(text_edit->has_undo());
  2642. CHECK_FALSE(text_edit->has_redo());
  2643. CHECK(text_edit->get_version() == 0);
  2644. CHECK(text_edit->get_saved_version() == 0);
  2645. text_edit->begin_complex_operation();
  2646. text_edit->begin_complex_operation();
  2647. text_edit->begin_complex_operation();
  2648. text_edit->insert_text_at_caret("test");
  2649. CHECK(text_edit->get_version() == 1);
  2650. CHECK(text_edit->get_saved_version() == 0);
  2651. CHECK(text_edit->has_undo());
  2652. CHECK_FALSE(text_edit->has_redo());
  2653. text_edit->end_complex_operation();
  2654. // Can undo and redo mid op.
  2655. text_edit->insert_text_at_caret(" nested");
  2656. CHECK(text_edit->get_version() == 2);
  2657. CHECK(text_edit->get_saved_version() == 0);
  2658. CHECK(text_edit->has_undo());
  2659. CHECK_FALSE(text_edit->has_redo());
  2660. text_edit->undo();
  2661. CHECK(text_edit->has_redo());
  2662. text_edit->redo();
  2663. text_edit->end_complex_operation();
  2664. text_edit->insert_text_at_caret(" ops");
  2665. CHECK(text_edit->get_version() == 3);
  2666. CHECK(text_edit->get_saved_version() == 0);
  2667. CHECK(text_edit->has_undo());
  2668. CHECK_FALSE(text_edit->has_redo());
  2669. text_edit->end_complex_operation();
  2670. text_edit->tag_saved_version();
  2671. CHECK(text_edit->get_saved_version() == 3);
  2672. text_edit->undo();
  2673. CHECK(text_edit->get_line(0) == "");
  2674. CHECK(text_edit->get_version() == 0);
  2675. CHECK(text_edit->get_saved_version() == 3);
  2676. CHECK_FALSE(text_edit->has_undo());
  2677. CHECK(text_edit->has_redo());
  2678. text_edit->redo();
  2679. CHECK(text_edit->get_line(0) == "test ops nested");
  2680. CHECK(text_edit->get_version() == 3);
  2681. CHECK(text_edit->get_saved_version() == 3);
  2682. CHECK(text_edit->has_undo());
  2683. CHECK_FALSE(text_edit->has_redo());
  2684. text_edit->clear_undo_history();
  2685. CHECK_FALSE(text_edit->has_undo());
  2686. CHECK_FALSE(text_edit->has_redo());
  2687. CHECK(text_edit->get_version() == 3); // Should this be cleared?
  2688. CHECK(text_edit->get_saved_version() == 0);
  2689. memdelete(text_edit);
  2690. }
  2691. TEST_CASE("[SceneTree][TextEdit] search") {
  2692. TextEdit *text_edit = memnew(TextEdit);
  2693. SceneTree::get_singleton()->get_root()->add_child(text_edit);
  2694. text_edit->set_text("hay needle, hay\nHAY NEEDLE, HAY");
  2695. int length = text_edit->get_line(1).length();
  2696. CHECK(text_edit->search("test", 0, 0, 0) == Point2i(-1, -1));
  2697. CHECK(text_edit->search("test", TextEdit::SEARCH_MATCH_CASE, 0, 0) == Point2i(-1, -1));
  2698. CHECK(text_edit->search("test", TextEdit::SEARCH_WHOLE_WORDS, 0, 0) == Point2i(-1, -1));
  2699. CHECK(text_edit->search("test", TextEdit::SEARCH_BACKWARDS, 0, 0) == Point2i(-1, -1));
  2700. CHECK(text_edit->search("test", 0, 1, length) == Point2i(-1, -1));
  2701. CHECK(text_edit->search("test", TextEdit::SEARCH_MATCH_CASE, 1, length) == Point2i(-1, -1));
  2702. CHECK(text_edit->search("test", TextEdit::SEARCH_WHOLE_WORDS, 1, length) == Point2i(-1, -1));
  2703. CHECK(text_edit->search("test", TextEdit::SEARCH_BACKWARDS, 1, length) == Point2i(-1, -1));
  2704. CHECK(text_edit->search("needle", 0, 0, 0) == Point2i(4, 0));
  2705. CHECK(text_edit->search("needle", 0, 1, length) == Point2i(4, 0));
  2706. CHECK(text_edit->search("needle", 0, 0, 5) == Point2i(4, 1));
  2707. CHECK(text_edit->search("needle", TextEdit::SEARCH_BACKWARDS, 0, 0) == Point2i(4, 1));
  2708. CHECK(text_edit->search("needle", TextEdit::SEARCH_BACKWARDS, 1, 5) == Point2i(4, 1));
  2709. CHECK(text_edit->search("needle", TextEdit::SEARCH_BACKWARDS, 1, 3) == Point2i(4, 0));
  2710. CHECK(text_edit->search("needle", TextEdit::SEARCH_MATCH_CASE, 0, 0) == Point2i(4, 0));
  2711. CHECK(text_edit->search("needle", TextEdit::SEARCH_MATCH_CASE | TextEdit::SEARCH_BACKWARDS, 0, 0) == Point2i(4, 0));
  2712. CHECK(text_edit->search("needle", TextEdit::SEARCH_WHOLE_WORDS | TextEdit::SEARCH_MATCH_CASE, 0, 0) == Point2i(4, 0));
  2713. CHECK(text_edit->search("needle", TextEdit::SEARCH_WHOLE_WORDS | TextEdit::SEARCH_MATCH_CASE | TextEdit::SEARCH_BACKWARDS, 0, 0) == Point2i(4, 0));
  2714. CHECK(text_edit->search("need", TextEdit::SEARCH_MATCH_CASE, 0, 0) == Point2i(4, 0));
  2715. CHECK(text_edit->search("need", TextEdit::SEARCH_MATCH_CASE | TextEdit::SEARCH_BACKWARDS, 0, 0) == Point2i(4, 0));
  2716. CHECK(text_edit->search("need", TextEdit::SEARCH_WHOLE_WORDS | TextEdit::SEARCH_MATCH_CASE, 0, 0) == Point2i(-1, -1));
  2717. CHECK(text_edit->search("need", TextEdit::SEARCH_WHOLE_WORDS | TextEdit::SEARCH_MATCH_CASE | TextEdit::SEARCH_BACKWARDS, 0, 0) == Point2i(-1, -1));
  2718. ERR_PRINT_OFF;
  2719. CHECK(text_edit->search("", 0, 0, 0) == Point2i(-1, -1));
  2720. CHECK(text_edit->search("needle", 0, -1, 0) == Point2i(-1, -1));
  2721. CHECK(text_edit->search("needle", 0, 0, -1) == Point2i(-1, -1));
  2722. CHECK(text_edit->search("needle", 0, 100, 0) == Point2i(-1, -1));
  2723. CHECK(text_edit->search("needle", 0, 0, 100) == Point2i(-1, -1));
  2724. ERR_PRINT_ON;
  2725. memdelete(text_edit);
  2726. }
  2727. TEST_CASE("[SceneTree][TextEdit] mouse") {
  2728. TextEdit *text_edit = memnew(TextEdit);
  2729. SceneTree::get_singleton()->get_root()->add_child(text_edit);
  2730. text_edit->set_size(Size2(800, 200));
  2731. text_edit->set_line(0, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vasius mattis leo, sed porta ex lacinia bibendum. Nunc bibendum pellentesque.");
  2732. MessageQueue::get_singleton()->flush();
  2733. CHECK(text_edit->get_word_at_pos(text_edit->get_pos_at_line_column(0, 1)) == "Lorem");
  2734. CHECK(text_edit->get_word_at_pos(text_edit->get_pos_at_line_column(0, 9)) == "ipsum");
  2735. ERR_PRINT_OFF;
  2736. CHECK(text_edit->get_pos_at_line_column(0, -1) == Point2i(-1, -1));
  2737. CHECK(text_edit->get_pos_at_line_column(-1, 0) == Point2i(-1, -1));
  2738. CHECK(text_edit->get_pos_at_line_column(-1, -1) == Point2i(-1, -1));
  2739. CHECK(text_edit->get_pos_at_line_column(0, 500) == Point2i(-1, -1));
  2740. CHECK(text_edit->get_pos_at_line_column(2, 0) == Point2i(-1, -1));
  2741. CHECK(text_edit->get_pos_at_line_column(2, 500) == Point2i(-1, -1));
  2742. // Out of view.
  2743. CHECK(text_edit->get_pos_at_line_column(0, text_edit->get_line(0).length() - 1) == Point2i(-1, -1));
  2744. ERR_PRINT_ON;
  2745. // Add method to get drawn column count?
  2746. Point2i start_pos = text_edit->get_pos_at_line_column(0, 0);
  2747. Point2i end_pos = text_edit->get_pos_at_line_column(0, 105);
  2748. CHECK(text_edit->get_line_column_at_pos(Point2i(start_pos.x, start_pos.y)) == Point2i(0, 0));
  2749. CHECK(text_edit->get_line_column_at_pos(Point2i(end_pos.x, end_pos.y)) == Point2i(104, 0));
  2750. // Should this return Point2i(-1, -1) if its also < 0 not just > vis_lines.
  2751. CHECK(text_edit->get_line_column_at_pos(Point2i(end_pos.x - 100, end_pos.y), false) == Point2i(90, 0));
  2752. CHECK(text_edit->get_line_column_at_pos(Point2i(end_pos.x, end_pos.y + 100), false) == Point2i(-1, -1));
  2753. CHECK(text_edit->get_line_column_at_pos(Point2i(end_pos.x - 100, end_pos.y + 100), false) == Point2i(-1, -1));
  2754. CHECK(text_edit->get_line_column_at_pos(Point2i(end_pos.x, end_pos.y - 100), false) == Point2i(104, 0));
  2755. CHECK(text_edit->get_line_column_at_pos(Point2i(end_pos.x - 100, end_pos.y - 100), false) == Point2i(90, 0));
  2756. CHECK(text_edit->get_line_column_at_pos(Point2i(end_pos.x - 100, end_pos.y)) == Point2i(90, 0));
  2757. CHECK(text_edit->get_line_column_at_pos(Point2i(end_pos.x, end_pos.y + 100)) == Point2i(140, 0));
  2758. CHECK(text_edit->get_line_column_at_pos(Point2i(end_pos.x - 100, end_pos.y + 100)) == Point2i(140, 0));
  2759. CHECK(text_edit->get_line_column_at_pos(Point2i(end_pos.x, end_pos.y - 100)) == Point2i(104, 0));
  2760. CHECK(text_edit->get_line_column_at_pos(Point2i(end_pos.x - 100, end_pos.y - 100)) == Point2i(90, 0));
  2761. memdelete(text_edit);
  2762. }
  2763. TEST_CASE("[SceneTree][TextEdit] caret") {
  2764. TextEdit *text_edit = memnew(TextEdit);
  2765. text_edit->set_context_menu_enabled(false); // Prohibit sending InputEvents to the context menu.
  2766. SceneTree::get_singleton()->get_root()->add_child(text_edit);
  2767. text_edit->set_size(Size2(800, 200));
  2768. text_edit->grab_focus();
  2769. text_edit->set_line(0, "ffi");
  2770. text_edit->set_caret_mid_grapheme_enabled(true);
  2771. CHECK(text_edit->is_caret_mid_grapheme_enabled());
  2772. SEND_GUI_ACTION("ui_text_caret_right");
  2773. CHECK(text_edit->get_caret_column() == 1);
  2774. SEND_GUI_ACTION("ui_text_caret_right");
  2775. CHECK(text_edit->get_caret_column() == 2);
  2776. SEND_GUI_ACTION("ui_text_caret_right");
  2777. CHECK(text_edit->get_caret_column() == 3);
  2778. SEND_GUI_ACTION("ui_text_caret_left");
  2779. CHECK(text_edit->get_caret_column() == 2);
  2780. text_edit->set_caret_mid_grapheme_enabled(false);
  2781. CHECK_FALSE(text_edit->is_caret_mid_grapheme_enabled());
  2782. SEND_GUI_ACTION("ui_text_caret_left");
  2783. CHECK(text_edit->get_caret_column() == 0);
  2784. SEND_GUI_ACTION("ui_text_caret_right");
  2785. CHECK(text_edit->get_caret_column() == 3);
  2786. SEND_GUI_ACTION("ui_text_caret_left");
  2787. CHECK(text_edit->get_caret_column() == 0);
  2788. text_edit->set_line(0, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vasius mattis leo, sed porta ex lacinia bibendum. Nunc bibendum pellentesque.");
  2789. for (int i = 0; i < 3; i++) {
  2790. text_edit->insert_line_at(0, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vasius mattis leo, sed porta ex lacinia bibendum. Nunc bibendum pellentesque.");
  2791. }
  2792. MessageQueue::get_singleton()->flush();
  2793. text_edit->set_caret_blink_enabled(false);
  2794. CHECK_FALSE(text_edit->is_caret_blink_enabled());
  2795. text_edit->set_caret_blink_enabled(true);
  2796. CHECK(text_edit->is_caret_blink_enabled());
  2797. text_edit->set_caret_blink_interval(10);
  2798. CHECK(text_edit->get_caret_blink_interval() == 10);
  2799. ERR_PRINT_OFF;
  2800. text_edit->set_caret_blink_interval(-1);
  2801. CHECK(text_edit->get_caret_blink_interval() == 10);
  2802. text_edit->set_caret_blink_interval(0);
  2803. CHECK(text_edit->get_caret_blink_interval() == 10);
  2804. ERR_PRINT_ON;
  2805. text_edit->set_caret_type(TextEdit::CaretType::CARET_TYPE_LINE);
  2806. CHECK(text_edit->get_caret_type() == TextEdit::CaretType::CARET_TYPE_LINE);
  2807. text_edit->set_caret_type(TextEdit::CaretType::CARET_TYPE_BLOCK);
  2808. CHECK(text_edit->get_caret_type() == TextEdit::CaretType::CARET_TYPE_BLOCK);
  2809. text_edit->set_caret_type(TextEdit::CaretType::CARET_TYPE_LINE);
  2810. CHECK(text_edit->get_caret_type() == TextEdit::CaretType::CARET_TYPE_LINE);
  2811. int caret_col = text_edit->get_caret_column();
  2812. text_edit->set_move_caret_on_right_click_enabled(false);
  2813. CHECK_FALSE(text_edit->is_move_caret_on_right_click_enabled());
  2814. SEND_GUI_MOUSE_BUTTON_EVENT(Point2i(100, 1), MouseButton::RIGHT, MouseButtonMask::RIGHT, Key::NONE);
  2815. CHECK(text_edit->get_caret_column() == caret_col);
  2816. text_edit->set_move_caret_on_right_click_enabled(true);
  2817. CHECK(text_edit->is_move_caret_on_right_click_enabled());
  2818. SEND_GUI_MOUSE_BUTTON_EVENT(Point2i(100, 1), MouseButton::RIGHT, MouseButtonMask::RIGHT, Key::NONE);
  2819. CHECK(text_edit->get_caret_column() != caret_col);
  2820. text_edit->set_move_caret_on_right_click_enabled(false);
  2821. CHECK_FALSE(text_edit->is_move_caret_on_right_click_enabled());
  2822. text_edit->set_caret_column(0);
  2823. CHECK(text_edit->get_word_under_caret() == "Lorem");
  2824. text_edit->set_caret_column(4);
  2825. CHECK(text_edit->get_word_under_caret() == "Lorem");
  2826. // Should this work?
  2827. text_edit->set_caret_column(5);
  2828. CHECK(text_edit->get_word_under_caret() == "");
  2829. text_edit->set_caret_column(6);
  2830. CHECK(text_edit->get_word_under_caret() == "");
  2831. text_edit->set_caret_line(1);
  2832. CHECK(text_edit->get_caret_line() == 1);
  2833. text_edit->set_caret_line(-1);
  2834. CHECK(text_edit->get_caret_line() == 0);
  2835. text_edit->set_caret_line(100);
  2836. CHECK(text_edit->get_caret_line() == 3);
  2837. text_edit->set_caret_column(-1);
  2838. CHECK(text_edit->get_caret_column() == 0);
  2839. text_edit->set_caret_column(10000000);
  2840. CHECK(text_edit->get_caret_column() == 141);
  2841. memdelete(text_edit);
  2842. }
  2843. TEST_CASE("[SceneTree][TextEdit] multicaret") {
  2844. TextEdit *text_edit = memnew(TextEdit);
  2845. SceneTree::get_singleton()->get_root()->add_child(text_edit);
  2846. text_edit->set_multiple_carets_enabled(true);
  2847. Array empty_signal_args;
  2848. empty_signal_args.push_back(Array());
  2849. SIGNAL_WATCH(text_edit, "caret_changed");
  2850. text_edit->set_text("this is\nsome test\ntext");
  2851. text_edit->set_caret_line(0);
  2852. text_edit->set_caret_column(0);
  2853. MessageQueue::get_singleton()->flush();
  2854. SIGNAL_DISCARD("caret_changed");
  2855. SUBCASE("[TextEdit] add remove caret") {
  2856. // Overlapping
  2857. CHECK(text_edit->add_caret(0, 0) == -1);
  2858. MessageQueue::get_singleton()->flush();
  2859. SIGNAL_CHECK_FALSE("caret_changed");
  2860. // Selection
  2861. text_edit->select(0, 0, 2, 4);
  2862. CHECK(text_edit->add_caret(0, 0) == -1);
  2863. CHECK(text_edit->add_caret(2, 4) == -1);
  2864. CHECK(text_edit->add_caret(1, 2) == -1);
  2865. // Out of bounds
  2866. CHECK(text_edit->add_caret(-1, 0) == -1);
  2867. CHECK(text_edit->add_caret(5, 0) == -1);
  2868. CHECK(text_edit->add_caret(0, 100) == -1);
  2869. MessageQueue::get_singleton()->flush();
  2870. SIGNAL_CHECK_FALSE("caret_changed");
  2871. CHECK(text_edit->get_caret_count() == 1);
  2872. text_edit->deselect();
  2873. SIGNAL_CHECK_FALSE("caret_changed");
  2874. CHECK(text_edit->add_caret(0, 1) == 1);
  2875. MessageQueue::get_singleton()->flush();
  2876. SIGNAL_CHECK("caret_changed", empty_signal_args);
  2877. CHECK(text_edit->get_caret_count() == 2);
  2878. CHECK(text_edit->get_caret_line(0) == 0);
  2879. CHECK(text_edit->get_caret_column(0) == 0);
  2880. CHECK(text_edit->get_caret_line(1) == 0);
  2881. CHECK(text_edit->get_caret_column(1) == 1);
  2882. ERR_PRINT_OFF;
  2883. text_edit->remove_caret(-1);
  2884. text_edit->remove_caret(5);
  2885. ERR_PRINT_ON;
  2886. CHECK(text_edit->get_caret_count() == 2);
  2887. SIGNAL_CHECK_FALSE("caret_changed");
  2888. text_edit->remove_caret(0);
  2889. SIGNAL_CHECK_FALSE("caret_changed");
  2890. CHECK(text_edit->get_caret_count() == 1);
  2891. CHECK(text_edit->get_caret_line(0) == 0);
  2892. CHECK(text_edit->get_caret_column(0) == 1);
  2893. ERR_PRINT_OFF;
  2894. text_edit->remove_caret(0);
  2895. CHECK(text_edit->get_caret_count() == 1);
  2896. ERR_PRINT_ON;
  2897. }
  2898. SUBCASE("[TextEdit] caret index edit order") {
  2899. Vector<int> caret_index_get_order;
  2900. caret_index_get_order.push_back(1);
  2901. caret_index_get_order.push_back(0);
  2902. CHECK(text_edit->add_caret(1, 0));
  2903. CHECK(text_edit->get_caret_count() == 2);
  2904. CHECK(text_edit->get_caret_index_edit_order() == caret_index_get_order);
  2905. text_edit->remove_secondary_carets();
  2906. text_edit->set_caret_line(1);
  2907. CHECK(text_edit->add_caret(0, 0));
  2908. CHECK(text_edit->get_caret_count() == 2);
  2909. caret_index_get_order.write[0] = 0;
  2910. caret_index_get_order.write[1] = 1;
  2911. CHECK(text_edit->get_caret_index_edit_order() == caret_index_get_order);
  2912. }
  2913. SUBCASE("[TextEdit] add caret at carets") {
  2914. text_edit->remove_secondary_carets();
  2915. text_edit->set_caret_line(1);
  2916. text_edit->set_caret_column(9);
  2917. text_edit->add_caret_at_carets(true);
  2918. CHECK(text_edit->get_caret_count() == 2);
  2919. CHECK(text_edit->get_caret_line(1) == 2);
  2920. CHECK(text_edit->get_caret_column(1) == 4);
  2921. text_edit->add_caret_at_carets(true);
  2922. CHECK(text_edit->get_caret_count() == 2);
  2923. text_edit->add_caret_at_carets(false);
  2924. CHECK(text_edit->get_caret_count() == 3);
  2925. CHECK(text_edit->get_caret_line(2) == 0);
  2926. CHECK(text_edit->get_caret_column(2) == 7);
  2927. text_edit->remove_secondary_carets();
  2928. text_edit->set_caret_line(0);
  2929. text_edit->set_caret_column(4);
  2930. text_edit->select(0, 0, 0, 4);
  2931. text_edit->add_caret_at_carets(true);
  2932. CHECK(text_edit->get_caret_count() == 2);
  2933. CHECK(text_edit->get_selection_from_line(1) == 1);
  2934. CHECK(text_edit->get_selection_to_line(1) == 1);
  2935. CHECK(text_edit->get_selection_from_column(1) == 0);
  2936. CHECK(text_edit->get_selection_to_column(1) == 3);
  2937. text_edit->add_caret_at_carets(true);
  2938. CHECK(text_edit->get_caret_count() == 3);
  2939. CHECK(text_edit->get_selection_from_line(2) == 2);
  2940. CHECK(text_edit->get_selection_to_line(2) == 2);
  2941. CHECK(text_edit->get_selection_from_column(2) == 0);
  2942. CHECK(text_edit->get_selection_to_column(2) == 4);
  2943. }
  2944. memdelete(text_edit);
  2945. }
  2946. TEST_CASE("[SceneTree][TextEdit] line wrapping") {
  2947. TextEdit *text_edit = memnew(TextEdit);
  2948. SceneTree::get_singleton()->get_root()->add_child(text_edit);
  2949. text_edit->grab_focus();
  2950. // Set size for boundary.
  2951. text_edit->set_size(Size2(800, 200));
  2952. text_edit->set_line(0, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vasius mattis leo, sed porta ex lacinia bibendum. Nunc bibendum pellentesque.");
  2953. CHECK_FALSE(text_edit->is_line_wrapped(0));
  2954. CHECK(text_edit->get_line_wrap_count(0) == 0);
  2955. CHECK(text_edit->get_line_wrap_index_at_column(0, 130) == 0);
  2956. CHECK(text_edit->get_line_wrapped_text(0).size() == 1);
  2957. SIGNAL_WATCH(text_edit, "text_set");
  2958. SIGNAL_WATCH(text_edit, "text_changed");
  2959. SIGNAL_WATCH(text_edit, "lines_edited_from");
  2960. SIGNAL_WATCH(text_edit, "caret_changed");
  2961. text_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY);
  2962. SIGNAL_CHECK_FALSE("text_set");
  2963. SIGNAL_CHECK_FALSE("text_changed");
  2964. SIGNAL_CHECK_FALSE("lines_edited_from");
  2965. SIGNAL_CHECK_FALSE("caret_changed");
  2966. CHECK(text_edit->is_line_wrapped(0));
  2967. CHECK(text_edit->get_line_wrap_count(0) == 1);
  2968. CHECK(text_edit->get_line_wrap_index_at_column(0, 130) == 1);
  2969. CHECK(text_edit->get_line_wrapped_text(0).size() == 2);
  2970. SIGNAL_UNWATCH(text_edit, "text_set");
  2971. SIGNAL_UNWATCH(text_edit, "text_changed");
  2972. SIGNAL_UNWATCH(text_edit, "lines_edited_from");
  2973. SIGNAL_UNWATCH(text_edit, "caret_changed");
  2974. ERR_PRINT_OFF;
  2975. CHECK_FALSE(text_edit->is_line_wrapped(-1));
  2976. CHECK_FALSE(text_edit->is_line_wrapped(1));
  2977. CHECK(text_edit->get_line_wrap_count(-1) == 0);
  2978. CHECK(text_edit->get_line_wrap_count(1) == 0);
  2979. CHECK(text_edit->get_line_wrap_index_at_column(-1, 0) == 0);
  2980. CHECK(text_edit->get_line_wrap_index_at_column(0, -1) == 0);
  2981. CHECK(text_edit->get_line_wrap_index_at_column(1, 0) == 0);
  2982. CHECK(text_edit->get_line_wrap_index_at_column(0, 10000) == 0);
  2983. CHECK(text_edit->get_line_wrapped_text(-1).size() == 0);
  2984. CHECK(text_edit->get_line_wrapped_text(1).size() == 0);
  2985. ERR_PRINT_ON;
  2986. memdelete(text_edit);
  2987. }
  2988. TEST_CASE("[SceneTree][TextEdit] viewport") {
  2989. TextEdit *text_edit = memnew(TextEdit);
  2990. SceneTree::get_singleton()->get_root()->add_child(text_edit);
  2991. // No subcases here for performance.
  2992. text_edit->set_size(Size2(800, 600));
  2993. for (int i = 0; i < 50; i++) {
  2994. text_edit->insert_line_at(0, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vasius mattis leo, sed porta ex lacinia bibendum. Nunc bibendum pellentesque.");
  2995. }
  2996. MessageQueue::get_singleton()->flush();
  2997. const int visible_lines = text_edit->get_visible_line_count();
  2998. const int total_visible_lines = text_edit->get_total_visible_line_count();
  2999. CHECK(total_visible_lines == 51);
  3000. // First visible line.
  3001. CHECK(text_edit->get_first_visible_line() == 0);
  3002. CHECK(text_edit->get_v_scroll() == 0);
  3003. CHECK(text_edit->get_last_full_visible_line() == visible_lines - 1);
  3004. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3005. text_edit->set_line_as_first_visible(visible_lines);
  3006. MessageQueue::get_singleton()->flush();
  3007. CHECK(text_edit->get_first_visible_line() == visible_lines);
  3008. CHECK(text_edit->get_v_scroll() == visible_lines);
  3009. CHECK(text_edit->get_last_full_visible_line() == (visible_lines * 2) - 1);
  3010. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3011. ERR_PRINT_OFF;
  3012. text_edit->set_line_as_first_visible(-1);
  3013. text_edit->set_line_as_first_visible(500);
  3014. text_edit->set_line_as_first_visible(0, -1);
  3015. text_edit->set_line_as_first_visible(0, 500);
  3016. CHECK(text_edit->get_first_visible_line() == visible_lines);
  3017. ERR_PRINT_ON;
  3018. // Wrap.
  3019. text_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY);
  3020. MessageQueue::get_singleton()->flush();
  3021. CHECK(text_edit->get_total_visible_line_count() > total_visible_lines);
  3022. text_edit->set_line_as_first_visible(5, 1);
  3023. MessageQueue::get_singleton()->flush();
  3024. CHECK(text_edit->get_first_visible_line() == 5);
  3025. CHECK(text_edit->get_v_scroll() == 11);
  3026. CHECK(text_edit->get_last_full_visible_line() == visible_lines - 6);
  3027. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 1);
  3028. // Reset.
  3029. text_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_NONE);
  3030. MessageQueue::get_singleton()->flush();
  3031. CHECK(text_edit->get_total_visible_line_count() == total_visible_lines);
  3032. text_edit->set_line_as_first_visible(0);
  3033. MessageQueue::get_singleton()->flush();
  3034. CHECK(text_edit->get_first_visible_line() == 0);
  3035. CHECK(text_edit->get_v_scroll() == 0);
  3036. CHECK(text_edit->get_last_full_visible_line() == visible_lines - 1);
  3037. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3038. // Last visible line.
  3039. text_edit->set_line_as_last_visible(visible_lines * 2);
  3040. MessageQueue::get_singleton()->flush();
  3041. CHECK(text_edit->get_first_visible_line() == visible_lines);
  3042. CHECK(text_edit->get_v_scroll() == visible_lines);
  3043. CHECK(text_edit->get_last_full_visible_line() == (visible_lines * 2) - 1);
  3044. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3045. ERR_PRINT_OFF;
  3046. text_edit->set_line_as_last_visible(-1);
  3047. text_edit->set_line_as_last_visible(500);
  3048. text_edit->set_line_as_last_visible(0, -1);
  3049. text_edit->set_line_as_last_visible(0, 500);
  3050. CHECK(text_edit->get_first_visible_line() == visible_lines);
  3051. CHECK(text_edit->get_last_full_visible_line() == (visible_lines * 2) - 1);
  3052. ERR_PRINT_ON;
  3053. // Wrap.
  3054. text_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY);
  3055. MessageQueue::get_singleton()->flush();
  3056. CHECK(text_edit->get_total_visible_line_count() > total_visible_lines);
  3057. text_edit->set_line_as_last_visible(visible_lines + 5, 1);
  3058. MessageQueue::get_singleton()->flush();
  3059. CHECK(text_edit->get_first_visible_line() == 16);
  3060. CHECK(text_edit->get_v_scroll() == 32.0);
  3061. CHECK(text_edit->get_last_full_visible_line() == visible_lines + 5);
  3062. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3063. // Reset.
  3064. text_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_NONE);
  3065. MessageQueue::get_singleton()->flush();
  3066. CHECK(text_edit->get_total_visible_line_count() == total_visible_lines);
  3067. text_edit->set_line_as_first_visible(0);
  3068. MessageQueue::get_singleton()->flush();
  3069. CHECK(text_edit->get_first_visible_line() == 0);
  3070. CHECK(text_edit->get_v_scroll() == 0);
  3071. CHECK(text_edit->get_last_full_visible_line() == visible_lines - 1);
  3072. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3073. // Center.
  3074. text_edit->set_line_as_center_visible(visible_lines + (visible_lines / 2));
  3075. MessageQueue::get_singleton()->flush();
  3076. CHECK(text_edit->get_first_visible_line() == visible_lines);
  3077. CHECK(text_edit->get_v_scroll() == visible_lines);
  3078. CHECK(text_edit->get_last_full_visible_line() == (visible_lines * 2) - 1);
  3079. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3080. ERR_PRINT_OFF;
  3081. text_edit->set_line_as_last_visible(-1);
  3082. text_edit->set_line_as_last_visible(500);
  3083. text_edit->set_line_as_last_visible(0, -1);
  3084. text_edit->set_line_as_last_visible(0, 500);
  3085. CHECK(text_edit->get_first_visible_line() == visible_lines);
  3086. CHECK(text_edit->get_last_full_visible_line() == (visible_lines * 2) - 1);
  3087. ERR_PRINT_ON;
  3088. // Wrap.
  3089. text_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY);
  3090. MessageQueue::get_singleton()->flush();
  3091. CHECK(text_edit->get_total_visible_line_count() > total_visible_lines);
  3092. text_edit->set_line_as_center_visible(visible_lines + (visible_lines / 2) + 5, 1);
  3093. MessageQueue::get_singleton()->flush();
  3094. CHECK(text_edit->get_first_visible_line() == visible_lines + (visible_lines / 2));
  3095. CHECK(text_edit->get_v_scroll() == (visible_lines * 3));
  3096. CHECK(text_edit->get_last_full_visible_line() == (visible_lines * 2) - 1);
  3097. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 1);
  3098. // Scroll past eof.
  3099. int line_count = text_edit->get_line_count();
  3100. text_edit->set_scroll_past_end_of_file_enabled(true);
  3101. MessageQueue::get_singleton()->flush();
  3102. text_edit->set_line_as_center_visible(line_count - 1);
  3103. MessageQueue::get_singleton()->flush();
  3104. CHECK(text_edit->get_first_visible_line() == (visible_lines * 2) + 3);
  3105. CHECK(text_edit->get_v_scroll() == (visible_lines * 4) + 6);
  3106. CHECK(text_edit->get_last_full_visible_line() == (visible_lines * 2) + 8);
  3107. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3108. text_edit->set_scroll_past_end_of_file_enabled(false);
  3109. MessageQueue::get_singleton()->flush();
  3110. CHECK(text_edit->get_first_visible_line() == (visible_lines * 2) + 3);
  3111. CHECK(text_edit->get_v_scroll() == (visible_lines * 4) - 4);
  3112. CHECK(text_edit->get_last_full_visible_line() == (visible_lines * 2) + 8);
  3113. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3114. // Reset.
  3115. text_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_NONE);
  3116. MessageQueue::get_singleton()->flush();
  3117. CHECK(text_edit->get_total_visible_line_count() == total_visible_lines);
  3118. text_edit->set_line_as_first_visible(0);
  3119. MessageQueue::get_singleton()->flush();
  3120. CHECK(text_edit->get_first_visible_line() == 0);
  3121. CHECK(text_edit->get_v_scroll() == 0);
  3122. CHECK(text_edit->get_last_full_visible_line() == visible_lines - 1);
  3123. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3124. // Auto adjust - todo: horizontal scroll.
  3125. // Below.
  3126. MessageQueue::get_singleton()->flush();
  3127. CHECK_FALSE(text_edit->is_caret_visible());
  3128. text_edit->set_caret_line(visible_lines + 5, false);
  3129. CHECK_FALSE(text_edit->is_caret_visible());
  3130. text_edit->adjust_viewport_to_caret();
  3131. MessageQueue::get_singleton()->flush();
  3132. CHECK(text_edit->is_caret_visible());
  3133. CHECK(text_edit->get_first_visible_line() == 5);
  3134. CHECK(text_edit->get_v_scroll() == 5);
  3135. CHECK(text_edit->get_last_full_visible_line() == (visible_lines - 1) + 5);
  3136. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3137. text_edit->center_viewport_to_caret();
  3138. MessageQueue::get_singleton()->flush();
  3139. CHECK(text_edit->get_first_visible_line() == visible_lines - 5);
  3140. CHECK(text_edit->get_v_scroll() == visible_lines - 5);
  3141. CHECK(text_edit->get_last_full_visible_line() == (visible_lines * 2) - 6);
  3142. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3143. // Caret visible, do nothing.
  3144. text_edit->adjust_viewport_to_caret();
  3145. MessageQueue::get_singleton()->flush();
  3146. CHECK(text_edit->get_first_visible_line() == visible_lines - 5);
  3147. CHECK(text_edit->get_v_scroll() == visible_lines - 5);
  3148. CHECK(text_edit->get_last_full_visible_line() == (visible_lines * 2) - 6);
  3149. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3150. // Above.
  3151. text_edit->set_caret_line(1, false);
  3152. MessageQueue::get_singleton()->flush();
  3153. text_edit->adjust_viewport_to_caret();
  3154. MessageQueue::get_singleton()->flush();
  3155. CHECK(text_edit->is_caret_visible());
  3156. CHECK(text_edit->get_first_visible_line() == 1);
  3157. CHECK(text_edit->get_v_scroll() == 1);
  3158. CHECK(text_edit->get_last_full_visible_line() == visible_lines);
  3159. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3160. CHECK(text_edit->get_caret_wrap_index() == 0);
  3161. text_edit->set_line_as_first_visible(0);
  3162. MessageQueue::get_singleton()->flush();
  3163. CHECK(text_edit->get_first_visible_line() == 0);
  3164. CHECK(text_edit->get_v_scroll() == 0);
  3165. CHECK(text_edit->get_last_full_visible_line() == visible_lines - 1);
  3166. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3167. text_edit->adjust_viewport_to_caret();
  3168. MessageQueue::get_singleton()->flush();
  3169. CHECK(text_edit->get_first_visible_line() == 0);
  3170. CHECK(text_edit->get_v_scroll() == 0);
  3171. CHECK(text_edit->get_last_full_visible_line() == visible_lines - 1);
  3172. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3173. // Wrap
  3174. text_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY);
  3175. MessageQueue::get_singleton()->flush();
  3176. CHECK(text_edit->get_total_visible_line_count() > total_visible_lines);
  3177. text_edit->set_caret_line(visible_lines + 5, false, true, 1);
  3178. MessageQueue::get_singleton()->flush();
  3179. text_edit->adjust_viewport_to_caret();
  3180. MessageQueue::get_singleton()->flush();
  3181. CHECK(text_edit->get_first_visible_line() == (visible_lines / 2) + 4);
  3182. CHECK(text_edit->get_v_scroll() == (visible_lines + (visible_lines / 2)) - 1);
  3183. CHECK(text_edit->get_last_full_visible_line() == (visible_lines) + 3);
  3184. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 1);
  3185. CHECK(text_edit->get_caret_wrap_index() == 1);
  3186. text_edit->center_viewport_to_caret();
  3187. MessageQueue::get_singleton()->flush();
  3188. CHECK(text_edit->get_first_visible_line() == visible_lines);
  3189. CHECK(text_edit->get_v_scroll() == (visible_lines * 2) + 1);
  3190. CHECK(text_edit->get_last_full_visible_line() == (visible_lines * 2) - 11);
  3191. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 1);
  3192. // Caret visible, do nothing.
  3193. text_edit->adjust_viewport_to_caret();
  3194. MessageQueue::get_singleton()->flush();
  3195. CHECK(text_edit->get_first_visible_line() == visible_lines);
  3196. CHECK(text_edit->get_v_scroll() == (visible_lines * 2) + 1);
  3197. CHECK(text_edit->get_last_full_visible_line() == (visible_lines * 2) - 11);
  3198. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 1);
  3199. // Above.
  3200. text_edit->set_caret_line(1, false, true, 1);
  3201. MessageQueue::get_singleton()->flush();
  3202. text_edit->adjust_viewport_to_caret();
  3203. MessageQueue::get_singleton()->flush();
  3204. CHECK(text_edit->is_caret_visible());
  3205. CHECK(text_edit->get_first_visible_line() == 1);
  3206. CHECK(text_edit->get_v_scroll() == 3);
  3207. CHECK(text_edit->get_last_full_visible_line() == (visible_lines / 2) + 1);
  3208. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 1);
  3209. CHECK(text_edit->get_caret_wrap_index() == 1);
  3210. text_edit->set_line_as_first_visible(0);
  3211. MessageQueue::get_singleton()->flush();
  3212. CHECK(text_edit->is_caret_visible());
  3213. CHECK(text_edit->get_first_visible_line() == 0);
  3214. CHECK(text_edit->get_v_scroll() == 0);
  3215. CHECK(text_edit->get_last_full_visible_line() == visible_lines - 11);
  3216. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3217. text_edit->adjust_viewport_to_caret();
  3218. MessageQueue::get_singleton()->flush();
  3219. CHECK(text_edit->get_first_visible_line() == 0);
  3220. CHECK(text_edit->get_v_scroll() == 0);
  3221. CHECK(text_edit->get_last_full_visible_line() == visible_lines - 11);
  3222. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3223. // Reset.
  3224. text_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_NONE);
  3225. MessageQueue::get_singleton()->flush();
  3226. CHECK(text_edit->get_total_visible_line_count() == total_visible_lines);
  3227. text_edit->set_line_as_first_visible(0);
  3228. MessageQueue::get_singleton()->flush();
  3229. CHECK(text_edit->get_first_visible_line() == 0);
  3230. CHECK(text_edit->get_v_scroll() == 0);
  3231. CHECK(text_edit->get_last_full_visible_line() == visible_lines - 1);
  3232. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3233. CHECK(text_edit->get_caret_wrap_index() == 0);
  3234. // Smooth scroll.
  3235. text_edit->set_v_scroll_speed(10);
  3236. CHECK(text_edit->get_v_scroll_speed() == 10);
  3237. ERR_PRINT_OFF;
  3238. text_edit->set_v_scroll_speed(-1);
  3239. CHECK(text_edit->get_v_scroll_speed() == 10);
  3240. text_edit->set_v_scroll_speed(0);
  3241. CHECK(text_edit->get_v_scroll_speed() == 10);
  3242. text_edit->set_v_scroll_speed(1);
  3243. CHECK(text_edit->get_v_scroll_speed() == 1);
  3244. ERR_PRINT_ON;
  3245. // Scroll.
  3246. int v_scroll = text_edit->get_v_scroll();
  3247. SEND_GUI_MOUSE_BUTTON_EVENT(Point2i(10, 10), MouseButton::WHEEL_DOWN, 0, Key::NONE);
  3248. CHECK(text_edit->get_v_scroll() > v_scroll);
  3249. SEND_GUI_MOUSE_BUTTON_EVENT(Point2i(10, 10), MouseButton::WHEEL_UP, 0, Key::NONE);
  3250. CHECK(text_edit->get_v_scroll() == v_scroll);
  3251. // smooth scroll speed.
  3252. text_edit->set_smooth_scroll_enabled(true);
  3253. v_scroll = text_edit->get_v_scroll();
  3254. SEND_GUI_MOUSE_BUTTON_EVENT(Point2i(10, 10), MouseButton::WHEEL_DOWN, 0, Key::NONE);
  3255. text_edit->notification(TextEdit::NOTIFICATION_INTERNAL_PHYSICS_PROCESS);
  3256. CHECK(text_edit->get_v_scroll() >= v_scroll);
  3257. SEND_GUI_MOUSE_BUTTON_EVENT(Point2i(10, 10), MouseButton::WHEEL_UP, 0, Key::NONE);
  3258. text_edit->notification(TextEdit::NOTIFICATION_INTERNAL_PHYSICS_PROCESS);
  3259. CHECK(text_edit->get_v_scroll() == v_scroll);
  3260. v_scroll = text_edit->get_v_scroll();
  3261. text_edit->set_v_scroll_speed(10000);
  3262. SEND_GUI_MOUSE_BUTTON_EVENT(Point2i(10, 10), MouseButton::WHEEL_DOWN, 0, Key::NONE);
  3263. text_edit->notification(TextEdit::NOTIFICATION_INTERNAL_PHYSICS_PROCESS);
  3264. CHECK(text_edit->get_v_scroll() >= v_scroll);
  3265. SEND_GUI_MOUSE_BUTTON_EVENT(Point2i(10, 10), MouseButton::WHEEL_UP, 0, Key::NONE);
  3266. text_edit->notification(TextEdit::NOTIFICATION_INTERNAL_PHYSICS_PROCESS);
  3267. CHECK(text_edit->get_v_scroll() == v_scroll);
  3268. ERR_PRINT_OFF;
  3269. CHECK(text_edit->get_scroll_pos_for_line(-1) == 0);
  3270. CHECK(text_edit->get_scroll_pos_for_line(1000) == 0);
  3271. CHECK(text_edit->get_scroll_pos_for_line(1, -1) == 0);
  3272. CHECK(text_edit->get_scroll_pos_for_line(1, 100) == 0);
  3273. ERR_PRINT_ON;
  3274. text_edit->set_h_scroll(-100);
  3275. CHECK(text_edit->get_h_scroll() == 0);
  3276. text_edit->set_h_scroll(10000000);
  3277. CHECK(text_edit->get_h_scroll() == 314);
  3278. text_edit->set_h_scroll(-100);
  3279. CHECK(text_edit->get_h_scroll() == 0);
  3280. text_edit->set_smooth_scroll_enabled(false);
  3281. CHECK(text_edit->get_first_visible_line() == 0);
  3282. CHECK(text_edit->get_v_scroll() == 0);
  3283. CHECK(text_edit->get_last_full_visible_line() == visible_lines - 1);
  3284. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3285. text_edit->grab_focus();
  3286. SEND_GUI_ACTION("ui_text_scroll_down");
  3287. CHECK(text_edit->get_viewport()->is_input_handled());
  3288. CHECK(text_edit->get_caret_line() == 1);
  3289. CHECK(text_edit->get_first_visible_line() == 1);
  3290. CHECK(text_edit->get_v_scroll() == 1);
  3291. CHECK(text_edit->get_last_full_visible_line() == visible_lines);
  3292. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3293. CHECK(text_edit->get_caret_wrap_index() == 0);
  3294. SEND_GUI_ACTION("ui_text_scroll_up");
  3295. CHECK(text_edit->get_viewport()->is_input_handled());
  3296. CHECK(text_edit->get_caret_line() == 1);
  3297. CHECK(text_edit->get_first_visible_line() == 0);
  3298. CHECK(text_edit->get_v_scroll() == 0);
  3299. CHECK(text_edit->get_last_full_visible_line() == visible_lines - 1);
  3300. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3301. CHECK(text_edit->get_caret_wrap_index() == 0);
  3302. // Page down, similar to VSCode, to end of page then scroll.
  3303. SEND_GUI_ACTION("ui_text_caret_page_down");
  3304. CHECK(text_edit->get_viewport()->is_input_handled());
  3305. CHECK(text_edit->get_caret_line() == 21);
  3306. CHECK(text_edit->get_first_visible_line() == 0);
  3307. CHECK(text_edit->get_v_scroll() == 0);
  3308. CHECK(text_edit->get_last_full_visible_line() == visible_lines - 1);
  3309. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3310. CHECK(text_edit->get_caret_wrap_index() == 0);
  3311. SEND_GUI_ACTION("ui_text_caret_page_down");
  3312. CHECK(text_edit->get_viewport()->is_input_handled());
  3313. CHECK(text_edit->get_caret_line() == 41);
  3314. CHECK(text_edit->get_first_visible_line() == 20);
  3315. CHECK(text_edit->get_v_scroll() == 20);
  3316. CHECK(text_edit->get_last_full_visible_line() == (visible_lines - 1) * 2);
  3317. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3318. CHECK(text_edit->get_caret_wrap_index() == 0);
  3319. SEND_GUI_ACTION("ui_text_caret_page_up");
  3320. CHECK(text_edit->get_viewport()->is_input_handled());
  3321. CHECK(text_edit->get_caret_line() == 21);
  3322. CHECK(text_edit->get_first_visible_line() == 20);
  3323. CHECK(text_edit->get_v_scroll() == 20);
  3324. CHECK(text_edit->get_last_full_visible_line() == (visible_lines - 1) * 2);
  3325. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3326. CHECK(text_edit->get_caret_wrap_index() == 0);
  3327. SEND_GUI_ACTION("ui_text_caret_page_up");
  3328. CHECK(text_edit->get_viewport()->is_input_handled());
  3329. CHECK(text_edit->get_caret_line() == 1);
  3330. CHECK(text_edit->get_first_visible_line() == 1);
  3331. CHECK(text_edit->get_v_scroll() == 1);
  3332. CHECK(text_edit->get_last_full_visible_line() == visible_lines);
  3333. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3334. CHECK(text_edit->get_caret_wrap_index() == 0);
  3335. text_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_NONE);
  3336. MessageQueue::get_singleton()->flush();
  3337. text_edit->grab_focus();
  3338. SEND_GUI_ACTION("ui_text_scroll_down");
  3339. CHECK(text_edit->get_viewport()->is_input_handled());
  3340. CHECK(text_edit->get_caret_line() == 2);
  3341. CHECK(text_edit->get_first_visible_line() == 2);
  3342. CHECK(text_edit->get_v_scroll() == 2);
  3343. CHECK(text_edit->get_last_full_visible_line() == visible_lines + 1);
  3344. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3345. CHECK(text_edit->get_caret_wrap_index() == 0);
  3346. SEND_GUI_ACTION("ui_text_scroll_up");
  3347. CHECK(text_edit->get_viewport()->is_input_handled());
  3348. CHECK(text_edit->get_caret_line() == 2);
  3349. CHECK(text_edit->get_first_visible_line() == 1);
  3350. CHECK(text_edit->get_v_scroll() == 1);
  3351. CHECK(text_edit->get_last_full_visible_line() == visible_lines);
  3352. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3353. CHECK(text_edit->get_caret_wrap_index() == 0);
  3354. // Page down, similar to VSCode, to end of page then scroll.
  3355. SEND_GUI_ACTION("ui_text_caret_page_down");
  3356. CHECK(text_edit->get_viewport()->is_input_handled());
  3357. CHECK(text_edit->get_caret_line() == 22);
  3358. CHECK(text_edit->get_first_visible_line() == 1);
  3359. CHECK(text_edit->get_v_scroll() == 1);
  3360. CHECK(text_edit->get_last_full_visible_line() == visible_lines);
  3361. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3362. CHECK(text_edit->get_caret_wrap_index() == 0);
  3363. SEND_GUI_ACTION("ui_text_caret_page_down");
  3364. CHECK(text_edit->get_viewport()->is_input_handled());
  3365. CHECK(text_edit->get_caret_line() == 42);
  3366. CHECK(text_edit->get_first_visible_line() == 21);
  3367. CHECK(text_edit->get_v_scroll() == 21);
  3368. CHECK(text_edit->get_last_full_visible_line() == (visible_lines * 2) - 1);
  3369. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3370. CHECK(text_edit->get_caret_wrap_index() == 0);
  3371. SEND_GUI_ACTION("ui_text_caret_page_up");
  3372. CHECK(text_edit->get_viewport()->is_input_handled());
  3373. CHECK(text_edit->get_caret_line() == 22);
  3374. CHECK(text_edit->get_first_visible_line() == 21);
  3375. CHECK(text_edit->get_v_scroll() == 21);
  3376. CHECK(text_edit->get_last_full_visible_line() == (visible_lines * 2) - 1);
  3377. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3378. CHECK(text_edit->get_caret_wrap_index() == 0);
  3379. SEND_GUI_ACTION("ui_text_caret_page_up");
  3380. CHECK(text_edit->get_viewport()->is_input_handled());
  3381. CHECK(text_edit->get_caret_line() == 2);
  3382. CHECK(text_edit->get_first_visible_line() == 2);
  3383. CHECK(text_edit->get_v_scroll() == 2);
  3384. CHECK(text_edit->get_last_full_visible_line() == visible_lines + 1);
  3385. CHECK(text_edit->get_last_full_visible_line_wrap_index() == 0);
  3386. CHECK(text_edit->get_caret_wrap_index() == 0);
  3387. // Typing and undo / redo should adjust viewport
  3388. text_edit->set_caret_line(0);
  3389. text_edit->set_caret_column(0);
  3390. text_edit->set_line_as_first_visible(5);
  3391. MessageQueue::get_singleton()->flush();
  3392. CHECK(text_edit->get_first_visible_line() == 5);
  3393. SEND_GUI_KEY_EVENT(Key::A);
  3394. CHECK(text_edit->get_first_visible_line() == 0);
  3395. text_edit->set_line_as_first_visible(5);
  3396. MessageQueue::get_singleton()->flush();
  3397. CHECK(text_edit->get_first_visible_line() == 5);
  3398. text_edit->undo();
  3399. MessageQueue::get_singleton()->flush();
  3400. CHECK(text_edit->get_first_visible_line() == 0);
  3401. text_edit->set_line_as_first_visible(5);
  3402. MessageQueue::get_singleton()->flush();
  3403. CHECK(text_edit->get_first_visible_line() == 5);
  3404. text_edit->redo();
  3405. MessageQueue::get_singleton()->flush();
  3406. CHECK(text_edit->get_first_visible_line() == 0);
  3407. memdelete(text_edit);
  3408. }
  3409. TEST_CASE("[SceneTree][TextEdit] setter getters") {
  3410. TextEdit *text_edit = memnew(TextEdit);
  3411. SceneTree::get_singleton()->get_root()->add_child(text_edit);
  3412. SUBCASE("[TextEdit] set and get placeholder") {
  3413. text_edit->set_placeholder("test\nplaceholder");
  3414. CHECK(text_edit->get_placeholder() == "test\nplaceholder");
  3415. CHECK(text_edit->get_text() == "");
  3416. CHECK(text_edit->get_line_count() == 1);
  3417. CHECK(text_edit->get_last_full_visible_line() == 0);
  3418. }
  3419. SUBCASE("[TextEdit] highlight current line") {
  3420. text_edit->set_highlight_current_line(true);
  3421. CHECK(text_edit->is_highlight_current_line_enabled());
  3422. text_edit->set_highlight_current_line(false);
  3423. CHECK_FALSE(text_edit->is_highlight_current_line_enabled());
  3424. }
  3425. SUBCASE("[TextEdit] highlight all occurrences") {
  3426. text_edit->set_highlight_all_occurrences(true);
  3427. CHECK(text_edit->is_highlight_all_occurrences_enabled());
  3428. text_edit->set_highlight_all_occurrences(false);
  3429. CHECK_FALSE(text_edit->is_highlight_all_occurrences_enabled());
  3430. }
  3431. SUBCASE("[TextEdit] draw control chars") {
  3432. text_edit->set_draw_control_chars(true);
  3433. CHECK(text_edit->get_draw_control_chars());
  3434. text_edit->set_draw_control_chars(false);
  3435. CHECK_FALSE(text_edit->get_draw_control_chars());
  3436. }
  3437. SUBCASE("[TextEdit] draw tabs") {
  3438. text_edit->set_draw_tabs(true);
  3439. CHECK(text_edit->is_drawing_tabs());
  3440. text_edit->set_draw_tabs(false);
  3441. CHECK_FALSE(text_edit->is_drawing_tabs());
  3442. }
  3443. SUBCASE("[TextEdit] draw spaces") {
  3444. text_edit->set_draw_spaces(true);
  3445. CHECK(text_edit->is_drawing_spaces());
  3446. text_edit->set_draw_spaces(false);
  3447. CHECK_FALSE(text_edit->is_drawing_spaces());
  3448. }
  3449. SUBCASE("[TextEdit] draw minimao") {
  3450. text_edit->set_draw_minimap(true);
  3451. CHECK(text_edit->is_drawing_minimap());
  3452. text_edit->set_draw_minimap(false);
  3453. CHECK_FALSE(text_edit->is_drawing_minimap());
  3454. }
  3455. SUBCASE("[TextEdit] minimap width") {
  3456. text_edit->set_minimap_width(-1);
  3457. CHECK(text_edit->get_minimap_width() == -1);
  3458. text_edit->set_minimap_width(1000);
  3459. CHECK(text_edit->get_minimap_width() == 1000);
  3460. }
  3461. SUBCASE("[TextEdit] line color background") {
  3462. ERR_PRINT_OFF;
  3463. text_edit->set_line_background_color(-1, Color("#ff0000"));
  3464. text_edit->set_line_background_color(0, Color("#00ff00"));
  3465. text_edit->set_line_background_color(1, Color("#0000ff"));
  3466. CHECK(text_edit->get_line_background_color(-1) == Color());
  3467. CHECK(text_edit->get_line_background_color(0) == Color("#00ff00"));
  3468. CHECK(text_edit->get_line_background_color(1) == Color());
  3469. ERR_PRINT_ON;
  3470. text_edit->set_line_background_color(0, Color("#ffff00"));
  3471. CHECK(text_edit->get_line_background_color(0) == Color("#ffff00"));
  3472. }
  3473. memdelete(text_edit);
  3474. }
  3475. TEST_CASE("[SceneTree][TextEdit] gutters") {
  3476. TextEdit *text_edit = memnew(TextEdit);
  3477. SceneTree::get_singleton()->get_root()->add_child(text_edit);
  3478. Array empty_signal_args;
  3479. empty_signal_args.push_back(Array());
  3480. SIGNAL_WATCH(text_edit, "gutter_clicked");
  3481. SIGNAL_WATCH(text_edit, "gutter_added");
  3482. SIGNAL_WATCH(text_edit, "gutter_removed");
  3483. SUBCASE("[TextEdit] gutter add and remove") {
  3484. text_edit->add_gutter();
  3485. CHECK(text_edit->get_gutter_count() == 1);
  3486. CHECK(text_edit->get_gutter_width(0) == 24);
  3487. CHECK(text_edit->get_total_gutter_width() == 24 + 2);
  3488. SIGNAL_CHECK("gutter_added", empty_signal_args);
  3489. text_edit->set_gutter_name(0, "test_gutter");
  3490. CHECK(text_edit->get_gutter_name(0) == "test_gutter");
  3491. text_edit->set_gutter_width(0, 10);
  3492. CHECK(text_edit->get_gutter_width(0) == 10);
  3493. CHECK(text_edit->get_total_gutter_width() == 10 + 2);
  3494. text_edit->add_gutter(-100);
  3495. text_edit->set_gutter_width(1, 10);
  3496. CHECK(text_edit->get_gutter_width(1) == 10);
  3497. CHECK(text_edit->get_total_gutter_width() == 20 + 2);
  3498. CHECK(text_edit->get_gutter_count() == 2);
  3499. CHECK(text_edit->get_gutter_name(0) == "test_gutter");
  3500. SIGNAL_CHECK("gutter_added", empty_signal_args);
  3501. text_edit->set_gutter_draw(1, false);
  3502. CHECK(text_edit->get_total_gutter_width() == 10 + 2);
  3503. text_edit->add_gutter(100);
  3504. CHECK(text_edit->get_gutter_count() == 3);
  3505. CHECK(text_edit->get_gutter_width(2) == 24);
  3506. CHECK(text_edit->get_total_gutter_width() == 34 + 2);
  3507. CHECK(text_edit->get_gutter_name(0) == "test_gutter");
  3508. SIGNAL_CHECK("gutter_added", empty_signal_args);
  3509. text_edit->add_gutter(0);
  3510. CHECK(text_edit->get_gutter_count() == 4);
  3511. CHECK(text_edit->get_gutter_width(0) == 24);
  3512. CHECK(text_edit->get_total_gutter_width() == 58 + 2);
  3513. CHECK(text_edit->get_gutter_name(1) == "test_gutter");
  3514. SIGNAL_CHECK("gutter_added", empty_signal_args);
  3515. text_edit->remove_gutter(2);
  3516. CHECK(text_edit->get_gutter_name(1) == "test_gutter");
  3517. CHECK(text_edit->get_gutter_count() == 3);
  3518. CHECK(text_edit->get_total_gutter_width() == 58 + 2);
  3519. SIGNAL_CHECK("gutter_removed", empty_signal_args);
  3520. text_edit->remove_gutter(0);
  3521. CHECK(text_edit->get_gutter_name(0) == "test_gutter");
  3522. CHECK(text_edit->get_gutter_count() == 2);
  3523. CHECK(text_edit->get_total_gutter_width() == 34 + 2);
  3524. SIGNAL_CHECK("gutter_removed", empty_signal_args);
  3525. ERR_PRINT_OFF;
  3526. text_edit->remove_gutter(-1);
  3527. SIGNAL_CHECK_FALSE("gutter_removed");
  3528. text_edit->remove_gutter(100);
  3529. SIGNAL_CHECK_FALSE("gutter_removed");
  3530. CHECK(text_edit->get_gutter_name(-1) == "");
  3531. CHECK(text_edit->get_gutter_name(100) == "");
  3532. ERR_PRINT_ON;
  3533. }
  3534. SUBCASE("[TextEdit] gutter data") {
  3535. text_edit->add_gutter();
  3536. CHECK(text_edit->get_gutter_count() == 1);
  3537. SIGNAL_CHECK("gutter_added", empty_signal_args);
  3538. text_edit->set_gutter_name(0, "test_gutter");
  3539. CHECK(text_edit->get_gutter_name(0) == "test_gutter");
  3540. text_edit->set_gutter_width(0, 10);
  3541. CHECK(text_edit->get_gutter_width(0) == 10);
  3542. text_edit->set_gutter_clickable(0, true);
  3543. CHECK(text_edit->is_gutter_clickable(0));
  3544. text_edit->set_gutter_overwritable(0, true);
  3545. CHECK(text_edit->is_gutter_overwritable(0));
  3546. text_edit->set_gutter_type(0, TextEdit::GutterType::GUTTER_TYPE_CUSTOM);
  3547. CHECK(text_edit->get_gutter_type(0) == TextEdit::GutterType::GUTTER_TYPE_CUSTOM);
  3548. text_edit->set_text("test\ntext");
  3549. ERR_PRINT_OFF;
  3550. text_edit->set_line_gutter_metadata(1, 0, "test");
  3551. text_edit->set_line_gutter_metadata(0, -1, "test");
  3552. text_edit->set_line_gutter_metadata(0, 2, "test");
  3553. text_edit->set_line_gutter_metadata(2, 0, "test");
  3554. text_edit->set_line_gutter_metadata(-1, 0, "test");
  3555. CHECK(text_edit->get_line_gutter_metadata(1, 0) == "test");
  3556. CHECK(text_edit->get_line_gutter_metadata(0, -1) == "");
  3557. CHECK(text_edit->get_line_gutter_metadata(0, 2) == "");
  3558. CHECK(text_edit->get_line_gutter_metadata(2, 0) == "");
  3559. CHECK(text_edit->get_line_gutter_metadata(-1, 0) == "");
  3560. text_edit->set_line_gutter_text(1, 0, "test");
  3561. text_edit->set_line_gutter_text(0, -1, "test");
  3562. text_edit->set_line_gutter_text(0, 2, "test");
  3563. text_edit->set_line_gutter_text(2, 0, "test");
  3564. text_edit->set_line_gutter_text(-1, 0, "test");
  3565. CHECK(text_edit->get_line_gutter_text(1, 0) == "test");
  3566. CHECK(text_edit->get_line_gutter_text(0, -1) == "");
  3567. CHECK(text_edit->get_line_gutter_text(0, 2) == "");
  3568. CHECK(text_edit->get_line_gutter_text(2, 0) == "");
  3569. CHECK(text_edit->get_line_gutter_text(-1, 0) == "");
  3570. text_edit->set_line_gutter_item_color(1, 0, Color(1, 0, 0));
  3571. text_edit->set_line_gutter_item_color(0, -1, Color(1, 0, 0));
  3572. text_edit->set_line_gutter_item_color(0, 2, Color(1, 0, 0));
  3573. text_edit->set_line_gutter_item_color(2, 0, Color(1, 0, 0));
  3574. text_edit->set_line_gutter_item_color(-1, 0, Color(1, 0, 0));
  3575. CHECK(text_edit->get_line_gutter_item_color(1, 0) == Color(1, 0, 0));
  3576. CHECK(text_edit->get_line_gutter_item_color(0, -1) == Color());
  3577. CHECK(text_edit->get_line_gutter_item_color(0, 2) == Color());
  3578. CHECK(text_edit->get_line_gutter_item_color(2, 0) == Color());
  3579. CHECK(text_edit->get_line_gutter_item_color(-1, 0) == Color());
  3580. text_edit->set_line_gutter_clickable(1, 0, true);
  3581. text_edit->set_line_gutter_clickable(0, -1, true);
  3582. text_edit->set_line_gutter_clickable(0, 2, true);
  3583. text_edit->set_line_gutter_clickable(2, 0, true);
  3584. text_edit->set_line_gutter_clickable(-1, 0, true);
  3585. CHECK(text_edit->is_line_gutter_clickable(1, 0) == true);
  3586. CHECK(text_edit->is_line_gutter_clickable(0, -1) == false);
  3587. CHECK(text_edit->is_line_gutter_clickable(0, 2) == false);
  3588. CHECK(text_edit->is_line_gutter_clickable(2, 0) == false);
  3589. CHECK(text_edit->is_line_gutter_clickable(-1, 0) == false);
  3590. ERR_PRINT_ON;
  3591. // Merging tested via CodeEdit gutters.
  3592. }
  3593. SIGNAL_UNWATCH(text_edit, "gutter_clicked");
  3594. SIGNAL_UNWATCH(text_edit, "gutter_added");
  3595. SIGNAL_UNWATCH(text_edit, "gutter_removed");
  3596. memdelete(text_edit);
  3597. }
  3598. } // namespace TestTextEdit
  3599. #endif // TEST_TEXT_EDIT_H