PanelViewTests.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Xunit;
  7. using Xunit.Abstractions;
  8. namespace Terminal.Gui.ViewTests {
  9. public class PanelViewTests {
  10. readonly ITestOutputHelper output;
  11. public PanelViewTests (ITestOutputHelper output)
  12. {
  13. this.output = output;
  14. }
  15. [Fact]
  16. public void Constructor_Defaults ()
  17. {
  18. var pv = new PanelView ();
  19. Assert.False (pv.CanFocus);
  20. Assert.False (pv.Visible);
  21. Assert.False (pv.UsePanelFrame);
  22. Assert.Null (pv.Child);
  23. pv = new PanelView (new Label ("This is a test."));
  24. Assert.False (pv.CanFocus);
  25. Assert.True (pv.Visible);
  26. Assert.False (pv.UsePanelFrame);
  27. Assert.NotNull (pv.Child);
  28. Assert.NotNull (pv.Border);
  29. Assert.NotNull (pv.Child.Border);
  30. }
  31. [Fact]
  32. public void Child_Sets_To_Null_Remove_From_Subviews_PanelView ()
  33. {
  34. var pv = new PanelView (new Label ("This is a test."));
  35. Assert.NotNull (pv.Child);
  36. Assert.Single (pv.Subviews [0].Subviews);
  37. pv.Child = null;
  38. Assert.Null (pv.Child);
  39. Assert.Empty (pv.Subviews [0].Subviews);
  40. }
  41. [Fact]
  42. public void Add_View_Also_Sets_Child ()
  43. {
  44. var pv = new PanelView ();
  45. Assert.Null (pv.Child);
  46. Assert.Empty (pv.Subviews [0].Subviews);
  47. pv.Add (new Label ("This is a test."));
  48. Assert.NotNull (pv.Child);
  49. Assert.Single (pv.Subviews [0].Subviews);
  50. }
  51. [Fact]
  52. public void Add_More_Views_Remove_Last_Child_Before__Only_One_Is_Allowed ()
  53. {
  54. var pv = new PanelView (new Label ("This is a test."));
  55. Assert.NotNull (pv.Child);
  56. Assert.Single (pv.Subviews [0].Subviews);
  57. Assert.IsType<Label> (pv.Child);
  58. pv.Add (new TextField ("This is a test."));
  59. Assert.NotNull (pv.Child);
  60. Assert.Single (pv.Subviews [0].Subviews);
  61. Assert.IsNotType<Label> (pv.Child);
  62. Assert.IsType<TextField> (pv.Child);
  63. }
  64. [Fact]
  65. public void Remove_RemoveAll_View_Also_Sets_Child_To_Null ()
  66. {
  67. var pv = new PanelView (new Label ("This is a test."));
  68. Assert.NotNull (pv.Child);
  69. Assert.Single (pv.Subviews [0].Subviews);
  70. pv.Remove (pv.Child);
  71. Assert.Null (pv.Child);
  72. Assert.Empty (pv.Subviews [0].Subviews);
  73. pv = new PanelView (new Label ("This is a test."));
  74. Assert.NotNull (pv.Child);
  75. Assert.Single (pv.Subviews [0].Subviews);
  76. pv.RemoveAll ();
  77. Assert.Null (pv.Child);
  78. Assert.Empty (pv.Subviews [0].Subviews);
  79. }
  80. [Fact]
  81. [AutoInitShutdown]
  82. public void AdjustContainer_Without_Border ()
  83. {
  84. var top = Application.Top;
  85. var win = new Window ();
  86. var pv = new PanelView (new Label ("This is a test."));
  87. win.Add (pv);
  88. top.Add (win);
  89. Application.Begin (top);
  90. Assert.Equal (new Rect (0, 0, 15, 1), pv.Frame);
  91. Assert.Equal (new Rect (0, 0, 15, 1), pv.Child.Frame);
  92. }
  93. [Fact]
  94. [AutoInitShutdown]
  95. public void AdjustContainer_With_Border_Absolute_Values ()
  96. {
  97. var top = Application.Top;
  98. var win = new Window ();
  99. var pv = new PanelView (new Label ("This is a test.") {
  100. Border = new Border () {
  101. BorderStyle = BorderStyle.Double,
  102. BorderThickness = new Thickness (1, 2, 3, 4),
  103. Padding = new Thickness (1, 2, 3, 4)
  104. }
  105. });
  106. win.Add (pv);
  107. top.Add (win);
  108. Application.Begin (top);
  109. Assert.False (pv.Child.Border.Effect3D);
  110. Assert.Equal (new Rect (0, 0, 25, 15), pv.Frame);
  111. Assert.Equal (new Rect (0, 0, 15, 1), pv.Child.Frame);
  112. pv.Child.Border.Effect3D = true;
  113. Assert.True (pv.Child.Border.Effect3D);
  114. Assert.Equal (new Rect (0, 0, 25, 15), pv.Frame);
  115. Assert.Equal (new Rect (0, 0, 15, 1), pv.Child.Frame);
  116. pv.Child.Border.Effect3DOffset = new Point (-1, -1);
  117. Assert.Equal (new Point (-1, -1), pv.Child.Border.Effect3DOffset);
  118. Assert.Equal (new Rect (0, 0, 25, 15), pv.Frame);
  119. Assert.Equal (new Rect (0, 0, 15, 1), pv.Child.Frame);
  120. }
  121. [Fact]
  122. [AutoInitShutdown]
  123. public void AdjustContainer_With_Border_Computed_Values ()
  124. {
  125. var top = Application.Top;
  126. var win = new Window ();
  127. var pv = new PanelView (new TextView () {
  128. Width = Dim.Fill (),
  129. Height = Dim.Fill (),
  130. Border = new Border () {
  131. BorderStyle = BorderStyle.Double,
  132. BorderThickness = new Thickness (1, 2, 3, 4),
  133. Padding = new Thickness (1, 2, 3, 4)
  134. }
  135. });
  136. var pv1 = new PanelView (new TextView () {
  137. Width = Dim.Fill (1),
  138. Height = Dim.Fill (1),
  139. Border = new Border () {
  140. BorderStyle = BorderStyle.Double,
  141. BorderThickness = new Thickness (1, 2, 3, 4),
  142. Padding = new Thickness (1, 2, 3, 4)
  143. }
  144. });
  145. var pv2 = new PanelView (new TextView () {
  146. Width = Dim.Fill (2),
  147. Height = Dim.Fill (2),
  148. Border = new Border () {
  149. BorderStyle = BorderStyle.Double,
  150. BorderThickness = new Thickness (1, 2, 3, 4),
  151. Padding = new Thickness (1, 2, 3, 4)
  152. }
  153. });
  154. win.Add (pv, pv1, pv2);
  155. top.Add (win);
  156. Application.Begin (top);
  157. Assert.Equal (new Rect (0, 0, 78, 23), pv.Frame);
  158. Assert.Equal (new Rect (0, 0, 68, 9), pv.Child.Frame);
  159. Assert.Equal (new Rect (0, 0, 77, 22), pv1.Frame);
  160. Assert.Equal (new Rect (0, 0, 65, 6), pv1.Child.Frame);
  161. Assert.Equal (new Rect (0, 0, 76, 21), pv2.Frame);
  162. Assert.Equal (new Rect (0, 0, 62, 3), pv2.Child.Frame);
  163. pv.Child.Border.Effect3D = pv1.Child.Border.Effect3D = pv2.Child.Border.Effect3D = true;
  164. Assert.True (pv.Child.Border.Effect3D);
  165. Assert.Equal (new Rect (0, 0, 78, 23), pv.Frame);
  166. Assert.Equal (new Rect (0, 0, 68, 9), pv.Child.Frame);
  167. Assert.Equal (new Rect (0, 0, 77, 22), pv1.Frame);
  168. Assert.Equal (new Rect (0, 0, 65, 6), pv1.Child.Frame);
  169. Assert.Equal (new Rect (0, 0, 76, 21), pv2.Frame);
  170. Assert.Equal (new Rect (0, 0, 62, 3), pv2.Child.Frame);
  171. pv.Child.Border.Effect3DOffset = pv1.Child.Border.Effect3DOffset = pv2.Child.Border.Effect3DOffset = new Point (-1, -1);
  172. Assert.Equal (new Point (-1, -1), pv.Child.Border.Effect3DOffset);
  173. Assert.Equal (new Rect (0, 0, 78, 23), pv.Frame);
  174. Assert.Equal (new Rect (0, 0, 68, 9), pv.Child.Frame);
  175. Assert.Equal (new Rect (0, 0, 77, 22), pv1.Frame);
  176. Assert.Equal (new Rect (0, 0, 65, 6), pv1.Child.Frame);
  177. Assert.Equal (new Rect (0, 0, 76, 21), pv2.Frame);
  178. Assert.Equal (new Rect (0, 0, 62, 3), pv2.Child.Frame);
  179. }
  180. [Fact]
  181. [AutoInitShutdown]
  182. public void UsePanelFrame_False_PanelView_Always_Respect_The_PanelView_Upper_Left_Corner_Position_And_The_Child_Size ()
  183. {
  184. var top = Application.Top;
  185. var win = new Window ();
  186. var pv = new PanelView (new Label ("This is a test.")) {
  187. X = 2,
  188. Y = 4,
  189. Width = 20,
  190. Height = 10
  191. };
  192. var pv1 = new PanelView (new TextField (3, 4, 15, "This is a test.")) {
  193. X = 2,
  194. Y = 4,
  195. Width = 20,
  196. Height = 10
  197. };
  198. var pv2 = new PanelView (new TextView () {
  199. X = 5,
  200. Y = 6,
  201. Width = Dim.Fill (),
  202. Height = Dim.Fill ()
  203. }) {
  204. X = 2,
  205. Y = 4,
  206. Width = 20,
  207. Height = 10
  208. };
  209. win.Add (pv, pv1, pv2);
  210. top.Add (win);
  211. Application.Begin (top);
  212. Assert.False (pv.UsePanelFrame);
  213. Assert.False (pv.Border.Effect3D);
  214. Assert.Equal (pv.Child.Border, pv.Border);
  215. Assert.False (pv1.UsePanelFrame);
  216. Assert.False (pv1.Border.Effect3D);
  217. Assert.Equal (pv1.Child.Border, pv1.Border);
  218. Assert.False (pv2.UsePanelFrame);
  219. Assert.False (pv2.Border.Effect3D);
  220. Assert.Equal (pv2.Child.Border, pv2.Border);
  221. Assert.Equal (new Rect (2, 4, 15, 1), pv.Frame);
  222. Assert.Equal (new Rect (0, 0, 15, 1), pv.Child.Frame);
  223. Assert.Equal (new Rect (2, 4, 18, 5), pv1.Frame);
  224. Assert.Equal (new Rect (3, 4, 15, 1), pv1.Child.Frame);
  225. Assert.Equal (new Rect (2, 4, 76, 19), pv2.Frame);
  226. Assert.Equal (new Rect (5, 6, 71, 13), pv2.Child.Frame);
  227. pv.Border.Effect3D = pv1.Border.Effect3D = pv2.Border.Effect3D = true;
  228. Assert.Equal (new Rect (2, 4, 15, 1), pv.Frame);
  229. Assert.Equal (new Rect (0, 0, 15, 1), pv.Child.Frame);
  230. Assert.Equal (new Rect (2, 4, 18, 5), pv1.Frame);
  231. Assert.Equal (new Rect (3, 4, 15, 1), pv1.Child.Frame);
  232. Assert.Equal (new Rect (2, 4, 76, 19), pv2.Frame);
  233. Assert.Equal (new Rect (5, 6, 71, 13), pv2.Child.Frame);
  234. pv.Border.Effect3DOffset = pv1.Border.Effect3DOffset = pv2.Border.Effect3DOffset = new Point (-1, -1);
  235. Assert.Equal (new Rect (2, 4, 15, 1), pv.Frame);
  236. Assert.Equal (new Rect (0, 0, 15, 1), pv.Child.Frame);
  237. Assert.Equal (new Rect (2, 4, 18, 5), pv1.Frame);
  238. Assert.Equal (new Rect (3, 4, 15, 1), pv1.Child.Frame);
  239. Assert.Equal (new Rect (2, 4, 76, 19), pv2.Frame);
  240. Assert.Equal (new Rect (5, 6, 71, 13), pv2.Child.Frame);
  241. }
  242. [Fact]
  243. [AutoInitShutdown]
  244. public void UsePanelFrame_True_PanelView_Position_And_Size_Are_Used_Depending_On_Effect3DOffset ()
  245. {
  246. var top = Application.Top;
  247. var win = new Window ();
  248. var pv = new PanelView (new TextView () {
  249. X = 2,
  250. Y = 4,
  251. Width = 20,
  252. Height = 10
  253. }) {
  254. X = 5,
  255. Y = 6,
  256. Width = Dim.Fill (),
  257. Height = Dim.Fill (),
  258. UsePanelFrame = true
  259. };
  260. var pv1 = new PanelView (new TextView () {
  261. X = 5,
  262. Y = 6,
  263. Width = Dim.Fill (),
  264. Height = Dim.Fill ()
  265. }) {
  266. X = 2,
  267. Y = 4,
  268. Width = 20,
  269. Height = 10,
  270. UsePanelFrame = true
  271. };
  272. win.Add (pv, pv1);
  273. top.Add (win);
  274. Application.Begin (top);
  275. Assert.Equal (new Rect (5, 6, 73, 17), pv.Frame);
  276. Assert.Equal (new Rect (2, 4, 20, 10), pv.Child.Frame);
  277. Assert.Equal (new Rect (2, 4, 20, 10), pv1.Frame);
  278. Assert.Equal (new Rect (5, 6, 15, 4), pv1.Child.Frame);
  279. pv.Border.Effect3D = pv1.Border.Effect3D = true;
  280. Assert.Equal (new Rect (5, 6, 73, 17), pv.Frame);
  281. Assert.Equal (new Rect (2, 4, 20, 10), pv.Child.Frame);
  282. Assert.Equal (new Rect (2, 4, 20, 10), pv1.Frame);
  283. Assert.Equal (new Rect (5, 6, 15, 4), pv1.Child.Frame);
  284. pv.Border.Effect3DOffset = pv1.Border.Effect3DOffset = new Point (-1, -1);
  285. Assert.Equal (new Rect (6, 7, 73, 17), pv.Frame);
  286. Assert.Equal (new Rect (2, 4, 20, 10), pv.Child.Frame);
  287. Assert.Equal (new Rect (3, 5, 20, 10), pv1.Frame);
  288. Assert.Equal (new Rect (5, 6, 15, 4), pv1.Child.Frame);
  289. }
  290. [Fact, AutoInitShutdown]
  291. public void Setting_Child_Size_Disable_AutoSize ()
  292. {
  293. var top = Application.Top;
  294. var win = new Window ();
  295. var label = new Label () {
  296. ColorScheme = Colors.TopLevel,
  297. Text = "This is a test\nwith a \nPanelView",
  298. TextAlignment = TextAlignment.Centered,
  299. Width = 24,
  300. Height = 13,
  301. AutoSize = false
  302. };
  303. var pv = new PanelView (label) {
  304. Width = 24,
  305. Height = 13,
  306. Border = new Border () {
  307. BorderStyle = BorderStyle.Single,
  308. DrawMarginFrame = true,
  309. BorderThickness = new Thickness (2),
  310. BorderBrush = Color.Red,
  311. Padding = new Thickness (2),
  312. Background = Color.BrightGreen,
  313. Effect3D = true
  314. },
  315. };
  316. win.Add (pv);
  317. top.Add (win);
  318. Application.Begin (top);
  319. Assert.False (label.AutoSize);
  320. Assert.Equal (new Rect (0, 0, 24, 13), label.Frame);
  321. Assert.Equal (new Rect (0, 0, 34, 23), pv.Frame);
  322. Assert.Equal (new Rect (0, 0, 80, 25), win.Frame);
  323. Assert.Equal (new Rect (0, 0, 80, 25), Application.Top.Frame);
  324. var expected = @"
  325. ┌──────────────────────────────────────────────────────────────────────────────┐
  326. │ │
  327. │ │
  328. │ │
  329. │ │
  330. │ ┌────────────────────────┐ │
  331. │ │ This is a test │ │
  332. │ │ with a │ │
  333. │ │ PanelView │ │
  334. │ │ │ │
  335. │ │ │ │
  336. │ │ │ │
  337. │ │ │ │
  338. │ │ │ │
  339. │ │ │ │
  340. │ │ │ │
  341. │ │ │ │
  342. │ │ │ │
  343. │ │ │ │
  344. │ └────────────────────────┘ │
  345. │ │
  346. │ │
  347. │ │
  348. │ │
  349. └──────────────────────────────────────────────────────────────────────────────┘
  350. ";
  351. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  352. Assert.Equal (new Rect (0, 0, 80, 25), pos);
  353. }
  354. [Fact, AutoInitShutdown]
  355. public void Not_Setting_Child_Size_Default_AutoSize_True ()
  356. {
  357. var top = Application.Top;
  358. var win = new Window ();
  359. var label = new Label () {
  360. ColorScheme = Colors.TopLevel,
  361. Text = "This is a test\nwith a \nPanelView",
  362. TextAlignment = TextAlignment.Centered
  363. };
  364. var pv = new PanelView (label) {
  365. Width = 24,
  366. Height = 13,
  367. Border = new Border () {
  368. BorderStyle = BorderStyle.Single,
  369. DrawMarginFrame = true,
  370. BorderThickness = new Thickness (2),
  371. BorderBrush = Color.Red,
  372. Padding = new Thickness (2),
  373. Background = Color.BrightGreen,
  374. Effect3D = true
  375. },
  376. };
  377. win.Add (pv);
  378. top.Add (win);
  379. Application.Begin (top);
  380. Assert.True (label.AutoSize);
  381. Assert.False (pv.UsePanelFrame);
  382. Assert.Equal (new Rect (0, 0, 14, 3), label.Frame);
  383. Assert.Equal (new Rect (0, 0, 24, 13), pv.Frame);
  384. Assert.Equal (new Rect (0, 0, 80, 25), win.Frame);
  385. Assert.Equal (new Rect (0, 0, 80, 25), Application.Top.Frame);
  386. var expected = @"
  387. ┌──────────────────────────────────────────────────────────────────────────────┐
  388. │ │
  389. │ │
  390. │ │
  391. │ │
  392. │ ┌──────────────┐ │
  393. │ │This is a test│ │
  394. │ │ with a │ │
  395. │ │ PanelView │ │
  396. │ └──────────────┘ │
  397. │ │
  398. │ │
  399. │ │
  400. │ │
  401. │ │
  402. │ │
  403. │ │
  404. │ │
  405. │ │
  406. │ │
  407. │ │
  408. │ │
  409. │ │
  410. │ │
  411. └──────────────────────────────────────────────────────────────────────────────┘
  412. ";
  413. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  414. Assert.Equal (new Rect (0, 0, 80, 25), pos);
  415. }
  416. }
  417. }