AutoSizeTrueTests.cs 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041
  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 (5, 1), 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 (new (5, 1), 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 (5, 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. var view = new View ();
  1394. win.Add (view);
  1395. var top = new Toplevel ();
  1396. top.Add (win);
  1397. RunState rs = Application.Begin (top);
  1398. ((FakeDriver)Application.Driver).SetBufferSize (15, 15);
  1399. Assert.Equal (new Rectangle (0, 0, 15, 15), win.Frame);
  1400. Assert.Equal (new Rectangle (0, 0, 15, 15), win.Margin.Frame);
  1401. Assert.Equal (new Rectangle (0, 0, 15, 15), win.Border.Frame);
  1402. Assert.Equal (new Rectangle (1, 1, 13, 13), win.Padding.Frame);
  1403. Assert.False (view.AutoSize);
  1404. Assert.Equal (TextDirection.LeftRight_TopBottom, view.TextDirection);
  1405. Assert.Equal (Rectangle.Empty, view.Frame);
  1406. Assert.Equal ("Absolute(0)", view.X.ToString ());
  1407. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1408. Assert.Equal ("Absolute(0)", view.Width.ToString ());
  1409. Assert.Equal ("Absolute(0)", view.Height.ToString ());
  1410. var expected = @"
  1411. ┌─────────────┐
  1412. │ │
  1413. │ │
  1414. │ │
  1415. │ │
  1416. │ │
  1417. │ │
  1418. │ │
  1419. │ │
  1420. │ │
  1421. │ │
  1422. │ │
  1423. │ │
  1424. │ │
  1425. └─────────────┘
  1426. ";
  1427. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1428. view.Text = "Hello World";
  1429. view.Width = 11;
  1430. view.Height = 1;
  1431. win.LayoutSubviews ();
  1432. Application.Refresh ();
  1433. Assert.Equal (new Rectangle (0, 0, 11, 1), view.Frame);
  1434. Assert.Equal ("Absolute(0)", view.X.ToString ());
  1435. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  1436. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  1437. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  1438. expected = @"
  1439. ┌─────────────┐
  1440. │Hello World │
  1441. │ │
  1442. │ │
  1443. │ │
  1444. │ │
  1445. │ │
  1446. │ │
  1447. │ │
  1448. │ │
  1449. │ │
  1450. │ │
  1451. │ │
  1452. │ │
  1453. └─────────────┘
  1454. ";
  1455. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1456. view.AutoSize = true;
  1457. view.Text = "Hello Worlds";
  1458. Application.Refresh ();
  1459. int len = "Hello Worlds".Length;
  1460. Assert.Equal (12, len);
  1461. Assert.Equal (new Rectangle (0, 0, len, 1), view.Frame);
  1462. expected = @"
  1463. ┌─────────────┐
  1464. │Hello Worlds │
  1465. │ │
  1466. │ │
  1467. │ │
  1468. │ │
  1469. │ │
  1470. │ │
  1471. │ │
  1472. │ │
  1473. │ │
  1474. │ │
  1475. │ │
  1476. │ │
  1477. └─────────────┘
  1478. ";
  1479. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1480. view.TextDirection = TextDirection.TopBottom_LeftRight;
  1481. Application.Refresh ();
  1482. Assert.Equal (new Rectangle (0, 0, 1, 12), view.Frame);
  1483. Assert.Equal (new Rectangle (0, 0, 1, 12), view.Frame);
  1484. expected = @"
  1485. ┌─────────────┐
  1486. │H │
  1487. │e │
  1488. │l │
  1489. │l │
  1490. │o │
  1491. │ │
  1492. │W │
  1493. │o │
  1494. │r │
  1495. │l │
  1496. │d │
  1497. │s │
  1498. │ │
  1499. └─────────────┘
  1500. ";
  1501. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1502. // Setting to false causes Width and Height to be set to the current ContentSize
  1503. view.AutoSize = false;
  1504. Assert.Equal (new Rectangle (0, 0, 1, 12), view.Frame);
  1505. view.Width = 12;
  1506. view.Height = 1;
  1507. view.TextFormatter.Size = new (12, 1);
  1508. win.LayoutSubviews ();
  1509. Assert.Equal (new Size (12, 1), view.TextFormatter.Size);
  1510. Assert.Equal (new Rectangle (0, 0, 12, 1), view.Frame);
  1511. top.Clear ();
  1512. view.Draw ();
  1513. expected = @" HelloWorlds";
  1514. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1515. Application.Refresh ();
  1516. // TextDirection.TopBottom_LeftRight - Height of 1 and Width of 12 means
  1517. // that the text will be spread "vertically" across 1 line.
  1518. // Hence no space.
  1519. expected = @"
  1520. ┌─────────────┐
  1521. │HelloWorlds │
  1522. │ │
  1523. │ │
  1524. │ │
  1525. │ │
  1526. │ │
  1527. │ │
  1528. │ │
  1529. │ │
  1530. │ │
  1531. │ │
  1532. │ │
  1533. │ │
  1534. └─────────────┘
  1535. ";
  1536. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1537. view.PreserveTrailingSpaces = true;
  1538. Application.Refresh ();
  1539. Assert.Equal (new Rectangle (0, 0, 12, 1), view.Frame);
  1540. expected = @"
  1541. ┌─────────────┐
  1542. │Hello Worlds │
  1543. │ │
  1544. │ │
  1545. │ │
  1546. │ │
  1547. │ │
  1548. │ │
  1549. │ │
  1550. │ │
  1551. │ │
  1552. │ │
  1553. │ │
  1554. │ │
  1555. └─────────────┘
  1556. ";
  1557. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1558. view.PreserveTrailingSpaces = false;
  1559. Rectangle f = view.Frame;
  1560. view.Width = f.Height;
  1561. view.Height = f.Width;
  1562. view.TextDirection = TextDirection.TopBottom_LeftRight;
  1563. Application.Refresh ();
  1564. Assert.Equal (new Rectangle (0, 0, 1, 12), view.Frame);
  1565. expected = @"
  1566. ┌─────────────┐
  1567. │H │
  1568. │e │
  1569. │l │
  1570. │l │
  1571. │o │
  1572. │ │
  1573. │W │
  1574. │o │
  1575. │r │
  1576. │l │
  1577. │d │
  1578. │s │
  1579. │ │
  1580. └─────────────┘
  1581. ";
  1582. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1583. view.AutoSize = true;
  1584. Application.Refresh ();
  1585. Assert.Equal (new Rectangle (0, 0, 1, 12), view.Frame);
  1586. expected = @"
  1587. ┌─────────────┐
  1588. │H │
  1589. │e │
  1590. │l │
  1591. │l │
  1592. │o │
  1593. │ │
  1594. │W │
  1595. │o │
  1596. │r │
  1597. │l │
  1598. │d │
  1599. │s │
  1600. │ │
  1601. └─────────────┘
  1602. ";
  1603. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1604. Application.End (rs);
  1605. }
  1606. [Fact]
  1607. [AutoInitShutdown]
  1608. public void AutoSize_True_View_IsEmpty_False_Minimum_Width ()
  1609. {
  1610. var text = "Views";
  1611. var view = new View
  1612. {
  1613. TextDirection = TextDirection.TopBottom_LeftRight,
  1614. Height = Dim.Fill () - text.Length,
  1615. Text = text,
  1616. AutoSize = true
  1617. };
  1618. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  1619. win.Add (view);
  1620. var top = new Toplevel ();
  1621. top.Add (win);
  1622. Application.Begin (top);
  1623. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  1624. Assert.Equal (5, text.Length);
  1625. Assert.True (view.AutoSize);
  1626. Assert.Equal (new (0, 0, 1, 5), view.Frame);
  1627. Assert.Equal (new (1, 5), view.TextFormatter.Size);
  1628. Assert.Equal (new List<string> { "Views" }, view.TextFormatter.GetLines ());
  1629. Assert.Equal (new (0, 0, 4, 10), win.Frame);
  1630. Assert.Equal (new (0, 0, 4, 10), Application.Top.Frame);
  1631. var expected = @"
  1632. ┌──┐
  1633. │V │
  1634. │i │
  1635. │e │
  1636. │w │
  1637. │s │
  1638. │ │
  1639. │ │
  1640. │ │
  1641. └──┘
  1642. ";
  1643. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1644. Assert.Equal (new (0, 0, 4, 10), pos);
  1645. text = "0123456789";
  1646. Assert.Equal (10, text.Length);
  1647. //view.Height = Dim.Fill () - text.Length;
  1648. Application.Refresh ();
  1649. Assert.Equal (new (0, 0, 1, 5), view.Frame);
  1650. Assert.Equal (new (1, 5), view.TextFormatter.Size);
  1651. Exception exception = Record.Exception (() => Assert.Single (view.TextFormatter.GetLines ()));
  1652. Assert.Null (exception);
  1653. expected = @"
  1654. ┌──┐
  1655. │V │
  1656. │i │
  1657. │e │
  1658. │w │
  1659. │s │
  1660. │ │
  1661. │ │
  1662. │ │
  1663. └──┘
  1664. ";
  1665. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1666. Assert.Equal (new Rectangle (0, 0, 4, 10), pos);
  1667. }
  1668. [Fact]
  1669. [AutoInitShutdown]
  1670. public void AutoSize_True_View_IsEmpty_False_Minimum_Width_Wide_Rune ()
  1671. {
  1672. var text = "界View";
  1673. var view = new View
  1674. {
  1675. TextDirection = TextDirection.TopBottom_LeftRight,
  1676. Height = Dim.Fill () - text.Length,
  1677. Text = text,
  1678. AutoSize = true
  1679. };
  1680. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  1681. win.Add (view);
  1682. var top = new Toplevel ();
  1683. top.Add (win);
  1684. Application.Begin (top);
  1685. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  1686. Assert.Equal (5, text.Length);
  1687. Assert.True (view.AutoSize);
  1688. Assert.Equal (new (0, 0, 2, 5), view.Frame);
  1689. Assert.Equal (new (2, 5), view.TextFormatter.Size);
  1690. Assert.Equal (new List<string> { "界View" }, view.TextFormatter.GetLines ());
  1691. Assert.Equal (new (0, 0, 4, 10), win.Frame);
  1692. Assert.Equal (new (0, 0, 4, 10), Application.Top.Frame);
  1693. var expected = @"
  1694. ┌──┐
  1695. │界│
  1696. │V │
  1697. │i │
  1698. │e │
  1699. │w │
  1700. │ │
  1701. │ │
  1702. │ │
  1703. └──┘
  1704. ";
  1705. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1706. Assert.Equal (new (0, 0, 4, 10), pos);
  1707. text = "0123456789";
  1708. Assert.Equal (10, text.Length);
  1709. //view.Height = Dim.Fill () - text.Length;
  1710. Application.Refresh ();
  1711. Assert.Equal (new (0, 0, 2, 5), view.Frame);
  1712. Assert.Equal (new (2, 5), view.TextFormatter.Size);
  1713. Exception exception = Record.Exception (
  1714. () => Assert.Equal (
  1715. new List<string> { "界View" },
  1716. view.TextFormatter.GetLines ()
  1717. )
  1718. );
  1719. Assert.Null (exception);
  1720. expected = @"
  1721. ┌──┐
  1722. │界│
  1723. │V │
  1724. │i │
  1725. │e │
  1726. │w │
  1727. │ │
  1728. │ │
  1729. │ │
  1730. └──┘
  1731. ";
  1732. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1733. Assert.Equal (new Rectangle (0, 0, 4, 10), pos);
  1734. }
  1735. [Fact]
  1736. [AutoInitShutdown]
  1737. public void AutoSize_True_Width_Height_SetMinWidthHeight_Narrow_Wide_Runes ()
  1738. {
  1739. var text = $"0123456789{Environment.NewLine}01234567891";
  1740. var horizontalView = new View
  1741. {
  1742. AutoSize = true,
  1743. //Width = 11,
  1744. //Height = 2,
  1745. Text = text
  1746. };
  1747. var verticalView = new View
  1748. {
  1749. AutoSize = true,
  1750. Y = 3,
  1751. //Height = 11,
  1752. //Width = 2,
  1753. Text = text,
  1754. TextDirection = TextDirection.TopBottom_LeftRight
  1755. };
  1756. var win = new Window
  1757. {
  1758. AutoSize = true,
  1759. Width = Dim.Fill (),
  1760. Height = Dim.Fill (),
  1761. Text = "Window"
  1762. };
  1763. win.Add (horizontalView, verticalView);
  1764. var top = new Toplevel ();
  1765. top.Add (win);
  1766. RunState rs = Application.Begin (top);
  1767. ((FakeDriver)Application.Driver).SetBufferSize (20, 20);
  1768. Assert.True (horizontalView.AutoSize);
  1769. Assert.True (verticalView.AutoSize);
  1770. Assert.Equal (new Rectangle (0, 0, 11, 2), horizontalView.Frame);
  1771. Assert.Equal (new Rectangle (0, 3, 2, 11), verticalView.Frame);
  1772. var expected = @"
  1773. ┌──────────────────┐
  1774. │0123456789 │
  1775. │01234567891 │
  1776. │ │
  1777. │00 │
  1778. │11 │
  1779. │22 │
  1780. │33 │
  1781. │44 │
  1782. │55 │
  1783. │66 │
  1784. │77 │
  1785. │88 │
  1786. │99 │
  1787. │ 1 │
  1788. │ │
  1789. │ │
  1790. │ │
  1791. │ │
  1792. └──────────────────┘
  1793. ";
  1794. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1795. verticalView.Text = $"最初の行{Environment.NewLine}二行目";
  1796. Application.Top.Draw ();
  1797. Assert.Equal (new Rectangle (0, 3, 4, 4), verticalView.Frame);
  1798. expected = @"
  1799. ┌──────────────────┐
  1800. │0123456789 │
  1801. │01234567891 │
  1802. │ │
  1803. │最二 │
  1804. │初行 │
  1805. │の目 │
  1806. │行 │
  1807. │ │
  1808. │ │
  1809. │ │
  1810. │ │
  1811. │ │
  1812. │ │
  1813. │ │
  1814. │ │
  1815. │ │
  1816. │ │
  1817. │ │
  1818. └──────────────────┘
  1819. ";
  1820. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1821. Application.End (rs);
  1822. }
  1823. [Fact]
  1824. [AutoInitShutdown]
  1825. public void AutoSize_True_Width_Height_Stay_True_If_TextFormatter_Size_Fit ()
  1826. {
  1827. var text = "Finish 終";
  1828. var horizontalView = new View
  1829. {
  1830. Id = "horizontalView", AutoSize = true, Text = text
  1831. };
  1832. var verticalView = new View
  1833. {
  1834. Id = "verticalView",
  1835. Y = 3,
  1836. AutoSize = true,
  1837. Text = text,
  1838. TextDirection = TextDirection.TopBottom_LeftRight
  1839. };
  1840. var win = new Window { Id = "win", Width = Dim.Fill (), Height = Dim.Fill (), Text = "Window" };
  1841. win.Add (horizontalView, verticalView);
  1842. var top = new Toplevel ();
  1843. top.Add (win);
  1844. RunState rs = Application.Begin (top);
  1845. ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  1846. Assert.True (horizontalView.AutoSize);
  1847. Assert.True (verticalView.AutoSize);
  1848. Assert.Equal (new (text.GetColumns (), 1), horizontalView.TextFormatter.Size);
  1849. Assert.Equal (new (2, 8), verticalView.TextFormatter.Size);
  1850. //Assert.Equal (new (0, 0, 10, 1), horizontalView.Frame);
  1851. //Assert.Equal (new (0, 3, 10, 9), verticalView.Frame);
  1852. var expected = @"
  1853. ┌────────────────────┐
  1854. │Finish 終 │
  1855. │ │
  1856. │ │
  1857. │F │
  1858. │i │
  1859. │n │
  1860. │i │
  1861. │s │
  1862. │h │
  1863. │ │
  1864. │終 │
  1865. │ │
  1866. │ │
  1867. │ │
  1868. │ │
  1869. │ │
  1870. │ │
  1871. │ │
  1872. │ │
  1873. │ │
  1874. └────────────────────┘
  1875. ";
  1876. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1877. verticalView.Text = "最初の行二行目";
  1878. Application.Top.Draw ();
  1879. Assert.True (horizontalView.AutoSize);
  1880. Assert.True (verticalView.AutoSize);
  1881. // height was initialized with 8 and can only grow or keep initial value
  1882. Assert.Equal (new Rectangle (0, 3, 2, 7), verticalView.Frame);
  1883. expected = @"
  1884. ┌────────────────────┐
  1885. │Finish 終 │
  1886. │ │
  1887. │ │
  1888. │最 │
  1889. │初 │
  1890. │の │
  1891. │行 │
  1892. │二 │
  1893. │行 │
  1894. │目 │
  1895. │ │
  1896. │ │
  1897. │ │
  1898. │ │
  1899. │ │
  1900. │ │
  1901. │ │
  1902. │ │
  1903. │ │
  1904. │ │
  1905. └────────────────────┘
  1906. ";
  1907. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  1908. Application.End (rs);
  1909. }
  1910. // Moved from DimTests.cs - This is really a bogus test
  1911. [Theory]
  1912. [AutoInitShutdown]
  1913. [InlineData (0, true)]
  1914. [InlineData (0, false)]
  1915. [InlineData (50, true)]
  1916. [InlineData (50, false)]
  1917. public void DimPercentPlusOne (int startingDistance, bool testHorizontal)
  1918. {
  1919. var container = new View { Width = 100, Height = 100 };
  1920. var label = new Label
  1921. {
  1922. X = testHorizontal ? startingDistance : 0, Y = testHorizontal ? 0 : startingDistance
  1923. //Width = testHorizontal ? Dim.Percent (50) + 1 : 1,
  1924. //Height = testHorizontal ? 1 : Dim.Percent (50) + 1
  1925. };
  1926. container.Add (label);
  1927. var top = new Toplevel ();
  1928. top.Add (container);
  1929. top.BeginInit ();
  1930. top.EndInit ();
  1931. top.LayoutSubviews ();
  1932. Assert.Equal (100, container.Frame.Width);
  1933. Assert.Equal (100, container.Frame.Height);
  1934. if (testHorizontal)
  1935. {
  1936. Assert.Equal (0, label.Frame.Width); // BUGBUG: Shoudl be 0 not since there's no text
  1937. Assert.Equal (0, label.Frame.Height);
  1938. }
  1939. else
  1940. {
  1941. Assert.Equal (0, label.Frame.Width); // BUGBUG: Shoudl be 0 not since there's no text
  1942. Assert.Equal (0, label.Frame.Height);
  1943. }
  1944. }
  1945. [Fact]
  1946. [AutoInitShutdown]
  1947. public void Excess_Text_Is_Erased_When_The_Width_Is_Reduced ()
  1948. {
  1949. var lbl = new Label { Text = "123" };
  1950. var top = new Toplevel ();
  1951. top.Add (lbl);
  1952. RunState rs = Application.Begin (top);
  1953. Assert.True (lbl.AutoSize);
  1954. Assert.Equal ("123 ", GetContents ());
  1955. lbl.Text = "12";
  1956. Assert.Equal (new Rectangle (0, 0, 2, 1), lbl.Frame);
  1957. Assert.Equal (new Rectangle (0, 0, 3, 1), lbl._needsDisplayRect);
  1958. Assert.Equal (new Rectangle (0, 0, 0, 0), lbl.SuperView._needsDisplayRect);
  1959. Assert.True (lbl.SuperView.LayoutNeeded);
  1960. lbl.SuperView.Draw ();
  1961. Assert.Equal ("12 ", GetContents ());
  1962. string GetContents ()
  1963. {
  1964. var text = "";
  1965. for (var i = 0; i < 4; i++)
  1966. {
  1967. text += Application.Driver.Contents [0, i].Rune;
  1968. }
  1969. return text;
  1970. }
  1971. Application.End (rs);
  1972. }
  1973. //[Fact]
  1974. //public void GetCurrentHeight_TrySetHeight ()
  1975. //{
  1976. // var top = new View { X = 0, Y = 0, Height = 20 };
  1977. // var v = new View { Height = Dim.Fill (), ValidatePosDim = true };
  1978. // top.Add (v);
  1979. // top.BeginInit ();
  1980. // top.EndInit ();
  1981. // top.LayoutSubviews ();
  1982. // Assert.False (v.AutoSize);
  1983. // Assert.False (v.TrySetHeight (0, out _));
  1984. // Assert.Equal (20, v.Frame.Height);
  1985. // v.Height = Dim.Fill (1);
  1986. // top.LayoutSubviews ();
  1987. // Assert.False (v.TrySetHeight (0, out _));
  1988. // Assert.True (v.Height is Dim.DimFill);
  1989. // Assert.Equal (19, v.Frame.Height);
  1990. // v.AutoSize = true;
  1991. // top.LayoutSubviews ();
  1992. // Assert.True (v.TrySetHeight (0, out _));
  1993. // Assert.True (v.Height is Dim.DimAbsolute);
  1994. // Assert.Equal (0, v.Frame.Height); // No text, so height is 0
  1995. // top.Dispose ();
  1996. //}
  1997. //[Fact]
  1998. //[TestRespondersDisposed]
  1999. //public void GetCurrentWidth_TrySetWidth ()
  2000. //{
  2001. // var top = new View { X = 0, Y = 0, Width = 80 };
  2002. // var v = new View { Width = Dim.Fill (), ValidatePosDim = true };
  2003. // top.Add (v);
  2004. // top.BeginInit ();
  2005. // top.EndInit ();
  2006. // top.LayoutSubviews ();
  2007. // Assert.False (v.AutoSize);
  2008. // Assert.False (v.TrySetWidth (0, out _));
  2009. // Assert.True (v.Width is Dim.DimFill);
  2010. // Assert.Equal (80, v.Frame.Width);
  2011. // v.Width = Dim.Fill (1);
  2012. // top.LayoutSubviews ();
  2013. // Assert.False (v.TrySetWidth (0, out _));
  2014. // Assert.True (v.Width is Dim.DimFill);
  2015. // Assert.Equal (79, v.Frame.Width);
  2016. // v.AutoSize = true;
  2017. // top.LayoutSubviews ();
  2018. // Assert.True (v.TrySetWidth (0, out _));
  2019. // Assert.True (v.Width is Dim.DimAbsolute);
  2020. // Assert.Equal (0, v.Frame.Width); // No text, so width is 0
  2021. // top.Dispose ();
  2022. //}
  2023. // [Fact]
  2024. // [AutoInitShutdown]
  2025. // public void AutoSize_False_TextDirection_Toggle ()
  2026. // {
  2027. // var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  2028. // // View is AutoSize == true
  2029. // var view = new View ();
  2030. // win.Add (view);
  2031. // var top = new Toplevel ();
  2032. // top.Add (win);
  2033. // var rs = Application.Begin (top);
  2034. // ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  2035. // Assert.Equal (new (0, 0, 22, 22), win.Frame);
  2036. // Assert.Equal (new (0, 0, 22, 22), win.Margin.Frame);
  2037. // Assert.Equal (new (0, 0, 22, 22), win.Border.Frame);
  2038. // Assert.Equal (new (1, 1, 20, 20), win.Padding.Frame);
  2039. // Assert.False (view.AutoSize);
  2040. // Assert.Equal (TextDirection.LeftRight_TopBottom, view.TextDirection);
  2041. // Assert.Equal (Rectangle.Empty, view.Frame);
  2042. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2043. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2044. // Assert.Equal ("Absolute(0)", view.Width.ToString ());
  2045. // Assert.Equal ("Absolute(0)", view.Height.ToString ());
  2046. // var expected = @"
  2047. //┌────────────────────┐
  2048. //│ │
  2049. //│ │
  2050. //│ │
  2051. //│ │
  2052. //│ │
  2053. //│ │
  2054. //│ │
  2055. //│ │
  2056. //│ │
  2057. //│ │
  2058. //│ │
  2059. //│ │
  2060. //│ │
  2061. //│ │
  2062. //│ │
  2063. //│ │
  2064. //│ │
  2065. //│ │
  2066. //│ │
  2067. //│ │
  2068. //└────────────────────┘";
  2069. // var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2070. // Assert.Equal (new (0, 0, 22, 22), pos);
  2071. // view.Text = "Hello World";
  2072. // view.Width = 11;
  2073. // view.Height = 1;
  2074. // win.LayoutSubviews ();
  2075. // Application.Refresh ();
  2076. // Assert.Equal (new (0, 0, 11, 1), view.Frame);
  2077. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2078. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2079. // Assert.Equal ("Absolute(11)", view.Width.ToString ());
  2080. // Assert.Equal ("Absolute(1)", view.Height.ToString ());
  2081. // expected = @"
  2082. //┌────────────────────┐
  2083. //│Hello World │
  2084. //│ │
  2085. //│ │
  2086. //│ │
  2087. //│ │
  2088. //│ │
  2089. //│ │
  2090. //│ │
  2091. //│ │
  2092. //│ │
  2093. //│ │
  2094. //│ │
  2095. //│ │
  2096. //│ │
  2097. //│ │
  2098. //│ │
  2099. //│ │
  2100. //│ │
  2101. //│ │
  2102. //│ │
  2103. //└────────────────────┘";
  2104. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2105. // Assert.Equal (new (0, 0, 22, 22), pos);
  2106. // view.AutoSize = true;
  2107. // view.Text = "Hello Worlds";
  2108. // Application.Refresh ();
  2109. // Assert.Equal (new (0, 0, 12, 1), view.Frame);
  2110. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2111. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2112. // Assert.Equal ("Absolute(11)", view.Width.ToString ());
  2113. // Assert.Equal ("Absolute(1)", view.Height.ToString ());
  2114. // expected = @"
  2115. //┌────────────────────┐
  2116. //│Hello Worlds │
  2117. //│ │
  2118. //│ │
  2119. //│ │
  2120. //│ │
  2121. //│ │
  2122. //│ │
  2123. //│ │
  2124. //│ │
  2125. //│ │
  2126. //│ │
  2127. //│ │
  2128. //│ │
  2129. //│ │
  2130. //│ │
  2131. //│ │
  2132. //│ │
  2133. //│ │
  2134. //│ │
  2135. //│ │
  2136. //└────────────────────┘";
  2137. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2138. // Assert.Equal (new (0, 0, 22, 22), pos);
  2139. // view.TextDirection = TextDirection.TopBottom_LeftRight;
  2140. // Application.Refresh ();
  2141. // Assert.Equal (new (0, 0, 11, 12), view.Frame);
  2142. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2143. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2144. // Assert.Equal ("Absolute(11)", view.Width.ToString ());
  2145. // Assert.Equal ("Absolute(1)", view.Height.ToString ());
  2146. // expected = @"
  2147. //┌────────────────────┐
  2148. //│H │
  2149. //│e │
  2150. //│l │
  2151. //│l │
  2152. //│o │
  2153. //│ │
  2154. //│W │
  2155. //│o │
  2156. //│r │
  2157. //│l │
  2158. //│d │
  2159. //│s │
  2160. //│ │
  2161. //│ │
  2162. //│ │
  2163. //│ │
  2164. //│ │
  2165. //│ │
  2166. //│ │
  2167. //│ │
  2168. //└────────────────────┘";
  2169. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2170. // Assert.Equal (new (0, 0, 22, 22), pos);
  2171. // view.AutoSize = false;
  2172. // view.Height = 1;
  2173. // Application.Refresh ();
  2174. // Assert.Equal (new (0, 0, 11, 1), view.Frame);
  2175. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2176. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2177. // Assert.Equal ("Absolute(11)", view.Width.ToString ());
  2178. // Assert.Equal ("Absolute(1)", view.Height.ToString ());
  2179. // expected = @"
  2180. //┌────────────────────┐
  2181. //│HelloWorlds │
  2182. //│ │
  2183. //│ │
  2184. //│ │
  2185. //│ │
  2186. //│ │
  2187. //│ │
  2188. //│ │
  2189. //│ │
  2190. //│ │
  2191. //│ │
  2192. //│ │
  2193. //│ │
  2194. //│ │
  2195. //│ │
  2196. //│ │
  2197. //│ │
  2198. //│ │
  2199. //│ │
  2200. //│ │
  2201. //└────────────────────┘";
  2202. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2203. // Assert.Equal (new (0, 0, 22, 22), pos);
  2204. // view.PreserveTrailingSpaces = true;
  2205. // Application.Refresh ();
  2206. // Assert.Equal (new (0, 0, 11, 1), view.Frame);
  2207. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2208. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2209. // Assert.Equal ("Absolute(11)", view.Width.ToString ());
  2210. // Assert.Equal ("Absolute(1)", view.Height.ToString ());
  2211. // expected = @"
  2212. //┌────────────────────┐
  2213. //│Hello World │
  2214. //│ │
  2215. //│ │
  2216. //│ │
  2217. //│ │
  2218. //│ │
  2219. //│ │
  2220. //│ │
  2221. //│ │
  2222. //│ │
  2223. //│ │
  2224. //│ │
  2225. //│ │
  2226. //│ │
  2227. //│ │
  2228. //│ │
  2229. //│ │
  2230. //│ │
  2231. //│ │
  2232. //│ │
  2233. //└────────────────────┘";
  2234. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2235. // Assert.Equal (new (0, 0, 22, 22), pos);
  2236. // view.PreserveTrailingSpaces = false;
  2237. // var f = view.Frame;
  2238. // view.Width = f.Height;
  2239. // view.Height = f.Width;
  2240. // view.TextDirection = TextDirection.TopBottom_LeftRight;
  2241. // Application.Refresh ();
  2242. // Assert.Equal (new (0, 0, 1, 11), view.Frame);
  2243. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2244. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2245. // Assert.Equal ("Absolute(1)", view.Width.ToString ());
  2246. // Assert.Equal ("Absolute(11)", view.Height.ToString ());
  2247. // expected = @"
  2248. //┌────────────────────┐
  2249. //│H │
  2250. //│e │
  2251. //│l │
  2252. //│l │
  2253. //│o │
  2254. //│ │
  2255. //│W │
  2256. //│o │
  2257. //│r │
  2258. //│l │
  2259. //│d │
  2260. //│ │
  2261. //│ │
  2262. //│ │
  2263. //│ │
  2264. //│ │
  2265. //│ │
  2266. //│ │
  2267. //│ │
  2268. //│ │
  2269. //└────────────────────┘";
  2270. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2271. // Assert.Equal (new (0, 0, 22, 22), pos);
  2272. // view.AutoSize = true;
  2273. // Application.Refresh ();
  2274. // Assert.Equal (new (0, 0, 1, 12), view.Frame);
  2275. // Assert.Equal ("Absolute(0)", view.X.ToString ());
  2276. // Assert.Equal ("Absolute(0)", view.Y.ToString ());
  2277. // Assert.Equal ("Absolute(1)", view.Width.ToString ());
  2278. // Assert.Equal ("Absolute(12)", view.Height.ToString ());
  2279. // expected = @"
  2280. //┌────────────────────┐
  2281. //│H │
  2282. //│e │
  2283. //│l │
  2284. //│l │
  2285. //│o │
  2286. //│ │
  2287. //│W │
  2288. //│o │
  2289. //│r │
  2290. //│l │
  2291. //│d │
  2292. //│s │
  2293. //│ │
  2294. //│ │
  2295. //│ │
  2296. //│ │
  2297. //│ │
  2298. //│ │
  2299. //│ │
  2300. //│ │
  2301. //└────────────────────┘";
  2302. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2303. // Assert.Equal (new (0, 0, 22, 22), pos);
  2304. // Application.End (rs);
  2305. // }
  2306. [Fact]
  2307. [AutoInitShutdown]
  2308. public void GetTextFormatterBoundsSize_GetSizeNeededForText_HotKeySpecifier ()
  2309. {
  2310. var text = "Say Hello 你";
  2311. // Frame: 0, 0, 12, 1
  2312. var horizontalView = new View { AutoSize = true };
  2313. horizontalView.TextFormatter.HotKeySpecifier = (Rune)'_';
  2314. horizontalView.Text = text;
  2315. // Frame: 0, 0, 1, 12
  2316. var verticalView = new View
  2317. {
  2318. AutoSize = true, TextDirection = TextDirection.TopBottom_LeftRight
  2319. };
  2320. verticalView.Text = text;
  2321. verticalView.TextFormatter.HotKeySpecifier = (Rune)'_';
  2322. var top = new Toplevel ();
  2323. top.Add (horizontalView, verticalView);
  2324. Application.Begin (top);
  2325. ((FakeDriver)Application.Driver).SetBufferSize (50, 50);
  2326. Assert.True (horizontalView.AutoSize);
  2327. Assert.Equal (new (0, 0, 12, 1), horizontalView.Frame);
  2328. Assert.Equal (new (12, 1), horizontalView.GetSizeNeededForTextWithoutHotKey ());
  2329. Assert.Equal (horizontalView.Frame.Size, horizontalView.GetSizeNeededForTextWithoutHotKey ());
  2330. Assert.True (verticalView.AutoSize);
  2331. Assert.Equal (new (0, 0, 2, 11), verticalView.Frame);
  2332. Assert.Equal (new (2, 11), verticalView.GetSizeNeededForTextWithoutHotKey ());
  2333. Assert.Equal (verticalView.Frame.Size, verticalView.GetSizeNeededForTextWithoutHotKey ());
  2334. text = "012345678你";
  2335. horizontalView.Text = text;
  2336. verticalView.Text = text;
  2337. Assert.True (horizontalView.AutoSize);
  2338. Assert.Equal (new (0, 0, 11, 1), horizontalView.Frame);
  2339. Assert.Equal (new (11, 1), horizontalView.GetSizeNeededForTextWithoutHotKey ());
  2340. Assert.Equal (horizontalView.Frame.Size, horizontalView.GetSizeNeededForTextWithoutHotKey ());
  2341. Assert.True (verticalView.AutoSize);
  2342. Assert.Equal (new (0, 0, 2, 10), verticalView.Frame);
  2343. Assert.Equal (new (2, 10), verticalView.GetSizeNeededForTextWithoutHotKey ());
  2344. Assert.Equal (verticalView.Frame.Size, verticalView.GetSizeNeededForTextWithoutHotKey ());
  2345. }
  2346. [Fact]
  2347. public void SetRelativeLayout_Respects_AutoSize ()
  2348. {
  2349. var view = new View { Frame = new (0, 0, 10, 0), AutoSize = true };
  2350. view.Text = "01234567890123456789";
  2351. Assert.True (view.AutoSize);
  2352. Assert.Equal (new (0, 0, 20, 1), view.Frame);
  2353. view.SetRelativeLayout (new (25, 5));
  2354. Assert.Equal (new (0, 0, 20, 1), view.Frame);
  2355. }
  2356. //[Fact]
  2357. //[AutoInitShutdown]
  2358. //public void TrySetHeight_ForceValidatePosDim ()
  2359. //{
  2360. // var top = new View { X = 0, Y = 0, Height = 20 };
  2361. // var v = new View { Height = Dim.Fill (), ValidatePosDim = true };
  2362. // top.Add (v);
  2363. // Assert.False (v.TrySetHeight (10, out int rHeight));
  2364. // Assert.Equal (10, rHeight);
  2365. // v.Height = Dim.Fill (1);
  2366. // Assert.False (v.TrySetHeight (10, out rHeight));
  2367. // Assert.Equal (9, rHeight);
  2368. // v.Height = 0;
  2369. // Assert.True (v.TrySetHeight (10, out rHeight));
  2370. // Assert.Equal (10, rHeight);
  2371. // Assert.False (v.IsInitialized);
  2372. // var toplevel = new Toplevel ();
  2373. // toplevel.Add (top);
  2374. // Application.Begin (toplevel);
  2375. // Assert.True (v.IsInitialized);
  2376. // v.Height = 15;
  2377. // Assert.True (v.TrySetHeight (5, out rHeight));
  2378. // Assert.Equal (5, rHeight);
  2379. //}
  2380. //[Fact]
  2381. //[AutoInitShutdown]
  2382. //public void TrySetWidth_ForceValidatePosDim ()
  2383. //{
  2384. // var top = new View { X = 0, Y = 0, Width = 80 };
  2385. // var v = new View { Width = Dim.Fill (), ValidatePosDim = true };
  2386. // top.Add (v);
  2387. // Assert.False (v.TrySetWidth (70, out int rWidth));
  2388. // Assert.Equal (70, rWidth);
  2389. // v.Width = Dim.Fill (1);
  2390. // Assert.False (v.TrySetWidth (70, out rWidth));
  2391. // Assert.Equal (69, rWidth);
  2392. // v.Width = 0;
  2393. // Assert.True (v.TrySetWidth (70, out rWidth));
  2394. // Assert.Equal (70, rWidth);
  2395. // Assert.False (v.IsInitialized);
  2396. // var toplevel = new Toplevel ();
  2397. // toplevel.Add (top);
  2398. // Application.Begin (toplevel);
  2399. // Assert.True (v.IsInitialized);
  2400. // v.Width = 75;
  2401. // Assert.True (v.TrySetWidth (60, out rWidth));
  2402. // Assert.Equal (60, rWidth);
  2403. //}
  2404. [Theory]
  2405. [AutoInitShutdown]
  2406. [InlineData (true)]
  2407. [InlineData (false)]
  2408. public void View_Draw_Horizontal_Simple_TextAlignments (bool autoSize)
  2409. {
  2410. var text = "Hello World";
  2411. var width = 20;
  2412. var lblLeft = new View { Text = text, Width = width, Height = 1, AutoSize = autoSize };
  2413. var lblCenter = new View
  2414. {
  2415. Text = text,
  2416. Y = 1,
  2417. Width = width,
  2418. Height = 1,
  2419. TextAlignment = TextAlignment.Centered,
  2420. AutoSize = autoSize
  2421. };
  2422. var lblRight = new View
  2423. {
  2424. Text = text,
  2425. Y = 2,
  2426. Width = width,
  2427. Height = 1,
  2428. TextAlignment = TextAlignment.Right,
  2429. AutoSize = autoSize
  2430. };
  2431. var lblJust = new View
  2432. {
  2433. Text = text,
  2434. Y = 3,
  2435. Width = width,
  2436. Height = 1,
  2437. TextAlignment = TextAlignment.Justified,
  2438. AutoSize = autoSize
  2439. };
  2440. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  2441. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2442. var top = new Toplevel ();
  2443. top.Add (frame);
  2444. Application.Begin (top);
  2445. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 6);
  2446. Assert.True (lblLeft.AutoSize == autoSize);
  2447. Assert.True (lblCenter.AutoSize == autoSize);
  2448. Assert.True (lblRight.AutoSize == autoSize);
  2449. Assert.True (lblJust.AutoSize == autoSize);
  2450. Assert.True (lblLeft.TextFormatter.AutoSize == autoSize);
  2451. Assert.True (lblCenter.TextFormatter.AutoSize == autoSize);
  2452. Assert.True (lblRight.TextFormatter.AutoSize == autoSize);
  2453. Assert.True (lblJust.TextFormatter.AutoSize == autoSize);
  2454. if (autoSize)
  2455. {
  2456. Size expectedSize = new (11, 1);
  2457. Assert.Equal (expectedSize, lblLeft.TextFormatter.Size);
  2458. Assert.Equal (expectedSize, lblCenter.TextFormatter.Size);
  2459. Assert.Equal (expectedSize, lblRight.TextFormatter.Size);
  2460. Assert.Equal (expectedSize, lblJust.TextFormatter.Size);
  2461. }
  2462. else
  2463. {
  2464. Size expectedSize = new (width, 1);
  2465. Assert.Equal (expectedSize, lblLeft.TextFormatter.Size);
  2466. Assert.Equal (expectedSize, lblCenter.TextFormatter.Size);
  2467. Assert.Equal (expectedSize, lblRight.TextFormatter.Size);
  2468. Assert.Equal (expectedSize, lblJust.TextFormatter.Size);
  2469. }
  2470. Assert.Equal (new (0, 0, width + 2, 6), frame.Frame);
  2471. string expected;
  2472. if (autoSize)
  2473. {
  2474. expected = @"
  2475. ┌────────────────────┐
  2476. │Hello World │
  2477. │Hello World │
  2478. │Hello World │
  2479. │Hello World │
  2480. └────────────────────┘
  2481. ";
  2482. }
  2483. else
  2484. {
  2485. expected = @"
  2486. ┌────────────────────┐
  2487. │Hello World │
  2488. │ Hello World │
  2489. │ Hello World│
  2490. │Hello World│
  2491. └────────────────────┘
  2492. ";
  2493. }
  2494. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2495. Assert.Equal (new (0, 0, width + 2, 6), pos);
  2496. }
  2497. [Theory]
  2498. [AutoInitShutdown]
  2499. [InlineData (true)]
  2500. [InlineData (false)]
  2501. public void View_Draw_Vertical_Simple_TextAlignments (bool autoSize)
  2502. {
  2503. var text = "Hello World";
  2504. var height = 20;
  2505. var lblLeft = new View
  2506. {
  2507. Text = text,
  2508. Width = 1,
  2509. Height = height,
  2510. TextDirection = TextDirection.TopBottom_LeftRight,
  2511. AutoSize = autoSize
  2512. };
  2513. var lblCenter = new View
  2514. {
  2515. Text = text,
  2516. X = 2,
  2517. Width = 1,
  2518. Height = height,
  2519. TextDirection = TextDirection.TopBottom_LeftRight,
  2520. AutoSize = autoSize,
  2521. VerticalTextAlignment = VerticalTextAlignment.Middle
  2522. };
  2523. var lblRight = new View
  2524. {
  2525. Text = text,
  2526. X = 4,
  2527. Width = 1,
  2528. Height = height,
  2529. TextDirection = TextDirection.TopBottom_LeftRight,
  2530. AutoSize = autoSize,
  2531. VerticalTextAlignment = VerticalTextAlignment.Bottom
  2532. };
  2533. var lblJust = new View
  2534. {
  2535. Text = text,
  2536. X = 6,
  2537. Width = 1,
  2538. Height = height,
  2539. TextDirection = TextDirection.TopBottom_LeftRight,
  2540. AutoSize = autoSize,
  2541. VerticalTextAlignment = VerticalTextAlignment.Justified
  2542. };
  2543. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  2544. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  2545. var top = new Toplevel ();
  2546. top.Add (frame);
  2547. Application.Begin (top);
  2548. ((FakeDriver)Application.Driver).SetBufferSize (9, height + 2);
  2549. Assert.True (lblLeft.AutoSize == autoSize);
  2550. Assert.True (lblCenter.AutoSize == autoSize);
  2551. Assert.True (lblRight.AutoSize == autoSize);
  2552. Assert.True (lblJust.AutoSize == autoSize);
  2553. Assert.True (lblLeft.TextFormatter.AutoSize == autoSize);
  2554. Assert.True (lblCenter.TextFormatter.AutoSize == autoSize);
  2555. Assert.True (lblRight.TextFormatter.AutoSize == autoSize);
  2556. Assert.True (lblJust.TextFormatter.AutoSize == autoSize);
  2557. if (autoSize)
  2558. {
  2559. Assert.Equal (new (1, 11), lblLeft.TextFormatter.Size);
  2560. Assert.Equal (new (1, 11), lblCenter.TextFormatter.Size);
  2561. Assert.Equal (new (1, 11), lblRight.TextFormatter.Size);
  2562. Assert.Equal (new (1, 11), lblJust.TextFormatter.Size);
  2563. Assert.Equal (new (0, 0, 9, height + 2), frame.Frame);
  2564. }
  2565. else
  2566. {
  2567. Assert.Equal (new (1, height), lblLeft.TextFormatter.Size);
  2568. Assert.Equal (new (1, height), lblCenter.TextFormatter.Size);
  2569. Assert.Equal (new (1, height), lblRight.TextFormatter.Size);
  2570. Assert.Equal (new (1, height), lblJust.TextFormatter.Size);
  2571. Assert.Equal (new (0, 0, 9, height + 2), frame.Frame);
  2572. }
  2573. string expected;
  2574. if (autoSize)
  2575. {
  2576. expected = @"
  2577. ┌───────┐
  2578. │H H H H│
  2579. │e e e e│
  2580. │l l l l│
  2581. │l l l l│
  2582. │o o o o│
  2583. │ │
  2584. │W W W W│
  2585. │o o o o│
  2586. │r r r r│
  2587. │l l l l│
  2588. │d d d d│
  2589. │ │
  2590. │ │
  2591. │ │
  2592. │ │
  2593. │ │
  2594. │ │
  2595. │ │
  2596. │ │
  2597. │ │
  2598. └───────┘
  2599. ";
  2600. }
  2601. else
  2602. {
  2603. expected = @"
  2604. ┌───────┐
  2605. │H H│
  2606. │e e│
  2607. │l l│
  2608. │l l│
  2609. │o H o│
  2610. │ e │
  2611. │W l │
  2612. │o l │
  2613. │r o │
  2614. │l H │
  2615. │d W e │
  2616. │ o l │
  2617. │ r l │
  2618. │ l o │
  2619. │ d │
  2620. │ W W│
  2621. │ o o│
  2622. │ r r│
  2623. │ l l│
  2624. │ d d│
  2625. └───────┘
  2626. ";
  2627. }
  2628. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  2629. Assert.Equal (new (0, 0, 9, height + 2), pos);
  2630. }
  2631. }