File: song_test.cpp

package info (click to toggle)
clementine 1.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 24,792 kB
  • ctags: 20,160
  • sloc: cpp: 111,170; xml: 4,413; python: 2,687; ansic: 984; sql: 753; sh: 66; makefile: 25
file content (295 lines) | stat: -rw-r--r-- 8,191 bytes parent folder | download
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* This file is part of Clementine.
   Copyright 2010, David Sansome <me@davidsansome.com>

   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 "config.h"
#include "tagreader.h"
#include "core/song.h"
#ifdef HAVE_LIBLASTFM
  #include "internet/lastfmcompat.h"
#endif

#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include "test_utils.h"

#include <QTemporaryFile>
#include <QTextCodec>

#include <id3v2tag.h>

namespace {

class SongTest : public ::testing::Test {
 protected:
  static void SetUpTestCase() {
    // Return something from uninteresting mock functions.
    testing::DefaultValue<TagLib::String>::Set("foobarbaz");
  }

  static Song ReadSongFromFile(const QString& filename) {
    TagReader tag_reader;
    Song song;
    ::pb::tagreader::SongMetadata pb_song;

    // We need to init protobuf object from a Song object, to have default
    // values initialized correctly. For example, Song's rating is -1 by
    // default: using protobuf directly would lead to 0 by default, which is not
    // what we want.
    song.ToProtobuf(&pb_song);
    tag_reader.ReadFile(filename, &pb_song);
    song.InitFromProtobuf(pb_song);
    return song;
  }

  static void WriteSongToFile(const Song& song, const QString& filename) {
    TagReader tag_reader;
    ::pb::tagreader::SongMetadata pb_song;
    song.ToProtobuf(&pb_song);
    tag_reader.SaveFile(filename, pb_song);
  }

  static void WriteSongStatisticsToFile(const Song& song, const QString& filename) {
    TagReader tag_reader;
    ::pb::tagreader::SongMetadata pb_song;
    song.ToProtobuf(&pb_song);
    tag_reader.SaveSongStatisticsToFile(filename, pb_song);
  }

