AutoSizeTrueTests.cs 65 KB

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