AutoSizeTests.cs 43 KB

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