ViewTests.cs 85 KB

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