Repository navigation
Expand file tree
/
Copy pathtestnotificationsoundplayer.cpp
More file actions
278 lines (234 loc) · 8.65 KB
/
Copy pathtestnotificationsoundplayer.cpp
File metadata and controls
278 lines (234 loc) · 8.65 KB
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
/*
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QSignalSpy>
#include <QStandardPaths>
#include <QTemporaryFile>
#include <QUrl>
#include <QtTest>
#include "gui/notificationsoundplayer.h"
#include "gui/notificationsoundplayer_p.h"
using namespace OCC;
namespace {
class TestBackend : public NotificationSoundPlayer::Backend
{
public:
explicit TestBackend(bool nativeLoops, bool wantsFilesystemPath = true)
: _nativeLoops(nativeLoops)
, _wantsFilesystemPath(wantsFilesystemPath)
{
}
void setSource(const QString &sourceArg) override { source = sourceArg; }
void play(int loops) override
{
++playCallCount;
lastLoopsArg = loops;
}
void stop() override { ++stopCallCount; }
[[nodiscard]] bool handlesLoopsNatively() const override { return _nativeLoops; }
[[nodiscard]] bool needsFilesystemPath() const override { return _wantsFilesystemPath; }
void fireFinished()
{
if (onFinished) {
onFinished();
}
}
QString source;
int playCallCount = 0;
int stopCallCount = 0;
int lastLoopsArg = -1;
private:
bool _nativeLoops;
bool _wantsFilesystemPath;
};
}
class TestNotificationSoundPlayer : public QObject
{
Q_OBJECT
private:
static QString writeTempSoundFile()
{
auto *tmp = new QTemporaryFile(QDir::tempPath() + QStringLiteral("/nc-sound-test-XXXXXX.wav"));
tmp->setAutoRemove(true);
if (!tmp->open()) {
delete tmp;
return {};
}
tmp->write("RIFF\0\0\0\0WAVE", 12);
const auto path = tmp->fileName();
tmp->close();
// Leak deliberately: keep the file alive for the test's lifetime
// by attaching it as a child of qApp.
tmp->setParent(qApp);
return path;
}
private Q_SLOTS:
void initTestCase()
{
QStandardPaths::setTestModeEnabled(true);
// nextcloudCore is statically linked into the test binary, so the QRC
// collections it embeds (`resources.qrc` and the generated `theme.qrc`)
// need to be initialised explicitly before any `qrc:` lookup runs.
Q_INIT_RESOURCE(resources);
Q_INIT_RESOURCE(theme);
}
// ----- resolveToFilesystemPath -----
//
// The extractor is a generic QRC-to-disk copier; it doesn't care about
// the resource's file type. We use an arbitrary always-embedded SVG
// (`theme/delete.svg`) so the tests run on every platform, including
// Linux where the production WAV is intentionally not embedded.
void resolve_qrcExtractsToCache()
{
const auto path = NotificationSoundPlayer::resolveToFilesystemPath(
QStringLiteral("qrc:///client/theme/delete.svg"));
QVERIFY(!path.isEmpty());
QVERIFY(QFile::exists(path));
}
void resolve_qrcIsIdempotent()
{
const auto first = NotificationSoundPlayer::resolveToFilesystemPath(
QStringLiteral("qrc:///client/theme/delete.svg"));
const auto second = NotificationSoundPlayer::resolveToFilesystemPath(
QStringLiteral("qrc:///client/theme/delete.svg"));
QVERIFY(!first.isEmpty());
QCOMPARE(first, second);
}
void resolve_fileUrlReturnsLocalPath()
{
QTemporaryFile tmp;
QVERIFY(tmp.open());
tmp.close();
const auto fileUrl = QUrl::fromLocalFile(tmp.fileName()).toString();
const auto resolved = NotificationSoundPlayer::resolveToFilesystemPath(fileUrl);
QCOMPARE(resolved, tmp.fileName());
}
void resolve_plainPathReturnsAsIs()
{
QTemporaryFile tmp;
QVERIFY(tmp.open());
tmp.close();
const auto resolved = NotificationSoundPlayer::resolveToFilesystemPath(tmp.fileName());
QCOMPARE(resolved, tmp.fileName());
}
void resolve_emptyReturnsEmpty()
{
QCOMPARE(NotificationSoundPlayer::resolveToFilesystemPath({}), QString());
}
void resolve_nonexistentPathReturnsEmpty()
{
QCOMPARE(NotificationSoundPlayer::resolveToFilesystemPath(QStringLiteral("/definitely/does/not/exist.wav")),
QString());
}
// ----- Loop bookkeeping -----
void loops_perPlayBackendReissuesPlayUntilExhausted()
{
const auto soundPath = writeTempSoundFile();
QVERIFY(!soundPath.isEmpty());
auto backendOwned = std::make_unique<TestBackend>(/*nativeLoops*/ false);
auto *backend = backendOwned.get();
NotificationSoundPlayer player(std::move(backendOwned), this);
player.setSource(soundPath);
player.setLoops(3);
player.play();
QCOMPARE(backend->playCallCount, 1);
QVERIFY(player.isPlaying());
backend->fireFinished();
QCoreApplication::processEvents();
QCOMPARE(backend->playCallCount, 2);
QVERIFY(player.isPlaying());
backend->fireFinished();
QCoreApplication::processEvents();
QCOMPARE(backend->playCallCount, 3);
QVERIFY(player.isPlaying());
backend->fireFinished();
QCoreApplication::processEvents();
QCOMPARE(backend->playCallCount, 3);
QVERIFY(!player.isPlaying());
}
void loops_nativeBackendDoesNotReissuePlay()
{
const auto soundPath = writeTempSoundFile();
QVERIFY(!soundPath.isEmpty());
auto backendOwned = std::make_unique<TestBackend>(/*nativeLoops*/ true);
auto *backend = backendOwned.get();
NotificationSoundPlayer player(std::move(backendOwned), this);
player.setSource(soundPath);
player.setLoops(9);
player.play();
QCOMPARE(backend->playCallCount, 1);
QCOMPARE(backend->lastLoopsArg, 9);
QVERIFY(player.isPlaying());
backend->fireFinished();
QCoreApplication::processEvents();
QCOMPARE(backend->playCallCount, 1);
QVERIFY(!player.isPlaying());
}
void loops_stopMidSequenceCancelsRemainingPlays()
{
const auto soundPath = writeTempSoundFile();
QVERIFY(!soundPath.isEmpty());
auto backendOwned = std::make_unique<TestBackend>(/*nativeLoops*/ false);
auto *backend = backendOwned.get();
NotificationSoundPlayer player(std::move(backendOwned), this);
player.setSource(soundPath);
player.setLoops(5);
player.play();
backend->fireFinished();
QCoreApplication::processEvents();
QCOMPARE(backend->playCallCount, 2);
player.stop();
QCOMPARE(backend->stopCallCount, 1);
QVERIFY(!player.isPlaying());
// A late finished callback that races the stop must not re-issue play.
backend->fireFinished();
QCoreApplication::processEvents();
QCOMPARE(backend->playCallCount, 2);
QVERIFY(!player.isPlaying());
}
void play_emptySourceIsNoop()
{
auto backendOwned = std::make_unique<TestBackend>(true);
auto *backend = backendOwned.get();
NotificationSoundPlayer player(std::move(backendOwned), this);
player.setLoops(3);
player.play();
QCOMPARE(backend->playCallCount, 0);
QVERIFY(!player.isPlaying());
}
void play_backendThatDoesNotNeedFilesystemPath_skipsResolution()
{
// Source is a bogus QRC URL that doesn't exist; if the dispatcher
// tried to extract it, play() would bail with an empty resolved path.
// Because the backend declares it doesn't need a filesystem path,
// the dispatcher should hand the raw source over and play anyway.
auto backendOwned = std::make_unique<TestBackend>(/*nativeLoops*/ true, /*wantsFilesystemPath*/ false);
auto *backend = backendOwned.get();
NotificationSoundPlayer player(std::move(backendOwned), this);
player.setSource(QStringLiteral("qrc:///does/not/exist.wav"));
player.setLoops(3);
player.play();
QCOMPARE(backend->playCallCount, 1);
QCOMPARE(backend->source, QStringLiteral("qrc:///does/not/exist.wav"));
QVERIFY(player.isPlaying());
}
void play_zeroLoopsIsNoop()
{
const auto soundPath = writeTempSoundFile();
QVERIFY(!soundPath.isEmpty());
auto backendOwned = std::make_unique<TestBackend>(true);
auto *backend = backendOwned.get();
NotificationSoundPlayer player(std::move(backendOwned), this);
player.setSource(soundPath);
player.setLoops(0);
player.play();
QCOMPARE(backend->playCallCount, 0);
QVERIFY(!player.isPlaying());
}
};
QTEST_MAIN(TestNotificationSoundPlayer)
#include "testnotificationsoundplayer.moc"