  static void WriteSongRatingToFile(const Song& song, const QString& filename) {
    TagReader tag_reader;
    ::pb::tagreader::SongMetadata pb_song;
    song.ToProtobuf(&pb_song);
    tag_reader.SaveSongRatingToFile(filename, pb_song);
  }
};


#ifdef HAVE_LIBLASTFM
TEST_F(SongTest, InitsFromLastFM) {
  Song song;
  lastfm::MutableTrack track;
  track.setTitle("Foo");
  lastfm::Artist artist("Bar");
  track.setArtist(artist);
  lastfm::Album album(artist, "Baz");
  track.setAlbum(album);

  song.InitFromLastFM(track);
  EXPECT_EQ("Foo", song.title());
  EXPECT_EQ("Baz", song.album());
  EXPECT_EQ("Bar", song.artist());
}
#endif // HAVE_LIBLASTFM

/*TEST_F(SongTest, InitsFromFile) {
  QTemporaryFile temp;
  temp.open();
  mock_factory_.ExpectCall(temp.fileName(), "Foo", "Bar", "Baz");
  Song song(&mock_factory_);
  song.InitFromFile(temp.fileName(), 42);
  EXPECT_EQ("Foo", song.title());
  EXPECT_EQ("Bar", song.artist());
  EXPECT_EQ("Baz", song.album());
}*/

TEST_F(SongTest, FMPSRating) {
  TemporaryResource r(":/testdata/fmpsrating.mp3");
  Song song = ReadSongFromFile(r.fileName());
  EXPECT_FLOAT_EQ(0.42, song.rating());
}

TEST_F(SongTest, FMPSRatingUser) {
  TemporaryResource r(":/testdata/fmpsratinguser.mp3");
  Song song = ReadSongFromFile(r.fileName());
  EXPECT_FLOAT_EQ(0.10, song.rating());

  song.set_rating(0.20);
  WriteSongRatingToFile(song, r.fileName());
  Song new_song = ReadSongFromFile(r.fileName());
  EXPECT_FLOAT_EQ(0.20, new_song.rating());
}

TEST_F(SongTest, FMPSRatingBoth) {
  TemporaryResource r(":/testdata/fmpsratingboth.mp3");
  Song song = ReadSongFromFile(r.fileName());
  EXPECT_FLOAT_EQ(0.42, song.rating());
}

TEST_F(SongTest, FMPSPlayCount) {
  TemporaryResource r(":/testdata/fmpsplaycount.mp3");
  Song song = ReadSongFromFile(r.fileName());
  EXPECT_EQ(123, song.playcount());

  song.set_playcount(69);
  WriteSongStatisticsToFile(song, r.fileName());
  Song new_song = ReadSongFromFile(r.fileName());
  EXPECT_EQ(69, new_song.playcount());
}

TEST_F(SongTest, FMPSPlayCountUser) {
  TemporaryResource r(":/testdata/fmpsplaycountuser.mp3");
  Song song = ReadSongFromFile(r.fileName());
  EXPECT_EQ(42, song.playcount());
}

TEST_F(SongTest, FMPSPlayCountBoth) {
  TemporaryResource r(":/testdata/fmpsplaycountboth.mp3");
  Song song = ReadSongFromFile(r.fileName());
  EXPECT_EQ(123, song.playcount());
}

TEST_F(SongTest, FMPSScore) {
  TemporaryResource r(":/testdata/beep.mp3");
  {
    Song song = ReadSongFromFile(r.fileName());
    song.set_score(87);

    WriteSongStatisticsToFile(song, r.fileName());
  }

  Song new_song = ReadSongFromFile(r.fileName());
  EXPECT_EQ(87, new_song.score());
}

TEST_F(SongTest, POPMRating) {
  TemporaryResource r(":/testdata/popmrating.mp3");
  Song song = ReadSongFromFile(r.fileName());
  EXPECT_FLOAT_EQ(0.60, song.rating());
}

TEST_F(SongTest, BothFMPSPOPMRating) {
  // fmpspopmrating.mp3 contains FMPS with rating 0.42 and POPM with 0x80
  // (corresponds to 0.60 rating for us): check that FMPS tag has precedence
  TemporaryResource r(":/testdata/fmpspopmrating.mp3");
  Song song = ReadSongFromFile(r.fileName());
  EXPECT_FLOAT_EQ(0.42, song.rating());
}

TEST_F(SongTest, RatingOgg) {
  TemporaryResource r(":/testdata/beep.ogg");
  {
    Song song = ReadSongFromFile(r.fileName());
    song.set_rating(0.20);
    WriteSongRatingToFile(song, r.fileName());
  }

  Song new_song = ReadSongFromFile(r.fileName());
  EXPECT_FLOAT_EQ(0.20, new_song.rating());
}

TEST_F(SongTest, StatisticsOgg) {
  TemporaryResource r(":/testdata/beep.ogg");
  {
    Song song = ReadSongFromFile(r.fileName());
    song.set_playcount(1337);
    song.set_score(87);

    WriteSongStatisticsToFile(song, r.fileName());
  }

  Song new_song = ReadSongFromFile(r.fileName());
  EXPECT_EQ(1337, new_song.playcount());
  EXPECT_EQ(87, new_song.score());
}

TEST_F(SongTest, RatingFLAC) {
  TemporaryResource r(":/testdata/beep.flac");
  {
    Song song = ReadSongFromFile(r.fileName());
    song.set_rating(0.20);
    WriteSongRatingToFile(song, r.fileName());
  }

  Song new_song = ReadSongFromFile(r.fileName());
  EXPECT_FLOAT_EQ(0.20, new_song.rating());
}

TEST_F(SongTest, StatisticsFLAC) {
  TemporaryResource r(":/testdata/beep.flac");
  {
    Song song = ReadSongFromFile(r.fileName());
    song.set_playcount(1337);
    song.set_score(87);

    WriteSongStatisticsToFile(song, r.fileName());
  }

  Song new_song = ReadSongFromFile(r.fileName());
  EXPECT_EQ(1337, new_song.playcount());
  EXPECT_EQ(87, new_song.score());
}

#ifdef TAGLIB_WITH_ASF
TEST_F(SongTest, RatingASF) {
  TemporaryResource r(":/testdata/beep.wma");
  {
    Song song = ReadSongFromFile(r.fileName());
    song.set_rating(0.20);

    WriteSongRatingToFile(song, r.fileName());
  }

  Song new_song = ReadSongFromFile(r.fileName());
  EXPECT_FLOAT_EQ(0.20, new_song.rating());
}

TEST_F(SongTest, StatisticsASF) {
  TemporaryResource r(":/testdata/beep.wma");
  {
    Song song = ReadSongFromFile(r.fileName());
    song.set_playcount(1337);
    song.set_score(87);

    WriteSongStatisticsToFile(song, r.fileName());
  }

  Song new_song = ReadSongFromFile(r.fileName());
  EXPECT_EQ(1337, new_song.playcount());
  EXPECT_EQ(87, new_song.score());
}
#endif // TAGLIB_WITH_ASF

TEST_F(SongTest, RatingMP4) {
  TemporaryResource r(":/testdata/beep.m4a");
  {
    Song song = ReadSongFromFile(r.fileName());
    song.set_rating(0.20);

    WriteSongRatingToFile(song, r.fileName());
  }

  Song new_song = ReadSongFromFile(r.fileName());
  EXPECT_FLOAT_EQ(0.20, new_song.rating());
}

TEST_F(SongTest, StatisticsMP4) {
  TemporaryResource r(":/testdata/beep.m4a");
  {
    Song song = ReadSongFromFile(r.fileName());
    song.set_playcount(1337);
    song.set_score(87);

    WriteSongStatisticsToFile(song, r.fileName());
  }

  Song new_song = ReadSongFromFile(r.fileName());
  EXPECT_EQ(1337, new_song.playcount());
  EXPECT_EQ(87, new_song.score());
}

}  // namespace