1 /***************************************************************************
2 Copyright (C) 2011 Robby Stephenson <robby@periapsis.org>
3 ***************************************************************************/
4
5 /***************************************************************************
6 * *
7 * This program is free software; you can redistribute it and/or *
8 * modify it under the terms of the GNU General Public License as *
9 * published by the Free Software Foundation; either version 2 of *
10 * the License or (at your option) version 3 or any later version *
11 * accepted by the membership of KDE e.V. (or its successor approved *
12 * by the membership of KDE e.V.), which shall act as a proxy *
13 * defined in Section 14 of version 3 of the license. *
14 * *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
22 * *
23 ***************************************************************************/
24
25 #undef QT_NO_CAST_FROM_ASCII
26
27 #include "abstractfetchertest.h"
28
29 #include "../fetch/fetcherjob.h"
30
31 #include <QNetworkInterface>
32 #include <QStandardPaths>
33
AbstractFetcherTest()34 AbstractFetcherTest::AbstractFetcherTest() : QObject(), m_loop(this), m_hasNetwork(false) {
35 foreach(const QNetworkInterface& net, QNetworkInterface::allInterfaces()) {
36 if(net.flags().testFlag(QNetworkInterface::IsUp) && !net.flags().testFlag(QNetworkInterface::IsLoopBack)) {
37 m_hasNetwork = true;
38 break;
39 }
40 }
41 QStandardPaths::setTestModeEnabled(true);
42 }
43
doFetch(Tellico::Fetch::Fetcher::Ptr fetcher,const Tellico::Fetch::FetchRequest & request,int maxResults)44 Tellico::Data::EntryList AbstractFetcherTest::doFetch(Tellico::Fetch::Fetcher::Ptr fetcher,
45 const Tellico::Fetch::FetchRequest& request,
46 int maxResults) {
47 // don't use 'this' as job parent, it crashes
48 Tellico::Fetch::FetcherJob* job = new Tellico::Fetch::FetcherJob(nullptr, fetcher, request);
49 connect(job, &KJob::result, this, &AbstractFetcherTest::slotResult);
50
51 if(maxResults > 0) {
52 job->setMaximumResults(maxResults);
53 }
54
55 job->start();
56 m_loop.exec();
57 return m_results;
58 }
59
slotResult(KJob * job_)60 void AbstractFetcherTest::slotResult(KJob* job_) {
61 m_results = static_cast<Tellico::Fetch::FetcherJob*>(job_)->entries();
62 m_loop.quit();
63 }
64
set(Tellico::Data::EntryPtr entry_,const char * field_)65 QString AbstractFetcherTest::set(Tellico::Data::EntryPtr entry_, const char* field_) {
66 return set(entry_->field(QLatin1String(field_)));
67 }
68
set(const char * value_)69 QString AbstractFetcherTest::set(const char* value_) {
70 return set(QLatin1String(value_));
71 }
72
set(const QString & value_)73 QString AbstractFetcherTest::set(const QString& value_) {
74 QStringList values = Tellico::FieldFormat::splitValue(value_);
75 values.sort();
76 return values.join(QLatin1String("; "));
77 }
78