ViewTests.cs 40 KB

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