Skip to content

Commit

Permalink
test: [workspace]workspace ut
Browse files Browse the repository at this point in the history
add fileviewmodel ut

Log: add workspace ut
Task: https://pms.uniontech.com/task-view-278215.html
  • Loading branch information
Lighto-Ku committed Jul 24, 2023
1 parent a535e07 commit c7ea43d
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <dfm-base/base/schemefactory.h>
#include <dfm-base/file/local/asyncfileinfo.h>
#include <dfm-base/file/local/syncfileinfo.h>
#include <dfm-base/file/local/localdiriterator.h>
#include <dfm-base/file/local/localfilewatcher.h>
#include <dfm-base/base/application/application.h>
#include <dfm-base/base/application/settings.h>

#include <gtest/gtest.h>

Expand Down Expand Up @@ -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<QUrl>();
});

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<ItemRoles> roleList = QList<ItemRoles>() << 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -434,7 +434,7 @@ TEST_F(UT_RootInfo, HandleTraversalFinish)
QObject::connect(rootInfoObj, &RootInfo::traversalFinished, rootInfoObj,
[&currentKey](const QString &key) { currentKey.append(key); });

rootInfoObj->handleTraversalFinish("");
rootInfoObj->handleTraversalFinish("travseToken");

EXPECT_TRUE(rootInfoObj->traversalFinish);
}
Expand All @@ -446,7 +446,7 @@ TEST_F(UT_RootInfo, HandleTraversalSort)
QObject::connect(rootInfoObj, &RootInfo::requestSort, rootInfoObj,
[&currentKey](const QString &key) { currentKey.append(key); });

rootInfoObj->handleTraversalSort("");
rootInfoObj->handleTraversalSort("travseToken");
}

TEST_F(UT_RootInfo, HandleGetSourceData)
Expand Down Expand Up @@ -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);
}

0 comments on commit c7ea43d

Please sign in to comment.