Skip to content

Commit

Permalink
Update tests to use QtTest
Browse files Browse the repository at this point in the history
  • Loading branch information
leorudczenko committed Aug 21, 2023
1 parent 3fb0fba commit c7e8c49
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 62 deletions.
20 changes: 20 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
Definitions for test fixtures used within other tests.
"""
import pytest
from PyQt5.QtCore import (
Qt,
QPoint,
)
from PyQt5.QtTest import QTest
from PyQt5.QtWidgets import QWidget

from tactool.table_model import TableModel
from tactool.main import TACtool
Expand Down Expand Up @@ -36,3 +42,17 @@ def public_index(model: TableModel):
The index in the list of headers where the public headers end.
"""
return len(model.public_headers)


def click_viewport(
widget: QWidget,
x: int,
y: int,
button: Qt.MouseButton,
) -> None:
"""
Use QTest to simulate a click at the given coordinates on a QWidget with a viewport.
"""
# Some tests fail if the mouse isn't initially moved onto the graphicsview before clicking
QTest.mouseMove(widget.viewport(), QPoint(x, y))
QTest.mouseClick(widget.viewport(), button, Qt.NoModifier, QPoint(x, y))
20 changes: 11 additions & 9 deletions test/test_import_export.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from pathlib import Path

import pytest
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap

from tactool.main import TACtool
from tactool.analysis_point import (
AnalysisPoint,
export_tactool_csv,
)
from conftest import click_viewport


def test_export_image(tactool: TACtool, tmp_path: Path):
Expand All @@ -17,9 +19,9 @@ def test_export_image(tactool: TACtool, tmp_path: Path):
tmp_image_path = tmp_path / "exported_image.png"

# Add some Analysis Points
tactool.graphics_view.left_click.emit(101, 101)
tactool.graphics_view.left_click.emit(202, 202)
tactool.graphics_view.left_click.emit(303, 303)
click_viewport(tactool.graphics_view, 101, 101, Qt.MouseButton.LeftButton)
click_viewport(tactool.graphics_view, 202, 202, Qt.MouseButton.LeftButton)
click_viewport(tactool.graphics_view, 303, 303, Qt.MouseButton.LeftButton)
tactool.window.update_point_settings(
sample_name="sample_x83",
mount_name="mount_x81",
Expand All @@ -28,9 +30,9 @@ def test_export_image(tactool: TACtool, tmp_path: Path):
diameter=100,
colour="#ff0000",
)
tactool.graphics_view.left_click.emit(404, 404)
click_viewport(tactool.graphics_view, 404, 404, Qt.MouseButton.LeftButton)
# The 5th point purposefully goes over the imported image border
tactool.graphics_view.left_click.emit(555, 555)
click_viewport(tactool.graphics_view, 555, 555, Qt.MouseButton.LeftButton)

# Zoom in on the PyQt Graphics View
factor = 1.25
Expand Down Expand Up @@ -112,7 +114,7 @@ def test_import_tactool_csv(
assert expected_point.aslist()[:public_index] == loaded_point.aslist()[:public_index]

# Click new points
tactool.graphics_view.left_click.emit(111, 111)
click_viewport(tactool.graphics_view, 111, 111, Qt.MouseButton.LeftButton)

# Check that the ID values continue from the maximum ID value in the CSV file
assert len(tactool.table_model.analysis_points) == 6
Expand All @@ -135,8 +137,8 @@ def test_export_tactool_csv(tactool: TACtool, tmp_path: Path):
]

# Add 2 Analysis Points
tactool.graphics_view.left_click.emit(101, 101)
tactool.graphics_view.left_click.emit(202, 202)
click_viewport(tactool.graphics_view, 101, 101, Qt.MouseButton.LeftButton)
click_viewport(tactool.graphics_view, 202, 202, Qt.MouseButton.LeftButton)

# Adjust the settings for the 3rd Analysis Point
tactool.window.update_point_settings(
Expand All @@ -148,7 +150,7 @@ def test_export_tactool_csv(tactool: TACtool, tmp_path: Path):
scale=1.5,
colour="#444444",
)
tactool.graphics_view.left_click.emit(303, 303)
click_viewport(tactool.graphics_view, 303, 303, Qt.MouseButton.LeftButton)

