| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | //TESTED_COMPONENT=src/multimedia |
| 30 | |
| 31 | #include <QtTest/QtTest> |
| 32 | #include <private/qsamplecache_p.h> |
| 33 | |
| 34 | class tst_QSampleCache : public QObject |
| 35 | { |
| 36 | Q_OBJECT |
| 37 | public: |
| 38 | |
| 39 | public slots: |
| 40 | |
| 41 | private slots: |
| 42 | void testCachedSample(); |
| 43 | void testNotCachedSample(); |
| 44 | void testEnoughCapacity(); |
| 45 | void testNotEnoughCapacity(); |
| 46 | void testInvalidFile(); |
| 47 | |
| 48 | private: |
| 49 | |
| 50 | }; |
| 51 | |
| 52 | void tst_QSampleCache::testCachedSample() |
| 53 | { |
| 54 | QSampleCache cache; |
| 55 | QSignalSpy loadingSpy(&cache, SIGNAL(isLoadingChanged())); |
| 56 | |
| 57 | QSample* sample = cache.requestSample(url: QUrl::fromLocalFile(QFINDTESTDATA("testdata/test.wav" ))); |
| 58 | QVERIFY(sample); |
| 59 | QTRY_COMPARE(loadingSpy.count(), 2); |
| 60 | QTRY_VERIFY(!cache.isLoading()); |
| 61 | |
| 62 | loadingSpy.clear(); |
| 63 | QSample* sampleCached = cache.requestSample(url: QUrl::fromLocalFile(QFINDTESTDATA("testdata/test.wav" ))); |
| 64 | QCOMPARE(sample, sampleCached); // sample is cached |
| 65 | QVERIFY(cache.isCached(QUrl::fromLocalFile(QFINDTESTDATA("testdata/test.wav" )))); |
| 66 | // loading thread still starts, but does nothing in this case |
| 67 | QTRY_COMPARE(loadingSpy.count(), 2); |
| 68 | QTRY_VERIFY(!cache.isLoading()); |
| 69 | |
| 70 | sample->release(); |
| 71 | sampleCached->release(); |
| 72 | } |
| 73 | |
| 74 | void tst_QSampleCache::testNotCachedSample() |
| 75 | { |
| 76 | QSampleCache cache; |
| 77 | QSignalSpy loadingSpy(&cache, SIGNAL(isLoadingChanged())); |
| 78 | |
| 79 | QSample* sample = cache.requestSample(url: QUrl::fromLocalFile(QFINDTESTDATA("testdata/test.wav" ))); |
| 80 | QVERIFY(sample); |
| 81 | QTRY_COMPARE(loadingSpy.count(), 2); |
| 82 | QTRY_VERIFY(!cache.isLoading()); |
| 83 | sample->release(); |
| 84 | |
| 85 | QVERIFY(!cache.isCached(QUrl::fromLocalFile(QFINDTESTDATA("testdata/test.wav" )))); |
| 86 | } |
| 87 | |
| 88 | void tst_QSampleCache::testEnoughCapacity() |
| 89 | { |
| 90 | QSampleCache cache; |
| 91 | QSignalSpy loadingSpy(&cache, SIGNAL(isLoadingChanged())); |
| 92 | |
| 93 | QSample* sample = cache.requestSample(url: QUrl::fromLocalFile(QFINDTESTDATA("testdata/test.wav" ))); |
| 94 | QVERIFY(sample); |
| 95 | QTRY_COMPARE(loadingSpy.count(), 2); // make sure sample is loaded |
| 96 | QTRY_VERIFY(!cache.isLoading()); |
| 97 | int sampleSize = sample->data().size(); |
| 98 | sample->release(); |
| 99 | cache.setCapacity(sampleSize * 2); |
| 100 | |
| 101 | QVERIFY(!cache.isCached(QUrl::fromLocalFile(QFINDTESTDATA("testdata/test.wav" )))); |
| 102 | |
| 103 | loadingSpy.clear(); |
| 104 | sample = cache.requestSample(url: QUrl::fromLocalFile(QFINDTESTDATA("testdata/test.wav" ))); |
| 105 | QVERIFY(sample); |
| 106 | QTRY_COMPARE(loadingSpy.count(), 2); |
| 107 | QTRY_VERIFY(!cache.isLoading()); |
| 108 | sample->release(); |
| 109 | |
| 110 | QVERIFY(cache.isCached(QUrl::fromLocalFile(QFINDTESTDATA("testdata/test.wav" )))); |
| 111 | |
| 112 | // load another sample and make sure first sample is not destroyed |
| 113 | loadingSpy.clear(); |
| 114 | QSample* sampleOther = cache.requestSample(url: QUrl::fromLocalFile(QFINDTESTDATA("testdata/test2.wav" ))); |
| 115 | QVERIFY(sampleOther); |
| 116 | QTRY_COMPARE(loadingSpy.count(), 2); |
| 117 | QTRY_VERIFY(!cache.isLoading()); |
| 118 | sampleOther->release(); |
| 119 | |
| 120 | QVERIFY(cache.isCached(QUrl::fromLocalFile(QFINDTESTDATA("testdata/test.wav" )))); |
| 121 | QVERIFY(cache.isCached(QUrl::fromLocalFile(QFINDTESTDATA("testdata/test2.wav" )))); |
| 122 | |
| 123 | loadingSpy.clear(); |
| 124 | QSample* sampleCached = cache.requestSample(url: QUrl::fromLocalFile(QFINDTESTDATA("testdata/test.wav" ))); |
| 125 | QCOMPARE(sample, sampleCached); // sample is cached |
| 126 | QVERIFY(cache.isCached(QUrl::fromLocalFile(QFINDTESTDATA("testdata/test.wav" )))); |
| 127 | QVERIFY(cache.isCached(QUrl::fromLocalFile(QFINDTESTDATA("testdata/test2.wav" )))); |
| 128 | QTRY_COMPARE(loadingSpy.count(), 2); |
| 129 | QTRY_VERIFY(!cache.isLoading()); |
| 130 | |
| 131 | sampleCached->release(); |
| 132 | } |
| 133 | |
| 134 | void tst_QSampleCache::testNotEnoughCapacity() |
| 135 | { |
| 136 | QSampleCache cache; |
| 137 | QSignalSpy loadingSpy(&cache, SIGNAL(isLoadingChanged())); |
| 138 | |
| 139 | QSample* sample = cache.requestSample(url: QUrl::fromLocalFile(QFINDTESTDATA("testdata/test.wav" ))); |
| 140 | QVERIFY(sample); |
| 141 | QTRY_COMPARE(loadingSpy.count(), 2); // make sure sample is loaded |
| 142 | QTRY_VERIFY(!cache.isLoading()); |
| 143 | int sampleSize = sample->data().size(); |
| 144 | sample->release(); |
| 145 | cache.setCapacity(sampleSize / 2); // unloads all samples |
| 146 | |
| 147 | QVERIFY(!cache.isCached(QUrl::fromLocalFile(QFINDTESTDATA("testdata/test.wav" )))); |
| 148 | |
| 149 | loadingSpy.clear(); |
| 150 | sample = cache.requestSample(url: QUrl::fromLocalFile(QFINDTESTDATA("testdata/test.wav" ))); |
| 151 | QVERIFY(sample); |
| 152 | QTRY_COMPARE(loadingSpy.count(), 2); |
| 153 | QTRY_VERIFY(!cache.isLoading()); |
| 154 | sample->release(); |
| 155 | |
| 156 | QVERIFY(cache.isCached(QUrl::fromLocalFile(QFINDTESTDATA("testdata/test.wav" )))); |
| 157 | |
| 158 | // load another sample to force sample cache to destroy first sample |
| 159 | loadingSpy.clear(); |
| 160 | QSample* sampleOther = cache.requestSample(url: QUrl::fromLocalFile(QFINDTESTDATA("testdata/test2.wav" ))); |
| 161 | QVERIFY(sampleOther); |
| 162 | QTRY_COMPARE(loadingSpy.count(), 2); |
| 163 | QTRY_VERIFY(!cache.isLoading()); |
| 164 | sampleOther->release(); |
| 165 | |
| 166 | QVERIFY(!cache.isCached(QUrl::fromLocalFile(QFINDTESTDATA("testdata/test.wav" )))); |
| 167 | } |
| 168 | |
| 169 | void tst_QSampleCache::testInvalidFile() |
| 170 | { |
| 171 | QSampleCache cache; |
| 172 | QSignalSpy loadingSpy(&cache, SIGNAL(isLoadingChanged())); |
| 173 | |
| 174 | QSample* sample = cache.requestSample(url: QUrl::fromLocalFile(localfile: "invalid" )); |
| 175 | QVERIFY(sample); |
| 176 | QTRY_COMPARE(sample->state(), QSample::Error); |
| 177 | QTRY_COMPARE(loadingSpy.count(), 2); |
| 178 | QTRY_VERIFY(!cache.isLoading()); |
| 179 | sample->release(); |
| 180 | |
| 181 | QVERIFY(!cache.isCached(QUrl::fromLocalFile("invalid" ))); |
| 182 | } |
| 183 | |
| 184 | QTEST_MAIN(tst_QSampleCache) |
| 185 | |
| 186 | #include "tst_qsamplecache.moc" |
| 187 | |