diff options
author | Jean-Luc Lamadon <jean-luc.lamadon@nokia.com> | 2009-12-10 16:43:57 +0200 |
---|---|---|
committer | Jean-Luc Lamadon <jean-luc.lamadon@nokia.com> | 2009-12-10 16:43:57 +0200 |
commit | 3463f92c2bfa6dfbcec483fa5ef9af1ee51cbbac (patch) | |
tree | 3e685c9bf185aa8b49d5082837b5ea44f2794de3 | |
parent | 8e2c9f447b84fad42a4fcd3c6db35c6b900078cc (diff) | |
parent | c435fb3c7b523ce8abbb6b87d802dbd7c12b6a1c (diff) |
Merge branch 'needed-for-commander' to master
Signed-off-by: Jean-Luc Lamadon <jean-luc.lamadon@nokia.com>
-rw-r--r-- | configure.ac | 3 | ||||
-rw-r--r-- | debian/changelog | 10 | ||||
-rw-r--r-- | debian/control | 1 | ||||
-rw-r--r-- | libcontextsubscriber/src/Makefile.am | 5 | ||||
-rw-r--r-- | libcontextsubscriber/src/contextjson.h | 70 | ||||
-rw-r--r-- | libcontextsubscriber/src/contexttypeinfo.cpp | 88 | ||||
-rw-r--r-- | libcontextsubscriber/src/contexttypeinfo.h | 31 | ||||
-rw-r--r-- | libcontextsubscriber/src/duration.cpp | 204 | ||||
-rw-r--r-- | libcontextsubscriber/src/duration.h | 71 | ||||
-rw-r--r-- | libcontextsubscriber/unit-tests/Makefile.am | 1 | ||||
-rw-r--r-- | libcontextsubscriber/unit-tests/duration/.gitignore | 3 | ||||
-rw-r--r-- | libcontextsubscriber/unit-tests/duration/Makefile.am | 21 | ||||
-rw-r--r-- | libcontextsubscriber/unit-tests/duration/testduration.cpp | 189 |
13 files changed, 677 insertions, 20 deletions
diff --git a/configure.ac b/configure.ac index 557e045b..8d4814ea 100644 --- a/configure.ac +++ b/configure.ac @@ -1,5 +1,5 @@ AC_PREREQ([2.61]) -AC_INIT([ContextKit], [0.5.3~unreleased], [marius.vollmer@nokia.com], ContextKit) +AC_INIT([ContextKit], [0.5.4~unreleased], [marius.vollmer@nokia.com], ContextKit) AC_CONFIG_SRCDIR([Makefile.am]) AM_INIT_AUTOMAKE([-Wall -Werror foreign dist-bzip2 tar-ustar 1.9]) @@ -79,6 +79,7 @@ AC_CONFIG_FILES([ libcontextsubscriber/unit-tests/nanoxml/Makefile libcontextsubscriber/unit-tests/contexttypeinfo/Makefile libcontextsubscriber/unit-tests/contexttyperegistryinfo/Makefile + libcontextsubscriber/unit-tests/duration/Makefile libcontextsubscriber/update-contextkit-providers/Makefile libcontextsubscriber/propertymonitor/Makefile libcontextprovider/contextprovider-1.0.pc diff --git a/debian/changelog b/debian/changelog index 4e5b8bc1..6797d3f6 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,11 +1,17 @@ -contextkit (0.5.3~unreleased) unstable; urgency=low +contextkit (0.5.4~unreleased) unstable; urgency=low + + * + + -- Jean-Luc Lamadon <jean-luc.lamadon@nokia.com> Wed, 09 Dec 2009 11:49:06 +0200 + +contextkit (0.5.3) unstable; urgency=low * deprecated the old D-Bus protocol on the subscriber side, now it is only tried for talking to the commander, not for regular providers. Implemented: Swp#CntFr-386 - -- Gergely Risko <gergely+context@risko.hu> Fri, 27 Nov 2009 12:04:30 +0200 + -- Jean-Luc Lamadon <jean-luc.lamadon@nokia.com> Wed, 09 Dec 2009 11:45:29 +0200 contextkit (0.5.2) unstable; urgency=low diff --git a/debian/control b/debian/control index 796c45ad..d66f1b91 100644 --- a/debian/control +++ b/debian/control @@ -129,6 +129,7 @@ Description: Helper library for subscribers of context properties Package: libcontextsubscriber-dev Section: libdevel +Suggests: libqjson-dev Architecture: any Depends: libcontextsubscriber0 (= ${binary:Version}), libqt4-dev Description: Helper library for subscribers of context properties diff --git a/libcontextsubscriber/src/Makefile.am b/libcontextsubscriber/src/Makefile.am index d686265e..1c21a087 100644 --- a/libcontextsubscriber/src/Makefile.am +++ b/libcontextsubscriber/src/Makefile.am @@ -14,14 +14,15 @@ libcontextsubscriber_la_SOURCES = contextproperty.cpp \ iproviderplugin.h contextproviderinfo.h nanoxml.h nanoxml.cpp \ asyncdbusinterface.cpp timedvalue.h contexttypeinfo.cpp \ contexttypeinfo.h contexttyperegistryinfo.cpp \ - contexttyperegistryinfo.h assoctree.h assoctree.cpp + contexttyperegistryinfo.h assoctree.h assoctree.cpp duration.h \ + duration.cpp includecontextsubscriberdir=$(includedir)/contextsubscriber includecontextsubscriber_HEADERS = contextproperty.h \ contextpropertyinfo.h contextregistryinfo.h iproviderplugin.h \ contextproviderinfo.h asyncdbusinterface.h timedvalue.h \ contexttypeinfo.h contexttypeinfo.cpp \ - contexttyperegistryinfo.h assoctree.h + contexttyperegistryinfo.h assoctree.h duration.h contextjson.h AM_CXXFLAGS = -I$(top_srcdir)/common \ $(QtCore_CFLAGS) $(QtXml_CFLAGS) $(QtDBus_CFLAGS) \ diff --git a/libcontextsubscriber/src/contextjson.h b/libcontextsubscriber/src/contextjson.h new file mode 100644 index 00000000..ec6e5077 --- /dev/null +++ b/libcontextsubscriber/src/contextjson.h @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2008, 2009 Nokia Corporation. + * + * Contact: Marius Vollmer <marius.vollmer@nokia.com> + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * version 2.1 as published by the Free Software Foundation. + * + * This program 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 program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA + * + */ + +#ifndef CONTEXTJSON_H +#define CONTEXTJSON_H + +#include <QString> +#include <QVariant> +#include <qjson/serializer.h> +#include <qjson/parser.h> + +// stolen from glib +#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4) +#define CKIT_GNUC_UNUSED __attribute__((__unused__)) +#else +#define CKIT_GNUC_UNUSED +#endif + +namespace ContextSubscriber { + +// Please note that it is intentional that this header file contains +// implementation. This way libcontextsubscriber doesn't have to link +// against libqjson (this would be one more extra dependency), but +// only relatively rare users of this feature have to. + +/// Dumps a QVariant into a QString in JSON format. This is very +/// useful if the value is quite complex (list of maps of lists) and +/// you would like to see it for debugging reasons. +/// +/// If you use this function, you have to add libqjson to the list of +/// libraries you link against. +CKIT_GNUC_UNUSED +static QString qVariantToJSON(const QVariant &v) { + QJson::Serializer serializer; + return serializer.serialize(v); +} + +/// Loads json data into a QVariant. On parse error it returns QVariant(). +CKIT_GNUC_UNUSED +static QVariant jsonToQVariant(const QString &s) { + QJson::Parser parser; + bool ok; + QVariant v = parser.parse(s.toUtf8(), &ok); + if (ok) + return QVariant(v); + else + return QVariant(); +} + +} // namespace + +#endif diff --git a/libcontextsubscriber/src/contexttypeinfo.cpp b/libcontextsubscriber/src/contexttypeinfo.cpp index e3eac1fa..df72e22d 100644 --- a/libcontextsubscriber/src/contexttypeinfo.cpp +++ b/libcontextsubscriber/src/contexttypeinfo.cpp @@ -59,6 +59,18 @@ i.e., you can use it when the list of parameters is empty. */ +ContextTypeInfo::ContextTypeInfo(const AssocTree &tree) : AssocTree (tree) +{ +} + +ContextTypeInfo::ContextTypeInfo(const QVariant &tree) : AssocTree (tree) +{ +} + +ContextTypeInfo::ContextTypeInfo() +{ +} + /// Returns a ContexTypeInfo where old-style type names (INTEGER, /// STRING, BOOL) are replaced with new-style type names (integer, /// string, bool). Returns self if the ContextTypeInfo is already @@ -232,6 +244,64 @@ bool ContextTypeInfo::typeCheck(const QVariant &value) const return true; } +bool ContextTypeInfo::hasBase(QString wanted, int depth) const +{ + if (name() == wanted) + return true; + + if (base() == AssocTree()) + return false; + + if (depth == 0) + return false; + + return base().hasBase(wanted, depth-1); +} + +ContextTypeInfo ContextTypeInfo::getBase(QString wanted, int depth) const +{ + if (name() == wanted) + return *this; + + if (base() == AssocTree()) + return ContextTypeInfo(); + + if (depth == 0) + return ContextTypeInfo(); + + return base().getBase(wanted, depth-1); +} + +QVariantList ContextTypeInfo::parameters() const +{ + return nodes(); +} + +QVariant ContextTypeInfo::parameterValue(QString p) const +{ + return value(p); +} + +AssocTree ContextTypeInfo::parameterNode(QString p) const +{ + return node(p); +} + +QString ContextTypeInfo::parameterDoc(QString p) const +{ + return definition().value("params", p, "doc").toString(); +} + +QString ContextTypeInfo::doc() const +{ + return definition().value("doc").toString(); +} + +ContextTypeInfo ContextTypeInfo::base() const +{ + return ContextTypeInfo(definition().value("base")); +} + /// Returns the AssocTree with the type definition for this type. AssocTree ContextTypeInfo::definition() const { @@ -241,6 +311,15 @@ AssocTree ContextTypeInfo::definition() const /* ContextStringEnumInfo */ +ContextStringEnumInfo::ContextStringEnumInfo(const ContextTypeInfo &info) : ContextTypeInfo (info) +{ +} + +QString ContextStringEnumInfo::choiceDoc(const QString &choice) const +{ + return value (choice, "doc").toString(); +} + QStringList ContextStringEnumInfo::choices() const { QStringList result; @@ -249,3 +328,12 @@ QStringList ContextStringEnumInfo::choices() const } return result; } + +ContextListInfo::ContextListInfo(const ContextTypeInfo &info) : ContextTypeInfo (info) +{ +} + +ContextTypeInfo ContextListInfo::type() +{ + return ContextTypeInfo(parameterValue("type")); +} diff --git a/libcontextsubscriber/src/contexttypeinfo.h b/libcontextsubscriber/src/contexttypeinfo.h index 79c8eb33..1dba18b7 100644 --- a/libcontextsubscriber/src/contexttypeinfo.h +++ b/libcontextsubscriber/src/contexttypeinfo.h @@ -30,39 +30,40 @@ class ContextTypeInfo : public AssocTree { public: - ContextTypeInfo (const AssocTree &tree) : AssocTree (tree) { } - ContextTypeInfo (const QVariant &tree) : AssocTree (tree) { } - ContextTypeInfo () { } + ContextTypeInfo(const AssocTree &tree); + ContextTypeInfo(const QVariant &tree); + ContextTypeInfo(); ContextTypeInfo ensureNewTypes(); bool typeCheck(const QVariant &value) const; - QString name() const { return AssocTree::name(); } - QVariantList parameters() const { return nodes(); } - QVariant parameterValue(QString p) const { return value(p); } - AssocTree parameterNode(QString p) const { return node(p); } + bool hasBase(QString wanted, int depth = 10) const; + ContextTypeInfo getBase(QString wanted, int depth = 10) const; + QVariantList parameters() const; + QVariant parameterValue(QString p) const; + AssocTree parameterNode(QString p) const; AssocTree definition() const; - QString parameterDoc(QString p) const { return definition().value("params", p, "doc").toString(); } - QString doc() const { return definition().value("doc").toString(); } - ContextTypeInfo base() const { return ContextTypeInfo(definition().value("base")); } + QString parameterDoc(QString p) const; + QString doc() const; + ContextTypeInfo base() const; }; class ContextStringEnumInfo : public ContextTypeInfo { public: - ContextStringEnumInfo (const ContextTypeInfo &info) : ContextTypeInfo (info) { } + ContextStringEnumInfo(const ContextTypeInfo &info); - QStringList choices () const; - QString choiceDoc (const QString &choice) const { return value (choice, "doc").toString(); } + QStringList choices() const; + QString choiceDoc(const QString &choice) const; }; class ContextListInfo : public ContextTypeInfo { public: - ContextListInfo (const ContextTypeInfo &info) : ContextTypeInfo (info) { } + ContextListInfo(const ContextTypeInfo &info); - ContextTypeInfo type() { return ContextTypeInfo(parameterValue("type")); } + ContextTypeInfo type(); }; #endif diff --git a/libcontextsubscriber/src/duration.cpp b/libcontextsubscriber/src/duration.cpp new file mode 100644 index 00000000..0cd618e2 --- /dev/null +++ b/libcontextsubscriber/src/duration.cpp @@ -0,0 +1,204 @@ +/* + * Copyright (C) 2008,2009 Nokia Corporation. + * + * Contact: Marius Vollmer <marius.vollmer@nokia.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * version 2.1 as published by the Free Software Foundation. + * + * 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 St, Fifth Floor, Boston, MA + * 02110-1301 USA + * + */ + +#include <QRegExp> +#include <QTime> +#include <QDate> +#include <QStringList> + +#include "duration.h" + +/*! + \class Duration + + \brief The Duration class represents a period of elapsed time between two events. + + A Duration object is created either by giving the number of nanoseconds explicitly or a string representation of a Duration. + + The Duration object assumes the following definitions: + + A year = 52 weeks and 1 day OR 365 days + + A week = 7 days + + A day = 24 hours + + A hour = 60 minutes + + A minute = 60 seconds + + A second = 10e9 nanoseconds + +*/ + +///Convenient time conversions +const qint64 Duration::NANOSECS_PER_MSEC; +const qint64 Duration::NANOSECS_PER_SEC; +const qint64 Duration::NANOSECS_PER_MIN; +const qint64 Duration::NANOSECS_PER_HOUR; +const qint64 Duration::NANOSECS_PER_DAY; +const qint64 Duration::NANOSECS_PER_WEEK; +const qint64 Duration::MSECS_PER_DAY; +const qint64 Duration::SECS_PER_DAY; +const qint64 Duration::DAYS_PER_YEAR; +const qint64 Duration::DAYS_PER_WEEK; + +/// Constructs a Duration with an initial value of 0 +Duration::Duration() +{ + totalNanoSecs_p = 0; +} + +/// Constructs a Duration with the given number of nanoseconds +Duration::Duration(quint64 nanoSecs) : totalNanoSecs_p(nanoSecs) +{ + QTime reftime = QTime().addMSecs((nanoSecs/NANOSECS_PER_MSEC)% MSECS_PER_DAY); + quint64 days = nanoSecs/NANOSECS_PER_DAY; + + nanoSecs_p = totalNanoSecs_p%(NANOSECS_PER_SEC); + seconds_p = reftime.second(); + minutes_p = reftime.minute(); + hours_p = reftime.hour(); + days_p = (days%DAYS_PER_YEAR)%DAYS_PER_WEEK; + weeks_p = (days%DAYS_PER_YEAR)/DAYS_PER_WEEK; + years_p = days/DAYS_PER_YEAR; +} + +/// Constructs a copy of the other Duration. +Duration::Duration(const QString &duration) +{ + QRegExp re("(?:(\\d+)Y)?\\s*(?:(\\d+)W)?\\s*(?:(\\d+)D)?\\s*(?:(\\d+)H)?\\s*(?:(\\d+)M)?\\s*(?:(\\d+)S)?"); + re.setCaseSensitivity(Qt::CaseInsensitive); + + if (re.exactMatch(duration.simplified()) && re.matchedLength()){ + QStringList list; + list = re.capturedTexts(); + list.removeFirst(); + + qint64 nanosec_year = list.at(0).toLongLong()*(52*NANOSECS_PER_WEEK+NANOSECS_PER_DAY); + qint64 nanosec_week = list.at(1).toLongLong()*NANOSECS_PER_WEEK; + qint64 nanosec_day = list.at(2).toLongLong()*NANOSECS_PER_DAY; + qint64 nanosec_hour = list.at(3).toLongLong()*NANOSECS_PER_HOUR; + qint64 nanosec_min = list.at(4).toLongLong()*NANOSECS_PER_MIN; + qint64 nanosec_sec = list.at(5).toLongLong()*NANOSECS_PER_SEC; + *this = Duration(nanosec_year+nanosec_week+nanosec_day+nanosec_hour+nanosec_min+nanosec_sec); + } else { + totalNanoSecs_p = 0; + } +} +/// Returns true if this Duration is equal to the other Duration +bool Duration::operator==(const Duration &other) const { + return (totalNanoSecs_p == other.totalNanoSecs_p); +} + +/// Returns the number of nanoseconds in the duration +int Duration::nanoSecs() const +{ + return totalNanoSecs_p%(NANOSECS_PER_SEC); +} + +/// Returns the number of seconds in the duration +int Duration::seconds() const +{ + return seconds_p; +} + +/// Returns the number of minutes in the duration +int Duration::minutes() const +{ + return minutes_p; +} + +/// Returns the number of hours in the duration +int Duration::hours() const +{ + return hours_p; +} + +/// Returns the number of days in the duration +int Duration::days() const +{ + return days_p; +} + +/// Returns the number of weeks in the duration +int Duration::weeks() const +{ + return weeks_p; +} + +/// Returns the number of years in the duration +int Duration::years() const +{ + return years_p; +} +/// Returns a string representation of the duration +/// using the following format: Y W D H M S +QString Duration::toString() const +{ + QString retval; + if(years_p) { + retval.append(QString::number(years_p)); + retval.append(QString("Y ")); + } + + if(weeks_p) { + retval.append(QString::number(weeks_p)); + retval.append(QString("W ")); + } + + if(days_p) { + retval.append(QString::number(days_p)); + retval.append(QString("D ")); + } + + if(hours_p) { + retval.append(QString::number(hours_p)); + retval.append(QString("H ")); + } + + if(minutes_p) { + retval.append(QString::number(minutes_p)); + retval.append(QString("M ")); + } + + if(seconds_p) { + retval.append(QString::number(seconds_p)); + retval.append(QString("S")); + } else if (!years_p && !weeks_p && !days_p && !hours_p && !minutes_p && !seconds_p) { + retval.append(QString("0S")); + } + return retval.simplified(); +} + +/// Returns the Duration expressed in nanoseconds +quint64 Duration::toNanoSeconds() const +{ + return totalNanoSecs_p; +} + +/// Returns true if the string format used to represent the Duration is valid i.e. Y W D H M S , each being optional +bool Duration::isDuration(const QString &duration) +{ + QRegExp re("(?:(\\d+)Y)?\\s*(?:(\\d+)W)?\\s*(?:(\\d+)D)?\\s*(?:(\\d+)H)?\\s*(?:(\\d+)M)?\\s*(?:(\\d+)S)?"); + re.setCaseSensitivity(Qt::CaseInsensitive); + return re.exactMatch(duration.simplified()) && re.matchedLength(); +} diff --git a/libcontextsubscriber/src/duration.h b/libcontextsubscriber/src/duration.h new file mode 100644 index 00000000..4892de7f --- /dev/null +++ b/libcontextsubscriber/src/duration.h @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2008,2009 Nokia Corporation. + * + * Contact: Marius Vollmer <marius.vollmer@nokia.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * version 2.1 as published by the Free Software Foundation. + * + * 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 St, Fifth Floor, Boston, MA + * 02110-1301 USA + * + */ + +#ifndef DURATION_H +#define DURATION_H + +#include <QString> +#include <QtCore/QMetaType> + +class Duration +{ + +public: + Duration(); + Duration(quint64 nanoSecs); + Duration(const QString &duration); + + int nanoSecs() const; + int seconds() const; + int minutes() const; + int hours() const; + int days() const; + int weeks() const; + int years() const; + QString toString() const; + quint64 toNanoSeconds() const; + bool operator==(const Duration &other) const; + + static bool isDuration(const QString &duration); + static const qint64 NANOSECS_PER_MSEC = Q_INT64_C(1000000); + static const qint64 NANOSECS_PER_SEC = 1000*NANOSECS_PER_MSEC; + static const qint64 NANOSECS_PER_MIN = 60*NANOSECS_PER_SEC; + static const qint64 NANOSECS_PER_HOUR = 60*NANOSECS_PER_MIN; + static const qint64 NANOSECS_PER_DAY = 24*NANOSECS_PER_HOUR; + static const qint64 NANOSECS_PER_WEEK = 7*NANOSECS_PER_DAY; + static const qint64 SECS_PER_DAY = Q_INT64_C(86400); + static const qint64 MSECS_PER_DAY = 1000*SECS_PER_DAY; + static const qint64 DAYS_PER_YEAR = 365; + static const qint64 DAYS_PER_WEEK = 7; + +private: + quint64 totalNanoSecs_p;///the duration expressed in nanoseconds + int nanoSecs_p;///the number of nanoseconds in the duration + int seconds_p;///the number of seconds in the duration + int minutes_p;///the number of minutes in the duration + int hours_p;///the number of hours in the duration + int days_p;///the number of days in the duration + int weeks_p;///the number of weeks in the duration + int years_p;///the number of years in the duration + +}; +Q_DECLARE_METATYPE(Duration) +#endif diff --git a/libcontextsubscriber/unit-tests/Makefile.am b/libcontextsubscriber/unit-tests/Makefile.am index ca5dca89..6458cead 100644 --- a/libcontextsubscriber/unit-tests/Makefile.am +++ b/libcontextsubscriber/unit-tests/Makefile.am @@ -11,6 +11,7 @@ SUBDIRSTESTS = \ nanoxml \ assoctree \ contexttypeinfo \ + duration \ contexttyperegistryinfo SUBDIRS = $(SUBDIRSTESTS) util diff --git a/libcontextsubscriber/unit-tests/duration/.gitignore b/libcontextsubscriber/unit-tests/duration/.gitignore new file mode 100644 index 00000000..00733055 --- /dev/null +++ b/libcontextsubscriber/unit-tests/duration/.gitignore @@ -0,0 +1,3 @@ +duration.cpp +duration.h +testduration
\ No newline at end of file diff --git a/libcontextsubscriber/unit-tests/duration/Makefile.am b/libcontextsubscriber/unit-tests/duration/Makefile.am new file mode 100644 index 00000000..4969f03a --- /dev/null +++ b/libcontextsubscriber/unit-tests/duration/Makefile.am @@ -0,0 +1,21 @@ +check_PROGRAMS = testduration + +# your test's sources +testduration_SOURCES = testduration.cpp + +# only include these files in the coverage +COVERAGE_FILES = duration.cpp + +# do the testing, coverage, etc. stuff +# tests.am is using +=, so we have to set a value here for these four always +AM_CXXFLAGS = $(QtCore_CFLAGS) +AM_LDFLAGS = $(QtCore_LIBS) +FROM_SOURCE = duration.h duration.cpp +FROM_SOURCE_DIR = $(srcdir)/../../src +LDADD = +include $(top_srcdir)/am/tests.am + +nodist_testduration_SOURCES = mocs.cpp +QT_TOMOC = $(filter %.h, $(testduration_SOURCES) $(FROM_SOURCE)) +include $(top_srcdir)/am/qt.am +testduration.o: testduration.moc diff --git a/libcontextsubscriber/unit-tests/duration/testduration.cpp b/libcontextsubscriber/unit-tests/duration/testduration.cpp new file mode 100644 index 00000000..9f065dd3 --- /dev/null +++ b/libcontextsubscriber/unit-tests/duration/testduration.cpp @@ -0,0 +1,189 @@ +/* + * Copyright (C) 2008,2009 Nokia Corporation. + * + * Contact: Marius Vollmer <marius.vollmer@nokia.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * version 2.1 as published by the Free Software Foundation. + * + * 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 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA + * + */ +#include <QObject> +#include <QtTest/QtTest> + +#include "duration.h" // Class to be tested + +class DurationUnitTest : public QObject +{ + Q_OBJECT + +private slots: + void init(); + + // Tests + void toStringTest_data(); + void toStringTest(); + void accessorsTest(); + void fromStringTest_data(); + void fromStringTest(); + void toNanoSecsTest_data(); + void toNanoSecsTest(); + void isDurationTest_data(); + void isDurationTest(); +}; + +void DurationUnitTest::init() +{ +} + +void DurationUnitTest::toStringTest_data() +{ + QTest::addColumn<quint64>("string"); + QTest::addColumn<QString>("result"); + QTest::newRow("fulldate") << Q_UINT64_C(97959392000000000) + << QString("3Y 5W 3D 18H 56M 32S"); + QTest::newRow("oneyear") << Q_UINT64_C(31536000000000000) + << QString("1Y"); + QTest::newRow("allbutyear") << Q_UINT64_C(31532459000000000) + << QString("52W 23H 59S"); + QTest::newRow("dhms") << Q_UINT64_C(601259000000000) + << QString("6D 23H 59S"); + QTest::newRow("hms") << Q_UINT64_C(14410000000000) + << QString("4H 10S"); + QTest::newRow("ms") << Q_UINT64_C(3565000000000) + << QString("59M 25S"); + QTest::newRow("onlysec") << Q_UINT64_C(25000000000) + << QString("25S"); + QTest::newRow("conversiondefinition") << Q_UINT64_C(63072000000000000) + << QString("2Y"); + QTest::newRow("conversiondefinition1") << Q_UINT64_C(31622400000000000) + << QString("1Y 1D"); + QTest::newRow("conversiondefinition2") << Q_UINT64_C(31536001000000000) + << QString("1Y 1S"); +} + +void DurationUnitTest::toStringTest() +{ + QFETCH(quint64, string); + QFETCH(QString, result); + Duration duration (string); + QCOMPARE(duration.toString(), result); +} + +void DurationUnitTest::accessorsTest() +{ + Duration duration (Q_UINT64_C(97959392000000000)); + QCOMPARE(duration.seconds(), 32); + QCOMPARE(duration.minutes(), 56); + QCOMPARE(duration.hours(), 18); + QCOMPARE(duration.days(), 3); + QCOMPARE(duration.weeks(), 5); + QCOMPARE(duration.years(), 3); +} + +void DurationUnitTest::fromStringTest_data() +{ + QTest::addColumn<QString>("string"); + QTest::addColumn<quint64>("result"); + QTest::newRow("fulldate") << QString("3Y 5W 3D 18H 56M 32S") + << Q_UINT64_C(97959392000000000); + QTest::newRow("oneyear") << QString("1Y 0W 0D 0H 0M 0S") + << Q_UINT64_C(31536000000000000); + QTest::newRow("allbutyear") << QString("52W 23H 59S") + << Q_UINT64_C(31532459000000000); + QTest::newRow("dhms") << QString("6D 23H 59S") + << Q_UINT64_C(601259000000000); + QTest::newRow("hms") << QString("4H 10S") + << Q_UINT64_C(14410000000000); + QTest::newRow("ms") << QString("59M 25S") + << Q_UINT64_C(3565000000000); + QTest::newRow("onlysec") << QString("25S") + << Q_UINT64_C(25000000000); + QTest::newRow("conversiondefinition") << QString("730D") + << Q_UINT64_C(63072000000000000); + QTest::newRow("conversiondefinition2") << QString("366D") + << Q_UINT64_C(31622400000000000); + QTest::newRow("conversiondefinition3") << QString("1Y 1D") + << Q_UINT64_C(31622400000000000); + QTest::newRow("conversiondefinition4") << QString("1Y 1S") + << Q_UINT64_C(31536001000000000); + QTest::newRow("conversiondefinition5") << QString("52W 1D ") + << Q_UINT64_C(31536000000000000); + QTest::newRow("emptystring") << QString("") + << Q_UINT64_C(0); +} + +void DurationUnitTest::fromStringTest() +{ + QFETCH(QString, string); + QFETCH(quint64, result); + QCOMPARE((Duration(string)).toNanoSeconds(), result); +} + +void DurationUnitTest::toNanoSecsTest_data() +{ + QTest::addColumn<quint64>("string"); + QTest::addColumn<quint64>("result"); + QTest::newRow("fulldate") << Q_UINT64_C(97959392000000000) + << Q_UINT64_C(97959392000000000); + QTest::newRow("oneyear") << Q_UINT64_C(31536000000000000) + << Q_UINT64_C(31536000000000000); + QTest::newRow("allbutyear") << Q_UINT64_C(31532459000000000) + << Q_UINT64_C(31532459000000000); + QTest::newRow("dhms") << Q_UINT64_C(601259000000000) + << Q_UINT64_C(601259000000000); + QTest::newRow("hms") << Q_UINT64_C(14410000000000) + << Q_UINT64_C(14410000000000); + QTest::newRow("ms") << Q_UINT64_C(3565000000000) + << Q_UINT64_C(3565000000000); + QTest::newRow("onlysec") << Q_UINT64_C(25000000000) + << Q_UINT64_C(25000000000); + QTest::newRow("conversiondefinition") << Q_UINT64_C(63072000000000000) + << Q_UINT64_C(63072000000000000); + QTest::newRow("conversiondefinition1") << Q_UINT64_C(31622400000000000) + << Q_UINT64_C(31622400000000000); + QTest::newRow("conversiondefinition2") << Q_UINT64_C(31536001000000000) + << Q_UINT64_C(31536001000000000); +} + +void DurationUnitTest::toNanoSecsTest() +{ + QFETCH(quint64, string); + QFETCH(quint64, result); + Duration duration (string); + QCOMPARE(duration.toNanoSeconds(), result); +} + +void DurationUnitTest::isDurationTest_data() +{ + QTest::addColumn<QString>("string"); + QTest::addColumn<bool>("result"); + QTest::newRow("fulldate") << QString("3Y 5W 3D 18H 56M 32S") + << true; + QTest::newRow("emptystring") << QString("") + << false; + QTest::newRow("space") << QString(" ") + << false; + QTest::newRow("invalid") << QString("foobar") + << false; +} + +void DurationUnitTest::isDurationTest() +{ + QFETCH(QString, string); + QFETCH(bool, result); + QCOMPARE(Duration::isDuration(string), result); +} + + +QTEST_MAIN(DurationUnitTest); +#include "testduration.moc" |