2
0

GraphViewTests.cs 43 KB

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