CrabUI
Loading...
Searching...
No Matches
AutomaticTestRunnerGUI.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5using System.Diagnostics;
6using System.Runtime.CompilerServices;
7using System.IO;
8
9using Barotrauma;
10using Microsoft.Xna.Framework;
11using Microsoft.Xna.Framework.Input;
12using Microsoft.Xna.Framework.Graphics;
13using HarmonyLib;
14using CrabUI;
15using System.Text;
16using System.Threading;
17using Barotrauma.Extensions;
18
19namespace CrabUITest
20{
21 public class AutoTestGUI
22 {
23 public class TestButton : CUIHorizontalList
24 {
25 public CUIButton RunButton;
26 public CUIButton StashButton;
27 public CUIToggleButton AutorunButton;
28
29 public string Name
30 {
31 get => RunButton.Text;
32 set => RunButton.Text = value;
33 }
34
35 public void UseReport(TestReport report)
36 {
37 RunButton.MasterColor = report.Status switch
38 {
39 TestStatus.Passed => Color.Lime,
40 TestStatus.Unknown => Color.Yellow,
41 TestStatus.Stashed => Color.Lime,
42 TestStatus.Failed => Color.Red,
43 _ => Color.White,
44 };
45 }
46
47 public TestButton()
48 {
49 FitContent = new CUIBool2(true, true);
50
51 this["RunButton"] = RunButton = new CUIButton()
52 {
53 FillEmptySpace = new CUIBool2(true, false),
54 TextAlign = CUIAnchor.TopLeft,
55 Style = new CUIStyle() {
56 { "MasterColor", "128,128,128" },
57 { "TextAlign", "[0,0]" },
58 { "Border", "Transparent" },
59 },
60 };
61
62 this["autorun"] = AutorunButton = new CUIToggleButton("A")
63 {
64 AbsoluteMin = new CUINullRect(w: 20),
65 AddOnStateChange = (state) =>
66 {
67 DispatchUp(new CUICommand("set autorun", state ? Name : ""));
68 },
69 Style = new CUIStyle() {
70 { "Border", "Transparent" },
71 { "OnColor", "0,200,0"},
72 { "OnHoverColor", "0,255,255"},
73 },
74 };
75
76 this["StashButton"] = StashButton = new CUIButton()
77 {
78 Text = "stash",
79 Style = new CUIStyle() {
80 { "MasterColor", "255,0,255" },
81 { "Border", "Transparent" },
82 },
83 };
84
85 IgnoreDebug = true;
86 }
87
88 public TestButton(string name) : this()
89 {
90 Name = name;
91 }
92 }
93
94
95 public AutomaticTestRunner Runner;
96
97 public CUIButton OpenButton;
98 public CUIFrame MainFrame;
99
100 public CUIVerticalList CUITestList;
101
102 public Dictionary<MethodInfo, TestButton> TestButtons = new();
103
104 public bool Opened
105 {
106 get => MainFrame.Revealed;
107 set
108 {
109 OpenButton.Revealed = !value;
110 MainFrame.Revealed = value;
111 }
112 }
113
114 public void UseConfig(TestingConfig config)
115 {
116 if (config.Autorun == "") return;
117 (Type testClass, MethodInfo testMethod) = AutomaticTestRunner.DeconstructFullName(config.Autorun);
118
119 if (TestButtons.ContainsKey(testMethod))
120 {
121 if (!TestButtons[testMethod].AutorunButton.State)
122 {
123 TestButtons[testMethod].AutorunButton.State = true;
124 }
125 }
126 }
127
128 public void FindTests()
129 {
130 TestButtons?.Clear();
131 CUITestList?.RemoveAllChildren();
132
133 foreach (Type testClass in Runner.GetAllTestClasses())
134 {
135 foreach (MethodInfo testMethod in Runner.GetAllTestMethodsForAClass(testClass))
136 {
137 TestButtons[testMethod] = new TestButton($"{testClass.Name}.{testMethod.Name}");
138 TestButtons[testMethod].RunButton.OnMouseDown += (e) => Runner.RunTest(testClass, testMethod);
139 TestButtons[testMethod].StashButton.OnMouseDown += (e) => Runner.StashTest(testClass, testMethod);
140
141 CUITestList.Append(TestButtons[testMethod]);
142 }
143 }
144 }
145
146 public void CreateUI()
147 {
148 OpenButton = new CUIButton("TEST")
149 {
150 Vertical = true,
151 Anchor = CUIAnchor.CenterRight,
152 AddOnMouseDown = (e) => Opened = !Opened,
153 Font = GUIStyle.MonospacedFont,
154 ZIndex = 1001,
155 AKA = "AutoTest open button",
156 };
157
158 MainFrame = new CUIFrame()
159 {
160 Anchor = CUIAnchor.CenterRight,
161 Relative = new CUINullRect(0, 0, 0.2f, 0.4f),
162 ZIndex = 1000,
163 AKA = "AutoTest MainFrame"
164 };
165
166 MainFrame.SaveStateAs("init");
167 MainFrame.OnDClick += (e) => MainFrame.LoadState("init");
168 MainFrame.Revealed = false;
169
170
171 MainFrame["list"] = new CUIVerticalList()
172 {
173 Relative = new CUINullRect(0, 0, 1, 1),
174 };
175
176 MainFrame["list"]["header"] = new CUIHorizontalList()
177 {
178 Absolute = new CUINullRect(h: 20),
179 };
180
181 MainFrame["list"]["header"]["main"] = new CUIHorizontalList()
182 {
183 FillEmptySpace = new CUIBool2(true, false),
184 };
185
186 MainFrame["list"]["header"]["main"]["caption"] = new CUITextBlock("Test")
187 {
188 TextAlign = CUIAnchor.Center,
189 };
190 MainFrame["list"]["header"]["main"]["runall"] = new CUIButton("Run all")
191 {
192 AddOnMouseDown = (e) => Runner.RunAllTests(),
193 };
194 MainFrame["list"]["header"]["main"]["end"] = new CUIButton("End test")
195 {
196 AddOnMouseDown = (e) => Runner.StopTest(),
197 };
198 MainFrame["list"]["header"]["main"]["clear"] = new CUIButton("clear stash")
199 {
200 AddOnMouseDown = (e) => Runner.ClearStash(),
201 Style = new CUIStyle() { { "MasterColor", "Red" } },
202 };
203
204 MainFrame["list"]["header"]["closebutton"] = new CUIButton("X")
205 {
206 Style = new CUIStyle() { { "MasterColor", "Red" } },
207 AddOnMouseDown = (e) => Opened = false,
208 Font = GUIStyle.MonospacedFont,
209 };
210
211
212
213 MainFrame["list"]["testlist"] = CUITestList = new CUIVerticalList()
214 {
215 FillEmptySpace = new CUIBool2(false, true),
216 Scrollable = true,
217 };
218
219 MainFrame.IgnoreDebug = true;
220 OpenButton.IgnoreDebug = true;
221
222 CUI.Main.Append(MainFrame);
223 CUI.Main.Append(OpenButton);
224 }
225
226 public AutoTestGUI(AutomaticTestRunner runner)
227 {
228 Runner = runner;
229 CreateUI();
230 FindTests();
231
232 MainFrame.AddCommand("set autorun", (o) => runner.Config.Autorun = (string)o);
233
234 Runner.OnTestReport += (report) =>
235 {
236 TestButtons.GetValueOrDefault(report.TestMethod)?.UseReport(report);
237 };
238 }
239 }
240
241}
CUIAnchor is just a Vector2 This is a static class containing some helper methods.
Definition CUIAnchor.cs:18
A button It's derived from CUITextBlock and has all its props.
Definition CUIButton.cs:19
CUIBool2 FillEmptySpace
Will be resized to fill empty space in list components.
bool Revealed
Visible + !IgnoreEvents.
virtual CUIComponent Append(CUIComponent child, string name=null, [CallerMemberName] string memberName="")
Adds children to the end of the list.
void AddCommand(string name, Action< object > action)
Manually adds command.
CUIStyle Style
Allows you to assing parsable string or link to CUIPalette to any prop It's indexable,...
CUIBool2 FitContent
Will resize itself to fit components with absolute size, e.g. text.
void DispatchUp(CUICommand command)
Dispathes command up the component tree until someone consumes it.
Draggable and resizable container for other components.
Definition CUIFrame.cs:16
Resizing components to it's Height and placing them sequentially.
In fact a static class managing static things.
Definition CUIErrors.cs:16
static CUIMainComponent Main
Orchestrates Drawing and updates, there could be only one CUI.Main is located under vanilla GUI.
Definition CUI.cs:84
In Fact just an observable dict.
Definition CUIStyle.cs:21
Passive block of text.
A button which can be on and off It's derived from CUITextBlock and has all its props.
Resizing components to it's Width and placing them sequentially.
record CUICommand(string Name, object Data=null)
Can be dispatched up the component tree to notify parent about something add pass some event data wit...
Vector2 but with bools.
Definition CUIBool2.cs:17
Rectangle with float?
Definition CUIRect.cs:104