GraphViewTests.cs 43 KB

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