AutoSizeTrueTests.cs 99 KB

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