GraphViewTests.cs 48 KB

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