PageRenderTime 59ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/Editor/UnitTestingWindow.cs

https://bitbucket.org/createdbyx/codefarts.unitytesting
C# | 260 lines | 152 code | 43 blank | 65 comment | 10 complexity | f2587d61c81100418bff02e5e4d4f950 MD5 | raw file
  1. /*
  2. <copyright>
  3. Codefarts.UnityTesting spawned from SharpUnit
  4. contact@codefarts.com
  5. http://www.codefarts.com
  6. SharpUnit was written by:
  7. Mark Gants | mark@markgants.com
  8. May 20, 2010
  9. This software is provided "as is" for free.
  10. You may do anything you like with this software as long as you leave this notice in place.
  11. See original LICENCE file for full license details.
  12. </copyright>
  13. */
  14. namespace Codefarts.GeneralTools.Windows
  15. {
  16. using System;
  17. using System.Collections.Generic;
  18. using System.IO;
  19. using System.Linq;
  20. using Codefarts.UnitTesting;
  21. using Codefarts.UnitTesting.Editor;
  22. using Codefarts.UnitTesting.Unity;
  23. using UnityEngine;
  24. using UnityEditor;
  25. using Vector2 = UnityEngine.Vector2;
  26. public class UnitTestingWindow : EditorWindow
  27. {
  28. /// <summary>
  29. /// Provides a key type for the list of tests shown in the test window.
  30. /// </summary>
  31. public class KeyType
  32. {
  33. /// <summary>
  34. /// Gets or sets whether or not the key is shown expanded.
  35. /// </summary>
  36. public bool Expanded { get; set; }
  37. /// <summary>
  38. /// Gets or sets the name of the test class containing the test methods.
  39. /// </summary>
  40. public string Name { get; set; }
  41. }
  42. /// <summary>
  43. /// Used to draw the test results in a table format.
  44. /// </summary>
  45. private readonly Table<TestResult> grid;
  46. /// <summary>
  47. /// Used to draw the test names for the tests.
  48. /// </summary>
  49. private Dictionary<KeyType, List<TestModel>> testModels;
  50. /// <summary>
  51. /// Holds the scroll value for the results table.
  52. /// </summary>
  53. private Vector2 resultsScrollPosition;
  54. /// <summary>
  55. /// Holds the scroll value for the test names.
  56. /// </summary>
  57. private Vector2 testsScrollPosition;
  58. /// <summary>
  59. /// Default Constructor.
  60. /// </summary>
  61. public UnitTestingWindow()
  62. {
  63. this.grid = new Table<TestResult>(Localization.LocalizationManager.Instance.Get("Results")) { AlwaysDraw = true };
  64. }
  65. /// <summary>
  66. /// Called by unity to draw the ui.
  67. /// </summary>
  68. public void OnGUI()
  69. {
  70. var local = Localization.LocalizationManager.Instance;
  71. GUILayout.BeginVertical(); // top level
  72. GUILayout.BeginHorizontal(); // tool bar
  73. // draw output to console check box
  74. this.ConsoleOutput = GUILayout.Toggle(this.ConsoleOutput, local.Get("Console"));
  75. GUILayout.FlexibleSpace();
  76. if (GUILayout.Button(local.Get("RunTests")))
  77. {
  78. // get test methods to run
  79. var items = from t in this.testModels
  80. from x in t.Value
  81. where (bool)x.Tag
  82. select x;
  83. var results = Helpers.RunTests(this.ConsoleOutput, items);
  84. this.grid.Model = new UnitTestResultModel(results);
  85. }
  86. GUILayout.EndHorizontal(); // tool bar
  87. GUILayout.BeginHorizontal(); // panels
  88. this.DrawTestNameList();
  89. this.DrawResultsTable();
  90. GUILayout.EndHorizontal(); // panels
  91. GUILayout.EndVertical(); // top level
  92. }
  93. /// <summary>
  94. /// Gets or sets a value indicating whether or not the results from the tests will be reported out to the unity console window.
  95. /// </summary>
  96. public bool ConsoleOutput { get; set; }
  97. /// <summary>
  98. /// Draws the results table.
  99. /// </summary>
  100. private void DrawResultsTable()
  101. {
  102. this.resultsScrollPosition = GUILayout.BeginScrollView(this.resultsScrollPosition, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
  103. this.grid.Draw();
  104. GUILayout.FlexibleSpace();
  105. GUILayout.EndScrollView();
  106. }
  107. /// <summary>
  108. /// Draws the list of test names.
  109. /// </summary>
  110. private void DrawTestNameList()
  111. {
  112. GUILayout.BeginVertical(GUILayout.MaxWidth(300));
  113. var local = Localization.LocalizationManager.Instance;
  114. // setup callback for setting check box values
  115. var setValues = new Action<bool>(value =>
  116. {
  117. foreach (var model in this.testModels)
  118. {
  119. foreach (var item in model.Value)
  120. {
  121. item.Tag = value;
  122. }
  123. }
  124. });
  125. GUILayout.BeginHorizontal(); // all none buttons
  126. if (GUILayout.Button(local.Get("Expand")))
  127. {
  128. foreach (var item in this.testModels)
  129. {
  130. item.Key.Expanded = true;
  131. }
  132. }
  133. if (GUILayout.Button(local.Get("Collapse")))
  134. {
  135. foreach (var item in this.testModels)
  136. {
  137. item.Key.Expanded = false;
  138. }
  139. }
  140. GUILayout.FlexibleSpace();
  141. if (GUILayout.Button(local.Get("All")))
  142. {
  143. setValues(true);
  144. }
  145. if (GUILayout.Button(local.Get("None")))
  146. {
  147. setValues(false);
  148. }
  149. GUILayout.EndHorizontal(); // all none buttons
  150. this.testsScrollPosition = GUILayout.BeginScrollView(this.testsScrollPosition, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(true));
  151. foreach (var item in this.testModels)
  152. {
  153. var category = Path.GetExtension(item.Key.Name);
  154. category = category != null && category.StartsWith(".") ? category.Substring(1) : category;
  155. GUILayout.BeginHorizontal();
  156. var all = item.Value.All(x => (bool)x.Tag);
  157. var result = GUILayout.Toggle(all, string.Empty, GUILayout.ExpandWidth(false));
  158. // draw test class
  159. item.Key.Expanded = EditorGUILayout.Foldout(item.Key.Expanded, category);
  160. GUILayout.EndHorizontal();
  161. if (all != result)
  162. {
  163. foreach (var model in item.Value)
  164. {
  165. model.Tag = result;
  166. }
  167. }
  168. if (!item.Key.Expanded)
  169. {
  170. // if not expanded then skip to next test class
  171. continue;
  172. }
  173. GUILayout.BeginHorizontal();
  174. GUILayout.Space(16);
  175. GUILayout.BeginVertical();
  176. // draw test methods
  177. foreach (var value in item.Value)
  178. {
  179. value.Tag = GUILayout.Toggle((bool)value.Tag, value.MethodName.Replace("_", " "), GUILayout.ExpandWidth(false));
  180. }
  181. GUILayout.EndVertical();
  182. GUILayout.EndHorizontal();
  183. }
  184. GUILayout.FlexibleSpace();
  185. GUILayout.EndScrollView();
  186. GUILayout.EndVertical();
  187. }
  188. /// <summary>
  189. /// Called by unity when the window is enabled.
  190. /// </summary>
  191. public void OnEnable()
  192. {
  193. this.testModels = Helpers.GetTestModels()
  194. .GroupBy(o => o.Reference.GetType().FullName)
  195. .ToDictionary(g => new KeyType() { Name = g.Key, Expanded = true }, g => g.Select(x =>
  196. {
  197. x.Tag = false;
  198. return x;
  199. }).ToList());
  200. }
  201. /// <summary>
  202. /// Used to initialize the window.
  203. /// </summary>
  204. [MenuItem("Window/Codefarts/Unit Testing")]
  205. public static void ShowWindow()
  206. {
  207. // get the window, show it, and hand it focus
  208. var window = GetWindow<UnitTestingWindow>(Localization.LocalizationManager.Instance.Get("UnitTesting"));
  209. window.Show();
  210. window.Focus();
  211. window.Repaint();
  212. }
  213. }
  214. }