Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions Editor/MCPTestWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace UnityMCP.Editor
/// </summary>
/// <remarks>
/// The window builds a TextField, Button, and Label with stable names. Verification helpers show the window, reset state,
/// send UI automation JSON-RPC calls, and assert that UI callbacks update the static test state.
/// send UI automation JSON-RPC calls, and assert that UI callbacks update this window's test state.
/// </remarks>
public class MCPTestWindow : EditorWindow
{
Expand All @@ -18,15 +18,15 @@ public class MCPTestWindow : EditorWindow
/// <summary>
/// Stores the last input value for verification.
/// </summary>
public static string LastInputValue = "";
public string LastInputValue { get; private set; } = "";

/// <summary>
/// Tracks if the test button has been clicked.
/// </summary>
public static bool ButtonClicked = false;
public bool ButtonClicked { get; private set; }

/// <summary>
/// Resets static test flags and clears the UI Toolkit input/label elements in the current visual tree.
/// Resets this window's test state and clears the UI Toolkit input/label elements in the current visual tree.
/// </summary>
public void ResetState()
{
Expand Down Expand Up @@ -58,25 +58,22 @@ private void OnEnable()
/// Creates named UI Toolkit controls and event handlers used by Nexus Unity UI automation smoke tests.
/// </summary>
/// <remarks>
/// This method mutates <see cref="EditorWindow.rootVisualElement"/>, resets static test state, registers a text-change
/// This method mutates <see cref="EditorWindow.rootVisualElement"/>, preserves instance test state, registers a text-change
/// callback, and wires the button click to update the label and <see cref="ButtonClicked"/>.
/// </remarks>
public void CreateGUI()
{
NexusEditorUi.SetupRoot(rootVisualElement);
rootVisualElement.name = "NexusTestWindowRoot";

LastInputValue = "";
ButtonClicked = false;

var header = NexusEditorUi.Panel("TestWindowHeader");
header.Add(NexusEditorUi.Label("UI Automation Test", 16, true));
header.Add(NexusEditorUi.Label("Named controls used by Nexus Unity UI automation checks.", 11, false, NexusEditorUi.Muted));
rootVisualElement.Add(header);

var panel = NexusEditorUi.Panel("TestWindowControls");

var label = new Label("Initial State");
var label = new Label(ButtonClicked ? "Button Clicked!" : "Initial State");
label.name = "TestLabel";
label.style.marginBottom = 8;
label.style.unityFontStyleAndWeight = FontStyle.Bold;
Expand All @@ -85,8 +82,7 @@ public void CreateGUI()
var textField = new TextField("Input:");
textField.name = "TestInput";
textField.style.marginBottom = 8;
textField.value = "";
LastInputValue = textField.value;
textField.value = LastInputValue;
textField.RegisterValueChangedCallback(evt => LastInputValue = evt.newValue);
panel.Add(textField);

Expand Down
8 changes: 4 additions & 4 deletions Editor/UIVerification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static void Verify()
wnd.ResetState();

TestListAndHierarchy();
TestInputAndClick();
TestInputAndClick(wnd);
NexusEditorLog.Log(NexusLogCategory.Diagnostics, "VERIFICATION SUCCESS", true);
}

Expand All @@ -38,13 +38,13 @@ private static void TestListAndHierarchy()
if (!hier.Contains("TestButton")) throw new System.Exception("Hierarchy failed");
}

private static void TestInputAndClick()
private static void TestInputAndClick(MCPTestWindow wnd)
{
string txt = "Test";
Call("ui_input_text", new JObject { ["window_title"] = MCPTestWindow.WindowTitle, ["element_name"] = "TestInput", ["text"] = txt });
if (MCPTestWindow.LastInputValue != txt) throw new System.Exception($"Input failed: expected '{txt}' but got '{MCPTestWindow.LastInputValue}'");
if (wnd.LastInputValue != txt) throw new System.Exception($"Input failed: expected '{txt}' but got '{wnd.LastInputValue}'");
Call("ui_click", new JObject { ["window_title"] = MCPTestWindow.WindowTitle, ["element_name"] = "TestButton" });
if (!MCPTestWindow.ButtonClicked) throw new System.Exception("Click failed");
if (!wnd.ButtonClicked) throw new System.Exception("Click failed");
}

private static string Call(string m, JObject p)
Expand Down
23 changes: 23 additions & 0 deletions Tests~/Editor/EditorWindowUiToolkitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,29 @@ public void TestWindowPreservesAutomationElementNames()
}
}

[Test]
public void TestWindowPreservesVerificationStateWhenRebuilt()
{
var window = ScriptableObject.CreateInstance<MCPTestWindow>();
try
{
window.CreateGUI();
window.rootVisualElement.Q<TextField>("TestInput").value = "Persisted";
window.rootVisualElement.Q<Button>("TestButton").Click();

window.CreateGUI();

Assert.AreEqual("Persisted", window.LastInputValue);
Assert.IsTrue(window.ButtonClicked);
Assert.AreEqual("Persisted", window.rootVisualElement.Q<TextField>("TestInput").value);
Assert.AreEqual("Button Clicked!", window.rootVisualElement.Q<Label>("TestLabel").text);
}
finally
{
Object.DestroyImmediate(window);
}
}

[Test]
public void ServerWindowAppliesMinimumUsableSize()
{
Expand Down
Loading