diff --git a/src/core/mpris1.cpp b/src/core/mpris1.cpp index 84467ccd1..2ebca3e7f 100644 --- a/src/core/mpris1.cpp +++ b/src/core/mpris1.cpp @@ -31,13 +31,27 @@ namespace mpris { -Mpris1::Mpris1(PlayerInterface* player, ArtLoader* art_loader, QObject* parent) - : QObject(parent) +const char* Mpris1::kDefaultDbusServiceName = "org.mpris.clementine"; + +Mpris1::Mpris1(PlayerInterface* player, ArtLoader* art_loader, QObject* parent, + const QString& dbus_service_name) + : QObject(parent), + dbus_service_name_(dbus_service_name), + root_(NULL), + player_(NULL), + tracklist_(NULL) { qDBusRegisterMetaType(); qDBusRegisterMetaType(); - QDBusConnection::sessionBus().registerService("org.mpris.clementine"); + if (dbus_service_name_.isEmpty()) { + dbus_service_name_ = kDefaultDbusServiceName; + } + + if (!QDBusConnection::sessionBus().registerService(dbus_service_name_)) { + qWarning() << "Failed to register" << dbus_service_name_ << "on the session bus"; + return; + } root_ = new Mpris1Root(player, this); player_ = new Mpris1Player(player, this); @@ -47,6 +61,10 @@ Mpris1::Mpris1(PlayerInterface* player, ArtLoader* art_loader, QObject* parent) player_, SLOT(CurrentSongChanged(Song,QString))); } +Mpris1::~Mpris1() { + QDBusConnection::sessionBus().unregisterService(dbus_service_name_); +} + Mpris1Root::Mpris1Root(PlayerInterface* player, QObject* parent) : QObject(parent), player_(player) { new MprisRoot(this); diff --git a/src/core/mpris1.h b/src/core/mpris1.h index 3a29678d9..cc6d07f06 100644 --- a/src/core/mpris1.h +++ b/src/core/mpris1.h @@ -79,7 +79,9 @@ class Mpris1 : public QObject { Q_OBJECT public: - Mpris1(PlayerInterface* player, ArtLoader* art_loader, QObject* parent = 0); + Mpris1(PlayerInterface* player, ArtLoader* art_loader, QObject* parent = 0, + const QString& dbus_service_name = QString()); + ~Mpris1(); static QVariantMap GetMetadata(const Song& song); @@ -88,6 +90,10 @@ public: Mpris1TrackList* tracklist() const { return tracklist_; } private: + static const char* kDefaultDbusServiceName; + + QString dbus_service_name_; + Mpris1Root* root_; Mpris1Player* player_; Mpris1TrackList* tracklist_; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b5e09592f..d2ed675a5 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -42,6 +42,7 @@ set(TESTUTILS-SOURCES test_utils.cpp mock_networkaccessmanager.cpp mock_taglib.cpp + mock_player.cpp mock_playlistitem.cpp ) @@ -114,6 +115,7 @@ add_test_file(organiseformat_test.cpp false) add_test_file(playlist_test.cpp true) add_test_file(plsparser_test.cpp false) add_test_file(python_test.cpp true) +add_test_file(mpris1_test.cpp false) add_test_file(scopedtransaction_test.cpp false) add_test_file(songloader_test.cpp false) add_test_file(songplaylistitem_test.cpp false) diff --git a/tests/mock_player.cpp b/tests/mock_player.cpp new file mode 100644 index 000000000..e28d2f750 --- /dev/null +++ b/tests/mock_player.cpp @@ -0,0 +1,5 @@ +#include "mock_player.h" + +MockPlayer::MockPlayer() +{ +} diff --git a/tests/mock_player.h b/tests/mock_player.h new file mode 100644 index 000000000..2e4238fa5 --- /dev/null +++ b/tests/mock_player.h @@ -0,0 +1,64 @@ +/* This file is part of Clementine. + Copyright 2010, David Sansome + + 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 . +*/ + +#ifndef MOCK_PLAYER_H +#define MOCK_PLAYER_H + +#include "core/player.h" +#include "core/song.h" +#include "core/settingsprovider.h" +#include "library/sqlrow.h" + +#include + +class MockPlayer : public PlayerInterface { +public: + MockPlayer(); + + MOCK_CONST_METHOD0(engine, EngineBase*()); + MOCK_CONST_METHOD0(GetState, Engine::State()); + MOCK_CONST_METHOD0(GetVolume, int()); + + MOCK_CONST_METHOD0(GetCurrentItem, PlaylistItemPtr()); + MOCK_CONST_METHOD1(GetItemAt, PlaylistItemPtr(int)); + MOCK_CONST_METHOD0(playlists, PlaylistManager*()); + + MOCK_METHOD0(ReloadSettings, void()); + + MOCK_METHOD3(PlayAt, void(int, Engine::TrackChangeType, bool)); + MOCK_METHOD0(PlayPause, void()); + + MOCK_METHOD0(Next, void()); + + MOCK_METHOD0(Previous, void()); + MOCK_METHOD1(SetVolume, void(int)); + MOCK_METHOD0(VolumeUp, void()); + MOCK_METHOD0(VolumeDown, void()); + MOCK_METHOD1(SeekTo, void(int)); + MOCK_METHOD0(SeekForward, void()); + MOCK_METHOD0(SeekBackward, void()); + MOCK_METHOD1(HandleSpecialLoad, void(const PlaylistItem::SpecialLoadResult&)); + MOCK_METHOD1(CurrentMetadataChanged, void(const Song&)); + + MOCK_METHOD0(Mute, void()); + MOCK_METHOD0(Pause, void()); + MOCK_METHOD0(Stop, void()); + MOCK_METHOD0(Play, void()); + MOCK_METHOD0(ShowOSD, void()); +}; + +#endif // MOCK_PLAYER_H diff --git a/tests/mpris1_test.cpp b/tests/mpris1_test.cpp new file mode 100644 index 000000000..30f12e666 --- /dev/null +++ b/tests/mpris1_test.cpp @@ -0,0 +1,68 @@ +/* This file is part of Clementine. + Copyright 2010, David Sansome + + 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 . +*/ + +#include "core/encoding.h" +#include "core/mpris1.h" +#include "core/song.h" +#include "radio/fixlastfm.h" +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +#include "test_utils.h" +#include "mock_player.h" + +#include +#include +#include +#include + +#include + +namespace { + +class Mpris1Test : public ::testing::Test { +protected: + void SetUp() { + } + + QString service_name() const { + return "org.clementine.unittest" + + QString::number(QCoreApplication::applicationPid()); + } + + MockPlayer player_; +}; + + +TEST_F(Mpris1Test, CreatesDBusService) { + EXPECT_FALSE(QDBusConnection::sessionBus().interface()-> + isServiceRegistered(service_name())); + + boost::scoped_ptr mpris( + new mpris::Mpris1(&player_, NULL, NULL, service_name())); + EXPECT_TRUE(QDBusConnection::sessionBus().interface()-> + isServiceRegistered(service_name())); + + mpris.reset(); + EXPECT_FALSE(QDBusConnection::sessionBus().interface()-> + isServiceRegistered(service_name())); +} + +} // namespace +