GraphViewTests.cs 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  1. using System.Text;
  2. using Terminal.Gui.ViewMouseTests;
  3. using Xunit.Abstractions;
  4. namespace Terminal.Gui.ViewsTests;
  5. #region Helper Classes
  6. internal class FakeHAxis : HorizontalAxis
  7. {
  8. public List<Point> DrawAxisLinePoints = new ();
  9. public List<int> LabelPoints = new ();
  10. public override void DrawAxisLabel (GraphView graph, int screenPosition, string text)
  11. {
  12. base.DrawAxisLabel (graph, screenPosition, text);
  13. LabelPoints.Add (screenPosition);
  14. }
  15. protected override void DrawAxisLine (GraphView graph, int x, int y)
  16. {
  17. base.DrawAxisLine (graph, x, y);
  18. DrawAxisLinePoints.Add (new Point (x, y));
  19. }
  20. }
  21. internal class FakeVAxis : VerticalAxis
  22. {
  23. public List<Point> DrawAxisLinePoints = new ();
  24. public List<int> LabelPoints = new ();
  25. public override void DrawAxisLabel (GraphView graph, int screenPosition, string text)
  26. {
  27. base.DrawAxisLabel (graph, screenPosition, text);
  28. LabelPoints.Add (screenPosition);
  29. }
  30. protected override void DrawAxisLine (GraphView graph, int x, int y)
  31. {
  32. base.DrawAxisLine (graph, x, y);
  33. DrawAxisLinePoints.Add (new Point (x, y));
  34. }
  35. }
  36. #endregion
  37. public class GraphViewTests
  38. {
  39. private static string LastInitFakeDriver;
  40. /// <summary>
  41. /// A cell size of 0 would result in mapping all graph space into the same cell of the console. Since
  42. /// <see cref="GraphView.CellSize"/> is mutable a sensible place to check this is in redraw.
  43. /// </summary>
  44. [Fact]
  45. public void CellSizeZero ()
  46. {
  47. InitFakeDriver ();
  48. var gv = new GraphView ();
  49. gv.BeginInit ();
  50. gv.EndInit ();
  51. gv.ColorScheme = new ColorScheme ();
  52. gv.Viewport = new Rectangle (0, 0, 50, 30);
  53. gv.Series.Add (new ScatterSeries { Points = new List<PointF> { new (1, 1) } });
  54. gv.CellSize = new PointF (0, 5);
  55. var ex = Assert.Throws<Exception> (() => gv.Draw ());
  56. Assert.Equal ("CellSize cannot be 0", ex.Message);
  57. // Shutdown must be called to safely clean up Application if Init has been called
  58. Application.Shutdown ();
  59. }
  60. /// <summary>Returns a basic very small graph (10 x 5)</summary>
  61. /// <returns></returns>
  62. public static GraphView GetGraph ()
  63. {
  64. InitFakeDriver ();
  65. var gv = new GraphView ();
  66. gv.BeginInit ();
  67. gv.EndInit ();
  68. gv.ColorScheme = new ColorScheme ();
  69. gv.MarginBottom = 1;
  70. gv.MarginLeft = 1;
  71. gv.Viewport = new Rectangle (0, 0, 10, 5);
  72. return gv;
  73. }
  74. public static FakeDriver InitFakeDriver ()
  75. {
  76. var driver = new FakeDriver ();
  77. try
  78. {
  79. Application.Init (driver);
  80. }
  81. catch (InvalidOperationException)
  82. {
  83. // close it so that we don't get a thousand of these errors in a row
  84. Application.Shutdown ();
  85. // but still report a failure and name the test that didn't shut down. Note
  86. // that the test that didn't shutdown won't be the one currently running it will
  87. // be the last one
  88. throw new Exception (
  89. "A test did not call shutdown correctly. Test stack trace was:" + LastInitFakeDriver
  90. );
  91. }
  92. driver.Init ();
  93. LastInitFakeDriver = Environment.StackTrace;
  94. return driver;
  95. }
  96. /// <summary>
  97. /// Tests that each point in the screen space maps to a rectangle of (float) graph space and that each corner of
  98. /// that rectangle of graph space maps back to the same row/col of the graph that was fed in
  99. /// </summary>
  100. [Fact]
  101. public void TestReversing_ScreenToGraphSpace ()
  102. {
  103. var gv = new GraphView ();
  104. gv.BeginInit ();
  105. gv.EndInit ();
  106. gv.Viewport = new Rectangle (0, 0, 50, 30);
  107. // How much graph space each cell of the console depicts
  108. gv.CellSize = new PointF (0.1f, 0.25f);
  109. gv.AxisX.Increment = 1;
  110. gv.AxisX.ShowLabelsEvery = 1;
  111. gv.AxisY.Increment = 1;
  112. gv.AxisY.ShowLabelsEvery = 1;
  113. // Start the graph at 80
  114. gv.ScrollOffset = new PointF (0, 80);
  115. for (var x = 0; x < gv.Viewport.Width; x++)
  116. {
  117. for (var y = 0; y < gv.Viewport.Height; y++)
  118. {
  119. RectangleF graphSpace = gv.ScreenToGraphSpace (x, y);
  120. // See
  121. // https://en.wikipedia.org/wiki/Machine_epsilon
  122. var epsilon = 0.0001f;
  123. Point p = gv.GraphSpaceToScreen (
  124. new PointF (
  125. graphSpace.Left + epsilon,
  126. graphSpace.Top + epsilon
  127. )
  128. );
  129. Assert.Equal (x, p.X);
  130. Assert.Equal (y, p.Y);
  131. p = gv.GraphSpaceToScreen (
  132. new PointF (
  133. graphSpace.Right - epsilon,
  134. graphSpace.Top + epsilon
  135. )
  136. );
  137. Assert.Equal (x, p.X);
  138. Assert.Equal (y, p.Y);
  139. p = gv.GraphSpaceToScreen (
  140. new PointF (
  141. graphSpace.Left + epsilon,
  142. graphSpace.Bottom - epsilon
  143. )
  144. );
  145. Assert.Equal (x, p.X);
  146. Assert.Equal (y, p.Y);
  147. p = gv.GraphSpaceToScreen (
  148. new PointF (
  149. graphSpace.Right - epsilon,
  150. graphSpace.Bottom - epsilon
  151. )
  152. );
  153. Assert.Equal (x, p.X);
  154. Assert.Equal (y, p.Y);
  155. }
  156. }
  157. }
  158. #region Screen to Graph Tests
  159. [Fact]
  160. public void ScreenToGraphSpace_DefaultCellSize ()
  161. {
  162. var gv = new GraphView ();
  163. gv.BeginInit ();
  164. gv.EndInit ();
  165. gv.Viewport = new Rectangle (0, 0, 20, 10);
  166. // origin should be bottom left
  167. RectangleF botLeft = gv.ScreenToGraphSpace (0, 9);
  168. Assert.Equal (0, botLeft.X);
  169. Assert.Equal (0, botLeft.Y);
  170. Assert.Equal (1, botLeft.Width);
  171. Assert.Equal (1, botLeft.Height);
  172. // up 2 rows of the console and along 1 col
  173. RectangleF up2along1 = gv.ScreenToGraphSpace (1, 7);
  174. Assert.Equal (1, up2along1.X);
  175. Assert.Equal (2, up2along1.Y);
  176. }
  177. [Fact]
  178. public void ScreenToGraphSpace_DefaultCellSize_WithMargin ()
  179. {
  180. var gv = new GraphView ();
  181. gv.BeginInit ();
  182. gv.EndInit ();
  183. gv.Viewport = new Rectangle (0, 0, 20, 10);
  184. // origin should be bottom left
  185. RectangleF botLeft = gv.ScreenToGraphSpace (0, 9);
  186. Assert.Equal (0, botLeft.X);
  187. Assert.Equal (0, botLeft.Y);
  188. Assert.Equal (1, botLeft.Width);
  189. Assert.Equal (1, botLeft.Height);
  190. gv.MarginLeft = 1;
  191. botLeft = gv.ScreenToGraphSpace (0, 9);
  192. // Origin should be at 1,9 now to leave a margin of 1
  193. // so screen position 0,9 would be data space -1,0
  194. Assert.Equal (-1, botLeft.X);
  195. Assert.Equal (0, botLeft.Y);
  196. Assert.Equal (1, botLeft.Width);
  197. Assert.Equal (1, botLeft.Height);
  198. gv.MarginLeft = 1;
  199. gv.MarginBottom = 1;
  200. botLeft = gv.ScreenToGraphSpace (0, 9);
  201. // Origin should be at 1,0 (to leave a margin of 1 in both sides)
  202. // so screen position 0,9 would be data space -1,-1
  203. Assert.Equal (-1, botLeft.X);
  204. Assert.Equal (-1, botLeft.Y);
  205. Assert.Equal (1, botLeft.Width);
  206. Assert.Equal (1, botLeft.Height);
  207. }
  208. [Fact]
  209. public void ScreenToGraphSpace_CustomCellSize ()
  210. {
  211. var gv = new GraphView ();
  212. gv.BeginInit ();
  213. gv.EndInit ();
  214. gv.Viewport = new Rectangle (0, 0, 20, 10);
  215. // Each cell of screen measures 5 units in graph data model vertically and 1/4 horizontally
  216. gv.CellSize = new PointF (0.25f, 5);
  217. // origin should be bottom left
  218. // (note that y=10 is actually overspilling the control, the last row is 9)
  219. RectangleF botLeft = gv.ScreenToGraphSpace (0, 9);
  220. Assert.Equal (0, botLeft.X);
  221. Assert.Equal (0, botLeft.Y);
  222. Assert.Equal (0.25f, botLeft.Width);
  223. Assert.Equal (5, botLeft.Height);
  224. // up 2 rows of the console and along 1 col
  225. RectangleF up2along1 = gv.ScreenToGraphSpace (1, 7);
  226. Assert.Equal (0.25f, up2along1.X);
  227. Assert.Equal (10, up2along1.Y);
  228. Assert.Equal (0.25f, botLeft.Width);
  229. Assert.Equal (5, botLeft.Height);
  230. }
  231. #endregion
  232. #region Graph to Screen Tests
  233. [Fact]
  234. public void GraphSpaceToScreen_DefaultCellSize ()
  235. {
  236. var gv = new GraphView ();
  237. gv.BeginInit ();
  238. gv.EndInit ();
  239. gv.Viewport = new Rectangle (0, 0, 20, 10);
  240. // origin should be bottom left
  241. Point botLeft = gv.GraphSpaceToScreen (new PointF (0, 0));
  242. Assert.Equal (0, botLeft.X);
  243. Assert.Equal (9, botLeft.Y); // row 9 of the view is the bottom left
  244. // along 2 and up 1 in graph space
  245. Point along2up1 = gv.GraphSpaceToScreen (new PointF (2, 1));
  246. Assert.Equal (2, along2up1.X);
  247. Assert.Equal (8, along2up1.Y);
  248. }
  249. [Fact]
  250. public void GraphSpaceToScreen_DefaultCellSize_WithMargin ()
  251. {
  252. var gv = new GraphView ();
  253. gv.BeginInit ();
  254. gv.EndInit ();
  255. gv.Viewport = new Rectangle (0, 0, 20, 10);
  256. // origin should be bottom left
  257. Point botLeft = gv.GraphSpaceToScreen (new PointF (0, 0));
  258. Assert.Equal (0, botLeft.X);
  259. Assert.Equal (9, botLeft.Y); // row 9 of the view is the bottom left
  260. gv.MarginLeft = 1;
  261. // With a margin of 1 the origin should be at x=1 y= 9
  262. botLeft = gv.GraphSpaceToScreen (new PointF (0, 0));
  263. Assert.Equal (1, botLeft.X);
  264. Assert.Equal (9, botLeft.Y); // row 9 of the view is the bottom left
  265. gv.MarginLeft = 1;
  266. gv.MarginBottom = 1;
  267. // With a margin of 1 in both directions the origin should be at x=1 y= 9
  268. botLeft = gv.GraphSpaceToScreen (new PointF (0, 0));
  269. Assert.Equal (1, botLeft.X);
  270. Assert.Equal (8, botLeft.Y); // row 8 of the view is the bottom left up 1 cell
  271. }
  272. [Fact]
  273. public void GraphSpaceToScreen_ScrollOffset ()
  274. {
  275. var gv = new GraphView ();
  276. gv.BeginInit ();
  277. gv.EndInit ();
  278. gv.Viewport = new Rectangle (0, 0, 20, 10);
  279. //graph is scrolled to present chart space -5 to 5 in both axes
  280. gv.ScrollOffset = new PointF (-5, -5);
  281. // origin should be right in the middle of the control
  282. Point botLeft = gv.GraphSpaceToScreen (new PointF (0, 0));
  283. Assert.Equal (5, botLeft.X);
  284. Assert.Equal (4, botLeft.Y);
  285. // along 2 and up 1 in graph space
  286. Point along2up1 = gv.GraphSpaceToScreen (new PointF (2, 1));
  287. Assert.Equal (7, along2up1.X);
  288. Assert.Equal (3, along2up1.Y);
  289. }
  290. [Fact]
  291. public void GraphSpaceToScreen_CustomCellSize ()
  292. {
  293. var gv = new GraphView ();
  294. gv.BeginInit ();
  295. gv.EndInit ();
  296. gv.Viewport = new Rectangle (0, 0, 20, 10);
  297. // Each cell of screen is responsible for rendering 5 units in graph data model
  298. // vertically and 1/4 horizontally
  299. gv.CellSize = new PointF (0.25f, 5);
  300. // origin should be bottom left
  301. Point botLeft = gv.GraphSpaceToScreen (new PointF (0, 0));
  302. Assert.Equal (0, botLeft.X);
  303. // row 9 of the view is the bottom left (height is 10 so 0,1,2,3..9)
  304. Assert.Equal (9, botLeft.Y);
  305. // along 2 and up 1 in graph space
  306. Point along2up1 = gv.GraphSpaceToScreen (new PointF (2, 1));
  307. Assert.Equal (8, along2up1.X);
  308. Assert.Equal (9, along2up1.Y);
  309. // Y value 4 should be rendered in bottom most row
  310. Assert.Equal (9, gv.GraphSpaceToScreen (new PointF (2, 4)).Y);
  311. // Cell height is 5 so this is the first point of graph space that should
  312. // be rendered in the graph in next row up (row 9)
  313. Assert.Equal (8, gv.GraphSpaceToScreen (new PointF (2, 5)).Y);
  314. // More boundary testing for this cell size
  315. Assert.Equal (8, gv.GraphSpaceToScreen (new PointF (2, 6)).Y);
  316. Assert.Equal (8, gv.GraphSpaceToScreen (new PointF (2, 7)).Y);
  317. Assert.Equal (8, gv.GraphSpaceToScreen (new PointF (2, 8)).Y);
  318. Assert.Equal (8, gv.GraphSpaceToScreen (new PointF (2, 9)).Y);
  319. Assert.Equal (7, gv.GraphSpaceToScreen (new PointF (2, 10)).Y);
  320. Assert.Equal (7, gv.GraphSpaceToScreen (new PointF (2, 11)).Y);
  321. }
  322. [Fact]
  323. public void GraphSpaceToScreen_CustomCellSize_WithScrollOffset ()
  324. {
  325. var gv = new GraphView ();
  326. gv.BeginInit ();
  327. gv.EndInit ();
  328. gv.Viewport = new Rectangle (0, 0, 20, 10);
  329. // Each cell of screen is responsible for rendering 5 units in graph data model
  330. // vertically and 1/4 horizontally
  331. gv.CellSize = new PointF (0.25f, 5);
  332. //graph is scrolled to present some negative chart (4 negative cols and 2 negative rows)
  333. gv.ScrollOffset = new PointF (-1, -10);
  334. // origin should be in the lower left (but not right at the bottom)
  335. Point botLeft = gv.GraphSpaceToScreen (new PointF (0, 0));
  336. Assert.Equal (4, botLeft.X);
  337. Assert.Equal (7, botLeft.Y);
  338. // along 2 and up 1 in graph space
  339. Point along2up1 = gv.GraphSpaceToScreen (new PointF (2, 1));
  340. Assert.Equal (12, along2up1.X);
  341. Assert.Equal (7, along2up1.Y);
  342. // More boundary testing for this cell size/offset
  343. Assert.Equal (6, gv.GraphSpaceToScreen (new PointF (2, 6)).Y);
  344. Assert.Equal (6, gv.GraphSpaceToScreen (new PointF (2, 7)).Y);
  345. Assert.Equal (6, gv.GraphSpaceToScreen (new PointF (2, 8)).Y);
  346. Assert.Equal (6, gv.GraphSpaceToScreen (new PointF (2, 9)).Y);
  347. Assert.Equal (5, gv.GraphSpaceToScreen (new PointF (2, 10)).Y);
  348. Assert.Equal (5, gv.GraphSpaceToScreen (new PointF (2, 11)).Y);
  349. }
  350. #endregion
  351. }
  352. public class SeriesTests
  353. {
  354. [Fact]
  355. public void Series_GetsPassedCorrectViewport_AllAtOnce ()
  356. {
  357. GraphViewTests.InitFakeDriver ();
  358. var gv = new GraphView ();
  359. gv.BeginInit ();
  360. gv.EndInit ();
  361. gv.Viewport = new Rectangle (0, 0, 50, 30);
  362. gv.ColorScheme = new ColorScheme ();
  363. var fullGraphBounds = RectangleF.Empty;
  364. var graphScreenBounds = Rectangle.Empty;
  365. var series = new FakeSeries (
  366. (v, s, g) =>
  367. {
  368. graphScreenBounds = s;
  369. fullGraphBounds = g;
  370. }
  371. );
  372. gv.Series.Add (series);
  373. gv.LayoutSubviews ();
  374. gv.Draw ();
  375. Assert.Equal (new RectangleF (0, 0, 50, 30), fullGraphBounds);
  376. Assert.Equal (new Rectangle (0, 0, 50, 30), graphScreenBounds);
  377. // Now we put a margin in
  378. // Graph should not spill into the margins
  379. gv.MarginBottom = 2;
  380. gv.MarginLeft = 5;
  381. // Even with a margin the graph should be drawn from
  382. // the origin, we just get less visible width/height
  383. gv.LayoutSubviews ();
  384. gv.SetNeedsDraw ();
  385. gv.Draw ();
  386. Assert.Equal (new RectangleF (0, 0, 45, 28), fullGraphBounds);
  387. // The screen space the graph will be rendered into should
  388. // not overspill the margins
  389. Assert.Equal (new Rectangle (5, 0, 45, 28), graphScreenBounds);
  390. // Shutdown must be called to safely clean up Application if Init has been called
  391. Application.Shutdown ();
  392. }
  393. /// <summary>
  394. /// Tests that the bounds passed to the ISeries for drawing into are correct even when the
  395. /// <see cref="GraphView.CellSize"/> results in multiple units of graph space being condensed into each cell of console
  396. /// </summary>
  397. [Fact]
  398. public void Series_GetsPassedCorrectViewport_AllAtOnce_LargeCellSize ()
  399. {
  400. GraphViewTests.InitFakeDriver ();
  401. var gv = new GraphView ();
  402. gv.BeginInit ();
  403. gv.EndInit ();
  404. gv.ColorScheme = new ColorScheme ();
  405. gv.Viewport = new Rectangle (0, 0, 50, 30);
  406. // the larger the cell size the more condensed (smaller) the graph space is
  407. gv.CellSize = new PointF (2, 5);
  408. var fullGraphBounds = RectangleF.Empty;
  409. var graphScreenBounds = Rectangle.Empty;
  410. var series = new FakeSeries (
  411. (v, s, g) =>
  412. {
  413. graphScreenBounds = s;
  414. fullGraphBounds = g;
  415. }
  416. );
  417. gv.Series.Add (series);
  418. gv.LayoutSubviews ();
  419. gv.Draw ();
  420. // Since each cell of the console is 2x5 of graph space the graph
  421. // bounds to be rendered are larger
  422. Assert.Equal (new RectangleF (0, 0, 100, 150), fullGraphBounds);
  423. Assert.Equal (new Rectangle (0, 0, 50, 30), graphScreenBounds);
  424. // Graph should not spill into the margins
  425. gv.MarginBottom = 2;
  426. gv.MarginLeft = 5;
  427. // Even with a margin the graph should be drawn from
  428. // the origin, we just get less visible width/height
  429. gv.LayoutSubviews ();
  430. gv.SetNeedsDraw ();
  431. gv.Draw ();
  432. Assert.Equal (new RectangleF (0, 0, 90, 140), fullGraphBounds);
  433. // The screen space the graph will be rendered into should
  434. // not overspill the margins
  435. Assert.Equal (new Rectangle (5, 0, 45, 28), graphScreenBounds);
  436. // Shutdown must be called to safely clean up Application if Init has been called
  437. Application.Shutdown ();
  438. }
  439. private class FakeSeries : ISeries
  440. {
  441. private readonly Action<GraphView, Rectangle, RectangleF> _drawSeries;
  442. public FakeSeries (
  443. Action<GraphView, Rectangle, RectangleF> drawSeries
  444. )
  445. {
  446. _drawSeries = drawSeries;
  447. }
  448. public void DrawSeries (GraphView graph, Rectangle bounds, RectangleF graphBounds) { _drawSeries (graph, bounds, graphBounds); }
  449. }
  450. }
  451. public class MultiBarSeriesTests
  452. {
  453. private readonly ITestOutputHelper _output;
  454. public MultiBarSeriesTests (ITestOutputHelper output) { _output = output; }
  455. [Fact]
  456. public void MultiBarSeries_BarSpacing ()
  457. {
  458. // Creates clusters of 5 adjacent bars with 2 spaces between clusters
  459. var series = new MultiBarSeries (5, 7, 1);
  460. Assert.Equal (5, series.SubSeries.Count);
  461. Assert.Equal (0, series.SubSeries.ElementAt (0).Offset);
  462. Assert.Equal (1, series.SubSeries.ElementAt (1).Offset);
  463. Assert.Equal (2, series.SubSeries.ElementAt (2).Offset);
  464. Assert.Equal (3, series.SubSeries.ElementAt (3).Offset);
  465. Assert.Equal (4, series.SubSeries.ElementAt (4).Offset);
  466. }
  467. [Fact]
  468. public void MultiBarSeriesAddValues_WrongNumber ()
  469. {
  470. // user asks for 3 bars per category
  471. var series = new MultiBarSeries (3, 7, 1);
  472. var ex = Assert.Throws<ArgumentException> (() => series.AddBars ("Cars", (Rune)'#', 1));
  473. Assert.Equal (
  474. "Number of values must match the number of bars per category (Parameter 'values')",
  475. ex.Message
  476. );
  477. }
  478. [Fact]
  479. public void MultiBarSeriesColors_RightNumber ()
  480. {
  481. Attribute [] colors =
  482. {
  483. new (Color.Green, Color.Black), new (Color.Green, Color.White), new (Color.BrightYellow, Color.White)
  484. };
  485. // user passes 3 colors and asks for 3 bars
  486. var series = new MultiBarSeries (3, 7, 1, colors);
  487. Assert.Equal (series.SubSeries.ElementAt (0).OverrideBarColor, colors [0]);
  488. Assert.Equal (series.SubSeries.ElementAt (1).OverrideBarColor, colors [1]);
  489. Assert.Equal (series.SubSeries.ElementAt (2).OverrideBarColor, colors [2]);
  490. // Shutdown must be called to safely clean up Application if Init has been called
  491. Application.Shutdown ();
  492. }
  493. [Fact]
  494. public void MultiBarSeriesColors_WrongNumber ()
  495. {
  496. Attribute [] colors = { new (Color.Green, Color.Black) };
  497. // user passes 1 color only but asks for 5 bars
  498. var ex = Assert.Throws<ArgumentException> (() => new MultiBarSeries (5, 7, 1, colors));
  499. Assert.Equal (
  500. "Number of colors must match the number of bars (Parameter 'numberOfBarsPerCategory')",
  501. ex.Message
  502. );
  503. // Shutdown must be called to safely clean up Application if Init has been called
  504. Application.Shutdown ();
  505. }
  506. [Fact]
  507. public void TestRendering_MultibarSeries ()
  508. {
  509. GraphViewTests.InitFakeDriver ();
  510. var gv = new GraphView ();
  511. gv.ColorScheme = new ColorScheme ();
  512. // y axis goes from 0.1 to 1 across 10 console rows
  513. // x axis goes from 0 to 20 across 20 console columns
  514. gv.Viewport = new Rectangle (0, 0, 20, 10);
  515. gv.CellSize = new PointF (1f, 0.1f);
  516. gv.MarginBottom = 1;
  517. gv.MarginLeft = 1;
  518. var multibarSeries = new MultiBarSeries (2, 4, 1);
  519. //nudge them left to avoid float rounding errors at the boundaries of cells
  520. foreach (BarSeries sub in multibarSeries.SubSeries)
  521. {
  522. sub.Offset -= 0.001f;
  523. }
  524. gv.Series.Add (multibarSeries);
  525. FakeHAxis fakeXAxis;
  526. // don't show axis labels that means any labels
  527. // that appear are explicitly from the bars
  528. gv.AxisX = fakeXAxis = new FakeHAxis { Increment = 0 };
  529. gv.AxisY = new FakeVAxis { Increment = 0 };
  530. gv.LayoutSubviews ();
  531. gv.Draw ();
  532. // Since bar series has no bars yet no labels should be displayed
  533. Assert.Empty (fakeXAxis.LabelPoints);
  534. multibarSeries.AddBars ("hey", (Rune)'M', 0.5001f, 0.5001f);
  535. fakeXAxis.LabelPoints.Clear ();
  536. gv.LayoutSubviews ();
  537. gv.SetNeedsDraw ();
  538. gv.Draw ();
  539. Assert.Equal (4, fakeXAxis.LabelPoints.Single ());
  540. multibarSeries.AddBars ("there", (Rune)'M', 0.24999f, 0.74999f);
  541. multibarSeries.AddBars ("bob", (Rune)'M', 1, 2);
  542. fakeXAxis.LabelPoints.Clear ();
  543. gv.LayoutSubviews ();
  544. gv.SetNeedsDraw ();
  545. View.SetClipToScreen ();
  546. gv.Draw ();
  547. Assert.Equal (3, fakeXAxis.LabelPoints.Count);
  548. Assert.Equal (4, fakeXAxis.LabelPoints [0]);
  549. Assert.Equal (8, fakeXAxis.LabelPoints [1]);
  550. Assert.Equal (12, fakeXAxis.LabelPoints [2]);
  551. var looksLike =
  552. @"
  553. │ MM
  554. │ M MM
  555. │ M MM
  556. │ MM M MM
  557. │ MM M MM
  558. │ MM M MM
  559. │ MM MM MM
  560. │ MM MM MM
  561. ┼──┬M──┬M──┬M──────
  562. heytherebob ";
  563. TestHelpers.AssertDriverContentsAre (looksLike, _output);
  564. // Shutdown must be called to safely clean up Application if Init has been called
  565. Application.Shutdown ();
  566. }
  567. }
  568. public class BarSeriesTests
  569. {
  570. [Fact]
  571. public void TestOneLongOneShortHorizontalBars_WithOffset ()
  572. {
  573. GraphView graph = GetGraph (out FakeBarSeries barSeries, out FakeHAxis axisX, out FakeVAxis axisY);
  574. graph.Draw ();
  575. // no bars
  576. Assert.Empty (barSeries.BarScreenStarts);
  577. Assert.Empty (axisX.LabelPoints);
  578. Assert.Empty (axisY.LabelPoints);
  579. // 0.1 units of graph y fit every screen row
  580. // so 1 unit of graph y space is 10 screen rows
  581. graph.CellSize = new PointF (0.5f, 0.1f);
  582. // Start bar 3 screen units up (y = height-3)
  583. barSeries.Offset = 0.25f;
  584. // 1 bar every 3 rows of screen
  585. barSeries.BarEvery = 0.3f;
  586. barSeries.Orientation = Orientation.Horizontal;
  587. // 1 bar that is very wide (100 graph units horizontally = screen pos 50 but bounded by screen)
  588. barSeries.Bars.Add (
  589. new BarSeriesBar ("hi1", new GraphCellToRender ((Rune)'.'), 100)
  590. );
  591. // 1 bar that is shorter
  592. barSeries.Bars.Add (
  593. new BarSeriesBar ("hi2", new GraphCellToRender ((Rune)'.'), 5)
  594. );
  595. // redraw graph
  596. graph.SetNeedsDraw ();
  597. graph.Draw ();
  598. // since bars are horizontal all have the same X start cordinates
  599. Assert.Equal (0, barSeries.BarScreenStarts [0].X);
  600. Assert.Equal (0, barSeries.BarScreenStarts [1].X);
  601. // bar goes all the way to the end so bumps up against right screen boundary
  602. // width of graph is 20
  603. Assert.Equal (19, barSeries.BarScreenEnds [0].X);
  604. // shorter bar is 5 graph units wide which is 10 screen units
  605. Assert.Equal (10, barSeries.BarScreenEnds [1].X);
  606. // first bar should be offset 6 screen units (0.25f + 0.3f graph units)
  607. // since height of control is 10 then first bar should be at screen row 4 (10-6)
  608. Assert.Equal (4, barSeries.BarScreenStarts [0].Y);
  609. // second bar should be offset 9 screen units (0.25f + 0.6f graph units)
  610. // since height of control is 10 then second bar should be at screen row 1 (10-9)
  611. Assert.Equal (1, barSeries.BarScreenStarts [1].Y);
  612. // both bars should have labels but on the y axis
  613. Assert.Equal (2, axisY.LabelPoints.Count);
  614. Assert.Empty (axisX.LabelPoints);
  615. // labels should align with the bars (same screen y axis point)
  616. Assert.Contains (4, axisY.LabelPoints);
  617. Assert.Contains (1, axisY.LabelPoints);
  618. // Shutdown must be called to safely clean up Application if Init has been called
  619. Application.Shutdown ();
  620. }
  621. [Fact]
  622. public void TestTwoTallBars_WithOffset ()
  623. {
  624. GraphView graph = GetGraph (out FakeBarSeries barSeries, out FakeHAxis axisX, out FakeVAxis axisY);
  625. graph.Draw ();
  626. // no bars
  627. Assert.Empty (barSeries.BarScreenStarts);
  628. Assert.Empty (axisX.LabelPoints);
  629. Assert.Empty (axisY.LabelPoints);
  630. // 0.5 units of graph fit every screen cell
  631. // so 1 unit of graph space is 2 screen columns
  632. graph.CellSize = new PointF (0.5f, 0.1f);
  633. // Start bar 1 screen unit along
  634. barSeries.Offset = 0.5f;
  635. barSeries.BarEvery = 1f;
  636. barSeries.Bars.Add (
  637. new BarSeriesBar ("hi1", new GraphCellToRender ((Rune)'.'), 100)
  638. );
  639. barSeries.Bars.Add (
  640. new BarSeriesBar ("hi2", new GraphCellToRender ((Rune)'.'), 100)
  641. );
  642. barSeries.Orientation = Orientation.Vertical;
  643. // redraw graph
  644. graph.SetNeedsDraw ();
  645. graph.Draw ();
  646. // bar should be drawn at BarEvery 1f + offset 0.5f = 3 screen units
  647. Assert.Equal (3, barSeries.BarScreenStarts [0].X);
  648. Assert.Equal (3, barSeries.BarScreenEnds [0].X);
  649. // second bar should be BarEveryx2 = 2f + offset 0.5f = 5 screen units
  650. Assert.Equal (5, barSeries.BarScreenStarts [1].X);
  651. Assert.Equal (5, barSeries.BarScreenEnds [1].X);
  652. // both bars should have labels
  653. Assert.Equal (2, axisX.LabelPoints.Count);
  654. Assert.Contains (3, axisX.LabelPoints);
  655. Assert.Contains (5, axisX.LabelPoints);
  656. // bars are very tall but should not draw up off top of screen
  657. Assert.Equal (9, barSeries.BarScreenStarts [0].Y);
  658. Assert.Equal (0, barSeries.BarScreenEnds [0].Y);
  659. Assert.Equal (9, barSeries.BarScreenStarts [1].Y);
  660. Assert.Equal (0, barSeries.BarScreenEnds [1].Y);
  661. // Shutdown must be called to safely clean up Application if Init has been called
  662. Application.Shutdown ();
  663. }
  664. [Fact]
  665. public void TestZeroHeightBar_WithName ()
  666. {
  667. GraphView graph = GetGraph (out FakeBarSeries barSeries, out FakeHAxis axisX, out FakeVAxis axisY);
  668. graph.Draw ();
  669. // no bars
  670. Assert.Empty (barSeries.BarScreenStarts);
  671. Assert.Empty (axisX.LabelPoints);
  672. Assert.Empty (axisY.LabelPoints);
  673. // bar of height 0
  674. barSeries.Bars.Add (new BarSeriesBar ("hi", new GraphCellToRender ((Rune)'.'), 0));
  675. barSeries.Orientation = Orientation.Vertical;
  676. // redraw graph
  677. graph.SetNeedsDraw ();
  678. graph.Draw ();
  679. // bar should not be drawn
  680. Assert.Empty (barSeries.BarScreenStarts);
  681. Assert.NotEmpty (axisX.LabelPoints);
  682. Assert.Empty (axisY.LabelPoints);
  683. // but bar name should be
  684. // Screen position x=2 because bars are drawn every 1f of
  685. // graph space and CellSize.X is 0.5f
  686. Assert.Contains (2, axisX.LabelPoints);
  687. // Shutdown must be called to safely clean up Application if Init has been called
  688. Application.Shutdown ();
  689. }
  690. private GraphView GetGraph (out FakeBarSeries series, out FakeHAxis axisX, out FakeVAxis axisY)
  691. {
  692. GraphViewTests.InitFakeDriver ();
  693. var gv = new GraphView ();
  694. gv.BeginInit ();
  695. gv.EndInit ();
  696. // y axis goes from 0.1 to 1 across 10 console rows
  697. // x axis goes from 0 to 10 across 20 console columns
  698. gv.Viewport = new Rectangle (0, 0, 20, 10);
  699. gv.ColorScheme = new ColorScheme ();
  700. gv.CellSize = new PointF (0.5f, 0.1f);
  701. gv.Series.Add (series = new FakeBarSeries ());
  702. // don't show axis labels that means any labels
  703. // that appaer are explicitly from the bars
  704. gv.AxisX = axisX = new FakeHAxis { Increment = 0 };
  705. gv.AxisY = axisY = new FakeVAxis { Increment = 0 };
  706. return gv;
  707. }
  708. private class FakeBarSeries : BarSeries
  709. {
  710. public List<Point> BarScreenEnds { get; } = new ();
  711. public List<Point> BarScreenStarts { get; } = new ();
  712. public GraphCellToRender FinalColor { get; private set; }
  713. protected override GraphCellToRender AdjustColor (GraphCellToRender graphCellToRender) { return FinalColor = base.AdjustColor (graphCellToRender); }
  714. protected override void DrawBarLine (GraphView graph, Point start, Point end, BarSeriesBar beingDrawn)
  715. {
  716. base.DrawBarLine (graph, start, end, beingDrawn);
  717. BarScreenStarts.Add (start);
  718. BarScreenEnds.Add (end);
  719. }
  720. }
  721. }
  722. public class AxisTests
  723. {
  724. private GraphView GetGraph (out FakeHAxis axis) { return GetGraph (out axis, out _); }
  725. private GraphView GetGraph (out FakeVAxis axis) { return GetGraph (out _, out axis); }
  726. private GraphView GetGraph (out FakeHAxis axisX, out FakeVAxis axisY)
  727. {
  728. GraphViewTests.InitFakeDriver ();
  729. var gv = new GraphView ();
  730. gv.Viewport = new Rectangle (0, 0, 50, 30);
  731. gv.ColorScheme = new ColorScheme ();
  732. // graph can't be completely empty or it won't draw
  733. gv.Series.Add (new ScatterSeries ());
  734. axisX = new FakeHAxis ();
  735. axisY = new FakeVAxis ();
  736. gv.AxisX = axisX;
  737. gv.AxisY = axisY;
  738. return gv;
  739. }
  740. #region HorizontalAxis Tests
  741. /// <summary>Tests that the horizontal axis is computed correctly and does not over spill it's bounds</summary>
  742. [Fact]
  743. public void TestHAxisLocation_NoMargin ()
  744. {
  745. GraphView gv = GetGraph (out FakeHAxis axis);
  746. gv.LayoutSubviews ();
  747. gv.Draw ();
  748. Assert.DoesNotContain (new Point (-1, 29), axis.DrawAxisLinePoints);
  749. Assert.Contains (new Point (0, 29), axis.DrawAxisLinePoints);
  750. Assert.Contains (new Point (1, 29), axis.DrawAxisLinePoints);
  751. Assert.Contains (new Point (48, 29), axis.DrawAxisLinePoints);
  752. Assert.Contains (new Point (49, 29), axis.DrawAxisLinePoints);
  753. Assert.DoesNotContain (new Point (50, 29), axis.DrawAxisLinePoints);
  754. Assert.InRange (axis.LabelPoints.Max (), 0, 49);
  755. Assert.InRange (axis.LabelPoints.Min (), 0, 49);
  756. // Shutdown must be called to safely clean up Application if Init has been called
  757. Application.Shutdown ();
  758. }
  759. [Fact]
  760. public void TestHAxisLocation_MarginBottom ()
  761. {
  762. GraphView gv = GetGraph (out FakeHAxis axis);
  763. gv.MarginBottom = 10;
  764. gv.LayoutSubviews ();
  765. gv.Draw ();
  766. Assert.DoesNotContain (new Point (-1, 19), axis.DrawAxisLinePoints);
  767. Assert.Contains (new Point (0, 19), axis.DrawAxisLinePoints);
  768. Assert.Contains (new Point (1, 19), axis.DrawAxisLinePoints);
  769. Assert.Contains (new Point (48, 19), axis.DrawAxisLinePoints);
  770. Assert.Contains (new Point (49, 19), axis.DrawAxisLinePoints);
  771. Assert.DoesNotContain (new Point (50, 19), axis.DrawAxisLinePoints);
  772. Assert.InRange (axis.LabelPoints.Max (), 0, 49);
  773. Assert.InRange (axis.LabelPoints.Min (), 0, 49);
  774. // Shutdown must be called to safely clean up Application if Init has been called
  775. Application.Shutdown ();
  776. }
  777. [Fact]
  778. public void TestHAxisLocation_MarginLeft ()
  779. {
  780. GraphView gv = GetGraph (out FakeHAxis axis);
  781. gv.MarginLeft = 5;
  782. gv.LayoutSubviews ();
  783. gv.Draw ();
  784. Assert.DoesNotContain (new Point (4, 29), axis.DrawAxisLinePoints);
  785. Assert.Contains (new Point (5, 29), axis.DrawAxisLinePoints);
  786. Assert.Contains (new Point (6, 29), axis.DrawAxisLinePoints);
  787. Assert.Contains (new Point (48, 29), axis.DrawAxisLinePoints);
  788. Assert.Contains (new Point (49, 29), axis.DrawAxisLinePoints);
  789. Assert.DoesNotContain (new Point (50, 29), axis.DrawAxisLinePoints);
  790. // Axis lables should not be drawn in the margin
  791. Assert.InRange (axis.LabelPoints.Max (), 5, 49);
  792. Assert.InRange (axis.LabelPoints.Min (), 5, 49);
  793. // Shutdown must be called to safely clean up Application if Init has been called
  794. Application.Shutdown ();
  795. }
  796. #endregion
  797. #region VerticalAxisTests
  798. /// <summary>Tests that the horizontal axis is computed correctly and does not over spill it's bounds</summary>
  799. [Fact]
  800. public void TestVAxisLocation_NoMargin ()
  801. {
  802. GraphView gv = GetGraph (out FakeVAxis axis);
  803. gv.LayoutSubviews ();
  804. gv.Draw ();
  805. Assert.DoesNotContain (new Point (0, -1), axis.DrawAxisLinePoints);
  806. Assert.Contains (new Point (0, 1), axis.DrawAxisLinePoints);
  807. Assert.Contains (new Point (0, 2), axis.DrawAxisLinePoints);
  808. Assert.Contains (new Point (0, 28), axis.DrawAxisLinePoints);
  809. Assert.Contains (new Point (0, 29), axis.DrawAxisLinePoints);
  810. Assert.DoesNotContain (new Point (0, 30), axis.DrawAxisLinePoints);
  811. Assert.InRange (axis.LabelPoints.Max (), 0, 29);
  812. Assert.InRange (axis.LabelPoints.Min (), 0, 29);
  813. // Shutdown must be called to safely clean up Application if Init has been called
  814. Application.Shutdown ();
  815. }
  816. [Fact]
  817. public void TestVAxisLocation_MarginBottom ()
  818. {
  819. GraphView gv = GetGraph (out FakeVAxis axis);
  820. gv.MarginBottom = 10;
  821. gv.LayoutSubviews ();
  822. gv.Draw ();
  823. Assert.DoesNotContain (new Point (0, -1), axis.DrawAxisLinePoints);
  824. Assert.Contains (new Point (0, 1), axis.DrawAxisLinePoints);
  825. Assert.Contains (new Point (0, 2), axis.DrawAxisLinePoints);
  826. Assert.Contains (new Point (0, 18), axis.DrawAxisLinePoints);
  827. Assert.Contains (new Point (0, 19), axis.DrawAxisLinePoints);
  828. Assert.DoesNotContain (new Point (0, 20), axis.DrawAxisLinePoints);
  829. // Labels should not be drawn into the axis
  830. Assert.InRange (axis.LabelPoints.Max (), 0, 19);
  831. Assert.InRange (axis.LabelPoints.Min (), 0, 19);
  832. // Shutdown must be called to safely clean up Application if Init has been called
  833. Application.Shutdown ();
  834. }
  835. [Fact]
  836. public void TestVAxisLocation_MarginLeft ()
  837. {
  838. GraphView gv = GetGraph (out FakeVAxis axis);
  839. gv.MarginLeft = 5;
  840. gv.LayoutSubviews ();
  841. gv.Draw ();
  842. Assert.DoesNotContain (new Point (5, -1), axis.DrawAxisLinePoints);
  843. Assert.Contains (new Point (5, 1), axis.DrawAxisLinePoints);
  844. Assert.Contains (new Point (5, 2), axis.DrawAxisLinePoints);
  845. Assert.Contains (new Point (5, 28), axis.DrawAxisLinePoints);
  846. Assert.Contains (new Point (5, 29), axis.DrawAxisLinePoints);
  847. Assert.DoesNotContain (new Point (5, 30), axis.DrawAxisLinePoints);
  848. Assert.InRange (axis.LabelPoints.Max (), 0, 29);
  849. Assert.InRange (axis.LabelPoints.Min (), 0, 29);
  850. // Shutdown must be called to safely clean up Application if Init has been called
  851. Application.Shutdown ();
  852. }
  853. #endregion
  854. }
  855. public class TextAnnotationTests
  856. {
  857. private readonly ITestOutputHelper _output;
  858. public TextAnnotationTests (ITestOutputHelper output) { _output = output; }
  859. [Theory]
  860. [InlineData (null)]
  861. [InlineData (" ")]
  862. [InlineData ("\t\t")]
  863. public void TestTextAnnotation_EmptyText (string whitespace)
  864. {
  865. GraphView gv = GraphViewTests.GetGraph ();
  866. gv.Annotations.Add (
  867. new TextAnnotation { Text = whitespace, GraphPosition = new PointF (4, 2) }
  868. );
  869. // add a point a bit further along the graph so if the whitespace were rendered
  870. // the test would pick it up (AssertDriverContentsAre ignores trailing whitespace on lines)
  871. var points = new ScatterSeries ();
  872. points.Points.Add (new PointF (7, 2));
  873. gv.Series.Add (points);
  874. gv.LayoutSubviews ();
  875. gv.Draw ();
  876. var expected =
  877. @$"
  878. ┤ {CM.Glyphs.Dot}
  879. 0┼┬┬┬┬┬┬┬┬
  880. 0 5";
  881. TestHelpers.AssertDriverContentsAre (expected, _output);
  882. // Shutdown must be called to safely clean up Application if Init has been called
  883. Application.Shutdown ();
  884. }
  885. [Fact]
  886. public void TestTextAnnotation_GraphUnits ()
  887. {
  888. GraphView gv = GraphViewTests.GetGraph ();
  889. gv.Annotations.Add (
  890. new TextAnnotation { Text = "hey!", GraphPosition = new PointF (2, 2) }
  891. );
  892. gv.LayoutSubviews ();
  893. gv.Draw ();
  894. var expected =
  895. @"
  896. ┤ hey!
  897. 0┼┬┬┬┬┬┬┬┬
  898. 0 5";
  899. TestHelpers.AssertDriverContentsAre (expected, _output);
  900. // user scrolls up one unit of graph space
  901. gv.ScrollOffset = new PointF (0, 1f);
  902. gv.SetNeedsDraw ();
  903. View.SetClipToScreen ();
  904. gv.Draw ();
  905. // we expect the text annotation to go down one line since
  906. // the scroll offset means that that point of graph space is
  907. // lower down in the view. Note the 1 on the axis too, our viewport
  908. // (excluding margins) now shows y of 1 to 4 (previously 0 to 5)
  909. expected =
  910. @"
  911. ┤ hey!
  912. 1┼┬┬┬┬┬┬┬┬
  913. 0 5";
  914. TestHelpers.AssertDriverContentsAre (expected, _output);
  915. // Shutdown must be called to safely clean up Application if Init has been called
  916. Application.Shutdown ();
  917. }
  918. [Fact]
  919. public void TestTextAnnotation_LongText ()
  920. {
  921. GraphView gv = GraphViewTests.GetGraph ();
  922. gv.Annotations.Add (
  923. new TextAnnotation
  924. {
  925. Text = "hey there partner hows it going boy its great", GraphPosition = new PointF (2, 2)
  926. }
  927. );
  928. gv.LayoutSubviews ();
  929. gv.SetNeedsDraw ();
  930. gv.Draw ();
  931. // long text should get truncated
  932. // margin takes up 1 units
  933. // the GraphPosition of the anntation is 2
  934. // Leaving 7 characters of the annotation renderable (including space)
  935. var expected =
  936. @"
  937. ┤ hey the
  938. 0┼┬┬┬┬┬┬┬┬
  939. 0 5";
  940. TestHelpers.AssertDriverContentsAre (expected, _output);
  941. // Shutdown must be called to safely clean up Application if Init has been called
  942. Application.Shutdown ();
  943. }
  944. [Fact]
  945. public void TestTextAnnotation_Offscreen ()
  946. {
  947. GraphView gv = GraphViewTests.GetGraph ();
  948. gv.Annotations.Add (
  949. new TextAnnotation
  950. {
  951. Text = "hey there partner hows it going boy its great", GraphPosition = new PointF (9, 2)
  952. }
  953. );
  954. gv.LayoutSubviews ();
  955. gv.Draw ();
  956. // Text is off the screen (graph x axis runs to 8 not 9)
  957. var expected =
  958. @"
  959. 0┼┬┬┬┬┬┬┬┬
  960. 0 5";
  961. TestHelpers.AssertDriverContentsAre (expected, _output);
  962. // Shutdown must be called to safely clean up Application if Init has been called
  963. Application.Shutdown ();
  964. }
  965. [Fact]
  966. public void TestTextAnnotation_ScreenUnits ()
  967. {
  968. GraphView gv = GraphViewTests.GetGraph ();
  969. gv.Annotations.Add (
  970. new TextAnnotation { Text = "hey!", ScreenPosition = new Point (3, 1) }
  971. );
  972. gv.LayoutSubviews ();
  973. View.SetClipToScreen ();
  974. gv.Draw ();
  975. var expected =
  976. @"
  977. ┤ hey!
  978. 0┼┬┬┬┬┬┬┬┬
  979. 0 5";
  980. TestHelpers.AssertDriverContentsAre (expected, _output);
  981. // user scrolls up one unit of graph space
  982. gv.ScrollOffset = new PointF (0, 1f);
  983. gv.SetNeedsDraw ();
  984. View.SetClipToScreen ();
  985. gv.Draw ();
  986. // we expect no change in the location of the annotation (only the axis label changes)
  987. // this is because screen units are constant and do not change as the viewport into
  988. // graph space scrolls to different areas of the graph
  989. expected =
  990. @"
  991. ┤ hey!
  992. 1┼┬┬┬┬┬┬┬┬
  993. 0 5";
  994. TestHelpers.AssertDriverContentsAre (expected, _output);
  995. // user scrolls up one unit of graph space
  996. gv.ScrollOffset = new PointF (0, 1f);
  997. gv.SetNeedsDraw ();
  998. View.SetClipToScreen ();
  999. gv.Draw ();
  1000. // we expect no change in the location of the annotation (only the axis label changes)
  1001. // this is because screen units are constant and do not change as the viewport into
  1002. // graph space scrolls to different areas of the graph
  1003. expected =
  1004. @"
  1005. ┤ hey!
  1006. 1┼┬┬┬┬┬┬┬┬
  1007. 0 5";
  1008. TestHelpers.AssertDriverContentsAre (expected, _output);
  1009. // Shutdown must be called to safely clean up Application if Init has been called
  1010. Application.Shutdown ();
  1011. }
  1012. }
  1013. public class LegendTests
  1014. {
  1015. private readonly ITestOutputHelper _output;
  1016. public LegendTests (ITestOutputHelper output) { _output = output; }
  1017. [Fact]
  1018. public void Constructors_Defaults ()
  1019. {
  1020. var legend = new LegendAnnotation ();
  1021. Assert.Equal (Rectangle.Empty, legend.Viewport);
  1022. Assert.Equal (Rectangle.Empty, legend.Frame);
  1023. Assert.Equal (LineStyle.Single, legend.BorderStyle);
  1024. Assert.False (legend.BeforeSeries);
  1025. var bounds = new Rectangle (1, 2, 10, 3);
  1026. legend = new LegendAnnotation (bounds);
  1027. Assert.Equal (new Rectangle (0, 0, 8, 1), legend.Viewport);
  1028. Assert.Equal (bounds, legend.Frame);
  1029. Assert.Equal (LineStyle.Single, legend.BorderStyle);
  1030. Assert.False (legend.BeforeSeries);
  1031. legend.BorderStyle = LineStyle.None;
  1032. Assert.Equal (new Rectangle (0, 0, 10, 3), legend.Viewport);
  1033. Assert.Equal (bounds, legend.Frame);
  1034. }
  1035. [Fact]
  1036. public void LegendNormalUsage_WithBorder ()
  1037. {
  1038. GraphView gv = GraphViewTests.GetGraph ();
  1039. var legend = new LegendAnnotation (new Rectangle (2, 0, 5, 3));
  1040. legend.AddEntry (new GraphCellToRender ((Rune)'A'), "Ant");
  1041. legend.AddEntry (new GraphCellToRender ((Rune)'B'), "Bat");
  1042. gv.Annotations.Add (legend);
  1043. gv.Layout ();
  1044. gv.Draw ();
  1045. var expected =
  1046. @"
  1047. │┌───┐
  1048. ┤│AAn│
  1049. ┤└───┘
  1050. 0┼┬┬┬┬┬┬┬┬
  1051. 0 5";
  1052. TestHelpers.AssertDriverContentsAre (expected, _output);
  1053. // Shutdown must be called to safely clean up Application if Init has been called
  1054. Application.Shutdown ();
  1055. }
  1056. [Fact]
  1057. public void LegendNormalUsage_WithoutBorder ()
  1058. {
  1059. GraphView gv = GraphViewTests.GetGraph ();
  1060. var legend = new LegendAnnotation (new Rectangle (2, 0, 5, 3));
  1061. legend.AddEntry (new GraphCellToRender ((Rune)'A'), "Ant");
  1062. legend.AddEntry (new GraphCellToRender ((Rune)'B'), "?"); // this will exercise pad
  1063. legend.AddEntry (new GraphCellToRender ((Rune)'C'), "Cat");
  1064. legend.AddEntry (new GraphCellToRender ((Rune)'H'), "Hattter"); // not enough space for this oen
  1065. legend.BorderStyle = LineStyle.None;
  1066. gv.Annotations.Add (legend);
  1067. gv.Draw ();
  1068. var expected =
  1069. @"
  1070. │AAnt
  1071. ┤B?
  1072. ┤CCat
  1073. 0┼┬┬┬┬┬┬┬┬
  1074. 0 5";
  1075. TestHelpers.AssertDriverContentsAre (expected, _output);
  1076. // Shutdown must be called to safely clean up Application if Init has been called
  1077. Application.Shutdown ();
  1078. }
  1079. }
  1080. public class PathAnnotationTests
  1081. {
  1082. private readonly ITestOutputHelper _output;
  1083. public PathAnnotationTests (ITestOutputHelper output) { _output = output; }
  1084. [Fact]
  1085. public void MarginBottom_BiggerThanHeight_ExpectBlankGraph ()
  1086. {
  1087. GraphView gv = GraphViewTests.GetGraph ();
  1088. gv.Height = 10;
  1089. gv.MarginBottom = 20;
  1090. gv.Series.Add (
  1091. new ScatterSeries { Points = { new PointF (1, 1), new PointF (5, 0) } }
  1092. );
  1093. gv.LayoutSubviews ();
  1094. gv.Draw ();
  1095. var expected =
  1096. @"
  1097. ";
  1098. TestHelpers.AssertDriverContentsAre (expected, _output);
  1099. // Shutdown must be called to safely clean up Application if Init has been called
  1100. Application.Shutdown ();
  1101. }
  1102. [Fact]
  1103. public void MarginLeft_BiggerThanWidth_ExpectBlankGraph ()
  1104. {
  1105. GraphView gv = GraphViewTests.GetGraph ();
  1106. gv.Width = 10;
  1107. gv.MarginLeft = 20;
  1108. gv.Series.Add (
  1109. new ScatterSeries { Points = { new PointF (1, 1), new PointF (5, 0) } }
  1110. );
  1111. gv.LayoutSubviews ();
  1112. gv.Draw ();
  1113. var expected =
  1114. @"
  1115. ";
  1116. TestHelpers.AssertDriverContentsAre (expected, _output);
  1117. // Shutdown must be called to safely clean up Application if Init has been called
  1118. Application.Shutdown ();
  1119. }
  1120. [Fact]
  1121. public void PathAnnotation_Box ()
  1122. {
  1123. GraphView gv = GraphViewTests.GetGraph ();
  1124. var path = new PathAnnotation ();
  1125. path.Points.Add (new PointF (1, 1));
  1126. path.Points.Add (new PointF (1, 3));
  1127. path.Points.Add (new PointF (6, 3));
  1128. path.Points.Add (new PointF (6, 1));
  1129. // list the starting point again so that it draws a complete square
  1130. // (otherwise it will miss out the last line along the bottom)
  1131. path.Points.Add (new PointF (1, 1));
  1132. gv.Annotations.Add (path);
  1133. gv.LayoutSubviews ();
  1134. gv.Draw ();
  1135. var expected =
  1136. @"
  1137. │......
  1138. ┤. .
  1139. ┤......
  1140. 0┼┬┬┬┬┬┬┬┬
  1141. 0 5";
  1142. TestHelpers.AssertDriverContentsAre (expected, _output);
  1143. // Shutdown must be called to safely clean up Application if Init has been called
  1144. Application.Shutdown ();
  1145. }
  1146. [Fact]
  1147. public void PathAnnotation_Diamond ()
  1148. {
  1149. GraphView gv = GraphViewTests.GetGraph ();
  1150. var path = new PathAnnotation ();
  1151. path.Points.Add (new PointF (1, 2));
  1152. path.Points.Add (new PointF (3, 3));
  1153. path.Points.Add (new PointF (6, 2));
  1154. path.Points.Add (new PointF (3, 1));
  1155. // list the starting point again to close the shape
  1156. path.Points.Add (new PointF (1, 2));
  1157. gv.Annotations.Add (path);
  1158. gv.LayoutSubviews ();
  1159. gv.Draw ();
  1160. var expected =
  1161. @"
  1162. │ ..
  1163. ┤.. ..
  1164. ┤ ...
  1165. 0┼┬┬┬┬┬┬┬┬
  1166. 0 5";
  1167. TestHelpers.AssertDriverContentsAre (expected, _output);
  1168. // Shutdown must be called to safely clean up Application if Init has been called
  1169. Application.Shutdown ();
  1170. }
  1171. [Theory]
  1172. [InlineData (true)]
  1173. [InlineData (false)]
  1174. public void ViewChangeText_RendersCorrectly (bool useFill)
  1175. {
  1176. var driver = new FakeDriver ();
  1177. Application.Init (driver);
  1178. driver.Init ();
  1179. // create a wide window
  1180. var mount = new View { Width = 100, Height = 100 };
  1181. var top = new Toplevel ();
  1182. try
  1183. {
  1184. // Create a view with a short text
  1185. var view = new View { Text = "ff", Width = 2, Height = 1 };
  1186. // Specify that the label should be very wide
  1187. if (useFill)
  1188. {
  1189. view.Width = Dim.Fill ();
  1190. }
  1191. else
  1192. {
  1193. view.Width = 100;
  1194. }
  1195. //put label into view
  1196. mount.Add (view);
  1197. //putting mount into Toplevel since changing size
  1198. top.Add (mount);
  1199. Application.Begin (top);
  1200. // render view
  1201. view.ColorScheme = new ColorScheme ();
  1202. Assert.Equal (1, view.Height);
  1203. mount.SetNeedsDraw ();
  1204. mount.Draw ();
  1205. // should have the initial text
  1206. TestHelpers.AssertDriverContentsAre ("ff", null);
  1207. // change the text and redraw
  1208. view.Text = "ff1234";
  1209. mount.SetNeedsDraw ();
  1210. View.SetClipToScreen ();
  1211. mount.Draw ();
  1212. // should have the new text rendered
  1213. TestHelpers.AssertDriverContentsAre ("ff1234", null);
  1214. }
  1215. finally
  1216. {
  1217. top.Dispose ();
  1218. Application.Shutdown ();
  1219. }
  1220. }
  1221. [Fact]
  1222. public void XAxisLabels_With_MarginLeft ()
  1223. {
  1224. GraphViewTests.InitFakeDriver ();
  1225. var gv = new GraphView { ColorScheme = new ColorScheme (), Viewport = new Rectangle (0, 0, 10, 7) };
  1226. gv.CellSize = new PointF (1, 0.5f);
  1227. gv.AxisY.Increment = 1;
  1228. gv.AxisY.ShowLabelsEvery = 1;
  1229. gv.Series.Add (
  1230. new ScatterSeries { Points = { new PointF (1, 1), new PointF (5, 0) } }
  1231. );
  1232. // reserve 3 cells of the left for the margin
  1233. gv.MarginLeft = 3;
  1234. gv.MarginBottom = 1;
  1235. gv.LayoutSubviews ();
  1236. gv.SetNeedsDraw ();
  1237. gv.Draw ();
  1238. var expected =
  1239. @$"
  1240. 2┤
  1241. 1┤{CM.Glyphs.Dot}
  1242. 0┼┬┬┬┬{CM.Glyphs.Dot}┬
  1243. 0 5
  1244. ";
  1245. TestHelpers.AssertDriverContentsAre (expected, _output);
  1246. // Shutdown must be called to safely clean up Application if Init has been called
  1247. Application.Shutdown ();
  1248. }
  1249. [Fact]
  1250. public void YAxisLabels_With_MarginBottom ()
  1251. {
  1252. GraphViewTests.InitFakeDriver ();
  1253. var gv = new GraphView { ColorScheme = new ColorScheme (), Viewport = new Rectangle (0, 0, 10, 7) };
  1254. gv.CellSize = new PointF (1, 0.5f);
  1255. gv.AxisY.Increment = 1;
  1256. gv.AxisY.ShowLabelsEvery = 1;
  1257. gv.Series.Add (
  1258. new ScatterSeries { Points = { new PointF (1, 1), new PointF (5, 0) } }
  1259. );
  1260. // reserve 3 cells of the console for the margin
  1261. gv.MarginBottom = 3;
  1262. gv.MarginLeft = 1;
  1263. gv.LayoutSubviews ();
  1264. gv.SetNeedsDraw ();
  1265. gv.Draw ();
  1266. var expected =
  1267. @$"
  1268. 1┤{CM.Glyphs.Dot}
  1269. 0┼┬┬┬┬{CM.Glyphs.Dot}┬┬┬
  1270. 0 5
  1271. ";
  1272. TestHelpers.AssertDriverContentsAre (expected, _output);
  1273. // Shutdown must be called to safely clean up Application if Init has been called
  1274. Application.Shutdown ();
  1275. }
  1276. }
  1277. public class AxisIncrementToRenderTests
  1278. {
  1279. [Fact]
  1280. public void AxisIncrementToRenderTests_Constructor ()
  1281. {
  1282. var render = new AxisIncrementToRender (Orientation.Horizontal, 1, 6.6f);
  1283. Assert.Equal (Orientation.Horizontal, render.Orientation);
  1284. Assert.Equal (1, render.ScreenLocation);
  1285. Assert.Equal (6.6f, render.Value);
  1286. }
  1287. }