ViewTests.cs 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  1. using System;
  2. using Xunit;
  3. using Xunit.Abstractions;
  4. using System.Text;
  5. // Alias Console to MockConsole so we don't accidentally use Console
  6. using Console = Terminal.Gui.FakeConsole;
  7. namespace Terminal.Gui.ViewTests {
  8. public class ViewTests {
  9. readonly ITestOutputHelper output;
  10. public ViewTests (ITestOutputHelper output)
  11. {
  12. this.output = output;
  13. }
  14. [Fact, TestRespondersDisposed]
  15. public void New_Initializes ()
  16. {
  17. // Parameterless
  18. var r = new View ();
  19. Assert.NotNull (r);
  20. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  21. Assert.Equal ("View()(0,0,0,0)", r.ToString ());
  22. Assert.False (r.CanFocus);
  23. Assert.False (r.HasFocus);
  24. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  25. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  26. Assert.Null (r.Focused);
  27. Assert.Null (r.ColorScheme);
  28. Assert.Equal (0, r.Width);
  29. Assert.Equal (0, r.Height);
  30. Assert.Equal (0, r.X);
  31. Assert.Equal (0, r.Y);
  32. Assert.False (r.IsCurrentTop);
  33. Assert.Empty (r.Id);
  34. Assert.Empty (r.Subviews);
  35. Assert.False (r.WantContinuousButtonPressed);
  36. Assert.False (r.WantMousePositionReports);
  37. Assert.Null (r.SuperView);
  38. Assert.Null (r.MostFocused);
  39. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  40. r.Dispose ();
  41. // Empty Rect
  42. r = new View (Rect.Empty);
  43. Assert.NotNull (r);
  44. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  45. Assert.Equal ("View()(0,0,0,0)", r.ToString ());
  46. Assert.False (r.CanFocus);
  47. Assert.False (r.HasFocus);
  48. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  49. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  50. Assert.Null (r.Focused);
  51. Assert.Null (r.ColorScheme);
  52. Assert.Equal (0, r.Width);
  53. Assert.Equal (0, r.Height);
  54. Assert.Equal (0, r.X);
  55. Assert.Equal (0, r.Y);
  56. Assert.False (r.IsCurrentTop);
  57. Assert.Empty (r.Id);
  58. Assert.Empty (r.Subviews);
  59. Assert.False (r.WantContinuousButtonPressed);
  60. Assert.False (r.WantMousePositionReports);
  61. Assert.Null (r.SuperView);
  62. Assert.Null (r.MostFocused);
  63. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  64. r.Dispose ();
  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()(1,2,3,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.Equal (3, r.Width);
  77. Assert.Equal (4, r.Height);
  78. Assert.Equal (1, r.X);
  79. Assert.Equal (2, 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. r.Dispose ();
  89. // Initializes a view with a vertical direction
  90. r = new View ("Vertical View", TextDirection.TopBottom_LeftRight);
  91. Assert.NotNull (r);
  92. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  93. Assert.Equal ("View(Vertical View)(0,0,1,13)", r.ToString ());
  94. Assert.False (r.CanFocus);
  95. Assert.False (r.HasFocus);
  96. Assert.Equal (new Rect (0, 0, 1, 13), r.Bounds);
  97. Assert.Equal (new Rect (0, 0, 1, 13), r.Frame);
  98. Assert.Null (r.Focused);
  99. Assert.Null (r.ColorScheme);
  100. Assert.False (r.IsCurrentTop);
  101. Assert.Equal ("Vertical View", r.Id);
  102. Assert.Empty (r.Subviews);
  103. Assert.False (r.WantContinuousButtonPressed);
  104. Assert.False (r.WantMousePositionReports);
  105. Assert.Null (r.SuperView);
  106. Assert.Null (r.MostFocused);
  107. Assert.Equal (TextDirection.TopBottom_LeftRight, r.TextDirection);
  108. r.Dispose ();
  109. }
  110. [Fact, TestRespondersDisposed]
  111. public void New_Methods_Return_False ()
  112. {
  113. var r = new View ();
  114. Assert.False (r.OnKeyDown (new Key () { KeyCode = KeyCode.Null }));
  115. //Assert.False (r.OnKeyDown (new KeyEventArgs () { Key = Key.Unknown }));
  116. Assert.False (r.OnKeyUp (new Key () { KeyCode = KeyCode.Null }));
  117. Assert.False (r.MouseEvent (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  118. Assert.False (r.OnMouseEnter (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  119. Assert.False (r.OnMouseLeave (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  120. var v1 = new View ();
  121. Assert.False (r.OnEnter (v1));
  122. v1.Dispose ();
  123. var v2 = new View ();
  124. Assert.False (r.OnLeave (v2));
  125. v2.Dispose ();
  126. r.Dispose ();
  127. // TODO: Add more
  128. }
  129. [Fact, TestRespondersDisposed]
  130. public void View_With_No_Difference_Between_An_Object_Initializer_And_A_Constructor ()
  131. {
  132. // Object Initializer
  133. var view = new View () {
  134. X = 1,
  135. Y = 2,
  136. Width = 3,
  137. Height = 4
  138. };
  139. var super = new View (new Rect (0, 0, 10, 10));
  140. super.Add (view);
  141. super.BeginInit ();
  142. super.EndInit ();
  143. super.LayoutSubviews ();
  144. Assert.Equal (1, view.X);
  145. Assert.Equal (2, view.Y);
  146. Assert.Equal (3, view.Width);
  147. Assert.Equal (4, view.Height);
  148. Assert.False (view.Frame.IsEmpty);
  149. Assert.Equal (new Rect (1, 2, 3, 4), view.Frame);
  150. Assert.False (view.Bounds.IsEmpty);
  151. Assert.Equal (new Rect (0, 0, 3, 4), view.Bounds);
  152. view.LayoutSubviews ();
  153. Assert.Equal (1, view.X);
  154. Assert.Equal (2, view.Y);
  155. Assert.Equal (3, view.Width);
  156. Assert.Equal (4, view.Height);
  157. Assert.False (view.Frame.IsEmpty);
  158. Assert.False (view.Bounds.IsEmpty);
  159. super.Dispose ();
  160. #if DEBUG_IDISPOSABLE
  161. Assert.Empty (Responder.Instances);
  162. #endif
  163. // Default Constructor
  164. view = new View ();
  165. Assert.Equal (0, view.X);
  166. Assert.Equal (0, view.Y);
  167. Assert.Equal (0, view.Width);
  168. Assert.Equal (0, view.Height);
  169. Assert.True (view.Frame.IsEmpty);
  170. Assert.True (view.Bounds.IsEmpty);
  171. view.Dispose ();
  172. // Constructor
  173. view = new View (1, 2, "");
  174. Assert.Equal (1, view.X);
  175. Assert.Equal (2, view.Y);
  176. Assert.Equal (0, view.Width);
  177. Assert.Equal (0, view.Height);
  178. Assert.False (view.Frame.IsEmpty);
  179. Assert.True (view.Bounds.IsEmpty);
  180. view.Dispose ();
  181. // Default Constructor and post assignment equivalent to Object Initializer
  182. view = new View ();
  183. view.X = 1;
  184. view.Y = 2;
  185. view.Width = 3;
  186. view.Height = 4;
  187. super = new View (new Rect (0, 0, 10, 10));
  188. super.Add (view);
  189. super.BeginInit ();
  190. super.EndInit ();
  191. super.LayoutSubviews ();
  192. Assert.Equal (1, view.X);
  193. Assert.Equal (2, view.Y);
  194. Assert.Equal (3, view.Width);
  195. Assert.Equal (4, view.Height);
  196. Assert.False (view.Frame.IsEmpty);
  197. Assert.Equal (new Rect (1, 2, 3, 4), view.Frame);
  198. Assert.False (view.Bounds.IsEmpty);
  199. Assert.Equal (new Rect (0, 0, 3, 4), view.Bounds);
  200. super.Dispose ();
  201. }
  202. [Fact, TestRespondersDisposed]
  203. public void Added_Removed ()
  204. {
  205. var v = new View (new Rect (0, 0, 10, 24));
  206. var t = new View ();
  207. v.Added += (s, e) => {
  208. Assert.Same (v.SuperView, e.Parent);
  209. Assert.Same (t, e.Parent);
  210. Assert.Same (v, e.Child);
  211. };
  212. v.Removed += (s, e) => {
  213. Assert.Same (t, e.Parent);
  214. Assert.Same (v, e.Child);
  215. Assert.True (v.SuperView == null);
  216. };
  217. t.Add (v);
  218. Assert.True (t.Subviews.Count == 1);
  219. t.Remove (v);
  220. Assert.True (t.Subviews.Count == 0);
  221. t.Dispose ();
  222. v.Dispose ();
  223. }
  224. [Fact, TestRespondersDisposed]
  225. public void Initialized_Event_Comparing_With_Added_Event ()
  226. {
  227. Application.Init (new FakeDriver ());
  228. var top = new Toplevel () { Id = "0", }; // Frame: 0, 0, 80, 25; Bounds: 0, 0, 80, 25
  229. var winAddedToTop = new Window () { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () }; // Frame: 0, 0, 80, 25; Bounds: 0, 0, 78, 23
  230. var v1AddedToWin = new View () { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () }; // Frame: 1, 1, 78, 23 (because Windows has a border)
  231. var v2AddedToWin = new View () { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () }; // Frame: 1, 1, 78, 23 (because Windows has a border)
  232. var svAddedTov1 = new View () { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () }; // Frame: 1, 1, 78, 23 (same as it's superview v1AddedToWin)
  233. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  234. winAddedToTop.Added += (s, e) => {
  235. Assert.Equal (e.Parent.Frame.Width, winAddedToTop.Frame.Width);
  236. Assert.Equal (e.Parent.Frame.Height, winAddedToTop.Frame.Height);
  237. };
  238. v1AddedToWin.Added += (s, e) => {
  239. Assert.Equal (e.Parent.Frame.Width, v1AddedToWin.Frame.Width);
  240. Assert.Equal (e.Parent.Frame.Height, v1AddedToWin.Frame.Height);
  241. };
  242. v2AddedToWin.Added += (s, e) => {
  243. Assert.Equal (e.Parent.Frame.Width, v2AddedToWin.Frame.Width);
  244. Assert.Equal (e.Parent.Frame.Height, v2AddedToWin.Frame.Height);
  245. };
  246. svAddedTov1.Added += (s, e) => {
  247. Assert.Equal (e.Parent.Frame.Width, svAddedTov1.Frame.Width);
  248. Assert.Equal (e.Parent.Frame.Height, svAddedTov1.Frame.Height);
  249. };
  250. top.Initialized += (s, e) => {
  251. tc++;
  252. Assert.Equal (1, tc);
  253. Assert.Equal (1, wc);
  254. Assert.Equal (1, v1c);
  255. Assert.Equal (1, v2c);
  256. Assert.Equal (1, sv1c);
  257. Assert.True (top.CanFocus);
  258. Assert.True (winAddedToTop.CanFocus);
  259. Assert.False (v1AddedToWin.CanFocus);
  260. Assert.False (v2AddedToWin.CanFocus);
  261. Assert.False (svAddedTov1.CanFocus);
  262. Application.Refresh ();
  263. };
  264. winAddedToTop.Initialized += (s, e) => {
  265. wc++;
  266. Assert.Equal (top.Bounds.Width, winAddedToTop.Frame.Width);
  267. Assert.Equal (top.Bounds.Height, winAddedToTop.Frame.Height);
  268. };
  269. v1AddedToWin.Initialized += (s, e) => {
  270. v1c++;
  271. // Top.Frame: 0, 0, 80, 25; Top.Bounds: 0, 0, 80, 25
  272. // BUGBUG: This is wrong, it should be 78, 23. This test has always been broken.
  273. // in no way should the v1AddedToWin.Frame be the same as the Top.Frame/Bounds
  274. // as it is a subview of winAddedToTop, which has a border!
  275. //Assert.Equal (top.Bounds.Width, v1AddedToWin.Frame.Width);
  276. //Assert.Equal (top.Bounds.Height, v1AddedToWin.Frame.Height);
  277. };
  278. v2AddedToWin.Initialized += (s, e) => {
  279. v2c++;
  280. // Top.Frame: 0, 0, 80, 25; Top.Bounds: 0, 0, 80, 25
  281. // BUGBUG: This is wrong, it should be 78, 23. This test has always been broken.
  282. // in no way should the v2AddedToWin.Frame be the same as the Top.Frame/Bounds
  283. // as it is a subview of winAddedToTop, which has a border!
  284. //Assert.Equal (top.Bounds.Width, v2AddedToWin.Frame.Width);
  285. //Assert.Equal (top.Bounds.Height, v2AddedToWin.Frame.Height);
  286. };
  287. svAddedTov1.Initialized += (s, e) => {
  288. sv1c++;
  289. // Top.Frame: 0, 0, 80, 25; Top.Bounds: 0, 0, 80, 25
  290. // BUGBUG: This is wrong, it should be 78, 23. This test has always been broken.
  291. // in no way should the svAddedTov1.Frame be the same as the Top.Frame/Bounds
  292. // because sv1AddedTov1 is a subview of v1AddedToWin, which is a subview of
  293. // winAddedToTop, which has a border!
  294. //Assert.Equal (top.Bounds.Width, svAddedTov1.Frame.Width);
  295. //Assert.Equal (top.Bounds.Height, svAddedTov1.Frame.Height);
  296. Assert.False (svAddedTov1.CanFocus);
  297. Assert.Throws<InvalidOperationException> (() => svAddedTov1.CanFocus = true);
  298. Assert.False (svAddedTov1.CanFocus);
  299. };
  300. v1AddedToWin.Add (svAddedTov1);
  301. winAddedToTop.Add (v1AddedToWin, v2AddedToWin);
  302. top.Add (winAddedToTop);
  303. Application.Iteration += (s, a) => {
  304. Application.Refresh ();
  305. top.Running = false;
  306. };
  307. Application.Run (top);
  308. Application.Shutdown ();
  309. Assert.Equal (1, tc);
  310. Assert.Equal (1, wc);
  311. Assert.Equal (1, v1c);
  312. Assert.Equal (1, v2c);
  313. Assert.Equal (1, sv1c);
  314. Assert.True (top.CanFocus);
  315. Assert.True (winAddedToTop.CanFocus);
  316. Assert.False (v1AddedToWin.CanFocus);
  317. Assert.False (v2AddedToWin.CanFocus);
  318. Assert.False (svAddedTov1.CanFocus);
  319. v1AddedToWin.CanFocus = true;
  320. Assert.False (svAddedTov1.CanFocus); // False because sv1 was disposed and it isn't a subview of v1.
  321. }
  322. [Fact, TestRespondersDisposed]
  323. public void Initialized_Event_Will_Be_Invoked_When_Added_Dynamically ()
  324. {
  325. Application.Init (new FakeDriver ());
  326. var t = new Toplevel () { Id = "0", };
  327. var w = new Window () { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  328. var v1 = new View () { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  329. var v2 = new View () { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  330. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  331. t.Initialized += (s, e) => {
  332. tc++;
  333. Assert.Equal (1, tc);
  334. Assert.Equal (1, wc);
  335. Assert.Equal (1, v1c);
  336. Assert.Equal (1, v2c);
  337. Assert.Equal (0, sv1c); // Added after t in the Application.Iteration.
  338. Assert.True (t.CanFocus);
  339. Assert.True (w.CanFocus);
  340. Assert.False (v1.CanFocus);
  341. Assert.False (v2.CanFocus);
  342. Application.Refresh ();
  343. };
  344. w.Initialized += (s, e) => {
  345. wc++;
  346. Assert.Equal (t.Bounds.Width, w.Frame.Width);
  347. Assert.Equal (t.Bounds.Height, w.Frame.Height);
  348. };
  349. v1.Initialized += (s, e) => {
  350. v1c++;
  351. //Assert.Equal (t.Bounds.Width, v1.Frame.Width);
  352. //Assert.Equal (t.Bounds.Height, v1.Frame.Height);
  353. };
  354. v2.Initialized += (s, e) => {
  355. v2c++;
  356. //Assert.Equal (t.Bounds.Width, v2.Frame.Width);
  357. //Assert.Equal (t.Bounds.Height, v2.Frame.Height);
  358. };
  359. w.Add (v1, v2);
  360. t.Add (w);
  361. Application.Iteration += (s, a) => {
  362. var sv1 = new View () { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  363. sv1.Initialized += (s, e) => {
  364. sv1c++;
  365. Assert.NotEqual (t.Frame.Width, sv1.Frame.Width);
  366. Assert.NotEqual (t.Frame.Height, sv1.Frame.Height);
  367. Assert.False (sv1.CanFocus);
  368. Assert.Throws<InvalidOperationException> (() => sv1.CanFocus = true);
  369. Assert.False (sv1.CanFocus);
  370. };
  371. v1.Add (sv1);
  372. Application.Refresh ();
  373. t.Running = false;
  374. };
  375. Application.Run (t);
  376. Application.Shutdown ();
  377. Assert.Equal (1, tc);
  378. Assert.Equal (1, wc);
  379. Assert.Equal (1, v1c);
  380. Assert.Equal (1, v2c);
  381. Assert.Equal (1, sv1c);
  382. Assert.True (t.CanFocus);
  383. Assert.True (w.CanFocus);
  384. Assert.False (v1.CanFocus);
  385. Assert.False (v2.CanFocus);
  386. }
  387. [Theory, TestRespondersDisposed]
  388. [InlineData (1)]
  389. [InlineData (2)]
  390. [InlineData (3)]
  391. public void LabelChangeText_RendersCorrectly_Constructors (int choice)
  392. {
  393. var driver = new FakeDriver ();
  394. Application.Init (driver);
  395. try {
  396. // Create a label with a short text
  397. Label lbl;
  398. var text = "test";
  399. if (choice == 1) {
  400. // An object initializer should call the default constructor.
  401. lbl = new Label { Text = text };
  402. } else if (choice == 2) {
  403. // Calling the default constructor followed by the object initializer.
  404. lbl = new Label () { Text = text };
  405. } else {
  406. // Calling the Text constructor.
  407. lbl = new Label (text);
  408. }
  409. Application.Top.Add (lbl);
  410. Application.Begin (Application.Top);
  411. // should have the initial text
  412. Assert.Equal ((Rune)'t', driver.Contents [0, 0].Rune);
  413. Assert.Equal ((Rune)'e', driver.Contents [0, 1].Rune);
  414. Assert.Equal ((Rune)'s', driver.Contents [0, 2].Rune);
  415. Assert.Equal ((Rune)'t', driver.Contents [0, 3].Rune);
  416. Assert.Equal ((Rune)' ', driver.Contents [0, 4].Rune);
  417. } finally {
  418. Application.Shutdown ();
  419. }
  420. }
  421. [Fact, AutoInitShutdown]
  422. public void Internal_Tests ()
  423. {
  424. Assert.Equal (new [] { View.Direction.Forward, View.Direction.Backward },
  425. Enum.GetValues (typeof (View.Direction)));
  426. var rect = new Rect (1, 1, 10, 1);
  427. var view = new View (rect);
  428. var top = Application.Top;
  429. top.Add (view);
  430. Assert.Equal (View.Direction.Forward, view.FocusDirection);
  431. view.FocusDirection = View.Direction.Backward;
  432. Assert.Equal (View.Direction.Backward, view.FocusDirection);
  433. Assert.Empty (view.InternalSubviews);
  434. // BUGBUG: v2 - _needsDisplay needs debugging - test disabled for now.
  435. //Assert.Equal (new Rect (new Point (0, 0), rect.Size), view._needsDisplay);
  436. Assert.True (view.LayoutNeeded);
  437. Assert.False (view.SubViewNeedsDisplay);
  438. Assert.False (view._addingView);
  439. view._addingView = true;
  440. Assert.True (view._addingView);
  441. view.BoundsToScreen (0, 0, out int rcol, out int rrow);
  442. Assert.Equal (1, rcol);
  443. Assert.Equal (1, rrow);
  444. Assert.Equal (rect, view.BoundsToScreen (view.Bounds));
  445. Assert.Equal (top.Bounds, view.ScreenClip (top.Bounds));
  446. Assert.True (view.LayoutStyle == LayoutStyle.Absolute);
  447. var runState = Application.Begin (top);
  448. // BUGBUG: This is a SetRelativeLayout test. It should be moved to SetRelativeLayoutTests.cs
  449. view.Width = Dim.Fill (); // Width should be 79 (Top.Width - 1)
  450. Assert.Equal ("Fill(0)", view.Width.ToString ());
  451. view.Height = Dim.Fill (); // Height should be 24 (Top.Height - 1)
  452. // #3127: Before: Frame was not being set when Width and Height were set to Dim.Fill()
  453. // After: Frame is set to the parent's Frame when Width and Height are set to Dim.Fill()
  454. Assert.Equal (79, view.Bounds.Width);
  455. Assert.Equal (24, view.Bounds.Height);
  456. //view.LayoutStyle = LayoutStyle.Computed;
  457. view.SetRelativeLayout (top.Bounds);
  458. Assert.Equal ("Fill(0)", view.Width.ToString ());
  459. Assert.Equal (1, view.Frame.X);
  460. Assert.Equal (1, view.Frame.Y);
  461. Assert.Equal (79, view.Frame.Width);
  462. Assert.Equal (24, view.Frame.Height);
  463. Assert.Equal (0, view.Bounds.X);
  464. Assert.Equal (0, view.Bounds.Y);
  465. Assert.Equal (79, view.Bounds.Width);
  466. Assert.Equal (24, view.Bounds.Height);
  467. // BUGBUG: This is a SetRelativeLayout test. It should be moved to SetRelativeLayoutTests.cs
  468. view.X = 0;
  469. view.Y = 0;
  470. Assert.Equal ("Absolute(0)", view.X.ToString ());
  471. Assert.Equal ("Fill(0)", view.Width.ToString ());
  472. view.SetRelativeLayout (top.Bounds);
  473. Assert.Equal (0, view.Frame.X);
  474. Assert.Equal (0, view.Frame.Y);
  475. Assert.Equal (80, view.Frame.Width);
  476. Assert.Equal (25, view.Frame.Height);
  477. Assert.Equal (0, view.Bounds.X);
  478. Assert.Equal (0, view.Bounds.Y);
  479. Assert.Equal (80, view.Bounds.Width);
  480. Assert.Equal (25, view.Bounds.Height);
  481. // BUGBUG: This is a layout test. It should be moved to LayoutTests.cs
  482. bool layoutStarted = false;
  483. view.LayoutStarted += (s, e) => layoutStarted = true;
  484. view.OnLayoutStarted (null);
  485. Assert.True (layoutStarted);
  486. view.LayoutComplete += (s, e) => layoutStarted = false;
  487. view.OnLayoutComplete (null);
  488. Assert.False (layoutStarted);
  489. // This test has been moved to SetRlativeLayoutTests because it is testing
  490. // SetRelativeLayout. In addition, the old test was bogus because it was testing the wrong thing (and
  491. // because in v1 Pos.Center was broken in this regard!
  492. view.X = Pos.Center () - 41;
  493. view.Y = Pos.Center () - 13;
  494. view.SetRelativeLayout (top.Bounds);
  495. top.LayoutSubviews (); // BUGBUG: v2 - ??
  496. view.BoundsToScreen (0, 0, out rcol, out rrow);
  497. Assert.Equal (-41, rcol);
  498. Assert.Equal (-13, rrow);
  499. Application.End (runState);
  500. }
  501. [Fact, AutoInitShutdown]
  502. public void Visible_Sets_Also_Sets_Subviews ()
  503. {
  504. var button = new Button ("Click Me");
  505. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  506. win.Add (button);
  507. var top = Application.Top;
  508. top.Add (win);
  509. var iterations = 0;
  510. Application.Iteration += (s, a) => {
  511. iterations++;
  512. Assert.True (button.Visible);
  513. Assert.True (button.CanFocus);
  514. Assert.True (button.HasFocus);
  515. Assert.True (win.Visible);
  516. Assert.True (win.CanFocus);
  517. Assert.True (win.HasFocus);
  518. Assert.True (RunesCount () > 0);
  519. win.Visible = false;
  520. Assert.True (button.Visible);
  521. Assert.True (button.CanFocus);
  522. Assert.False (button.HasFocus);
  523. Assert.False (win.Visible);
  524. Assert.True (win.CanFocus);
  525. Assert.False (win.HasFocus);
  526. button.SetFocus ();
  527. Assert.False (button.HasFocus);
  528. Assert.False (win.HasFocus);
  529. win.SetFocus ();
  530. Assert.False (button.HasFocus);
  531. Assert.False (win.HasFocus);
  532. top.Draw ();
  533. Assert.True (RunesCount () == 0);
  534. win.Visible = true;
  535. win.FocusFirst ();
  536. Assert.True (button.HasFocus);
  537. Assert.True (win.HasFocus);
  538. top.Draw ();
  539. Assert.True (RunesCount () > 0);
  540. Application.RequestStop ();
  541. };
  542. Application.Run ();
  543. Assert.Equal (1, iterations);
  544. int RunesCount ()
  545. {
  546. var contents = ((FakeDriver)Application.Driver).Contents;
  547. var runesCount = 0;
  548. for (int i = 0; i < Application.Driver.Rows; i++) {
  549. for (int j = 0; j < Application.Driver.Cols; j++) {
  550. if (contents [i, j].Rune != (Rune)' ') {
  551. runesCount++;
  552. }
  553. }
  554. }
  555. return runesCount;
  556. }
  557. }
  558. [Fact, AutoInitShutdown]
  559. public void GetTopSuperView_Test ()
  560. {
  561. var v1 = new View ();
  562. var fv1 = new FrameView ();
  563. fv1.Add (v1);
  564. var tf1 = new TextField ();
  565. var w1 = new Window ();
  566. w1.Add (fv1, tf1);
  567. var top1 = new Toplevel ();
  568. top1.Add (w1);
  569. var v2 = new View ();
  570. var fv2 = new FrameView ();
  571. fv2.Add (v2);
  572. var tf2 = new TextField ();
  573. var w2 = new Window ();
  574. w2.Add (fv2, tf2);
  575. var top2 = new Toplevel ();
  576. top2.Add (w2);
  577. Assert.Equal (top1, v1.GetTopSuperView ());
  578. Assert.Equal (top2, v2.GetTopSuperView ());
  579. v1.Dispose ();
  580. fv1.Dispose ();
  581. tf1.Dispose ();
  582. w1.Dispose ();
  583. top1.Dispose ();
  584. v2.Dispose ();
  585. fv2.Dispose ();
  586. tf2.Dispose ();
  587. w2.Dispose ();
  588. top2.Dispose ();
  589. }
  590. [Fact, AutoInitShutdown]
  591. public void Clear_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  592. {
  593. var view = new FrameView () {
  594. Width = Dim.Fill (),
  595. Height = Dim.Fill ()
  596. };
  597. view.DrawContent += (s, e) => {
  598. var savedClip = Application.Driver.Clip;
  599. Application.Driver.Clip = new Rect (1, 1, view.Bounds.Width, view.Bounds.Height);
  600. for (int row = 0; row < view.Bounds.Height; row++) {
  601. Application.Driver.Move (1, row + 1);
  602. for (int col = 0; col < view.Bounds.Width; col++) {
  603. Application.Driver.AddStr ($"{col}");
  604. }
  605. }
  606. Application.Driver.Clip = savedClip;
  607. e.Cancel = true;
  608. };
  609. Application.Top.Add (view);
  610. Application.Begin (Application.Top);
  611. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  612. var expected = @"
  613. ┌──────────────────┐
  614. │012345678910111213│
  615. │012345678910111213│
  616. │012345678910111213│
  617. │012345678910111213│
  618. │012345678910111213│
  619. │012345678910111213│
  620. │012345678910111213│
  621. │012345678910111213│
  622. └──────────────────┘
  623. ";
  624. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  625. Assert.Equal (new Rect (0, 0, 20, 10), pos);
  626. view.Clear (view.Frame);
  627. expected = @"
  628. ";
  629. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  630. Assert.Equal (Rect.Empty, pos);
  631. }
  632. [Fact, AutoInitShutdown]
  633. public void Clear_Bounds_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  634. {
  635. var view = new FrameView () {
  636. Width = Dim.Fill (),
  637. Height = Dim.Fill ()
  638. };
  639. view.DrawContent += (s, e) => {
  640. var savedClip = Application.Driver.Clip;
  641. Application.Driver.Clip = new Rect (1, 1, view.Bounds.Width, view.Bounds.Height);
  642. for (int row = 0; row < view.Bounds.Height; row++) {
  643. Application.Driver.Move (1, row + 1);
  644. for (int col = 0; col < view.Bounds.Width; col++) {
  645. Application.Driver.AddStr ($"{col}");
  646. }
  647. }
  648. Application.Driver.Clip = savedClip;
  649. e.Cancel = true;
  650. };
  651. Application.Top.Add (view);
  652. Application.Begin (Application.Top);
  653. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  654. var expected = @"
  655. ┌──────────────────┐
  656. │012345678910111213│
  657. │012345678910111213│
  658. │012345678910111213│
  659. │012345678910111213│
  660. │012345678910111213│
  661. │012345678910111213│
  662. │012345678910111213│
  663. │012345678910111213│
  664. └──────────────────┘
  665. ";
  666. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  667. Assert.Equal (new Rect (0, 0, 20, 10), pos);
  668. view.Clear (view.Frame);
  669. expected = @"
  670. ";
  671. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  672. Assert.Equal (Rect.Empty, pos);
  673. }
  674. [Fact, TestRespondersDisposed]
  675. public void IsAdded_Added_Removed ()
  676. {
  677. var top = new Toplevel ();
  678. var view = new View ();
  679. Assert.False (view.IsAdded);
  680. top.Add (view);
  681. Assert.True (view.IsAdded);
  682. top.Remove (view);
  683. Assert.False (view.IsAdded);
  684. top.Dispose ();
  685. view.Dispose ();
  686. }
  687. [Fact, AutoInitShutdown]
  688. public void Visible_Clear_The_View_Output ()
  689. {
  690. var view = new View ("Testing visibility."); // use View, not Label to avoid AutoSize == true
  691. Assert.Equal ("Testing visibility.".Length, view.Frame.Width);
  692. Assert.Equal (1, view.Height);
  693. var win = new Window ();
  694. win.Add (view);
  695. var top = Application.Top;
  696. top.Add (win);
  697. var rs = Application.Begin (top);
  698. Assert.True (view.Visible);
  699. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  700. TestHelpers.AssertDriverContentsWithFrameAre (@"
  701. ┌────────────────────────────┐
  702. │Testing visibility. │
  703. │ │
  704. │ │
  705. └────────────────────────────┘
  706. ", output);
  707. view.Visible = false;
  708. bool firstIteration = false;
  709. Application.RunIteration (ref rs, ref firstIteration);
  710. TestHelpers.AssertDriverContentsWithFrameAre (@"
  711. ┌────────────────────────────┐
  712. │ │
  713. │ │
  714. │ │
  715. └────────────────────────────┘
  716. ", output);
  717. Application.End (rs);
  718. }
  719. [Fact, AutoInitShutdown]
  720. public void DrawContentComplete_Event_Is_Always_Called ()
  721. {
  722. var viewCalled = false;
  723. var tvCalled = false;
  724. var view = new View ("View") { Width = 10, Height = 10 };
  725. view.DrawContentComplete += (s, e) => viewCalled = true;
  726. var tv = new TextView () { Y = 11, Width = 10, Height = 10 };
  727. tv.DrawContentComplete += (s, e) => tvCalled = true;
  728. Application.Top.Add (view, tv);
  729. Application.Begin (Application.Top);
  730. Assert.True (viewCalled);
  731. Assert.True (tvCalled);
  732. }
  733. [Fact, AutoInitShutdown]
  734. public void GetNormalColor_ColorScheme ()
  735. {
  736. var view = new View { ColorScheme = Colors.Base };
  737. Assert.Equal (view.ColorScheme.Normal, view.GetNormalColor ());
  738. view.Enabled = false;
  739. Assert.Equal (view.ColorScheme.Disabled, view.GetNormalColor ());
  740. view.Dispose ();
  741. }
  742. [Fact, AutoInitShutdown]
  743. public void GetHotNormalColor_ColorScheme ()
  744. {
  745. var view = new View { ColorScheme = Colors.Base };
  746. Assert.Equal (view.ColorScheme.HotNormal, view.GetHotNormalColor ());
  747. view.Enabled = false;
  748. Assert.Equal (view.ColorScheme.Disabled, view.GetHotNormalColor ());
  749. view.Dispose ();
  750. }
  751. [Theory, AutoInitShutdown]
  752. [InlineData (true)]
  753. [InlineData (false)]
  754. public void Clear_Does_Not_Spillover_Its_Parent (bool label)
  755. {
  756. var root = new View () { Width = 20, Height = 10, ColorScheme = Colors.Base };
  757. var v = label == true ?
  758. new Label (new string ('c', 100)) {
  759. Width = Dim.Fill (),
  760. } :
  761. (View)new TextView () {
  762. Height = 1,
  763. Text = new string ('c', 100),
  764. Width = Dim.Fill ()
  765. };
  766. root.Add (v);
  767. Application.Top.Add (root);
  768. var runState = Application.Begin (Application.Top);
  769. if (label) {
  770. Assert.True (v.AutoSize);
  771. Assert.False (v.CanFocus);
  772. Assert.Equal (new Rect (0, 0, 100, 1), v.Frame);
  773. } else {
  774. Assert.False (v.AutoSize);
  775. Assert.True (v.CanFocus);
  776. Assert.Equal (new Rect (0, 0, 20, 1), v.Frame);
  777. }
  778. TestHelpers.AssertDriverContentsWithFrameAre (@"
  779. cccccccccccccccccccc", output);
  780. var attributes = new Attribute [] {
  781. Colors.TopLevel.Normal,
  782. Colors.Base.Normal,
  783. Colors.Base.Focus
  784. };
  785. if (label) {
  786. TestHelpers.AssertDriverColorsAre (@"
  787. 111111111111111111110
  788. 111111111111111111110", driver: Application.Driver, attributes);
  789. } else {
  790. TestHelpers.AssertDriverColorsAre (@"
  791. 222222222222222222220
  792. 111111111111111111110", driver: Application.Driver, attributes);
  793. }
  794. if (label) {
  795. root.CanFocus = true;
  796. v.CanFocus = true;
  797. Assert.False (v.HasFocus);
  798. v.SetFocus ();
  799. Assert.True (v.HasFocus);
  800. Application.Refresh ();
  801. TestHelpers.AssertDriverColorsAre (@"
  802. 222222222222222222220
  803. 111111111111111111110", driver: Application.Driver, attributes);
  804. }
  805. Application.End (runState);
  806. }
  807. public class DerivedView : View {
  808. public DerivedView ()
  809. {
  810. CanFocus = true;
  811. }
  812. public bool IsKeyDown { get; set; }
  813. public bool IsKeyPress { get; set; }
  814. public bool IsKeyUp { get; set; }
  815. public override string Text { get; set; }
  816. public override bool OnKeyDown (Key keyEvent)
  817. {
  818. IsKeyDown = true;
  819. return true;
  820. }
  821. public override bool OnProcessKeyDown (Key keyEvent)
  822. {
  823. IsKeyPress = true;
  824. return true;
  825. }
  826. public override bool OnKeyUp (Key keyEvent)
  827. {
  828. IsKeyUp = true;
  829. return true;
  830. }
  831. public override void OnDrawContent (Rect contentArea)
  832. {
  833. var idx = 0;
  834. // BUGBUG: v2 - this should use Boudns, not Frame
  835. for (int r = 0; r < Frame.Height; r++) {
  836. for (int c = 0; c < Frame.Width; c++) {
  837. if (idx < Text.Length) {
  838. var rune = Text [idx];
  839. if (rune != '\n') {
  840. AddRune (c, r, (Rune)Text [idx]);
  841. }
  842. idx++;
  843. if (rune == '\n') {
  844. break;
  845. }
  846. }
  847. }
  848. }
  849. ClearLayoutNeeded ();
  850. ClearNeedsDisplay ();
  851. }
  852. }
  853. [Fact, AutoInitShutdown]
  854. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  855. {
  856. var label = new Label ("At 0,0");
  857. var view = new DerivedView () {
  858. X = 2,
  859. Y = 2,
  860. Width = 30,
  861. Height = 2,
  862. Text = "A text with some long width\n and also with two lines."
  863. };
  864. var top = Application.Top;
  865. top.Add (label, view);
  866. var runState = Application.Begin (top);
  867. top.Draw ();
  868. TestHelpers.AssertDriverContentsWithFrameAre (@"
  869. At 0,0
  870. A text with some long width
  871. and also with two lines. ", output);
  872. view.Frame = new Rect (1, 1, 10, 1);
  873. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  874. Assert.Equal (LayoutStyle.Absolute, view.LayoutStyle);
  875. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  876. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplayRect);
  877. top.Draw ();
  878. TestHelpers.AssertDriverContentsWithFrameAre (@"
  879. At 0,0
  880. A text wit", output);
  881. Application.End (runState);
  882. }
  883. [Fact, AutoInitShutdown]
  884. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  885. {
  886. var label = new Label ("At 0,0");
  887. var view = new DerivedView () {
  888. X = 2,
  889. Y = 2,
  890. Width = 30,
  891. Height = 2,
  892. Text = "A text with some long width\n and also with two lines."
  893. };
  894. var top = Application.Top;
  895. top.Add (label, view);
  896. var runState = Application.Begin (top);
  897. top.Draw ();
  898. TestHelpers.AssertDriverContentsWithFrameAre (@"
  899. At 0,0
  900. A text with some long width
  901. and also with two lines. ", output);
  902. view.X = 1;
  903. view.Y = 1;
  904. view.Width = 10;
  905. view.Height = 1;
  906. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  907. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  908. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplayRect);
  909. top.Draw ();
  910. TestHelpers.AssertDriverContentsWithFrameAre (@"
  911. At 0,0
  912. A text wit", output);
  913. Application.End (runState);
  914. }
  915. [Fact, AutoInitShutdown]
  916. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  917. {
  918. var label = new Label ("At 0,0");
  919. var view = new DerivedView () {
  920. X = 2,
  921. Y = 2,
  922. Width = 30,
  923. Height = 2,
  924. Text = "A text with some long width\n and also with two lines."
  925. };
  926. var top = Application.Top;
  927. top.Add (label, view);
  928. var runState = Application.Begin (top);
  929. view.Draw ();
  930. TestHelpers.AssertDriverContentsWithFrameAre (@"
  931. At 0,0
  932. A text with some long width
  933. and also with two lines. ", output);
  934. view.Frame = new Rect (1, 1, 10, 1);
  935. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  936. Assert.Equal (LayoutStyle.Absolute, view.LayoutStyle);
  937. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  938. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplayRect);
  939. view.Draw ();
  940. TestHelpers.AssertDriverContentsWithFrameAre (@"
  941. At 0,0
  942. A text wit
  943. A text with some long width
  944. and also with two lines. ", output);
  945. Application.End (runState);
  946. }
  947. [Fact, AutoInitShutdown]
  948. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  949. {
  950. var label = new Label ("At 0,0");
  951. var view = new DerivedView () {
  952. X = 2,
  953. Y = 2,
  954. Width = 30,
  955. Height = 2,
  956. Text = "A text with some long width\n and also with two lines."
  957. };
  958. var top = Application.Top;
  959. top.Add (label, view);
  960. var runState = Application.Begin (top);
  961. view.Draw ();
  962. TestHelpers.AssertDriverContentsWithFrameAre (@"
  963. At 0,0
  964. A text with some long width
  965. and also with two lines. ", output);
  966. view.X = 1;
  967. view.Y = 1;
  968. view.Width = 10;
  969. view.Height = 1;
  970. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  971. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  972. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplayRect);
  973. view.Draw ();
  974. TestHelpers.AssertDriverContentsWithFrameAre (@"
  975. At 0,0
  976. A text wit
  977. A text with some long width
  978. and also with two lines. ", output);
  979. Application.End (runState);
  980. }
  981. [Fact, AutoInitShutdown]
  982. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  983. {
  984. var label = new Label ("At 0,0");
  985. var view = new DerivedView () {
  986. X = 2,
  987. Y = 2,
  988. Width = 30,
  989. Height = 2,
  990. Text = "A text with some long width\n and also with two lines."
  991. };
  992. var top = Application.Top;
  993. top.Add (label, view);
  994. var runState = Application.Begin (top);
  995. TestHelpers.AssertDriverContentsWithFrameAre (@"
  996. At 0,0
  997. A text with some long width
  998. and also with two lines. ", output);
  999. view.Frame = new Rect (3, 3, 10, 1);
  1000. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  1001. Assert.Equal (LayoutStyle.Absolute, view.LayoutStyle);
  1002. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  1003. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplayRect);
  1004. top.Draw ();
  1005. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1006. At 0,0
  1007. A text wit", output);
  1008. Application.End (runState);
  1009. }
  1010. [Fact, AutoInitShutdown]
  1011. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  1012. {
  1013. var label = new Label ("At 0,0");
  1014. var view = new DerivedView () {
  1015. X = 2,
  1016. Y = 2,
  1017. Width = 30,
  1018. Height = 2,
  1019. Text = "A text with some long width\n and also with two lines."
  1020. };
  1021. var top = Application.Top;
  1022. top.Add (label, view);
  1023. var runState = Application.Begin (top);
  1024. top.Draw ();
  1025. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1026. At 0,0
  1027. A text with some long width
  1028. and also with two lines. ", output);
  1029. view.X = 3;
  1030. view.Y = 3;
  1031. view.Width = 10;
  1032. view.Height = 1;
  1033. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  1034. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  1035. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplayRect);
  1036. top.Draw ();
  1037. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1038. At 0,0
  1039. A text wit", output);
  1040. Application.End (runState);
  1041. }
  1042. [Fact, AutoInitShutdown]
  1043. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  1044. {
  1045. var label = new Label ("At 0,0");
  1046. var view = new DerivedView () {
  1047. X = 2,
  1048. Y = 2,
  1049. Width = 30,
  1050. Height = 2,
  1051. Text = "A text with some long width\n and also with two lines."
  1052. };
  1053. var top = Application.Top;
  1054. top.Add (label, view);
  1055. var runState = Application.Begin (top);
  1056. view.Draw ();
  1057. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1058. At 0,0
  1059. A text with some long width
  1060. and also with two lines. ", output);
  1061. view.Frame = new Rect (3, 3, 10, 1);
  1062. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  1063. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplayRect);
  1064. view.Draw ();
  1065. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1066. At 0,0
  1067. A text with some long width
  1068. A text witith two lines. ", output);
  1069. Application.End (runState);
  1070. }
  1071. [Fact, AutoInitShutdown]
  1072. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  1073. {
  1074. var label = new Label ("At 0,0");
  1075. var view = new DerivedView () {
  1076. X = 2,
  1077. Y = 2,
  1078. Width = 30,
  1079. Height = 2,
  1080. Text = "A text with some long width\n and also with two lines."
  1081. };
  1082. var top = Application.Top;
  1083. top.Add (label, view);
  1084. var runState = Application.Begin (top);
  1085. view.Draw ();
  1086. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1087. At 0,0
  1088. A text with some long width
  1089. and also with two lines. ", output);
  1090. view.X = 3;
  1091. view.Y = 3;
  1092. view.Width = 10;
  1093. view.Height = 1;
  1094. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  1095. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  1096. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplayRect);
  1097. view.Draw ();
  1098. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1099. At 0,0
  1100. A text with some long width
  1101. A text witith two lines. ", output);
  1102. Application.End (runState);
  1103. }
  1104. [Fact, AutoInitShutdown]
  1105. public void Test_Nested_Views_With_Height_Equal_To_One ()
  1106. {
  1107. var v = new View () { Width = 11, Height = 3, ColorScheme = new ColorScheme () };
  1108. var top = new View () { Width = Dim.Fill (), Height = 1 };
  1109. var bottom = new View () { Width = Dim.Fill (), Height = 1, Y = 2 };
  1110. top.Add (new Label ("111"));
  1111. v.Add (top);
  1112. v.Add (new LineView (Orientation.Horizontal) { Y = 1 });
  1113. bottom.Add (new Label ("222"));
  1114. v.Add (bottom);
  1115. v.BeginInit ();
  1116. v.EndInit ();
  1117. v.LayoutSubviews ();
  1118. v.Draw ();
  1119. string looksLike =
  1120. @"
  1121. 111
  1122. ───────────
  1123. 222";
  1124. TestHelpers.AssertDriverContentsAre (looksLike, output);
  1125. v.Dispose ();
  1126. top.Dispose ();
  1127. bottom.Dispose ();
  1128. }
  1129. [Fact, AutoInitShutdown]
  1130. public void Frame_Set_After_Initialize_Update_NeededDisplay ()
  1131. {
  1132. var frame = new FrameView ();
  1133. var label = new Label ("This should be the first line.") {
  1134. ColorScheme = Colors.Menu,
  1135. Width = Dim.Fill (),
  1136. X = 0, // don't overcomplicate unit tests
  1137. Y = 0
  1138. };
  1139. var button = new Button ("Press me!") {
  1140. X = 0, // don't overcomplicate unit tests
  1141. Y = 1
  1142. };
  1143. frame.Add (label, button);
  1144. frame.X = Pos.Center ();
  1145. frame.Y = Pos.Center ();
  1146. frame.Width = 40;
  1147. frame.Height = 8;
  1148. var top = Application.Top;
  1149. top.Add (frame);
  1150. var runState = Application.Begin (top);
  1151. top.LayoutComplete += (s, e) => {
  1152. Assert.Equal (new Rect (0, 0, 80, 25), top._needsDisplayRect);
  1153. };
  1154. frame.LayoutComplete += (s, e) => {
  1155. Assert.Equal (new Rect (0, 0, 40, 8), frame._needsDisplayRect);
  1156. };
  1157. label.LayoutComplete += (s, e) => {
  1158. Assert.Equal (new Rect (0, 0, 38, 1), label._needsDisplayRect);
  1159. };
  1160. button.LayoutComplete += (s, e) => {
  1161. Assert.Equal (new Rect (0, 0, 13, 1), button._needsDisplayRect);
  1162. };
  1163. Assert.True (label.AutoSize);
  1164. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  1165. Assert.Equal (new Rect (20, 8, 40, 8), frame.Frame);
  1166. Assert.Equal (new Rect (20, 8, 60, 16), new Rect (
  1167. frame.Frame.Left, frame.Frame.Top,
  1168. frame.Frame.Right, frame.Frame.Bottom));
  1169. Assert.Equal (new Rect (0, 0, 38, 1), label.Frame);
  1170. Assert.Equal (new Rect (0, 1, 13, 1), button.Frame); // this proves frame was set
  1171. Application.End (runState);
  1172. }
  1173. [Fact, TestRespondersDisposed]
  1174. public void Dispose_View ()
  1175. {
  1176. var view = new View ();
  1177. Assert.NotNull (view.Margin);
  1178. Assert.NotNull (view.Border);
  1179. Assert.NotNull (view.Padding);
  1180. #if DEBUG_IDISPOSABLE
  1181. Assert.Equal (4, Responder.Instances.Count);
  1182. #endif
  1183. view.Dispose ();
  1184. Assert.Null (view.Margin);
  1185. Assert.Null (view.Border);
  1186. Assert.Null (view.Padding);
  1187. }
  1188. }
  1189. }