AutoSizeTextTests.cs 83 KB

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