GraphViewTests.cs 45 KB

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