PageRenderTime 28ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/main/src/addins/NUnit/Gui/TestChart.cs

https://github.com/jfcantin/monodevelop
C# | 406 lines | 327 code | 49 blank | 30 comment | 86 complexity | fcf35eb902e7547b6f0a7edd948b89e0 MD5 | raw file
  1. //
  2. // TestChart.cs
  3. //
  4. // Author:
  5. // Lluis Sanchez Gual
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using Gtk;
  31. using Gdk;
  32. using MonoDevelop.Components.Chart;
  33. namespace MonoDevelop.NUnit
  34. {
  35. public enum TestChartType {
  36. Results,
  37. Time
  38. }
  39. class TestRunAxis: IntegerAxis
  40. {
  41. public UnitTestResult[] CurrentResults;
  42. public TestRunAxis (bool showLabel): base (showLabel)
  43. {
  44. }
  45. public override string GetValueLabel (double value)
  46. {
  47. if (CurrentResults == null)
  48. return "";
  49. int val = (int) value;
  50. if (val >= CurrentResults.Length)
  51. return "";
  52. UnitTestResult res = CurrentResults [CurrentResults.Length - val - 1];
  53. return string.Format ("{0}/{1}", res.TestDate.Day, res.TestDate.Month);
  54. }
  55. }
  56. public class TestChart: BasicChart
  57. {
  58. Serie serieFailed;
  59. Serie serieSuccess;
  60. Serie serieIgnored;
  61. Serie serieTime;
  62. bool timeScale = false;
  63. bool singleDayResult = false;
  64. TestChartType type;
  65. TimeSpan currentSpan = TimeSpan.FromDays (5);
  66. int testCount = 20;
  67. UnitTest test;
  68. bool showLastTest = true;
  69. bool resetCursors = true;
  70. double lastDateValue;
  71. double lastTestNumber;
  72. UnitTestResult[] currentResults;
  73. TestRunAxis testRunAxis;
  74. public TestChart ()
  75. {
  76. AllowSelection = true;
  77. SetAutoScale (AxisDimension.Y, false, true);
  78. StartY = 0;
  79. serieFailed = new Serie ("Failed tests");
  80. serieFailed.Color = new Cairo.Color (1, 0, 0);
  81. serieSuccess = new Serie ("Successful tests");
  82. serieSuccess.Color = new Cairo.Color (0, 0.65, 0);
  83. serieIgnored = new Serie ("Ignored tests");
  84. serieIgnored.Color = new Cairo.Color (0.8, 0.8, 0);
  85. serieTime = new Serie ("Time");
  86. serieTime.Color = new Cairo.Color (0, 0, 1);
  87. UpdateMode ();
  88. /* EndX = DateTime.Now.Ticks;
  89. StartX = EndX - currentSpan.Ticks;
  90. */
  91. EndX = 5;
  92. StartX = 0;
  93. }
  94. public bool ShowSuccessfulTests {
  95. get { return serieSuccess.Visible; }
  96. set { serieSuccess.Visible = value; }
  97. }
  98. public bool ShowFailedTests {
  99. get { return serieFailed.Visible; }
  100. set { serieFailed.Visible = value; }
  101. }
  102. public bool ShowIgnoredTests {
  103. get { return serieIgnored.Visible; }
  104. set { serieIgnored.Visible = value; }
  105. }
  106. public bool UseTimeScale {
  107. get { return timeScale; }
  108. set { timeScale = value; UpdateMode (); }
  109. }
  110. public bool SingleDayResult {
  111. get { return singleDayResult; }
  112. set { singleDayResult = value; UpdateMode (); }
  113. }
  114. public TestChartType Type {
  115. get { return type; }
  116. set { type = value; UpdateMode (); }
  117. }
  118. public DateTime CurrentDate {
  119. get {
  120. if (timeScale)
  121. return new DateTime ((long) SelectionEnd.Value);
  122. else {
  123. int n = (int) SelectionStart.Value;
  124. if (currentResults != null && n >= 0 && n < currentResults.Length)
  125. return currentResults [currentResults.Length - n - 1].TestDate;
  126. else
  127. return DateTime.MinValue;
  128. }
  129. }
  130. }
  131. public DateTime ReferenceDate {
  132. get {
  133. if (timeScale)
  134. return new DateTime ((long) SelectionStart.Value);
  135. else {
  136. int n = (int) SelectionEnd.Value;
  137. if (currentResults != null && n >= 0 && n < currentResults.Length)
  138. return currentResults [currentResults.Length - n - 1].TestDate;
  139. else
  140. return DateTime.MinValue;
  141. }
  142. }
  143. }
  144. void UpdateMode ()
  145. {
  146. AllowSelection = false;
  147. Reset ();
  148. if (type == TestChartType.Results) {
  149. AddSerie (serieIgnored);
  150. AddSerie (serieFailed);
  151. AddSerie (serieSuccess);
  152. } else {
  153. AddSerie (serieTime);
  154. }
  155. if (timeScale) {
  156. ReverseXAxis = false;
  157. Axis ax = new DateTimeAxis (true);
  158. AddAxis (new DateTimeAxis (false), AxisPosition.Top);
  159. AddAxis (ax, AxisPosition.Bottom);
  160. SelectionEnd.Value = SelectionStart.Value = DateTime.Now.Ticks;
  161. SelectionStart.LabelAxis = ax;
  162. SelectionEnd.LabelAxis = ax;
  163. } else {
  164. ReverseXAxis = true;
  165. AddAxis (new TestRunAxis (false), AxisPosition.Top);
  166. testRunAxis = new TestRunAxis (true);
  167. AddAxis (testRunAxis, AxisPosition.Bottom);
  168. SelectionEnd.Value = SelectionStart.Value = 0;
  169. SelectionStart.LabelAxis = testRunAxis;
  170. SelectionEnd.LabelAxis = testRunAxis;
  171. }
  172. showLastTest = true;
  173. resetCursors = true;
  174. AddAxis (new IntegerAxis (true), AxisPosition.Left);
  175. AddAxis (new IntegerAxis (true), AxisPosition.Right);
  176. if (test != null)
  177. Fill (test);
  178. AllowSelection = true;
  179. }
  180. public new void Clear ()
  181. {
  182. base.Clear ();
  183. test = null;
  184. }
  185. public void ZoomIn ()
  186. {
  187. if (test == null)
  188. return;
  189. if (timeScale) {
  190. currentSpan = new TimeSpan (currentSpan.Ticks / 2);
  191. if (currentSpan.TotalSeconds < 60)
  192. currentSpan = TimeSpan.FromSeconds (60);
  193. } else {
  194. testCount = testCount / 2;
  195. if (testCount < 5)
  196. testCount = 5;
  197. }
  198. Fill (test);
  199. }
  200. public void ZoomOut ()
  201. {
  202. if (test == null)
  203. return;
  204. if (timeScale) {
  205. currentSpan = new TimeSpan (currentSpan.Ticks * 2);
  206. if (currentSpan.TotalDays > 50 * 365)
  207. currentSpan = TimeSpan.FromDays (50 * 365);
  208. } else {
  209. testCount *= 2;
  210. if (testCount > 100000)
  211. testCount = 100000;
  212. }
  213. Fill (test);
  214. }
  215. public void GoNext ()
  216. {
  217. if (showLastTest)
  218. return;
  219. if (timeScale) {
  220. lastDateValue += (EndX - StartX) / 3;
  221. UnitTestResult lastResult = test.Results.GetLastResult (DateTime.Now);
  222. if (lastResult != null && new DateTime ((long)lastDateValue) > lastResult.TestDate)
  223. showLastTest = true;
  224. } else {
  225. lastTestNumber -= (EndX - StartX) / 3;
  226. if (lastTestNumber < 0)
  227. showLastTest = true;
  228. }
  229. Fill (test);
  230. }
  231. public void GoPrevious ()
  232. {
  233. if (timeScale) {
  234. lastDateValue -= (EndX - StartX) / 3;
  235. } else {
  236. lastTestNumber += (EndX - StartX) / 3;
  237. }
  238. showLastTest = false;
  239. Fill (test);
  240. }
  241. public void GoLast ()
  242. {
  243. showLastTest = true;
  244. resetCursors = true;
  245. Fill (test);
  246. }
  247. public void Fill (UnitTest test)
  248. {
  249. serieFailed.Clear ();
  250. serieSuccess.Clear ();
  251. serieIgnored.Clear ();
  252. serieTime.Clear ();
  253. this.test = test;
  254. if (showLastTest) {
  255. if (timeScale)
  256. lastDateValue = DateTime.Now.Ticks;
  257. else
  258. lastTestNumber = 0;
  259. }
  260. UnitTestResult first = null;
  261. UnitTestResult[] results;
  262. UnitTestResult lastResult = test.Results.GetLastResult (DateTime.Now);
  263. if (lastResult == null)
  264. return;
  265. if (timeScale) {
  266. DateTime startDate;
  267. if (showLastTest) {
  268. startDate = lastResult.TestDate - currentSpan;
  269. StartX = startDate.Ticks;
  270. EndX = lastResult.TestDate.Ticks;
  271. first = test.Results.GetLastResult (startDate);
  272. results = test.Results.GetResults (startDate, lastResult.TestDate);
  273. } else {
  274. DateTime endDate = new DateTime ((long)lastDateValue);
  275. startDate = endDate - currentSpan;
  276. StartX = (double) startDate.Ticks;
  277. EndX = (double) endDate.Ticks;
  278. first = test.Results.GetLastResult (startDate);
  279. results = test.Results.GetResults (startDate, lastResult.TestDate);
  280. }
  281. if (singleDayResult) {
  282. first = test.Results.GetPreviousResult (new DateTime (startDate.Year, startDate.Month, startDate.Day));
  283. ArrayList list = new ArrayList ();
  284. if (first != null)
  285. list.Add (first);
  286. for (int n=0; n<results.Length - 1; n++) {
  287. DateTime d1 = results [n].TestDate;
  288. DateTime d2 = results [n + 1].TestDate;
  289. if (d1.Day != d2.Day || d1.Month != d2.Month || d1.Year != d2.Year)
  290. list.Add (results[n]);
  291. }
  292. list.Add (results [results.Length - 1]);
  293. results = (UnitTestResult[]) list.ToArray (typeof(UnitTestResult));
  294. }
  295. if (resetCursors) {
  296. SelectionEnd.Value = EndX;
  297. if (results.Length > 1)
  298. SelectionStart.Value = results [results.Length - 2].TestDate.Ticks;
  299. else
  300. SelectionStart.Value = EndX;
  301. resetCursors = false;
  302. }
  303. } else {
  304. if (singleDayResult) {
  305. ArrayList list = new ArrayList ();
  306. list.Add (lastResult);
  307. while (list.Count < testCount + (int)lastTestNumber + 1) {
  308. UnitTestResult res = test.Results.GetPreviousResult (lastResult.TestDate);
  309. if (res == null) break;
  310. if (res.TestDate.Day != lastResult.TestDate.Day || res.TestDate.Month != lastResult.TestDate.Month || res.TestDate.Year != lastResult.TestDate.Year)
  311. list.Add (res);
  312. lastResult = res;
  313. }
  314. results = (UnitTestResult[]) list.ToArray (typeof(UnitTestResult));
  315. Array.Reverse (results);
  316. } else {
  317. results = test.Results.GetResultsToDate (DateTime.Now, testCount + (int)lastTestNumber + 1);
  318. }
  319. EndX = lastTestNumber + testCount;
  320. StartX = lastTestNumber;
  321. if (resetCursors) {
  322. SelectionStart.Value = StartX;
  323. SelectionEnd.Value = StartX + 1;
  324. resetCursors = false;
  325. }
  326. }
  327. currentResults = results;
  328. if (testRunAxis != null)
  329. testRunAxis.CurrentResults = currentResults;
  330. if (Type == TestChartType.Results) {
  331. if (first != null) {
  332. double x = timeScale ? first.TestDate.Ticks : results.Length;
  333. serieFailed.AddData (x, first.TotalFailures);
  334. serieSuccess.AddData (x, first.TotalSuccess);
  335. serieIgnored.AddData (x, first.TotalIgnored);
  336. }
  337. for (int n=0; n < results.Length; n++) {
  338. UnitTestResult res = results [n];
  339. double x = timeScale ? res.TestDate.Ticks : results.Length - n - 1;
  340. serieFailed.AddData (x, res.TotalFailures);
  341. serieSuccess.AddData (x, res.TotalSuccess);
  342. serieIgnored.AddData (x, res.TotalIgnored);
  343. }
  344. } else {
  345. if (first != null) {
  346. double x = timeScale ? first.TestDate.Ticks : results.Length;
  347. serieTime.AddData (x, first.Time.TotalMilliseconds);
  348. }
  349. for (int n=0; n < results.Length; n++) {
  350. UnitTestResult res = results [n];
  351. double x = timeScale ? res.TestDate.Ticks : results.Length - n - 1;
  352. serieTime.AddData (x, results [n].Time.TotalMilliseconds);
  353. }
  354. }
  355. }
  356. }
  357. }