AutoSizeTrueTests.cs 104 KB

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