# Save the data to the given CSV file path
export_tactool_csv(
Expand Down
62 changes: 33 additions & 29 deletions test/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
tactool fixtures start a running QApplication for the context of the test.
"""
from PyQt5.QtCore import Qt
from PyQt5.QtTest import QTest

from tactool.main import TACtool
from tactool.analysis_point import AnalysisPoint
from conftest import click_viewport


def test_add_and_remove_points(tactool: TACtool, public_index: int):
Expand All @@ -22,7 +26,7 @@ def test_add_and_remove_points(tactool: TACtool, public_index: int):
offset = 4

# The 1st Analysis Point has default settings
tactool.graphics_view.left_click.emit(101, 101)
click_viewport(tactool.graphics_view, 101, 101, Qt.MouseButton.LeftButton)

# Adjust the settings for the 2nd Analysis Point
tactool.window.update_point_settings(
Expand All @@ -34,7 +38,7 @@ def test_add_and_remove_points(tactool: TACtool, public_index: int):
scale=2.0,
colour="#222222",
)
tactool.graphics_view.left_click.emit(202, 202)
click_viewport(tactool.graphics_view, 202, 202, Qt.MouseButton.LeftButton)

# Adjust the settings for the 3rd Analysis Point
# Purposefully making it overlap the 2nd Analysis Point
Expand All @@ -45,7 +49,7 @@ def test_add_and_remove_points(tactool: TACtool, public_index: int):
label="RefMark",
colour="#333333",
)
tactool.graphics_view.left_click.emit(240, 240)
click_viewport(tactool.graphics_view, 240, 240, Qt.MouseButton.LeftButton)

expected_data = [
AnalysisPoint(1, "RefMark", 101, 101, 10, 1.0, "#ffff00", "", "", "", "", None, None, None),
Expand All @@ -64,17 +68,17 @@ def test_add_and_remove_points(tactool: TACtool, public_index: int):
# Remove the Analysis Point with an ID value of 3
# Purposefully clicking between the 2nd and 3rd point to ensure the 3rd one is still deleted
# It should work like a stack when deleting overlapping points
tactool.graphics_view.right_click.emit(221, 221)
click_viewport(tactool.graphics_view, 221, 221, Qt.MouseButton.RightButton)

# Right click on an empty part of the PyQt Graphics View
# Nothing should change
tactool.graphics_view.right_click.emit(0, 0)
click_viewport(tactool.graphics_view, 0, 0, Qt.MouseButton.RightButton)

# Adjust the settings for the 4th Analysis Point to match those of the 2nd Analysis Point
# This is done by emitting a signal from the PyQt Table View of the selected Analysis Point
tactool.table_view.selected_analysis_point.emit(expected_data[1], 0)
# The 4th Analysis Point ID value should be 3
tactool.graphics_view.left_click.emit(404, 404)
click_viewport(tactool.graphics_view, 404, 404, Qt.MouseButton.LeftButton)

expected_data = [
AnalysisPoint(1, "RefMark", 101, 101, 10, 1.0, "#ffff00", "", "", "", "", None, None, None),
Expand All @@ -99,9 +103,9 @@ def test_clear_points(tactool: TACtool):
assert tactool.table_model.analysis_points == []

# Add some Analysis Points
tactool.graphics_view.left_click.emit(101, 101)
tactool.graphics_view.left_click.emit(202, 202)
tactool.graphics_view.left_click.emit(303, 303)
click_viewport(tactool.graphics_view, 101, 101, Qt.MouseButton.LeftButton)
click_viewport(tactool.graphics_view, 202, 202, Qt.MouseButton.LeftButton)
click_viewport(tactool.graphics_view, 303, 303, Qt.MouseButton.LeftButton)
# The 5th point partially overlaps the 4th point
# This is intentional as this used to cause issues
tactool.window.update_point_settings(
Expand All @@ -112,11 +116,11 @@ def test_clear_points(tactool: TACtool):
diameter=100,
colour="#ff0000",
)
tactool.graphics_view.left_click.emit(404, 404)
tactool.graphics_view.left_click.emit(440, 440)
click_viewport(tactool.graphics_view, 404, 404, Qt.MouseButton.LeftButton)
click_viewport(tactool.graphics_view, 440, 440, Qt.MouseButton.LeftButton)

# Simulate a button click of the Clear Points button
tactool.window.clear_points_button.click()
QTest.mouseClick(tactool.window.clear_points_button, Qt.MouseButton.LeftButton)

# Check that all Analysis Points have been removed
assert tactool.table_model.analysis_points == []
Expand All @@ -127,19 +131,19 @@ def test_reset_id_values(tactool: TACtool):
Function to test the functionality of the Reset IDs button.
"""
# Add some Analysis Points
tactool.graphics_view.left_click.emit(101, 101)
tactool.graphics_view.left_click.emit(202, 202)
tactool.graphics_view.left_click.emit(303, 303)
tactool.graphics_view.left_click.emit(404, 404)
tactool.graphics_view.left_click.emit(505, 505)
click_viewport(tactool.graphics_view, 101, 101, Qt.MouseButton.LeftButton)
click_viewport(tactool.graphics_view, 202, 202, Qt.MouseButton.LeftButton)
click_viewport(tactool.graphics_view, 303, 303, Qt.MouseButton.LeftButton)
click_viewport(tactool.graphics_view, 404, 404, Qt.MouseButton.LeftButton)
click_viewport(tactool.graphics_view, 505, 505, Qt.MouseButton.LeftButton)

# Remove the 1st and 4th Analysis Points
# This will make the ID values go 2, 3, 5
tactool.graphics_view.right_click.emit(101, 101)
tactool.graphics_view.right_click.emit(404, 404)
click_viewport(tactool.graphics_view, 101, 101, Qt.MouseButton.RightButton)
click_viewport(tactool.graphics_view, 404, 404, Qt.MouseButton.RightButton)

# Simulate a button click of the Reset IDs button
tactool.window.reset_ids_button.click()
QTest.mouseClick(tactool.window.reset_ids_button, Qt.MouseButton.LeftButton)

# Iterate through each actual Analysis Point
for current_id, analysis_point in enumerate(tactool.table_model.analysis_points):
Expand All @@ -164,12 +168,12 @@ def test_reset_settings(tactool: TACtool):
)

