GraphViewTests.cs 49 KB

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