PageRenderTime 60ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/Assets/UnityTestTools/IntegrationTestsFramework/TestRunner/Editor/PlatformRunner/PlatformRunnerSettingsWindow.cs

https://gitlab.com/wizcas/Unity-GitLab-CI-Test
C# | 318 lines | 245 code | 61 blank | 12 comment | 42 complexity | 10a3c1dc57cce5e427672cce34b68c85 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using UnityEditor;
  7. using UnityEngine;
  8. using Object = UnityEngine.Object;
  9. using UnityEngine.SceneManagement;
  10. namespace UnityTest.IntegrationTests
  11. {
  12. [Serializable]
  13. public class PlatformRunnerSettingsWindow : EditorWindow
  14. {
  15. private BuildTarget m_BuildTarget;
  16. private List<string> m_IntegrationTestScenes;
  17. private List<string> m_OtherScenesToBuild;
  18. private List<string> m_AllScenesInProject;
  19. private Vector2 m_ScrollPosition;
  20. private readonly List<string> m_Interfaces = new List<string>();
  21. private readonly List<string> m_SelectedScenes = new List<string>();
  22. private int m_SelectedInterface;
  23. [SerializeField]
  24. private bool m_AdvancedNetworkingSettings;
  25. private PlatformRunnerSettings m_Settings;
  26. private string m_SelectedSceneInAll;
  27. private string m_SelectedSceneInTest;
  28. private string m_SelectedSceneInBuild;
  29. readonly GUIContent m_Label = new GUIContent("Results target directory", "Directory where the results will be saved. If no value is specified, the results will be generated in project's data folder.");
  30. public PlatformRunnerSettingsWindow()
  31. {
  32. if (m_OtherScenesToBuild == null)
  33. m_OtherScenesToBuild = new List<string> ();
  34. if (m_IntegrationTestScenes == null)
  35. m_IntegrationTestScenes = new List<string> ();
  36. titleContent = new GUIContent("Platform runner");
  37. m_BuildTarget = PlatformRunner.defaultBuildTarget;
  38. position.Set(position.xMin, position.yMin, 200, position.height);
  39. m_AllScenesInProject = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.unity", SearchOption.AllDirectories).ToList();
  40. m_AllScenesInProject.Sort();
  41. var currentScene = (Directory.GetCurrentDirectory() + SceneManager.GetActiveScene().path).Replace("\\", "").Replace("/", "");
  42. var currentScenePath = m_AllScenesInProject.Where(s => s.Replace("\\", "").Replace("/", "") == currentScene);
  43. m_SelectedScenes.AddRange(currentScenePath);
  44. m_Interfaces.Add("(Any)");
  45. m_Interfaces.AddRange(TestRunnerConfigurator.GetAvailableNetworkIPs());
  46. m_Interfaces.Add("127.0.0.1");
  47. LoadFromPrefereneces ();
  48. }
  49. public void OnEnable()
  50. {
  51. m_Settings = ProjectSettingsBase.Load<PlatformRunnerSettings>();
  52. // If not configured pre populate with all scenes that have test components on game objects
  53. // This needs to be done outsie of constructor
  54. if (m_IntegrationTestScenes.Count == 0)
  55. m_IntegrationTestScenes = GetScenesWithTestComponents (m_AllScenesInProject);
  56. }
  57. public void OnGUI()
  58. {
  59. EditorGUILayout.BeginVertical();
  60. GUIContent label;
  61. /* We have three lists here, The tests to run, supporting scenes to include in the build and the list of all scenes so users can
  62. * pick the scenes they want to include. The motiviation here is that test scenes may require to additively load other scenes as part of the tests
  63. */
  64. EditorGUILayout.BeginHorizontal ();
  65. // Integration Tests To Run
  66. EditorGUILayout.BeginVertical ();
  67. label = new GUIContent("Tests:", "All Integration Test scenes that you wish to run on the platform");
  68. EditorGUILayout.LabelField(label, EditorStyles.boldLabel, GUILayout.Height(20f));
  69. EditorGUI.BeginDisabledGroup(string.IsNullOrEmpty(m_SelectedSceneInTest));
  70. if (GUILayout.Button("Remove Integration Test")) {
  71. m_IntegrationTestScenes.Remove(m_SelectedSceneInTest);
  72. m_SelectedSceneInTest = "";
  73. }
  74. EditorGUI.EndDisabledGroup();
  75. DrawVerticalSceneList (ref m_IntegrationTestScenes, ref m_SelectedSceneInTest);
  76. EditorGUILayout.EndVertical ();
  77. // Extra scenes to include in build
  78. EditorGUILayout.BeginVertical ();
  79. label = new GUIContent("Other Scenes in Build:", "If your Integration Tests additivly load any other scenes then you want to include them here so they are part of the build");
  80. EditorGUILayout.LabelField(label, EditorStyles.boldLabel, GUILayout.Height(20f));
  81. EditorGUI.BeginDisabledGroup(string.IsNullOrEmpty(m_SelectedSceneInBuild));
  82. if (GUILayout.Button("Remove From Build")) {
  83. m_OtherScenesToBuild.Remove(m_SelectedSceneInBuild);
  84. m_SelectedSceneInBuild = "";
  85. }
  86. EditorGUI.EndDisabledGroup();
  87. DrawVerticalSceneList (ref m_OtherScenesToBuild, ref m_SelectedSceneInBuild);
  88. EditorGUILayout.EndVertical ();
  89. EditorGUILayout.Separator ();
  90. // All Scenes
  91. EditorGUILayout.BeginVertical ();
  92. label = new GUIContent("Availble Scenes", "These are all the scenes within your project, please select some to run tests");
  93. EditorGUILayout.LabelField(label, EditorStyles.boldLabel, GUILayout.Height(20f));
  94. EditorGUILayout.BeginHorizontal ();
  95. EditorGUI.BeginDisabledGroup(string.IsNullOrEmpty(m_SelectedSceneInAll));
  96. if (GUILayout.Button("Add As Test")) {
  97. if (!m_IntegrationTestScenes.Contains (m_SelectedSceneInAll) && !m_OtherScenesToBuild.Contains (m_SelectedSceneInAll)) {
  98. m_IntegrationTestScenes.Add(m_SelectedSceneInAll);
  99. }
  100. }
  101. if (GUILayout.Button("Add to Build")) {
  102. if (!m_IntegrationTestScenes.Contains (m_SelectedSceneInAll) && !m_OtherScenesToBuild.Contains (m_SelectedSceneInAll)) {
  103. m_OtherScenesToBuild.Add(m_SelectedSceneInAll);
  104. }
  105. }
  106. EditorGUI.EndDisabledGroup();
  107. EditorGUILayout.EndHorizontal ();
  108. DrawVerticalSceneList (ref m_AllScenesInProject, ref m_SelectedSceneInAll);
  109. EditorGUILayout.EndVertical ();
  110. // ButtoNetworkResultsReceiverns to edit scenes in lists
  111. EditorGUILayout.EndHorizontal ();
  112. GUILayout.Space(3);
  113. // Select target platform
  114. m_BuildTarget = (BuildTarget)EditorGUILayout.EnumPopup("Build tests for", m_BuildTarget);
  115. if (PlatformRunner.defaultBuildTarget != m_BuildTarget)
  116. {
  117. if (GUILayout.Button("Make default target platform"))
  118. {
  119. PlatformRunner.defaultBuildTarget = m_BuildTarget;
  120. }
  121. }
  122. GUI.enabled = true;
  123. // Select various Network settings
  124. DrawSetting();
  125. var build = GUILayout.Button("Build and run tests");
  126. EditorGUILayout.EndVertical();
  127. if (build)
  128. {
  129. BuildAndRun ();
  130. }
  131. }
  132. private void DrawVerticalSceneList(ref List<string> sourceList, ref string selectString)
  133. {
  134. m_ScrollPosition = EditorGUILayout.BeginScrollView(m_ScrollPosition, Styles.testList);
  135. EditorGUI.indentLevel++;
  136. foreach (var scenePath in sourceList)
  137. {
  138. var path = Path.GetFileNameWithoutExtension(scenePath);
  139. var guiContent = new GUIContent(path, scenePath);
  140. var rect = GUILayoutUtility.GetRect(guiContent, EditorStyles.label);
  141. if (rect.Contains(Event.current.mousePosition))
  142. {
  143. if (Event.current.type == EventType.mouseDown && Event.current.button == 0)
  144. {
  145. selectString = scenePath;
  146. Event.current.Use();
  147. }
  148. }
  149. var style = new GUIStyle(EditorStyles.label);
  150. if (selectString == scenePath)
  151. style.normal.textColor = new Color(0.3f, 0.5f, 0.85f);
  152. EditorGUI.LabelField(rect, guiContent, style);
  153. }
  154. EditorGUI.indentLevel--;
  155. EditorGUILayout.EndScrollView();
  156. }
  157. public static List<string> GetScenesWithTestComponents(List<string> allScenes)
  158. {
  159. List<Object> results = EditorReferencesUtil.FindScenesWhichContainAsset("TestComponent.cs");
  160. List<string> integrationTestScenes = new List<string>();
  161. foreach (Object obj in results) {
  162. string result = allScenes.FirstOrDefault(s => s.Contains(obj.name));
  163. if (!string.IsNullOrEmpty(result))
  164. integrationTestScenes.Add(result);
  165. }
  166. return integrationTestScenes;
  167. }
  168. private void DrawSetting()
  169. {
  170. EditorGUI.BeginChangeCheck();
  171. EditorGUILayout.BeginHorizontal();
  172. m_Settings.resultsPath = EditorGUILayout.TextField(m_Label, m_Settings.resultsPath);
  173. if (GUILayout.Button("Search", EditorStyles.miniButton, GUILayout.Width(50)))
  174. {
  175. var selectedPath = EditorUtility.SaveFolderPanel("Result files destination", m_Settings.resultsPath, "");
  176. if (!string.IsNullOrEmpty(selectedPath))
  177. m_Settings.resultsPath = Path.GetFullPath(selectedPath);
  178. }
  179. EditorGUILayout.EndHorizontal();
  180. if (!string.IsNullOrEmpty(m_Settings.resultsPath))
  181. {
  182. Uri uri;
  183. if (!Uri.TryCreate(m_Settings.resultsPath, UriKind.Absolute, out uri) || !uri.IsFile || uri.IsWellFormedOriginalString())
  184. {
  185. EditorGUILayout.HelpBox("Invalid URI path", MessageType.Warning);
  186. }
  187. }
  188. m_Settings.sendResultsOverNetwork = EditorGUILayout.Toggle("Send results to editor", m_Settings.sendResultsOverNetwork);
  189. EditorGUI.BeginDisabledGroup(!m_Settings.sendResultsOverNetwork);
  190. m_AdvancedNetworkingSettings = EditorGUILayout.Foldout(m_AdvancedNetworkingSettings, "Advanced network settings");
  191. if (m_AdvancedNetworkingSettings)
  192. {
  193. m_SelectedInterface = EditorGUILayout.Popup("Network interface", m_SelectedInterface, m_Interfaces.ToArray());
  194. EditorGUI.BeginChangeCheck();
  195. m_Settings.port = EditorGUILayout.IntField("Network port", m_Settings.port);
  196. if (EditorGUI.EndChangeCheck())
  197. {
  198. if (m_Settings.port > IPEndPoint.MaxPort)
  199. m_Settings.port = IPEndPoint.MaxPort;
  200. else if (m_Settings.port < IPEndPoint.MinPort)
  201. m_Settings.port = IPEndPoint.MinPort;
  202. }
  203. }
  204. EditorGUI.EndDisabledGroup();
  205. if (EditorGUI.EndChangeCheck())
  206. {
  207. m_Settings.Save();
  208. }
  209. }
  210. private void BuildAndRun()
  211. {
  212. SaveToPreferences ();
  213. var config = new PlatformRunnerConfiguration
  214. {
  215. buildTarget = m_BuildTarget,
  216. buildScenes = m_OtherScenesToBuild,
  217. testScenes = m_IntegrationTestScenes,
  218. projectName = m_IntegrationTestScenes.Count > 1 ? "IntegrationTests" : Path.GetFileNameWithoutExtension(SceneManager.GetActiveScene().path),
  219. resultsDir = m_Settings.resultsPath,
  220. sendResultsOverNetwork = m_Settings.sendResultsOverNetwork,
  221. ipList = m_Interfaces.Skip(1).ToList(),
  222. port = m_Settings.port
  223. };
  224. if (m_SelectedInterface > 0)
  225. config.ipList = new List<string> {m_Interfaces.ElementAt(m_SelectedInterface)};
  226. PlatformRunner.BuildAndRunInPlayer(config);
  227. Close ();
  228. }
  229. public void OnLostFocus() {
  230. SaveToPreferences ();
  231. }
  232. public void OnDestroy() {
  233. SaveToPreferences ();
  234. }
  235. private void SaveToPreferences()
  236. {
  237. EditorPrefs.SetString (Animator.StringToHash (Application.dataPath + "uttTestScenes").ToString (), String.Join (",",m_IntegrationTestScenes.ToArray()));
  238. EditorPrefs.SetString (Animator.StringToHash (Application.dataPath + "uttBuildScenes").ToString (), String.Join (",",m_OtherScenesToBuild.ToArray()));
  239. }
  240. private void LoadFromPrefereneces()
  241. {
  242. string storedTestScenes = EditorPrefs.GetString (Animator.StringToHash (Application.dataPath + "uttTestScenes").ToString ());
  243. string storedBuildScenes = EditorPrefs.GetString (Animator.StringToHash (Application.dataPath + "uttBuildScenes").ToString ());
  244. List<string> parsedTestScenes = storedTestScenes.Split (',').ToList ();
  245. List<string> parsedBuildScenes = storedBuildScenes.Split (',').ToList ();
  246. // Sanity check scenes actually exist
  247. foreach (string str in parsedTestScenes) {
  248. if (m_AllScenesInProject.Contains(str))
  249. m_IntegrationTestScenes.Add(str);
  250. }
  251. foreach (string str in parsedBuildScenes) {
  252. if (m_AllScenesInProject.Contains(str))
  253. m_OtherScenesToBuild.Add(str);
  254. }
  255. }
  256. }
  257. }