LayoutTests.cs 55 KB

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