GraphViewTests.cs 53 KB

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