File indexing completed on 2024-04-21 04:57:09

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2009 Dario Massarin <nekkar@libero.it>
0004    Copyright (C) 2010 Matthias Fuchs <mat69@gmx.net>
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public
0008    License as published by the Free Software Foundation; either
0009    version 2 of the License, or (at your option) any later version.
0010 */
0011 
0012 #ifndef _TESTTRANSFERS_H
0013 #define _TESTTRANSFERS_H
0014 
0015 #include <QDBusVariant>
0016 #include <QDomElement>
0017 #include <QTemporaryDir>
0018 #include <QtTest>
0019 class OrgKdeKgetTransferInterface;
0020 class OrgKdeKgetVerifierInterface;
0021 class TestTransfers;
0022 
0023 class Commands : public QObject
0024 {
0025     Q_OBJECT
0026 
0027 public:
0028     Commands(const QString &source, QObject *parent);
0029 
0030     QString source() const;
0031     bool hasCommands() const;
0032     void setCommands(const QList<QPair<int, QVariant>> &commands);
0033     void executeCommands();
0034     void associateTransfer(OrgKdeKgetTransferInterface *transfer);
0035 
0036     static QList<QPair<int, QVariant>> parseCommands(const QDomElement &e, TestTransfers *transfer);
0037 
0038     enum Action {
0039         Start,
0040         Stop,
0041         AddChecksum, // QStringList() << type << hash
0042         AddPartialChecksums, // QList<QVariant>() << QString typ << qulonglong length << QStringList checksums
0043         IsVerifyable, // bool if verifyable
0044         Verify,
0045         FindBrokenPieces,
0046         Repair, // bool if it works
0047         SetDirectory,
0048         Wait, // int time; waits until time is over and then proceeds to executeCommands
0049         RandomAction, // QList<QVariant> << bool false OR QList<QVariant> << bool true << int timeBetweenActions; chooses automatically periodically Start or
0050                       // Stop
0051         CustomEvent = 100
0052     };
0053 
0054     /**
0055      * Event is a list of Events, events are blocking, so every command after an event
0056      * is blocked, unless the event happens, that way one can start a Repair when Verified returns false
0057      */
0058     enum Event {
0059         Verified = CustomEvent + 1, // bool verified
0060         ChangedEvent =
0061             CustomEvent + 2, // QList<Variant>() << int TransferChange << int triggerValue = optional; when the event and the triggervalue match then executing
0062                              // continues; e.g. QList<Variant>() << Transfer::Tc_Percent << 50; //goes on when more than 50% have been downloaded
0063         BrokenPieces = CustomEvent + 3
0064     };
0065 
0066 protected:
0067     void timerEvent(QTimerEvent *event) override;
0068 
0069 private Q_SLOTS:
0070     void slotVerified(bool verified);
0071     void slotBrokenPieces(const QStringList &offsets, qulonglong length);
0072     void slotChangedEvent(int event);
0073     void slotWaitEvent();
0074 
0075 private:
0076     int m_timerId;
0077     const QString m_source;
0078     QList<QPair<int, QVariant>> m_commands;
0079     OrgKdeKgetTransferInterface *m_transfer;
0080     OrgKdeKgetVerifierInterface *m_verifier;
0081 
0082     static QHash<QString, int> s_stringCommands;
0083     static QHash<QString, int> s_transferChangeEvents;
0084 };
0085 
0086 class TestTransfers : public QObject
0087 {
0088     Q_OBJECT
0089 
0090 public:
0091     TestTransfers();
0092 
0093     QString tempDir() const;
0094 
0095 public Q_SLOTS:
0096     void createTransfer();
0097 
0098 private Q_SLOTS:
0099     void simpleTest();
0100 
0101 private:
0102     void parseFile();
0103 
0104 private:
0105     QList<OrgKdeKgetTransferInterface *> m_transferIfaces;
0106     QList<Commands *> m_commands;
0107     QScopedPointer<QTemporaryDir> m_dir;
0108 };
0109 
0110 #endif