GraphViewTests.cs 43 KB

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