ViewTests.cs 38 KB

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