GraphViewTests.cs 52 KB

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