GraphViewTests.cs 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649
  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.Viewport = new Rectangle (0, 0, 50, 30);
  361. gv.ColorScheme = new ColorScheme ();
  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.SetNeedsDraw ();
  384. gv.Draw ();
  385. Assert.Equal (new RectangleF (0, 0, 45, 28), fullGraphBounds);
  386. // The screen space the graph will be rendered into should
  387. // not overspill the margins
  388. Assert.Equal (new Rectangle (5, 0, 45, 28), graphScreenBounds);
  389. // Shutdown must be called to safely clean up Application if Init has been called
  390. Application.Shutdown ();
  391. }
  392. /// <summary>
  393. /// Tests that the bounds passed to the ISeries for drawing into are correct even when the
  394. /// <see cref="GraphView.CellSize"/> results in multiple units of graph space being condensed into each cell of console
  395. /// </summary>
  396. [Fact]
  397. public void Series_GetsPassedCorrectViewport_AllAtOnce_LargeCellSize ()
  398. {
  399. GraphViewTests.InitFakeDriver ();
  400. var gv = new GraphView ();
  401. gv.BeginInit ();
  402. gv.EndInit ();
  403. gv.ColorScheme = new ColorScheme ();
  404. gv.Viewport = new Rectangle (0, 0, 50, 30);
  405. // the larger the cell size the more condensed (smaller) the graph space is
  406. gv.CellSize = new PointF (2, 5);
  407. var fullGraphBounds = RectangleF.Empty;
  408. var graphScreenBounds = Rectangle.Empty;
  409. var series = new FakeSeries (
  410. (v, s, g) =>
  411. {
  412. graphScreenBounds = s;
  413. fullGraphBounds = g;
  414. }
  415. );
  416. gv.Series.Add (series);
  417. gv.LayoutSubviews ();
  418. gv.Draw ();
  419. // Since each cell of the console is 2x5 of graph space the graph
  420. // bounds to be rendered are larger
  421. Assert.Equal (new RectangleF (0, 0, 100, 150), fullGraphBounds);
  422. Assert.Equal (new Rectangle (0, 0, 50, 30), graphScreenBounds);
  423. // Graph should not spill into the margins
  424. gv.MarginBottom = 2;
  425. gv.MarginLeft = 5;
  426. // Even with a margin the graph should be drawn from
  427. // the origin, we just get less visible width/height
  428. gv.LayoutSubviews ();
  429. gv.SetNeedsDraw ();
  430. gv.Draw ();
  431. Assert.Equal (new RectangleF (0, 0, 90, 140), fullGraphBounds);
  432. // The screen space the graph will be rendered into should
  433. // not overspill the margins
  434. Assert.Equal (new Rectangle (5, 0, 45, 28), graphScreenBounds);
  435. // Shutdown must be called to safely clean up Application if Init has been called
  436. Application.Shutdown ();
  437. }
  438. private class FakeSeries : ISeries
  439. {
  440. private readonly Action<GraphView, Rectangle, RectangleF> _drawSeries;
  441. public FakeSeries (
  442. Action<GraphView, Rectangle, RectangleF> drawSeries
  443. )
  444. {
  445. _drawSeries = drawSeries;
  446. }
  447. public void DrawSeries (GraphView graph, Rectangle bounds, RectangleF graphBounds) { _drawSeries (graph, bounds, graphBounds); }
  448. }
  449. }
  450. public class MultiBarSeriesTests
  451. {
  452. private readonly ITestOutputHelper _output;
  453. public MultiBarSeriesTests (ITestOutputHelper output) { _output = output; }
  454. [Fact]
  455. public void MultiBarSeries_BarSpacing ()
  456. {
  457. // Creates clusters of 5 adjacent bars with 2 spaces between clusters
  458. var series = new MultiBarSeries (5, 7, 1);
  459. Assert.Equal (5, series.SubSeries.Count);
  460. Assert.Equal (0, series.SubSeries.ElementAt (0).Offset);
  461. Assert.Equal (1, series.SubSeries.ElementAt (1).Offset);
  462. Assert.Equal (2, series.SubSeries.ElementAt (2).Offset);
  463. Assert.Equal (3, series.SubSeries.ElementAt (3).Offset);
  464. Assert.Equal (4, series.SubSeries.ElementAt (4).Offset);
  465. }
  466. [Fact]
  467. public void MultiBarSeriesAddValues_WrongNumber ()
  468. {
  469. // user asks for 3 bars per category
  470. var series = new MultiBarSeries (3, 7, 1);
  471. var ex = Assert.Throws<ArgumentException> (() => series.AddBars ("Cars", (Rune)'#', 1));
  472. Assert.Equal (
  473. "Number of values must match the number of bars per category (Parameter 'values')",
  474. ex.Message
  475. );
  476. }
  477. [Fact]
  478. public void MultiBarSeriesColors_RightNumber ()
  479. {
  480. Attribute [] colors =
  481. {
  482. new (Color.Green, Color.Black), new (Color.Green, Color.White), new (Color.BrightYellow, Color.White)
  483. };
  484. // user passes 3 colors and asks for 3 bars
  485. var series = new MultiBarSeries (3, 7, 1, colors);
  486. Assert.Equal (series.SubSeries.ElementAt (0).OverrideBarColor, colors [0]);
  487. Assert.Equal (series.SubSeries.ElementAt (1).OverrideBarColor, colors [1]);
  488. Assert.Equal (series.SubSeries.ElementAt (2).OverrideBarColor, colors [2]);
  489. // Shutdown must be called to safely clean up Application if Init has been called
  490. Application.Shutdown ();
  491. }
  492. [Fact]
  493. public void MultiBarSeriesColors_WrongNumber ()
  494. {
  495. Attribute [] colors = { new (Color.Green, Color.Black) };
  496. // user passes 1 color only but asks for 5 bars
  497. var ex = Assert.Throws<ArgumentException> (() => new MultiBarSeries (5, 7, 1, colors));
  498. Assert.Equal (
  499. "Number of colors must match the number of bars (Parameter 'numberOfBarsPerCategory')",
  500. ex.Message
  501. );
  502. // Shutdown must be called to safely clean up Application if Init has been called
  503. Application.Shutdown ();
  504. }
  505. [Fact]
  506. public void TestRendering_MultibarSeries ()
  507. {
  508. GraphViewTests.InitFakeDriver ();
  509. var gv = new GraphView ();
  510. gv.ColorScheme = new ColorScheme ();
  511. // y axis goes from 0.1 to 1 across 10 console rows
  512. // x axis goes from 0 to 20 across 20 console columns
  513. gv.Viewport = new Rectangle (0, 0, 20, 10);
  514. gv.CellSize = new PointF (1f, 0.1f);
  515. gv.MarginBottom = 1;
  516. gv.MarginLeft = 1;
  517. var multibarSeries = new MultiBarSeries (2, 4, 1);
  518. //nudge them left to avoid float rounding errors at the boundaries of cells
  519. foreach (BarSeries sub in multibarSeries.SubSeries)
  520. {
  521. sub.Offset -= 0.001f;
  522. }
  523. gv.Series.Add (multibarSeries);
  524. FakeHAxis fakeXAxis;
  525. // don't show axis labels that means any labels
  526. // that appear are explicitly from the bars
  527. gv.AxisX = fakeXAxis = new FakeHAxis { Increment = 0 };
  528. gv.AxisY = new FakeVAxis { Increment = 0 };
  529. gv.LayoutSubviews ();
  530. gv.Draw ();
  531. // Since bar series has no bars yet no labels should be displayed
  532. Assert.Empty (fakeXAxis.LabelPoints);
  533. multibarSeries.AddBars ("hey", (Rune)'M', 0.5001f, 0.5001f);
  534. fakeXAxis.LabelPoints.Clear ();
  535. gv.LayoutSubviews ();
  536. gv.SetNeedsDraw ();
  537. gv.Draw ();
  538. Assert.Equal (4, fakeXAxis.LabelPoints.Single ());
  539. multibarSeries.AddBars ("there", (Rune)'M', 0.24999f, 0.74999f);
  540. multibarSeries.AddBars ("bob", (Rune)'M', 1, 2);
  541. fakeXAxis.LabelPoints.Clear ();
  542. gv.LayoutSubviews ();
  543. gv.SetNeedsDraw ();
  544. gv.Draw ();
  545. Assert.Equal (3, fakeXAxis.LabelPoints.Count);
  546. Assert.Equal (4, fakeXAxis.LabelPoints [0]);
  547. Assert.Equal (8, fakeXAxis.LabelPoints [1]);
  548. Assert.Equal (12, fakeXAxis.LabelPoints [2]);
  549. var looksLike =
  550. @"
  551. │ MM
  552. │ M MM
  553. │ M MM
  554. │ MM M MM
  555. │ MM M MM
  556. │ MM M MM
  557. │ MM MM MM
  558. │ MM MM MM
  559. ┼──┬M──┬M──┬M──────
  560. heytherebob ";
  561. TestHelpers.AssertDriverContentsAre (looksLike, _output);
  562. // Shutdown must be called to safely clean up Application if Init has been called
  563. Application.Shutdown ();
  564. }
  565. }
  566. public class BarSeriesTests
  567. {
  568. [Fact]
  569. public void TestOneLongOneShortHorizontalBars_WithOffset ()
  570. {
  571. GraphView graph = GetGraph (out FakeBarSeries barSeries, out FakeHAxis axisX, out FakeVAxis axisY);
  572. graph.Draw ();
  573. // no bars
  574. Assert.Empty (barSeries.BarScreenStarts);
  575. Assert.Empty (axisX.LabelPoints);
  576. Assert.Empty (axisY.LabelPoints);
  577. // 0.1 units of graph y fit every screen row
  578. // so 1 unit of graph y space is 10 screen rows
  579. graph.CellSize = new PointF (0.5f, 0.1f);
  580. // Start bar 3 screen units up (y = height-3)
  581. barSeries.Offset = 0.25f;
  582. // 1 bar every 3 rows of screen
  583. barSeries.BarEvery = 0.3f;
  584. barSeries.Orientation = Orientation.Horizontal;
  585. // 1 bar that is very wide (100 graph units horizontally = screen pos 50 but bounded by screen)
  586. barSeries.Bars.Add (
  587. new BarSeriesBar ("hi1", new GraphCellToRender ((Rune)'.'), 100)
  588. );
  589. // 1 bar that is shorter
  590. barSeries.Bars.Add (
  591. new BarSeriesBar ("hi2", new GraphCellToRender ((Rune)'.'), 5)
  592. );
  593. // redraw graph
  594. graph.SetNeedsDraw ();
  595. graph.Draw ();
  596. // since bars are horizontal all have the same X start cordinates
  597. Assert.Equal (0, barSeries.BarScreenStarts [0].X);
  598. Assert.Equal (0, barSeries.BarScreenStarts [1].X);
  599. // bar goes all the way to the end so bumps up against right screen boundary
  600. // width of graph is 20
  601. Assert.Equal (19, barSeries.BarScreenEnds [0].X);
  602. // shorter bar is 5 graph units wide which is 10 screen units
  603. Assert.Equal (10, barSeries.BarScreenEnds [1].X);
  604. // first bar should be offset 6 screen units (0.25f + 0.3f graph units)
  605. // since height of control is 10 then first bar should be at screen row 4 (10-6)
  606. Assert.Equal (4, barSeries.BarScreenStarts [0].Y);
  607. // second bar should be offset 9 screen units (0.25f + 0.6f graph units)
  608. // since height of control is 10 then second bar should be at screen row 1 (10-9)
  609. Assert.Equal (1, barSeries.BarScreenStarts [1].Y);
  610. // both bars should have labels but on the y axis
  611. Assert.Equal (2, axisY.LabelPoints.Count);
  612. Assert.Empty (axisX.LabelPoints);
  613. // labels should align with the bars (same screen y axis point)
  614. Assert.Contains (4, axisY.LabelPoints);
  615. Assert.Contains (1, axisY.LabelPoints);
  616. // Shutdown must be called to safely clean up Application if Init has been called
  617. Application.Shutdown ();
  618. }
  619. [Fact]
  620. public void TestTwoTallBars_WithOffset ()
  621. {
  622. GraphView graph = GetGraph (out FakeBarSeries barSeries, out FakeHAxis axisX, out FakeVAxis axisY);
  623. graph.Draw ();
  624. // no bars
  625. Assert.Empty (barSeries.BarScreenStarts);
  626. Assert.Empty (axisX.LabelPoints);
  627. Assert.Empty (axisY.LabelPoints);
  628. // 0.5 units of graph fit every screen cell
  629. // so 1 unit of graph space is 2 screen columns
  630. graph.CellSize = new PointF (0.5f, 0.1f);
  631. // Start bar 1 screen unit along
  632. barSeries.Offset = 0.5f;
  633. barSeries.BarEvery = 1f;
  634. barSeries.Bars.Add (
  635. new BarSeriesBar ("hi1", new GraphCellToRender ((Rune)'.'), 100)
  636. );
  637. barSeries.Bars.Add (
  638. new BarSeriesBar ("hi2", new GraphCellToRender ((Rune)'.'), 100)
  639. );
  640. barSeries.Orientation = Orientation.Vertical;
  641. // redraw graph
  642. graph.SetNeedsDraw ();
  643. graph.Draw ();
  644. // bar should be drawn at BarEvery 1f + offset 0.5f = 3 screen units
  645. Assert.Equal (3, barSeries.BarScreenStarts [0].X);
  646. Assert.Equal (3, barSeries.BarScreenEnds [0].X);
  647. // second bar should be BarEveryx2 = 2f + offset 0.5f = 5 screen units
  648. Assert.Equal (5, barSeries.BarScreenStarts [1].X);
  649. Assert.Equal (5, barSeries.BarScreenEnds [1].X);
  650. // both bars should have labels
  651. Assert.Equal (2, axisX.LabelPoints.Count);
  652. Assert.Contains (3, axisX.LabelPoints);
  653. Assert.Contains (5, axisX.LabelPoints);
  654. // bars are very tall but should not draw up off top of screen
  655. Assert.Equal (9, barSeries.BarScreenStarts [0].Y);
  656. Assert.Equal (0, barSeries.BarScreenEnds [0].Y);
  657. Assert.Equal (9, barSeries.BarScreenStarts [1].Y);
  658. Assert.Equal (0, barSeries.BarScreenEnds [1].Y);
  659. // Shutdown must be called to safely clean up Application if Init has been called
  660. Application.Shutdown ();
  661. }
  662. [Fact]
  663. public void TestZeroHeightBar_WithName ()
  664. {
  665. GraphView graph = GetGraph (out FakeBarSeries barSeries, out FakeHAxis axisX, out FakeVAxis axisY);
  666. graph.Draw ();
  667. // no bars
  668. Assert.Empty (barSeries.BarScreenStarts);
  669. Assert.Empty (axisX.LabelPoints);
  670. Assert.Empty (axisY.LabelPoints);
  671. // bar of height 0
  672. barSeries.Bars.Add (new BarSeriesBar ("hi", new GraphCellToRender ((Rune)'.'), 0));
  673. barSeries.Orientation = Orientation.Vertical;
  674. // redraw graph
  675. graph.SetNeedsDraw ();
  676. graph.Draw ();
  677. // bar should not be drawn
  678. Assert.Empty (barSeries.BarScreenStarts);
  679. Assert.NotEmpty (axisX.LabelPoints);
  680. Assert.Empty (axisY.LabelPoints);
  681. // but bar name should be
  682. // Screen position x=2 because bars are drawn every 1f of
  683. // graph space and CellSize.X is 0.5f
  684. Assert.Contains (2, axisX.LabelPoints);
  685. // Shutdown must be called to safely clean up Application if Init has been called
  686. Application.Shutdown ();
  687. }
  688. private GraphView GetGraph (out FakeBarSeries series, out FakeHAxis axisX, out FakeVAxis axisY)
  689. {
  690. GraphViewTests.InitFakeDriver ();
  691. var gv = new GraphView ();
  692. gv.BeginInit ();
  693. gv.EndInit ();
  694. // y axis goes from 0.1 to 1 across 10 console rows
  695. // x axis goes from 0 to 10 across 20 console columns
  696. gv.Viewport = new Rectangle (0, 0, 20, 10);
  697. gv.ColorScheme = new ColorScheme ();
  698. gv.CellSize = new PointF (0.5f, 0.1f);
  699. gv.Series.Add (series = new FakeBarSeries ());
  700. // don't show axis labels that means any labels
  701. // that appaer are explicitly from the bars
  702. gv.AxisX = axisX = new FakeHAxis { Increment = 0 };
  703. gv.AxisY = axisY = new FakeVAxis { Increment = 0 };
  704. return gv;
  705. }
  706. private class FakeBarSeries : BarSeries
  707. {
  708. public List<Point> BarScreenEnds { get; } = new ();
  709. public List<Point> BarScreenStarts { get; } = new ();
  710. public GraphCellToRender FinalColor { get; private set; }
  711. protected override GraphCellToRender AdjustColor (GraphCellToRender graphCellToRender) { return FinalColor = base.AdjustColor (graphCellToRender); }
  712. protected override void DrawBarLine (GraphView graph, Point start, Point end, BarSeriesBar beingDrawn)
  713. {
  714. base.DrawBarLine (graph, start, end, beingDrawn);
  715. BarScreenStarts.Add (start);
  716. BarScreenEnds.Add (end);
  717. }
  718. }
  719. }
  720. public class AxisTests
  721. {
  722. private GraphView GetGraph (out FakeHAxis axis) { return GetGraph (out axis, out _); }
  723. private GraphView GetGraph (out FakeVAxis axis) { return GetGraph (out _, out axis); }
  724. private GraphView GetGraph (out FakeHAxis axisX, out FakeVAxis axisY)
  725. {
  726. GraphViewTests.InitFakeDriver ();
  727. var gv = new GraphView ();
  728. gv.Viewport = new Rectangle (0, 0, 50, 30);
  729. gv.ColorScheme = new ColorScheme ();
  730. // graph can't be completely empty or it won't draw
  731. gv.Series.Add (new ScatterSeries ());
  732. axisX = new FakeHAxis ();
  733. axisY = new FakeVAxis ();
  734. gv.AxisX = axisX;
  735. gv.AxisY = axisY;
  736. return gv;
  737. }
  738. #region HorizontalAxis Tests
  739. /// <summary>Tests that the horizontal axis is computed correctly and does not over spill it's bounds</summary>
  740. [Fact]
  741. public void TestHAxisLocation_NoMargin ()
  742. {
  743. GraphView gv = GetGraph (out FakeHAxis axis);
  744. gv.LayoutSubviews ();
  745. gv.Draw ();
  746. Assert.DoesNotContain (new Point (-1, 29), axis.DrawAxisLinePoints);
  747. Assert.Contains (new Point (0, 29), axis.DrawAxisLinePoints);
  748. Assert.Contains (new Point (1, 29), axis.DrawAxisLinePoints);
  749. Assert.Contains (new Point (48, 29), axis.DrawAxisLinePoints);
  750. Assert.Contains (new Point (49, 29), axis.DrawAxisLinePoints);
  751. Assert.DoesNotContain (new Point (50, 29), axis.DrawAxisLinePoints);
  752. Assert.InRange (axis.LabelPoints.Max (), 0, 49);
  753. Assert.InRange (axis.LabelPoints.Min (), 0, 49);
  754. // Shutdown must be called to safely clean up Application if Init has been called
  755. Application.Shutdown ();
  756. }
  757. [Fact]
  758. public void TestHAxisLocation_MarginBottom ()
  759. {
  760. GraphView gv = GetGraph (out FakeHAxis axis);
  761. gv.MarginBottom = 10;
  762. gv.LayoutSubviews ();
  763. gv.Draw ();
  764. Assert.DoesNotContain (new Point (-1, 19), axis.DrawAxisLinePoints);
  765. Assert.Contains (new Point (0, 19), axis.DrawAxisLinePoints);
  766. Assert.Contains (new Point (1, 19), axis.DrawAxisLinePoints);
  767. Assert.Contains (new Point (48, 19), axis.DrawAxisLinePoints);
  768. Assert.Contains (new Point (49, 19), axis.DrawAxisLinePoints);
  769. Assert.DoesNotContain (new Point (50, 19), axis.DrawAxisLinePoints);
  770. Assert.InRange (axis.LabelPoints.Max (), 0, 49);
  771. Assert.InRange (axis.LabelPoints.Min (), 0, 49);
  772. // Shutdown must be called to safely clean up Application if Init has been called
  773. Application.Shutdown ();
  774. }
  775. [Fact]
  776. public void TestHAxisLocation_MarginLeft ()
  777. {
  778. GraphView gv = GetGraph (out FakeHAxis axis);
  779. gv.MarginLeft = 5;
  780. gv.LayoutSubviews ();
  781. gv.Draw ();
  782. Assert.DoesNotContain (new Point (4, 29), axis.DrawAxisLinePoints);
  783. Assert.Contains (new Point (5, 29), axis.DrawAxisLinePoints);
  784. Assert.Contains (new Point (6, 29), axis.DrawAxisLinePoints);
  785. Assert.Contains (new Point (48, 29), axis.DrawAxisLinePoints);
  786. Assert.Contains (new Point (49, 29), axis.DrawAxisLinePoints);
  787. Assert.DoesNotContain (new Point (50, 29), axis.DrawAxisLinePoints);
  788. // Axis lables should not be drawn in the margin
  789. Assert.InRange (axis.LabelPoints.Max (), 5, 49);
  790. Assert.InRange (axis.LabelPoints.Min (), 5, 49);
  791. // Shutdown must be called to safely clean up Application if Init has been called
  792. Application.Shutdown ();
  793. }
  794. #endregion
  795. #region VerticalAxisTests
  796. /// <summary>Tests that the horizontal axis is computed correctly and does not over spill it's bounds</summary>
  797. [Fact]
  798. public void TestVAxisLocation_NoMargin ()
  799. {
  800. GraphView gv = GetGraph (out FakeVAxis axis);
  801. gv.LayoutSubviews ();
  802. gv.Draw ();
  803. Assert.DoesNotContain (new Point (0, -1), axis.DrawAxisLinePoints);
  804. Assert.Contains (new Point (0, 1), axis.DrawAxisLinePoints);
  805. Assert.Contains (new Point (0, 2), axis.DrawAxisLinePoints);
  806. Assert.Contains (new Point (0, 28), axis.DrawAxisLinePoints);
  807. Assert.Contains (new Point (0, 29), axis.DrawAxisLinePoints);
  808. Assert.DoesNotContain (new Point (0, 30), axis.DrawAxisLinePoints);
  809. Assert.InRange (axis.LabelPoints.Max (), 0, 29);
  810. Assert.InRange (axis.LabelPoints.Min (), 0, 29);
  811. // Shutdown must be called to safely clean up Application if Init has been called
  812. Application.Shutdown ();
  813. }
  814. [Fact]
  815. public void TestVAxisLocation_MarginBottom ()
  816. {
  817. GraphView gv = GetGraph (out FakeVAxis axis);
  818. gv.MarginBottom = 10;
  819. gv.LayoutSubviews ();
  820. gv.Draw ();
  821. Assert.DoesNotContain (new Point (0, -1), axis.DrawAxisLinePoints);
  822. Assert.Contains (new Point (0, 1), axis.DrawAxisLinePoints);
  823. Assert.Contains (new Point (0, 2), axis.DrawAxisLinePoints);
  824. Assert.Contains (new Point (0, 18), axis.DrawAxisLinePoints);
  825. Assert.Contains (new Point (0, 19), axis.DrawAxisLinePoints);
  826. Assert.DoesNotContain (new Point (0, 20), axis.DrawAxisLinePoints);
  827. // Labels should not be drawn into the axis
  828. Assert.InRange (axis.LabelPoints.Max (), 0, 19);
  829. Assert.InRange (axis.LabelPoints.Min (), 0, 19);
  830. // Shutdown must be called to safely clean up Application if Init has been called
  831. Application.Shutdown ();
  832. }
  833. [Fact]
  834. public void TestVAxisLocation_MarginLeft ()
  835. {
  836. GraphView gv = GetGraph (out FakeVAxis axis);
  837. gv.MarginLeft = 5;
  838. gv.LayoutSubviews ();
  839. gv.Draw ();
  840. Assert.DoesNotContain (new Point (5, -1), axis.DrawAxisLinePoints);
  841. Assert.Contains (new Point (5, 1), axis.DrawAxisLinePoints);
  842. Assert.Contains (new Point (5, 2), axis.DrawAxisLinePoints);
  843. Assert.Contains (new Point (5, 28), axis.DrawAxisLinePoints);
  844. Assert.Contains (new Point (5, 29), axis.DrawAxisLinePoints);
  845. Assert.DoesNotContain (new Point (5, 30), axis.DrawAxisLinePoints);
  846. Assert.InRange (axis.LabelPoints.Max (), 0, 29);
  847. Assert.InRange (axis.LabelPoints.Min (), 0, 29);
  848. // Shutdown must be called to safely clean up Application if Init has been called
  849. Application.Shutdown ();
  850. }
  851. #endregion
  852. }
  853. public class TextAnnotationTests
  854. {
  855. private readonly ITestOutputHelper _output;
  856. public TextAnnotationTests (ITestOutputHelper output) { _output = output; }
  857. [Theory]
  858. [InlineData (null)]
  859. [InlineData (" ")]
  860. [InlineData ("\t\t")]
  861. public void TestTextAnnotation_EmptyText (string whitespace)
  862. {
  863. GraphView gv = GraphViewTests.GetGraph ();
  864. gv.Annotations.Add (
  865. new TextAnnotation { Text = whitespace, GraphPosition = new PointF (4, 2) }
  866. );
  867. // add a point a bit further along the graph so if the whitespace were rendered
  868. // the test would pick it up (AssertDriverContentsAre ignores trailing whitespace on lines)
  869. var points = new ScatterSeries ();
  870. points.Points.Add (new PointF (7, 2));
  871. gv.Series.Add (points);
  872. gv.LayoutSubviews ();
  873. gv.Draw ();
  874. var expected =
  875. @$"
  876. ┤ {CM.Glyphs.Dot}
  877. 0┼┬┬┬┬┬┬┬┬
  878. 0 5";
  879. TestHelpers.AssertDriverContentsAre (expected, _output);
  880. // Shutdown must be called to safely clean up Application if Init has been called
  881. Application.Shutdown ();
  882. }
  883. [Fact]
  884. public void TestTextAnnotation_GraphUnits ()
  885. {
  886. GraphView gv = GraphViewTests.GetGraph ();
  887. gv.Annotations.Add (
  888. new TextAnnotation { Text = "hey!", GraphPosition = new PointF (2, 2) }
  889. );
  890. gv.LayoutSubviews ();
  891. gv.Draw ();
  892. var expected =
  893. @"
  894. ┤ hey!
  895. 0┼┬┬┬┬┬┬┬┬
  896. 0 5";
  897. TestHelpers.AssertDriverContentsAre (expected, _output);
  898. // user scrolls up one unit of graph space
  899. gv.ScrollOffset = new PointF (0, 1f);
  900. gv.SetNeedsDraw ();
  901. gv.Draw ();
  902. // we expect the text annotation to go down one line since
  903. // the scroll offset means that that point of graph space is
  904. // lower down in the view. Note the 1 on the axis too, our viewport
  905. // (excluding margins) now shows y of 1 to 4 (previously 0 to 5)
  906. expected =
  907. @"
  908. ┤ hey!
  909. 1┼┬┬┬┬┬┬┬┬
  910. 0 5";
  911. TestHelpers.AssertDriverContentsAre (expected, _output);
  912. // Shutdown must be called to safely clean up Application if Init has been called
  913. Application.Shutdown ();
  914. }
  915. [Fact]
  916. public void TestTextAnnotation_LongText ()
  917. {
  918. GraphView gv = GraphViewTests.GetGraph ();
  919. gv.Annotations.Add (
  920. new TextAnnotation
  921. {
  922. Text = "hey there partner hows it going boy its great", GraphPosition = new PointF (2, 2)
  923. }
  924. );
  925. gv.LayoutSubviews ();
  926. gv.SetNeedsDraw ();
  927. gv.Draw ();
  928. // long text should get truncated
  929. // margin takes up 1 units
  930. // the GraphPosition of the anntation is 2
  931. // Leaving 7 characters of the annotation renderable (including space)
  932. var expected =
  933. @"
  934. ┤ hey the
  935. 0┼┬┬┬┬┬┬┬┬
  936. 0 5";
  937. TestHelpers.AssertDriverContentsAre (expected, _output);
  938. // Shutdown must be called to safely clean up Application if Init has been called
  939. Application.Shutdown ();
  940. }
  941. [Fact]
  942. public void TestTextAnnotation_Offscreen ()
  943. {
  944. GraphView gv = GraphViewTests.GetGraph ();
  945. gv.Annotations.Add (
  946. new TextAnnotation
  947. {
  948. Text = "hey there partner hows it going boy its great", GraphPosition = new PointF (9, 2)
  949. }
  950. );
  951. gv.LayoutSubviews ();
  952. gv.Draw ();
  953. // Text is off the screen (graph x axis runs to 8 not 9)
  954. var expected =
  955. @"
  956. 0┼┬┬┬┬┬┬┬┬
  957. 0 5";
  958. TestHelpers.AssertDriverContentsAre (expected, _output);
  959. // Shutdown must be called to safely clean up Application if Init has been called
  960. Application.Shutdown ();
  961. }
  962. [Fact]
  963. public void TestTextAnnotation_ScreenUnits ()
  964. {
  965. GraphView gv = GraphViewTests.GetGraph ();
  966. gv.Annotations.Add (
  967. new TextAnnotation { Text = "hey!", ScreenPosition = new Point (3, 1) }
  968. );
  969. gv.LayoutSubviews ();
  970. gv.Draw ();
  971. var expected =
  972. @"
  973. ┤ hey!
  974. 0┼┬┬┬┬┬┬┬┬
  975. 0 5";
  976. TestHelpers.AssertDriverContentsAre (expected, _output);
  977. // user scrolls up one unit of graph space
  978. gv.ScrollOffset = new PointF (0, 1f);
  979. gv.SetNeedsDraw ();
  980. gv.Draw ();
  981. // we expect no change in the location of the annotation (only the axis label changes)
  982. // this is because screen units are constant and do not change as the viewport into
  983. // graph space scrolls to different areas of the graph
  984. expected =
  985. @"
  986. ┤ hey!
  987. 1┼┬┬┬┬┬┬┬┬
  988. 0 5";
  989. TestHelpers.AssertDriverContentsAre (expected, _output);
  990. // user scrolls up one unit of graph space
  991. gv.ScrollOffset = new PointF (0, 1f);
  992. gv.SetNeedsDraw ();
  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. TestHelpers.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. public void LegendNormalUsage_WithBorder ()
  1031. {
  1032. GraphView gv = GraphViewTests.GetGraph ();
  1033. var legend = new LegendAnnotation (new Rectangle (2, 0, 5, 3));
  1034. legend.AddEntry (new GraphCellToRender ((Rune)'A'), "Ant");
  1035. legend.AddEntry (new GraphCellToRender ((Rune)'B'), "Bat");
  1036. gv.Annotations.Add (legend);
  1037. gv.Draw ();
  1038. var expected =
  1039. @"
  1040. │┌───┐
  1041. ┤│AAn│
  1042. ┤└───┘
  1043. 0┼┬┬┬┬┬┬┬┬
  1044. 0 5";
  1045. TestHelpers.AssertDriverContentsAre (expected, _output);
  1046. // Shutdown must be called to safely clean up Application if Init has been called
  1047. Application.Shutdown ();
  1048. }
  1049. [Fact]
  1050. public void LegendNormalUsage_WithoutBorder ()
  1051. {
  1052. GraphView gv = GraphViewTests.GetGraph ();
  1053. var legend = new LegendAnnotation (new Rectangle (2, 0, 5, 3));
  1054. legend.AddEntry (new GraphCellToRender ((Rune)'A'), "Ant");
  1055. legend.AddEntry (new GraphCellToRender ((Rune)'B'), "?"); // this will exercise pad
  1056. legend.AddEntry (new GraphCellToRender ((Rune)'C'), "Cat");
  1057. legend.AddEntry (new GraphCellToRender ((Rune)'H'), "Hattter"); // not enough space for this oen
  1058. legend.BorderStyle = LineStyle.None;
  1059. gv.Annotations.Add (legend);
  1060. gv.Draw ();
  1061. var expected =
  1062. @"
  1063. │AAnt
  1064. ┤B?
  1065. ┤CCat
  1066. 0┼┬┬┬┬┬┬┬┬
  1067. 0 5";
  1068. TestHelpers.AssertDriverContentsAre (expected, _output);
  1069. // Shutdown must be called to safely clean up Application if Init has been called
  1070. Application.Shutdown ();
  1071. }
  1072. }
  1073. public class PathAnnotationTests
  1074. {
  1075. private readonly ITestOutputHelper _output;
  1076. public PathAnnotationTests (ITestOutputHelper output) { _output = output; }
  1077. [Fact]
  1078. public void MarginBottom_BiggerThanHeight_ExpectBlankGraph ()
  1079. {
  1080. GraphView gv = GraphViewTests.GetGraph ();
  1081. gv.Height = 10;
  1082. gv.MarginBottom = 20;
  1083. gv.Series.Add (
  1084. new ScatterSeries { Points = { new PointF (1, 1), new PointF (5, 0) } }
  1085. );
  1086. gv.LayoutSubviews ();
  1087. gv.Draw ();
  1088. var expected =
  1089. @"
  1090. ";
  1091. TestHelpers.AssertDriverContentsAre (expected, _output);
  1092. // Shutdown must be called to safely clean up Application if Init has been called
  1093. Application.Shutdown ();
  1094. }
  1095. [Fact]
  1096. public void MarginLeft_BiggerThanWidth_ExpectBlankGraph ()
  1097. {
  1098. GraphView gv = GraphViewTests.GetGraph ();
  1099. gv.Width = 10;
  1100. gv.MarginLeft = 20;
  1101. gv.Series.Add (
  1102. new ScatterSeries { Points = { new PointF (1, 1), new PointF (5, 0) } }
  1103. );
  1104. gv.LayoutSubviews ();
  1105. gv.Draw ();
  1106. var expected =
  1107. @"
  1108. ";
  1109. TestHelpers.AssertDriverContentsAre (expected, _output);
  1110. // Shutdown must be called to safely clean up Application if Init has been called
  1111. Application.Shutdown ();
  1112. }
  1113. [Fact]
  1114. public void PathAnnotation_Box ()
  1115. {
  1116. GraphView gv = GraphViewTests.GetGraph ();
  1117. var path = new PathAnnotation ();
  1118. path.Points.Add (new PointF (1, 1));
  1119. path.Points.Add (new PointF (1, 3));
  1120. path.Points.Add (new PointF (6, 3));
  1121. path.Points.Add (new PointF (6, 1));
  1122. // list the starting point again so that it draws a complete square
  1123. // (otherwise it will miss out the last line along the bottom)
  1124. path.Points.Add (new PointF (1, 1));
  1125. gv.Annotations.Add (path);
  1126. gv.LayoutSubviews ();
  1127. gv.Draw ();
  1128. var expected =
  1129. @"
  1130. │......
  1131. ┤. .
  1132. ┤......
  1133. 0┼┬┬┬┬┬┬┬┬
  1134. 0 5";
  1135. TestHelpers.AssertDriverContentsAre (expected, _output);
  1136. // Shutdown must be called to safely clean up Application if Init has been called
  1137. Application.Shutdown ();
  1138. }
  1139. [Fact]
  1140. public void PathAnnotation_Diamond ()
  1141. {
  1142. GraphView gv = GraphViewTests.GetGraph ();
  1143. var path = new PathAnnotation ();
  1144. path.Points.Add (new PointF (1, 2));
  1145. path.Points.Add (new PointF (3, 3));
  1146. path.Points.Add (new PointF (6, 2));
  1147. path.Points.Add (new PointF (3, 1));
  1148. // list the starting point again to close the shape
  1149. path.Points.Add (new PointF (1, 2));
  1150. gv.Annotations.Add (path);
  1151. gv.LayoutSubviews ();
  1152. gv.Draw ();
  1153. var expected =
  1154. @"
  1155. │ ..
  1156. ┤.. ..
  1157. ┤ ...
  1158. 0┼┬┬┬┬┬┬┬┬
  1159. 0 5";
  1160. TestHelpers.AssertDriverContentsAre (expected, _output);
  1161. // Shutdown must be called to safely clean up Application if Init has been called
  1162. Application.Shutdown ();
  1163. }
  1164. [Theory]
  1165. [InlineData (true)]
  1166. [InlineData (false)]
  1167. public void ViewChangeText_RendersCorrectly (bool useFill)
  1168. {
  1169. var driver = new FakeDriver ();
  1170. Application.Init (driver);
  1171. driver.Init ();
  1172. // create a wide window
  1173. var mount = new View { Width = 100, Height = 100 };
  1174. var top = new Toplevel ();
  1175. try
  1176. {
  1177. // Create a view with a short text
  1178. var view = new View { Text = "ff", Width = 2, Height = 1 };
  1179. // Specify that the label should be very wide
  1180. if (useFill)
  1181. {
  1182. view.Width = Dim.Fill ();
  1183. }
  1184. else
  1185. {
  1186. view.Width = 100;
  1187. }
  1188. //put label into view
  1189. mount.Add (view);
  1190. //putting mount into Toplevel since changing size
  1191. top.Add (mount);
  1192. Application.Begin (top);
  1193. // render view
  1194. view.ColorScheme = new ColorScheme ();
  1195. Assert.Equal (1, view.Height);
  1196. mount.SetNeedsDraw ();
  1197. mount.Draw ();
  1198. // should have the initial text
  1199. TestHelpers.AssertDriverContentsAre ("ff", null);
  1200. // change the text and redraw
  1201. view.Text = "ff1234";
  1202. mount.SetNeedsDraw ();
  1203. mount.Draw ();
  1204. // should have the new text rendered
  1205. TestHelpers.AssertDriverContentsAre ("ff1234", null);
  1206. }
  1207. finally
  1208. {
  1209. top.Dispose ();
  1210. Application.Shutdown ();
  1211. }
  1212. }
  1213. [Fact]
  1214. public void XAxisLabels_With_MarginLeft ()
  1215. {
  1216. GraphViewTests.InitFakeDriver ();
  1217. var gv = new GraphView { ColorScheme = new ColorScheme (), Viewport = new Rectangle (0, 0, 10, 7) };
  1218. gv.CellSize = new PointF (1, 0.5f);
  1219. gv.AxisY.Increment = 1;
  1220. gv.AxisY.ShowLabelsEvery = 1;
  1221. gv.Series.Add (
  1222. new ScatterSeries { Points = { new PointF (1, 1), new PointF (5, 0) } }
  1223. );
  1224. // reserve 3 cells of the left for the margin
  1225. gv.MarginLeft = 3;
  1226. gv.MarginBottom = 1;
  1227. gv.LayoutSubviews ();
  1228. gv.SetNeedsDraw ();
  1229. gv.Draw ();
  1230. var expected =
  1231. @$"
  1232. 2┤
  1233. 1┤{CM.Glyphs.Dot}
  1234. 0┼┬┬┬┬{CM.Glyphs.Dot}┬
  1235. 0 5
  1236. ";
  1237. TestHelpers.AssertDriverContentsAre (expected, _output);
  1238. // Shutdown must be called to safely clean up Application if Init has been called
  1239. Application.Shutdown ();
  1240. }
  1241. [Fact]
  1242. public void YAxisLabels_With_MarginBottom ()
  1243. {
  1244. GraphViewTests.InitFakeDriver ();
  1245. var gv = new GraphView { ColorScheme = new ColorScheme (), Viewport = new Rectangle (0, 0, 10, 7) };
  1246. gv.CellSize = new PointF (1, 0.5f);
  1247. gv.AxisY.Increment = 1;
  1248. gv.AxisY.ShowLabelsEvery = 1;
  1249. gv.Series.Add (
  1250. new ScatterSeries { Points = { new PointF (1, 1), new PointF (5, 0) } }
  1251. );
  1252. // reserve 3 cells of the console for the margin
  1253. gv.MarginBottom = 3;
  1254. gv.MarginLeft = 1;
  1255. gv.LayoutSubviews ();
  1256. gv.SetNeedsDraw ();
  1257. gv.Draw ();
  1258. var expected =
  1259. @$"
  1260. 1┤{CM.Glyphs.Dot}
  1261. 0┼┬┬┬┬{CM.Glyphs.Dot}┬┬┬
  1262. 0 5
  1263. ";
  1264. TestHelpers.AssertDriverContentsAre (expected, _output);
  1265. // Shutdown must be called to safely clean up Application if Init has been called
  1266. Application.Shutdown ();
  1267. }
  1268. }
  1269. public class AxisIncrementToRenderTests
  1270. {
  1271. [Fact]
  1272. public void AxisIncrementToRenderTests_Constructor ()
  1273. {
  1274. var render = new AxisIncrementToRender (Orientation.Horizontal, 1, 6.6f);
  1275. Assert.Equal (Orientation.Horizontal, render.Orientation);
  1276. Assert.Equal (1, render.ScreenLocation);
  1277. Assert.Equal (6.6f, render.Value);
  1278. }
  1279. }