AutoSizeTrueTests.cs 65 KB

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