2010-03-24 00:11:46 +01:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-03-24 00:11:46 +01:00
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-05-10 23:50:31 +02:00
|
|
|
#include "core/albumcoverfetcher.h"
|
|
|
|
#include "core/networkaccessmanager.h"
|
2010-03-03 20:14:14 +01:00
|
|
|
|
|
|
|
#include <lastfm/ws.h>
|
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QEventLoop>
|
|
|
|
#include <QSignalSpy>
|
|
|
|
|
|
|
|
#include "mock_networkaccessmanager.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2010-03-04 12:43:44 +01:00
|
|
|
namespace {
|
|
|
|
|
2010-03-03 20:14:14 +01:00
|
|
|
class AlbumCoverFetcherTest : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
static void SetUpTestCase() {
|
|
|
|
lastfm::ws::ApiKey = "foobar";
|
2010-05-10 16:19:43 +02:00
|
|
|
|
|
|
|
// Lastfm takes ownership of this.
|
|
|
|
mock_network_ = new MockNetworkAccessManager;
|
|
|
|
lastfm::setNetworkAccessManager(mock_network_);
|
2010-03-03 20:14:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetUp() {
|
2014-02-21 17:24:49 +01:00
|
|
|
network_ = new NetworkAccessManager(nullptr, mock_network_);
|
2010-03-03 20:14:14 +01:00
|
|
|
}
|
|
|
|
|
2010-05-10 16:19:43 +02:00
|
|
|
void TearDown() {
|
2010-03-30 17:53:45 +02:00
|
|
|
delete network_;
|
|
|
|
}
|
|
|
|
|
2010-05-10 16:19:43 +02:00
|
|
|
static void TearDownTestCase() {
|
|
|
|
delete mock_network_;
|
|
|
|
}
|
|
|
|
|
|
|
|
static MockNetworkAccessManager* mock_network_;
|
|
|
|
NetworkAccessManager* network_;
|
2010-03-03 20:14:14 +01:00
|
|
|
};
|
|
|
|
|
2010-05-10 16:19:43 +02:00
|
|
|
MockNetworkAccessManager* AlbumCoverFetcherTest::mock_network_;
|
2010-03-30 17:53:45 +02:00
|
|
|
|
2010-03-03 20:14:14 +01:00
|
|
|
|
|
|
|
TEST_F(AlbumCoverFetcherTest, FetchesAlbumCover) {
|
2010-03-04 12:43:44 +01:00
|
|
|
QByteArray data("<lfm status=\"ok\"><album><name>Bar</name><artist>Foo</artist>"
|
|
|
|
"<image size=\"large\">http://example.com/image.jpg</image></album></lfm>");
|
2010-03-03 20:14:14 +01:00
|
|
|
|
|
|
|
QMap<QString, QString> params;
|
|
|
|
params["artist"] = "Foo";
|
|
|
|
params["album"] = "Bar";
|
|
|
|
params["api_key"] = "foobar";
|
2010-03-04 12:48:19 +01:00
|
|
|
params["method"] = "album.getInfo";
|
2010-05-10 16:19:43 +02:00
|
|
|
MockNetworkReply* get_info_reply = mock_network_->ExpectGet("audioscrobbler", params, 200, data);
|
2010-03-03 20:14:14 +01:00
|
|
|
params.clear();
|
2010-05-10 16:19:43 +02:00
|
|
|
MockNetworkReply* album_reply = mock_network_->ExpectGet("http://example.com/image.jpg", params, 200, "");
|
2010-03-03 20:14:14 +01:00
|
|
|
|
2014-02-21 17:24:49 +01:00
|
|
|
AlbumCoverFetcher fetcher(network_, nullptr);
|
2010-03-03 20:14:14 +01:00
|
|
|
QSignalSpy spy(&fetcher, SIGNAL(AlbumCoverFetched(quint64, const QImage&)));
|
|
|
|
ASSERT_TRUE(spy.isValid());
|
|
|
|
fetcher.FetchAlbumCover("Foo", "Bar");
|
|
|
|
|
|
|
|
get_info_reply->Done();
|
2010-03-23 22:44:43 +01:00
|
|
|
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
|
2010-03-03 20:14:14 +01:00
|
|
|
|
|
|
|
album_reply->Done();
|
2010-03-23 22:44:43 +01:00
|
|
|
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
|
2010-03-03 20:14:14 +01:00
|
|
|
|
|
|
|
EXPECT_EQ(1, spy.count());
|
|
|
|
}
|
2010-03-04 12:43:44 +01:00
|
|
|
|
|
|
|
} // namespace
|