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