import carb.settings
import omni.kit.test
from ..menu_item.resolution_collection.model import ComboBoxResolutionModel, ResolutionComboBoxItem

SETTING_CUSTOM_RESOLUTION_LIST = "/persistent/app/renderer/resolution/custom/list"


class TestCustomResolution(omni.kit.test.AsyncTestCase):
    async def setUp(self):
        self.__settings = carb.settings.get_settings()
        # Useles fake data that needs to go to ComboBoxResolutionModel
        resolution_setter = None
        resolution_settings = (("setting", (0,0)), ("setting", (0,0)))
        self.__model = ComboBoxResolutionModel(None, resolution_settings, self.__settings)
        super().setUp()

    async def tearDown(self):
        self.__settings.set(SETTING_CUSTOM_RESOLUTION_LIST, [])
        super().tearDown()

    async def test_custom_resolutions(self):
        items = self.__model.get_item_children(None)
        num_items = len(items)

        self.__settings.set(SETTING_CUSTOM_RESOLUTION_LIST, [{"name": "test", "width": 100, "height": 200}])
        for _ in range(2):
            await omni.kit.app.get_app().next_update_async()
        items = self.__model.get_item_children(None)
        self.assertEqual(num_items + 2, len(items))
        new_item: ResolutionComboBoxItem = items[-1]
        self.assertEqual(new_item.name, "test")
        self.assertEqual(new_item.resolution, (100, 200))
        self.assertTrue(new_item.custom)

        self.__settings.set(SETTING_CUSTOM_RESOLUTION_LIST, [])
        for _ in range(2):
            await omni.kit.app.get_app().next_update_async()
        items = self.__model.get_item_children(None)
        self.assertEqual(num_items, len(items))
