File indexing completed on 2024-04-21 14:55:07

0001 /* This file is part of the KDE libraries
0002    Copyright (C) 2007 David Faure <faure@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 #include <QTest>
0021 #include <k4timezonewidget.h>
0022 #include <ksystemtimezone.h>
0023 #include <kconfiggroup.h>
0024 #include "ktimezonestest_p.h"
0025 
0026 class K4TimeZoneWidgetTest : public QObject
0027 {
0028     Q_OBJECT
0029 
0030 public:
0031     void init()
0032     {
0033         initTestCase();
0034     }
0035 
0036 private Q_SLOTS:
0037 
0038     void initTestCase()
0039     {
0040         //mTestData.setupTimeZoneTest(); // see ktimezonestest_p.h
0041     }
0042 
0043     void cleanupTestCase()
0044     {
0045         //mTestData.cleanupTimeZoneTest();
0046     }
0047 
0048     void testSetSelected()
0049     {
0050         if (!KSystemTimeZones::isTimeZoneDaemonAvailable()) {
0051             QSKIP("ktimezoned not available, check that kded5 is running and /modules/ktimezoned is available");
0052         }
0053 
0054         K4TimeZoneWidget tzw;
0055         QVERIFY(tzw.topLevelItemCount() > 0);
0056         QVERIFY(tzw.selectedItems().isEmpty());
0057 
0058         // Single selection mode (default)
0059         QVERIFY(tzw.selectionMode() == K4TimeZoneWidget::SingleSelection);
0060         tzw.setSelected("Europe/Paris", true);
0061         QCOMPARE(tzw.selectedItems().count(), 1);
0062         QCOMPARE(tzw.selection(), QStringList() << "Europe/Paris");
0063         tzw.setSelected("Africa/Cairo", true);
0064         QCOMPARE(tzw.selectedItems().count(), 1);
0065         QCOMPARE(tzw.selection(), QStringList() << "Africa/Cairo");
0066 
0067         // Multiple selections explicitly allowed
0068         tzw.setSelectionMode(K4TimeZoneWidget::MultiSelection);
0069         tzw.clearSelection();
0070         QVERIFY(tzw.selectedItems().isEmpty());
0071         tzw.setSelected("Europe/Paris", true);
0072         QCOMPARE(tzw.selectedItems().count(), 1);
0073         QCOMPARE(tzw.selection(), QStringList() << "Europe/Paris");
0074         tzw.setSelected("America/Los_Angeles", true);
0075         QCOMPARE(tzw.selectedItems().count(), 2);
0076         QCOMPARE(tzw.selection(), QStringList() << "America/Los_Angeles" << "Europe/Paris");
0077     }
0078 
0079     void testCheckableItems()
0080     {
0081         if (!KSystemTimeZones::isTimeZoneDaemonAvailable()) {
0082             QSKIP("ktimezoned not available, check that kded5 is running and /modules/ktimezoned is available");
0083         }
0084 
0085         K4TimeZoneWidget tzw;
0086         tzw.setItemsCheckable(true);
0087         QVERIFY(tzw.topLevelItemCount() > 0);
0088         QVERIFY(tzw.selectedItems().isEmpty());
0089         QVERIFY(tzw.selection().isEmpty());
0090 
0091         // Single selection mode (default)
0092         QVERIFY(tzw.selectionMode() == K4TimeZoneWidget::SingleSelection);
0093         tzw.setSelected("Europe/Paris", true);
0094         QCOMPARE(tzw.selectedItems().count(), 0); // it got checked, not selected
0095         QCOMPARE(tzw.selection(), QStringList() << "Europe/Paris");
0096         tzw.setSelected("Africa/Cairo", true);
0097         QCOMPARE(tzw.selection(), QStringList() << "Africa/Cairo");
0098 
0099         // Multiple selections explicitly allowed
0100         tzw.setSelectionMode(K4TimeZoneWidget::MultiSelection);
0101         tzw.clearSelection();
0102         tzw.setSelected("Europe/Paris", true);
0103         QCOMPARE(tzw.selection(), QStringList() << "Europe/Paris");
0104         tzw.setSelected("America/Los_Angeles", true);
0105         QCOMPARE(tzw.selection(), QStringList() << "America/Los_Angeles" << "Europe/Paris");
0106     }
0107 private:
0108     // Because we don't use a separate KDEHOME, we can't use TimeZoneTestData.
0109     // It would remove ktimezonedrc from the user!
0110     //TimeZoneTestData mTestData;
0111 };
0112 
0113 // Note: no QStandardPaths::setTestModeEnabled(true) here.
0114 // The kded module writes out a config file, but unit tests have
0115 // a different configuration directory so they don't see those config files.
0116 // Ideally unit tests should talk to their own kded instance,
0117 // but that means starting a new DBus session bus for all (each?) unit tests, somehow...
0118 
0119 QTEST_MAIN(K4TimeZoneWidgetTest)
0120 
0121 #include "k4timezonewidget_unittest.moc"