forked from clementine-player/Clementine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusicbrainzclient_test.cpp
250 lines (203 loc) · 9.06 KB
/
musicbrainzclient_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* This file is part of Clementine.
Copyright 2010, David Sansome <[email protected]>
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/>.
*/
#include <memory>
#include "core/logging.h"
#include "musicbrainz/musicbrainzclient.h"
#include <QCoreApplication>
#include <QEventLoop>
#include <QFile>
#include <QMetaType>
#include <QSignalSpy>
#include <QString>
#include <QStringList>
#include "mock_networkaccessmanager.h"
#include "gtest/gtest.h"
#include "test_utils.h"
typedef QList<MusicBrainzClient::Result> ResultList;
Q_DECLARE_METATYPE(ResultList);
class MusicBrainzClientTest : public ::testing::Test {
protected:
static void SetUpTestCase() {
qRegisterMetaType<ResultList>("MusicBrainzClient::ResultList");
}
void SetUp() { mock_network_.reset(new MockNetworkAccessManager); }
// Reads the data from a file into a QByteArray and returns it.
QByteArray ReadDataFromFile(const QString& filename) {
QFile file(filename);
file.open(QIODevice::ReadOnly);
QByteArray data = file.readAll();
return data;
}
std::unique_ptr<MockNetworkAccessManager> mock_network_;
};
// Test if a discid that do not exist in the musicbrainz database
// generates an empty result.
TEST_F(MusicBrainzClientTest, DiscIdNotFound) {
QByteArray data =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><error><text>Not "
"Found</text><text>For usage, please see: "
"http://musicbrainz.org/development/mmd</text></error>";
// Create a MusicBrainzClient instance with mock_network_.
MusicBrainzClient musicbrainz_client(nullptr, mock_network_.get());
// Hook the data as the response to a query of a given type.
QMap<QString, QString> params;
params["inc"] = "artists+recordings";
MockNetworkReply* discid_reply =
mock_network_->ExpectGet("discid", params, 200, data);
// Set up a QSignalSpy which stores the result.
QSignalSpy spy(&musicbrainz_client,
SIGNAL(Finished(const QString&, const QString,
const MusicBrainzClient::ResultList&)));
ASSERT_TRUE(spy.isValid());
EXPECT_EQ(0, spy.count());
// Start the request and get a result. The argument doesn't matter
// in the test.
musicbrainz_client.StartDiscIdRequest("fooDiscid");
discid_reply->Done();
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
EXPECT_EQ(1, spy.count());
QList<QVariant> result = spy.takeFirst();
QString artist = result.takeFirst().toString();
QString album = result.takeFirst().toString();
ResultList tracks = result.takeFirst().value<ResultList>();
// Check that title and artist are empty, and that there are zero tracks.
EXPECT_TRUE(artist.isEmpty());
EXPECT_TRUE(album.isEmpty());
EXPECT_EQ(0, tracks.count());
}
// Test if MusicBrainzClient::StartDiscIdRequest() parses a discid
// correctly.
TEST_F(MusicBrainzClientTest, ParseDiscID) {
QByteArray data = ReadDataFromFile(":testdata/discid_2cd.xml");
ASSERT_FALSE(data.isEmpty());
// The following are the expected values given for the test file
// discid_2cd.xml. The discid corresponds to the 2nd disc in the
// set. The test file contains two releases but we only parse the first.
const QString expected_artist = "Symphony X";
const QString expected_title = "Live on the Edge of Forever";
const int expected_number_of_tracks = 6;
const int expected_year = 2001;
// Create a MusicBrainzClient instance with mock_network_.
MusicBrainzClient musicbrainz_client(nullptr, mock_network_.get());
// Hook the data as the response to a query of a given type.
QMap<QString, QString> params;
params["inc"] = "artists+recordings";
MockNetworkReply* discid_reply =
mock_network_->ExpectGet("discid", params, 200, data);
// Set up a QSignalSpy which stores the result.
QSignalSpy spy(&musicbrainz_client,
SIGNAL(Finished(const QString&, const QString,
const MusicBrainzClient::ResultList&)));
ASSERT_TRUE(spy.isValid());
EXPECT_EQ(0, spy.count());
// Start the request and get a result. The argument doesn't matter
// in the test. It is here set to the discid of the requested disc.
musicbrainz_client.StartDiscIdRequest("lvcH9_vbw_rJAbXieTOo1CbyNmQ-");
discid_reply->Done();
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
EXPECT_EQ(1, spy.count());
QList<QVariant> result = spy.takeFirst();
QString artist = result.takeFirst().toString();
QString album = result.takeFirst().toString();
ResultList tracks = result.takeFirst().value<ResultList>();
// Check that title and artist are correct.
EXPECT_EQ(expected_artist, artist);
EXPECT_EQ(expected_title, album);
// Check that we get the correct number of tracks, i.e. that the
// correct disc is chosen in a multi-disc release.
EXPECT_EQ(expected_number_of_tracks, tracks.count());
// Check that the tracks is ordered by track number in ascending
// order and that the expected year is added to all tracks.
for (int i = 0; i < tracks.count(); ++i) {
EXPECT_EQ(i + 1, tracks[i].track_);
EXPECT_EQ(expected_year, tracks[i].year_);
}
// Check some track information.
EXPECT_EQ("Smoke and Mirrors", tracks[0].title_);
EXPECT_EQ(1, tracks[0].track_);
EXPECT_EQ(394600, tracks[0].duration_msec_);
EXPECT_EQ("Church of the Machine", tracks[1].title_);
EXPECT_EQ(2, tracks[1].track_);
EXPECT_EQ(441866, tracks[1].duration_msec_);
}
// Test if MusicBrainzClient::Start() parses a track correctly.
TEST_F(MusicBrainzClientTest, ParseTrack) {
QByteArray data = ReadDataFromFile(":testdata/recording.xml");
ASSERT_FALSE(data.isEmpty());
// Expected results from the test file recording.xml:
const int expected_track_number = 6;
const QString expected_title = "Victoria und ihr Husar: Pardon Madame";
const QString expected_artist = "Paul Abraham";
const QString expected_album = "An Evening at the Operetta";
const int expected_year = 1992;
// Create a MusicBrainzClient instance with mock_network_.
MusicBrainzClient musicbrainz_client(nullptr, mock_network_.get());
// Hook the data as the response to a query of a given type.
QMap<QString, QString> params;
params["inc"] = "artists+releases+media";
MockNetworkReply* discid_reply =
mock_network_->ExpectGet("recording", params, 200, data);
QSignalSpy spy(&musicbrainz_client,
SIGNAL(Finished(int, const MusicBrainzClient::ResultList&)));
ASSERT_TRUE(spy.isValid());
EXPECT_EQ(0, spy.count());
// Start the request and get a result.
// The mbid argument doesn't matter in the test.
const int sent_id = 0;
musicbrainz_client.Start(sent_id, QStringList() << "fooMbid");
discid_reply->Done();
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
EXPECT_EQ(1, spy.count());
QList<QVariant> result = spy.takeFirst();
int id = result.takeFirst().toInt();
EXPECT_EQ(sent_id, id);
ResultList tracks = result.takeFirst().value<ResultList>();
for (const MusicBrainzClient::Result& track : tracks) {
EXPECT_EQ(expected_track_number, track.track_);
EXPECT_EQ(expected_title, track.title_);
EXPECT_EQ(expected_artist, track.artist_);
EXPECT_EQ(expected_album, track.album_);
EXPECT_EQ(expected_year, track.year_);
}
}
// For a recording with multiple releases, we should get them all.
TEST_F(MusicBrainzClientTest, ParseTrackWithMultipleReleases) {
QByteArray data =
ReadDataFromFile(":testdata/recording_with_multiple_releases.xml");
ASSERT_FALSE(data.isEmpty());
const int expected_number_of_releases = 7;
// Create a MusicBrainzClient instance with mock_network_.
MusicBrainzClient musicbrainz_client(nullptr, mock_network_.get());
// Hook the data as the response to a query of a given type.
QMap<QString, QString> params;
params["inc"] = "artists+releases+media";
MockNetworkReply* discid_reply =
mock_network_->ExpectGet("recording", params, 200, data);
QSignalSpy spy(&musicbrainz_client,
SIGNAL(Finished(int, const MusicBrainzClient::ResultList&)));
ASSERT_TRUE(spy.isValid());
EXPECT_EQ(0, spy.count());
// Start the request and get a result.
// The mbid argument doesn't matter in the test.
const int sent_id = 0;
musicbrainz_client.Start(sent_id, QStringList() << "fooMbid");
discid_reply->Done();
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
EXPECT_EQ(1, spy.count());
QList<QVariant> result = spy.takeFirst();
int id = result.takeFirst().toInt();
EXPECT_EQ(sent_id, id);
ResultList tracks = result.takeFirst().value<ResultList>();
EXPECT_EQ(expected_number_of_releases, tracks.count());
}