GraphViewTests.cs 52 KB

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