Commit 9f548b84 authored by Eric Jiang's avatar Eric Jiang Committed by Julius Künzel
Browse files

Fix `if (ok)` in ThumbnailCache::getAudioKey

In ThumbnailCache::getAudioKey, `if (ok)` checks whether the pointer is null, not the actual value of *ok
which is computed right above.
parent cf47be8a
Pipeline #200055 passed with stage
in 7 minutes and 17 seconds
......@@ -337,8 +337,12 @@ QString ThumbnailCache::getKey(const QString &binId, int pos, bool *ok)
QStringList ThumbnailCache::getAudioKey(const QString &binId, bool *ok)
{
auto binClip = pCore->projectItemModel()->getClipByBinID(binId);
*ok = binClip != nullptr;
if (ok) {
if (binClip == nullptr) {
*ok = false;
qWarning() << "[BUG] Could not find binClip for binId" << binId;
return {};
} else {
*ok = true;
QString streams = binClip->getProducerProperty(QStringLiteral("kdenlive:active_streams"));
if (streams == QString::number(INT_MAX)) {
// activate all audio streams
......@@ -365,7 +369,6 @@ QStringList ThumbnailCache::getAudioKey(const QString &binId, bool *ok)
}
return streamsList;
}
return {};
}
// static
......
......@@ -54,3 +54,47 @@ TEST_CASE("Cache insert-remove", "[Cache]")
binModel->clean();
pCore->m_projectManager = nullptr;
}
TEST_CASE("getAudioKey() should dereference `ok` param", "ThumbnailCache") {
// Create timeline
auto binModel = pCore->projectItemModel();
std::shared_ptr<DocUndoStack> undoStack = std::make_shared<DocUndoStack>(nullptr);
std::shared_ptr<MarkerListModel> guideModel = std::make_shared<MarkerListModel>(undoStack);
// Here we do some trickery to enable testing.
// We mock the project class so that the undoStack function returns our undoStack
Mock<ProjectManager> pmMock;
When(Method(pmMock, undoStack)).AlwaysReturn(undoStack);
When(Method(pmMock, cacheDir)).AlwaysReturn(QDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)));
ProjectManager &mocked = pmMock.get();
pCore->m_projectManager = &mocked;
// We also mock timeline object to spy few functions and mock others
TimelineItemModel tim(&profile_cache, undoStack);
Mock<TimelineItemModel> timMock(tim);
auto timeline = std::shared_ptr<TimelineItemModel>(&timMock.get(), [](...) {});
TimelineItemModel::finishConstruct(timeline, guideModel);
// Create bin clip
QString binId = createProducer(profile_cache, "red", binModel, 20, false);
SECTION("Request invalid id")
{
// Catches a bug where, after setting *ok, the code checks
// if (ok) {
// instead of
// if (*ok) {
bool ok = true;
ThumbnailCache::getAudioKey(QStringLiteral("nonexistent-key"), &ok);
REQUIRE(ok == false);
}
SECTION("Request valid id")
{
bool ok = false;
ThumbnailCache::getAudioKey(binId, &ok);
REQUIRE(ok == true);
}
binModel->clean();
pCore->m_projectManager = nullptr;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment