/Editor/UnitTestingWindow.cs
https://bitbucket.org/createdbyx/codefarts.unitytesting · C# · 260 lines · 152 code · 43 blank · 65 comment · 10 complexity · f2587d61c81100418bff02e5e4d4f950 MD5 · raw file
- /*
- <copyright>
- Codefarts.UnityTesting spawned from SharpUnit
- contact@codefarts.com
- http://www.codefarts.com
- SharpUnit was written by:
- Mark Gants | mark@markgants.com
- May 20, 2010
- This software is provided "as is" for free.
- You may do anything you like with this software as long as you leave this notice in place.
- See original LICENCE file for full license details.
- </copyright>
- */
- namespace Codefarts.GeneralTools.Windows
- {
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using Codefarts.UnitTesting;
- using Codefarts.UnitTesting.Editor;
- using Codefarts.UnitTesting.Unity;
- using UnityEngine;
- using UnityEditor;
- using Vector2 = UnityEngine.Vector2;
- public class UnitTestingWindow : EditorWindow
- {
- /// <summary>
- /// Provides a key type for the list of tests shown in the test window.
- /// </summary>
- public class KeyType
- {
- /// <summary>
- /// Gets or sets whether or not the key is shown expanded.
- /// </summary>
- public bool Expanded { get; set; }
- /// <summary>
- /// Gets or sets the name of the test class containing the test methods.
- /// </summary>
- public string Name { get; set; }
- }
- /// <summary>
- /// Used to draw the test results in a table format.
- /// </summary>
- private readonly Table<TestResult> grid;
- /// <summary>
- /// Used to draw the test names for the tests.
- /// </summary>
- private Dictionary<KeyType, List<TestModel>> testModels;
- /// <summary>
- /// Holds the scroll value for the results table.
- /// </summary>
- private Vector2 resultsScrollPosition;
- /// <summary>
- /// Holds the scroll value for the test names.
- /// </summary>
- private Vector2 testsScrollPosition;
- /// <summary>
- /// Default Constructor.
- /// </summary>
- public UnitTestingWindow()
- {
- this.grid = new Table<TestResult>(Localization.LocalizationManager.Instance.Get("Results")) { AlwaysDraw = true };
- }
- /// <summary>
- /// Called by unity to draw the ui.
- /// </summary>
- public void OnGUI()
- {
- var local = Localization.LocalizationManager.Instance;
- GUILayout.BeginVertical(); // top level
- GUILayout.BeginHorizontal(); // tool bar
- // draw output to console check box
- this.ConsoleOutput = GUILayout.Toggle(this.ConsoleOutput, local.Get("Console"));
- GUILayout.FlexibleSpace();
- if (GUILayout.Button(local.Get("RunTests")))
- {
- // get test methods to run
- var items = from t in this.testModels
- from x in t.Value
- where (bool)x.Tag
- select x;
- var results = Helpers.RunTests(this.ConsoleOutput, items);
- this.grid.Model = new UnitTestResultModel(results);
- }
- GUILayout.EndHorizontal(); // tool bar
- GUILayout.BeginHorizontal(); // panels
- this.DrawTestNameList();
- this.DrawResultsTable();
- GUILayout.EndHorizontal(); // panels
- GUILayout.EndVertical(); // top level
- }
- /// <summary>
- /// Gets or sets a value indicating whether or not the results from the tests will be reported out to the unity console window.
- /// </summary>
- public bool ConsoleOutput { get; set; }
- /// <summary>
- /// Draws the results table.
- /// </summary>
- private void DrawResultsTable()
- {
- this.resultsScrollPosition = GUILayout.BeginScrollView(this.resultsScrollPosition, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
- this.grid.Draw();
- GUILayout.FlexibleSpace();
- GUILayout.EndScrollView();
- }
- /// <summary>
- /// Draws the list of test names.
- /// </summary>
- private void DrawTestNameList()
- {
- GUILayout.BeginVertical(GUILayout.MaxWidth(300));
- var local = Localization.LocalizationManager.Instance;
- // setup callback for setting check box values
- var setValues = new Action<bool>(value =>
- {
- foreach (var model in this.testModels)
- {
- foreach (var item in model.Value)
- {
- item.Tag = value;
- }
- }
- });
- GUILayout.BeginHorizontal(); // all none buttons
- if (GUILayout.Button(local.Get("Expand")))
- {
- foreach (var item in this.testModels)
- {
- item.Key.Expanded = true;
- }
- }
- if (GUILayout.Button(local.Get("Collapse")))
- {
- foreach (var item in this.testModels)
- {
- item.Key.Expanded = false;
- }
- }
- GUILayout.FlexibleSpace();
- if (GUILayout.Button(local.Get("All")))
- {
- setValues(true);
- }
- if (GUILayout.Button(local.Get("None")))
- {
- setValues(false);
- }
- GUILayout.EndHorizontal(); // all none buttons
- this.testsScrollPosition = GUILayout.BeginScrollView(this.testsScrollPosition, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(true));
- foreach (var item in this.testModels)
- {
- var category = Path.GetExtension(item.Key.Name);
- category = category != null && category.StartsWith(".") ? category.Substring(1) : category;
- GUILayout.BeginHorizontal();
- var all = item.Value.All(x => (bool)x.Tag);
- var result = GUILayout.Toggle(all, string.Empty, GUILayout.ExpandWidth(false));
- // draw test class
- item.Key.Expanded = EditorGUILayout.Foldout(item.Key.Expanded, category);
- GUILayout.EndHorizontal();
- if (all != result)
- {
- foreach (var model in item.Value)
- {
- model.Tag = result;
- }
- }
- if (!item.Key.Expanded)
- {
- // if not expanded then skip to next test class
- continue;
- }
- GUILayout.BeginHorizontal();
- GUILayout.Space(16);
- GUILayout.BeginVertical();
- // draw test methods
- foreach (var value in item.Value)
- {
- value.Tag = GUILayout.Toggle((bool)value.Tag, value.MethodName.Replace("_", " "), GUILayout.ExpandWidth(false));
- }
- GUILayout.EndVertical();
- GUILayout.EndHorizontal();
- }
- GUILayout.FlexibleSpace();
- GUILayout.EndScrollView();
- GUILayout.EndVertical();
- }
- /// <summary>
- /// Called by unity when the window is enabled.
- /// </summary>
- public void OnEnable()
- {
- this.testModels = Helpers.GetTestModels()
- .GroupBy(o => o.Reference.GetType().FullName)
- .ToDictionary(g => new KeyType() { Name = g.Key, Expanded = true }, g => g.Select(x =>
- {
- x.Tag = false;
- return x;
- }).ToList());
- }
- /// <summary>
- /// Used to initialize the window.
- /// </summary>
- [MenuItem("Window/Codefarts/Unit Testing")]
- public static void ShowWindow()
- {
- // get the window, show it, and hand it focus
- var window = GetWindow<UnitTestingWindow>(Localization.LocalizationManager.Instance.Get("UnitTesting"));
- window.Show();
- window.Focus();
- window.Repaint();
- }
- }
- }