/Assets/UnityTestTools/Assertions/Editor/AssertionComponentEditor.cs
C# | 226 lines | 200 code | 26 blank | 0 comment | 32 complexity | 601b111a8ffe1b5b8c13cc3b79f6b270 MD5 | raw file
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5using UnityEditor;
6using UnityEngine;
7using UnityEditor.SceneManagement;
8
9namespace UnityTest
10{
11 [CustomEditor(typeof(AssertionComponent))]
12 public class AssertionComponentEditor : Editor
13 {
14 private readonly DropDownControl<Type> m_ComparerDropDown = new DropDownControl<Type>();
15
16 private readonly PropertyPathSelector m_ThisPathSelector = new PropertyPathSelector("Compare");
17 private readonly PropertyPathSelector m_OtherPathSelector = new PropertyPathSelector("Compare to");
18
19 #region GUI Contents
20 private readonly GUIContent m_GUICheckAfterTimeGuiContent = new GUIContent("Check after (seconds)", "After how many seconds the assertion should be checked");
21 private readonly GUIContent m_GUIRepeatCheckTimeGuiContent = new GUIContent("Repeat check", "Should the check be repeated.");
22 private readonly GUIContent m_GUIRepeatEveryTimeGuiContent = new GUIContent("Frequency of repetitions", "How often should the check be done");
23 private readonly GUIContent m_GUICheckAfterFramesGuiContent = new GUIContent("Check after (frames)", "After how many frames the assertion should be checked");
24 private readonly GUIContent m_GUIRepeatCheckFrameGuiContent = new GUIContent("Repeat check", "Should the check be repeated.");
25 #endregion
26
27 private static List<Type> allComparersList = null;
28
29 public AssertionComponentEditor()
30 {
31 m_ComparerDropDown.convertForButtonLabel = type => type.Name;
32 m_ComparerDropDown.convertForGUIContent = type => type.Name;
33 m_ComparerDropDown.ignoreConvertForGUIContent = types => false;
34 m_ComparerDropDown.tooltip = "Comparer that will be used to compare values and determine the result of assertion.";
35 }
36
37 public override void OnInspectorGUI()
38 {
39 var script = (AssertionComponent)target;
40 EditorGUILayout.BeginHorizontal();
41 var obj = DrawComparerSelection(script);
42 script.checkMethods = (CheckMethod)EditorGUILayout.EnumMaskField(script.checkMethods,
43 EditorStyles.popup,
44 GUILayout.ExpandWidth(false));
45 EditorGUILayout.EndHorizontal();
46
47 if (script.IsCheckMethodSelected(CheckMethod.AfterPeriodOfTime))
48 {
49 DrawOptionsForAfterPeriodOfTime(script);
50 }
51
52 if (script.IsCheckMethodSelected(CheckMethod.Update))
53 {
54 DrawOptionsForOnUpdate(script);
55 }
56
57 if (obj)
58 {
59 EditorGUILayout.Space();
60
61 m_ThisPathSelector.Draw(script.Action.go, script.Action,
62 script.Action.thisPropertyPath, script.Action.GetAccepatbleTypesForA(),
63 go =>
64 {
65 script.Action.go = go;
66 AssertionExplorerWindow.Reload();
67 },
68 s =>
69 {
70 script.Action.thisPropertyPath = s;
71 AssertionExplorerWindow.Reload();
72 });
73
74 EditorGUILayout.Space();
75
76 DrawCustomFields(script);
77
78 EditorGUILayout.Space();
79
80 if (script.Action is ComparerBase)
81 {
82 DrawCompareToType(script.Action as ComparerBase);
83 }
84 }
85 if(GUI.changed)
86 EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
87 }
88
89 private void DrawOptionsForAfterPeriodOfTime(AssertionComponent script)
90 {
91 EditorGUILayout.Space();
92 script.checkAfterTime = EditorGUILayout.FloatField(m_GUICheckAfterTimeGuiContent,
93 script.checkAfterTime);
94 if (script.checkAfterTime < 0)
95 script.checkAfterTime = 0;
96 script.repeatCheckTime = EditorGUILayout.Toggle(m_GUIRepeatCheckTimeGuiContent,
97 script.repeatCheckTime);
98 if (script.repeatCheckTime)
99 {
100 script.repeatEveryTime = EditorGUILayout.FloatField(m_GUIRepeatEveryTimeGuiContent,
101 script.repeatEveryTime);
102 if (script.repeatEveryTime < 0)
103 script.repeatEveryTime = 0;
104 }
105 }
106
107 private void DrawOptionsForOnUpdate(AssertionComponent script)
108 {
109 EditorGUILayout.Space();
110 script.checkAfterFrames = EditorGUILayout.IntField(m_GUICheckAfterFramesGuiContent,
111 script.checkAfterFrames);
112 if (script.checkAfterFrames < 1)
113 script.checkAfterFrames = 1;
114 script.repeatCheckFrame = EditorGUILayout.Toggle(m_GUIRepeatCheckFrameGuiContent,
115 script.repeatCheckFrame);
116 if (script.repeatCheckFrame)
117 {
118 script.repeatEveryFrame = EditorGUILayout.IntField(m_GUIRepeatEveryTimeGuiContent,
119 script.repeatEveryFrame);
120 if (script.repeatEveryFrame < 1)
121 script.repeatEveryFrame = 1;
122 }
123 }
124
125 private void DrawCompareToType(ComparerBase comparer)
126 {
127 comparer.compareToType = (ComparerBase.CompareToType)EditorGUILayout.EnumPopup("Compare to type",
128 comparer.compareToType,
129 EditorStyles.popup);
130
131 if (comparer.compareToType == ComparerBase.CompareToType.CompareToConstantValue)
132 {
133 try
134 {
135 DrawConstCompareField(comparer);
136 }
137 catch (NotImplementedException)
138 {
139 Debug.LogWarning("This comparer can't compare to static value");
140 comparer.compareToType = ComparerBase.CompareToType.CompareToObject;
141 }
142 }
143 else if (comparer.compareToType == ComparerBase.CompareToType.CompareToObject)
144 {
145 DrawObjectCompareField(comparer);
146 }
147 }
148
149 private void DrawObjectCompareField(ComparerBase comparer)
150 {
151 m_OtherPathSelector.Draw(comparer.other, comparer,
152 comparer.otherPropertyPath, comparer.GetAccepatbleTypesForB(),
153 go =>
154 {
155 comparer.other = go;
156 AssertionExplorerWindow.Reload();
157 },
158 s =>
159 {
160 comparer.otherPropertyPath = s;
161 AssertionExplorerWindow.Reload();
162 }
163 );
164 }
165
166 private void DrawConstCompareField(ComparerBase comparer)
167 {
168 if (comparer.ConstValue == null)
169 {
170 comparer.ConstValue = comparer.GetDefaultConstValue();
171 }
172
173 var so = new SerializedObject(comparer);
174 var sp = so.FindProperty("constantValueGeneric");
175 if (sp != null)
176 {
177 EditorGUILayout.PropertyField(sp, new GUIContent("Constant"), true);
178 so.ApplyModifiedProperties();
179 }
180 }
181
182 private bool DrawComparerSelection(AssertionComponent script)
183 {
184 if(allComparersList == null)
185 {
186 allComparersList = new List<Type>();
187 var allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
188 foreach (var assembly in allAssemblies)
189 {
190 var types = assembly.GetTypes();
191 allComparersList.AddRange(types.Where(type => type.IsSubclassOf(typeof(ActionBase)) && !type.IsAbstract));
192 }
193 }
194 var allComparers = allComparersList.ToArray();
195
196 if (script.Action == null)
197 script.Action = (ActionBase)CreateInstance(allComparers.First());
198
199 m_ComparerDropDown.Draw(script.Action.GetType(), allComparers,
200 type =>
201 {
202 if (script.Action == null || script.Action.GetType().Name != type.Name)
203 {
204 script.Action = (ActionBase)CreateInstance(type);
205 AssertionExplorerWindow.Reload();
206 }
207 });
208
209 return script.Action != null;
210 }
211
212 private void DrawCustomFields(AssertionComponent script)
213 {
214 foreach (var prop in script.Action.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
215 {
216 var so = new SerializedObject(script.Action);
217 var sp = so.FindProperty(prop.Name);
218 if (sp != null)
219 {
220 EditorGUILayout.PropertyField(sp, true);
221 so.ApplyModifiedProperties();
222 }
223 }
224 }
225 }
226}