LayoutTests.cs 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  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. // Here the AutoSize ensuring the right size with width 28 (Dim.Fill)
  265. // and height 0 because wasn't set and the text is empty
  266. Assert.Equal ("{X=0,Y=0,Width=28,Height=0}", label.Bounds.ToString ());
  267. label.Text = "First line\nSecond line";
  268. Application.Refresh ();
  269. // Here the AutoSize ensuring the right size with width 28 (Dim.Fill)
  270. // and height 2 because wasn't set and the text has 2 lines
  271. Assert.True (label.AutoSize);
  272. Assert.Equal ("{X=0,Y=0,Width=28,Height=2}", label.Bounds.ToString ());
  273. label.AutoSize = false;
  274. Application.Refresh ();
  275. // Here the SetMinWidthHeight ensuring the minimum height
  276. Assert.False (label.AutoSize);
  277. Assert.Equal ("{X=0,Y=0,Width=28,Height=1}", label.Bounds.ToString ());
  278. label.Text = "First changed line\nSecond changed line\nNew line";
  279. Application.Refresh ();
  280. // Here the AutoSize is false and the width 28 (Dim.Fill) and
  281. // height 1 because wasn't set and SetMinWidthHeight ensuring the minimum height
  282. Assert.False (label.AutoSize);
  283. Assert.Equal ("{X=0,Y=0,Width=28,Height=1}", label.Bounds.ToString ());
  284. label.AutoSize = true;
  285. Application.Refresh ();
  286. // Here the AutoSize ensuring the right size with width 28 (Dim.Fill)
  287. // and height 3 because wasn't set and the text has 3 lines
  288. Assert.True (label.AutoSize);
  289. Assert.Equal ("{X=0,Y=0,Width=28,Height=3}", label.Bounds.ToString ());
  290. }
  291. [Fact, AutoInitShutdown]
  292. public void AutoSize_True_Setting_With_Height_Horizontal ()
  293. {
  294. var label = new Label ("Hello") { Width = 10, Height = 2 };
  295. var viewX = new View ("X") { X = Pos.Right (label) };
  296. var viewY = new View ("Y") { Y = Pos.Bottom (label) };
  297. Application.Top.Add (label, viewX, viewY);
  298. Application.Begin (Application.Top);
  299. Assert.True (label.AutoSize);
  300. Assert.Equal (new Rect (0, 0, 10, 2), label.Frame);
  301. var expected = @"
  302. Hello X
  303. Y
  304. ";
  305. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  306. Assert.Equal (new Rect (0, 0, 11, 3), pos);
  307. label.AutoSize = false;
  308. Application.Refresh ();
  309. Assert.False (label.AutoSize);
  310. Assert.Equal (new Rect (0, 0, 10, 2), label.Frame);
  311. expected = @"
  312. Hello X
  313. Y
  314. ";
  315. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  316. Assert.Equal (new Rect (0, 0, 11, 3), pos);
  317. }
  318. [Fact, AutoInitShutdown]
  319. public void AutoSize_True_Setting_With_Height_Vertical ()
  320. {
  321. var label = new Label ("Hello") { Width = 2, Height = 10, TextDirection = TextDirection.TopBottom_LeftRight };
  322. var viewX = new View ("X") { X = Pos.Right (label) };
  323. var viewY = new View ("Y") { Y = Pos.Bottom (label) };
  324. Application.Top.Add (label, viewX, viewY);
  325. Application.Begin (Application.Top);
  326. Assert.True (label.AutoSize);
  327. Assert.Equal (new Rect (0, 0, 2, 10), label.Frame);
  328. var expected = @"
  329. H X
  330. e
  331. l
  332. l
  333. o
  334. Y
  335. ";
  336. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  337. Assert.Equal (new Rect (0, 0, 3, 11), pos);
  338. label.AutoSize = false;
  339. Application.Refresh ();
  340. Assert.False (label.AutoSize);
  341. Assert.Equal (new Rect (0, 0, 2, 10), label.Frame);
  342. expected = @"
  343. H X
  344. e
  345. l
  346. l
  347. o
  348. Y
  349. ";
  350. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  351. Assert.Equal (new Rect (0, 0, 3, 11), pos);
  352. }
  353. [Fact]
  354. [AutoInitShutdown]
  355. public void Excess_Text_Is_Erased_When_The_Width_Is_Reduced ()
  356. {
  357. var lbl = new Label ("123");
  358. Application.Top.Add (lbl);
  359. Application.Begin (Application.Top);
  360. Assert.True (lbl.AutoSize);
  361. Assert.Equal ("123 ", GetContents ());
  362. lbl.Text = "12";
  363. // Here the AutoSize ensuring the right size with width 3 (Dim.Absolute)
  364. // that was set on the OnAdded method with the text length of 3
  365. // and height 1 because wasn't set and the text has 1 line
  366. Assert.Equal (new Rect (0, 0, 3, 1), lbl.Frame);
  367. Assert.Equal (new Rect (0, 0, 3, 1), lbl._needsDisplay);
  368. Assert.Equal (new Rect (0, 0, 0, 0), lbl.SuperView._needsDisplay);
  369. Assert.True (lbl.SuperView.LayoutNeeded);
  370. lbl.SuperView.Redraw (lbl.SuperView.Bounds);
  371. Assert.Equal ("12 ", GetContents ());
  372. string GetContents ()
  373. {
  374. var text = "";
  375. for (int i = 0; i < 4; i++) {
  376. text += (char)Application.Driver.Contents [0, i, 0];
  377. }
  378. return text;
  379. }
  380. }
  381. [Fact, AutoInitShutdown]
  382. public void Width_Height_SetMinWidthHeight_Narrow_Wide_Runes ()
  383. {
  384. var text = $"First line{Environment.NewLine}Second line";
  385. var horizontalView = new View () {
  386. Width = 20,
  387. Text = text
  388. };
  389. var verticalView = new View () {
  390. Y = 3,
  391. Height = 20,
  392. Text = text,
  393. TextDirection = TextDirection.TopBottom_LeftRight
  394. };
  395. var win = new Window () {
  396. Width = Dim.Fill (),
  397. Height = Dim.Fill (),
  398. Text = "Window"
  399. };
  400. win.Add (horizontalView, verticalView);
  401. Application.Top.Add (win);
  402. Application.Begin (Application.Top);
  403. ((FakeDriver)Application.Driver).SetBufferSize (32, 32);
  404. Assert.False (horizontalView.AutoSize);
  405. Assert.False (verticalView.AutoSize);
  406. Assert.Equal (new Rect (0, 0, 20, 1), horizontalView.Frame);
  407. Assert.Equal (new Rect (0, 3, 1, 20), verticalView.Frame);
  408. var expected = @"
  409. ┌──────────────────────────────┐
  410. │First line Second li │
  411. │ │
  412. │ │
  413. │F │
  414. │i │
  415. │r │
  416. │s │
  417. │t │
  418. │ │
  419. │l │
  420. │i │
  421. │n │
  422. │e │
  423. │ │
  424. │S │
  425. │e │
  426. │c │
  427. │o │
  428. │n │
  429. │d │
  430. │ │
  431. │l │
  432. │i │
  433. │ │
  434. │ │
  435. │ │
  436. │ │
  437. │ │
  438. │ │
  439. │ │
  440. └──────────────────────────────┘
  441. ";
  442. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  443. Assert.Equal (new Rect (0, 0, 32, 32), pos);
  444. verticalView.Text = $"最初の行{Environment.NewLine}二行目";
  445. Application.Top.Redraw (Application.Top.Bounds);
  446. Assert.Equal (new Rect (0, 3, 2, 20), verticalView.Frame);
  447. expected = @"
  448. ┌──────────────────────────────┐
  449. │First line Second li │
  450. │ │
  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. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  482. Assert.Equal (new Rect (0, 0, 32, 32), pos);
  483. }
  484. [Fact, AutoInitShutdown]
  485. public void TextDirection_Toggle ()
  486. {
  487. var view = new View ();
  488. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  489. win.Add (view);
  490. Application.Top.Add (win);
  491. Application.Begin (Application.Top);
  492. ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  493. Assert.False (view.AutoSize);
  494. Assert.Equal (TextDirection.LeftRight_TopBottom, view.TextDirection);
  495. Assert.Equal (Rect.Empty, view.Frame);
  496. Assert.Equal ("Absolute(0)", view.X.ToString ());
  497. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  498. Assert.Equal ("Absolute(0)", view.Width.ToString ());
  499. Assert.Equal ("Absolute(0)", view.Height.ToString ());
  500. var expected = @"
  501. ┌────────────────────┐
  502. │ │
  503. │ │
  504. │ │
  505. │ │
  506. │ │
  507. │ │
  508. │ │
  509. │ │
  510. │ │
  511. │ │
  512. │ │
  513. │ │
  514. │ │
  515. │ │
  516. │ │
  517. │ │
  518. │ │
  519. │ │
  520. │ │
  521. │ │
  522. └────────────────────┘
  523. ";
  524. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  525. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  526. view.Text = "Hello World";
  527. view.Width = 11;
  528. Application.Refresh ();
  529. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  530. Assert.Equal ("Absolute(0)", view.X.ToString ());
  531. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  532. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  533. Assert.Equal ("Absolute(0)", view.Height.ToString ());
  534. expected = @"
  535. ┌────────────────────┐
  536. │Hello World │
  537. │ │
  538. │ │
  539. │ │
  540. │ │
  541. │ │
  542. │ │
  543. │ │
  544. │ │
  545. │ │
  546. │ │
  547. │ │
  548. │ │
  549. │ │
  550. │ │
  551. │ │
  552. │ │
  553. │ │
  554. │ │
  555. │ │
  556. └────────────────────┘
  557. ";
  558. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  559. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  560. view.AutoSize = true;
  561. view.Text = "Hello Worlds";
  562. Application.Refresh ();
  563. Assert.Equal (new Rect (0, 0, 12, 1), view.Frame);
  564. Assert.Equal ("Absolute(0)", view.X.ToString ());
  565. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  566. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  567. Assert.Equal ("Absolute(0)", view.Height.ToString ());
  568. expected = @"
  569. ┌────────────────────┐
  570. │Hello Worlds │
  571. │ │
  572. │ │
  573. │ │
  574. │ │
  575. │ │
  576. │ │
  577. │ │
  578. │ │
  579. │ │
  580. │ │
  581. │ │
  582. │ │
  583. │ │
  584. │ │
  585. │ │
  586. │ │
  587. │ │
  588. │ │
  589. │ │
  590. └────────────────────┘
  591. ";
  592. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  593. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  594. view.TextDirection = TextDirection.TopBottom_LeftRight;
  595. Application.Refresh ();
  596. Assert.Equal (new Rect (0, 0, 11, 12), view.Frame);
  597. Assert.Equal ("Absolute(0)", view.X.ToString ());
  598. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  599. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  600. Assert.Equal ("Absolute(0)", view.Height.ToString ());
  601. expected = @"
  602. ┌────────────────────┐
  603. │H │
  604. │e │
  605. │l │
  606. │l │
  607. │o │
  608. │ │
  609. │W │
  610. │o │
  611. │r │
  612. │l │
  613. │d │
  614. │s │
  615. │ │
  616. │ │
  617. │ │
  618. │ │
  619. │ │
  620. │ │
  621. │ │
  622. │ │
  623. └────────────────────┘
  624. ";
  625. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  626. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  627. view.AutoSize = false;
  628. view.Height = 1;
  629. Application.Refresh ();
  630. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  631. Assert.Equal ("Absolute(0)", view.X.ToString ());
  632. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  633. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  634. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  635. expected = @"
  636. ┌────────────────────┐
  637. │HelloWorlds │
  638. │ │
  639. │ │
  640. │ │
  641. │ │
  642. │ │
  643. │ │
  644. │ │
  645. │ │
  646. │ │
  647. │ │
  648. │ │
  649. │ │
  650. │ │
  651. │ │
  652. │ │
  653. │ │
  654. │ │
  655. │ │
  656. │ │
  657. └────────────────────┘
  658. ";
  659. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  660. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  661. view.PreserveTrailingSpaces = true;
  662. Application.Refresh ();
  663. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  664. Assert.Equal ("Absolute(0)", view.X.ToString ());
  665. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  666. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  667. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  668. expected = @"
  669. ┌────────────────────┐
  670. │Hello World │
  671. │ │
  672. │ │
  673. │ │
  674. │ │
  675. │ │
  676. │ │
  677. │ │
  678. │ │
  679. │ │
  680. │ │
  681. │ │
  682. │ │
  683. │ │
  684. │ │
  685. │ │
  686. │ │
  687. │ │
  688. │ │
  689. │ │
  690. └────────────────────┘
  691. ";
  692. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  693. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  694. view.PreserveTrailingSpaces = false;
  695. var f = view.Frame;
  696. view.Width = f.Height;
  697. view.Height = f.Width;
  698. view.TextDirection = TextDirection.TopBottom_LeftRight;
  699. Application.Refresh ();
  700. Assert.Equal (new Rect (0, 0, 1, 11), view.Frame);
  701. Assert.Equal ("Absolute(0)", view.X.ToString ());
  702. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  703. Assert.Equal ("Absolute(1)", view.Width.ToString ());
  704. Assert.Equal ("Absolute(11)", view.Height.ToString ());
  705. expected = @"
  706. ┌────────────────────┐
  707. │H │
  708. │e │
  709. │l │
  710. │l │
  711. │o │
  712. │ │
  713. │W │
  714. │o │
  715. │r │
  716. │l │
  717. │d │
  718. │ │
  719. │ │
  720. │ │
  721. │ │
  722. │ │
  723. │ │
  724. │ │
  725. │ │
  726. │ │
  727. └────────────────────┘
  728. ";
  729. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  730. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  731. view.AutoSize = true;
  732. Application.Refresh ();
  733. Assert.Equal (new Rect (0, 0, 1, 12), view.Frame);
  734. Assert.Equal ("Absolute(0)", view.X.ToString ());
  735. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  736. Assert.Equal ("Absolute(1)", view.Width.ToString ());
  737. Assert.Equal ("Absolute(11)", view.Height.ToString ());
  738. expected = @"
  739. ┌────────────────────┐
  740. │H │
  741. │e │
  742. │l │
  743. │l │
  744. │o │
  745. │ │
  746. │W │
  747. │o │
  748. │r │
  749. │l │
  750. │d │
  751. │s │
  752. │ │
  753. │ │
  754. │ │
  755. │ │
  756. │ │
  757. │ │
  758. │ │
  759. │ │
  760. └────────────────────┘
  761. ";
  762. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  763. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  764. }
  765. [Fact, AutoInitShutdown]
  766. public void Width_Height_AutoSize_True_Stay_True_If_TextFormatter_Size_Fit ()
  767. {
  768. var text = $"Fi_nish 終";
  769. var horizontalView = new View () {
  770. AutoSize = true,
  771. HotKeySpecifier = '_',
  772. Text = text
  773. };
  774. var verticalView = new View () {
  775. Y = 3,
  776. AutoSize = true,
  777. HotKeySpecifier = '_',
  778. Text = text,
  779. TextDirection = TextDirection.TopBottom_LeftRight
  780. };
  781. var win = new Window () {
  782. Width = Dim.Fill (),
  783. Height = Dim.Fill (),
  784. Text = "Window"
  785. };
  786. win.Add (horizontalView, verticalView);
  787. Application.Top.Add (win);
  788. Application.Begin (Application.Top);
  789. ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  790. Assert.True (horizontalView.AutoSize);
  791. Assert.True (verticalView.AutoSize);
  792. Assert.Equal (new Size (10, 1), horizontalView.TextFormatter.Size);
  793. Assert.Equal (new Size (2, 9), verticalView.TextFormatter.Size);
  794. Assert.Equal (new Rect (0, 0, 9, 1), horizontalView.Frame);
  795. Assert.Equal ("Absolute(0)", horizontalView.X.ToString ());
  796. Assert.Equal ("Absolute(0)", horizontalView.Y.ToString ());
  797. Assert.Equal ("Absolute(9)", horizontalView.Width.ToString ());
  798. Assert.Equal ("Absolute(1)", horizontalView.Height.ToString ());
  799. Assert.Equal (new Rect (0, 3, 2, 8), verticalView.Frame);
  800. Assert.Equal ("Absolute(0)", verticalView.X.ToString ());
  801. Assert.Equal ("Absolute(3)", verticalView.Y.ToString ());
  802. Assert.Equal ("Absolute(2)", verticalView.Width.ToString ());
  803. Assert.Equal ("Absolute(8)", verticalView.Height.ToString ());
  804. var expected = @"
  805. ┌────────────────────┐
  806. │Finish 終 │
  807. │ │
  808. │ │
  809. │F │
  810. │i │
  811. │n │
  812. │i │
  813. │s │
  814. │h │
  815. │ │
  816. │終 │
  817. │ │
  818. │ │
  819. │ │
  820. │ │
  821. │ │
  822. │ │
  823. │ │
  824. │ │
  825. │ │
  826. └────────────────────┘
  827. ";
  828. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  829. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  830. verticalView.Text = $"最初_の行二行目";
  831. Application.Top.Redraw (Application.Top.Bounds);
  832. Assert.True (horizontalView.AutoSize);
  833. Assert.True (verticalView.AutoSize);
  834. // height was initialized with 8 and is kept as minimum
  835. Assert.Equal (new Rect (0, 3, 2, 8), verticalView.Frame);
  836. Assert.Equal ("Absolute(0)", verticalView.X.ToString ());
  837. Assert.Equal ("Absolute(3)", verticalView.Y.ToString ());
  838. Assert.Equal ("Absolute(2)", verticalView.Width.ToString ());
  839. Assert.Equal ("Absolute(8)", verticalView.Height.ToString ());
  840. expected = @"
  841. ┌────────────────────┐
  842. │Finish 終 │
  843. │ │
  844. │ │
  845. │最 │
  846. │初 │
  847. │の │
  848. │行 │
  849. │二 │
  850. │行 │
  851. │目 │
  852. │ │
  853. │ │
  854. │ │
  855. │ │
  856. │ │
  857. │ │
  858. │ │
  859. │ │
  860. │ │
  861. │ │
  862. └────────────────────┘
  863. ";
  864. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  865. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  866. }
  867. [Fact, AutoInitShutdown]
  868. public void AutoSize_Stays_True_Center_HotKeySpecifier ()
  869. {
  870. var btn = new Button () {
  871. X = Pos.Center (),
  872. Y = Pos.Center (),
  873. Text = "Say He_llo 你"
  874. };
  875. var win = new Window () {
  876. Width = Dim.Fill (),
  877. Height = Dim.Fill (),
  878. Title = "Test Demo 你"
  879. };
  880. win.Add (btn);
  881. Application.Top.Add (win);
  882. Assert.True (btn.AutoSize);
  883. Application.Begin (Application.Top);
  884. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  885. var expected = @"
  886. ┌ Test Demo 你 ──────────────┐
  887. │ │
  888. │ [ Say Hello 你 ] │
  889. │ │
  890. └────────────────────────────┘
  891. ";
  892. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  893. Assert.True (btn.AutoSize);
  894. btn.Text = "Say He_llo 你 changed";
  895. Assert.True (btn.AutoSize);
  896. Application.Refresh ();
  897. expected = @"
  898. ┌ Test Demo 你 ──────────────┐
  899. │ │
  900. │ [ Say Hello 你 changed ] │
  901. │ │
  902. └────────────────────────────┘
  903. ";
  904. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  905. }
  906. [Fact, AutoInitShutdown]
  907. public void AutoSize_False_Equal_Before_And_After_IsInitialized_With_Differents_Orders ()
  908. {
  909. var view1 = new View () { Text = "Say Hello view1 你", AutoSize = false, Width = 10, Height = 5 };
  910. var view2 = new View () { Text = "Say Hello view2 你", Width = 10, Height = 5, AutoSize = false };
  911. var view3 = new View () { AutoSize = false, Width = 10, Height = 5, Text = "Say Hello view3 你" };
  912. var view4 = new View () {
  913. Text = "Say Hello view4 你",
  914. AutoSize = false,
  915. Width = 10,
  916. Height = 5,
  917. TextDirection = TextDirection.TopBottom_LeftRight
  918. };
  919. var view5 = new View () {
  920. Text = "Say Hello view5 你",
  921. Width = 10,
  922. Height = 5,
  923. AutoSize = false,
  924. TextDirection = TextDirection.TopBottom_LeftRight
  925. };
  926. var view6 = new View () {
  927. AutoSize = false,
  928. Width = 10,
  929. Height = 5,
  930. TextDirection = TextDirection.TopBottom_LeftRight,
  931. Text = "Say Hello view6 你",
  932. };
  933. Application.Top.Add (view1, view2, view3, view4, view5, view6);
  934. Assert.False (view1.IsInitialized);
  935. Assert.False (view2.IsInitialized);
  936. Assert.False (view3.IsInitialized);
  937. Assert.False (view4.IsInitialized);
  938. Assert.False (view5.IsInitialized);
  939. Assert.False (view1.AutoSize);
  940. Assert.Equal (new Rect (0, 0, 10, 5), view1.Frame);
  941. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  942. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  943. Assert.False (view2.AutoSize);
  944. Assert.Equal (new Rect (0, 0, 10, 5), view2.Frame);
  945. Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  946. Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  947. Assert.False (view3.AutoSize);
  948. Assert.Equal (new Rect (0, 0, 10, 5), view3.Frame);
  949. Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  950. Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  951. Assert.False (view4.AutoSize);
  952. Assert.Equal (new Rect (0, 0, 10, 5), view4.Frame);
  953. Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  954. Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  955. Assert.False (view5.AutoSize);
  956. Assert.Equal (new Rect (0, 0, 10, 5), view5.Frame);
  957. Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  958. Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  959. Assert.False (view6.AutoSize);
  960. Assert.Equal (new Rect (0, 0, 10, 5), view6.Frame);
  961. Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  962. Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  963. Application.Begin (Application.Top);
  964. Assert.True (view1.IsInitialized);
  965. Assert.True (view2.IsInitialized);
  966. Assert.True (view3.IsInitialized);
  967. Assert.True (view4.IsInitialized);
  968. Assert.True (view5.IsInitialized);
  969. Assert.False (view1.AutoSize);
  970. Assert.Equal (new Rect (0, 0, 10, 5), view1.Frame);
  971. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  972. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  973. Assert.False (view2.AutoSize);
  974. Assert.Equal (new Rect (0, 0, 10, 5), view2.Frame);
  975. Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  976. Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  977. Assert.False (view3.AutoSize);
  978. Assert.Equal (new Rect (0, 0, 10, 5), view3.Frame);
  979. Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  980. Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  981. Assert.False (view4.AutoSize);
  982. Assert.Equal (new Rect (0, 0, 10, 5), view4.Frame);
  983. Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  984. Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  985. Assert.False (view5.AutoSize);
  986. Assert.Equal (new Rect (0, 0, 10, 5), view5.Frame);
  987. Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  988. Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  989. Assert.False (view6.AutoSize);
  990. Assert.Equal (new Rect (0, 0, 10, 5), view6.Frame);
  991. Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  992. Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  993. }
  994. [Fact, AutoInitShutdown]
  995. public void AutoSize_True_Equal_Before_And_After_IsInitialized_With_Differents_Orders ()
  996. {
  997. var view1 = new View () { Text = "Say Hello view1 你", AutoSize = true, Width = 10, Height = 5 };
  998. var view2 = new View () { Text = "Say Hello view2 你", Width = 10, Height = 5, AutoSize = true };
  999. var view3 = new View () { AutoSize = true, Width = 10, Height = 5, Text = "Say Hello view3 你" };
  1000. var view4 = new View () {
  1001. Text = "Say Hello view4 你",
  1002. AutoSize = true,
  1003. Width = 10,
  1004. Height = 5,
  1005. TextDirection = TextDirection.TopBottom_LeftRight
  1006. };
  1007. var view5 = new View () {
  1008. Text = "Say Hello view5 你",
  1009. Width = 10,
  1010. Height = 5,
  1011. AutoSize = true,
  1012. TextDirection = TextDirection.TopBottom_LeftRight
  1013. };
  1014. var view6 = new View () {
  1015. AutoSize = true,
  1016. Width = 10,
  1017. Height = 5,
  1018. TextDirection = TextDirection.TopBottom_LeftRight,
  1019. Text = "Say Hello view6 你",
  1020. };
  1021. Application.Top.Add (view1, view2, view3, view4, view5, view6);
  1022. Assert.False (view1.IsInitialized);
  1023. Assert.False (view2.IsInitialized);
  1024. Assert.False (view3.IsInitialized);
  1025. Assert.False (view4.IsInitialized);
  1026. Assert.False (view5.IsInitialized);
  1027. Assert.True (view1.AutoSize);
  1028. Assert.Equal (new Rect (0, 0, 18, 5), view1.Frame);
  1029. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  1030. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  1031. Assert.True (view2.AutoSize);
  1032. Assert.Equal (new Rect (0, 0, 18, 5), view2.Frame);
  1033. Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  1034. Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  1035. Assert.True (view3.AutoSize);
  1036. Assert.Equal (new Rect (0, 0, 18, 5), view3.Frame);
  1037. Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  1038. Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  1039. Assert.True (view4.AutoSize);
  1040. Assert.Equal (new Rect (0, 0, 10, 17), view4.Frame);
  1041. Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  1042. Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  1043. Assert.True (view5.AutoSize);
  1044. Assert.Equal (new Rect (0, 0, 10, 17), view5.Frame);
  1045. Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  1046. Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  1047. Assert.True (view6.AutoSize);
  1048. Assert.Equal (new Rect (0, 0, 10, 17), view6.Frame);
  1049. Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  1050. Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  1051. Application.Begin (Application.Top);
  1052. Assert.True (view1.IsInitialized);
  1053. Assert.True (view2.IsInitialized);
  1054. Assert.True (view3.IsInitialized);
  1055. Assert.True (view4.IsInitialized);
  1056. Assert.True (view5.IsInitialized);
  1057. Assert.True (view1.AutoSize);
  1058. Assert.Equal (new Rect (0, 0, 18, 5), view1.Frame);
  1059. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  1060. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  1061. Assert.True (view2.AutoSize);
  1062. Assert.Equal (new Rect (0, 0, 18, 5), view2.Frame);
  1063. Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  1064. Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  1065. Assert.True (view3.AutoSize);
  1066. Assert.Equal (new Rect (0, 0, 18, 5), view3.Frame);
  1067. Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  1068. Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  1069. Assert.True (view4.AutoSize);
  1070. Assert.Equal (new Rect (0, 0, 10, 17), view4.Frame);
  1071. Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  1072. Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  1073. Assert.True (view5.AutoSize);
  1074. Assert.Equal (new Rect (0, 0, 10, 17), view5.Frame);
  1075. Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  1076. Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  1077. Assert.True (view6.AutoSize);
  1078. Assert.Equal (new Rect (0, 0, 10, 17), view6.Frame);
  1079. Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  1080. Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  1081. }
  1082. [Fact, AutoInitShutdown]
  1083. public void Setting_Frame_Dont_Respect_AutoSize_True_On_Layout_Absolute ()
  1084. {
  1085. var view1 = new View (new Rect (0, 0, 10, 0)) { Text = "Say Hello view1 你", AutoSize = true };
  1086. var view2 = new View (new Rect (0, 0, 0, 10)) {
  1087. Text = "Say Hello view2 你",
  1088. AutoSize = true,
  1089. TextDirection = TextDirection.TopBottom_LeftRight
  1090. };
  1091. Application.Top.Add (view1, view2);
  1092. var rs = Application.Begin (Application.Top);
  1093. Assert.True (view1.AutoSize);
  1094. Assert.Equal (LayoutStyle.Absolute, view1.LayoutStyle);
  1095. Assert.Equal (new Rect (0, 0, 18, 1), view1.Frame);
  1096. Assert.Equal ("Absolute(0)", view1.X.ToString ());
  1097. Assert.Equal ("Absolute(0)", view1.Y.ToString ());
  1098. Assert.Equal ("Absolute(18)", view1.Width.ToString ());
  1099. Assert.Equal ("Absolute(1)", view1.Height.ToString ());
  1100. Assert.True (view2.AutoSize);
  1101. Assert.Equal (LayoutStyle.Absolute, view2.LayoutStyle);
  1102. Assert.Equal (new Rect (0, 0, 2, 17), view2.Frame);
  1103. Assert.Equal ("Absolute(0)", view2.X.ToString ());
  1104. Assert.Equal ("Absolute(0)", view2.Y.ToString ());
  1105. Assert.Equal ("Absolute(2)", view2.Width.ToString ());
  1106. Assert.Equal ("Absolute(17)", view2.Height.ToString ());
  1107. view1.Frame = new Rect (0, 0, 25, 4);
  1108. bool firstIteration = false;
  1109. Application.RunMainLoopIteration (ref rs, true, ref firstIteration);
  1110. Assert.True (view1.AutoSize);
  1111. Assert.Equal (LayoutStyle.Absolute, view1.LayoutStyle);
  1112. Assert.Equal (new Rect (0, 0, 25, 4), view1.Frame);
  1113. Assert.Equal ("Absolute(0)", view1.X.ToString ());
  1114. Assert.Equal ("Absolute(0)", view1.Y.ToString ());
  1115. Assert.Equal ("Absolute(18)", view1.Width.ToString ());
  1116. Assert.Equal ("Absolute(1)", view1.Height.ToString ());
  1117. view2.Frame = new Rect (0, 0, 1, 25);
  1118. Application.RunMainLoopIteration (ref rs, true, ref firstIteration);
  1119. Assert.True (view2.AutoSize);
  1120. Assert.Equal (LayoutStyle.Absolute, view2.LayoutStyle);
  1121. Assert.Equal (new Rect (0, 0, 1, 25), view2.Frame);
  1122. Assert.Equal ("Absolute(0)", view2.X.ToString ());
  1123. Assert.Equal ("Absolute(0)", view2.Y.ToString ());
  1124. Assert.Equal ("Absolute(2)", view2.Width.ToString ());
  1125. Assert.Equal ("Absolute(17)", view2.Height.ToString ());
  1126. }
  1127. [Fact, AutoInitShutdown]
  1128. public void Pos_Dim_Are_Null_If_Not_Initialized_On_Constructor_IsAdded_False ()
  1129. {
  1130. var top = Application.Top;
  1131. var view1 = new View ();
  1132. Assert.False (view1.IsAdded);
  1133. Assert.Null (view1.X);
  1134. Assert.Null (view1.Y);
  1135. Assert.Null (view1.Width);
  1136. Assert.Null (view1.Height);
  1137. top.Add (view1);
  1138. Assert.True (view1.IsAdded);
  1139. Assert.Equal ("Absolute(0)", view1.X.ToString ());
  1140. Assert.Equal ("Absolute(0)", view1.Y.ToString ());
  1141. Assert.Equal ("Absolute(0)", view1.Width.ToString ());
  1142. Assert.Equal ("Absolute(0)", view1.Height.ToString ());
  1143. var view2 = new View () {
  1144. X = Pos.Center (),
  1145. Y = Pos.Center (),
  1146. Width = Dim.Fill (),
  1147. Height = Dim.Fill ()
  1148. };
  1149. Assert.False (view2.IsAdded);
  1150. Assert.Equal ("Center", view2.X.ToString ());
  1151. Assert.Equal ("Center", view2.Y.ToString ());
  1152. Assert.Equal ("Fill(0)", view2.Width.ToString ());
  1153. Assert.Equal ("Fill(0)", view2.Height.ToString ());
  1154. top.Add (view2);
  1155. Assert.True (view2.IsAdded);
  1156. Assert.Equal ("Center", view2.X.ToString ());
  1157. Assert.Equal ("Center", view2.Y.ToString ());
  1158. Assert.Equal ("Fill(0)", view2.Width.ToString ());
  1159. Assert.Equal ("Fill(0)", view2.Height.ToString ());
  1160. }
  1161. [Fact]
  1162. public void SetRelativeLayout_PosCombine_Center_Plus_Absolute ()
  1163. {
  1164. var superView = new View () {
  1165. AutoSize = false,
  1166. Width = 10,
  1167. Height = 10
  1168. };
  1169. var testView = new View () {
  1170. AutoSize = false,
  1171. X = Pos.Center (),
  1172. Y = Pos.Center (),
  1173. Width = 1,
  1174. Height = 1
  1175. };
  1176. superView.Add (testView);
  1177. testView.SetRelativeLayout (superView.Frame);
  1178. Assert.Equal (4, testView.Frame.X);
  1179. Assert.Equal (4, testView.Frame.Y);
  1180. testView = new View () {
  1181. AutoSize = false,
  1182. X = Pos.Center () + 1,
  1183. Y = Pos.Center () + 1,
  1184. Width = 1,
  1185. Height = 1
  1186. };
  1187. superView.Add (testView);
  1188. testView.SetRelativeLayout (superView.Frame);
  1189. Assert.Equal (5, testView.Frame.X);
  1190. Assert.Equal (5, testView.Frame.Y);
  1191. testView = new View () {
  1192. AutoSize = false,
  1193. X = 1 + Pos.Center (),
  1194. Y = 1 + Pos.Center (),
  1195. Width = 1,
  1196. Height = 1
  1197. };
  1198. superView.Add (testView);
  1199. testView.SetRelativeLayout (superView.Frame);
  1200. Assert.Equal (5, testView.Frame.X);
  1201. Assert.Equal (5, testView.Frame.Y);
  1202. testView = new View () {
  1203. AutoSize = false,
  1204. X = 1 + Pos.Percent (50),
  1205. Y = Pos.Percent (50) + 1,
  1206. Width = 1,
  1207. Height = 1
  1208. };
  1209. superView.Add (testView);
  1210. testView.SetRelativeLayout (superView.Frame);
  1211. Assert.Equal (6, testView.Frame.X);
  1212. Assert.Equal (6, testView.Frame.Y);
  1213. testView = new View () {
  1214. AutoSize = false,
  1215. X = Pos.Percent (10) + Pos.Percent (40),
  1216. Y = Pos.Percent (10) + Pos.Percent (40),
  1217. Width = 1,
  1218. Height = 1
  1219. };
  1220. superView.Add (testView);
  1221. testView.SetRelativeLayout (superView.Frame);
  1222. Assert.Equal (5, testView.Frame.X);
  1223. Assert.Equal (5, testView.Frame.Y);
  1224. testView = new View () {
  1225. AutoSize = false,
  1226. X = 1 + Pos.Percent (10) + Pos.Percent (40) - 1,
  1227. Y = 5 + Pos.Percent (10) + Pos.Percent (40) - 5,
  1228. Width = 1,
  1229. Height = 1
  1230. };
  1231. superView.Add (testView);
  1232. testView.SetRelativeLayout (superView.Frame);
  1233. Assert.Equal (5, testView.Frame.X);
  1234. Assert.Equal (5, testView.Frame.Y);
  1235. testView = new View () {
  1236. AutoSize = false,
  1237. X = Pos.Left(testView),
  1238. Y = Pos.Left (testView),
  1239. Width = 1,
  1240. Height = 1
  1241. };
  1242. superView.Add (testView);
  1243. testView.SetRelativeLayout (superView.Frame);
  1244. Assert.Equal (5, testView.Frame.X);
  1245. Assert.Equal (5, testView.Frame.Y);
  1246. testView = new View () {
  1247. AutoSize = false,
  1248. X = 1 + Pos.Left (testView),
  1249. Y = Pos.Top (testView) + 1,
  1250. Width = 1,
  1251. Height = 1
  1252. };
  1253. superView.Add (testView);
  1254. testView.SetRelativeLayout (superView.Frame);
  1255. Assert.Equal (6, testView.Frame.X);
  1256. Assert.Equal (6, testView.Frame.Y);
  1257. }
  1258. }
  1259. }