ViewTests.cs 42 KB

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