Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/audio/recording_curves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ void RecordingCurves::pauseRecording()
qInfo() << "pauseRecording finished";
}

bool RecordingCurves::isRecordingActive() const
{
return m_timer && m_timer->isActive();
}

void RecordingCurves::updateCurves()
{
// qInfo() << "updateCurves called";
Expand Down
3 changes: 3 additions & 0 deletions src/audio/recording_curves.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class RecordingCurves : public QQuickPaintedItem
Q_INVOKABLE void startRecording();
Q_INVOKABLE void stopRecording();
Q_INVOKABLE void pauseRecording();

// 查询录音定时器是否处于激活状态(供测试与外部状态查询使用)
bool isRecordingActive() const;
public:
void paint(QPainter *painter);

Expand Down
7 changes: 6 additions & 1 deletion src/handler/voice_to_text_task_manager.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2024-2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -90,3 +90,8 @@ void VoiceToTextTaskManager::removeTask(const QString &voiceId)
qDebug() << "Removed task for voiceId:" << voiceId;
}
}

void VoiceToTextTaskManager::clearAllTasks()
{
m_tasks.clear();
}
7 changes: 6 additions & 1 deletion src/handler/voice_to_text_task_manager.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2024-2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -81,6 +81,11 @@ class VoiceToTextTaskManager : public QObject
*/
void removeTask(const QString &voiceId);

/**
* @brief 清除所有任务(仅供测试环境重置单例状态)
*/
void clearAllTasks();

