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
286 changes: 286 additions & 0 deletions tests/uiframe/ut_docsheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,27 @@
#include "SheetBrowser.h"
#include "SheetRenderer.h"
#include "Database.h"
#include "Utils.h"

Check warning on line 10 in tests/uiframe/ut_docsheet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "Utils.h" not found.
#include "dpdfannot.h"

Check warning on line 11 in tests/uiframe/ut_docsheet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "dpdfannot.h" not found.
#include "PDFModel.h"

Check warning on line 12 in tests/uiframe/ut_docsheet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "PDFModel.h" not found.
#include "BrowserPage.h"

Check warning on line 13 in tests/uiframe/ut_docsheet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "BrowserPage.h" not found.
#include "CentralDocPage.h"

Check warning on line 14 in tests/uiframe/ut_docsheet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "CentralDocPage.h" not found.
#include "SheetSidebar.h"

Check warning on line 15 in tests/uiframe/ut_docsheet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "SheetSidebar.h" not found.
#include "EncryptionPage.h"

Check warning on line 16 in tests/uiframe/ut_docsheet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "EncryptionPage.h" not found.
#include "PageSearchThread.h"
#include "stub.h"

Check warning on line 18 in tests/uiframe/ut_docsheet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "stub.h" not found.

#include <DPrintPreviewDialog>

Check warning on line 20 in tests/uiframe/ut_docsheet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

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

Check warning on line 21 in tests/uiframe/ut_docsheet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

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

#include <QUuid>
#include <QClipboard>
#include <QSignalSpy>
#include <QEvent>
#include <QMainWindow>
#include <QPrinter>
#include <QDialog>
#include <QTest>
//#include <QStackedLayout>
//#include <QFileDialog>

Expand Down Expand Up @@ -1382,3 +1388,283 @@
m_tester->setAlive(false);
EXPECT_TRUE(g_funcName == "saveOperation_stub");
}

namespace {
int QDialog_exec_stub()
{
g_funcName = __FUNCTION__;
return 0;
}

QSizeF getPageSize_stub2(int)
{
g_funcName = __FUNCTION__;
return QSizeF(100.0, 200.0);
}

void openFileAsync_stub2(const QString &)
{
g_funcName = __FUNCTION__;
}

QImage getImage_stub2(int, int, int, const QRect &)
{
g_funcName = __FUNCTION__;
return QImage(10, 20, QImage::Format_ARGB32);
}
}

TEST_F(TestDocSheet, UT_DocSheet_getPageLabelByIndex_001)
{
QString label = m_tester->getPageLabelByIndex(0);
EXPECT_FALSE(label.isEmpty());
}

TEST_F(TestDocSheet, UT_DocSheet_onExtractPassword_001)
{
Stub s;
s.set(ADDR(SheetRenderer, openFileAsync), openFileAsync_stub2);

m_tester->onExtractPassword("testpassword");
EXPECT_TRUE(m_tester->m_password == "testpassword");
EXPECT_TRUE(g_funcName == "openFileAsync_stub2");
}

TEST_F(TestDocSheet, UT_DocSheet_onPopInfoDialog_001)
{
Stub s;
s.set(ADDR(SheetRenderer, getPageSize), getPageSize_stub2);
s.set(ADDR(SheetRenderer, getImage), getImage_stub2);

typedef int (*fptr)();
fptr QDialog_exec = (fptr)(&QDialog::exec);
s.set(QDialog_exec, QDialog_exec_stub);

m_tester->onPopInfoDialog();
EXPECT_TRUE(g_funcName == "QDialog_exec_stub");
}

TEST_F(TestDocSheet, UT_DocSheet_readLastFileOperation_001)
{
DocSheet::g_lastOperationFile.clear();
EXPECT_FALSE(m_tester->readLastFileOperation());
}

TEST_F(TestDocSheet, UT_DocSheet_readLastFileOperation_002)
{
QString strPath = UTSOURCEDIR;
strPath += "/files/normal.pdf";

DocSheet *other = new DocSheet(Dr::FileType::PDF, strPath, nullptr);
DocSheet::g_sheetList.append(other);
DocSheet::g_uuidList.append(QUuid::createUuid().toString());
DocSheet::g_lastOperationFile = strPath;

EXPECT_TRUE(m_tester->readLastFileOperation());

DocSheet::g_sheetList.removeAll(other);
DocSheet::g_uuidList.removeLast();
DocSheet::g_lastOperationFile.clear();
delete other;
}

