Newer
Older

Thomas Holene Løkkeborg
committed
"""Test module that looks for differences in SOI modules across resolutions.
This was written as a regression test for #125. Ideally it should also test the
header of the SOI, but this is much more complicated to test for..
## Important
This test can not test across different resolutions by itself, this is up to
the user. Our pipeline will test across a couple of different resolutions.
"""
import unittest
from PySide2.QtWidgets import QApplication
from PySide2 import QtGui
from PySide2.QtCore import QTimer, Qt, QSysInfo

Thomas Holene Løkkeborg
committed
from PySide2.QtTest import QTest
from PySide2.QtGui import QGuiApplication

Thomas Holene Løkkeborg
committed
from soitool.soi import SOI
from soitool.modules.module_authentication_board import (
AuthenticationBoardModule,
)
from soitool.modules.module_subtractorcodes import SubtractorcodesModule
from soitool.modules.module_predefined_codes import PredefinedCodesModule
from soitool.modules.module_code_phrase import CodePhraseModule

Thomas Holene Løkkeborg
committed
from soitool.new_module_dialog import MODULE_CHOICES
from soitool.soi_workspace_widget import DATABASE_MODULES
from soitool.database import Database
# The error being ignored here is pylint telling us that 'test' is a standard
# module, so the import should be placed further up. In our case we have an
# actual custom module called 'test', so pylint is confused.
# pylint: disable=C0411
from test.test_database import TESTDBPATH

Thomas Holene Løkkeborg
committed
if isinstance(QtGui.qApp, type(None)):
app = QApplication([])
else:
app = QtGui.qApp
# Modules with a popup as part of their __init__
POPUP_MODULES = [
AuthenticationBoardModule,
SubtractorcodesModule,
PredefinedCodesModule,
]

Thomas Holene Løkkeborg
committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def screen_information():
"""Get string with information about the screen.
Returns
-------
str
String with screen information.
"""
screen_string = ""
screen = QGuiApplication.primaryScreen()
screen_string += "screen.size() -> " + str(screen.size()) + "\n"
screen_string += (
"screen.physicalDotsPerInchX() -> "
+ str(screen.physicalDotsPerInchX())
+ "\n"
)
screen_string += (
"screen.physicalDotsPerInchY() -> "
+ str(screen.physicalDotsPerInchY())
+ "\n"
)
screen_string += (
"screen.logicalDotsPerInchX() -> "
+ str(screen.logicalDotsPerInchX())
+ "\n"
)
screen_string += (
"screen.logicalDotsPerInchY() -> "
+ str(screen.logicalDotsPerInchY())
+ "\n"
)
screen_string += (
"screen.devicePixelRatio() -> " + str(screen.devicePixelRatio()) + "\n"
)
return screen_string

Thomas Holene Løkkeborg
committed
class TestModulesAcrossResolutions(unittest.TestCase):
"""TestCase for modules across resolutions."""
@unittest.skipUnless(
QSysInfo.productType() == "windows",
"Test currently only able to target Windows",
)

Thomas Holene Løkkeborg
committed
def test_add_all_modules(self):
"""Add all modules, reorganize, and assert result.
Expected result was gotten by simply running reorganize and noting down
the results. We should get the same result across different
resolutions.
Modules added to SOI must be the same every time the program is ran.
Test only works on Windows, as the expected result is based on the
Windows look-and-feel.
NOTE: This test needs to be updated when a new module is added,
deleted, or changed. It will fail until it is updated.

Thomas Holene Løkkeborg
committed
"""
expected_result = [
{"x": 0, "y": 0, "page": 1, "name": "PredefinedCodesModule"},
{"x": 654, "y": 0, "page": 1, "name": "AuthenticationBoardModule"},
{"x": 1061.5, "y": 0, "page": 1, "name": "SubtractorcodesModule"},
{"x": 1287.0, "y": 0, "page": 1, "name": "CodePhraseModule"},
{"x": 1489.0, "y": 0, "page": 1, "name": "FreeTextModule"},
{"x": 1589.0, "y": 0, "page": 1, "name": "TableModule"},

Thomas Holene Løkkeborg
committed
]
# For use with modules that require a database

Thomas Holene Løkkeborg
committed
def press_enter():
active_widget = app.activeModalWidget()
# AuthenticationBoardModule needs special treatment because the
# title can contain random info
if isinstance(active_widget, AuthenticationBoardModule):
# triple click to select existing text for overwrite
QTest.mouseDClick(active_widget.edit_headline, Qt.LeftButton)
QTest.mouseClick(active_widget.edit_headline, Qt.LeftButton)
# need to overwrite text because title otherwise contains
# unpredictable text
QTest.keyClicks(active_widget.edit_headline, "TestTitle")

Thomas Holene Løkkeborg
committed
QTest.keyClick(active_widget, Qt.Key_Enter)
soi = SOI()
for module in MODULE_CHOICES:
# If we're adding one of the modules with a popup as part of it's
# constructor we need to singleShot pressing enter to close it
if module in POPUP_MODULES:

Thomas Holene Løkkeborg
committed
QTimer.singleShot(0, press_enter)
if module in DATABASE_MODULES:
if module == CodePhraseModule:
# CodePhraseModule needs a category injected in it's init.
# Choosing "Tresort" because it contains words short enough to
# not expand the module beyond default width
module_instance = module(
database=database, category="Tresort"
)
else:
module_instance = module(database=database)
module_instance = module()
soi.add_module(module.__name__, module_instance)

Thomas Holene Løkkeborg
committed
soi.reorganize()
self.assertEqual(
expected_result,
[module["meta"] for module in soi.modules],
"All modules added to SOI and calling reorganize should result in "
"expected 'meta' information. Screen is: \n"
+ screen_information(),