GraphViewTests.cs 44 KB

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