imgui_bundle icon indicating copy to clipboard operation
imgui_bundle copied to clipboard

Using pure python backend with test_engine

Open csuckow opened this issue 2 years ago • 6 comments

Hello! I am tinkering with the recent example for glfw and trying to integrate the test_engine by following the instructions here and here. I was trying to click the button from the example window via test engine, but ran into some difficulties. When calling imgui.test_engine.start, the application freezes. Is there something obvious that I'm missing?

(...)
def main():
    imgui.create_context()
    # initializing test_engine
    test_engine = imgui.test_engine.create_context()
    test_click_button = imgui.test_engine.register_test(test_engine, "Demo Tests", "click button")
    def test_open_popup_func(ctx: imgui.test_engine.TestContext) -> None:
        ctx.set_ref("Custom window")
        ctx.item_click("**/Hello")
    test_click_button.test_func = test_open_popup_func
    #imgui.test_engine.start(test_engine, imgui.get_current_context())   <---- the application freezes
    (...)

csuckow avatar Dec 06 '23 15:12 csuckow

Hello,

Integrating ImGui TestEngine directly from python, and without using HelloImGui and ImmApp is very difficult.

ImGui Test Engine uses two different threads (one for the main gui, and one for the scenario runner). Your python code will be called from two separate threads, and this breaks the GIL!

HelloImGui and ImmApp handle this well by transferring the GIL between threads (from C++). Doing this from pure python seems difficult.

pthom avatar Dec 06 '23 18:12 pthom

I ran into this too, #166. Two thoughts:

  1. Can you document this, perhaps in the docs that come up for imgui.test_engine.create_context() and imgui.test_engine.start()?
  2. If i understand correctly, runner_params.use_imgui_test_engine can only be set before the start of the app. Can you make it such that on each iteration your display functions check if its true, and if its true but there is not test engine context yet, make, register and start it?

Regarding the second: just an idea, no idea 1. how needed it is, 2. how useful it is.

dcnieho avatar Jan 16 '24 06:01 dcnieho

Can you document this, perhaps in the docs that come up for imgui.test_engine.create_context() and imgui.test_engine.start()?

Yes, see https://github.com/pthom/imgui_bundle/commit/20eecc036b6e6e5d3affd2058feae46bd2ef0d63

If i understand correctly, runner_params.use_imgui_test_engine can only be set before the start of the app. Can you make it such that on each iteration your display functions check if its true, and if its true but there is not test engine context yet, make, register and start it?

I see no real use case for this.

However, I added the possibility to call register_test several times during the app lifetime.

https://github.com/pthom/imgui_bundle/commit/745ded62dc889a2f11a9c36f798fcea3643b8e88

pthom avatar Jan 16 '24 11:01 pthom

Ok, thanks for those additions!

So the logic would be that, until the patch upstream is in, you'd have to decide whether the test engine should be started or not before run()ing the app. I have not seen any performance issue connected to this, so guess its ok.

dcnieho avatar Jan 16 '24 14:01 dcnieho

The patch is already upstream

pthom avatar Jan 16 '24 14:01 pthom

Ah super! Nice work!

dcnieho avatar Jan 16 '24 14:01 dcnieho