import omni.kit.test
from omni.kit.widget.inspector import PreviewMode, InspectorWidget
import functools


class TestInspectorWidget(omni.kit.test.AsyncTestCase):
    async def setUp(self):
        self._widget = InspectorWidget()

    def test_widget_init(self):
        self.assertTrue(self._widget)

    def test_widget_get_set_widget(self):
        widget = self._widget.widget
        self.assertEqual(widget, None) # Check default

        label = omni.ui.Label("Blah")
        self._widget.widget = label
        self.assertEqual(self._widget.widget, label)

    def test_widget_get_set_preview(self):
        preview_mode = self._widget.preview_mode
        self.assertEqual(preview_mode, PreviewMode.WIREFRAME_AND_COLOR)  # Check default

        self._widget.preview_mode = PreviewMode.WIRE_ONLY
        self.assertEqual(self._widget.preview_mode, PreviewMode.WIRE_ONLY)

    def test_widget_get_set_selected_widget(self):

        self.assertEqual(self._widget.selected_widget, None)  # Check default

        label = omni.ui.Label("Blah")
        self._widget.selected_widget = label

        self.assertEqual(self._widget.selected_widget, label)

    def test_widget_get_set_start_depth(self):
        start_depth = self._widget.start_depth
        self.assertEqual(start_depth, 0)  # Check default
        self._widget.start_depth = 25
        self.assertEqual(self._widget.start_depth, 25)

    def test_widget_get_set_end_depth(self):
        end_depth = self._widget.end_depth
        self.assertEqual(end_depth, 65535)  # Check default
        self._widget.end_depth = 25
        self.assertEqual(self._widget.end_depth, 25)

    def test_widget_set_widget_changed_fn(self):
        def selection_changed(the_list, widget):
            the_list.append(widget)

        the_list = []
        self._widget.set_selected_widget_changed_fn(functools.partial(selection_changed, the_list))
        label = omni.ui.Label("Blah")
        self.assertTrue(len(the_list) == 0)
        self._widget.selected_widget = label
        self.assertTrue(len(the_list) == 1)
        self.assertEqual(the_list[0], label)

    def test_widget_set_preview_mode_changed_fn(self):
        def preview_mode_value_changed(the_list, value):
            the_list.append(value)

        the_list = []

        self._widget.set_preview_mode_change_fn(functools.partial(preview_mode_value_changed, the_list))
        self.assertTrue(len(the_list) == 0)

        self._widget.preview_mode = PreviewMode.WIRE_ONLY
        self.assertTrue(len(the_list) == 1)
        self.assertTrue(the_list[0] == PreviewMode.WIRE_ONLY)
