AutoSizeTrueTests.cs 102 KB

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