AutoSizeTrueTests.cs 106 KB

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