TestResultRenderer.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using NUnit.Framework.Interfaces;
  4. using NUnit.Framework.Internal;
  5. namespace UnityEngine.TestTools.TestRunner.Callbacks
  6. {
  7. internal class TestResultRenderer
  8. {
  9. private static class Styles
  10. {
  11. public static readonly GUIStyle SucceedLabelStyle;
  12. public static readonly GUIStyle FailedLabelStyle;
  13. public static readonly GUIStyle FailedMessagesStyle;
  14. static Styles()
  15. {
  16. SucceedLabelStyle = new GUIStyle("label");
  17. SucceedLabelStyle.normal.textColor = Color.green;
  18. SucceedLabelStyle.fontSize = 48;
  19. FailedLabelStyle = new GUIStyle("label");
  20. FailedLabelStyle.normal.textColor = Color.red;
  21. FailedLabelStyle.fontSize = 32;
  22. FailedMessagesStyle = new GUIStyle("label");
  23. FailedMessagesStyle.wordWrap = false;
  24. FailedMessagesStyle.richText = true;
  25. }
  26. }
  27. private readonly List<ITestResult> m_FailedTestCollection;
  28. private bool m_ShowResults;
  29. private Vector2 m_ScrollPosition;
  30. public TestResultRenderer(ITestResult testResults)
  31. {
  32. m_FailedTestCollection = new List<ITestResult>();
  33. GetFailedTests(testResults);
  34. }
  35. private void GetFailedTests(ITestResult testResults)
  36. {
  37. if (testResults is TestCaseResult)
  38. {
  39. if (testResults.ResultState.Status == TestStatus.Failed)
  40. m_FailedTestCollection.Add(testResults);
  41. }
  42. else if (testResults.HasChildren)
  43. {
  44. foreach (var testResultsChild in testResults.Children)
  45. {
  46. GetFailedTests(testResultsChild);
  47. }
  48. }
  49. }
  50. private const int k_MaxStringLength = 15000;
  51. public void ShowResults()
  52. {
  53. m_ShowResults = true;
  54. Cursor.visible = true;
  55. }
  56. public void Draw()
  57. {
  58. if (!m_ShowResults) return;
  59. if (m_FailedTestCollection.Count == 0)
  60. {
  61. GUILayout.Label("All test(s) succeeded", Styles.SucceedLabelStyle, GUILayout.Width(600));
  62. }
  63. else
  64. {
  65. int count = m_FailedTestCollection.Count;
  66. GUILayout.Label(count + " tests failed!", Styles.FailedLabelStyle);
  67. m_ScrollPosition = GUILayout.BeginScrollView(m_ScrollPosition, GUILayout.ExpandWidth(true));
  68. var text = "";
  69. text += "<b><size=18>Code-based tests</size></b>\n";
  70. text += string.Join("\n", m_FailedTestCollection
  71. .Select(result => result.Name + " " + result.ResultState + "\n" + result.Message)
  72. .ToArray());
  73. if (text.Length > k_MaxStringLength)
  74. text = text.Substring(0, k_MaxStringLength);
  75. GUILayout.TextArea(text, Styles.FailedMessagesStyle);
  76. GUILayout.EndScrollView();
  77. }
  78. if (GUILayout.Button("Close"))
  79. Application.Quit();
  80. }
  81. }
  82. }