AutoSizeTrueTests.cs 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064
  1. using System.Text;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.ViewTests;
  4. /// <summary>Tests of the <see cref="View.AutoSize"/> property which auto sizes Views based on <see cref="Text"/>.</summary>
  5. public class AutoSizeTrueTests
  6. {
  7. private readonly ITestOutputHelper _output;
  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. public AutoSizeTrueTests (ITestOutputHelper output) { _output = output; }
  328. [Fact]
  329. [AutoInitShutdown]
  330. public void AutoSize_AnchorEnd_Better_Than_Bottom_Equal_Inside_Window ()
  331. {
  332. var win = new Window ();
  333. // Label is AutoSize == true
  334. var label = new Label
  335. {
  336. Text = "This should be the last line.",
  337. ColorScheme = Colors.ColorSchemes ["Menu"],
  338. //Width = Dim.Fill (),
  339. X = 0, // keep unit test focused; don't use Center here
  340. Y = Pos.AnchorEnd (1)
  341. };
  342. win.Add (label);
  343. Toplevel top = Application.Top;
  344. top.Add (win);
  345. RunState rs = Application.Begin (top);
  346. ((FakeDriver)Application.Driver).SetBufferSize (40, 10);
  347. Assert.True (label.AutoSize);
  348. Assert.Equal (29, label.Text.Length);
  349. Assert.Equal (new Rectangle (0, 0, 40, 10), top.Frame);
  350. Assert.Equal (new Rectangle (0, 0, 40, 10), win.Frame);
  351. Assert.Equal (new Rectangle (0, 7, 29, 1), label.Frame);
  352. var expected = @"
  353. ┌──────────────────────────────────────┐
  354. │ │
  355. │ │
  356. │ │
  357. │ │
  358. │ │
  359. │ │
  360. │ │
  361. │This should be the last line. │
  362. └──────────────────────────────────────┘
  363. ";
  364. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  365. Application.End (rs);
  366. }
  367. [Fact]
  368. [AutoInitShutdown]
  369. public void AutoSize_AnchorEnd_Better_Than_Bottom_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
  370. {
  371. var win = new Window ();
  372. // Label is AutoSize == true
  373. var label = new Label
  374. {
  375. Text = "This should be the last line.",
  376. ColorScheme = Colors.ColorSchemes ["Menu"],
  377. //Width = Dim.Fill (),
  378. X = 0,
  379. Y = Pos.AnchorEnd (1)
  380. };
  381. win.Add (label);
  382. var menu = new MenuBar { Menus = new MenuBarItem [] { new ("Menu", "", null) } };
  383. var status = new StatusBar (new StatusItem [] { new (KeyCode.F1, "~F1~ Help", null) });
  384. Toplevel top = Application.Top;
  385. top.Add (win, menu, status);
  386. RunState rs = Application.Begin (top);
  387. Assert.True (label.AutoSize);
  388. Assert.Equal (new Rectangle (0, 0, 80, 25), top.Frame);
  389. Assert.Equal (new Rectangle (0, 0, 80, 1), menu.Frame);
  390. Assert.Equal (new Rectangle (0, 24, 80, 1), status.Frame);
  391. Assert.Equal (new Rectangle (0, 1, 80, 23), win.Frame);
  392. Assert.Equal (new Rectangle (0, 20, 29, 1), label.Frame);
  393. var expected = @"
  394. Menu
  395. ┌──────────────────────────────────────────────────────────────────────────────┐
  396. │ │
  397. │ │
  398. │ │
  399. │ │
  400. │ │
  401. │ │
  402. │ │
  403. │ │
  404. │ │
  405. │ │
  406. │ │
  407. │ │
  408. │ │
  409. │ │
  410. │ │
  411. │ │
  412. │ │
  413. │ │
  414. │ │
  415. │ │
  416. │This should be the last line. │
  417. └──────────────────────────────────────────────────────────────────────────────┘
  418. F1 Help
  419. ";
  420. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  421. Application.End (rs);
  422. }
  423. [Fact]
  424. [AutoInitShutdown]
  425. public void AutoSize_Bottom_Equal_Inside_Window ()
  426. {
  427. var win = new Window ();
  428. // Label is AutoSize == true
  429. var label = new Label
  430. {
  431. Text = "This should be the last line.",
  432. ColorScheme = Colors.ColorSchemes ["Menu"],
  433. //Width = Dim.Fill (),
  434. X = 0,
  435. Y = Pos.Bottom (win)
  436. - 3 // two lines top and bottom borders more one line above the bottom border
  437. };
  438. win.Add (label);
  439. Toplevel top = Application.Top;
  440. top.Add (win);
  441. RunState rs = Application.Begin (top);
  442. ((FakeDriver)Application.Driver).SetBufferSize (40, 10);
  443. Assert.True (label.AutoSize);
  444. Assert.Equal (new Rectangle (0, 0, 40, 10), top.Frame);
  445. Assert.Equal (new Rectangle (0, 0, 40, 10), win.Frame);
  446. Assert.Equal (new Rectangle (0, 7, 29, 1), label.Frame);
  447. var expected = @"
  448. ┌──────────────────────────────────────┐
  449. │ │
  450. │ │
  451. │ │
  452. │ │
  453. │ │
  454. │ │
  455. │ │
  456. │This should be the last line. │
  457. └──────────────────────────────────────┘
  458. ";
  459. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  460. Application.End (rs);
  461. }
  462. [Fact]
  463. [AutoInitShutdown]
  464. public void AutoSize_Bottom_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
  465. {
  466. var win = new Window ();
  467. // Label is AutoSize == true
  468. var label = new Label
  469. {
  470. Text = "This should be the last line.",
  471. ColorScheme = Colors.ColorSchemes ["Menu"],
  472. //Width = Dim.Fill (),
  473. X = 0,
  474. Y = Pos.Bottom (win)
  475. - 4 // two lines top and bottom borders more two lines above border
  476. };
  477. win.Add (label);
  478. var menu = new MenuBar { Menus = new MenuBarItem [] { new ("Menu", "", null) } };
  479. var status = new StatusBar (new StatusItem [] { new (KeyCode.F1, "~F1~ Help", null) });
  480. Toplevel top = Application.Top;
  481. top.Add (win, menu, status);
  482. RunState rs = Application.Begin (top);
  483. Assert.True (label.AutoSize);
  484. Assert.Equal (new Rectangle (0, 0, 80, 25), top.Frame);
  485. Assert.Equal (new Rectangle (0, 0, 80, 1), menu.Frame);
  486. Assert.Equal (new Rectangle (0, 24, 80, 1), status.Frame);
  487. Assert.Equal (new Rectangle (0, 1, 80, 23), win.Frame);
  488. Assert.Equal (new Rectangle (0, 20, 29, 1), label.Frame);
  489. var expected = @"
  490. Menu
  491. ┌──────────────────────────────────────────────────────────────────────────────┐
  492. │ │
  493. │ │
  494. │ │
  495. │ │
  496. │ │
  497. │ │
  498. │ │
  499. │ │
  500. │ │
  501. │ │
  502. │ │
  503. │ │
  504. │ │
  505. │ │
  506. │ │
  507. │ │
  508. │ │
  509. │ │
  510. │ │
  511. │ │
  512. │This should be the last line. │
  513. └──────────────────────────────────────────────────────────────────────────────┘
  514. F1 Help
  515. ";
  516. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  517. Application.End (rs);
  518. }
  519. [Fact]
  520. [AutoInitShutdown]
  521. public void AutoSize_Dim_Add_Operator_With_Text ()
  522. {
  523. Toplevel top = Application.Top;
  524. var view = new View
  525. {
  526. Text = "View with long text",
  527. X = 0,
  528. Y = 0,
  529. Width = 20,
  530. Height = 1
  531. };
  532. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  533. var count = 0;
  534. // Label is AutoSize == true
  535. List<Label> listLabels = new ();
  536. field.KeyDown += (s, k) =>
  537. {
  538. if (k.KeyCode == KeyCode.Enter)
  539. {
  540. ((FakeDriver)Application.Driver).SetBufferSize (22, count + 4);
  541. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expecteds [count], _output);
  542. Assert.Equal (new Rectangle (0, 0, 22, count + 4), pos);
  543. if (count < 20)
  544. {
  545. field.Text = $"Label {count}";
  546. // Label is AutoSize = true
  547. var label = new Label { Text = field.Text, X = 0, Y = view.Bounds.Height /*, Width = 10*/ };
  548. view.Add (label);
  549. Assert.Equal ($"Label {count}", label.Text);
  550. Assert.Equal ($"Absolute({count + 1})", label.Y.ToString ());
  551. listLabels.Add (label);
  552. //if (count == 0) {
  553. // Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  554. // view.Height += 2;
  555. //} else {
  556. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  557. view.Height += 1;
  558. //}
  559. count++;
  560. }
  561. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  562. }
  563. };
  564. Application.Iteration += (s, a) =>
  565. {
  566. while (count < 21)
  567. {
  568. field.NewKeyDownEvent (Key.Enter);
  569. if (count == 20)
  570. {
  571. field.NewKeyDownEvent (Key.Enter);
  572. break;
  573. }
  574. }
  575. Application.RequestStop ();
  576. };
  577. var win = new Window ();
  578. win.Add (view);
  579. win.Add (field);
  580. top.Add (win);
  581. Application.Run (top);
  582. Assert.Equal (20, count);
  583. Assert.Equal (count, listLabels.Count);
  584. }
  585. [Fact]
  586. [AutoInitShutdown]
  587. public void AutoSize_Dim_Subtract_Operator_With_Text ()
  588. {
  589. Toplevel top = Application.Top;
  590. var view = new View
  591. {
  592. Text = "View with long text",
  593. X = 0,
  594. Y = 0,
  595. Width = 20,
  596. Height = 1
  597. };
  598. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  599. var count = 20;
  600. // Label is AutoSize == true
  601. List<Label> listLabels = new ();
  602. for (var i = 0; i < count; i++)
  603. {
  604. field.Text = $"Label {i}";
  605. var label = new Label { Text = field.Text, X = 0, Y = i + 1 /*, Width = 10*/ };
  606. view.Add (label);
  607. Assert.Equal ($"Label {i}", label.Text);
  608. Assert.Equal ($"Absolute({i + 1})", label.Y.ToString ());
  609. listLabels.Add (label);
  610. if (i == 0)
  611. {
  612. Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
  613. view.Height += 1;
  614. Assert.Equal ($"Absolute({i + 2})", view.Height.ToString ());
  615. }
  616. else
  617. {
  618. Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
  619. view.Height += 1;
  620. Assert.Equal ($"Absolute({i + 2})", view.Height.ToString ());
  621. }
  622. }
  623. field.KeyDown += (s, k) =>
  624. {
  625. if (k.KeyCode == KeyCode.Enter)
  626. {
  627. ((FakeDriver)Application.Driver).SetBufferSize (22, count + 4);
  628. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expecteds [count], _output);
  629. Assert.Equal (new Rectangle (0, 0, 22, count + 4), pos);
  630. if (count > 0)
  631. {
  632. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  633. view.Remove (listLabels [count - 1]);
  634. listLabels [count - 1].Dispose ();
  635. listLabels.RemoveAt (count - 1);
  636. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  637. view.Height -= 1;
  638. count--;
  639. if (listLabels.Count > 0)
  640. {
  641. field.Text = listLabels [count - 1].Text;
  642. }
  643. else
  644. {
  645. field.Text = string.Empty;
  646. }
  647. }
  648. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  649. }
  650. };
  651. Application.Iteration += (s, a) =>
  652. {
  653. while (count > -1)
  654. {
  655. field.NewKeyDownEvent (Key.Enter);
  656. if (count == 0)
  657. {
  658. field.NewKeyDownEvent (Key.Enter);
  659. break;
  660. }
  661. }
  662. Application.RequestStop ();
  663. };
  664. var win = new Window ();
  665. win.Add (view);
  666. win.Add (field);
  667. top.Add (win);
  668. Application.Run (top);
  669. Assert.Equal (0, count);
  670. Assert.Equal (count, listLabels.Count);
  671. }
  672. // [Fact]
  673. // [AutoInitShutdown]
  674. // public void AutoSize_False_Label_IsEmpty_True_Return_Null_Lines ()
  675. // {
  676. // var text = "Label";
  677. // var label = new Label {
  678. // AutoSize = false,
  679. // Height = 1,
  680. // Text = text,
  681. // };
  682. // var win = new Window {
  683. // Width = Dim.Fill (),
  684. // Height = Dim.Fill ()
  685. // };
  686. // win.Add (label);
  687. // Application.Top.Add (win);
  688. // Application.Begin (Application.Top);
  689. // ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  690. // Assert.Equal (5, text.Length);
  691. // Assert.False (label.AutoSize);
  692. // Assert.Equal (new (0, 0, 0, 1), label.Frame);
  693. // Assert.Equal (new (3, 1), label.TextFormatter.Size);
  694. // Assert.Equal (new List<string> { "Lab" }, label.TextFormatter.Lines);
  695. // Assert.Equal (new (0, 0, 10, 4), win.Frame);
  696. // Assert.Equal (new (0, 0, 10, 4), Application.Top.Frame);
  697. // var expected = @"
  698. //┌────────┐
  699. //│Lab │
  700. //│ │
  701. //└────────┘
  702. //";
  703. // var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  704. // Assert.Equal (new (0, 0, 10, 4), pos);
  705. // text = "0123456789";
  706. // Assert.Equal (10, text.Length);
  707. // //label.Width = Dim.Fill () - text.Length;
  708. // Application.Refresh ();
  709. // Assert.False (label.AutoSize);
  710. // Assert.Equal (new (0, 0, 0, 1), label.Frame);
  711. // Assert.Equal (new (0, 1), label.TextFormatter.Size);
  712. // Assert.Equal (new List<string> { string.Empty }, label.TextFormatter.Lines);
  713. // expected = @"
  714. //┌────────┐
  715. //│ │
  716. //│ │
  717. //└────────┘
  718. //";
  719. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  720. // Assert.Equal (new (0, 0, 10, 4), pos);
  721. // }
  722. [Fact]
  723. [SetupFakeDriver]
  724. public void AutoSize_False_Label_Height_Zero_Stays_Zero ()
  725. {
  726. var text = "Label";
  727. var label = new Label { Text = text, AutoSize = false };
  728. label.Width = Dim.Fill () - text.Length;
  729. label.Height = 0;
  730. var win = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  731. win.Add (label);
  732. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  733. win.BeginInit ();
  734. win.EndInit ();
  735. win.LayoutSubviews ();
  736. win.Draw ();
  737. Assert.Equal (5, text.Length);
  738. Assert.False (label.AutoSize);
  739. Assert.Equal (new (0, 0, 3, 0), label.Frame);
  740. Assert.Equal (new (3, 0), label.TextFormatter.Size);
  741. Assert.Single (label.TextFormatter.GetLines ());
  742. Assert.Equal (new (0, 0, 10, 4), win.Frame);
  743. var expected = @"
  744. ┌────────┐
  745. │ │
  746. │ │
  747. └────────┘
  748. ";
  749. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  750. Assert.Equal (new (0, 0, 10, 4), pos);
  751. text = "0123456789";
  752. Assert.Equal (10, text.Length);
  753. label.Width = Dim.Fill () - text.Length;
  754. win.LayoutSubviews ();
  755. win.Clear ();
  756. win.Draw ();
  757. Assert.Equal (Rectangle.Empty, label.Frame);
  758. Assert.Equal (Size.Empty, label.TextFormatter.Size);
  759. Exception exception = Record.Exception (
  760. () => Assert.Equal (
  761. new List<string> { string.Empty },
  762. label.TextFormatter.GetLines ()
  763. )
  764. );
  765. Assert.Null (exception);
  766. expected = @"
  767. ┌────────┐
  768. │ │
  769. │ │
  770. └────────┘
  771. ";
  772. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  773. Assert.Equal (new (0, 0, 10, 4), pos);
  774. }
  775. [Fact]
  776. public void AutoSize_False_SetWidthHeight_With_Dim_Fill_And_Dim_Absolute_With_Initialization ()
  777. {
  778. var win = new Window { Frame = new (0, 0, 30, 80) };
  779. var label = new Label ();
  780. win.Add (label);
  781. win.BeginInit ();
  782. win.EndInit ();
  783. Assert.True (label.AutoSize);
  784. Rectangle expectedLabelBounds = Rectangle.Empty;
  785. Assert.Equal (expectedLabelBounds, label.Bounds);
  786. Assert.True (label.AutoSize);
  787. label.Text = "First line\nSecond line";
  788. win.LayoutSubviews ();
  789. expectedLabelBounds = new (0, 0, 11, 2);
  790. Assert.True (label.AutoSize);
  791. Assert.Equal (expectedLabelBounds, label.Bounds);
  792. label.AutoSize = false;
  793. label.Width = Dim.Fill ();
  794. label.Height = 2;
  795. win.LayoutSubviews ();
  796. // Here the SetMinWidthHeight ensuring the minimum height
  797. // #3127: After: (0,0,28,2) because turning off AutoSize leaves
  798. // Height set to 2.
  799. expectedLabelBounds = new (0, 0, 28, 2);
  800. Assert.False (label.AutoSize);
  801. Assert.Equal (expectedLabelBounds, label.Bounds);
  802. label.Text = "First changed line\nSecond changed line\nNew line";
  803. win.LayoutSubviews ();
  804. // Here the AutoSize is false and the width 28 (Dim.Fill) and
  805. // #3127: Before: height 1 because it wasn't set and SetMinWidthHeight ensuring the minimum height
  806. // #3127: After: (0,0,28,2) because setting Text leaves Height set to 2.
  807. expectedLabelBounds = new (0, 0, 28, 2);
  808. Assert.False (label.AutoSize);
  809. Assert.Equal (expectedLabelBounds, label.Bounds);
  810. label.AutoSize = true;
  811. win.LayoutSubviews ();
  812. // Here the AutoSize ensuring the right size with width 19 (width of longest line)
  813. // and height 3 because the text has 3 lines
  814. expectedLabelBounds = new (0, 0, 19, 3);
  815. Assert.True (label.AutoSize);
  816. Assert.Equal (expectedLabelBounds, label.Bounds);
  817. }
  818. [Fact]
  819. [AutoInitShutdown]
  820. public void AutoSize_GetAutoSize_Centered ()
  821. {
  822. var text = "This is some text.";
  823. var view = new View { Text = text, TextAlignment = TextAlignment.Centered, AutoSize = true };
  824. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  825. win.Add (view);
  826. Application.Top.Add (win);
  827. Application.Begin (Application.Top);
  828. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  829. Size size = view.GetAutoSize ();
  830. Assert.Equal (new (text.Length, 1), size);
  831. view.Text = $"{text}\n{text}";
  832. size = view.GetAutoSize ();
  833. Assert.Equal (new (text.Length, 2), size);
  834. view.Text = $"{text}\n{text}\n{text}+";
  835. size = view.GetAutoSize ();
  836. Assert.Equal (new (text.Length + 1, 3), size);
  837. text = string.Empty;
  838. view.Text = text;
  839. size = view.GetAutoSize ();
  840. Assert.Equal (Size.Empty, size);
  841. text = "1";
  842. view.Text = text;
  843. size = view.GetAutoSize ();
  844. Assert.Equal (_size1x1, size);
  845. text = "界";
  846. view.Text = text;
  847. size = view.GetAutoSize ();
  848. Assert.Equal (new (2, 1), size);
  849. }
  850. [Fact]
  851. [AutoInitShutdown]
  852. public void AutoSize_GetAutoSize_Horizontal ()
  853. {
  854. var text = "text";
  855. var view = new View { Text = text, AutoSize = true };
  856. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  857. win.Add (view);
  858. Application.Top.Add (win);
  859. Application.Begin (Application.Top);
  860. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  861. Size size = view.GetAutoSize ();
  862. Assert.Equal (new (text.Length, 1), size);
  863. view.Text = $"{text}\n{text}";
  864. size = view.GetAutoSize ();
  865. Assert.Equal (new (text.Length, 2), size);
  866. view.Text = $"{text}\n{text}\n{text}+";
  867. size = view.GetAutoSize ();
  868. Assert.Equal (new (text.Length + 1, 3), size);
  869. text = string.Empty;
  870. view.Text = text;
  871. size = view.GetAutoSize ();
  872. Assert.Equal (Size.Empty, size);
  873. text = "1";
  874. view.Text = text;
  875. size = view.GetAutoSize ();
  876. Assert.Equal (_size1x1, size);
  877. text = "界";
  878. view.Text = text;
  879. size = view.GetAutoSize ();
  880. Assert.Equal (new (2, 1), size);
  881. }
  882. [Fact]
  883. [AutoInitShutdown]
  884. public void AutoSize_GetAutoSize_Left ()
  885. {
  886. var text = "This is some text.";
  887. var view = new View { Text = text, TextAlignment = TextAlignment.Left, AutoSize = true };
  888. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  889. win.Add (view);
  890. Application.Top.Add (win);
  891. Application.Begin (Application.Top);
  892. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  893. Size size = view.GetAutoSize ();
  894. Assert.Equal (new (text.Length, 1), size);
  895. view.Text = $"{text}\n{text}";
  896. size = view.GetAutoSize ();
  897. Assert.Equal (new (text.Length, 2), size);
  898. view.Text = $"{text}\n{text}\n{text}+";
  899. size = view.GetAutoSize ();
  900. Assert.Equal (new (text.Length + 1, 3), size);
  901. text = string.Empty;
  902. view.Text = text;
  903. size = view.GetAutoSize ();
  904. Assert.Equal (Size.Empty, size);
  905. text = "1";
  906. view.Text = text;
  907. size = view.GetAutoSize ();
  908. Assert.Equal (_size1x1, size);
  909. text = "界";
  910. view.Text = text;
  911. size = view.GetAutoSize ();
  912. Assert.Equal (new (2, 1), size);
  913. }
  914. [Fact]
  915. [AutoInitShutdown]
  916. public void AutoSize_GetAutoSize_Right ()
  917. {
  918. var text = "This is some text.";
  919. var view = new View { Text = text, TextAlignment = TextAlignment.Right, AutoSize = true };
  920. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  921. win.Add (view);
  922. Application.Top.Add (win);
  923. Application.Begin (Application.Top);
  924. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  925. Size size = view.GetAutoSize ();
  926. Assert.Equal (new (text.Length, 1), size);
  927. view.Text = $"{text}\n{text}";
  928. size = view.GetAutoSize ();
  929. Assert.Equal (new (text.Length, 2), size);
  930. view.Text = $"{text}\n{text}\n{text}+";
  931. size = view.GetAutoSize ();
  932. Assert.Equal (new (text.Length + 1, 3), size);
  933. text = string.Empty;
  934. view.Text = text;
  935. size = view.GetAutoSize ();
  936. Assert.Equal (Size.Empty, size);
  937. text = "1";
  938. view.Text = text;
  939. size = view.GetAutoSize ();
  940. Assert.Equal (_size1x1, size);
  941. text = "界";
  942. view.Text = text;
  943. size = view.GetAutoSize ();
  944. Assert.Equal (new (2, 1), size);
  945. }
  946. [Fact]
  947. [AutoInitShutdown]
  948. public void AutoSize_GetAutoSize_Vertical ()
  949. {
  950. var text = "text";
  951. var view = new View { Text = text, TextDirection = TextDirection.TopBottom_LeftRight, AutoSize = true };
  952. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  953. win.Add (view);
  954. Application.Top.Add (win);
  955. Application.Begin (Application.Top);
  956. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  957. Size size = view.GetAutoSize ();
  958. Assert.Equal (new (1, text.Length), size);
  959. view.Text = $"{text}\n{text}";
  960. size = view.GetAutoSize ();
  961. Assert.Equal (new (2, text.Length), size);
  962. view.Text = $"{text}\n{text}\n{text}+";
  963. size = view.GetAutoSize ();
  964. Assert.Equal (new (3, text.Length + 1), size);
  965. text = string.Empty;
  966. view.Text = text;
  967. size = view.GetAutoSize ();
  968. Assert.Equal (Size.Empty, size);
  969. text = "1";
  970. view.Text = text;
  971. size = view.GetAutoSize ();
  972. Assert.Equal (_size1x1, size);
  973. text = "界";
  974. view.Text = text;
  975. size = view.GetAutoSize ();
  976. Assert.Equal (new (2, 1), size);
  977. }
  978. [Fact]
  979. [SetupFakeDriver]
  980. public void AutoSize_Label_Set_AutoSize_To_False_Height_Positive_Does_Not_Change ()
  981. {
  982. var text = "Label";
  983. var label = new Label { Text = text };
  984. Assert.Equal ("Absolute(1)", label.Height.ToString ());
  985. label.AutoSize = false;
  986. label.Width = Dim.Fill () - text.Length;
  987. label.Height = 1;
  988. Assert.Equal ("Absolute(1)", label.Height.ToString ());
  989. var win = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  990. win.Add (label);
  991. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  992. win.BeginInit ();
  993. win.EndInit ();
  994. win.LayoutSubviews ();
  995. win.Draw ();
  996. Assert.Equal (5, text.Length);
  997. Assert.False (label.AutoSize);
  998. Assert.Equal (new (0, 0, 3, 1), label.Frame);
  999. Assert.Equal (new (3, 1), label.TextFormatter.Size);
  1000. Assert.Single (label.TextFormatter.GetLines ());
  1001. Assert.Equal (new (0, 0, 10, 4), win.Frame);
  1002. var expected = @"
  1003. ┌────────┐
  1004. │Lab │
  1005. │ │
  1006. └────────┘
  1007. ";
  1008. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1009. Assert.Equal (new (0, 0, 10, 4), pos);
  1010. text = "0123456789";
  1011. Assert.Equal (10, text.Length);
  1012. label.Width = Dim.Fill () - text.Length;
  1013. win.LayoutSubviews ();
  1014. win.Clear ();
  1015. win.Draw ();
  1016. Assert.Equal (new (0, 0, 0, 1), label.Frame);
  1017. Assert.Equal (new (0, 1), label.TextFormatter.Size);
  1018. Exception exception = Record.Exception (
  1019. () => Assert.Equal (
  1020. new List<string> { string.Empty },
  1021. label.TextFormatter.GetLines ()
  1022. )
  1023. );
  1024. Assert.Null (exception);
  1025. expected = @"
  1026. ┌────────┐
  1027. │ │
  1028. │ │
  1029. └────────┘
  1030. ";
  1031. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1032. Assert.Equal (new (0, 0, 10, 4), pos);
  1033. }
  1034. [Fact]
  1035. [AutoInitShutdown]
  1036. public void AutoSize_Stays_True_Center_HotKeySpecifier ()
  1037. {
  1038. var label = new Label { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
  1039. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
  1040. win.Add (label);
  1041. Application.Top.Add (win);
  1042. Assert.True (label.AutoSize);
  1043. RunState rs = Application.Begin (Application.Top);
  1044. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  1045. var expected = @"
  1046. ┌┤Test Demo 你├──────────────┐
  1047. │ │
  1048. │ Say Hello 你 │
  1049. │ │
  1050. └────────────────────────────┘
  1051. ";
  1052. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1053. Assert.True (label.AutoSize);
  1054. label.Text = "Say Hello 你 changed";
  1055. Assert.True (label.AutoSize);
  1056. Application.Refresh ();
  1057. expected = @"
  1058. ┌┤Test Demo 你├──────────────┐
  1059. │ │
  1060. │ Say Hello 你 changed │
  1061. │ │
  1062. └────────────────────────────┘
  1063. ";
  1064. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1065. Application.End (rs);
  1066. }
  1067. [Fact]
  1068. public void AutoSize_True_Equal_Before_And_After_IsInitialized_With_Different_Orders ()
  1069. {
  1070. var top = new Toplevel ();
  1071. var view1 = new View
  1072. {
  1073. Text = "Say Hello view1 你", AutoSize = true /*, Width = 10, Height = 5*/, ValidatePosDim = true
  1074. };
  1075. var view2 = new View
  1076. {
  1077. Text = "Say Hello view2 你",
  1078. Width = 10,
  1079. Height = 5,
  1080. AutoSize = true,
  1081. ValidatePosDim = true
  1082. };
  1083. var view3 = new View
  1084. {
  1085. AutoSize = true /*, Width = 10, Height = 5*/, Text = "Say Hello view3 你", ValidatePosDim = true
  1086. };
  1087. var view4 = new View
  1088. {
  1089. Text = "Say Hello view4 你",
  1090. AutoSize = true,
  1091. //Width = 10,
  1092. //Height = 5,
  1093. TextDirection = TextDirection.TopBottom_LeftRight,
  1094. ValidatePosDim = true
  1095. };
  1096. var view5 = new View
  1097. {
  1098. Text = "Say Hello view5 你",
  1099. AutoSize = true,
  1100. //Width = 10,
  1101. //Height = 5,
  1102. TextDirection = TextDirection.TopBottom_LeftRight,
  1103. ValidatePosDim = true
  1104. };
  1105. var view6 = new View
  1106. {
  1107. AutoSize = true,
  1108. //Width = 10,
  1109. //Height = 5,
  1110. TextDirection = TextDirection.TopBottom_LeftRight,
  1111. Text = "Say Hello view6 你",
  1112. ValidatePosDim = true
  1113. };
  1114. top.Add (view1, view2, view3, view4, view5, view6);
  1115. Assert.False (view1.IsInitialized);
  1116. Assert.False (view2.IsInitialized);
  1117. Assert.False (view3.IsInitialized);
  1118. Assert.False (view4.IsInitialized);
  1119. Assert.False (view5.IsInitialized);
  1120. Assert.True (view1.AutoSize);
  1121. Assert.Equal (new (0, 0, 18, 1), view1.Frame);
  1122. Assert.Equal ("Absolute(18)", view1.Width.ToString ());
  1123. Assert.Equal ("Absolute(1)", view1.Height.ToString ());
  1124. Assert.True (view2.AutoSize);
  1125. Assert.Equal ("Say Hello view2 你".GetColumns (), view2.Width);
  1126. Assert.Equal (18, view2.Width);
  1127. Assert.Equal (new (0, 0, 18, 5), view2.Frame);
  1128. Assert.Equal ("Absolute(18)", view2.Width.ToString ());
  1129. Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  1130. Assert.True (view3.AutoSize);
  1131. Assert.Equal (new (0, 0, 18, 1), view3.Frame); // BUGBUG: AutoSize = true, so the height should be 1.
  1132. Assert.Equal ("Absolute(18)", view2.Width.ToString ());
  1133. Assert.Equal ("Absolute(1)", view3.Height.ToString ());
  1134. Assert.True (view4.AutoSize);
  1135. Assert.Equal ("Say Hello view4 你".GetColumns (), view2.Width);
  1136. Assert.Equal (18, view2.Width);
  1137. Assert.Equal (new (0, 0, 18, 17), view4.Frame);
  1138. Assert.Equal ("Absolute(18)", view4.Width.ToString ());
  1139. Assert.Equal ("Absolute(17)", view4.Height.ToString ());
  1140. Assert.True (view5.AutoSize);
  1141. Assert.Equal (new (0, 0, 18, 17), view5.Frame);
  1142. Assert.True (view6.AutoSize);
  1143. Assert.Equal (new (0, 0, 2, 17), view6.Frame); // BUGBUG: AutoSize = true, so the Width should be 2.
  1144. top.BeginInit ();
  1145. top.EndInit ();
  1146. Assert.True (view1.IsInitialized);
  1147. Assert.True (view2.IsInitialized);
  1148. Assert.True (view3.IsInitialized);
  1149. Assert.True (view4.IsInitialized);
  1150. Assert.True (view5.IsInitialized);
  1151. Assert.True (view1.AutoSize);
  1152. Assert.Equal (new (0, 0, 18, 1), view1.Frame);
  1153. Assert.Equal ("Absolute(18)", view1.Width.ToString ());
  1154. Assert.Equal ("Absolute(1)", view1.Height.ToString ());
  1155. Assert.True (view2.AutoSize);
  1156. Assert.Equal (new (0, 0, 18, 5), view2.Frame);
  1157. Assert.Equal ("Absolute(18)", view2.Width.ToString ());
  1158. Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  1159. Assert.True (view3.AutoSize);
  1160. Assert.Equal (new (0, 0, 18, 1), view3.Frame); // BUGBUG: AutoSize = true, so the height should be 1.
  1161. Assert.Equal ("Absolute(18)", view5.Width.ToString ());
  1162. Assert.Equal ("Absolute(1)", view3.Height.ToString ());
  1163. Assert.True (view4.AutoSize);
  1164. Assert.Equal (new (0, 0, 18, 17), view4.Frame);
  1165. Assert.Equal ("Absolute(18)", view5.Width.ToString ());
  1166. Assert.Equal ("Absolute(17)", view4.Height.ToString ());
  1167. Assert.True (view5.AutoSize);
  1168. Assert.Equal (new (0, 0, 18, 17), view5.Frame);
  1169. Assert.Equal ("Absolute(18)", view5.Width.ToString ());
  1170. Assert.Equal ("Absolute(17)", view5.Height.ToString ());
  1171. Assert.True (view6.AutoSize);
  1172. Assert.Equal (new (0, 0, 2, 17), view6.Frame); // BUGBUG: AutoSize = true, so the Width should be 2.
  1173. Assert.Equal ("Absolute(2)", view6.Width.ToString ());
  1174. Assert.Equal ("Absolute(17)", view6.Height.ToString ());
  1175. }
  1176. [Fact]
  1177. public void AutoSize_True_Label_If_Text_Empty ()
  1178. {
  1179. var label1 = new Label ();
  1180. var label3 = new Label { Text = "" };
  1181. Assert.True (label1.AutoSize);
  1182. Assert.True (label3.AutoSize);
  1183. label1.Dispose ();
  1184. label3.Dispose ();
  1185. }
  1186. [Fact]
  1187. public void AutoSize_True_Label_If_Text_Is_Not_Empty ()
  1188. {
  1189. var label1 = new Label ();
  1190. label1.Text = "Hello World";
  1191. var label3 = new Label { Text = "Hello World" };
  1192. Assert.True (label1.AutoSize);
  1193. Assert.True (label3.AutoSize);
  1194. label1.Dispose ();
  1195. label3.Dispose ();
  1196. }
  1197. [Fact]
  1198. [AutoInitShutdown]
  1199. public void AutoSize_True_Label_IsEmpty_False_Minimum_Height ()
  1200. {
  1201. var text = "Label";
  1202. var label = new Label
  1203. {
  1204. //Width = Dim.Fill () - text.Length,
  1205. Text = text
  1206. };
  1207. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  1208. win.Add (label);
  1209. Application.Top.Add (win);
  1210. Application.Begin (Application.Top);
  1211. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  1212. Assert.Equal (5, text.Length);
  1213. Assert.True (label.AutoSize);
  1214. Assert.Equal (new (0, 0, 5, 1), label.Frame);
  1215. Assert.Equal (new (5, 1), label.TextFormatter.Size);
  1216. Assert.Equal (["Label"], label.TextFormatter.GetLines ());
  1217. Assert.Equal (new (0, 0, 10, 4), win.Frame);
  1218. Assert.Equal (new (0, 0, 10, 4), Application.Top.Frame);
  1219. var expected = @"
  1220. ┌────────┐
  1221. │Label │
  1222. │ │
  1223. └────────┘
  1224. ";
  1225. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1226. Assert.Equal (new (0, 0, 10, 4), pos);
  1227. text = "0123456789";
  1228. Assert.Equal (10, text.Length);
  1229. //label.Width = Dim.Fill () - text.Length;
  1230. Application.Refresh ();
  1231. Assert.Equal (new (0, 0, 5, 1), label.Frame);
  1232. Assert.Equal (new (5, 1), label.TextFormatter.Size);
  1233. Exception exception = Record.Exception (() => Assert.Single (label.TextFormatter.GetLines ()));
  1234. Assert.Null (exception);
  1235. expected = @"
  1236. ┌────────┐
  1237. │Label │
  1238. │ │
  1239. └────────┘
  1240. ";
  1241. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1242. Assert.Equal (new (0, 0, 10, 4), pos);
  1243. }
  1244. [Fact]
  1245. [AutoInitShutdown]
  1246. public void AutoSize_True_Label_IsEmpty_False_Never_Return_Null_Lines ()
  1247. {
  1248. var text = "Label";
  1249. var label = new Label
  1250. {
  1251. //Width = Dim.Fill () - text.Length,
  1252. //Height = 1,
  1253. Text = text
  1254. };
  1255. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  1256. win.Add (label);
  1257. Application.Top.Add (win);
  1258. Application.Begin (Application.Top);
  1259. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  1260. Assert.Equal (5, text.Length);
  1261. Assert.True (label.AutoSize);
  1262. Assert.Equal (new (0, 0, 5, 1), label.Frame);
  1263. Assert.Equal (new (5, 1), label.TextFormatter.Size);
  1264. Assert.Equal (["Label"], label.TextFormatter.GetLines ());
  1265. Assert.Equal (new (0, 0, 10, 4), win.Frame);
  1266. Assert.Equal (new (0, 0, 10, 4), Application.Top.Frame);
  1267. var expected = @"
  1268. ┌────────┐
  1269. │Label │
  1270. │ │
  1271. └────────┘
  1272. ";
  1273. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1274. Assert.Equal (new (0, 0, 10, 4), pos);
  1275. text = "0123456789";
  1276. Assert.Equal (10, text.Length);
  1277. //label.Width = Dim.Fill () - text.Length;
  1278. Application.Refresh ();
  1279. Assert.True (label.AutoSize);
  1280. Assert.Equal (new (0, 0, 5, 1), label.Frame);
  1281. Assert.Equal (new (5, 1), label.TextFormatter.Size);
  1282. Assert.Single (label.TextFormatter.GetLines ());
  1283. expected = @"
  1284. ┌────────┐
  1285. │Label │
  1286. │ │
  1287. └────────┘
  1288. ";
  1289. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1290. Assert.Equal (new (0, 0, 10, 4), pos);
  1291. }
  1292. [Fact]
  1293. public void AutoSize_True_ResizeView_With_Dim_Absolute ()
  1294. {
  1295. var super = new View ();
  1296. var label = new Label ();
  1297. label.Text = "New text";
  1298. super.Add (label);
  1299. super.LayoutSubviews ();
  1300. Assert.True (label.AutoSize);
  1301. Rectangle expectedLabelBounds = new (0, 0, 8, 1);
  1302. Assert.Equal (expectedLabelBounds, label.Bounds);
  1303. super.Dispose ();
  1304. }
  1305. [Fact]
  1306. [SetupFakeDriver]
  1307. public void AutoSize_True_Setting_With_Height_Horizontal ()
  1308. {
  1309. var top = new View { Width = 25, Height = 25 };
  1310. var label = new Label { Text = "Hello", /* Width = 10, Height = 2, */ ValidatePosDim = true };
  1311. var viewX = new View { Text = "X", X = Pos.Right (label), Width = 1, Height = 1 };
  1312. var viewY = new View { Text = "Y", Y = Pos.Bottom (label), Width = 1, Height = 1 };
  1313. top.Add (label, viewX, viewY);
  1314. top.BeginInit ();
  1315. top.EndInit ();
  1316. Assert.True (label.AutoSize);
  1317. Assert.Equal (new (0, 0, 5, 1), label.Frame);
  1318. top.LayoutSubviews ();
  1319. top.Draw ();
  1320. var expected = @"
  1321. HelloX
  1322. Y
  1323. ";
  1324. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1325. label.AutoSize = false;
  1326. label.Width = 10;
  1327. label.Height = 2;
  1328. Assert.False (label.AutoSize);
  1329. Assert.Equal (new (0, 0, 10, 2), label.Frame);
  1330. top.LayoutSubviews ();
  1331. top.Draw ();
  1332. expected = @"
  1333. Hello X
  1334. Y
  1335. ";
  1336. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1337. }
  1338. [Fact]
  1339. [AutoInitShutdown]
  1340. public void AutoSize_True_Setting_With_Height_Vertical ()
  1341. {
  1342. // BUGBUG: Label is AutoSize = true, so Width & Height are ignored
  1343. var label = new Label
  1344. { /*Width = 2, Height = 10, */
  1345. TextDirection = TextDirection.TopBottom_LeftRight, ValidatePosDim = true
  1346. };
  1347. var viewX = new View { Text = "X", X = Pos.Right (label), Width = 1, Height = 1 };
  1348. var viewY = new View { Text = "Y", Y = Pos.Bottom (label), Width = 1, Height = 1 };
  1349. Application.Top.Add (label, viewX, viewY);
  1350. RunState rs = Application.Begin (Application.Top);
  1351. Assert.True (label.AutoSize);
  1352. label.Text = "Hello";
  1353. Application.Refresh ();
  1354. Assert.Equal (new (0, 0, 1, 5), label.Frame); // BUGBUG: AutoSize = true, so the Width should be 1.
  1355. var expected = @"
  1356. HX
  1357. e
  1358. l
  1359. l
  1360. o
  1361. Y
  1362. ";
  1363. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1364. label.AutoSize = false;
  1365. label.Width = 2;
  1366. label.Height = 10;
  1367. Application.Refresh ();
  1368. Assert.False (label.AutoSize);
  1369. Assert.Equal (new (0, 0, 2, 10), label.Frame);
  1370. expected = @"
  1371. H X
  1372. e
  1373. l
  1374. l
  1375. o
  1376. Y
  1377. "
  1378. ;
  1379. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1380. Application.End (rs);
  1381. }
  1382. [Fact]
  1383. [AutoInitShutdown]
  1384. public void AutoSize_True_TextDirection_Toggle ()
  1385. {
  1386. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  1387. // View is AutoSize == true
  1388. var view = new View ();
  1389. win.Add (view);
  1390. Application.Top.Add (win);
  1391. RunState rs = Application.Begin (Application.Top);
  1392. ((FakeDriver)Application.Driver).SetBufferSize (15, 15);
  1393. Assert.Equal (new Rectangle (0, 0, 15, 15), win.Frame);
  1394. Assert.Equal (new Rectangle (0, 0, 15, 15), win.Margin.Frame);
  1395. Assert.Equal (new Rectangle (0, 0, 15, 15), win.Border.Frame);
  1396. Assert.Equal (new Rectangle (1, 1, 13, 13), win.Padding.Frame);
  1397. Assert.False (view.AutoSize);
  1398. Assert.Equal (TextDirection.LeftRight_TopBottom, view.TextDirection);
  1399. Assert.Equal (Rectangle.Empty, view.Frame);
  1400. Assert.Equal ("Absolute(0)", view.X.ToString ());
  1401. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1402. Assert.Equal ("Absolute(0)", view.Width.ToString ());
  1403. Assert.Equal ("Absolute(0)", view.Height.ToString ());
  1404. var expected = @"
  1405. ┌─────────────┐
  1406. │ │
  1407. │ │
  1408. │ │
  1409. │ │
  1410. │ │
  1411. │ │
  1412. │ │
  1413. │ │
  1414. │ │
  1415. │ │
  1416. │ │
  1417. │ │
  1418. │ │
  1419. └─────────────┘
  1420. ";
  1421. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1422. view.Text = "Hello World";
  1423. view.Width = 11;
  1424. view.Height = 1;
  1425. win.LayoutSubviews ();
  1426. Application.Refresh ();
  1427. Assert.Equal (new Rectangle (0, 0, 11, 1), view.Frame);
  1428. Assert.Equal ("Absolute(0)", view.X.ToString ());
  1429. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1430. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  1431. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  1432. expected = @"
  1433. ┌─────────────┐
  1434. │Hello World │
  1435. │ │
  1436. │ │
  1437. │ │
  1438. │ │
  1439. │ │
  1440. │ │
  1441. │ │
  1442. │ │
  1443. │ │
  1444. │ │
  1445. │ │
  1446. │ │
  1447. └─────────────┘
  1448. ";
  1449. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1450. view.AutoSize = true;
  1451. view.Text = "Hello Worlds";
  1452. Application.Refresh ();
  1453. int len = "Hello Worlds".Length;
  1454. Assert.Equal (12, len);
  1455. Assert.Equal (new Rectangle (0, 0, len, 1), view.Frame);
  1456. Assert.Equal ("Absolute(0)", view.X.ToString ());
  1457. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1458. Assert.Equal ("Absolute(12)", view.Width.ToString ());
  1459. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  1460. expected = @"
  1461. ┌─────────────┐
  1462. │Hello Worlds │
  1463. │ │
  1464. │ │
  1465. │ │
  1466. │ │
  1467. │ │
  1468. │ │
  1469. │ │
  1470. │ │
  1471. │ │
  1472. │ │
  1473. │ │
  1474. │ │
  1475. └─────────────┘
  1476. ";
  1477. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1478. view.TextDirection = TextDirection.TopBottom_LeftRight;
  1479. Application.Refresh ();
  1480. Assert.Equal (new Rectangle (0, 0, 12, 12), view.Frame);
  1481. Assert.Equal ("Absolute(0)", view.X.ToString ());
  1482. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1483. Assert.Equal ("Absolute(12)", view.Width.ToString ());
  1484. Assert.Equal ("Absolute(12)", view.Height.ToString ());
  1485. expected = @"
  1486. ┌─────────────┐
  1487. │H │
  1488. │e │
  1489. │l │
  1490. │l │
  1491. │o │
  1492. │ │
  1493. │W │
  1494. │o │
  1495. │r │
  1496. │l │
  1497. │d │
  1498. │s │
  1499. │ │
  1500. └─────────────┘
  1501. ";
  1502. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1503. view.AutoSize = false;
  1504. view.Height = 1;
  1505. Application.Refresh ();
  1506. Assert.Equal (new Rectangle (0, 0, 12, 1), view.Frame);
  1507. Assert.Equal ("Absolute(0)", view.X.ToString ());
  1508. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1509. Assert.Equal ("Absolute(12)", view.Width.ToString ());
  1510. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  1511. // TextDirection.TopBottom_LeftRight - Height of 1 and Width of 12 means
  1512. // that the text will be spread "vertically" across 1 line.
  1513. // Hence no space.
  1514. expected = @"
  1515. ┌─────────────┐
  1516. │HelloWorlds │
  1517. │ │
  1518. │ │
  1519. │ │
  1520. │ │
  1521. │ │
  1522. │ │
  1523. │ │
  1524. │ │
  1525. │ │
  1526. │ │
  1527. │ │
  1528. │ │
  1529. └─────────────┘
  1530. ";
  1531. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1532. view.PreserveTrailingSpaces = true;
  1533. Application.Refresh ();
  1534. Assert.Equal (new Rectangle (0, 0, 12, 1), view.Frame);
  1535. Assert.Equal ("Absolute(0)", view.X.ToString ());
  1536. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1537. Assert.Equal ("Absolute(12)", view.Width.ToString ());
  1538. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  1539. expected = @"
  1540. ┌─────────────┐
  1541. │Hello Worlds │
  1542. │ │
  1543. │ │
  1544. │ │
  1545. │ │
  1546. │ │
  1547. │ │
  1548. │ │
  1549. │ │
  1550. │ │
  1551. │ │
  1552. │ │
  1553. │ │
  1554. └─────────────┘
  1555. ";
  1556. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1557. view.PreserveTrailingSpaces = false;
  1558. Rectangle f = view.Frame;
  1559. view.Width = f.Height;
  1560. view.Height = f.Width;
  1561. view.TextDirection = TextDirection.TopBottom_LeftRight;
  1562. Application.Refresh ();
  1563. Assert.Equal (new Rectangle (0, 0, 1, 12), view.Frame);
  1564. Assert.Equal ("Absolute(0)", view.X.ToString ());
  1565. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1566. Assert.Equal ("Absolute(1)", view.Width.ToString ());
  1567. Assert.Equal ("Absolute(12)", view.Height.ToString ());
  1568. expected = @"
  1569. ┌─────────────┐
  1570. │H │
  1571. │e │
  1572. │l │
  1573. │l │
  1574. │o │
  1575. │ │
  1576. │W │
  1577. │o │
  1578. │r │
  1579. │l │
  1580. │d │
  1581. │s │
  1582. │ │
  1583. └─────────────┘
  1584. ";
  1585. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1586. view.AutoSize = true;
  1587. Application.Refresh ();
  1588. Assert.Equal (new Rectangle (0, 0, 1, 12), view.Frame);
  1589. Assert.Equal ("Absolute(0)", view.X.ToString ());
  1590. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1591. Assert.Equal ("Absolute(1)", view.Width.ToString ());
  1592. Assert.Equal ("Absolute(12)", view.Height.ToString ());
  1593. expected = @"
  1594. ┌─────────────┐
  1595. │H │
  1596. │e │
  1597. │l │
  1598. │l │
  1599. │o │
  1600. │ │
  1601. │W │
  1602. │o │
  1603. │r │
  1604. │l │
  1605. │d │
  1606. │s │
  1607. │ │
  1608. └─────────────┘
  1609. ";
  1610. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1611. Application.End (rs);
  1612. }
  1613. [Fact]
  1614. [AutoInitShutdown]
  1615. public void AutoSize_True_View_IsEmpty_False_Minimum_Width ()
  1616. {
  1617. var text = "Views";
  1618. var view = new View
  1619. {
  1620. TextDirection = TextDirection.TopBottom_LeftRight,
  1621. Height = Dim.Fill () - text.Length,
  1622. Text = text,
  1623. AutoSize = true
  1624. };
  1625. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  1626. win.Add (view);
  1627. Application.Top.Add (win);
  1628. Application.Begin (Application.Top);
  1629. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  1630. Assert.Equal (5, text.Length);
  1631. Assert.True (view.AutoSize);
  1632. Assert.Equal (new (0, 0, 1, 5), view.Frame);
  1633. Assert.Equal (new (1, 5), view.TextFormatter.Size);
  1634. Assert.Equal (new List<string> { "Views" }, view.TextFormatter.GetLines ());
  1635. Assert.Equal (new (0, 0, 4, 10), win.Frame);
  1636. Assert.Equal (new (0, 0, 4, 10), Application.Top.Frame);
  1637. var expected = @"
  1638. ┌──┐
  1639. │V │
  1640. │i │
  1641. │e │
  1642. │w │
  1643. │s │
  1644. │ │
  1645. │ │
  1646. │ │
  1647. └──┘
  1648. ";
  1649. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1650. Assert.Equal (new (0, 0, 4, 10), pos);
  1651. text = "0123456789";
  1652. Assert.Equal (10, text.Length);
  1653. //view.Height = Dim.Fill () - text.Length;
  1654. Application.Refresh ();
  1655. Assert.Equal (new (0, 0, 1, 5), view.Frame);
  1656. Assert.Equal (new (1, 5), view.TextFormatter.Size);
  1657. Exception exception = Record.Exception (() => Assert.Single (view.TextFormatter.GetLines ()));
  1658. Assert.Null (exception);
  1659. expected = @"
  1660. ┌──┐
  1661. │V │
  1662. │i │
  1663. │e │
  1664. │w │
  1665. │s │
  1666. │ │
  1667. │ │
  1668. │ │
  1669. └──┘
  1670. ";
  1671. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1672. Assert.Equal (new Rectangle (0, 0, 4, 10), pos);
  1673. }
  1674. [Fact]
  1675. [AutoInitShutdown]
  1676. public void AutoSize_True_View_IsEmpty_False_Minimum_Width_Wide_Rune ()
  1677. {
  1678. var text = "界View";
  1679. var view = new View
  1680. {
  1681. TextDirection = TextDirection.TopBottom_LeftRight,
  1682. Height = Dim.Fill () - text.Length,
  1683. Text = text,
  1684. AutoSize = true
  1685. };
  1686. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  1687. win.Add (view);
  1688. Application.Top.Add (win);
  1689. Application.Begin (Application.Top);
  1690. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  1691. Assert.Equal (5, text.Length);
  1692. Assert.True (view.AutoSize);
  1693. Assert.Equal (new (0, 0, 2, 5), view.Frame);
  1694. Assert.Equal (new (2, 5), view.TextFormatter.Size);
  1695. Assert.Equal (new List<string> { "界View" }, view.TextFormatter.GetLines ());
  1696. Assert.Equal (new (0, 0, 4, 10), win.Frame);
  1697. Assert.Equal (new (0, 0, 4, 10), Application.Top.Frame);
  1698. var expected = @"
  1699. ┌──┐
  1700. │界│
  1701. │V │
  1702. │i │
  1703. │e │
  1704. │w │
  1705. │ │
  1706. │ │
  1707. │ │
  1708. └──┘
  1709. ";
  1710. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1711. Assert.Equal (new (0, 0, 4, 10), pos);
  1712. text = "0123456789";
  1713. Assert.Equal (10, text.Length);
  1714. //view.Height = Dim.Fill () - text.Length;
  1715. Application.Refresh ();
  1716. Assert.Equal (new (0, 0, 2, 5), view.Frame);
  1717. Assert.Equal (new (2, 5), view.TextFormatter.Size);
  1718. Exception exception = Record.Exception (
  1719. () => Assert.Equal (
  1720. new List<string> { "界View" },
  1721. view.TextFormatter.GetLines ()
  1722. )
  1723. );
  1724. Assert.Null (exception);
  1725. expected = @"
  1726. ┌──┐
  1727. │界│
  1728. │V │
  1729. │i │
  1730. │e │
  1731. │w │
  1732. │ │
  1733. │ │
  1734. │ │
  1735. └──┘
  1736. ";
  1737. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1738. Assert.Equal (new Rectangle (0, 0, 4, 10), pos);
  1739. }
  1740. [Fact]
  1741. [AutoInitShutdown]
  1742. public void AutoSize_True_Width_Height_SetMinWidthHeight_Narrow_Wide_Runes ()
  1743. {
  1744. var text = $"First line{Environment.NewLine}Second line";
  1745. var horizontalView = new View
  1746. {
  1747. AutoSize = true,
  1748. //Width = 20, // BUGBUG: These are ignored
  1749. //Height = 1, // BUGBUG: These are ignored
  1750. Text = text
  1751. };
  1752. var verticalView = new View
  1753. {
  1754. AutoSize = true,
  1755. Y = 3,
  1756. //Height = 20, // BUGBUG: These are ignored
  1757. //Width = 1, // BUGBUG: These are ignored
  1758. Text = text,
  1759. TextDirection = TextDirection.TopBottom_LeftRight
  1760. };
  1761. var win = new Window
  1762. {
  1763. AutoSize = true,
  1764. //Width = Dim.Fill (), // BUGBUG: These are ignored
  1765. //Height = Dim.Fill (),// BUGBUG: These are ignored
  1766. Text = "Window"
  1767. };
  1768. win.Add (horizontalView, verticalView);
  1769. Application.Top.Add (win);
  1770. RunState rs = Application.Begin (Application.Top);
  1771. ((FakeDriver)Application.Driver).SetBufferSize (20, 20);
  1772. Assert.True (horizontalView.AutoSize);
  1773. Assert.True (verticalView.AutoSize);
  1774. Assert.Equal (new Rectangle (0, 0, 11, 2), horizontalView.Frame);
  1775. Assert.Equal (new Rectangle (0, 3, 11, 11), verticalView.Frame);
  1776. var expected = @"
  1777. ┌──────────────────┐
  1778. │First line │
  1779. │Second line │
  1780. │ │
  1781. │FS │
  1782. │ie │
  1783. │rc │
  1784. │so │
  1785. │tn │
  1786. │ d │
  1787. │l │
  1788. │il │
  1789. │ni │
  1790. │en │
  1791. │ e │
  1792. │ │
  1793. │ │
  1794. │ │
  1795. │ │
  1796. └──────────────────┘
  1797. ";
  1798. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1799. verticalView.Text = $"最初の行{Environment.NewLine}二行目";
  1800. Application.Top.Draw ();
  1801. Assert.Equal (new Rectangle (0, 3, 11, 11), verticalView.Frame);
  1802. expected = @"
  1803. ┌──────────────────┐
  1804. │First line │
  1805. │Second line │
  1806. │ │
  1807. │最二 │
  1808. │初行 │
  1809. │の目 │
  1810. │行 │
  1811. │ │
  1812. │ │
  1813. │ │
  1814. │ │
  1815. │ │
  1816. │ │
  1817. │ │
  1818. │ │
  1819. │ │
  1820. │ │
  1821. │ │
  1822. └──────────────────┘
  1823. ";
  1824. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1825. Application.End (rs);
  1826. }
  1827. [Fact]
  1828. [AutoInitShutdown]
  1829. public void AutoSize_True_Width_Height_Stay_True_If_TextFormatter_Size_Fit ()
  1830. {
  1831. var text = "Fi_nish 終";
  1832. var horizontalView = new View
  1833. {
  1834. Id = "horizontalView", AutoSize = true, Text = text
  1835. };
  1836. var verticalView = new View
  1837. {
  1838. Id = "verticalView",
  1839. Y = 3,
  1840. AutoSize = true,
  1841. Text = text,
  1842. TextDirection = TextDirection.TopBottom_LeftRight
  1843. };
  1844. var win = new Window { Id = "win", Width = Dim.Fill (), Height = Dim.Fill (), Text = "Window" };
  1845. win.Add (horizontalView, verticalView);
  1846. Application.Top.Add (win);
  1847. RunState rs = Application.Begin (Application.Top);
  1848. ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  1849. Assert.True (horizontalView.AutoSize);
  1850. Assert.True (verticalView.AutoSize);
  1851. Assert.Equal (new (text.GetColumns (), 1), horizontalView.TextFormatter.Size);
  1852. Assert.Equal (new (2, 9), verticalView.TextFormatter.Size);
  1853. Assert.Equal (new (0, 0, 10, 1), horizontalView.Frame);
  1854. Assert.Equal (new (0, 3, 10, 9), verticalView.Frame);
  1855. var expected = @"
  1856. ┌────────────────────┐
  1857. │Fi_nish 終 │
  1858. │ │
  1859. │ │
  1860. │F │
  1861. │i │
  1862. │_ │
  1863. │n │
  1864. │i │
  1865. │s │
  1866. │h │
  1867. │ │
  1868. │終 │
  1869. │ │
  1870. │ │
  1871. │ │
  1872. │ │
  1873. │ │
  1874. │ │
  1875. │ │
  1876. │ │
  1877. └────────────────────┘
  1878. ";
  1879. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1880. verticalView.Text = "最初_の行二行目";
  1881. Application.Top.Draw ();
  1882. Assert.True (horizontalView.AutoSize);
  1883. Assert.True (verticalView.AutoSize);
  1884. // height was initialized with 8 and can only grow or keep initial value
  1885. Assert.Equal (new Rectangle (0, 3, 10, 9), verticalView.Frame);
  1886. expected = @"
  1887. ┌────────────────────┐
  1888. │Fi_nish 終 │
  1889. │ │
  1890. │ │
  1891. │最 │
  1892. │初 │
  1893. │_ │
  1894. │の │
  1895. │行 │
  1896. │二 │
  1897. │行 │
  1898. │目 │
  1899. │ │
  1900. │ │
  1901. │ │
  1902. │ │
  1903. │ │
  1904. │ │
  1905. │ │
  1906. │ │
  1907. │ │
  1908. └────────────────────┘
  1909. ";
  1910. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1911. Application.End (rs);
  1912. }
  1913. // Moved from DimTests.cs - This is really a bogus test
  1914. [Theory]
  1915. [AutoInitShutdown]
  1916. [InlineData (0, true)]
  1917. [InlineData (0, false)]
  1918. [InlineData (50, true)]
  1919. [InlineData (50, false)]
  1920. public void DimPercentPlusOne (int startingDistance, bool testHorizontal)
  1921. {
  1922. var container = new View { Width = 100, Height = 100 };
  1923. var label = new Label
  1924. {
  1925. X = testHorizontal ? startingDistance : 0, Y = testHorizontal ? 0 : startingDistance
  1926. //Width = testHorizontal ? Dim.Percent (50) + 1 : 1,
  1927. //Height = testHorizontal ? 1 : Dim.Percent (50) + 1
  1928. };
  1929. container.Add (label);
  1930. Application.Top.Add (container);
  1931. Application.Top.BeginInit ();
  1932. Application.Top.EndInit ();
  1933. Application.Top.LayoutSubviews ();
  1934. Assert.Equal (100, container.Frame.Width);
  1935. Assert.Equal (100, container.Frame.Height);
  1936. if (testHorizontal)
  1937. {
  1938. Assert.Equal (0, label.Frame.Width); // BUGBUG: Shoudl be 0 not since there's no text
  1939. Assert.Equal (0, label.Frame.Height);
  1940. }
  1941. else
  1942. {
  1943. Assert.Equal (0, label.Frame.Width); // BUGBUG: Shoudl be 0 not since there's no text
  1944. Assert.Equal (0, label.Frame.Height);
  1945. }
  1946. }
  1947. [Fact]
  1948. [AutoInitShutdown]
  1949. public void Excess_Text_Is_Erased_When_The_Width_Is_Reduced ()
  1950. {
  1951. var lbl = new Label { Text = "123" };
  1952. Application.Top.Add (lbl);
  1953. RunState rs = Application.Begin (Application.Top);
  1954. Assert.True (lbl.AutoSize);
  1955. Assert.Equal ("123 ", GetContents ());
  1956. lbl.Text = "12";
  1957. // Here the AutoSize ensuring the right size with width 3 (Dim.Absolute)
  1958. // that was set on the OnAdded method with the text length of 3
  1959. // and height 1 because wasn't set and the text has 1 line
  1960. Assert.Equal (new Rectangle (0, 0, 3, 1), lbl.Frame);
  1961. Assert.Equal (new Rectangle (0, 0, 3, 1), lbl._needsDisplayRect);
  1962. Assert.Equal (new Rectangle (0, 0, 0, 0), lbl.SuperView._needsDisplayRect);
  1963. Assert.True (lbl.SuperView.LayoutNeeded);
  1964. lbl.SuperView.Draw ();
  1965. Assert.Equal ("12 ", GetContents ());
  1966. string GetContents ()
  1967. {
  1968. var text = "";
  1969. for (var i = 0; i < 4; i++)
  1970. {
  1971. text += Application.Driver.Contents [0, i].Rune;
  1972. }
  1973. return text;
  1974. }
  1975. Application.End (rs);
  1976. }
  1977. [Fact]
  1978. public void GetCurrentHeight_TrySetHeight ()
  1979. {
  1980. var top = new View { X = 0, Y = 0, Height = 20 };
  1981. var v = new View { Height = Dim.Fill (), ValidatePosDim = true };
  1982. top.Add (v);
  1983. top.BeginInit ();
  1984. top.EndInit ();
  1985. top.LayoutSubviews ();
  1986. Assert.False (v.AutoSize);
  1987. Assert.False (v.TrySetHeight (0, out _));
  1988. Assert.Equal (20, v.Frame.Height);
  1989. v.Height = Dim.Fill (1);
  1990. top.LayoutSubviews ();
  1991. Assert.False (v.TrySetHeight (0, out _));
  1992. Assert.True (v.Height is Dim.DimFill);
  1993. Assert.Equal (19, v.Frame.Height);
  1994. v.AutoSize = true;
  1995. top.LayoutSubviews ();
  1996. Assert.True (v.TrySetHeight (0, out _));
  1997. Assert.True (v.Height is Dim.DimAbsolute);
  1998. Assert.Equal (0, v.Frame.Height); // No text, so height is 0
  1999. top.Dispose ();
  2000. }
  2001. [Fact]
  2002. [TestRespondersDisposed]
  2003. public void GetCurrentWidth_TrySetWidth ()
  2004. {
  2005. var top = new View { X = 0, Y = 0, Width = 80 };
  2006. var v = new View { Width = Dim.Fill (), ValidatePosDim = true };
  2007. top.Add (v);
  2008. top.BeginInit ();
  2009. top.EndInit ();
  2010. top.LayoutSubviews ();
  2011. Assert.False (v.AutoSize);
  2012. Assert.False (v.TrySetWidth (0, out _));
  2013. Assert.True (v.Width is Dim.DimFill);
  2014. Assert.Equal (80, v.Frame.Width);
  2015. v.Width = Dim.Fill (1);
  2016. top.LayoutSubviews ();
  2017. Assert.False (v.TrySetWidth (0, out _));
  2018. Assert.True (v.Width is Dim.DimFill);
  2019. Assert.Equal (79, v.Frame.Width);
  2020. v.AutoSize = true;
  2021. top.LayoutSubviews ();
  2022. Assert.True (v.TrySetWidth (0, out _));
  2023. Assert.True (v.Width is Dim.DimAbsolute);
  2024. Assert.Equal (0, v.Frame.Width); // No text, so width is 0
  2025. top.Dispose ();
  2026. }
  2027. // [Fact]
  2028. // [AutoInitShutdown]
  2029. // public void AutoSize_False_TextDirection_Toggle ()
  2030. // {
  2031. // var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  2032. // // View is AutoSize == true
  2033. // var view = new View ();
  2034. // win.Add (view);
  2035. // Application.Top.Add (win);
  2036. // var rs = Application.Begin (Application.Top);
  2037. // ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  2038. // Assert.Equal (new (0, 0, 22, 22), win.Frame);
  2039. // Assert.Equal (new (0, 0, 22, 22), win.Margin.Frame);
  2040. // Assert.Equal (new (0, 0, 22, 22), win.Border.Frame);
  2041. // Assert.Equal (new (1, 1, 20, 20), win.Padding.Frame);
  2042. // Assert.False (view.AutoSize);
  2043. // Assert.Equal (TextDirection.LeftRight_TopBottom, view.TextDirection);
  2044. // Assert.Equal (Rect.Empty, view.Frame);
  2045. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2046. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2047. // Assert.Equal ("Absolute(0)", view.Width.ToString ());
  2048. // Assert.Equal ("Absolute(0)", view.Height.ToString ());
  2049. // var expected = @"
  2050. //┌────────────────────┐
  2051. //│ │
  2052. //│ │
  2053. //│ │
  2054. //│ │
  2055. //│ │
  2056. //│ │
  2057. //│ │
  2058. //│ │
  2059. //│ │
  2060. //│ │
  2061. //│ │
  2062. //│ │
  2063. //│ │
  2064. //│ │
  2065. //│ │
  2066. //│ │
  2067. //│ │
  2068. //│ │
  2069. //│ │
  2070. //│ │
  2071. //└────────────────────┘
  2072. //";
  2073. // var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2074. // Assert.Equal (new (0, 0, 22, 22), pos);
  2075. // view.Text = "Hello World";
  2076. // view.Width = 11;
  2077. // view.Height = 1;
  2078. // win.LayoutSubviews ();
  2079. // Application.Refresh ();
  2080. // Assert.Equal (new (0, 0, 11, 1), view.Frame);
  2081. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2082. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2083. // Assert.Equal ("Absolute(11)", view.Width.ToString ());
  2084. // Assert.Equal ("Absolute(1)", view.Height.ToString ());
  2085. // expected = @"
  2086. //┌────────────────────┐
  2087. //│Hello World │
  2088. //│ │
  2089. //│ │
  2090. //│ │
  2091. //│ │
  2092. //│ │
  2093. //│ │
  2094. //│ │
  2095. //│ │
  2096. //│ │
  2097. //│ │
  2098. //│ │
  2099. //│ │
  2100. //│ │
  2101. //│ │
  2102. //│ │
  2103. //│ │
  2104. //│ │
  2105. //│ │
  2106. //│ │
  2107. //└────────────────────┘
  2108. //";
  2109. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2110. // Assert.Equal (new (0, 0, 22, 22), pos);
  2111. // view.AutoSize = true;
  2112. // view.Text = "Hello Worlds";
  2113. // Application.Refresh ();
  2114. // Assert.Equal (new (0, 0, 12, 1), view.Frame);
  2115. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2116. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2117. // Assert.Equal ("Absolute(11)", view.Width.ToString ());
  2118. // Assert.Equal ("Absolute(1)", view.Height.ToString ());
  2119. // expected = @"
  2120. //┌────────────────────┐
  2121. //│Hello Worlds │
  2122. //│ │
  2123. //│ │
  2124. //│ │
  2125. //│ │
  2126. //│ │
  2127. //│ │
  2128. //│ │
  2129. //│ │
  2130. //│ │
  2131. //│ │
  2132. //│ │
  2133. //│ │
  2134. //│ │
  2135. //│ │
  2136. //│ │
  2137. //│ │
  2138. //│ │
  2139. //│ │
  2140. //│ │
  2141. //└────────────────────┘
  2142. //";
  2143. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2144. // Assert.Equal (new (0, 0, 22, 22), pos);
  2145. // view.TextDirection = TextDirection.TopBottom_LeftRight;
  2146. // Application.Refresh ();
  2147. // Assert.Equal (new (0, 0, 11, 12), view.Frame);
  2148. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2149. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2150. // Assert.Equal ("Absolute(11)", view.Width.ToString ());
  2151. // Assert.Equal ("Absolute(1)", view.Height.ToString ());
  2152. // expected = @"
  2153. //┌────────────────────┐
  2154. //│H │
  2155. //│e │
  2156. //│l │
  2157. //│l │
  2158. //│o │
  2159. //│ │
  2160. //│W │
  2161. //│o │
  2162. //│r │
  2163. //│l │
  2164. //│d │
  2165. //│s │
  2166. //│ │
  2167. //│ │
  2168. //│ │
  2169. //│ │
  2170. //│ │
  2171. //│ │
  2172. //│ │
  2173. //│ │
  2174. //└────────────────────┘
  2175. //";
  2176. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2177. // Assert.Equal (new (0, 0, 22, 22), pos);
  2178. // view.AutoSize = false;
  2179. // view.Height = 1;
  2180. // Application.Refresh ();
  2181. // Assert.Equal (new (0, 0, 11, 1), view.Frame);
  2182. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2183. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2184. // Assert.Equal ("Absolute(11)", view.Width.ToString ());
  2185. // Assert.Equal ("Absolute(1)", view.Height.ToString ());
  2186. // expected = @"
  2187. //┌────────────────────┐
  2188. //│HelloWorlds │
  2189. //│ │
  2190. //│ │
  2191. //│ │
  2192. //│ │
  2193. //│ │
  2194. //│ │
  2195. //│ │
  2196. //│ │
  2197. //│ │
  2198. //│ │
  2199. //│ │
  2200. //│ │
  2201. //│ │
  2202. //│ │
  2203. //│ │
  2204. //│ │
  2205. //│ │
  2206. //│ │
  2207. //│ │
  2208. //└────────────────────┘
  2209. //";
  2210. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2211. // Assert.Equal (new (0, 0, 22, 22), pos);
  2212. // view.PreserveTrailingSpaces = true;
  2213. // Application.Refresh ();
  2214. // Assert.Equal (new (0, 0, 11, 1), view.Frame);
  2215. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2216. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2217. // Assert.Equal ("Absolute(11)", view.Width.ToString ());
  2218. // Assert.Equal ("Absolute(1)", view.Height.ToString ());
  2219. // expected = @"
  2220. //┌────────────────────┐
  2221. //│Hello World │
  2222. //│ │
  2223. //│ │
  2224. //│ │
  2225. //│ │
  2226. //│ │
  2227. //│ │
  2228. //│ │
  2229. //│ │
  2230. //│ │
  2231. //│ │
  2232. //│ │
  2233. //│ │
  2234. //│ │
  2235. //│ │
  2236. //│ │
  2237. //│ │
  2238. //│ │
  2239. //│ │
  2240. //│ │
  2241. //└────────────────────┘
  2242. //";
  2243. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2244. // Assert.Equal (new (0, 0, 22, 22), pos);
  2245. // view.PreserveTrailingSpaces = false;
  2246. // var f = view.Frame;
  2247. // view.Width = f.Height;
  2248. // view.Height = f.Width;
  2249. // view.TextDirection = TextDirection.TopBottom_LeftRight;
  2250. // Application.Refresh ();
  2251. // Assert.Equal (new (0, 0, 1, 11), view.Frame);
  2252. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2253. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2254. // Assert.Equal ("Absolute(1)", view.Width.ToString ());
  2255. // Assert.Equal ("Absolute(11)", view.Height.ToString ());
  2256. // expected = @"
  2257. //┌────────────────────┐
  2258. //│H │
  2259. //│e │
  2260. //│l │
  2261. //│l │
  2262. //│o │
  2263. //│ │
  2264. //│W │
  2265. //│o │
  2266. //│r │
  2267. //│l │
  2268. //│d │
  2269. //│ │
  2270. //│ │
  2271. //│ │
  2272. //│ │
  2273. //│ │
  2274. //│ │
  2275. //│ │
  2276. //│ │
  2277. //│ │
  2278. //└────────────────────┘
  2279. //";
  2280. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2281. // Assert.Equal (new (0, 0, 22, 22), pos);
  2282. // view.AutoSize = true;
  2283. // Application.Refresh ();
  2284. // Assert.Equal (new (0, 0, 1, 12), view.Frame);
  2285. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2286. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2287. // Assert.Equal ("Absolute(1)", view.Width.ToString ());
  2288. // Assert.Equal ("Absolute(12)", view.Height.ToString ());
  2289. // expected = @"
  2290. //┌────────────────────┐
  2291. //│H │
  2292. //│e │
  2293. //│l │
  2294. //│l │
  2295. //│o │
  2296. //│ │
  2297. //│W │
  2298. //│o │
  2299. //│r │
  2300. //│l │
  2301. //│d │
  2302. //│s │
  2303. //│ │
  2304. //│ │
  2305. //│ │
  2306. //│ │
  2307. //│ │
  2308. //│ │
  2309. //│ │
  2310. //│ │
  2311. //└────────────────────┘
  2312. //";
  2313. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2314. // Assert.Equal (new (0, 0, 22, 22), pos);
  2315. // Application.End (rs);
  2316. // }
  2317. [Fact]
  2318. [AutoInitShutdown]
  2319. public void GetTextFormatterBoundsSize_GetSizeNeededForText_HotKeySpecifier ()
  2320. {
  2321. var text = "Say Hello 你";
  2322. // Frame: 0, 0, 12, 1
  2323. var horizontalView = new View { AutoSize = true };
  2324. horizontalView.TextFormatter.HotKeySpecifier = (Rune)'_';
  2325. horizontalView.Text = text;
  2326. // Frame: 0, 0, 1, 12
  2327. var verticalView = new View
  2328. {
  2329. AutoSize = true, TextDirection = TextDirection.TopBottom_LeftRight
  2330. };
  2331. verticalView.Text = text;
  2332. verticalView.TextFormatter.HotKeySpecifier = (Rune)'_';
  2333. Application.Top.Add (horizontalView, verticalView);
  2334. Application.Begin (Application.Top);
  2335. ((FakeDriver)Application.Driver).SetBufferSize (50, 50);
  2336. Assert.True (horizontalView.AutoSize);
  2337. Assert.Equal (new (0, 0, 12, 1), horizontalView.Frame);
  2338. Assert.Equal (new (12, 1), horizontalView.GetSizeNeededForTextWithoutHotKey ());
  2339. Assert.Equal (horizontalView.Frame.Size, horizontalView.GetSizeNeededForTextWithoutHotKey ());
  2340. Assert.True (verticalView.AutoSize);
  2341. Assert.Equal (new (0, 0, 2, 11), verticalView.Frame);
  2342. Assert.Equal (new (2, 11), verticalView.GetSizeNeededForTextWithoutHotKey ());
  2343. Assert.Equal (verticalView.Frame.Size, verticalView.GetSizeNeededForTextWithoutHotKey ());
  2344. text = "Say He_llo 你";
  2345. horizontalView.Text = text;
  2346. verticalView.Text = text;
  2347. Assert.True (horizontalView.AutoSize);
  2348. Assert.Equal (new (0, 0, 12, 1), horizontalView.Frame);
  2349. Assert.Equal (new (12, 1), horizontalView.GetSizeNeededForTextWithoutHotKey ());
  2350. Assert.Equal (horizontalView.Frame.Size, horizontalView.GetSizeNeededForTextWithoutHotKey ());
  2351. Assert.True (verticalView.AutoSize);
  2352. Assert.Equal (new (0, 0, 2, 11), verticalView.Frame);
  2353. Assert.Equal (new (2, 11), verticalView.GetSizeNeededForTextWithoutHotKey ());
  2354. Assert.Equal (verticalView.Frame.Size, verticalView.GetSizeNeededForTextWithoutHotKey ());
  2355. }
  2356. [Fact]
  2357. public void SetRelativeLayout_Respects_AutoSize ()
  2358. {
  2359. var view = new View { Frame = new (0, 0, 10, 0), AutoSize = true };
  2360. view.Text = "01234567890123456789";
  2361. Assert.True (view.AutoSize);
  2362. Assert.Equal (LayoutStyle.Absolute, view.LayoutStyle);
  2363. Assert.Equal (new (0, 0, 20, 1), view.Frame);
  2364. Assert.Equal ("Absolute(0)", view.X.ToString ());
  2365. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2366. Assert.Equal ("Absolute(20)", view.Width.ToString ());
  2367. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  2368. view.SetRelativeLayout (new (0, 0, 25, 5));
  2369. Assert.True (view.AutoSize);
  2370. Assert.Equal (LayoutStyle.Absolute, view.LayoutStyle);
  2371. Assert.Equal (new (0, 0, 20, 1), view.Frame);
  2372. Assert.Equal ("Absolute(0)", view.X.ToString ());
  2373. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2374. Assert.Equal ("Absolute(20)", view.Width.ToString ());
  2375. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  2376. }
  2377. [Fact]
  2378. [AutoInitShutdown]
  2379. public void Setting_Frame_Dont_Respect_AutoSize_True_On_Layout_Absolute ()
  2380. {
  2381. var view1 = new View { Frame = new (0, 0, 10, 0), Text = "Say Hello view1 你", AutoSize = true };
  2382. var viewTopBottom_LeftRight = new View
  2383. {
  2384. Frame = new (0, 0, 0, 10),
  2385. Text = "Say Hello view2 你",
  2386. AutoSize = true,
  2387. TextDirection =
  2388. TextDirection.TopBottom_LeftRight
  2389. };
  2390. Application.Top.Add (view1, viewTopBottom_LeftRight);
  2391. RunState rs = Application.Begin (Application.Top);
  2392. Assert.True (view1.AutoSize);
  2393. Assert.Equal (LayoutStyle.Absolute, view1.LayoutStyle);
  2394. Assert.Equal (new (0, 0, 18, 1), view1.Frame);
  2395. Assert.Equal ("Absolute(0)", view1.X.ToString ());
  2396. Assert.Equal ("Absolute(0)", view1.Y.ToString ());
  2397. Assert.Equal ("Absolute(18)", view1.Width.ToString ());
  2398. Assert.Equal ("Absolute(1)", view1.Height.ToString ());
  2399. Assert.True (viewTopBottom_LeftRight.AutoSize);
  2400. Assert.Equal (LayoutStyle.Absolute, viewTopBottom_LeftRight.LayoutStyle);
  2401. Assert.Equal (new (0, 0, 18, 17), viewTopBottom_LeftRight.Frame);
  2402. Assert.Equal ("Absolute(0)", viewTopBottom_LeftRight.X.ToString ());
  2403. Assert.Equal ("Absolute(0)", viewTopBottom_LeftRight.Y.ToString ());
  2404. Assert.Equal ("Absolute(18)", viewTopBottom_LeftRight.Width.ToString ());
  2405. Assert.Equal ("Absolute(17)", viewTopBottom_LeftRight.Height.ToString ());
  2406. view1.Frame = new (0, 0, 25, 4);
  2407. var firstIteration = false;
  2408. Application.RunIteration (ref rs, ref firstIteration);
  2409. Assert.True (view1.AutoSize);
  2410. Assert.Equal (LayoutStyle.Absolute, view1.LayoutStyle);
  2411. Assert.Equal (new (0, 0, 25, 4), view1.Frame);
  2412. Assert.Equal ("Absolute(0)", view1.X.ToString ());
  2413. Assert.Equal ("Absolute(0)", view1.Y.ToString ());
  2414. Assert.Equal ("Absolute(25)", view1.Width.ToString ());
  2415. Assert.Equal ("Absolute(4)", view1.Height.ToString ());
  2416. viewTopBottom_LeftRight.Frame = new (0, 0, 1, 25);
  2417. Application.RunIteration (ref rs, ref firstIteration);
  2418. Assert.True (viewTopBottom_LeftRight.AutoSize);
  2419. Assert.Equal (LayoutStyle.Absolute, viewTopBottom_LeftRight.LayoutStyle);
  2420. Assert.Equal (new (0, 0, 2, 25), viewTopBottom_LeftRight.Frame);
  2421. Assert.Equal ("Absolute(0)", viewTopBottom_LeftRight.X.ToString ());
  2422. Assert.Equal ("Absolute(0)", viewTopBottom_LeftRight.Y.ToString ());
  2423. Assert.Equal ("Absolute(2)", viewTopBottom_LeftRight.Width.ToString ());
  2424. Assert.Equal ("Absolute(25)", viewTopBottom_LeftRight.Height.ToString ());
  2425. Application.End (rs);
  2426. }
  2427. [Fact]
  2428. [AutoInitShutdown]
  2429. public void TrySetHeight_ForceValidatePosDim ()
  2430. {
  2431. var top = new View { X = 0, Y = 0, Height = 20 };
  2432. var v = new View { Height = Dim.Fill (), ValidatePosDim = true };
  2433. top.Add (v);
  2434. Assert.False (v.TrySetHeight (10, out int rHeight));
  2435. Assert.Equal (10, rHeight);
  2436. v.Height = Dim.Fill (1);
  2437. Assert.False (v.TrySetHeight (10, out rHeight));
  2438. Assert.Equal (9, rHeight);
  2439. v.Height = 0;
  2440. Assert.True (v.TrySetHeight (10, out rHeight));
  2441. Assert.Equal (10, rHeight);
  2442. Assert.False (v.IsInitialized);
  2443. Application.Top.Add (top);
  2444. Application.Begin (Application.Top);
  2445. Assert.True (v.IsInitialized);
  2446. v.Height = 15;
  2447. Assert.True (v.TrySetHeight (5, out rHeight));
  2448. Assert.Equal (5, rHeight);
  2449. }
  2450. [Fact]
  2451. [AutoInitShutdown]
  2452. public void TrySetWidth_ForceValidatePosDim ()
  2453. {
  2454. var top = new View { X = 0, Y = 0, Width = 80 };
  2455. var v = new View { Width = Dim.Fill (), ValidatePosDim = true };
  2456. top.Add (v);
  2457. Assert.False (v.TrySetWidth (70, out int rWidth));
  2458. Assert.Equal (70, rWidth);
  2459. v.Width = Dim.Fill (1);
  2460. Assert.False (v.TrySetWidth (70, out rWidth));
  2461. Assert.Equal (69, rWidth);
  2462. v.Width = 0;
  2463. Assert.True (v.TrySetWidth (70, out rWidth));
  2464. Assert.Equal (70, rWidth);
  2465. Assert.False (v.IsInitialized);
  2466. Application.Top.Add (top);
  2467. Application.Begin (Application.Top);
  2468. Assert.True (v.IsInitialized);
  2469. v.Width = 75;
  2470. Assert.True (v.TrySetWidth (60, out rWidth));
  2471. Assert.Equal (60, rWidth);
  2472. }
  2473. [Theory]
  2474. [AutoInitShutdown]
  2475. [InlineData (true)]
  2476. [InlineData (false)]
  2477. public void View_Draw_Horizontal_Simple_TextAlignments (bool autoSize)
  2478. {
  2479. var text = "Hello World";
  2480. var width = 20;
  2481. var lblLeft = new View { Text = text, Width = width, Height = 1, AutoSize = autoSize };
  2482. var lblCenter = new View
  2483. {
  2484. Text = text,
  2485. Y = 1,
  2486. Width = width,
  2487. Height = 1,
  2488. TextAlignment = TextAlignment.Centered,
  2489. AutoSize = autoSize
  2490. };
  2491. var lblRight = new View
  2492. {
  2493. Text = text,
  2494. Y = 2,
  2495. Width = width,
  2496. Height = 1,
  2497. TextAlignment = TextAlignment.Right,
  2498. AutoSize = autoSize
  2499. };
  2500. var lblJust = new View
  2501. {
  2502. Text = text,
  2503. Y = 3,
  2504. Width = width,
  2505. Height = 1,
  2506. TextAlignment = TextAlignment.Justified,
  2507. AutoSize = autoSize
  2508. };
  2509. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  2510. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2511. Application.Top.Add (frame);
  2512. Application.Begin (Application.Top);
  2513. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 6);
  2514. Assert.True (lblLeft.AutoSize == autoSize);
  2515. Assert.True (lblCenter.AutoSize == autoSize);
  2516. Assert.True (lblRight.AutoSize == autoSize);
  2517. Assert.True (lblJust.AutoSize == autoSize);
  2518. Assert.True (lblLeft.TextFormatter.AutoSize == autoSize);
  2519. Assert.True (lblCenter.TextFormatter.AutoSize == autoSize);
  2520. Assert.True (lblRight.TextFormatter.AutoSize == autoSize);
  2521. Assert.True (lblJust.TextFormatter.AutoSize == autoSize);
  2522. if (autoSize)
  2523. {
  2524. Size expectedSize = new (11, 1);
  2525. Assert.Equal (expectedSize, lblLeft.TextFormatter.Size);
  2526. Assert.Equal (expectedSize, lblCenter.TextFormatter.Size);
  2527. Assert.Equal (expectedSize, lblRight.TextFormatter.Size);
  2528. expectedSize = new (width, 1);
  2529. Assert.Equal (expectedSize, lblJust.TextFormatter.Size);
  2530. }
  2531. else
  2532. {
  2533. Size expectedSize = new (width, 1);
  2534. Assert.Equal (expectedSize, lblLeft.TextFormatter.Size);
  2535. Assert.Equal (expectedSize, lblCenter.TextFormatter.Size);
  2536. Assert.Equal (expectedSize, lblRight.TextFormatter.Size);
  2537. Assert.Equal (expectedSize, lblJust.TextFormatter.Size);
  2538. }
  2539. Assert.Equal (new (0, 0, width + 2, 6), frame.Frame);
  2540. var expected = @"
  2541. ┌────────────────────┐
  2542. │Hello World │
  2543. │ Hello World │
  2544. │ Hello World│
  2545. │Hello World│
  2546. └────────────────────┘
  2547. "
  2548. ;
  2549. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2550. Assert.Equal (new (0, 0, width + 2, 6), pos);
  2551. }
  2552. [Theory]
  2553. [AutoInitShutdown]
  2554. [InlineData (true)]
  2555. [InlineData (false)]
  2556. public void View_Draw_Vertical_Simple_TextAlignments (bool autoSize)
  2557. {
  2558. var text = "Hello World";
  2559. var height = 20;
  2560. var lblLeft = new View
  2561. {
  2562. Text = text,
  2563. Width = 1,
  2564. Height = height,
  2565. TextDirection = TextDirection.TopBottom_LeftRight,
  2566. AutoSize = autoSize
  2567. };
  2568. var lblCenter = new View
  2569. {
  2570. Text = text,
  2571. X = 2,
  2572. Width = 1,
  2573. Height = height,
  2574. TextDirection = TextDirection.TopBottom_LeftRight,
  2575. AutoSize = autoSize,
  2576. VerticalTextAlignment = VerticalTextAlignment.Middle
  2577. };
  2578. var lblRight = new View
  2579. {
  2580. Text = text,
  2581. X = 4,
  2582. Width = 1,
  2583. Height = height,
  2584. TextDirection = TextDirection.TopBottom_LeftRight,
  2585. AutoSize = autoSize,
  2586. VerticalTextAlignment = VerticalTextAlignment.Bottom
  2587. };
  2588. var lblJust = new View
  2589. {
  2590. Text = text,
  2591. X = 6,
  2592. Width = 1,
  2593. Height = height,
  2594. TextDirection = TextDirection.TopBottom_LeftRight,
  2595. AutoSize = autoSize,
  2596. VerticalTextAlignment = VerticalTextAlignment.Justified
  2597. };
  2598. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  2599. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2600. Application.Top.Add (frame);
  2601. Application.Begin (Application.Top);
  2602. ((FakeDriver)Application.Driver).SetBufferSize (9, height + 2);
  2603. Assert.True (lblLeft.AutoSize == autoSize);
  2604. Assert.True (lblCenter.AutoSize == autoSize);
  2605. Assert.True (lblRight.AutoSize == autoSize);
  2606. Assert.True (lblJust.AutoSize == autoSize);
  2607. Assert.True (lblLeft.TextFormatter.AutoSize == autoSize);
  2608. Assert.True (lblCenter.TextFormatter.AutoSize == autoSize);
  2609. Assert.True (lblRight.TextFormatter.AutoSize == autoSize);
  2610. Assert.True (lblJust.TextFormatter.AutoSize == autoSize);
  2611. if (autoSize)
  2612. {
  2613. Assert.Equal (new (1, 11), lblLeft.TextFormatter.Size);
  2614. Assert.Equal (new (1, 11), lblCenter.TextFormatter.Size);
  2615. Assert.Equal (new (1, 11), lblRight.TextFormatter.Size);
  2616. Assert.Equal (new (1, height), lblJust.TextFormatter.Size);
  2617. Assert.Equal (new (0, 0, 9, height + 2), frame.Frame);
  2618. }
  2619. else
  2620. {
  2621. Assert.Equal (new (1, height), lblLeft.TextFormatter.Size);
  2622. Assert.Equal (new (1, height), lblCenter.TextFormatter.Size);
  2623. Assert.Equal (new (1, height), lblRight.TextFormatter.Size);
  2624. Assert.Equal (new (1, height), lblJust.TextFormatter.Size);
  2625. Assert.Equal (new (0, 0, 9, height + 2), frame.Frame);
  2626. }
  2627. var expected = @"
  2628. ┌───────┐
  2629. │H H│
  2630. │e e│
  2631. │l l│
  2632. │l l│
  2633. │o H o│
  2634. │ e │
  2635. │W l │
  2636. │o l │
  2637. │r o │
  2638. │l H │
  2639. │d W e │
  2640. │ o l │
  2641. │ r l │
  2642. │ l o │
  2643. │ d │
  2644. │ W W│
  2645. │ o o│
  2646. │ r r│
  2647. │ l l│
  2648. │ d d│
  2649. └───────┘
  2650. "
  2651. ;
  2652. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2653. Assert.Equal (new (0, 0, 9, height + 2), pos);
  2654. }
  2655. }