GraphViewTests.cs 40 KB

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