ViewTests.cs 85 KB

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