AutoSizeTextTests.cs 87 KB

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