LabelTests.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. using System;
  2. using Xunit;
  3. using Xunit.Abstractions;
  4. namespace Terminal.Gui.ViewsTests {
  5. public class LabelTests {
  6. readonly ITestOutputHelper output;
  7. public LabelTests (ITestOutputHelper output)
  8. {
  9. this.output = output;
  10. }
  11. [Fact, AutoInitShutdown]
  12. public void Constructors_Defaults ()
  13. {
  14. var label = new Label ();
  15. Assert.Equal (string.Empty, label.Text);
  16. Application.Top.Add (label);
  17. var rs = Application.Begin (Application.Top);
  18. Assert.Equal (TextAlignment.Left, label.TextAlignment);
  19. Assert.True (label.AutoSize);
  20. Assert.False (label.CanFocus);
  21. Assert.Equal (new Rect (0, 0, 0, 1), label.Frame);
  22. Assert.Equal (Key.Null, label.HotKey);
  23. var expected = @"";
  24. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  25. Application.End (rs);
  26. label = new Label ("ARGS", true) { Text = "Test" };
  27. Assert.True (label.AutoSize);
  28. Assert.Equal ("Test", label.Text);
  29. Application.Top.Add (label);
  30. rs = Application.Begin (Application.Top);
  31. Assert.Equal ("Test", label.TextFormatter.Text);
  32. Assert.Equal (new Rect (0, 0, 4, 1), label.Frame);
  33. expected = @"
  34. Test
  35. ";
  36. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  37. Application.End (rs);
  38. label = new Label (3, 4, "Test", true);
  39. Assert.Equal ("Test", label.Text);
  40. Application.Top.Add (label);
  41. rs = Application.Begin (Application.Top);
  42. Assert.Equal ("Test", label.TextFormatter.Text);
  43. Assert.Equal (new Rect (3, 4, 4, 1), label.Frame);
  44. expected = @"
  45. Test
  46. ";
  47. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  48. Application.End (rs);
  49. }
  50. [Fact]
  51. public void TestAssignTextToLabel ()
  52. {
  53. View b = new Label () { Text = "heya" };
  54. Assert.Equal ("heya", b.Text);
  55. Assert.True (b.TextFormatter.Text.Contains ("heya"));
  56. b.Text = "heyb";
  57. Assert.Equal ("heyb", b.Text);
  58. Assert.True (b.TextFormatter.Text.Contains ("heyb"));
  59. // with cast
  60. Assert.Equal ("heyb", ((Label)b).Text);
  61. }
  62. [Fact, AutoInitShutdown]
  63. public void Update_Only_On_Or_After_Initialize ()
  64. {
  65. var label = new Label ("Say Hello 你") {
  66. X = Pos.Center (),
  67. Y = Pos.Center ()
  68. };
  69. var win = new Window () {
  70. Width = Dim.Fill (),
  71. Height = Dim.Fill ()
  72. };
  73. win.Add (label);
  74. Application.Top.Add (win);
  75. Assert.False (label.IsInitialized);
  76. Application.Begin (Application.Top);
  77. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  78. Assert.True (label.IsInitialized);
  79. Assert.Equal ("Say Hello 你", label.Text);
  80. Assert.Equal ("Say Hello 你", label.TextFormatter.Text);
  81. Assert.Equal (new Rect (0, 0, 12, 1), label.Bounds);
  82. var expected = @"
  83. ┌────────────────────────────┐
  84. │ │
  85. │ Say Hello 你 │
  86. │ │
  87. └────────────────────────────┘
  88. ";
  89. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  90. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  91. }
  92. [Fact, AutoInitShutdown]
  93. public void Update_Parameterless_Only_On_Or_After_Initialize ()
  94. {
  95. var label = new Label () {
  96. X = Pos.Center (),
  97. Y = Pos.Center (),
  98. Text = "Say Hello 你",
  99. AutoSize = true
  100. };
  101. var win = new Window () {
  102. Width = Dim.Fill (),
  103. Height = Dim.Fill (),
  104. };
  105. win.Add (label);
  106. Application.Top.Add (win);
  107. Assert.False (label.IsInitialized);
  108. Application.Begin (Application.Top);
  109. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  110. Assert.True (label.IsInitialized);
  111. Assert.Equal ("Say Hello 你", label.Text);
  112. Assert.Equal ("Say Hello 你", label.TextFormatter.Text);
  113. Assert.Equal (new Rect (0, 0, 12, 1), label.Bounds);
  114. var expected = @"
  115. ┌────────────────────────────┐
  116. │ │
  117. │ Say Hello 你 │
  118. │ │
  119. └────────────────────────────┘
  120. ";
  121. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  122. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  123. }
  124. [Fact, AutoInitShutdown]
  125. public void AutoSize_Stays_True_With_EmptyText ()
  126. {
  127. var label = new Label () {
  128. X = Pos.Center (),
  129. Y = Pos.Center (),
  130. AutoSize = true
  131. };
  132. var win = new Window () {
  133. Width = Dim.Fill (),
  134. Height = Dim.Fill (),
  135. };
  136. win.Add (label);
  137. Application.Top.Add (win);
  138. Assert.True (label.AutoSize);
  139. label.Text = "Say Hello 你";
  140. Assert.True (label.AutoSize);
  141. Application.Begin (Application.Top);
  142. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  143. var expected = @"
  144. ┌────────────────────────────┐
  145. │ │
  146. │ Say Hello 你 │
  147. │ │
  148. └────────────────────────────┘
  149. ";
  150. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  151. }
  152. [Fact, AutoInitShutdown]
  153. public void AutoSize_Stays_True_Center ()
  154. {
  155. var label = new Label () {
  156. X = Pos.Center (),
  157. Y = Pos.Center (),
  158. Text = "Say Hello 你"
  159. };
  160. var win = new Window () {
  161. Width = Dim.Fill (),
  162. Height = Dim.Fill (),
  163. };
  164. win.Add (label);
  165. Application.Top.Add (win);
  166. Assert.True (label.AutoSize);
  167. Application.Begin (Application.Top);
  168. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  169. var expected = @"
  170. ┌────────────────────────────┐
  171. │ │
  172. │ Say Hello 你 │
  173. │ │
  174. └────────────────────────────┘
  175. ";
  176. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  177. Assert.True (label.AutoSize);
  178. label.Text = "Say Hello 你 changed";
  179. Assert.True (label.AutoSize);
  180. Application.Refresh ();
  181. expected = @"
  182. ┌────────────────────────────┐
  183. │ │
  184. │ Say Hello 你 changed │
  185. │ │
  186. └────────────────────────────┘
  187. ";
  188. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  189. }
  190. [Fact, AutoInitShutdown]
  191. public void AutoSize_Stays_True_AnchorEnd ()
  192. {
  193. var label = new Label () {
  194. Y = Pos.Center (),
  195. Text = "Say Hello 你",
  196. AutoSize = true
  197. };
  198. label.X = Pos.AnchorEnd () - Pos.Function (() => TextFormatter.GetTextWidth (label.TextFormatter.Text));
  199. var win = new Window () {
  200. Width = Dim.Fill (),
  201. Height = Dim.Fill (),
  202. };
  203. win.Add (label);
  204. Application.Top.Add (win);
  205. Assert.True (label.AutoSize);
  206. Application.Begin (Application.Top);
  207. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  208. var expected = @"
  209. ┌────────────────────────────┐
  210. │ │
  211. │ Say Hello 你│
  212. │ │
  213. └────────────────────────────┘
  214. ";
  215. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  216. Assert.True (label.AutoSize);
  217. label.Text = "Say Hello 你 changed";
  218. Assert.True (label.AutoSize);
  219. Application.Refresh ();
  220. expected = @"
  221. ┌────────────────────────────┐
  222. │ │
  223. │ Say Hello 你 changed│
  224. │ │
  225. └────────────────────────────┘
  226. ";
  227. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  228. }
  229. [Fact, AutoInitShutdown]
  230. public void Pos_Center_Layout_AutoSize_True ()
  231. {
  232. var Label = new Label ("012345678901") {
  233. X = Pos.Center (),
  234. Y = Pos.Center (),
  235. };
  236. var win = new Window () {
  237. Width = Dim.Fill (),
  238. Height = Dim.Fill ()
  239. };
  240. win.Add (Label);
  241. Application.Top.Add (win);
  242. Application.Begin (Application.Top);
  243. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  244. Assert.True (Label.AutoSize);
  245. //Assert.Equal (new Rect (5, 1, 18, 1), Label.Frame);
  246. var expected = @"
  247. ┌────────────────────────────┐
  248. │ │
  249. │ 012345678901 │
  250. │ │
  251. └────────────────────────────┘
  252. ";
  253. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  254. }
  255. [Fact, AutoInitShutdown]
  256. public void Pos_Center_Layout_AutoSize_False ()
  257. {
  258. var Label = new Label ("012345678901") {
  259. X = Pos.Center (),
  260. Y = Pos.Center (),
  261. AutoSize = false,
  262. Width = 20,
  263. TextAlignment = TextAlignment.Centered
  264. };
  265. var win = new Window () {
  266. Width = Dim.Fill (),
  267. Height = Dim.Fill ()
  268. };
  269. win.Add (Label);
  270. Application.Top.Add (win);
  271. Application.Begin (Application.Top);
  272. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  273. Assert.False (Label.AutoSize);
  274. Assert.Equal (new Rect (4, 1, 20, 1), Label.Frame);
  275. var expected = @"
  276. ┌────────────────────────────┐
  277. │ │
  278. │ 012345678901 │
  279. │ │
  280. └────────────────────────────┘
  281. ";
  282. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  283. }
  284. //[Fact, AutoInitShutdown]
  285. //public void Label_HotKeyChanged_EventFires ()
  286. //{
  287. // var label = new Label ("Yar");
  288. // object sender = null;
  289. // KeyChangedEventArgs args = null;
  290. // label.HotKeyChanged += (s, e) =>{
  291. // sender = s;
  292. // args = e;
  293. // };
  294. // label.HotKey = Key.r;
  295. // Assert.Same (label, sender);
  296. // Assert.Equal (Key.Y, args.OldKey);
  297. // Assert.Equal (Key.r, args.NewKey);
  298. //}
  299. [Fact, AutoInitShutdown]
  300. public void Label_HotKeyChanged_EventFires_WithNone ()
  301. {
  302. var label = new Label ();
  303. object sender = null;
  304. KeyChangedEventArgs args = null;
  305. label.HotKeyChanged += (s, e) => {
  306. sender = s;
  307. args = e;
  308. };
  309. label.HotKey = Key.r;
  310. Assert.Same (label, sender);
  311. Assert.Equal (Key.Null, args.OldKey);
  312. Assert.Equal (Key.r, args.NewKey);
  313. }
  314. [Fact, AutoInitShutdown]
  315. public void Label_WordWrap_PreserveTrailingSpaces_Horizontal_With_Simple_Runes ()
  316. {
  317. var text = "A sentence has words.";
  318. var width = 3;
  319. var height = 8;
  320. var wrappedLines = TextFormatter.WordWrapText (text, width, true);
  321. var breakLines = "";
  322. foreach (var line in wrappedLines) breakLines += $"{line}{Environment.NewLine}";
  323. var label = new Label (breakLines) { Width = Dim.Fill (), Height = Dim.Fill () };
  324. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  325. frame.Add (label);
  326. Application.Top.Add (frame);
  327. Application.Begin (Application.Top);
  328. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, height + 2);
  329. Assert.True (label.AutoSize);
  330. Assert.Equal (new Rect (0, 0, width, height + 1), label.Frame);
  331. Assert.Equal (new Rect (0, 0, width + 2, height + 2), frame.Frame);
  332. var expected = @"
  333. ┌───┐
  334. │A │
  335. │sen│
  336. │ten│
  337. │ce │
  338. │has│
  339. │ │
  340. │wor│
  341. │ds.│
  342. └───┘
  343. ";
  344. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  345. Assert.Equal (new Rect (0, 0, width + 2, height + 2), pos);
  346. }
  347. [Fact, AutoInitShutdown]
  348. public void Label_WordWrap_PreserveTrailingSpaces_Vertical_With_Simple_Runes ()
  349. {
  350. var text = "A sentence has words.";
  351. var width = 8;
  352. var height = 3;
  353. var wrappedLines = TextFormatter.WordWrapText (text, height, true);
  354. var breakLines = "";
  355. for (int i = 0; i < wrappedLines.Count; i++) breakLines += $"{wrappedLines [i]}{(i < wrappedLines.Count - 1 ? Environment.NewLine : string.Empty)}";
  356. var label = new Label (breakLines) {
  357. TextDirection = TextDirection.TopBottom_LeftRight,
  358. Width = Dim.Fill (),
  359. Height = Dim.Fill ()
  360. };
  361. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  362. frame.Add (label);
  363. Application.Top.Add (frame);
  364. Application.Begin (Application.Top);
  365. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, height + 2);
  366. Assert.True (label.AutoSize);
  367. Assert.Equal (new Rect (0, 0, width, height), label.Frame);
  368. Assert.Equal (new Rect (0, 0, width + 2, height + 2), frame.Frame);
  369. var expected = @"
  370. ┌────────┐
  371. │Astch wd│
  372. │ eeea os│
  373. │ nn s r.│
  374. └────────┘
  375. ";
  376. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  377. Assert.Equal (new Rect (0, 0, width + 2, height + 2), pos);
  378. }
  379. [Fact, AutoInitShutdown]
  380. public void Label_WordWrap_PreserveTrailingSpaces_Horizontal_With_Wide_Runes ()
  381. {
  382. var text = "文に は言葉 があり ます。";
  383. var width = 6;
  384. var height = 8;
  385. var wrappedLines = TextFormatter.WordWrapText (text, width, true);
  386. var breakLines = "";
  387. foreach (var line in wrappedLines) breakLines += $"{line}{Environment.NewLine}";
  388. var label = new Label (breakLines) { Width = Dim.Fill (), Height = Dim.Fill () };
  389. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  390. frame.Add (label);
  391. Application.Top.Add (frame);
  392. Application.Begin (Application.Top);
  393. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, height + 2);
  394. Assert.True (label.AutoSize);
  395. Assert.Equal (new Rect (0, 0, width, height), label.Frame);
  396. Assert.Equal (new Size (width, height), label.TextFormatter.Size);
  397. Assert.Equal (new Rect (0, 0, width + 2, height + 2), frame.Frame);
  398. var expected = @"
  399. ┌──────┐
  400. │文に │
  401. │は言葉│
  402. │ があ │
  403. │り ま │
  404. │す。 │
  405. │ │
  406. │ │
  407. │ │
  408. └──────┘
  409. ";
  410. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  411. Assert.Equal (new Rect (0, 0, width + 2, height + 2), pos);
  412. }
  413. [Fact, AutoInitShutdown]
  414. public void Label_WordWrap_PreserveTrailingSpaces_Vertical_With_Wide_Runes ()
  415. {
  416. var text = "文に は言葉 があり ます。";
  417. var width = 8;
  418. var height = 4;
  419. var wrappedLines = TextFormatter.WordWrapText (text, width, true);
  420. var breakLines = "";
  421. for (int i = 0; i < wrappedLines.Count; i++) breakLines += $"{wrappedLines [i]}{(i < wrappedLines.Count - 1 ? Environment.NewLine : string.Empty)}";
  422. var label = new Label (breakLines) {
  423. TextDirection = TextDirection.TopBottom_LeftRight,
  424. Width = Dim.Fill (),
  425. Height = Dim.Fill ()
  426. };
  427. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  428. frame.Add (label);
  429. Application.Top.Add (frame);
  430. Application.Begin (Application.Top);
  431. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, height + 2);
  432. Assert.True (label.AutoSize);
  433. Assert.Equal (new Rect (0, 0, width, height), label.Frame);
  434. Assert.Equal (new Rect (0, 0, width + 2, height + 2), frame.Frame);
  435. var expected = @"
  436. ┌────────┐
  437. │文言あす│
  438. │に葉り。│
  439. │ │
  440. │はがま │
  441. └────────┘
  442. ";
  443. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  444. Assert.Equal (new Rect (0, 0, width + 2, height + 2), pos);
  445. }
  446. [Fact, AutoInitShutdown]
  447. public void Label_Draw_Horizontal_Simple_TextAlignments_Justified ()
  448. {
  449. var text = "01234 01234";
  450. var width = 20;
  451. var lblJust = new Label (text) { Y = 0, Width = width, TextAlignment = TextAlignment.Justified };
  452. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  453. frame.Add (lblJust);
  454. Application.Top.Add (frame);
  455. Application.Begin (Application.Top);
  456. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 3);
  457. var expected = @"
  458. ┌────────────────────┐
  459. │01234 01234│
  460. └────────────────────┘
  461. ";
  462. Assert.Equal (new Rect (0, 0, width, 1), lblJust.Frame);
  463. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  464. Assert.Equal (new Rect (0, 0, width + 2, 3), pos);
  465. }
  466. [Fact, AutoInitShutdown]
  467. public void Label_Draw_Horizontal_Simple_Runes ()
  468. {
  469. var label = new Label ("Demo Simple Rune");
  470. Application.Top.Add (label);
  471. Application.Begin (Application.Top);
  472. Assert.True (label.AutoSize);
  473. Assert.Equal (new Rect (0, 0, 16, 1), label.Frame);
  474. var expected = @"
  475. Demo Simple Rune
  476. ";
  477. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  478. Assert.Equal (new Rect (0, 0, 16, 1), pos);
  479. }
  480. [Fact, AutoInitShutdown]
  481. public void Label_Draw_Vertical_Simple_Runes ()
  482. {
  483. var label = new Label ("Demo Simple Rune") {
  484. TextDirection = TextDirection.TopBottom_LeftRight
  485. };
  486. Application.Top.Add (label);
  487. Application.Begin (Application.Top);
  488. Assert.NotNull (label.Width);
  489. Assert.NotNull (label.Height);
  490. var expected = @"
  491. D
  492. e
  493. m
  494. o
  495. S
  496. i
  497. m
  498. p
  499. l
  500. e
  501. R
  502. u
  503. n
  504. e
  505. ";
  506. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  507. Assert.Equal (new Rect (0, 0, 1, 16), pos);
  508. }
  509. [Fact, AutoInitShutdown]
  510. public void Label_Draw_Horizontal_Wide_Runes ()
  511. {
  512. var label = new Label ("デモエムポンズ");
  513. Application.Top.Add (label);
  514. Application.Begin (Application.Top);
  515. Assert.True (label.AutoSize);
  516. Assert.Equal (new Rect (0, 0, 14, 1), label.Frame);
  517. var expected = @"
  518. デモエムポンズ
  519. ";
  520. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  521. Assert.Equal (new Rect (0, 0, 14, 1), pos);
  522. }
  523. [Fact, AutoInitShutdown]
  524. public void Label_Draw_Vertical_Wide_Runes ()
  525. {
  526. var label = new Label ("デモエムポンズ") {
  527. TextDirection = TextDirection.TopBottom_LeftRight
  528. };
  529. Application.Top.Add (label);
  530. Application.Begin (Application.Top);
  531. var expected = @"
  532. ";
  533. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  534. Assert.Equal (new Rect (0, 0, 2, 7), pos);
  535. }
  536. [Fact, AutoInitShutdown]
  537. public void Label_Draw_Vertical_Wide_Runes_With_ForceValidatePosDim ()
  538. {
  539. var label = new Label ("デモエムポンズ") {
  540. Width = Dim.Fill (),
  541. Height = Dim.Percent (50f),
  542. TextDirection = TextDirection.TopBottom_LeftRight,
  543. ForceValidatePosDim = true
  544. };
  545. Application.Top.Add (label);
  546. Application.Begin (Application.Top);
  547. Assert.True (label.AutoSize);
  548. Assert.Equal (new Rect (0, 0, 80, 12), label.Frame);
  549. var expected = @"
  550. ";
  551. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  552. Assert.Equal (new Rect (0, 0, 2, 7), pos);
  553. }
  554. [Fact, AutoInitShutdown]
  555. public void Label_Draw_Horizontal_Simple_TextAlignments ()
  556. {
  557. var text = "Hello World";
  558. var width = 20;
  559. var lblLeft = new Label (text) { Width = width };
  560. var lblCenter = new Label (text) { Y = 1, Width = width, TextAlignment = TextAlignment.Centered };
  561. var lblRight = new Label (text) { Y = 2, Width = width, TextAlignment = TextAlignment.Right };
  562. var lblJust = new Label (text) { Y = 3, Width = width, TextAlignment = TextAlignment.Justified };
  563. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  564. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  565. Application.Top.Add (frame);
  566. Application.Begin (Application.Top);
  567. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 6);
  568. Assert.True (lblLeft.AutoSize);
  569. Assert.True (lblCenter.AutoSize);
  570. Assert.True (lblRight.AutoSize);
  571. Assert.True (lblJust.AutoSize);
  572. Assert.Equal (new Rect (0, 0, width, 1), lblLeft.Frame);
  573. Assert.Equal (new Rect (0, 1, width, 1), lblCenter.Frame);
  574. Assert.Equal (new Rect (0, 2, width, 1), lblRight.Frame);
  575. Assert.Equal (new Rect (0, 3, width, 1), lblJust.Frame);
  576. Assert.Equal (new Rect (0, 0, width + 2, 6), frame.Frame);
  577. var expected = @"
  578. ┌────────────────────┐
  579. │Hello World │
  580. │ Hello World │
  581. │ Hello World│
  582. │Hello World│
  583. └────────────────────┘
  584. ";
  585. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  586. Assert.Equal (new Rect (0, 0, width + 2, 6), pos);
  587. }
  588. [Fact, AutoInitShutdown]
  589. public void Label_Draw_Vertical_Simple_TextAlignments ()
  590. {
  591. var text = "Hello World";
  592. var height = 20;
  593. var lblLeft = new Label (text, direction: TextDirection.TopBottom_LeftRight) { Height = height };
  594. var lblCenter = new Label (text, direction: TextDirection.TopBottom_LeftRight) { X = 2, Height = height, VerticalTextAlignment = VerticalTextAlignment.Middle };
  595. var lblRight = new Label (text, direction: TextDirection.TopBottom_LeftRight) { X = 4, Height = height, VerticalTextAlignment = VerticalTextAlignment.Bottom };
  596. var lblJust = new Label (text, direction: TextDirection.TopBottom_LeftRight) { X = 6, Height = height, VerticalTextAlignment = VerticalTextAlignment.Justified };
  597. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  598. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  599. Application.Top.Add (frame);
  600. Application.Begin (Application.Top);
  601. ((FakeDriver)Application.Driver).SetBufferSize (9, height + 2);
  602. Assert.True (lblLeft.AutoSize);
  603. Assert.True (lblCenter.AutoSize);
  604. Assert.True (lblRight.AutoSize);
  605. Assert.True (lblJust.AutoSize);
  606. Assert.Equal (new Rect (0, 0, 1, height), lblLeft.Frame);
  607. Assert.Equal (new Rect (2, 0, 1, height), lblCenter.Frame);
  608. Assert.Equal (new Rect (4, 0, 1, height), lblRight.Frame);
  609. Assert.Equal (new Rect (6, 0, 1, height), lblJust.Frame);
  610. Assert.Equal (new Rect (0, 0, 9, height + 2), frame.Frame);
  611. var expected = @"
  612. ┌───────┐
  613. │H H│
  614. │e e│
  615. │l l│
  616. │l l│
  617. │o H o│
  618. │ e │
  619. │W l │
  620. │o l │
  621. │r o │
  622. │l H │
  623. │d W e │
  624. │ o l │
  625. │ r l │
  626. │ l o │
  627. │ d │
  628. │ W W│
  629. │ o o│
  630. │ r r│
  631. │ l l│
  632. │ d d│
  633. └───────┘
  634. ";
  635. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  636. Assert.Equal (new Rect (0, 0, 9, height + 2), pos);
  637. }
  638. [Fact, AutoInitShutdown]
  639. public void Label_Draw_Horizontal_Wide_TextAlignments ()
  640. {
  641. var text = "こんにちは 世界";
  642. var width = 25;
  643. var lblLeft = new Label (text) { Width = width };
  644. var lblCenter = new Label (text) { Y = 1, Width = width, TextAlignment = TextAlignment.Centered };
  645. var lblRight = new Label (text) { Y = 2, Width = width, TextAlignment = TextAlignment.Right };
  646. var lblJust = new Label (text) { Y = 3, Width = width, TextAlignment = TextAlignment.Justified };
  647. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  648. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  649. Application.Top.Add (frame);
  650. Application.Begin (Application.Top);
  651. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 6);
  652. Assert.True (lblLeft.AutoSize);
  653. Assert.True (lblCenter.AutoSize);
  654. Assert.True (lblRight.AutoSize);
  655. Assert.True (lblJust.AutoSize);
  656. Assert.Equal (new Rect (0, 0, width, 1), lblLeft.Frame);
  657. Assert.Equal (new Rect (0, 1, width, 1), lblCenter.Frame);
  658. Assert.Equal (new Rect (0, 2, width, 1), lblRight.Frame);
  659. Assert.Equal (new Rect (0, 3, width, 1), lblJust.Frame);
  660. Assert.Equal (new Rect (0, 0, width + 2, 6), frame.Frame);
  661. var expected = @"
  662. ┌─────────────────────────┐
  663. │こんにちは 世界 │
  664. │ こんにちは 世界 │
  665. │ こんにちは 世界│
  666. │こんにちは 世界│
  667. └─────────────────────────┘
  668. ";
  669. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  670. Assert.Equal (new Rect (0, 0, width + 2, 6), pos);
  671. }
  672. [Fact, AutoInitShutdown]
  673. public void Label_Draw_Vertical_Wide_TextAlignments ()
  674. {
  675. var text = "こんにちは 世界";
  676. var height = 23;
  677. var lblLeft = new Label (text) { Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight };
  678. var lblCenter = new Label (text) { X = 3, Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Middle };
  679. var lblRight = new Label (text) { X = 6, Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Bottom };
  680. var lblJust = new Label (text) { X = 9, Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Justified };
  681. var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
  682. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  683. Application.Top.Add (frame);
  684. Application.Begin (Application.Top);
  685. ((FakeDriver)Application.Driver).SetBufferSize (13, height + 2);
  686. // All AutoSize are false because the Frame.Height != TextFormatter.Size.Height
  687. Assert.True (lblLeft.AutoSize);
  688. Assert.True (lblCenter.AutoSize);
  689. Assert.True (lblRight.AutoSize);
  690. Assert.True (lblJust.AutoSize);
  691. Assert.Equal (new Rect (0, 0, 2, height), lblLeft.Frame);
  692. Assert.Equal (new Rect (3, 0, 2, height), lblCenter.Frame);
  693. Assert.Equal (new Rect (6, 0, 2, height), lblRight.Frame);
  694. Assert.Equal (new Rect (9, 0, 2, height), lblJust.Frame);
  695. Assert.Equal (new Rect (0, 0, 13, height + 2), frame.Frame);
  696. var expected = @"
  697. ┌───────────┐
  698. │こ こ│
  699. │ん ん│
  700. │に に│
  701. │ち ち│
  702. │は は│
  703. │ │
  704. │世 │
  705. │界 こ │
  706. │ ん │
  707. │ に │
  708. │ ち │
  709. │ は │
  710. │ │
  711. │ 世 │
  712. │ 界 │
  713. │ こ │
  714. │ ん │
  715. │ に │
  716. │ ち │
  717. │ は │
  718. │ │
  719. │ 世 世│
  720. │ 界 界│
  721. └───────────┘
  722. ";
  723. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  724. Assert.Equal (new Rect (0, 0, 13, height + 2), pos);
  725. }
  726. [Fact, AutoInitShutdown]
  727. public void Label_Draw_Fill_Remaining ()
  728. {
  729. var view = new View ("This view needs to be cleared before rewritten.");
  730. var tf1 = new TextFormatter ();
  731. tf1.Text = "This TextFormatter (tf1) without fill will not be cleared on rewritten.";
  732. var tf1Size = tf1.Size;
  733. var tf2 = new TextFormatter ();
  734. tf2.Text = "This TextFormatter (tf2) with fill will be cleared on rewritten.";
  735. var tf2Size = tf2.Size;
  736. Application.Top.Add (view);
  737. Application.Begin (Application.Top);
  738. tf1.Draw (new Rect (new Point (0, 1), tf1Size), view.GetNormalColor (), view.ColorScheme.HotNormal, default, false);
  739. tf2.Draw (new Rect (new Point (0, 2), tf2Size), view.GetNormalColor (), view.ColorScheme.HotNormal);
  740. TestHelpers.AssertDriverContentsWithFrameAre (@"
  741. This view needs to be cleared before rewritten.
  742. This TextFormatter (tf1) without fill will not be cleared on rewritten.
  743. This TextFormatter (tf2) with fill will be cleared on rewritten.
  744. ", output);
  745. view.Text = "This view is rewritten.";
  746. view.Draw ();
  747. tf1.Text = "This TextFormatter (tf1) is rewritten.";
  748. tf1.Draw (new Rect (new Point (0, 1), tf1Size), view.GetNormalColor (), view.ColorScheme.HotNormal, default, false);
  749. tf2.Text = "This TextFormatter (tf2) is rewritten.";
  750. tf2.Draw (new Rect (new Point (0, 2), tf2Size), view.GetNormalColor (), view.ColorScheme.HotNormal);
  751. TestHelpers.AssertDriverContentsWithFrameAre (@"
  752. This view is rewritten.
  753. This TextFormatter (tf1) is rewritten.will not be cleared on rewritten.
  754. This TextFormatter (tf2) is rewritten.
  755. ", output);
  756. }
  757. }
  758. }