LayoutTests.cs 44 KB

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