GraphViewTests.cs 43 KB

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