GraphViewTests.cs 49 KB

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