Search Unity

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?