简体   繁体   中英

How to get registered actions from UnityEngine.UIElements.TextField & Button

I am creating automated unit tests in Unity, and want to test if a method used TextField.RegisterValueChangedCallback(SomeMethod); with a specific method. I also want to test if a UIElement Button has a specific method registered with: Button.clicked += SomeMethod; .

My question: Is there a way to access all registered methods from these text fields and buttons? Or maybe a (not too messy) work around?

I am using UnityEngine.UIElements; in my project.

Simple version of what method I want to test :

VisualTreeAsset root; // Assigned in some other method

public void TestedMethod(){
    // Get the field from the VisualTreeAsset root
    textField = root.Q<TextField>("FieldName");

    // Register
    // This is the line I want to be tested:
    textField.RegisterValueChangedCallback(SomeMethod);
}

Pseudo code to illustrate what I want to test:

[Test]
public void SomeTest()
{
    // Get references from wherever they may be...
    Action SomeMethod = [...];
    TextField textField = [...];
    Button button = [...];

    // Assertions
    // This is the part I need help with:
    Assert.IsTrue( textField.GetRegisteredValueChange().Contains(SomeMethod) );
    Assert.IsTrue( button.clicked.Contains(SomeMethod) );
}

I have looked through the Unity API, and can't find anything that seems usefull: https://docs.unity3d.com/ScriptReference/UIElements.TextField.htmlhttps://docs.unity3d.com/ScriptReference/UIElements.Button.html

You might use UnityEngine.EventSystems

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM