## 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 omni.kit import ui_test
from pxr import Sdf, UsdShade
from omni.kit.test_suite.helpers import open_stage, get_test_data_path, select_prims, wait_stage_loading, arrange_windows



class ShaderMaterialSubidProperty(AsyncTestCase):

    # Before running each test
    async def setUp(self):
        await arrange_windows()
        await open_stage(get_test_data_path(__name__, "usd/bound_shapes.usda"))

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

    async def test_l1_shader_material_subid_property(self):
        import omni.kit.commands

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

        usd_context = omni.usd.get_context()
        stage = usd_context.get_stage()
        to_select = ['/World/Cone', '/World/Cylinder', '/World/Cube', '/World/Sphere']

        # bind OmniSurface_Plastic to prims
        omni.kit.commands.execute('BindMaterial', material_path='/World/Looks/OmniSurface_Plastic', prim_path=to_select)

        # wait for material to load & UI to refresh
        await wait_stage_loading()

        # select OmniSurface_Plastic shader
        await select_prims(['/World/Looks/OmniSurface_Plastic/Shader'])
        await ui_test.human_delay()

        # get OmniSurface_Plastic shader
        prim = stage.GetPrimAtPath('/World/Looks/OmniSurface_Plastic/Shader')
        shader = UsdShade.Shader(prim)

        # get subid widget
        property_widget = ui_test.find("Property//Frame/**/ComboBox[*].identifier=='sdf_asset_info:mdl:sourceAsset:subIdentifier'")
        if property_widget.widget.enabled:
            items = property_widget.model.get_item_children(None)
            for item in items:
                # change selection
                property_widget.model.set_value(item.token)

                # wait for material to load & UI to refresh
                await wait_stage_loading()

                # verify shader value
                self.assertTrue(shader.GetSourceAssetSubIdentifier('mdl') == item.token)
