Skip to content

Commit

Permalink
Merge pull request #186 from TrevisanGMW/dev-feature/unittests
Browse files Browse the repository at this point in the history
Created UI unittests
  • Loading branch information
TrevisanGMW authored Sep 8, 2023
2 parents 2723cc3 + ffa4661 commit beefeb1
Show file tree
Hide file tree
Showing 10 changed files with 738 additions and 6 deletions.
1 change: 0 additions & 1 deletion gt/ui/progress_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,3 @@ def close_parent_window(self):
out = window.get_output_box_plain_text()
print(out)
sys.exit(app.exec_())

12 changes: 8 additions & 4 deletions gt/ui/qt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,14 +449,14 @@ def update_formatted_label(target_label,
target_label.setText(_html)


def load_and_scale_pixmap(image_path, scale_percentage, exact_height=None, exact_width=None):
def load_and_scale_pixmap(image_path, scale_percentage=100, exact_height=None, exact_width=None):
"""
Load an image from the given path, and scale it by the specified percentage,
then return the scaled QPixmap.
Args:
image_path (str): Path to the image file.
scale_percentage (float): Percentage to scale the image by.
scale_percentage (float): Percentage to scale the image by. (Default is 100%, which is the original resolution)
100 = Same resolution. 50 = half the resolution. 200 = double the resolution.
exact_height (int, optional): If provided, it will overwrite scale percentage and use this height instead.
exact_width (int, optional): If provided, it will overwrite scale percentage and use this width instead.
Expand All @@ -474,7 +474,7 @@ def load_and_scale_pixmap(image_path, scale_percentage, exact_height=None, exact
if exact_height and isinstance(exact_height, int):
scaled_height = exact_height
if exact_width and isinstance(exact_width, int):
scaled_height = exact_width
scaled_width = exact_width

scaled_pixmap = pixmap.scaled(scaled_width, scaled_height, mode=QtCore.Qt.SmoothTransformation)
return scaled_pixmap
Expand Down Expand Up @@ -526,7 +526,11 @@ def __enter__(self):
self.parent = get_maya_main_window()
else:
logger.debug('Running Qt outside Maya. Initializing QApplication.')
self.app = QtWidgets.QApplication(sys.argv)
_app_instance = QApplication.instance()
if not _app_instance:
self.app = QApplication(sys.argv)
else:
self.app = _app_instance
return self

def __exit__(self, exc_type, exc_value, traceback):
Expand Down
11 changes: 10 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,19 @@

# Modules to Test
modules_to_test = [
# Ui
test_ui.test_input_window_text,
test_ui.test_line_text_widget,
test_ui.test_maya_menu,
test_ui.test_progress_bar,
test_ui.test_python_output_view,
test_ui.test_qt_utils,
test_ui.test_resource_library,
# Tools
test_curve_library.test_curve_library_model,
test_package_updater.test_package_updater_model,
test_sample_tool.test_sample_tool_model,
test_ui.test_resource_library,
# Utils
test_utils.test_alembic_utils,
test_utils.test_anim_utils,
test_utils.test_attr_utils,
Expand Down
6 changes: 6 additions & 0 deletions tests/test_ui/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
from . import test_input_window_text
from . import test_line_text_widget
from . import test_maya_menu
from . import test_progress_bar
from . import test_python_output_view
from . import test_qt_utils
from . import test_resource_library
55 changes: 55 additions & 0 deletions tests/test_ui/test_input_window_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from PySide2.QtWidgets import QApplication
import unittest
import logging
import sys
import os

# Logging Setup
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

# Import Tested Script
test_utils_dir = os.path.dirname(__file__)
tests_dir = os.path.dirname(test_utils_dir)
package_root_dir = os.path.dirname(tests_dir)
for to_append in [package_root_dir, tests_dir]:
if to_append not in sys.path:
sys.path.append(to_append)
from gt.ui.input_window_text import InputWindowText


class TestInputWindowText(unittest.TestCase):
@classmethod
def setUpClass(cls):
app = QApplication.instance()
if not app:
cls.app = QApplication(sys.argv)

def setUp(self):
self.window = InputWindowText()

def test_window_title(self):
expected_title = "New Window Title"
self.window.set_window_title(expected_title)
self.assertEqual(self.window.windowTitle(), expected_title)

def test_text_field_text(self):
expected_text = "Sample Text"
self.window.set_text_field_text(expected_text)
self.assertEqual(self.window.get_text_field_text(), expected_text)

def test_text_field_placeholder(self):
expected_placeholder = "Enter text here"
self.window.set_text_field_placeholder(expected_placeholder)
self.assertEqual(self.window.text_field.placeholderText(), expected_placeholder)

def test_confirm_button_text(self):
expected_button_text = "OK"
self.window.set_confirm_button_text(expected_button_text)
self.assertEqual(self.window.confirm_button.text(), expected_button_text)

def test_message_label(self):
expected_message = "This is a test message"
self.window.set_message(expected_message)
self.assertEqual(self.window.description_label.text(), expected_message)
47 changes: 47 additions & 0 deletions tests/test_ui/test_line_text_widget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from PySide2.QtWidgets import QApplication, QDialog, QVBoxLayout
from PySide2.QtGui import QColor
import unittest
import logging
import sys
import os

# Logging Setup
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

# Import Tested Script
test_utils_dir = os.path.dirname(__file__)
tests_dir = os.path.dirname(test_utils_dir)
package_root_dir = os.path.dirname(tests_dir)
for to_append in [package_root_dir, tests_dir]:
if to_append not in sys.path:
sys.path.append(to_append)
from gt.ui.line_text_widget import LineTextWidget


class TestLineTextWidget(unittest.TestCase):
@classmethod
def setUpClass(cls):
app = QApplication.instance()
if not app:
cls.app = QApplication(sys.argv)

def test_line_number_color(self):
line_text_widget = LineTextWidget()
line_text_widget.set_line_number_color(color=QColor(255, 0, 0)) # Set a red color
self.assertEqual(line_text_widget.number_bar.number_color, QColor(255, 0, 0))

def test_line_number_bold_color(self):
line_text_widget = LineTextWidget()
line_text_widget.line_number_bold_color(color=QColor(0, 255, 0)) # Set a green color
self.assertEqual(line_text_widget.number_bar.number_bold_color, QColor(0, 255, 0))

def test_dialog_with_syntax_highlighter(self):
from gt.ui.syntax_highlighter import PythonSyntaxHighlighter
dialog = QDialog()
layout = QVBoxLayout(dialog)
line_text_widget = LineTextWidget()
dialog.setLayout(layout)
layout.addWidget(line_text_widget)
PythonSyntaxHighlighter(line_text_widget.get_text_edit().document())
89 changes: 89 additions & 0 deletions tests/test_ui/test_maya_menu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from PySide2.QtWidgets import QApplication
import unittest
import logging
import sys
import os

# Logging Setup
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

# Import Tested Script
test_utils_dir = os.path.dirname(__file__)
tests_dir = os.path.dirname(test_utils_dir)
package_root_dir = os.path.dirname(tests_dir)
for to_append in [package_root_dir, tests_dir]:
if to_append not in sys.path:
sys.path.append(to_append)
from gt.ui.maya_menu import MayaMenu, MenuItem
from tests import maya_test_tools


class TestMayaMenu(unittest.TestCase):
@classmethod
def setUpClass(cls):
app = QApplication.instance()
if not app:
cls.app = QApplication(sys.argv)

def setUp(self):
# Create a MayaMenu object for testing
self.menu = MayaMenu(name="TestMenu", parent=None)
maya_test_tools.import_maya_standalone(initialize=True)

def tearDown(self):
# Delete the menu after each test
self.menu.delete_menu()

def test_create_menu(self):
# Test if the menu is created successfully
self.menu.create_menu()
self.assertTrue(self.menu.initialized)

def test_add_menu_item(self):
# Add a menu item and check if it exists in the menu items list
label = "TestMenuItem"
self.menu.add_menu_item(label)
self.assertTrue(any(item.label == label for item in self.menu.menu_items))

def test_add_sub_menu(self):
# Add a sub-menu and check if it exists in the sub_menus list
label = "TestSubMenu"
self.menu.add_sub_menu(label)
self.assertTrue(label in self.menu.sub_menus)

def test_add_divider(self):
# Add a divider and check if it exists in the menu items list
label = "TestDivider"
self.menu.add_divider(label)
self.assertTrue(any(item.label == label for item in self.menu.menu_items))

def test_get_item_parameters(self):
# Test the get_item_parameters method
item = MenuItem(label="label",
command='command',
tooltip='tooltip',
icon='icon',
enable=True,
parent="parent",
divider=True,
divider_label="divider_label",
sub_menu=False,
tear_off=False,
enable_command_repeat=False,
option_box=False,
option_box_icon='')
params = self.menu.get_item_parameters(item)
self.assertEqual(params["label"], "label")
self.assertEqual(params["command"], "command")
self.assertEqual(params["annotation"], "tooltip")
self.assertEqual(params["image"], "icon")
self.assertEqual(params["enable"], True)
self.assertEqual(params["parent"], "parent")
self.assertEqual(params["divider"], True)
self.assertEqual(params["dividerLabel"], "divider_label")
self.assertEqual(params["subMenu"], False)
self.assertEqual(params.get("tearOff"), None)
self.assertEqual(params["enableCommandRepeat"], False)
self.assertEqual(params.get("optionBox"), None)
73 changes: 73 additions & 0 deletions tests/test_ui/test_progress_bar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from PySide2.QtWidgets import QApplication
import unittest
import logging
import sys
import os

# Logging Setup
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

# Import Tested Script
test_utils_dir = os.path.dirname(__file__)
tests_dir = os.path.dirname(test_utils_dir)
package_root_dir = os.path.dirname(tests_dir)
for to_append in [package_root_dir, tests_dir]:
if to_append not in sys.path:
sys.path.append(to_append)
from gt.ui.progress_bar import ProgressBarWindow


class TestProgressBar(unittest.TestCase):
@classmethod
def setUpClass(cls):
app = QApplication.instance()
if not app:
cls.app = QApplication(sys.argv)

def setUp(self):
self.window = ProgressBarWindow()

def test_set_progress_bar_value(self):
window = ProgressBarWindow()

# Test setting the progress bar value
window.set_progress_bar_value(50)
self.assertEqual(window.progress_bar.value(), 50)

def test_add_text_to_output_box(self):
window = ProgressBarWindow()

# Test adding text to the output box
text_to_add = "This is a test message."
window.add_text_to_output_box(text_to_add)
output_text = window.get_output_box_plain_text()
self.assertIn(text_to_add, output_text)

def test_change_line_color(self):
window = ProgressBarWindow()

# Test changing the color of a specific line
text_to_add = "This is a test message."
window.add_text_to_output_box(text_to_add)
window.change_line_color(line_number=1, color="red")

# Get the formatted text and check if it contains the color tag
output_text = window.get_output_box_plain_text()
self.assertIn(text_to_add, output_text)

def test_change_last_line_color(self):
window = ProgressBarWindow()

# Test changing the color of the last line
text_to_add1 = "This is line 1."
text_to_add2 = "This is line 2."
window.add_text_to_output_box(text_to_add1)
window.add_text_to_output_box(text_to_add2)
window.change_last_line_color("blue")

# Get the formatted text and check if the last line contains the color tag
output_text = window.get_output_box_plain_text()
self.assertIn(text_to_add1, output_text)
self.assertIn(text_to_add2, output_text)
54 changes: 54 additions & 0 deletions tests/test_ui/test_python_output_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from PySide2.QtWidgets import QApplication
from unittest.mock import MagicMock
import unittest
import logging
import sys
import os

# Logging Setup
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

# Import Tested Script
test_utils_dir = os.path.dirname(__file__)
tests_dir = os.path.dirname(test_utils_dir)
package_root_dir = os.path.dirname(tests_dir)
for to_append in [package_root_dir, tests_dir]:
if to_append not in sys.path:
sys.path.append(to_append)
from gt.ui.python_output_view import PythonOutputView


class TestPythonOutputView(unittest.TestCase):
@classmethod
def setUpClass(cls):
app = QApplication.instance()
if not app:
cls.app = QApplication(sys.argv)

def setUp(self):
# Create an instance of PythonOutputView
self.python_output_view = PythonOutputView()

def test_clear_python_output(self):
# Test clearing Python output
self.python_output_view.output_python_box.get_text_edit().setPlainText("Sample text")
self.python_output_view.clear_python_output()
result = self.python_output_view.get_python_output_text()
expected = ""
self.assertEqual(result, expected)

def test_set_python_output_text(self):
# Test setting Python output text
self.python_output_view.set_python_output_text("Sample text")
result = self.python_output_view.get_python_output_text()
expected = "Sample text"
self.assertEqual(result, expected)

def test_get_python_output_text(self):
# Test getting Python output text
self.python_output_view.output_python_box.get_text_edit().setPlainText("Sample text")
result = self.python_output_view.get_python_output_text()
expected = "Sample text"
self.assertEqual(result, expected)
Loading

0 comments on commit beefeb1

Please sign in to comment.