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

0001 /*
0002     SPDX-FileCopyrightText: 2018 Ralf Habacker ralf.habacker @freenet.de
0003     SPDX-FileCopyrightText: 2020 Thomas Baumgart <tbaumgart@kde.org>
0004 
0005     This file is part of libalkimia.
0006 
0007     SPDX-License-Identifier: LGPL-2.1-or-later
0008 */
0009 
0010 #include "alkonlinequotetest.h"
0011 
0012 #include "alkonlinequote.h"
0013 #include "alkonlinequotesprofile.h"
0014 #include "alkquotereceiver.h"
0015 #include "alkwebpage.h"
0016 
0017 #include <QtTest/QtTest>
0018 
0019 QTEST_MAIN(AlkOnlineQuoteTest)
0020 
0021 void checkSet(const AlkOnlineQuote::Errors& errors, QList<AlkOnlineQuote::Errors::Type> setFlags, QList<AlkOnlineQuote::Errors::Type> resetFlags)
0022 {
0023     for (const auto& flag : setFlags) {
0024         QCOMPARE(errors & flag, true);
0025     }
0026     for (const auto& flag : resetFlags) {
0027         QCOMPARE(errors & flag, false);
0028     }
0029 }
0030 
0031 void AlkOnlineQuoteTest::testAlkOnlineQuoteErrorCtor()
0032 {
0033     AlkOnlineQuote::Errors errors;
0034 
0035     QList<AlkOnlineQuote::Errors::Type> reset({AlkOnlineQuote::Errors::Data,
0036                                               AlkOnlineQuote::Errors::Date,
0037                                               AlkOnlineQuote::Errors::DateFormat,
0038                                               AlkOnlineQuote::Errors::Price,
0039                                               AlkOnlineQuote::Errors::Script,
0040                                               AlkOnlineQuote::Errors::Source,
0041                                               AlkOnlineQuote::Errors::Symbol,
0042                                               AlkOnlineQuote::Errors::Success,
0043                                               AlkOnlineQuote::Errors::URL});
0044 
0045     checkSet(errors, QList<AlkOnlineQuote::Errors::Type>(), reset);
0046 }
0047 
0048 void AlkOnlineQuoteTest::testAlkOnlineQuoteErrorCtorWithArg()
0049 {
0050     AlkOnlineQuote::Errors errors(AlkOnlineQuote::Errors::Price);
0051 
0052     QList<AlkOnlineQuote::Errors::Type> reset({AlkOnlineQuote::Errors::Data,
0053         AlkOnlineQuote::Errors::Date,
0054         AlkOnlineQuote::Errors::DateFormat,
0055         AlkOnlineQuote::Errors::Script,
0056         AlkOnlineQuote::Errors::Source,
0057         AlkOnlineQuote::Errors::Symbol,
0058         AlkOnlineQuote::Errors::Success,
0059         AlkOnlineQuote::Errors::URL});
0060 
0061     QList<AlkOnlineQuote::Errors::Type> set({AlkOnlineQuote::Errors::Price});
0062 
0063     checkSet(errors, set, reset);
0064 
0065     errors = AlkOnlineQuote::Errors::None;
0066 
0067     reset.append(set);
0068     set.clear();
0069 
0070     checkSet(errors, set, reset);
0071 }
0072 
0073 void AlkOnlineQuoteTest::testAlkOnlineQuoteErrorSetter()
0074 {
0075     AlkOnlineQuote::Errors errors;
0076 
0077     QList<AlkOnlineQuote::Errors::Type> reset({AlkOnlineQuote::Errors::Data,
0078                                               AlkOnlineQuote::Errors::Date,
0079                                               AlkOnlineQuote::Errors::DateFormat,
0080                                               AlkOnlineQuote::Errors::Price,
0081                                               AlkOnlineQuote::Errors::Script,
0082                                               AlkOnlineQuote::Errors::Source,
0083                                               AlkOnlineQuote::Errors::Symbol,
0084                                               AlkOnlineQuote::Errors::Success,
0085                                               AlkOnlineQuote::Errors::URL});
0086 
0087     QList<AlkOnlineQuote::Errors::Type> set;
0088 
0089     QList<AlkOnlineQuote::Errors::Type>::iterator it;
0090     for (it = reset.begin(); it != reset.end(); ) {
0091         const auto flag = *it;
0092         it = reset.erase(it);
0093         set.append(flag);
0094         errors |= flag;
0095         checkSet(errors, set, reset);
0096     }
0097 }
0098 
0099 void AlkOnlineQuoteTest::testAlkOnlineQuoteErrorCopy()
0100 {
0101     AlkOnlineQuote::Errors errors;
0102 
0103     QList<AlkOnlineQuote::Errors::Type> reset({AlkOnlineQuote::Errors::None,
0104                                               AlkOnlineQuote::Errors::Date,
0105                                               AlkOnlineQuote::Errors::Price,
0106                                               AlkOnlineQuote::Errors::Source,
0107                                               AlkOnlineQuote::Errors::Success});
0108 
0109     QList<AlkOnlineQuote::Errors::Type> set({AlkOnlineQuote::Errors::Data,
0110                                             AlkOnlineQuote::Errors::DateFormat,
0111                                             AlkOnlineQuote::Errors::Script,
0112                                             AlkOnlineQuote::Errors::Symbol,
0113                                             AlkOnlineQuote::Errors::URL});
0114 
0115     for (const auto& flag : set) {
0116         errors |= flag;
0117     }
0118     checkSet(errors, set, reset);
0119 
0120     const auto errorsCopy(errors);
0121     checkSet(errorsCopy, set, reset);
0122 }
0123 
0124 void AlkOnlineQuoteTest::init()
0125 {
0126     m_profile = new AlkOnlineQuotesProfile("alkimia");
0127 }
0128 
0129 void AlkOnlineQuoteTest::cleanup()
0130 {
0131     delete m_profile;
0132 }
0133 
0134 void AlkOnlineQuoteTest::testQuoteSources()
0135 {
0136     QStringList sources = m_profile->quoteSources();
0137     qDebug() << sources;
0138     QVERIFY(sources.size() > 0);
0139 }
0140 
0141 void AlkOnlineQuoteTest::testLaunch()
0142 {
0143     AlkOnlineQuote quote(m_profile);
0144     convertertest::AlkQuoteReceiver receiver(&quote);
0145     receiver.setVerbose(true);
0146 
0147     for (const QString &source : m_profile->quoteSources())
0148         if (!source.endsWith(QLatin1String(".webkit")))
0149             QVERIFY(quote.launch("EUR USD", "EUR USD", source));
0150 }
0151 
0152 void AlkOnlineQuoteTest::testLaunchWithBrowser()
0153 {
0154 #if defined(BUILD_WITH_WEBKIT) || defined(BUILD_WITH_WEBENGINE)
0155     AlkOnlineQuote quote(m_profile);
0156     convertertest::AlkQuoteReceiver receiver(&quote);
0157     receiver.setVerbose(true);
0158 
0159     for (const QString &source : m_profile->quoteSources())
0160         if (source.endsWith(QLatin1String(".webkit")))
0161             QVERIFY(quote.launch("EUR USD", "EUR USD", source));
0162 #else
0163     QSKIP("Browser based tests skipped because requirements are not met", SkipAll);
0164 #endif
0165 }