ViewTests.cs 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  1. using System.Text;
  2. using Xunit.Abstractions;
  3. // Alias Console to MockConsole so we don't accidentally use Console
  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. [TestRespondersDisposed]
  11. public void Added_Removed ()
  12. {
  13. var v = new View { Frame = new Rect (0, 0, 10, 24) };
  14. var t = new View ();
  15. v.Added += (s, e) =>
  16. {
  17. Assert.Same (v.SuperView, e.Parent);
  18. Assert.Same (t, e.Parent);
  19. Assert.Same (v, e.Child);
  20. };
  21. v.Removed += (s, e) =>
  22. {
  23. Assert.Same (t, e.Parent);
  24. Assert.Same (v, e.Child);
  25. Assert.True (v.SuperView == null);
  26. };
  27. t.Add (v);
  28. Assert.True (t.Subviews.Count == 1);
  29. t.Remove (v);
  30. Assert.True (t.Subviews.Count == 0);
  31. t.Dispose ();
  32. v.Dispose ();
  33. }
  34. [Fact]
  35. [AutoInitShutdown]
  36. public void Clear_Bounds_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  37. {
  38. var view = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  39. view.DrawContent += (s, e) =>
  40. {
  41. Rect savedClip = Application.Driver.Clip;
  42. Application.Driver.Clip = new Rect (1, 1, view.Bounds.Width, view.Bounds.Height);
  43. for (var row = 0; row < view.Bounds.Height; row++)
  44. {
  45. Application.Driver.Move (1, row + 1);
  46. for (var col = 0; col < view.Bounds.Width; col++)
  47. {
  48. Application.Driver.AddStr ($"{col}");
  49. }
  50. }
  51. Application.Driver.Clip = savedClip;
  52. e.Cancel = true;
  53. };
  54. Application.Top.Add (view);
  55. Application.Begin (Application.Top);
  56. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  57. var expected = @"
  58. ┌──────────────────┐
  59. │012345678910111213│
  60. │012345678910111213│
  61. │012345678910111213│
  62. │012345678910111213│
  63. │012345678910111213│
  64. │012345678910111213│
  65. │012345678910111213│
  66. │012345678910111213│
  67. └──────────────────┘
  68. ";
  69. Rect pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  70. Assert.Equal (new Rect (0, 0, 20, 10), pos);
  71. view.Clear (view.Frame);
  72. expected = @"
  73. ";
  74. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  75. Assert.Equal (Rect.Empty, pos);
  76. }
  77. [Fact]
  78. [AutoInitShutdown]
  79. public void Clear_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  80. {
  81. var view = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  82. view.DrawContent += (s, e) =>
  83. {
  84. Rect savedClip = Application.Driver.Clip;
  85. Application.Driver.Clip = new Rect (1, 1, view.Bounds.Width, view.Bounds.Height);
  86. for (var row = 0; row < view.Bounds.Height; row++)
  87. {
  88. Application.Driver.Move (1, row + 1);
  89. for (var col = 0; col < view.Bounds.Width; col++)
  90. {
  91. Application.Driver.AddStr ($"{col}");
  92. }
  93. }
  94. Application.Driver.Clip = savedClip;
  95. e.Cancel = true;
  96. };
  97. Application.Top.Add (view);
  98. Application.Begin (Application.Top);
  99. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  100. var expected = @"
  101. ┌──────────────────┐
  102. │012345678910111213│
  103. │012345678910111213│
  104. │012345678910111213│
  105. │012345678910111213│
  106. │012345678910111213│
  107. │012345678910111213│
  108. │012345678910111213│
  109. │012345678910111213│
  110. └──────────────────┘
  111. ";
  112. Rect pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  113. Assert.Equal (new Rect (0, 0, 20, 10), pos);
  114. view.Clear (view.Frame);
  115. expected = @"
  116. ";
  117. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  118. Assert.Equal (Rect.Empty, pos);
  119. }
  120. [Theory]
  121. [AutoInitShutdown]
  122. [InlineData (true)]
  123. [InlineData (false)]
  124. public void Clear_Does_Not_Spillover_Its_Parent (bool label)
  125. {
  126. var root = new View { Width = 20, Height = 10, ColorScheme = Colors.ColorSchemes ["Base"] };
  127. View v = label
  128. ? new Label { Text = new string ('c', 100) }
  129. : new TextView { Height = 1, Text = new string ('c', 100), Width = Dim.Fill () };
  130. root.Add (v);
  131. Application.Top.Add (root);
  132. RunState runState = Application.Begin (Application.Top);
  133. if (label)
  134. {
  135. Assert.True (v.AutoSize);
  136. Assert.False (v.CanFocus);
  137. Assert.Equal (new Rect (0, 0, 100, 1), v.Frame);
  138. }
  139. else
  140. {
  141. Assert.False (v.AutoSize);
  142. Assert.True (v.CanFocus);
  143. Assert.Equal (new Rect (0, 0, 20, 1), v.Frame);
  144. }
  145. TestHelpers.AssertDriverContentsWithFrameAre (
  146. @"
  147. cccccccccccccccccccc",
  148. _output
  149. );
  150. Attribute [] attributes =
  151. {
  152. Colors.ColorSchemes ["TopLevel"].Normal,
  153. Colors.ColorSchemes ["Base"].Normal,
  154. Colors.ColorSchemes ["Base"].Focus
  155. };
  156. if (label)
  157. {
  158. TestHelpers.AssertDriverAttributesAre (
  159. @"
  160. 111111111111111111110
  161. 111111111111111111110",
  162. Application.Driver,
  163. attributes
  164. );
  165. }
  166. else
  167. {
  168. TestHelpers.AssertDriverAttributesAre (
  169. @"
  170. 222222222222222222220
  171. 111111111111111111110",
  172. Application.Driver,
  173. attributes
  174. );
  175. }
  176. if (label)
  177. {
  178. root.CanFocus = true;
  179. v.CanFocus = true;
  180. Assert.False (v.HasFocus);
  181. v.SetFocus ();
  182. Assert.True (v.HasFocus);
  183. Application.Refresh ();
  184. TestHelpers.AssertDriverAttributesAre (
  185. @"
  186. 222222222222222222220
  187. 111111111111111111110",
  188. Application.Driver,
  189. attributes
  190. );
  191. }
  192. Application.End (runState);
  193. }
  194. [Fact]
  195. [AutoInitShutdown]
  196. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  197. {
  198. var label = new Label { Text = "At 0,0" };
  199. var view = new DerivedView
  200. {
  201. X = 2,
  202. Y = 2,
  203. Width = 30,
  204. Height = 2,
  205. Text = "A text with some long width\n and also with two lines."
  206. };
  207. Toplevel top = Application.Top;
  208. top.Add (label, view);
  209. RunState runState = Application.Begin (top);
  210. TestHelpers.AssertDriverContentsWithFrameAre (
  211. @"
  212. At 0,0
  213. A text with some long width
  214. and also with two lines. ",
  215. _output
  216. );
  217. view.Frame = new Rect (3, 3, 10, 1);
  218. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  219. Assert.Equal (LayoutStyle.Absolute, view.LayoutStyle);
  220. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  221. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplayRect);
  222. top.Draw ();
  223. TestHelpers.AssertDriverContentsWithFrameAre (
  224. @"
  225. At 0,0
  226. A text wit",
  227. _output
  228. );
  229. Application.End (runState);
  230. }
  231. [Fact]
  232. [AutoInitShutdown]
  233. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  234. {
  235. var label = new Label { Text = "At 0,0" };
  236. var view = new DerivedView
  237. {
  238. X = 2,
  239. Y = 2,
  240. Width = 30,
  241. Height = 2,
  242. Text = "A text with some long width\n and also with two lines."
  243. };
  244. Toplevel top = Application.Top;
  245. top.Add (label, view);
  246. RunState runState = Application.Begin (top);
  247. top.Draw ();
  248. TestHelpers.AssertDriverContentsWithFrameAre (
  249. @"
  250. At 0,0
  251. A text with some long width
  252. and also with two lines. ",
  253. _output
  254. );
  255. view.X = 3;
  256. view.Y = 3;
  257. view.Width = 10;
  258. view.Height = 1;
  259. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  260. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  261. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplayRect);
  262. top.Draw ();
  263. TestHelpers.AssertDriverContentsWithFrameAre (
  264. @"
  265. At 0,0
  266. A text wit",
  267. _output
  268. );
  269. Application.End (runState);
  270. }
  271. [Fact]
  272. [AutoInitShutdown]
  273. public void Correct_Redraw_Bounds_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 = Application.Top;
  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 Rect (1, 1, 10, 1);
  296. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  297. Assert.Equal (LayoutStyle.Absolute, view.LayoutStyle);
  298. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  299. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplayRect);
  300. top.Draw ();
  301. TestHelpers.AssertDriverContentsWithFrameAre (
  302. @"
  303. At 0,0
  304. A text wit",
  305. _output
  306. );
  307. Application.End (runState);
  308. }
  309. [Fact]
  310. [AutoInitShutdown]
  311. public void Correct_Redraw_Bounds_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 = Application.Top;
  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 Rect (1, 1, 10, 1), view.Frame);
  338. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  339. Assert.Equal (new Rect (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. }
  349. [Fact]
  350. [TestRespondersDisposed]
  351. public void Dispose_View ()
  352. {
  353. var view = new View ();
  354. Assert.NotNull (view.Margin);
  355. Assert.NotNull (view.Border);
  356. Assert.NotNull (view.Padding);
  357. #if DEBUG_IDISPOSABLE
  358. Assert.Equal (4, Responder.Instances.Count);
  359. #endif
  360. view.Dispose ();
  361. Assert.Null (view.Margin);
  362. Assert.Null (view.Border);
  363. Assert.Null (view.Padding);
  364. }
  365. [Fact]
  366. [AutoInitShutdown]
  367. public void DrawContentComplete_Event_Is_Always_Called ()
  368. {
  369. var viewCalled = false;
  370. var tvCalled = false;
  371. var view = new View { Width = 10, Height = 10, Text = "View" };
  372. view.DrawContentComplete += (s, e) => viewCalled = true;
  373. var tv = new TextView { Y = 11, Width = 10, Height = 10 };
  374. tv.DrawContentComplete += (s, e) => tvCalled = true;
  375. Application.Top.Add (view, tv);
  376. Application.Begin (Application.Top);
  377. Assert.True (viewCalled);
  378. Assert.True (tvCalled);
  379. }
  380. [Fact]
  381. [AutoInitShutdown]
  382. public void Frame_Set_After_Initialize_Update_NeededDisplay ()
  383. {
  384. var frame = new FrameView ();
  385. var label = new Label
  386. {
  387. ColorScheme = Colors.ColorSchemes ["Menu"], X = 0, Y = 0, Text = "This should be the first line."
  388. };
  389. var button = new Button
  390. {
  391. X = 0, // don't overcomplicate unit tests
  392. Y = 1,
  393. Text = "Press me!"
  394. };
  395. frame.Add (label, button);
  396. frame.X = Pos.Center ();
  397. frame.Y = Pos.Center ();
  398. frame.Width = 40;
  399. frame.Height = 8;
  400. Toplevel top = Application.Top;
  401. top.Add (frame);
  402. RunState runState = Application.Begin (top);
  403. top.LayoutComplete += (s, e) => { Assert.Equal (new Rect (0, 0, 80, 25), top._needsDisplayRect); };
  404. frame.LayoutComplete += (s, e) => { Assert.Equal (new Rect (0, 0, 40, 8), frame._needsDisplayRect); };
  405. label.LayoutComplete += (s, e) => { Assert.Equal (new Rect (0, 0, 38, 1), label._needsDisplayRect); };
  406. button.LayoutComplete += (s, e) => { Assert.Equal (new Rect (0, 0, 13, 1), button._needsDisplayRect); };
  407. Assert.True (label.AutoSize);
  408. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  409. Assert.Equal (new Rect (20, 8, 40, 8), frame.Frame);
  410. Assert.Equal (
  411. new Rect (20, 8, 60, 16),
  412. new Rect (
  413. frame.Frame.Left,
  414. frame.Frame.Top,
  415. frame.Frame.Right,
  416. frame.Frame.Bottom
  417. )
  418. );
  419. Assert.Equal (new Rect (0, 0, 30, 1), label.Frame);
  420. Assert.Equal (new Rect (0, 1, 13, 1), button.Frame); // this proves frame was set
  421. Application.End (runState);
  422. }
  423. [Fact]
  424. [AutoInitShutdown]
  425. public void GetHotNormalColor_ColorScheme ()
  426. {
  427. var view = new View { ColorScheme = Colors.ColorSchemes ["Base"] };
  428. Assert.Equal (view.ColorScheme.HotNormal, view.GetHotNormalColor ());
  429. view.Enabled = false;
  430. Assert.Equal (view.ColorScheme.Disabled, view.GetHotNormalColor ());
  431. view.Dispose ();
  432. }
  433. [Fact]
  434. [AutoInitShutdown]
  435. public void GetNormalColor_ColorScheme ()
  436. {
  437. var view = new View { ColorScheme = Colors.ColorSchemes ["Base"] };
  438. Assert.Equal (view.ColorScheme.Normal, view.GetNormalColor ());
  439. view.Enabled = false;
  440. Assert.Equal (view.ColorScheme.Disabled, view.GetNormalColor ());
  441. view.Dispose ();
  442. }
  443. [Fact]
  444. [AutoInitShutdown]
  445. public void GetTopSuperView_Test ()
  446. {
  447. var v1 = new View ();
  448. var fv1 = new FrameView ();
  449. fv1.Add (v1);
  450. var tf1 = new TextField ();
  451. var w1 = new Window ();
  452. w1.Add (fv1, tf1);
  453. var top1 = new Toplevel ();
  454. top1.Add (w1);
  455. var v2 = new View ();
  456. var fv2 = new FrameView ();
  457. fv2.Add (v2);
  458. var tf2 = new TextField ();
  459. var w2 = new Window ();
  460. w2.Add (fv2, tf2);
  461. var top2 = new Toplevel ();
  462. top2.Add (w2);
  463. Assert.Equal (top1, v1.GetTopSuperView ());
  464. Assert.Equal (top2, v2.GetTopSuperView ());
  465. v1.Dispose ();
  466. fv1.Dispose ();
  467. tf1.Dispose ();
  468. w1.Dispose ();
  469. top1.Dispose ();
  470. v2.Dispose ();
  471. fv2.Dispose ();
  472. tf2.Dispose ();
  473. w2.Dispose ();
  474. top2.Dispose ();
  475. }
  476. [Fact]
  477. [AutoInitShutdown]
  478. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  479. {
  480. var label = new Label { Text = "At 0,0" };
  481. var view = new DerivedView
  482. {
  483. X = 2,
  484. Y = 2,
  485. Width = 30,
  486. Height = 2,
  487. Text = "A text with some long width\n and also with two lines."
  488. };
  489. Toplevel top = Application.Top;
  490. top.Add (label, view);
  491. RunState runState = Application.Begin (top);
  492. view.Draw ();
  493. TestHelpers.AssertDriverContentsWithFrameAre (
  494. @"
  495. At 0,0
  496. A text with some long width
  497. and also with two lines. ",
  498. _output
  499. );
  500. view.Frame = new Rect (3, 3, 10, 1);
  501. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  502. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplayRect);
  503. view.Draw ();
  504. TestHelpers.AssertDriverContentsWithFrameAre (
  505. @"
  506. At 0,0
  507. A text with some long width
  508. A text witith two lines. ",
  509. _output
  510. );
  511. Application.End (runState);
  512. }
  513. [Fact]
  514. [AutoInitShutdown]
  515. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  516. {
  517. var label = new Label { Text = "At 0,0" };
  518. var view = new DerivedView
  519. {
  520. X = 2,
  521. Y = 2,
  522. Width = 30,
  523. Height = 2,
  524. Text = "A text with some long width\n and also with two lines."
  525. };
  526. Toplevel top = Application.Top;
  527. top.Add (label, view);
  528. RunState runState = Application.Begin (top);
  529. view.Draw ();
  530. TestHelpers.AssertDriverContentsWithFrameAre (
  531. @"
  532. At 0,0
  533. A text with some long width
  534. and also with two lines. ",
  535. _output
  536. );
  537. view.X = 3;
  538. view.Y = 3;
  539. view.Width = 10;
  540. view.Height = 1;
  541. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  542. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  543. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplayRect);
  544. view.Draw ();
  545. TestHelpers.AssertDriverContentsWithFrameAre (
  546. @"
  547. At 0,0
  548. A text with some long width
  549. A text witith two lines. ",
  550. _output
  551. );
  552. Application.End (runState);
  553. }
  554. [Fact]
  555. [AutoInitShutdown]
  556. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  557. {
  558. var label = new Label { Text = "At 0,0" };
  559. var view = new DerivedView
  560. {
  561. X = 2,
  562. Y = 2,
  563. Width = 30,
  564. Height = 2,
  565. Text = "A text with some long width\n and also with two lines."
  566. };
  567. Toplevel top = Application.Top;
  568. top.Add (label, view);
  569. RunState runState = Application.Begin (top);
  570. view.Draw ();
  571. TestHelpers.AssertDriverContentsWithFrameAre (
  572. @"
  573. At 0,0
  574. A text with some long width
  575. and also with two lines. ",
  576. _output
  577. );
  578. view.Frame = new Rect (1, 1, 10, 1);
  579. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  580. Assert.Equal (LayoutStyle.Absolute, view.LayoutStyle);
  581. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  582. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplayRect);
  583. view.Draw ();
  584. TestHelpers.AssertDriverContentsWithFrameAre (
  585. @"
  586. At 0,0
  587. A text wit
  588. A text with some long width
  589. and also with two lines. ",
  590. _output
  591. );
  592. Application.End (runState);
  593. }
  594. [Fact]
  595. [AutoInitShutdown]
  596. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  597. {
  598. var label = new Label { Text = "At 0,0" };
  599. var view = new DerivedView
  600. {
  601. X = 2,
  602. Y = 2,
  603. Width = 30,
  604. Height = 2,
  605. Text = "A text with some long width\n and also with two lines."
  606. };
  607. Toplevel top = Application.Top;
  608. top.Add (label, view);
  609. RunState runState = Application.Begin (top);
  610. view.Draw ();
  611. TestHelpers.AssertDriverContentsWithFrameAre (
  612. @"
  613. At 0,0
  614. A text with some long width
  615. and also with two lines. ",
  616. _output
  617. );
  618. view.X = 1;
  619. view.Y = 1;
  620. view.Width = 10;
  621. view.Height = 1;
  622. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  623. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  624. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplayRect);
  625. view.Draw ();
  626. TestHelpers.AssertDriverContentsWithFrameAre (
  627. @"
  628. At 0,0
  629. A text wit
  630. A text with some long width
  631. and also with two lines. ",
  632. _output
  633. );
  634. Application.End (runState);
  635. }
  636. [Fact]
  637. [TestRespondersDisposed]
  638. public void Initialized_Event_Comparing_With_Added_Event ()
  639. {
  640. Application.Init (new FakeDriver ());
  641. var top = new Toplevel { Id = "0" }; // Frame: 0, 0, 80, 25; Bounds: 0, 0, 80, 25
  642. var winAddedToTop = new Window
  643. {
  644. Id = "t", Width = Dim.Fill (), Height = Dim.Fill ()
  645. }; // Frame: 0, 0, 80, 25; Bounds: 0, 0, 78, 23
  646. var v1AddedToWin = new View
  647. {
  648. Id = "v1", Width = Dim.Fill (), Height = Dim.Fill ()
  649. }; // Frame: 1, 1, 78, 23 (because Windows has a border)
  650. var v2AddedToWin = new View
  651. {
  652. Id = "v2", Width = Dim.Fill (), Height = Dim.Fill ()
  653. }; // Frame: 1, 1, 78, 23 (because Windows has a border)
  654. var svAddedTov1 = new View
  655. {
  656. Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill ()
  657. }; // Frame: 1, 1, 78, 23 (same as it's superview v1AddedToWin)
  658. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  659. winAddedToTop.Added += (s, e) =>
  660. {
  661. Assert.Equal (e.Parent.Frame.Width, winAddedToTop.Frame.Width);
  662. Assert.Equal (e.Parent.Frame.Height, winAddedToTop.Frame.Height);
  663. };
  664. v1AddedToWin.Added += (s, e) =>
  665. {
  666. Assert.Equal (e.Parent.Frame.Width, v1AddedToWin.Frame.Width);
  667. Assert.Equal (e.Parent.Frame.Height, v1AddedToWin.Frame.Height);
  668. };
  669. v2AddedToWin.Added += (s, e) =>
  670. {
  671. Assert.Equal (e.Parent.Frame.Width, v2AddedToWin.Frame.Width);
  672. Assert.Equal (e.Parent.Frame.Height, v2AddedToWin.Frame.Height);
  673. };
  674. svAddedTov1.Added += (s, e) =>
  675. {
  676. Assert.Equal (e.Parent.Frame.Width, svAddedTov1.Frame.Width);
  677. Assert.Equal (e.Parent.Frame.Height, svAddedTov1.Frame.Height);
  678. };
  679. top.Initialized += (s, e) =>
  680. {
  681. tc++;
  682. Assert.Equal (1, tc);
  683. Assert.Equal (1, wc);
  684. Assert.Equal (1, v1c);
  685. Assert.Equal (1, v2c);
  686. Assert.Equal (1, sv1c);
  687. Assert.True (top.CanFocus);
  688. Assert.True (winAddedToTop.CanFocus);
  689. Assert.False (v1AddedToWin.CanFocus);
  690. Assert.False (v2AddedToWin.CanFocus);
  691. Assert.False (svAddedTov1.CanFocus);
  692. Application.Refresh ();
  693. };
  694. winAddedToTop.Initialized += (s, e) =>
  695. {
  696. wc++;
  697. Assert.Equal (top.Bounds.Width, winAddedToTop.Frame.Width);
  698. Assert.Equal (top.Bounds.Height, winAddedToTop.Frame.Height);
  699. };
  700. v1AddedToWin.Initialized += (s, e) =>
  701. {
  702. v1c++;
  703. // Top.Frame: 0, 0, 80, 25; Top.Bounds: 0, 0, 80, 25
  704. // BUGBUG: This is wrong, it should be 78, 23. This test has always been broken.
  705. // in no way should the v1AddedToWin.Frame be the same as the Top.Frame/Bounds
  706. // as it is a subview of winAddedToTop, which has a border!
  707. //Assert.Equal (top.Bounds.Width, v1AddedToWin.Frame.Width);
  708. //Assert.Equal (top.Bounds.Height, v1AddedToWin.Frame.Height);
  709. };
  710. v2AddedToWin.Initialized += (s, e) =>
  711. {
  712. v2c++;
  713. // Top.Frame: 0, 0, 80, 25; Top.Bounds: 0, 0, 80, 25
  714. // BUGBUG: This is wrong, it should be 78, 23. This test has always been broken.
  715. // in no way should the v2AddedToWin.Frame be the same as the Top.Frame/Bounds
  716. // as it is a subview of winAddedToTop, which has a border!
  717. //Assert.Equal (top.Bounds.Width, v2AddedToWin.Frame.Width);
  718. //Assert.Equal (top.Bounds.Height, v2AddedToWin.Frame.Height);
  719. };
  720. svAddedTov1.Initialized += (s, e) =>
  721. {
  722. sv1c++;
  723. // Top.Frame: 0, 0, 80, 25; Top.Bounds: 0, 0, 80, 25
  724. // BUGBUG: This is wrong, it should be 78, 23. This test has always been broken.
  725. // in no way should the svAddedTov1.Frame be the same as the Top.Frame/Bounds
  726. // because sv1AddedTov1 is a subview of v1AddedToWin, which is a subview of
  727. // winAddedToTop, which has a border!
  728. //Assert.Equal (top.Bounds.Width, svAddedTov1.Frame.Width);
  729. //Assert.Equal (top.Bounds.Height, svAddedTov1.Frame.Height);
  730. Assert.False (svAddedTov1.CanFocus);
  731. Assert.Throws<InvalidOperationException> (() => svAddedTov1.CanFocus = true);
  732. Assert.False (svAddedTov1.CanFocus);
  733. };
  734. v1AddedToWin.Add (svAddedTov1);
  735. winAddedToTop.Add (v1AddedToWin, v2AddedToWin);
  736. top.Add (winAddedToTop);
  737. Application.Iteration += (s, a) =>
  738. {
  739. Application.Refresh ();
  740. top.Running = false;
  741. };
  742. Application.Run (top);
  743. Application.Shutdown ();
  744. Assert.Equal (1, tc);
  745. Assert.Equal (1, wc);
  746. Assert.Equal (1, v1c);
  747. Assert.Equal (1, v2c);
  748. Assert.Equal (1, sv1c);
  749. Assert.True (top.CanFocus);
  750. Assert.True (winAddedToTop.CanFocus);
  751. Assert.False (v1AddedToWin.CanFocus);
  752. Assert.False (v2AddedToWin.CanFocus);
  753. Assert.False (svAddedTov1.CanFocus);
  754. v1AddedToWin.CanFocus = true;
  755. Assert.False (svAddedTov1.CanFocus); // False because sv1 was disposed and it isn't a subview of v1.
  756. }
  757. [Fact]
  758. [TestRespondersDisposed]
  759. public void Initialized_Event_Will_Be_Invoked_When_Added_Dynamically ()
  760. {
  761. Application.Init (new FakeDriver ());
  762. var t = new Toplevel { Id = "0" };
  763. var w = new Window { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  764. var v1 = new View { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  765. var v2 = new View { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  766. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  767. t.Initialized += (s, e) =>
  768. {
  769. tc++;
  770. Assert.Equal (1, tc);
  771. Assert.Equal (1, wc);
  772. Assert.Equal (1, v1c);
  773. Assert.Equal (1, v2c);
  774. Assert.Equal (0, sv1c); // Added after t in the Application.Iteration.
  775. Assert.True (t.CanFocus);
  776. Assert.True (w.CanFocus);
  777. Assert.False (v1.CanFocus);
  778. Assert.False (v2.CanFocus);
  779. Application.Refresh ();
  780. };
  781. w.Initialized += (s, e) =>
  782. {
  783. wc++;
  784. Assert.Equal (t.Bounds.Width, w.Frame.Width);
  785. Assert.Equal (t.Bounds.Height, w.Frame.Height);
  786. };
  787. v1.Initialized += (s, e) =>
  788. {
  789. v1c++;
  790. //Assert.Equal (t.Bounds.Width, v1.Frame.Width);
  791. //Assert.Equal (t.Bounds.Height, v1.Frame.Height);
  792. };
  793. v2.Initialized += (s, e) =>
  794. {
  795. v2c++;
  796. //Assert.Equal (t.Bounds.Width, v2.Frame.Width);
  797. //Assert.Equal (t.Bounds.Height, v2.Frame.Height);
  798. };
  799. w.Add (v1, v2);
  800. t.Add (w);
  801. Application.Iteration += (s, a) =>
  802. {
  803. var sv1 = new View { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  804. sv1.Initialized += (s, e) =>
  805. {
  806. sv1c++;
  807. Assert.NotEqual (t.Frame.Width, sv1.Frame.Width);
  808. Assert.NotEqual (t.Frame.Height, sv1.Frame.Height);
  809. Assert.False (sv1.CanFocus);
  810. Assert.Throws<InvalidOperationException> (() => sv1.CanFocus = true);
  811. Assert.False (sv1.CanFocus);
  812. };
  813. v1.Add (sv1);
  814. Application.Refresh ();
  815. t.Running = false;
  816. };
  817. Application.Run (t);
  818. Application.Shutdown ();
  819. Assert.Equal (1, tc);
  820. Assert.Equal (1, wc);
  821. Assert.Equal (1, v1c);
  822. Assert.Equal (1, v2c);
  823. Assert.Equal (1, sv1c);
  824. Assert.True (t.CanFocus);
  825. Assert.True (w.CanFocus);
  826. Assert.False (v1.CanFocus);
  827. Assert.False (v2.CanFocus);
  828. }
  829. [Fact]
  830. public void Internal_Tests ()
  831. {
  832. var rect = new Rect (1, 1, 10, 1);
  833. var view = new View { Frame = rect };
  834. }
  835. [Fact]
  836. [TestRespondersDisposed]
  837. public void IsAdded_Added_Removed ()
  838. {
  839. var top = new Toplevel ();
  840. var view = new View ();
  841. Assert.False (view.IsAdded);
  842. top.Add (view);
  843. Assert.True (view.IsAdded);
  844. top.Remove (view);
  845. Assert.False (view.IsAdded);
  846. top.Dispose ();
  847. view.Dispose ();
  848. }
  849. [Theory]
  850. [TestRespondersDisposed]
  851. [InlineData (1)]
  852. [InlineData (2)]
  853. [InlineData (3)]
  854. public void LabelChangeText_RendersCorrectly_Constructors (int choice)
  855. {
  856. var driver = new FakeDriver ();
  857. Application.Init (driver);
  858. try
  859. {
  860. // Create a label with a short text
  861. Label lbl;
  862. var text = "test";
  863. if (choice == 1)
  864. {
  865. // An object initializer should call the default constructor.
  866. lbl = new Label { Text = text };
  867. }
  868. else if (choice == 2)
  869. {
  870. // Calling the default constructor followed by the object initializer.
  871. lbl = new Label { Text = text };
  872. }
  873. else
  874. {
  875. // Calling the Text constructor.
  876. lbl = new Label { Text = text };
  877. }
  878. Application.Top.Add (lbl);
  879. Application.Begin (Application.Top);
  880. // should have the initial text
  881. Assert.Equal ((Rune)'t', driver.Contents [0, 0].Rune);
  882. Assert.Equal ((Rune)'e', driver.Contents [0, 1].Rune);
  883. Assert.Equal ((Rune)'s', driver.Contents [0, 2].Rune);
  884. Assert.Equal ((Rune)'t', driver.Contents [0, 3].Rune);
  885. Assert.Equal ((Rune)' ', driver.Contents [0, 4].Rune);
  886. }
  887. finally
  888. {
  889. Application.Shutdown ();
  890. }
  891. }
  892. [Fact]
  893. [TestRespondersDisposed]
  894. public void New_Initializes ()
  895. {
  896. // Parameterless
  897. var r = new View ();
  898. Assert.NotNull (r);
  899. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  900. Assert.Equal ("View()(0,0,0,0)", r.ToString ());
  901. Assert.False (r.CanFocus);
  902. Assert.False (r.HasFocus);
  903. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  904. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  905. Assert.Null (r.Focused);
  906. Assert.Null (r.ColorScheme);
  907. Assert.Equal (0, r.Width);
  908. Assert.Equal (0, r.Height);
  909. Assert.Equal (0, r.X);
  910. Assert.Equal (0, r.Y);
  911. Assert.False (r.IsCurrentTop);
  912. Assert.Empty (r.Id);
  913. Assert.Empty (r.Subviews);
  914. Assert.False (r.WantContinuousButtonPressed);
  915. Assert.False (r.WantMousePositionReports);
  916. Assert.Null (r.SuperView);
  917. Assert.Null (r.MostFocused);
  918. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  919. r.Dispose ();
  920. // Empty Rect
  921. r = new View { Frame = Rect.Empty };
  922. Assert.NotNull (r);
  923. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  924. Assert.Equal ("View()(0,0,0,0)", r.ToString ());
  925. Assert.False (r.CanFocus);
  926. Assert.False (r.HasFocus);
  927. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  928. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  929. Assert.Null (r.Focused);
  930. Assert.Null (r.ColorScheme);
  931. Assert.Equal (0, r.Width);
  932. Assert.Equal (0, r.Height);
  933. Assert.Equal (0, r.X);
  934. Assert.Equal (0, r.Y);
  935. Assert.False (r.IsCurrentTop);
  936. Assert.Empty (r.Id);
  937. Assert.Empty (r.Subviews);
  938. Assert.False (r.WantContinuousButtonPressed);
  939. Assert.False (r.WantMousePositionReports);
  940. Assert.Null (r.SuperView);
  941. Assert.Null (r.MostFocused);
  942. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  943. r.Dispose ();
  944. // Rect with values
  945. r = new View { Frame = new Rect (1, 2, 3, 4) };
  946. Assert.NotNull (r);
  947. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  948. Assert.Equal ("View()(1,2,3,4)", r.ToString ());
  949. Assert.False (r.CanFocus);
  950. Assert.False (r.HasFocus);
  951. Assert.Equal (new Rect (0, 0, 3, 4), r.Bounds);
  952. Assert.Equal (new Rect (1, 2, 3, 4), r.Frame);
  953. Assert.Null (r.Focused);
  954. Assert.Null (r.ColorScheme);
  955. Assert.Equal (3, r.Width);
  956. Assert.Equal (4, r.Height);
  957. Assert.Equal (1, r.X);
  958. Assert.Equal (2, r.Y);
  959. Assert.False (r.IsCurrentTop);
  960. Assert.Empty (r.Id);
  961. Assert.Empty (r.Subviews);
  962. Assert.False (r.WantContinuousButtonPressed);
  963. Assert.False (r.WantMousePositionReports);
  964. Assert.Null (r.SuperView);
  965. Assert.Null (r.MostFocused);
  966. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  967. r.Dispose ();
  968. // Initializes a view with a vertical direction
  969. r = new View
  970. {
  971. Text = "Vertical View", TextDirection = TextDirection.TopBottom_LeftRight, AutoSize = true
  972. }; // BUGBUG: AutoSize or Height need be set
  973. Assert.NotNull (r);
  974. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  975. // BUGBUG: IsInitialized must be true to process calculation
  976. r.BeginInit ();
  977. r.EndInit ();
  978. #if DEBUG
  979. Assert.Equal ("View(Vertical View)(0,0,1,13)", r.ToString ());
  980. #else
  981. Assert.Equal ("View()(0,0,1,13)", r.ToString ());
  982. #endif
  983. Assert.False (r.CanFocus);
  984. Assert.False (r.HasFocus);
  985. Assert.Equal (new Rect (0, 0, 1, 13), r.Bounds);
  986. Assert.Equal (new Rect (0, 0, 1, 13), r.Frame);
  987. Assert.Null (r.Focused);
  988. Assert.Null (r.ColorScheme);
  989. Assert.False (r.IsCurrentTop);
  990. #if DEBUG
  991. Assert.Equal ("Vertical View", r.Id);
  992. #else
  993. Assert.Equal (string.Empty, r.Id);
  994. #endif
  995. Assert.Empty (r.Subviews);
  996. Assert.False (r.WantContinuousButtonPressed);
  997. Assert.False (r.WantMousePositionReports);
  998. Assert.Null (r.SuperView);
  999. Assert.Null (r.MostFocused);
  1000. Assert.Equal (TextDirection.TopBottom_LeftRight, r.TextDirection);
  1001. r.Dispose ();
  1002. }
  1003. [Fact]
  1004. [TestRespondersDisposed]
  1005. public void New_Methods_Return_False ()
  1006. {
  1007. var r = new View ();
  1008. Assert.False (r.OnKeyDown (new Key { KeyCode = KeyCode.Null }));
  1009. //Assert.False (r.OnKeyDown (new KeyEventArgs () { Key = Key.Unknown }));
  1010. Assert.False (r.OnKeyUp (new Key { KeyCode = KeyCode.Null }));
  1011. Assert.False (r.MouseEvent (new MouseEvent { Flags = MouseFlags.AllEvents }));
  1012. Assert.False (r.OnMouseEnter (new MouseEvent { Flags = MouseFlags.AllEvents }));
  1013. Assert.False (r.OnMouseLeave (new MouseEvent { Flags = MouseFlags.AllEvents }));
  1014. var v1 = new View ();
  1015. Assert.False (r.OnEnter (v1));
  1016. v1.Dispose ();
  1017. var v2 = new View ();
  1018. Assert.False (r.OnLeave (v2));
  1019. v2.Dispose ();
  1020. r.Dispose ();
  1021. // TODO: Add more
  1022. }
  1023. [Fact]
  1024. [AutoInitShutdown]
  1025. public void Test_Nested_Views_With_Height_Equal_To_One ()
  1026. {
  1027. var v = new View { Width = 11, Height = 3, ColorScheme = new ColorScheme () };
  1028. var top = new View { Width = Dim.Fill (), Height = 1 };
  1029. var bottom = new View { Width = Dim.Fill (), Height = 1, Y = 2 };
  1030. top.Add (new Label { Text = "111" });
  1031. v.Add (top);
  1032. v.Add (new LineView (Orientation.Horizontal) { Y = 1 });
  1033. bottom.Add (new Label { Text = "222" });
  1034. v.Add (bottom);
  1035. v.BeginInit ();
  1036. v.EndInit ();
  1037. v.LayoutSubviews ();
  1038. v.Draw ();
  1039. var looksLike =
  1040. @"
  1041. 111
  1042. ───────────
  1043. 222";
  1044. TestHelpers.AssertDriverContentsAre (looksLike, _output);
  1045. v.Dispose ();
  1046. top.Dispose ();
  1047. bottom.Dispose ();
  1048. }
  1049. [Fact]
  1050. [TestRespondersDisposed]
  1051. public void View_With_No_Difference_Between_An_Object_Initializer_Compute_And_A_Absolute ()
  1052. {
  1053. // Object Initializer Computed
  1054. var view = new View { X = 1, Y = 2, Width = 3, Height = 4 };
  1055. // Object Initializer Absolute
  1056. var super = new View { Frame = new Rect (0, 0, 10, 10) };
  1057. super.Add (view);
  1058. super.BeginInit ();
  1059. super.EndInit ();
  1060. super.LayoutSubviews ();
  1061. Assert.Equal (1, view.X);
  1062. Assert.Equal (2, view.Y);
  1063. Assert.Equal (3, view.Width);
  1064. Assert.Equal (4, view.Height);
  1065. Assert.False (view.Frame.IsEmpty);
  1066. Assert.Equal (new Rect (1, 2, 3, 4), view.Frame);
  1067. Assert.False (view.Bounds.IsEmpty);
  1068. Assert.Equal (new Rect (0, 0, 3, 4), view.Bounds);
  1069. view.LayoutSubviews ();
  1070. Assert.Equal (1, view.X);
  1071. Assert.Equal (2, view.Y);
  1072. Assert.Equal (3, view.Width);
  1073. Assert.Equal (4, view.Height);
  1074. Assert.False (view.Frame.IsEmpty);
  1075. Assert.False (view.Bounds.IsEmpty);
  1076. super.Dispose ();
  1077. #if DEBUG_IDISPOSABLE
  1078. Assert.Empty (Responder.Instances);
  1079. #endif
  1080. // Default Constructor
  1081. view = new View ();
  1082. Assert.Equal (0, view.X);
  1083. Assert.Equal (0, view.Y);
  1084. Assert.Equal (0, view.Width);
  1085. Assert.Equal (0, view.Height);
  1086. Assert.True (view.Frame.IsEmpty);
  1087. Assert.True (view.Bounds.IsEmpty);
  1088. view.Dispose ();
  1089. // Object Initializer
  1090. view = new View { X = 1, Y = 2, Text = "" };
  1091. Assert.Equal (1, view.X);
  1092. Assert.Equal (2, view.Y);
  1093. Assert.Equal (0, view.Width);
  1094. Assert.Equal (0, view.Height);
  1095. Assert.False (view.Frame.IsEmpty);
  1096. Assert.True (view.Bounds.IsEmpty);
  1097. view.Dispose ();
  1098. // Default Constructor and post assignment equivalent to Object Initializer
  1099. view = new View ();
  1100. view.X = 1;
  1101. view.Y = 2;
  1102. view.Width = 3;
  1103. view.Height = 4;
  1104. super = new View { Frame = new Rect (0, 0, 10, 10) };
  1105. super.Add (view);
  1106. super.BeginInit ();
  1107. super.EndInit ();
  1108. super.LayoutSubviews ();
  1109. Assert.Equal (1, view.X);
  1110. Assert.Equal (2, view.Y);
  1111. Assert.Equal (3, view.Width);
  1112. Assert.Equal (4, view.Height);
  1113. Assert.False (view.Frame.IsEmpty);
  1114. Assert.Equal (new Rect (1, 2, 3, 4), view.Frame);
  1115. Assert.False (view.Bounds.IsEmpty);
  1116. Assert.Equal (new Rect (0, 0, 3, 4), view.Bounds);
  1117. super.Dispose ();
  1118. }
  1119. [Fact]
  1120. [AutoInitShutdown]
  1121. public void Visible_Clear_The_View_Output ()
  1122. {
  1123. var view = new View { Text = "Testing visibility." }; // use View, not Label to avoid AutoSize == true
  1124. // BUGBUG: AutoSize is false and size wasn't provided so it's 0,0
  1125. Assert.Equal (0, view.Frame.Width);
  1126. Assert.Equal (0, view.Height);
  1127. var win = new Window ();
  1128. win.Add (view);
  1129. Toplevel top = Application.Top;
  1130. top.Add (win);
  1131. RunState rs = Application.Begin (top);
  1132. view.AutoSize = true;
  1133. Assert.Equal ("Testing visibility.".Length, view.Frame.Width);
  1134. Assert.True (view.Visible);
  1135. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  1136. TestHelpers.AssertDriverContentsWithFrameAre (
  1137. @"
  1138. ┌────────────────────────────┐
  1139. │Testing visibility. │
  1140. │ │
  1141. │ │
  1142. └────────────────────────────┘
  1143. ",
  1144. _output
  1145. );
  1146. view.Visible = false;
  1147. var firstIteration = false;
  1148. Application.RunIteration (ref rs, ref firstIteration);
  1149. TestHelpers.AssertDriverContentsWithFrameAre (
  1150. @"
  1151. ┌────────────────────────────┐
  1152. │ │
  1153. │ │
  1154. │ │
  1155. └────────────────────────────┘
  1156. ",
  1157. _output
  1158. );
  1159. Application.End (rs);
  1160. }
  1161. [Fact]
  1162. [AutoInitShutdown]
  1163. public void Visible_Sets_Also_Sets_Subviews ()
  1164. {
  1165. var button = new Button { Text = "Click Me" };
  1166. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  1167. win.Add (button);
  1168. Toplevel top = Application.Top;
  1169. top.Add (win);
  1170. var iterations = 0;
  1171. Application.Iteration += (s, a) =>
  1172. {
  1173. iterations++;
  1174. Assert.True (button.Visible);
  1175. Assert.True (button.CanFocus);
  1176. Assert.True (button.HasFocus);
  1177. Assert.True (win.Visible);
  1178. Assert.True (win.CanFocus);
  1179. Assert.True (win.HasFocus);
  1180. Assert.True (RunesCount () > 0);
  1181. win.Visible = false;
  1182. Assert.True (button.Visible);
  1183. Assert.True (button.CanFocus);
  1184. Assert.False (button.HasFocus);
  1185. Assert.False (win.Visible);
  1186. Assert.True (win.CanFocus);
  1187. Assert.False (win.HasFocus);
  1188. button.SetFocus ();
  1189. Assert.False (button.HasFocus);
  1190. Assert.False (win.HasFocus);
  1191. win.SetFocus ();
  1192. Assert.False (button.HasFocus);
  1193. Assert.False (win.HasFocus);
  1194. top.Draw ();
  1195. Assert.True (RunesCount () == 0);
  1196. win.Visible = true;
  1197. win.FocusFirst ();
  1198. Assert.True (button.HasFocus);
  1199. Assert.True (win.HasFocus);
  1200. top.Draw ();
  1201. Assert.True (RunesCount () > 0);
  1202. Application.RequestStop ();
  1203. };
  1204. Application.Run ();
  1205. Assert.Equal (1, iterations);
  1206. int RunesCount ()
  1207. {
  1208. Cell [,] contents = ((FakeDriver)Application.Driver).Contents;
  1209. var runesCount = 0;
  1210. for (var i = 0; i < Application.Driver.Rows; i++)
  1211. {
  1212. for (var j = 0; j < Application.Driver.Cols; j++)
  1213. {
  1214. if (contents [i, j].Rune != (Rune)' ')
  1215. {
  1216. runesCount++;
  1217. }
  1218. }
  1219. }
  1220. return runesCount;
  1221. }
  1222. }
  1223. public class DerivedView : View
  1224. {
  1225. public DerivedView () { CanFocus = true; }
  1226. public bool IsKeyDown { get; set; }
  1227. public bool IsKeyPress { get; set; }
  1228. public bool IsKeyUp { get; set; }
  1229. public override string Text { get; set; }
  1230. public override void OnDrawContent (Rect contentArea)
  1231. {
  1232. var idx = 0;
  1233. // BUGBUG: v2 - this should use Bounds, not Frame
  1234. for (var r = 0; r < Frame.Height; r++)
  1235. {
  1236. for (var c = 0; c < Frame.Width; c++)
  1237. {
  1238. if (idx < Text.Length)
  1239. {
  1240. char rune = Text [idx];
  1241. if (rune != '\n')
  1242. {
  1243. AddRune (c, r, (Rune)Text [idx]);
  1244. }
  1245. idx++;
  1246. if (rune == '\n')
  1247. {
  1248. break;
  1249. }
  1250. }
  1251. }
  1252. }
  1253. ClearLayoutNeeded ();
  1254. ClearNeedsDisplay ();
  1255. }
  1256. public override bool OnKeyDown (Key keyEvent)
  1257. {
  1258. IsKeyDown = true;
  1259. return true;
  1260. }
  1261. public override bool OnKeyUp (Key keyEvent)
  1262. {
  1263. IsKeyUp = true;
  1264. return true;
  1265. }
  1266. public override bool OnProcessKeyDown (Key keyEvent)
  1267. {
  1268. IsKeyPress = true;
  1269. return true;
  1270. }
  1271. }
  1272. }