GraphViewTests.cs 48 KB

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