GraphViewTests.cs 52 KB

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