File indexing completed on 2024-04-14 04:47:36

0001 /*
0002     SPDX-FileCopyrightText: 2022 Jean-Baptiste Mardelle <jb@kdenlive.org>
0003     SPDX-FileCopyrightText: 2022 Eric Jiang
0004     SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 #include "catch.hpp"
0007 #include "test_utils.hpp"
0008 // test specific headers
0009 #include "doc/docundostack.hpp"
0010 #include "doc/kdenlivedoc.h"
0011 #include <cmath>
0012 #include <iostream>
0013 #include <tuple>
0014 #include <unordered_set>
0015 
0016 #include "core.h"
0017 #include "definitions.h"
0018 #include "utils/thumbnailcache.hpp"
0019 
0020 TEST_CASE("Cache insert-remove", "[Cache]")
0021 {
0022     // Create timeline
0023     auto binModel = pCore->projectItemModel();
0024     std::shared_ptr<DocUndoStack> undoStack = std::make_shared<DocUndoStack>(nullptr);
0025 
0026     // Here we do some trickery to enable testing.
0027     // We mock the project class so that the undoStack function returns our undoStack
0028     KdenliveDoc document(undoStack);
0029     Mock<KdenliveDoc> docMock(document);
0030     When(Method(docMock, getCacheDir)).AlwaysReturn(QDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)));
0031     KdenliveDoc &mockedDoc = docMock.get();
0032 
0033     pCore->projectManager()->m_project = &mockedDoc;
0034     QDateTime documentDate = QDateTime::currentDateTime();
0035     pCore->projectManager()->updateTimeline(false, QString(), QString(), documentDate, 0);
0036     auto timeline = mockedDoc.getTimeline(mockedDoc.uuid());
0037     pCore->projectManager()->m_activeTimelineModel = timeline;
0038 
0039     pCore->projectManager()->testSetActiveDocument(&mockedDoc, timeline);
0040 
0041     // Create bin clip
0042     QString binId = createProducer(pCore->getProjectProfile(), "red", binModel, 20, false);
0043 
0044     SECTION("Insert and remove thumbnail")
0045     {
0046         QImage img(100, 100, QImage::Format_ARGB32_Premultiplied);
0047         img.fill(Qt::red);
0048         ThumbnailCache::get()->storeThumbnail(binId, 0, img, false);
0049         REQUIRE(ThumbnailCache::get()->checkIntegrity());
0050         ThumbnailCache::get()->storeThumbnail(binId, 0, img, false);
0051         REQUIRE(ThumbnailCache::get()->checkIntegrity());
0052     }
0053     pCore->projectManager()->closeCurrentDocument(false, false);
0054 }
0055 
0056 TEST_CASE("getAudioKey() should dereference `ok` param", "ThumbnailCache") {
0057     // Create timeline
0058     auto binModel = pCore->projectItemModel();
0059     std::shared_ptr<DocUndoStack> undoStack = std::make_shared<DocUndoStack>(nullptr);
0060 
0061     // Here we do some trickery to enable testing.
0062     // We mock the project class so that the undoStack function returns our undoStack
0063     KdenliveDoc document(undoStack);
0064     Mock<KdenliveDoc> docMock(document);
0065     When(Method(docMock, getCacheDir)).AlwaysReturn(QDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)));
0066     KdenliveDoc &mockedDoc = docMock.get();
0067 
0068     pCore->m_projectManager->m_project = &mockedDoc;
0069     QDateTime documentDate = QDateTime::currentDateTime();
0070     pCore->m_projectManager->updateTimeline(false, QString(), QString(), documentDate, 0);
0071     auto timeline = mockedDoc.getTimeline(mockedDoc.uuid());
0072     pCore->m_projectManager->m_activeTimelineModel = timeline;
0073 
0074     pCore->m_projectManager->testSetActiveDocument(&mockedDoc, timeline);
0075 
0076     // Create bin clip
0077     QString binId = createProducer(pCore->getProjectProfile(), "red", binModel, 20, false);
0078 
0079     SECTION("Request invalid id")
0080     {
0081         // Catches a bug where, after setting *ok, the code checks
0082         //     if (ok) {
0083         // instead of
0084         //     if (*ok) {
0085         bool ok = true;
0086         ThumbnailCache::getAudioKey(QStringLiteral("nonexistent-key"), &ok);
0087         REQUIRE(ok == false);
0088     }
0089     SECTION("Request valid id")
0090     {
0091         bool ok = false;
0092         ThumbnailCache::getAudioKey(binId, &ok);
0093         REQUIRE(ok == true);
0094     }
0095     pCore->projectManager()->closeCurrentDocument(false, false);
0096 }