import asyncio
from pathlib import Path

import omni.kit.app
from omni.ui.tests.test_base import OmniUiTest

from ..extension import AboutPageExtension

CURRENT_PATH = Path(__file__).parent
TEST_DATA_PATH = CURRENT_PATH.parent.parent.parent.parent.parent.joinpath("data").joinpath("tests")


class TestPage(OmniUiTest):
    # Before running each test
    async def setUp(self):
        await super().setUp()

        self._golden_img_dir = TEST_DATA_PATH.absolute().joinpath("golden_img").absolute()

    # After running each test
    async def tearDown(self):
        await super().tearDown()

    async def test_page(self):
        window = await self.create_test_window(width=800, height=500)
        with window.frame:
            ext = AboutPageExtension()
            ext.on_startup("omni.kit.welcome.extensions-1.1.0")
            ext.build_ui()
            
            ext._about_widget.app_info = "#App Name# #App Version#"
            ext._about_widget.versions = ["About test version #1", "About test version #2", "About test version #3"]

            class FakePluginImpl():
                def __init__(self, name):
                    self.name = name

            class FakePlugin():
                def __init__(self, name):
                    self.libPath = "Lib Path " + name
                    self.impl = FakePluginImpl("Impl " + name)
                    self.interfaces = "Interface " + name
            ext._about_widget.plugins = [FakePlugin("Test 1"), FakePlugin("Test 2"), FakePlugin("Test 3"), FakePlugin("Test 4")]

        await omni.kit.app.get_app().next_update_async()
        # Wait for image loaded
        await asyncio.sleep(5)
        await self.finalize_test(golden_img_dir=self._golden_img_dir, golden_img_name="page.png")
