TextTests.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. using System.Collections.Generic;
  2. using Xunit;
  3. using Xunit.Abstractions;
  4. namespace Terminal.Gui.ViewTests;
  5. /// <summary>
  6. /// Tests of the <see cref="View.Text"/> property with <see cref="View.AutoSize"/> set to false.
  7. /// </summary>
  8. public class TextTests {
  9. readonly ITestOutputHelper _output;
  10. public TextTests (ITestOutputHelper output) => _output = output;
  11. [Fact]
  12. [AutoInitShutdown]
  13. public void AutoSize_False_View_IsEmpty_False_Return_Null_Lines ()
  14. {
  15. var text = "Views";
  16. var view = new View {
  17. Width = Dim.Fill () - text.Length,
  18. Height = 1,
  19. Text = text
  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. Assert.Equal (5, text.Length);
  30. Assert.False (view.AutoSize);
  31. Assert.Equal (new Rect (0, 0, 3, 1), view.Frame);
  32. Assert.Equal (new Size (3, 1), view.TextFormatter.Size);
  33. Assert.Equal (new List<string> { "Vie" }, view.TextFormatter.Lines);
  34. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  35. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  36. var expected = @"
  37. ┌────────┐
  38. │Vie │
  39. │ │
  40. └────────┘
  41. ";
  42. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  43. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  44. text = "0123456789";
  45. Assert.Equal (10, text.Length);
  46. view.Width = Dim.Fill () - text.Length;
  47. Application.Refresh ();
  48. Assert.Equal (new Rect (0, 0, 0, 1), view.Frame);
  49. Assert.Equal (new Size (0, 1), view.TextFormatter.Size);
  50. Assert.Equal (new List<string> { string.Empty }, view.TextFormatter.Lines);
  51. expected = @"
  52. ┌────────┐
  53. │ │
  54. │ │
  55. └────────┘
  56. ";
  57. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  58. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  59. }
  60. [Fact]
  61. [AutoInitShutdown]
  62. public void AutoSize_False_View_IsEmpty_True_Minimum_Height ()
  63. {
  64. var text = "Views";
  65. var view = new View {
  66. Width = Dim.Fill () - text.Length,
  67. Text = text
  68. };
  69. var win = new Window {
  70. Width = Dim.Fill (),
  71. Height = Dim.Fill ()
  72. };
  73. win.Add (view);
  74. Application.Top.Add (win);
  75. Application.Begin (Application.Top);
  76. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  77. Assert.Equal (5, text.Length);
  78. Assert.False (view.AutoSize);
  79. Assert.Equal (new Rect (0, 0, 3, 1), view.Frame);
  80. Assert.Equal (new Size (3, 1), view.TextFormatter.Size);
  81. Assert.Single (view.TextFormatter.Lines);
  82. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  83. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  84. var expected = @"
  85. ┌────────┐
  86. │Vie │
  87. │ │
  88. └────────┘
  89. ";
  90. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  91. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  92. text = "0123456789";
  93. Assert.Equal (10, text.Length);
  94. view.Width = Dim.Fill () - text.Length;
  95. Application.Refresh ();
  96. Assert.Equal (new Rect (0, 0, 0, 1), view.Frame);
  97. Assert.Equal (new Size (0, 1), view.TextFormatter.Size);
  98. var exception = Record.Exception (() => Assert.Equal (new List<string> { string.Empty }, view.TextFormatter.Lines));
  99. Assert.Null (exception);
  100. expected = @"
  101. ┌────────┐
  102. │ │
  103. │ │
  104. └────────┘
  105. ";
  106. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  107. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  108. }
  109. [Fact]
  110. [AutoInitShutdown]
  111. public void AutoSize_False_Label_IsEmpty_True_Return_Null_Lines ()
  112. {
  113. var text = "Label";
  114. var label = new Label {
  115. Width = Dim.Fill () - text.Length,
  116. Height = 1,
  117. Text = text,
  118. AutoSize = false
  119. };
  120. var win = new Window {
  121. Width = Dim.Fill (),
  122. Height = Dim.Fill ()
  123. };
  124. win.Add (label);
  125. Application.Top.Add (win);
  126. Application.Begin (Application.Top);
  127. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  128. Assert.Equal (5, text.Length);
  129. Assert.False (label.AutoSize);
  130. Assert.Equal (new Rect (0, 0, 3, 1), label.Frame);
  131. Assert.Equal (new Size (3, 1), label.TextFormatter.Size);
  132. Assert.Equal (new List<string> { "Lab" }, label.TextFormatter.Lines);
  133. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  134. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  135. var expected = @"
  136. ┌────────┐
  137. │Lab │
  138. │ │
  139. └────────┘
  140. ";
  141. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  142. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  143. text = "0123456789";
  144. Assert.Equal (10, text.Length);
  145. label.Width = Dim.Fill () - text.Length;
  146. Application.Refresh ();
  147. Assert.False (label.AutoSize);
  148. Assert.Equal (new Rect (0, 0, 0, 1), label.Frame);
  149. Assert.Equal (new Size (0, 1), label.TextFormatter.Size);
  150. Assert.Equal (new List<string> { string.Empty }, label.TextFormatter.Lines);
  151. expected = @"
  152. ┌────────┐
  153. │ │
  154. │ │
  155. └────────┘
  156. ";
  157. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  158. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  159. }
  160. [Fact]
  161. [AutoInitShutdown]
  162. public void AutoSize_False_Label_Height_Zero_Returns_Minimum_Height ()
  163. {
  164. var text = "Label";
  165. var label = new Label {
  166. Width = Dim.Fill () - text.Length,
  167. Text = text,
  168. AutoSize = false
  169. };
  170. var win = new Window {
  171. Width = Dim.Fill (),
  172. Height = Dim.Fill ()
  173. };
  174. win.Add (label);
  175. Application.Top.Add (win);
  176. Application.Begin (Application.Top);
  177. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  178. Assert.Equal (5, text.Length);
  179. Assert.False (label.AutoSize);
  180. Assert.Equal (new Rect (0, 0, 3, 1), label.Frame);
  181. Assert.Equal (new Size (3, 1), label.TextFormatter.Size);
  182. Assert.Single (label.TextFormatter.Lines);
  183. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  184. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  185. var expected = @"
  186. ┌────────┐
  187. │Lab │
  188. │ │
  189. └────────┘
  190. ";
  191. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  192. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  193. text = "0123456789";
  194. Assert.Equal (10, text.Length);
  195. label.Width = Dim.Fill () - text.Length;
  196. Application.Refresh ();
  197. Assert.Equal (new Rect (0, 0, 0, 1), label.Frame);
  198. Assert.Equal (new Size (0, 1), label.TextFormatter.Size);
  199. var exception = Record.Exception (() => Assert.Equal (new List<string> { string.Empty }, label.TextFormatter.Lines));
  200. Assert.Null (exception);
  201. expected = @"
  202. ┌────────┐
  203. │ │
  204. │ │
  205. └────────┘
  206. ";
  207. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  208. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  209. }
  210. [Fact]
  211. [AutoInitShutdown]
  212. public void AutoSize_False_View_Width_Null_Returns_Host_Frame_Width ()
  213. {
  214. var text = "Views";
  215. var view = new View {
  216. TextDirection = TextDirection.TopBottom_LeftRight,
  217. Height = Dim.Fill () - text.Length,
  218. Text = text
  219. };
  220. var win = new Window {
  221. Width = Dim.Fill (),
  222. Height = Dim.Fill ()
  223. };
  224. win.Add (view);
  225. Application.Top.Add (win);
  226. Application.Begin (Application.Top);
  227. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  228. Assert.Equal (5, text.Length);
  229. Assert.False (view.AutoSize);
  230. Assert.Equal (new Rect (0, 0, 1, 3), view.Frame);
  231. Assert.Equal (new Size (1, 3), view.TextFormatter.Size);
  232. Assert.Single (view.TextFormatter.Lines);
  233. Assert.Equal (new Rect (0, 0, 4, 10), win.Frame);
  234. Assert.Equal (new Rect (0, 0, 4, 10), Application.Top.Frame);
  235. var expected = @"
  236. ┌──┐
  237. │V │
  238. │i │
  239. │e │
  240. │ │
  241. │ │
  242. │ │
  243. │ │
  244. │ │
  245. └──┘
  246. ";
  247. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  248. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  249. text = "0123456789";
  250. Assert.Equal (10, text.Length);
  251. view.Height = Dim.Fill () - text.Length;
  252. Application.Refresh ();
  253. Assert.Equal (new Rect (0, 0, 1, 0), view.Frame);
  254. Assert.Equal (new Size (1, 0), view.TextFormatter.Size);
  255. var exception = Record.Exception (() => Assert.Equal (new List<string> { string.Empty }, view.TextFormatter.Lines));
  256. Assert.Null (exception);
  257. expected = @"
  258. ┌──┐
  259. │ │
  260. │ │
  261. │ │
  262. │ │
  263. │ │
  264. │ │
  265. │ │
  266. │ │
  267. └──┘
  268. ";
  269. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  270. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  271. }
  272. [Fact]
  273. [AutoInitShutdown]
  274. public void AutoSize_False_View_Width_Zero_Returns_Minimum_Width_With_Wide_Rune ()
  275. {
  276. var text = "界View";
  277. var view = new View {
  278. TextDirection = TextDirection.TopBottom_LeftRight,
  279. Height = Dim.Fill () - text.Length,
  280. Text = text
  281. };
  282. var win = new Window {
  283. Width = Dim.Fill (),
  284. Height = Dim.Fill ()
  285. };
  286. win.Add (view);
  287. Application.Top.Add (win);
  288. Application.Begin (Application.Top);
  289. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  290. Assert.Equal (5, text.Length);
  291. Assert.False (view.AutoSize);
  292. Assert.Equal (new Rect (0, 0, 2, 3), view.Frame);
  293. Assert.Equal (new Size (2, 3), view.TextFormatter.Size);
  294. Assert.Single (view.TextFormatter.Lines);
  295. Assert.Equal (new Rect (0, 0, 4, 10), win.Frame);
  296. Assert.Equal (new Rect (0, 0, 4, 10), Application.Top.Frame);
  297. var expected = @"
  298. ┌──┐
  299. │界│
  300. │V │
  301. │i │
  302. │ │
  303. │ │
  304. │ │
  305. │ │
  306. │ │
  307. └──┘
  308. ";
  309. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  310. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  311. text = "0123456789";
  312. Assert.Equal (10, text.Length);
  313. view.Height = Dim.Fill () - text.Length;
  314. Application.Refresh ();
  315. Assert.Equal (new Rect (0, 0, 2, 0), view.Frame);
  316. Assert.Equal (new Size (2, 0), view.TextFormatter.Size);
  317. var exception = Record.Exception (() => Assert.Equal (new List<string> { string.Empty }, view.TextFormatter.Lines));
  318. Assert.Null (exception);
  319. expected = @"
  320. ┌──┐
  321. │ │
  322. │ │
  323. │ │
  324. │ │
  325. │ │
  326. │ │
  327. │ │
  328. │ │
  329. └──┘
  330. ";
  331. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  332. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  333. }
  334. [Fact]
  335. public void AutoSize_False_If_Text_Empty ()
  336. {
  337. var view1 = new View ();
  338. var view2 = new View ("");
  339. var view3 = new View { Text = "" };
  340. Assert.False (view1.AutoSize);
  341. Assert.False (view2.AutoSize);
  342. Assert.False (view3.AutoSize);
  343. view1.Dispose ();
  344. view2.Dispose ();
  345. view3.Dispose ();
  346. }
  347. [Fact]
  348. public void AutoSize_False_If_Text_Is_Not_Empty ()
  349. {
  350. var view1 = new View ();
  351. view1.Text = "Hello World";
  352. var view2 = new View ("Hello World");
  353. var view3 = new View { Text = "Hello World" };
  354. Assert.False (view1.AutoSize);
  355. Assert.False (view2.AutoSize);
  356. Assert.False (view3.AutoSize);
  357. view1.Dispose ();
  358. view2.Dispose ();
  359. view3.Dispose ();
  360. }
  361. [Fact]
  362. public void AutoSize_False_ResizeView_Is_Always_False ()
  363. {
  364. var super = new View ();
  365. var label = new Label { AutoSize = false };
  366. super.Add (label);
  367. label.Text = "New text";
  368. super.LayoutSubviews ();
  369. Assert.False (label.AutoSize);
  370. Assert.Equal ("(0,0,0,1)", label.Bounds.ToString ());
  371. super.Dispose ();
  372. }
  373. [Fact]
  374. [AutoInitShutdown]
  375. public void AutoSize_False_ResizeView_With_Dim_Fill_After_IsInitialized ()
  376. {
  377. var win = new Window (new Rect (0, 0, 30, 80));
  378. var label = new Label { AutoSize = false, Width = Dim.Fill (), Height = Dim.Fill () };
  379. win.Add (label);
  380. Application.Top.Add (win);
  381. // Text is empty but height=1 by default, see Label view
  382. Assert.False (label.AutoSize);
  383. Assert.Equal ("(0,0,0,1)", label.Bounds.ToString ());
  384. label.Text = "New text\nNew line";
  385. Application.Top.LayoutSubviews ();
  386. Assert.False (label.AutoSize);
  387. Assert.Equal ("(0,0,28,78)", label.Bounds.ToString ());
  388. Assert.False (label.IsInitialized);
  389. var rs = Application.Begin (Application.Top);
  390. Assert.True (label.IsInitialized);
  391. Assert.False (label.AutoSize);
  392. Assert.Equal ("(0,0,28,78)", label.Bounds.ToString ());
  393. Application.End (rs);
  394. }
  395. [Fact]
  396. [AutoInitShutdown]
  397. public void AutoSize_False_SetWidthHeight_With_Dim_Fill_And_Dim_Absolute_After_IsAdded_And_IsInitialized ()
  398. {
  399. var win = new Window (new Rect (0, 0, 30, 80));
  400. var label = new Label { Width = Dim.Fill () };
  401. win.Add (label);
  402. Application.Top.Add (win);
  403. Assert.True (label.IsAdded);
  404. // Text is empty but height=1 by default, see Label view
  405. Assert.True (label.AutoSize);
  406. // BUGBUG: LayoutSubviews has not been called, so this test is not really valid (pos/dim are indeterminate, not 0)
  407. // Not really a bug because View call OnResizeNeeded method on the SetInitialProperties method
  408. Assert.Equal ("(0,0,0,1)", label.Bounds.ToString ());
  409. label.Text = "First line\nSecond line";
  410. Application.Top.LayoutSubviews ();
  411. Assert.True (label.AutoSize);
  412. // BUGBUG: This test is bogus: label has not been initialized. pos/dim is indeterminate!
  413. Assert.Equal ("(0,0,28,2)", label.Bounds.ToString ());
  414. Assert.False (label.IsInitialized);
  415. var rs = Application.Begin (Application.Top);
  416. Assert.True (label.AutoSize);
  417. Assert.Equal ("(0,0,28,2)", label.Bounds.ToString ());
  418. Assert.True (label.IsInitialized);
  419. label.AutoSize = false;
  420. Application.Refresh ();
  421. Assert.False (label.AutoSize);
  422. Assert.Equal ("(0,0,28,1)", label.Bounds.ToString ());
  423. Application.End (rs);
  424. }
  425. [Fact]
  426. [AutoInitShutdown]
  427. public void AutoSize_False_SetWidthHeight_With_Dim_Fill_And_Dim_Absolute_With_Initialization ()
  428. {
  429. var win = new Window (new Rect (0, 0, 30, 80));
  430. var label = new Label { Width = Dim.Fill () };
  431. win.Add (label);
  432. Application.Top.Add (win);
  433. // Text is empty but height=1 by default, see Label view
  434. Assert.True (label.AutoSize);
  435. Assert.Equal ("(0,0,0,1)", label.Bounds.ToString ());
  436. var rs = Application.Begin (Application.Top);
  437. Assert.True (label.AutoSize);
  438. // Here the AutoSize ensuring the right size with width 28 (Dim.Fill)
  439. // and height 0 because wasn't set and the text is empty
  440. // BUGBUG: Because of #2450, this test is bogus: pos/dim is indeterminate!
  441. //Assert.Equal ("(0,0,28,0)", label.Bounds.ToString ());
  442. label.Text = "First line\nSecond line";
  443. Application.Refresh ();
  444. // Here the AutoSize ensuring the right size with width 28 (Dim.Fill)
  445. // and height 2 because wasn't set and the text has 2 lines
  446. Assert.True (label.AutoSize);
  447. Assert.Equal ("(0,0,28,2)", label.Bounds.ToString ());
  448. label.AutoSize = false;
  449. Application.Refresh ();
  450. // Here the SetMinWidthHeight ensuring the minimum height
  451. Assert.False (label.AutoSize);
  452. Assert.Equal ("(0,0,28,1)", label.Bounds.ToString ());
  453. label.Text = "First changed line\nSecond changed line\nNew line";
  454. Application.Refresh ();
  455. // Here the AutoSize is false and the width 28 (Dim.Fill) and
  456. // height 1 because wasn't set and SetMinWidthHeight ensuring the minimum height
  457. Assert.False (label.AutoSize);
  458. Assert.Equal ("(0,0,28,1)", label.Bounds.ToString ());
  459. label.AutoSize = true;
  460. Application.Refresh ();
  461. // Here the AutoSize ensuring the right size with width 28 (Dim.Fill)
  462. // and height 3 because wasn't set and the text has 3 lines
  463. Assert.True (label.AutoSize);
  464. // BUGBUG: v2 - AutoSize is broken - temporarily disabling test See #2432
  465. //Assert.Equal ("(0,0,28,3)", label.Bounds.ToString ());
  466. Application.End (rs);
  467. }
  468. [Fact]
  469. [AutoInitShutdown]
  470. public void AutoSize_False_Equal_Before_And_After_IsInitialized_With_Differents_Orders ()
  471. {
  472. var view1 = new View { Text = "Say Hello view1 你", AutoSize = false, Width = 10, Height = 5 };
  473. var view2 = new View { Text = "Say Hello view2 你", Width = 10, Height = 5, AutoSize = false };
  474. var view3 = new View { AutoSize = false, Width = 10, Height = 5, Text = "Say Hello view3 你" };
  475. var view4 = new View {
  476. Text = "Say Hello view4 你",
  477. AutoSize = false,
  478. Width = 10,
  479. Height = 5,
  480. TextDirection = TextDirection.TopBottom_LeftRight
  481. };
  482. var view5 = new View {
  483. Text = "Say Hello view5 你",
  484. Width = 10,
  485. Height = 5,
  486. AutoSize = false,
  487. TextDirection = TextDirection.TopBottom_LeftRight
  488. };
  489. var view6 = new View {
  490. AutoSize = false,
  491. Width = 10,
  492. Height = 5,
  493. TextDirection = TextDirection.TopBottom_LeftRight,
  494. Text = "Say Hello view6 你"
  495. };
  496. Application.Top.Add (view1, view2, view3, view4, view5, view6);
  497. Assert.False (view1.IsInitialized);
  498. Assert.False (view2.IsInitialized);
  499. Assert.False (view3.IsInitialized);
  500. Assert.False (view4.IsInitialized);
  501. Assert.False (view5.IsInitialized);
  502. Assert.False (view1.AutoSize);
  503. Assert.Equal (new Rect (0, 0, 10, 5), view1.Frame);
  504. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  505. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  506. Assert.False (view2.AutoSize);
  507. Assert.Equal (new Rect (0, 0, 10, 5), view2.Frame);
  508. Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  509. Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  510. Assert.False (view3.AutoSize);
  511. Assert.Equal (new Rect (0, 0, 10, 5), view3.Frame);
  512. Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  513. Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  514. Assert.False (view4.AutoSize);
  515. Assert.Equal (new Rect (0, 0, 10, 5), view4.Frame);
  516. Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  517. Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  518. Assert.False (view5.AutoSize);
  519. Assert.Equal (new Rect (0, 0, 10, 5), view5.Frame);
  520. Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  521. Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  522. Assert.False (view6.AutoSize);
  523. Assert.Equal (new Rect (0, 0, 10, 5), view6.Frame);
  524. Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  525. Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  526. var rs = Application.Begin (Application.Top);
  527. Assert.True (view1.IsInitialized);
  528. Assert.True (view2.IsInitialized);
  529. Assert.True (view3.IsInitialized);
  530. Assert.True (view4.IsInitialized);
  531. Assert.True (view5.IsInitialized);
  532. Assert.False (view1.AutoSize);
  533. Assert.Equal (new Rect (0, 0, 10, 5), view1.Frame);
  534. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  535. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  536. Assert.False (view2.AutoSize);
  537. Assert.Equal (new Rect (0, 0, 10, 5), view2.Frame);
  538. Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  539. Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  540. Assert.False (view3.AutoSize);
  541. Assert.Equal (new Rect (0, 0, 10, 5), view3.Frame);
  542. Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  543. Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  544. Assert.False (view4.AutoSize);
  545. Assert.Equal (new Rect (0, 0, 10, 5), view4.Frame);
  546. Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  547. Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  548. Assert.False (view5.AutoSize);
  549. Assert.Equal (new Rect (0, 0, 10, 5), view5.Frame);
  550. Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  551. Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  552. Assert.False (view6.AutoSize);
  553. Assert.Equal (new Rect (0, 0, 10, 5), view6.Frame);
  554. Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  555. Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  556. Application.End (rs);
  557. }
  558. [Fact]
  559. [AutoInitShutdown]
  560. public void AutoSize_False_TextDirection_Toggle ()
  561. {
  562. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  563. // View is AutoSize == true
  564. var view = new View ();
  565. win.Add (view);
  566. Application.Top.Add (win);
  567. var rs = Application.Begin (Application.Top);
  568. ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  569. Assert.Equal (new Rect (0, 0, 22, 22), win.Frame);
  570. Assert.Equal (new Rect (0, 0, 22, 22), win.Margin.Frame);
  571. Assert.Equal (new Rect (0, 0, 22, 22), win.Border.Frame);
  572. Assert.Equal (new Rect (1, 1, 20, 20), win.Padding.Frame);
  573. Assert.False (view.AutoSize);
  574. Assert.Equal (TextDirection.LeftRight_TopBottom, view.TextDirection);
  575. Assert.Equal (Rect.Empty, view.Frame);
  576. Assert.Equal ("Absolute(0)", view.X.ToString ());
  577. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  578. Assert.Equal ("Absolute(0)", view.Width.ToString ());
  579. Assert.Equal ("Absolute(0)", view.Height.ToString ());
  580. var expected = @"
  581. ┌────────────────────┐
  582. │ │
  583. │ │
  584. │ │
  585. │ │
  586. │ │
  587. │ │
  588. │ │
  589. │ │
  590. │ │
  591. │ │
  592. │ │
  593. │ │
  594. │ │
  595. │ │
  596. │ │
  597. │ │
  598. │ │
  599. │ │
  600. │ │
  601. │ │
  602. └────────────────────┘
  603. ";
  604. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  605. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  606. view.Text = "Hello World";
  607. view.Width = 11;
  608. view.Height = 1;
  609. win.LayoutSubviews ();
  610. Application.Refresh ();
  611. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  612. Assert.Equal ("Absolute(0)", view.X.ToString ());
  613. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  614. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  615. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  616. expected = @"
  617. ┌────────────────────┐
  618. │Hello World │
  619. │ │
  620. │ │
  621. │ │
  622. │ │
  623. │ │
  624. │ │
  625. │ │
  626. │ │
  627. │ │
  628. │ │
  629. │ │
  630. │ │
  631. │ │
  632. │ │
  633. │ │
  634. │ │
  635. │ │
  636. │ │
  637. │ │
  638. └────────────────────┘
  639. ";
  640. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  641. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  642. view.AutoSize = true;
  643. view.Text = "Hello Worlds";
  644. Application.Refresh ();
  645. Assert.Equal (new Rect (0, 0, 12, 1), view.Frame);
  646. Assert.Equal ("Absolute(0)", view.X.ToString ());
  647. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  648. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  649. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  650. expected = @"
  651. ┌────────────────────┐
  652. │Hello Worlds │
  653. │ │
  654. │ │
  655. │ │
  656. │ │
  657. │ │
  658. │ │
  659. │ │
  660. │ │
  661. │ │
  662. │ │
  663. │ │
  664. │ │
  665. │ │
  666. │ │
  667. │ │
  668. │ │
  669. │ │
  670. │ │
  671. │ │
  672. └────────────────────┘
  673. ";
  674. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  675. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  676. view.TextDirection = TextDirection.TopBottom_LeftRight;
  677. Application.Refresh ();
  678. Assert.Equal (new Rect (0, 0, 11, 12), view.Frame);
  679. Assert.Equal ("Absolute(0)", view.X.ToString ());
  680. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  681. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  682. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  683. expected = @"
  684. ┌────────────────────┐
  685. │H │
  686. │e │
  687. │l │
  688. │l │
  689. │o │
  690. │ │
  691. │W │
  692. │o │
  693. │r │
  694. │l │
  695. │d │
  696. │s │
  697. │ │
  698. │ │
  699. │ │
  700. │ │
  701. │ │
  702. │ │
  703. │ │
  704. │ │
  705. └────────────────────┘
  706. ";
  707. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  708. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  709. view.AutoSize = false;
  710. view.Height = 1;
  711. Application.Refresh ();
  712. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  713. Assert.Equal ("Absolute(0)", view.X.ToString ());
  714. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  715. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  716. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  717. expected = @"
  718. ┌────────────────────┐
  719. │HelloWorlds │
  720. │ │
  721. │ │
  722. │ │
  723. │ │
  724. │ │
  725. │ │
  726. │ │
  727. │ │
  728. │ │
  729. │ │
  730. │ │
  731. │ │
  732. │ │
  733. │ │
  734. │ │
  735. │ │
  736. │ │
  737. │ │
  738. │ │
  739. └────────────────────┘
  740. ";
  741. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  742. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  743. view.PreserveTrailingSpaces = true;
  744. Application.Refresh ();
  745. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  746. Assert.Equal ("Absolute(0)", view.X.ToString ());
  747. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  748. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  749. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  750. expected = @"
  751. ┌────────────────────┐
  752. │Hello World │
  753. │ │
  754. │ │
  755. │ │
  756. │ │
  757. │ │
  758. │ │
  759. │ │
  760. │ │
  761. │ │
  762. │ │
  763. │ │
  764. │ │
  765. │ │
  766. │ │
  767. │ │
  768. │ │
  769. │ │
  770. │ │
  771. │ │
  772. └────────────────────┘
  773. ";
  774. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  775. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  776. view.PreserveTrailingSpaces = false;
  777. var f = view.Frame;
  778. view.Width = f.Height;
  779. view.Height = f.Width;
  780. view.TextDirection = TextDirection.TopBottom_LeftRight;
  781. Application.Refresh ();
  782. Assert.Equal (new Rect (0, 0, 1, 11), view.Frame);
  783. Assert.Equal ("Absolute(0)", view.X.ToString ());
  784. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  785. Assert.Equal ("Absolute(1)", view.Width.ToString ());
  786. Assert.Equal ("Absolute(11)", view.Height.ToString ());
  787. expected = @"
  788. ┌────────────────────┐
  789. │H │
  790. │e │
  791. │l │
  792. │l │
  793. │o │
  794. │ │
  795. │W │
  796. │o │
  797. │r │
  798. │l │
  799. │d │
  800. │ │
  801. │ │
  802. │ │
  803. │ │
  804. │ │
  805. │ │
  806. │ │
  807. │ │
  808. │ │
  809. └────────────────────┘
  810. ";
  811. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  812. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  813. view.AutoSize = true;
  814. Application.Refresh ();
  815. Assert.Equal (new Rect (0, 0, 1, 12), view.Frame);
  816. Assert.Equal ("Absolute(0)", view.X.ToString ());
  817. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  818. Assert.Equal ("Absolute(1)", view.Width.ToString ());
  819. Assert.Equal ("Absolute(12)", view.Height.ToString ());
  820. expected = @"
  821. ┌────────────────────┐
  822. │H │
  823. │e │
  824. │l │
  825. │l │
  826. │o │
  827. │ │
  828. │W │
  829. │o │
  830. │r │
  831. │l │
  832. │d │
  833. │s │
  834. │ │
  835. │ │
  836. │ │
  837. │ │
  838. │ │
  839. │ │
  840. │ │
  841. │ │
  842. └────────────────────┘
  843. ";
  844. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  845. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  846. Application.End (rs);
  847. }
  848. }