From 732b1cdada860106509c61b07eb98b3ca4adb752 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Thu, 22 Aug 2024 19:10:46 -0400 Subject: [PATCH] Expand AccountManager tests with more rigorous model testing --- autotests/accountmanagertest.cpp | 78 ++++++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 9 deletions(-) diff --git a/autotests/accountmanagertest.cpp b/autotests/accountmanagertest.cpp index 155e0fc..8ea3818 100644 --- a/autotests/accountmanagertest.cpp +++ b/autotests/accountmanagertest.cpp @@ -12,20 +12,80 @@ class AccountManagerTest : public QObject private Q_SLOTS: void testNothing() { - AccountManager profileManager; - QAbstractItemModelTester modelTester(&profileManager); + AccountManager accountManager; + QAbstractItemModelTester modelTester(&accountManager); - QCOMPARE(profileManager.rowCount({}), 0); - profileManager.load(); + const QSignalSpy accountsChangedSpy(&accountManager, &AccountManager::accountsChanged); + QVERIFY(accountsChangedSpy.isValid()); + + const QSignalSpy accountAddedSpy(&accountManager, &AccountManager::accountAdded); + QVERIFY(accountAddedSpy.isValid()); + + QCOMPARE(accountManager.rowCount({}), 0); + accountManager.load(); + QCOMPARE(accountsChangedSpy.count(), 0); // no accounts were changed + QCOMPARE(accountAddedSpy.count(), 0); // no accounts were added // There should be no profiles, not even a dummy one - QCOMPARE(profileManager.rowCount({}), 0); - QCOMPARE(profileManager.numAccounts(), 0); - QVERIFY(!profileManager.hasAnyAccounts()); + QCOMPARE(accountManager.rowCount({}), 0); + QCOMPARE(accountManager.numAccounts(), 0); + QVERIFY(!accountManager.hasAnyAccounts()); // These functions shouldn't crash and return empty when there's no acccounts - QCOMPARE(profileManager.getByUuid(QString{}), nullptr); - QVERIFY(!profileManager.canDelete(nullptr)); + QCOMPARE(accountManager.getByUuid(QString{}), nullptr); + QVERIFY(!accountManager.canDelete(nullptr)); + } + + void testAccountManagement_data() + { + QTest::addColumn("isSapphire"); + + QTest::addRow("se") << false; + QTest::addRow("sapphire") << true; + } + + void testAccountManagement() + { + AccountManager accountManager; + QAbstractItemModelTester modelTester(&accountManager); + + const QSignalSpy accountsChangedSpy(&accountManager, &AccountManager::accountsChanged); + QVERIFY(accountsChangedSpy.isValid()); + + const QSignalSpy accountAddedSpy(&accountManager, &AccountManager::accountAdded); + QVERIFY(accountAddedSpy.isValid()); + + QCOMPARE(accountManager.rowCount({}), 0); + accountManager.load(); + QCOMPARE(accountsChangedSpy.count(), 0); + QCOMPARE(accountAddedSpy.count(), 0); + QVERIFY(!accountManager.hasAnyAccounts()); + + QFETCH(bool, isSapphire); + + if (isSapphire) { + accountManager.createSapphireAccount(QStringLiteral("foo.com"), QStringLiteral("foo")); + } else { + accountManager.createSquareEnixAccount(QStringLiteral("foo"), static_cast(Account::GameLicense::WindowsStandalone), true); + } + + QCOMPARE(accountsChangedSpy.count(), 1); + QCOMPARE(accountAddedSpy.count(), 1); + QCOMPARE(accountManager.rowCount({}), 1); + QCOMPARE(accountManager.numAccounts(), 1); + QVERIFY(accountManager.hasAnyAccounts()); + + auto account = accountManager.data(accountManager.index(0, 0), AccountManager::AccountRole).value(); + QVERIFY(!accountManager.canDelete(account)); + + // TODO: maybe deleteAccount shouldn't work when canDelete == false? + accountManager.deleteAccount(account); + + QCOMPARE(accountsChangedSpy.count(), 2); // this signal should be called + QCOMPARE(accountAddedSpy.count(), 1); // but not here, because nothing was added obviously + QCOMPARE(accountManager.rowCount({}), 0); + QCOMPARE(accountManager.numAccounts(), 0); + QVERIFY(!accountManager.hasAnyAccounts()); } };