-
Notifications
You must be signed in to change notification settings - Fork 33
/
tst_xdgdesktopfile.cpp
116 lines (98 loc) · 3.44 KB
/
tst_xdgdesktopfile.cpp
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* libqtxdg - An Qt implementation of freedesktop.org xdg specs.
* Copyright (C) 2016 Luís Pereira <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "tst_xdgdesktopfile.h"
#include "XdgDesktopFile"
#include <QtTest>
class Language
{
public:
Language (const QString& lang)
: mPreviousLang(QString::fromLocal8Bit(qgetenv("LC_MESSAGES")))
{
qputenv("LC_MESSAGES", lang.toLocal8Bit());
}
~Language()
{
qputenv("LC_MESSAGES", mPreviousLang.toLocal8Bit());
}
private:
QString mPreviousLang;
};
QTEST_MAIN(tst_xdgdesktopfile)
void tst_xdgdesktopfile::testRead()
{
QTemporaryFile file(QDir::temp().filePath(QStringLiteral("testReadXXXXXX.desktop")));
QVERIFY(file.open());
const QString fileName = file.fileName();
QTextStream ts(&file);
ts <<
"[Desktop Entry]\n"
"Type=Application\n"
"Name=MyApp\n"
"Icon=foobar\n"
"MimeType=text/plain;image/png;;\n"
"\n";
file.close();
QVERIFY(QFile::exists(fileName));
XdgDesktopFile df;
df.load(fileName);
QVERIFY(df.isValid());
QCOMPARE(df.type(), XdgDesktopFile::ApplicationType);
QCOMPARE(df.name(), QString::fromLatin1("MyApp"));
QCOMPARE(df.iconName(), QString::fromLatin1("foobar"));
QCOMPARE(df.mimeTypes(), QStringList() << QString::fromLatin1("text/plain")
<< QString::fromLatin1("image/png"));
QCOMPARE(df.fileName(), QFileInfo(fileName).canonicalFilePath());
}
void tst_xdgdesktopfile::testReadLocalized_data()
{
QTest::addColumn<QString>("locale");
QTest::addColumn<QString>("translation");
const QString pt = QString::fromUtf8("A Minha Aplicação");
const QString pt_BR = QString::fromUtf8("O Meu Aplicativo");
QTest::newRow("pt") << QStringLiteral("pt") << pt;
QTest::newRow("pt_PT") << QStringLiteral("pt_PT") << pt;
QTest::newRow("pt_BR") << QStringLiteral("pt_BR") << pt_BR;
QTest::newRow("de") << QStringLiteral("de") << QStringLiteral("My Application");
}
void tst_xdgdesktopfile::testReadLocalized()
{
QTemporaryFile file(QDir::temp().filePath(QStringLiteral("testReadLocalizedXXXXXX.desktop")));
QVERIFY(file.open());
const QString fileName = file.fileName();
QTextStream ts(&file);
ts << QString::fromUtf8(
"[Desktop Entry]\n"
"Type=Application\n"
"Name=My Application\n"
"Name[pt]=A Minha Aplicação\n"
"Name[pt_BR]=O Meu Aplicativo\n"
"Icon=foo\n"
"\n");
file.close();
QVERIFY(QFile::exists(fileName));
XdgDesktopFile df;
df.load(fileName);
QVERIFY(df.isValid());
QFETCH(QString, locale);
QFETCH(QString, translation);
Language lang(locale);
QCOMPARE(df.localizedValue(QStringLiteral("Name")).toString(), translation);
}