GraphViewTests.cs 48 KB

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