ViewTests.cs 41 KB

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