AutoSizeTests.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. using System.Text;
  2. using System;
  3. using System.Collections.Generic;
  4. using Xunit;
  5. using Xunit.Abstractions;
  6. namespace Terminal.Gui.ViewTests {
  7. public class AutoSizeTests {
  8. readonly ITestOutputHelper output;
  9. public AutoSizeTests (ITestOutputHelper output)
  10. {
  11. this.output = output;
  12. }
  13. [Fact, AutoInitShutdown]
  14. public void AutoSize_GetAutoSize_Horizontal ()
  15. {
  16. var text = "text";
  17. var view = new View () {
  18. Text = text,
  19. AutoSize = true
  20. };
  21. var win = new Window () {
  22. Width = Dim.Fill (),
  23. Height = Dim.Fill ()
  24. };
  25. win.Add (view);
  26. Application.Top.Add (win);
  27. Application.Begin (Application.Top);
  28. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  29. var size = view.GetAutoSize ();
  30. Assert.Equal (new Size (text.Length, 1), size);
  31. view.Text = $"{text}\n{text}";
  32. size = view.GetAutoSize ();
  33. Assert.Equal (new Size (text.Length, 2), size);
  34. view.Text = $"{text}\n{text}\n{text}+";
  35. size = view.GetAutoSize ();
  36. Assert.Equal (new Size (text.Length + 1, 3), size);
  37. text = string.Empty;
  38. view.Text = text;
  39. size = view.GetAutoSize ();
  40. Assert.Equal (new Size (0, 0), size);
  41. text = "1";
  42. view.Text = text;
  43. size = view.GetAutoSize ();
  44. Assert.Equal (new Size (1, 1), size);
  45. text = "界";
  46. view.Text = text;
  47. size = view.GetAutoSize ();
  48. Assert.Equal (new Size (2, 1), size);
  49. }
  50. [Fact, AutoInitShutdown]
  51. public void AutoSize_GetAutoSize_Vertical()
  52. {
  53. var text = "text";
  54. var view = new View () {
  55. Text = text,
  56. TextDirection = TextDirection.TopBottom_LeftRight,
  57. AutoSize = true
  58. };
  59. var win = new Window () {
  60. Width = Dim.Fill (),
  61. Height = Dim.Fill ()
  62. };
  63. win.Add (view);
  64. Application.Top.Add (win);
  65. Application.Begin (Application.Top);
  66. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  67. var size = view.GetAutoSize ();
  68. Assert.Equal (new Size (1, text.Length), size);
  69. view.Text = $"{text}\n{text}";
  70. size = view.GetAutoSize ();
  71. Assert.Equal (new Size (2, text.Length), size);
  72. view.Text = $"{text}\n{text}\n{text}+";
  73. size = view.GetAutoSize ();
  74. Assert.Equal (new Size (3, text.Length + 1), size);
  75. text = string.Empty;
  76. view.Text = text;
  77. size = view.GetAutoSize ();
  78. Assert.Equal (new Size (0, 0), size);
  79. text = "1";
  80. view.Text = text;
  81. size = view.GetAutoSize ();
  82. Assert.Equal (new Size (1, 1), size);
  83. text = "界";
  84. view.Text = text;
  85. size = view.GetAutoSize ();
  86. Assert.Equal (new Size (2, 1), size);
  87. }
  88. [Fact, AutoInitShutdown]
  89. public void AutoSize_GetAutoSize_Left()
  90. {
  91. var text = "This is some text.";
  92. var view = new View () {
  93. Text = text,
  94. TextAlignment = TextAlignment.Left,
  95. AutoSize = true
  96. };
  97. var win = new Window () {
  98. Width = Dim.Fill (),
  99. Height = Dim.Fill ()
  100. };
  101. win.Add (view);
  102. Application.Top.Add (win);
  103. Application.Begin (Application.Top);
  104. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  105. var size = view.GetAutoSize ();
  106. Assert.Equal (new Size (text.Length, 1), size);
  107. view.Text = $"{text}\n{text}";
  108. size = view.GetAutoSize ();
  109. Assert.Equal (new Size (text.Length, 2), size);
  110. view.Text = $"{text}\n{text}\n{text}+";
  111. size = view.GetAutoSize ();
  112. Assert.Equal (new Size (text.Length + 1, 3), size);
  113. text = string.Empty;
  114. view.Text = text;
  115. size = view.GetAutoSize ();
  116. Assert.Equal (new Size (0, 0), size);
  117. text = "1";
  118. view.Text = text;
  119. size = view.GetAutoSize ();
  120. Assert.Equal (new Size (1, 1), size);
  121. text = "界";
  122. view.Text = text;
  123. size = view.GetAutoSize ();
  124. Assert.Equal (new Size (2, 1), size);
  125. }
  126. [Fact, AutoInitShutdown]
  127. public void AutoSize_GetAutoSize_Right ()
  128. {
  129. var text = "This is some text.";
  130. var view = new View () {
  131. Text = text,
  132. TextAlignment = TextAlignment.Right,
  133. AutoSize = true
  134. };
  135. var win = new Window () {
  136. Width = Dim.Fill (),
  137. Height = Dim.Fill ()
  138. };
  139. win.Add (view);
  140. Application.Top.Add (win);
  141. Application.Begin (Application.Top);
  142. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  143. var size = view.GetAutoSize ();
  144. Assert.Equal (new Size (text.Length, 1), size);
  145. view.Text = $"{text}\n{text}";
  146. size = view.GetAutoSize ();
  147. Assert.Equal (new Size (text.Length, 2), size);
  148. view.Text = $"{text}\n{text}\n{text}+";
  149. size = view.GetAutoSize ();
  150. Assert.Equal (new Size (text.Length + 1, 3), size);
  151. text = string.Empty;
  152. view.Text = text;
  153. size = view.GetAutoSize ();
  154. Assert.Equal (new Size (0, 0), size);
  155. text = "1";
  156. view.Text = text;
  157. size = view.GetAutoSize ();
  158. Assert.Equal (new Size (1, 1), size);
  159. text = "界";
  160. view.Text = text;
  161. size = view.GetAutoSize ();
  162. Assert.Equal (new Size (2, 1), size);
  163. }
  164. [Fact, AutoInitShutdown]
  165. public void AutoSize_GetAutoSize_Centered ()
  166. {
  167. var text = "This is some text.";
  168. var view = new View () {
  169. Text = text,
  170. TextAlignment = TextAlignment.Centered,
  171. AutoSize = true
  172. };
  173. var win = new Window () {
  174. Width = Dim.Fill (),
  175. Height = Dim.Fill ()
  176. };
  177. win.Add (view);
  178. Application.Top.Add (win);
  179. Application.Begin (Application.Top);
  180. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  181. var size = view.GetAutoSize ();
  182. Assert.Equal (new Size (text.Length, 1), size);
  183. view.Text = $"{text}\n{text}";
  184. size = view.GetAutoSize ();
  185. Assert.Equal (new Size (text.Length, 2), size);
  186. view.Text = $"{text}\n{text}\n{text}+";
  187. size = view.GetAutoSize ();
  188. Assert.Equal (new Size (text.Length + 1, 3), size);
  189. text = string.Empty;
  190. view.Text = text;
  191. size = view.GetAutoSize ();
  192. Assert.Equal (new Size (0, 0), size);
  193. text = "1";
  194. view.Text = text;
  195. size = view.GetAutoSize ();
  196. Assert.Equal (new Size (1, 1), size);
  197. text = "界";
  198. view.Text = text;
  199. size = view.GetAutoSize ();
  200. Assert.Equal (new Size (2, 1), size);
  201. }
  202. [Fact, AutoInitShutdown]
  203. public void AutoSize_False_View_IsEmpty_False_Return_Null_Lines ()
  204. {
  205. var text = "Views";
  206. var view = new View () {
  207. Width = Dim.Fill () - text.Length,
  208. Height = 1,
  209. Text = text
  210. };
  211. var win = new Window () {
  212. Width = Dim.Fill (),
  213. Height = Dim.Fill ()
  214. };
  215. win.Add (view);
  216. Application.Top.Add (win);
  217. Application.Begin (Application.Top);
  218. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  219. Assert.Equal (5, text.Length);
  220. Assert.False (view.AutoSize);
  221. Assert.Equal (new Rect (0, 0, 3, 1), view.Frame);
  222. Assert.Equal (new Size (3, 1), view.TextFormatter.Size);
  223. Assert.Equal (new List<string> () { "Vie" }, view.TextFormatter.Lines);
  224. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  225. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  226. var expected = @"
  227. ┌────────┐
  228. │Vie │
  229. │ │
  230. └────────┘
  231. ";
  232. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  233. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  234. text = "0123456789";
  235. Assert.Equal (10, text.Length);
  236. view.Width = Dim.Fill () - text.Length;
  237. Application.Refresh ();
  238. Assert.Equal (new Rect (0, 0, 0, 1), view.Frame);
  239. Assert.Equal (new Size (0, 1), view.TextFormatter.Size);
  240. Assert.Equal (new List<string> () { string.Empty }, view.TextFormatter.Lines);
  241. expected = @"
  242. ┌────────┐
  243. │ │
  244. │ │
  245. └────────┘
  246. ";
  247. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  248. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  249. }
  250. [Fact, AutoInitShutdown]
  251. public void AutoSize_False_View_IsEmpty_True_Minimum_Height ()
  252. {
  253. var text = "Views";
  254. var view = new View () {
  255. Width = Dim.Fill () - text.Length,
  256. Text = text
  257. };
  258. var win = new Window () {
  259. Width = Dim.Fill (),
  260. Height = Dim.Fill ()
  261. };
  262. win.Add (view);
  263. Application.Top.Add (win);
  264. Application.Begin (Application.Top);
  265. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  266. Assert.Equal (5, text.Length);
  267. Assert.False (view.AutoSize);
  268. Assert.Equal (new Rect (0, 0, 3, 1), view.Frame);
  269. Assert.Equal (new Size (3, 1), view.TextFormatter.Size);
  270. Assert.Single (view.TextFormatter.Lines);
  271. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  272. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  273. var expected = @"
  274. ┌────────┐
  275. │Vie │
  276. │ │
  277. └────────┘
  278. ";
  279. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  280. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  281. text = "0123456789";
  282. Assert.Equal (10, text.Length);
  283. view.Width = Dim.Fill () - text.Length;
  284. Application.Refresh ();
  285. Assert.Equal (new Rect (0, 0, 0, 1), view.Frame);
  286. Assert.Equal (new Size (0, 1), view.TextFormatter.Size);
  287. var exception = Record.Exception (() => Assert.Equal (new List<string> () { string.Empty }, view.TextFormatter.Lines));
  288. Assert.Null (exception);
  289. expected = @"
  290. ┌────────┐
  291. │ │
  292. │ │
  293. └────────┘
  294. ";
  295. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  296. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  297. }
  298. [Fact, AutoInitShutdown]
  299. public void AutoSize_True_Label_IsEmpty_False_Never_Return_Null_Lines ()
  300. {
  301. var text = "Label";
  302. var label = new Label () {
  303. Width = Dim.Fill () - text.Length,
  304. Height = 1,
  305. Text = text
  306. };
  307. var win = new Window () {
  308. Width = Dim.Fill (),
  309. Height = Dim.Fill ()
  310. };
  311. win.Add (label);
  312. Application.Top.Add (win);
  313. Application.Begin (Application.Top);
  314. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  315. Assert.Equal (5, text.Length);
  316. Assert.True (label.AutoSize);
  317. Assert.Equal (new Rect (0, 0, 5, 1), label.Frame);
  318. Assert.Equal (new Size (5, 1), label.TextFormatter.Size);
  319. Assert.Equal (new List<string> () { "Label" }, label.TextFormatter.Lines);
  320. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  321. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  322. var expected = @"
  323. ┌────────┐
  324. │Label │
  325. │ │
  326. └────────┘
  327. ";
  328. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  329. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  330. text = "0123456789";
  331. Assert.Equal (10, text.Length);
  332. label.Width = Dim.Fill () - text.Length;
  333. Application.Refresh ();
  334. Assert.True (label.AutoSize);
  335. Assert.Equal (new Rect (0, 0, 5, 1), label.Frame);
  336. Assert.Equal (new Size (5, 1), label.TextFormatter.Size);
  337. Assert.Single (label.TextFormatter.Lines);
  338. expected = @"
  339. ┌────────┐
  340. │Label │
  341. │ │
  342. └────────┘
  343. ";
  344. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  345. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  346. }
  347. [Fact, AutoInitShutdown]
  348. public void AutoSize_False_Label_IsEmpty_True_Return_Null_Lines ()
  349. {
  350. var text = "Label";
  351. var label = new Label () {
  352. Width = Dim.Fill () - text.Length,
  353. Height = 1,
  354. Text = text,
  355. AutoSize = false
  356. };
  357. var win = new Window () {
  358. Width = Dim.Fill (),
  359. Height = Dim.Fill ()
  360. };
  361. win.Add (label);
  362. Application.Top.Add (win);
  363. Application.Begin (Application.Top);
  364. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  365. Assert.Equal (5, text.Length);
  366. Assert.False (label.AutoSize);
  367. Assert.Equal (new Rect (0, 0, 3, 1), label.Frame);
  368. Assert.Equal (new Size (3, 1), label.TextFormatter.Size);
  369. Assert.Equal (new List<string> () { "Lab" }, label.TextFormatter.Lines);
  370. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  371. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  372. var expected = @"
  373. ┌────────┐
  374. │Lab │
  375. │ │
  376. └────────┘
  377. ";
  378. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  379. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  380. text = "0123456789";
  381. Assert.Equal (10, text.Length);
  382. label.Width = Dim.Fill () - text.Length;
  383. Application.Refresh ();
  384. Assert.False (label.AutoSize);
  385. Assert.Equal (new Rect (0, 0, 0, 1), label.Frame);
  386. Assert.Equal (new Size (0, 1), label.TextFormatter.Size);
  387. Assert.Equal (new List<string> { string.Empty }, label.TextFormatter.Lines);
  388. expected = @"
  389. ┌────────┐
  390. │ │
  391. │ │
  392. └────────┘
  393. ";
  394. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  395. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  396. }
  397. [Fact, AutoInitShutdown]
  398. public void AutoSize_True_Label_IsEmpty_False_Minimum_Height ()
  399. {
  400. var text = "Label";
  401. var label = new Label () {
  402. Width = Dim.Fill () - text.Length,
  403. Text = text
  404. };
  405. var win = new Window () {
  406. Width = Dim.Fill (),
  407. Height = Dim.Fill ()
  408. };
  409. win.Add (label);
  410. Application.Top.Add (win);
  411. Application.Begin (Application.Top);
  412. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  413. Assert.Equal (5, text.Length);
  414. Assert.True (label.AutoSize);
  415. Assert.Equal (new Rect (0, 0, 5, 1), label.Frame);
  416. Assert.Equal (new Size (5, 1), label.TextFormatter.Size);
  417. Assert.Equal (new List<string> () { "Label" }, label.TextFormatter.Lines);
  418. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  419. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  420. var expected = @"
  421. ┌────────┐
  422. │Label │
  423. │ │
  424. └────────┘
  425. ";
  426. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  427. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  428. text = "0123456789";
  429. Assert.Equal (10, text.Length);
  430. label.Width = Dim.Fill () - text.Length;
  431. Application.Refresh ();
  432. Assert.Equal (new Rect (0, 0, 5, 1), label.Frame);
  433. Assert.Equal (new Size (5, 1), label.TextFormatter.Size);
  434. var exception = Record.Exception (() => Assert.Single (label.TextFormatter.Lines));
  435. Assert.Null (exception);
  436. expected = @"
  437. ┌────────┐
  438. │Label │
  439. │ │
  440. └────────┘
  441. ";
  442. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  443. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  444. }
  445. [Fact, AutoInitShutdown]
  446. public void AutoSize_False_Label_Height_Zero_Returns_Minimum_Height ()
  447. {
  448. var text = "Label";
  449. var label = new Label () {
  450. Width = Dim.Fill () - text.Length,
  451. Text = text,
  452. AutoSize = false
  453. };
  454. var win = new Window () {
  455. Width = Dim.Fill (),
  456. Height = Dim.Fill ()
  457. };
  458. win.Add (label);
  459. Application.Top.Add (win);
  460. Application.Begin (Application.Top);
  461. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  462. Assert.Equal (5, text.Length);
  463. Assert.False (label.AutoSize);
  464. Assert.Equal (new Rect (0, 0, 3, 1), label.Frame);
  465. Assert.Equal (new Size (3, 1), label.TextFormatter.Size);
  466. Assert.Single (label.TextFormatter.Lines);
  467. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  468. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  469. var expected = @"
  470. ┌────────┐
  471. │Lab │
  472. │ │
  473. └────────┘
  474. ";
  475. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  476. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  477. text = "0123456789";
  478. Assert.Equal (10, text.Length);
  479. label.Width = Dim.Fill () - text.Length;
  480. Application.Refresh ();
  481. Assert.Equal (new Rect (0, 0, 0, 1), label.Frame);
  482. Assert.Equal (new Size (0, 1), label.TextFormatter.Size);
  483. var exception = Record.Exception (() => Assert.Equal (new List<string> () { string.Empty }, label.TextFormatter.Lines));
  484. Assert.Null (exception);
  485. expected = @"
  486. ┌────────┐
  487. │ │
  488. │ │
  489. └────────┘
  490. ";
  491. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  492. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  493. }
  494. [Fact, AutoInitShutdown]
  495. public void AutoSize_True_View_IsEmpty_False_Minimum_Width ()
  496. {
  497. var text = "Views";
  498. var view = new View () {
  499. TextDirection = TextDirection.TopBottom_LeftRight,
  500. Height = Dim.Fill () - text.Length,
  501. Text = text,
  502. AutoSize = true
  503. };
  504. var win = new Window () {
  505. Width = Dim.Fill (),
  506. Height = Dim.Fill ()
  507. };
  508. win.Add (view);
  509. Application.Top.Add (win);
  510. Application.Begin (Application.Top);
  511. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  512. Assert.Equal (5, text.Length);
  513. Assert.True (view.AutoSize);
  514. Assert.Equal (new Rect (0, 0, 1, 5), view.Frame);
  515. Assert.Equal (new Size (1, 5), view.TextFormatter.Size);
  516. Assert.Equal (new List<string> () { "Views" }, view.TextFormatter.Lines);
  517. Assert.Equal (new Rect (0, 0, 4, 10), win.Frame);
  518. Assert.Equal (new Rect (0, 0, 4, 10), Application.Top.Frame);
  519. var expected = @"
  520. ┌──┐
  521. │V │
  522. │i │
  523. │e │
  524. │w │
  525. │s │
  526. │ │
  527. │ │
  528. │ │
  529. └──┘
  530. ";
  531. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  532. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  533. text = "0123456789";
  534. Assert.Equal (10, text.Length);
  535. view.Height = Dim.Fill () - text.Length;
  536. Application.Refresh ();
  537. Assert.Equal (new Rect (0, 0, 1, 5), view.Frame);
  538. Assert.Equal (new Size (1, 5), view.TextFormatter.Size);
  539. var exception = Record.Exception (() => Assert.Single (view.TextFormatter.Lines));
  540. Assert.Null (exception);
  541. expected = @"
  542. ┌──┐
  543. │V │
  544. │i │
  545. │e │
  546. │w │
  547. │s │
  548. │ │
  549. │ │
  550. │ │
  551. └──┘
  552. ";
  553. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  554. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  555. }
  556. [Fact, AutoInitShutdown]
  557. public void AutoSize_False_View_Width_Null_Returns_Host_Frame_Width ()
  558. {
  559. var text = "Views";
  560. var view = new View () {
  561. TextDirection = TextDirection.TopBottom_LeftRight,
  562. Height = Dim.Fill () - text.Length,
  563. Text = text
  564. };
  565. var win = new Window () {
  566. Width = Dim.Fill (),
  567. Height = Dim.Fill ()
  568. };
  569. win.Add (view);
  570. Application.Top.Add (win);
  571. Application.Begin (Application.Top);
  572. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  573. Assert.Equal (5, text.Length);
  574. Assert.False (view.AutoSize);
  575. Assert.Equal (new Rect (0, 0, 1, 3), view.Frame);
  576. Assert.Equal (new Size (1, 3), view.TextFormatter.Size);
  577. Assert.Single (view.TextFormatter.Lines);
  578. Assert.Equal (new Rect (0, 0, 4, 10), win.Frame);
  579. Assert.Equal (new Rect (0, 0, 4, 10), Application.Top.Frame);
  580. var expected = @"
  581. ┌──┐
  582. │V │
  583. │i │
  584. │e │
  585. │ │
  586. │ │
  587. │ │
  588. │ │
  589. │ │
  590. └──┘
  591. ";
  592. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  593. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  594. text = "0123456789";
  595. Assert.Equal (10, text.Length);
  596. view.Height = Dim.Fill () - text.Length;
  597. Application.Refresh ();
  598. Assert.Equal (new Rect (0, 0, 1, 0), view.Frame);
  599. Assert.Equal (new Size (1, 0), view.TextFormatter.Size);
  600. var exception = Record.Exception (() => Assert.Equal (new List<string> () { string.Empty }, view.TextFormatter.Lines));
  601. Assert.Null (exception);
  602. expected = @"
  603. ┌──┐
  604. │ │
  605. │ │
  606. │ │
  607. │ │
  608. │ │
  609. │ │
  610. │ │
  611. │ │
  612. └──┘
  613. ";
  614. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  615. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  616. }
  617. [Fact, AutoInitShutdown]
  618. public void AutoSize_True_View_IsEmpty_False_Minimum_Width_Wide_Rune ()
  619. {
  620. var text = "界View";
  621. var view = new View () {
  622. TextDirection = TextDirection.TopBottom_LeftRight,
  623. Height = Dim.Fill () - text.Length,
  624. Text = text,
  625. AutoSize = true
  626. };
  627. var win = new Window () {
  628. Width = Dim.Fill (),
  629. Height = Dim.Fill ()
  630. };
  631. win.Add (view);
  632. Application.Top.Add (win);
  633. Application.Begin (Application.Top);
  634. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  635. Assert.Equal (5, text.Length);
  636. Assert.True (view.AutoSize);
  637. Assert.Equal (new Rect (0, 0, 2, 5), view.Frame);
  638. Assert.Equal (new Size (2, 5), view.TextFormatter.Size);
  639. Assert.Equal (new List<string> () { "界View" }, view.TextFormatter.Lines);
  640. Assert.Equal (new Rect (0, 0, 4, 10), win.Frame);
  641. Assert.Equal (new Rect (0, 0, 4, 10), Application.Top.Frame);
  642. var expected = @"
  643. ┌──┐
  644. │界│
  645. │V │
  646. │i │
  647. │e │
  648. │w │
  649. │ │
  650. │ │
  651. │ │
  652. └──┘
  653. ";
  654. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  655. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  656. text = "0123456789";
  657. Assert.Equal (10, text.Length);
  658. view.Height = Dim.Fill () - text.Length;
  659. Application.Refresh ();
  660. Assert.Equal (new Rect (0, 0, 2, 5), view.Frame);
  661. Assert.Equal (new Size (2, 5), view.TextFormatter.Size);
  662. var exception = Record.Exception (() => Assert.Equal (new List<string> () { "界View" }, view.TextFormatter.Lines));
  663. Assert.Null (exception);
  664. expected = @"
  665. ┌──┐
  666. │界│
  667. │V │
  668. │i │
  669. │e │
  670. │w │
  671. │ │
  672. │ │
  673. │ │
  674. └──┘
  675. ";
  676. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  677. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  678. }
  679. [Fact, AutoInitShutdown]
  680. public void AutoSize_False_View_Width_Zero_Returns_Minimum_Width_With_Wide_Rune ()
  681. {
  682. var text = "界View";
  683. var view = new View () {
  684. TextDirection = TextDirection.TopBottom_LeftRight,
  685. Height = Dim.Fill () - text.Length,
  686. Text = text
  687. };
  688. var win = new Window () {
  689. Width = Dim.Fill (),
  690. Height = Dim.Fill ()
  691. };
  692. win.Add (view);
  693. Application.Top.Add (win);
  694. Application.Begin (Application.Top);
  695. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  696. Assert.Equal (5, text.Length);
  697. Assert.False (view.AutoSize);
  698. Assert.Equal (new Rect (0, 0, 2, 3), view.Frame);
  699. Assert.Equal (new Size (2, 3), view.TextFormatter.Size);
  700. Assert.Single (view.TextFormatter.Lines);
  701. Assert.Equal (new Rect (0, 0, 4, 10), win.Frame);
  702. Assert.Equal (new Rect (0, 0, 4, 10), Application.Top.Frame);
  703. var expected = @"
  704. ┌──┐
  705. │界│
  706. │V │
  707. │i │
  708. │ │
  709. │ │
  710. │ │
  711. │ │
  712. │ │
  713. └──┘
  714. ";
  715. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  716. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  717. text = "0123456789";
  718. Assert.Equal (10, text.Length);
  719. view.Height = Dim.Fill () - text.Length;
  720. Application.Refresh ();
  721. Assert.Equal (new Rect (0, 0, 2, 0), view.Frame);
  722. Assert.Equal (new Size (2, 0), view.TextFormatter.Size);
  723. var exception = Record.Exception (() => Assert.Equal (new List<string> () { string.Empty }, view.TextFormatter.Lines));
  724. Assert.Null (exception);
  725. expected = @"
  726. ┌──┐
  727. │ │
  728. │ │
  729. │ │
  730. │ │
  731. │ │
  732. │ │
  733. │ │
  734. │ │
  735. └──┘
  736. ";
  737. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  738. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  739. }
  740. }
  741. }