TEST_F(TestDocSheet, UT_DocSheet_showEncryPage_001)
{
EXPECT_TRUE(m_tester->m_encryPage == nullptr);
m_tester->showEncryPage();
EXPECT_TRUE(m_tester->m_encryPage != nullptr);

m_tester->showEncryPage();
EXPECT_TRUE(m_tester->m_encryPage != nullptr);
}

TEST_F(TestDocSheet, UT_DocSheet_calculatePrintTargetSize_001)
{
QPrinter printer;
QRectF pageRect(0, 0, 100, 100);
QSize result = m_tester->calculatePrintTargetSize(0, printer, pageRect);
EXPECT_TRUE(result.isEmpty());
}

TEST_F(TestDocSheet, UT_DocSheet_calculatePrintTargetSize_002)
{
Stub s;
s.set(ADDR(SheetRenderer, getPageSize), getPageSize_stub2);

m_tester->m_fileType = Dr::XPS;

QPrinter printer;
QRectF pageRect(0, 0, 100, 100);
QSize result = m_tester->calculatePrintTargetSize(0, printer, pageRect);
EXPECT_FALSE(result.isEmpty());

m_tester->m_fileType = Dr::PDF;
}

// LoadingWidget tests - need parent widget for Q_ASSERT
TEST_F(TestDocSheet, UT_DocSheet_LoadingWidget_constructor)
{
DWidget parent;
parent.resize(100, 100);
{
DocSheet::LoadingWidget lw(&parent);
EXPECT_TRUE(lw.parent() == &parent);
}
}

TEST_F(TestDocSheet, UT_DocSheet_LoadingWidget_paintEvent)
{
DWidget parent;
parent.resize(100, 100);
DocSheet::LoadingWidget lw(&parent);
QPaintEvent paint(QRect(0, 0, 100, 100));
lw.paintEvent(&paint);
EXPECT_FALSE(lw.grab().isNull());
}

TEST_F(TestDocSheet, UT_DocSheet_LoadingWidget_getImage)
{
Stub s;
s.set(ADDR(SheetRenderer, getImage), getImage_stub);
DWidget parent;
parent.resize(100, 100);
DocSheet::LoadingWidget lw(&parent);
QImage img = lw.getImage(m_tester, 0, 10, 20);
EXPECT_TRUE(g_funcName == "getImage_stub");
EXPECT_FALSE(img.isNull());
}

TEST_F(TestDocSheet, UT_DocSheet_LoadingWidget_getImageForPrint_valid)
{
Stub s;
s.set(ADDR(SheetRenderer, getImage), getImage_stub);
DWidget parent;
parent.resize(100, 100);
DocSheet::LoadingWidget lw(&parent);
QImage img = lw.getImageForPrint(m_tester, 0, QSize(10, 20));
EXPECT_TRUE(g_funcName == "getImage_stub");
EXPECT_FALSE(img.isNull());
}

TEST_F(TestDocSheet, UT_DocSheet_LoadingWidget_getImageForPrint_nullDoc)
{
DWidget parent;
parent.resize(100, 100);
DocSheet::LoadingWidget lw(&parent);
QImage img = lw.getImageForPrint(nullptr, 0, QSize(10, 20));
EXPECT_TRUE(img.isNull());
}

TEST_F(TestDocSheet, UT_DocSheet_LoadingWidget_getImageForPrint_invalidSize)
{
DWidget parent;
parent.resize(100, 100);
DocSheet::LoadingWidget lw(&parent);
QImage img = lw.getImageForPrint(m_tester, 0, QSize(0, 0));
EXPECT_TRUE(img.isNull());

QImage img2 = lw.getImageForPrint(m_tester, 0, QSize(-1, 10));
EXPECT_TRUE(img2.isNull());
}

