From c7ea43d81708422fd80de5fae595bcc510f69000 Mon Sep 17 00:00:00 2001 From: liuyangming Date: Mon, 24 Jul 2023 09:10:35 +0800 Subject: [PATCH] test: [workspace]workspace ut add fileviewmodel ut Log: add workspace ut Task: https://pms.uniontech.com/task-view-278215.html --- .../models/ut_fileviewmodel.cpp | 120 ++++++++++++++++++ .../models/ut_rootinfo.cpp | 15 ++- 2 files changed, 128 insertions(+), 7 deletions(-) diff --git a/tests/plugins/filemanager/core/dfmplugin-workspace/models/ut_fileviewmodel.cpp b/tests/plugins/filemanager/core/dfmplugin-workspace/models/ut_fileviewmodel.cpp index ccdf4f9713..22b7d34be9 100644 --- a/tests/plugins/filemanager/core/dfmplugin-workspace/models/ut_fileviewmodel.cpp +++ b/tests/plugins/filemanager/core/dfmplugin-workspace/models/ut_fileviewmodel.cpp @@ -7,12 +7,15 @@ #include "plugins/filemanager/core/dfmplugin-workspace/models/fileviewmodel.h" #include "plugins/filemanager/core/dfmplugin-workspace/utils/workspacehelper.h" #include "plugins/filemanager/core/dfmplugin-workspace/utils/filesortworker.h" +#include "plugins/filemanager/core/dfmplugin-workspace/utils/filedatamanager.h" #include #include #include #include #include +#include +#include #include @@ -125,3 +128,120 @@ TEST_F(UT_FileViewModel, InitFilterSortWork) { EXPECT_EQ(model->filterSortWorker->currentKey, key); } } + +TEST_F(UT_FileViewModel, RootIndex) { + QModelIndex index = model->rootIndex(); + EXPECT_FALSE(index.isValid()); + + model->initFilterSortWork(); + index = model->rootIndex(); + EXPECT_TRUE(index.isValid()); +} + +TEST_F(UT_FileViewModel, RootUrl) { + QUrl url(QStandardPaths::standardLocations(QStandardPaths::HomeLocation).first()); + url.setScheme(Scheme::kFile); + + model->dirRootUrl = url; + EXPECT_EQ(url, model->rootUrl()); +} + +TEST_F(UT_FileViewModel, Refresh) { + bool cleanRootCalled = false; + stub.set_lamda((void(FileDataManager::*)(const QUrl &, const QString &, const bool))ADDR(FileDataManager, cleanRoot), [&cleanRootCalled]{ cleanRootCalled = true;}); + + model->refresh(); + EXPECT_TRUE(cleanRootCalled); +} + +TEST_F(UT_FileViewModel, CurrentState) { + ModelState testState = ModelState::kIdle; + model->state = testState; + + EXPECT_EQ(testState, model->currentState()); +} + +TEST_F(UT_FileViewModel, FileInfo) { + QModelIndex index; + auto infoPtr = model->fileInfo(index); + EXPECT_TRUE(infoPtr.isNull()); + + QUrl url(QStandardPaths::standardLocations(QStandardPaths::HomeLocation).first()); + url.setScheme(Scheme::kFile); + + index = model->setRootUrl(url); + model->initFilterSortWork(); + + infoPtr = model->fileInfo(index); + EXPECT_FALSE(infoPtr.isNull()); +} + +TEST_F(UT_FileViewModel, GetChildrenUrls) { + bool getChildrenUrlsCalled { false }; + stub.set_lamda(ADDR(FileSortWorker, getChildrenUrls), [&getChildrenUrlsCalled]{ + getChildrenUrlsCalled = true; + return QList(); + }); + + auto urls = model->getChildrenUrls(); + EXPECT_TRUE(urls.isEmpty()); + EXPECT_FALSE(getChildrenUrlsCalled); + + model->initFilterSortWork(); + model->getChildrenUrls(); + EXPECT_TRUE(getChildrenUrlsCalled); +} + +TEST_F(UT_FileViewModel, GetIndexByUrl) { + QUrl url(QStandardPaths::standardLocations(QStandardPaths::HomeLocation).first()); + url.setScheme(Scheme::kFile); + + QModelIndex index = model->getIndexByUrl(url); + EXPECT_FALSE(index.isValid()); + + model->initFilterSortWork(); + + stub.set_lamda(ADDR(FileSortWorker, getChildShowIndex), [&url](FileSortWorker *, const QUrl &itemUrl){ + if (url == itemUrl) + return 0; + return -1; + }); + + index = model->getIndexByUrl(url); + EXPECT_EQ(index, model->index(0, 0, model->rootIndex())); + + index = model->getIndexByUrl(QUrl()); + EXPECT_FALSE(index.isValid()); +} + +TEST_F(UT_FileViewModel, GetColumnWidth) { + stub.set_lamda(ADDR(FileViewModel, getRoleByColumn), []{ return ItemRoles::kItemUnknowRole; }); + int width = model->getColumnWidth(0); + EXPECT_EQ(width, 120); + + stub.clear(); + stub.set_lamda(ADDR(FileViewModel, getRoleByColumn), []{ return ItemRoles::kItemFileDisplayNameRole; }); + width = model->getColumnWidth(0); + + const QVariantMap &state = Application::appObtuselySetting()->value("WindowManager", "ViewColumnState").toMap(); + int colWidth = state.value(QString::number(ItemRoles::kItemFileDisplayNameRole), -1).toInt(); + if (colWidth > 0) { + EXPECT_EQ(width, colWidth); + } else { + EXPECT_EQ(width, 120); + } +} + +TEST_F(UT_FileViewModel, GetRoleByColumn) { + QList roleList = QList() << kItemFileDisplayNameRole + << kItemFileLastModifiedRole + << kItemFileSizeRole + << kItemFileMimeTypeRole; + stub.set_lamda(ADDR(FileViewModel, getColumnRoles), [&roleList]{ return roleList; }); + + ItemRoles role = model->getRoleByColumn(1); + EXPECT_EQ(role, kItemFileLastModifiedRole); + + role = model->getRoleByColumn(roleList.length()); + EXPECT_EQ(role, kItemFileDisplayNameRole); +} diff --git a/tests/plugins/filemanager/core/dfmplugin-workspace/models/ut_rootinfo.cpp b/tests/plugins/filemanager/core/dfmplugin-workspace/models/ut_rootinfo.cpp index 0780f5fbc7..4dfd4b7837 100644 --- a/tests/plugins/filemanager/core/dfmplugin-workspace/models/ut_rootinfo.cpp +++ b/tests/plugins/filemanager/core/dfmplugin-workspace/models/ut_rootinfo.cpp @@ -127,7 +127,7 @@ TEST_F(UT_RootInfo, StartWork) EXPECT_FALSE(calledGetSourceData); rootInfoObj->startWork(key, true); - EXPECT_FALSE(calledGetSourceData); + EXPECT_TRUE(calledGetSourceData); rootInfoObj->sourceDataList.append(SortInfoPointer(new SortFileInfo())); rootInfoObj->startWork(key, true); @@ -367,7 +367,7 @@ TEST_F(UT_RootInfo, HandleTraversalResult) QObject::connect(rootInfoObj, &RootInfo::iteratorAddFile, rootInfoObj, [&sendIteratorAddFile] { sendIteratorAddFile = true; }); - rootInfoObj->handleTraversalResult(info, ""); + rootInfoObj->handleTraversalResult(info, "travseToken"); EXPECT_TRUE(calledAddChild); EXPECT_TRUE(sendIteratorAddFile); @@ -392,7 +392,7 @@ TEST_F(UT_RootInfo, HandleTraversalResults) QObject::connect(rootInfoObj, &RootInfo::iteratorAddFiles, rootInfoObj, [&sendIteratorAddFiles] { sendIteratorAddFiles = true; }); - rootInfoObj->handleTraversalResults({ info }, ""); + rootInfoObj->handleTraversalResults({ info }, "travseToken"); EXPECT_TRUE(calledAddChild); EXPECT_TRUE(sendIteratorAddFiles); @@ -417,7 +417,7 @@ TEST_F(UT_RootInfo, HandleTraversalLocalResult) [&sendIteratorLocalFiles] { sendIteratorLocalFiles = true; }); SortInfoPointer info(new SortFileInfo); - rootInfoObj->handleTraversalLocalResult({ info }, sortRole, sortOrder, mixDirAndFile, ""); + rootInfoObj->handleTraversalLocalResult({ info }, sortRole, sortOrder, mixDirAndFile, "travseToken"); EXPECT_FALSE(addedChildren.isEmpty()); EXPECT_EQ(rootInfoObj->originSortRole, sortRole); @@ -434,7 +434,7 @@ TEST_F(UT_RootInfo, HandleTraversalFinish) QObject::connect(rootInfoObj, &RootInfo::traversalFinished, rootInfoObj, [¤tKey](const QString &key) { currentKey.append(key); }); - rootInfoObj->handleTraversalFinish(""); + rootInfoObj->handleTraversalFinish("travseToken"); EXPECT_TRUE(rootInfoObj->traversalFinish); } @@ -446,7 +446,7 @@ TEST_F(UT_RootInfo, HandleTraversalSort) QObject::connect(rootInfoObj, &RootInfo::requestSort, rootInfoObj, [¤tKey](const QString &key) { currentKey.append(key); }); - rootInfoObj->handleTraversalSort(""); + rootInfoObj->handleTraversalSort("travseToken"); } TEST_F(UT_RootInfo, HandleGetSourceData) @@ -587,5 +587,6 @@ TEST_F(UT_RootInfo, Bug_195309_fileInfo) EXPECT_FALSE(calledRefresh); rootInfoObj->fileInfo(validUrl); - EXPECT_TRUE(calledRefresh); + // rootInfoObj->fileInfo() did not call fileinfo.refresh() anymore. + // EXPECT_TRUE(calledRefresh); }