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 #include <memory>
19 
20 #include "test_utils.h"
21 #include "gtest/gtest.h"
22 
23 #include "core/timeconstants.h"
24 #include "playlistparsers/plsparser.h"
25 
26 #include <QBuffer>
27 #include <QFile>
28 #include <QTemporaryFile>
29 #include <QUrl>
30 #include <QtDebug>
31 
32 using std::shared_ptr;
33 
34 class PLSParserTest : public ::testing::Test {
35 protected:
PLSParserTest()36   PLSParserTest() : parser_(nullptr) {}
37 
Open(const QString & filename)38   shared_ptr<QFile> Open(const QString& filename) {
39     shared_ptr<QFile> ret(new QFile(":/testdata/" + filename));
40     if (!ret->open(QIODevice::ReadOnly))
41       ret.reset();
42     return ret;
43   }
44 
45   PLSParser parser_;
46 };
47 
TEST_F(PLSParserTest,ParseOneTrack)48 TEST_F(PLSParserTest, ParseOneTrack) {
49   shared_ptr<QFile> file(Open("pls_one.pls"));
50 
51   SongList songs = parser_.Load(file.get(), "", QDir("/relative/to/"));
52   ASSERT_EQ(1, songs.length());
53   EXPECT_EQ(QUrl("file:///relative/to/filename with spaces.mp3"), songs[0].url());
54   EXPECT_EQ("Title", songs[0].title());
55   EXPECT_EQ(123 * kNsecPerSec, songs[0].length_nanosec());
56 }
57 
TEST_F(PLSParserTest,ParseSomaFM)58 TEST_F(PLSParserTest, ParseSomaFM) {
59   shared_ptr<QFile> file(Open("pls_somafm.pls"));
60 
61   SongList songs = parser_.Load(file.get());
62   ASSERT_EQ(4, songs.length());
63   EXPECT_EQ(QUrl("http://streamer-dtc-aa05.somafm.com:80/stream/1018"), songs[0].url());
64   EXPECT_EQ(QUrl("http://streamer-mtc-aa03.somafm.com:80/stream/1018"), songs[1].url());
65   EXPECT_EQ(QUrl("http://streamer-ntc-aa04.somafm.com:80/stream/1018"), songs[2].url());
66   EXPECT_EQ(QUrl("http://ice.somafm.com/groovesalad"), songs[3].url());
67   EXPECT_EQ("SomaFM: Groove Salad (#1 128k mp3): A nicely chilled plate of ambient beats and grooves.", songs[0].title());
68   EXPECT_EQ("SomaFM: Groove Salad (#2 128k mp3): A nicely chilled plate of ambient beats and grooves.", songs[1].title());
69   EXPECT_EQ("SomaFM: Groove Salad (#3 128k mp3): A nicely chilled plate of ambient beats and grooves.", songs[2].title());
70   EXPECT_EQ(-1, songs[0].length_nanosec());
71   EXPECT_TRUE(songs[0].is_stream());
72 }
73 
TEST_F(PLSParserTest,ParseSomaFM2)74 TEST_F(PLSParserTest, ParseSomaFM2) {
75   shared_ptr<QFile> file(Open("secretagent.pls"));
76 
77   SongList songs = parser_.Load(file.get());
78   ASSERT_EQ(4, songs.length());
79   EXPECT_EQ(QUrl("http://streamer-ntc-aa03.somafm.com:80/stream/1021"), songs[0].url());
80   EXPECT_EQ(QUrl("http://streamer-mtc-aa04.somafm.com:80/stream/1021"), songs[1].url());
81   EXPECT_EQ(QUrl("http://streamer-dtc-aa05.somafm.com:80/stream/1021"), songs[2].url());
82   EXPECT_EQ(QUrl("http://ice.somafm.com/secretagent"), songs[3].url());
83   EXPECT_EQ("SomaFM: Secret Agent (#1 128k mp3): The soundtrack for your stylish, mysterious, dangerous life. For Spies and PIs too!", songs[0].title());
84   EXPECT_EQ("SomaFM: Secret Agent (#2 128k mp3): The soundtrack for your stylish, mysterious, dangerous life. For Spies and PIs too!", songs[1].title());
85   EXPECT_EQ("SomaFM: Secret Agent (#3 128k mp3): The soundtrack for your stylish, mysterious, dangerous life. For Spies and PIs too!", songs[2].title());
86   EXPECT_EQ(-1, songs[0].length_nanosec());
87   EXPECT_TRUE(songs[0].is_stream());
88 }
89 
TEST_F(PLSParserTest,SaveAndLoad)90 TEST_F(PLSParserTest, SaveAndLoad) {
91   Song one;
92   one.set_url(QUrl("http://www.example.com/foo.mp3"));
93   one.set_title("Foo, with, some, commas");
94 
95   Song two;
96   two.set_url(QUrl("relative/bar.mp3"));
97   two.set_title("Bar");
98   two.set_length_nanosec(123 * kNsecPerSec);
99 
100   SongList songs;
101   songs << one << two;
102 
103   QTemporaryFile temp;
104   temp.open();
105   parser_.Save(songs, &temp);
106 
107   temp.seek(0);
108   songs = parser_.Load(&temp, "", QDir("/meep"));
109 
110   ASSERT_EQ(2, songs.count());
111   EXPECT_EQ(one.url(), songs[0].url());
112   EXPECT_EQ(QUrl("file:///meep/relative/bar.mp3"), songs[1].url());
113   EXPECT_EQ(one.title(), songs[0].title());
114   EXPECT_EQ(two.title(), songs[1].title());
115   EXPECT_EQ(one.length_nanosec(), songs[0].length_nanosec());
116   EXPECT_EQ(two.length_nanosec(), songs[1].length_nanosec());
117 }
118