ViewTests.cs 40 KB

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