## Copyright (c) 2022, 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 omni.kit.test
import omni.kit.app
import omni.ui as ui
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
from omni.kit.quicklayout import QuickLayout
from omni.ui.workspace_utils import CompareDelegate


class CompareLayout(AsyncTestCase):
    # Before running each test
    async def setUp(self):
        await arrange_windows("Stage", 256)

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

    async def test_compare_layout(self):
        layout_path = get_test_data_path(__name__, "layout.json")

        # compare layout, this should fail
        result = QuickLayout.compare_file(layout_path)
        self.assertNotEqual(result, [])

        # load layout
        QuickLayout.load_file(layout_path)
        # need to pause as load_file does some async stuff
        await ui_test.human_delay(10)

        # compare layout, this should pass
        result = QuickLayout.compare_file(layout_path)
        self.assertEqual(result, [])

    async def test_compare_layout_delegate(self):
        called_delegate = False
        layout_path = get_test_data_path(__name__, "layout.json")

        class TestCompareDelegate(CompareDelegate):
            def failed_get_window(self, error_list: list, target: dict):
                nonlocal called_delegate

                called_delegate = True
                return super().failed_get_window(error_list, target)

            def failed_window_key(self, error_list: list, key: str, target: dict, target_window: ui.Window):
                nonlocal called_delegate

                called_delegate = True
                return super().failed_window_key(error_list, key, target, target_window)

            def failed_window_value(self, error_list: list, key: str, value, target: dict, target_window: ui.Window):
                nonlocal called_delegate

                called_delegate = True
                return super().failed_window_value(error_list, key, value, target, target_window)

            def compare_value(self, key:str, value, target):
                nonlocal called_delegate

                called_delegate = True
                return super().compare_value(key, value, target)

        self.assertFalse(called_delegate)

        # compare layout, this should fail
        result = QuickLayout.compare_file(layout_path, compare_delegate=TestCompareDelegate())
        self.assertNotEqual(result, [])

        # load layout
        QuickLayout.load_file(layout_path)
        # need to pause as load_file does some async stuff
        await ui_test.human_delay(10)

        # compare layout, this should pass
        result = QuickLayout.compare_file(layout_path, compare_delegate=TestCompareDelegate())
        self.assertEqual(result, [])
        self.assertTrue(called_delegate)
