1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# input-remapper - GUI for device specific keyboard mappings
# Copyright (C) 2025 sezanzeb <b8x45ygc9@mozmail.com>
#
# This file is part of input-remapper.
#
# input-remapper is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# input-remapper is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.
import time
import unittest
from typing import Optional, Tuple, Union
from unittest.mock import MagicMock, call
import evdev
import gi
from evdev.ecodes import KEY_A, KEY_B, KEY_C
gi.require_version("Gdk", "3.0")
gi.require_version("Gtk", "3.0")
gi.require_version("GLib", "2.0")
gi.require_version("GtkSource", "4")
from gi.repository import Gtk, GLib, GtkSource, Gdk
from tests.lib.spy import spy
from tests.lib.logger import logger
from inputremapper.gui.controller import Controller
from inputremapper.configs.keyboard_layout import XKB_KEYCODE_OFFSET
from inputremapper.gui.utils import CTX_ERROR, CTX_WARNING, gtk_iteration
from inputremapper.gui.messages.message_broker import (
MessageBroker,
MessageType,
)
from inputremapper.gui.messages.message_data import (
UInputsData,
GroupsData,
GroupData,
PresetData,
StatusData,
CombinationUpdate,
DoStackSwitch,
)
from inputremapper.groups import DeviceType
from inputremapper.gui.components.editor import (
TargetSelection,
MappingListBox,
MappingSelectionLabel,
CodeEditor,
RecordingToggle,
AutoloadSwitch,
ReleaseCombinationSwitch,
CombinationListbox,
InputConfigEntry,
AnalogInputSwitch,
TriggerThresholdInput,
ReleaseTimeoutInput,
OutputAxisSelector,
KeyAxisStackSwitcher,
Sliders,
TransformationDrawArea,
RelativeInputCutoffInput,
RecordingStatus,
RequireActiveMapping,
GdkEventRecorder,
)
from inputremapper.gui.components.main import Stack, StatusBar
from inputremapper.gui.components.common import FlowBoxEntry, Breadcrumbs
from inputremapper.gui.components.presets import PresetSelection
from inputremapper.gui.components.device_groups import (
DeviceGroupEntry,
DeviceGroupSelection,
)
from inputremapper.configs.mapping import MappingData
from inputremapper.configs.input_config import InputCombination, InputConfig
from tests.lib.test_setup import test_setup
class ComponentBaseTest(unittest.TestCase):
"""Test a gui component."""
def setUp(self) -> None:
self.message_broker = MessageBroker()
self.controller_mock: Controller = MagicMock()
def destroy_all_member_widgets(self):
# destroy all Gtk Widgets that are stored in self
# TODO why is this necessary?
for attribute in dir(self):
stuff = getattr(self, attribute, None)
if isinstance(stuff, Gtk.Widget):
logger.info('destroying member "%s" %s', attribute, stuff)
GLib.timeout_add(0, stuff.destroy)
setattr(self, attribute, None)
def tearDown(self) -> None:
super().tearDown()
self.message_broker.signal(MessageType.terminate)
# Shut down the gui properly
self.destroy_all_member_widgets()
GLib.timeout_add(0, Gtk.main_quit)
# Gtk.main() will start the Gtk event loop and process all pending events.
# So the gui will do whatever is queued up this ensures that the next tests
# starts without pending events.
Gtk.main()
class FlowBoxTestUtils:
"""Methods to test the FlowBoxes that contain presets and devices.
Those are only used in tests, so I moved them here instead.
"""
@staticmethod
def set_active(flow_box: Gtk.FlowBox, name: str):
"""Change the currently selected group."""
for child in flow_box.get_children():
flow_box_entry: FlowBoxEntry = child.get_children()[0]
flow_box_entry.set_active(flow_box_entry.name == name)
@staticmethod
def get_active_entry(flow_box: Gtk.FlowBox) -> Union[DeviceGroupEntry, None]:
"""Find the currently selected DeviceGroupEntry."""
children = flow_box.get_children()
if len(children) == 0:
return None
for child in children:
flow_box_entry: FlowBoxEntry = child.get_children()[0]
if flow_box_entry.get_active():
return flow_box_entry
raise AssertionError("Expected one entry to be selected.")
@staticmethod
def get_child_names(flow_box: Gtk.FlowBox):
names = []
for child in flow_box.get_children():
flow_box_entry: FlowBoxEntry = child.get_children()[0]
names.append(flow_box_entry.name)
return names
@staticmethod
def get_child_icons(flow_box: Gtk.FlowBox):
icon_names = []
for child in flow_box.get_children():
flow_box_entry: FlowBoxEntry = child.get_children()[0]
icon_names.append(flow_box_entry.icon_name)
return icon_names
@test_setup
class TestDeviceGroupSelection(ComponentBaseTest):
def setUp(self) -> None:
super().setUp()
self.gui = Gtk.FlowBox()
self.selection = DeviceGroupSelection(
self.message_broker,
self.controller_mock,
self.gui,
)
self.message_broker.publish(
GroupsData(
{
"foo": [DeviceType.GAMEPAD, DeviceType.KEYBOARD],
"bar": [],
"baz": [DeviceType.GRAPHICS_TABLET],
}
)
)
def get_displayed_group_keys_and_icons(self):
"""Get a list of all group_keys and icons of the displayed groups."""
group_keys = []
icons = []
for child in self.gui.get_children():
device_group_entry = child.get_children()[0]
group_keys.append(device_group_entry.group_key)
icons.append(device_group_entry.icon_name)
return group_keys, icons
def test_populates_devices(self):
# tests that all devices sent via the broker end up in the gui
group_keys, icons = self.get_displayed_group_keys_and_icons()
self.assertEqual(group_keys, ["foo", "bar", "baz"])
self.assertEqual(icons, ["input-gaming", None, "input-tablet"])
self.message_broker.publish(
GroupsData(
{
"kuu": [DeviceType.KEYBOARD],
"qux": [DeviceType.GAMEPAD],
}
)
)
group_keys, icons = self.get_displayed_group_keys_and_icons()
self.assertEqual(group_keys, ["kuu", "qux"])
self.assertEqual(icons, ["input-keyboard", "input-gaming"])
def test_selects_correct_device(self):
self.message_broker.publish(GroupData("bar", ()))
self.assertEqual(FlowBoxTestUtils.get_active_entry(self.gui).group_key, "bar")
self.message_broker.publish(GroupData("baz", ()))
self.assertEqual(FlowBoxTestUtils.get_active_entry(self.gui).group_key, "baz")
def test_loads_group(self):
FlowBoxTestUtils.set_active(self.gui, "bar")
self.controller_mock.load_group.assert_called_once_with("bar")
def test_avoids_infinite_recursion(self):
self.message_broker.publish(GroupData("bar", ()))
self.controller_mock.load_group.assert_not_called()
@test_setup
class TestTargetSelection(ComponentBaseTest):
def setUp(self) -> None:
super().setUp()
self.gui = Gtk.ComboBox()
self.selection = TargetSelection(
self.message_broker, self.controller_mock, self.gui
)
self.message_broker.publish(
UInputsData(
{
"foo": {},
"bar": {},
"baz": {},
}
)
)
def test_populates_devices(self):
names = [row[0] for row in self.gui.get_model()]
self.assertEqual(names, ["foo", "bar", "baz"])
self.message_broker.publish(
UInputsData(
{
"kuu": {},
"qux": {},
}
)
)
names = [row[0] for row in self.gui.get_model()]
self.assertEqual(names, ["kuu", "qux"])
def test_updates_mapping(self):
self.gui.set_active_id("baz")
self.controller_mock.update_mapping.assert_called_once_with(target_uinput="baz")
def test_selects_correct_target(self):
self.message_broker.publish(MappingData(target_uinput="baz"))
self.assertEqual(self.gui.get_active_id(), "baz")
self.message_broker.publish(MappingData(target_uinput="bar"))
self.assertEqual(self.gui.get_active_id(), "bar")
def test_avoids_infinite_recursion(self):
self.message_broker.publish(MappingData(target_uinput="baz"))
self.controller_mock.update_mapping.assert_not_called()
@test_setup
class TestPresetSelection(ComponentBaseTest):
def setUp(self) -> None:
super().setUp()
self.gui = Gtk.FlowBox()
self.selection = PresetSelection(
self.message_broker, self.controller_mock, self.gui
)
self.message_broker.publish(GroupData("foo", ("preset1", "preset2")))
def test_populates_presets(self):
names = FlowBoxTestUtils.get_child_names(self.gui)
self.assertEqual(names, ["preset1", "preset2"])
self.message_broker.publish(GroupData("foo", ("preset3", "preset4")))
names = FlowBoxTestUtils.get_child_names(self.gui)
self.assertEqual(names, ["preset3", "preset4"])
def test_selects_preset(self):
self.message_broker.publish(
PresetData(
"preset2",
(
MappingData(
name="m1",
input_combination=InputCombination(
[InputConfig(type=1, code=2)]
),
),
),
)
)
self.assertEqual(FlowBoxTestUtils.get_active_entry(self.gui).name, "preset2")
self.message_broker.publish(
PresetData(
"preset1",
(
MappingData(
name="m1",
input_combination=InputCombination(
[InputConfig(type=1, code=2)]
),
),
),
)
)
self.assertEqual(FlowBoxTestUtils.get_active_entry(self.gui).name, "preset1")
def test_avoids_infinite_recursion(self):
self.message_broker.publish(
PresetData(
"preset2",
(
MappingData(
name="m1",
input_combination=InputCombination(
[InputConfig(type=1, code=2)]
),
),
),
)
)
self.controller_mock.load_preset.assert_not_called()
def test_loads_preset(self):
FlowBoxTestUtils.set_active(self.gui, "preset2")
self.controller_mock.load_preset.assert_called_once_with("preset2")
@test_setup
class TestMappingListbox(ComponentBaseTest):
def setUp(self) -> None:
super().setUp()
self.gui = Gtk.ListBox()
self.listbox = MappingListBox(
self.message_broker, self.controller_mock, self.gui
)
self.message_broker.publish(
PresetData(
"preset1",
(
MappingData(
name="mapping1",
input_combination=InputCombination(
[InputConfig(type=1, code=KEY_C)]
),
),
MappingData(
name="",
input_combination=InputCombination(
[
InputConfig(type=1, code=KEY_A),
InputConfig(type=1, code=KEY_B),
]
),
),
MappingData(
name="mapping2",
input_combination=InputCombination(
[InputConfig(type=1, code=KEY_B)]
),
),
),
)
)
def get_selected_row(self) -> MappingSelectionLabel:
for label in self.gui.get_children():
if label.is_selected():
return label
raise Exception("Expected one MappingSelectionLabel to be selected")
def select_row(self, combination: InputCombination):
def select(label_: MappingSelectionLabel):
if label_.combination == combination:
self.gui.select_row(label_)
for label in self.gui.get_children():
select(label)
def test_populates_listbox(self):
labels = {row.name for row in self.gui.get_children()}
self.assertEqual(labels, {"mapping1", "mapping2", "a + b"})
def test_alphanumerically_sorted(self):
labels = [row.name for row in self.gui.get_children()]
self.assertEqual(labels, ["a + b", "mapping1", "mapping2"])
def test_activates_correct_row(self):
self.message_broker.publish(
MappingData(
name="mapping1",
input_combination=InputCombination([InputConfig(type=1, code=KEY_C)]),
)
)
selected = self.get_selected_row()
self.assertEqual(selected.name, "mapping1")
self.assertEqual(
selected.combination,
InputCombination([InputConfig(type=1, code=KEY_C)]),
)
def test_loads_mapping(self):
self.select_row(InputCombination([InputConfig(type=1, code=KEY_B)]))
self.controller_mock.load_mapping.assert_called_once_with(
InputCombination([InputConfig(type=1, code=KEY_B)])
)
def test_avoids_infinite_recursion(self):
self.message_broker.publish(
MappingData(
name="mapping1",
input_combination=InputCombination([InputConfig(type=1, code=KEY_C)]),
)
)
self.controller_mock.load_mapping.assert_not_called()
def test_sorts_empty_mapping_to_bottom(self):
self.message_broker.publish(
PresetData(
"preset1",
(
MappingData(
name="qux",
input_combination=InputCombination(
[InputConfig(type=1, code=KEY_C)]
),
),
MappingData(
name="foo",
input_combination=InputCombination.empty_combination(),
),
MappingData(
name="bar",
input_combination=InputCombination(
[InputConfig(type=1, code=KEY_B)]
),
),
),
)
)
bottom_row: MappingSelectionLabel = self.gui.get_row_at_index(2)
self.assertEqual(bottom_row.combination, InputCombination.empty_combination())
self.message_broker.publish(
PresetData(
"preset1",
(
MappingData(
name="foo",
input_combination=InputCombination.empty_combination(),
),
MappingData(
name="qux",
input_combination=InputCombination(
[InputConfig(type=1, code=KEY_C)]
),
),
MappingData(
name="bar",
input_combination=InputCombination(
[InputConfig(type=1, code=KEY_B)]
),
),
),
)
)
bottom_row: MappingSelectionLabel = self.gui.get_row_at_index(2)
self.assertEqual(bottom_row.combination, InputCombination.empty_combination())
@test_setup
class TestMappingSelectionLabel(ComponentBaseTest):
def setUp(self) -> None:
super().setUp()
self.gui = Gtk.ListBox()
self.mapping_selection_label = MappingSelectionLabel(
self.message_broker,
self.controller_mock,
"",
InputCombination(
[
InputConfig(type=1, code=KEY_A),
InputConfig(type=1, code=KEY_B),
]
),
)
self.gui.insert(self.mapping_selection_label, -1)
def assert_edit_mode(self):
self.assertTrue(self.mapping_selection_label.name_input.get_visible())
self.assertFalse(self.mapping_selection_label.label.get_visible())
def assert_selected(self):
self.assertTrue(self.mapping_selection_label.label.get_visible())
self.assertFalse(self.mapping_selection_label.name_input.get_visible())
def test_repr(self):
self.mapping_selection_label.name = "name"
self.assertIn("name", repr(self.mapping_selection_label))
self.assertIn("KEY_A", repr(self.mapping_selection_label))
self.assertIn("KEY_B", repr(self.mapping_selection_label))
def test_shows_combination_without_name(self):
self.assertEqual(self.mapping_selection_label.label.get_label(), "a + b")
def test_shows_name_when_given(self):
self.gui = MappingSelectionLabel(
self.message_broker,
self.controller_mock,
"foo",
InputCombination(
(
InputConfig(type=1, code=KEY_A),
InputConfig(type=1, code=KEY_B),
)
),
)
self.assertEqual(self.gui.label.get_label(), "foo")
def test_updates_combination_when_selected(self):
self.gui.select_row(self.mapping_selection_label)
self.assertEqual(
self.mapping_selection_label.combination,
InputCombination(
(
InputConfig(type=1, code=KEY_A),
InputConfig(type=1, code=KEY_B),
)
),
)
self.message_broker.publish(
CombinationUpdate(
InputCombination(
(
InputConfig(type=1, code=KEY_A),
InputConfig(type=1, code=KEY_B),
)
),
InputCombination([InputConfig(type=1, code=KEY_A)]),
)
)
self.assertEqual(
self.mapping_selection_label.combination,
InputCombination([InputConfig(type=1, code=KEY_A)]),
)
def test_doesnt_update_combination_when_not_selected(self):
self.assertEqual(
self.mapping_selection_label.combination,
InputCombination(
(
InputConfig(type=1, code=KEY_A),
InputConfig(type=1, code=KEY_B),
)
),
)
self.message_broker.publish(
CombinationUpdate(
InputCombination(
(
InputConfig(type=1, code=KEY_A),
InputConfig(type=1, code=KEY_B),
)
),
InputCombination([InputConfig(type=1, code=KEY_A)]),
)
)
self.assertEqual(
self.mapping_selection_label.combination,
InputCombination(
(
InputConfig(type=1, code=KEY_A),
InputConfig(type=1, code=KEY_B),
)
),
)
def test_updates_name_when_mapping_changed_and_combination_matches(self):
self.message_broker.publish(
MappingData(
input_combination=InputCombination(
(
InputConfig(type=1, code=KEY_A),
InputConfig(type=1, code=KEY_B),
)
),
name="foo",
)
)
self.assertEqual(self.mapping_selection_label.label.get_label(), "foo")
def test_ignores_mapping_when_combination_does_not_match(self):
self.message_broker.publish(
MappingData(
input_combination=InputCombination(
(
InputConfig(type=1, code=KEY_A),
InputConfig(type=1, code=KEY_C),
)
),
name="foo",
)
)
self.assertEqual(self.mapping_selection_label.label.get_label(), "a + b")
def test_edit_button_visibility(self):
# start off invisible
self.assertFalse(self.mapping_selection_label.edit_btn.get_visible())
# load the mapping associated with the ListBoxRow
self.message_broker.publish(
MappingData(
input_combination=InputCombination(
(
InputConfig(type=1, code=KEY_A),
InputConfig(type=1, code=KEY_B),
)
),
)
)
self.assertTrue(self.mapping_selection_label.edit_btn.get_visible())
# load a different row
self.message_broker.publish(
MappingData(
input_combination=InputCombination(
(
InputConfig(type=1, code=KEY_A),
InputConfig(type=1, code=KEY_C),
)
),
)
)
self.assertFalse(self.mapping_selection_label.edit_btn.get_visible())
def test_enter_edit_mode_focuses_name_input(self):
self.message_broker.publish(
MappingData(
input_combination=InputCombination(
(
InputConfig(type=1, code=KEY_A),
InputConfig(type=1, code=KEY_B),
)
),
)
)
self.mapping_selection_label.edit_btn.clicked()
self.controller_mock.set_focus.assert_called_once_with(
self.mapping_selection_label.name_input
)
def test_enter_edit_mode_updates_visibility(self):
self.message_broker.publish(
MappingData(
input_combination=InputCombination(
(
InputConfig(type=1, code=KEY_A),
InputConfig(type=1, code=KEY_B),
)
),
)
)
self.assert_selected()
self.mapping_selection_label.edit_btn.clicked()
self.assert_edit_mode()
self.mapping_selection_label.name_input.activate() # aka hit the return key
self.assert_selected()
def test_leaves_edit_mode_on_esc(self):
self.message_broker.publish(
MappingData(
input_combination=InputCombination(
(
InputConfig(type=1, code=KEY_A),
InputConfig(type=1, code=KEY_B),
)
),
)
)
self.mapping_selection_label.edit_btn.clicked()
self.assert_edit_mode()
self.mapping_selection_label.name_input.set_text("foo")
event = Gdk.Event()
event.key.keyval = Gdk.KEY_Escape
# send the "key-press-event"
self.mapping_selection_label._on_gtk_rename_abort(None, event.key)
self.assert_selected()
self.assertEqual(self.mapping_selection_label.label.get_text(), "a + b")
self.controller_mock.update_mapping.assert_not_called()
def test_update_name(self):
self.message_broker.publish(
MappingData(
input_combination=InputCombination(
(
InputConfig(type=1, code=KEY_A),
InputConfig(type=1, code=KEY_B),
)
),
)
)
self.mapping_selection_label.edit_btn.clicked()
self.mapping_selection_label.name_input.set_text("foo")
self.mapping_selection_label.name_input.activate()
self.controller_mock.update_mapping.assert_called_once_with(name="foo")
def test_name_input_contains_combination_when_name_not_set(self):
self.message_broker.publish(
MappingData(
input_combination=InputCombination(
(
InputConfig(type=1, code=KEY_A),
InputConfig(type=1, code=KEY_B),
)
),
)
)
self.mapping_selection_label.edit_btn.clicked()
self.assertEqual(self.mapping_selection_label.name_input.get_text(), "a + b")
def test_name_input_contains_name(self):
self.message_broker.publish(
MappingData(
input_combination=InputCombination(
(
InputConfig(type=1, code=KEY_A),
InputConfig(type=1, code=KEY_B),
)
),
name="foo",
)
)
self.mapping_selection_label.edit_btn.clicked()
self.assertEqual(self.mapping_selection_label.name_input.get_text(), "foo")
def test_removes_name_when_name_matches_combination(self):
self.message_broker.publish(
MappingData(
input_combination=InputCombination(
(
InputConfig(type=1, code=KEY_A),
InputConfig(type=1, code=KEY_B),
)
),
name="foo",
)
)
self.mapping_selection_label.edit_btn.clicked()
self.mapping_selection_label.name_input.set_text("a + b")
self.mapping_selection_label.name_input.activate()
self.controller_mock.update_mapping.assert_called_once_with(name="")
@test_setup
class TestGdkEventRecorder(ComponentBaseTest):
def _emit_key(self, window, code, type_):
event = Gdk.Event()
event.type = type_
event.hardware_keycode = code + XKB_KEYCODE_OFFSET
window.emit("event", event)
gtk_iteration()
def _emit_button(self, window, button, type_):
event = Gdk.Event()
event.type = type_
event.button = button
window.emit("event", event)
gtk_iteration()
def test_records_combinations(self):
label = Gtk.Label()
window = Gtk.Window()
GdkEventRecorder(window, label)
self._emit_key(window, KEY_A, Gdk.EventType.KEY_PRESS)
self._emit_key(window, KEY_B, Gdk.EventType.KEY_PRESS)
self.assertEqual(label.get_text(), "a + b")
self._emit_key(window, KEY_A, Gdk.EventType.KEY_RELEASE)
self._emit_key(window, KEY_B, Gdk.EventType.KEY_RELEASE)
self.assertEqual(label.get_text(), "a + b")
self._emit_key(window, KEY_C, Gdk.EventType.KEY_PRESS)
self.assertEqual(label.get_text(), "c")
# buttons
self._emit_button(window, Gdk.BUTTON_PRIMARY, Gdk.EventType.BUTTON_PRESS)
self._emit_button(window, Gdk.BUTTON_SECONDARY, Gdk.EventType.BUTTON_PRESS)
self._emit_button(window, Gdk.BUTTON_MIDDLE, Gdk.EventType.BUTTON_PRESS)
# no constants seem to exist, but this is the value that was observed during
# usage:
self._emit_button(window, 8, Gdk.EventType.BUTTON_PRESS)
self._emit_button(window, 9, Gdk.EventType.BUTTON_PRESS)
self.assertEqual(
label.get_text(),
"c + BTN_LEFT + BTN_RIGHT + BTN_MIDDLE + BTN_SIDE + BTN_EXTRA",
)
# releasing anything resets the combination
self._emit_button(window, 9, Gdk.EventType.BUTTON_RELEASE)
self._emit_key(window, KEY_A, Gdk.EventType.KEY_PRESS)
self.assertEqual(label.get_text(), "a")
@test_setup
class TestCodeEditor(ComponentBaseTest):
def setUp(self) -> None:
super().setUp()
self.gui = GtkSource.View()
self.editor = CodeEditor(self.message_broker, self.controller_mock, self.gui)
# TODO why is mocking this to False needed?
self.controller_mock.is_empty_mapping.return_value = False
def get_text(self) -> str:
buffer = self.gui.get_buffer()
return buffer.get_text(buffer.get_start_iter(), buffer.get_end_iter(), True)
def test_shows_output_symbol(self):
self.message_broker.publish(MappingData(output_symbol="foo"))
self.assertEqual(self.get_text(), "foo")
def test_shows_record_input_first_message_when_mapping_is_empty(self):
self.controller_mock.is_empty_mapping.return_value = True
self.message_broker.publish(MappingData(output_symbol="foo"))
self.assertEqual(self.get_text(), "Record the input first")
def test_active_when_mapping_is_not_empty(self):
self.message_broker.publish(MappingData(output_symbol="foo"))
self.assertTrue(self.gui.get_sensitive())
self.assertEqual(self.gui.get_opacity(), 1)
def test_expands_to_multiline(self):
self.message_broker.publish(MappingData(output_symbol="foo\nbar"))
self.assertIn("multiline", self.gui.get_style_context().list_classes())
def test_shows_line_numbers_when_multiline(self):
self.message_broker.publish(MappingData(output_symbol="foo\nbar"))
self.assertTrue(self.gui.get_show_line_numbers())
def test_no_multiline_when_macro_not_multiline(self):
self.message_broker.publish(MappingData(output_symbol="foo"))
self.assertNotIn("multiline", self.gui.get_style_context().list_classes())
def test_no_line_numbers_macro_not_multiline(self):
self.message_broker.publish(MappingData(output_symbol="foo"))
self.assertFalse(self.gui.get_show_line_numbers())
def test_shows_placeholder_when_mapping_has_no_output_symbol(self):
self.message_broker.publish(MappingData())
self.assertEqual(self.get_text(), self.editor.placeholder)
# there are no side-effects because the placeholder is inserted:
self.controller_mock.update_mapping.assert_not_called()
def test_updates_mapping(self):
self.message_broker.publish(MappingData())
buffer = self.gui.get_buffer()
self.controller_mock.update_mapping.assert_not_called()
buffer.set_text("foo")
call_args_list = self.controller_mock.update_mapping.call_args_list
# this test emits 2 events for whatever reason, the first one with an empty
# symbol. this doesn't actually seem to happen when using it.
self.assertEqual(call_args_list[-1], call(output_symbol="foo"))
def test_avoids_infinite_recursion_when_loading_mapping(self):
self.message_broker.publish(MappingData(output_symbol="foo"))
self.controller_mock.update_mapping.assert_not_called()
def test_gets_focus_when_input_recording_finises(self):
self.message_broker.signal(MessageType.recording_finished)
self.controller_mock.set_focus.assert_called_once_with(self.gui)
def test_placeholder(self):
self.assertEqual(self.get_text(), self.editor.placeholder)
window = Gtk.Window()
window.add(self.gui)
window.show_all()
def focus():
self.gui.grab_focus()
# Do as many iterations as needed to make it work.
gtk_iteration(15)
def unfocus():
window.set_focus(None)
gtk_iteration(15)
# clears the input when we enter the editor widget
focus()
self.assertEqual(self.get_text(), "")
self.assertNotIn("opaque-text", self.gui.get_style_context().list_classes())
# adds the placeholder back when we leave it
unfocus()
self.assertEqual(self.get_text(), self.editor.placeholder)
self.assertIn("opaque-text", self.gui.get_style_context().list_classes())
# if we enter text and then leave, it won't show the placeholder
focus()
self.assertEqual(self.get_text(), "")
buffer = self.gui.get_buffer()
buffer.set_text("foo")
self.assertNotIn("opaque-text", self.gui.get_style_context().list_classes())
unfocus()
self.assertEqual(self.get_text(), "foo")
self.assertNotIn("opaque-text", self.gui.get_style_context().list_classes())
@test_setup
class TestRecordingToggle(ComponentBaseTest):
def setUp(self) -> None:
super().setUp()
self.toggle_button = Gtk.ToggleButton()
self.recording_toggle = RecordingToggle(
self.message_broker,
self.controller_mock,
self.toggle_button,
)
self.label = Gtk.Label()
self.recording_status = RecordingStatus(self.message_broker, self.label)
def assert_not_recording(self):
self.assertFalse(self.label.get_visible())
self.assertFalse(self.toggle_button.get_active())
def test_starts_recording(self):
self.toggle_button.set_active(True)
self.controller_mock.start_key_recording.assert_called_once()
def test_stops_recording_when_clicked(self):
self.toggle_button.set_active(True)
self.toggle_button.set_active(False)
self.controller_mock.stop_key_recording.assert_called_once()
def test_not_recording_initially(self):
self.assert_not_recording()
def test_shows_recording_when_message_sent(self):
self.assertFalse(self.label.get_visible())
self.message_broker.signal(MessageType.recording_started)
self.assertTrue(self.label.get_visible())
def test_shows_not_recording_after_toggle(self):
self.toggle_button.set_active(True)
self.toggle_button.set_active(False)
self.assert_not_recording()
def test_shows_not_recording_when_recording_finished(self):
self.toggle_button.set_active(True)
self.message_broker.signal(MessageType.recording_finished)
self.assert_not_recording()
@test_setup
class TestStatusBar(ComponentBaseTest):
def setUp(self) -> None:
super().setUp()
self.gui = Gtk.Statusbar()
self.err_icon = Gtk.Image()
self.warn_icon = Gtk.Image()
self.statusbar = StatusBar(
self.message_broker,
self.controller_mock,
self.gui,
self.err_icon,
self.warn_icon,
)
self.message_broker.signal(MessageType.init)
def assert_empty(self):
self.assertFalse(self.err_icon.get_visible())
self.assertFalse(self.warn_icon.get_visible())
self.assertEqual(self.get_text(), "")
self.assertIsNone(self.get_tooltip())
def assert_error_status(self):
self.assertTrue(self.err_icon.get_visible())
self.assertFalse(self.warn_icon.get_visible())
def assert_warning_status(self):
self.assertFalse(self.err_icon.get_visible())
self.assertTrue(self.warn_icon.get_visible())
def get_text(self) -> str:
return self.gui.get_message_area().get_children()[0].get_text()
def get_tooltip(self) -> Optional[str]:
return self.gui.get_tooltip_text()
def test_starts_empty(self):
self.assert_empty()
def test_shows_error_status(self):
self.message_broker.publish(StatusData(CTX_ERROR, "msg", "tooltip"))
self.assertEqual(self.get_text(), "msg")
self.assertEqual(self.get_tooltip(), "tooltip")
self.assert_error_status()
def test_shows_warning_status(self):
self.message_broker.publish(StatusData(CTX_WARNING, "msg", "tooltip"))
self.assertEqual(self.get_text(), "msg")
self.assertEqual(self.get_tooltip(), "tooltip")
self.assert_warning_status()
def test_shows_newest_message(self):
self.message_broker.publish(StatusData(CTX_ERROR, "msg", "tooltip"))
self.message_broker.publish(StatusData(CTX_WARNING, "msg2", "tooltip2"))
self.assertEqual(self.get_text(), "msg2")
self.assertEqual(self.get_tooltip(), "tooltip2")
self.assert_warning_status()
def test_data_without_message_removes_messages(self):
self.message_broker.publish(StatusData(CTX_WARNING, "msg", "tooltip"))
self.message_broker.publish(StatusData(CTX_WARNING, "msg2", "tooltip2"))
self.message_broker.publish(StatusData(CTX_WARNING))
self.assert_empty()
def test_restores_message_from_not_removed_ctx_id(self):
self.message_broker.publish(StatusData(CTX_ERROR, "msg", "tooltip"))
self.message_broker.publish(StatusData(CTX_WARNING, "msg2", "tooltip2"))
self.message_broker.publish(StatusData(CTX_WARNING))
self.assertEqual(self.get_text(), "msg")
self.assert_error_status()
# works also the other way round
self.message_broker.publish(StatusData(CTX_ERROR))
self.message_broker.publish(StatusData(CTX_WARNING, "msg", "tooltip"))
self.message_broker.publish(StatusData(CTX_ERROR, "msg2", "tooltip2"))
self.message_broker.publish(StatusData(CTX_ERROR))
self.assertEqual(self.get_text(), "msg")
self.assert_warning_status()
def test_sets_msg_as_tooltip_if_tooltip_is_none(self):
self.message_broker.publish(StatusData(CTX_ERROR, "msg"))
self.assertEqual(self.get_tooltip(), "msg")
@test_setup
class TestAutoloadSwitch(ComponentBaseTest):
def setUp(self) -> None:
super().setUp()
self.gui = Gtk.Switch()
self.switch = AutoloadSwitch(
self.message_broker, self.controller_mock, self.gui
)
def test_sets_autoload(self):
self.gui.set_active(True)
self.controller_mock.set_autoload.assert_called_once_with(True)
self.controller_mock.reset_mock()
self.gui.set_active(False)
self.controller_mock.set_autoload.assert_called_once_with(False)
def test_updates_state(self):
self.message_broker.publish(PresetData(None, None, autoload=True))
self.assertTrue(self.gui.get_active())
self.message_broker.publish(PresetData(None, None, autoload=False))
self.assertFalse(self.gui.get_active())
def test_avoids_infinite_recursion(self):
self.message_broker.publish(PresetData(None, None, autoload=True))
self.message_broker.publish(PresetData(None, None, autoload=False))
self.controller_mock.set_autoload.assert_not_called()
@test_setup
class TestReleaseCombinationSwitch(ComponentBaseTest):
def setUp(self) -> None:
super().setUp()
self.gui = Gtk.Switch()
self.switch = ReleaseCombinationSwitch(
self.message_broker, self.controller_mock, self.gui
)
def test_updates_mapping(self):
self.gui.set_active(True)
self.controller_mock.update_mapping.assert_called_once_with(
release_combination_keys=True
)
self.controller_mock.reset_mock()
self.gui.set_active(False)
self.controller_mock.update_mapping.assert_called_once_with(
release_combination_keys=False
)
def test_updates_state(self):
self.message_broker.publish(MappingData(release_combination_keys=True))
self.assertTrue(self.gui.get_active())
self.message_broker.publish(MappingData(release_combination_keys=False))
self.assertFalse(self.gui.get_active())
def test_avoids_infinite_recursion(self):
self.message_broker.publish(MappingData(release_combination_keys=True))
self.message_broker.publish(MappingData(release_combination_keys=False))
self.controller_mock.update_mapping.assert_not_called()
@test_setup
class TestEventEntry(ComponentBaseTest):
def setUp(self) -> None:
super().setUp()
self.gui = InputConfigEntry(
InputConfig(type=3, code=0, analog_threshold=1), self.controller_mock
)
def test_move_event(self):
self.gui._up_btn.clicked()
self.controller_mock.move_input_config_in_combination.assert_called_once_with(
InputConfig(type=3, code=0, analog_threshold=1), "up"
)
self.controller_mock.reset_mock()
self.gui._down_btn.clicked()
self.controller_mock.move_input_config_in_combination.assert_called_once_with(
InputConfig(type=3, code=0, analog_threshold=1), "down"
)
@test_setup
class TestCombinationListbox(ComponentBaseTest):
def setUp(self) -> None:
super().setUp()
self.gui = Gtk.ListBox()
self.listbox = CombinationListbox(
self.message_broker, self.controller_mock, self.gui
)
self.controller_mock.is_empty_mapping.return_value = False
combination = InputCombination(
(
InputConfig(type=1, code=1),
InputConfig(type=3, code=0, analog_threshold=1),
InputConfig(type=1, code=2),
)
)
self.message_broker.publish(
MappingData(
input_combination=combination.to_config(), target_uinput="keyboard"
)
)
def get_selected_row(self) -> InputConfigEntry:
for entry in self.gui.get_children():
if entry.is_selected():
return entry
raise Exception("Expected one InputConfigEntry to be selected")
def select_row(self, input_cfg: InputConfig):
for entry in self.gui.get_children():
if entry.input_event == input_cfg:
self.gui.select_row(entry)
def test_loads_selected_row(self):
self.select_row(InputConfig(type=1, code=2))
self.controller_mock.load_input_config.assert_called_once_with(
InputConfig(type=1, code=2)
)
def test_does_not_create_rows_when_mapping_is_empty(self):
self.controller_mock.is_empty_mapping.return_value = True
combination = InputCombination(
(
InputConfig(type=1, code=1),
InputConfig(type=3, code=0, analog_threshold=1),
)
)
self.message_broker.publish(MappingData(input_combination=combination))
self.assertEqual(len(self.gui.get_children()), 0)
def test_selects_row_when_selected_event_message_arrives(self):
self.message_broker.publish(InputConfig(type=3, code=0, analog_threshold=1))
self.assertEqual(
self.get_selected_row().input_event,
InputConfig(type=3, code=0, analog_threshold=1),
)
def test_avoids_infinite_recursion(self):
self.message_broker.publish(InputConfig(type=3, code=0, analog_threshold=1))
self.controller_mock.load_event.assert_not_called()
@test_setup
class TestAnalogInputSwitch(ComponentBaseTest):
def setUp(self) -> None:
super().setUp()
self.gui = Gtk.Switch()
self.switch = AnalogInputSwitch(
self.message_broker, self.controller_mock, self.gui
)
def test_updates_event_as_analog(self):
self.gui.set_active(True)
self.controller_mock.set_event_as_analog.assert_called_once_with(True)
self.controller_mock.reset_mock()
self.gui.set_active(False)
self.controller_mock.set_event_as_analog.assert_called_once_with(False)
def test_updates_state(self):
self.message_broker.publish(InputConfig(type=3, code=0))
self.assertTrue(self.gui.get_active())
self.message_broker.publish(InputConfig(type=3, code=0, analog_threshold=10))
self.assertFalse(self.gui.get_active())
def test_avoids_infinite_recursion(self):
self.message_broker.publish(InputConfig(type=3, code=0))
self.message_broker.publish(InputConfig(type=3, code=0, analog_threshold=-10))
self.controller_mock.set_event_as_analog.assert_not_called()
def test_disables_switch_when_key_event(self):
self.message_broker.publish(InputConfig(type=1, code=1))
self.assertLess(self.gui.get_opacity(), 0.6)
self.assertFalse(self.gui.get_sensitive())
def test_enables_switch_when_axis_event(self):
self.message_broker.publish(InputConfig(type=1, code=1))
self.message_broker.publish(InputConfig(type=3, code=0, analog_threshold=10))
self.assertEqual(self.gui.get_opacity(), 1)
self.assertTrue(self.gui.get_sensitive())
self.message_broker.publish(InputConfig(type=1, code=1))
self.message_broker.publish(InputConfig(type=2, code=0, analog_threshold=10))
self.assertEqual(self.gui.get_opacity(), 1)
self.assertTrue(self.gui.get_sensitive())
@test_setup
class TestTriggerThresholdInput(ComponentBaseTest):
def setUp(self) -> None:
super().setUp()
self.gui = Gtk.SpinButton()
self.input = TriggerThresholdInput(
self.message_broker, self.controller_mock, self.gui
)
self.message_broker.publish(InputConfig(type=3, code=0, analog_threshold=-10))
def assert_abs_event_config(self):
self.assertEqual(self.gui.get_range(), (-99, 99))
self.assertTrue(self.gui.get_sensitive())
self.assertEqual(self.gui.get_opacity(), 1)
def assert_rel_event_config(self):
self.assertEqual(self.gui.get_range(), (-999, 999))
self.assertTrue(self.gui.get_sensitive())
self.assertEqual(self.gui.get_opacity(), 1)
def assert_key_event_config(self):
self.assertFalse(self.gui.get_sensitive())
self.assertLess(self.gui.get_opacity(), 0.6)
def test_updates_event(self):
self.gui.set_value(15)
self.controller_mock.update_input_config.assert_called_once_with(
InputConfig(type=3, code=0, analog_threshold=15)
)
def test_sets_value_on_selected_event_message(self):
self.message_broker.publish(InputConfig(type=3, code=0, analog_threshold=10))
self.assertEqual(self.gui.get_value(), 10)
def test_avoids_infinite_recursion(self):
self.message_broker.publish(InputConfig(type=3, code=0, analog_threshold=10))
self.controller_mock.update_input_config.assert_not_called()
def test_updates_configuration_according_to_selected_event(self):
self.assert_abs_event_config()
self.message_broker.publish(InputConfig(type=2, code=0, analog_threshold=-10))
self.assert_rel_event_config()
self.message_broker.publish(InputConfig(type=1, code=1))
self.assert_key_event_config()
@test_setup
class TestReleaseTimeoutInput(ComponentBaseTest):
def setUp(self) -> None:
super().setUp()
self.gui = Gtk.SpinButton()
self.input = ReleaseTimeoutInput(
self.message_broker, self.controller_mock, self.gui
)
self.message_broker.publish(
MappingData(
input_combination=InputCombination(
[InputConfig(type=2, code=0, analog_threshold=1)]
),
target_uinput="keyboard",
)
)
def test_updates_timeout_on_mapping_message(self):
self.message_broker.publish(
MappingData(
input_combination=InputCombination(
[InputConfig(type=2, code=0, analog_threshold=1)]
),
release_timeout=1,
)
)
self.assertEqual(self.gui.get_value(), 1)
def test_updates_mapping(self):
self.gui.set_value(0.5)
self.controller_mock.update_mapping.assert_called_once_with(release_timeout=0.5)
def test_avoids_infinite_recursion(self):
self.message_broker.publish(
MappingData(
input_combination=InputCombination(
[InputConfig(type=2, code=0, analog_threshold=1)]
),
release_timeout=1,
)
)
self.controller_mock.update_mapping.assert_not_called()
def test_disables_input_based_on_input_combination(self):
self.message_broker.publish(
MappingData(
input_combination=InputCombination(
(
InputConfig(type=2, code=0, analog_threshold=1),
InputConfig(type=1, code=1),
)
)
)
)
self.assertTrue(self.gui.get_sensitive())
self.assertEqual(self.gui.get_opacity(), 1)
self.message_broker.publish(
MappingData(
input_combination=InputCombination(
(
InputConfig(type=1, code=1),
InputConfig(type=1, code=2),
)
)
)
)
self.assertFalse(self.gui.get_sensitive())
self.assertLess(self.gui.get_opacity(), 0.6)
self.message_broker.publish(
MappingData(
input_combination=InputCombination(
(
InputConfig(type=2, code=0, analog_threshold=1),
InputConfig(type=1, code=1),
)
)
)
)
self.message_broker.publish(
MappingData(
input_combination=InputCombination(
(
InputConfig(type=3, code=0, analog_threshold=1),
InputConfig(
type=1,
code=2,
),
)
)
)
)
self.assertFalse(self.gui.get_sensitive())
self.assertLess(self.gui.get_opacity(), 0.6)
@test_setup
class TestOutputAxisSelector(ComponentBaseTest):
def setUp(self) -> None:
super().setUp()
self.gui = Gtk.ComboBox()
self.selection = OutputAxisSelector(
self.message_broker, self.controller_mock, self.gui
)
absinfo = evdev.AbsInfo(0, -10, 10, 0, 0, 0)
self.message_broker.publish(
UInputsData(
{
"mouse": {1: [1, 2, 3, 4], 2: [0, 1, 2, 3]},
"keyboard": {1: [1, 2, 3, 4]},
"gamepad": {
2: [0, 1, 2, 3],
3: [(0, absinfo), (1, absinfo), (2, absinfo), (3, absinfo)],
},
}
)
)
self.message_broker.publish(
MappingData(
target_uinput="mouse",
input_combination=InputCombination([InputConfig(type=1, code=1)]),
)
)
def set_active_selection(self, selection: Tuple):
self.gui.set_active_id(f"{selection[0]}, {selection[1]}")
def get_active_selection(self) -> Tuple[int, int]:
return tuple(int(i) for i in self.gui.get_active_id().split(",")) # type: ignore
def test_updates_mapping(self):
self.set_active_selection((2, 0))
self.controller_mock.update_mapping.assert_called_once_with(
output_type=2, output_code=0
)
def test_updates_mapping_with_none(self):
self.set_active_selection((2, 0))
self.controller_mock.reset_mock()
self.set_active_selection((None, None))
self.controller_mock.update_mapping.assert_called_once_with(
output_type=None, output_code=None
)
def test_selects_correct_entry(self):
self.assertEqual(self.gui.get_active_id(), "None, None")
self.message_broker.publish(
MappingData(target_uinput="mouse", output_type=2, output_code=3)
)
self.assertEqual(self.get_active_selection(), (2, 3))
def test_avoids_infinite_recursion(self):
self.message_broker.publish(
MappingData(target_uinput="mouse", output_type=2, output_code=3)
)
self.controller_mock.update_mapping.assert_not_called()
def test_updates_dropdown_model(self):
self.assertEqual(len(self.gui.get_model()), 5)
self.message_broker.publish(MappingData(target_uinput="keyboard"))
self.assertEqual(len(self.gui.get_model()), 1)
self.message_broker.publish(MappingData(target_uinput="gamepad"))
self.assertEqual(len(self.gui.get_model()), 9)
@test_setup
class TestKeyAxisStackSwitcher(ComponentBaseTest):
def setUp(self) -> None:
super().setUp()
self.gui = Gtk.Box()
self.gtk_stack = Gtk.Stack()
self.analog_toggle = Gtk.ToggleButton()
self.key_toggle = Gtk.ToggleButton()
self.gui.add(self.gtk_stack)
self.gui.add(self.analog_toggle)
self.gui.add(self.key_toggle)
self.gtk_stack.add_named(Gtk.Box(), "Analog Axis")
self.gtk_stack.add_named(Gtk.Box(), "Key or Macro")
self.stack = KeyAxisStackSwitcher(
self.message_broker,
self.controller_mock,
self.gtk_stack,
self.key_toggle,
self.analog_toggle,
)
self.gui.show_all()
self.gtk_stack.set_visible_child_name("Key or Macro")
def assert_key_macro_active(self):
self.assertEqual(self.gtk_stack.get_visible_child_name(), "Key or Macro")
self.assertTrue(self.key_toggle.get_active())
self.assertFalse(self.analog_toggle.get_active())
def assert_analog_active(self):
self.assertEqual(self.gtk_stack.get_visible_child_name(), "Analog Axis")
self.assertFalse(self.key_toggle.get_active())
self.assertTrue(self.analog_toggle.get_active())
def test_switches_to_axis(self):
self.message_broker.publish(MappingData(mapping_type="analog"))
self.assert_analog_active()
def test_switches_to_key_macro(self):
self.message_broker.publish(MappingData(mapping_type="analog"))
self.message_broker.publish(MappingData(mapping_type="key_macro"))
self.assert_key_macro_active()
def test_updates_mapping_type(self):
self.key_toggle.set_active(True)
self.controller_mock.update_mapping.assert_called_once_with(
mapping_type="key_macro"
)
self.controller_mock.update_mapping.reset_mock()
self.analog_toggle.set_active(True)
self.controller_mock.update_mapping.assert_called_once_with(
mapping_type="analog"
)
def test_avoids_infinite_recursion(self):
self.message_broker.publish(MappingData(mapping_type="analog"))
self.message_broker.publish(MappingData(mapping_type="key_macro"))
self.controller_mock.update_mapping.assert_not_called()
@test_setup
class TestTransformationDrawArea(ComponentBaseTest):
def setUp(self) -> None:
super().setUp()
self.gui = Gtk.Window()
self.draw_area = Gtk.DrawingArea()
self.gui.add(self.draw_area)
self.transform_draw_area = TransformationDrawArea(
self.message_broker,
self.controller_mock,
self.draw_area,
)
def test_draws_transform(self):
with spy(self.transform_draw_area, "_transformation") as mock:
# show the window, it takes some time and iterations until it pops up
self.gui.show_all()
for _ in range(5):
gtk_iteration()
time.sleep(0.01)
mock.assert_called()
def test_updates_transform_when_mapping_updates(self):
old_tf = self.transform_draw_area._transformation
self.message_broker.publish(MappingData(gain=2))
self.assertIsNot(old_tf, self.transform_draw_area._transformation)
def test_redraws_when_mapping_updates(self):
self.gui.show_all()
gtk_iteration(20)
mock = MagicMock()
self.draw_area.connect("draw", mock)
self.message_broker.publish(MappingData(gain=2))
gtk_iteration(20)
mock.assert_called()
@test_setup
class TestSliders(ComponentBaseTest):
def setUp(self) -> None:
super().setUp()
self.gui = Gtk.Box()
self.gain = Gtk.Scale()
self.deadzone = Gtk.Scale()
self.expo = Gtk.Scale()
# add everything to a box: it will be cleand up properly
self.gui.add(self.gain)
self.gui.add(self.deadzone)
self.gui.add(self.expo)
self.sliders = Sliders(
self.message_broker,
self.controller_mock,
self.gain,
self.deadzone,
self.expo,
)
self.message_broker.publish(
MappingData(
input_combination=InputCombination([InputConfig(type=3, code=0)]),
target_uinput="mouse",
)
)
@staticmethod
def get_range(range: Gtk.Range) -> Tuple[int, int]:
"""the Gtk.Range, has no get_range method. this is a workaround"""
v = range.get_value()
range.set_value(-(2**16))
min_ = range.get_value()
range.set_value(2**16)
max_ = range.get_value()
range.set_value(v)
return min_, max_
def test_slider_ranges(self):
self.assertEqual(self.get_range(self.gain), (-2, 2))
self.assertEqual(self.get_range(self.deadzone), (0, 0.9))
self.assertEqual(self.get_range(self.expo), (-1, 1))
def test_updates_value(self):
self.message_broker.publish(
MappingData(
gain=0.5,
deadzone=0.6,
expo=0.3,
)
)
self.assertEqual(self.gain.get_value(), 0.5)
self.assertEqual(self.expo.get_value(), 0.3)
self.assertEqual(self.deadzone.get_value(), 0.6)
def test_gain_updates_mapping(self):
self.gain.set_value(0.5)
self.controller_mock.update_mapping.assert_called_once_with(gain=0.5)
def test_expo_updates_mapping(self):
self.expo.set_value(0.5)
self.controller_mock.update_mapping.assert_called_once_with(expo=0.5)
def test_deadzone_updates_mapping(self):
self.deadzone.set_value(0.5)
self.controller_mock.update_mapping.assert_called_once_with(deadzone=0.5)
def test_avoids_recursion(self):
self.message_broker.publish(MappingData(gain=0.5))
self.controller_mock.update_mapping.assert_not_called()
self.message_broker.publish(MappingData(expo=0.5))
self.controller_mock.update_mapping.assert_not_called()
self.message_broker.publish(MappingData(deadzone=0.5))
self.controller_mock.update_mapping.assert_not_called()
@test_setup
class TestRelativeInputCutoffInput(ComponentBaseTest):
def setUp(self) -> None:
super().setUp()
self.gui = Gtk.SpinButton()
self.input = RelativeInputCutoffInput(
self.message_broker, self.controller_mock, self.gui
)
self.message_broker.publish(
MappingData(
target_uinput="mouse",
input_combination=InputCombination([InputConfig(type=2, code=0)]),
rel_to_abs_input_cutoff=1,
output_type=3,
output_code=0,
)
)
def assert_active(self):
self.assertTrue(self.gui.get_sensitive())
self.assertEqual(self.gui.get_opacity(), 1)
def assert_inactive(self):
self.assertFalse(self.gui.get_sensitive())
self.assertLess(self.gui.get_opacity(), 0.6)
def test_avoids_infinite_recursion(self):
self.message_broker.publish(
MappingData(
target_uinput="mouse",
input_combination=InputCombination([InputConfig(type=2, code=0)]),
rel_to_abs_input_cutoff=3,
output_type=3,
output_code=0,
)
)
self.controller_mock.update_mapping.assert_not_called()
def test_updates_value(self):
rel_to_abs_input_cutoff = 3
self.message_broker.publish(
MappingData(
target_uinput="mouse",
input_combination=InputCombination([InputConfig(type=2, code=0)]),
rel_to_abs_input_cutoff=rel_to_abs_input_cutoff,
output_type=3,
output_code=0,
)
)
self.assertEqual(self.gui.get_value(), rel_to_abs_input_cutoff)
def test_updates_mapping(self):
self.gui.set_value(300)
self.controller_mock.update_mapping.assert_called_once_with(rel_xy_cutoff=300)
def test_disables_input_when_no_rel_axis_input(self):
self.assert_active()
self.message_broker.publish(
MappingData(
target_uinput="mouse",
input_combination=InputCombination([InputConfig(type=3, code=0)]),
output_type=3,
output_code=0,
)
)
self.assert_inactive()
def test_disables_input_when_no_abs_axis_output(self):
self.assert_active()
self.message_broker.publish(
MappingData(
target_uinput="mouse",
input_combination=InputCombination([InputConfig(type=2, code=0)]),
rel_to_abs_input_cutoff=3,
output_type=2,
output_code=0,
)
)
self.assert_inactive()
def test_enables_input(self):
self.message_broker.publish(
MappingData(
target_uinput="mouse",
input_combination=InputCombination([InputConfig(type=3, code=0)]),
output_type=3,
output_code=0,
)
)
self.assert_inactive()
self.message_broker.publish(
MappingData(
target_uinput="mouse",
input_combination=InputCombination([InputConfig(type=2, code=0)]),
rel_to_abs_input_cutoff=1,
output_type=3,
output_code=0,
)
)
self.assert_active()
@test_setup
class TestRequireActiveMapping(ComponentBaseTest):
def test_no_reqorded_input_required(self):
self.box = Gtk.Box()
RequireActiveMapping(
self.message_broker,
self.box,
require_recorded_input=False,
)
combination = InputCombination([InputConfig(type=1, code=KEY_A)])
self.message_broker.publish(MappingData())
self.assert_inactive(self.box)
self.message_broker.publish(PresetData(name="preset", mappings=()))
self.assert_inactive(self.box)
# a mapping is available, that is all the widget needs to be activated. one
# mapping is always selected, so there is no need to check the mapping message
self.message_broker.publish(PresetData(name="preset", mappings=(combination,)))
self.assert_active(self.box)
self.message_broker.publish(MappingData(input_combination=combination))
self.assert_active(self.box)
self.message_broker.publish(MappingData())
self.assert_active(self.box)
def test_recorded_input_required(self):
self.box = Gtk.Box()
RequireActiveMapping(
self.message_broker,
self.box,
require_recorded_input=True,
)
combination = InputCombination([InputConfig(type=1, code=KEY_A)])
self.message_broker.publish(MappingData())
self.assert_inactive(self.box)
self.message_broker.publish(PresetData(name="preset", mappings=()))
self.assert_inactive(self.box)
self.message_broker.publish(PresetData(name="preset", mappings=(combination,)))
self.assert_inactive(self.box)
# the widget will be enabled once a mapping with recorded input is selected
self.message_broker.publish(MappingData(input_combination=combination))
self.assert_active(self.box)
# this mapping doesn't have input recorded, so the box is disabled
self.message_broker.publish(MappingData())
self.assert_inactive(self.box)
def assert_inactive(self, widget: Gtk.Widget):
self.assertFalse(widget.get_sensitive())
self.assertLess(widget.get_opacity(), 0.6)
self.assertGreater(widget.get_opacity(), 0.4)
def assert_active(self, widget: Gtk.Widget):
self.assertTrue(widget.get_sensitive())
self.assertEqual(widget.get_opacity(), 1)
@test_setup
class TestStack(ComponentBaseTest):
def test_switches_pages(self):
self.stack = Gtk.Stack()
self.stack.add_named(Gtk.Label(), "Devices")
self.stack.add_named(Gtk.Label(), "Presets")
self.stack.add_named(Gtk.Label(), "Editor")
self.stack.show_all()
stack_wrapper = Stack(self.message_broker, self.controller_mock, self.stack)
self.message_broker.publish(DoStackSwitch(Stack.devices_page))
self.assertEqual(self.stack.get_visible_child_name(), "Devices")
self.message_broker.publish(DoStackSwitch(Stack.presets_page))
self.assertEqual(self.stack.get_visible_child_name(), "Presets")
self.message_broker.publish(DoStackSwitch(Stack.editor_page))
self.assertEqual(self.stack.get_visible_child_name(), "Editor")
@test_setup
class TestBreadcrumbs(ComponentBaseTest):
def test_breadcrumbs(self):
self.label_1 = Gtk.Label()
self.label_2 = Gtk.Label()
self.label_3 = Gtk.Label()
self.label_4 = Gtk.Label()
self.label_5 = Gtk.Label()
Breadcrumbs(
self.message_broker,
self.label_1,
show_device_group=False,
show_preset=False,
show_mapping=False,
)
Breadcrumbs(
self.message_broker,
self.label_2,
show_device_group=True,
show_preset=False,
show_mapping=False,
)
Breadcrumbs(
self.message_broker,
self.label_3,
show_device_group=True,
show_preset=True,
show_mapping=False,
)
Breadcrumbs(
self.message_broker,
self.label_4,
show_device_group=True,
show_preset=True,
show_mapping=True,
)
Breadcrumbs(
self.message_broker,
self.label_5,
show_device_group=False,
show_preset=False,
show_mapping=True,
)
self.assertEqual(self.label_1.get_text(), "")
self.assertEqual(self.label_2.get_text(), "?")
self.assertEqual(self.label_3.get_text(), "? / ?")
self.assertEqual(self.label_4.get_text(), "? / ? / ?")
self.assertEqual(self.label_5.get_text(), "?")
self.message_broker.publish(PresetData("preset", None))
self.assertEqual(self.label_1.get_text(), "")
self.assertEqual(self.label_2.get_text(), "?")
self.assertEqual(self.label_3.get_text(), "? / preset")
self.assertEqual(self.label_4.get_text(), "? / preset / ?")
self.assertEqual(self.label_5.get_text(), "?")
self.message_broker.publish(GroupData("group", ()))
self.assertEqual(self.label_1.get_text(), "")
self.assertEqual(self.label_2.get_text(), "group")
self.assertEqual(self.label_3.get_text(), "group / preset")
self.assertEqual(self.label_4.get_text(), "group / preset / ?")
self.assertEqual(self.label_5.get_text(), "?")
self.message_broker.publish(MappingData())
self.assertEqual(self.label_1.get_text(), "")
self.assertEqual(self.label_2.get_text(), "group")
self.assertEqual(self.label_3.get_text(), "group / preset")
self.assertEqual(self.label_4.get_text(), "group / preset / Empty Mapping")
self.assertEqual(self.label_5.get_text(), "Empty Mapping")
self.message_broker.publish(MappingData(name="mapping"))
self.assertEqual(self.label_4.get_text(), "group / preset / mapping")
self.assertEqual(self.label_5.get_text(), "mapping")
combination = InputCombination(
(
InputConfig(type=1, code=KEY_A),
InputConfig(type=1, code=KEY_B),
)
)
self.message_broker.publish(MappingData(input_combination=combination))
self.assertEqual(self.label_4.get_text(), "group / preset / a + b")
self.assertEqual(self.label_5.get_text(), "a + b")
combination = InputCombination([InputConfig(type=1, code=KEY_A)])
self.message_broker.publish(
MappingData(name="qux", input_combination=combination)
)
self.assertEqual(self.label_4.get_text(), "group / preset / qux")
self.assertEqual(self.label_5.get_text(), "qux")
|