signals:
/**
* @brief 任务状态变化信号
Expand Down
1 change: 0 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ list(REMOVE_ITEM VNOTE_SRC_TEST1
"${CMAKE_CURRENT_LIST_DIR}/src/common/ut_utils.cpp"
"${CMAKE_CURRENT_LIST_DIR}/src/common/ut_vlcplayer.cpp"
"${CMAKE_CURRENT_LIST_DIR}/src/common/ut_vntaskworker.cpp"
"${CMAKE_CURRENT_LIST_DIR}/src/task/ut_vnmainwnddelayinittask.cpp"
)

list(REMOVE_ITEM VNOTE_SRC_TEST "${CMAKE_CURRENT_LIST_DIR}/../src/main.cpp")
Expand Down
100 changes: 100 additions & 0 deletions tests/src/audio/ut_recording_curves.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "ut_recording_curves.h"
#include "recording_curves.h"

Check warning on line 6 in tests/src/audio/ut_recording_curves.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "recording_curves.h" not found.

#include <QPainter>

Check warning on line 8 in tests/src/audio/ut_recording_curves.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QPainter> not found. Please note: Cppcheck does not need standard library headers to get proper results.

UT_RecordingCurves::UT_RecordingCurves()
{
}

TEST_F(UT_RecordingCurves, Constructor_CreatesValidInstance)
{
RecordingCurves curves;
EXPECT_FALSE(curves.isRecordingActive());
}

TEST_F(UT_RecordingCurves, UpdateVolume_DoesNotCrash)
{
RecordingCurves curves;
curves.updateVolume(0.5);
curves.updateVolume(0.0);
curves.updateVolume(1.0);
SUCCEED();
}

TEST_F(UT_RecordingCurves, StartRecording_TimerBecomesActive)
{
RecordingCurves curves;
EXPECT_FALSE(curves.isRecordingActive());

curves.startRecording();
EXPECT_TRUE(curves.isRecordingActive());
}

TEST_F(UT_RecordingCurves, StopRecording_AfterStart_TimerStops)
{
RecordingCurves curves;
curves.startRecording();
ASSERT_TRUE(curves.isRecordingActive());

curves.stopRecording();
EXPECT_FALSE(curves.isRecordingActive());
}

TEST_F(UT_RecordingCurves, StopRecording_WithoutStart_DoesNotCrash)
{
RecordingCurves curves;
EXPECT_FALSE(curves.isRecordingActive());

curves.stopRecording();
EXPECT_FALSE(curves.isRecordingActive());
}

TEST_F(UT_RecordingCurves, PauseRecording_TogglesTimer)
{
RecordingCurves curves;
curves.startRecording();
ASSERT_TRUE(curves.isRecordingActive());

// pauseRecording should stop the timer
curves.pauseRecording();
EXPECT_FALSE(curves.isRecordingActive());

// Second call should restart the timer
curves.pauseRecording();
EXPECT_TRUE(curves.isRecordingActive());
}

// 注:pauseRecording 在未 startRecording 时调用会启动定时器,
// 这是 RecordingCurves 源码的已知行为(pauseRecording 内部用
// m_timer->isActive() 做切换),此处不作为预期断言,避免将
// 源码行为固化为"正确"。

TEST_F(UT_RecordingCurves, Paint_WithValidSize_DoesNotCrash)
{
RecordingCurves curves;
curves.setSize(QSizeF(100, 50));
curves.updateVolume(0.5);

QImage image(100, 50, QImage::Format_ARGB32);
image.fill(Qt::white);
QPainter painter(&image);
curves.paint(&painter);
SUCCEED();
}

TEST_F(UT_RecordingCurves, Paint_WithZeroGain_DoesNotCrash)
{
RecordingCurves curves;
curves.setSize(QSizeF(100, 50));

QImage image(100, 50, QImage::Format_ARGB32);
image.fill(Qt::white);
QPainter painter(&image);
curves.paint(&painter);
SUCCEED();
}
20 changes: 20 additions & 0 deletions tests/src/audio/ut_recording_curves.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef UT_RECORDING_CURVES_H
#define UT_RECORDING_CURVES_H

#include "gtest/gtest.h"

Check warning on line 8 in tests/src/audio/ut_recording_curves.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "gtest/gtest.h" not found.
#include <QTest>

Check warning on line 9 in tests/src/audio/ut_recording_curves.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QTest> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QObject>

Check warning on line 10 in tests/src/audio/ut_recording_curves.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QObject> not found. Please note: Cppcheck does not need standard library headers to get proper results.

class UT_RecordingCurves : public QObject
, public ::testing::Test
{
Q_OBJECT
public:
UT_RecordingCurves();
};

#endif // UT_RECORDING_CURVES_H
169 changes: 169 additions & 0 deletions tests/src/handler/ut_voice_to_text_task_manager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "ut_voice_to_text_task_manager.h"
#include "voice_to_text_task_manager.h"

Check warning on line 6 in tests/src/handler/ut_voice_to_text_task_manager.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "voice_to_text_task_manager.h" not found.

#include <QSignalSpy>

Check warning on line 8 in tests/src/handler/ut_voice_to_text_task_manager.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QSignalSpy> not found. Please note: Cppcheck does not need standard library headers to get proper results.

UT_VoiceToTextTaskManager::UT_VoiceToTextTaskManager()
{
}

void UT_VoiceToTextTaskManager::SetUp()
{
}

void UT_VoiceToTextTaskManager::TearDown()
{
// 使用 clearAllTasks 统一重置单例状态,确保用例间完全隔离
VoiceToTextTaskManager::instance()->clearAllTasks();
}

// ---------------------------------------------------------------------------
// addTask / getTask
// ---------------------------------------------------------------------------

TEST_F(UT_VoiceToTextTaskManager, AddTask_ValidVoiceId_AddsTask)
{
VoiceToTextTaskManager *mgr = VoiceToTextTaskManager::instance();
QSignalSpy spy(mgr, &VoiceToTextTaskManager::taskStatusChanged);

mgr->addTask(1, QStringLiteral("voice-add-001"));
VoiceToTextTask *task = mgr->getTask(QStringLiteral("voice-add-001"));

ASSERT_NE(nullptr, task);
EXPECT_EQ(1, task->noteId);
EXPECT_EQ(VoiceToTextTask::Converting, task->status);
EXPECT_EQ(QStringLiteral("voice-add-001"), task->voiceId);

EXPECT_EQ(1, spy.count());
}

TEST_F(UT_VoiceToTextTaskManager, AddTask_EmptyVoiceId_DoesNotAdd)
{
VoiceToTextTaskManager *mgr = VoiceToTextTaskManager::instance();
QSignalSpy spy(mgr, &VoiceToTextTaskManager::taskStatusChanged);

mgr->addTask(2, QString());
EXPECT_EQ(nullptr, mgr->getTask(QString()));
EXPECT_EQ(0, spy.count());
}

TEST_F(UT_VoiceToTextTaskManager, GetTask_NotExist_ReturnsNull)
{
VoiceToTextTaskManager *mgr = VoiceToTextTaskManager::instance();
EXPECT_EQ(nullptr, mgr->getTask(QStringLiteral("nonexistent-uuid-999")));
}

// ---------------------------------------------------------------------------
// setTaskResult
// ---------------------------------------------------------------------------

TEST_F(UT_VoiceToTextTaskManager, SetTaskResult_Success_UpdatesStatus)
{
VoiceToTextTaskManager *mgr = VoiceToTextTaskManager::instance();
mgr->addTask(3, QStringLiteral("voice-result-001"));

QSignalSpy statusSpy(mgr, &VoiceToTextTaskManager::taskStatusChanged);
QSignalSpy completedSpy(mgr, &VoiceToTextTaskManager::taskCompleted);

mgr->setTaskResult(QStringLiteral("voice-result-001"), QStringLiteral("转写结果"), true);

VoiceToTextTask *task = mgr->getTask(QStringLiteral("voice-result-001"));
ASSERT_NE(nullptr, task);
EXPECT_EQ(VoiceToTextTask::Completed, task->status);
EXPECT_EQ(QStringLiteral("转写结果"), task->resultText);

EXPECT_EQ(1, statusSpy.count());
EXPECT_EQ(1, completedSpy.count());

QList<QVariant> args = completedSpy.takeFirst();
EXPECT_EQ(3, args.at(0).toInt());
EXPECT_EQ(QStringLiteral("voice-result-001"), args.at(1).toString());
EXPECT_EQ(QStringLiteral("转写结果"), args.at(2).toString());
EXPECT_TRUE(args.at(3).toBool());
}

TEST_F(UT_VoiceToTextTaskManager, SetTaskResult_Failure_UpdatesStatus)
{
VoiceToTextTaskManager *mgr = VoiceToTextTaskManager::instance();
mgr->addTask(4, QStringLiteral("voice-result-002"));

QSignalSpy statusSpy(mgr, &VoiceToTextTaskManager::taskStatusChanged);
QSignalSpy completedSpy(mgr, &VoiceToTextTaskManager::taskCompleted);

mgr->setTaskResult(QStringLiteral("voice-result-002"), QString(), false);

VoiceToTextTask *task = mgr->getTask(QStringLiteral("voice-result-002"));
ASSERT_NE(nullptr, task);
EXPECT_EQ(VoiceToTextTask::Failed, task->status);
EXPECT_EQ(1, statusSpy.count());
EXPECT_EQ(1, completedSpy.count());
}

TEST_F(UT_VoiceToTextTaskManager, SetTaskResult_NotExist_NoSignal)
{
VoiceToTextTaskManager *mgr = VoiceToTextTaskManager::instance();
QSignalSpy statusSpy(mgr, &VoiceToTextTaskManager::taskStatusChanged);
QSignalSpy completedSpy(mgr, &VoiceToTextTaskManager::taskCompleted);

mgr->setTaskResult(QStringLiteral("nonexistent-uuid-998"), QString(), false);

EXPECT_EQ(0, statusSpy.count());
EXPECT_EQ(0, completedSpy.count());
}

// ---------------------------------------------------------------------------
// getTasksForNote / hasActiveTask
// ---------------------------------------------------------------------------

TEST_F(UT_VoiceToTextTaskManager, GetTasksForNote_ReturnsMatchingTasks)
{
VoiceToTextTaskManager *mgr = VoiceToTextTaskManager::instance();
mgr->addTask(10, QStringLiteral("voice-note-a"));
mgr->addTask(10, QStringLiteral("voice-note-b"));
mgr->addTask(20, QStringLiteral("voice-note-c"));

QList<VoiceToTextTask> tasks = mgr->getTasksForNote(10);
EXPECT_EQ(2, tasks.size());
}

TEST_F(UT_VoiceToTextTaskManager, HasActiveTask_WithConvertingTask_ReturnsTrue)
{
VoiceToTextTaskManager *mgr = VoiceToTextTaskManager::instance();
mgr->addTask(11, QStringLiteral("voice-active-001"));
EXPECT_TRUE(mgr->hasActiveTask());
}

TEST_F(UT_VoiceToTextTaskManager, HasActiveTask_WithNoConvertingTask_ReturnsFalse)
{
VoiceToTextTaskManager *mgr = VoiceToTextTaskManager::instance();
mgr->addTask(12, QStringLiteral("voice-active-002"));
ASSERT_TRUE(mgr->hasActiveTask());

mgr->setTaskResult(QStringLiteral("voice-active-002"), QStringLiteral("done"), true);
EXPECT_FALSE(mgr->hasActiveTask());
}

// ---------------------------------------------------------------------------
// removeTask
// ---------------------------------------------------------------------------

TEST_F(UT_VoiceToTextTaskManager, RemoveTask_Existing_RemovesIt)
{
VoiceToTextTaskManager *mgr = VoiceToTextTaskManager::instance();
mgr->addTask(13, QStringLiteral("voice-remove-001"));
ASSERT_NE(nullptr, mgr->getTask(QStringLiteral("voice-remove-001")));

mgr->removeTask(QStringLiteral("voice-remove-001"));
EXPECT_EQ(nullptr, mgr->getTask(QStringLiteral("voice-remove-001")));
}

TEST_F(UT_VoiceToTextTaskManager, RemoveTask_NonExistent_NoCrash)
{
VoiceToTextTaskManager *mgr = VoiceToTextTaskManager::instance();
mgr->removeTask(QStringLiteral("nonexistent-uuid-997"));
SUCCEED();
}
24 changes: 24 additions & 0 deletions tests/src/handler/ut_voice_to_text_task_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef UT_VOICETOTEXTTASKMANAGER_H
#define UT_VOICETOTEXTTASKMANAGER_H

#include "gtest/gtest.h"

Check warning on line 8 in tests/src/handler/ut_voice_to_text_task_manager.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "gtest/gtest.h" not found.
#include <QTest>

Check warning on line 9 in tests/src/handler/ut_voice_to_text_task_manager.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QTest> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QObject>

Check warning on line 10 in tests/src/handler/ut_voice_to_text_task_manager.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QObject> not found. Please note: Cppcheck does not need standard library headers to get proper results.

class UT_VoiceToTextTaskManager : public QObject
, public ::testing::Test
{
Q_OBJECT
public:
UT_VoiceToTextTaskManager();

protected:
virtual void SetUp() override;
virtual void TearDown() override;
};

#endif // UT_VOICETOTEXTTASKMANAGER_H
Loading
Loading