GraphViewTests.cs 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586
  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.Redraw (gv.Bounds);
  816. var expected =
  817. @"
  818. ┤ hey!
  819. 0┼┬┬┬┬┬┬┬┬
  820. 0 5";
  821. TestHelpers.AssertDriverContentsAre (expected, output);
  822. // user scrolls up one unit of graph space
  823. gv.ScrollOffset = new PointF (0, 1f);
  824. gv.Redraw (gv.Bounds);
  825. // we expect no change in the location of the annotation (only the axis label changes)
  826. // this is because screen units are constant and do not change as the viewport into
  827. // graph space scrolls to different areas of the graph
  828. expected =
  829. @"
  830. ┤ hey!
  831. 1┼┬┬┬┬┬┬┬┬
  832. 0 5";
  833. TestHelpers.AssertDriverContentsAre (expected, output);
  834. // Shutdown must be called to safely clean up Application if Init has been called
  835. Application.Shutdown ();
  836. }
  837. [Fact]
  838. public void TestTextAnnotation_GraphUnits ()
  839. {
  840. var gv = GraphViewTests.GetGraph ();
  841. gv.Annotations.Add (new TextAnnotation () {
  842. Text = "hey!",
  843. GraphPosition = new PointF (2, 2)
  844. });
  845. gv.Redraw (gv.Bounds);
  846. var expected =
  847. @"
  848. ┤ hey!
  849. 0┼┬┬┬┬┬┬┬┬
  850. 0 5";
  851. TestHelpers.AssertDriverContentsAre (expected, output);
  852. // user scrolls up one unit of graph space
  853. gv.ScrollOffset = new PointF (0, 1f);
  854. gv.Redraw (gv.Bounds);
  855. // we expect the text annotation to go down one line since
  856. // the scroll offset means that that point of graph space is
  857. // lower down in the view. Note the 1 on the axis too, our viewport
  858. // (excluding margins) now shows y of 1 to 4 (previously 0 to 5)
  859. expected =
  860. @"
  861. ┤ hey!
  862. 1┼┬┬┬┬┬┬┬┬
  863. 0 5";
  864. TestHelpers.AssertDriverContentsAre (expected, output);
  865. // Shutdown must be called to safely clean up Application if Init has been called
  866. Application.Shutdown ();
  867. }
  868. [Fact]
  869. public void TestTextAnnotation_LongText ()
  870. {
  871. var gv = GraphViewTests.GetGraph ();
  872. gv.Annotations.Add (new TextAnnotation () {
  873. Text = "hey there partner hows it going boy its great",
  874. GraphPosition = new PointF (2, 2)
  875. });
  876. gv.Redraw (gv.Bounds);
  877. // long text should get truncated
  878. // margin takes up 1 units
  879. // the GraphPosition of the anntation is 2
  880. // Leaving 7 characters of the annotation renderable (including space)
  881. var expected =
  882. @"
  883. ┤ hey the
  884. 0┼┬┬┬┬┬┬┬┬
  885. 0 5";
  886. TestHelpers.AssertDriverContentsAre (expected, output);
  887. // Shutdown must be called to safely clean up Application if Init has been called
  888. Application.Shutdown ();
  889. }
  890. [Fact]
  891. public void TestTextAnnotation_Offscreen ()
  892. {
  893. var gv = GraphViewTests.GetGraph ();
  894. gv.Annotations.Add (new TextAnnotation () {
  895. Text = "hey there partner hows it going boy its great",
  896. GraphPosition = new PointF (9, 2)
  897. });
  898. gv.Redraw (gv.Bounds);
  899. // Text is off the screen (graph x axis runs to 8 not 9)
  900. var expected =
  901. @"
  902. 0┼┬┬┬┬┬┬┬┬
  903. 0 5";
  904. TestHelpers.AssertDriverContentsAre (expected, output);
  905. // Shutdown must be called to safely clean up Application if Init has been called
  906. Application.Shutdown ();
  907. }
  908. [Theory]
  909. [InlineData (null)]
  910. [InlineData (" ")]
  911. [InlineData ("\t\t")]
  912. public void TestTextAnnotation_EmptyText (string whitespace)
  913. {
  914. var gv = GraphViewTests.GetGraph ();
  915. gv.Annotations.Add (new TextAnnotation () {
  916. Text = whitespace,
  917. GraphPosition = new PointF (4, 2)
  918. });
  919. // add a point a bit further along the graph so if the whitespace were rendered
  920. // the test would pick it up (AssertDriverContentsAre ignores trailing whitespace on lines)
  921. var points = new ScatterSeries ();
  922. points.Points.Add (new PointF (7, 2));
  923. gv.Series.Add (points);
  924. gv.Redraw (gv.Bounds);
  925. var expected =
  926. @"
  927. ┤ x
  928. 0┼┬┬┬┬┬┬┬┬
  929. 0 5";
  930. TestHelpers.AssertDriverContentsAre (expected, output);
  931. // Shutdown must be called to safely clean up Application if Init has been called
  932. Application.Shutdown ();
  933. }
  934. }
  935. public class LegendTests {
  936. readonly ITestOutputHelper output;
  937. public LegendTests (ITestOutputHelper output)
  938. {
  939. this.output = output;
  940. }
  941. [Fact]
  942. public void LegendNormalUsage_WithBorder ()
  943. {
  944. var gv = GraphViewTests.GetGraph ();
  945. var legend = new LegendAnnotation (new Rect (2, 0, 5, 3));
  946. legend.AddEntry (new GraphCellToRender ('A'), "Ant");
  947. legend.AddEntry (new GraphCellToRender ('B'), "Bat");
  948. gv.Annotations.Add (legend);
  949. gv.Redraw (gv.Bounds);
  950. var expected =
  951. @"
  952. │┌───┐
  953. ┤│AAn│
  954. ┤└───┘
  955. 0┼┬┬┬┬┬┬┬┬
  956. 0 5";
  957. TestHelpers.AssertDriverContentsAre (expected, output);
  958. // Shutdown must be called to safely clean up Application if Init has been called
  959. Application.Shutdown ();
  960. }
  961. [Fact]
  962. public void LegendNormalUsage_WithoutBorder ()
  963. {
  964. var gv = GraphViewTests.GetGraph ();
  965. var legend = new LegendAnnotation (new Rect (2, 0, 5, 3));
  966. legend.AddEntry (new GraphCellToRender ('A'), "Ant");
  967. legend.AddEntry (new GraphCellToRender ('B'), "?"); // this will exercise pad
  968. legend.AddEntry (new GraphCellToRender ('C'), "Cat");
  969. legend.AddEntry (new GraphCellToRender ('H'), "Hattter"); // not enough space for this oen
  970. legend.Border = false;
  971. gv.Annotations.Add (legend);
  972. gv.Redraw (gv.Bounds);
  973. var expected =
  974. @"
  975. │AAnt
  976. ┤B?
  977. ┤CCat
  978. 0┼┬┬┬┬┬┬┬┬
  979. 0 5";
  980. TestHelpers.AssertDriverContentsAre (expected, output);
  981. // Shutdown must be called to safely clean up Application if Init has been called
  982. Application.Shutdown ();
  983. }
  984. }
  985. public class PathAnnotationTests {
  986. readonly ITestOutputHelper output;
  987. public PathAnnotationTests (ITestOutputHelper output)
  988. {
  989. this.output = output;
  990. }
  991. [Fact]
  992. public void PathAnnotation_Box ()
  993. {
  994. var gv = GraphViewTests.GetGraph ();
  995. var path = new PathAnnotation ();
  996. path.Points.Add (new PointF (1, 1));
  997. path.Points.Add (new PointF (1, 3));
  998. path.Points.Add (new PointF (6, 3));
  999. path.Points.Add (new PointF (6, 1));
  1000. // list the starting point again so that it draws a complete square
  1001. // (otherwise it will miss out the last line along the bottom)
  1002. path.Points.Add (new PointF (1, 1));
  1003. gv.Annotations.Add (path);
  1004. gv.LayoutSubviews ();
  1005. gv.Redraw (gv.Bounds);
  1006. var expected =
  1007. @"
  1008. │......
  1009. ┤. .
  1010. ┤......
  1011. 0┼┬┬┬┬┬┬┬┬
  1012. 0 5";
  1013. TestHelpers.AssertDriverContentsAre (expected, output);
  1014. // Shutdown must be called to safely clean up Application if Init has been called
  1015. Application.Shutdown ();
  1016. }
  1017. [Fact]
  1018. public void YAxisLabels_With_MarginBottom ()
  1019. {
  1020. GraphViewTests.InitFakeDriver ();
  1021. var gv = new GraphView {
  1022. ColorScheme = new ColorScheme (),
  1023. Bounds = new Rect (0, 0, 10, 7)
  1024. };
  1025. gv.CellSize = new PointF (1, 0.5f);
  1026. gv.AxisY.Increment = 1;
  1027. gv.AxisY.ShowLabelsEvery = 1;
  1028. gv.Series.Add (new ScatterSeries {
  1029. Points = { new PointF (1, 1), new PointF (5, 0) }
  1030. });
  1031. // reserve 3 cells of the console for the margin
  1032. gv.MarginBottom = 3;
  1033. gv.MarginLeft = 1;
  1034. gv.LayoutSubviews ();
  1035. gv.Redraw (gv.Bounds);
  1036. var expected =
  1037. @"
  1038. 1┤x
  1039. 0┼┬┬┬┬x┬┬┬
  1040. 0 5
  1041. ";
  1042. TestHelpers.AssertDriverContentsAre (expected, output);
  1043. // Shutdown must be called to safely clean up Application if Init has been called
  1044. Application.Shutdown ();
  1045. }
  1046. [Fact]
  1047. public void XAxisLabels_With_MarginLeft ()
  1048. {
  1049. GraphViewTests.InitFakeDriver ();
  1050. var gv = new GraphView {
  1051. ColorScheme = new ColorScheme (),
  1052. Bounds = new Rect (0, 0, 10, 7)
  1053. };
  1054. gv.CellSize = new PointF (1, 0.5f);
  1055. gv.AxisY.Increment = 1;
  1056. gv.AxisY.ShowLabelsEvery = 1;
  1057. gv.Series.Add (new ScatterSeries {
  1058. Points = { new PointF (1, 1), new PointF (5, 0) }
  1059. });
  1060. // reserve 3 cells of the left for the margin
  1061. gv.MarginLeft = 3;
  1062. gv.MarginBottom = 1;
  1063. gv.LayoutSubviews ();
  1064. gv.Redraw (gv.Bounds);
  1065. var expected =
  1066. @"
  1067. 2┤
  1068. 1┤x
  1069. 0┼┬┬┬┬x┬
  1070. 0 5
  1071. ";
  1072. TestHelpers.AssertDriverContentsAre (expected, output);
  1073. // Shutdown must be called to safely clean up Application if Init has been called
  1074. Application.Shutdown ();
  1075. }
  1076. [Fact]
  1077. public void MarginBottom_BiggerThanHeight_ExpectBlankGraph ()
  1078. {
  1079. var gv = GraphViewTests.GetGraph ();
  1080. gv.Height = 10;
  1081. gv.MarginBottom = 20;
  1082. gv.Series.Add (new ScatterSeries {
  1083. Points = { new PointF (1, 1), new PointF (5, 0) }
  1084. });
  1085. gv.LayoutSubviews ();
  1086. gv.Redraw (gv.Bounds);
  1087. var expected =
  1088. @"
  1089. ";
  1090. TestHelpers.AssertDriverContentsAre (expected, output);
  1091. // Shutdown must be called to safely clean up Application if Init has been called
  1092. Application.Shutdown ();
  1093. }
  1094. [Fact]
  1095. public void MarginLeft_BiggerThanWidth_ExpectBlankGraph ()
  1096. {
  1097. var gv = GraphViewTests.GetGraph ();
  1098. gv.Width = 10;
  1099. gv.MarginLeft = 20;
  1100. gv.Series.Add (new ScatterSeries {
  1101. Points = { new PointF (1, 1), new PointF (5, 0) }
  1102. });
  1103. gv.LayoutSubviews ();
  1104. gv.Redraw (gv.Bounds);
  1105. var expected =
  1106. @"
  1107. ";
  1108. TestHelpers.AssertDriverContentsAre (expected, output);
  1109. // Shutdown must be called to safely clean up Application if Init has been called
  1110. Application.Shutdown ();
  1111. }
  1112. [Fact]
  1113. public void PathAnnotation_Diamond ()
  1114. {
  1115. var gv = GraphViewTests.GetGraph ();
  1116. var path = new PathAnnotation ();
  1117. path.Points.Add (new PointF (1, 2));
  1118. path.Points.Add (new PointF (3, 3));
  1119. path.Points.Add (new PointF (6, 2));
  1120. path.Points.Add (new PointF (3, 1));
  1121. // list the starting point again to close the shape
  1122. path.Points.Add (new PointF (1, 2));
  1123. gv.Annotations.Add (path);
  1124. gv.LayoutSubviews ();
  1125. gv.Redraw (gv.Bounds);
  1126. var expected =
  1127. @"
  1128. │ ..
  1129. ┤.. ..
  1130. ┤ ...
  1131. 0┼┬┬┬┬┬┬┬┬
  1132. 0 5";
  1133. TestHelpers.AssertDriverContentsAre (expected, output);
  1134. // Shutdown must be called to safely clean up Application if Init has been called
  1135. Application.Shutdown ();
  1136. }
  1137. [Theory]
  1138. [InlineData (true)]
  1139. [InlineData (false)]
  1140. public void LabelChangeText_RendersCorrectly (bool useFill)
  1141. {
  1142. var driver = new FakeDriver ();
  1143. Application.Init (driver);
  1144. driver.Init (() => { });
  1145. // create a wide window
  1146. var mount = new View () {
  1147. Width = 100,
  1148. Height = 100
  1149. };
  1150. try {
  1151. // Create a label with a short text
  1152. var lbl1 = new Label ("ff");
  1153. // Specify that the label should be very wide
  1154. if (useFill) {
  1155. lbl1.Width = Dim.Fill ();
  1156. } else {
  1157. lbl1.Width = 100;
  1158. }
  1159. //put label into view
  1160. mount.Add (lbl1);
  1161. //putting mount into toplevel since changing size
  1162. //also change AutoSize to false
  1163. Application.Top.Add (mount);
  1164. Application.Begin (Application.Top);
  1165. // render view
  1166. lbl1.ColorScheme = new ColorScheme ();
  1167. Assert.Equal (1, lbl1.Height);
  1168. mount.Redraw (mount.Bounds);
  1169. // should have the initial text
  1170. TestHelpers.AssertDriverContentsAre ("ff", null);
  1171. // change the text and redraw
  1172. lbl1.Text = "ff1234";
  1173. mount.Redraw (mount.Bounds);
  1174. // should have the new text rendered
  1175. TestHelpers.AssertDriverContentsAre ("ff1234", null);
  1176. } finally {
  1177. Application.Shutdown ();
  1178. }
  1179. }
  1180. }
  1181. public class AxisIncrementToRenderTests {
  1182. [Fact]
  1183. public void AxisIncrementToRenderTests_Constructor ()
  1184. {
  1185. var render = new AxisIncrementToRender (Orientation.Horizontal, 1, 6.6f);
  1186. Assert.Equal (Orientation.Horizontal, render.Orientation);
  1187. Assert.Equal (1, render.ScreenLocation);
  1188. Assert.Equal (6.6f, render.Value);
  1189. }
  1190. }
  1191. }