LayoutTests.cs 54 KB

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