GraphViewTests.cs 43 KB

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