From 68b14617ccd3aa89dff67488443600dc11b537be Mon Sep 17 00:00:00 2001 From: Christian Hohnstaedt Date: Mon, 1 Apr 2024 11:39:18 +0200 Subject: [PATCH] Add tests for the DateTime input for validity times. Test the correct behavior of the "Midnight" and setDiff methods. Especially to keep the time when switching from hidden time (midnoght) back to shown time. --- test/CMakeLists.txt | 2 +- test/main.h | 1 + test/validity.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++ widgets/validity.cpp | 10 +++++--- widgets/validity.h | 6 ++--- 5 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 test/validity.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 5ad2132b..99824d9a 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,6 +1,6 @@ list(APPEND srcs PwDialogMock.h importPEM.cpp main.cpp main.h - newKey.cpp pem.cpp export.cpp renewal.cpp) + newKey.cpp pem.cpp export.cpp renewal.cpp validity.cpp) list(TRANSFORM srcs PREPEND ${PROJECT_SOURCE_DIR}/test/) diff --git a/test/main.h b/test/main.h index 0afab213..7165afa0 100644 --- a/test/main.h +++ b/test/main.h @@ -35,6 +35,7 @@ class test_main: public QObject void importPEM(); void exportFormat(); void revoke(); + void testValidity(); public: template static T *findWindow(const QString &name) diff --git a/test/validity.cpp b/test/validity.cpp new file mode 100644 index 00000000..bf63913a --- /dev/null +++ b/test/validity.cpp @@ -0,0 +1,55 @@ +/* vi: set sw=4 ts=4: + * + * Copyright (C) 2023 Christian Hohnstaedt. + * + * All rights reserved. + */ + +#include + +#include "widgets/MainWindow.h" +#include "widgets/validity.h" + +#include "main.h" + +void test_main::testValidity() +{ + try { + + Validity *start = new Validity(mainwin); + Validity *end = new Validity(mainwin); + + QCOMPARE(start->displayFormat(), "yyyy-MM-dd hh:mm 'GMT'"); + QCOMPARE(end->displayFormat(), "yyyy-MM-dd hh:mm 'GMT'"); + end->setEndDate(true); + start->hideTime(true); + end->hideTime(true); + QCOMPARE(start->displayFormat(), "yyyy-MM-dd 00:00 'GMT'"); + QCOMPARE(end->displayFormat(), "yyyy-MM-dd 23:59 'GMT'"); + + start->setDate(a1time("20130921094317Z")); + end->setDiff(start, 7, 0); + QCOMPARE(start->getDate().toPlain(), "20130921000000Z"); + QCOMPARE(end->getDate().toPlain(), "20130927235959Z"); + start->hideTime(false); + end->hideTime(false); + QCOMPARE(start->getDate().toPlain(), "20130921094300Z"); + QCOMPARE(end->getDate().toPlain(), "20130928094300Z"); + start->hideTime(false); + QCOMPARE(start->getDate().toPlain(), "20130921094300Z"); + start->hideTime(true); + end->hideTime(true); + QCOMPARE(start->getDate().toPlain(), "20130921000000Z"); + QCOMPARE(end->getDate().toPlain(), "20130927235959Z"); + + end->setDiff(start, 2, 1); + QCOMPARE(end->getDate().toPlain(), "20131120235959Z"); + end->hideTime(true); + QCOMPARE(end->getDate().toPlain(), "20131120235959Z"); + end->hideTime(false); + QCOMPARE(end->getDate().toPlain(), "20131121094300Z"); + + } catch (...) { + QVERIFY2(false, "Exception thrown"); + } +} diff --git a/widgets/validity.cpp b/widgets/validity.cpp index db1f9882..b5ae45d1 100644 --- a/widgets/validity.cpp +++ b/widgets/validity.cpp @@ -57,7 +57,6 @@ void Validity::localTime(int state) break; } updateFormatString(); - setMyTime(time()); } void Validity::hideTimeCheck(int state) @@ -112,6 +111,7 @@ void Validity::updateFormatString() void Validity::setDate(const a1time &a) { setDateTime(a); + setMyTime(a.time()); } void Validity::setDiff(const Validity *start, int number, int range) @@ -134,8 +134,7 @@ void Validity::setDiff(const Validity *start, int number, int range) void Validity::setNow() { - setDateTime(a1time::now()); - setMyTime(time()); + setDate(a1time()); } void Validity::setMyTime(const QTime &time) @@ -143,3 +142,8 @@ void Validity::setMyTime(const QTime &time) mytime = time; } +void Validity::setEndDate(bool ed) +{ + endDate = ed; + hideTime(midnight); +} diff --git a/widgets/validity.h b/widgets/validity.h index cbfcb1fc..7f67d914 100644 --- a/widgets/validity.h +++ b/widgets/validity.h @@ -28,10 +28,8 @@ class Validity : public QDateTimeEdit void setDate(const a1time &a); void setDiff(const Validity *start, int number, int range); void hideTime(bool hide); - void setEndDate(bool ed) - { - endDate = ed; - } + void setEndDate(bool ed); + protected slots: void setMyTime(const QTime & time);