Debian Codegraph @

tests/ut_ddesktopentrytest.cpp

raw
1 // SPDX-FileCopyrightText: 2019 - 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include <QDebug>
6 #include <QString>
7 #include <QTemporaryFile>
8 #include <DDesktopEntry>
9 #include <gtest/gtest.h>
10 #include <QLocale>
11
12 DCORE_USE_NAMESPACE
13
14 const QString testFileContent = { QStringLiteral(R"desktop(# A. Example Desktop Entry File
15 [Desktop Entry]
16 Version=1.0
17 Type=Application
18 Name=Foo Viewer
19 Name[zh_CN]=福查看器
20 Comment=The best viewer for Foo objects available!
21 # Next line have an extra " character
22 Comment[zh_CN]=最棒的 "福 查看器!
23 TryExec=fooview
24 Exec=fooview %F
25 Icon=fooview
26 MimeType=image/x-foo;
27 Actions=Gallery;Create;
28
29 [Desktop Action Gallery]
30 Exec=fooview --gallery
31 Name=Browse Gallery
32
33 [Desktop Action Create]
34 Exec=fooview --create-new
35 Name=Create a new Foo!
36 Icon=fooview-new
37 )desktop") };
38
39 class ut_DesktopEntry : public testing::Test
40 {
41 public:
42 static void SetUpTestCase()
43 {
44 //qDebug() << "*****************" << __FUNCTION__;
45 }
46 static void TearDownTestCase()
47 {
48 //qDebug() << "*****************" << __FUNCTION__;
49 }
50 virtual void SetUp();
51 virtual void TearDown();
52 };
53 void ut_DesktopEntry::SetUp()
54 {
55
56 }
57 void ut_DesktopEntry::TearDown()
58 {
59
60 }
61
62 TEST_F(ut_DesktopEntry, ParseFile)
63 {
64 QTemporaryFile file("testReadXXXXXX.desktop");
65 ASSERT_TRUE(file.open());
66 const QString fileName = file.fileName();
67 QTextStream ts(&file);
68 ts << testFileContent;
69 file.close();
70 ASSERT_TRUE(QFile::exists(fileName));
71
72 QScopedPointer<DDesktopEntry> desktopFile(new DDesktopEntry(fileName));
73 QStringList allGroups = desktopFile->allGroups();
74 ASSERT_EQ(allGroups.count(), 3);
75 ASSERT_TRUE(allGroups.contains("Desktop Entry") &&
76 allGroups.contains("Desktop Action Gallery") &&
77 allGroups.contains("Desktop Action Create"));
78 ASSERT_EQ(desktopFile->allGroups(true)[0], QStringLiteral("Desktop Entry"));
79 ASSERT_EQ(desktopFile->localizedValue("Name", "zh_CN"), QStringLiteral("福查看器"));
80 ASSERT_EQ(desktopFile->localizedValue("Name", "empty"), QStringLiteral("Foo Viewer"));
81 ASSERT_EQ(desktopFile->keys("Desktop Entry"),
82 QStringList({"Actions", "Comment", "Comment[zh_CN]", "Exec", "Icon", "MimeType", "Name", "Name[zh_CN]", "TryExec", "Type", "Version"}));
83
84 {
85 struct RestoreLocale {
86 ~RestoreLocale() { QLocale::setDefault(QLocale::system()); }
87 } restoreLocale;
88 Q_UNUSED(restoreLocale);
89
90 QLocale::setDefault(QLocale("zh_CN"));
91 ASSERT_EQ(desktopFile->localizedValue("Name"), QStringLiteral("福查看器"));
92
93 QLocale::setDefault(QLocale::c());
94 ASSERT_EQ(desktopFile->localizedValue("Name"), QStringLiteral("Foo Viewer"));
95 }
96
97 ASSERT_EQ(desktopFile->stringValue("Name"), QStringLiteral("Foo Viewer"));
98 ASSERT_EQ(desktopFile->setRawValue("Bar Viewer", "Name"), true);
99 ASSERT_EQ(desktopFile->stringValue("Name"), QStringLiteral("Bar Viewer"));
100 ASSERT_EQ(desktopFile->setLocalizedValue("霸查看器", "zh_CN", "Name"), true);
101 ASSERT_EQ(desktopFile->localizedValue("Name", "zh_CN"), QStringLiteral("霸查看器"));
102 ASSERT_EQ(desktopFile->contains("Semicolon"), false);
103 ASSERT_EQ(desktopFile->setRawValue(";grp\\;2;grp3;", "Semicolon"), true);
104 ASSERT_EQ(desktopFile->stringListValue("Semicolon"), QStringList({"", "grp;2", "grp3"}));
105 ASSERT_EQ(desktopFile->contains("Semicolon"), true);
106 ASSERT_EQ(desktopFile->removeEntry("Semicolon"), true);
107 ASSERT_EQ(desktopFile->contains("Semicolon"), false);
108
109 //qDebug() << desktopFile->save();
110 //qDebug() << fileName;
111 }
112
113 TEST_F(ut_DesktopEntry, escape)
114 {
115 QString slash("\\\\");
116 ASSERT_TRUE(DDesktopEntry::escapeExec(slash) == slash);
117 ASSERT_TRUE(DDesktopEntry::unescapeExec(slash) == slash);
118 }