## Copyright (c) 2023, NVIDIA CORPORATION.  All rights reserved.
##
## NVIDIA CORPORATION and its licensors retain all intellectual property
## and proprietary rights in and to this software, related documentation
## and any modifications thereto.  Any use, reproduction, disclosure or
## distribution of this software and related documentation without an express
## license agreement from NVIDIA CORPORATION is strictly prohibited.
##
import carb
import omni.kit.test
import omni.kit.app
import omni.ui as ui
import omni.kit.commands
from omni.kit.test.async_unittest import AsyncTestCase
from omni.kit import ui_test
from omni.kit.test_suite.helpers import get_test_data_path, arrange_windows


class VerifyDockspace(AsyncTestCase):
    # Before running each test
    async def setUp(self):
        await arrange_windows("Stage", 256)
        await omni.usd.get_context().new_stage_async()
        omni.kit.commands.execute("CreatePrimWithDefaultXform", prim_type="Sphere", select_new_prim=True)

    # After running each test
    async def tearDown(self):
        pass

    async def test_verify_dockspace(self):
        # verify "Dockspace" window doesn't disapear when a prim is selected as this breaks layout load/save
        omni.usd.get_context().get_selection().set_selected_prim_paths([], True)
        await ui_test.human_delay(100)
        self.assertNotEqual(ui.Workspace.get_window("DockSpace"), None)

        omni.usd.get_context().get_selection().set_selected_prim_paths(["/Sphere"], True)
        await ui_test.human_delay(100)
        self.assertNotEqual(ui.Workspace.get_window("DockSpace"), None)

    async def test_verify_dockspace_shutdown(self):
        self._hooks = []
        self._hooks.append(omni.kit.app.get_app().get_shutdown_event_stream().create_subscription_to_pop_by_type(
                omni.kit.app.POST_QUIT_EVENT_TYPE,
                self._on_shutdown_handler,
                name="omni.create.app.setup shutdown for layout",
                order=0))

        omni.usd.get_context().get_selection().set_selected_prim_paths(["/Sphere"], True)
        await ui_test.human_delay(100)

    def _on_shutdown_handler(self, e: carb.events.IEvent):
        # verify "Dockspace" window hasn't disapeared as prims are selected as this breaks layout load/save
        print("running _on_shutdown_handler test")
        self.assertNotEqual(ui.Workspace.get_window("DockSpace"), None)
