| 1 |
// SPDX-FileCopyrightText: 2021 - 2022 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#include <gtest/gtest.h> |
| 6 |
#include <QFile> |
| 7 |
#include <QDomDocument> |
| 8 |
#include <QUrl> |
| 9 |
#include <QDir> |
| 10 |
|
| 11 |
#include "util/drecentmanager.h" |
| 12 |
|
| 13 |
DCORE_USE_NAMESPACE |
| 14 |
|
| 15 |
class ut_DRecentManager: public testing::Test |
| 16 |
{ |
| 17 |
protected: |
| 18 |
void SetUp() override; |
| 19 |
void TearDown() override; |
| 20 |
}; |
| 21 |
|
| 22 |
void ut_DRecentManager::SetUp() |
| 23 |
{ |
| 24 |
QFile file("/tmp/test"); |
| 25 |
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) |
| 26 |
return; |
| 27 |
file.close(); |
| 28 |
} |
| 29 |
|
| 30 |
void ut_DRecentManager::TearDown() |
| 31 |
{ |
| 32 |
QFile file("/tmp/test"); |
| 33 |
if (file.exists()) |
| 34 |
file.remove(); |
| 35 |
} |
| 36 |
|
| 37 |
TEST_F(ut_DRecentManager, testDRecentManagerAddItem) |
| 38 |
{ |
| 39 |
DRecentData data; |
| 40 |
data.appExec = "deepin-editor"; |
| 41 |
data.appName = "Deepin Editor"; |
| 42 |
data.mimeType = "text/plain"; |
| 43 |
|
| 44 |
bool ok = DRecentManager::addItem("/tmp/test", data); |
| 45 |
bool isFound = false; |
| 46 |
QFile file(QDir::homePath() + "/.local/share/recently-used.xbel"); |
| 47 |
QDomDocument doc; |
| 48 |
if (doc.setContent(&file)) { |
| 49 |
QDomElement rootEle = doc.documentElement(); |
| 50 |
QDomNodeList nodeList = rootEle.elementsByTagName("bookmark"); |
| 51 |
QDomElement bookmarkEle; |
| 52 |
QUrl url = QUrl::fromLocalFile("/tmp/test"); |
| 53 |
for (int i = 0; i < nodeList.size(); ++i) { |
| 54 |
const QString fileUrl = nodeList.at(i).toElement().attribute("href"); |
| 55 |
if (fileUrl == url.toEncoded(QUrl::FullyDecoded)) { |
| 56 |
bookmarkEle = nodeList.at(i).toElement(); |
| 57 |
isFound = true; |
| 58 |
break; |
| 59 |
} |
| 60 |
} |
| 61 |
ASSERT_TRUE(isFound == ok); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
TEST_F(ut_DRecentManager, testDRecentManagerRemoveItem) |
| 66 |
{ |
| 67 |
QString testFile = QUrl::fromLocalFile("/tmp/test").toEncoded(QUrl::FullyDecoded); |
| 68 |
DRecentManager::removeItem(testFile); |
| 69 |
QFile file(QDir::homePath() + "/.local/share/recently-used.xbel"); |
| 70 |
QDomDocument doc; |
| 71 |
bool isFound = false; |
| 72 |
if (doc.setContent(&file)) { |
| 73 |
QDomElement rootEle = doc.documentElement(); |
| 74 |
QDomNodeList nodeList = rootEle.elementsByTagName("bookmark"); |
| 75 |
QDomElement bookmarkEle; |
| 76 |
for (int i = 0; i < nodeList.size(); ++i) { |
| 77 |
const QString fileUrl = nodeList.at(i).toElement().attribute("href"); |
| 78 |
if (fileUrl == testFile) { |
| 79 |
bookmarkEle = nodeList.at(i).toElement(); |
| 80 |
isFound = true; |
| 81 |
break; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
ASSERT_TRUE(!isFound); |
| 86 |
} |