ViewTests.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  1. using System.ComponentModel;
  2. using System.Text;
  3. using Xunit.Abstractions;
  4. namespace Terminal.Gui.ViewTests;
  5. public class ViewTests (ITestOutputHelper output)
  6. {
  7. [Fact]
  8. [AutoInitShutdown]
  9. public void Clear_Viewport_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  10. {
  11. var view = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  12. view.DrawContent += (s, e) =>
  13. {
  14. Rectangle savedClip = Application.Driver.Clip;
  15. Application.Driver.Clip = new (1, 1, view.Viewport.Width, view.Viewport.Height);
  16. for (var row = 0; row < view.Viewport.Height; row++)
  17. {
  18. Application.Driver.Move (1, row + 1);
  19. for (var col = 0; col < view.Viewport.Width; col++)
  20. {
  21. Application.Driver.AddStr ($"{col}");
  22. }
  23. }
  24. Application.Driver.Clip = savedClip;
  25. e.Cancel = true;
  26. };
  27. var top = new Toplevel ();
  28. top.Add (view);
  29. Application.Begin (top);
  30. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  31. var expected = @"
  32. ┌──────────────────┐
  33. │012345678910111213│
  34. │012345678910111213│
  35. │012345678910111213│
  36. │012345678910111213│
  37. │012345678910111213│
  38. │012345678910111213│
  39. │012345678910111213│
  40. │012345678910111213│
  41. └──────────────────┘
  42. ";
  43. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  44. Assert.Equal (new (0, 0, 20, 10), pos);
  45. view.FillRect (view.Viewport);
  46. expected = @"
  47. ┌──────────────────┐
  48. │ │
  49. │ │
  50. │ │
  51. │ │
  52. │ │
  53. │ │
  54. │ │
  55. │ │
  56. └──────────────────┘
  57. ";
  58. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  59. top.Dispose ();
  60. }
  61. [Fact]
  62. [AutoInitShutdown]
  63. public void Clear_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  64. {
  65. var view = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  66. view.DrawContent += (s, e) =>
  67. {
  68. Rectangle savedClip = Application.Driver.Clip;
  69. Application.Driver.Clip = new (1, 1, view.Viewport.Width, view.Viewport.Height);
  70. for (var row = 0; row < view.Viewport.Height; row++)
  71. {
  72. Application.Driver.Move (1, row + 1);
  73. for (var col = 0; col < view.Viewport.Width; col++)
  74. {
  75. Application.Driver.AddStr ($"{col}");
  76. }
  77. }
  78. Application.Driver.Clip = savedClip;
  79. e.Cancel = true;
  80. };
  81. var top = new Toplevel ();
  82. top.Add (view);
  83. Application.Begin (top);
  84. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  85. var expected = @"
  86. ┌──────────────────┐
  87. │012345678910111213│
  88. │012345678910111213│
  89. │012345678910111213│
  90. │012345678910111213│
  91. │012345678910111213│
  92. │012345678910111213│
  93. │012345678910111213│
  94. │012345678910111213│
  95. └──────────────────┘
  96. ";
  97. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  98. Assert.Equal (new (0, 0, 20, 10), pos);
  99. view.FillRect (view.Viewport);
  100. expected = @"
  101. ┌──────────────────┐
  102. │ │
  103. │ │
  104. │ │
  105. │ │
  106. │ │
  107. │ │
  108. │ │
  109. │ │
  110. └──────────────────┘
  111. ";
  112. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  113. top.Dispose ();
  114. }
  115. [Theory]
  116. [AutoInitShutdown]
  117. [InlineData (true)]
  118. [InlineData (false)]
  119. public void Clear_Does_Not_Spillover_Its_Parent (bool label)
  120. {
  121. var root = new View { Width = 20, Height = 10, ColorScheme = Colors.ColorSchemes ["Base"] };
  122. string text = new ('c', 100);
  123. View v = label
  124. // Label has Width/Height == AutoSize, so Frame.Size will be (100, 1)
  125. ? new Label { Text = text }
  126. // TextView has Width/Height == (Dim.Fill, 1), so Frame.Size will be 20 (width of root), 1
  127. : new TextView { Width = Dim.Fill (), Height = 1, Text = text };
  128. root.Add (v);
  129. var top = new Toplevel ();
  130. top.Add (root);
  131. RunState runState = Application.Begin (top);
  132. if (label)
  133. {
  134. Assert.False (v.CanFocus);
  135. Assert.Equal (new (0, 0, text.Length, 1), v.Frame);
  136. }
  137. else
  138. {
  139. Assert.True (v.CanFocus);
  140. Assert.Equal (new (0, 0, 20, 1), v.Frame);
  141. }
  142. TestHelpers.AssertDriverContentsWithFrameAre (
  143. @"
  144. cccccccccccccccccccc",
  145. output
  146. );
  147. Attribute [] attributes =
  148. {
  149. Colors.ColorSchemes ["TopLevel"].Normal,
  150. Colors.ColorSchemes ["Base"].Normal,
  151. Colors.ColorSchemes ["Base"].Focus
  152. };
  153. if (label)
  154. {
  155. TestHelpers.AssertDriverAttributesAre (
  156. @"
  157. 111111111111111111110
  158. 111111111111111111110",
  159. Application.Driver,
  160. attributes
  161. );
  162. }
  163. else
  164. {
  165. TestHelpers.AssertDriverAttributesAre (
  166. @"
  167. 222222222222222222220
  168. 111111111111111111110",
  169. Application.Driver,
  170. attributes
  171. );
  172. }
  173. if (label)
  174. {
  175. root.CanFocus = true;
  176. v.CanFocus = true;
  177. Assert.False (v.HasFocus);
  178. v.SetFocus ();
  179. Assert.True (v.HasFocus);
  180. Application.Refresh ();
  181. TestHelpers.AssertDriverAttributesAre (
  182. @"
  183. 222222222222222222220
  184. 111111111111111111110",
  185. Application.Driver,
  186. attributes
  187. );
  188. }
  189. Application.End (runState);
  190. top.Dispose ();
  191. }
  192. [Fact]
  193. [AutoInitShutdown]
  194. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  195. {
  196. var label = new Label { Text = "At 0,0" };
  197. var view = new DerivedView
  198. {
  199. X = 2,
  200. Y = 2,
  201. Width = 30,
  202. Height = 2,
  203. Text = "A text with some long width\n and also with two lines."
  204. };
  205. Toplevel top = new ();
  206. top.Add (label, view);
  207. RunState runState = Application.Begin (top);
  208. TestHelpers.AssertDriverContentsWithFrameAre (
  209. @"
  210. At 0,0
  211. A text with some long width
  212. and also with two lines. ",
  213. output
  214. );
  215. view.Frame = new (3, 3, 10, 1);
  216. Assert.Equal (new (3, 3, 10, 1), view.Frame);
  217. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  218. Assert.Equal (new (0, 0, 10, 1), view._needsDisplayRect);
  219. top.Draw ();
  220. TestHelpers.AssertDriverContentsWithFrameAre (
  221. @"
  222. At 0,0
  223. A text wit",
  224. output
  225. );
  226. Application.End (runState);
  227. top.Dispose ();
  228. }
  229. [Fact]
  230. [AutoInitShutdown]
  231. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  232. {
  233. var label = new Label { Text = "At 0,0" };
  234. var view = new DerivedView
  235. {
  236. X = 2,
  237. Y = 2,
  238. Width = 30,
  239. Height = 2,
  240. Text = "A text with some long width\n and also with two lines."
  241. };
  242. Toplevel top = new ();
  243. top.Add (label, view);
  244. RunState runState = Application.Begin (top);
  245. top.Draw ();
  246. TestHelpers.AssertDriverContentsWithFrameAre (
  247. @"
  248. At 0,0
  249. A text with some long width
  250. and also with two lines. ",
  251. output
  252. );
  253. view.X = 3;
  254. view.Y = 3;
  255. view.Width = 10;
  256. view.Height = 1;
  257. Assert.Equal (new (3, 3, 10, 1), view.Frame);
  258. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  259. Assert.Equal (new (0, 0, 30, 2), view._needsDisplayRect);
  260. top.Draw ();
  261. TestHelpers.AssertDriverContentsWithFrameAre (
  262. @"
  263. At 0,0
  264. A text wit",
  265. output
  266. );
  267. Application.End (runState);
  268. top.Dispose ();
  269. }
  270. [Fact]
  271. [AutoInitShutdown]
  272. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  273. {
  274. var label = new Label { Text = "At 0,0" };
  275. var view = new DerivedView
  276. {
  277. X = 2,
  278. Y = 2,
  279. Width = 30,
  280. Height = 2,
  281. Text = "A text with some long width\n and also with two lines."
  282. };
  283. Toplevel top = new ();
  284. top.Add (label, view);
  285. RunState runState = Application.Begin (top);
  286. top.Draw ();
  287. TestHelpers.AssertDriverContentsWithFrameAre (
  288. @"
  289. At 0,0
  290. A text with some long width
  291. and also with two lines. ",
  292. output
  293. );
  294. view.Frame = new (1, 1, 10, 1);
  295. Assert.Equal (new (1, 1, 10, 1), view.Frame);
  296. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  297. Assert.Equal (new (0, 0, 10, 1), view._needsDisplayRect);
  298. top.Draw ();
  299. TestHelpers.AssertDriverContentsWithFrameAre (
  300. @"
  301. At 0,0
  302. A text wit",
  303. output
  304. );
  305. Application.End (runState);
  306. top.Dispose ();
  307. }
  308. [Fact]
  309. [AutoInitShutdown]
  310. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  311. {
  312. var label = new Label { Text = "At 0,0" };
  313. var view = new DerivedView
  314. {
  315. X = 2,
  316. Y = 2,
  317. Width = 30,
  318. Height = 2,
  319. Text = "A text with some long width\n and also with two lines."
  320. };
  321. Toplevel top = new ();
  322. top.Add (label, view);
  323. RunState runState = Application.Begin (top);
  324. top.Draw ();
  325. TestHelpers.AssertDriverContentsWithFrameAre (
  326. @"
  327. At 0,0
  328. A text with some long width
  329. and also with two lines. ",
  330. output
  331. );
  332. view.X = 1;
  333. view.Y = 1;
  334. view.Width = 10;
  335. view.Height = 1;
  336. Assert.Equal (new (1, 1, 10, 1), view.Frame);
  337. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  338. Assert.Equal (new (0, 0, 30, 2), view._needsDisplayRect);
  339. top.Draw ();
  340. TestHelpers.AssertDriverContentsWithFrameAre (
  341. @"
  342. At 0,0
  343. A text wit",
  344. output
  345. );
  346. Application.End (runState);
  347. top.Dispose ();
  348. }
  349. [Fact]
  350. [TestRespondersDisposed]
  351. public void Dispose_View ()
  352. {
  353. var view = new View ();
  354. Assert.NotNull (view.Margin);
  355. Assert.NotNull (view.Border);
  356. Assert.NotNull (view.Padding);
  357. #if DEBUG_IDISPOSABLE
  358. Assert.Equal (4, Responder.Instances.Count);
  359. #endif
  360. view.Dispose ();
  361. Assert.Null (view.Margin);
  362. Assert.Null (view.Border);
  363. Assert.Null (view.Padding);
  364. }
  365. [Fact]
  366. [AutoInitShutdown]
  367. public void DrawContentComplete_Event_Is_Always_Called ()
  368. {
  369. var viewCalled = false;
  370. var tvCalled = false;
  371. var view = new View { Width = 10, Height = 10, Text = "View" };
  372. view.DrawContentComplete += (s, e) => viewCalled = true;
  373. var tv = new TextView { Y = 11, Width = 10, Height = 10 };
  374. tv.DrawContentComplete += (s, e) => tvCalled = true;
  375. var top = new Toplevel ();
  376. top.Add (view, tv);
  377. Application.Begin (top);
  378. Assert.True (viewCalled);
  379. Assert.True (tvCalled);
  380. top.Dispose ();
  381. }
  382. [Fact]
  383. [AutoInitShutdown]
  384. public void Frame_Set_After_Initialize_Update_NeededDisplay ()
  385. {
  386. var frame = new FrameView ();
  387. var label = new Label
  388. {
  389. ColorScheme = Colors.ColorSchemes ["Menu"], X = 0, Y = 0, Text = "This should be the first line."
  390. };
  391. var view = new View
  392. {
  393. X = 0, // don't overcomplicate unit tests
  394. Y = 1,
  395. Height = Dim.Auto (DimAutoStyle.Text),
  396. Width = Dim.Auto(DimAutoStyle.Text),
  397. Text = "Press me!"
  398. };
  399. frame.Add (label, view);
  400. frame.X = Pos.Center ();
  401. frame.Y = Pos.Center ();
  402. frame.Width = 40;
  403. frame.Height = 8;
  404. Toplevel top = new ();
  405. top.Add (frame);
  406. RunState runState = Application.Begin (top);
  407. top.LayoutComplete += (s, e) => { Assert.Equal (new (0, 0, 80, 25), top._needsDisplayRect); };
  408. frame.LayoutComplete += (s, e) => { Assert.Equal (new (0, 0, 40, 8), frame._needsDisplayRect); };
  409. label.LayoutComplete += (s, e) => { Assert.Equal (new (0, 0, 38, 1), label._needsDisplayRect); };
  410. view.LayoutComplete += (s, e) => { Assert.Equal (new (0, 0, 13, 1), view._needsDisplayRect); };
  411. Assert.Equal (new (0, 0, 80, 25), top.Frame);
  412. Assert.Equal (new (20, 8, 40, 8), frame.Frame);
  413. Assert.Equal (
  414. new (20, 8, 60, 16),
  415. new Rectangle (
  416. frame.Frame.Left,
  417. frame.Frame.Top,
  418. frame.Frame.Right,
  419. frame.Frame.Bottom
  420. )
  421. );
  422. Assert.Equal (new (0, 0, 30, 1), label.Frame);
  423. Assert.Equal (new (0, 1, 9, 1), view.Frame); // this proves frame was set
  424. Application.End (runState);
  425. top.Dispose ();
  426. }
  427. [Fact]
  428. public void GetHotNormalColor_ColorScheme ()
  429. {
  430. var view = new View { ColorScheme = Colors.ColorSchemes ["Base"] };
  431. Assert.Equal (view.ColorScheme.HotNormal, view.GetHotNormalColor ());
  432. view.Enabled = false;
  433. Assert.Equal (view.ColorScheme.Disabled, view.GetHotNormalColor ());
  434. view.Dispose ();
  435. }
  436. [Fact]
  437. public void GetNormalColor_ColorScheme ()
  438. {
  439. var view = new View { ColorScheme = Colors.ColorSchemes ["Base"] };
  440. Assert.Equal (view.ColorScheme.Normal, view.GetNormalColor ());
  441. view.Enabled = false;
  442. Assert.Equal (view.ColorScheme.Disabled, view.GetNormalColor ());
  443. view.Dispose ();
  444. }
  445. [Fact]
  446. [AutoInitShutdown]
  447. public void Incorrect_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  448. {
  449. var label = new Label { Text = "At 0,0" };
  450. var view = new DerivedView
  451. {
  452. X = 2,
  453. Y = 2,
  454. Width = 30,
  455. Height = 2,
  456. Text = "A text with some long width\n and also with two lines."
  457. };
  458. Toplevel top = new ();
  459. top.Add (label, view);
  460. RunState runState = Application.Begin (top);
  461. view.Draw ();
  462. TestHelpers.AssertDriverContentsWithFrameAre (
  463. @"
  464. At 0,0
  465. A text with some long width
  466. and also with two lines. ",
  467. output
  468. );
  469. view.Frame = new (3, 3, 10, 1);
  470. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  471. Assert.Equal (new (0, 0, 10, 1), view._needsDisplayRect);
  472. view.Draw ();
  473. TestHelpers.AssertDriverContentsWithFrameAre (
  474. @"
  475. At 0,0
  476. A text with some long width
  477. A text witith two lines. ",
  478. output
  479. );
  480. Application.End (runState);
  481. top.Dispose ();
  482. }
  483. [Fact]
  484. [AutoInitShutdown]
  485. public void Incorrect_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  486. {
  487. var label = new Label { Text = "At 0,0" };
  488. var view = new DerivedView
  489. {
  490. X = 2,
  491. Y = 2,
  492. Width = 30,
  493. Height = 2,
  494. Text = "A text with some long width\n and also with two lines."
  495. };
  496. Toplevel top = new ();
  497. top.Add (label, view);
  498. RunState runState = Application.Begin (top);
  499. view.Draw ();
  500. TestHelpers.AssertDriverContentsWithFrameAre (
  501. @"
  502. At 0,0
  503. A text with some long width
  504. and also with two lines. ",
  505. output
  506. );
  507. view.X = 3;
  508. view.Y = 3;
  509. view.Width = 10;
  510. view.Height = 1;
  511. Assert.Equal (new (3, 3, 10, 1), view.Frame);
  512. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  513. Assert.Equal (new (0, 0, 30, 2), view._needsDisplayRect);
  514. view.Draw ();
  515. TestHelpers.AssertDriverContentsWithFrameAre (
  516. @"
  517. At 0,0
  518. A text with some long width
  519. A text witith two lines. ",
  520. output
  521. );
  522. Application.End (runState);
  523. top.Dispose ();
  524. }
  525. [Fact]
  526. [AutoInitShutdown]
  527. public void Incorrect_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  528. {
  529. var label = new Label { Text = "At 0,0" };
  530. var view = new DerivedView
  531. {
  532. X = 2,
  533. Y = 2,
  534. Width = 30,
  535. Height = 2,
  536. Text = "A text with some long width\n and also with two lines."
  537. };
  538. Toplevel top = new ();
  539. top.Add (label, view);
  540. RunState runState = Application.Begin (top);
  541. view.Draw ();
  542. TestHelpers.AssertDriverContentsWithFrameAre (
  543. @"
  544. At 0,0
  545. A text with some long width
  546. and also with two lines. ",
  547. output
  548. );
  549. view.Frame = new (1, 1, 10, 1);
  550. Assert.Equal (new (1, 1, 10, 1), view.Frame);
  551. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  552. Assert.Equal (new (0, 0, 10, 1), view._needsDisplayRect);
  553. view.Draw ();
  554. TestHelpers.AssertDriverContentsWithFrameAre (
  555. @"
  556. At 0,0
  557. A text wit
  558. A text with some long width
  559. and also with two lines. ",
  560. output
  561. );
  562. Application.End (runState);
  563. top.Dispose ();
  564. }
  565. [Fact]
  566. [AutoInitShutdown]
  567. public void Incorrect_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  568. {
  569. var label = new Label { Text = "At 0,0" };
  570. var view = new DerivedView
  571. {
  572. X = 2,
  573. Y = 2,
  574. Width = 30,
  575. Height = 2,
  576. Text = "A text with some long width\n and also with two lines."
  577. };
  578. Toplevel top = new ();
  579. top.Add (label, view);
  580. RunState runState = Application.Begin (top);
  581. view.Draw ();
  582. TestHelpers.AssertDriverContentsWithFrameAre (
  583. @"
  584. At 0,0
  585. A text with some long width
  586. and also with two lines. ",
  587. output
  588. );
  589. view.X = 1;
  590. view.Y = 1;
  591. view.Width = 10;
  592. view.Height = 1;
  593. Assert.Equal (new (1, 1, 10, 1), view.Frame);
  594. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  595. Assert.Equal (new (0, 0, 30, 2), view._needsDisplayRect);
  596. view.Draw ();
  597. TestHelpers.AssertDriverContentsWithFrameAre (
  598. @"
  599. At 0,0
  600. A text wit
  601. A text with some long width
  602. and also with two lines. ",
  603. output
  604. );
  605. Application.End (runState);
  606. top.Dispose ();
  607. }
  608. [Fact]
  609. public void Internal_Tests ()
  610. {
  611. var rect = new Rectangle (1, 1, 10, 1);
  612. var view = new View { Frame = rect };
  613. }
  614. [Fact]
  615. [SetupFakeDriver]
  616. public void SetText_RendersCorrectly ()
  617. {
  618. View view;
  619. var text = "test";
  620. view = new Label { Text = text };
  621. view.BeginInit ();
  622. view.EndInit ();
  623. view.Draw ();
  624. TestHelpers.AssertDriverContentsWithFrameAre (text, output);
  625. }
  626. [Fact]
  627. [TestRespondersDisposed]
  628. public void New_Initializes ()
  629. {
  630. // Parameterless
  631. var r = new View ();
  632. Assert.NotNull (r);
  633. Assert.True (r.Enabled);
  634. Assert.True (r.Visible);
  635. Assert.Equal ($"View(){r.Viewport}", r.ToString ());
  636. Assert.False (r.CanFocus);
  637. Assert.False (r.HasFocus);
  638. Assert.Equal (new (0, 0, 0, 0), r.Viewport);
  639. Assert.Equal (new (0, 0, 0, 0), r.Frame);
  640. Assert.Null (r.Focused);
  641. Assert.Null (r.ColorScheme);
  642. Assert.Equal (0, r.Width);
  643. Assert.Equal (0, r.Height);
  644. Assert.Equal (0, r.X);
  645. Assert.Equal (0, r.Y);
  646. Assert.False (r.IsCurrentTop);
  647. Assert.Empty (r.Id);
  648. Assert.Empty (r.Subviews);
  649. Assert.False (r.WantContinuousButtonPressed);
  650. Assert.False (r.WantMousePositionReports);
  651. Assert.Null (r.SuperView);
  652. Assert.Null (r.MostFocused);
  653. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  654. r.Dispose ();
  655. // Empty Rect
  656. r = new() { Frame = Rectangle.Empty };
  657. Assert.NotNull (r);
  658. Assert.Equal ($"View(){r.Viewport}", r.ToString ());
  659. Assert.False (r.CanFocus);
  660. Assert.False (r.HasFocus);
  661. Assert.Equal (new (0, 0, 0, 0), r.Viewport);
  662. Assert.Equal (new (0, 0, 0, 0), r.Frame);
  663. Assert.Null (r.Focused);
  664. Assert.Null (r.ColorScheme);
  665. Assert.Equal (0, r.Width);
  666. Assert.Equal (0, r.Height);
  667. Assert.Equal (0, r.X);
  668. Assert.Equal (0, r.Y);
  669. Assert.False (r.IsCurrentTop);
  670. Assert.Empty (r.Id);
  671. Assert.Empty (r.Subviews);
  672. Assert.False (r.WantContinuousButtonPressed);
  673. Assert.False (r.WantMousePositionReports);
  674. Assert.Null (r.SuperView);
  675. Assert.Null (r.MostFocused);
  676. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  677. r.Dispose ();
  678. // Rect with values
  679. r = new() { Frame = new (1, 2, 3, 4) };
  680. Assert.NotNull (r);
  681. Assert.Equal ($"View(){r.Frame}", r.ToString ());
  682. Assert.False (r.CanFocus);
  683. Assert.False (r.HasFocus);
  684. Assert.Equal (new (0, 0, 3, 4), r.Viewport);
  685. Assert.Equal (new (1, 2, 3, 4), r.Frame);
  686. Assert.Null (r.Focused);
  687. Assert.Null (r.ColorScheme);
  688. Assert.Equal (3, r.Width);
  689. Assert.Equal (4, r.Height);
  690. Assert.Equal (1, r.X);
  691. Assert.Equal (2, r.Y);
  692. Assert.False (r.IsCurrentTop);
  693. Assert.Empty (r.Id);
  694. Assert.Empty (r.Subviews);
  695. Assert.False (r.WantContinuousButtonPressed);
  696. Assert.False (r.WantMousePositionReports);
  697. Assert.Null (r.SuperView);
  698. Assert.Null (r.MostFocused);
  699. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  700. r.Dispose ();
  701. // Initializes a view with a vertical direction
  702. r = new()
  703. {
  704. Text = "Vertical View",
  705. TextDirection = TextDirection.TopBottom_LeftRight,
  706. Width = Dim.Auto (),
  707. Height = Dim.Auto ()
  708. };
  709. r.TextFormatter.WordWrap = false;
  710. Assert.NotNull (r);
  711. r.BeginInit ();
  712. r.EndInit ();
  713. Assert.False (r.CanFocus);
  714. Assert.False (r.HasFocus);
  715. Assert.Equal (new (0, 0, 1, 13), r.Viewport);
  716. Assert.Equal (new (0, 0, 1, 13), r.Frame);
  717. Assert.Null (r.Focused);
  718. Assert.Null (r.ColorScheme);
  719. Assert.False (r.IsCurrentTop);
  720. #if DEBUG
  721. Assert.Equal ("Vertical View", r.Id);
  722. #else
  723. Assert.Equal (string.Empty, r.Id);
  724. #endif
  725. Assert.Empty (r.Subviews);
  726. Assert.False (r.WantContinuousButtonPressed);
  727. Assert.False (r.WantMousePositionReports);
  728. Assert.Null (r.SuperView);
  729. Assert.Null (r.MostFocused);
  730. Assert.Equal (TextDirection.TopBottom_LeftRight, r.TextDirection);
  731. r.Dispose ();
  732. }
  733. [Fact]
  734. [TestRespondersDisposed]
  735. public void New_Methods_Return_False ()
  736. {
  737. var r = new View ();
  738. Assert.False (r.OnKeyDown (new() { KeyCode = KeyCode.Null }));
  739. //Assert.False (r.OnKeyDown (new KeyEventArgs () { Key = Key.Unknown }));
  740. Assert.False (r.OnKeyUp (new() { KeyCode = KeyCode.Null }));
  741. Assert.False (r.NewMouseEvent (new() { Flags = MouseFlags.AllEvents }));
  742. Assert.False (r.NewMouseEnterEvent (new() { Flags = MouseFlags.AllEvents }));
  743. Assert.False (r.NewMouseLeaveEvent (new() { Flags = MouseFlags.AllEvents }));
  744. var v1 = new View ();
  745. Assert.False (r.OnEnter (v1));
  746. v1.Dispose ();
  747. var v2 = new View ();
  748. Assert.False (r.OnLeave (v2));
  749. v2.Dispose ();
  750. r.Dispose ();
  751. // TODO: Add more
  752. }
  753. [Fact]
  754. [AutoInitShutdown]
  755. public void Test_Nested_Views_With_Height_Equal_To_One ()
  756. {
  757. var v = new View { Width = 11, Height = 3, ColorScheme = new () };
  758. var top = new View { Width = Dim.Fill (), Height = 1 };
  759. var bottom = new View { Width = Dim.Fill (), Height = 1, Y = 2 };
  760. top.Add (new Label { Text = "111" });
  761. v.Add (top);
  762. v.Add (new LineView (Orientation.Horizontal) { Y = 1 });
  763. bottom.Add (new Label { Text = "222" });
  764. v.Add (bottom);
  765. v.BeginInit ();
  766. v.EndInit ();
  767. v.LayoutSubviews ();
  768. v.Draw ();
  769. var looksLike =
  770. @"
  771. 111
  772. ───────────
  773. 222";
  774. TestHelpers.AssertDriverContentsAre (looksLike, output);
  775. v.Dispose ();
  776. top.Dispose ();
  777. bottom.Dispose ();
  778. }
  779. [Fact]
  780. [TestRespondersDisposed]
  781. public void View_With_No_Difference_Between_An_Object_Initializer_Compute_And_A_Absolute ()
  782. {
  783. // Object Initializer Computed
  784. var view = new View { X = 1, Y = 2, Width = 3, Height = 4 };
  785. // Object Initializer Absolute
  786. var super = new View { Frame = new (0, 0, 10, 10) };
  787. super.Add (view);
  788. super.BeginInit ();
  789. super.EndInit ();
  790. super.LayoutSubviews ();
  791. Assert.Equal (1, view.X);
  792. Assert.Equal (2, view.Y);
  793. Assert.Equal (3, view.Width);
  794. Assert.Equal (4, view.Height);
  795. Assert.False (view.Frame.IsEmpty);
  796. Assert.Equal (new (1, 2, 3, 4), view.Frame);
  797. Assert.False (view.Viewport.IsEmpty);
  798. Assert.Equal (new (0, 0, 3, 4), view.Viewport);
  799. view.LayoutSubviews ();
  800. Assert.Equal (1, view.X);
  801. Assert.Equal (2, view.Y);
  802. Assert.Equal (3, view.Width);
  803. Assert.Equal (4, view.Height);
  804. Assert.False (view.Frame.IsEmpty);
  805. Assert.False (view.Viewport.IsEmpty);
  806. super.Dispose ();
  807. #if DEBUG_IDISPOSABLE
  808. Assert.Empty (Responder.Instances);
  809. #endif
  810. // Default Constructor
  811. view = new ();
  812. Assert.Equal (0, view.X);
  813. Assert.Equal (0, view.Y);
  814. Assert.Equal (0, view.Width);
  815. Assert.Equal (0, view.Height);
  816. Assert.True (view.Frame.IsEmpty);
  817. Assert.True (view.Viewport.IsEmpty);
  818. view.Dispose ();
  819. // Object Initializer
  820. view = new() { X = 1, Y = 2, Text = "" };
  821. Assert.Equal (1, view.X);
  822. Assert.Equal (2, view.Y);
  823. Assert.Equal (0, view.Width);
  824. Assert.Equal (0, view.Height);
  825. Assert.False (view.Frame.IsEmpty);
  826. Assert.True (view.Viewport.IsEmpty);
  827. view.Dispose ();
  828. // Default Constructor and post assignment equivalent to Object Initializer
  829. view = new ();
  830. view.X = 1;
  831. view.Y = 2;
  832. view.Width = 3;
  833. view.Height = 4;
  834. super = new() { Frame = new (0, 0, 10, 10) };
  835. super.Add (view);
  836. super.BeginInit ();
  837. super.EndInit ();
  838. super.LayoutSubviews ();
  839. Assert.Equal (1, view.X);
  840. Assert.Equal (2, view.Y);
  841. Assert.Equal (3, view.Width);
  842. Assert.Equal (4, view.Height);
  843. Assert.False (view.Frame.IsEmpty);
  844. Assert.Equal (new (1, 2, 3, 4), view.Frame);
  845. Assert.False (view.Viewport.IsEmpty);
  846. Assert.Equal (new (0, 0, 3, 4), view.Viewport);
  847. super.Dispose ();
  848. }
  849. [Fact]
  850. [AutoInitShutdown]
  851. public void Visible_Clear_The_View_Output ()
  852. {
  853. var view = new View { Text = "Testing visibility." }; // use View, not Label to avoid AutoSize == true
  854. // BUGBUG: AutoSize is false and size wasn't provided so it's 0,0
  855. Assert.Equal (0, view.Frame.Width);
  856. Assert.Equal (0, view.Height);
  857. var win = new Window ();
  858. win.Add (view);
  859. Toplevel top = new ();
  860. top.Add (win);
  861. RunState rs = Application.Begin (top);
  862. view.Width = Dim.Auto ();
  863. view.Height = Dim.Auto ();
  864. Assert.Equal ("Testing visibility.".Length, view.Frame.Width);
  865. Assert.True (view.Visible);
  866. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  867. TestHelpers.AssertDriverContentsWithFrameAre (
  868. @"
  869. ┌────────────────────────────┐
  870. │Testing visibility. │
  871. │ │
  872. │ │
  873. └────────────────────────────┘
  874. ",
  875. output
  876. );
  877. view.Visible = false;
  878. var firstIteration = false;
  879. Application.RunIteration (ref rs, ref firstIteration);
  880. TestHelpers.AssertDriverContentsWithFrameAre (
  881. @"
  882. ┌────────────────────────────┐
  883. │ │
  884. │ │
  885. │ │
  886. └────────────────────────────┘
  887. ",
  888. output
  889. );
  890. Application.End (rs);
  891. top.Dispose ();
  892. }
  893. [Fact]
  894. [AutoInitShutdown]
  895. public void Visible_Sets_Also_Sets_Subviews ()
  896. {
  897. var button = new Button { Text = "Click Me" };
  898. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  899. win.Add (button);
  900. Toplevel top = new ();
  901. top.Add (win);
  902. var iterations = 0;
  903. Application.Iteration += (s, a) =>
  904. {
  905. iterations++;
  906. Assert.True (button.Visible);
  907. Assert.True (button.CanFocus);
  908. Assert.True (button.HasFocus);
  909. Assert.True (win.Visible);
  910. Assert.True (win.CanFocus);
  911. Assert.True (win.HasFocus);
  912. Assert.True (RunesCount () > 0);
  913. win.Visible = false;
  914. Assert.True (button.Visible);
  915. Assert.True (button.CanFocus);
  916. Assert.False (button.HasFocus);
  917. Assert.False (win.Visible);
  918. Assert.True (win.CanFocus);
  919. Assert.False (win.HasFocus);
  920. button.SetFocus ();
  921. Assert.False (button.HasFocus);
  922. Assert.False (win.HasFocus);
  923. win.SetFocus ();
  924. Assert.False (button.HasFocus);
  925. Assert.False (win.HasFocus);
  926. top.Draw ();
  927. Assert.True (RunesCount () == 0);
  928. win.Visible = true;
  929. win.FocusFirst ();
  930. Assert.True (button.HasFocus);
  931. Assert.True (win.HasFocus);
  932. top.Draw ();
  933. Assert.True (RunesCount () > 0);
  934. Application.RequestStop ();
  935. };
  936. Application.Run (top);
  937. top.Dispose ();
  938. Assert.Equal (1, iterations);
  939. int RunesCount ()
  940. {
  941. Cell [,] contents = ((FakeDriver)Application.Driver).Contents;
  942. var runesCount = 0;
  943. for (var i = 0; i < Application.Driver.Rows; i++)
  944. {
  945. for (var j = 0; j < Application.Driver.Cols; j++)
  946. {
  947. if (contents [i, j].Rune != (Rune)' ')
  948. {
  949. runesCount++;
  950. }
  951. }
  952. }
  953. return runesCount;
  954. }
  955. }
  956. public class DerivedView : View
  957. {
  958. public DerivedView () { CanFocus = true; }
  959. public bool IsKeyDown { get; set; }
  960. public bool IsKeyPress { get; set; }
  961. public bool IsKeyUp { get; set; }
  962. public override string Text { get; set; }
  963. public override void OnDrawContent (Rectangle viewport)
  964. {
  965. var idx = 0;
  966. // BUGBUG: v2 - this should use Viewport, not Frame
  967. for (var r = 0; r < Frame.Height; r++)
  968. {
  969. for (var c = 0; c < Frame.Width; c++)
  970. {
  971. if (idx < Text.Length)
  972. {
  973. char rune = Text [idx];
  974. if (rune != '\n')
  975. {
  976. AddRune (c, r, (Rune)Text [idx]);
  977. }
  978. idx++;
  979. if (rune == '\n')
  980. {
  981. break;
  982. }
  983. }
  984. }
  985. }
  986. ClearLayoutNeeded ();
  987. ClearNeedsDisplay ();
  988. }
  989. public override bool OnKeyDown (Key keyEvent)
  990. {
  991. IsKeyDown = true;
  992. return true;
  993. }
  994. public override bool OnKeyUp (Key keyEvent)
  995. {
  996. IsKeyUp = true;
  997. return true;
  998. }
  999. public override bool OnProcessKeyDown (Key keyEvent)
  1000. {
  1001. IsKeyPress = true;
  1002. return true;
  1003. }
  1004. }
  1005. // OnAccept/Accept tests
  1006. [Fact]
  1007. public void OnAccept_Fires_Accept ()
  1008. {
  1009. var view = new View ();
  1010. var accepted = false;
  1011. view.Accept += ViewOnAccept;
  1012. view.InvokeCommand (Command.Accept);
  1013. Assert.True (accepted);
  1014. return;
  1015. void ViewOnAccept (object sender, HandledEventArgs e) { accepted = true; }
  1016. }
  1017. [Fact]
  1018. public void Accept_Cancel_Event_OnAccept_Returns_True ()
  1019. {
  1020. var view = new View ();
  1021. var acceptInvoked = false;
  1022. view.Accept += ViewOnAccept;
  1023. bool? ret = view.InvokeCommand (Command.Accept);
  1024. Assert.True (ret);
  1025. Assert.True (acceptInvoked);
  1026. return;
  1027. void ViewOnAccept (object sender, HandledEventArgs e)
  1028. {
  1029. acceptInvoked = true;
  1030. e.Handled = true;
  1031. }
  1032. }
  1033. [Fact]
  1034. public void Accept_Command_Invokes_Accept_Event ()
  1035. {
  1036. var view = new View ();
  1037. var accepted = false;
  1038. view.Accept += ViewOnAccept;
  1039. view.InvokeCommand (Command.Accept);
  1040. Assert.True (accepted);
  1041. return;
  1042. void ViewOnAccept (object sender, HandledEventArgs e) { accepted = true; }
  1043. }
  1044. [Fact]
  1045. public void HotKey_Command_SetsFocus ()
  1046. {
  1047. var view = new View ();
  1048. view.CanFocus = true;
  1049. Assert.False (view.HasFocus);
  1050. view.InvokeCommand (Command.HotKey);
  1051. Assert.True (view.HasFocus);
  1052. }
  1053. }