|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: CC0-1.0 |
| 4 | + * |
| 5 | + * This software is in the public domain, furnished "as is", without technical |
| 6 | + * support, and with no warranty, express or implied, as to its usefulness for |
| 7 | + * any purpose. |
| 8 | + */ |
| 9 | + |
| 10 | +#include <QtTest> |
| 11 | + |
| 12 | +#include <QMenu> |
| 13 | +#include <QTemporaryDir> |
| 14 | + |
| 15 | +#include "account.h" |
| 16 | +#include "accountmanager.h" |
| 17 | +#include "configfile.h" |
| 18 | +#include "folder.h" |
| 19 | +#include "folderman.h" |
| 20 | +#include "systray.h" |
| 21 | + |
| 22 | +#include "foldermantestutils.h" |
| 23 | +#include "testhelper.h" |
| 24 | + |
| 25 | +using namespace OCC; |
| 26 | + |
| 27 | +class TestSystraySyncControl : public QObject |
| 28 | +{ |
| 29 | + Q_OBJECT |
| 30 | + |
| 31 | + QTemporaryDir _configDir; |
| 32 | + QTemporaryDir _firstFolderDir; |
| 33 | + QTemporaryDir _secondFolderDir; |
| 34 | + FolderManTestHelper _folderManHelper; |
| 35 | + AccountState *_firstAccountState = nullptr; |
| 36 | + AccountState *_secondAccountState = nullptr; |
| 37 | + Folder *_firstFolder = nullptr; |
| 38 | + Folder *_secondFolder = nullptr; |
| 39 | + |
| 40 | + static AccountState *addTestAccount(const QString &url, const QString &user) |
| 41 | + { |
| 42 | + auto account = Account::create(); |
| 43 | + account->setUrl(QUrl(url)); |
| 44 | + account->setDavUser(user); |
| 45 | + account->setCredentials(new HttpCredentialsTest(user, QStringLiteral("secret"))); |
| 46 | + return AccountManager::instance()->addAccount(account); |
| 47 | + } |
| 48 | + |
| 49 | +#ifndef Q_OS_MACOS |
| 50 | + static QAction *syncControlAction(QMenu &menu, Systray *systray) |
| 51 | + { |
| 52 | + setupQtTrayContextMenu(&menu, systray); |
| 53 | + return menu.findChild<QAction *>(QStringLiteral("traySyncControlAction")); |
| 54 | + } |
| 55 | +#endif |
| 56 | + |
| 57 | +private slots: |
| 58 | + void initTestCase() |
| 59 | + { |
| 60 | + QVERIFY(_configDir.isValid()); |
| 61 | + QVERIFY(_firstFolderDir.isValid()); |
| 62 | + QVERIFY(_secondFolderDir.isValid()); |
| 63 | + |
| 64 | + QStandardPaths::setTestModeEnabled(true); |
| 65 | + ConfigFile::setConfDir(_configDir.path()); |
| 66 | + |
| 67 | + _firstAccountState = addTestAccount(QStringLiteral("https://one.example.com"), QStringLiteral("alice")); |
| 68 | + QVERIFY(_firstAccountState); |
| 69 | + |
| 70 | + Systray::instance()->create(); |
| 71 | + } |
| 72 | + |
| 73 | + void cleanupTestCase() |
| 74 | + { |
| 75 | + const auto folderMan = FolderMan::instance(); |
| 76 | + if (_firstFolder) { |
| 77 | + folderMan->removeFolder(_firstFolder); |
| 78 | + } |
| 79 | + if (_secondFolder) { |
| 80 | + folderMan->removeFolder(_secondFolder); |
| 81 | + } |
| 82 | + _firstFolder = nullptr; |
| 83 | + _secondFolder = nullptr; |
| 84 | + |
| 85 | + if (_firstAccountState) { |
| 86 | + AccountManager::instance()->removeAccountState(_firstAccountState); |
| 87 | + } |
| 88 | + if (_secondAccountState) { |
| 89 | + AccountManager::instance()->removeAccountState(_secondAccountState); |
| 90 | + } |
| 91 | + _firstAccountState = nullptr; |
| 92 | + _secondAccountState = nullptr; |
| 93 | + } |
| 94 | + |
| 95 | + void globalActionIsHiddenWithoutClassicFoldersAndTogglesAllFolders() |
| 96 | + { |
| 97 | + const auto systray = Systray::instance(); |
| 98 | + |
| 99 | + // An account without classic folders is also the state used by a File Provider-only client. |
| 100 | + QVERIFY(systray->syncControlState() == Systray::SyncControlState::Unavailable); |
| 101 | + systray->toggleSyncPaused(); |
| 102 | + QVERIFY(systray->syncControlState() == Systray::SyncControlState::Unavailable); |
| 103 | +#ifndef Q_OS_MACOS |
| 104 | + auto unavailableMenu = QMenu{}; |
| 105 | + QVERIFY(!syncControlAction(unavailableMenu, systray)); |
| 106 | +#endif |
| 107 | + |
| 108 | + _firstFolder = FolderMan::instance()->addFolder(_firstAccountState, folderDefinition(_firstFolderDir.path())); |
| 109 | + QVERIFY(_firstFolder); |
| 110 | + |
| 111 | + _secondAccountState = addTestAccount(QStringLiteral("https://two.example.com"), QStringLiteral("bob")); |
| 112 | + QVERIFY(_secondAccountState); |
| 113 | + _secondFolder = FolderMan::instance()->addFolder(_secondAccountState, folderDefinition(_secondFolderDir.path())); |
| 114 | + QVERIFY(_secondFolder); |
| 115 | + |
| 116 | + QVERIFY(systray->syncControlState() == Systray::SyncControlState::Pause); |
| 117 | + |
| 118 | +#ifndef Q_OS_MACOS |
| 119 | + auto pauseMenu = QMenu{}; |
| 120 | + const auto pauseAction = syncControlAction(pauseMenu, systray); |
| 121 | + QVERIFY(pauseAction); |
| 122 | + QCOMPARE(pauseAction->text(), Systray::tr("Pause sync for all")); |
| 123 | + pauseAction->trigger(); |
| 124 | +#else |
| 125 | + systray->toggleSyncPaused(); |
| 126 | +#endif |
| 127 | + |
| 128 | + QVERIFY(_firstFolder->syncPaused()); |
| 129 | + QVERIFY(_secondFolder->syncPaused()); |
| 130 | + QVERIFY(systray->syncControlState() == Systray::SyncControlState::Resume); |
| 131 | + |
| 132 | +#ifndef Q_OS_MACOS |
| 133 | + auto resumeMenu = QMenu{}; |
| 134 | + const auto resumeAction = syncControlAction(resumeMenu, systray); |
| 135 | + QVERIFY(resumeAction); |
| 136 | + QCOMPARE(resumeAction->text(), Systray::tr("Resume sync for all")); |
| 137 | + resumeAction->trigger(); |
| 138 | +#else |
| 139 | + systray->toggleSyncPaused(); |
| 140 | +#endif |
| 141 | + |
| 142 | + QVERIFY(!_firstFolder->syncPaused()); |
| 143 | + QVERIFY(!_secondFolder->syncPaused()); |
| 144 | + QVERIFY(systray->syncControlState() == Systray::SyncControlState::Pause); |
| 145 | + } |
| 146 | +}; |
| 147 | + |
| 148 | +QTEST_MAIN(TestSystraySyncControl) |
| 149 | +#include "testsystraysynccontrol.moc" |
0 commit comments