# Simulate a button click of the Reset Settings button
tactool.window.reset_settings_button.click()
QTest.mouseClick(tactool.window.reset_settings_button, Qt.MouseButton.LeftButton)

# Add some points, these should now have the default settings and metadata
tactool.graphics_view.left_click.emit(101, 101)
tactool.graphics_view.left_click.emit(202, 202)
tactool.graphics_view.left_click.emit(303, 303)
click_viewport(tactool.graphics_view, 101, 101, Qt.MouseButton.LeftButton)
click_viewport(tactool.graphics_view, 202, 202, Qt.MouseButton.LeftButton)
click_viewport(tactool.graphics_view, 303, 303, Qt.MouseButton.LeftButton)

expected_settings = [
tactool.window.default_settings["label"],
Expand Down Expand Up @@ -206,17 +210,17 @@ def test_reference_point_hint(tactool: TACtool):

# Add 3 analysis points with the label 'RefMark'
tactool.window.label_input.setCurrentText("RefMark")
tactool.graphics_view.left_click.emit(100, 100)
tactool.graphics_view.left_click.emit(150, 150)
tactool.graphics_view.left_click.emit(200, 200)
click_viewport(tactool.graphics_view, 100, 100, Qt.MouseButton.LeftButton)
click_viewport(tactool.graphics_view, 150, 150, Qt.MouseButton.LeftButton)
click_viewport(tactool.graphics_view, 200, 200, Qt.MouseButton.LeftButton)

# Check reference Points hint not is visible
ref_points_status = tactool.window.status_bar_messages["ref_points"]["status"]
assert ref_points_status is None
assert ref_points_status not in tactool.window.status_bar.children()

# Remove 1 Analysis Point with label 'RefMark', bringing the total to 2 reference Points
tactool.graphics_view.right_click.emit(100, 100)
click_viewport(tactool.graphics_view, 100, 100, Qt.MouseButton.RightButton)

# Check reference Points hint is visible
ref_points_status = tactool.window.status_bar_messages["ref_points"]["status"]
Expand All @@ -225,7 +229,7 @@ def test_reference_point_hint(tactool: TACtool):

# Add 1 Analysis Point with label 'Spot', keeping the total at 2 reference Points
tactool.window.label_input.setCurrentText("Spot")
tactool.graphics_view.left_click.emit(100, 100)
click_viewport(tactool.graphics_view, 100, 100, Qt.MouseButton.LeftButton)

# Check reference Points hint is visible
ref_points_status = tactool.window.status_bar_messages["ref_points"]["status"]
Expand Down
30 changes: 17 additions & 13 deletions test/test_scaling.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from PyQt5.QtCore import Qt
from PyQt5.QtTest import QTest

from tactool.main import TACtool
from conftest import click_viewport


def test_toggle_scaling_mode(tactool: TACtool):
Expand All @@ -14,7 +18,7 @@ def test_toggle_scaling_mode(tactool: TACtool):
assert tactool.graphics_scene.scaling_rect is None

# Start the scaling mode
tactool.window.toggle_scaling_mode()
QTest.mouseClick(tactool.window.set_scale_button, Qt.MouseButton.LeftButton)

# Check that the SetScaleDialog does exist
assert tactool.window.set_scale_dialog is not None
Expand All @@ -26,7 +30,7 @@ def test_toggle_scaling_mode(tactool: TACtool):

# Set the scale, following the same steps as the user would
tactool.window.set_scale_dialog.scale_value.setText(str(2.0))
tactool.window.set_scale_dialog.set_scale()
QTest.mouseClick(tactool.window.set_scale_dialog.set_scale_button, Qt.MouseButton.LeftButton)

# Check that the SetScaleDialog does not exist
assert tactool.window.set_scale_dialog is None
Expand All @@ -43,14 +47,14 @@ def test_set_scale(tactool: TACtool):
"""
# Set the scale, following the same steps as the user would
scale = 2.0
tactool.window.toggle_scaling_mode()
QTest.mouseClick(tactool.window.set_scale_button, Qt.MouseButton.LeftButton)
tactool.set_scale_dialog.scale_value.setText(str(scale))
tactool.set_scale_dialog.set_scale()
QTest.mouseClick(tactool.window.set_scale_dialog.set_scale_button, Qt.MouseButton.LeftButton)

# Add some points, these should now have the new scale
tactool.graphics_view.left_click.emit(101, 101)
tactool.graphics_view.left_click.emit(202, 202)
tactool.graphics_view.left_click.emit(303, 303)
click_viewport(tactool.graphics_view, 101, 101, Qt.MouseButton.LeftButton)
click_viewport(tactool.graphics_view, 202, 202, Qt.MouseButton.LeftButton)
click_viewport(tactool.graphics_view, 303, 303, Qt.MouseButton.LeftButton)

# Iterate through each actual Analysis Point
for analysis_point in tactool.table_model.analysis_points:
Expand All @@ -68,27 +72,27 @@ def test_scale_hint(tactool: TACtool):
assert set_scale_status not in tactool.window.status_bar.children()

# Add some points by clicking
tactool.graphics_view.left_click.emit(101, 101)
tactool.graphics_view.left_click.emit(202, 202)
tactool.graphics_view.left_click.emit(303, 303)
click_viewport(tactool.graphics_view, 101, 101, Qt.MouseButton.LeftButton)
click_viewport(tactool.graphics_view, 202, 202, Qt.MouseButton.LeftButton)
click_viewport(tactool.graphics_view, 303, 303, Qt.MouseButton.LeftButton)

# Check Set Scale hint is visible
set_scale_status = tactool.window.status_bar_messages["set_scale"]["status"]
assert set_scale_status is not None
assert set_scale_status in tactool.window.status_bar.children()

# Set the scale, following the same steps as the user would
tactool.window.toggle_scaling_mode()
QTest.mouseClick(tactool.window.set_scale_button, Qt.MouseButton.LeftButton)
tactool.set_scale_dialog.scale_value.setText(str(2.0))
tactool.set_scale_dialog.set_scale()
QTest.mouseClick(tactool.window.set_scale_dialog.set_scale_button, Qt.MouseButton.LeftButton)

# Check Set Scale hint is not visible
set_scale_status = tactool.window.status_bar_messages["set_scale"]["status"]
assert set_scale_status is None
assert set_scale_status not in tactool.window.status_bar.children()

# Reset the Scale value to the default value
tactool.window.reset_settings()
QTest.mouseClick(tactool.window.reset_settings_button, Qt.MouseButton.LeftButton)

# Check Set Scale hint is visible
set_scale_status = tactool.window.status_bar_messages["set_scale"]["status"]
Expand Down
Loading

0 comments on commit c7e8c49

Please sign in to comment.