GraphViewTests.cs 48 KB

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