GraphViewTests.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  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. }
  287. /// <summary>
  288. /// Tests that each point in the screen space maps to a rectangle of
  289. /// (float) graph space and that each corner of that rectangle of graph
  290. /// space maps back to the same row/col of the graph that was fed in
  291. /// </summary>
  292. [Fact]
  293. public void TestReversing_ScreenToGraphSpace ()
  294. {
  295. var gv = new GraphView ();
  296. gv.Bounds = new Rect (0, 0, 50, 30);
  297. // How much graph space each cell of the console depicts
  298. gv.CellSize = new PointF (0.1f, 0.25f);
  299. gv.AxisX.Increment = 1;
  300. gv.AxisX.ShowLabelsEvery = 1;
  301. gv.AxisY.Increment = 1;
  302. gv.AxisY.ShowLabelsEvery = 1;
  303. // Start the graph at 80
  304. gv.ScrollOffset = new PointF (0, 80);
  305. for (int x = 0; x < gv.Bounds.Width; x++) {
  306. for (int y = 0; y < gv.Bounds.Height; y++) {
  307. var graphSpace = gv.ScreenToGraphSpace (x, y);
  308. // See
  309. // https://en.wikipedia.org/wiki/Machine_epsilon
  310. float epsilon = 0.0001f;
  311. var p = gv.GraphSpaceToScreen (new PointF (graphSpace.Left + epsilon, graphSpace.Top + epsilon));
  312. Assert.Equal (x, p.X);
  313. Assert.Equal (y, p.Y);
  314. p = gv.GraphSpaceToScreen (new PointF (graphSpace.Right - epsilon , graphSpace.Top + epsilon));
  315. Assert.Equal (x, p.X);
  316. Assert.Equal (y, p.Y);
  317. p = gv.GraphSpaceToScreen (new PointF (graphSpace.Left + epsilon, graphSpace.Bottom - epsilon));
  318. Assert.Equal (x, p.X);
  319. Assert.Equal (y, p.Y);
  320. p = gv.GraphSpaceToScreen (new PointF (graphSpace.Right - epsilon, graphSpace.Bottom - epsilon));
  321. Assert.Equal (x, p.X);
  322. Assert.Equal (y, p.Y);
  323. }
  324. }
  325. }
  326. }
  327. public class SeriesTests {
  328. [Fact]
  329. public void Series_GetsPassedCorrectBounds_AllAtOnce ()
  330. {
  331. GraphViewTests.InitFakeDriver ();
  332. var gv = new GraphView ();
  333. gv.ColorScheme = new ColorScheme ();
  334. gv.Bounds = new Rect (0, 0, 50, 30);
  335. RectangleF fullGraphBounds = RectangleF.Empty;
  336. Rect graphScreenBounds = Rect.Empty;
  337. var series = new FakeSeries ((v, s, g) => { graphScreenBounds = s; fullGraphBounds = g; });
  338. gv.Series.Add (series);
  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.Redraw (gv.Bounds);
  349. Assert.Equal (new RectangleF (0, 0, 45, 28), fullGraphBounds);
  350. // The screen space the graph will be rendered into should
  351. // not overspill the margins
  352. Assert.Equal (new Rect (5, 0, 45, 28), graphScreenBounds);
  353. }
  354. /// <summary>
  355. /// Tests that the bounds passed to the ISeries for drawing into are
  356. /// correct even when the <see cref="GraphView.CellSize"/> results in
  357. /// multiple units of graph space being condensed into each cell of
  358. /// console
  359. /// </summary>
  360. [Fact]
  361. public void Series_GetsPassedCorrectBounds_AllAtOnce_LargeCellSize ()
  362. {
  363. GraphViewTests.InitFakeDriver ();
  364. var gv = new GraphView ();
  365. gv.ColorScheme = new ColorScheme ();
  366. gv.Bounds = new Rect (0, 0, 50, 30);
  367. // the larger the cell size the more condensed (smaller) the graph space is
  368. gv.CellSize = new PointF (2, 5);
  369. RectangleF fullGraphBounds = RectangleF.Empty;
  370. Rect graphScreenBounds = Rect.Empty;
  371. var series = new FakeSeries ((v, s, g) => { graphScreenBounds = s; fullGraphBounds = g; });
  372. gv.Series.Add (series);
  373. gv.Redraw (gv.Bounds);
  374. // Since each cell of the console is 2x5 of graph space the graph
  375. // bounds to be rendered are larger
  376. Assert.Equal (new RectangleF (0, 0, 100, 150), fullGraphBounds);
  377. Assert.Equal (new Rect (0, 0, 50, 30), graphScreenBounds);
  378. // Graph should not spill into the margins
  379. gv.MarginBottom = 2;
  380. gv.MarginLeft = 5;
  381. // Even with a margin the graph should be drawn from
  382. // the origin, we just get less visible width/height
  383. gv.Redraw (gv.Bounds);
  384. Assert.Equal (new RectangleF (0, 0, 90, 140), fullGraphBounds);
  385. // The screen space the graph will be rendered into should
  386. // not overspill the margins
  387. Assert.Equal (new Rect (5, 0, 45, 28), graphScreenBounds);
  388. }
  389. private class FakeSeries : ISeries {
  390. readonly Action<GraphView, Rect, RectangleF> drawSeries;
  391. public FakeSeries (
  392. Action<GraphView, Rect, RectangleF> drawSeries
  393. )
  394. {
  395. this.drawSeries = drawSeries;
  396. }
  397. public void DrawSeries (GraphView graph, Rect bounds, RectangleF graphBounds)
  398. {
  399. drawSeries (graph, bounds, graphBounds);
  400. }
  401. }
  402. }
  403. public class MultiBarSeriesTests{
  404. readonly ITestOutputHelper output;
  405. public MultiBarSeriesTests(ITestOutputHelper output)
  406. {
  407. this.output = output;
  408. }
  409. [Fact]
  410. public void MultiBarSeries_BarSpacing(){
  411. // Creates clusters of 5 adjacent bars with 2 spaces between clusters
  412. var series = new MultiBarSeries(5,7,1);
  413. Assert.Equal(5,series.SubSeries.Count);
  414. Assert.Equal(0,series.SubSeries.ElementAt(0).Offset);
  415. Assert.Equal(1,series.SubSeries.ElementAt(1).Offset);
  416. Assert.Equal(2,series.SubSeries.ElementAt(2).Offset);
  417. Assert.Equal(3,series.SubSeries.ElementAt(3).Offset);
  418. Assert.Equal(4,series.SubSeries.ElementAt(4).Offset);
  419. }
  420. [Fact]
  421. public void MultiBarSeriesColors_WrongNumber(){
  422. var fake = new FakeDriver ();
  423. var colors = new []{
  424. fake.MakeAttribute(Color.Green,Color.Black)
  425. };
  426. // user passes 1 color only but asks for 5 bars
  427. var ex = Assert.Throws<ArgumentException>(()=>new MultiBarSeries(5,7,1,colors));
  428. Assert.Equal("Number of colors must match the number of bars (Parameter 'numberOfBarsPerCategory')",ex.Message);
  429. }
  430. [Fact]
  431. public void MultiBarSeriesColors_RightNumber(){
  432. var fake = new FakeDriver ();
  433. var colors = new []{
  434. fake.MakeAttribute(Color.Green,Color.Black),
  435. fake.MakeAttribute(Color.Green,Color.White),
  436. fake.MakeAttribute(Color.BrightYellow,Color.White)
  437. };
  438. // user passes 3 colors and asks for 3 bars
  439. var series = new MultiBarSeries(3,7,1,colors);
  440. Assert.Equal(series.SubSeries.ElementAt(0).OverrideBarColor,colors[0]);
  441. Assert.Equal(series.SubSeries.ElementAt(1).OverrideBarColor,colors[1]);
  442. Assert.Equal(series.SubSeries.ElementAt(2).OverrideBarColor,colors[2]);
  443. }
  444. [Fact]
  445. public void MultiBarSeriesAddValues_WrongNumber(){
  446. // user asks for 3 bars per category
  447. var series = new MultiBarSeries(3,7,1);
  448. var ex = Assert.Throws<ArgumentException>(()=>series.AddBars("Cars",'#',1));
  449. Assert.Equal("Number of values must match the number of bars per category (Parameter 'values')",ex.Message);
  450. }
  451. [Fact]
  452. public void TestRendering_MultibarSeries(){
  453. GraphViewTests.InitFakeDriver ();
  454. var gv = new GraphView ();
  455. gv.ColorScheme = new ColorScheme ();
  456. // y axis goes from 0.1 to 1 across 10 console rows
  457. // x axis goes from 0 to 20 across 20 console columns
  458. gv.Bounds = new Rect (0, 0, 20, 10);
  459. gv.CellSize = new PointF(1f,0.1f);
  460. gv.MarginBottom = 1;
  461. gv.MarginLeft = 1;
  462. var multibarSeries = new MultiBarSeries (2,4,1);
  463. //nudge them left to avoid float rounding errors at the boundaries of cells
  464. foreach(var sub in multibarSeries.SubSeries) {
  465. sub.Offset -= 0.001f;
  466. }
  467. gv.Series.Add (multibarSeries);
  468. FakeHAxis fakeXAxis;
  469. // don't show axis labels that means any labels
  470. // that appaer are explicitly from the bars
  471. gv.AxisX = fakeXAxis = new FakeHAxis(){Increment=0};
  472. gv.AxisY = new FakeVAxis(){Increment=0};
  473. gv.Redraw(gv.Bounds);
  474. // Since bar series has no bars yet no labels should be displayed
  475. Assert.Empty(fakeXAxis.LabelPoints);
  476. multibarSeries.AddBars("hey",'M',0.5001f, 0.5001f);
  477. fakeXAxis.LabelPoints.Clear();
  478. gv.Redraw(gv.Bounds);
  479. Assert.Equal(4,fakeXAxis.LabelPoints.Single());
  480. multibarSeries.AddBars("there",'M',0.24999f,0.74999f);
  481. multibarSeries.AddBars("bob",'M',1,2);
  482. fakeXAxis.LabelPoints.Clear();
  483. gv.Redraw(gv.Bounds);
  484. Assert.Equal(3,fakeXAxis.LabelPoints.Count);
  485. Assert.Equal(4,fakeXAxis.LabelPoints[0]);
  486. Assert.Equal(8,fakeXAxis.LabelPoints[1]);
  487. Assert.Equal (12, fakeXAxis.LabelPoints [2]);
  488. string looksLike =
  489. @"
  490. │ MM
  491. │ M MM
  492. │ M MM
  493. │ MM M MM
  494. │ MM M MM
  495. │ MM M MM
  496. │ MM MM MM
  497. │ MM MM MM
  498. ┼──┬M──┬M──┬M──────
  499. heytherebob ";
  500. GraphViewTests.AssertDriverContentsAre (looksLike, output);
  501. }
  502. }
  503. public class BarSeriesTests{
  504. private GraphView GetGraph (out FakeBarSeries series, out FakeHAxis axisX, out FakeVAxis axisY)
  505. {
  506. GraphViewTests.InitFakeDriver ();
  507. var gv = new GraphView ();
  508. gv.ColorScheme = new ColorScheme ();
  509. // y axis goes from 0.1 to 1 across 10 console rows
  510. // x axis goes from 0 to 10 across 20 console columns
  511. gv.Bounds = new Rect (0, 0, 20, 10);
  512. gv.CellSize = new PointF(0.5f,0.1f);
  513. gv.Series.Add (series = new FakeBarSeries ());
  514. // don't show axis labels that means any labels
  515. // that appaer are explicitly from the bars
  516. gv.AxisX = axisX = new FakeHAxis(){Increment=0};
  517. gv.AxisY = axisY = new FakeVAxis(){Increment=0};
  518. return gv;
  519. }
  520. [Fact]
  521. public void TestZeroHeightBar_WithName(){
  522. var graph = GetGraph(out FakeBarSeries barSeries, out FakeHAxis axisX, out FakeVAxis axisY);
  523. graph.Redraw(graph.Bounds);
  524. // no bars
  525. Assert.Empty(barSeries.BarScreenStarts);
  526. Assert.Empty(axisX.LabelPoints);
  527. Assert.Empty(axisY.LabelPoints);
  528. // bar of height 0
  529. barSeries.Bars.Add(new BarSeries.Bar("hi",new GraphCellToRender('.'),0));
  530. barSeries.Orientation = Orientation.Vertical;
  531. // redraw graph
  532. graph.Redraw(graph.Bounds);
  533. // bar should not be drawn
  534. Assert.Empty(barSeries.BarScreenStarts);
  535. Assert.NotEmpty(axisX.LabelPoints);
  536. Assert.Empty(axisY.LabelPoints);
  537. // but bar name should be
  538. // Screen position x=2 because bars are drawn every 1f of
  539. // graph space and CellSize.X is 0.5f
  540. Assert.Contains(2, axisX.LabelPoints);
  541. }
  542. [Fact]
  543. public void TestTwoTallBars_WithOffset(){
  544. var graph = GetGraph(out FakeBarSeries barSeries, out FakeHAxis axisX, out FakeVAxis axisY);
  545. graph.Redraw(graph.Bounds);
  546. // no bars
  547. Assert.Empty(barSeries.BarScreenStarts);
  548. Assert.Empty(axisX.LabelPoints);
  549. Assert.Empty(axisY.LabelPoints);
  550. // 0.5 units of graph fit every screen cell
  551. // so 1 unit of graph space is 2 screen columns
  552. graph.CellSize = new PointF(0.5f,0.1f);
  553. // Start bar 1 screen unit along
  554. barSeries.Offset = 0.5f;
  555. barSeries.BarEvery = 1f;
  556. barSeries.Bars.Add(
  557. new BarSeries.Bar("hi1",new GraphCellToRender('.'),100));
  558. barSeries.Bars.Add(
  559. new BarSeries.Bar("hi2",new GraphCellToRender('.'),100));
  560. barSeries.Orientation = Orientation.Vertical;
  561. // redraw graph
  562. graph.Redraw(graph.Bounds);
  563. // bar should be drawn at BarEvery 1f + offset 0.5f = 3 screen units
  564. Assert.Equal(3,barSeries.BarScreenStarts[0].X);
  565. Assert.Equal(3,barSeries.BarScreenEnds[0].X);
  566. // second bar should be BarEveryx2 = 2f + offset 0.5f = 5 screen units
  567. Assert.Equal(5,barSeries.BarScreenStarts[1].X);
  568. Assert.Equal(5,barSeries.BarScreenEnds[1].X);
  569. // both bars should have labels
  570. Assert.Equal(2,axisX.LabelPoints.Count);
  571. Assert.Contains(3, axisX.LabelPoints);
  572. Assert.Contains(5, axisX.LabelPoints);
  573. // bars are very tall but should not draw up off top of screen
  574. Assert.Equal(9,barSeries.BarScreenStarts[0].Y);
  575. Assert.Equal(0,barSeries.BarScreenEnds[0].Y);
  576. Assert.Equal(9,barSeries.BarScreenStarts[1].Y);
  577. Assert.Equal(0,barSeries.BarScreenEnds[1].Y);
  578. }
  579. [Fact]
  580. public void TestOneLongOneShortHorizontalBars_WithOffset(){
  581. var graph = GetGraph(out FakeBarSeries barSeries, out FakeHAxis axisX, out FakeVAxis axisY);
  582. graph.Redraw(graph.Bounds);
  583. // no bars
  584. Assert.Empty(barSeries.BarScreenStarts);
  585. Assert.Empty(axisX.LabelPoints);
  586. Assert.Empty(axisY.LabelPoints);
  587. // 0.1 units of graph y fit every screen row
  588. // so 1 unit of graph y space is 10 screen rows
  589. graph.CellSize = new PointF(0.5f,0.1f);
  590. // Start bar 3 screen units up (y = height-3)
  591. barSeries.Offset = 0.25f;
  592. // 1 bar every 3 rows of screen
  593. barSeries.BarEvery = 0.3f;
  594. barSeries.Orientation = Orientation.Horizontal;
  595. // 1 bar that is very wide (100 graph units horizontally = screen pos 50 but bounded by screen)
  596. barSeries.Bars.Add(
  597. new BarSeries.Bar("hi1",new GraphCellToRender('.'),100));
  598. // 1 bar that is shorter
  599. barSeries.Bars.Add(
  600. new BarSeries.Bar("hi2",new GraphCellToRender('.'),5));
  601. // redraw graph
  602. graph.Redraw(graph.Bounds);
  603. // since bars are horizontal all have the same X start cordinates
  604. Assert.Equal(0,barSeries.BarScreenStarts[0].X);
  605. Assert.Equal(0,barSeries.BarScreenStarts[1].X);
  606. // bar goes all the way to the end so bumps up against right screen boundary
  607. // width of graph is 20
  608. Assert.Equal(19,barSeries.BarScreenEnds[0].X);
  609. // shorter bar is 5 graph units wide which is 10 screen units
  610. Assert.Equal(10,barSeries.BarScreenEnds[1].X);
  611. // first bar should be offset 6 screen units (0.25f + 0.3f graph units)
  612. // since height of control is 10 then first bar should be at screen row 4 (10-6)
  613. Assert.Equal(4,barSeries.BarScreenStarts[0].Y);
  614. // second bar should be offset 9 screen units (0.25f + 0.6f graph units)
  615. // since height of control is 10 then second bar should be at screen row 1 (10-9)
  616. Assert.Equal(1,barSeries.BarScreenStarts[1].Y);
  617. // both bars should have labels but on the y axis
  618. Assert.Equal(2,axisY.LabelPoints.Count);
  619. Assert.Empty(axisX.LabelPoints);
  620. // labels should align with the bars (same screen y axis point)
  621. Assert.Contains(4, axisY.LabelPoints);
  622. Assert.Contains(1, axisY.LabelPoints);
  623. }
  624. private class FakeBarSeries : BarSeries{
  625. public GraphCellToRender FinalColor { get; private set; }
  626. public List<Point> BarScreenStarts { get; private set; } = new List<Point>();
  627. public List<Point> BarScreenEnds { get; private set; } = new List<Point>();
  628. protected override GraphCellToRender AdjustColor (GraphCellToRender graphCellToRender)
  629. {
  630. return FinalColor = base.AdjustColor (graphCellToRender);
  631. }
  632. protected override void DrawBarLine (GraphView graph, Point start, Point end, Bar beingDrawn)
  633. {
  634. base.DrawBarLine (graph, start, end, beingDrawn);
  635. BarScreenStarts.Add(start);
  636. BarScreenEnds.Add(end);
  637. }
  638. }
  639. }
  640. public class AxisTests {
  641. private GraphView GetGraph (out FakeHAxis axis)
  642. {
  643. return GetGraph(out axis, out _);
  644. }
  645. private GraphView GetGraph (out FakeVAxis axis)
  646. {
  647. return GetGraph(out _, out axis);
  648. }
  649. private GraphView GetGraph (out FakeHAxis axisX, out FakeVAxis axisY)
  650. {
  651. GraphViewTests.InitFakeDriver ();
  652. var gv = new GraphView ();
  653. gv.ColorScheme = new ColorScheme ();
  654. gv.Bounds = new Rect (0, 0, 50, 30);
  655. // graph can't be completely empty or it won't draw
  656. gv.Series.Add (new ScatterSeries ());
  657. axisX = new FakeHAxis ();
  658. axisY = new FakeVAxis ();
  659. gv.AxisX = axisX;
  660. gv.AxisY = axisY;
  661. return gv;
  662. }
  663. #region HorizontalAxis Tests
  664. /// <summary>
  665. /// Tests that the horizontal axis is computed correctly and does not over spill
  666. /// it's bounds
  667. /// </summary>
  668. [Fact]
  669. public void TestHAxisLocation_NoMargin ()
  670. {
  671. var gv = GetGraph (out FakeHAxis axis);
  672. gv.Redraw (gv.Bounds);
  673. Assert.DoesNotContain (new Point (-1, 29), axis.DrawAxisLinePoints);
  674. Assert.Contains (new Point (0, 29),axis.DrawAxisLinePoints);
  675. Assert.Contains (new Point (1, 29), axis.DrawAxisLinePoints);
  676. Assert.Contains (new Point (48, 29), axis.DrawAxisLinePoints);
  677. Assert.Contains (new Point (49, 29), axis.DrawAxisLinePoints);
  678. Assert.DoesNotContain (new Point (50, 29), axis.DrawAxisLinePoints);
  679. Assert.InRange(axis.LabelPoints.Max(),0,49);
  680. Assert.InRange(axis.LabelPoints.Min(),0,49);
  681. }
  682. [Fact]
  683. public void TestHAxisLocation_MarginBottom ()
  684. {
  685. var gv = GetGraph (out FakeHAxis axis);
  686. gv.MarginBottom = 10;
  687. gv.Redraw (gv.Bounds);
  688. Assert.DoesNotContain (new Point (-1, 19), axis.DrawAxisLinePoints);
  689. Assert.Contains (new Point (0, 19), axis.DrawAxisLinePoints);
  690. Assert.Contains (new Point (1, 19), axis.DrawAxisLinePoints);
  691. Assert.Contains (new Point (48, 19), axis.DrawAxisLinePoints);
  692. Assert.Contains (new Point (49, 19), axis.DrawAxisLinePoints);
  693. Assert.DoesNotContain (new Point (50, 19), axis.DrawAxisLinePoints);
  694. Assert.InRange(axis.LabelPoints.Max(),0,49);
  695. Assert.InRange(axis.LabelPoints.Min(),0,49);
  696. }
  697. [Fact]
  698. public void TestHAxisLocation_MarginLeft ()
  699. {
  700. var gv = GetGraph (out FakeHAxis axis);
  701. gv.MarginLeft = 5;
  702. gv.Redraw (gv.Bounds);
  703. Assert.DoesNotContain (new Point (4, 29), axis.DrawAxisLinePoints);
  704. Assert.Contains (new Point (5, 29), axis.DrawAxisLinePoints);
  705. Assert.Contains (new Point (6, 29), axis.DrawAxisLinePoints);
  706. Assert.Contains (new Point (48, 29), axis.DrawAxisLinePoints);
  707. Assert.Contains (new Point (49, 29), axis.DrawAxisLinePoints);
  708. Assert.DoesNotContain (new Point (50, 29), axis.DrawAxisLinePoints);
  709. // Axis lables should not be drawn in the margin
  710. Assert.InRange(axis.LabelPoints.Max(),5,49);
  711. Assert.InRange(axis.LabelPoints.Min(),5,49);
  712. }
  713. #endregion
  714. #region VerticalAxisTests
  715. /// <summary>
  716. /// Tests that the horizontal axis is computed correctly and does not over spill
  717. /// it's bounds
  718. /// </summary>
  719. [Fact]
  720. public void TestVAxisLocation_NoMargin ()
  721. {
  722. var gv = GetGraph (out FakeVAxis axis);
  723. gv.Redraw (gv.Bounds);
  724. Assert.DoesNotContain (new Point (0, -1), axis.DrawAxisLinePoints);
  725. Assert.Contains (new Point (0, 1),axis.DrawAxisLinePoints);
  726. Assert.Contains (new Point (0, 2), axis.DrawAxisLinePoints);
  727. Assert.Contains (new Point (0, 28), axis.DrawAxisLinePoints);
  728. Assert.Contains (new Point (0, 29), axis.DrawAxisLinePoints);
  729. Assert.DoesNotContain (new Point (0, 30), axis.DrawAxisLinePoints);
  730. Assert.InRange(axis.LabelPoints.Max(),0,29);
  731. Assert.InRange(axis.LabelPoints.Min(),0,29);
  732. }
  733. [Fact]
  734. public void TestVAxisLocation_MarginBottom ()
  735. {
  736. var gv = GetGraph (out FakeVAxis axis);
  737. gv.MarginBottom = 10;
  738. gv.Redraw (gv.Bounds);
  739. Assert.DoesNotContain (new Point (0, -1), axis.DrawAxisLinePoints);
  740. Assert.Contains (new Point (0, 1),axis.DrawAxisLinePoints);
  741. Assert.Contains (new Point (0, 2), axis.DrawAxisLinePoints);
  742. Assert.Contains (new Point (0, 18), axis.DrawAxisLinePoints);
  743. Assert.Contains (new Point (0, 19), axis.DrawAxisLinePoints);
  744. Assert.DoesNotContain (new Point (0, 20), axis.DrawAxisLinePoints);
  745. // Labels should not be drawn into the axis
  746. Assert.InRange(axis.LabelPoints.Max(),0,19);
  747. Assert.InRange(axis.LabelPoints.Min(),0,19);
  748. }
  749. [Fact]
  750. public void TestVAxisLocation_MarginLeft ()
  751. {
  752. var gv = GetGraph (out FakeVAxis axis);
  753. gv.MarginLeft = 5;
  754. gv.Redraw (gv.Bounds);
  755. Assert.DoesNotContain (new Point (5, -1), axis.DrawAxisLinePoints);
  756. Assert.Contains (new Point (5, 1),axis.DrawAxisLinePoints);
  757. Assert.Contains (new Point (5, 2), axis.DrawAxisLinePoints);
  758. Assert.Contains (new Point (5, 28), axis.DrawAxisLinePoints);
  759. Assert.Contains (new Point (5, 29), axis.DrawAxisLinePoints);
  760. Assert.DoesNotContain (new Point (5, 30), axis.DrawAxisLinePoints);
  761. Assert.InRange(axis.LabelPoints.Max(),0,29);
  762. Assert.InRange(axis.LabelPoints.Min(),0,29);
  763. }
  764. #endregion
  765. }
  766. public class TextAnnotationTests {
  767. readonly ITestOutputHelper output;
  768. public TextAnnotationTests(ITestOutputHelper output)
  769. {
  770. this.output = output;
  771. }
  772. [Fact]
  773. public void TestTextAnnotation_ScreenUnits()
  774. {
  775. var gv = GraphViewTests.GetGraph ();
  776. gv.Annotations.Add (new TextAnnotation () {
  777. Text = "hey!",
  778. ScreenPosition = new Point (3, 1)
  779. });
  780. gv.Redraw (gv.Bounds);
  781. var expected =
  782. @"
  783. ┤ hey!
  784. 0┼┬┬┬┬┬┬┬┬
  785. 0 5";
  786. GraphViewTests.AssertDriverContentsAre (expected, output);
  787. // user scrolls up one unit of graph space
  788. gv.ScrollOffset = new PointF (0, 1f);
  789. gv.Redraw (gv.Bounds);
  790. // we expect no change in the location of the annotation (only the axis label changes)
  791. // this is because screen units are constant and do not change as the viewport into
  792. // graph space scrolls to different areas of the graph
  793. expected =
  794. @"
  795. ┤ hey!
  796. 1┼┬┬┬┬┬┬┬┬
  797. 0 5";
  798. GraphViewTests.AssertDriverContentsAre (expected, output);
  799. }
  800. [Fact]
  801. public void TestTextAnnotation_GraphUnits ()
  802. {
  803. var gv = GraphViewTests.GetGraph ();
  804. gv.Annotations.Add (new TextAnnotation () {
  805. Text = "hey!",
  806. GraphPosition = new PointF (2, 2)
  807. });
  808. gv.Redraw (gv.Bounds);
  809. var expected =
  810. @"
  811. ┤ hey!
  812. 0┼┬┬┬┬┬┬┬┬
  813. 0 5";
  814. GraphViewTests.AssertDriverContentsAre (expected, output);
  815. // user scrolls up one unit of graph space
  816. gv.ScrollOffset = new PointF (0, 1f);
  817. gv.Redraw (gv.Bounds);
  818. // we expect the text annotation to go down one line since
  819. // the scroll offset means that that point of graph space is
  820. // lower down in the view. Note the 1 on the axis too, our viewport
  821. // (excluding margins) now shows y of 1 to 4 (previously 0 to 5)
  822. expected =
  823. @"
  824. ┤ hey!
  825. 1┼┬┬┬┬┬┬┬┬
  826. 0 5";
  827. GraphViewTests.AssertDriverContentsAre (expected, output);
  828. }
  829. [Fact]
  830. public void TestTextAnnotation_LongText ()
  831. {
  832. var gv = GraphViewTests.GetGraph ();
  833. gv.Annotations.Add (new TextAnnotation () {
  834. Text = "hey there partner hows it going boy its great",
  835. GraphPosition = new PointF (2, 2)
  836. });
  837. gv.Redraw (gv.Bounds);
  838. // long text should get truncated
  839. // margin takes up 1 units
  840. // the GraphPosition of the anntation is 2
  841. // Leaving 7 characters of the annotation renderable (including space)
  842. var expected =
  843. @"
  844. ┤ hey the
  845. 0┼┬┬┬┬┬┬┬┬
  846. 0 5";
  847. GraphViewTests.AssertDriverContentsAre (expected, output);
  848. }
  849. [Fact]
  850. public void TestTextAnnotation_Offscreen ()
  851. {
  852. var gv = GraphViewTests.GetGraph ();
  853. gv.Annotations.Add (new TextAnnotation () {
  854. Text = "hey there partner hows it going boy its great",
  855. GraphPosition = new PointF (9, 2)
  856. });
  857. gv.Redraw (gv.Bounds);
  858. // Text is off the screen (graph x axis runs to 8 not 9)
  859. var expected =
  860. @"
  861. 0┼┬┬┬┬┬┬┬┬
  862. 0 5";
  863. GraphViewTests.AssertDriverContentsAre (expected, output);
  864. }
  865. [Theory]
  866. [InlineData(null)]
  867. [InlineData (" ")]
  868. [InlineData ("\t\t")]
  869. public void TestTextAnnotation_EmptyText (string whitespace)
  870. {
  871. var gv = GraphViewTests.GetGraph ();
  872. gv.Annotations.Add (new TextAnnotation () {
  873. Text = whitespace,
  874. GraphPosition = new PointF (4, 2)
  875. });
  876. // add a point a bit further along the graph so if the whitespace were rendered
  877. // the test would pick it up (AssertDriverContentsAre ignores trailing whitespace on lines)
  878. var points = new ScatterSeries ();
  879. points.Points.Add(new PointF(7, 2));
  880. gv.Series.Add (points);
  881. gv.Redraw (gv.Bounds);
  882. var expected =
  883. @"
  884. ┤ x
  885. 0┼┬┬┬┬┬┬┬┬
  886. 0 5";
  887. GraphViewTests.AssertDriverContentsAre (expected, output);
  888. }
  889. }
  890. public class LegendTests {
  891. readonly ITestOutputHelper output;
  892. public LegendTests(ITestOutputHelper output)
  893. {
  894. this.output = output;
  895. }
  896. [Fact]
  897. public void LegendNormalUsage_WithBorder ()
  898. {
  899. var gv = GraphViewTests.GetGraph ();
  900. var legend = new LegendAnnotation(new Rect(2,0,5,3));
  901. legend.AddEntry (new GraphCellToRender ('A'), "Ant");
  902. legend.AddEntry (new GraphCellToRender ('B'), "Bat");
  903. gv.Annotations.Add (legend);
  904. gv.Redraw (gv.Bounds);
  905. var expected =
  906. @"
  907. │┌───┐
  908. ┤│AAn│
  909. ┤└───┘
  910. 0┼┬┬┬┬┬┬┬┬
  911. 0 5";
  912. GraphViewTests.AssertDriverContentsAre (expected, output);
  913. }
  914. [Fact]
  915. public void LegendNormalUsage_WithoutBorder ()
  916. {
  917. var gv = GraphViewTests.GetGraph ();
  918. var legend = new LegendAnnotation (new Rect (2, 0, 5, 3));
  919. legend.AddEntry (new GraphCellToRender ('A'), "Ant");
  920. legend.AddEntry (new GraphCellToRender ('B'), "?"); // this will exercise pad
  921. legend.AddEntry (new GraphCellToRender ('C'), "Cat");
  922. legend.AddEntry (new GraphCellToRender ('H'), "Hattter"); // not enough space for this oen
  923. legend.Border = false;
  924. gv.Annotations.Add (legend);
  925. gv.Redraw (gv.Bounds);
  926. var expected =
  927. @"
  928. │AAnt
  929. ┤B?
  930. ┤CCat
  931. 0┼┬┬┬┬┬┬┬┬
  932. 0 5";
  933. GraphViewTests.AssertDriverContentsAre (expected, output);
  934. }
  935. }
  936. public class PathAnnotationTests {
  937. readonly ITestOutputHelper output;
  938. public PathAnnotationTests( ITestOutputHelper output)
  939. {
  940. this.output = output;
  941. }
  942. [Fact]
  943. public void PathAnnotation_Box()
  944. {
  945. var gv = GraphViewTests.GetGraph ();
  946. var path = new PathAnnotation ();
  947. path.Points.Add (new PointF (1, 1));
  948. path.Points.Add (new PointF (1, 3));
  949. path.Points.Add (new PointF (6, 3));
  950. path.Points.Add (new PointF (6, 1));
  951. // list the starting point again so that it draws a complete square
  952. // (otherwise it will miss out the last line along the bottom)
  953. path.Points.Add (new PointF (1, 1));
  954. gv.Annotations.Add (path);
  955. gv.Redraw (gv.Bounds);
  956. var expected =
  957. @"
  958. │......
  959. ┤. .
  960. ┤......
  961. 0┼┬┬┬┬┬┬┬┬
  962. 0 5";
  963. GraphViewTests.AssertDriverContentsAre (expected, output);
  964. }
  965. [Fact]
  966. public void PathAnnotation_Diamond ()
  967. {
  968. var gv = GraphViewTests.GetGraph ();
  969. var path = new PathAnnotation ();
  970. path.Points.Add (new PointF (1, 2));
  971. path.Points.Add (new PointF (3, 3));
  972. path.Points.Add (new PointF (6, 2));
  973. path.Points.Add (new PointF (3, 1));
  974. // list the starting point again to close the shape
  975. path.Points.Add (new PointF (1, 2));
  976. gv.Annotations.Add (path);
  977. gv.Redraw (gv.Bounds);
  978. var expected =
  979. @"
  980. │ ..
  981. ┤.. ..
  982. ┤ ...
  983. 0┼┬┬┬┬┬┬┬┬
  984. 0 5";
  985. GraphViewTests.AssertDriverContentsAre (expected,output);
  986. }
  987. }
  988. public class AxisIncrementToRenderTests {
  989. [Fact]
  990. public void AxisIncrementToRenderTests_Constructor ()
  991. {
  992. var render = new AxisIncrementToRender (Orientation.Horizontal,1,6.6f);
  993. Assert.Equal (Orientation.Horizontal, render.Orientation);
  994. Assert.Equal (1, render.ScreenLocation);
  995. Assert.Equal (6.6f, render.Value);
  996. }
  997. }
  998. }