ViewTests.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  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 (configLocation: ConfigurationManager.ConfigLocations.DefaultOnly)]
  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. output,
  160. Application.Driver,
  161. attributes
  162. );
  163. }
  164. else
  165. {
  166. TestHelpers.AssertDriverAttributesAre (
  167. @"
  168. 222222222222222222220
  169. 111111111111111111110",
  170. output,
  171. Application.Driver,
  172. attributes
  173. );
  174. }
  175. if (label)
  176. {
  177. root.CanFocus = true;
  178. v.CanFocus = true;
  179. Assert.True (v.HasFocus);
  180. v.SetFocus ();
  181. Assert.True (v.HasFocus);
  182. Application.Refresh ();
  183. TestHelpers.AssertDriverAttributesAre (
  184. @"
  185. 222222222222222222220
  186. 111111111111111111110",
  187. output,
  188. Application.Driver,
  189. attributes
  190. );
  191. }
  192. Application.End (runState);
  193. top.Dispose ();
  194. }
  195. [Fact]
  196. [AutoInitShutdown]
  197. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  198. {
  199. var label = new Label { Text = "At 0,0" };
  200. var view = new DerivedView
  201. {
  202. X = 2,
  203. Y = 2,
  204. Width = 30,
  205. Height = 2,
  206. Text = "A text with some long width\n and also with two lines."
  207. };
  208. Toplevel top = new ();
  209. top.Add (label, view);
  210. RunState runState = Application.Begin (top);
  211. TestHelpers.AssertDriverContentsWithFrameAre (
  212. @"
  213. At 0,0
  214. A text with some long width
  215. and also with two lines. ",
  216. output
  217. );
  218. view.Frame = new (3, 3, 10, 1);
  219. Assert.Equal (new (3, 3, 10, 1), view.Frame);
  220. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  221. Assert.Equal (new (0, 0, 10, 1), view._needsDisplayRect);
  222. //Application.Refresh();
  223. top.Draw ();
  224. TestHelpers.AssertDriverContentsWithFrameAre (
  225. @"
  226. At 0,0
  227. A text wit",
  228. output
  229. );
  230. Application.End (runState);
  231. top.Dispose ();
  232. }
  233. [Fact]
  234. [AutoInitShutdown]
  235. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  236. {
  237. var label = new Label { Text = "At 0,0" };
  238. var view = new DerivedView
  239. {
  240. X = 2,
  241. Y = 2,
  242. Width = 30,
  243. Height = 2,
  244. Text = "A text with some long width\n and also with two lines."
  245. };
  246. Toplevel top = new ();
  247. top.Add (label, view);
  248. RunState runState = Application.Begin (top);
  249. top.Draw ();
  250. TestHelpers.AssertDriverContentsWithFrameAre (
  251. @"
  252. At 0,0
  253. A text with some long width
  254. and also with two lines. ",
  255. output
  256. );
  257. view.X = 3;
  258. view.Y = 3;
  259. view.Width = 10;
  260. view.Height = 1;
  261. Assert.Equal (new (3, 3, 10, 1), view.Frame);
  262. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  263. Assert.Equal (new (0, 0, 30, 2), view._needsDisplayRect);
  264. top.Draw ();
  265. TestHelpers.AssertDriverContentsWithFrameAre (
  266. @"
  267. At 0,0
  268. A text wit",
  269. output
  270. );
  271. Application.End (runState);
  272. top.Dispose ();
  273. }
  274. [Fact]
  275. [AutoInitShutdown]
  276. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  277. {
  278. var label = new Label { Text = "At 0,0" };
  279. var view = new DerivedView
  280. {
  281. X = 2,
  282. Y = 2,
  283. Width = 30,
  284. Height = 2,
  285. Text = "A text with some long width\n and also with two lines."
  286. };
  287. Toplevel top = new ();
  288. top.Add (label, view);
  289. RunState runState = Application.Begin (top);
  290. top.Draw ();
  291. TestHelpers.AssertDriverContentsWithFrameAre (
  292. @"
  293. At 0,0
  294. A text with some long width
  295. and also with two lines. ",
  296. output
  297. );
  298. view.Frame = new (1, 1, 10, 1);
  299. Assert.Equal (new (1, 1, 10, 1), view.Frame);
  300. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  301. Assert.Equal (new (0, 0, 10, 1), view._needsDisplayRect);
  302. top.Draw ();
  303. TestHelpers.AssertDriverContentsWithFrameAre (
  304. @"
  305. At 0,0
  306. A text wit",
  307. output
  308. );
  309. Application.End (runState);
  310. top.Dispose ();
  311. }
  312. [Fact]
  313. [AutoInitShutdown]
  314. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  315. {
  316. var label = new Label { Text = "At 0,0" };
  317. var view = new DerivedView
  318. {
  319. X = 2,
  320. Y = 2,
  321. Width = 30,
  322. Height = 2,
  323. Text = "A text with some long width\n and also with two lines."
  324. };
  325. Toplevel top = new ();
  326. top.Add (label, view);
  327. RunState runState = Application.Begin (top);
  328. top.Draw ();
  329. TestHelpers.AssertDriverContentsWithFrameAre (
  330. @"
  331. At 0,0
  332. A text with some long width
  333. and also with two lines. ",
  334. output
  335. );
  336. view.X = 1;
  337. view.Y = 1;
  338. view.Width = 10;
  339. view.Height = 1;
  340. Assert.Equal (new (1, 1, 10, 1), view.Frame);
  341. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  342. Assert.Equal (new (0, 0, 30, 2), view._needsDisplayRect);
  343. top.Draw ();
  344. TestHelpers.AssertDriverContentsWithFrameAre (
  345. @"
  346. At 0,0
  347. A text wit",
  348. output
  349. );
  350. Application.End (runState);
  351. top.Dispose ();
  352. }
  353. [Fact]
  354. [TestRespondersDisposed]
  355. public void Dispose_View ()
  356. {
  357. var view = new View ();
  358. Assert.NotNull (view.Margin);
  359. Assert.NotNull (view.Border);
  360. Assert.NotNull (view.Padding);
  361. #if DEBUG_IDISPOSABLE
  362. Assert.Equal (4, Responder.Instances.Count);
  363. #endif
  364. view.Dispose ();
  365. Assert.Null (view.Margin);
  366. Assert.Null (view.Border);
  367. Assert.Null (view.Padding);
  368. }
  369. [Fact]
  370. [AutoInitShutdown]
  371. public void DrawContentComplete_Event_Is_Always_Called ()
  372. {
  373. var viewCalled = false;
  374. var tvCalled = false;
  375. var view = new View { Width = 10, Height = 10, Text = "View" };
  376. view.DrawContentComplete += (s, e) => viewCalled = true;
  377. var tv = new TextView { Y = 11, Width = 10, Height = 10 };
  378. tv.DrawContentComplete += (s, e) => tvCalled = true;
  379. var top = new Toplevel ();
  380. top.Add (view, tv);
  381. Application.Begin (top);
  382. Assert.True (viewCalled);
  383. Assert.True (tvCalled);
  384. top.Dispose ();
  385. }
  386. [Fact]
  387. [AutoInitShutdown]
  388. public void Frame_Set_After_Initialize_Update_NeededDisplay ()
  389. {
  390. var frame = new FrameView ();
  391. var label = new Label
  392. {
  393. ColorScheme = Colors.ColorSchemes ["Menu"], X = 0, Y = 0, Text = "This should be the first line."
  394. };
  395. var view = new View
  396. {
  397. X = 0, // don't overcomplicate unit tests
  398. Y = 1,
  399. Height = Dim.Auto (DimAutoStyle.Text),
  400. Width = Dim.Auto (DimAutoStyle.Text),
  401. Text = "Press me!"
  402. };
  403. frame.Add (label, view);
  404. frame.X = Pos.Center ();
  405. frame.Y = Pos.Center ();
  406. frame.Width = 40;
  407. frame.Height = 8;
  408. Toplevel top = new ();
  409. top.Add (frame);
  410. RunState runState = Application.Begin (top);
  411. top.LayoutComplete += (s, e) => { Assert.Equal (new (0, 0, 80, 25), top._needsDisplayRect); };
  412. frame.LayoutComplete += (s, e) => { Assert.Equal (new (0, 0, 40, 8), frame._needsDisplayRect); };
  413. label.LayoutComplete += (s, e) => { Assert.Equal (new (0, 0, 38, 1), label._needsDisplayRect); };
  414. view.LayoutComplete += (s, e) => { Assert.Equal (new (0, 0, 13, 1), view._needsDisplayRect); };
  415. Assert.Equal (new (0, 0, 80, 25), top.Frame);
  416. Assert.Equal (new (20, 8, 40, 8), frame.Frame);
  417. Assert.Equal (
  418. new (20, 8, 60, 16),
  419. new Rectangle (
  420. frame.Frame.Left,
  421. frame.Frame.Top,
  422. frame.Frame.Right,
  423. frame.Frame.Bottom
  424. )
  425. );
  426. Assert.Equal (new (0, 0, 30, 1), label.Frame);
  427. Assert.Equal (new (0, 1, 9, 1), view.Frame); // this proves frame was set
  428. Application.End (runState);
  429. top.Dispose ();
  430. }
  431. [Fact]
  432. public void GetHotNormalColor_ColorScheme ()
  433. {
  434. var view = new View { ColorScheme = Colors.ColorSchemes ["Base"] };
  435. Assert.Equal (view.ColorScheme.HotNormal, view.GetHotNormalColor ());
  436. view.Enabled = false;
  437. Assert.Equal (view.ColorScheme.Disabled, view.GetHotNormalColor ());
  438. view.Dispose ();
  439. }
  440. [Fact]
  441. public void GetNormalColor_ColorScheme ()
  442. {
  443. var view = new View { ColorScheme = Colors.ColorSchemes ["Base"] };
  444. Assert.Equal (view.ColorScheme.Normal, view.GetNormalColor ());
  445. view.Enabled = false;
  446. Assert.Equal (view.ColorScheme.Disabled, view.GetNormalColor ());
  447. view.Dispose ();
  448. }
  449. [Fact]
  450. [AutoInitShutdown]
  451. public void Incorrect_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  452. {
  453. var label = new Label { Text = "At 0,0" };
  454. var view = new DerivedView
  455. {
  456. X = 2,
  457. Y = 2,
  458. Width = 30,
  459. Height = 2,
  460. Text = "A text with some long width\n and also with two lines."
  461. };
  462. Toplevel top = new ();
  463. top.Add (label, view);
  464. RunState runState = Application.Begin (top);
  465. view.Draw ();
  466. TestHelpers.AssertDriverContentsWithFrameAre (
  467. @"
  468. At 0,0
  469. A text with some long width
  470. and also with two lines. ",
  471. output
  472. );
  473. view.Frame = new (3, 3, 10, 1);
  474. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  475. Assert.Equal (new (0, 0, 10, 1), view._needsDisplayRect);
  476. view.Draw ();
  477. TestHelpers.AssertDriverContentsWithFrameAre (
  478. @"
  479. At 0,0
  480. A text with some long width
  481. A text witith two lines. ",
  482. output
  483. );
  484. Application.End (runState);
  485. top.Dispose ();
  486. }
  487. [Fact]
  488. [AutoInitShutdown]
  489. public void Incorrect_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  490. {
  491. var label = new Label { Text = "At 0,0" };
  492. var view = new DerivedView
  493. {
  494. X = 2,
  495. Y = 2,
  496. Width = 30,
  497. Height = 2,
  498. Text = "A text with some long width\n and also with two lines."
  499. };
  500. Toplevel top = new ();
  501. top.Add (label, view);
  502. RunState runState = Application.Begin (top);
  503. view.Draw ();
  504. TestHelpers.AssertDriverContentsWithFrameAre (
  505. @"
  506. At 0,0
  507. A text with some long width
  508. and also with two lines. ",
  509. output
  510. );
  511. view.X = 3;
  512. view.Y = 3;
  513. view.Width = 10;
  514. view.Height = 1;
  515. Assert.Equal (new (3, 3, 10, 1), view.Frame);
  516. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  517. Assert.Equal (new (0, 0, 30, 2), view._needsDisplayRect);
  518. view.Draw ();
  519. TestHelpers.AssertDriverContentsWithFrameAre (
  520. @"
  521. At 0,0
  522. A text with some long width
  523. A text witith two lines. ",
  524. output
  525. );
  526. Application.End (runState);
  527. top.Dispose ();
  528. }
  529. [Fact]
  530. [AutoInitShutdown]
  531. public void Incorrect_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  532. {
  533. var label = new Label { Text = "At 0,0" };
  534. var view = new DerivedView
  535. {
  536. X = 2,
  537. Y = 2,
  538. Width = 30,
  539. Height = 2,
  540. Text = "A text with some long width\n and also with two lines."
  541. };
  542. Toplevel top = new ();
  543. top.Add (label, view);
  544. RunState runState = Application.Begin (top);
  545. view.Draw ();
  546. TestHelpers.AssertDriverContentsWithFrameAre (
  547. @"
  548. At 0,0
  549. A text with some long width
  550. and also with two lines. ",
  551. output
  552. );
  553. view.Frame = new (1, 1, 10, 1);
  554. Assert.Equal (new (1, 1, 10, 1), view.Frame);
  555. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  556. Assert.Equal (new (0, 0, 10, 1), view._needsDisplayRect);
  557. view.Draw ();
  558. TestHelpers.AssertDriverContentsWithFrameAre (
  559. @"
  560. At 0,0
  561. A text wit
  562. A text with some long width
  563. and also with two lines. ",
  564. output
  565. );
  566. Application.End (runState);
  567. top.Dispose ();
  568. }
  569. [Fact]
  570. [AutoInitShutdown]
  571. public void Incorrect_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  572. {
  573. var label = new Label { Text = "At 0,0" };
  574. var view = new DerivedView
  575. {
  576. X = 2,
  577. Y = 2,
  578. Width = 30,
  579. Height = 2,
  580. Text = "A text with some long width\n and also with two lines."
  581. };
  582. Toplevel top = new ();
  583. top.Add (label, view);
  584. RunState runState = Application.Begin (top);
  585. view.Draw ();
  586. TestHelpers.AssertDriverContentsWithFrameAre (
  587. @"
  588. At 0,0
  589. A text with some long width
  590. and also with two lines. ",
  591. output
  592. );
  593. view.X = 1;
  594. view.Y = 1;
  595. view.Width = 10;
  596. view.Height = 1;
  597. Assert.Equal (new (1, 1, 10, 1), view.Frame);
  598. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  599. Assert.Equal (new (0, 0, 30, 2), view._needsDisplayRect);
  600. view.Draw ();
  601. TestHelpers.AssertDriverContentsWithFrameAre (
  602. @"
  603. At 0,0
  604. A text wit
  605. A text with some long width
  606. and also with two lines. ",
  607. output
  608. );
  609. Application.End (runState);
  610. top.Dispose ();
  611. }
  612. [Fact]
  613. public void Internal_Tests ()
  614. {
  615. var rect = new Rectangle (1, 1, 10, 1);
  616. var view = new View { Frame = rect };
  617. }
  618. [Fact]
  619. [SetupFakeDriver]
  620. public void SetText_RendersCorrectly ()
  621. {
  622. View view;
  623. var text = "test";
  624. view = new Label { Text = text };
  625. view.BeginInit ();
  626. view.EndInit ();
  627. view.Draw ();
  628. TestHelpers.AssertDriverContentsWithFrameAre (text, output);
  629. }
  630. [Fact]
  631. [TestRespondersDisposed]
  632. public void New_Initializes ()
  633. {
  634. // Parameterless
  635. var r = new View ();
  636. Assert.NotNull (r);
  637. Assert.True (r.Enabled);
  638. Assert.True (r.Visible);
  639. Assert.Equal ($"View(){r.Viewport}", r.ToString ());
  640. Assert.False (r.CanFocus);
  641. Assert.False (r.HasFocus);
  642. Assert.Equal (new (0, 0, 0, 0), r.Viewport);
  643. Assert.Equal (new (0, 0, 0, 0), r.Frame);
  644. Assert.Null (r.Focused);
  645. Assert.Null (r.ColorScheme);
  646. Assert.Equal (0, r.Width);
  647. Assert.Equal (0, r.Height);
  648. Assert.Equal (0, r.X);
  649. Assert.Equal (0, r.Y);
  650. Assert.False (r.IsCurrentTop);
  651. Assert.Empty (r.Id);
  652. Assert.Empty (r.Subviews);
  653. Assert.False (r.WantContinuousButtonPressed);
  654. Assert.False (r.WantMousePositionReports);
  655. Assert.Null (r.SuperView);
  656. Assert.Null (r.MostFocused);
  657. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  658. r.Dispose ();
  659. // Empty Rect
  660. r = new () { Frame = Rectangle.Empty };
  661. Assert.NotNull (r);
  662. Assert.Equal ($"View(){r.Viewport}", r.ToString ());
  663. Assert.False (r.CanFocus);
  664. Assert.False (r.HasFocus);
  665. Assert.Equal (new (0, 0, 0, 0), r.Viewport);
  666. Assert.Equal (new (0, 0, 0, 0), r.Frame);
  667. Assert.Null (r.Focused);
  668. Assert.Null (r.ColorScheme);
  669. Assert.Equal (0, r.Width);
  670. Assert.Equal (0, r.Height);
  671. Assert.Equal (0, r.X);
  672. Assert.Equal (0, r.Y);
  673. Assert.False (r.IsCurrentTop);
  674. Assert.Empty (r.Id);
  675. Assert.Empty (r.Subviews);
  676. Assert.False (r.WantContinuousButtonPressed);
  677. Assert.False (r.WantMousePositionReports);
  678. Assert.Null (r.SuperView);
  679. Assert.Null (r.MostFocused);
  680. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  681. r.Dispose ();
  682. // Rect with values
  683. r = new () { Frame = new (1, 2, 3, 4) };
  684. Assert.NotNull (r);
  685. Assert.Equal ($"View(){r.Frame}", r.ToString ());
  686. Assert.False (r.CanFocus);
  687. Assert.False (r.HasFocus);
  688. Assert.Equal (new (0, 0, 3, 4), r.Viewport);
  689. Assert.Equal (new (1, 2, 3, 4), r.Frame);
  690. Assert.Null (r.Focused);
  691. Assert.Null (r.ColorScheme);
  692. Assert.Equal (3, r.Width);
  693. Assert.Equal (4, r.Height);
  694. Assert.Equal (1, r.X);
  695. Assert.Equal (2, r.Y);
  696. Assert.False (r.IsCurrentTop);
  697. Assert.Empty (r.Id);
  698. Assert.Empty (r.Subviews);
  699. Assert.False (r.WantContinuousButtonPressed);
  700. Assert.False (r.WantMousePositionReports);
  701. Assert.Null (r.SuperView);
  702. Assert.Null (r.MostFocused);
  703. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  704. r.Dispose ();
  705. // Initializes a view with a vertical direction
  706. r = new ()
  707. {
  708. Text = "Vertical View",
  709. TextDirection = TextDirection.TopBottom_LeftRight,
  710. Width = Dim.Auto (),
  711. Height = Dim.Auto ()
  712. };
  713. r.TextFormatter.WordWrap = false;
  714. Assert.NotNull (r);
  715. r.BeginInit ();
  716. r.EndInit ();
  717. Assert.False (r.CanFocus);
  718. Assert.False (r.HasFocus);
  719. Assert.Equal (new (0, 0, 1, 13), r.Viewport);
  720. Assert.Equal (new (0, 0, 1, 13), r.Frame);
  721. Assert.Null (r.Focused);
  722. Assert.Null (r.ColorScheme);
  723. Assert.False (r.IsCurrentTop);
  724. #if DEBUG
  725. Assert.Equal ("Vertical View", r.Id);
  726. #else
  727. Assert.Equal (string.Empty, r.Id);
  728. #endif
  729. Assert.Empty (r.Subviews);
  730. Assert.False (r.WantContinuousButtonPressed);
  731. Assert.False (r.WantMousePositionReports);
  732. Assert.Null (r.SuperView);
  733. Assert.Null (r.MostFocused);
  734. Assert.Equal (TextDirection.TopBottom_LeftRight, r.TextDirection);
  735. r.Dispose ();
  736. }
  737. [Fact]
  738. [TestRespondersDisposed]
  739. public void New_Methods_Return_False ()
  740. {
  741. var r = new View ();
  742. Assert.False (r.NewKeyDownEvent (Key.Empty));
  743. //Assert.False (r.OnKeyDown (new KeyEventArgs () { Key = Key.Unknown }));
  744. Assert.False (r.NewKeyUpEvent (Key.Empty));
  745. Assert.False (r.NewMouseEvent (new () { Flags = MouseFlags.AllEvents }));
  746. r.Dispose ();
  747. // TODO: Add more
  748. }
  749. [Fact]
  750. [AutoInitShutdown]
  751. public void Test_Nested_Views_With_Height_Equal_To_One ()
  752. {
  753. var v = new View { Width = 11, Height = 3, ColorScheme = new () };
  754. var top = new View { Width = Dim.Fill (), Height = 1 };
  755. var bottom = new View { Width = Dim.Fill (), Height = 1, Y = 2 };
  756. top.Add (new Label { Text = "111" });
  757. v.Add (top);
  758. v.Add (new LineView (Orientation.Horizontal) { Y = 1 });
  759. bottom.Add (new Label { Text = "222" });
  760. v.Add (bottom);
  761. v.BeginInit ();
  762. v.EndInit ();
  763. v.LayoutSubviews ();
  764. v.Draw ();
  765. var looksLike =
  766. @"
  767. 111
  768. ───────────
  769. 222";
  770. TestHelpers.AssertDriverContentsAre (looksLike, output);
  771. v.Dispose ();
  772. top.Dispose ();
  773. bottom.Dispose ();
  774. }
  775. [Fact]
  776. [TestRespondersDisposed]
  777. public void View_With_No_Difference_Between_An_Object_Initializer_Compute_And_A_Absolute ()
  778. {
  779. // Object Initializer Computed
  780. var view = new View { X = 1, Y = 2, Width = 3, Height = 4 };
  781. // Object Initializer Absolute
  782. var super = new View { Frame = new (0, 0, 10, 10) };
  783. super.Add (view);
  784. super.BeginInit ();
  785. super.EndInit ();
  786. super.LayoutSubviews ();
  787. Assert.Equal (1, view.X);
  788. Assert.Equal (2, view.Y);
  789. Assert.Equal (3, view.Width);
  790. Assert.Equal (4, view.Height);
  791. Assert.False (view.Frame.IsEmpty);
  792. Assert.Equal (new (1, 2, 3, 4), view.Frame);
  793. Assert.False (view.Viewport.IsEmpty);
  794. Assert.Equal (new (0, 0, 3, 4), view.Viewport);
  795. view.LayoutSubviews ();
  796. Assert.Equal (1, view.X);
  797. Assert.Equal (2, view.Y);
  798. Assert.Equal (3, view.Width);
  799. Assert.Equal (4, view.Height);
  800. Assert.False (view.Frame.IsEmpty);
  801. Assert.False (view.Viewport.IsEmpty);
  802. super.Dispose ();
  803. #if DEBUG_IDISPOSABLE
  804. Assert.Empty (Responder.Instances);
  805. #endif
  806. // Default Constructor
  807. view = new ();
  808. Assert.Equal (0, view.X);
  809. Assert.Equal (0, view.Y);
  810. Assert.Equal (0, view.Width);
  811. Assert.Equal (0, view.Height);
  812. Assert.True (view.Frame.IsEmpty);
  813. Assert.True (view.Viewport.IsEmpty);
  814. view.Dispose ();
  815. // Object Initializer
  816. view = new () { X = 1, Y = 2, Text = "" };
  817. Assert.Equal (1, view.X);
  818. Assert.Equal (2, view.Y);
  819. Assert.Equal (0, view.Width);
  820. Assert.Equal (0, view.Height);
  821. Assert.False (view.Frame.IsEmpty);
  822. Assert.True (view.Viewport.IsEmpty);
  823. view.Dispose ();
  824. // Default Constructor and post assignment equivalent to Object Initializer
  825. view = new ();
  826. view.X = 1;
  827. view.Y = 2;
  828. view.Width = 3;
  829. view.Height = 4;
  830. super = new () { Frame = new (0, 0, 10, 10) };
  831. super.Add (view);
  832. super.BeginInit ();
  833. super.EndInit ();
  834. super.LayoutSubviews ();
  835. Assert.Equal (1, view.X);
  836. Assert.Equal (2, view.Y);
  837. Assert.Equal (3, view.Width);
  838. Assert.Equal (4, view.Height);
  839. Assert.False (view.Frame.IsEmpty);
  840. Assert.Equal (new (1, 2, 3, 4), view.Frame);
  841. Assert.False (view.Viewport.IsEmpty);
  842. Assert.Equal (new (0, 0, 3, 4), view.Viewport);
  843. super.Dispose ();
  844. }
  845. [Fact]
  846. [AutoInitShutdown]
  847. public void Visible_Clear_The_View_Output ()
  848. {
  849. var view = new View { Text = "Testing visibility." }; // use View, not Label to avoid AutoSize == true
  850. Assert.Equal (0, view.Frame.Width);
  851. Assert.Equal (0, view.Height);
  852. var win = new Window ();
  853. win.Add (view);
  854. Toplevel top = new ();
  855. top.Add (win);
  856. RunState rs = Application.Begin (top);
  857. view.Width = Dim.Auto ();
  858. view.Height = Dim.Auto ();
  859. Assert.Equal ("Testing visibility.".Length, view.Frame.Width);
  860. Assert.True (view.Visible);
  861. ((FakeDriver)Application.Driver!).SetBufferSize (30, 5);
  862. TestHelpers.AssertDriverContentsWithFrameAre (
  863. @"
  864. ┌────────────────────────────┐
  865. │Testing visibility. │
  866. │ │
  867. │ │
  868. └────────────────────────────┘
  869. ",
  870. output
  871. );
  872. view.Visible = false;
  873. var firstIteration = false;
  874. Application.RunIteration (ref rs, firstIteration);
  875. TestHelpers.AssertDriverContentsWithFrameAre (
  876. @"
  877. ┌────────────────────────────┐
  878. │ │
  879. │ │
  880. │ │
  881. └────────────────────────────┘
  882. ",
  883. output
  884. );
  885. Application.End (rs);
  886. top.Dispose ();
  887. }
  888. [Fact]
  889. [AutoInitShutdown]
  890. public void Visible_Sets_Also_Sets_Subviews ()
  891. {
  892. var button = new Button { Text = "Click Me" };
  893. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  894. win.Add (button);
  895. Toplevel top = new ();
  896. top.Add (win);
  897. var iterations = 0;
  898. Application.Iteration += (s, a) =>
  899. {
  900. iterations++;
  901. Assert.True (button.Visible);
  902. Assert.True (button.CanFocus);
  903. Assert.True (button.HasFocus);
  904. Assert.True (win.Visible);
  905. Assert.True (win.CanFocus);
  906. Assert.True (win.HasFocus);
  907. win.Visible = false;
  908. Assert.True (button.Visible);
  909. Assert.True (button.CanFocus);
  910. Assert.False (button.HasFocus);
  911. Assert.False (win.Visible);
  912. Assert.True (win.CanFocus);
  913. Assert.False (win.HasFocus);
  914. button.SetFocus ();
  915. Assert.False (button.HasFocus);
  916. Assert.False (win.HasFocus);
  917. win.SetFocus ();
  918. Assert.False (button.HasFocus);
  919. Assert.False (win.HasFocus);
  920. win.Visible = true;
  921. Assert.True (button.HasFocus);
  922. Assert.True (win.HasFocus);
  923. Application.RequestStop ();
  924. };
  925. Application.Run (top);
  926. top.Dispose ();
  927. Assert.Equal (1, iterations);
  928. }
  929. public class DerivedView : View
  930. {
  931. public DerivedView () { CanFocus = true; }
  932. public bool IsKeyDown { get; set; }
  933. public bool IsKeyPress { get; set; }
  934. public bool IsKeyUp { get; set; }
  935. public override string Text { get; set; }
  936. public override void OnDrawContent (Rectangle viewport)
  937. {
  938. var idx = 0;
  939. // BUGBUG: v2 - this should use Viewport, not Frame
  940. for (var r = 0; r < Frame.Height; r++)
  941. {
  942. for (var c = 0; c < Frame.Width; c++)
  943. {
  944. if (idx < Text.Length)
  945. {
  946. char rune = Text [idx];
  947. if (rune != '\n')
  948. {
  949. AddRune (c, r, (Rune)Text [idx]);
  950. }
  951. idx++;
  952. if (rune == '\n')
  953. {
  954. break;
  955. }
  956. }
  957. }
  958. }
  959. ClearNeedsDisplay ();
  960. }
  961. protected override bool OnKeyDown (Key keyEvent)
  962. {
  963. IsKeyDown = true;
  964. return true;
  965. }
  966. public override bool OnKeyUp (Key keyEvent)
  967. {
  968. IsKeyUp = true;
  969. return true;
  970. }
  971. protected override bool OnKeyDownNotHandled (Key keyEvent)
  972. {
  973. IsKeyPress = true;
  974. return true;
  975. }
  976. }
  977. }