|
5 | 5 |
|
6 | 6 | #include "../src/configuration/HotkeyManager.h" |
7 | 7 |
|
| 8 | +#include <QSettings> |
| 9 | +#include <QStringList> |
8 | 10 | #include <QtTest/QtTest> |
9 | 11 |
|
10 | 12 | TestHotkeyManager::TestHotkeyManager() = default; |
@@ -372,4 +374,111 @@ void TestHotkeyManager::invalidKeyValidationTest() |
372 | 374 | QVERIFY(manager.hasHotkey("HYPHEN")); |
373 | 375 | } |
374 | 376 |
|
| 377 | +void TestHotkeyManager::duplicateKeyBehaviorTest() |
| 378 | +{ |
| 379 | + HotkeyManager manager; |
| 380 | + |
| 381 | + // Test that duplicate keys in imported content use the last definition |
| 382 | + QString contentWithDuplicates = "_hotkey F1 first\n" |
| 383 | + "_hotkey F2 middle\n" |
| 384 | + "_hotkey F1 second\n"; |
| 385 | + |
| 386 | + manager.importFromCliFormat(contentWithDuplicates); |
| 387 | + |
| 388 | + // getCommand should return the last definition |
| 389 | + QCOMPARE(manager.getCommand("F1"), QString("second")); |
| 390 | + QCOMPARE(manager.getCommand("F2"), QString("middle")); |
| 391 | + |
| 392 | + // Test that setHotkey replaces existing entry |
| 393 | + manager.importFromCliFormat("_hotkey F1 original\n"); |
| 394 | + QCOMPARE(manager.getCommand("F1"), QString("original")); |
| 395 | + QCOMPARE(manager.getAllHotkeys().size(), 1); |
| 396 | + |
| 397 | + manager.setHotkey("F1", "replaced"); |
| 398 | + QCOMPARE(manager.getCommand("F1"), QString("replaced")); |
| 399 | + QCOMPARE(manager.getAllHotkeys().size(), 1); // Still 1, not 2 |
| 400 | +} |
| 401 | + |
| 402 | +void TestHotkeyManager::commentPreservationTest() |
| 403 | +{ |
| 404 | + HotkeyManager manager; |
| 405 | + |
| 406 | + // Test that comments and formatting survive import/export round trip |
| 407 | + const QString cliFormat = "# Leading comment\n" |
| 408 | + "\n" |
| 409 | + "# Section header\n" |
| 410 | + "_hotkey F1 open\n" |
| 411 | + "\n" |
| 412 | + "# Another comment\n" |
| 413 | + "_hotkey F2 close\n"; |
| 414 | + |
| 415 | + manager.importFromCliFormat(cliFormat); |
| 416 | + const QString exported = manager.exportToCliFormat(); |
| 417 | + |
| 418 | + // Verify comments are preserved in export |
| 419 | + QVERIFY(exported.contains("# Leading comment")); |
| 420 | + QVERIFY(exported.contains("# Section header")); |
| 421 | + QVERIFY(exported.contains("# Another comment")); |
| 422 | + |
| 423 | + // Verify hotkeys are still correct |
| 424 | + QVERIFY(exported.contains("_hotkey F1 open")); |
| 425 | + QVERIFY(exported.contains("_hotkey F2 close")); |
| 426 | + |
| 427 | + // Verify order is preserved (comments before their hotkeys) |
| 428 | + int posLeading = exported.indexOf("# Leading comment"); |
| 429 | + int posSection = exported.indexOf("# Section header"); |
| 430 | + int posF1 = exported.indexOf("_hotkey F1"); |
| 431 | + int posAnother = exported.indexOf("# Another comment"); |
| 432 | + int posF2 = exported.indexOf("_hotkey F2"); |
| 433 | + |
| 434 | + QVERIFY(posLeading < posSection); |
| 435 | + QVERIFY(posSection < posF1); |
| 436 | + QVERIFY(posF1 < posAnother); |
| 437 | + QVERIFY(posAnother < posF2); |
| 438 | +} |
| 439 | + |
| 440 | +void TestHotkeyManager::settingsPersistenceTest() |
| 441 | +{ |
| 442 | + // Use a unique organization/app name to avoid conflicts with real settings |
| 443 | + const QString testOrg = "MMapperTest"; |
| 444 | + const QString testApp = "HotkeyManagerTest"; |
| 445 | + |
| 446 | + // Clean up any existing test settings |
| 447 | + QSettings cleanupSettings(testOrg, testApp); |
| 448 | + cleanupSettings.clear(); |
| 449 | + cleanupSettings.sync(); |
| 450 | + |
| 451 | + { |
| 452 | + // Create a manager and set some hotkeys |
| 453 | + HotkeyManager manager; |
| 454 | + manager.importFromCliFormat("# Test config\n" |
| 455 | + "_hotkey F1 look\n" |
| 456 | + "_hotkey CTRL+F2 attack\n"); |
| 457 | + |
| 458 | + QCOMPARE(manager.getCommand("F1"), QString("look")); |
| 459 | + QCOMPARE(manager.getCommand("CTRL+F2"), QString("attack")); |
| 460 | + |
| 461 | + // Save to settings |
| 462 | + manager.saveToSettings(); |
| 463 | + } |
| 464 | + |
| 465 | + { |
| 466 | + // Create a new manager and load from settings |
| 467 | + HotkeyManager manager; |
| 468 | + manager.loadFromSettings(); |
| 469 | + |
| 470 | + // Verify hotkeys were persisted |
| 471 | + QCOMPARE(manager.getCommand("F1"), QString("look")); |
| 472 | + QCOMPARE(manager.getCommand("CTRL+F2"), QString("attack")); |
| 473 | + |
| 474 | + // Verify comment was preserved |
| 475 | + QString exported = manager.exportToCliFormat(); |
| 476 | + QVERIFY(exported.contains("# Test config")); |
| 477 | + } |
| 478 | + |
| 479 | + // Clean up test settings |
| 480 | + cleanupSettings.clear(); |
| 481 | + cleanupSettings.sync(); |
| 482 | +} |
| 483 | + |
375 | 484 | QTEST_MAIN(TestHotkeyManager) |
0 commit comments