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