GraphViewTests.cs 41 KB

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