TestListGuiHelper.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using UnityEditor.ProjectWindowCallback;
  5. using UnityEditor.Scripting.ScriptCompilation;
  6. using UnityEngine;
  7. namespace UnityEditor.TestTools.TestRunner.GUI
  8. {
  9. internal class TestListGUIHelper
  10. {
  11. private const string kResourcesTemplatePath = "Resources/ScriptTemplates";
  12. private const string kAssemblyDefinitionTestTemplate = "92-Assembly Definition-NewTestAssembly.asmdef.txt";
  13. private const string kAssemblyDefinitionEditModeTestTemplate =
  14. "92-Assembly Definition-NewEditModeTestAssembly.asmdef.txt";
  15. private const string kTestScriptTemplate = "83-C# Script-NewTestScript.cs.txt";
  16. private const string kNewTestScriptName = "NewTestScript.cs";
  17. private const string kNunit = "nunit.framework.dll";
  18. [MenuItem("Assets/Create/Testing/Tests Assembly Folder", false, 83)]
  19. public static void MenuItemAddFolderAndAsmDefForTesting()
  20. {
  21. AddFolderAndAsmDefForTesting();
  22. }
  23. [MenuItem("Assets/Create/Testing/Tests Assembly Folder", true, 83)]
  24. public static bool MenuItemAddFolderAndAsmDefForTestingWithValidation()
  25. {
  26. return !SelectedFolderContainsTestAssembly();
  27. }
  28. public static void AddFolderAndAsmDefForTesting(bool isEditorOnly = false)
  29. {
  30. ProjectWindowUtil.CreateFolderWithTemplates("Tests",
  31. isEditorOnly ? kAssemblyDefinitionEditModeTestTemplate : kAssemblyDefinitionTestTemplate);
  32. }
  33. public static bool SelectedFolderContainsTestAssembly()
  34. {
  35. var theNearestCustomScriptAssembly = GetTheNearestCustomScriptAssembly();
  36. if (theNearestCustomScriptAssembly != null)
  37. {
  38. return theNearestCustomScriptAssembly.PrecompiledReferences != null && theNearestCustomScriptAssembly.PrecompiledReferences.Any(x => Path.GetFileName(x) == kNunit);
  39. }
  40. return false;
  41. }
  42. [MenuItem("Assets/Create/Testing/C# Test Script", false, 83)]
  43. public static void AddTest()
  44. {
  45. var basePath = Path.Combine(EditorApplication.applicationContentsPath, kResourcesTemplatePath);
  46. var destPath = Path.Combine(GetActiveFolderPath(), kNewTestScriptName);
  47. var templatePath = Path.Combine(basePath, kTestScriptTemplate);
  48. var icon = EditorGUIUtility.IconContent("cs Script Icon").image as Texture2D;
  49. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,
  50. ScriptableObject.CreateInstance<DoCreateScriptAsset>(), destPath, icon, templatePath);
  51. AssetDatabase.Refresh();
  52. }
  53. [MenuItem("Assets/Create/Testing/C# Test Script", true, 83)]
  54. public static bool CanAddScriptAndItWillCompile()
  55. {
  56. return CanAddEditModeTestScriptAndItWillCompile() || CanAddPlayModeTestScriptAndItWillCompile();
  57. }
  58. public static bool CanAddEditModeTestScriptAndItWillCompile()
  59. {
  60. var theNearestCustomScriptAssembly = GetTheNearestCustomScriptAssembly();
  61. if (theNearestCustomScriptAssembly != null)
  62. {
  63. return (theNearestCustomScriptAssembly.AssemblyFlags & AssemblyFlags.EditorOnly) ==
  64. AssemblyFlags.EditorOnly;
  65. }
  66. var activeFolderPath = GetActiveFolderPath();
  67. return activeFolderPath.ToLower().Contains("/editor");
  68. }
  69. public static bool CanAddPlayModeTestScriptAndItWillCompile()
  70. {
  71. if (PlayerSettings.playModeTestRunnerEnabled)
  72. {
  73. return true;
  74. }
  75. var theNearestCustomScriptAssembly = GetTheNearestCustomScriptAssembly();
  76. if (theNearestCustomScriptAssembly == null)
  77. {
  78. return false;
  79. }
  80. var hasTestAssemblyFlag = theNearestCustomScriptAssembly.PrecompiledReferences != null && theNearestCustomScriptAssembly.PrecompiledReferences.Any(x => Path.GetFileName(x) == kNunit);;
  81. var editorOnlyAssembly = (theNearestCustomScriptAssembly.AssemblyFlags & AssemblyFlags.EditorOnly) != 0;
  82. return hasTestAssemblyFlag && !editorOnlyAssembly;
  83. }
  84. public static string GetActiveFolderPath()
  85. {
  86. var path = "Assets";
  87. foreach (var obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets))
  88. {
  89. path = AssetDatabase.GetAssetPath(obj);
  90. if (!string.IsNullOrEmpty(path) && File.Exists(path))
  91. {
  92. path = Path.GetDirectoryName(path);
  93. break;
  94. }
  95. }
  96. return path;
  97. }
  98. private static CustomScriptAssembly GetTheNearestCustomScriptAssembly()
  99. {
  100. CustomScriptAssembly findCustomScriptAssemblyFromScriptPath;
  101. try
  102. {
  103. findCustomScriptAssemblyFromScriptPath =
  104. EditorCompilationInterface.Instance.FindCustomScriptAssemblyFromScriptPath(
  105. Path.Combine(GetActiveFolderPath(), "Foo.cs"));
  106. }
  107. catch (Exception)
  108. {
  109. return null;
  110. }
  111. return findCustomScriptAssemblyFromScriptPath;
  112. }
  113. }
  114. }