namespace {
int QDialog_exec_stub3()
{
g_funcName = __FUNCTION__;
return 0;
}

bool SheetRenderer_opened_stub_false()
{
return false;
}
}

TEST_F(TestDocSheet, UT_DocSheet_onPrintRequested_emptyRange)
{
DPrinter printer;
QVector<int> pageRange;
m_tester->onPrintRequested(&printer, pageRange);
SUCCEED();
}

TEST_F(TestDocSheet, UT_DocSheet_onPopPrintDialog_notOpened)
{
// Stub opened() to return false so dialog is not created
Stub s;
s.set(ADDR(SheetRenderer, opened), SheetRenderer_opened_stub_false);
m_tester->onPopPrintDialog();
SUCCEED();
}

TEST_F(TestDocSheet, UT_DocSheet_onPrintRequested_singleArg)
{
// Need an active window for LoadingWidget creation
QWidget activeWindow;
activeWindow.resize(200, 200);
activeWindow.show();
activeWindow.activateWindow();
QTest::qWait(50);

Stub s;
s.set(ADDR(SheetRenderer, getImage), getImage_stub);
s.set(ADDR(DocSheet, pageCount), pageCount_stub);

DPrinter printer;
m_tester->onPrintRequested(&printer);
SUCCEED();
}

TEST_F(TestDocSheet, UT_DocSheet_onPrintRequested_pageRange_lambda)
{
// Cover the targetRectForSize lambda inside
// DocSheet::onPrintRequested(DPrinter*, QVector<int>) (XPS path).
// LoadingWidget requires a QMainWindow in qApp->topLevelWidgets().
QMainWindow mainWindow;
mainWindow.resize(200, 200);
mainWindow.show();
QTest::qWait(50);

Stub s;
s.set(ADDR(SheetRenderer, getPageSize), getPageSize_stub2);
s.set(ADDR(SheetRenderer, getImage), getImage_stub);
s.set(ADDR(DocSheet, pageCount), pageCount_stub);

m_tester->m_fileType = Dr::XPS;

// m_browser->pages() must have at least one entry to satisfy the
// `pageRange[i] <= m_browser->pages().count()` check.
BrowserPage *bp = new BrowserPage(nullptr, 0, m_tester);
m_tester->m_browser->m_items.append(bp);

DPrinter printer;
QVector<int> pageRange = {1};
m_tester->onPrintRequested(&printer, pageRange);

m_tester->m_fileType = Dr::PDF;
SUCCEED();
}

TEST_F(TestDocSheet, UT_DocSheet_onPrintRequested_singleArg_lambda)
{
// Cover the targetRectForSize lambda inside
// DocSheet::onPrintRequested(DPrinter*) (XPS path).
QMainWindow mainWindow;
mainWindow.resize(200, 200);
mainWindow.show();
mainWindow.activateWindow();
QTest::qWait(50);

Stub s;
s.set(ADDR(SheetRenderer, getPageSize), getPageSize_stub2);
s.set(ADDR(SheetRenderer, getImage), getImage_stub);
s.set(ADDR(DocSheet, pageCount), pageCount_stub);

m_tester->m_fileType = Dr::XPS;

DPrinter printer;
m_tester->onPrintRequested(&printer);

m_tester->m_fileType = Dr::PDF;
SUCCEED();
}
19 changes: 19 additions & 0 deletions tests/uiframe/ut_titlemenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,22 @@ TEST_F(TestTitleMenu, testOnActionTriggeredValid)
EXPECT_EQ(spy.count(), 1);
EXPECT_EQ(spy.takeFirst().at(0).toString(), QString("New window"));
}

TEST_F(TestTitleMenu, testDisableSaveButton)
{
QList<QAction *> actionsBefore = m_tester->findChildren<QAction *>();
QAction *saveAction = nullptr;
for (QAction *a : actionsBefore) {
if (a->text() == TitleMenu::tr("Save")) {
saveAction = a;
break;
}
}
ASSERT_NE(saveAction, nullptr);

m_tester->disableSaveButton(true);
EXPECT_FALSE(saveAction->isEnabled());

m_tester->disableSaveButton(false);
EXPECT_TRUE(saveAction->isEnabled());
}
Loading
Loading