GraphViewTests.cs 52 KB

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