2
0

ViewTests.cs 39 KB

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