GraphViewTests.cs 52 KB

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