## Copyright (c) 2021, 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 os
import omni.kit.app
from omni.kit.test.async_unittest import AsyncTestCase
import omni.usd
from pxr import Sdf, UsdShade


class ShaderGotoMaterialProperty(AsyncTestCase):
    # Before running each test
    async def setUp(self):
        from omni.kit.test_suite.helpers import open_stage, get_test_data_path, arrange_windows

        await arrange_windows("Stage", 128)
        await open_stage(get_test_data_path(__name__, "usd/locate_file_material.usda"))

    # After running each test
    async def tearDown(self):
        from omni.kit.test_suite.helpers import wait_stage_loading

        await wait_stage_loading()

    async def test_l1_shader_goto_material_property(self):
        from omni.kit import ui_test
        from omni.kit.test_suite.helpers import get_test_data_path, select_prims, wait_stage_loading
        from omni.kit.window.content_browser import get_content_window

        await ui_test.find("Content").focus()

        stage_window = ui_test.find("Stage")
        await stage_window.focus()

        usd_context = omni.usd.get_context()
        stage = usd_context.get_stage()

        await select_prims(["/World/Looks/OmniPBR/Shader"])
        await wait_stage_loading()

        # verify corrent prim is selected
        content_browser = get_content_window()
        for material_attr, enabled, expected in [
                                       ("sdf_locate_asset_inputs:ao_texture", False, []),                                       # ""
                                       ("sdf_locate_asset_inputs:diffuse_texture", True, ["granite_a_mask.png"]),               # granite_a_mask.png
                                       ("sdf_locate_asset_inputs:emissive_mask_texture", True, ["granite_a_mask.png"]),         # granite_a_mask.png
                                       ("sdf_locate_asset_inputs:opacity_texture", False, []),                                  # http
                                       ("sdf_locate_asset_inputs:reflectionroughness_texture", True, [])]:                      # test
            content_browser.navigate_to(get_test_data_path(__name__, ""))
            widget = ui_test.find(f"Property//Frame/**/Button[*].identifier=='{material_attr}'")
            self.assertEqual(widget.widget.visible, enabled)
            if enabled:
                widget.widget.scroll_here_y(0.0)
                await ui_test.human_delay(10)
                await widget.click()
                await ui_test.human_delay(10)
                selected = [os.path.basename(path) for path in content_browser.get_current_selections()]
                self.assertEqual(selected, expected)
