GraphViewTests.cs 44 KB

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