TestTreeViewItem.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Reflection;
  3. using System.Text;
  4. using UnityEditor.IMGUI.Controls;
  5. using UnityEditor.TestTools.TestRunner.Api;
  6. namespace UnityEditor.TestTools.TestRunner.GUI
  7. {
  8. internal sealed class TestTreeViewItem : TreeViewItem
  9. {
  10. public TestRunnerResult result;
  11. internal ITestAdaptor m_Test;
  12. public Type type;
  13. public MethodInfo method;
  14. private const int k_ResultTestMaxLength = 15000;
  15. public bool IsGroupNode { get { return m_Test.IsSuite; } }
  16. public string FullName { get { return m_Test.FullName; } }
  17. public string GetAssemblyName()
  18. {
  19. var test = m_Test;
  20. while (test != null)
  21. {
  22. if (test.IsTestAssembly)
  23. {
  24. return test.FullName;
  25. }
  26. test = test.Parent;
  27. }
  28. return null;
  29. }
  30. public TestTreeViewItem(ITestAdaptor test, int depth, TreeViewItem parent)
  31. : base(GetId(test), depth, parent, test.Name)
  32. {
  33. m_Test = test;
  34. if (test.TypeInfo != null)
  35. {
  36. type = test.TypeInfo.Type;
  37. }
  38. if (test.Method != null)
  39. {
  40. method = test.Method.MethodInfo;
  41. }
  42. displayName = test.Name.Replace("\n", "");
  43. icon = Icons.s_UnknownImg;
  44. }
  45. private static int GetId(ITestAdaptor test)
  46. {
  47. return test.UniqueName.GetHashCode();
  48. }
  49. public void SetResult(TestRunnerResult testResult)
  50. {
  51. result = testResult;
  52. result.SetResultChangedCallback(ResultUpdated);
  53. ResultUpdated(result);
  54. }
  55. public string GetResultText()
  56. {
  57. if (result.resultStatus == TestRunnerResult.ResultStatus.NotRun)
  58. {
  59. return string.Empty;
  60. }
  61. var durationString = String.Format("{0:0.000}", result.duration);
  62. var sb = new StringBuilder(string.Format("{0} ({1}s)", displayName.Trim(), durationString));
  63. if (!string.IsNullOrEmpty(result.description))
  64. {
  65. sb.AppendFormat("\n{0}", result.description);
  66. }
  67. if (!string.IsNullOrEmpty(result.messages))
  68. {
  69. sb.Append("\n---\n");
  70. sb.Append(result.messages.Trim());
  71. }
  72. if (!string.IsNullOrEmpty(result.stacktrace))
  73. {
  74. sb.Append("\n---\n");
  75. sb.Append(result.stacktrace.Trim());
  76. }
  77. if (!string.IsNullOrEmpty(result.output))
  78. {
  79. sb.Append("\n---\n");
  80. sb.Append(result.output.Trim());
  81. }
  82. if (sb.Length > k_ResultTestMaxLength)
  83. {
  84. sb.Length = k_ResultTestMaxLength;
  85. sb.AppendFormat("...\n\n---MESSAGE TRUNCATED AT {0} CHARACTERS---", k_ResultTestMaxLength);
  86. }
  87. return sb.ToString().Trim();
  88. }
  89. private void ResultUpdated(TestRunnerResult testResult)
  90. {
  91. switch (testResult.resultStatus)
  92. {
  93. case TestRunnerResult.ResultStatus.Passed:
  94. icon = Icons.s_SuccessImg;
  95. break;
  96. case TestRunnerResult.ResultStatus.Failed:
  97. icon = Icons.s_FailImg;
  98. break;
  99. case TestRunnerResult.ResultStatus.Inconclusive:
  100. icon = Icons.s_InconclusiveImg;
  101. break;
  102. case TestRunnerResult.ResultStatus.Skipped:
  103. icon = Icons.s_IgnoreImg;
  104. break;
  105. default:
  106. if (testResult.ignoredOrSkipped)
  107. {
  108. icon = Icons.s_IgnoreImg;
  109. }
  110. else if (testResult.notRunnable)
  111. {
  112. icon = Icons.s_FailImg;
  113. }
  114. else
  115. {
  116. icon = Icons.s_UnknownImg;
  117. }
  118. break;
  119. }
  120. }
  121. }
  122. }