File: ut_drecentmanager.cpp

package info (click to toggle)
dtkcore 5.7.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,728 kB
  • sloc: cpp: 22,021; ansic: 183; python: 68; xml: 58; makefile: 27; sh: 15
file content (86 lines) | stat: -rw-r--r-- 2,503 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// SPDX-FileCopyrightText: 2021 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include <gtest/gtest.h>
#include <QFile>
#include <QDomDocument>
#include <QUrl>
#include <QDir>

#include "util/drecentmanager.h"

DCORE_USE_NAMESPACE

class ut_DRecentManager: public testing::Test
{
protected:
    void SetUp() override;
    void TearDown() override;
};

void ut_DRecentManager::SetUp()
{
    QFile file("/tmp/test");
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
        return;
    file.close();
}

void ut_DRecentManager::TearDown()
{
    QFile file("/tmp/test");
    if (file.exists())
        file.remove();
}

TEST_F(ut_DRecentManager, testDRecentManagerAddItem)
{
    DRecentData data;
    data.appExec = "deepin-editor";
    data.appName = "Deepin Editor";
    data.mimeType = "text/plain";

    bool ok = DRecentManager::addItem("/tmp/test", data);
    bool isFound = false;
    QFile file(QDir::homePath() + "/.local/share/recently-used.xbel");
    QDomDocument doc;
    if (doc.setContent(&file)) {
        QDomElement rootEle = doc.documentElement();
        QDomNodeList nodeList = rootEle.elementsByTagName("bookmark");
        QDomElement bookmarkEle;
        QUrl url = QUrl::fromLocalFile("/tmp/test");
        for (int i = 0; i < nodeList.size(); ++i) {
            const QString fileUrl = nodeList.at(i).toElement().attribute("href");
            if (fileUrl == url.toEncoded(QUrl::FullyDecoded)) {
                bookmarkEle = nodeList.at(i).toElement();
                isFound = true;
                break;
            }
        }
        ASSERT_TRUE(isFound == ok);
    }
}

TEST_F(ut_DRecentManager, testDRecentManagerRemoveItem)
{
    QString testFile = QUrl::fromLocalFile("/tmp/test").toEncoded(QUrl::FullyDecoded);
    DRecentManager::removeItem(testFile);
    QFile file(QDir::homePath() + "/.local/share/recently-used.xbel");
    QDomDocument doc;
    bool isFound = false;
    if (doc.setContent(&file)) {
        QDomElement rootEle = doc.documentElement();
        QDomNodeList nodeList = rootEle.elementsByTagName("bookmark");
        QDomElement bookmarkEle;
        for (int i = 0; i < nodeList.size(); ++i) {
            const QString fileUrl = nodeList.at(i).toElement().attribute("href");
            if (fileUrl == testFile) {
                bookmarkEle = nodeList.at(i).toElement();
                isFound = true;
                break;
            }
        }
    }
    ASSERT_TRUE(!isFound);
}