AutoSizeTextTests.cs 79 KB

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