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.True (v.AutoSize);
  134. Assert.False (v.CanFocus);
  135. // The text is 100 characters long, but Dim.Auto constrains to SuperView, so it should be truncated.
  136. Assert.Equal (new Rectangle (0, 0, 100, 1), v.Frame);
  137. }
  138. else
  139. {
  140. Assert.False (v.AutoSize);
  141. Assert.True (v.CanFocus);
  142. Assert.Equal (new Rectangle (0, 0, 20, 1), v.Frame);
  143. }
  144. TestHelpers.AssertDriverContentsWithFrameAre (
  145. @"
  146. cccccccccccccccccccc",
  147. _output
  148. );
  149. Attribute [] attributes =
  150. {
  151. Colors.ColorSchemes ["TopLevel"].Normal,
  152. Colors.ColorSchemes ["Base"].Normal,
  153. Colors.ColorSchemes ["Base"].Focus
  154. };
  155. if (label)
  156. {
  157. TestHelpers.AssertDriverAttributesAre (
  158. @"
  159. 111111111111111111110
  160. 111111111111111111110",
  161. Application.Driver,
  162. attributes
  163. );
  164. }
  165. else
  166. {
  167. TestHelpers.AssertDriverAttributesAre (
  168. @"
  169. 222222222222222222220
  170. 111111111111111111110",
  171. Application.Driver,
  172. attributes
  173. );
  174. }
  175. if (label)
  176. {
  177. root.CanFocus = true;
  178. v.CanFocus = true;
  179. Assert.False (v.HasFocus);
  180. v.SetFocus ();
  181. Assert.True (v.HasFocus);
  182. Application.Refresh ();
  183. TestHelpers.AssertDriverAttributesAre (
  184. @"
  185. 222222222222222222220
  186. 111111111111111111110",
  187. Application.Driver,
  188. attributes
  189. );
  190. }
  191. Application.End (runState);
  192. }
  193. [Fact]
  194. [AutoInitShutdown]
  195. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  196. {
  197. var label = new Label { Text = "At 0,0" };
  198. var view = new DerivedView
  199. {
  200. X = 2,
  201. Y = 2,
  202. Width = 30,
  203. Height = 2,
  204. Text = "A text with some long width\n and also with two lines."
  205. };
  206. Toplevel top = new ();
  207. top.Add (label, view);
  208. RunState runState = Application.Begin (top);
  209. TestHelpers.AssertDriverContentsWithFrameAre (
  210. @"
  211. At 0,0
  212. A text with some long width
  213. and also with two lines. ",
  214. _output
  215. );
  216. view.Frame = new Rectangle (3, 3, 10, 1);
  217. Assert.Equal (new Rectangle (3, 3, 10, 1), view.Frame);
  218. Assert.Equal (LayoutStyle.Absolute, view.LayoutStyle);
  219. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Viewport);
  220. Assert.Equal (new Rectangle (0, 0, 10, 1), view._needsDisplayRect);
  221. top.Draw ();
  222. TestHelpers.AssertDriverContentsWithFrameAre (
  223. @"
  224. At 0,0
  225. A text wit",
  226. _output
  227. );
  228. Application.End (runState);
  229. }
  230. [Fact]
  231. [AutoInitShutdown]
  232. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  233. {
  234. var label = new Label { Text = "At 0,0" };
  235. var view = new DerivedView
  236. {
  237. X = 2,
  238. Y = 2,
  239. Width = 30,
  240. Height = 2,
  241. Text = "A text with some long width\n and also with two lines."
  242. };
  243. Toplevel top = new ();
  244. top.Add (label, view);
  245. RunState runState = Application.Begin (top);
  246. top.Draw ();
  247. TestHelpers.AssertDriverContentsWithFrameAre (
  248. @"
  249. At 0,0
  250. A text with some long width
  251. and also with two lines. ",
  252. _output
  253. );
  254. view.X = 3;
  255. view.Y = 3;
  256. view.Width = 10;
  257. view.Height = 1;
  258. Assert.Equal (new Rectangle (3, 3, 10, 1), view.Frame);
  259. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Viewport);
  260. Assert.Equal (new Rectangle (0, 0, 30, 2), view._needsDisplayRect);
  261. top.Draw ();
  262. TestHelpers.AssertDriverContentsWithFrameAre (
  263. @"
  264. At 0,0
  265. A text wit",
  266. _output
  267. );
  268. Application.End (runState);
  269. }
  270. [Fact]
  271. [AutoInitShutdown]
  272. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  273. {
  274. var label = new Label { Text = "At 0,0" };
  275. var view = new DerivedView
  276. {
  277. X = 2,
  278. Y = 2,
  279. Width = 30,
  280. Height = 2,
  281. Text = "A text with some long width\n and also with two lines."
  282. };
  283. Toplevel top = new ();
  284. top.Add (label, view);
  285. RunState runState = Application.Begin (top);
  286. top.Draw ();
  287. TestHelpers.AssertDriverContentsWithFrameAre (
  288. @"
  289. At 0,0
  290. A text with some long width
  291. and also with two lines. ",
  292. _output
  293. );
  294. view.Frame = new Rectangle (1, 1, 10, 1);
  295. Assert.Equal (new Rectangle (1, 1, 10, 1), view.Frame);
  296. Assert.Equal (LayoutStyle.Absolute, view.LayoutStyle);
  297. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Viewport);
  298. Assert.Equal (new Rectangle (0, 0, 10, 1), view._needsDisplayRect);
  299. top.Draw ();
  300. TestHelpers.AssertDriverContentsWithFrameAre (
  301. @"
  302. At 0,0
  303. A text wit",
  304. _output
  305. );
  306. Application.End (runState);
  307. }
  308. [Fact]
  309. [AutoInitShutdown]
  310. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  311. {
  312. var label = new Label { Text = "At 0,0" };
  313. var view = new DerivedView
  314. {
  315. X = 2,
  316. Y = 2,
  317. Width = 30,
  318. Height = 2,
  319. Text = "A text with some long width\n and also with two lines."
  320. };
  321. Toplevel top = new ();
  322. top.Add (label, view);
  323. RunState runState = Application.Begin (top);
  324. top.Draw ();
  325. TestHelpers.AssertDriverContentsWithFrameAre (
  326. @"
  327. At 0,0
  328. A text with some long width
  329. and also with two lines. ",
  330. _output
  331. );
  332. view.X = 1;
  333. view.Y = 1;
  334. view.Width = 10;
  335. view.Height = 1;
  336. Assert.Equal (new Rectangle (1, 1, 10, 1), view.Frame);
  337. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Viewport);
  338. Assert.Equal (new Rectangle (0, 0, 30, 2), view._needsDisplayRect);
  339. top.Draw ();
  340. TestHelpers.AssertDriverContentsWithFrameAre (
  341. @"
  342. At 0,0
  343. A text wit",
  344. _output
  345. );
  346. Application.End (runState);
  347. }
  348. [Fact]
  349. [TestRespondersDisposed]
  350. public void Dispose_View ()
  351. {
  352. var view = new View ();
  353. Assert.NotNull (view.Margin);
  354. Assert.NotNull (view.Border);
  355. Assert.NotNull (view.Padding);
  356. #if DEBUG_IDISPOSABLE
  357. Assert.Equal (4, Responder.Instances.Count);
  358. #endif
  359. view.Dispose ();
  360. Assert.Null (view.Margin);
  361. Assert.Null (view.Border);
  362. Assert.Null (view.Padding);
  363. }
  364. [Fact]
  365. [AutoInitShutdown]
  366. public void DrawContentComplete_Event_Is_Always_Called ()
  367. {
  368. var viewCalled = false;
  369. var tvCalled = false;
  370. var view = new View { Width = 10, Height = 10, Text = "View" };
  371. view.DrawContentComplete += (s, e) => viewCalled = true;
  372. var tv = new TextView { Y = 11, Width = 10, Height = 10 };
  373. tv.DrawContentComplete += (s, e) => tvCalled = true;
  374. var top = new Toplevel ();
  375. top.Add (view, tv);
  376. Application.Begin (top);
  377. Assert.True (viewCalled);
  378. Assert.True (tvCalled);
  379. }
  380. [Fact]
  381. [AutoInitShutdown]
  382. public void Frame_Set_After_Initialize_Update_NeededDisplay ()
  383. {
  384. var frame = new FrameView ();
  385. var label = new Label
  386. {
  387. ColorScheme = Colors.ColorSchemes ["Menu"], X = 0, Y = 0, Text = "This should be the first line."
  388. };
  389. var button = new Button
  390. {
  391. X = 0, // don't overcomplicate unit tests
  392. Y = 1,
  393. Text = "Press me!"
  394. };
  395. frame.Add (label, button);
  396. frame.X = Pos.Center ();
  397. frame.Y = Pos.Center ();
  398. frame.Width = 40;
  399. frame.Height = 8;
  400. Toplevel top = new ();
  401. top.Add (frame);
  402. RunState runState = Application.Begin (top);
  403. top.LayoutComplete += (s, e) => { Assert.Equal (new Rectangle (0, 0, 80, 25), top._needsDisplayRect); };
  404. frame.LayoutComplete += (s, e) => { Assert.Equal (new Rectangle (0, 0, 40, 8), frame._needsDisplayRect); };
  405. label.LayoutComplete += (s, e) => { Assert.Equal (new Rectangle (0, 0, 38, 1), label._needsDisplayRect); };
  406. button.LayoutComplete += (s, e) => { Assert.Equal (new Rectangle (0, 0, 13, 1), button._needsDisplayRect); };
  407. Assert.True (label.AutoSize);
  408. Assert.Equal (new Rectangle (0, 0, 80, 25), top.Frame);
  409. Assert.Equal (new Rectangle (20, 8, 40, 8), frame.Frame);
  410. Assert.Equal (
  411. new Rectangle (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 Rectangle (0, 0, 30, 1), label.Frame);
  420. Assert.Equal (new Rectangle (0, 1, 13, 1), button.Frame); // this proves frame was set
  421. Application.End (runState);
  422. }
  423. [Fact]
  424. [AutoInitShutdown]
  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. [AutoInitShutdown]
  435. public void GetNormalColor_ColorScheme ()
  436. {
  437. var view = new View { ColorScheme = Colors.ColorSchemes ["Base"] };
  438. Assert.Equal (view.ColorScheme.Normal, view.GetNormalColor ());
  439. view.Enabled = false;
  440. Assert.Equal (view.ColorScheme.Disabled, view.GetNormalColor ());
  441. view.Dispose ();
  442. }
  443. [Fact]
  444. [AutoInitShutdown]
  445. public void Incorrect_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  446. {
  447. var label = new Label { Text = "At 0,0" };
  448. var view = new DerivedView
  449. {
  450. X = 2,
  451. Y = 2,
  452. Width = 30,
  453. Height = 2,
  454. Text = "A text with some long width\n and also with two lines."
  455. };
  456. Toplevel top = new ();
  457. top.Add (label, view);
  458. RunState runState = Application.Begin (top);
  459. view.Draw ();
  460. TestHelpers.AssertDriverContentsWithFrameAre (
  461. @"
  462. At 0,0
  463. A text with some long width
  464. and also with two lines. ",
  465. _output
  466. );
  467. view.Frame = new Rectangle (3, 3, 10, 1);
  468. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Viewport);
  469. Assert.Equal (new Rectangle (0, 0, 10, 1), view._needsDisplayRect);
  470. view.Draw ();
  471. TestHelpers.AssertDriverContentsWithFrameAre (
  472. @"
  473. At 0,0
  474. A text with some long width
  475. A text witith two lines. ",
  476. _output
  477. );
  478. Application.End (runState);
  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 Rectangle (3, 3, 10, 1), view.Frame);
  509. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Viewport);
  510. Assert.Equal (new Rectangle (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. }
  521. [Fact]
  522. [AutoInitShutdown]
  523. public void Incorrect_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  524. {
  525. var label = new Label { Text = "At 0,0" };
  526. var view = new DerivedView
  527. {
  528. X = 2,
  529. Y = 2,
  530. Width = 30,
  531. Height = 2,
  532. Text = "A text with some long width\n and also with two lines."
  533. };
  534. Toplevel top = new ();
  535. top.Add (label, view);
  536. RunState runState = Application.Begin (top);
  537. view.Draw ();
  538. TestHelpers.AssertDriverContentsWithFrameAre (
  539. @"
  540. At 0,0
  541. A text with some long width
  542. and also with two lines. ",
  543. _output
  544. );
  545. view.Frame = new Rectangle (1, 1, 10, 1);
  546. Assert.Equal (new Rectangle (1, 1, 10, 1), view.Frame);
  547. Assert.Equal (LayoutStyle.Absolute, view.LayoutStyle);
  548. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Viewport);
  549. Assert.Equal (new Rectangle (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. }
  561. [Fact]
  562. [AutoInitShutdown]
  563. public void Incorrect_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  564. {
  565. var label = new Label { Text = "At 0,0" };
  566. var view = new DerivedView
  567. {
  568. X = 2,
  569. Y = 2,
  570. Width = 30,
  571. Height = 2,
  572. Text = "A text with some long width\n and also with two lines."
  573. };
  574. Toplevel top = new ();
  575. top.Add (label, view);
  576. RunState runState = Application.Begin (top);
  577. view.Draw ();
  578. TestHelpers.AssertDriverContentsWithFrameAre (
  579. @"
  580. At 0,0
  581. A text with some long width
  582. and also with two lines. ",
  583. _output
  584. );
  585. view.X = 1;
  586. view.Y = 1;
  587. view.Width = 10;
  588. view.Height = 1;
  589. Assert.Equal (new Rectangle (1, 1, 10, 1), view.Frame);
  590. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Viewport);
  591. Assert.Equal (new Rectangle (0, 0, 30, 2), view._needsDisplayRect);
  592. view.Draw ();
  593. TestHelpers.AssertDriverContentsWithFrameAre (
  594. @"
  595. At 0,0
  596. A text wit
  597. A text with some long width
  598. and also with two lines. ",
  599. _output
  600. );
  601. Application.End (runState);
  602. }
  603. [Fact]
  604. public void Internal_Tests ()
  605. {
  606. var rect = new Rectangle (1, 1, 10, 1);
  607. var view = new View { Frame = rect };
  608. }
  609. [Fact]
  610. [SetupFakeDriver]
  611. public void SetText_RendersCorrectly ()
  612. {
  613. View view;
  614. var text = "test";
  615. view = new Label { Text = text };
  616. view.BeginInit ();
  617. view.EndInit ();
  618. view.Draw ();
  619. TestHelpers.AssertDriverContentsWithFrameAre (text, _output);
  620. }
  621. [Fact]
  622. [TestRespondersDisposed]
  623. public void New_Initializes ()
  624. {
  625. // Parameterless
  626. var r = new View ();
  627. Assert.NotNull (r);
  628. Assert.True (r.Enabled);
  629. Assert.True (r.Visible);
  630. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  631. Assert.Equal ($"View(){r.Viewport}", r.ToString ());
  632. Assert.False (r.CanFocus);
  633. Assert.False (r.HasFocus);
  634. Assert.Equal (new Rectangle (0, 0, 0, 0), r.Viewport);
  635. Assert.Equal (new Rectangle (0, 0, 0, 0), r.Frame);
  636. Assert.Null (r.Focused);
  637. Assert.Null (r.ColorScheme);
  638. Assert.Equal (0, r.Width);
  639. Assert.Equal (0, r.Height);
  640. Assert.Equal (0, r.X);
  641. Assert.Equal (0, r.Y);
  642. Assert.False (r.IsCurrentTop);
  643. Assert.Empty (r.Id);
  644. Assert.Empty (r.Subviews);
  645. Assert.False (r.WantContinuousButtonPressed);
  646. Assert.False (r.WantMousePositionReports);
  647. Assert.Null (r.SuperView);
  648. Assert.Null (r.MostFocused);
  649. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  650. r.Dispose ();
  651. // Empty Rect
  652. r = new View { Frame = Rectangle.Empty };
  653. Assert.NotNull (r);
  654. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  655. Assert.Equal ($"View(){r.Viewport}", r.ToString ());
  656. Assert.False (r.CanFocus);
  657. Assert.False (r.HasFocus);
  658. Assert.Equal (new Rectangle (0, 0, 0, 0), r.Viewport);
  659. Assert.Equal (new Rectangle (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 View { Frame = new Rectangle (1, 2, 3, 4) };
  677. Assert.NotNull (r);
  678. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  679. Assert.Equal ($"View(){r.Frame}", r.ToString ());
  680. Assert.False (r.CanFocus);
  681. Assert.False (r.HasFocus);
  682. Assert.Equal (new Rectangle (0, 0, 3, 4), r.Viewport);
  683. Assert.Equal (new Rectangle (1, 2, 3, 4), r.Frame);
  684. Assert.Null (r.Focused);
  685. Assert.Null (r.ColorScheme);
  686. Assert.Equal (3, r.Width);
  687. Assert.Equal (4, r.Height);
  688. Assert.Equal (1, r.X);
  689. Assert.Equal (2, r.Y);
  690. Assert.False (r.IsCurrentTop);
  691. Assert.Empty (r.Id);
  692. Assert.Empty (r.Subviews);
  693. Assert.False (r.WantContinuousButtonPressed);
  694. Assert.False (r.WantMousePositionReports);
  695. Assert.Null (r.SuperView);
  696. Assert.Null (r.MostFocused);
  697. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  698. r.Dispose ();
  699. // Initializes a view with a vertical direction
  700. r = new View
  701. {
  702. Text = "Vertical View", TextDirection = TextDirection.TopBottom_LeftRight, AutoSize = true
  703. }; // BUGBUG: AutoSize or Height need be set
  704. Assert.NotNull (r);
  705. Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
  706. // BUGBUG: IsInitialized must be true to process calculation
  707. r.BeginInit ();
  708. r.EndInit ();
  709. Assert.False (r.CanFocus);
  710. Assert.False (r.HasFocus);
  711. Assert.Equal (new Rectangle (0, 0, 1, 13), r.Viewport);
  712. Assert.Equal (new Rectangle (0, 0, 1, 13), r.Frame);
  713. Assert.Null (r.Focused);
  714. Assert.Null (r.ColorScheme);
  715. Assert.False (r.IsCurrentTop);
  716. #if DEBUG
  717. Assert.Equal ("Vertical View", r.Id);
  718. #else
  719. Assert.Equal (string.Empty, r.Id);
  720. #endif
  721. Assert.Empty (r.Subviews);
  722. Assert.False (r.WantContinuousButtonPressed);
  723. Assert.False (r.WantMousePositionReports);
  724. Assert.Null (r.SuperView);
  725. Assert.Null (r.MostFocused);
  726. Assert.Equal (TextDirection.TopBottom_LeftRight, r.TextDirection);
  727. r.Dispose ();
  728. }
  729. [Fact]
  730. [TestRespondersDisposed]
  731. public void New_Methods_Return_False ()
  732. {
  733. var r = new View ();
  734. Assert.False (r.OnKeyDown (new Key { KeyCode = KeyCode.Null }));
  735. //Assert.False (r.OnKeyDown (new KeyEventArgs () { Key = Key.Unknown }));
  736. Assert.False (r.OnKeyUp (new Key { KeyCode = KeyCode.Null }));
  737. Assert.False (r.NewMouseEvent (new MouseEvent { Flags = MouseFlags.AllEvents }));
  738. Assert.False (r.NewMouseEnterEvent (new MouseEvent { Flags = MouseFlags.AllEvents }));
  739. Assert.False (r.NewMouseLeaveEvent (new MouseEvent { Flags = MouseFlags.AllEvents }));
  740. var v1 = new View ();
  741. Assert.False (r.OnEnter (v1));
  742. v1.Dispose ();
  743. var v2 = new View ();
  744. Assert.False (r.OnLeave (v2));
  745. v2.Dispose ();
  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 ColorScheme () };
  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 Rectangle (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 Rectangle (1, 2, 3, 4), view.Frame);
  793. Assert.False (view.Viewport.IsEmpty);
  794. Assert.Equal (new Rectangle (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 View ();
  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 View { 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 View ();
  826. view.X = 1;
  827. view.Y = 2;
  828. view.Width = 3;
  829. view.Height = 4;
  830. super = new View { Frame = new Rectangle (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 Rectangle (1, 2, 3, 4), view.Frame);
  841. Assert.False (view.Viewport.IsEmpty);
  842. Assert.Equal (new Rectangle (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. // BUGBUG: AutoSize is false and size wasn't provided so it's 0,0
  851. Assert.Equal (0, view.Frame.Width);
  852. Assert.Equal (0, view.Height);
  853. var win = new Window ();
  854. win.Add (view);
  855. Toplevel top = new ();
  856. top.Add (win);
  857. RunState rs = Application.Begin (top);
  858. view.AutoSize = true;
  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. }