LayoutTests.cs 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Xml.Linq;
  5. using Terminal.Gui.Graphs;
  6. using Xunit;
  7. using Xunit.Abstractions;
  8. //using GraphViewTests = Terminal.Gui.Views.GraphViewTests;
  9. // Alias Console to MockConsole so we don't accidentally use Console
  10. using Console = Terminal.Gui.FakeConsole;
  11. namespace Terminal.Gui.CoreTests {
  12. public class LayoutTests {
  13. readonly ITestOutputHelper output;
  14. public LayoutTests (ITestOutputHelper output)
  15. {
  16. this.output = output;
  17. }
  18. [Fact]
  19. public void TopologicalSort_Missing_Add ()
  20. {
  21. var root = new View ();
  22. var sub1 = new View ();
  23. root.Add (sub1);
  24. var sub2 = new View ();
  25. sub1.Width = Dim.Width (sub2);
  26. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  27. sub2.Width = Dim.Width (sub1);
  28. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  29. }
  30. [Fact]
  31. public void TopologicalSort_Does_Never_Throws_If_Root_Is_Not_Null ()
  32. {
  33. var root = new View () { Id = "root", Width = 20, Height = 20 };
  34. var sub1 = new View () {
  35. Id = "sub1",
  36. X = Pos.Left (root) + 1,
  37. Y = Pos.Top (root) + 1,
  38. Width = Dim.Width (root) - 2,
  39. Height = Dim.Height (root) - 2
  40. };
  41. var sub2 = new View () {
  42. Id = "sub2",
  43. X = Pos.Left (root) + 1,
  44. Y = Pos.Top (root) + 1,
  45. Width = Dim.Width (root) - 2,
  46. Height = Dim.Height (root) - 2
  47. };
  48. var sub3 = new View () {
  49. Id = "sub3",
  50. X = Pos.Left (root) + 1,
  51. Y = Pos.Top (root) + 1,
  52. Width = Dim.Width (root) - 2,
  53. Height = Dim.Height (root) - 2
  54. };
  55. sub2.Add (sub3);
  56. sub1.Add (sub2);
  57. root.Add (sub1);
  58. var exception = Record.Exception (root.LayoutSubviews);
  59. Assert.Null (exception);
  60. Assert.Equal (new Rect (0, 0, 20, 20), root.Frame);
  61. Assert.Equal (new Rect (1, 1, 18, 18), sub1.Frame);
  62. Assert.Equal (new Rect (1, 1, 18, 18), sub2.Frame);
  63. Assert.Equal (new Rect (1, 1, 18, 18), sub3.Frame);
  64. sub2.Width = Dim.Width (root);
  65. exception = Record.Exception (root.LayoutSubviews);
  66. Assert.Null (exception);
  67. Assert.Equal (new Rect (1, 1, 20, 18), sub2.Frame);
  68. sub3.Width = Dim.Width (root);
  69. exception = Record.Exception (root.LayoutSubviews);
  70. Assert.Null (exception);
  71. Assert.Equal (new Rect (1, 1, 20, 18), sub3.Frame);
  72. }
  73. [Fact]
  74. public void TopologicalSort_Recursive_Ref ()
  75. {
  76. var root = new View ();
  77. var sub1 = new View ();
  78. root.Add (sub1);
  79. var sub2 = new View ();
  80. root.Add (sub2);
  81. sub2.Width = Dim.Width (sub2);
  82. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  83. }
  84. [Fact, AutoInitShutdown]
  85. public void SetWidth_CanSetWidth_ForceValidatePosDim ()
  86. {
  87. var top = new View () {
  88. X = 0,
  89. Y = 0,
  90. Width = 80,
  91. };
  92. var v = new View () {
  93. Width = Dim.Fill (),
  94. ForceValidatePosDim = true
  95. };
  96. top.Add (v);
  97. Assert.False (v.SetWidth (70, out int rWidth));
  98. Assert.Equal (70, rWidth);
  99. v.Width = Dim.Fill (1);
  100. Assert.False (v.SetWidth (70, out rWidth));
  101. Assert.Equal (69, rWidth);
  102. v.Width = null;
  103. Assert.True (v.SetWidth (70, out rWidth));
  104. Assert.Equal (70, rWidth);
  105. Assert.False (v.IsInitialized);
  106. Application.Top.Add (top);
  107. Application.Begin (Application.Top);
  108. Assert.True (v.IsInitialized);
  109. v.Width = Dim.Fill (1);
  110. Assert.Throws<ArgumentException> (() => v.Width = 75);
  111. v.LayoutStyle = LayoutStyle.Absolute;
  112. v.Width = 75;
  113. Assert.True (v.SetWidth (60, out rWidth));
  114. Assert.Equal (60, rWidth);
  115. }
  116. [Fact, AutoInitShutdown]
  117. public void SetHeight_CanSetHeight_ForceValidatePosDim ()
  118. {
  119. var top = new View () {
  120. X = 0,
  121. Y = 0,
  122. Height = 20
  123. };
  124. var v = new View () {
  125. Height = Dim.Fill (),
  126. ForceValidatePosDim = true
  127. };
  128. top.Add (v);
  129. Assert.False (v.SetHeight (10, out int rHeight));
  130. Assert.Equal (10, rHeight);
  131. v.Height = Dim.Fill (1);
  132. Assert.False (v.SetHeight (10, out rHeight));
  133. Assert.Equal (9, rHeight);
  134. v.Height = null;
  135. Assert.True (v.SetHeight (10, out rHeight));
  136. Assert.Equal (10, rHeight);
  137. Assert.False (v.IsInitialized);
  138. Application.Top.Add (top);
  139. Application.Begin (Application.Top);
  140. Assert.True (v.IsInitialized);
  141. v.Height = Dim.Fill (1);
  142. Assert.Throws<ArgumentException> (() => v.Height = 15);
  143. v.LayoutStyle = LayoutStyle.Absolute;
  144. v.Height = 15;
  145. Assert.True (v.SetHeight (5, out rHeight));
  146. Assert.Equal (5, rHeight);
  147. }
  148. [Fact]
  149. public void GetCurrentWidth_CanSetWidth ()
  150. {
  151. var top = new View () {
  152. X = 0,
  153. Y = 0,
  154. Width = 80,
  155. };
  156. var v = new View () {
  157. Width = Dim.Fill ()
  158. };
  159. top.Add (v);
  160. Assert.False (v.AutoSize);
  161. Assert.True (v.GetCurrentWidth (out int cWidth));
  162. Assert.Equal (80, cWidth);
  163. v.Width = Dim.Fill (1);
  164. Assert.True (v.GetCurrentWidth (out cWidth));
  165. Assert.Equal (79, cWidth);
  166. v.AutoSize = true;
  167. Assert.True (v.GetCurrentWidth (out cWidth));
  168. Assert.Equal (79, cWidth);
  169. }
  170. [Fact]
  171. public void GetCurrentHeight_CanSetHeight ()
  172. {
  173. var top = new View () {
  174. X = 0,
  175. Y = 0,
  176. Height = 20
  177. };
  178. var v = new View () {
  179. Height = Dim.Fill ()
  180. };
  181. top.Add (v);
  182. Assert.False (v.AutoSize);
  183. Assert.True (v.GetCurrentHeight (out int cHeight));
  184. Assert.Equal (20, cHeight);
  185. v.Height = Dim.Fill (1);
  186. Assert.True (v.GetCurrentHeight (out cHeight));
  187. Assert.Equal (19, cHeight);
  188. v.AutoSize = true;
  189. Assert.True (v.GetCurrentHeight (out cHeight));
  190. Assert.Equal (19, cHeight);
  191. }
  192. [Fact]
  193. public void AutoSize_False_If_Text_Emmpty ()
  194. {
  195. var view1 = new View ();
  196. var view2 = new View ("");
  197. var view3 = new View () { Text = "" };
  198. Assert.False (view1.AutoSize);
  199. Assert.False (view2.AutoSize);
  200. Assert.False (view3.AutoSize);
  201. }
  202. [Fact]
  203. public void AutoSize_False_If_Text_Is_Not_Emmpty ()
  204. {
  205. var view1 = new View ();
  206. view1.Text = "Hello World";
  207. var view2 = new View ("Hello World");
  208. var view3 = new View () { Text = "Hello World" };
  209. Assert.False (view1.AutoSize);
  210. Assert.False (view2.AutoSize);
  211. Assert.False (view3.AutoSize);
  212. }
  213. [Fact]
  214. public void AutoSize_True_Label_If_Text_Emmpty ()
  215. {
  216. var label1 = new Label ();
  217. var label2 = new Label ("");
  218. var label3 = new Label () { Text = "" };
  219. Assert.True (label1.AutoSize);
  220. Assert.True (label2.AutoSize);
  221. Assert.True (label3.AutoSize);
  222. }
  223. [Fact]
  224. public void AutoSize_True_Label_If_Text_Is_Not_Emmpty ()
  225. {
  226. var label1 = new Label ();
  227. label1.Text = "Hello World";
  228. var label2 = new Label ("Hello World");
  229. var label3 = new Label () { Text = "Hello World" };
  230. Assert.True (label1.AutoSize);
  231. Assert.True (label2.AutoSize);
  232. Assert.True (label3.AutoSize);
  233. }
  234. [Fact]
  235. public void AutoSize_False_ResizeView_Is_Always_False ()
  236. {
  237. var label = new Label () { AutoSize = false };
  238. label.Text = "New text";
  239. Assert.False (label.AutoSize);
  240. Assert.Equal ("{X=0,Y=0,Width=0,Height=1}", label.Bounds.ToString ());
  241. }
  242. [Fact]
  243. public void AutoSize_True_ResizeView_With_Dim_Absolute ()
  244. {
  245. var label = new Label ();
  246. label.Text = "New text";
  247. Assert.True (label.AutoSize);
  248. Assert.Equal ("{X=0,Y=0,Width=8,Height=1}", label.Bounds.ToString ());
  249. }
  250. [Fact, AutoInitShutdown]
  251. public void AutoSize_False_ResizeView_With_Dim_Fill_After_IsInitialized ()
  252. {
  253. var win = new Window (new Rect (0, 0, 30, 80), "");
  254. var label = new Label () { AutoSize = false, Width = Dim.Fill (), Height = Dim.Fill () };
  255. win.Add (label);
  256. Application.Top.Add (win);
  257. // Text is empty so height=0
  258. Assert.False (label.AutoSize);
  259. Assert.Equal ("{X=0,Y=0,Width=0,Height=0}", label.Bounds.ToString ());
  260. label.Text = "New text\nNew line";
  261. Application.Top.LayoutSubviews ();
  262. Assert.False (label.AutoSize);
  263. Assert.Equal ("{X=0,Y=0,Width=28,Height=78}", label.Bounds.ToString ());
  264. Assert.False (label.IsInitialized);
  265. Application.Begin (Application.Top);
  266. Assert.True (label.IsInitialized);
  267. Assert.False (label.AutoSize);
  268. Assert.Equal ("{X=0,Y=0,Width=28,Height=78}", label.Bounds.ToString ());
  269. }
  270. [Fact, AutoInitShutdown]
  271. public void AutoSize_False_SetWidthHeight_With_Dim_Fill_And_Dim_Absolute_After_IsAdded_And_IsInitialized ()
  272. {
  273. var win = new Window (new Rect (0, 0, 30, 80), "");
  274. var label = new Label () { Width = Dim.Fill () };
  275. win.Add (label);
  276. Application.Top.Add (win);
  277. Assert.True (label.IsAdded);
  278. // Text is empty so height=0
  279. Assert.True (label.AutoSize);
  280. Assert.Equal ("{X=0,Y=0,Width=0,Height=0}", label.Bounds.ToString ());
  281. label.Text = "First line\nSecond line";
  282. Application.Top.LayoutSubviews ();
  283. Assert.True (label.AutoSize);
  284. Assert.Equal ("{X=0,Y=0,Width=28,Height=2}", label.Bounds.ToString ());
  285. Assert.False (label.IsInitialized);
  286. Application.Begin (Application.Top);
  287. Assert.True (label.AutoSize);
  288. Assert.Equal ("{X=0,Y=0,Width=28,Height=2}", label.Bounds.ToString ());
  289. Assert.True (label.IsInitialized);
  290. label.AutoSize = false;
  291. Application.Refresh ();
  292. Assert.False (label.AutoSize);
  293. Assert.Equal ("{X=0,Y=0,Width=28,Height=1}", label.Bounds.ToString ());
  294. }
  295. [Fact, AutoInitShutdown]
  296. public void AutoSize_False_SetWidthHeight_With_Dim_Fill_And_Dim_Absolute_With_Initialization ()
  297. {
  298. var win = new Window (new Rect (0, 0, 30, 80), "");
  299. var label = new Label () { Width = Dim.Fill () };
  300. win.Add (label);
  301. Application.Top.Add (win);
  302. // Text is empty so height=0
  303. Assert.True (label.AutoSize);
  304. Assert.Equal ("{X=0,Y=0,Width=0,Height=0}", label.Bounds.ToString ());
  305. Application.Begin (Application.Top);
  306. Assert.True (label.AutoSize);
  307. Assert.Equal ("{X=0,Y=0,Width=28,Height=0}", label.Bounds.ToString ());
  308. label.Text = "First line\nSecond line";
  309. Application.Refresh ();
  310. // Here the AutoSize ensuring the right size
  311. Assert.True (label.AutoSize);
  312. Assert.Equal ("{X=0,Y=0,Width=28,Height=2}", label.Bounds.ToString ());
  313. label.AutoSize = false;
  314. Application.Refresh ();
  315. // Here the SetMinWidthHeight ensuring the minimum height
  316. Assert.False (label.AutoSize);
  317. Assert.Equal ("{X=0,Y=0,Width=28,Height=1}", label.Bounds.ToString ());
  318. label.Text = "First changed line\nSecond changed line\nNew line";
  319. Application.Refresh ();
  320. Assert.False (label.AutoSize);
  321. Assert.Equal ("{X=0,Y=0,Width=28,Height=1}", label.Bounds.ToString ());
  322. label.AutoSize = true;
  323. Application.Refresh ();
  324. Assert.True (label.AutoSize);
  325. Assert.Equal ("{X=0,Y=0,Width=28,Height=3}", label.Bounds.ToString ());
  326. }
  327. [Fact, AutoInitShutdown]
  328. public void AutoSize_True_Setting_With_Height_Horizontal ()
  329. {
  330. var label = new Label ("Hello") { Width = 10, Height = 2 };
  331. var viewX = new View ("X") { X = Pos.Right (label) };
  332. var viewY = new View ("Y") { Y = Pos.Bottom (label) };
  333. Application.Top.Add (label, viewX, viewY);
  334. Application.Begin (Application.Top);
  335. Assert.True (label.AutoSize);
  336. Assert.Equal (new Rect (0, 0, 10, 2), label.Frame);
  337. var expected = @"
  338. Hello X
  339. Y
  340. ";
  341. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  342. Assert.Equal (new Rect (0, 0, 11, 3), pos);
  343. label.AutoSize = false;
  344. Application.Refresh ();
  345. Assert.False (label.AutoSize);
  346. Assert.Equal (new Rect (0, 0, 10, 2), label.Frame);
  347. expected = @"
  348. Hello X
  349. Y
  350. ";
  351. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  352. Assert.Equal (new Rect (0, 0, 11, 3), pos);
  353. }
  354. [Fact, AutoInitShutdown]
  355. public void AutoSize_True_Setting_With_Height_Vertical ()
  356. {
  357. var label = new Label ("Hello") { Width = 2, Height = 10, TextDirection = TextDirection.TopBottom_LeftRight };
  358. var viewX = new View ("X") { X = Pos.Right (label) };
  359. var viewY = new View ("Y") { Y = Pos.Bottom (label) };
  360. Application.Top.Add (label, viewX, viewY);
  361. Application.Begin (Application.Top);
  362. Assert.True (label.AutoSize);
  363. Assert.Equal (new Rect (0, 0, 2, 10), label.Frame);
  364. var expected = @"
  365. H X
  366. e
  367. l
  368. l
  369. o
  370. Y
  371. ";
  372. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  373. Assert.Equal (new Rect (0, 0, 3, 11), pos);
  374. label.AutoSize = false;
  375. Application.Refresh ();
  376. Assert.False (label.AutoSize);
  377. Assert.Equal (new Rect (0, 0, 2, 10), label.Frame);
  378. expected = @"
  379. H X
  380. e
  381. l
  382. l
  383. o
  384. Y
  385. ";
  386. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  387. Assert.Equal (new Rect (0, 0, 3, 11), pos);
  388. }
  389. [Fact]
  390. [AutoInitShutdown]
  391. public void Excess_Text_Is_Erased_When_The_Width_Is_Reduced ()
  392. {
  393. var lbl = new Label ("123");
  394. Application.Top.Add (lbl);
  395. Application.Begin (Application.Top);
  396. Assert.True (lbl.AutoSize);
  397. Assert.Equal ("123 ", GetContents ());
  398. lbl.Text = "12";
  399. lbl.SuperView.Redraw (lbl.SuperView.NeedDisplay);
  400. Assert.Equal ("12 ", GetContents ());
  401. string GetContents ()
  402. {
  403. var text = "";
  404. for (int i = 0; i < 4; i++) {
  405. text += (char)Application.Driver.Contents [0, i, 0];
  406. }
  407. return text;
  408. }
  409. }
  410. [Fact, AutoInitShutdown]
  411. public void Width_Height_SetMinWidthHeight_Narrow_Wide_Runes ()
  412. {
  413. var text = $"First line{Environment.NewLine}Second line";
  414. var horizontalView = new View () {
  415. Width = 20,
  416. Text = text
  417. };
  418. var verticalView = new View () {
  419. Y = 3,
  420. Height = 20,
  421. Text = text,
  422. TextDirection = TextDirection.TopBottom_LeftRight
  423. };
  424. var win = new Window () {
  425. Width = Dim.Fill (),
  426. Height = Dim.Fill (),
  427. Text = "Window"
  428. };
  429. win.Add (horizontalView, verticalView);
  430. Application.Top.Add (win);
  431. Application.Begin (Application.Top);
  432. ((FakeDriver)Application.Driver).SetBufferSize (32, 32);
  433. Assert.False (horizontalView.AutoSize);
  434. Assert.False (verticalView.AutoSize);
  435. Assert.Equal (new Rect (0, 0, 20, 1), horizontalView.Frame);
  436. Assert.Equal (new Rect (0, 3, 1, 20), verticalView.Frame);
  437. var expected = @"
  438. ┌──────────────────────────────┐
  439. │First line Second li │
  440. │ │
  441. │ │
  442. │F │
  443. │i │
  444. │r │
  445. │s │
  446. │t │
  447. │ │
  448. │l │
  449. │i │
  450. │n │
  451. │e │
  452. │ │
  453. │S │
  454. │e │
  455. │c │
  456. │o │
  457. │n │
  458. │d │
  459. │ │
  460. │l │
  461. │i │
  462. │ │
  463. │ │
  464. │ │
  465. │ │
  466. │ │
  467. │ │
  468. │ │
  469. └──────────────────────────────┘
  470. ";
  471. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  472. Assert.Equal (new Rect (0, 0, 32, 32), pos);
  473. verticalView.Text = $"最初の行{Environment.NewLine}二行目";
  474. Application.Top.Redraw (Application.Top.Bounds);
  475. Assert.Equal (new Rect (0, 3, 2, 20), verticalView.Frame);
  476. expected = @"
  477. ┌──────────────────────────────┐
  478. │First line Second li │
  479. │ │
  480. │ │
  481. │最 │
  482. │初 │
  483. │の │
  484. │行 │
  485. │ │
  486. │二 │
  487. │行 │
  488. │目 │
  489. │ │
  490. │ │
  491. │ │
  492. │ │
  493. │ │
  494. │ │
  495. │ │
  496. │ │
  497. │ │
  498. │ │
  499. │ │
  500. │ │
  501. │ │
  502. │ │
  503. │ │
  504. │ │
  505. │ │
  506. │ │
  507. │ │
  508. └──────────────────────────────┘
  509. ";
  510. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  511. Assert.Equal (new Rect (0, 0, 32, 32), pos);
  512. }
  513. [Fact, AutoInitShutdown]
  514. public void TextDirection_Toggle ()
  515. {
  516. var view = new View ();
  517. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  518. win.Add (view);
  519. Application.Top.Add (win);
  520. Application.Begin (Application.Top);
  521. ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  522. Assert.False (view.AutoSize);
  523. Assert.Equal (TextDirection.LeftRight_TopBottom, view.TextDirection);
  524. Assert.Equal (Rect.Empty, view.Frame);
  525. Assert.Equal ("PosAbsolute(0)", view.X.ToString ());
  526. Assert.Equal ("PosAbsolute(0)", view.Y.ToString ());
  527. Assert.Equal ("DimAbsolute(0)", view.Width.ToString ());
  528. Assert.Equal ("DimAbsolute(0)", view.Height.ToString ());
  529. var expected = @"
  530. ┌────────────────────┐
  531. │ │
  532. │ │
  533. │ │
  534. │ │
  535. │ │
  536. │ │
  537. │ │
  538. │ │
  539. │ │
  540. │ │
  541. │ │
  542. │ │
  543. │ │
  544. │ │
  545. │ │
  546. │ │
  547. │ │
  548. │ │
  549. │ │
  550. │ │
  551. └────────────────────┘
  552. ";
  553. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  554. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  555. view.Text = "Hello World";
  556. view.Width = 11;
  557. Application.Refresh ();
  558. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  559. Assert.Equal ("PosAbsolute(0)", view.X.ToString ());
  560. Assert.Equal ("PosAbsolute(0)", view.Y.ToString ());
  561. Assert.Equal ("DimAbsolute(11)", view.Width.ToString ());
  562. Assert.Equal ("DimAbsolute(0)", view.Height.ToString ());
  563. expected = @"
  564. ┌────────────────────┐
  565. │Hello World │
  566. │ │
  567. │ │
  568. │ │
  569. │ │
  570. │ │
  571. │ │
  572. │ │
  573. │ │
  574. │ │
  575. │ │
  576. │ │
  577. │ │
  578. │ │
  579. │ │
  580. │ │
  581. │ │
  582. │ │
  583. │ │
  584. │ │
  585. └────────────────────┘
  586. ";
  587. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  588. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  589. view.AutoSize = true;
  590. view.Text = "Hello Worlds";
  591. Application.Refresh ();
  592. Assert.Equal (new Rect (0, 0, 12, 1), view.Frame);
  593. Assert.Equal ("PosAbsolute(0)", view.X.ToString ());
  594. Assert.Equal ("PosAbsolute(0)", view.Y.ToString ());
  595. Assert.Equal ("DimAbsolute(11)", view.Width.ToString ());
  596. Assert.Equal ("DimAbsolute(0)", view.Height.ToString ());
  597. expected = @"
  598. ┌────────────────────┐
  599. │Hello Worlds │
  600. │ │
  601. │ │
  602. │ │
  603. │ │
  604. │ │
  605. │ │
  606. │ │
  607. │ │
  608. │ │
  609. │ │
  610. │ │
  611. │ │
  612. │ │
  613. │ │
  614. │ │
  615. │ │
  616. │ │
  617. │ │
  618. │ │
  619. └────────────────────┘
  620. ";
  621. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  622. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  623. view.TextDirection = TextDirection.TopBottom_LeftRight;
  624. Application.Refresh ();
  625. Assert.Equal (new Rect (0, 0, 11, 12), view.Frame);
  626. Assert.Equal ("PosAbsolute(0)", view.X.ToString ());
  627. Assert.Equal ("PosAbsolute(0)", view.Y.ToString ());
  628. Assert.Equal ("DimAbsolute(11)", view.Width.ToString ());
  629. Assert.Equal ("DimAbsolute(0)", view.Height.ToString ());
  630. expected = @"
  631. ┌────────────────────┐
  632. │H │
  633. │e │
  634. │l │
  635. │l │
  636. │o │
  637. │ │
  638. │W │
  639. │o │
  640. │r │
  641. │l │
  642. │d │
  643. │s │
  644. │ │
  645. │ │
  646. │ │
  647. │ │
  648. │ │
  649. │ │
  650. │ │
  651. │ │
  652. └────────────────────┘
  653. ";
  654. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  655. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  656. view.AutoSize = false;
  657. view.Height = 1;
  658. Application.Refresh ();
  659. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  660. Assert.Equal ("PosAbsolute(0)", view.X.ToString ());
  661. Assert.Equal ("PosAbsolute(0)", view.Y.ToString ());
  662. Assert.Equal ("DimAbsolute(11)", view.Width.ToString ());
  663. Assert.Equal ("DimAbsolute(1)", view.Height.ToString ());
  664. expected = @"
  665. ┌────────────────────┐
  666. │HelloWorlds │
  667. │ │
  668. │ │
  669. │ │
  670. │ │
  671. │ │
  672. │ │
  673. │ │
  674. │ │
  675. │ │
  676. │ │
  677. │ │
  678. │ │
  679. │ │
  680. │ │
  681. │ │
  682. │ │
  683. │ │
  684. │ │
  685. │ │
  686. └────────────────────┘
  687. ";
  688. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  689. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  690. view.PreserveTrailingSpaces = true;
  691. Application.Refresh ();
  692. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  693. Assert.Equal ("PosAbsolute(0)", view.X.ToString ());
  694. Assert.Equal ("PosAbsolute(0)", view.Y.ToString ());
  695. Assert.Equal ("DimAbsolute(11)", view.Width.ToString ());
  696. Assert.Equal ("DimAbsolute(1)", view.Height.ToString ());
  697. expected = @"
  698. ┌────────────────────┐
  699. │Hello World │
  700. │ │
  701. │ │
  702. │ │
  703. │ │
  704. │ │
  705. │ │
  706. │ │
  707. │ │
  708. │ │
  709. │ │
  710. │ │
  711. │ │
  712. │ │
  713. │ │
  714. │ │
  715. │ │
  716. │ │
  717. │ │
  718. │ │
  719. └────────────────────┘
  720. ";
  721. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  722. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  723. view.PreserveTrailingSpaces = false;
  724. var f = view.Frame;
  725. view.Width = f.Height;
  726. view.Height = f.Width;
  727. view.TextDirection = TextDirection.TopBottom_LeftRight;
  728. Application.Refresh ();
  729. Assert.Equal (new Rect (0, 0, 1, 11), view.Frame);
  730. Assert.Equal ("PosAbsolute(0)", view.X.ToString ());
  731. Assert.Equal ("PosAbsolute(0)", view.Y.ToString ());
  732. Assert.Equal ("DimAbsolute(1)", view.Width.ToString ());
  733. Assert.Equal ("DimAbsolute(11)", view.Height.ToString ());
  734. expected = @"
  735. ┌────────────────────┐
  736. │H │
  737. │e │
  738. │l │
  739. │l │
  740. │o │
  741. │ │
  742. │W │
  743. │o │
  744. │r │
  745. │l │
  746. │d │
  747. │ │
  748. │ │
  749. │ │
  750. │ │
  751. │ │
  752. │ │
  753. │ │
  754. │ │
  755. │ │
  756. └────────────────────┘
  757. ";
  758. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  759. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  760. view.AutoSize = true;
  761. Application.Refresh ();
  762. Assert.Equal (new Rect (0, 0, 1, 12), view.Frame);
  763. Assert.Equal ("PosAbsolute(0)", view.X.ToString ());
  764. Assert.Equal ("PosAbsolute(0)", view.Y.ToString ());
  765. Assert.Equal ("DimAbsolute(1)", view.Width.ToString ());
  766. Assert.Equal ("DimAbsolute(11)", view.Height.ToString ());
  767. expected = @"
  768. ┌────────────────────┐
  769. │H │
  770. │e │
  771. │l │
  772. │l │
  773. │o │
  774. │ │
  775. │W │
  776. │o │
  777. │r │
  778. │l │
  779. │d │
  780. │s │
  781. │ │
  782. │ │
  783. │ │
  784. │ │
  785. │ │
  786. │ │
  787. │ │
  788. │ │
  789. └────────────────────┘
  790. ";
  791. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  792. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  793. }
  794. [Fact, AutoInitShutdown]
  795. public void Width_Height_AutoSize_True_Stay_True_If_TextFormatter_Size_Fit ()
  796. {
  797. var text = $"Fi_nish 終";
  798. var horizontalView = new View () {
  799. AutoSize = true,
  800. HotKeySpecifier = '_',
  801. Text = text
  802. };
  803. var verticalView = new View () {
  804. Y = 3,
  805. AutoSize = true,
  806. HotKeySpecifier = '_',
  807. Text = text,
  808. TextDirection = TextDirection.TopBottom_LeftRight
  809. };
  810. var win = new Window () {
  811. Width = Dim.Fill (),
  812. Height = Dim.Fill (),
  813. Text = "Window"
  814. };
  815. win.Add (horizontalView, verticalView);
  816. Application.Top.Add (win);
  817. Application.Begin (Application.Top);
  818. ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  819. Assert.True (horizontalView.AutoSize);
  820. Assert.True (verticalView.AutoSize);
  821. Assert.Equal (new Size (10, 1), horizontalView.TextFormatter.Size);
  822. Assert.Equal (new Size (2, 9), verticalView.TextFormatter.Size);
  823. Assert.Equal (new Rect (0, 0, 9, 1), horizontalView.Frame);
  824. Assert.Equal ("PosAbsolute(0)", horizontalView.X.ToString ());
  825. Assert.Equal ("PosAbsolute(0)", horizontalView.Y.ToString ());
  826. Assert.Equal ("DimAbsolute(9)", horizontalView.Width.ToString ());
  827. Assert.Equal ("DimAbsolute(1)", horizontalView.Height.ToString ());
  828. Assert.Equal (new Rect (0, 3, 2, 8), verticalView.Frame);
  829. Assert.Equal ("PosAbsolute(0)", verticalView.X.ToString ());
  830. Assert.Equal ("PosAbsolute(3)", verticalView.Y.ToString ());
  831. Assert.Equal ("DimAbsolute(2)", verticalView.Width.ToString ());
  832. Assert.Equal ("DimAbsolute(8)", verticalView.Height.ToString ());
  833. var expected = @"
  834. ┌────────────────────┐
  835. │Finish 終 │
  836. │ │
  837. │ │
  838. │F │
  839. │i │
  840. │n │
  841. │i │
  842. │s │
  843. │h │
  844. │ │
  845. │終 │
  846. │ │
  847. │ │
  848. │ │
  849. │ │
  850. │ │
  851. │ │
  852. │ │
  853. │ │
  854. │ │
  855. └────────────────────┘
  856. ";
  857. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  858. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  859. verticalView.Text = $"最初_の行二行目";
  860. Application.Top.Redraw (Application.Top.Bounds);
  861. Assert.True (horizontalView.AutoSize);
  862. Assert.True (verticalView.AutoSize);
  863. // height was initialized with 8 and is kept as minimum
  864. Assert.Equal (new Rect (0, 3, 2, 8), verticalView.Frame);
  865. Assert.Equal ("PosAbsolute(0)", verticalView.X.ToString ());
  866. Assert.Equal ("PosAbsolute(3)", verticalView.Y.ToString ());
  867. Assert.Equal ("DimAbsolute(2)", verticalView.Width.ToString ());
  868. Assert.Equal ("DimAbsolute(8)", verticalView.Height.ToString ());
  869. expected = @"
  870. ┌────────────────────┐
  871. │Finish 終 │
  872. │ │
  873. │ │
  874. │最 │
  875. │初 │
  876. │の │
  877. │行 │
  878. │二 │
  879. │行 │
  880. │目 │
  881. │ │
  882. │ │
  883. │ │
  884. │ │
  885. │ │
  886. │ │
  887. │ │
  888. │ │
  889. │ │
  890. │ │
  891. └────────────────────┘
  892. ";
  893. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  894. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  895. }
  896. [Fact, AutoInitShutdown]
  897. public void AutoSize_Stays_True_Center_HotKeySpecifier ()
  898. {
  899. var btn = new Button () {
  900. X = Pos.Center (),
  901. Y = Pos.Center (),
  902. Text = "Say He_llo 你"
  903. };
  904. var win = new Window () {
  905. Width = Dim.Fill (),
  906. Height = Dim.Fill (),
  907. Title = "Test Demo 你"
  908. };
  909. win.Add (btn);
  910. Application.Top.Add (win);
  911. Assert.True (btn.AutoSize);
  912. Application.Begin (Application.Top);
  913. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  914. var expected = @"
  915. ┌ Test Demo 你 ──────────────┐
  916. │ │
  917. │ [ Say Hello 你 ] │
  918. │ │
  919. └────────────────────────────┘
  920. ";
  921. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  922. Assert.True (btn.AutoSize);
  923. btn.Text = "Say He_llo 你 changed";
  924. Assert.True (btn.AutoSize);
  925. Application.Refresh ();
  926. expected = @"
  927. ┌ Test Demo 你 ──────────────┐
  928. │ │
  929. │ [ Say Hello 你 changed ] │
  930. │ │
  931. └────────────────────────────┘
  932. ";
  933. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  934. }
  935. [Fact, AutoInitShutdown]
  936. public void AutoSize_False_Equal_Before_And_After_IsInitialized_With_Differents_Orders ()
  937. {
  938. var view1 = new View () { Text = "Say Hello view1 你", AutoSize = false, Width = 10, Height = 5 };
  939. var view2 = new View () { Text = "Say Hello view2 你", Width = 10, Height = 5, AutoSize = false };
  940. var view3 = new View () { AutoSize = false, Width = 10, Height = 5, Text = "Say Hello view3 你" };
  941. var view4 = new View () {
  942. Text = "Say Hello view4 你",
  943. AutoSize = false,
  944. Width = 10,
  945. Height = 5,
  946. TextDirection = TextDirection.TopBottom_LeftRight
  947. };
  948. var view5 = new View () {
  949. Text = "Say Hello view5 你",
  950. Width = 10,
  951. Height = 5,
  952. AutoSize = false,
  953. TextDirection = TextDirection.TopBottom_LeftRight
  954. };
  955. var view6 = new View () {
  956. AutoSize = false,
  957. Width = 10,
  958. Height = 5,
  959. TextDirection = TextDirection.TopBottom_LeftRight,
  960. Text = "Say Hello view6 你",
  961. };
  962. Application.Top.Add (view1, view2, view3, view4, view5, view6);
  963. Assert.False (view1.IsInitialized);
  964. Assert.False (view2.IsInitialized);
  965. Assert.False (view3.IsInitialized);
  966. Assert.False (view4.IsInitialized);
  967. Assert.False (view5.IsInitialized);
  968. Assert.False (view1.AutoSize);
  969. Assert.Equal (new Rect (0, 0, 10, 5), view1.Frame);
  970. Assert.Equal ("DimAbsolute(10)", view1.Width.ToString ());
  971. Assert.Equal ("DimAbsolute(5)", view1.Height.ToString ());
  972. Assert.False (view2.AutoSize);
  973. Assert.Equal (new Rect (0, 0, 10, 5), view2.Frame);
  974. Assert.Equal ("DimAbsolute(10)", view2.Width.ToString ());
  975. Assert.Equal ("DimAbsolute(5)", view2.Height.ToString ());
  976. Assert.False (view3.AutoSize);
  977. Assert.Equal (new Rect (0, 0, 10, 5), view3.Frame);
  978. Assert.Equal ("DimAbsolute(10)", view3.Width.ToString ());
  979. Assert.Equal ("DimAbsolute(5)", view3.Height.ToString ());
  980. Assert.False (view4.AutoSize);
  981. Assert.Equal (new Rect (0, 0, 10, 5), view4.Frame);
  982. Assert.Equal ("DimAbsolute(10)", view4.Width.ToString ());
  983. Assert.Equal ("DimAbsolute(5)", view4.Height.ToString ());
  984. Assert.False (view5.AutoSize);
  985. Assert.Equal (new Rect (0, 0, 10, 5), view5.Frame);
  986. Assert.Equal ("DimAbsolute(10)", view5.Width.ToString ());
  987. Assert.Equal ("DimAbsolute(5)", view5.Height.ToString ());
  988. Assert.False (view6.AutoSize);
  989. Assert.Equal (new Rect (0, 0, 10, 5), view6.Frame);
  990. Assert.Equal ("DimAbsolute(10)", view6.Width.ToString ());
  991. Assert.Equal ("DimAbsolute(5)", view6.Height.ToString ());
  992. Application.Begin (Application.Top);
  993. Assert.True (view1.IsInitialized);
  994. Assert.True (view2.IsInitialized);
  995. Assert.True (view3.IsInitialized);
  996. Assert.True (view4.IsInitialized);
  997. Assert.True (view5.IsInitialized);
  998. Assert.False (view1.AutoSize);
  999. Assert.Equal (new Rect (0, 0, 10, 5), view1.Frame);
  1000. Assert.Equal ("DimAbsolute(10)", view1.Width.ToString ());
  1001. Assert.Equal ("DimAbsolute(5)", view1.Height.ToString ());
  1002. Assert.False (view2.AutoSize);
  1003. Assert.Equal (new Rect (0, 0, 10, 5), view2.Frame);
  1004. Assert.Equal ("DimAbsolute(10)", view2.Width.ToString ());
  1005. Assert.Equal ("DimAbsolute(5)", view2.Height.ToString ());
  1006. Assert.False (view3.AutoSize);
  1007. Assert.Equal (new Rect (0, 0, 10, 5), view3.Frame);
  1008. Assert.Equal ("DimAbsolute(10)", view3.Width.ToString ());
  1009. Assert.Equal ("DimAbsolute(5)", view3.Height.ToString ());
  1010. Assert.False (view4.AutoSize);
  1011. Assert.Equal (new Rect (0, 0, 10, 5), view4.Frame);
  1012. Assert.Equal ("DimAbsolute(10)", view4.Width.ToString ());
  1013. Assert.Equal ("DimAbsolute(5)", view4.Height.ToString ());
  1014. Assert.False (view5.AutoSize);
  1015. Assert.Equal (new Rect (0, 0, 10, 5), view5.Frame);
  1016. Assert.Equal ("DimAbsolute(10)", view5.Width.ToString ());
  1017. Assert.Equal ("DimAbsolute(5)", view5.Height.ToString ());
  1018. Assert.False (view6.AutoSize);
  1019. Assert.Equal (new Rect (0, 0, 10, 5), view6.Frame);
  1020. Assert.Equal ("DimAbsolute(10)", view6.Width.ToString ());
  1021. Assert.Equal ("DimAbsolute(5)", view6.Height.ToString ());
  1022. }
  1023. [Fact, AutoInitShutdown]
  1024. public void AutoSize_True_Equal_Before_And_After_IsInitialized_With_Differents_Orders ()
  1025. {
  1026. var view1 = new View () { Text = "Say Hello view1 你", AutoSize = true, Width = 10, Height = 5 };
  1027. var view2 = new View () { Text = "Say Hello view2 你", Width = 10, Height = 5, AutoSize = true };
  1028. var view3 = new View () { AutoSize = true, Width = 10, Height = 5, Text = "Say Hello view3 你" };
  1029. var view4 = new View () {
  1030. Text = "Say Hello view4 你",
  1031. AutoSize = true,
  1032. Width = 10,
  1033. Height = 5,
  1034. TextDirection = TextDirection.TopBottom_LeftRight
  1035. };
  1036. var view5 = new View () {
  1037. Text = "Say Hello view5 你",
  1038. Width = 10,
  1039. Height = 5,
  1040. AutoSize = true,
  1041. TextDirection = TextDirection.TopBottom_LeftRight
  1042. };
  1043. var view6 = new View () {
  1044. AutoSize = true,
  1045. Width = 10,
  1046. Height = 5,
  1047. TextDirection = TextDirection.TopBottom_LeftRight,
  1048. Text = "Say Hello view6 你",
  1049. };
  1050. Application.Top.Add (view1, view2, view3, view4, view5, view6);
  1051. Assert.False (view1.IsInitialized);
  1052. Assert.False (view2.IsInitialized);
  1053. Assert.False (view3.IsInitialized);
  1054. Assert.False (view4.IsInitialized);
  1055. Assert.False (view5.IsInitialized);
  1056. Assert.True (view1.AutoSize);
  1057. Assert.Equal (new Rect (0, 0, 18, 5), view1.Frame);
  1058. Assert.Equal ("DimAbsolute(10)", view1.Width.ToString ());
  1059. Assert.Equal ("DimAbsolute(5)", view1.Height.ToString ());
  1060. Assert.True (view2.AutoSize);
  1061. Assert.Equal (new Rect (0, 0, 18, 5), view2.Frame);
  1062. Assert.Equal ("DimAbsolute(10)", view2.Width.ToString ());
  1063. Assert.Equal ("DimAbsolute(5)", view2.Height.ToString ());
  1064. Assert.True (view3.AutoSize);
  1065. Assert.Equal (new Rect (0, 0, 18, 5), view3.Frame);
  1066. Assert.Equal ("DimAbsolute(10)", view3.Width.ToString ());
  1067. Assert.Equal ("DimAbsolute(5)", view3.Height.ToString ());
  1068. Assert.True (view4.AutoSize);
  1069. Assert.Equal (new Rect (0, 0, 10, 17), view4.Frame);
  1070. Assert.Equal ("DimAbsolute(10)", view4.Width.ToString ());
  1071. Assert.Equal ("DimAbsolute(5)", view4.Height.ToString ());
  1072. Assert.True (view5.AutoSize);
  1073. Assert.Equal (new Rect (0, 0, 10, 17), view5.Frame);
  1074. Assert.Equal ("DimAbsolute(10)", view5.Width.ToString ());
  1075. Assert.Equal ("DimAbsolute(5)", view5.Height.ToString ());
  1076. Assert.True (view6.AutoSize);
  1077. Assert.Equal (new Rect (0, 0, 10, 17), view6.Frame);
  1078. Assert.Equal ("DimAbsolute(10)", view6.Width.ToString ());
  1079. Assert.Equal ("DimAbsolute(5)", view6.Height.ToString ());
  1080. Application.Begin (Application.Top);
  1081. Assert.True (view1.IsInitialized);
  1082. Assert.True (view2.IsInitialized);
  1083. Assert.True (view3.IsInitialized);
  1084. Assert.True (view4.IsInitialized);
  1085. Assert.True (view5.IsInitialized);
  1086. Assert.True (view1.AutoSize);
  1087. Assert.Equal (new Rect (0, 0, 18, 5), view1.Frame);
  1088. Assert.Equal ("DimAbsolute(10)", view1.Width.ToString ());
  1089. Assert.Equal ("DimAbsolute(5)", view1.Height.ToString ());
  1090. Assert.True (view2.AutoSize);
  1091. Assert.Equal (new Rect (0, 0, 18, 5), view2.Frame);
  1092. Assert.Equal ("DimAbsolute(10)", view2.Width.ToString ());
  1093. Assert.Equal ("DimAbsolute(5)", view2.Height.ToString ());
  1094. Assert.True (view3.AutoSize);
  1095. Assert.Equal (new Rect (0, 0, 18, 5), view3.Frame);
  1096. Assert.Equal ("DimAbsolute(10)", view3.Width.ToString ());
  1097. Assert.Equal ("DimAbsolute(5)", view3.Height.ToString ());
  1098. Assert.True (view4.AutoSize);
  1099. Assert.Equal (new Rect (0, 0, 10, 17), view4.Frame);
  1100. Assert.Equal ("DimAbsolute(10)", view4.Width.ToString ());
  1101. Assert.Equal ("DimAbsolute(5)", view4.Height.ToString ());
  1102. Assert.True (view5.AutoSize);
  1103. Assert.Equal (new Rect (0, 0, 10, 17), view5.Frame);
  1104. Assert.Equal ("DimAbsolute(10)", view5.Width.ToString ());
  1105. Assert.Equal ("DimAbsolute(5)", view5.Height.ToString ());
  1106. Assert.True (view6.AutoSize);
  1107. Assert.Equal (new Rect (0, 0, 10, 17), view6.Frame);
  1108. Assert.Equal ("DimAbsolute(10)", view6.Width.ToString ());
  1109. Assert.Equal ("DimAbsolute(5)", view6.Height.ToString ());
  1110. }
  1111. [Fact, AutoInitShutdown]
  1112. public void Setting_Frame_Dont_Respect_AutoSize_True_On_Layout_Absolute ()
  1113. {
  1114. var view1 = new View (new Rect (0, 0, 10, 0)) { Text = "Say Hello view1 你", AutoSize = true };
  1115. var view2 = new View (new Rect (0, 0, 0, 10)) {
  1116. Text = "Say Hello view2 你",
  1117. AutoSize = true,
  1118. TextDirection = TextDirection.TopBottom_LeftRight
  1119. };
  1120. Application.Top.Add (view1, view2);
  1121. var rs = Application.Begin (Application.Top);
  1122. Assert.True (view1.AutoSize);
  1123. Assert.Equal (LayoutStyle.Absolute, view1.LayoutStyle);
  1124. Assert.Equal (new Rect (0, 0, 18, 1), view1.Frame);
  1125. Assert.Equal ("PosAbsolute(0)", view1.X.ToString ());
  1126. Assert.Equal ("PosAbsolute(0)", view1.Y.ToString ());
  1127. Assert.Equal ("DimAbsolute(18)", view1.Width.ToString ());
  1128. Assert.Equal ("DimAbsolute(1)", view1.Height.ToString ());
  1129. Assert.True (view2.AutoSize);
  1130. Assert.Equal (LayoutStyle.Absolute, view2.LayoutStyle);
  1131. Assert.Equal (new Rect (0, 0, 2, 17), view2.Frame);
  1132. Assert.Equal ("PosAbsolute(0)", view2.X.ToString ());
  1133. Assert.Equal ("PosAbsolute(0)", view2.Y.ToString ());
  1134. Assert.Equal ("DimAbsolute(2)", view2.Width.ToString ());
  1135. Assert.Equal ("DimAbsolute(17)", view2.Height.ToString ());
  1136. view1.Frame = new Rect (0, 0, 25, 4);
  1137. bool firstIteration = false;
  1138. Application.RunMainLoopIteration (ref rs, true, ref firstIteration);
  1139. Assert.True (view1.AutoSize);
  1140. Assert.Equal (LayoutStyle.Absolute, view1.LayoutStyle);
  1141. Assert.Equal (new Rect (0, 0, 25, 4), view1.Frame);
  1142. Assert.Equal ("PosAbsolute(0)", view1.X.ToString ());
  1143. Assert.Equal ("PosAbsolute(0)", view1.Y.ToString ());
  1144. Assert.Equal ("DimAbsolute(18)", view1.Width.ToString ());
  1145. Assert.Equal ("DimAbsolute(1)", view1.Height.ToString ());
  1146. view2.Frame = new Rect (0, 0, 1, 25);
  1147. Application.RunMainLoopIteration (ref rs, true, ref firstIteration);
  1148. Assert.True (view2.AutoSize);
  1149. Assert.Equal (LayoutStyle.Absolute, view2.LayoutStyle);
  1150. Assert.Equal (new Rect (0, 0, 1, 25), view2.Frame);
  1151. Assert.Equal ("PosAbsolute(0)", view2.X.ToString ());
  1152. Assert.Equal ("PosAbsolute(0)", view2.Y.ToString ());
  1153. Assert.Equal ("DimAbsolute(2)", view2.Width.ToString ());
  1154. Assert.Equal ("DimAbsolute(17)", view2.Height.ToString ());
  1155. }
  1156. [Fact, AutoInitShutdown]
  1157. public void Pos_Dim_Are_Null_If_Not_Initialized_On_Constructor_IsAdded_False ()
  1158. {
  1159. var top = Application.Top;
  1160. var view1 = new View ();
  1161. Assert.False (view1.IsAdded);
  1162. Assert.Null (view1.X);
  1163. Assert.Null (view1.Y);
  1164. Assert.Null (view1.Width);
  1165. Assert.Null (view1.Height);
  1166. top.Add (view1);
  1167. Assert.True (view1.IsAdded);
  1168. Assert.Equal ("PosAbsolute(0)", view1.X.ToString ());
  1169. Assert.Equal ("PosAbsolute(0)", view1.Y.ToString ());
  1170. Assert.Equal ("DimAbsolute(0)", view1.Width.ToString ());
  1171. Assert.Equal ("DimAbsolute(0)", view1.Height.ToString ());
  1172. var view2 = new View () {
  1173. X = Pos.Center (),
  1174. Y = Pos.Center (),
  1175. Width = Dim.Fill (),
  1176. Height = Dim.Fill ()
  1177. };
  1178. Assert.False (view2.IsAdded);
  1179. Assert.Equal ("PosCenter", view2.X.ToString ());
  1180. Assert.Equal ("PosCenter", view2.Y.ToString ());
  1181. Assert.Equal ("DimFill(0)", view2.Width.ToString ());
  1182. Assert.Equal ("DimFill(0)", view2.Height.ToString ());
  1183. top.Add (view2);
  1184. Assert.True (view2.IsAdded);
  1185. Assert.Equal ("PosCenter", view2.X.ToString ());
  1186. Assert.Equal ("PosCenter", view2.Y.ToString ());
  1187. Assert.Equal ("DimFill(0)", view2.Width.ToString ());
  1188. Assert.Equal ("DimFill(0)", view2.Height.ToString ());
  1189. }
  1190. [Fact]
  1191. public void SetRelativeLayout_PosCombine_Center_Plus_Absolute ()
  1192. {
  1193. var superView = new View () {
  1194. AutoSize = false,
  1195. Width = 10,
  1196. Height = 10
  1197. };
  1198. var testView = new View () {
  1199. AutoSize = false,
  1200. X = Pos.Center (),
  1201. Y = Pos.Center (),
  1202. Width = 1,
  1203. Height = 1
  1204. };
  1205. superView.Add (testView);
  1206. testView.SetRelativeLayout (superView.Frame);
  1207. Assert.Equal (4, testView.Frame.X);
  1208. Assert.Equal (4, testView.Frame.Y);
  1209. testView = new View () {
  1210. AutoSize = false,
  1211. X = Pos.Center () + 1,
  1212. Y = Pos.Center () + 1,
  1213. Width = 1,
  1214. Height = 1
  1215. };
  1216. superView.Add (testView);
  1217. testView.SetRelativeLayout (superView.Frame);
  1218. Assert.Equal (5, testView.Frame.X);
  1219. Assert.Equal (5, testView.Frame.Y);
  1220. testView = new View () {
  1221. AutoSize = false,
  1222. X = 1 + Pos.Center (),
  1223. Y = 1 + Pos.Center (),
  1224. Width = 1,
  1225. Height = 1
  1226. };
  1227. superView.Add (testView);
  1228. testView.SetRelativeLayout (superView.Frame);
  1229. Assert.Equal (5, testView.Frame.X);
  1230. Assert.Equal (5, testView.Frame.Y);
  1231. testView = new View () {
  1232. AutoSize = false,
  1233. X = 1 + Pos.Percent (50),
  1234. Y = Pos.Percent (50) + 1,
  1235. Width = 1,
  1236. Height = 1
  1237. };
  1238. superView.Add (testView);
  1239. testView.SetRelativeLayout (superView.Frame);
  1240. Assert.Equal (6, testView.Frame.X);
  1241. Assert.Equal (6, testView.Frame.Y);
  1242. testView = new View () {
  1243. AutoSize = false,
  1244. X = Pos.Percent (10) + Pos.Percent (40),
  1245. Y = Pos.Percent (10) + Pos.Percent (40),
  1246. Width = 1,
  1247. Height = 1
  1248. };
  1249. superView.Add (testView);
  1250. testView.SetRelativeLayout (superView.Frame);
  1251. Assert.Equal (5, testView.Frame.X);
  1252. Assert.Equal (5, testView.Frame.Y);
  1253. testView = new View () {
  1254. AutoSize = false,
  1255. X = 1 + Pos.Percent (10) + Pos.Percent (40) - 1,
  1256. Y = 5 + Pos.Percent (10) + Pos.Percent (40) - 5,
  1257. Width = 1,
  1258. Height = 1
  1259. };
  1260. superView.Add (testView);
  1261. testView.SetRelativeLayout (superView.Frame);
  1262. Assert.Equal (5, testView.Frame.X);
  1263. Assert.Equal (5, testView.Frame.Y);
  1264. testView = new View () {
  1265. AutoSize = false,
  1266. X = Pos.Left (testView),
  1267. Y = Pos.Left (testView),
  1268. Width = 1,
  1269. Height = 1
  1270. };
  1271. superView.Add (testView);
  1272. testView.SetRelativeLayout (superView.Frame);
  1273. Assert.Equal (5, testView.Frame.X);
  1274. Assert.Equal (5, testView.Frame.Y);
  1275. testView = new View () {
  1276. AutoSize = false,
  1277. X = 1 + Pos.Left (testView),
  1278. Y = Pos.Top (testView) + 1,
  1279. Width = 1,
  1280. Height = 1
  1281. };
  1282. superView.Add (testView);
  1283. testView.SetRelativeLayout (superView.Frame);
  1284. Assert.Equal (6, testView.Frame.X);
  1285. Assert.Equal (6, testView.Frame.Y);
  1286. }
  1287. [Fact]
  1288. [AutoInitShutdown]
  1289. public void Y_Center_Minus_Absolute_Inside_Window_Height_Ten ()
  1290. {
  1291. var win = new Window ();
  1292. var label = new Label ("This should be the first line.") {
  1293. TextAlignment = Terminal.Gui.TextAlignment.Centered,
  1294. ColorScheme = Colors.Menu,
  1295. Width = Dim.Fill (),
  1296. X = Pos.Center (),
  1297. Y = Pos.Center () - 3 // center minus 3 minus two lines top and bottom borders equal to zero (5-3-2=0)
  1298. };
  1299. var button = new Button ("Press me!") {
  1300. X = Pos.Center (),
  1301. Y = Pos.Center ()
  1302. };
  1303. win.Add (label, button);
  1304. var top = Application.Top;
  1305. top.Add (win);
  1306. Application.Begin (top);
  1307. ((FakeDriver)Application.Driver).SetBufferSize (40, 10);
  1308. Assert.True (label.AutoSize);
  1309. Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
  1310. Assert.Equal (new Rect (0, 0, 40, 10), win.Frame);
  1311. Assert.Equal (new Rect (1, 1, 38, 8), win.Subviews [0].Frame);
  1312. Assert.Equal ("ContentView()({X=1,Y=1,Width=38,Height=8})", win.Subviews [0].ToString ());
  1313. Assert.Equal (new Rect (0, 0, 40, 10), new Rect (
  1314. win.Frame.Left, win.Frame.Top,
  1315. win.Frame.Right, win.Frame.Bottom));
  1316. Assert.Equal (new Rect (0, 0, 38, 1), label.Frame);
  1317. Assert.Equal (new Rect (12, 3, 13, 1), button.Frame);
  1318. var expected = @"
  1319. ┌──────────────────────────────────────┐
  1320. │ This should be the first line. │
  1321. │ │
  1322. │ │
  1323. │ [ Press me! ] │
  1324. │ │
  1325. │ │
  1326. │ │
  1327. │ │
  1328. └──────────────────────────────────────┘
  1329. ";
  1330. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1331. }
  1332. [Fact]
  1333. [AutoInitShutdown]
  1334. public void Y_Center_Minus_Absolute_Inside_Window_Height_Nine ()
  1335. {
  1336. var win = new Window ();
  1337. var label = new Label ("This should be the first line.") {
  1338. TextAlignment = Terminal.Gui.TextAlignment.Centered,
  1339. ColorScheme = Colors.Menu,
  1340. Width = Dim.Fill (),
  1341. X = Pos.Center (),
  1342. Y = Pos.Center () - 3 // center minus 2 minus two lines top and bottom borders equal to zero (5-3-2=0)
  1343. };
  1344. var button = new Button ("Press me!") {
  1345. X = Pos.Center (),
  1346. Y = Pos.Center ()
  1347. };
  1348. win.Add (label, button);
  1349. var top = Application.Top;
  1350. top.Add (win);
  1351. Application.Begin (top);
  1352. ((FakeDriver)Application.Driver).SetBufferSize (40, 9);
  1353. Assert.True (label.AutoSize);
  1354. Assert.Equal (new Rect (0, 0, 40, 9), top.Frame);
  1355. Assert.Equal (new Rect (0, 0, 40, 9), win.Frame);
  1356. Assert.Equal (new Rect (1, 1, 38, 7), win.Subviews [0].Frame);
  1357. Assert.Equal ("ContentView()({X=1,Y=1,Width=38,Height=7})", win.Subviews [0].ToString ());
  1358. Assert.Equal (new Rect (0, 0, 40, 9), new Rect (
  1359. win.Frame.Left, win.Frame.Top,
  1360. win.Frame.Right, win.Frame.Bottom));
  1361. Assert.Equal (new Rect (0, 0, 38, 1), label.Frame);
  1362. Assert.Equal (new Rect (12, 3, 13, 1), button.Frame);
  1363. var expected = @"
  1364. ┌──────────────────────────────────────┐
  1365. │ This should be the first line. │
  1366. │ │
  1367. │ │
  1368. │ [ Press me! ] │
  1369. │ │
  1370. │ │
  1371. │ │
  1372. └──────────────────────────────────────┘
  1373. ";
  1374. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1375. }
  1376. [Fact]
  1377. [AutoInitShutdown]
  1378. public void Y_Center_Minus_Absolute_Inside_Window_Height_Eight ()
  1379. {
  1380. var win = new Window ();
  1381. var label = new Label ("This should be the first line.") {
  1382. TextAlignment = Terminal.Gui.TextAlignment.Centered,
  1383. ColorScheme = Colors.Menu,
  1384. Width = Dim.Fill (),
  1385. X = Pos.Center (),
  1386. Y = Pos.Center () - 2 // center minus 2 minus two lines top and bottom borders equal to zero (4-2-2=0)
  1387. };
  1388. var button = new Button ("Press me!") {
  1389. X = Pos.Center (),
  1390. Y = Pos.Center ()
  1391. };
  1392. win.Add (label, button);
  1393. var top = Application.Top;
  1394. top.Add (win);
  1395. Application.Begin (top);
  1396. ((FakeDriver)Application.Driver).SetBufferSize (40, 8);
  1397. Assert.True (label.AutoSize);
  1398. Assert.Equal (new Rect (0, 0, 40, 8), top.Frame);
  1399. Assert.Equal (new Rect (0, 0, 40, 8), win.Frame);
  1400. Assert.Equal (new Rect (1, 1, 38, 6), win.Subviews [0].Frame);
  1401. Assert.Equal ("ContentView()({X=1,Y=1,Width=38,Height=6})", win.Subviews [0].ToString ());
  1402. Assert.Equal (new Rect (0, 0, 40, 8), new Rect (
  1403. win.Frame.Left, win.Frame.Top,
  1404. win.Frame.Right, win.Frame.Bottom));
  1405. Assert.Equal (new Rect (0, 0, 38, 1), label.Frame);
  1406. Assert.Equal (new Rect (12, 2, 13, 1), button.Frame);
  1407. var expected = @"
  1408. ┌──────────────────────────────────────┐
  1409. │ This should be the first line. │
  1410. │ │
  1411. │ [ Press me! ] │
  1412. │ │
  1413. │ │
  1414. │ │
  1415. └──────────────────────────────────────┘
  1416. ";
  1417. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1418. }
  1419. }
  1420. }