1 /* This file is part of the KDE project
2 Copyright 2007 David Faure <faure@kde.org>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License or ( at
7 your option ) version 3 or, at the discretion of KDE e.V. ( which shall
8 act as a proxy as in section 14 of the GPLv3 ), any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 Boston, MA 02110-1301, USA.
21 #include <qtest_kde.h>
23 #include <kconfiggroup.h>
25 #include <kdesktopfile.h>
26 #include <kstandarddirs.h>
29 #include <mimetypedata.h>
30 #include <mimetypewriter.h>
33 class FileTypesTest
: public QObject
40 m_kdehome
= QDir::home().canonicalPath() + "/.kde-unit-test";
41 // We need a place where we can hack a mimeapps.list without harm, so not ~/.local
42 // This test relies on shared-mime-info being installed in /usr/share [or kdedir/share]
43 ::setenv("XDG_DATA_DIRS", QFile::encodeName(m_kdehome
+ "/share:/usr/share"), 1 );
44 QFile::remove(m_kdehome
+ "/share/applications/mimeapps.list");
46 // Create fake application for some tests below.
47 bool mustUpdateKSycoca
= false;
48 fakeApplication
= KStandardDirs::locateLocal("xdgdata-apps", "fakeapplication.desktop");
49 const bool mustCreateFakeService
= !QFile::exists(fakeApplication
);
50 if (mustCreateFakeService
) {
51 mustUpdateKSycoca
= true;
52 KDesktopFile
file(fakeApplication
);
53 KConfigGroup group
= file
.desktopGroup();
54 group
.writeEntry("Name", "FakeApplication");
55 group
.writeEntry("Type", "Application");
56 group
.writeEntry("Exec", "ls");
59 // Cleanup after testMimeTypePatterns if it failed mid-way
60 const QString packageFileName
= KStandardDirs::locateLocal( "xdgdata-mime", "packages/text-plain.xml" );
61 if (!packageFileName
.isEmpty()) {
62 QFile::remove(packageFileName
);
63 MimeTypeWriter::runUpdateMimeDatabase();
64 mustUpdateKSycoca
= true;
67 if ( mustUpdateKSycoca
) {
68 // Update ksycoca in ~/.kde-unit-test after creating the above
69 QProcess::execute( KGlobal::dirs()->findExe(KBUILDSYCOCA_EXENAME
) );
71 KService::Ptr fakeApplicationService
= KService::serviceByDesktopPath(fakeApplication
);
72 QVERIFY(fakeApplicationService
);
75 void testMimeTypeGroupAutoEmbed()
77 MimeTypeData
data("text");
78 QCOMPARE(data
.majorType(), QString("text"));
79 QCOMPARE(data
.name(), QString("text"));
80 QVERIFY(data
.isMeta());
81 QCOMPARE(data
.autoEmbed(), MimeTypeData::No
); // text doesn't autoembed by default
82 QVERIFY(!data
.isDirty());
83 data
.setAutoEmbed(MimeTypeData::Yes
);
84 QCOMPARE(data
.autoEmbed(), MimeTypeData::Yes
);
85 QVERIFY(data
.isDirty());
86 data
.sync(); // save to disk
87 QVERIFY(!data
.isDirty());
88 // Check what's on disk by creating another MimeTypeData instance
89 MimeTypeData
data2("text");
90 QCOMPARE(data2
.autoEmbed(), MimeTypeData::Yes
);
91 QVERIFY(!data2
.isDirty());
92 data2
.setAutoEmbed(MimeTypeData::No
); // revert to default, for next time
93 QVERIFY(data2
.isDirty());
95 QVERIFY(!data2
.isDirty());
97 // TODO test askSave after cleaning up the code
100 void testMimeTypeAutoEmbed()
102 MimeTypeData
data(KMimeType::mimeType("text/plain"));
103 QCOMPARE(data
.majorType(), QString("text"));
104 QCOMPARE(data
.minorType(), QString("plain"));
105 QCOMPARE(data
.name(), QString("text/plain"));
106 QVERIFY(!data
.isMeta());
107 QCOMPARE(data
.autoEmbed(), MimeTypeData::UseGroupSetting
);
108 QVERIFY(!data
.isDirty());
109 data
.setAutoEmbed(MimeTypeData::Yes
);
110 QCOMPARE(data
.autoEmbed(), MimeTypeData::Yes
);
111 QVERIFY(data
.isDirty());
112 data
.sync(); // save to disk
113 QVERIFY(!data
.isDirty());
114 // Check what's on disk by creating another MimeTypeData instance
115 MimeTypeData
data2(KMimeType::mimeType("text/plain"));
116 QCOMPARE(data2
.autoEmbed(), MimeTypeData::Yes
);
117 QVERIFY(!data2
.isDirty());
118 data2
.setAutoEmbed(MimeTypeData::UseGroupSetting
); // revert to default, for next time
119 QVERIFY(data2
.isDirty());
121 QVERIFY(!data2
.isDirty());
124 void testMimeTypePatterns()
126 MimeTypeData
data(KMimeType::mimeType("text/plain"));
127 QCOMPARE(data
.name(), QString("text/plain"));
128 QCOMPARE(data
.majorType(), QString("text"));
129 QCOMPARE(data
.minorType(), QString("plain"));
130 QVERIFY(!data
.isMeta());
131 QStringList patterns
= data
.patterns();
132 QVERIFY(patterns
.contains("*.txt"));
133 QVERIFY(!patterns
.contains("*.toto"));
134 const QStringList origPatterns
= patterns
;
135 patterns
.append("*.toto"); // yes, a french guy wrote this, as you can see
136 patterns
.sort(); // for future comparisons
137 QVERIFY(!data
.isDirty());
138 data
.setPatterns(patterns
);
139 QVERIFY(data
.isDirty());
140 bool needUpdateMimeDb
= data
.sync();
141 QVERIFY(needUpdateMimeDb
);
142 MimeTypeWriter::runUpdateMimeDatabase();
144 QCOMPARE(data
.patterns(), patterns
);
145 data
.refresh(); // reload from ksycoca
146 QCOMPARE(data
.patterns(), patterns
);
147 QVERIFY(!data
.isDirty());
148 // Check what's in ksycoca
149 QStringList newPatterns
= KMimeType::mimeType("text/plain")->patterns();
151 QCOMPARE(newPatterns
, patterns
);
153 // Remove custom file (it's in ~/.local, not in ~/.kde-unit-test, so it messes up the user's configuration!)
154 const QString packageFileName
= KStandardDirs::locateLocal( "xdgdata-mime", "packages/text-plain.xml" );
155 QVERIFY(!packageFileName
.isEmpty());
156 QFile::remove(packageFileName
);
157 MimeTypeWriter::runUpdateMimeDatabase();
159 // Check what's in ksycoca
160 newPatterns
= KMimeType::mimeType("text/plain")->patterns();
162 QCOMPARE(newPatterns
, origPatterns
);
165 void testAddService()
167 const char* mimeTypeName
= "application/vnd.oasis.opendocument.text";
168 MimeTypeData
data(KMimeType::mimeType(mimeTypeName
));
169 QStringList appServices
= data
.appServices();
170 qDebug() << appServices
;
171 QVERIFY(!appServices
.contains(fakeApplication
)); // already there? hmm can't really test then
172 QVERIFY(!data
.isDirty());
173 appServices
.prepend(fakeApplication
);
174 data
.setAppServices(appServices
);
175 QVERIFY(data
.isDirty());
178 // Check what's in ksycoca
179 MimeTypeData
data2(KMimeType::mimeType(mimeTypeName
));
180 qDebug() << data2
.appServices();
181 QVERIFY(data2
.appServices().contains(fakeApplication
));
182 QCOMPARE(data2
.appServices(), appServices
);
183 QVERIFY(!data
.isDirty());
185 // Now test removing (in the same test, since it's inter-dependent)
186 appServices
.removeAll(fakeApplication
);
187 data
.setAppServices(appServices
);
188 QVERIFY(data
.isDirty());
191 QCOMPARE(data
.appServices(), appServices
);
192 QVERIFY(!data
.appServices().contains(fakeApplication
));
195 // TODO test removing an "implicit association"
198 // TODO see TODO in filetypesview
199 //void testDeleteMimeType()
203 void cleanupTestCase()
205 // If we remove it, then every run of the unit test has to run kbuildsycoca... slow.
206 //QFile::remove(KStandardDirs::locateLocal("xdgdata-apps", "fakeapplication.desktop"));
209 private: // helper methods
211 void runKBuildSycoca()
213 QProcess::execute( KGlobal::dirs()->findExe(KBUILDSYCOCA_EXENAME
) );
214 // Wait for notifyDatabaseChanged DBus signal
215 // (The real KCM code simply does the refresh in a slot, asynchronously)
217 QObject::connect(KSycoca::self(), SIGNAL(databaseChanged()), &loop
, SLOT(quit()));
221 QString fakeApplication
;
225 QTEST_KDEMAIN( FileTypesTest
, NoGUI
)
227 #include "filetypestest.moc"