GraphViewTests.cs 52 KB

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