Debian Codegraph @

src/tests/tests_playsound.cpp

raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "test_utils.h"
4 #include "tests.h"
5 #include "tests_common.h"
6
7 #include "common/commandstatus.h"
8
9 #include <QDataStream>
10 #include <QTemporaryFile>
11
12 namespace {
13
14 /// Build a minimal valid WAV file containing silence.
15 /// PCM 8-bit mono at 100 Hz; \a durationMs in milliseconds.
16 /// Produces a tiny file (~100 bytes/s) for testing playback paths.
17 QByteArray makeSilentWav(int durationMs)
18 {
19 const int sampleRate = 100;
20 const int bitsPerSample = 8;
21 const int numChannels = 1;
22 const int bytesPerSample = bitsPerSample / 8;
23 const int numSamples = sampleRate * durationMs / 1000;
24 const int dataSize = numSamples * numChannels * bytesPerSample;
25
26 QByteArray wav;
27 QDataStream s(&wav, QIODevice::WriteOnly);
28 s.setByteOrder(QDataStream::LittleEndian);
29
30 // RIFF header
31 s.writeRawData("RIFF", 4);
32 s << quint32(36 + dataSize); // file size - 8
33 s.writeRawData("WAVE", 4);
34
35 // fmt sub-chunk
36 s.writeRawData("fmt ", 4);
37 s << quint32(16); // sub-chunk size (PCM)
38 s << quint16(1); // audio format (PCM)
39 s << quint16(numChannels);
40 s << quint32(sampleRate);
41 s << quint32(sampleRate * numChannels * bytesPerSample); // byte rate
42 s << quint16(numChannels * bytesPerSample); // block align
43 s << quint16(bitsPerSample);
44
45 // data sub-chunk — 8-bit PCM silence is 0x80 (midpoint), not 0x00
46 s.writeRawData("data", 4);
47 s << quint32(dataSize);
48 wav.append(QByteArray(dataSize, '\x80'));
49
50 return wav;
51 }
52
53 /// Write a silent WAV to a QTemporaryFile; returns false on I/O error.
54 bool writeSilentWav(QTemporaryFile &tmp, int durationMs)
55 {
56 if (!tmp.open())
57 return false;
58 const QByteArray wav = makeSilentWav(durationMs);
59 if (tmp.write(wav) != wav.size())
60 return false;
61 if (!tmp.flush())
62 return false;
63 tmp.close();
64 return true;
65 }
66
67 } // namespace
68
69 void CoreTests::commandPlaySound()
70 {
71 // Object argument with missing file property
72 RUN_EXPECT_ERROR_WITH_STDERR(
73 "playSound({})",
74 CommandException, "ScriptError: playSound: object argument requires a 'file' string property");
75
76 // Object argument with non-string file property
77 RUN_EXPECT_ERROR_WITH_STDERR(
78 "playSound({file: 123})",
79 CommandException, "ScriptError: playSound: object argument requires a 'file' string property");
80
81 // Object argument with non-number volume
82 RUN_EXPECT_ERROR_WITH_STDERR(
83 "playSound({file: '/no/such/file.wav', volume: 'loud'})",
84 CommandException, "ScriptError: playSound: 'volume' must be a number");
85
86 #ifndef WITH_AUDIO
87 SKIP("Audio playback not compiled in (WITH_AUDIO is off)");
88 #endif
89
90 SKIP_ON_ENV("COPYQ_TESTS_SKIP_AUDIO");
91
92 // Missing argument — empty path reaches the server which reports file-not-found
93 RUN_EXPECT_ERROR_WITH_STDERR(
94 "playSound", CommandException, "ScriptError: File not found:");
95
96 // File does not exist (string argument)
97 RUN_EXPECT_ERROR_WITH_STDERR(
98 "playSound" << "/no/such/file.wav", CommandException, "ScriptError: File not found:");
99
100 // Object argument with nonexistent file
101 RUN_EXPECT_ERROR_WITH_STDERR(
102 "playSound({file: '/no/such/file.wav', volume: 50})",
103 CommandException, "ScriptError: File not found:");
104
105 // Existing file: playSound delegates to the server which starts
106 // playback asynchronously.
107 {
108 QTemporaryFile tmp;
109 QVERIFY(writeSilentWav(tmp, 500));
110
111 // String argument (default volume)
112 RUN("playSound" << tmp.fileName(), "");
113
114 // Object argument with explicit volume
115 RUN("eval" << QStringLiteral("playSound({file: '%1', volume: 50})").arg(tmp.fileName()), "");
116
117 // Object argument without volume (default 100%)
118 RUN("eval" << QStringLiteral("playSound({file: '%1'})").arg(tmp.fileName()), "");
119 }
120 }