AutoSizeTrueTests.cs 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031
  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 = 2,
  1102. //Height = 17,
  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 = 2,
  1111. //Height = 17,
  1112. TextDirection = TextDirection.TopBottom_LeftRight,
  1113. ValidatePosDim = true
  1114. };
  1115. var view6 = new View
  1116. {
  1117. AutoSize = true,
  1118. //Width = 2,
  1119. //Height = 17,
  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 (view2.Text.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);
  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. // Use Lenght for vertical text, not GetColumns
  1149. Assert.Equal (view4.Text.Length, view4.Frame.Height);
  1150. Assert.Equal (new (0, 0, 2, 17), view4.Frame);
  1151. Assert.True (view5.AutoSize);
  1152. Assert.Equal (new (0, 0, 2, 17), view5.Frame);
  1153. Assert.True (view6.AutoSize);
  1154. Assert.Equal (new (0, 0, 2, 17), view6.Frame);
  1155. top.BeginInit ();
  1156. top.EndInit ();
  1157. Assert.True (view1.IsInitialized);
  1158. Assert.True (view2.IsInitialized);
  1159. Assert.True (view3.IsInitialized);
  1160. Assert.True (view4.IsInitialized);
  1161. Assert.True (view5.IsInitialized);
  1162. Assert.True (view1.AutoSize);
  1163. Assert.Equal (new (0, 0, 18, 1), view1.Frame);
  1164. Assert.True (view2.AutoSize);
  1165. Assert.Equal (new (0, 0, 18, 1), view2.Frame);
  1166. Assert.True (view3.AutoSize);
  1167. Assert.Equal (new (0, 0, 18, 1), view3.Frame);
  1168. Assert.True (view4.AutoSize);
  1169. Assert.Equal (new (0, 0, 2, 17), view4.Frame);
  1170. Assert.True (view5.AutoSize);
  1171. Assert.Equal (new (0, 0, 2, 17), view5.Frame);
  1172. Assert.True (view6.AutoSize);
  1173. Assert.Equal (new (0, 0, 2, 17), view6.Frame);
  1174. }
  1175. [Fact]
  1176. public void AutoSize_True_Label_If_Text_Empty ()
  1177. {
  1178. var label1 = new Label ();
  1179. var label3 = new Label { Text = "" };
  1180. Assert.True (label1.AutoSize);
  1181. Assert.True (label3.AutoSize);
  1182. label1.Dispose ();
  1183. label3.Dispose ();
  1184. }
  1185. [Fact]
  1186. public void AutoSize_True_Label_If_Text_Is_Not_Empty ()
  1187. {
  1188. var label1 = new Label ();
  1189. label1.Text = "Hello World";
  1190. var label3 = new Label { Text = "Hello World" };
  1191. Assert.True (label1.AutoSize);
  1192. Assert.True (label3.AutoSize);
  1193. label1.Dispose ();
  1194. label3.Dispose ();
  1195. }
  1196. [Fact]
  1197. [AutoInitShutdown]
  1198. public void AutoSize_True_Label_IsEmpty_False_Minimum_Height ()
  1199. {
  1200. var text = "Label";
  1201. var label = new Label
  1202. {
  1203. //Width = Dim.Fill () - text.Length,
  1204. Text = text
  1205. };
  1206. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  1207. win.Add (label);
  1208. var top = new Toplevel ();
  1209. top.Add (win);
  1210. Application.Begin (top);
  1211. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  1212. Assert.Equal (5, text.Length);
  1213. Assert.True (label.AutoSize);
  1214. Assert.Equal (new (0, 0, 5, 1), label.Frame);
  1215. Assert.Equal (new (5, 1), label.TextFormatter.Size);
  1216. Assert.Equal (["Label"], label.TextFormatter.GetLines ());
  1217. Assert.Equal (new (0, 0, 10, 4), win.Frame);
  1218. Assert.Equal (new (0, 0, 10, 4), Application.Top.Frame);
  1219. var expected = @"
  1220. ┌────────┐
  1221. │Label │
  1222. │ │
  1223. └────────┘
  1224. ";
  1225. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1226. Assert.Equal (new (0, 0, 10, 4), pos);
  1227. text = "0123456789";
  1228. Assert.Equal (10, text.Length);
  1229. //label.Width = Dim.Fill () - text.Length;
  1230. Application.Refresh ();
  1231. Assert.Equal (new (0, 0, 5, 1), label.Frame);
  1232. Assert.Equal (new (5, 1), label.TextFormatter.Size);
  1233. Exception exception = Record.Exception (() => Assert.Single (label.TextFormatter.GetLines ()));
  1234. Assert.Null (exception);
  1235. expected = @"
  1236. ┌────────┐
  1237. │Label │
  1238. │ │
  1239. └────────┘
  1240. ";
  1241. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1242. Assert.Equal (new (0, 0, 10, 4), pos);
  1243. }
  1244. [Fact]
  1245. [AutoInitShutdown]
  1246. public void AutoSize_True_Label_IsEmpty_False_Never_Return_Null_Lines ()
  1247. {
  1248. var text = "Label";
  1249. var label = new Label
  1250. {
  1251. //Width = Dim.Fill () - text.Length,
  1252. //Height = 1,
  1253. Text = text
  1254. };
  1255. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  1256. win.Add (label);
  1257. var top = new Toplevel ();
  1258. top.Add (win);
  1259. Application.Begin (top);
  1260. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  1261. Assert.Equal (5, text.Length);
  1262. Assert.True (label.AutoSize);
  1263. Assert.Equal (new (0, 0, 5, 1), label.Frame);
  1264. Assert.Equal (new (5, 1), label.TextFormatter.Size);
  1265. Assert.Equal (["Label"], label.TextFormatter.GetLines ());
  1266. Assert.Equal (new (0, 0, 10, 4), win.Frame);
  1267. Assert.Equal (new (0, 0, 10, 4), Application.Top.Frame);
  1268. var expected = @"
  1269. ┌────────┐
  1270. │Label │
  1271. │ │
  1272. └────────┘
  1273. ";
  1274. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1275. Assert.Equal (new (0, 0, 10, 4), pos);
  1276. text = "0123456789";
  1277. Assert.Equal (10, text.Length);
  1278. //label.Width = Dim.Fill () - text.Length;
  1279. Application.Refresh ();
  1280. Assert.True (label.AutoSize);
  1281. Assert.Equal (new (0, 0, 5, 1), label.Frame);
  1282. Assert.Equal (new (5, 1), label.TextFormatter.Size);
  1283. Assert.Single (label.TextFormatter.GetLines ());
  1284. expected = @"
  1285. ┌────────┐
  1286. │Label │
  1287. │ │
  1288. └────────┘
  1289. ";
  1290. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1291. Assert.Equal (new (0, 0, 10, 4), pos);
  1292. }
  1293. [Fact]
  1294. public void AutoSize_True_ResizeView_With_Dim_Absolute ()
  1295. {
  1296. var super = new View ()
  1297. {
  1298. Width = Dim.Fill (),
  1299. Height = Dim.Fill ()
  1300. };
  1301. var label = new Label ();
  1302. label.Text = "New text";
  1303. super.Add (label);
  1304. super.LayoutSubviews ();
  1305. Assert.True (label.AutoSize);
  1306. Rectangle expectedLabelBounds = new (0, 0, 8, 1);
  1307. Assert.Equal (expectedLabelBounds, label.Viewport);
  1308. super.Dispose ();
  1309. }
  1310. [Fact]
  1311. [SetupFakeDriver]
  1312. public void AutoSize_True_Setting_With_Height_Horizontal ()
  1313. {
  1314. var top = new View { Width = 25, Height = 25 };
  1315. var label = new Label { Text = "Hello", /* Width = 10, Height = 2, */ ValidatePosDim = true };
  1316. var viewX = new View { Text = "X", X = Pos.Right (label), Width = 1, Height = 1 };
  1317. var viewY = new View { Text = "Y", Y = Pos.Bottom (label), Width = 1, Height = 1 };
  1318. top.Add (label, viewX, viewY);
  1319. top.BeginInit ();
  1320. top.EndInit ();
  1321. Assert.True (label.AutoSize);
  1322. Assert.Equal (new (0, 0, 5, 1), label.Frame);
  1323. top.LayoutSubviews ();
  1324. top.Draw ();
  1325. var expected = @"
  1326. HelloX
  1327. Y
  1328. ";
  1329. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1330. label.AutoSize = false;
  1331. label.Width = 10;
  1332. label.Height = 2;
  1333. Assert.False (label.AutoSize);
  1334. Assert.Equal (new (0, 0, 10, 2), label.Frame);
  1335. top.LayoutSubviews ();
  1336. top.Draw ();
  1337. expected = @"
  1338. Hello X
  1339. Y
  1340. ";
  1341. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1342. }
  1343. [Fact]
  1344. [AutoInitShutdown]
  1345. public void AutoSize_True_Setting_With_Height_Vertical ()
  1346. {
  1347. // BUGBUG: Label is AutoSize = true, so Width & Height are ignored
  1348. var label = new Label
  1349. { /*Width = 2, Height = 10, */
  1350. TextDirection = TextDirection.TopBottom_LeftRight, ValidatePosDim = true
  1351. };
  1352. var viewX = new View { Text = "X", X = Pos.Right (label), Width = 1, Height = 1 };
  1353. var viewY = new View { Text = "Y", Y = Pos.Bottom (label), Width = 1, Height = 1 };
  1354. var top = new Toplevel ();
  1355. top.Add (label, viewX, viewY);
  1356. RunState rs = Application.Begin (top);
  1357. Assert.True (label.AutoSize);
  1358. label.Text = "Hello";
  1359. Application.Refresh ();
  1360. Assert.Equal (new (0, 0, 1, 5), label.Frame);
  1361. var expected = @"
  1362. HX
  1363. e
  1364. l
  1365. l
  1366. o
  1367. Y
  1368. ";
  1369. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1370. label.AutoSize = false;
  1371. label.Width = 2;
  1372. label.Height = 10;
  1373. Application.Refresh ();
  1374. Assert.False (label.AutoSize);
  1375. Assert.Equal (new (0, 0, 2, 10), label.Frame);
  1376. expected = @"
  1377. H X
  1378. e
  1379. l
  1380. l
  1381. o
  1382. Y
  1383. "
  1384. ;
  1385. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1386. Application.End (rs);
  1387. }
  1388. [Fact]
  1389. [AutoInitShutdown]
  1390. public void AutoSize_True_TextDirection_Toggle ()
  1391. {
  1392. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  1393. // View is AutoSize == true
  1394. var view = new View ();
  1395. win.Add (view);
  1396. var top = new Toplevel ();
  1397. top.Add (win);
  1398. RunState rs = Application.Begin (top);
  1399. ((FakeDriver)Application.Driver).SetBufferSize (15, 15);
  1400. Assert.Equal (new Rectangle (0, 0, 15, 15), win.Frame);
  1401. Assert.Equal (new Rectangle (0, 0, 15, 15), win.Margin.Frame);
  1402. Assert.Equal (new Rectangle (0, 0, 15, 15), win.Border.Frame);
  1403. Assert.Equal (new Rectangle (1, 1, 13, 13), win.Padding.Frame);
  1404. Assert.False (view.AutoSize);
  1405. Assert.Equal (TextDirection.LeftRight_TopBottom, view.TextDirection);
  1406. Assert.Equal (Rectangle.Empty, view.Frame);
  1407. Assert.Equal ("Absolute(0)", view.X.ToString ());
  1408. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1409. Assert.Equal ("Absolute(0)", view.Width.ToString ());
  1410. Assert.Equal ("Absolute(0)", view.Height.ToString ());
  1411. var expected = @"
  1412. ┌─────────────┐
  1413. │ │
  1414. │ │
  1415. │ │
  1416. │ │
  1417. │ │
  1418. │ │
  1419. │ │
  1420. │ │
  1421. │ │
  1422. │ │
  1423. │ │
  1424. │ │
  1425. │ │
  1426. └─────────────┘
  1427. ";
  1428. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1429. view.Text = "Hello World";
  1430. view.Width = 11;
  1431. view.Height = 1;
  1432. win.LayoutSubviews ();
  1433. Application.Refresh ();
  1434. Assert.Equal (new Rectangle (0, 0, 11, 1), view.Frame);
  1435. Assert.Equal ("Absolute(0)", view.X.ToString ());
  1436. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1437. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  1438. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  1439. expected = @"
  1440. ┌─────────────┐
  1441. │Hello World │
  1442. │ │
  1443. │ │
  1444. │ │
  1445. │ │
  1446. │ │
  1447. │ │
  1448. │ │
  1449. │ │
  1450. │ │
  1451. │ │
  1452. │ │
  1453. │ │
  1454. └─────────────┘
  1455. ";
  1456. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1457. view.AutoSize = true;
  1458. view.Text = "Hello Worlds";
  1459. Application.Refresh ();
  1460. int len = "Hello Worlds".Length;
  1461. Assert.Equal (12, len);
  1462. Assert.Equal (new Rectangle (0, 0, len, 1), view.Frame);
  1463. expected = @"
  1464. ┌─────────────┐
  1465. │Hello Worlds │
  1466. │ │
  1467. │ │
  1468. │ │
  1469. │ │
  1470. │ │
  1471. │ │
  1472. │ │
  1473. │ │
  1474. │ │
  1475. │ │
  1476. │ │
  1477. │ │
  1478. └─────────────┘
  1479. ";
  1480. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1481. view.TextDirection = TextDirection.TopBottom_LeftRight;
  1482. Application.Refresh ();
  1483. Assert.Equal (new Rectangle (0, 0, 1, 12), view.Frame);
  1484. expected = @"
  1485. ┌─────────────┐
  1486. │H │
  1487. │e │
  1488. │l │
  1489. │l │
  1490. │o │
  1491. │ │
  1492. │W │
  1493. │o │
  1494. │r │
  1495. │l │
  1496. │d │
  1497. │s │
  1498. │ │
  1499. └─────────────┘
  1500. ";
  1501. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1502. // Setting to false causes Width and Height to be set to the current ContentSize
  1503. view.AutoSize = false;
  1504. Assert.Equal (new Rectangle (0, 0, 1, 12), view.Frame);
  1505. view.Height = 1;
  1506. view.Width = 12;
  1507. Application.Refresh ();
  1508. // TextDirection.TopBottom_LeftRight - Height of 1 and Width of 12 means
  1509. // that the text will be spread "vertically" across 1 line.
  1510. // Hence no space.
  1511. expected = @"
  1512. ┌─────────────┐
  1513. │HelloWorlds │
  1514. │ │
  1515. │ │
  1516. │ │
  1517. │ │
  1518. │ │
  1519. │ │
  1520. │ │
  1521. │ │
  1522. │ │
  1523. │ │
  1524. │ │
  1525. │ │
  1526. └─────────────┘
  1527. ";
  1528. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1529. view.PreserveTrailingSpaces = true;
  1530. Application.Refresh ();
  1531. Assert.Equal (new Rectangle (0, 0, 12, 1), view.Frame);
  1532. expected = @"
  1533. ┌─────────────┐
  1534. │Hello Worlds │
  1535. │ │
  1536. │ │
  1537. │ │
  1538. │ │
  1539. │ │
  1540. │ │
  1541. │ │
  1542. │ │
  1543. │ │
  1544. │ │
  1545. │ │
  1546. │ │
  1547. └─────────────┘
  1548. ";
  1549. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1550. view.PreserveTrailingSpaces = false;
  1551. Rectangle f = view.Frame;
  1552. view.Width = f.Height;
  1553. view.Height = f.Width;
  1554. view.TextDirection = TextDirection.TopBottom_LeftRight;
  1555. Application.Refresh ();
  1556. Assert.Equal (new Rectangle (0, 0, 1, 12), view.Frame);
  1557. expected = @"
  1558. ┌─────────────┐
  1559. │H │
  1560. │e │
  1561. │l │
  1562. │l │
  1563. │o │
  1564. │ │
  1565. │W │
  1566. │o │
  1567. │r │
  1568. │l │
  1569. │d │
  1570. │s │
  1571. │ │
  1572. └─────────────┘
  1573. ";
  1574. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1575. view.AutoSize = true;
  1576. Application.Refresh ();
  1577. Assert.Equal (new Rectangle (0, 0, 1, 12), view.Frame);
  1578. expected = @"
  1579. ┌─────────────┐
  1580. │H │
  1581. │e │
  1582. │l │
  1583. │l │
  1584. │o │
  1585. │ │
  1586. │W │
  1587. │o │
  1588. │r │
  1589. │l │
  1590. │d │
  1591. │s │
  1592. │ │
  1593. └─────────────┘
  1594. ";
  1595. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1596. Application.End (rs);
  1597. }
  1598. [Fact]
  1599. [AutoInitShutdown]
  1600. public void AutoSize_True_View_IsEmpty_False_Minimum_Width ()
  1601. {
  1602. var text = "Views";
  1603. var view = new View
  1604. {
  1605. TextDirection = TextDirection.TopBottom_LeftRight,
  1606. Height = Dim.Fill () - text.Length,
  1607. Text = text,
  1608. AutoSize = true
  1609. };
  1610. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  1611. win.Add (view);
  1612. var top = new Toplevel ();
  1613. top.Add (win);
  1614. Application.Begin (top);
  1615. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  1616. Assert.Equal (5, text.Length);
  1617. Assert.True (view.AutoSize);
  1618. Assert.Equal (new (0, 0, 1, 5), view.Frame);
  1619. Assert.Equal (new (1, 5), view.TextFormatter.Size);
  1620. Assert.Equal (new List<string> { "Views" }, view.TextFormatter.GetLines ());
  1621. Assert.Equal (new (0, 0, 4, 10), win.Frame);
  1622. Assert.Equal (new (0, 0, 4, 10), Application.Top.Frame);
  1623. var expected = @"
  1624. ┌──┐
  1625. │V │
  1626. │i │
  1627. │e │
  1628. │w │
  1629. │s │
  1630. │ │
  1631. │ │
  1632. │ │
  1633. └──┘
  1634. ";
  1635. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1636. Assert.Equal (new (0, 0, 4, 10), pos);
  1637. text = "0123456789";
  1638. Assert.Equal (10, text.Length);
  1639. //view.Height = Dim.Fill () - text.Length;
  1640. Application.Refresh ();
  1641. Assert.Equal (new (0, 0, 1, 5), view.Frame);
  1642. Assert.Equal (new (1, 5), view.TextFormatter.Size);
  1643. Exception exception = Record.Exception (() => Assert.Single (view.TextFormatter.GetLines ()));
  1644. Assert.Null (exception);
  1645. expected = @"
  1646. ┌──┐
  1647. │V │
  1648. │i │
  1649. │e │
  1650. │w │
  1651. │s │
  1652. │ │
  1653. │ │
  1654. │ │
  1655. └──┘
  1656. ";
  1657. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1658. Assert.Equal (new Rectangle (0, 0, 4, 10), pos);
  1659. }
  1660. [Fact]
  1661. [AutoInitShutdown]
  1662. public void AutoSize_True_View_IsEmpty_False_Minimum_Width_Wide_Rune ()
  1663. {
  1664. var text = "界View";
  1665. var view = new View
  1666. {
  1667. TextDirection = TextDirection.TopBottom_LeftRight,
  1668. Height = Dim.Fill () - text.Length,
  1669. Text = text,
  1670. AutoSize = true
  1671. };
  1672. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  1673. win.Add (view);
  1674. var top = new Toplevel ();
  1675. top.Add (win);
  1676. Application.Begin (top);
  1677. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  1678. Assert.Equal (5, text.Length);
  1679. Assert.True (view.AutoSize);
  1680. Assert.Equal (new (0, 0, 2, 5), view.Frame);
  1681. Assert.Equal (new (2, 5), view.TextFormatter.Size);
  1682. Assert.Equal (new List<string> { "界View" }, view.TextFormatter.GetLines ());
  1683. Assert.Equal (new (0, 0, 4, 10), win.Frame);
  1684. Assert.Equal (new (0, 0, 4, 10), Application.Top.Frame);
  1685. var expected = @"
  1686. ┌──┐
  1687. │界│
  1688. │V │
  1689. │i │
  1690. │e │
  1691. │w │
  1692. │ │
  1693. │ │
  1694. │ │
  1695. └──┘
  1696. ";
  1697. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1698. Assert.Equal (new (0, 0, 4, 10), pos);
  1699. text = "0123456789";
  1700. Assert.Equal (10, text.Length);
  1701. //view.Height = Dim.Fill () - text.Length;
  1702. Application.Refresh ();
  1703. Assert.Equal (new (0, 0, 2, 5), view.Frame);
  1704. Assert.Equal (new (2, 5), view.TextFormatter.Size);
  1705. Exception exception = Record.Exception (
  1706. () => Assert.Equal (
  1707. new List<string> { "界View" },
  1708. view.TextFormatter.GetLines ()
  1709. )
  1710. );
  1711. Assert.Null (exception);
  1712. expected = @"
  1713. ┌──┐
  1714. │界│
  1715. │V │
  1716. │i │
  1717. │e │
  1718. │w │
  1719. │ │
  1720. │ │
  1721. │ │
  1722. └──┘
  1723. ";
  1724. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1725. Assert.Equal (new Rectangle (0, 0, 4, 10), pos);
  1726. }
  1727. [Fact]
  1728. [AutoInitShutdown]
  1729. public void AutoSize_True_Width_Height_SetMinWidthHeight_Narrow_Wide_Runes ()
  1730. {
  1731. var text = $"0123456789{Environment.NewLine}01234567891";
  1732. var horizontalView = new View
  1733. {
  1734. AutoSize = true,
  1735. //Width = 11,
  1736. //Height = 2,
  1737. Text = text
  1738. };
  1739. var verticalView = new View
  1740. {
  1741. AutoSize = true,
  1742. Y = 3,
  1743. //Height = 11,
  1744. //Width = 2,
  1745. Text = text,
  1746. TextDirection = TextDirection.TopBottom_LeftRight
  1747. };
  1748. var win = new Window
  1749. {
  1750. AutoSize = true,
  1751. Width = Dim.Fill (),
  1752. Height = Dim.Fill (),
  1753. Text = "Window"
  1754. };
  1755. win.Add (horizontalView, verticalView);
  1756. var top = new Toplevel ();
  1757. top.Add (win);
  1758. RunState rs = Application.Begin (top);
  1759. ((FakeDriver)Application.Driver).SetBufferSize (20, 20);
  1760. Assert.True (horizontalView.AutoSize);
  1761. Assert.True (verticalView.AutoSize);
  1762. Assert.Equal (new Rectangle (0, 0, 11, 2), horizontalView.Frame);
  1763. Assert.Equal (new Rectangle (0, 3, 2, 11), verticalView.Frame);
  1764. var expected = @"
  1765. ┌──────────────────┐
  1766. │0123456789 │
  1767. │01234567891 │
  1768. │ │
  1769. │00 │
  1770. │11 │
  1771. │22 │
  1772. │33 │
  1773. │44 │
  1774. │55 │
  1775. │66 │
  1776. │77 │
  1777. │88 │
  1778. │99 │
  1779. │ 1 │
  1780. │ │
  1781. │ │
  1782. │ │
  1783. │ │
  1784. └──────────────────┘
  1785. ";
  1786. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1787. verticalView.Text = $"最初の行{Environment.NewLine}二行目";
  1788. Application.Top.Draw ();
  1789. Assert.Equal (new Rectangle (0, 3, 4, 4), verticalView.Frame);
  1790. expected = @"
  1791. ┌──────────────────┐
  1792. │0123456789 │
  1793. │01234567891 │
  1794. │ │
  1795. │最二 │
  1796. │初行 │
  1797. │の目 │
  1798. │行 │
  1799. │ │
  1800. │ │
  1801. │ │
  1802. │ │
  1803. │ │
  1804. │ │
  1805. │ │
  1806. │ │
  1807. │ │
  1808. │ │
  1809. │ │
  1810. └──────────────────┘
  1811. ";
  1812. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1813. Application.End (rs);
  1814. }
  1815. [Fact]
  1816. [AutoInitShutdown]
  1817. public void AutoSize_True_Width_Height_Stay_True_If_TextFormatter_Size_Fit ()
  1818. {
  1819. var text = "Finish 終";
  1820. var horizontalView = new View
  1821. {
  1822. Id = "horizontalView", AutoSize = true, Text = text
  1823. };
  1824. var verticalView = new View
  1825. {
  1826. Id = "verticalView",
  1827. Y = 3,
  1828. AutoSize = true,
  1829. Text = text,
  1830. TextDirection = TextDirection.TopBottom_LeftRight
  1831. };
  1832. var win = new Window { Id = "win", Width = Dim.Fill (), Height = Dim.Fill (), Text = "Window" };
  1833. win.Add (horizontalView, verticalView);
  1834. var top = new Toplevel ();
  1835. top.Add (win);
  1836. RunState rs = Application.Begin (top);
  1837. ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  1838. Assert.True (horizontalView.AutoSize);
  1839. Assert.True (verticalView.AutoSize);
  1840. Assert.Equal (new (text.GetColumns (), 1), horizontalView.TextFormatter.Size);
  1841. Assert.Equal (new (2, 8), verticalView.TextFormatter.Size);
  1842. //Assert.Equal (new (0, 0, 10, 1), horizontalView.Frame);
  1843. //Assert.Equal (new (0, 3, 10, 9), verticalView.Frame);
  1844. var expected = @"
  1845. ┌────────────────────┐
  1846. │Finish 終 │
  1847. │ │
  1848. │ │
  1849. │F │
  1850. │i │
  1851. │n │
  1852. │i │
  1853. │s │
  1854. │h │
  1855. │ │
  1856. │終 │
  1857. │ │
  1858. │ │
  1859. │ │
  1860. │ │
  1861. │ │
  1862. │ │
  1863. │ │
  1864. │ │
  1865. │ │
  1866. └────────────────────┘
  1867. ";
  1868. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1869. verticalView.Text = "最初の行二行目";
  1870. Application.Top.Draw ();
  1871. Assert.True (horizontalView.AutoSize);
  1872. Assert.True (verticalView.AutoSize);
  1873. // height was initialized with 8 and can only grow or keep initial value
  1874. Assert.Equal (new Rectangle (0, 3, 2, 7), verticalView.Frame);
  1875. expected = @"
  1876. ┌────────────────────┐
  1877. │Finish 終 │
  1878. │ │
  1879. │ │
  1880. │最 │
  1881. │初 │
  1882. │の │
  1883. │行 │
  1884. │二 │
  1885. │行 │
  1886. │目 │
  1887. │ │
  1888. │ │
  1889. │ │
  1890. │ │
  1891. │ │
  1892. │ │
  1893. │ │
  1894. │ │
  1895. │ │
  1896. │ │
  1897. └────────────────────┘
  1898. ";
  1899. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1900. Application.End (rs);
  1901. }
  1902. // Moved from DimTests.cs - This is really a bogus test
  1903. [Theory]
  1904. [AutoInitShutdown]
  1905. [InlineData (0, true)]
  1906. [InlineData (0, false)]
  1907. [InlineData (50, true)]
  1908. [InlineData (50, false)]
  1909. public void DimPercentPlusOne (int startingDistance, bool testHorizontal)
  1910. {
  1911. var container = new View { Width = 100, Height = 100 };
  1912. var label = new Label
  1913. {
  1914. X = testHorizontal ? startingDistance : 0, Y = testHorizontal ? 0 : startingDistance
  1915. //Width = testHorizontal ? Dim.Percent (50) + 1 : 1,
  1916. //Height = testHorizontal ? 1 : Dim.Percent (50) + 1
  1917. };
  1918. container.Add (label);
  1919. var top = new Toplevel ();
  1920. top.Add (container);
  1921. top.BeginInit ();
  1922. top.EndInit ();
  1923. top.LayoutSubviews ();
  1924. Assert.Equal (100, container.Frame.Width);
  1925. Assert.Equal (100, container.Frame.Height);
  1926. if (testHorizontal)
  1927. {
  1928. Assert.Equal (0, label.Frame.Width); // BUGBUG: Shoudl be 0 not since there's no text
  1929. Assert.Equal (0, label.Frame.Height);
  1930. }
  1931. else
  1932. {
  1933. Assert.Equal (0, label.Frame.Width); // BUGBUG: Shoudl be 0 not since there's no text
  1934. Assert.Equal (0, label.Frame.Height);
  1935. }
  1936. }
  1937. [Fact]
  1938. [AutoInitShutdown]
  1939. public void Excess_Text_Is_Erased_When_The_Width_Is_Reduced ()
  1940. {
  1941. var lbl = new Label { Text = "123" };
  1942. var top = new Toplevel ();
  1943. top.Add (lbl);
  1944. RunState rs = Application.Begin (top);
  1945. Assert.True (lbl.AutoSize);
  1946. Assert.Equal ("123 ", GetContents ());
  1947. lbl.Text = "12";
  1948. Assert.Equal (new Rectangle (0, 0, 2, 1), lbl.Frame);
  1949. Assert.Equal (new Rectangle (0, 0, 3, 1), lbl._needsDisplayRect);
  1950. Assert.Equal (new Rectangle (0, 0, 0, 0), lbl.SuperView._needsDisplayRect);
  1951. Assert.True (lbl.SuperView.LayoutNeeded);
  1952. lbl.SuperView.Draw ();
  1953. Assert.Equal ("12 ", GetContents ());
  1954. string GetContents ()
  1955. {
  1956. var text = "";
  1957. for (var i = 0; i < 4; i++)
  1958. {
  1959. text += Application.Driver.Contents [0, i].Rune;
  1960. }
  1961. return text;
  1962. }
  1963. Application.End (rs);
  1964. }
  1965. //[Fact]
  1966. //public void GetCurrentHeight_TrySetHeight ()
  1967. //{
  1968. // var top = new View { X = 0, Y = 0, Height = 20 };
  1969. // var v = new View { Height = Dim.Fill (), ValidatePosDim = true };
  1970. // top.Add (v);
  1971. // top.BeginInit ();
  1972. // top.EndInit ();
  1973. // top.LayoutSubviews ();
  1974. // Assert.False (v.AutoSize);
  1975. // Assert.False (v.TrySetHeight (0, out _));
  1976. // Assert.Equal (20, v.Frame.Height);
  1977. // v.Height = Dim.Fill (1);
  1978. // top.LayoutSubviews ();
  1979. // Assert.False (v.TrySetHeight (0, out _));
  1980. // Assert.True (v.Height is Dim.DimFill);
  1981. // Assert.Equal (19, v.Frame.Height);
  1982. // v.AutoSize = true;
  1983. // top.LayoutSubviews ();
  1984. // Assert.True (v.TrySetHeight (0, out _));
  1985. // Assert.True (v.Height is Dim.DimAbsolute);
  1986. // Assert.Equal (0, v.Frame.Height); // No text, so height is 0
  1987. // top.Dispose ();
  1988. //}
  1989. //[Fact]
  1990. //[TestRespondersDisposed]
  1991. //public void GetCurrentWidth_TrySetWidth ()
  1992. //{
  1993. // var top = new View { X = 0, Y = 0, Width = 80 };
  1994. // var v = new View { Width = Dim.Fill (), ValidatePosDim = true };
  1995. // top.Add (v);
  1996. // top.BeginInit ();
  1997. // top.EndInit ();
  1998. // top.LayoutSubviews ();
  1999. // Assert.False (v.AutoSize);
  2000. // Assert.False (v.TrySetWidth (0, out _));
  2001. // Assert.True (v.Width is Dim.DimFill);
  2002. // Assert.Equal (80, v.Frame.Width);
  2003. // v.Width = Dim.Fill (1);
  2004. // top.LayoutSubviews ();
  2005. // Assert.False (v.TrySetWidth (0, out _));
  2006. // Assert.True (v.Width is Dim.DimFill);
  2007. // Assert.Equal (79, v.Frame.Width);
  2008. // v.AutoSize = true;
  2009. // top.LayoutSubviews ();
  2010. // Assert.True (v.TrySetWidth (0, out _));
  2011. // Assert.True (v.Width is Dim.DimAbsolute);
  2012. // Assert.Equal (0, v.Frame.Width); // No text, so width is 0
  2013. // top.Dispose ();
  2014. //}
  2015. // [Fact]
  2016. // [AutoInitShutdown]
  2017. // public void AutoSize_False_TextDirection_Toggle ()
  2018. // {
  2019. // var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  2020. // // View is AutoSize == true
  2021. // var view = new View ();
  2022. // win.Add (view);
  2023. // var top = new Toplevel ();
  2024. // top.Add (win);
  2025. // var rs = Application.Begin (top);
  2026. // ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  2027. // Assert.Equal (new (0, 0, 22, 22), win.Frame);
  2028. // Assert.Equal (new (0, 0, 22, 22), win.Margin.Frame);
  2029. // Assert.Equal (new (0, 0, 22, 22), win.Border.Frame);
  2030. // Assert.Equal (new (1, 1, 20, 20), win.Padding.Frame);
  2031. // Assert.False (view.AutoSize);
  2032. // Assert.Equal (TextDirection.LeftRight_TopBottom, view.TextDirection);
  2033. // Assert.Equal (Rectangle.Empty, view.Frame);
  2034. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2035. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2036. // Assert.Equal ("Absolute(0)", view.Width.ToString ());
  2037. // Assert.Equal ("Absolute(0)", view.Height.ToString ());
  2038. // var expected = @"
  2039. //┌────────────────────┐
  2040. //│ │
  2041. //│ │
  2042. //│ │
  2043. //│ │
  2044. //│ │
  2045. //│ │
  2046. //│ │
  2047. //│ │
  2048. //│ │
  2049. //│ │
  2050. //│ │
  2051. //│ │
  2052. //│ │
  2053. //│ │
  2054. //│ │
  2055. //│ │
  2056. //│ │
  2057. //│ │
  2058. //│ │
  2059. //│ │
  2060. //└────────────────────┘";
  2061. // var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2062. // Assert.Equal (new (0, 0, 22, 22), pos);
  2063. // view.Text = "Hello World";
  2064. // view.Width = 11;
  2065. // view.Height = 1;
  2066. // win.LayoutSubviews ();
  2067. // Application.Refresh ();
  2068. // Assert.Equal (new (0, 0, 11, 1), view.Frame);
  2069. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2070. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2071. // Assert.Equal ("Absolute(11)", view.Width.ToString ());
  2072. // Assert.Equal ("Absolute(1)", view.Height.ToString ());
  2073. // expected = @"
  2074. //┌────────────────────┐
  2075. //│Hello World │
  2076. //│ │
  2077. //│ │
  2078. //│ │
  2079. //│ │
  2080. //│ │
  2081. //│ │
  2082. //│ │
  2083. //│ │
  2084. //│ │
  2085. //│ │
  2086. //│ │
  2087. //│ │
  2088. //│ │
  2089. //│ │
  2090. //│ │
  2091. //│ │
  2092. //│ │
  2093. //│ │
  2094. //│ │
  2095. //└────────────────────┘";
  2096. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2097. // Assert.Equal (new (0, 0, 22, 22), pos);
  2098. // view.AutoSize = true;
  2099. // view.Text = "Hello Worlds";
  2100. // Application.Refresh ();
  2101. // Assert.Equal (new (0, 0, 12, 1), view.Frame);
  2102. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2103. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2104. // Assert.Equal ("Absolute(11)", view.Width.ToString ());
  2105. // Assert.Equal ("Absolute(1)", view.Height.ToString ());
  2106. // expected = @"
  2107. //┌────────────────────┐
  2108. //│Hello Worlds │
  2109. //│ │
  2110. //│ │
  2111. //│ │
  2112. //│ │
  2113. //│ │
  2114. //│ │
  2115. //│ │
  2116. //│ │
  2117. //│ │
  2118. //│ │
  2119. //│ │
  2120. //│ │
  2121. //│ │
  2122. //│ │
  2123. //│ │
  2124. //│ │
  2125. //│ │
  2126. //│ │
  2127. //│ │
  2128. //└────────────────────┘";
  2129. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2130. // Assert.Equal (new (0, 0, 22, 22), pos);
  2131. // view.TextDirection = TextDirection.TopBottom_LeftRight;
  2132. // Application.Refresh ();
  2133. // Assert.Equal (new (0, 0, 11, 12), view.Frame);
  2134. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2135. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2136. // Assert.Equal ("Absolute(11)", view.Width.ToString ());
  2137. // Assert.Equal ("Absolute(1)", view.Height.ToString ());
  2138. // expected = @"
  2139. //┌────────────────────┐
  2140. //│H │
  2141. //│e │
  2142. //│l │
  2143. //│l │
  2144. //│o │
  2145. //│ │
  2146. //│W │
  2147. //│o │
  2148. //│r │
  2149. //│l │
  2150. //│d │
  2151. //│s │
  2152. //│ │
  2153. //│ │
  2154. //│ │
  2155. //│ │
  2156. //│ │
  2157. //│ │
  2158. //│ │
  2159. //│ │
  2160. //└────────────────────┘";
  2161. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2162. // Assert.Equal (new (0, 0, 22, 22), pos);
  2163. // view.AutoSize = false;
  2164. // view.Height = 1;
  2165. // Application.Refresh ();
  2166. // Assert.Equal (new (0, 0, 11, 1), view.Frame);
  2167. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2168. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2169. // Assert.Equal ("Absolute(11)", view.Width.ToString ());
  2170. // Assert.Equal ("Absolute(1)", view.Height.ToString ());
  2171. // expected = @"
  2172. //┌────────────────────┐
  2173. //│HelloWorlds │
  2174. //│ │
  2175. //│ │
  2176. //│ │
  2177. //│ │
  2178. //│ │
  2179. //│ │
  2180. //│ │
  2181. //│ │
  2182. //│ │
  2183. //│ │
  2184. //│ │
  2185. //│ │
  2186. //│ │
  2187. //│ │
  2188. //│ │
  2189. //│ │
  2190. //│ │
  2191. //│ │
  2192. //│ │
  2193. //└────────────────────┘";
  2194. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2195. // Assert.Equal (new (0, 0, 22, 22), pos);
  2196. // view.PreserveTrailingSpaces = true;
  2197. // Application.Refresh ();
  2198. // Assert.Equal (new (0, 0, 11, 1), view.Frame);
  2199. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2200. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2201. // Assert.Equal ("Absolute(11)", view.Width.ToString ());
  2202. // Assert.Equal ("Absolute(1)", view.Height.ToString ());
  2203. // expected = @"
  2204. //┌────────────────────┐
  2205. //│Hello World │
  2206. //│ │
  2207. //│ │
  2208. //│ │
  2209. //│ │
  2210. //│ │
  2211. //│ │
  2212. //│ │
  2213. //│ │
  2214. //│ │
  2215. //│ │
  2216. //│ │
  2217. //│ │
  2218. //│ │
  2219. //│ │
  2220. //│ │
  2221. //│ │
  2222. //│ │
  2223. //│ │
  2224. //│ │
  2225. //└────────────────────┘";
  2226. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2227. // Assert.Equal (new (0, 0, 22, 22), pos);
  2228. // view.PreserveTrailingSpaces = false;
  2229. // var f = view.Frame;
  2230. // view.Width = f.Height;
  2231. // view.Height = f.Width;
  2232. // view.TextDirection = TextDirection.TopBottom_LeftRight;
  2233. // Application.Refresh ();
  2234. // Assert.Equal (new (0, 0, 1, 11), view.Frame);
  2235. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2236. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2237. // Assert.Equal ("Absolute(1)", view.Width.ToString ());
  2238. // Assert.Equal ("Absolute(11)", view.Height.ToString ());
  2239. // expected = @"
  2240. //┌────────────────────┐
  2241. //│H │
  2242. //│e │
  2243. //│l │
  2244. //│l │
  2245. //│o │
  2246. //│ │
  2247. //│W │
  2248. //│o │
  2249. //│r │
  2250. //│l │
  2251. //│d │
  2252. //│ │
  2253. //│ │
  2254. //│ │
  2255. //│ │
  2256. //│ │
  2257. //│ │
  2258. //│ │
  2259. //│ │
  2260. //│ │
  2261. //└────────────────────┘";
  2262. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2263. // Assert.Equal (new (0, 0, 22, 22), pos);
  2264. // view.AutoSize = true;
  2265. // Application.Refresh ();
  2266. // Assert.Equal (new (0, 0, 1, 12), view.Frame);
  2267. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2268. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2269. // Assert.Equal ("Absolute(1)", view.Width.ToString ());
  2270. // Assert.Equal ("Absolute(12)", view.Height.ToString ());
  2271. // expected = @"
  2272. //┌────────────────────┐
  2273. //│H │
  2274. //│e │
  2275. //│l │
  2276. //│l │
  2277. //│o │
  2278. //│ │
  2279. //│W │
  2280. //│o │
  2281. //│r │
  2282. //│l │
  2283. //│d │
  2284. //│s │
  2285. //│ │
  2286. //│ │
  2287. //│ │
  2288. //│ │
  2289. //│ │
  2290. //│ │
  2291. //│ │
  2292. //│ │
  2293. //└────────────────────┘";
  2294. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2295. // Assert.Equal (new (0, 0, 22, 22), pos);
  2296. // Application.End (rs);
  2297. // }
  2298. [Fact]
  2299. [AutoInitShutdown]
  2300. public void GetTextFormatterBoundsSize_GetSizeNeededForText_HotKeySpecifier ()
  2301. {
  2302. var text = "Say Hello 你";
  2303. // Frame: 0, 0, 12, 1
  2304. var horizontalView = new View { AutoSize = true };
  2305. horizontalView.TextFormatter.HotKeySpecifier = (Rune)'_';
  2306. horizontalView.Text = text;
  2307. // Frame: 0, 0, 1, 12
  2308. var verticalView = new View
  2309. {
  2310. AutoSize = true, TextDirection = TextDirection.TopBottom_LeftRight
  2311. };
  2312. verticalView.Text = text;
  2313. verticalView.TextFormatter.HotKeySpecifier = (Rune)'_';
  2314. var top = new Toplevel ();
  2315. top.Add (horizontalView, verticalView);
  2316. Application.Begin (top);
  2317. ((FakeDriver)Application.Driver).SetBufferSize (50, 50);
  2318. Assert.True (horizontalView.AutoSize);
  2319. Assert.Equal (new (0, 0, 12, 1), horizontalView.Frame);
  2320. Assert.Equal (new (12, 1), horizontalView.GetSizeNeededForTextWithoutHotKey ());
  2321. Assert.Equal (horizontalView.Frame.Size, horizontalView.GetSizeNeededForTextWithoutHotKey ());
  2322. Assert.True (verticalView.AutoSize);
  2323. Assert.Equal (new (0, 0, 2, 11), verticalView.Frame);
  2324. Assert.Equal (new (2, 11), verticalView.GetSizeNeededForTextWithoutHotKey ());
  2325. Assert.Equal (verticalView.Frame.Size, verticalView.GetSizeNeededForTextWithoutHotKey ());
  2326. text = "012345678你";
  2327. horizontalView.Text = text;
  2328. verticalView.Text = text;
  2329. Assert.True (horizontalView.AutoSize);
  2330. Assert.Equal (new (0, 0, 11, 1), horizontalView.Frame);
  2331. Assert.Equal (new (11, 1), horizontalView.GetSizeNeededForTextWithoutHotKey ());
  2332. Assert.Equal (horizontalView.Frame.Size, horizontalView.GetSizeNeededForTextWithoutHotKey ());
  2333. Assert.True (verticalView.AutoSize);
  2334. Assert.Equal (new (0, 0, 2, 10), verticalView.Frame);
  2335. Assert.Equal (new (2, 10), verticalView.GetSizeNeededForTextWithoutHotKey ());
  2336. Assert.Equal (verticalView.Frame.Size, verticalView.GetSizeNeededForTextWithoutHotKey ());
  2337. }
  2338. [Fact]
  2339. public void SetRelativeLayout_Respects_AutoSize ()
  2340. {
  2341. var view = new View { Frame = new (0, 0, 10, 0), AutoSize = true };
  2342. view.Text = "01234567890123456789";
  2343. Assert.True (view.AutoSize);
  2344. Assert.Equal (new (0, 0, 20, 1), view.Frame);
  2345. view.SetRelativeLayout (new (25, 5));
  2346. Assert.Equal (new (0, 0, 20, 1), view.Frame);
  2347. }
  2348. //[Fact]
  2349. //[AutoInitShutdown]
  2350. //public void TrySetHeight_ForceValidatePosDim ()
  2351. //{
  2352. // var top = new View { X = 0, Y = 0, Height = 20 };
  2353. // var v = new View { Height = Dim.Fill (), ValidatePosDim = true };
  2354. // top.Add (v);
  2355. // Assert.False (v.TrySetHeight (10, out int rHeight));
  2356. // Assert.Equal (10, rHeight);
  2357. // v.Height = Dim.Fill (1);
  2358. // Assert.False (v.TrySetHeight (10, out rHeight));
  2359. // Assert.Equal (9, rHeight);
  2360. // v.Height = 0;
  2361. // Assert.True (v.TrySetHeight (10, out rHeight));
  2362. // Assert.Equal (10, rHeight);
  2363. // Assert.False (v.IsInitialized);
  2364. // var toplevel = new Toplevel ();
  2365. // toplevel.Add (top);
  2366. // Application.Begin (toplevel);
  2367. // Assert.True (v.IsInitialized);
  2368. // v.Height = 15;
  2369. // Assert.True (v.TrySetHeight (5, out rHeight));
  2370. // Assert.Equal (5, rHeight);
  2371. //}
  2372. //[Fact]
  2373. //[AutoInitShutdown]
  2374. //public void TrySetWidth_ForceValidatePosDim ()
  2375. //{
  2376. // var top = new View { X = 0, Y = 0, Width = 80 };
  2377. // var v = new View { Width = Dim.Fill (), ValidatePosDim = true };
  2378. // top.Add (v);
  2379. // Assert.False (v.TrySetWidth (70, out int rWidth));
  2380. // Assert.Equal (70, rWidth);
  2381. // v.Width = Dim.Fill (1);
  2382. // Assert.False (v.TrySetWidth (70, out rWidth));
  2383. // Assert.Equal (69, rWidth);
  2384. // v.Width = 0;
  2385. // Assert.True (v.TrySetWidth (70, out rWidth));
  2386. // Assert.Equal (70, rWidth);
  2387. // Assert.False (v.IsInitialized);
  2388. // var toplevel = new Toplevel ();
  2389. // toplevel.Add (top);
  2390. // Application.Begin (toplevel);
  2391. // Assert.True (v.IsInitialized);
  2392. // v.Width = 75;
  2393. // Assert.True (v.TrySetWidth (60, out rWidth));
  2394. // Assert.Equal (60, rWidth);
  2395. //}
  2396. [Theory]
  2397. [AutoInitShutdown]
  2398. [InlineData (true)]
  2399. [InlineData (false)]
  2400. public void View_Draw_Horizontal_Simple_TextAlignments (bool autoSize)
  2401. {
  2402. var text = "Hello World";
  2403. var width = 20;
  2404. var lblLeft = new View { Text = text, Width = width, Height = 1, AutoSize = autoSize };
  2405. var lblCenter = new View
  2406. {
  2407. Text = text,
  2408. Y = 1,
  2409. Width = width,
  2410. Height = 1,
  2411. TextAlignment = TextAlignment.Centered,
  2412. AutoSize = autoSize
  2413. };
  2414. var lblRight = new View
  2415. {
  2416. Text = text,
  2417. Y = 2,
  2418. Width = width,
  2419. Height = 1,
  2420. TextAlignment = TextAlignment.Right,
  2421. AutoSize = autoSize
  2422. };
  2423. var lblJust = new View
  2424. {
  2425. Text = text,
  2426. Y = 3,
  2427. Width = width,
  2428. Height = 1,
  2429. TextAlignment = TextAlignment.Justified,
  2430. AutoSize = autoSize
  2431. };
  2432. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  2433. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2434. var top = new Toplevel ();
  2435. top.Add (frame);
  2436. Application.Begin (top);
  2437. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 6);
  2438. Assert.True (lblLeft.AutoSize == autoSize);
  2439. Assert.True (lblCenter.AutoSize == autoSize);
  2440. Assert.True (lblRight.AutoSize == autoSize);
  2441. Assert.True (lblJust.AutoSize == autoSize);
  2442. Assert.True (lblLeft.TextFormatter.AutoSize == autoSize);
  2443. Assert.True (lblCenter.TextFormatter.AutoSize == autoSize);
  2444. Assert.True (lblRight.TextFormatter.AutoSize == autoSize);
  2445. Assert.True (lblJust.TextFormatter.AutoSize == autoSize);
  2446. if (autoSize)
  2447. {
  2448. Size expectedSize = new (11, 1);
  2449. Assert.Equal (expectedSize, lblLeft.TextFormatter.Size);
  2450. Assert.Equal (expectedSize, lblCenter.TextFormatter.Size);
  2451. Assert.Equal (expectedSize, lblRight.TextFormatter.Size);
  2452. Assert.Equal (expectedSize, lblJust.TextFormatter.Size);
  2453. }
  2454. else
  2455. {
  2456. Size expectedSize = new (width, 1);
  2457. Assert.Equal (expectedSize, lblLeft.TextFormatter.Size);
  2458. Assert.Equal (expectedSize, lblCenter.TextFormatter.Size);
  2459. Assert.Equal (expectedSize, lblRight.TextFormatter.Size);
  2460. Assert.Equal (expectedSize, lblJust.TextFormatter.Size);
  2461. }
  2462. Assert.Equal (new (0, 0, width + 2, 6), frame.Frame);
  2463. string expected ;
  2464. if (autoSize)
  2465. {
  2466. expected = @"
  2467. ┌────────────────────┐
  2468. │Hello World │
  2469. │Hello World │
  2470. │Hello World │
  2471. │Hello World │
  2472. └────────────────────┘
  2473. ";
  2474. }
  2475. else
  2476. {
  2477. expected = @"
  2478. ┌────────────────────┐
  2479. │Hello World │
  2480. │ Hello World │
  2481. │ Hello World│
  2482. │Hello World│
  2483. └────────────────────┘
  2484. ";
  2485. }
  2486. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2487. Assert.Equal (new (0, 0, width + 2, 6), pos);
  2488. }
  2489. [Theory]
  2490. [AutoInitShutdown]
  2491. [InlineData (true)]
  2492. [InlineData (false)]
  2493. public void View_Draw_Vertical_Simple_TextAlignments (bool autoSize)
  2494. {
  2495. var text = "Hello World";
  2496. var height = 20;
  2497. var lblLeft = new View
  2498. {
  2499. Text = text,
  2500. Width = 1,
  2501. Height = height,
  2502. TextDirection = TextDirection.TopBottom_LeftRight,
  2503. AutoSize = autoSize
  2504. };
  2505. var lblCenter = new View
  2506. {
  2507. Text = text,
  2508. X = 2,
  2509. Width = 1,
  2510. Height = height,
  2511. TextDirection = TextDirection.TopBottom_LeftRight,
  2512. AutoSize = autoSize,
  2513. VerticalTextAlignment = VerticalTextAlignment.Middle
  2514. };
  2515. var lblRight = new View
  2516. {
  2517. Text = text,
  2518. X = 4,
  2519. Width = 1,
  2520. Height = height,
  2521. TextDirection = TextDirection.TopBottom_LeftRight,
  2522. AutoSize = autoSize,
  2523. VerticalTextAlignment = VerticalTextAlignment.Bottom
  2524. };
  2525. var lblJust = new View
  2526. {
  2527. Text = text,
  2528. X = 6,
  2529. Width = 1,
  2530. Height = height,
  2531. TextDirection = TextDirection.TopBottom_LeftRight,
  2532. AutoSize = autoSize,
  2533. VerticalTextAlignment = VerticalTextAlignment.Justified
  2534. };
  2535. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  2536. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2537. var top = new Toplevel ();
  2538. top.Add (frame);
  2539. Application.Begin (top);
  2540. ((FakeDriver)Application.Driver).SetBufferSize (9, height + 2);
  2541. Assert.True (lblLeft.AutoSize == autoSize);
  2542. Assert.True (lblCenter.AutoSize == autoSize);
  2543. Assert.True (lblRight.AutoSize == autoSize);
  2544. Assert.True (lblJust.AutoSize == autoSize);
  2545. Assert.True (lblLeft.TextFormatter.AutoSize == autoSize);
  2546. Assert.True (lblCenter.TextFormatter.AutoSize == autoSize);
  2547. Assert.True (lblRight.TextFormatter.AutoSize == autoSize);
  2548. Assert.True (lblJust.TextFormatter.AutoSize == autoSize);
  2549. if (autoSize)
  2550. {
  2551. Assert.Equal (new (1, 11), lblLeft.TextFormatter.Size);
  2552. Assert.Equal (new (1, 11), lblCenter.TextFormatter.Size);
  2553. Assert.Equal (new (1, 11), lblRight.TextFormatter.Size);
  2554. Assert.Equal (new (1, 11), lblJust.TextFormatter.Size);
  2555. Assert.Equal (new (0, 0, 9, height + 2), frame.Frame);
  2556. }
  2557. else
  2558. {
  2559. Assert.Equal (new (1, height), lblLeft.TextFormatter.Size);
  2560. Assert.Equal (new (1, height), lblCenter.TextFormatter.Size);
  2561. Assert.Equal (new (1, height), lblRight.TextFormatter.Size);
  2562. Assert.Equal (new (1, height), lblJust.TextFormatter.Size);
  2563. Assert.Equal (new (0, 0, 9, height + 2), frame.Frame);
  2564. }
  2565. string expected;
  2566. if (autoSize)
  2567. {
  2568. expected = @"
  2569. ┌───────┐
  2570. │H H H H│
  2571. │e e e e│
  2572. │l l l l│
  2573. │l l l l│
  2574. │o o o o│
  2575. │ │
  2576. │W W W W│
  2577. │o o o o│
  2578. │r r r r│
  2579. │l l l l│
  2580. │d d d d│
  2581. │ │
  2582. │ │
  2583. │ │
  2584. │ │
  2585. │ │
  2586. │ │
  2587. │ │
  2588. │ │
  2589. │ │
  2590. └───────┘
  2591. ";
  2592. }
  2593. else
  2594. {
  2595. expected = @"
  2596. ┌───────┐
  2597. │H H│
  2598. │e e│
  2599. │l l│
  2600. │l l│
  2601. │o H o│
  2602. │ e │
  2603. │W l │
  2604. │o l │
  2605. │r o │
  2606. │l H │
  2607. │d W e │
  2608. │ o l │
  2609. │ r l │
  2610. │ l o │
  2611. │ d │
  2612. │ W W│
  2613. │ o o│
  2614. │ r r│
  2615. │ l l│
  2616. │ d d│
  2617. └───────┘
  2618. ";
  2619. }
  2620. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2621. Assert.Equal (new (0, 0, 9, height + 2), pos);
  2622. }
  2623. }