Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Testing VisualElement events for Editor UIs

Discussion in 'Testing & Automation' started by sbergen, Jan 5, 2023.

  1. sbergen

    sbergen

    Joined:
    Jan 12, 2015
    Posts:
    53
    I've been working on some Editor UIs using the new UI Toolkit, and writing basic unit tests directly inspecting the
    VisualElement
    hierarchy has been very nice. However, when it comes to events, a panel is required, as event processing seems to be essentially disabled if there is no panel.

    My problem is that
    VisualElement
    can only use
    BaseVisualElementPanel
    as its panel type, and the class itself and all deriving classes, as well as
    VisualElement.SetPanel
    have internal visibility. So I can't find any way to make events fire for the
    VisualElement
    s I've instantiated in my tests.

    What I'd essentially like to do is something like this:
    Code (CSharp):
    1. var root = new VisualElement();
    2.  
    3. // This sets up subviews that use RegisterValueChangedCallback on a TextField
    4. var myView = new MyView(root);
    5.  
    6. root.Query<TextField>().AtIndex(0).value = "foo";
    7. // Assert that the visual tree is in the expected state
    Is there any way to achieve this?