AutoSizeTests.cs 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182
  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. readonly string [] expecteds = new string [21] {
  1281. @"
  1282. ┌────────────────────┐
  1283. │View with long text │
  1284. │ │
  1285. └────────────────────┘",
  1286. @"
  1287. ┌────────────────────┐
  1288. │View with long text │
  1289. │Label 0 │
  1290. │Label 0 │
  1291. └────────────────────┘",
  1292. @"
  1293. ┌────────────────────┐
  1294. │View with long text │
  1295. │Label 0 │
  1296. │Label 1 │
  1297. │Label 1 │
  1298. └────────────────────┘",
  1299. @"
  1300. ┌────────────────────┐
  1301. │View with long text │
  1302. │Label 0 │
  1303. │Label 1 │
  1304. │Label 2 │
  1305. │Label 2 │
  1306. └────────────────────┘",
  1307. @"
  1308. ┌────────────────────┐
  1309. │View with long text │
  1310. │Label 0 │
  1311. │Label 1 │
  1312. │Label 2 │
  1313. │Label 3 │
  1314. │Label 3 │
  1315. └────────────────────┘",
  1316. @"
  1317. ┌────────────────────┐
  1318. │View with long text │
  1319. │Label 0 │
  1320. │Label 1 │
  1321. │Label 2 │
  1322. │Label 3 │
  1323. │Label 4 │
  1324. │Label 4 │
  1325. └────────────────────┘",
  1326. @"
  1327. ┌────────────────────┐
  1328. │View with long text │
  1329. │Label 0 │
  1330. │Label 1 │
  1331. │Label 2 │
  1332. │Label 3 │
  1333. │Label 4 │
  1334. │Label 5 │
  1335. │Label 5 │
  1336. └────────────────────┘",
  1337. @"
  1338. ┌────────────────────┐
  1339. │View with long text │
  1340. │Label 0 │
  1341. │Label 1 │
  1342. │Label 2 │
  1343. │Label 3 │
  1344. │Label 4 │
  1345. │Label 5 │
  1346. │Label 6 │
  1347. │Label 6 │
  1348. └────────────────────┘",
  1349. @"
  1350. ┌────────────────────┐
  1351. │View with long text │
  1352. │Label 0 │
  1353. │Label 1 │
  1354. │Label 2 │
  1355. │Label 3 │
  1356. │Label 4 │
  1357. │Label 5 │
  1358. │Label 6 │
  1359. │Label 7 │
  1360. │Label 7 │
  1361. └────────────────────┘",
  1362. @"
  1363. ┌────────────────────┐
  1364. │View with long text │
  1365. │Label 0 │
  1366. │Label 1 │
  1367. │Label 2 │
  1368. │Label 3 │
  1369. │Label 4 │
  1370. │Label 5 │
  1371. │Label 6 │
  1372. │Label 7 │
  1373. │Label 8 │
  1374. │Label 8 │
  1375. └────────────────────┘",
  1376. @"
  1377. ┌────────────────────┐
  1378. │View with long text │
  1379. │Label 0 │
  1380. │Label 1 │
  1381. │Label 2 │
  1382. │Label 3 │
  1383. │Label 4 │
  1384. │Label 5 │
  1385. │Label 6 │
  1386. │Label 7 │
  1387. │Label 8 │
  1388. │Label 9 │
  1389. │Label 9 │
  1390. └────────────────────┘",
  1391. @"
  1392. ┌────────────────────┐
  1393. │View with long text │
  1394. │Label 0 │
  1395. │Label 1 │
  1396. │Label 2 │
  1397. │Label 3 │
  1398. │Label 4 │
  1399. │Label 5 │
  1400. │Label 6 │
  1401. │Label 7 │
  1402. │Label 8 │
  1403. │Label 9 │
  1404. │Label 10 │
  1405. │Label 10 │
  1406. └────────────────────┘",
  1407. @"
  1408. ┌────────────────────┐
  1409. │View with long text │
  1410. │Label 0 │
  1411. │Label 1 │
  1412. │Label 2 │
  1413. │Label 3 │
  1414. │Label 4 │
  1415. │Label 5 │
  1416. │Label 6 │
  1417. │Label 7 │
  1418. │Label 8 │
  1419. │Label 9 │
  1420. │Label 10 │
  1421. │Label 11 │
  1422. │Label 11 │
  1423. └────────────────────┘",
  1424. @"
  1425. ┌────────────────────┐
  1426. │View with long text │
  1427. │Label 0 │
  1428. │Label 1 │
  1429. │Label 2 │
  1430. │Label 3 │
  1431. │Label 4 │
  1432. │Label 5 │
  1433. │Label 6 │
  1434. │Label 7 │
  1435. │Label 8 │
  1436. │Label 9 │
  1437. │Label 10 │
  1438. │Label 11 │
  1439. │Label 12 │
  1440. │Label 12 │
  1441. └────────────────────┘",
  1442. @"
  1443. ┌────────────────────┐
  1444. │View with long text │
  1445. │Label 0 │
  1446. │Label 1 │
  1447. │Label 2 │
  1448. │Label 3 │
  1449. │Label 4 │
  1450. │Label 5 │
  1451. │Label 6 │
  1452. │Label 7 │
  1453. │Label 8 │
  1454. │Label 9 │
  1455. │Label 10 │
  1456. │Label 11 │
  1457. │Label 12 │
  1458. │Label 13 │
  1459. │Label 13 │
  1460. └────────────────────┘",
  1461. @"
  1462. ┌────────────────────┐
  1463. │View with long text │
  1464. │Label 0 │
  1465. │Label 1 │
  1466. │Label 2 │
  1467. │Label 3 │
  1468. │Label 4 │
  1469. │Label 5 │
  1470. │Label 6 │
  1471. │Label 7 │
  1472. │Label 8 │
  1473. │Label 9 │
  1474. │Label 10 │
  1475. │Label 11 │
  1476. │Label 12 │
  1477. │Label 13 │
  1478. │Label 14 │
  1479. │Label 14 │
  1480. └────────────────────┘",
  1481. @"
  1482. ┌────────────────────┐
  1483. │View with long text │
  1484. │Label 0 │
  1485. │Label 1 │
  1486. │Label 2 │
  1487. │Label 3 │
  1488. │Label 4 │
  1489. │Label 5 │
  1490. │Label 6 │
  1491. │Label 7 │
  1492. │Label 8 │
  1493. │Label 9 │
  1494. │Label 10 │
  1495. │Label 11 │
  1496. │Label 12 │
  1497. │Label 13 │
  1498. │Label 14 │
  1499. │Label 15 │
  1500. │Label 15 │
  1501. └────────────────────┘",
  1502. @"
  1503. ┌────────────────────┐
  1504. │View with long text │
  1505. │Label 0 │
  1506. │Label 1 │
  1507. │Label 2 │
  1508. │Label 3 │
  1509. │Label 4 │
  1510. │Label 5 │
  1511. │Label 6 │
  1512. │Label 7 │
  1513. │Label 8 │
  1514. │Label 9 │
  1515. │Label 10 │
  1516. │Label 11 │
  1517. │Label 12 │
  1518. │Label 13 │
  1519. │Label 14 │
  1520. │Label 15 │
  1521. │Label 16 │
  1522. │Label 16 │
  1523. └────────────────────┘",
  1524. @"
  1525. ┌────────────────────┐
  1526. │View with long text │
  1527. │Label 0 │
  1528. │Label 1 │
  1529. │Label 2 │
  1530. │Label 3 │
  1531. │Label 4 │
  1532. │Label 5 │
  1533. │Label 6 │
  1534. │Label 7 │
  1535. │Label 8 │
  1536. │Label 9 │
  1537. │Label 10 │
  1538. │Label 11 │
  1539. │Label 12 │
  1540. │Label 13 │
  1541. │Label 14 │
  1542. │Label 15 │
  1543. │Label 16 │
  1544. │Label 17 │
  1545. │Label 17 │
  1546. └────────────────────┘",
  1547. @"
  1548. ┌────────────────────┐
  1549. │View with long text │
  1550. │Label 0 │
  1551. │Label 1 │
  1552. │Label 2 │
  1553. │Label 3 │
  1554. │Label 4 │
  1555. │Label 5 │
  1556. │Label 6 │
  1557. │Label 7 │
  1558. │Label 8 │
  1559. │Label 9 │
  1560. │Label 10 │
  1561. │Label 11 │
  1562. │Label 12 │
  1563. │Label 13 │
  1564. │Label 14 │
  1565. │Label 15 │
  1566. │Label 16 │
  1567. │Label 17 │
  1568. │Label 18 │
  1569. │Label 18 │
  1570. └────────────────────┘",
  1571. @"
  1572. ┌────────────────────┐
  1573. │View with long text │
  1574. │Label 0 │
  1575. │Label 1 │
  1576. │Label 2 │
  1577. │Label 3 │
  1578. │Label 4 │
  1579. │Label 5 │
  1580. │Label 6 │
  1581. │Label 7 │
  1582. │Label 8 │
  1583. │Label 9 │
  1584. │Label 10 │
  1585. │Label 11 │
  1586. │Label 12 │
  1587. │Label 13 │
  1588. │Label 14 │
  1589. │Label 15 │
  1590. │Label 16 │
  1591. │Label 17 │
  1592. │Label 18 │
  1593. │Label 19 │
  1594. │Label 19 │
  1595. └────────────────────┘"
  1596. };
  1597. [Fact]
  1598. [AutoInitShutdown]
  1599. public void AutoSize_Dim_Add_Operator_With_Text ()
  1600. {
  1601. var top = Application.Top;
  1602. var view = new View ("View with long text") { X = 0, Y = 0, Width = 20, Height = 1 };
  1603. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  1604. var count = 0;
  1605. var listLabels = new List<Label> ();
  1606. field.KeyDown += (s, k) => {
  1607. if (k.KeyCode == KeyCode.Enter) {
  1608. ((FakeDriver)Application.Driver).SetBufferSize (22, count + 4);
  1609. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expecteds [count], _output);
  1610. Assert.Equal (new Rect (0, 0, 22, count + 4), pos);
  1611. if (count < 20) {
  1612. field.Text = $"Label {count}";
  1613. // Label is AutoSize = true
  1614. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 10 };
  1615. view.Add (label);
  1616. Assert.Equal ($"Label {count}", label.Text);
  1617. Assert.Equal ($"Absolute({count + 1})", label.Y.ToString ());
  1618. listLabels.Add (label);
  1619. //if (count == 0) {
  1620. // Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  1621. // view.Height += 2;
  1622. //} else {
  1623. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  1624. view.Height += 1;
  1625. //}
  1626. count++;
  1627. }
  1628. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  1629. }
  1630. };
  1631. Application.Iteration += (s, a) => {
  1632. while (count < 21) {
  1633. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  1634. if (count == 20) {
  1635. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  1636. break;
  1637. }
  1638. }
  1639. Application.RequestStop ();
  1640. };
  1641. var win = new Window ();
  1642. win.Add (view);
  1643. win.Add (field);
  1644. top.Add (win);
  1645. Application.Run (top);
  1646. Assert.Equal (20, count);
  1647. Assert.Equal (count, listLabels.Count);
  1648. }
  1649. [Fact]
  1650. [AutoInitShutdown]
  1651. public void AutoSize_Dim_Subtract_Operator_With_Text ()
  1652. {
  1653. var top = Application.Top;
  1654. // BUGBUG: v2 - If a View's height is zero, it should not be drawn.
  1655. //// Although view height is zero the text it's draw due the SetMinWidthHeight method
  1656. var view = new View ("View with long text") { X = 0, Y = 0, Width = 20, Height = 1 };
  1657. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  1658. var count = 20;
  1659. // Label is AutoSize = true
  1660. var listLabels = new List<Label> ();
  1661. for (var i = 0; i < count; i++) {
  1662. field.Text = $"Label {i}";
  1663. // BUGBUG: v2 - view has not been initialied yet; view.Bounds is indeterminate
  1664. var label = new Label (field.Text) { X = 0, Y = i + 1, Width = 10 };
  1665. view.Add (label);
  1666. Assert.Equal ($"Label {i}", label.Text);
  1667. // BUGBUG: Bogus test; views have not been initialized yet
  1668. //Assert.Equal ($"Absolute({i + 1})", label.Y.ToString ());
  1669. listLabels.Add (label);
  1670. //if (i == 0) {
  1671. // BUGBUG: Bogus test; views have not been initialized yet
  1672. //Assert.Equal ($"Absolute({i})", view.Height.ToString ());
  1673. //view.Height += 2;
  1674. // BUGBUG: Bogus test; views have not been initialized yet
  1675. //Assert.Equal ($"Absolute({i + 2})", view.Height.ToString ());
  1676. //} else {
  1677. // BUGBUG: Bogus test; views have not been initialized yet
  1678. //Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
  1679. view.Height += 1;
  1680. // BUGBUG: Bogus test; views have not been initialized yet
  1681. //Assert.Equal ($"Absolute({i + 2})", view.Height.ToString ());
  1682. //}
  1683. }
  1684. field.KeyDown += (s, k) => {
  1685. if (k.KeyCode == KeyCode.Enter) {
  1686. ((FakeDriver)Application.Driver).SetBufferSize (22, count + 4);
  1687. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expecteds [count], _output);
  1688. Assert.Equal (new Rect (0, 0, 22, count + 4), pos);
  1689. if (count > 0) {
  1690. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  1691. view.Remove (listLabels [count - 1]);
  1692. listLabels [count - 1].Dispose ();
  1693. listLabels.RemoveAt (count - 1);
  1694. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  1695. view.Height -= 1;
  1696. count--;
  1697. if (listLabels.Count > 0) {
  1698. field.Text = listLabels [count - 1].Text;
  1699. } else {
  1700. field.Text = string.Empty;
  1701. }
  1702. }
  1703. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  1704. }
  1705. };
  1706. Application.Iteration += (s, a) => {
  1707. while (count > -1) {
  1708. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  1709. if (count == 0) {
  1710. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  1711. break;
  1712. }
  1713. }
  1714. Application.RequestStop ();
  1715. };
  1716. var win = new Window ();
  1717. win.Add (view);
  1718. win.Add (field);
  1719. top.Add (win);
  1720. Application.Run (top);
  1721. Assert.Equal (0, count);
  1722. Assert.Equal (count, listLabels.Count);
  1723. }
  1724. [Fact]
  1725. [AutoInitShutdown]
  1726. public void AutoSize_AnchorEnd_Better_Than_Bottom_Equal_Inside_Window ()
  1727. {
  1728. var win = new Window ();
  1729. var label = new Label ("This should be the last line.") {
  1730. ColorScheme = Colors.Menu,
  1731. Width = Dim.Fill (),
  1732. X = 0, // keep unit test focused; don't use Center here
  1733. Y = Pos.AnchorEnd (1)
  1734. };
  1735. win.Add (label);
  1736. var top = Application.Top;
  1737. top.Add (win);
  1738. var rs = Application.Begin (top);
  1739. ((FakeDriver)Application.Driver).SetBufferSize (40, 10);
  1740. Assert.True (label.AutoSize);
  1741. Assert.Equal (29, label.Text.Length);
  1742. Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
  1743. Assert.Equal (new Rect (0, 0, 40, 10), win.Frame);
  1744. Assert.Equal (new Rect (0, 7, 38, 1), label.Frame);
  1745. string expected = @"
  1746. ┌──────────────────────────────────────┐
  1747. │ │
  1748. │ │
  1749. │ │
  1750. │ │
  1751. │ │
  1752. │ │
  1753. │ │
  1754. │This should be the last line. │
  1755. └──────────────────────────────────────┘
  1756. ";
  1757. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1758. Application.End (rs);
  1759. }
  1760. [Fact]
  1761. [AutoInitShutdown]
  1762. public void AutoSize_Bottom_Equal_Inside_Window ()
  1763. {
  1764. var win = new Window ();
  1765. var label = new Label ("This should be the last line.") {
  1766. ColorScheme = Colors.Menu,
  1767. Width = Dim.Fill (),
  1768. X = 0,
  1769. Y = Pos.Bottom (win) - 3 // two lines top and bottom borders more one line above the bottom border
  1770. };
  1771. win.Add (label);
  1772. var top = Application.Top;
  1773. top.Add (win);
  1774. var rs = Application.Begin (top);
  1775. ((FakeDriver)Application.Driver).SetBufferSize (40, 10);
  1776. Assert.True (label.AutoSize);
  1777. Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
  1778. Assert.Equal (new Rect (0, 0, 40, 10), win.Frame);
  1779. Assert.Equal (new Rect (0, 7, 38, 1), label.Frame);
  1780. string expected = @"
  1781. ┌──────────────────────────────────────┐
  1782. │ │
  1783. │ │
  1784. │ │
  1785. │ │
  1786. │ │
  1787. │ │
  1788. │ │
  1789. │This should be the last line. │
  1790. └──────────────────────────────────────┘
  1791. ";
  1792. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1793. Application.End (rs);
  1794. }
  1795. [Fact]
  1796. [AutoInitShutdown]
  1797. public void AutoSize_Bottom_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
  1798. {
  1799. var win = new Window ();
  1800. var label = new Label ("This should be the last line.") {
  1801. ColorScheme = Colors.Menu,
  1802. Width = Dim.Fill (),
  1803. X = 0,
  1804. Y = Pos.Bottom (win) - 4 // two lines top and bottom borders more two lines above border
  1805. };
  1806. win.Add (label);
  1807. var menu = new MenuBar (new MenuBarItem [] { new ("Menu", "", null) });
  1808. var status = new StatusBar (new StatusItem [] { new (KeyCode.F1, "~F1~ Help", null) });
  1809. var top = Application.Top;
  1810. top.Add (win, menu, status);
  1811. var rs = Application.Begin (top);
  1812. Assert.True (label.AutoSize);
  1813. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  1814. Assert.Equal (new Rect (0, 0, 80, 1), menu.Frame);
  1815. Assert.Equal (new Rect (0, 24, 80, 1), status.Frame);
  1816. Assert.Equal (new Rect (0, 1, 80, 23), win.Frame);
  1817. Assert.Equal (new Rect (0, 20, 78, 1), label.Frame);
  1818. string expected = @"
  1819. Menu
  1820. ┌──────────────────────────────────────────────────────────────────────────────┐
  1821. │ │
  1822. │ │
  1823. │ │
  1824. │ │
  1825. │ │
  1826. │ │
  1827. │ │
  1828. │ │
  1829. │ │
  1830. │ │
  1831. │ │
  1832. │ │
  1833. │ │
  1834. │ │
  1835. │ │
  1836. │ │
  1837. │ │
  1838. │ │
  1839. │ │
  1840. │ │
  1841. │This should be the last line. │
  1842. └──────────────────────────────────────────────────────────────────────────────┘
  1843. F1 Help
  1844. ";
  1845. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1846. Application.End (rs);
  1847. }
  1848. [Fact]
  1849. [AutoInitShutdown]
  1850. public void AutoSize_AnchorEnd_Better_Than_Bottom_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
  1851. {
  1852. var win = new Window ();
  1853. var label = new Label ("This should be the last line.") {
  1854. ColorScheme = Colors.Menu,
  1855. Width = Dim.Fill (),
  1856. X = 0,
  1857. Y = Pos.AnchorEnd (1)
  1858. };
  1859. win.Add (label);
  1860. var menu = new MenuBar (new MenuBarItem [] { new ("Menu", "", null) });
  1861. var status = new StatusBar (new StatusItem [] { new (KeyCode.F1, "~F1~ Help", null) });
  1862. var top = Application.Top;
  1863. top.Add (win, menu, status);
  1864. var rs = Application.Begin (top);
  1865. Assert.True (label.AutoSize);
  1866. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  1867. Assert.Equal (new Rect (0, 0, 80, 1), menu.Frame);
  1868. Assert.Equal (new Rect (0, 24, 80, 1), status.Frame);
  1869. Assert.Equal (new Rect (0, 1, 80, 23), win.Frame);
  1870. Assert.Equal (new Rect (0, 20, 78, 1), label.Frame);
  1871. string expected = @"
  1872. Menu
  1873. ┌──────────────────────────────────────────────────────────────────────────────┐
  1874. │ │
  1875. │ │
  1876. │ │
  1877. │ │
  1878. │ │
  1879. │ │
  1880. │ │
  1881. │ │
  1882. │ │
  1883. │ │
  1884. │ │
  1885. │ │
  1886. │ │
  1887. │ │
  1888. │ │
  1889. │ │
  1890. │ │
  1891. │ │
  1892. │ │
  1893. │ │
  1894. │This should be the last line. │
  1895. └──────────────────────────────────────────────────────────────────────────────┘
  1896. F1 Help
  1897. ";
  1898. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1899. Application.End (rs);
  1900. }
  1901. [Fact]
  1902. public void AutoSize_Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  1903. {
  1904. Application.Init (new FakeDriver ());
  1905. var t = Application.Top;
  1906. var w = new Window () {
  1907. X = Pos.Left (t) + 2,
  1908. Y = Pos.At (2)
  1909. };
  1910. // View is AutoSize = true
  1911. var v = new View () {
  1912. X = Pos.Center (),
  1913. Y = Pos.Percent (10)
  1914. };
  1915. w.Add (v);
  1916. t.Add (w);
  1917. t.Ready += (s, e) => {
  1918. v.LayoutStyle = LayoutStyle.Absolute;
  1919. Assert.Equal (2, v.X = 2);
  1920. Assert.Equal (2, v.Y = 2);
  1921. };
  1922. Application.Iteration += (s, a) => Application.RequestStop ();
  1923. Application.Run ();
  1924. Application.Shutdown ();
  1925. }
  1926. }