ViewTests.cs 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  1. using System;
  2. using Xunit;
  3. using Xunit.Abstractions;
  4. using NStack;
  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()({X=0,Y=0,Width=0,Height=0})", r.ToString ());
  22. Assert.False (r.CanFocus);
  23. Assert.False (r.HasFocus);
  24. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  25. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  26. Assert.Null (r.Focused);
  27. Assert.Null (r.ColorScheme);
  28. Assert.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()({X=0,Y=0,Width=0,Height=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()({X=1,Y=2,Width=3,Height=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)({X=0,Y=0,Width=1,Height=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.Top.LayoutSubviews ();
  466. Application.Top.Redraw (Application.Top.Bounds);
  467. // should have the initial text
  468. Assert.Equal ('t', driver.Contents [0, 0, 0]);
  469. Assert.Equal ('e', driver.Contents [0, 1, 0]);
  470. Assert.Equal ('s', driver.Contents [0, 2, 0]);
  471. Assert.Equal ('t', driver.Contents [0, 3, 0]);
  472. Assert.Equal (' ', driver.Contents [0, 4, 0]);
  473. } finally {
  474. Application.Shutdown ();
  475. }
  476. }
  477. [Fact]
  478. [AutoInitShutdown]
  479. public void Internal_Tests ()
  480. {
  481. Assert.Equal (new [] { View.Direction.Forward, View.Direction.Backward },
  482. Enum.GetValues (typeof (View.Direction)));
  483. var rect = new Rect (1, 1, 10, 1);
  484. var view = new View (rect);
  485. var top = Application.Top;
  486. top.Add (view);
  487. Assert.Equal (View.Direction.Forward, view.FocusDirection);
  488. view.FocusDirection = View.Direction.Backward;
  489. Assert.Equal (View.Direction.Backward, view.FocusDirection);
  490. Assert.Empty (view.InternalSubviews);
  491. // BUGBUG: v2 - _needsDisplay needs debugging - test disabled for now.
  492. //Assert.Equal (new Rect (new Point (0, 0), rect.Size), view._needsDisplay);
  493. Assert.True (view.LayoutNeeded);
  494. Assert.False (view._childNeedsDisplay);
  495. Assert.False (view.addingView);
  496. view.addingView = true;
  497. Assert.True (view.addingView);
  498. view.ViewToScreen (0, 0, out int rcol, out int rrow);
  499. Assert.Equal (1, rcol);
  500. Assert.Equal (1, rrow);
  501. Assert.Equal (rect, view.ViewToScreen (view.Bounds));
  502. Assert.Equal (top.Bounds, view.ScreenClip (top.Bounds));
  503. Assert.True (view.LayoutStyle == LayoutStyle.Absolute);
  504. Application.Begin (top);
  505. view.Width = Dim.Fill ();
  506. view.Height = Dim.Fill ();
  507. Assert.Equal (10, view.Bounds.Width);
  508. Assert.Equal (1, view.Bounds.Height);
  509. view.LayoutStyle = LayoutStyle.Computed;
  510. view.SetRelativeLayout (top.Bounds);
  511. Assert.Equal (1, view.Frame.X);
  512. Assert.Equal (1, view.Frame.Y);
  513. Assert.Equal (79, view.Frame.Width);
  514. Assert.Equal (24, view.Frame.Height);
  515. Assert.Equal (0, view.Bounds.X);
  516. Assert.Equal (0, view.Bounds.Y);
  517. Assert.Equal (79, view.Bounds.Width);
  518. Assert.Equal (24, view.Bounds.Height);
  519. view.X = 0;
  520. view.Y = 0;
  521. Assert.Equal ("Absolute(0)", view.X.ToString ());
  522. Assert.Equal ("Fill(0)", view.Width.ToString ());
  523. view.SetRelativeLayout (top.Bounds);
  524. Assert.Equal (0, view.Frame.X);
  525. Assert.Equal (0, view.Frame.Y);
  526. Assert.Equal (80, view.Frame.Width);
  527. Assert.Equal (25, view.Frame.Height);
  528. Assert.Equal (0, view.Bounds.X);
  529. Assert.Equal (0, view.Bounds.Y);
  530. Assert.Equal (80, view.Bounds.Width);
  531. Assert.Equal (25, view.Bounds.Height);
  532. bool layoutStarted = false;
  533. view.LayoutStarted += (s, e) => layoutStarted = true;
  534. view.OnLayoutStarted (null);
  535. Assert.True (layoutStarted);
  536. view.LayoutComplete += (s, e) => layoutStarted = false;
  537. view.OnLayoutComplete (null);
  538. Assert.False (layoutStarted);
  539. view.X = Pos.Center () - 41;
  540. view.Y = Pos.Center () - 13;
  541. view.SetRelativeLayout (top.Bounds);
  542. top.LayoutSubviews (); // BUGBUG: v2 - ??
  543. view.ViewToScreen (0, 0, out rcol, out rrow);
  544. Assert.Equal (-41, rcol);
  545. Assert.Equal (-13, rrow);
  546. }
  547. [Fact]
  548. [AutoInitShutdown]
  549. public void Visible_Sets_Also_Sets_Subviews ()
  550. {
  551. var button = new Button ("Click Me");
  552. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  553. win.Add (button);
  554. var top = Application.Top;
  555. top.Add (win);
  556. var iterations = 0;
  557. Application.Iteration += () => {
  558. iterations++;
  559. Assert.True (button.Visible);
  560. Assert.True (button.CanFocus);
  561. Assert.True (button.HasFocus);
  562. Assert.True (win.Visible);
  563. Assert.True (win.CanFocus);
  564. Assert.True (win.HasFocus);
  565. Assert.True (RunesCount () > 0);
  566. win.Visible = false;
  567. Assert.True (button.Visible);
  568. Assert.True (button.CanFocus);
  569. Assert.False (button.HasFocus);
  570. Assert.False (win.Visible);
  571. Assert.True (win.CanFocus);
  572. Assert.False (win.HasFocus);
  573. button.SetFocus ();
  574. Assert.False (button.HasFocus);
  575. Assert.False (win.HasFocus);
  576. win.SetFocus ();
  577. Assert.False (button.HasFocus);
  578. Assert.False (win.HasFocus);
  579. top.Redraw (top.Bounds);
  580. Assert.True (RunesCount () == 0);
  581. win.Visible = true;
  582. win.FocusFirst ();
  583. Assert.True (button.HasFocus);
  584. Assert.True (win.HasFocus);
  585. top.Redraw (top.Bounds);
  586. Assert.True (RunesCount () > 0);
  587. Application.RequestStop ();
  588. };
  589. Application.Run ();
  590. Assert.Equal (1, iterations);
  591. int RunesCount ()
  592. {
  593. var contents = ((FakeDriver)Application.Driver).Contents;
  594. var runesCount = 0;
  595. for (int i = 0; i < Application.Driver.Rows; i++) {
  596. for (int j = 0; j < Application.Driver.Cols; j++) {
  597. if (contents [i, j, 0] != ' ') {
  598. runesCount++;
  599. }
  600. }
  601. }
  602. return runesCount;
  603. }
  604. }
  605. [Fact]
  606. [AutoInitShutdown]
  607. public void GetTopSuperView_Test ()
  608. {
  609. var v1 = new View ();
  610. var fv1 = new FrameView ();
  611. fv1.Add (v1);
  612. var tf1 = new TextField ();
  613. var w1 = new Window ();
  614. w1.Add (fv1, tf1);
  615. var top1 = new Toplevel ();
  616. top1.Add (w1);
  617. var v2 = new View ();
  618. var fv2 = new FrameView ();
  619. fv2.Add (v2);
  620. var tf2 = new TextField ();
  621. var w2 = new Window ();
  622. w2.Add (fv2, tf2);
  623. var top2 = new Toplevel ();
  624. top2.Add (w2);
  625. Assert.Equal (top1, v1.GetTopSuperView ());
  626. Assert.Equal (top2, v2.GetTopSuperView ());
  627. }
  628. [Fact, AutoInitShutdown]
  629. public void DrawFrame_With_Positive_Positions ()
  630. {
  631. var view = new View (new Rect (0, 0, 8, 4));
  632. view.DrawContent += (s, e) => view.DrawFrame (view.Bounds, 0, true);
  633. Assert.Equal (Point.Empty, new Point (view.Frame.X, view.Frame.Y));
  634. Assert.Equal (new Size (8, 4), new Size (view.Frame.Width, view.Frame.Height));
  635. Application.Top.Add (view);
  636. Application.Begin (Application.Top);
  637. var expected = @"
  638. ┌──────┐
  639. │ │
  640. │ │
  641. └──────┘
  642. ";
  643. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  644. Assert.Equal (new Rect (0, 0, 8, 4), pos);
  645. }
  646. [Fact, AutoInitShutdown]
  647. public void DrawFrame_With_Minimum_Size ()
  648. {
  649. var view = new View (new Rect (0, 0, 2, 2));
  650. view.DrawContent += (s, e) => view.DrawFrame (view.Bounds, 0, true);
  651. Assert.Equal (Point.Empty, new Point (view.Frame.X, view.Frame.Y));
  652. Assert.Equal (new Size (2, 2), new Size (view.Frame.Width, view.Frame.Height));
  653. Application.Top.Add (view);
  654. Application.Begin (Application.Top);
  655. var expected = @"
  656. ┌┐
  657. └┘
  658. ";
  659. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  660. Assert.Equal (new Rect (0, 0, 2, 2), pos);
  661. }
  662. [Fact, AutoInitShutdown]
  663. public void DrawFrame_With_Negative_Positions ()
  664. {
  665. var view = new View (new Rect (-1, 0, 8, 4));
  666. view.DrawContent += (s, e) => view.DrawFrame (view.Bounds, 0, true);
  667. Assert.Equal (new Point (-1, 0), new Point (view.Frame.X, view.Frame.Y));
  668. Assert.Equal (new Size (8, 4), new Size (view.Frame.Width, view.Frame.Height));
  669. Application.Top.Add (view);
  670. Application.Begin (Application.Top);
  671. var expected = @"
  672. ──────┐
  673. ──────┘
  674. ";
  675. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  676. Assert.Equal (new Rect (0, 0, 7, 4), pos);
  677. view.Frame = new Rect (-1, -1, 8, 4);
  678. Application.Refresh ();
  679. expected = @"
  680. ──────┘
  681. ";
  682. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  683. Assert.Equal (new Rect (6, 0, 7, 3), pos);
  684. view.Frame = new Rect (0, 0, 8, 4);
  685. ((FakeDriver)Application.Driver).SetBufferSize (7, 4);
  686. expected = @"
  687. ┌──────
  688. └──────
  689. ";
  690. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  691. Assert.Equal (new Rect (0, 0, 7, 4), pos);
  692. view.Frame = new Rect (0, 0, 8, 4);
  693. ((FakeDriver)Application.Driver).SetBufferSize (7, 3);
  694. }
  695. [Fact, AutoInitShutdown]
  696. public void Clear_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  697. {
  698. var view = new View () {
  699. Width = Dim.Fill (),
  700. Height = Dim.Fill ()
  701. };
  702. view.DrawContent += (s, e) => {
  703. view.DrawFrame (view.Bounds);
  704. var savedClip = Application.Driver.Clip;
  705. Application.Driver.Clip = new Rect (1, 1, view.Bounds.Width - 2, view.Bounds.Height - 2);
  706. for (int row = 0; row < view.Bounds.Height - 2; row++) {
  707. Application.Driver.Move (1, row + 1);
  708. for (int col = 0; col < view.Bounds.Width - 2; col++) {
  709. Application.Driver.AddStr ($"{col}");
  710. }
  711. }
  712. Application.Driver.Clip = savedClip;
  713. };
  714. Application.Top.Add (view);
  715. Application.Begin (Application.Top);
  716. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  717. var expected = @"
  718. ┌──────────────────┐
  719. │012345678910111213│
  720. │012345678910111213│
  721. │012345678910111213│
  722. │012345678910111213│
  723. │012345678910111213│
  724. │012345678910111213│
  725. │012345678910111213│
  726. │012345678910111213│
  727. └──────────────────┘
  728. ";
  729. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  730. Assert.Equal (new Rect (0, 0, 20, 10), pos);
  731. view.Clear ();
  732. expected = @"
  733. ";
  734. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  735. Assert.Equal (Rect.Empty, pos);
  736. }
  737. [Fact, AutoInitShutdown]
  738. public void Clear_Bounds_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  739. {
  740. var view = new View () {
  741. Width = Dim.Fill (),
  742. Height = Dim.Fill ()
  743. };
  744. view.DrawContent += (s, e) => {
  745. view.DrawFrame (view.Bounds);
  746. var savedClip = Application.Driver.Clip;
  747. Application.Driver.Clip = new Rect (1, 1, view.Bounds.Width - 2, view.Bounds.Height - 2);
  748. for (int row = 0; row < view.Bounds.Height - 2; row++) {
  749. Application.Driver.Move (1, row + 1);
  750. for (int col = 0; col < view.Bounds.Width - 2; col++) {
  751. Application.Driver.AddStr ($"{col}");
  752. }
  753. }
  754. Application.Driver.Clip = savedClip;
  755. };
  756. Application.Top.Add (view);
  757. Application.Begin (Application.Top);
  758. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  759. var expected = @"
  760. ┌──────────────────┐
  761. │012345678910111213│
  762. │012345678910111213│
  763. │012345678910111213│
  764. │012345678910111213│
  765. │012345678910111213│
  766. │012345678910111213│
  767. │012345678910111213│
  768. │012345678910111213│
  769. └──────────────────┘
  770. ";
  771. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  772. Assert.Equal (new Rect (0, 0, 20, 10), pos);
  773. view.Clear (view.Bounds);
  774. expected = @"
  775. ";
  776. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  777. Assert.Equal (Rect.Empty, pos);
  778. }
  779. [Fact, AutoInitShutdown]
  780. public void GetTextFormatterBoundsSize_GetSizeNeededForText_HotKeySpecifier ()
  781. {
  782. var text = "Say Hello 你";
  783. var horizontalView = new View () {
  784. Text = text,
  785. AutoSize = true,
  786. HotKeySpecifier = '_' };
  787. var verticalView = new View () {
  788. Text = text,
  789. AutoSize = true,
  790. HotKeySpecifier = '_',
  791. TextDirection = TextDirection.TopBottom_LeftRight
  792. };
  793. Application.Top.Add (horizontalView, verticalView);
  794. Application.Begin (Application.Top);
  795. ((FakeDriver)Application.Driver).SetBufferSize (50, 50);
  796. Assert.True (horizontalView.AutoSize);
  797. Assert.Equal (new Rect (0, 0, 12, 1), horizontalView.Frame);
  798. Assert.Equal (new Size (12, 1), horizontalView.GetSizeNeededForTextWithoutHotKey ());
  799. Assert.Equal (new Size (12, 1), horizontalView.GetSizeNeededForTextAndHotKey ());
  800. Assert.Equal (horizontalView.TextFormatter.Size, horizontalView.GetSizeNeededForTextAndHotKey ());
  801. Assert.Equal (horizontalView.Frame.Size, horizontalView.GetSizeNeededForTextWithoutHotKey ());
  802. Assert.True (verticalView.AutoSize);
  803. // BUGBUG: v2 - Autosize is broken; disabling this test
  804. //Assert.Equal (new Rect (0, 0, 2, 11), verticalView.Frame);
  805. //Assert.Equal (new Size (2, 11), verticalView.GetSizeNeededForTextWithoutHotKey ());
  806. //Assert.Equal (new Size (2, 11), verticalView.GetSizeNeededForTextAndHotKey ());
  807. Assert.Equal (verticalView.TextFormatter.Size, verticalView.GetSizeNeededForTextAndHotKey ());
  808. Assert.Equal (verticalView.Frame.Size, verticalView.GetSizeNeededForTextWithoutHotKey ());
  809. text = "Say He_llo 你";
  810. horizontalView.Text = text;
  811. verticalView.Text = text;
  812. Assert.True (horizontalView.AutoSize);
  813. Assert.Equal (new Rect (0, 0, 12, 1), horizontalView.Frame);
  814. Assert.Equal (new Size (12, 1), horizontalView.GetSizeNeededForTextWithoutHotKey ());
  815. Assert.Equal (new Size (13, 1), horizontalView.GetSizeNeededForTextAndHotKey ());
  816. Assert.Equal (horizontalView.TextFormatter.Size, horizontalView.GetSizeNeededForTextAndHotKey ());
  817. Assert.Equal (horizontalView.Frame.Size, horizontalView.GetSizeNeededForTextWithoutHotKey ());
  818. Assert.True (verticalView.AutoSize);
  819. // BUGBUG: v2 - Autosize is broken; disabling this test
  820. //Assert.Equal (new Rect (0, 0, 2, 11), verticalView.Frame);
  821. //Assert.Equal (new Size (2, 11), verticalView.GetSizeNeededForTextWithoutHotKey ());
  822. //Assert.Equal (new Size (2, 12), verticalView.GetSizeNeededForTextAndHotKey ());
  823. //Assert.Equal (verticalView.TextFormatter.Size, verticalView.GetSizeNeededForTextAndHotKey ());
  824. //Assert.Equal (verticalView.Frame.Size, verticalView.GetSizeNeededForTextWithoutHotKey ());
  825. }
  826. [Fact]
  827. public void IsAdded_Added_Removed ()
  828. {
  829. var top = new Toplevel ();
  830. var view = new View ();
  831. Assert.False (view.IsAdded);
  832. top.Add (view);
  833. Assert.True (view.IsAdded);
  834. top.Remove (view);
  835. Assert.False (view.IsAdded);
  836. }
  837. [Fact, AutoInitShutdown]
  838. public void Visible_Clear_The_View_Output ()
  839. {
  840. var label = new Label ("Testing visibility.");
  841. var win = new Window ();
  842. win.Add (label);
  843. var top = Application.Top;
  844. top.Add (win);
  845. Application.Begin (top);
  846. Assert.True (label.Visible);
  847. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  848. TestHelpers.AssertDriverContentsWithFrameAre (@"
  849. ┌────────────────────────────┐
  850. │Testing visibility. │
  851. │ │
  852. │ │
  853. └────────────────────────────┘
  854. ", output);
  855. label.Visible = false;
  856. TestHelpers.AssertDriverContentsWithFrameAre (@"
  857. ┌────────────────────────────┐
  858. │ │
  859. │ │
  860. │ │
  861. └────────────────────────────┘
  862. ", output);
  863. }
  864. [Fact, AutoInitShutdown]
  865. public void DrawContentComplete_Event_Is_Always_Called ()
  866. {
  867. var viewCalled = false;
  868. var tvCalled = false;
  869. var view = new View ("View") { Width = 10, Height = 10 };
  870. view.DrawContentComplete += (s, e) => viewCalled = true;
  871. var tv = new TextView () { Y = 11, Width = 10, Height = 10 };
  872. tv.DrawContentComplete += (s, e) => tvCalled = true;
  873. Application.Top.Add (view, tv);
  874. Application.Begin (Application.Top);
  875. Assert.True (viewCalled);
  876. Assert.True (tvCalled);
  877. }
  878. [Fact, AutoInitShutdown]
  879. public void GetNormalColor_ColorScheme ()
  880. {
  881. var view = new View { ColorScheme = Colors.Base };
  882. Assert.Equal (view.ColorScheme.Normal, view.GetNormalColor ());
  883. view.Enabled = false;
  884. Assert.Equal (view.ColorScheme.Disabled, view.GetNormalColor ());
  885. }
  886. [Fact, AutoInitShutdown]
  887. public void GetHotNormalColor_ColorScheme ()
  888. {
  889. var view = new View { ColorScheme = Colors.Base };
  890. Assert.Equal (view.ColorScheme.HotNormal, view.GetHotNormalColor ());
  891. view.Enabled = false;
  892. Assert.Equal (view.ColorScheme.Disabled, view.GetHotNormalColor ());
  893. }
  894. [Theory, AutoInitShutdown]
  895. [InlineData (true)]
  896. [InlineData (false)]
  897. public void Clear_Does_Not_Spillover_Its_Parent (bool label)
  898. {
  899. var root = new View () { Width = 20, Height = 10, ColorScheme = Colors.Base };
  900. var v = label == true ?
  901. new Label (new string ('c', 100)) {
  902. Width = Dim.Fill (),
  903. } :
  904. (View)new TextView () {
  905. Height = 1,
  906. Text = new string ('c', 100),
  907. Width = Dim.Fill ()
  908. };
  909. root.Add (v);
  910. Application.Top.Add (root);
  911. Application.Begin (Application.Top);
  912. if (label) {
  913. Assert.True (v.AutoSize);
  914. Assert.False (v.CanFocus);
  915. Assert.Equal (new Rect (0, 0, 100, 1), v.Frame);
  916. } else {
  917. Assert.False (v.AutoSize);
  918. Assert.True (v.CanFocus);
  919. Assert.Equal (new Rect (0, 0, 20, 1), v.Frame);
  920. }
  921. TestHelpers.AssertDriverContentsWithFrameAre (@"
  922. cccccccccccccccccccc", output);
  923. var attributes = new Attribute [] {
  924. Colors.TopLevel.Normal,
  925. Colors.Base.Normal,
  926. Colors.Base.Focus
  927. };
  928. if (label) {
  929. TestHelpers.AssertDriverColorsAre (@"
  930. 111111111111111111110
  931. 111111111111111111110", attributes);
  932. } else {
  933. TestHelpers.AssertDriverColorsAre (@"
  934. 222222222222222222220
  935. 111111111111111111110", attributes);
  936. }
  937. if (label) {
  938. root.CanFocus = true;
  939. v.CanFocus = true;
  940. Assert.False (v.HasFocus);
  941. v.SetFocus ();
  942. Assert.True (v.HasFocus);
  943. Application.Refresh ();
  944. TestHelpers.AssertDriverColorsAre (@"
  945. 222222222222222222220
  946. 111111111111111111110", attributes);
  947. }
  948. }
  949. public class DerivedView : View {
  950. public DerivedView ()
  951. {
  952. CanFocus = true;
  953. }
  954. public bool IsKeyDown { get; set; }
  955. public bool IsKeyPress { get; set; }
  956. public bool IsKeyUp { get; set; }
  957. public override ustring Text { get; set; }
  958. public override bool OnKeyDown (KeyEvent keyEvent)
  959. {
  960. IsKeyDown = true;
  961. return true;
  962. }
  963. public override bool ProcessKey (KeyEvent keyEvent)
  964. {
  965. IsKeyPress = true;
  966. return true;
  967. }
  968. public override bool OnKeyUp (KeyEvent keyEvent)
  969. {
  970. IsKeyUp = true;
  971. return true;
  972. }
  973. public override void Redraw (Rect bounds)
  974. {
  975. var idx = 0;
  976. // BUGBUG: v2 - this should use Boudns, not Frame
  977. for (int r = 0; r < Frame.Height; r++) {
  978. for (int c = 0; c < Frame.Width; c++) {
  979. if (idx < Text.Length) {
  980. var rune = Text [idx];
  981. if (rune != '\n') {
  982. AddRune (c, r, Text [idx]);
  983. }
  984. idx++;
  985. if (rune == '\n') {
  986. break;
  987. }
  988. }
  989. }
  990. }
  991. ClearLayoutNeeded ();
  992. ClearNeedsDisplay ();
  993. }
  994. }
  995. [Fact, AutoInitShutdown]
  996. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  997. {
  998. var label = new Label ("At 0,0");
  999. var view = new DerivedView () {
  1000. X = 2,
  1001. Y = 2,
  1002. Width = 30,
  1003. Height = 2,
  1004. Text = "A text with some long width\n and also with two lines."
  1005. };
  1006. var top = Application.Top;
  1007. top.Add (label, view);
  1008. Application.Begin (top);
  1009. top.Redraw (top.Bounds);
  1010. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1011. At 0,0
  1012. A text with some long width
  1013. and also with two lines. ", output);
  1014. view.Frame = new Rect (1, 1, 10, 1);
  1015. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  1016. Assert.Equal (LayoutStyle.Computed, view.LayoutStyle);
  1017. view.LayoutStyle = LayoutStyle.Absolute;
  1018. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  1019. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplay);
  1020. top.Redraw (top.Bounds);
  1021. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1022. At 0,0
  1023. A text wit", output);
  1024. }
  1025. [Fact, AutoInitShutdown]
  1026. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  1027. {
  1028. var label = new Label ("At 0,0");
  1029. var view = new DerivedView () {
  1030. X = 2,
  1031. Y = 2,
  1032. Width = 30,
  1033. Height = 2,
  1034. Text = "A text with some long width\n and also with two lines."
  1035. };
  1036. var top = Application.Top;
  1037. top.Add (label, view);
  1038. Application.Begin (top);
  1039. top.Redraw (top.Bounds);
  1040. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1041. At 0,0
  1042. A text with some long width
  1043. and also with two lines. ", output);
  1044. view.X = 1;
  1045. view.Y = 1;
  1046. view.Width = 10;
  1047. view.Height = 1;
  1048. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  1049. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  1050. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplay);
  1051. top.Redraw (top.Bounds);
  1052. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1053. At 0,0
  1054. A text wit", output);
  1055. }
  1056. [Fact, AutoInitShutdown]
  1057. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_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. view.Redraw (view.Bounds);
  1071. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1072. At 0,0
  1073. A text with some long width
  1074. and also with two lines. ", output);
  1075. view.Frame = new Rect (1, 1, 10, 1);
  1076. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  1077. Assert.Equal (LayoutStyle.Computed, view.LayoutStyle);
  1078. view.LayoutStyle = LayoutStyle.Absolute;
  1079. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  1080. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplay);
  1081. view.Redraw (view.Bounds);
  1082. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1083. At 0,0
  1084. A text wit
  1085. A text with some long width
  1086. and also with two lines. ", output);
  1087. }
  1088. [Fact, AutoInitShutdown]
  1089. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  1090. {
  1091. var label = new Label ("At 0,0");
  1092. var view = new DerivedView () {
  1093. X = 2,
  1094. Y = 2,
  1095. Width = 30,
  1096. Height = 2,
  1097. Text = "A text with some long width\n and also with two lines."
  1098. };
  1099. var top = Application.Top;
  1100. top.Add (label, view);
  1101. Application.Begin (top);
  1102. view.Redraw (view.Bounds);
  1103. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1104. At 0,0
  1105. A text with some long width
  1106. and also with two lines. ", output);
  1107. view.X = 1;
  1108. view.Y = 1;
  1109. view.Width = 10;
  1110. view.Height = 1;
  1111. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  1112. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  1113. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplay);
  1114. view.Redraw (view.Bounds);
  1115. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1116. At 0,0
  1117. A text wit
  1118. A text with some long width
  1119. and also with two lines. ", output);
  1120. }
  1121. [Fact, AutoInitShutdown]
  1122. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  1123. {
  1124. var label = new Label ("At 0,0");
  1125. var view = new DerivedView () {
  1126. X = 2,
  1127. Y = 2,
  1128. Width = 30,
  1129. Height = 2,
  1130. Text = "A text with some long width\n and also with two lines."
  1131. };
  1132. var top = Application.Top;
  1133. top.Add (label, view);
  1134. Application.Begin (top);
  1135. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1136. At 0,0
  1137. A text with some long width
  1138. and also with two lines. ", output);
  1139. view.Frame = new Rect (3, 3, 10, 1);
  1140. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  1141. Assert.Equal (LayoutStyle.Computed, view.LayoutStyle);
  1142. view.LayoutStyle = LayoutStyle.Absolute;
  1143. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  1144. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplay);
  1145. top.Redraw (top.Bounds);
  1146. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1147. At 0,0
  1148. A text wit", output);
  1149. }
  1150. [Fact, AutoInitShutdown]
  1151. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  1152. {
  1153. var label = new Label ("At 0,0");
  1154. var view = new DerivedView () {
  1155. X = 2,
  1156. Y = 2,
  1157. Width = 30,
  1158. Height = 2,
  1159. Text = "A text with some long width\n and also with two lines."
  1160. };
  1161. var top = Application.Top;
  1162. top.Add (label, view);
  1163. Application.Begin (top);
  1164. top.Redraw (top.Bounds);
  1165. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1166. At 0,0
  1167. A text with some long width
  1168. and also with two lines. ", output);
  1169. view.X = 3;
  1170. view.Y = 3;
  1171. view.Width = 10;
  1172. view.Height = 1;
  1173. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  1174. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  1175. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplay);
  1176. top.Redraw (top.Bounds);
  1177. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1178. At 0,0
  1179. A text wit", output);
  1180. }
  1181. [Fact, AutoInitShutdown]
  1182. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  1183. {
  1184. var label = new Label ("At 0,0");
  1185. var view = new DerivedView () {
  1186. X = 2,
  1187. Y = 2,
  1188. Width = 30,
  1189. Height = 2,
  1190. Text = "A text with some long width\n and also with two lines."
  1191. };
  1192. var top = Application.Top;
  1193. top.Add (label, view);
  1194. Application.Begin (top);
  1195. view.Redraw (view.Bounds);
  1196. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1197. At 0,0
  1198. A text with some long width
  1199. and also with two lines. ", output);
  1200. view.Frame = new Rect (3, 3, 10, 1);
  1201. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  1202. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplay);
  1203. view.Redraw (view.Bounds);
  1204. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1205. At 0,0
  1206. A text with some long width
  1207. A text witith two lines. ", output);
  1208. }
  1209. [Fact, AutoInitShutdown]
  1210. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  1211. {
  1212. var label = new Label ("At 0,0");
  1213. var view = new DerivedView () {
  1214. X = 2,
  1215. Y = 2,
  1216. Width = 30,
  1217. Height = 2,
  1218. Text = "A text with some long width\n and also with two lines."
  1219. };
  1220. var top = Application.Top;
  1221. top.Add (label, view);
  1222. Application.Begin (top);
  1223. view.Redraw (view.Bounds);
  1224. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1225. At 0,0
  1226. A text with some long width
  1227. and also with two lines. ", output);
  1228. view.X = 3;
  1229. view.Y = 3;
  1230. view.Width = 10;
  1231. view.Height = 1;
  1232. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  1233. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  1234. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplay);
  1235. view.Redraw (view.Bounds);
  1236. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1237. At 0,0
  1238. A text with some long width
  1239. A text witith two lines. ", output);
  1240. }
  1241. [Fact, AutoInitShutdown]
  1242. public void Test_Nested_Views_With_Height_Equal_To_One ()
  1243. {
  1244. var v = new View () { Width = 11, Height = 3, ColorScheme = new ColorScheme () };
  1245. var top = new View () { Width = Dim.Fill (), Height = 1 };
  1246. var bottom = new View () { Width = Dim.Fill (), Height = 1, Y = 2 };
  1247. top.Add (new Label ("111"));
  1248. v.Add (top);
  1249. v.Add (new LineView (Orientation.Horizontal) { Y = 1 });
  1250. bottom.Add (new Label ("222"));
  1251. v.Add (bottom);
  1252. v.LayoutSubviews ();
  1253. v.Redraw (v.Bounds);
  1254. string looksLike =
  1255. @"
  1256. 111
  1257. ───────────
  1258. 222";
  1259. TestHelpers.AssertDriverContentsAre (looksLike, output);
  1260. }
  1261. [Fact]
  1262. [AutoInitShutdown]
  1263. public void Frame_Set_After_Initialize_Update_NeededDisplay ()
  1264. {
  1265. var frame = new FrameView ();
  1266. var label = new Label ("This should be the first line.") {
  1267. TextAlignment = Terminal.Gui.TextAlignment.Centered,
  1268. ColorScheme = Colors.Menu,
  1269. Width = Dim.Fill (),
  1270. X = Pos.Center (),
  1271. Y = Pos.Center () - 2 // center minus 2 minus two lines top and bottom borders equal to zero (4-2-2=0)
  1272. };
  1273. var button = new Button ("Press me!") {
  1274. X = Pos.Center (),
  1275. Y = Pos.Center ()
  1276. };
  1277. frame.Add (label, button);
  1278. frame.X = Pos.Center ();
  1279. frame.Y = Pos.Center ();
  1280. frame.Width = 40;
  1281. frame.Height = 8;
  1282. var top = Application.Top;
  1283. top.Add (frame);
  1284. Application.Begin (top);
  1285. top.LayoutComplete += (s, e) => {
  1286. Assert.Equal (new Rect (0, 0, 80, 25), top._needsDisplay);
  1287. };
  1288. frame.LayoutComplete += (s, e) => {
  1289. Assert.Equal (new Rect (0, 0, 40, 8), frame._needsDisplay);
  1290. };
  1291. label.LayoutComplete += (s, e) => {
  1292. Assert.Equal (new Rect (0, 0, 38, 1), label._needsDisplay);
  1293. };
  1294. button.LayoutComplete += (s, e) => {
  1295. Assert.Equal (new Rect (0, 0, 13, 1), button._needsDisplay);
  1296. };
  1297. Assert.True (label.AutoSize);
  1298. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  1299. Assert.Equal (new Rect (20, 8, 40, 8), frame.Frame);
  1300. Assert.Equal (new Rect (20, 8, 60, 16), new Rect (
  1301. frame.Frame.Left, frame.Frame.Top,
  1302. frame.Frame.Right, frame.Frame.Bottom));
  1303. Assert.Equal (new Rect (0, 0, 38, 1), label.Frame);
  1304. Assert.Equal (new Rect (12, 2, 13, 1), button.Frame);
  1305. var expected = @"
  1306. ┌──────────────────────────────────────┐
  1307. │ This should be the first line. │
  1308. │ │
  1309. │ [ Press me! ] │
  1310. │ │
  1311. │ │
  1312. │ │
  1313. └──────────────────────────────────────┘
  1314. ";
  1315. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1316. }
  1317. }
  1318. }