AutoSizeTrueTests.cs 102 KB

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