ViewTests.cs 37 KB

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