AutoSizeTextTests.cs 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440
  1. using System.Collections.Generic;
  2. using System.Text;
  3. using Xunit;
  4. using Xunit.Abstractions;
  5. namespace Terminal.Gui.ViewTests;
  6. /// <summary>
  7. /// Tests of the <see cref="View.AutoSize"/> property which auto sizes Views based on <see cref="Text"/>.
  8. /// </summary>
  9. public class AutoSizeTextTests {
  10. readonly ITestOutputHelper _output;
  11. readonly string [] expecteds = new string [21] {
  12. @"
  13. ┌────────────────────┐
  14. │View with long text │
  15. │ │
  16. └────────────────────┘",
  17. @"
  18. ┌────────────────────┐
  19. │View with long text │
  20. │Label 0 │
  21. │Label 0 │
  22. └────────────────────┘",
  23. @"
  24. ┌────────────────────┐
  25. │View with long text │
  26. │Label 0 │
  27. │Label 1 │
  28. │Label 1 │
  29. └────────────────────┘",
  30. @"
  31. ┌────────────────────┐
  32. │View with long text │
  33. │Label 0 │
  34. │Label 1 │
  35. │Label 2 │
  36. │Label 2 │
  37. └────────────────────┘",
  38. @"
  39. ┌────────────────────┐
  40. │View with long text │
  41. │Label 0 │
  42. │Label 1 │
  43. │Label 2 │
  44. │Label 3 │
  45. │Label 3 │
  46. └────────────────────┘",
  47. @"
  48. ┌────────────────────┐
  49. │View with long text │
  50. │Label 0 │
  51. │Label 1 │
  52. │Label 2 │
  53. │Label 3 │
  54. │Label 4 │
  55. │Label 4 │
  56. └────────────────────┘",
  57. @"
  58. ┌────────────────────┐
  59. │View with long text │
  60. │Label 0 │
  61. │Label 1 │
  62. │Label 2 │
  63. │Label 3 │
  64. │Label 4 │
  65. │Label 5 │
  66. │Label 5 │
  67. └────────────────────┘",
  68. @"
  69. ┌────────────────────┐
  70. │View with long text │
  71. │Label 0 │
  72. │Label 1 │
  73. │Label 2 │
  74. │Label 3 │
  75. │Label 4 │
  76. │Label 5 │
  77. │Label 6 │
  78. │Label 6 │
  79. └────────────────────┘",
  80. @"
  81. ┌────────────────────┐
  82. │View with long text │
  83. │Label 0 │
  84. │Label 1 │
  85. │Label 2 │
  86. │Label 3 │
  87. │Label 4 │
  88. │Label 5 │
  89. │Label 6 │
  90. │Label 7 │
  91. │Label 7 │
  92. └────────────────────┘",
  93. @"
  94. ┌────────────────────┐
  95. │View with long text │
  96. │Label 0 │
  97. │Label 1 │
  98. │Label 2 │
  99. │Label 3 │
  100. │Label 4 │
  101. │Label 5 │
  102. │Label 6 │
  103. │Label 7 │
  104. │Label 8 │
  105. │Label 8 │
  106. └────────────────────┘",
  107. @"
  108. ┌────────────────────┐
  109. │View with long text │
  110. │Label 0 │
  111. │Label 1 │
  112. │Label 2 │
  113. │Label 3 │
  114. │Label 4 │
  115. │Label 5 │
  116. │Label 6 │
  117. │Label 7 │
  118. │Label 8 │
  119. │Label 9 │
  120. │Label 9 │
  121. └────────────────────┘",
  122. @"
  123. ┌────────────────────┐
  124. │View with long text │
  125. │Label 0 │
  126. │Label 1 │
  127. │Label 2 │
  128. │Label 3 │
  129. │Label 4 │
  130. │Label 5 │
  131. │Label 6 │
  132. │Label 7 │
  133. │Label 8 │
  134. │Label 9 │
  135. │Label 10 │
  136. │Label 10 │
  137. └────────────────────┘",
  138. @"
  139. ┌────────────────────┐
  140. │View with long text │
  141. │Label 0 │
  142. │Label 1 │
  143. │Label 2 │
  144. │Label 3 │
  145. │Label 4 │
  146. │Label 5 │
  147. │Label 6 │
  148. │Label 7 │
  149. │Label 8 │
  150. │Label 9 │
  151. │Label 10 │
  152. │Label 11 │
  153. │Label 11 │
  154. └────────────────────┘",
  155. @"
  156. ┌────────────────────┐
  157. │View with long text │
  158. │Label 0 │
  159. │Label 1 │
  160. │Label 2 │
  161. │Label 3 │
  162. │Label 4 │
  163. │Label 5 │
  164. │Label 6 │
  165. │Label 7 │
  166. │Label 8 │
  167. │Label 9 │
  168. │Label 10 │
  169. │Label 11 │
  170. │Label 12 │
  171. │Label 12 │
  172. └────────────────────┘",
  173. @"
  174. ┌────────────────────┐
  175. │View with long text │
  176. │Label 0 │
  177. │Label 1 │
  178. │Label 2 │
  179. │Label 3 │
  180. │Label 4 │
  181. │Label 5 │
  182. │Label 6 │
  183. │Label 7 │
  184. │Label 8 │
  185. │Label 9 │
  186. │Label 10 │
  187. │Label 11 │
  188. │Label 12 │
  189. │Label 13 │
  190. │Label 13 │
  191. └────────────────────┘",
  192. @"
  193. ┌────────────────────┐
  194. │View with long text │
  195. │Label 0 │
  196. │Label 1 │
  197. │Label 2 │
  198. │Label 3 │
  199. │Label 4 │
  200. │Label 5 │
  201. │Label 6 │
  202. │Label 7 │
  203. │Label 8 │
  204. │Label 9 │
  205. │Label 10 │
  206. │Label 11 │
  207. │Label 12 │
  208. │Label 13 │
  209. │Label 14 │
  210. │Label 14 │
  211. └────────────────────┘",
  212. @"
  213. ┌────────────────────┐
  214. │View with long text │
  215. │Label 0 │
  216. │Label 1 │
  217. │Label 2 │
  218. │Label 3 │
  219. │Label 4 │
  220. │Label 5 │
  221. │Label 6 │
  222. │Label 7 │
  223. │Label 8 │
  224. │Label 9 │
  225. │Label 10 │
  226. │Label 11 │
  227. │Label 12 │
  228. │Label 13 │
  229. │Label 14 │
  230. │Label 15 │
  231. │Label 15 │
  232. └────────────────────┘",
  233. @"
  234. ┌────────────────────┐
  235. │View with long text │
  236. │Label 0 │
  237. │Label 1 │
  238. │Label 2 │
  239. │Label 3 │
  240. │Label 4 │
  241. │Label 5 │
  242. │Label 6 │
  243. │Label 7 │
  244. │Label 8 │
  245. │Label 9 │
  246. │Label 10 │
  247. │Label 11 │
  248. │Label 12 │
  249. │Label 13 │
  250. │Label 14 │
  251. │Label 15 │
  252. │Label 16 │
  253. │Label 16 │
  254. └────────────────────┘",
  255. @"
  256. ┌────────────────────┐
  257. │View with long text │
  258. │Label 0 │
  259. │Label 1 │
  260. │Label 2 │
  261. │Label 3 │
  262. │Label 4 │
  263. │Label 5 │
  264. │Label 6 │
  265. │Label 7 │
  266. │Label 8 │
  267. │Label 9 │
  268. │Label 10 │
  269. │Label 11 │
  270. │Label 12 │
  271. │Label 13 │
  272. │Label 14 │
  273. │Label 15 │
  274. │Label 16 │
  275. │Label 17 │
  276. │Label 17 │
  277. └────────────────────┘",
  278. @"
  279. ┌────────────────────┐
  280. │View with long text │
  281. │Label 0 │
  282. │Label 1 │
  283. │Label 2 │
  284. │Label 3 │
  285. │Label 4 │
  286. │Label 5 │
  287. │Label 6 │
  288. │Label 7 │
  289. │Label 8 │
  290. │Label 9 │
  291. │Label 10 │
  292. │Label 11 │
  293. │Label 12 │
  294. │Label 13 │
  295. │Label 14 │
  296. │Label 15 │
  297. │Label 16 │
  298. │Label 17 │
  299. │Label 18 │
  300. │Label 18 │
  301. └────────────────────┘",
  302. @"
  303. ┌────────────────────┐
  304. │View with long text │
  305. │Label 0 │
  306. │Label 1 │
  307. │Label 2 │
  308. │Label 3 │
  309. │Label 4 │
  310. │Label 5 │
  311. │Label 6 │
  312. │Label 7 │
  313. │Label 8 │
  314. │Label 9 │
  315. │Label 10 │
  316. │Label 11 │
  317. │Label 12 │
  318. │Label 13 │
  319. │Label 14 │
  320. │Label 15 │
  321. │Label 16 │
  322. │Label 17 │
  323. │Label 18 │
  324. │Label 19 │
  325. │Label 19 │
  326. └────────────────────┘"
  327. };
  328. public AutoSizeTextTests (ITestOutputHelper output) => _output = output;
  329. [Fact]
  330. [AutoInitShutdown]
  331. public void AutoSize_GetAutoSize_Horizontal ()
  332. {
  333. var text = "text";
  334. var view = new View {
  335. Text = text,
  336. AutoSize = true
  337. };
  338. var win = new Window {
  339. Width = Dim.Fill (),
  340. Height = Dim.Fill ()
  341. };
  342. win.Add (view);
  343. Application.Top.Add (win);
  344. Application.Begin (Application.Top);
  345. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  346. var size = view.GetAutoSize ();
  347. Assert.Equal (new Size (text.Length, 1), size);
  348. view.Text = $"{text}\n{text}";
  349. size = view.GetAutoSize ();
  350. Assert.Equal (new Size (text.Length, 2), size);
  351. view.Text = $"{text}\n{text}\n{text}+";
  352. size = view.GetAutoSize ();
  353. Assert.Equal (new Size (text.Length + 1, 3), size);
  354. text = string.Empty;
  355. view.Text = text;
  356. size = view.GetAutoSize ();
  357. Assert.Equal (new Size (0, 0), size);
  358. text = "1";
  359. view.Text = text;
  360. size = view.GetAutoSize ();
  361. Assert.Equal (new Size (1, 1), size);
  362. text = "界";
  363. view.Text = text;
  364. size = view.GetAutoSize ();
  365. Assert.Equal (new Size (2, 1), size);
  366. }
  367. [Fact]
  368. [AutoInitShutdown]
  369. public void AutoSize_GetAutoSize_Vertical ()
  370. {
  371. var text = "text";
  372. var view = new View {
  373. Text = text,
  374. TextDirection = TextDirection.TopBottom_LeftRight,
  375. AutoSize = true
  376. };
  377. var win = new Window {
  378. Width = Dim.Fill (),
  379. Height = Dim.Fill ()
  380. };
  381. win.Add (view);
  382. Application.Top.Add (win);
  383. Application.Begin (Application.Top);
  384. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  385. var size = view.GetAutoSize ();
  386. Assert.Equal (new Size (1, text.Length), size);
  387. view.Text = $"{text}\n{text}";
  388. size = view.GetAutoSize ();
  389. Assert.Equal (new Size (2, text.Length), size);
  390. view.Text = $"{text}\n{text}\n{text}+";
  391. size = view.GetAutoSize ();
  392. Assert.Equal (new Size (3, text.Length + 1), size);
  393. text = string.Empty;
  394. view.Text = text;
  395. size = view.GetAutoSize ();
  396. Assert.Equal (new Size (0, 0), size);
  397. text = "1";
  398. view.Text = text;
  399. size = view.GetAutoSize ();
  400. Assert.Equal (new Size (1, 1), size);
  401. text = "界";
  402. view.Text = text;
  403. size = view.GetAutoSize ();
  404. Assert.Equal (new Size (2, 1), size);
  405. }
  406. [Fact]
  407. [AutoInitShutdown]
  408. public void AutoSize_GetAutoSize_Left ()
  409. {
  410. var text = "This is some text.";
  411. var view = new View {
  412. Text = text,
  413. TextAlignment = TextAlignment.Left,
  414. AutoSize = true
  415. };
  416. var win = new Window {
  417. Width = Dim.Fill (),
  418. Height = Dim.Fill ()
  419. };
  420. win.Add (view);
  421. Application.Top.Add (win);
  422. Application.Begin (Application.Top);
  423. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  424. var size = view.GetAutoSize ();
  425. Assert.Equal (new Size (text.Length, 1), size);
  426. view.Text = $"{text}\n{text}";
  427. size = view.GetAutoSize ();
  428. Assert.Equal (new Size (text.Length, 2), size);
  429. view.Text = $"{text}\n{text}\n{text}+";
  430. size = view.GetAutoSize ();
  431. Assert.Equal (new Size (text.Length + 1, 3), size);
  432. text = string.Empty;
  433. view.Text = text;
  434. size = view.GetAutoSize ();
  435. Assert.Equal (new Size (0, 0), size);
  436. text = "1";
  437. view.Text = text;
  438. size = view.GetAutoSize ();
  439. Assert.Equal (new Size (1, 1), size);
  440. text = "界";
  441. view.Text = text;
  442. size = view.GetAutoSize ();
  443. Assert.Equal (new Size (2, 1), size);
  444. }
  445. [Fact]
  446. [AutoInitShutdown]
  447. public void AutoSize_GetAutoSize_Right ()
  448. {
  449. var text = "This is some text.";
  450. var view = new View {
  451. Text = text,
  452. TextAlignment = TextAlignment.Right,
  453. AutoSize = true
  454. };
  455. var win = new Window {
  456. Width = Dim.Fill (),
  457. Height = Dim.Fill ()
  458. };
  459. win.Add (view);
  460. Application.Top.Add (win);
  461. Application.Begin (Application.Top);
  462. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  463. var size = view.GetAutoSize ();
  464. Assert.Equal (new Size (text.Length, 1), size);
  465. view.Text = $"{text}\n{text}";
  466. size = view.GetAutoSize ();
  467. Assert.Equal (new Size (text.Length, 2), size);
  468. view.Text = $"{text}\n{text}\n{text}+";
  469. size = view.GetAutoSize ();
  470. Assert.Equal (new Size (text.Length + 1, 3), size);
  471. text = string.Empty;
  472. view.Text = text;
  473. size = view.GetAutoSize ();
  474. Assert.Equal (new Size (0, 0), size);
  475. text = "1";
  476. view.Text = text;
  477. size = view.GetAutoSize ();
  478. Assert.Equal (new Size (1, 1), size);
  479. text = "界";
  480. view.Text = text;
  481. size = view.GetAutoSize ();
  482. Assert.Equal (new Size (2, 1), size);
  483. }
  484. [Fact]
  485. [AutoInitShutdown]
  486. public void AutoSize_GetAutoSize_Centered ()
  487. {
  488. var text = "This is some text.";
  489. var view = new View {
  490. Text = text,
  491. TextAlignment = TextAlignment.Centered,
  492. AutoSize = true
  493. };
  494. var win = new Window {
  495. Width = Dim.Fill (),
  496. Height = Dim.Fill ()
  497. };
  498. win.Add (view);
  499. Application.Top.Add (win);
  500. Application.Begin (Application.Top);
  501. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  502. var size = view.GetAutoSize ();
  503. Assert.Equal (new Size (text.Length, 1), size);
  504. view.Text = $"{text}\n{text}";
  505. size = view.GetAutoSize ();
  506. Assert.Equal (new Size (text.Length, 2), size);
  507. view.Text = $"{text}\n{text}\n{text}+";
  508. size = view.GetAutoSize ();
  509. Assert.Equal (new Size (text.Length + 1, 3), size);
  510. text = string.Empty;
  511. view.Text = text;
  512. size = view.GetAutoSize ();
  513. Assert.Equal (new Size (0, 0), size);
  514. text = "1";
  515. view.Text = text;
  516. size = view.GetAutoSize ();
  517. Assert.Equal (new Size (1, 1), size);
  518. text = "界";
  519. view.Text = text;
  520. size = view.GetAutoSize ();
  521. Assert.Equal (new Size (2, 1), size);
  522. }
  523. [Fact]
  524. [AutoInitShutdown]
  525. public void AutoSize_True_Label_IsEmpty_False_Never_Return_Null_Lines ()
  526. {
  527. var text = "Label";
  528. var label = new Label {
  529. Width = Dim.Fill () - text.Length,
  530. Height = 1,
  531. Text = text
  532. };
  533. var win = new Window {
  534. Width = Dim.Fill (),
  535. Height = Dim.Fill ()
  536. };
  537. win.Add (label);
  538. Application.Top.Add (win);
  539. Application.Begin (Application.Top);
  540. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  541. Assert.Equal (5, text.Length);
  542. Assert.True (label.AutoSize);
  543. Assert.Equal (new Rect (0, 0, 5, 1), label.Frame);
  544. Assert.Equal (new Size (5, 1), label.TextFormatter.Size);
  545. Assert.Equal (new List<string> { "Label" }, label.TextFormatter.Lines);
  546. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  547. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  548. var expected = @"
  549. ┌────────┐
  550. │Label │
  551. │ │
  552. └────────┘
  553. ";
  554. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  555. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  556. text = "0123456789";
  557. Assert.Equal (10, text.Length);
  558. label.Width = Dim.Fill () - text.Length;
  559. Application.Refresh ();
  560. Assert.True (label.AutoSize);
  561. Assert.Equal (new Rect (0, 0, 5, 1), label.Frame);
  562. Assert.Equal (new Size (5, 1), label.TextFormatter.Size);
  563. Assert.Single (label.TextFormatter.Lines);
  564. expected = @"
  565. ┌────────┐
  566. │Label │
  567. │ │
  568. └────────┘
  569. ";
  570. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  571. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  572. }
  573. [Fact]
  574. [AutoInitShutdown]
  575. public void AutoSize_True_Label_IsEmpty_False_Minimum_Height ()
  576. {
  577. var text = "Label";
  578. var label = new Label {
  579. Width = Dim.Fill () - text.Length,
  580. Text = text
  581. };
  582. var win = new Window {
  583. Width = Dim.Fill (),
  584. Height = Dim.Fill ()
  585. };
  586. win.Add (label);
  587. Application.Top.Add (win);
  588. Application.Begin (Application.Top);
  589. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  590. Assert.Equal (5, text.Length);
  591. Assert.True (label.AutoSize);
  592. Assert.Equal (new Rect (0, 0, 5, 1), label.Frame);
  593. Assert.Equal (new Size (5, 1), label.TextFormatter.Size);
  594. Assert.Equal (new List<string> { "Label" }, label.TextFormatter.Lines);
  595. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  596. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  597. var expected = @"
  598. ┌────────┐
  599. │Label │
  600. │ │
  601. └────────┘
  602. ";
  603. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  604. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  605. text = "0123456789";
  606. Assert.Equal (10, text.Length);
  607. label.Width = Dim.Fill () - text.Length;
  608. Application.Refresh ();
  609. Assert.Equal (new Rect (0, 0, 5, 1), label.Frame);
  610. Assert.Equal (new Size (5, 1), label.TextFormatter.Size);
  611. var exception = Record.Exception (() => Assert.Single (label.TextFormatter.Lines));
  612. Assert.Null (exception);
  613. expected = @"
  614. ┌────────┐
  615. │Label │
  616. │ │
  617. └────────┘
  618. ";
  619. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  620. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  621. }
  622. [Fact]
  623. [AutoInitShutdown]
  624. public void AutoSize_True_View_IsEmpty_False_Minimum_Width ()
  625. {
  626. var text = "Views";
  627. var view = new View {
  628. TextDirection = TextDirection.TopBottom_LeftRight,
  629. Height = Dim.Fill () - text.Length,
  630. Text = text,
  631. AutoSize = true
  632. };
  633. var win = new Window {
  634. Width = Dim.Fill (),
  635. Height = Dim.Fill ()
  636. };
  637. win.Add (view);
  638. Application.Top.Add (win);
  639. Application.Begin (Application.Top);
  640. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  641. Assert.Equal (5, text.Length);
  642. Assert.True (view.AutoSize);
  643. Assert.Equal (new Rect (0, 0, 1, 5), view.Frame);
  644. Assert.Equal (new Size (1, 5), view.TextFormatter.Size);
  645. Assert.Equal (new List<string> { "Views" }, view.TextFormatter.Lines);
  646. Assert.Equal (new Rect (0, 0, 4, 10), win.Frame);
  647. Assert.Equal (new Rect (0, 0, 4, 10), Application.Top.Frame);
  648. var expected = @"
  649. ┌──┐
  650. │V │
  651. │i │
  652. │e │
  653. │w │
  654. │s │
  655. │ │
  656. │ │
  657. │ │
  658. └──┘
  659. ";
  660. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  661. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  662. text = "0123456789";
  663. Assert.Equal (10, text.Length);
  664. view.Height = Dim.Fill () - text.Length;
  665. Application.Refresh ();
  666. Assert.Equal (new Rect (0, 0, 1, 5), view.Frame);
  667. Assert.Equal (new Size (1, 5), view.TextFormatter.Size);
  668. var exception = Record.Exception (() => Assert.Single (view.TextFormatter.Lines));
  669. Assert.Null (exception);
  670. expected = @"
  671. ┌──┐
  672. │V │
  673. │i │
  674. │e │
  675. │w │
  676. │s │
  677. │ │
  678. │ │
  679. │ │
  680. └──┘
  681. ";
  682. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  683. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  684. }
  685. [Fact]
  686. [AutoInitShutdown]
  687. public void AutoSize_True_View_IsEmpty_False_Minimum_Width_Wide_Rune ()
  688. {
  689. var text = "界View";
  690. var view = new View {
  691. TextDirection = TextDirection.TopBottom_LeftRight,
  692. Height = Dim.Fill () - text.Length,
  693. Text = text,
  694. AutoSize = true
  695. };
  696. var win = new Window {
  697. Width = Dim.Fill (),
  698. Height = Dim.Fill ()
  699. };
  700. win.Add (view);
  701. Application.Top.Add (win);
  702. Application.Begin (Application.Top);
  703. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  704. Assert.Equal (5, text.Length);
  705. Assert.True (view.AutoSize);
  706. Assert.Equal (new Rect (0, 0, 2, 5), view.Frame);
  707. Assert.Equal (new Size (2, 5), view.TextFormatter.Size);
  708. Assert.Equal (new List<string> { "界View" }, view.TextFormatter.Lines);
  709. Assert.Equal (new Rect (0, 0, 4, 10), win.Frame);
  710. Assert.Equal (new Rect (0, 0, 4, 10), Application.Top.Frame);
  711. var expected = @"
  712. ┌──┐
  713. │界│
  714. │V │
  715. │i │
  716. │e │
  717. │w │
  718. │ │
  719. │ │
  720. │ │
  721. └──┘
  722. ";
  723. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  724. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  725. text = "0123456789";
  726. Assert.Equal (10, text.Length);
  727. view.Height = Dim.Fill () - text.Length;
  728. Application.Refresh ();
  729. Assert.Equal (new Rect (0, 0, 2, 5), view.Frame);
  730. Assert.Equal (new Size (2, 5), view.TextFormatter.Size);
  731. var exception = Record.Exception (() => Assert.Equal (new List<string> { "界View" }, view.TextFormatter.Lines));
  732. Assert.Null (exception);
  733. expected = @"
  734. ┌──┐
  735. │界│
  736. │V │
  737. │i │
  738. │e │
  739. │w │
  740. │ │
  741. │ │
  742. │ │
  743. └──┘
  744. ";
  745. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  746. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  747. }
  748. [Fact]
  749. public void AutoSize_True_Label_If_Text_Emmpty ()
  750. {
  751. var label1 = new Label ();
  752. var label2 = new Label ("");
  753. var label3 = new Label { Text = "" };
  754. Assert.True (label1.AutoSize);
  755. Assert.True (label2.AutoSize);
  756. Assert.True (label3.AutoSize);
  757. label1.Dispose ();
  758. label2.Dispose ();
  759. label3.Dispose ();
  760. }
  761. [Fact]
  762. public void AutoSize_True_Label_If_Text_Is_Not_Emmpty ()
  763. {
  764. var label1 = new Label ();
  765. label1.Text = "Hello World";
  766. var label2 = new Label ("Hello World");
  767. var label3 = new Label { Text = "Hello World" };
  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_ResizeView_With_Dim_Absolute ()
  777. {
  778. var super = new View ();
  779. var label = new Label ();
  780. label.Text = "New text";
  781. // BUGBUG: v2 - label was never added to super, so it was never laid out.
  782. super.Add (label);
  783. super.LayoutSubviews ();
  784. Assert.True (label.AutoSize);
  785. Assert.Equal ("(0,0,8,1)", label.Bounds.ToString ());
  786. super.Dispose ();
  787. }
  788. [Fact]
  789. [AutoInitShutdown]
  790. public void AutoSize_True_Setting_With_Height_Horizontal ()
  791. {
  792. var label = new Label ("Hello") { Width = 10, Height = 2 };
  793. var viewX = new View ("X") { X = Pos.Right (label) };
  794. var viewY = new View ("Y") { Y = Pos.Bottom (label) };
  795. Application.Top.Add (label, viewX, viewY);
  796. var rs = Application.Begin (Application.Top);
  797. Assert.True (label.AutoSize);
  798. Assert.Equal (new Rect (0, 0, 10, 2), label.Frame);
  799. var expected = @"
  800. Hello X
  801. Y
  802. "
  803. ;
  804. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  805. Assert.Equal (new Rect (0, 0, 11, 3), pos);
  806. label.AutoSize = false;
  807. Application.Refresh ();
  808. Assert.False (label.AutoSize);
  809. Assert.Equal (new Rect (0, 0, 10, 2), label.Frame);
  810. expected = @"
  811. Hello X
  812. Y
  813. "
  814. ;
  815. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  816. Assert.Equal (new Rect (0, 0, 11, 3), pos);
  817. Application.End (rs);
  818. }
  819. [Fact]
  820. [AutoInitShutdown]
  821. public void AutoSize_True_Setting_With_Height_Vertical ()
  822. {
  823. var label = new Label () { Width = 2, Height = 10, TextDirection = TextDirection.TopBottom_LeftRight };
  824. var viewX = new View ("X") { X = Pos.Right (label) };
  825. var viewY = new View ("Y") { Y = Pos.Bottom (label) };
  826. Application.Top.Add (label, viewX, viewY);
  827. var rs = Application.Begin (Application.Top);
  828. Assert.True (label.AutoSize);
  829. label.Text = "Hello";
  830. Application.Refresh ();
  831. // #3127: Label.Text is "Hello" - It's Vertical. So the width should be 2 (honoring Width = 2)
  832. // and the height is should be 10 (because 10 is greater than length of Hello).
  833. Assert.Equal (new Rect (0, 0, 2, 10), label.Frame);
  834. var expected = @"
  835. H X
  836. e
  837. l
  838. l
  839. o
  840. Y
  841. ";
  842. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  843. Assert.Equal (new Rect (0, 0, 3, 11), pos);
  844. label.AutoSize = false;
  845. Application.Refresh ();
  846. Assert.False (label.AutoSize);
  847. Assert.Equal (new Rect (0, 0, 2, 10), label.Frame);
  848. expected = @"
  849. H X
  850. e
  851. l
  852. l
  853. o
  854. Y
  855. "
  856. ;
  857. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  858. Assert.Equal (new Rect (0, 0, 3, 11), pos);
  859. Application.End (rs);
  860. }
  861. [Fact]
  862. [AutoInitShutdown]
  863. public void Excess_Text_Is_Erased_When_The_Width_Is_Reduced ()
  864. {
  865. var lbl = new Label ("123");
  866. Application.Top.Add (lbl);
  867. var rs = Application.Begin (Application.Top);
  868. Assert.True (lbl.AutoSize);
  869. Assert.Equal ("123 ", GetContents ());
  870. lbl.Text = "12";
  871. // Here the AutoSize ensuring the right size with width 3 (Dim.Absolute)
  872. // that was set on the OnAdded method with the text length of 3
  873. // and height 1 because wasn't set and the text has 1 line
  874. Assert.Equal (new Rect (0, 0, 3, 1), lbl.Frame);
  875. Assert.Equal (new Rect (0, 0, 3, 1), lbl._needsDisplayRect);
  876. Assert.Equal (new Rect (0, 0, 0, 0), lbl.SuperView._needsDisplayRect);
  877. Assert.True (lbl.SuperView.LayoutNeeded);
  878. lbl.SuperView.Draw ();
  879. Assert.Equal ("12 ", GetContents ());
  880. string GetContents ()
  881. {
  882. var text = "";
  883. for (var i = 0; i < 4; i++) {
  884. text += Application.Driver.Contents [0, i].Rune;
  885. }
  886. return text;
  887. }
  888. Application.End (rs);
  889. }
  890. [Fact]
  891. [AutoInitShutdown]
  892. public void AutoSize_True_Equal_Before_And_After_IsInitialized_With_Different_Orders ()
  893. {
  894. var view1 = new View { Text = "Say Hello view1 你", AutoSize = true, Width = 10, Height = 5 };
  895. var view2 = new View { Text = "Say Hello view2 你", Width = 10, Height = 5, AutoSize = true };
  896. var view3 = new View { AutoSize = true, Width = 10, Height = 5, Text = "Say Hello view3 你" };
  897. var view4 = new View {
  898. Text = "Say Hello view4 你",
  899. AutoSize = true,
  900. Width = 10,
  901. Height = 5,
  902. TextDirection = TextDirection.TopBottom_LeftRight
  903. };
  904. var view5 = new View {
  905. Text = "Say Hello view5 你",
  906. Width = 10,
  907. Height = 5,
  908. AutoSize = true,
  909. TextDirection = TextDirection.TopBottom_LeftRight
  910. };
  911. var view6 = new View {
  912. AutoSize = true,
  913. Width = 10,
  914. Height = 5,
  915. TextDirection = TextDirection.TopBottom_LeftRight,
  916. Text = "Say Hello view6 你"
  917. };
  918. Application.Top.Add (view1, view2, view3, view4, view5, view6);
  919. Assert.False (view1.IsInitialized);
  920. Assert.False (view2.IsInitialized);
  921. Assert.False (view3.IsInitialized);
  922. Assert.False (view4.IsInitialized);
  923. Assert.False (view5.IsInitialized);
  924. Assert.True (view1.AutoSize);
  925. Assert.Equal (new Rect (0, 0, 18, 5), view1.Frame);
  926. Assert.Equal ("Absolute(18)", view1.Width.ToString ());
  927. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  928. Assert.True (view2.AutoSize);
  929. Assert.Equal ("Say Hello view2 你".GetColumns (), view2.Width);
  930. Assert.Equal (18, view2.Width);
  931. Assert.Equal (new Rect (0, 0, 18, 5), view2.Frame);
  932. Assert.Equal ("Absolute(18)", view2.Width.ToString ());
  933. Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  934. Assert.True (view3.AutoSize);
  935. Assert.Equal (new Rect (0, 0, 18, 5), view3.Frame);
  936. Assert.Equal ("Absolute(18)", view2.Width.ToString ());
  937. Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  938. Assert.True (view4.AutoSize);
  939. Assert.Equal ("Say Hello view4 你".GetColumns (), view2.Width);
  940. Assert.Equal (18, view2.Width);
  941. Assert.Equal (new Rect (0, 0, 18, 17), view4.Frame);
  942. Assert.Equal ("Absolute(18)", view4.Width.ToString ());
  943. Assert.Equal ("Absolute(17)", view4.Height.ToString ());
  944. Assert.True (view5.AutoSize);
  945. Assert.Equal (new Rect (0, 0, 18, 17), view5.Frame);
  946. Assert.True (view6.AutoSize);
  947. Assert.Equal (new Rect (0, 0, 10, 17), view6.Frame);
  948. var rs = Application.Begin (Application.Top);
  949. Assert.True (view1.IsInitialized);
  950. Assert.True (view2.IsInitialized);
  951. Assert.True (view3.IsInitialized);
  952. Assert.True (view4.IsInitialized);
  953. Assert.True (view5.IsInitialized);
  954. Assert.True (view1.AutoSize);
  955. Assert.Equal (new Rect (0, 0, 18, 5), view1.Frame);
  956. Assert.Equal ("Absolute(18)", view1.Width.ToString ());
  957. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  958. Assert.True (view2.AutoSize);
  959. // BUGBUG: v2 - Autosize is broken when setting Width/Height AutoSize. Disabling test for now.
  960. //Assert.Equal (new Rect (0, 0, 18, 5), view2.Frame);
  961. //Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  962. //Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  963. //Assert.True (view3.AutoSize);
  964. //Assert.Equal (new Rect (0, 0, 18, 5), view3.Frame);
  965. //Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  966. //Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  967. //Assert.True (view4.AutoSize);
  968. //Assert.Equal (new Rect (0, 0, 10, 17), view4.Frame);
  969. //Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  970. //Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  971. //Assert.True (view5.AutoSize);
  972. //Assert.Equal (new Rect (0, 0, 10, 17), view5.Frame);
  973. //Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  974. //Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  975. //Assert.True (view6.AutoSize);
  976. //Assert.Equal (new Rect (0, 0, 10, 17), view6.Frame);
  977. //Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  978. //Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  979. Application.End (rs);
  980. }
  981. [Fact]
  982. public void SetRelativeLayout_Respects_AutoSize ()
  983. {
  984. var view = new View (new Rect (0, 0, 10, 0)) {
  985. AutoSize = true
  986. };
  987. view.Text = "01234567890123456789";
  988. Assert.True (view.AutoSize);
  989. Assert.Equal (LayoutStyle.Absolute, view.LayoutStyle);
  990. Assert.Equal (new Rect (0, 0, 20, 1), view.Frame);
  991. Assert.Equal ("Absolute(0)", view.X.ToString ());
  992. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  993. Assert.Equal ("Absolute(20)", view.Width.ToString ());
  994. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  995. view.SetRelativeLayout (new Rect (0, 0, 25, 5));
  996. Assert.True (view.AutoSize);
  997. Assert.Equal (LayoutStyle.Absolute, view.LayoutStyle);
  998. Assert.Equal (new Rect (0, 0, 20, 1), view.Frame);
  999. Assert.Equal ("Absolute(0)", view.X.ToString ());
  1000. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1001. Assert.Equal ("Absolute(20)", view.Width.ToString ());
  1002. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  1003. }
  1004. [Fact]
  1005. [AutoInitShutdown]
  1006. public void Setting_Frame_Dont_Respect_AutoSize_True_On_Layout_Absolute ()
  1007. {
  1008. var view1 = new View (new Rect (0, 0, 10, 0)) {
  1009. Text = "Say Hello view1 你",
  1010. AutoSize = true
  1011. };
  1012. var viewTopBottom_LeftRight = new View (new Rect (0, 0, 0, 10)) {
  1013. Text = "Say Hello view2 你",
  1014. AutoSize = true,
  1015. TextDirection = TextDirection.TopBottom_LeftRight
  1016. };
  1017. Application.Top.Add (view1, viewTopBottom_LeftRight);
  1018. var rs = Application.Begin (Application.Top);
  1019. Assert.True (view1.AutoSize);
  1020. Assert.Equal (LayoutStyle.Absolute, view1.LayoutStyle);
  1021. Assert.Equal (new Rect (0, 0, 18, 1), view1.Frame);
  1022. Assert.Equal ("Absolute(0)", view1.X.ToString ());
  1023. Assert.Equal ("Absolute(0)", view1.Y.ToString ());
  1024. Assert.Equal ("Absolute(18)", view1.Width.ToString ());
  1025. Assert.Equal ("Absolute(1)", view1.Height.ToString ());
  1026. Assert.True (viewTopBottom_LeftRight.AutoSize);
  1027. Assert.Equal (LayoutStyle.Absolute, viewTopBottom_LeftRight.LayoutStyle);
  1028. Assert.Equal (new Rect (0, 0, 18, 17), viewTopBottom_LeftRight.Frame);
  1029. Assert.Equal ("Absolute(0)", viewTopBottom_LeftRight.X.ToString ());
  1030. Assert.Equal ("Absolute(0)", viewTopBottom_LeftRight.Y.ToString ());
  1031. Assert.Equal ("Absolute(18)", viewTopBottom_LeftRight.Width.ToString ());
  1032. Assert.Equal ("Absolute(17)", viewTopBottom_LeftRight.Height.ToString ());
  1033. view1.Frame = new Rect (0, 0, 25, 4);
  1034. var firstIteration = false;
  1035. Application.RunIteration (ref rs, ref firstIteration);
  1036. Assert.True (view1.AutoSize);
  1037. Assert.Equal (LayoutStyle.Absolute, view1.LayoutStyle);
  1038. Assert.Equal (new Rect (0, 0, 25, 4), view1.Frame);
  1039. Assert.Equal ("Absolute(0)", view1.X.ToString ());
  1040. Assert.Equal ("Absolute(0)", view1.Y.ToString ());
  1041. Assert.Equal ("Absolute(25)", view1.Width.ToString ());
  1042. Assert.Equal ("Absolute(4)", view1.Height.ToString ());
  1043. viewTopBottom_LeftRight.Frame = new Rect (0, 0, 1, 25);
  1044. Application.RunIteration (ref rs, ref firstIteration);
  1045. Assert.True (viewTopBottom_LeftRight.AutoSize);
  1046. Assert.Equal (LayoutStyle.Absolute, viewTopBottom_LeftRight.LayoutStyle);
  1047. Assert.Equal (new Rect (0, 0, 2, 25), viewTopBottom_LeftRight.Frame);
  1048. Assert.Equal ("Absolute(0)", viewTopBottom_LeftRight.X.ToString ());
  1049. Assert.Equal ("Absolute(0)", viewTopBottom_LeftRight.Y.ToString ());
  1050. Assert.Equal ("Absolute(2)", viewTopBottom_LeftRight.Width.ToString ());
  1051. Assert.Equal ("Absolute(25)", viewTopBottom_LeftRight.Height.ToString ());
  1052. Application.End (rs);
  1053. }
  1054. [Fact]
  1055. [AutoInitShutdown]
  1056. public void AutoSize_Stays_True_Center_HotKeySpecifier ()
  1057. {
  1058. var label = new Label {
  1059. X = Pos.Center (),
  1060. Y = Pos.Center (),
  1061. Text = "Say Hello 你"
  1062. };
  1063. var win = new Window {
  1064. Width = Dim.Fill (),
  1065. Height = Dim.Fill (),
  1066. Title = "Test Demo 你"
  1067. };
  1068. win.Add (label);
  1069. Application.Top.Add (win);
  1070. Assert.True (label.AutoSize);
  1071. var rs = Application.Begin (Application.Top);
  1072. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  1073. var expected = @"
  1074. ┌┤Test Demo 你├──────────────┐
  1075. │ │
  1076. │ Say Hello 你 │
  1077. │ │
  1078. └────────────────────────────┘
  1079. ";
  1080. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1081. Assert.True (label.AutoSize);
  1082. label.Text = "Say Hello 你 changed";
  1083. Assert.True (label.AutoSize);
  1084. Application.Refresh ();
  1085. expected = @"
  1086. ┌┤Test Demo 你├──────────────┐
  1087. │ │
  1088. │ Say Hello 你 changed │
  1089. │ │
  1090. └────────────────────────────┘
  1091. ";
  1092. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1093. Application.End (rs);
  1094. }
  1095. [Fact]
  1096. [AutoInitShutdown]
  1097. public void AutoSize_Dim_Add_Operator_With_Text ()
  1098. {
  1099. var top = Application.Top;
  1100. var view = new View ("View with long text") { X = 0, Y = 0, Width = 20, Height = 1 };
  1101. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  1102. var count = 0;
  1103. // Label is AutoSize == true
  1104. var listLabels = new List<Label> ();
  1105. field.KeyDown += (s, k) => {
  1106. if (k.KeyCode == KeyCode.Enter) {
  1107. ((FakeDriver)Application.Driver).SetBufferSize (22, count + 4);
  1108. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expecteds [count], _output);
  1109. Assert.Equal (new Rect (0, 0, 22, count + 4), pos);
  1110. if (count < 20) {
  1111. field.Text = $"Label {count}";
  1112. // Label is AutoSize = true
  1113. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 10 };
  1114. view.Add (label);
  1115. Assert.Equal ($"Label {count}", label.Text);
  1116. Assert.Equal ($"Absolute({count + 1})", label.Y.ToString ());
  1117. listLabels.Add (label);
  1118. //if (count == 0) {
  1119. // Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  1120. // view.Height += 2;
  1121. //} else {
  1122. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  1123. view.Height += 1;
  1124. //}
  1125. count++;
  1126. }
  1127. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  1128. }
  1129. };
  1130. Application.Iteration += (s, a) => {
  1131. while (count < 21) {
  1132. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  1133. if (count == 20) {
  1134. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  1135. break;
  1136. }
  1137. }
  1138. Application.RequestStop ();
  1139. };
  1140. var win = new Window ();
  1141. win.Add (view);
  1142. win.Add (field);
  1143. top.Add (win);
  1144. Application.Run (top);
  1145. Assert.Equal (20, count);
  1146. Assert.Equal (count, listLabels.Count);
  1147. }
  1148. [Fact]
  1149. [AutoInitShutdown]
  1150. public void AutoSize_Dim_Subtract_Operator_With_Text ()
  1151. {
  1152. var top = Application.Top;
  1153. // BUGBUG: v2 - If a View's height is zero, it should not be drawn.
  1154. //// Although view height is zero the text it's draw due the SetMinWidthHeight method
  1155. var view = new View ("View with long text") { X = 0, Y = 0, Width = 20, Height = 1 };
  1156. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  1157. var count = 20;
  1158. // Label is AutoSize == true
  1159. var listLabels = new List<Label> ();
  1160. for (var i = 0; i < count; i++) {
  1161. field.Text = $"Label {i}";
  1162. // BUGBUG: v2 - view has not been initialied yet; view.Bounds is indeterminate
  1163. var label = new Label (field.Text) { X = 0, Y = i + 1, Width = 10 };
  1164. view.Add (label);
  1165. Assert.Equal ($"Label {i}", label.Text);
  1166. // BUGBUG: Bogus test; views have not been initialized yet
  1167. //Assert.Equal ($"Absolute({i + 1})", label.Y.ToString ());
  1168. listLabels.Add (label);
  1169. //if (i == 0) {
  1170. // BUGBUG: Bogus test; views have not been initialized yet
  1171. //Assert.Equal ($"Absolute({i})", view.Height.ToString ());
  1172. //view.Height += 2;
  1173. // BUGBUG: Bogus test; views have not been initialized yet
  1174. //Assert.Equal ($"Absolute({i + 2})", view.Height.ToString ());
  1175. //} else {
  1176. // BUGBUG: Bogus test; views have not been initialized yet
  1177. //Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
  1178. view.Height += 1;
  1179. // BUGBUG: Bogus test; views have not been initialized yet
  1180. //Assert.Equal ($"Absolute({i + 2})", view.Height.ToString ());
  1181. //}
  1182. }
  1183. field.KeyDown += (s, k) => {
  1184. if (k.KeyCode == KeyCode.Enter) {
  1185. ((FakeDriver)Application.Driver).SetBufferSize (22, count + 4);
  1186. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expecteds [count], _output);
  1187. Assert.Equal (new Rect (0, 0, 22, count + 4), pos);
  1188. if (count > 0) {
  1189. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  1190. view.Remove (listLabels [count - 1]);
  1191. listLabels [count - 1].Dispose ();
  1192. listLabels.RemoveAt (count - 1);
  1193. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  1194. view.Height -= 1;
  1195. count--;
  1196. if (listLabels.Count > 0) {
  1197. field.Text = listLabels [count - 1].Text;
  1198. } else {
  1199. field.Text = string.Empty;
  1200. }
  1201. }
  1202. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  1203. }
  1204. };
  1205. Application.Iteration += (s, a) => {
  1206. while (count > -1) {
  1207. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  1208. if (count == 0) {
  1209. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  1210. break;
  1211. }
  1212. }
  1213. Application.RequestStop ();
  1214. };
  1215. var win = new Window ();
  1216. win.Add (view);
  1217. win.Add (field);
  1218. top.Add (win);
  1219. Application.Run (top);
  1220. Assert.Equal (0, count);
  1221. Assert.Equal (count, listLabels.Count);
  1222. }
  1223. [Fact]
  1224. [AutoInitShutdown]
  1225. public void AutoSize_AnchorEnd_Better_Than_Bottom_Equal_Inside_Window ()
  1226. {
  1227. var win = new Window ();
  1228. // Label is AutoSize == true
  1229. var label = new Label ("This should be the last line.") {
  1230. ColorScheme = Colors.Menu,
  1231. Width = Dim.Fill (),
  1232. X = 0, // keep unit test focused; don't use Center here
  1233. Y = Pos.AnchorEnd (1)
  1234. };
  1235. win.Add (label);
  1236. var top = Application.Top;
  1237. top.Add (win);
  1238. var rs = Application.Begin (top);
  1239. ((FakeDriver)Application.Driver).SetBufferSize (40, 10);
  1240. Assert.True (label.AutoSize);
  1241. Assert.Equal (29, label.Text.Length);
  1242. Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
  1243. Assert.Equal (new Rect (0, 0, 40, 10), win.Frame);
  1244. Assert.Equal (new Rect (0, 7, 38, 1), label.Frame);
  1245. var expected = @"
  1246. ┌──────────────────────────────────────┐
  1247. │ │
  1248. │ │
  1249. │ │
  1250. │ │
  1251. │ │
  1252. │ │
  1253. │ │
  1254. │This should be the last line. │
  1255. └──────────────────────────────────────┘
  1256. ";
  1257. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1258. Application.End (rs);
  1259. }
  1260. [Fact]
  1261. [AutoInitShutdown]
  1262. public void AutoSize_Bottom_Equal_Inside_Window ()
  1263. {
  1264. var win = new Window ();
  1265. // Label is AutoSize == true
  1266. var label = new Label ("This should be the last line.") {
  1267. ColorScheme = Colors.Menu,
  1268. Width = Dim.Fill (),
  1269. X = 0,
  1270. Y = Pos.Bottom (win) - 3 // two lines top and bottom borders more one line above the bottom border
  1271. };
  1272. win.Add (label);
  1273. var top = Application.Top;
  1274. top.Add (win);
  1275. var rs = Application.Begin (top);
  1276. ((FakeDriver)Application.Driver).SetBufferSize (40, 10);
  1277. Assert.True (label.AutoSize);
  1278. Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
  1279. Assert.Equal (new Rect (0, 0, 40, 10), win.Frame);
  1280. Assert.Equal (new Rect (0, 7, 38, 1), label.Frame);
  1281. var expected = @"
  1282. ┌──────────────────────────────────────┐
  1283. │ │
  1284. │ │
  1285. │ │
  1286. │ │
  1287. │ │
  1288. │ │
  1289. │ │
  1290. │This should be the last line. │
  1291. └──────────────────────────────────────┘
  1292. ";
  1293. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1294. Application.End (rs);
  1295. }
  1296. [Fact]
  1297. [AutoInitShutdown]
  1298. public void AutoSize_Bottom_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
  1299. {
  1300. var win = new Window ();
  1301. // Label is AutoSize == true
  1302. var label = new Label ("This should be the last line.") {
  1303. ColorScheme = Colors.Menu,
  1304. Width = Dim.Fill (),
  1305. X = 0,
  1306. Y = Pos.Bottom (win) - 4 // two lines top and bottom borders more two lines above border
  1307. };
  1308. win.Add (label);
  1309. var menu = new MenuBar (new MenuBarItem [] { new ("Menu", "", null) });
  1310. var status = new StatusBar (new StatusItem [] { new (KeyCode.F1, "~F1~ Help", null) });
  1311. var top = Application.Top;
  1312. top.Add (win, menu, status);
  1313. var rs = Application.Begin (top);
  1314. Assert.True (label.AutoSize);
  1315. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  1316. Assert.Equal (new Rect (0, 0, 80, 1), menu.Frame);
  1317. Assert.Equal (new Rect (0, 24, 80, 1), status.Frame);
  1318. Assert.Equal (new Rect (0, 1, 80, 23), win.Frame);
  1319. Assert.Equal (new Rect (0, 20, 78, 1), label.Frame);
  1320. var expected = @"
  1321. Menu
  1322. ┌──────────────────────────────────────────────────────────────────────────────┐
  1323. │ │
  1324. │ │
  1325. │ │
  1326. │ │
  1327. │ │
  1328. │ │
  1329. │ │
  1330. │ │
  1331. │ │
  1332. │ │
  1333. │ │
  1334. │ │
  1335. │ │
  1336. │ │
  1337. │ │
  1338. │ │
  1339. │ │
  1340. │ │
  1341. │ │
  1342. │ │
  1343. │This should be the last line. │
  1344. └──────────────────────────────────────────────────────────────────────────────┘
  1345. F1 Help
  1346. ";
  1347. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1348. Application.End (rs);
  1349. }
  1350. [Fact]
  1351. [AutoInitShutdown]
  1352. public void AutoSize_AnchorEnd_Better_Than_Bottom_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
  1353. {
  1354. var win = new Window ();
  1355. // Label is AutoSize == true
  1356. var label = new Label ("This should be the last line.") {
  1357. ColorScheme = Colors.Menu,
  1358. Width = Dim.Fill (),
  1359. X = 0,
  1360. Y = Pos.AnchorEnd (1)
  1361. };
  1362. win.Add (label);
  1363. var menu = new MenuBar (new MenuBarItem [] { new ("Menu", "", null) });
  1364. var status = new StatusBar (new StatusItem [] { new (KeyCode.F1, "~F1~ Help", null) });
  1365. var top = Application.Top;
  1366. top.Add (win, menu, status);
  1367. var rs = Application.Begin (top);
  1368. Assert.True (label.AutoSize);
  1369. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  1370. Assert.Equal (new Rect (0, 0, 80, 1), menu.Frame);
  1371. Assert.Equal (new Rect (0, 24, 80, 1), status.Frame);
  1372. Assert.Equal (new Rect (0, 1, 80, 23), win.Frame);
  1373. Assert.Equal (new Rect (0, 20, 78, 1), label.Frame);
  1374. var expected = @"
  1375. Menu
  1376. ┌──────────────────────────────────────────────────────────────────────────────┐
  1377. │ │
  1378. │ │
  1379. │ │
  1380. │ │
  1381. │ │
  1382. │ │
  1383. │ │
  1384. │ │
  1385. │ │
  1386. │ │
  1387. │ │
  1388. │ │
  1389. │ │
  1390. │ │
  1391. │ │
  1392. │ │
  1393. │ │
  1394. │ │
  1395. │ │
  1396. │ │
  1397. │This should be the last line. │
  1398. └──────────────────────────────────────────────────────────────────────────────┘
  1399. F1 Help
  1400. ";
  1401. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1402. Application.End (rs);
  1403. }
  1404. [Fact]
  1405. [AutoInitShutdown]
  1406. public void AutoSize_True_TextDirection_Toggle ()
  1407. {
  1408. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  1409. // View is AutoSize == true
  1410. var view = new View ();
  1411. win.Add (view);
  1412. Application.Top.Add (win);
  1413. var rs = Application.Begin (Application.Top);
  1414. ((FakeDriver)Application.Driver).SetBufferSize (15, 15);
  1415. Assert.Equal (new Rect (0, 0, 15, 15), win.Frame);
  1416. Assert.Equal (new Rect (0, 0, 15, 15), win.Margin.Frame);
  1417. Assert.Equal (new Rect (0, 0, 15, 15), win.Border.Frame);
  1418. Assert.Equal (new Rect (1, 1, 13, 13), win.Padding.Frame);
  1419. Assert.False (view.AutoSize);
  1420. Assert.Equal (TextDirection.LeftRight_TopBottom, view.TextDirection);
  1421. Assert.Equal (Rect.Empty, view.Frame);
  1422. Assert.Equal ("Absolute(0)", view.X.ToString ());
  1423. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1424. Assert.Equal ("Absolute(0)", view.Width.ToString ());
  1425. Assert.Equal ("Absolute(0)", view.Height.ToString ());
  1426. var expected = @"
  1427. ┌─────────────┐
  1428. │ │
  1429. │ │
  1430. │ │
  1431. │ │
  1432. │ │
  1433. │ │
  1434. │ │
  1435. │ │
  1436. │ │
  1437. │ │
  1438. │ │
  1439. │ │
  1440. │ │
  1441. └─────────────┘
  1442. ";
  1443. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1444. view.Text = "Hello World";
  1445. view.Width = 11;
  1446. view.Height = 1;
  1447. win.LayoutSubviews ();
  1448. Application.Refresh ();
  1449. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  1450. Assert.Equal ("Absolute(0)", view.X.ToString ());
  1451. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1452. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  1453. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  1454. expected = @"
  1455. ┌─────────────┐
  1456. │Hello World │
  1457. │ │
  1458. │ │
  1459. │ │
  1460. │ │
  1461. │ │
  1462. │ │
  1463. │ │
  1464. │ │
  1465. │ │
  1466. │ │
  1467. │ │
  1468. │ │
  1469. └─────────────┘
  1470. ";
  1471. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1472. view.AutoSize = true;
  1473. view.Text = "Hello Worlds";
  1474. Application.Refresh ();
  1475. var len = "Hello Worlds".Length;
  1476. Assert.Equal (12, len);
  1477. Assert.Equal (new Rect (0, 0, len, 1), view.Frame);
  1478. Assert.Equal ("Absolute(0)", view.X.ToString ());
  1479. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1480. Assert.Equal ("Absolute(12)", view.Width.ToString ());
  1481. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  1482. expected = @"
  1483. ┌─────────────┐
  1484. │Hello Worlds │
  1485. │ │
  1486. │ │
  1487. │ │
  1488. │ │
  1489. │ │
  1490. │ │
  1491. │ │
  1492. │ │
  1493. │ │
  1494. │ │
  1495. │ │
  1496. │ │
  1497. └─────────────┘
  1498. ";
  1499. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1500. view.TextDirection = TextDirection.TopBottom_LeftRight;
  1501. Application.Refresh ();
  1502. Assert.Equal (new Rect (0, 0, 12, 12), view.Frame);
  1503. Assert.Equal ("Absolute(0)", view.X.ToString ());
  1504. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1505. Assert.Equal ("Absolute(12)", view.Width.ToString ());
  1506. Assert.Equal ("Absolute(12)", view.Height.ToString ());
  1507. expected = @"
  1508. ┌─────────────┐
  1509. │H │
  1510. │e │
  1511. │l │
  1512. │l │
  1513. │o │
  1514. │ │
  1515. │W │
  1516. │o │
  1517. │r │
  1518. │l │
  1519. │d │
  1520. │s │
  1521. │ │
  1522. └─────────────┘
  1523. ";
  1524. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1525. view.AutoSize = false;
  1526. view.Height = 1;
  1527. Application.Refresh ();
  1528. Assert.Equal (new Rect (0, 0, 12, 1), view.Frame);
  1529. Assert.Equal ("Absolute(0)", view.X.ToString ());
  1530. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1531. Assert.Equal ("Absolute(12)", view.Width.ToString ());
  1532. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  1533. // TextDirection.TopBottom_LeftRight - Height of 1 and Width of 12 means
  1534. // that the text will be spread "vertically" across 1 line.
  1535. // Hence no space.
  1536. expected = @"
  1537. ┌─────────────┐
  1538. │HelloWorlds │
  1539. │ │
  1540. │ │
  1541. │ │
  1542. │ │
  1543. │ │
  1544. │ │
  1545. │ │
  1546. │ │
  1547. │ │
  1548. │ │
  1549. │ │
  1550. │ │
  1551. └─────────────┘
  1552. ";
  1553. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1554. view.PreserveTrailingSpaces = true;
  1555. Application.Refresh ();
  1556. Assert.Equal (new Rect (0, 0, 12, 1), view.Frame);
  1557. Assert.Equal ("Absolute(0)", view.X.ToString ());
  1558. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1559. Assert.Equal ("Absolute(12)", view.Width.ToString ());
  1560. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  1561. expected = @"
  1562. ┌─────────────┐
  1563. │Hello Worlds │
  1564. │ │
  1565. │ │
  1566. │ │
  1567. │ │
  1568. │ │
  1569. │ │
  1570. │ │
  1571. │ │
  1572. │ │
  1573. │ │
  1574. │ │
  1575. │ │
  1576. └─────────────┘
  1577. ";
  1578. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1579. view.PreserveTrailingSpaces = false;
  1580. var f = view.Frame;
  1581. view.Width = f.Height;
  1582. view.Height = f.Width;
  1583. view.TextDirection = TextDirection.TopBottom_LeftRight;
  1584. Application.Refresh ();
  1585. Assert.Equal (new Rect (0, 0, 1, 12), view.Frame);
  1586. Assert.Equal ("Absolute(0)", view.X.ToString ());
  1587. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1588. Assert.Equal ("Absolute(1)", view.Width.ToString ());
  1589. Assert.Equal ("Absolute(12)", view.Height.ToString ());
  1590. expected = @"
  1591. ┌─────────────┐
  1592. │H │
  1593. │e │
  1594. │l │
  1595. │l │
  1596. │o │
  1597. │ │
  1598. │W │
  1599. │o │
  1600. │r │
  1601. │l │
  1602. │d │
  1603. │s │
  1604. │ │
  1605. └─────────────┘
  1606. ";
  1607. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1608. view.AutoSize = true;
  1609. Application.Refresh ();
  1610. Assert.Equal (new Rect (0, 0, 1, 12), view.Frame);
  1611. Assert.Equal ("Absolute(0)", view.X.ToString ());
  1612. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1613. Assert.Equal ("Absolute(1)", view.Width.ToString ());
  1614. Assert.Equal ("Absolute(12)", view.Height.ToString ());
  1615. expected = @"
  1616. ┌─────────────┐
  1617. │H │
  1618. │e │
  1619. │l │
  1620. │l │
  1621. │o │
  1622. │ │
  1623. │W │
  1624. │o │
  1625. │r │
  1626. │l │
  1627. │d │
  1628. │s │
  1629. │ │
  1630. └─────────────┘
  1631. ";
  1632. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1633. Application.End (rs);
  1634. }
  1635. [Fact]
  1636. [AutoInitShutdown]
  1637. public void AutoSize_True_Width_Height_Stay_True_If_TextFormatter_Size_Fit ()
  1638. {
  1639. var text = "Fi_nish 終";
  1640. var horizontalView = new View {
  1641. Id = "horizontalView",
  1642. AutoSize = true,
  1643. HotKeySpecifier = (Rune)'_',
  1644. Text = text
  1645. };
  1646. var verticalView = new View {
  1647. Id = "verticalView",
  1648. Y = 3,
  1649. AutoSize = true,
  1650. HotKeySpecifier = (Rune)'_',
  1651. Text = text,
  1652. TextDirection = TextDirection.TopBottom_LeftRight
  1653. };
  1654. var win = new Window {
  1655. Id = "win",
  1656. Width = Dim.Fill (),
  1657. Height = Dim.Fill (),
  1658. Text = "Window"
  1659. };
  1660. win.Add (horizontalView, verticalView);
  1661. Application.Top.Add (win);
  1662. var rs = Application.Begin (Application.Top);
  1663. ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  1664. Assert.True (horizontalView.AutoSize);
  1665. Assert.True (verticalView.AutoSize);
  1666. Assert.Equal (new Size (text.GetColumns (), 1), horizontalView.TextFormatter.Size);
  1667. Assert.Equal (new Size (2, 9), verticalView.TextFormatter.Size);
  1668. Assert.Equal (new Rect (0, 0, 9, 1), horizontalView.Frame);
  1669. Assert.Equal ("Absolute(0)", horizontalView.X.ToString ());
  1670. Assert.Equal ("Absolute(0)", horizontalView.Y.ToString ());
  1671. // BUGBUG - v2 - With v1 AutoSize = true Width/Height should always grow or keep initial value,
  1672. Assert.Equal ("Absolute(9)", horizontalView.Width.ToString ());
  1673. Assert.Equal ("Absolute(1)", horizontalView.Height.ToString ());
  1674. Assert.Equal (new Rect (0, 3, 9, 8), verticalView.Frame);
  1675. Assert.Equal ("Absolute(0)", verticalView.X.ToString ());
  1676. Assert.Equal ("Absolute(3)", verticalView.Y.ToString ());
  1677. Assert.Equal ("Absolute(9)", verticalView.Width.ToString ());
  1678. Assert.Equal ("Absolute(8)", verticalView.Height.ToString ());
  1679. var expected = @"
  1680. ┌────────────────────┐
  1681. │Finish 終 │
  1682. │ │
  1683. │ │
  1684. │F │
  1685. │i │
  1686. │n │
  1687. │i │
  1688. │s │
  1689. │h │
  1690. │ │
  1691. │終 │
  1692. │ │
  1693. │ │
  1694. │ │
  1695. │ │
  1696. │ │
  1697. │ │
  1698. │ │
  1699. │ │
  1700. │ │
  1701. └────────────────────┘
  1702. ";
  1703. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1704. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  1705. verticalView.Text = "最初_の行二行目";
  1706. Application.Top.Draw ();
  1707. Assert.True (horizontalView.AutoSize);
  1708. Assert.True (verticalView.AutoSize);
  1709. // height was initialized with 8 and can only grow or keep initial value
  1710. Assert.Equal (new Rect (0, 3, 9, 8), verticalView.Frame);
  1711. Assert.Equal ("Absolute(0)", verticalView.X.ToString ());
  1712. Assert.Equal ("Absolute(3)", verticalView.Y.ToString ());
  1713. Assert.Equal ("Absolute(9)", verticalView.Width.ToString ());
  1714. Assert.Equal ("Absolute(8)", verticalView.Height.ToString ());
  1715. expected = @"
  1716. ┌────────────────────┐
  1717. │Finish 終 │
  1718. │ │
  1719. │ │
  1720. │最 │
  1721. │初 │
  1722. │の │
  1723. │行 │
  1724. │二 │
  1725. │行 │
  1726. │目 │
  1727. │ │
  1728. │ │
  1729. │ │
  1730. │ │
  1731. │ │
  1732. │ │
  1733. │ │
  1734. │ │
  1735. │ │
  1736. │ │
  1737. └────────────────────┘
  1738. ";
  1739. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1740. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  1741. Application.End (rs);
  1742. }
  1743. [Fact]
  1744. [AutoInitShutdown]
  1745. public void AutoSize_False_SetWidthHeight_With_Dim_Fill_And_Dim_Absolute_After_IsAdded_And_IsInitialized ()
  1746. {
  1747. var win = new Window (new Rect (0, 0, 30, 50));
  1748. var label = new Label { Width = Dim.Fill () };
  1749. win.Add (label);
  1750. Application.Top.Add (win);
  1751. Assert.True (label.IsAdded);
  1752. Assert.True (label.AutoSize);
  1753. // #3127: Before:
  1754. // Text is empty but height=1 by default, see Label view
  1755. // BUGBUG: LayoutSubviews has not been called, so this test is not really valid (pos/dim are indeterminate, not 0)
  1756. // Not really a bug because View call OnResizeNeeded method on the SetInitialProperties method
  1757. // #3127: After: Text is empty Width=Dim.Fill is honored.
  1758. // LayoutSubViews has not been called, and OnResizeNeeded ends up using Application.Top.Bounds
  1759. // Which has a width of 80.
  1760. Assert.Equal ("(0,0,80,1)", label.Bounds.ToString ());
  1761. label.Text = "First line\nSecond line";
  1762. Application.Top.LayoutSubviews ();
  1763. Assert.True (label.AutoSize);
  1764. Assert.Equal ("(0,0,28,2)", label.Bounds.ToString ());
  1765. Assert.False (label.IsInitialized);
  1766. var rs = Application.Begin (Application.Top);
  1767. Assert.True (label.AutoSize);
  1768. Assert.Equal ("(0,0,28,2)", label.Bounds.ToString ());
  1769. Assert.True (label.IsInitialized);
  1770. label.AutoSize = false;
  1771. // BUGBUG: Application.Refresh has nothing to do with layout! It just redraws and sets LayoutNeeded to true
  1772. // Application.Refresh ();
  1773. // Width should still be Dim.Fill
  1774. Assert.Equal ("Fill(0)", label.Width.ToString ());
  1775. // Height should be 2
  1776. Assert.Equal ("Absolute(2)", label.Height.ToString ());
  1777. Assert.Equal (2, label.Frame.Height);
  1778. Assert.False (label.AutoSize);
  1779. Assert.Equal ("(0,0,28,2)", label.Bounds.ToString ());
  1780. Application.End (rs);
  1781. }
  1782. [Fact]
  1783. [AutoInitShutdown]
  1784. public void AutoSize_False_SetWidthHeight_With_Dim_Fill_And_Dim_Absolute_With_Initialization ()
  1785. {
  1786. var win = new Window (new Rect (0, 0, 30, 80));
  1787. var label = new Label { Width = Dim.Fill () };
  1788. win.Add (label);
  1789. Application.Top.Add (win);
  1790. // Text is empty but height=1 by default.
  1791. Assert.True (label.AutoSize);
  1792. Assert.Equal ("(0,0,80,1)", label.Bounds.ToString ());
  1793. var rs = Application.Begin (Application.Top);
  1794. Assert.True (label.AutoSize);
  1795. // Here the AutoSize ensuring the right size with width 28 (Dim.Fill)
  1796. // and height 0 because wasn't set and the text is empty
  1797. // BUGBUG: Because of #2450, this test is bogus: pos/dim is indeterminate!
  1798. //Assert.Equal ("(0,0,28,0)", label.Bounds.ToString ());
  1799. label.Text = "First line\nSecond line";
  1800. Application.Refresh ();
  1801. // Here the AutoSize ensuring the right size with width 28 (Dim.Fill)
  1802. // and height 2 because wasn't set and the text has 2 lines
  1803. Assert.True (label.AutoSize);
  1804. Assert.Equal ("(0,0,28,2)", label.Bounds.ToString ());
  1805. label.AutoSize = false;
  1806. Application.Refresh ();
  1807. // Here the SetMinWidthHeight ensuring the minimum height
  1808. // #3127: After: (0,0,28,2) because turning off AutoSize leaves
  1809. // Height set to 2.
  1810. Assert.False (label.AutoSize);
  1811. Assert.Equal ("(0,0,28,2)", label.Bounds.ToString ());
  1812. label.Text = "First changed line\nSecond changed line\nNew line";
  1813. Application.Refresh ();
  1814. // Here the AutoSize is false and the width 28 (Dim.Fill) and
  1815. // #3127: Before: height 1 because it wasn't set and SetMinWidthHeight ensuring the minimum height
  1816. // #3127: After: (0,0,28,2) because setting Text leaves Height set to 2..
  1817. Assert.False (label.AutoSize);
  1818. Assert.Equal ("(0,0,28,2)", label.Bounds.ToString ());
  1819. label.AutoSize = true;
  1820. Application.Refresh ();
  1821. // Here the AutoSize ensuring the right size with width 19 (width of longest line)
  1822. // and height 3 because the text has 3 lines
  1823. Assert.True (label.AutoSize);
  1824. Assert.Equal ("(0,0,19,3)", label.Bounds.ToString ());
  1825. Application.End (rs);
  1826. }
  1827. // [Fact]
  1828. // [AutoInitShutdown]
  1829. // public void AutoSize_False_TextDirection_Toggle ()
  1830. // {
  1831. // var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  1832. // // View is AutoSize == true
  1833. // var view = new View ();
  1834. // win.Add (view);
  1835. // Application.Top.Add (win);
  1836. // var rs = Application.Begin (Application.Top);
  1837. // ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  1838. // Assert.Equal (new Rect (0, 0, 22, 22), win.Frame);
  1839. // Assert.Equal (new Rect (0, 0, 22, 22), win.Margin.Frame);
  1840. // Assert.Equal (new Rect (0, 0, 22, 22), win.Border.Frame);
  1841. // Assert.Equal (new Rect (1, 1, 20, 20), win.Padding.Frame);
  1842. // Assert.False (view.AutoSize);
  1843. // Assert.Equal (TextDirection.LeftRight_TopBottom, view.TextDirection);
  1844. // Assert.Equal (Rect.Empty, view.Frame);
  1845. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  1846. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1847. // Assert.Equal ("Absolute(0)", view.Width.ToString ());
  1848. // Assert.Equal ("Absolute(0)", view.Height.ToString ());
  1849. // var expected = @"
  1850. //┌────────────────────┐
  1851. //│ │
  1852. //│ │
  1853. //│ │
  1854. //│ │
  1855. //│ │
  1856. //│ │
  1857. //│ │
  1858. //│ │
  1859. //│ │
  1860. //│ │
  1861. //│ │
  1862. //│ │
  1863. //│ │
  1864. //│ │
  1865. //│ │
  1866. //│ │
  1867. //│ │
  1868. //│ │
  1869. //│ │
  1870. //│ │
  1871. //└────────────────────┘
  1872. //";
  1873. // var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1874. // Assert.Equal (new Rect (0, 0, 22, 22), pos);
  1875. // view.Text = "Hello World";
  1876. // view.Width = 11;
  1877. // view.Height = 1;
  1878. // win.LayoutSubviews ();
  1879. // Application.Refresh ();
  1880. // Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  1881. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  1882. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1883. // Assert.Equal ("Absolute(11)", view.Width.ToString ());
  1884. // Assert.Equal ("Absolute(1)", view.Height.ToString ());
  1885. // expected = @"
  1886. //┌────────────────────┐
  1887. //│Hello World │
  1888. //│ │
  1889. //│ │
  1890. //│ │
  1891. //│ │
  1892. //│ │
  1893. //│ │
  1894. //│ │
  1895. //│ │
  1896. //│ │
  1897. //│ │
  1898. //│ │
  1899. //│ │
  1900. //│ │
  1901. //│ │
  1902. //│ │
  1903. //│ │
  1904. //│ │
  1905. //│ │
  1906. //│ │
  1907. //└────────────────────┘
  1908. //";
  1909. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1910. // Assert.Equal (new Rect (0, 0, 22, 22), pos);
  1911. // view.AutoSize = true;
  1912. // view.Text = "Hello Worlds";
  1913. // Application.Refresh ();
  1914. // Assert.Equal (new Rect (0, 0, 12, 1), view.Frame);
  1915. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  1916. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1917. // Assert.Equal ("Absolute(11)", view.Width.ToString ());
  1918. // Assert.Equal ("Absolute(1)", view.Height.ToString ());
  1919. // expected = @"
  1920. //┌────────────────────┐
  1921. //│Hello Worlds │
  1922. //│ │
  1923. //│ │
  1924. //│ │
  1925. //│ │
  1926. //│ │
  1927. //│ │
  1928. //│ │
  1929. //│ │
  1930. //│ │
  1931. //│ │
  1932. //│ │
  1933. //│ │
  1934. //│ │
  1935. //│ │
  1936. //│ │
  1937. //│ │
  1938. //│ │
  1939. //│ │
  1940. //│ │
  1941. //└────────────────────┘
  1942. //";
  1943. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1944. // Assert.Equal (new Rect (0, 0, 22, 22), pos);
  1945. // view.TextDirection = TextDirection.TopBottom_LeftRight;
  1946. // Application.Refresh ();
  1947. // Assert.Equal (new Rect (0, 0, 11, 12), view.Frame);
  1948. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  1949. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1950. // Assert.Equal ("Absolute(11)", view.Width.ToString ());
  1951. // Assert.Equal ("Absolute(1)", view.Height.ToString ());
  1952. // expected = @"
  1953. //┌────────────────────┐
  1954. //│H │
  1955. //│e │
  1956. //│l │
  1957. //│l │
  1958. //│o │
  1959. //│ │
  1960. //│W │
  1961. //│o │
  1962. //│r │
  1963. //│l │
  1964. //│d │
  1965. //│s │
  1966. //│ │
  1967. //│ │
  1968. //│ │
  1969. //│ │
  1970. //│ │
  1971. //│ │
  1972. //│ │
  1973. //│ │
  1974. //└────────────────────┘
  1975. //";
  1976. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1977. // Assert.Equal (new Rect (0, 0, 22, 22), pos);
  1978. // view.AutoSize = false;
  1979. // view.Height = 1;
  1980. // Application.Refresh ();
  1981. // Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  1982. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  1983. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1984. // Assert.Equal ("Absolute(11)", view.Width.ToString ());
  1985. // Assert.Equal ("Absolute(1)", view.Height.ToString ());
  1986. // expected = @"
  1987. //┌────────────────────┐
  1988. //│HelloWorlds │
  1989. //│ │
  1990. //│ │
  1991. //│ │
  1992. //│ │
  1993. //│ │
  1994. //│ │
  1995. //│ │
  1996. //│ │
  1997. //│ │
  1998. //│ │
  1999. //│ │
  2000. //│ │
  2001. //│ │
  2002. //│ │
  2003. //│ │
  2004. //│ │
  2005. //│ │
  2006. //│ │
  2007. //│ │
  2008. //└────────────────────┘
  2009. //";
  2010. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2011. // Assert.Equal (new Rect (0, 0, 22, 22), pos);
  2012. // view.PreserveTrailingSpaces = true;
  2013. // Application.Refresh ();
  2014. // Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  2015. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2016. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2017. // Assert.Equal ("Absolute(11)", view.Width.ToString ());
  2018. // Assert.Equal ("Absolute(1)", view.Height.ToString ());
  2019. // expected = @"
  2020. //┌────────────────────┐
  2021. //│Hello World │
  2022. //│ │
  2023. //│ │
  2024. //│ │
  2025. //│ │
  2026. //│ │
  2027. //│ │
  2028. //│ │
  2029. //│ │
  2030. //│ │
  2031. //│ │
  2032. //│ │
  2033. //│ │
  2034. //│ │
  2035. //│ │
  2036. //│ │
  2037. //│ │
  2038. //│ │
  2039. //│ │
  2040. //│ │
  2041. //└────────────────────┘
  2042. //";
  2043. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2044. // Assert.Equal (new Rect (0, 0, 22, 22), pos);
  2045. // view.PreserveTrailingSpaces = false;
  2046. // var f = view.Frame;
  2047. // view.Width = f.Height;
  2048. // view.Height = f.Width;
  2049. // view.TextDirection = TextDirection.TopBottom_LeftRight;
  2050. // Application.Refresh ();
  2051. // Assert.Equal (new Rect (0, 0, 1, 11), view.Frame);
  2052. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2053. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2054. // Assert.Equal ("Absolute(1)", view.Width.ToString ());
  2055. // Assert.Equal ("Absolute(11)", view.Height.ToString ());
  2056. // expected = @"
  2057. //┌────────────────────┐
  2058. //│H │
  2059. //│e │
  2060. //│l │
  2061. //│l │
  2062. //│o │
  2063. //│ │
  2064. //│W │
  2065. //│o │
  2066. //│r │
  2067. //│l │
  2068. //│d │
  2069. //│ │
  2070. //│ │
  2071. //│ │
  2072. //│ │
  2073. //│ │
  2074. //│ │
  2075. //│ │
  2076. //│ │
  2077. //│ │
  2078. //└────────────────────┘
  2079. //";
  2080. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2081. // Assert.Equal (new Rect (0, 0, 22, 22), pos);
  2082. // view.AutoSize = true;
  2083. // Application.Refresh ();
  2084. // Assert.Equal (new Rect (0, 0, 1, 12), view.Frame);
  2085. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2086. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2087. // Assert.Equal ("Absolute(1)", view.Width.ToString ());
  2088. // Assert.Equal ("Absolute(12)", view.Height.ToString ());
  2089. // expected = @"
  2090. //┌────────────────────┐
  2091. //│H │
  2092. //│e │
  2093. //│l │
  2094. //│l │
  2095. //│o │
  2096. //│ │
  2097. //│W │
  2098. //│o │
  2099. //│r │
  2100. //│l │
  2101. //│d │
  2102. //│s │
  2103. //│ │
  2104. //│ │
  2105. //│ │
  2106. //│ │
  2107. //│ │
  2108. //│ │
  2109. //│ │
  2110. //│ │
  2111. //└────────────────────┘
  2112. //";
  2113. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2114. // Assert.Equal (new Rect (0, 0, 22, 22), pos);
  2115. // Application.End (rs);
  2116. // }
  2117. [Fact] [AutoInitShutdown]
  2118. public void GetTextFormatterBoundsSize_GetSizeNeededForText_HotKeySpecifier ()
  2119. {
  2120. var text = "Say Hello 你";
  2121. // Frame: 0, 0, 12, 1
  2122. var horizontalView = new View {
  2123. AutoSize = true,
  2124. HotKeySpecifier = (Rune)'_'
  2125. };
  2126. horizontalView.Text = text;
  2127. // Frame: 0, 0, 1, 12
  2128. var verticalView = new View {
  2129. AutoSize = true,
  2130. HotKeySpecifier = (Rune)'_',
  2131. TextDirection = TextDirection.TopBottom_LeftRight
  2132. };
  2133. verticalView.Text = text;
  2134. Application.Top.Add (horizontalView, verticalView);
  2135. Application.Begin (Application.Top);
  2136. ((FakeDriver)Application.Driver).SetBufferSize (50, 50);
  2137. Assert.True (horizontalView.AutoSize);
  2138. Assert.Equal (new Rect (0, 0, 12, 1), horizontalView.Frame);
  2139. Assert.Equal (new Size (12, 1), horizontalView.GetSizeNeededForTextWithoutHotKey ());
  2140. Assert.Equal (horizontalView.Frame.Size, horizontalView.GetSizeNeededForTextWithoutHotKey ());
  2141. Assert.True (verticalView.AutoSize);
  2142. // BUGBUG: v2 - Autosize is broken; disabling this test
  2143. Assert.Equal (new Rect (0, 0, 2, 11), verticalView.Frame);
  2144. Assert.Equal (new Size (2, 11), verticalView.GetSizeNeededForTextWithoutHotKey ());
  2145. //Assert.Equal (new Size (2, 11), verticalView.GetSizeNeededForTextAndHotKey ());
  2146. //Assert.Equal (verticalView.TextFormatter.Size, verticalView.GetSizeNeededForTextAndHotKey ());
  2147. Assert.Equal (verticalView.Frame.Size, verticalView.GetSizeNeededForTextWithoutHotKey ());
  2148. text = "Say He_llo 你";
  2149. horizontalView.Text = text;
  2150. verticalView.Text = text;
  2151. Assert.True (horizontalView.AutoSize);
  2152. Assert.Equal (new Rect (0, 0, 12, 1), horizontalView.Frame);
  2153. Assert.Equal (new Size (12, 1), horizontalView.GetSizeNeededForTextWithoutHotKey ());
  2154. //Assert.Equal (new Size (13, 1), horizontalView.GetSizeNeededForTextAndHotKey ());
  2155. //Assert.Equal (horizontalView.TextFormatter.Size, horizontalView.GetSizeNeededForTextAndHotKey ());
  2156. Assert.Equal (horizontalView.Frame.Size, horizontalView.GetSizeNeededForTextWithoutHotKey ());
  2157. Assert.True (verticalView.AutoSize);
  2158. // BUGBUG: v2 - Autosize is broken; disabling this test
  2159. //Assert.Equal (new Rect (0, 0, 2, 11), verticalView.Frame);
  2160. //Assert.Equal (new Size (2, 11), verticalView.GetSizeNeededForTextWithoutHotKey ());
  2161. //Assert.Equal (new Size (2, 12), verticalView.GetSizeNeededForTextAndHotKey ());
  2162. //Assert.Equal (verticalView.TextFormatter.Size, verticalView.GetSizeNeededForTextAndHotKey ());
  2163. //Assert.Equal (verticalView.Frame.Size, verticalView.GetSizeNeededForTextWithoutHotKey ());
  2164. }
  2165. }