diff --git a/src/engines/gstengine.cpp b/src/engines/gstengine.cpp index 2007fd3a0..5a5689e22 100644 --- a/src/engines/gstengine.cpp +++ b/src/engines/gstengine.cpp @@ -53,10 +53,7 @@ GstEngine::GstEngine() : Engine::Base(), delayq_(g_queue_new()), current_sample_(0), - equalizer_enabled_(false), - can_decode_pipeline_(NULL), - can_decode_src_(NULL), - can_decode_bin_(NULL) + equalizer_enabled_(false) { ReloadSettings(); } @@ -64,9 +61,6 @@ GstEngine::GstEngine() GstEngine::~GstEngine() { current_pipeline_.reset(); - if (can_decode_pipeline_) - gst_object_unref(GST_OBJECT(can_decode_pipeline_)); - // Destroy scope delay queue ClearScopeBuffers(); g_queue_free(delayq_); @@ -117,21 +111,19 @@ bool GstEngine::CanDecode(const QUrl &url) { can_decode_last_ = false; // Create the pipeline - if (!can_decode_pipeline_) { - can_decode_pipeline_ = CreateElement("pipeline"); - can_decode_src_ = CreateElement("giosrc", can_decode_pipeline_); - can_decode_bin_ = CreateElement("decodebin", can_decode_pipeline_); + GstElement* pipeline = CreateElement("pipeline"); + GstElement* src = CreateElement("giosrc", pipeline); + GstElement* bin = CreateElement("decodebin", pipeline); - gst_element_link(can_decode_src_, can_decode_bin_); - g_signal_connect(G_OBJECT(can_decode_bin_), "new-decoded-pad", G_CALLBACK(CanDecodeNewPadCallback), this); - g_signal_connect(G_OBJECT(can_decode_bin_), "no-more-pads", G_CALLBACK(CanDecodeLastCallback), this); - } + gst_element_link(src, bin); + g_signal_connect(G_OBJECT(bin), "new-decoded-pad", G_CALLBACK(CanDecodeNewPadCallback), this); + g_signal_connect(G_OBJECT(bin), "no-more-pads", G_CALLBACK(CanDecodeLastCallback), this); // Set the file we're testing - g_object_set(G_OBJECT(can_decode_src_), "location", url.toEncoded().constData(), NULL); + g_object_set(G_OBJECT(src), "location", url.toEncoded().constData(), NULL); // Start the pipeline playing - gst_element_set_state(can_decode_pipeline_, GST_STATE_PLAYING); + gst_element_set_state(pipeline, GST_STATE_PLAYING); // Wait until found audio stream int count = 0; @@ -141,7 +133,8 @@ bool GstEngine::CanDecode(const QUrl &url) { } // Stop playing - gst_element_set_state(can_decode_pipeline_, GST_STATE_NULL); + gst_element_set_state(pipeline, GST_STATE_NULL); + gst_object_unref(pipeline); return can_decode_success_; } diff --git a/src/engines/gstengine.h b/src/engines/gstengine.h index cf2c6a62e..3bf842fa1 100644 --- a/src/engines/gstengine.h +++ b/src/engines/gstengine.h @@ -136,10 +136,6 @@ class GstEngine : public Engine::Base { mutable bool can_decode_success_; mutable bool can_decode_last_; - - GstElement* can_decode_pipeline_; - GstElement* can_decode_src_; - GstElement* can_decode_bin_; }; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 941faf48a..365b246ee 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -54,11 +54,13 @@ add_custom_target(build_tests ) add_dependencies(test build_tests) -add_library(test_gui_main EXCLUDE_FROM_ALL main.cpp) +qt4_add_resources(TEST-RESOURCE-SOURCES data/testdata.qrc) + +add_library(test_gui_main EXCLUDE_FROM_ALL ${TEST-RESOURCE-SOURCES} main.cpp) target_link_libraries(test_gui_main clementine_lib) set_target_properties(test_gui_main PROPERTIES COMPILE_DEFINITIONS GUI) -add_library(test_main EXCLUDE_FROM_ALL main.cpp) +add_library(test_main EXCLUDE_FROM_ALL ${TEST-RESOURCE-SOURCES} main.cpp) target_link_libraries(test_main clementine_lib) # Given a file foo_test.cpp, creates a target foo_test and adds it to the test target. @@ -98,3 +100,4 @@ add_test_file(songplaylistitem_test.cpp false) add_test_file(translations_test.cpp false) add_test_file(playlist_test.cpp true) add_test_file(scopedtransaction_test.cpp true) +add_test_file(fileformats_test.cpp true) diff --git a/tests/data/beep.flac b/tests/data/beep.flac new file mode 100644 index 000000000..521ca2f46 Binary files /dev/null and b/tests/data/beep.flac differ diff --git a/tests/data/beep.mp3 b/tests/data/beep.mp3 new file mode 100644 index 000000000..80341ecc0 Binary files /dev/null and b/tests/data/beep.mp3 differ diff --git a/tests/data/beep.ogg b/tests/data/beep.ogg new file mode 100644 index 000000000..9a88f3679 Binary files /dev/null and b/tests/data/beep.ogg differ diff --git a/tests/data/beep.spx b/tests/data/beep.spx new file mode 100644 index 000000000..b6fe06e0b Binary files /dev/null and b/tests/data/beep.spx differ diff --git a/tests/data/beep.wav b/tests/data/beep.wav new file mode 100644 index 000000000..cb01bb413 Binary files /dev/null and b/tests/data/beep.wav differ diff --git a/tests/data/testdata.qrc b/tests/data/testdata.qrc new file mode 100644 index 000000000..da37a0422 --- /dev/null +++ b/tests/data/testdata.qrc @@ -0,0 +1,9 @@ +<RCC> + <qresource prefix="/testdata"> + <file>beep.flac</file> + <file>beep.mp3</file> + <file>beep.ogg</file> + <file>beep.spx</file> + <file>beep.wav</file> + </qresource> +</RCC> diff --git a/tests/fileformats_test.cpp b/tests/fileformats_test.cpp new file mode 100644 index 000000000..24a91ecfb --- /dev/null +++ b/tests/fileformats_test.cpp @@ -0,0 +1,100 @@ +/* This file is part of Clementine. + + 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 "song.h" +#include "engines/gstengine.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +#include "test_utils.h" + +#include <QTemporaryFile> +#include <QResource> +#include <QDir> +#include <QSet> + +namespace { + +class FileformatsTest : public ::testing::TestWithParam<const char*> { + public: + static void SetUpTestCase() { + sGstEngine = new GstEngine; + ASSERT_TRUE(sGstEngine->Init()); + } + + static void TearDownTestCase() { + delete sGstEngine; + sGstEngine = NULL; + } + + protected: + FileformatsTest() { + kFormatsWithoutMetadata.insert("wav"); + } + + void SetUp() { + format_ = GetParam(); + resource_filename_ = ":/testdata/beep." + format_; + temp_filetemplate_ = QDir::tempPath() + "/fileformatstest-XXXXXX." + format_; + } + + void SaveToTempFile(QTemporaryFile* file) { + QResource resource(resource_filename_); + file->open(); + file->write(reinterpret_cast<const char*>(resource.data()), resource.size()); + file->flush(); + } + + static GstEngine* sGstEngine; + + QSet<QString> kFormatsWithoutMetadata; + QString format_; + QString resource_filename_; + QString temp_filetemplate_; +}; + +GstEngine* FileformatsTest::sGstEngine = NULL; + + +TEST_P(FileformatsTest, Exists) { + EXPECT_TRUE(QFile::exists(resource_filename_)); +} + +TEST_P(FileformatsTest, LoadsTags) { + QTemporaryFile temp(temp_filetemplate_); + SaveToTempFile(&temp); + + Song song; + song.InitFromFile(temp.fileName(), -1); + ASSERT_TRUE(song.is_valid()); + + if (!kFormatsWithoutMetadata.contains(format_)) { + EXPECT_EQ("Beep " + format_, song.title()); + } +} + +TEST_P(FileformatsTest, GstCanDecode) { + QTemporaryFile temp(temp_filetemplate_); + SaveToTempFile(&temp); + + EXPECT_TRUE(sGstEngine->CanDecode(QUrl::fromLocalFile(temp.fileName()))); +} + +INSTANTIATE_TEST_CASE_P(Formats, FileformatsTest, ::testing::Values( + "flac", "mp3", "ogg", "spx", "wav")); + +} // namespace diff --git a/tests/resources_env.h b/tests/resources_env.h index e04e02ec6..0bca4357d 100644 --- a/tests/resources_env.h +++ b/tests/resources_env.h @@ -26,6 +26,7 @@ public: void SetUp() { Q_INIT_RESOURCE(data); Q_INIT_RESOURCE(translations); + Q_INIT_RESOURCE(testdata); } };