LayoutTests.cs 51 KB

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