File indexing completed on 2022-11-29 18:25:18

0001 /* This file is part of the KDE libraries
0002    Copyright (c) 2005-2007 David Jarvie <djarvie@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #ifndef KTIMEZONESTEST_P_H
0021 #define KTIMEZONESTEST_P_H
0022 
0023 #include <kconfig.h>
0024 #include <kconfiggroup.h>
0025 #include <qstandardpaths.h>
0026 #include <QDebug>
0027 #include <QDir>
0028 #include <qtest.h>
0029 
0030 class TimeZoneTestData
0031 {
0032 public:
0033     QString dataDir() const
0034     {
0035         return mDataDir;
0036     }
0037 
0038     void cleanupTimeZoneTest()
0039     {
0040         removeDir(QLatin1String("ktimezonestest/Africa"));
0041         removeDir(QLatin1String("ktimezonestest/America"));
0042         removeDir(QLatin1String("ktimezonestest/Asia"));
0043         removeDir(QLatin1String("ktimezonestest/Europe"));
0044         removeDir(QLatin1String("ktimezonestest"));
0045         QFile::remove(QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + QLatin1Char('/') + "ktimezonedrc");
0046     }
0047 
0048     void setupTimeZoneTest()
0049     {
0050         cleanupTimeZoneTest();
0051 
0052         mDataDir = QDir::homePath() + "/.kde-unit-test/ktimezonestest";
0053         QVERIFY(QDir().mkpath(mDataDir));
0054         writeZoneTab(false);
0055         QDir dir(mDataDir);
0056         QVERIFY(dir.mkdir("Africa"));
0057         QVERIFY(QFile::copy(QFINDTESTDATA("Cairo"), mDataDir + QLatin1String("/Africa/Cairo")));
0058         QVERIFY(QFile::copy(QFINDTESTDATA("Johannesburg"), mDataDir + QLatin1String("/Africa/Johannesburg")));
0059         QVERIFY(dir.mkdir("America"));
0060         QFile::copy(QFINDTESTDATA("Los_Angeles"), mDataDir + QLatin1String("/America/Los_Angeles"));
0061         QVERIFY(dir.mkdir("Asia"));
0062         QFile::copy(QFINDTESTDATA("Dili"), mDataDir + QLatin1String("/Asia/Dili"));
0063         QVERIFY(dir.mkdir("Europe"));
0064         QFile::copy(QFINDTESTDATA("Berlin"), mDataDir + QLatin1String("/Europe/Berlin"));
0065         QFile::copy(QFINDTESTDATA("London"), mDataDir + QLatin1String("/Europe/London"));
0066         QFile::copy(QFINDTESTDATA("Paris"), mDataDir + QLatin1String("/Europe/Paris"));
0067 
0068         KConfig config("ktimezonedrc");
0069         KConfigGroup group(&config, "TimeZones");
0070         group.writeEntry("ZoneinfoDir", mDataDir);
0071         group.writeEntry("Zonetab", QString(mDataDir + QString::fromLatin1("/zone.tab")));
0072         group.writeEntry("LocalZone", QString::fromLatin1("Europe/Paris"));
0073         config.sync();
0074     }
0075 
0076 private:
0077     void removeDir(const QString &subdir)
0078     {
0079         QDir local(QDir::homePath() + QLatin1String("/.kde-unit-test/") + subdir);
0080         foreach (const QString &file, local.entryList(QDir::Files))
0081             if (!local.remove(file)) {
0082                 qWarning("%s: removing failed", qPrintable(file));
0083             }
0084         QCOMPARE((int)local.entryList(QDir::Files).count(), 0);
0085         local.cdUp();
0086         QString subd = subdir;
0087         subd.remove(QRegExp("^.*/"));
0088         local.rmpath(subd);
0089     }
0090 
0091 public:
0092     void writeZoneTab(bool testcase)
0093     {
0094         QFile f(mDataDir + QLatin1String("/zone.tab"));
0095         qDebug() << "Writing" << f.fileName();
0096         QVERIFY(f.open(QIODevice::WriteOnly));
0097         QTextStream fStream(&f);
0098         if (testcase)
0099             fStream << "DE  +5230+01322 Europe/Berlin\n"
0100                     "EG +3003+03115 Africa/Cairo\n"
0101                     "FR +4852+00220 Europe/Paris\n"
0102                     "XX -512830+0001845 Europe/London   Greater Britain\n"
0103                     "TL -0833+12535 Asia/Dili\n"
0104                     "US +340308-1181434 America/Los_Angeles Pacific Time\n"
0105                     "ZA -2615+02800 Africa/Johannesburg\n";
0106         else
0107             fStream << "EG  +3003+03115 Africa/Cairo\n"
0108                     "FR +4852+00220 Europe/Paris\n"
0109                     "GB +512830-0001845 Europe/London   Great Britain\n"
0110                     "US +340308-1181434 America/Los_Angeles Pacific Time\n"
0111                     "ZA -2615+02800 Africa/Johannesburg\n";
0112         f.close();
0113     }
0114 
0115     QString mDataDir;
0116 };
0117 
0118 #endif //KTIMEZONESTEST_P_H