ViewTests.cs 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using Terminal.Gui.Graphs;
  5. using Xunit;
  6. using Xunit.Abstractions;
  7. // Alias Console to MockConsole so we don't accidentally use Console
  8. using Console = Terminal.Gui.FakeConsole;
  9. namespace Terminal.Gui.CoreTests {
  10. public class ViewTests {
  11. readonly ITestOutputHelper output;
  12. public ViewTests (ITestOutputHelper output)
  13. {
  14. this.output = output;
  15. }
  16. [Fact]
  17. public void New_Initializes ()
  18. {
  19. // Parameterless
  20. var r = new View ();
  21. Assert.NotNull (r);
  22. Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
  23. Assert.Equal ("View()({X=0,Y=0,Width=0,Height=0})", r.ToString ());
  24. Assert.False (r.CanFocus);
  25. Assert.False (r.HasFocus);
  26. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  27. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  28. Assert.Null (r.Focused);
  29. Assert.Null (r.ColorScheme);
  30. Assert.Null (r.Width);
  31. Assert.Null (r.Height);
  32. Assert.Null (r.X);
  33. Assert.Null (r.Y);
  34. Assert.False (r.IsCurrentTop);
  35. Assert.Empty (r.Id);
  36. Assert.Empty (r.Subviews);
  37. Assert.False (r.WantContinuousButtonPressed);
  38. Assert.False (r.WantMousePositionReports);
  39. Assert.Null (r.SuperView);
  40. Assert.Null (r.MostFocused);
  41. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  42. // Empty Rect
  43. r = new View (Rect.Empty);
  44. Assert.NotNull (r);
  45. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  46. Assert.Equal ("View()({X=0,Y=0,Width=0,Height=0})", r.ToString ());
  47. Assert.False (r.CanFocus);
  48. Assert.False (r.HasFocus);
  49. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  50. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  51. Assert.Null (r.Focused);
  52. Assert.Null (r.ColorScheme);
  53. Assert.Null (r.Width); // All view Dim are initialized now in the IsAdded setter,
  54. Assert.Null (r.Height); // avoiding Dim errors.
  55. Assert.Null (r.X); // All view Pos are initialized now in the IsAdded setter,
  56. Assert.Null (r.Y); // avoiding Pos errors.
  57. Assert.False (r.IsCurrentTop);
  58. Assert.Empty (r.Id);
  59. Assert.Empty (r.Subviews);
  60. Assert.False (r.WantContinuousButtonPressed);
  61. Assert.False (r.WantMousePositionReports);
  62. Assert.Null (r.SuperView);
  63. Assert.Null (r.MostFocused);
  64. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  65. // Rect with values
  66. r = new View (new Rect (1, 2, 3, 4));
  67. Assert.NotNull (r);
  68. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  69. Assert.Equal ("View()({X=1,Y=2,Width=3,Height=4})", r.ToString ());
  70. Assert.False (r.CanFocus);
  71. Assert.False (r.HasFocus);
  72. Assert.Equal (new Rect (0, 0, 3, 4), r.Bounds);
  73. Assert.Equal (new Rect (1, 2, 3, 4), r.Frame);
  74. Assert.Null (r.Focused);
  75. Assert.Null (r.ColorScheme);
  76. Assert.Null (r.Width);
  77. Assert.Null (r.Height);
  78. Assert.Null (r.X);
  79. Assert.Null (r.Y);
  80. Assert.False (r.IsCurrentTop);
  81. Assert.Empty (r.Id);
  82. Assert.Empty (r.Subviews);
  83. Assert.False (r.WantContinuousButtonPressed);
  84. Assert.False (r.WantMousePositionReports);
  85. Assert.Null (r.SuperView);
  86. Assert.Null (r.MostFocused);
  87. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  88. // Initializes a view with a vertical direction
  89. r = new View ("Vertical View", TextDirection.TopBottom_LeftRight);
  90. Assert.NotNull (r);
  91. Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
  92. Assert.Equal ("View()({X=0,Y=0,Width=1,Height=13})", r.ToString ());
  93. Assert.False (r.CanFocus);
  94. Assert.False (r.HasFocus);
  95. Assert.Equal (new Rect (0, 0, 1, 13), r.Bounds);
  96. Assert.Equal (new Rect (0, 0, 1, 13), r.Frame);
  97. Assert.Null (r.Focused);
  98. Assert.Null (r.ColorScheme);
  99. Assert.Null (r.Width); // All view Dim are initialized now in the IsAdded setter,
  100. Assert.Null (r.Height); // avoiding Dim errors.
  101. Assert.Null (r.X); // All view Pos are initialized now in the IsAdded setter,
  102. Assert.Null (r.Y); // avoiding Pos errors.
  103. Assert.False (r.IsCurrentTop);
  104. Assert.Empty (r.Id);
  105. Assert.Empty (r.Subviews);
  106. Assert.False (r.WantContinuousButtonPressed);
  107. Assert.False (r.WantMousePositionReports);
  108. Assert.Null (r.SuperView);
  109. Assert.Null (r.MostFocused);
  110. Assert.Equal (TextDirection.TopBottom_LeftRight, r.TextDirection);
  111. }
  112. [Fact]
  113. public void New_Methods_Return_False ()
  114. {
  115. var r = new View ();
  116. Assert.False (r.ProcessKey (new KeyEvent () { Key = Key.Unknown }));
  117. Assert.False (r.ProcessHotKey (new KeyEvent () { Key = Key.Unknown }));
  118. Assert.False (r.ProcessColdKey (new KeyEvent () { Key = Key.Unknown }));
  119. Assert.False (r.OnKeyDown (new KeyEvent () { Key = Key.Unknown }));
  120. Assert.False (r.OnKeyUp (new KeyEvent () { Key = Key.Unknown }));
  121. Assert.False (r.MouseEvent (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  122. Assert.False (r.OnMouseEnter (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  123. Assert.False (r.OnMouseLeave (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  124. Assert.False (r.OnEnter (new View ()));
  125. Assert.False (r.OnLeave (new View ()));
  126. // TODO: Add more
  127. }
  128. [Fact]
  129. public void View_With_No_Difference_Between_An_Object_Initializer_And_A_Constructor ()
  130. {
  131. // Object Initializer
  132. var view = new View () {
  133. X = 1,
  134. Y = 2,
  135. Width = 3,
  136. Height = 4
  137. };
  138. Assert.Equal (1, view.X);
  139. Assert.Equal (2, view.Y);
  140. Assert.Equal (3, view.Width);
  141. Assert.Equal (4, view.Height);
  142. Assert.False (view.Frame.IsEmpty);
  143. Assert.Equal (new Rect (1, 2, 3, 4), view.Frame);
  144. Assert.False (view.Bounds.IsEmpty);
  145. Assert.Equal (new Rect (0, 0, 3, 4), view.Bounds);
  146. view.LayoutSubviews ();
  147. Assert.Equal (1, view.X);
  148. Assert.Equal (2, view.Y);
  149. Assert.Equal (3, view.Width);
  150. Assert.Equal (4, view.Height);
  151. Assert.False (view.Frame.IsEmpty);
  152. Assert.False (view.Bounds.IsEmpty);
  153. // Default Constructor
  154. view = new View ();
  155. Assert.Null (view.X);
  156. Assert.Null (view.Y);
  157. Assert.Null (view.Width);
  158. Assert.Null (view.Height);
  159. Assert.True (view.Frame.IsEmpty);
  160. Assert.True (view.Bounds.IsEmpty);
  161. // Constructor
  162. view = new View (1, 2, "");
  163. Assert.Null (view.X);
  164. Assert.Null (view.Y);
  165. Assert.Null (view.Width);
  166. Assert.Null (view.Height);
  167. Assert.False (view.Frame.IsEmpty);
  168. Assert.True (view.Bounds.IsEmpty);
  169. // Default Constructor and post assignment equivalent to Object Initializer
  170. view = new View ();
  171. view.X = 1;
  172. view.Y = 2;
  173. view.Width = 3;
  174. view.Height = 4;
  175. Assert.Equal (1, view.X);
  176. Assert.Equal (2, view.Y);
  177. Assert.Equal (3, view.Width);
  178. Assert.Equal (4, view.Height);
  179. Assert.False (view.Frame.IsEmpty);
  180. Assert.Equal (new Rect (1, 2, 3, 4), view.Frame);
  181. Assert.False (view.Bounds.IsEmpty);
  182. Assert.Equal (new Rect (0, 0, 3, 4), view.Bounds);
  183. }
  184. [Fact]
  185. public void FocusNearestView_Ensure_Focus_Ordered ()
  186. {
  187. var top = new Toplevel ();
  188. var win = new Window ();
  189. var winSubview = new View ("WindowSubview") {
  190. CanFocus = true
  191. };
  192. win.Add (winSubview);
  193. top.Add (win);
  194. var frm = new FrameView ();
  195. var frmSubview = new View ("FrameSubview") {
  196. CanFocus = true
  197. };
  198. frm.Add (frmSubview);
  199. top.Add (frm);
  200. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  201. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  202. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  203. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  204. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  205. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  206. top.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ()));
  207. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  208. top.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ()));
  209. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  210. }
  211. [Fact]
  212. public void Added_Removed ()
  213. {
  214. var v = new View (new Rect (0, 0, 10, 24));
  215. var t = new View ();
  216. v.Added += (View e) => {
  217. Assert.True (v.SuperView == e);
  218. };
  219. v.Removed += (View e) => {
  220. Assert.True (v.SuperView == null);
  221. };
  222. t.Add (v);
  223. Assert.True (t.Subviews.Count == 1);
  224. t.Remove (v);
  225. Assert.True (t.Subviews.Count == 0);
  226. }
  227. [Fact]
  228. public void Subviews_TabIndexes_AreEqual ()
  229. {
  230. var r = new View ();
  231. var v1 = new View () { CanFocus = true };
  232. var v2 = new View () { CanFocus = true };
  233. var v3 = new View () { CanFocus = true };
  234. r.Add (v1, v2, v3);
  235. Assert.True (r.Subviews.IndexOf (v1) == 0);
  236. Assert.True (r.Subviews.IndexOf (v2) == 1);
  237. Assert.True (r.Subviews.IndexOf (v3) == 2);
  238. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  239. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  240. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  241. Assert.Equal (r.Subviews.IndexOf (v1), r.TabIndexes.IndexOf (v1));
  242. Assert.Equal (r.Subviews.IndexOf (v2), r.TabIndexes.IndexOf (v2));
  243. Assert.Equal (r.Subviews.IndexOf (v3), r.TabIndexes.IndexOf (v3));
  244. }
  245. [Fact]
  246. public void BringSubviewToFront_Subviews_vs_TabIndexes ()
  247. {
  248. var r = new View ();
  249. var v1 = new View () { CanFocus = true };
  250. var v2 = new View () { CanFocus = true };
  251. var v3 = new View () { CanFocus = true };
  252. r.Add (v1, v2, v3);
  253. r.BringSubviewToFront (v1);
  254. Assert.True (r.Subviews.IndexOf (v1) == 2);
  255. Assert.True (r.Subviews.IndexOf (v2) == 0);
  256. Assert.True (r.Subviews.IndexOf (v3) == 1);
  257. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  258. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  259. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  260. }
  261. [Fact]
  262. public void BringSubviewForward_Subviews_vs_TabIndexes ()
  263. {
  264. var r = new View ();
  265. var v1 = new View () { CanFocus = true };
  266. var v2 = new View () { CanFocus = true };
  267. var v3 = new View () { CanFocus = true };
  268. r.Add (v1, v2, v3);
  269. r.BringSubviewForward (v1);
  270. Assert.True (r.Subviews.IndexOf (v1) == 1);
  271. Assert.True (r.Subviews.IndexOf (v2) == 0);
  272. Assert.True (r.Subviews.IndexOf (v3) == 2);
  273. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  274. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  275. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  276. }
  277. [Fact]
  278. public void SendSubviewToBack_Subviews_vs_TabIndexes ()
  279. {
  280. var r = new View ();
  281. var v1 = new View () { CanFocus = true };
  282. var v2 = new View () { CanFocus = true };
  283. var v3 = new View () { CanFocus = true };
  284. r.Add (v1, v2, v3);
  285. r.SendSubviewToBack (v3);
  286. Assert.True (r.Subviews.IndexOf (v1) == 1);
  287. Assert.True (r.Subviews.IndexOf (v2) == 2);
  288. Assert.True (r.Subviews.IndexOf (v3) == 0);
  289. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  290. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  291. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  292. }
  293. [Fact]
  294. public void SendSubviewBackwards_Subviews_vs_TabIndexes ()
  295. {
  296. var r = new View ();
  297. var v1 = new View () { CanFocus = true };
  298. var v2 = new View () { CanFocus = true };
  299. var v3 = new View () { CanFocus = true };
  300. r.Add (v1, v2, v3);
  301. r.SendSubviewBackwards (v3);
  302. Assert.True (r.Subviews.IndexOf (v1) == 0);
  303. Assert.True (r.Subviews.IndexOf (v2) == 2);
  304. Assert.True (r.Subviews.IndexOf (v3) == 1);
  305. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  306. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  307. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  308. }
  309. [Fact]
  310. public void TabIndex_Set_CanFocus_ValidValues ()
  311. {
  312. var r = new View ();
  313. var v1 = new View () { CanFocus = true };
  314. var v2 = new View () { CanFocus = true };
  315. var v3 = new View () { CanFocus = true };
  316. r.Add (v1, v2, v3);
  317. v1.TabIndex = 1;
  318. Assert.True (r.Subviews.IndexOf (v1) == 0);
  319. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  320. v1.TabIndex = 2;
  321. Assert.True (r.Subviews.IndexOf (v1) == 0);
  322. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  323. }
  324. [Fact]
  325. public void TabIndex_Set_CanFocus_HigherValues ()
  326. {
  327. var r = new View ();
  328. var v1 = new View () { CanFocus = true };
  329. var v2 = new View () { CanFocus = true };
  330. var v3 = new View () { CanFocus = true };
  331. r.Add (v1, v2, v3);
  332. v1.TabIndex = 3;
  333. Assert.True (r.Subviews.IndexOf (v1) == 0);
  334. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  335. }
  336. [Fact]
  337. public void TabIndex_Set_CanFocus_LowerValues ()
  338. {
  339. var r = new View ();
  340. var v1 = new View () { CanFocus = true };
  341. var v2 = new View () { CanFocus = true };
  342. var v3 = new View () { CanFocus = true };
  343. r.Add (v1, v2, v3);
  344. v1.TabIndex = -1;
  345. Assert.True (r.Subviews.IndexOf (v1) == 0);
  346. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  347. }
  348. [Fact]
  349. public void TabIndex_Set_CanFocus_False ()
  350. {
  351. var r = new View ();
  352. var v1 = new View () { CanFocus = true };
  353. var v2 = new View () { CanFocus = true };
  354. var v3 = new View () { CanFocus = true };
  355. r.Add (v1, v2, v3);
  356. v1.CanFocus = false;
  357. v1.TabIndex = 0;
  358. Assert.True (r.Subviews.IndexOf (v1) == 0);
  359. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  360. Assert.Equal (-1, v1.TabIndex);
  361. }
  362. [Fact]
  363. public void TabIndex_Set_CanFocus_False_To_True ()
  364. {
  365. var r = new View ();
  366. var v1 = new View ();
  367. var v2 = new View () { CanFocus = true };
  368. var v3 = new View () { CanFocus = true };
  369. r.Add (v1, v2, v3);
  370. v1.CanFocus = true;
  371. v1.TabIndex = 1;
  372. Assert.True (r.Subviews.IndexOf (v1) == 0);
  373. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  374. }
  375. [Fact]
  376. public void TabStop_And_CanFocus_Are_All_True ()
  377. {
  378. var r = new View ();
  379. var v1 = new View () { CanFocus = true };
  380. var v2 = new View () { CanFocus = true };
  381. var v3 = new View () { CanFocus = true };
  382. r.Add (v1, v2, v3);
  383. r.FocusNext ();
  384. Assert.True (v1.HasFocus);
  385. Assert.False (v2.HasFocus);
  386. Assert.False (v3.HasFocus);
  387. r.FocusNext ();
  388. Assert.False (v1.HasFocus);
  389. Assert.True (v2.HasFocus);
  390. Assert.False (v3.HasFocus);
  391. r.FocusNext ();
  392. Assert.False (v1.HasFocus);
  393. Assert.False (v2.HasFocus);
  394. Assert.True (v3.HasFocus);
  395. }
  396. [Fact]
  397. public void TabStop_Are_All_True_And_CanFocus_Are_All_False ()
  398. {
  399. var r = new View ();
  400. var v1 = new View ();
  401. var v2 = new View ();
  402. var v3 = new View ();
  403. r.Add (v1, v2, v3);
  404. r.FocusNext ();
  405. Assert.False (v1.HasFocus);
  406. Assert.False (v2.HasFocus);
  407. Assert.False (v3.HasFocus);
  408. r.FocusNext ();
  409. Assert.False (v1.HasFocus);
  410. Assert.False (v2.HasFocus);
  411. Assert.False (v3.HasFocus);
  412. r.FocusNext ();
  413. Assert.False (v1.HasFocus);
  414. Assert.False (v2.HasFocus);
  415. Assert.False (v3.HasFocus);
  416. }
  417. [Fact]
  418. public void TabStop_Are_All_False_And_CanFocus_Are_All_True ()
  419. {
  420. var r = new View ();
  421. var v1 = new View () { CanFocus = true, TabStop = false };
  422. var v2 = new View () { CanFocus = true, TabStop = false };
  423. var v3 = new View () { CanFocus = true, TabStop = false };
  424. r.Add (v1, v2, v3);
  425. r.FocusNext ();
  426. Assert.False (v1.HasFocus);
  427. Assert.False (v2.HasFocus);
  428. Assert.False (v3.HasFocus);
  429. r.FocusNext ();
  430. Assert.False (v1.HasFocus);
  431. Assert.False (v2.HasFocus);
  432. Assert.False (v3.HasFocus);
  433. r.FocusNext ();
  434. Assert.False (v1.HasFocus);
  435. Assert.False (v2.HasFocus);
  436. Assert.False (v3.HasFocus);
  437. }
  438. [Fact]
  439. public void TabStop_And_CanFocus_Mixed_And_BothFalse ()
  440. {
  441. var r = new View ();
  442. var v1 = new View () { CanFocus = true, TabStop = false };
  443. var v2 = new View () { CanFocus = false, TabStop = true };
  444. var v3 = new View () { CanFocus = false, TabStop = false };
  445. r.Add (v1, v2, v3);
  446. r.FocusNext ();
  447. Assert.False (v1.HasFocus);
  448. Assert.False (v2.HasFocus);
  449. Assert.False (v3.HasFocus);
  450. r.FocusNext ();
  451. Assert.False (v1.HasFocus);
  452. Assert.False (v2.HasFocus);
  453. Assert.False (v3.HasFocus);
  454. r.FocusNext ();
  455. Assert.False (v1.HasFocus);
  456. Assert.False (v2.HasFocus);
  457. Assert.False (v3.HasFocus);
  458. }
  459. [Fact]
  460. public void TabStop_All_True_And_Changing_CanFocus_Later ()
  461. {
  462. var r = new View ();
  463. var v1 = new View ();
  464. var v2 = new View ();
  465. var v3 = new View ();
  466. r.Add (v1, v2, v3);
  467. r.FocusNext ();
  468. Assert.False (v1.HasFocus);
  469. Assert.False (v2.HasFocus);
  470. Assert.False (v3.HasFocus);
  471. v1.CanFocus = true;
  472. r.FocusNext ();
  473. Assert.True (v1.HasFocus);
  474. Assert.False (v2.HasFocus);
  475. Assert.False (v3.HasFocus);
  476. v2.CanFocus = true;
  477. r.FocusNext ();
  478. Assert.False (v1.HasFocus);
  479. Assert.True (v2.HasFocus);
  480. Assert.False (v3.HasFocus);
  481. v3.CanFocus = true;
  482. r.FocusNext ();
  483. Assert.False (v1.HasFocus);
  484. Assert.False (v2.HasFocus);
  485. Assert.True (v3.HasFocus);
  486. }
  487. [Fact]
  488. public void TabStop_All_False_And_All_True_And_Changing_TabStop_Later ()
  489. {
  490. var r = new View ();
  491. var v1 = new View () { CanFocus = true, TabStop = false };
  492. var v2 = new View () { CanFocus = true, TabStop = false };
  493. var v3 = new View () { CanFocus = true, TabStop = false };
  494. r.Add (v1, v2, v3);
  495. r.FocusNext ();
  496. Assert.False (v1.HasFocus);
  497. Assert.False (v2.HasFocus);
  498. Assert.False (v3.HasFocus);
  499. v1.TabStop = true;
  500. r.FocusNext ();
  501. Assert.True (v1.HasFocus);
  502. Assert.False (v2.HasFocus);
  503. Assert.False (v3.HasFocus);
  504. v2.TabStop = true;
  505. r.FocusNext ();
  506. Assert.False (v1.HasFocus);
  507. Assert.True (v2.HasFocus);
  508. Assert.False (v3.HasFocus);
  509. v3.TabStop = true;
  510. r.FocusNext ();
  511. Assert.False (v1.HasFocus);
  512. Assert.False (v2.HasFocus);
  513. Assert.True (v3.HasFocus);
  514. }
  515. [Fact]
  516. public void CanFocus_Set_Changes_TabIndex_And_TabStop ()
  517. {
  518. var r = new View ();
  519. var v1 = new View ("1");
  520. var v2 = new View ("2");
  521. var v3 = new View ("3");
  522. r.Add (v1, v2, v3);
  523. v2.CanFocus = true;
  524. Assert.Equal (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  525. Assert.Equal (0, v2.TabIndex);
  526. Assert.True (v2.TabStop);
  527. v1.CanFocus = true;
  528. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  529. Assert.Equal (1, v1.TabIndex);
  530. Assert.True (v1.TabStop);
  531. v1.TabIndex = 2;
  532. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  533. Assert.Equal (1, v1.TabIndex);
  534. v3.CanFocus = true;
  535. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  536. Assert.Equal (1, v1.TabIndex);
  537. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  538. Assert.Equal (2, v3.TabIndex);
  539. Assert.True (v3.TabStop);
  540. v2.CanFocus = false;
  541. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  542. Assert.Equal (1, v1.TabIndex);
  543. Assert.True (v1.TabStop);
  544. Assert.NotEqual (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  545. Assert.Equal (-1, v2.TabIndex);
  546. Assert.False (v2.TabStop);
  547. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  548. Assert.Equal (2, v3.TabIndex);
  549. Assert.True (v3.TabStop);
  550. }
  551. [Fact]
  552. public void Initialized_Event_Comparing_With_Added_Event ()
  553. {
  554. Application.Init (new FakeDriver ());
  555. var t = new Toplevel () { Id = "0", };
  556. var w = new Window () { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  557. var v1 = new View () { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  558. var v2 = new View () { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  559. var sv1 = new View () { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  560. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  561. w.Added += (e) => {
  562. Assert.Equal (e.Frame.Width, w.Frame.Width);
  563. Assert.Equal (e.Frame.Height, w.Frame.Height);
  564. };
  565. v1.Added += (e) => {
  566. Assert.Equal (e.Frame.Width, v1.Frame.Width);
  567. Assert.Equal (e.Frame.Height, v1.Frame.Height);
  568. };
  569. v2.Added += (e) => {
  570. Assert.Equal (e.Frame.Width, v2.Frame.Width);
  571. Assert.Equal (e.Frame.Height, v2.Frame.Height);
  572. };
  573. sv1.Added += (e) => {
  574. Assert.Equal (e.Frame.Width, sv1.Frame.Width);
  575. Assert.Equal (e.Frame.Height, sv1.Frame.Height);
  576. };
  577. t.Initialized += (s, e) => {
  578. tc++;
  579. Assert.Equal (1, tc);
  580. Assert.Equal (1, wc);
  581. Assert.Equal (1, v1c);
  582. Assert.Equal (1, v2c);
  583. Assert.Equal (1, sv1c);
  584. Assert.True (t.CanFocus);
  585. Assert.True (w.CanFocus);
  586. Assert.False (v1.CanFocus);
  587. Assert.False (v2.CanFocus);
  588. Assert.False (sv1.CanFocus);
  589. Application.Refresh ();
  590. };
  591. w.Initialized += (s, e) => {
  592. wc++;
  593. Assert.Equal (t.Frame.Width, w.Frame.Width);
  594. Assert.Equal (t.Frame.Height, w.Frame.Height);
  595. };
  596. v1.Initialized += (s, e) => {
  597. v1c++;
  598. Assert.Equal (t.Frame.Width, v1.Frame.Width);
  599. Assert.Equal (t.Frame.Height, v1.Frame.Height);
  600. };
  601. v2.Initialized += (s, e) => {
  602. v2c++;
  603. Assert.Equal (t.Frame.Width, v2.Frame.Width);
  604. Assert.Equal (t.Frame.Height, v2.Frame.Height);
  605. };
  606. sv1.Initialized += (s, e) => {
  607. sv1c++;
  608. Assert.Equal (t.Frame.Width, sv1.Frame.Width);
  609. Assert.Equal (t.Frame.Height, sv1.Frame.Height);
  610. Assert.False (sv1.CanFocus);
  611. Assert.Throws<InvalidOperationException> (() => sv1.CanFocus = true);
  612. Assert.False (sv1.CanFocus);
  613. };
  614. v1.Add (sv1);
  615. w.Add (v1, v2);
  616. t.Add (w);
  617. Application.Iteration = () => {
  618. Application.Refresh ();
  619. t.Running = false;
  620. };
  621. Application.Run (t);
  622. Application.Shutdown ();
  623. Assert.Equal (1, tc);
  624. Assert.Equal (1, wc);
  625. Assert.Equal (1, v1c);
  626. Assert.Equal (1, v2c);
  627. Assert.Equal (1, sv1c);
  628. Assert.True (t.CanFocus);
  629. Assert.True (w.CanFocus);
  630. Assert.False (v1.CanFocus);
  631. Assert.False (v2.CanFocus);
  632. Assert.False (sv1.CanFocus);
  633. v1.CanFocus = true;
  634. Assert.False (sv1.CanFocus); // False because sv1 was disposed and it isn't a subview of v1.
  635. }
  636. [Fact]
  637. public void Initialized_Event_Will_Be_Invoked_When_Added_Dynamically ()
  638. {
  639. Application.Init (new FakeDriver ());
  640. var t = new Toplevel () { Id = "0", };
  641. var w = new Window () { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  642. var v1 = new View () { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  643. var v2 = new View () { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  644. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  645. t.Initialized += (s, e) => {
  646. tc++;
  647. Assert.Equal (1, tc);
  648. Assert.Equal (1, wc);
  649. Assert.Equal (1, v1c);
  650. Assert.Equal (1, v2c);
  651. Assert.Equal (0, sv1c); // Added after t in the Application.Iteration.
  652. Assert.True (t.CanFocus);
  653. Assert.True (w.CanFocus);
  654. Assert.False (v1.CanFocus);
  655. Assert.False (v2.CanFocus);
  656. Application.Refresh ();
  657. };
  658. w.Initialized += (s, e) => {
  659. wc++;
  660. Assert.Equal (t.Frame.Width, w.Frame.Width);
  661. Assert.Equal (t.Frame.Height, w.Frame.Height);
  662. };
  663. v1.Initialized += (s, e) => {
  664. v1c++;
  665. Assert.Equal (t.Frame.Width, v1.Frame.Width);
  666. Assert.Equal (t.Frame.Height, v1.Frame.Height);
  667. };
  668. v2.Initialized += (s, e) => {
  669. v2c++;
  670. Assert.Equal (t.Frame.Width, v2.Frame.Width);
  671. Assert.Equal (t.Frame.Height, v2.Frame.Height);
  672. };
  673. w.Add (v1, v2);
  674. t.Add (w);
  675. Application.Iteration = () => {
  676. var sv1 = new View () { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  677. sv1.Initialized += (s, e) => {
  678. sv1c++;
  679. Assert.NotEqual (t.Frame.Width, sv1.Frame.Width);
  680. Assert.NotEqual (t.Frame.Height, sv1.Frame.Height);
  681. Assert.False (sv1.CanFocus);
  682. Assert.Throws<InvalidOperationException> (() => sv1.CanFocus = true);
  683. Assert.False (sv1.CanFocus);
  684. };
  685. v1.Add (sv1);
  686. Application.Refresh ();
  687. t.Running = false;
  688. };
  689. Application.Run (t);
  690. Application.Shutdown ();
  691. Assert.Equal (1, tc);
  692. Assert.Equal (1, wc);
  693. Assert.Equal (1, v1c);
  694. Assert.Equal (1, v2c);
  695. Assert.Equal (1, sv1c);
  696. Assert.True (t.CanFocus);
  697. Assert.True (w.CanFocus);
  698. Assert.False (v1.CanFocus);
  699. Assert.False (v2.CanFocus);
  700. }
  701. [Fact]
  702. [AutoInitShutdown]
  703. public void CanFocus_Faced_With_Container ()
  704. {
  705. var t = new Toplevel ();
  706. var w = new Window ();
  707. var f = new FrameView ();
  708. var v = new View () { CanFocus = true };
  709. f.Add (v);
  710. w.Add (f);
  711. t.Add (w);
  712. Assert.True (t.CanFocus);
  713. Assert.True (w.CanFocus);
  714. Assert.True (f.CanFocus);
  715. Assert.True (v.CanFocus);
  716. f.CanFocus = false;
  717. Assert.False (f.CanFocus);
  718. Assert.True (v.CanFocus);
  719. v.CanFocus = false;
  720. Assert.False (f.CanFocus);
  721. Assert.False (v.CanFocus);
  722. v.CanFocus = true;
  723. Assert.False (f.CanFocus);
  724. Assert.True (v.CanFocus);
  725. }
  726. [Fact]
  727. public void CanFocus_Faced_With_Container_Before_Run ()
  728. {
  729. Application.Init (new FakeDriver ());
  730. var t = Application.Top;
  731. var w = new Window ("w");
  732. var f = new FrameView ("f");
  733. var v = new View ("v") { CanFocus = true };
  734. f.Add (v);
  735. w.Add (f);
  736. t.Add (w);
  737. Assert.True (t.CanFocus);
  738. Assert.True (w.CanFocus);
  739. Assert.True (f.CanFocus);
  740. Assert.True (v.CanFocus);
  741. f.CanFocus = false;
  742. Assert.False (f.CanFocus);
  743. Assert.True (v.CanFocus);
  744. v.CanFocus = false;
  745. Assert.False (f.CanFocus);
  746. Assert.False (v.CanFocus);
  747. v.CanFocus = true;
  748. Assert.False (f.CanFocus);
  749. Assert.True (v.CanFocus);
  750. Application.Iteration += () => Application.RequestStop ();
  751. Application.Run ();
  752. Application.Shutdown ();
  753. }
  754. [Fact]
  755. public void CanFocus_Faced_With_Container_After_Run ()
  756. {
  757. Application.Init (new FakeDriver ());
  758. var t = Application.Top;
  759. var w = new Window ("w");
  760. var f = new FrameView ("f");
  761. var v = new View ("v") { CanFocus = true };
  762. f.Add (v);
  763. w.Add (f);
  764. t.Add (w);
  765. t.Ready += () => {
  766. Assert.True (t.CanFocus);
  767. Assert.True (w.CanFocus);
  768. Assert.True (f.CanFocus);
  769. Assert.True (v.CanFocus);
  770. f.CanFocus = false;
  771. Assert.False (f.CanFocus);
  772. Assert.False (v.CanFocus);
  773. v.CanFocus = false;
  774. Assert.False (f.CanFocus);
  775. Assert.False (v.CanFocus);
  776. Assert.Throws<InvalidOperationException> (() => v.CanFocus = true);
  777. Assert.False (f.CanFocus);
  778. Assert.False (v.CanFocus);
  779. f.CanFocus = true;
  780. Assert.True (f.CanFocus);
  781. Assert.True (v.CanFocus);
  782. };
  783. Application.Iteration += () => Application.RequestStop ();
  784. Application.Run ();
  785. Application.Shutdown ();
  786. }
  787. [Fact]
  788. public void CanFocus_Container_ToFalse_Turns_All_Subviews_ToFalse_Too ()
  789. {
  790. Application.Init (new FakeDriver ());
  791. var t = Application.Top;
  792. var w = new Window ("w");
  793. var f = new FrameView ("f");
  794. var v1 = new View ("v1") { CanFocus = true };
  795. var v2 = new View ("v2") { CanFocus = true };
  796. f.Add (v1, v2);
  797. w.Add (f);
  798. t.Add (w);
  799. t.Ready += () => {
  800. Assert.True (t.CanFocus);
  801. Assert.True (w.CanFocus);
  802. Assert.True (f.CanFocus);
  803. Assert.True (v1.CanFocus);
  804. Assert.True (v2.CanFocus);
  805. w.CanFocus = false;
  806. Assert.False (w.CanFocus);
  807. Assert.False (f.CanFocus);
  808. Assert.False (v1.CanFocus);
  809. Assert.False (v2.CanFocus);
  810. };
  811. Application.Iteration += () => Application.RequestStop ();
  812. Application.Run ();
  813. Application.Shutdown ();
  814. }
  815. [Fact]
  816. public void CanFocus_Container_Toggling_All_Subviews_To_Old_Value_When_Is_True ()
  817. {
  818. Application.Init (new FakeDriver ());
  819. var t = Application.Top;
  820. var w = new Window ("w");
  821. var f = new FrameView ("f");
  822. var v1 = new View ("v1");
  823. var v2 = new View ("v2") { CanFocus = true };
  824. f.Add (v1, v2);
  825. w.Add (f);
  826. t.Add (w);
  827. t.Ready += () => {
  828. Assert.True (t.CanFocus);
  829. Assert.True (w.CanFocus);
  830. Assert.True (f.CanFocus);
  831. Assert.False (v1.CanFocus);
  832. Assert.True (v2.CanFocus);
  833. w.CanFocus = false;
  834. Assert.False (w.CanFocus);
  835. Assert.False (f.CanFocus);
  836. Assert.False (v1.CanFocus);
  837. Assert.False (v2.CanFocus);
  838. w.CanFocus = true;
  839. Assert.True (w.CanFocus);
  840. Assert.True (f.CanFocus);
  841. Assert.False (v1.CanFocus);
  842. Assert.True (v2.CanFocus);
  843. };
  844. Application.Iteration += () => Application.RequestStop ();
  845. Application.Run ();
  846. Application.Shutdown ();
  847. }
  848. [Fact]
  849. public void Navigation_With_Null_Focused_View ()
  850. {
  851. // Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
  852. Application.Init (new FakeDriver ());
  853. Application.Top.Ready += () => {
  854. Assert.Null (Application.Top.Focused);
  855. };
  856. // Keyboard navigation with tab
  857. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('\t', ConsoleKey.Tab, false, false, false));
  858. Application.Iteration += () => Application.RequestStop ();
  859. Application.Run ();
  860. Application.Shutdown ();
  861. }
  862. [Fact]
  863. public void Multi_Thread_Toplevels ()
  864. {
  865. Application.Init (new FakeDriver ());
  866. var t = Application.Top;
  867. var w = new Window ();
  868. t.Add (w);
  869. int count = 0, count1 = 0, count2 = 0;
  870. bool log = false, log1 = false, log2 = false;
  871. bool fromTopStillKnowFirstIsRunning = false;
  872. bool fromTopStillKnowSecondIsRunning = false;
  873. bool fromFirstStillKnowSecondIsRunning = false;
  874. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  875. count++;
  876. if (count1 == 5) {
  877. log1 = true;
  878. }
  879. if (count1 == 14 && count2 == 10 && count == 15) { // count2 is already stopped
  880. fromTopStillKnowFirstIsRunning = true;
  881. }
  882. if (count1 == 7 && count2 == 7 && count == 8) {
  883. fromTopStillKnowSecondIsRunning = true;
  884. }
  885. if (count == 30) {
  886. Assert.Equal (30, count);
  887. Assert.Equal (20, count1);
  888. Assert.Equal (10, count2);
  889. Assert.True (log);
  890. Assert.True (log1);
  891. Assert.True (log2);
  892. Assert.True (fromTopStillKnowFirstIsRunning);
  893. Assert.True (fromTopStillKnowSecondIsRunning);
  894. Assert.True (fromFirstStillKnowSecondIsRunning);
  895. Application.RequestStop ();
  896. return false;
  897. }
  898. return true;
  899. });
  900. t.Ready += FirstDialogToplevel;
  901. void FirstDialogToplevel ()
  902. {
  903. var od = new OpenDialog ();
  904. od.Ready += SecoundDialogToplevel;
  905. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  906. count1++;
  907. if (count2 == 5) {
  908. log2 = true;
  909. }
  910. if (count2 == 4 && count1 == 5 && count == 5) {
  911. fromFirstStillKnowSecondIsRunning = true;
  912. }
  913. if (count1 == 20) {
  914. Assert.Equal (20, count1);
  915. Application.RequestStop ();
  916. return false;
  917. }
  918. return true;
  919. });
  920. Application.Run (od);
  921. }
  922. void SecoundDialogToplevel ()
  923. {
  924. var d = new Dialog ();
  925. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  926. count2++;
  927. if (count < 30) {
  928. log = true;
  929. }
  930. if (count2 == 10) {
  931. Assert.Equal (10, count2);
  932. Application.RequestStop ();
  933. return false;
  934. }
  935. return true;
  936. });
  937. Application.Run (d);
  938. }
  939. Application.Run ();
  940. Application.Shutdown ();
  941. }
  942. [Fact]
  943. public void KeyPress_Handled_To_True_Prevents_Changes ()
  944. {
  945. Application.Init (new FakeDriver ());
  946. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('N', ConsoleKey.N, false, false, false));
  947. var top = Application.Top;
  948. var text = new TextField ("");
  949. text.KeyPress += (e) => {
  950. e.Handled = true;
  951. Assert.True (e.Handled);
  952. Assert.Equal (Key.N, e.KeyEvent.Key);
  953. };
  954. top.Add (text);
  955. Application.Iteration += () => {
  956. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('N', ConsoleKey.N, false, false, false));
  957. Assert.Equal ("", text.Text);
  958. Application.RequestStop ();
  959. };
  960. Application.Run ();
  961. // Shutdown must be called to safely clean up Application if Init has been called
  962. Application.Shutdown ();
  963. }
  964. [Theory]
  965. [InlineData (1)]
  966. [InlineData (2)]
  967. [InlineData (3)]
  968. public void LabelChangeText_RendersCorrectly_Constructors (int choice)
  969. {
  970. var driver = new FakeDriver ();
  971. Application.Init (driver);
  972. try {
  973. // Create a label with a short text
  974. Label lbl;
  975. var text = "test";
  976. if (choice == 1) {
  977. // An object initializer should call the default constructor.
  978. lbl = new Label { Text = text };
  979. } else if (choice == 2) {
  980. // Calling the default constructor followed by the object initializer.
  981. lbl = new Label () { Text = text };
  982. } else {
  983. // Calling the Text constructor.
  984. lbl = new Label (text);
  985. }
  986. Application.Top.Add (lbl);
  987. Application.Top.Redraw (Application.Top.Bounds);
  988. // should have the initial text
  989. Assert.Equal ('t', driver.Contents [0, 0, 0]);
  990. Assert.Equal ('e', driver.Contents [0, 1, 0]);
  991. Assert.Equal ('s', driver.Contents [0, 2, 0]);
  992. Assert.Equal ('t', driver.Contents [0, 3, 0]);
  993. Assert.Equal (' ', driver.Contents [0, 4, 0]);
  994. } finally {
  995. Application.Shutdown ();
  996. }
  997. }
  998. [Fact]
  999. [AutoInitShutdown]
  1000. public void Internal_Tests ()
  1001. {
  1002. Assert.Equal (new [] { View.Direction.Forward, View.Direction.Backward },
  1003. Enum.GetValues (typeof (View.Direction)));
  1004. var rect = new Rect (1, 1, 10, 1);
  1005. var view = new View (rect);
  1006. var top = Application.Top;
  1007. top.Add (view);
  1008. Assert.Equal (View.Direction.Forward, view.FocusDirection);
  1009. view.FocusDirection = View.Direction.Backward;
  1010. Assert.Equal (View.Direction.Backward, view.FocusDirection);
  1011. Assert.Empty (view.InternalSubviews);
  1012. Assert.Equal (new Rect (new Point (0, 0), rect.Size), view.NeedDisplay);
  1013. Assert.True (view.LayoutNeeded);
  1014. Assert.False (view.ChildNeedsDisplay);
  1015. Assert.False (view.addingView);
  1016. view.addingView = true;
  1017. Assert.True (view.addingView);
  1018. view.ViewToScreen (0, 0, out int rcol, out int rrow);
  1019. Assert.Equal (1, rcol);
  1020. Assert.Equal (1, rrow);
  1021. Assert.Equal (rect, view.ViewToScreen (view.Bounds));
  1022. Assert.Equal (top.Bounds, view.ScreenClip (top.Bounds));
  1023. view.Width = Dim.Fill ();
  1024. view.Height = Dim.Fill ();
  1025. Assert.Equal (10, view.Bounds.Width);
  1026. Assert.Equal (1, view.Bounds.Height);
  1027. view.SetRelativeLayout (top.Bounds);
  1028. Assert.Equal (79, view.Bounds.Width);
  1029. Assert.Equal (24, view.Bounds.Height);
  1030. view.X = 0;
  1031. view.Y = 0;
  1032. view.SetRelativeLayout (top.Bounds);
  1033. Assert.Equal (80, view.Bounds.Width);
  1034. Assert.Equal (25, view.Bounds.Height);
  1035. bool layoutStarted = false;
  1036. view.LayoutStarted += (_) => layoutStarted = true;
  1037. view.OnLayoutStarted (null);
  1038. Assert.True (layoutStarted);
  1039. view.LayoutComplete += (_) => layoutStarted = false;
  1040. view.OnLayoutComplete (null);
  1041. Assert.False (layoutStarted);
  1042. view.X = Pos.Center () - 41;
  1043. view.Y = Pos.Center () - 13;
  1044. view.SetRelativeLayout (top.Bounds);
  1045. view.ViewToScreen (0, 0, out rcol, out rrow);
  1046. Assert.Equal (-41, rcol);
  1047. Assert.Equal (-13, rrow);
  1048. }
  1049. [Fact]
  1050. [AutoInitShutdown]
  1051. public void Enabled_False_Sets_HasFocus_To_False ()
  1052. {
  1053. var wasClicked = false;
  1054. var view = new Button ("Click Me");
  1055. view.Clicked += () => wasClicked = !wasClicked;
  1056. Application.Top.Add (view);
  1057. view.ProcessKey (new KeyEvent (Key.Enter, null));
  1058. Assert.True (wasClicked);
  1059. view.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1060. Assert.False (wasClicked);
  1061. Assert.True (view.Enabled);
  1062. Assert.True (view.CanFocus);
  1063. Assert.True (view.HasFocus);
  1064. view.Enabled = false;
  1065. view.ProcessKey (new KeyEvent (Key.Enter, null));
  1066. Assert.False (wasClicked);
  1067. view.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1068. Assert.False (wasClicked);
  1069. Assert.False (view.Enabled);
  1070. Assert.True (view.CanFocus);
  1071. Assert.False (view.HasFocus);
  1072. view.SetFocus ();
  1073. Assert.False (view.HasFocus);
  1074. }
  1075. [Fact]
  1076. [AutoInitShutdown]
  1077. public void Enabled_Sets_Also_Sets_Subviews ()
  1078. {
  1079. var wasClicked = false;
  1080. var button = new Button ("Click Me");
  1081. button.Clicked += () => wasClicked = !wasClicked;
  1082. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  1083. win.Add (button);
  1084. Application.Top.Add (win);
  1085. var iterations = 0;
  1086. Application.Iteration += () => {
  1087. iterations++;
  1088. button.ProcessKey (new KeyEvent (Key.Enter, null));
  1089. Assert.True (wasClicked);
  1090. button.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1091. Assert.False (wasClicked);
  1092. Assert.True (button.Enabled);
  1093. Assert.True (button.CanFocus);
  1094. Assert.True (button.HasFocus);
  1095. Assert.True (win.Enabled);
  1096. Assert.True (win.CanFocus);
  1097. Assert.True (win.HasFocus);
  1098. win.Enabled = false;
  1099. button.ProcessKey (new KeyEvent (Key.Enter, null));
  1100. Assert.False (wasClicked);
  1101. button.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1102. Assert.False (wasClicked);
  1103. Assert.False (button.Enabled);
  1104. Assert.True (button.CanFocus);
  1105. Assert.False (button.HasFocus);
  1106. Assert.False (win.Enabled);
  1107. Assert.True (win.CanFocus);
  1108. Assert.False (win.HasFocus);
  1109. button.SetFocus ();
  1110. Assert.False (button.HasFocus);
  1111. Assert.False (win.HasFocus);
  1112. win.SetFocus ();
  1113. Assert.False (button.HasFocus);
  1114. Assert.False (win.HasFocus);
  1115. win.Enabled = true;
  1116. win.FocusFirst ();
  1117. Assert.True (button.HasFocus);
  1118. Assert.True (win.HasFocus);
  1119. Application.RequestStop ();
  1120. };
  1121. Application.Run ();
  1122. Assert.Equal (1, iterations);
  1123. }
  1124. [Fact]
  1125. [AutoInitShutdown]
  1126. public void Visible_Sets_Also_Sets_Subviews ()
  1127. {
  1128. var button = new Button ("Click Me");
  1129. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  1130. win.Add (button);
  1131. var top = Application.Top;
  1132. top.Add (win);
  1133. var iterations = 0;
  1134. Application.Iteration += () => {
  1135. iterations++;
  1136. Assert.True (button.Visible);
  1137. Assert.True (button.CanFocus);
  1138. Assert.True (button.HasFocus);
  1139. Assert.True (win.Visible);
  1140. Assert.True (win.CanFocus);
  1141. Assert.True (win.HasFocus);
  1142. Assert.True (RunesCount () > 0);
  1143. win.Visible = false;
  1144. Assert.True (button.Visible);
  1145. Assert.True (button.CanFocus);
  1146. Assert.False (button.HasFocus);
  1147. Assert.False (win.Visible);
  1148. Assert.True (win.CanFocus);
  1149. Assert.False (win.HasFocus);
  1150. button.SetFocus ();
  1151. Assert.False (button.HasFocus);
  1152. Assert.False (win.HasFocus);
  1153. win.SetFocus ();
  1154. Assert.False (button.HasFocus);
  1155. Assert.False (win.HasFocus);
  1156. top.Redraw (top.Bounds);
  1157. Assert.True (RunesCount () == 0);
  1158. win.Visible = true;
  1159. win.FocusFirst ();
  1160. Assert.True (button.HasFocus);
  1161. Assert.True (win.HasFocus);
  1162. top.Redraw (top.Bounds);
  1163. Assert.True (RunesCount () > 0);
  1164. Application.RequestStop ();
  1165. };
  1166. Application.Run ();
  1167. Assert.Equal (1, iterations);
  1168. int RunesCount ()
  1169. {
  1170. var contents = ((FakeDriver)Application.Driver).Contents;
  1171. var runesCount = 0;
  1172. for (int i = 0; i < Application.Driver.Rows; i++) {
  1173. for (int j = 0; j < Application.Driver.Cols; j++) {
  1174. if (contents [i, j, 0] != ' ') {
  1175. runesCount++;
  1176. }
  1177. }
  1178. }
  1179. return runesCount;
  1180. }
  1181. }
  1182. [Fact]
  1183. [AutoInitShutdown]
  1184. public void GetTopSuperView_Test ()
  1185. {
  1186. var v1 = new View ();
  1187. var fv1 = new FrameView ();
  1188. fv1.Add (v1);
  1189. var tf1 = new TextField ();
  1190. var w1 = new Window ();
  1191. w1.Add (fv1, tf1);
  1192. var top1 = new Toplevel ();
  1193. top1.Add (w1);
  1194. var v2 = new View ();
  1195. var fv2 = new FrameView ();
  1196. fv2.Add (v2);
  1197. var tf2 = new TextField ();
  1198. var w2 = new Window ();
  1199. w2.Add (fv2, tf2);
  1200. var top2 = new Toplevel ();
  1201. top2.Add (w2);
  1202. Assert.Equal (top1, v1.GetTopSuperView ());
  1203. Assert.Equal (top2, v2.GetTopSuperView ());
  1204. }
  1205. [Fact]
  1206. [AutoInitShutdown]
  1207. public void CanFocus_Sets_To_False_Does_Not_Sets_HasFocus_To_True ()
  1208. {
  1209. var view = new View () { CanFocus = true };
  1210. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  1211. win.Add (view);
  1212. Application.Top.Add (win);
  1213. Application.Begin (Application.Top);
  1214. Assert.True (view.CanFocus);
  1215. Assert.True (view.HasFocus);
  1216. view.CanFocus = false;
  1217. Assert.False (view.CanFocus);
  1218. Assert.False (view.HasFocus);
  1219. Assert.Null (Application.Current.Focused);
  1220. Assert.Null (Application.Current.MostFocused);
  1221. }
  1222. [Fact]
  1223. [AutoInitShutdown]
  1224. public void CanFocus_Sets_To_False_On_Single_View_Focus_View_On_Another_Toplevel ()
  1225. {
  1226. var view1 = new View () { Width = 10, Height = 1, CanFocus = true };
  1227. var win1 = new Window () { Width = Dim.Percent (50), Height = Dim.Fill () };
  1228. win1.Add (view1);
  1229. var view2 = new View () { Width = 20, Height = 2, CanFocus = true };
  1230. var win2 = new Window () { X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  1231. win2.Add (view2);
  1232. Application.Top.Add (win1, win2);
  1233. Application.Begin (Application.Top);
  1234. Assert.True (view1.CanFocus);
  1235. Assert.True (view1.HasFocus);
  1236. Assert.True (view2.CanFocus);
  1237. Assert.True (view2.HasFocus);
  1238. view1.CanFocus = false;
  1239. Assert.False (view1.CanFocus);
  1240. Assert.False (view1.HasFocus);
  1241. Assert.Equal (win2, Application.Current.Focused);
  1242. Assert.Equal (view2, Application.Current.MostFocused);
  1243. }
  1244. [Fact]
  1245. [AutoInitShutdown]
  1246. public void CanFocus_Sets_To_False_With_Two_Views_Focus_Another_View_On_The_Same_Toplevel ()
  1247. {
  1248. var view1 = new View () { Width = 10, Height = 1, CanFocus = true };
  1249. var view12 = new View () { Y = 5, Width = 10, Height = 1, CanFocus = true };
  1250. var win1 = new Window () { Width = Dim.Percent (50), Height = Dim.Fill () };
  1251. win1.Add (view1, view12);
  1252. var view2 = new View () { Width = 20, Height = 2, CanFocus = true };
  1253. var win2 = new Window () { X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  1254. win2.Add (view2);
  1255. Application.Top.Add (win1, win2);
  1256. Application.Begin (Application.Top);
  1257. Assert.True (view1.CanFocus);
  1258. Assert.True (view1.HasFocus);
  1259. Assert.True (view2.CanFocus);
  1260. Assert.True (view2.HasFocus);
  1261. view1.CanFocus = false;
  1262. Assert.False (view1.CanFocus);
  1263. Assert.False (view1.HasFocus);
  1264. Assert.Equal (win1, Application.Current.Focused);
  1265. Assert.Equal (view12, Application.Current.MostFocused);
  1266. }
  1267. [Fact]
  1268. [AutoInitShutdown]
  1269. public void CanFocus_Sets_To_False_On_Toplevel_Focus_View_On_Another_Toplevel ()
  1270. {
  1271. var view1 = new View () { Width = 10, Height = 1, CanFocus = true };
  1272. var win1 = new Window () { Width = Dim.Percent (50), Height = Dim.Fill () };
  1273. win1.Add (view1);
  1274. var view2 = new View () { Width = 20, Height = 2, CanFocus = true };
  1275. var win2 = new Window () { X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  1276. win2.Add (view2);
  1277. Application.Top.Add (win1, win2);
  1278. Application.Begin (Application.Top);
  1279. Assert.True (view1.CanFocus);
  1280. Assert.True (view1.HasFocus);
  1281. Assert.True (view2.CanFocus);
  1282. Assert.True (view2.HasFocus);
  1283. win1.CanFocus = false;
  1284. Assert.False (view1.CanFocus);
  1285. Assert.False (view1.HasFocus);
  1286. Assert.False (win1.CanFocus);
  1287. Assert.False (win1.HasFocus);
  1288. Assert.Equal (win2, Application.Current.Focused);
  1289. Assert.Equal (view2, Application.Current.MostFocused);
  1290. }
  1291. [Fact]
  1292. [AutoInitShutdown]
  1293. public void ProcessHotKey_Will_Invoke_ProcessKey_Only_For_The_MostFocused_With_Top_KeyPress_Event ()
  1294. {
  1295. var sbQuiting = false;
  1296. var tfQuiting = false;
  1297. var topQuiting = false;
  1298. var sb = new StatusBar (new StatusItem [] {
  1299. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => sbQuiting = true )
  1300. });
  1301. var tf = new TextField ();
  1302. tf.KeyPress += Tf_KeyPress;
  1303. void Tf_KeyPress (View.KeyEventEventArgs obj)
  1304. {
  1305. if (obj.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  1306. obj.Handled = tfQuiting = true;
  1307. }
  1308. }
  1309. var win = new Window ();
  1310. win.Add (sb, tf);
  1311. var top = Application.Top;
  1312. top.KeyPress += Top_KeyPress;
  1313. void Top_KeyPress (View.KeyEventEventArgs obj)
  1314. {
  1315. if (obj.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  1316. obj.Handled = topQuiting = true;
  1317. }
  1318. }
  1319. top.Add (win);
  1320. Application.Begin (top);
  1321. Assert.False (sbQuiting);
  1322. Assert.False (tfQuiting);
  1323. Assert.False (topQuiting);
  1324. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1325. Assert.False (sbQuiting);
  1326. Assert.True (tfQuiting);
  1327. Assert.False (topQuiting);
  1328. tf.KeyPress -= Tf_KeyPress;
  1329. tfQuiting = false;
  1330. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1331. Application.MainLoop.MainIteration ();
  1332. Assert.True (sbQuiting);
  1333. Assert.False (tfQuiting);
  1334. Assert.False (topQuiting);
  1335. sb.RemoveItem (0);
  1336. sbQuiting = false;
  1337. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1338. Application.MainLoop.MainIteration ();
  1339. Assert.False (sbQuiting);
  1340. Assert.False (tfQuiting);
  1341. Assert.True (topQuiting);
  1342. }
  1343. [Fact]
  1344. [AutoInitShutdown]
  1345. public void ProcessHotKey_Will_Invoke_ProcessKey_Only_For_The_MostFocused_Without_Top_KeyPress_Event ()
  1346. {
  1347. var sbQuiting = false;
  1348. var tfQuiting = false;
  1349. var sb = new StatusBar (new StatusItem [] {
  1350. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => sbQuiting = true )
  1351. });
  1352. var tf = new TextField ();
  1353. tf.KeyPress += Tf_KeyPress;
  1354. void Tf_KeyPress (View.KeyEventEventArgs obj)
  1355. {
  1356. if (obj.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  1357. obj.Handled = tfQuiting = true;
  1358. }
  1359. }
  1360. var win = new Window ();
  1361. win.Add (sb, tf);
  1362. var top = Application.Top;
  1363. top.Add (win);
  1364. Application.Begin (top);
  1365. Assert.False (sbQuiting);
  1366. Assert.False (tfQuiting);
  1367. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1368. Assert.False (sbQuiting);
  1369. Assert.True (tfQuiting);
  1370. tf.KeyPress -= Tf_KeyPress;
  1371. tfQuiting = false;
  1372. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1373. Application.MainLoop.MainIteration ();
  1374. Assert.True (sbQuiting);
  1375. Assert.False (tfQuiting);
  1376. }
  1377. [Fact]
  1378. [AutoInitShutdown]
  1379. public void WindowDispose_CanFocusProblem ()
  1380. {
  1381. // Arrange
  1382. Application.Init ();
  1383. using var top = Toplevel.Create ();
  1384. using var view = new View (
  1385. x: 0,
  1386. y: 1,
  1387. text: nameof (WindowDispose_CanFocusProblem));
  1388. using var window = new Window ();
  1389. top.Add (window);
  1390. window.Add (view);
  1391. // Act
  1392. Application.Begin (top);
  1393. Application.Shutdown ();
  1394. // Assert does Not throw NullReferenceException
  1395. top.SetFocus ();
  1396. }
  1397. [Fact, AutoInitShutdown]
  1398. public void DrawFrame_With_Positive_Positions ()
  1399. {
  1400. var view = new View (new Rect (0, 0, 8, 4));
  1401. view.DrawContent += (_) => view.DrawFrame (view.Bounds, 0, true);
  1402. Assert.Equal (Point.Empty, new Point (view.Frame.X, view.Frame.Y));
  1403. Assert.Equal (new Size (8, 4), new Size (view.Frame.Width, view.Frame.Height));
  1404. Application.Top.Add (view);
  1405. Application.Begin (Application.Top);
  1406. var expected = @"
  1407. ┌──────┐
  1408. │ │
  1409. │ │
  1410. └──────┘
  1411. ";
  1412. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1413. Assert.Equal (new Rect (0, 0, 8, 4), pos);
  1414. }
  1415. [Fact, AutoInitShutdown]
  1416. public void DrawFrame_With_Minimum_Size ()
  1417. {
  1418. var view = new View (new Rect (0, 0, 2, 2));
  1419. view.DrawContent += (_) => view.DrawFrame (view.Bounds, 0, true);
  1420. Assert.Equal (Point.Empty, new Point (view.Frame.X, view.Frame.Y));
  1421. Assert.Equal (new Size (2, 2), new Size (view.Frame.Width, view.Frame.Height));
  1422. Application.Top.Add (view);
  1423. Application.Begin (Application.Top);
  1424. var expected = @"
  1425. ┌┐
  1426. └┘
  1427. ";
  1428. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1429. Assert.Equal (new Rect (0, 0, 2, 2), pos);
  1430. }
  1431. [Fact, AutoInitShutdown]
  1432. public void DrawFrame_With_Negative_Positions ()
  1433. {
  1434. var view = new View (new Rect (-1, 0, 8, 4));
  1435. view.DrawContent += (_) => view.DrawFrame (view.Bounds, 0, true);
  1436. Assert.Equal (new Point (-1, 0), new Point (view.Frame.X, view.Frame.Y));
  1437. Assert.Equal (new Size (8, 4), new Size (view.Frame.Width, view.Frame.Height));
  1438. Application.Top.Add (view);
  1439. Application.Begin (Application.Top);
  1440. var expected = @"
  1441. ──────┐
  1442. ──────┘
  1443. ";
  1444. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1445. Assert.Equal (new Rect (0, 0, 7, 4), pos);
  1446. view.Frame = new Rect (-1, -1, 8, 4);
  1447. Application.Refresh ();
  1448. expected = @"
  1449. ──────┘
  1450. ";
  1451. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1452. Assert.Equal (new Rect (6, 0, 7, 3), pos);
  1453. view.Frame = new Rect (0, 0, 8, 4);
  1454. ((FakeDriver)Application.Driver).SetBufferSize (7, 4);
  1455. expected = @"
  1456. ┌──────
  1457. └──────
  1458. ";
  1459. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1460. Assert.Equal (new Rect (0, 0, 7, 4), pos);
  1461. view.Frame = new Rect (0, 0, 8, 4);
  1462. ((FakeDriver)Application.Driver).SetBufferSize (7, 3);
  1463. }
  1464. [Fact, AutoInitShutdown]
  1465. public void DrawTextFormatter_Respects_The_Clip_Bounds ()
  1466. {
  1467. var view = new View (new Rect (0, 0, 20, 20));
  1468. view.Add (new Label ("0123456789abcdefghij"));
  1469. view.Add (new Label (0, 1, "1\n2\n3\n4\n5\n6\n7\n8\n9\n0"));
  1470. view.Add (new Button (1, 1, "Press me!"));
  1471. var scrollView = new ScrollView (new Rect (1, 1, 15, 10)) {
  1472. ContentSize = new Size (40, 40),
  1473. ShowHorizontalScrollIndicator = true,
  1474. ShowVerticalScrollIndicator = true
  1475. };
  1476. scrollView.Add (view);
  1477. var win = new Window (new Rect (1, 1, 20, 14), "Test");
  1478. win.Add (scrollView);
  1479. Application.Top.Add (win);
  1480. Application.Begin (Application.Top);
  1481. var expected = @"
  1482. ┌ Test ────────────┐
  1483. │ │
  1484. │ 0123456789abcd▲ │
  1485. │ 1[ Press me! ]┬ │
  1486. │ 2 │ │
  1487. │ 3 ┴ │
  1488. │ 4 ░ │
  1489. │ 5 ░ │
  1490. │ 6 ░ │
  1491. │ 7 ░ │
  1492. │ 8 ▼ │
  1493. │ ◄├───┤░░░░░░░► │
  1494. │ │
  1495. └──────────────────┘
  1496. ";
  1497. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1498. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1499. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1500. Application.Top.Redraw (Application.Top.Bounds);
  1501. expected = @"
  1502. ┌ Test ────────────┐
  1503. │ │
  1504. │ 123456789abcde▲ │
  1505. │ [ Press me! ] ┬ │
  1506. │ │ │
  1507. │ ┴ │
  1508. │ ░ │
  1509. │ ░ │
  1510. │ ░ │
  1511. │ ░ │
  1512. │ ▼ │
  1513. │ ◄├───┤░░░░░░░► │
  1514. │ │
  1515. └──────────────────┘
  1516. ";
  1517. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1518. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1519. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1520. Application.Top.Redraw (Application.Top.Bounds);
  1521. expected = @"
  1522. ┌ Test ────────────┐
  1523. │ │
  1524. │ 23456789abcdef▲ │
  1525. │ Press me! ] ┬ │
  1526. │ │ │
  1527. │ ┴ │
  1528. │ ░ │
  1529. │ ░ │
  1530. │ ░ │
  1531. │ ░ │
  1532. │ ▼ │
  1533. │ ◄├────┤░░░░░░► │
  1534. │ │
  1535. └──────────────────┘
  1536. ";
  1537. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1538. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1539. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1540. Application.Top.Redraw (Application.Top.Bounds);
  1541. expected = @"
  1542. ┌ Test ────────────┐
  1543. │ │
  1544. │ 3456789abcdefg▲ │
  1545. │ Press me! ] ┬ │
  1546. │ │ │
  1547. │ ┴ │
  1548. │ ░ │
  1549. │ ░ │
  1550. │ ░ │
  1551. │ ░ │
  1552. │ ▼ │
  1553. │ ◄├────┤░░░░░░► │
  1554. │ │
  1555. └──────────────────┘
  1556. ";
  1557. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1558. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1559. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1560. Application.Top.Redraw (Application.Top.Bounds);
  1561. expected = @"
  1562. ┌ Test ────────────┐
  1563. │ │
  1564. │ 456789abcdefgh▲ │
  1565. │ ress me! ] ┬ │
  1566. │ │ │
  1567. │ ┴ │
  1568. │ ░ │
  1569. │ ░ │
  1570. │ ░ │
  1571. │ ░ │
  1572. │ ▼ │
  1573. │ ◄░├───┤░░░░░░► │
  1574. │ │
  1575. └──────────────────┘
  1576. ";
  1577. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1578. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1579. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1580. Application.Top.Redraw (Application.Top.Bounds);
  1581. expected = @"
  1582. ┌ Test ────────────┐
  1583. │ │
  1584. │ 56789abcdefghi▲ │
  1585. │ ess me! ] ┬ │
  1586. │ │ │
  1587. │ ┴ │
  1588. │ ░ │
  1589. │ ░ │
  1590. │ ░ │
  1591. │ ░ │
  1592. │ ▼ │
  1593. │ ◄░├────┤░░░░░► │
  1594. │ │
  1595. └──────────────────┘
  1596. ";
  1597. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1598. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1599. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1600. Application.Top.Redraw (Application.Top.Bounds);
  1601. expected = @"
  1602. ┌ Test ────────────┐
  1603. │ │
  1604. │ 6789abcdefghij▲ │
  1605. │ ss me! ] ┬ │
  1606. │ │ │
  1607. │ ┴ │
  1608. │ ░ │
  1609. │ ░ │
  1610. │ ░ │
  1611. │ ░ │
  1612. │ ▼ │
  1613. │ ◄░├────┤░░░░░► │
  1614. │ │
  1615. └──────────────────┘
  1616. ";
  1617. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1618. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1619. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1620. Application.Top.Redraw (Application.Top.Bounds);
  1621. expected = @"
  1622. ┌ Test ────────────┐
  1623. │ │
  1624. │ 789abcdefghij ▲ │
  1625. │ s me! ] ┬ │
  1626. │ │ │
  1627. │ ┴ │
  1628. │ ░ │
  1629. │ ░ │
  1630. │ ░ │
  1631. │ ░ │
  1632. │ ▼ │
  1633. │ ◄░░├───┤░░░░░► │
  1634. │ │
  1635. └──────────────────┘
  1636. ";
  1637. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1638. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1639. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Home, new KeyModifiers ())));
  1640. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  1641. Application.Top.Redraw (Application.Top.Bounds);
  1642. expected = @"
  1643. ┌ Test ────────────┐
  1644. │ │
  1645. │ 1[ Press me! ]▲ │
  1646. │ 2 ┬ │
  1647. │ 3 │ │
  1648. │ 4 ┴ │
  1649. │ 5 ░ │
  1650. │ 6 ░ │
  1651. │ 7 ░ │
  1652. │ 8 ░ │
  1653. │ 9 ▼ │
  1654. │ ◄├───┤░░░░░░░► │
  1655. │ │
  1656. └──────────────────┘
  1657. ";
  1658. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1659. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1660. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  1661. Application.Top.Redraw (Application.Top.Bounds);
  1662. expected = @"
  1663. ┌ Test ────────────┐
  1664. │ │
  1665. │ 2 ▲ │
  1666. │ 3 ┬ │
  1667. │ 4 │ │
  1668. │ 5 ┴ │
  1669. │ 6 ░ │
  1670. │ 7 ░ │
  1671. │ 8 ░ │
  1672. │ 9 ░ │
  1673. │ 0 ▼ │
  1674. │ ◄├───┤░░░░░░░► │
  1675. │ │
  1676. └──────────────────┘
  1677. ";
  1678. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1679. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1680. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  1681. Application.Top.Redraw (Application.Top.Bounds);
  1682. expected = @"
  1683. ┌ Test ────────────┐
  1684. │ │
  1685. │ 3 ▲ │
  1686. │ 4 ┬ │
  1687. │ 5 │ │
  1688. │ 6 ┴ │
  1689. │ 7 ░ │
  1690. │ 8 ░ │
  1691. │ 9 ░ │
  1692. │ 0 ░ │
  1693. │ ▼ │
  1694. │ ◄├───┤░░░░░░░► │
  1695. │ │
  1696. └──────────────────┘
  1697. ";
  1698. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1699. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1700. }
  1701. [Fact, AutoInitShutdown]
  1702. public void Clear_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  1703. {
  1704. var view = new View () {
  1705. Width = Dim.Fill (),
  1706. Height = Dim.Fill ()
  1707. };
  1708. view.DrawContent += e => {
  1709. view.DrawFrame (view.Bounds);
  1710. var savedClip = Application.Driver.Clip;
  1711. Application.Driver.Clip = new Rect (1, 1, view.Bounds.Width - 2, view.Bounds.Height - 2);
  1712. for (int row = 0; row < view.Bounds.Height - 2; row++) {
  1713. Application.Driver.Move (1, row + 1);
  1714. for (int col = 0; col < view.Bounds.Width - 2; col++) {
  1715. Application.Driver.AddStr ($"{col}");
  1716. }
  1717. }
  1718. Application.Driver.Clip = savedClip;
  1719. };
  1720. Application.Top.Add (view);
  1721. Application.Begin (Application.Top);
  1722. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  1723. var expected = @"
  1724. ┌──────────────────┐
  1725. │012345678910111213│
  1726. │012345678910111213│
  1727. │012345678910111213│
  1728. │012345678910111213│
  1729. │012345678910111213│
  1730. │012345678910111213│
  1731. │012345678910111213│
  1732. │012345678910111213│
  1733. └──────────────────┘
  1734. ";
  1735. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1736. Assert.Equal (new Rect (0, 0, 20, 10), pos);
  1737. view.Clear ();
  1738. expected = @"
  1739. ";
  1740. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1741. Assert.Equal (Rect.Empty, pos);
  1742. }
  1743. [Fact, AutoInitShutdown]
  1744. public void Clear_Bounds_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  1745. {
  1746. var view = new View () {
  1747. Width = Dim.Fill (),
  1748. Height = Dim.Fill ()
  1749. };
  1750. view.DrawContent += e => {
  1751. view.DrawFrame (view.Bounds);
  1752. var savedClip = Application.Driver.Clip;
  1753. Application.Driver.Clip = new Rect (1, 1, view.Bounds.Width - 2, view.Bounds.Height - 2);
  1754. for (int row = 0; row < view.Bounds.Height - 2; row++) {
  1755. Application.Driver.Move (1, row + 1);
  1756. for (int col = 0; col < view.Bounds.Width - 2; col++) {
  1757. Application.Driver.AddStr ($"{col}");
  1758. }
  1759. }
  1760. Application.Driver.Clip = savedClip;
  1761. };
  1762. Application.Top.Add (view);
  1763. Application.Begin (Application.Top);
  1764. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  1765. var expected = @"
  1766. ┌──────────────────┐
  1767. │012345678910111213│
  1768. │012345678910111213│
  1769. │012345678910111213│
  1770. │012345678910111213│
  1771. │012345678910111213│
  1772. │012345678910111213│
  1773. │012345678910111213│
  1774. │012345678910111213│
  1775. └──────────────────┘
  1776. ";
  1777. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1778. Assert.Equal (new Rect (0, 0, 20, 10), pos);
  1779. view.Clear (view.Bounds);
  1780. expected = @"
  1781. ";
  1782. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1783. Assert.Equal (Rect.Empty, pos);
  1784. }
  1785. [Fact]
  1786. public void GetTextFormatterBoundsSize_GetBoundsTextFormatterSize_HotKeySpecifier ()
  1787. {
  1788. var text = "Say Hello 你";
  1789. var horizontalView = new View () { Text = text, AutoSize = true, HotKeySpecifier = '_' };
  1790. var verticalView = new View () {
  1791. Text = text,
  1792. AutoSize = true,
  1793. HotKeySpecifier = '_',
  1794. TextDirection = TextDirection.TopBottom_LeftRight
  1795. };
  1796. Assert.True (horizontalView.AutoSize);
  1797. Assert.Equal (new Rect (0, 0, 12, 1), horizontalView.Frame);
  1798. Assert.Equal (new Size (12, 1), horizontalView.GetTextFormatterBoundsSize ());
  1799. Assert.Equal (new Size (12, 1), horizontalView.GetBoundsTextFormatterSize ());
  1800. Assert.Equal (horizontalView.TextFormatter.Size, horizontalView.GetBoundsTextFormatterSize ());
  1801. Assert.Equal (horizontalView.Frame.Size, horizontalView.GetTextFormatterBoundsSize ());
  1802. Assert.True (verticalView.AutoSize);
  1803. Assert.Equal (new Rect (0, 0, 2, 11), verticalView.Frame);
  1804. Assert.Equal (new Size (2, 11), verticalView.GetTextFormatterBoundsSize ());
  1805. Assert.Equal (new Size (2, 11), verticalView.GetBoundsTextFormatterSize ());
  1806. Assert.Equal (verticalView.TextFormatter.Size, verticalView.GetBoundsTextFormatterSize ());
  1807. Assert.Equal (verticalView.Frame.Size, verticalView.GetTextFormatterBoundsSize ());
  1808. text = "Say He_llo 你";
  1809. horizontalView.Text = text;
  1810. verticalView.Text = text;
  1811. Assert.True (horizontalView.AutoSize);
  1812. Assert.Equal (new Rect (0, 0, 12, 1), horizontalView.Frame);
  1813. Assert.Equal (new Size (12, 1), horizontalView.GetTextFormatterBoundsSize ());
  1814. Assert.Equal (new Size (13, 1), horizontalView.GetBoundsTextFormatterSize ());
  1815. Assert.Equal (horizontalView.TextFormatter.Size, horizontalView.GetBoundsTextFormatterSize ());
  1816. Assert.Equal (horizontalView.Frame.Size, horizontalView.GetTextFormatterBoundsSize ());
  1817. Assert.True (verticalView.AutoSize);
  1818. Assert.Equal (new Rect (0, 0, 2, 11), verticalView.Frame);
  1819. Assert.Equal (new Size (2, 11), verticalView.GetTextFormatterBoundsSize ());
  1820. Assert.Equal (new Size (2, 12), verticalView.GetBoundsTextFormatterSize ());
  1821. Assert.Equal (verticalView.TextFormatter.Size, verticalView.GetBoundsTextFormatterSize ());
  1822. Assert.Equal (verticalView.Frame.Size, verticalView.GetTextFormatterBoundsSize ());
  1823. }
  1824. [Fact]
  1825. public void IsAdded_Added_Removed ()
  1826. {
  1827. var top = new Toplevel ();
  1828. var view = new View ();
  1829. Assert.False (view.IsAdded);
  1830. top.Add (view);
  1831. Assert.True (view.IsAdded);
  1832. top.Remove (view);
  1833. Assert.False (view.IsAdded);
  1834. }
  1835. [Fact, AutoInitShutdown]
  1836. public void Visible_Clear_The_View_Output ()
  1837. {
  1838. var label = new Label ("Testing visibility.");
  1839. var win = new Window ();
  1840. win.Add (label);
  1841. var top = Application.Top;
  1842. top.Add (win);
  1843. Application.Begin (top);
  1844. Assert.True (label.Visible);
  1845. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  1846. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1847. ┌────────────────────────────┐
  1848. │Testing visibility. │
  1849. │ │
  1850. │ │
  1851. └────────────────────────────┘
  1852. ", output);
  1853. label.Visible = false;
  1854. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1855. ┌────────────────────────────┐
  1856. │ │
  1857. │ │
  1858. │ │
  1859. └────────────────────────────┘
  1860. ", output);
  1861. }
  1862. [Fact, AutoInitShutdown]
  1863. public void ClearOnVisibleFalse_Gets_Sets ()
  1864. {
  1865. var text = "This is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test";
  1866. var label = new Label (text);
  1867. Application.Top.Add (label);
  1868. var sbv = new ScrollBarView (label, true, false) {
  1869. Size = 100,
  1870. ClearOnVisibleFalse = false
  1871. };
  1872. Application.Begin (Application.Top);
  1873. Assert.True (sbv.Visible);
  1874. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1875. This is a tes▲
  1876. This is a tes┬
  1877. This is a tes┴
  1878. This is a tes░
  1879. This is a tes░
  1880. This is a tes▼
  1881. ", output);
  1882. sbv.Visible = false;
  1883. Assert.False (sbv.Visible);
  1884. Application.Top.Redraw (Application.Top.Bounds);
  1885. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1886. This is a test
  1887. This is a test
  1888. This is a test
  1889. This is a test
  1890. This is a test
  1891. This is a test
  1892. ", output);
  1893. sbv.Visible = true;
  1894. Assert.True (sbv.Visible);
  1895. Application.Top.Redraw (Application.Top.Bounds);
  1896. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1897. This is a tes▲
  1898. This is a tes┬
  1899. This is a tes┴
  1900. This is a tes░
  1901. This is a tes░
  1902. This is a tes▼
  1903. ", output);
  1904. sbv.ClearOnVisibleFalse = true;
  1905. sbv.Visible = false;
  1906. Assert.False (sbv.Visible);
  1907. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1908. This is a tes
  1909. This is a tes
  1910. This is a tes
  1911. This is a tes
  1912. This is a tes
  1913. This is a tes
  1914. ", output);
  1915. }
  1916. [Fact, AutoInitShutdown]
  1917. public void DrawContentComplete_Event_Is_Always_Called ()
  1918. {
  1919. var viewCalled = false;
  1920. var tvCalled = false;
  1921. var view = new View ("View") { Width = 10, Height = 10 };
  1922. view.DrawContentComplete += (e) => viewCalled = true;
  1923. var tv = new TextView () { Y = 11, Width = 10, Height = 10 };
  1924. tv.DrawContentComplete += (e) => tvCalled = true;
  1925. Application.Top.Add (view, tv);
  1926. Application.Begin (Application.Top);
  1927. Assert.True (viewCalled);
  1928. Assert.True (tvCalled);
  1929. }
  1930. [Fact, AutoInitShutdown]
  1931. public void KeyDown_And_KeyUp_Events_Must_Called_Before_OnKeyDown_And_OnKeyUp ()
  1932. {
  1933. var keyDown = false;
  1934. var keyPress = false;
  1935. var keyUp = false;
  1936. var view = new DerivedView ();
  1937. view.KeyDown += (e) => {
  1938. Assert.Equal (Key.a, e.KeyEvent.Key);
  1939. Assert.False (keyDown);
  1940. Assert.False (view.IsKeyDown);
  1941. e.Handled = true;
  1942. keyDown = true;
  1943. };
  1944. view.KeyPress += (e) => {
  1945. Assert.Equal (Key.a, e.KeyEvent.Key);
  1946. Assert.False (keyPress);
  1947. Assert.False (view.IsKeyPress);
  1948. e.Handled = true;
  1949. keyPress = true;
  1950. };
  1951. view.KeyUp += (e) => {
  1952. Assert.Equal (Key.a, e.KeyEvent.Key);
  1953. Assert.False (keyUp);
  1954. Assert.False (view.IsKeyUp);
  1955. e.Handled = true;
  1956. keyUp = true;
  1957. };
  1958. Application.Top.Add (view);
  1959. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('a', ConsoleKey.A, false, false, false));
  1960. Application.Iteration += () => Application.RequestStop ();
  1961. Assert.True (view.CanFocus);
  1962. Application.Run ();
  1963. Application.Shutdown ();
  1964. Assert.True (keyDown);
  1965. Assert.True (keyPress);
  1966. Assert.True (keyUp);
  1967. Assert.False (view.IsKeyDown);
  1968. Assert.False (view.IsKeyPress);
  1969. Assert.False (view.IsKeyUp);
  1970. }
  1971. public class DerivedView : View {
  1972. public DerivedView ()
  1973. {
  1974. CanFocus = true;
  1975. }
  1976. public bool IsKeyDown { get; set; }
  1977. public bool IsKeyPress { get; set; }
  1978. public bool IsKeyUp { get; set; }
  1979. public override ustring Text { get; set; }
  1980. public override bool OnKeyDown (KeyEvent keyEvent)
  1981. {
  1982. IsKeyDown = true;
  1983. return true;
  1984. }
  1985. public override bool ProcessKey (KeyEvent keyEvent)
  1986. {
  1987. IsKeyPress = true;
  1988. return true;
  1989. }
  1990. public override bool OnKeyUp (KeyEvent keyEvent)
  1991. {
  1992. IsKeyUp = true;
  1993. return true;
  1994. }
  1995. public override void Redraw (Rect bounds)
  1996. {
  1997. var idx = 0;
  1998. for (int r = 0; r < Frame.Height; r++) {
  1999. for (int c = 0; c < Frame.Width; c++) {
  2000. if (idx < Text.Length) {
  2001. var rune = Text [idx];
  2002. if (rune != '\n') {
  2003. AddRune (c, r, Text [idx]);
  2004. }
  2005. idx++;
  2006. if (rune == '\n') {
  2007. break;
  2008. }
  2009. }
  2010. }
  2011. }
  2012. ClearLayoutNeeded ();
  2013. ClearNeedsDisplay ();
  2014. }
  2015. }
  2016. [Theory, AutoInitShutdown]
  2017. [InlineData (true, false, false)]
  2018. [InlineData (true, true, false)]
  2019. [InlineData (true, true, true)]
  2020. public void KeyDown_And_KeyUp_Events_With_Only_Key_Modifiers (bool shift, bool alt, bool control)
  2021. {
  2022. var keyDown = false;
  2023. var keyPress = false;
  2024. var keyUp = false;
  2025. var view = new DerivedView ();
  2026. view.KeyDown += (e) => {
  2027. Assert.Equal (-1, e.KeyEvent.KeyValue);
  2028. Assert.Equal (shift, e.KeyEvent.IsShift);
  2029. Assert.Equal (alt, e.KeyEvent.IsAlt);
  2030. Assert.Equal (control, e.KeyEvent.IsCtrl);
  2031. Assert.False (keyDown);
  2032. Assert.False (view.IsKeyDown);
  2033. keyDown = true;
  2034. };
  2035. view.KeyPress += (e) => {
  2036. keyPress = true;
  2037. };
  2038. view.KeyUp += (e) => {
  2039. Assert.Equal (-1, e.KeyEvent.KeyValue);
  2040. Assert.Equal (shift, e.KeyEvent.IsShift);
  2041. Assert.Equal (alt, e.KeyEvent.IsAlt);
  2042. Assert.Equal (control, e.KeyEvent.IsCtrl);
  2043. Assert.False (keyUp);
  2044. Assert.False (view.IsKeyUp);
  2045. keyUp = true;
  2046. };
  2047. Application.Top.Add (view);
  2048. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('\0', (ConsoleKey)'\0', shift, alt, control));
  2049. Application.Iteration += () => Application.RequestStop ();
  2050. Assert.True (view.CanFocus);
  2051. Application.Run ();
  2052. Application.Shutdown ();
  2053. Assert.True (keyDown);
  2054. Assert.False (keyPress);
  2055. Assert.True (keyUp);
  2056. Assert.True (view.IsKeyDown);
  2057. Assert.False (view.IsKeyPress);
  2058. Assert.True (view.IsKeyUp);
  2059. }
  2060. [Fact, AutoInitShutdown]
  2061. public void SetHasFocus_Do_Not_Throws_If_OnLeave_Remove_Focused_Changing_To_Null ()
  2062. {
  2063. var view1Leave = false;
  2064. var subView1Leave = false;
  2065. var subView1subView1Leave = false;
  2066. var top = Application.Top;
  2067. var view1 = new View { CanFocus = true };
  2068. var subView1 = new View { CanFocus = true };
  2069. var subView1subView1 = new View { CanFocus = true };
  2070. view1.Leave += (e) => {
  2071. view1Leave = true;
  2072. };
  2073. subView1.Leave += (e) => {
  2074. subView1.Remove (subView1subView1);
  2075. subView1Leave = true;
  2076. };
  2077. view1.Add (subView1);
  2078. subView1subView1.Leave += (e) => {
  2079. // This is never invoked
  2080. subView1subView1Leave = true;
  2081. };
  2082. subView1.Add (subView1subView1);
  2083. var view2 = new View { CanFocus = true };
  2084. top.Add (view1, view2);
  2085. Application.Begin (top);
  2086. view2.SetFocus ();
  2087. Assert.True (view1Leave);
  2088. Assert.True (subView1Leave);
  2089. Assert.False (subView1subView1Leave);
  2090. }
  2091. [Fact, AutoInitShutdown]
  2092. public void GetNormalColor_ColorScheme ()
  2093. {
  2094. var view = new View { ColorScheme = Colors.Base };
  2095. Assert.Equal (view.ColorScheme.Normal, view.GetNormalColor ());
  2096. view.Enabled = false;
  2097. Assert.Equal (view.ColorScheme.Disabled, view.GetNormalColor ());
  2098. }
  2099. [Fact, AutoInitShutdown]
  2100. public void GetHotNormalColor_ColorScheme ()
  2101. {
  2102. var view = new View { ColorScheme = Colors.Base };
  2103. Assert.Equal (view.ColorScheme.HotNormal, view.GetHotNormalColor ());
  2104. view.Enabled = false;
  2105. Assert.Equal (view.ColorScheme.Disabled, view.GetHotNormalColor ());
  2106. }
  2107. [Theory, AutoInitShutdown]
  2108. [InlineData (true)]
  2109. [InlineData (false)]
  2110. public void Clear_Does_Not_Spillover_Its_Parent (bool label)
  2111. {
  2112. var root = new View () { Width = 20, Height = 10, ColorScheme = Colors.Base };
  2113. var v = label == true ?
  2114. new Label (new string ('c', 100)) {
  2115. Width = Dim.Fill ()
  2116. } :
  2117. (View)new TextView () {
  2118. Height = 1,
  2119. Text = new string ('c', 100),
  2120. Width = Dim.Fill ()
  2121. };
  2122. root.Add (v);
  2123. Application.Top.Add (root);
  2124. Application.Begin (Application.Top);
  2125. if (label) {
  2126. Assert.True (v.AutoSize);
  2127. Assert.False (v.CanFocus);
  2128. Assert.Equal (new Rect (0, 0, 100, 1), v.Frame);
  2129. } else {
  2130. Assert.False (v.AutoSize);
  2131. Assert.True (v.CanFocus);
  2132. Assert.Equal (new Rect (0, 0, 20, 1), v.Frame);
  2133. }
  2134. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2135. cccccccccccccccccccc", output);
  2136. var attributes = new Attribute [] {
  2137. Colors.TopLevel.Normal,
  2138. Colors.Base.Normal,
  2139. Colors.Base.Focus
  2140. };
  2141. if (label) {
  2142. TestHelpers.AssertDriverColorsAre (@"
  2143. 111111111111111111110
  2144. 111111111111111111110", attributes);
  2145. } else {
  2146. TestHelpers.AssertDriverColorsAre (@"
  2147. 222222222222222222220
  2148. 222222222222222222220", attributes);
  2149. }
  2150. if (label) {
  2151. root.CanFocus = true;
  2152. v.CanFocus = true;
  2153. Assert.False (v.HasFocus);
  2154. v.SetFocus ();
  2155. Assert.True (v.HasFocus);
  2156. Application.Refresh ();
  2157. TestHelpers.AssertDriverColorsAre (@"
  2158. 222222222222222222220
  2159. 222222222222222222220", attributes);
  2160. }
  2161. }
  2162. [Fact, AutoInitShutdown]
  2163. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  2164. {
  2165. var label = new Label ("At 0,0");
  2166. var view = new DerivedView () {
  2167. X = 2,
  2168. Y = 2,
  2169. Width = 30,
  2170. Height = 2,
  2171. Text = "A text with some long width\n and also with two lines."
  2172. };
  2173. var top = Application.Top;
  2174. top.Add (label, view);
  2175. Application.Begin (top);
  2176. top.Redraw (top.Bounds);
  2177. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2178. At 0,0
  2179. A text with some long width
  2180. and also with two lines. ", output);
  2181. view.Frame = new Rect (1, 1, 10, 1);
  2182. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  2183. Assert.Equal (LayoutStyle.Computed, view.LayoutStyle);
  2184. view.LayoutStyle = LayoutStyle.Absolute;
  2185. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2186. Assert.Equal (new Rect (0, 0, 10, 1), view.NeedDisplay);
  2187. top.Redraw (top.Bounds);
  2188. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2189. At 0,0
  2190. A text wit", output);
  2191. }
  2192. [Fact, AutoInitShutdown]
  2193. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  2194. {
  2195. var label = new Label ("At 0,0");
  2196. var view = new DerivedView () {
  2197. X = 2,
  2198. Y = 2,
  2199. Width = 30,
  2200. Height = 2,
  2201. Text = "A text with some long width\n and also with two lines."
  2202. };
  2203. var top = Application.Top;
  2204. top.Add (label, view);
  2205. Application.Begin (top);
  2206. top.Redraw (top.Bounds);
  2207. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2208. At 0,0
  2209. A text with some long width
  2210. and also with two lines. ", output);
  2211. view.X = 1;
  2212. view.Y = 1;
  2213. view.Width = 10;
  2214. view.Height = 1;
  2215. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  2216. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2217. Assert.Equal (new Rect (0, 0, 30, 2), view.NeedDisplay);
  2218. top.Redraw (top.Bounds);
  2219. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2220. At 0,0
  2221. A text wit", output);
  2222. }
  2223. [Fact, AutoInitShutdown]
  2224. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  2225. {
  2226. var label = new Label ("At 0,0");
  2227. var view = new DerivedView () {
  2228. X = 2,
  2229. Y = 2,
  2230. Width = 30,
  2231. Height = 2,
  2232. Text = "A text with some long width\n and also with two lines."
  2233. };
  2234. var top = Application.Top;
  2235. top.Add (label, view);
  2236. Application.Begin (top);
  2237. view.Redraw (view.Bounds);
  2238. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2239. At 0,0
  2240. A text with some long width
  2241. and also with two lines. ", output);
  2242. view.Frame = new Rect (1, 1, 10, 1);
  2243. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  2244. Assert.Equal (LayoutStyle.Computed, view.LayoutStyle);
  2245. view.LayoutStyle = LayoutStyle.Absolute;
  2246. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2247. Assert.Equal (new Rect (0, 0, 10, 1), view.NeedDisplay);
  2248. view.Redraw (view.Bounds);
  2249. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2250. At 0,0
  2251. A text wit
  2252. A text with some long width
  2253. and also with two lines. ", output);
  2254. }
  2255. [Fact, AutoInitShutdown]
  2256. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  2257. {
  2258. var label = new Label ("At 0,0");
  2259. var view = new DerivedView () {
  2260. X = 2,
  2261. Y = 2,
  2262. Width = 30,
  2263. Height = 2,
  2264. Text = "A text with some long width\n and also with two lines."
  2265. };
  2266. var top = Application.Top;
  2267. top.Add (label, view);
  2268. Application.Begin (top);
  2269. view.Redraw (view.Bounds);
  2270. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2271. At 0,0
  2272. A text with some long width
  2273. and also with two lines. ", output);
  2274. view.X = 1;
  2275. view.Y = 1;
  2276. view.Width = 10;
  2277. view.Height = 1;
  2278. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  2279. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2280. Assert.Equal (new Rect (0, 0, 30, 2), view.NeedDisplay);
  2281. view.Redraw (view.Bounds);
  2282. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2283. At 0,0
  2284. A text wit
  2285. A text with some long width
  2286. and also with two lines. ", output);
  2287. }
  2288. [Fact, AutoInitShutdown]
  2289. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  2290. {
  2291. var label = new Label ("At 0,0");
  2292. var view = new DerivedView () {
  2293. X = 2,
  2294. Y = 2,
  2295. Width = 30,
  2296. Height = 2,
  2297. Text = "A text with some long width\n and also with two lines."
  2298. };
  2299. var top = Application.Top;
  2300. top.Add (label, view);
  2301. Application.Begin (top);
  2302. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2303. At 0,0
  2304. A text with some long width
  2305. and also with two lines. ", output);
  2306. view.Frame = new Rect (3, 3, 10, 1);
  2307. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  2308. Assert.Equal (LayoutStyle.Computed, view.LayoutStyle);
  2309. view.LayoutStyle = LayoutStyle.Absolute;
  2310. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2311. Assert.Equal (new Rect (0, 0, 10, 1), view.NeedDisplay);
  2312. top.Redraw (top.Bounds);
  2313. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2314. At 0,0
  2315. A text wit", output);
  2316. }
  2317. [Fact, AutoInitShutdown]
  2318. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  2319. {
  2320. var label = new Label ("At 0,0");
  2321. var view = new DerivedView () {
  2322. X = 2,
  2323. Y = 2,
  2324. Width = 30,
  2325. Height = 2,
  2326. Text = "A text with some long width\n and also with two lines."
  2327. };
  2328. var top = Application.Top;
  2329. top.Add (label, view);
  2330. Application.Begin (top);
  2331. top.Redraw (top.Bounds);
  2332. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2333. At 0,0
  2334. A text with some long width
  2335. and also with two lines. ", output);
  2336. view.X = 3;
  2337. view.Y = 3;
  2338. view.Width = 10;
  2339. view.Height = 1;
  2340. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  2341. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2342. Assert.Equal (new Rect (0, 0, 30, 2), view.NeedDisplay);
  2343. top.Redraw (top.Bounds);
  2344. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2345. At 0,0
  2346. A text wit", output);
  2347. }
  2348. [Fact, AutoInitShutdown]
  2349. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  2350. {
  2351. var label = new Label ("At 0,0");
  2352. var view = new DerivedView () {
  2353. X = 2,
  2354. Y = 2,
  2355. Width = 30,
  2356. Height = 2,
  2357. Text = "A text with some long width\n and also with two lines."
  2358. };
  2359. var top = Application.Top;
  2360. top.Add (label, view);
  2361. Application.Begin (top);
  2362. view.Redraw (view.Bounds);
  2363. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2364. At 0,0
  2365. A text with some long width
  2366. and also with two lines. ", output);
  2367. view.Frame = new Rect (3, 3, 10, 1);
  2368. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2369. Assert.Equal (new Rect (0, 0, 10, 1), view.NeedDisplay);
  2370. view.Redraw (view.Bounds);
  2371. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2372. At 0,0
  2373. A text with some long width
  2374. A text witith two lines. ", output);
  2375. }
  2376. [Fact, AutoInitShutdown]
  2377. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  2378. {
  2379. var label = new Label ("At 0,0");
  2380. var view = new DerivedView () {
  2381. X = 2,
  2382. Y = 2,
  2383. Width = 30,
  2384. Height = 2,
  2385. Text = "A text with some long width\n and also with two lines."
  2386. };
  2387. var top = Application.Top;
  2388. top.Add (label, view);
  2389. Application.Begin (top);
  2390. view.Redraw (view.Bounds);
  2391. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2392. At 0,0
  2393. A text with some long width
  2394. and also with two lines. ", output);
  2395. view.X = 3;
  2396. view.Y = 3;
  2397. view.Width = 10;
  2398. view.Height = 1;
  2399. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  2400. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2401. Assert.Equal (new Rect (0, 0, 30, 2), view.NeedDisplay);
  2402. view.Redraw (view.Bounds);
  2403. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2404. At 0,0
  2405. A text with some long width
  2406. A text witith two lines. ", output);
  2407. }
  2408. [Fact, AutoInitShutdown]
  2409. public void Test_Nested_Views_With_Height_Equal_To_One ()
  2410. {
  2411. var v = new View () { Width = 11, Height = 3, ColorScheme = new ColorScheme () };
  2412. var top = new View () { Width = Dim.Fill (), Height = 1 };
  2413. var bottom = new View () { Width = Dim.Fill (), Height = 1, Y = 2 };
  2414. top.Add (new Label ("111"));
  2415. v.Add (top);
  2416. v.Add (new LineView (Orientation.Horizontal) { Y = 1 });
  2417. bottom.Add (new Label ("222"));
  2418. v.Add (bottom);
  2419. v.LayoutSubviews ();
  2420. v.Redraw (v.Bounds);
  2421. string looksLike =
  2422. @"
  2423. 111
  2424. ───────────
  2425. 222";
  2426. TestHelpers.AssertDriverContentsAre (looksLike, output);
  2427. }
  2428. [Fact]
  2429. [AutoInitShutdown]
  2430. public void Frame_Set_After_Initialze_Update_NeededDisplay ()
  2431. {
  2432. var frame = new FrameView ();
  2433. var label = new Label ("This should be the first line.") {
  2434. TextAlignment = Terminal.Gui.TextAlignment.Centered,
  2435. ColorScheme = Colors.Menu,
  2436. Width = Dim.Fill (),
  2437. X = Pos.Center (),
  2438. Y = Pos.Center () - 2 // center minus 2 minus two lines top and bottom borders equal to zero (4-2-2=0)
  2439. };
  2440. var button = new Button ("Press me!") {
  2441. X = Pos.Center (),
  2442. Y = Pos.Center ()
  2443. };
  2444. frame.Add (label, button);
  2445. frame.X = Pos.Center ();
  2446. frame.Y = Pos.Center ();
  2447. frame.Width = 40;
  2448. frame.Height = 8;
  2449. var top = Application.Top;
  2450. top.Add (frame);
  2451. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  2452. Assert.Equal (new Rect (0, 0, 40, 8), frame.Frame);
  2453. Assert.Equal (new Rect (1, 1, 0, 0), frame.Subviews [0].Frame);
  2454. Assert.Equal ("ContentView()({X=1,Y=1,Width=0,Height=0})", frame.Subviews [0].ToString ());
  2455. Assert.Equal (new Rect (0, 0, 40, 8), new Rect (
  2456. frame.Frame.Left, frame.Frame.Top,
  2457. frame.Frame.Right, frame.Frame.Bottom));
  2458. Assert.Equal (new Rect (0, 0, 30, 1), label.Frame);
  2459. Assert.Equal (new Rect (0, 0, 13, 1), button.Frame);
  2460. Assert.Equal (new Rect (0, 0, 80, 25), top.NeedDisplay);
  2461. Assert.Equal (new Rect (0, 0, 40, 8), frame.NeedDisplay);
  2462. Assert.Equal (Rect.Empty, frame.Subviews [0].NeedDisplay);
  2463. Assert.Equal (new Rect (0, 0, 40, 8), new Rect (
  2464. frame.NeedDisplay.Left, frame.NeedDisplay.Top,
  2465. frame.NeedDisplay.Right, frame.NeedDisplay.Bottom));
  2466. Assert.Equal (new Rect (0, 0, 30, 1), label.NeedDisplay);
  2467. Assert.Equal (new Rect (0, 0, 13, 1), button.NeedDisplay);
  2468. top.LayoutComplete += e => {
  2469. Assert.Equal (new Rect (0, 0, 80, 25), top.NeedDisplay);
  2470. };
  2471. frame.LayoutComplete += e => {
  2472. Assert.Equal (new Rect (0, 0, 40, 8), frame.NeedDisplay);
  2473. };
  2474. frame.Subviews [0].LayoutComplete += e => {
  2475. if (top.IsLoaded) {
  2476. Assert.Equal (new Rect (0, 0, 38, 6), frame.Subviews [0].NeedDisplay);
  2477. } else {
  2478. Assert.Equal (new Rect (0, 0, 38, 6), frame.Subviews [0].NeedDisplay);
  2479. }
  2480. };
  2481. label.LayoutComplete += e => {
  2482. Assert.Equal (new Rect (0, 0, 38, 1), label.NeedDisplay);
  2483. };
  2484. button.LayoutComplete += e => {
  2485. Assert.Equal (new Rect (0, 0, 13, 1), button.NeedDisplay);
  2486. };
  2487. Application.Begin (top);
  2488. Assert.True (label.AutoSize);
  2489. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  2490. Assert.Equal (new Rect (20, 8, 40, 8), frame.Frame);
  2491. Assert.Equal (new Rect (1, 1, 38, 6), frame.Subviews [0].Frame);
  2492. Assert.Equal ("ContentView()({X=1,Y=1,Width=38,Height=6})", frame.Subviews [0].ToString ());
  2493. Assert.Equal (new Rect (20, 8, 60, 16), new Rect (
  2494. frame.Frame.Left, frame.Frame.Top,
  2495. frame.Frame.Right, frame.Frame.Bottom));
  2496. Assert.Equal (new Rect (0, 0, 38, 1), label.Frame);
  2497. Assert.Equal (new Rect (12, 2, 13, 1), button.Frame);
  2498. var expected = @"
  2499. ┌──────────────────────────────────────┐
  2500. │ This should be the first line. │
  2501. │ │
  2502. │ [ Press me! ] │
  2503. │ │
  2504. │ │
  2505. │ │
  2506. └──────────────────────────────────────┘
  2507. ";
  2508. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  2509. }
  2510. }
  2511. }