1 /* This file is part of Clementine.
2    Copyright 2010, David Sansome <me@davidsansome.com>
3 
4    Clementine is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8 
9    Clementine is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with Clementine.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #ifndef MOCK_NETWORKACCESSMANAGER_H
19 #define MOCK_NETWORKACCESSMANAGER_H
20 
21 #include <QByteArray>
22 #include <QMap>
23 #include <QNetworkAccessManager>
24 #include <QNetworkReply>
25 #include <QUrl>
26 
27 #include "test_utils.h"
28 #include "gmock/gmock.h"
29 
30 // Usage:
31 // Create a MockNetworkAccessManager.
32 // Call ExpectGet() with appropriate expectations and the data you want back.
33 // This will return a MockNetworkReply*. When you are ready for the reply to
34 // arrive, call MockNetworkReply::Done().
35 
36 class MockNetworkReply : public QNetworkReply {
37   Q_OBJECT
38  public:
39   MockNetworkReply();
40   MockNetworkReply(const QByteArray& data);
41 
42   // Use these to set expectations.
43   void SetData(const QByteArray& data);
44   virtual void setAttribute(QNetworkRequest::Attribute code, const QVariant& value);
45 
46   // Call this when you are ready for the finished() signal.
47   void Done();
48 
49  protected:
50   MOCK_METHOD0(abort, void());
51   virtual qint64 readData(char* data, qint64);
52   virtual qint64 writeData(const char* data, qint64);
53 
54   QByteArray data_;
55   qint64 pos_;
56 };
57 
58 
59 class MockNetworkAccessManager : public QNetworkAccessManager {
60   Q_OBJECT
61  public:
62   MockNetworkReply* ExpectGet(
63       const QString& contains,  // A string that should be present in the URL.
64       const QMap<QString, QString>& params,  // Required URL parameters.
65       int status,  // Returned HTTP status code.
66       const QByteArray& ret_data);  // Returned data.
67  protected:
68   MOCK_METHOD3(createRequest, QNetworkReply*(Operation, const QNetworkRequest&, QIODevice*));
69 };
70 
71 #endif
72