LayoutTests.cs 44 KB

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