ViewTests.cs 39 KB

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