diff options
-rw-r--r-- | Makefile | 8 | ||||
-rw-r--r-- | include/resource.h | 187 | ||||
-rw-r--r-- | libresourceqt.pro | 15 | ||||
-rw-r--r-- | src/resource.cpp | 136 | ||||
-rw-r--r-- | tests/Makefile | 1 | ||||
-rw-r--r-- | tests/test-resource.cpp | 183 | ||||
-rw-r--r-- | tests/test-resource.h | 30 | ||||
-rw-r--r-- | tests/test-resource.pro | 4 |
8 files changed, 168 insertions, 396 deletions
@@ -7,19 +7,19 @@ LIB = build/libresourceqt.so.1.0.0 all: $(MAKEFILE) $(MAKE) -f $(MAKEFILE) all - cd tests; $(MAKE) all + $(MAKE) -C tests all clean: $(RM) $(MAKEFILE) $(RM) -r build - cd tests; $(MAKE) clean + $(MAKE) -C tests clean install: $(MAKEFILE) $(MAKE) -f $(MAKEFILE) install - cd tests; $(MAKE) install + $(MAKE) -C tests install tests: force - cd tests; $(MAKE) tests + $(MAKE) -C tests tests force: ; diff --git a/include/resource.h b/include/resource.h index d1c386a..ea4eda8 100644 --- a/include/resource.h +++ b/include/resource.h @@ -1,173 +1,32 @@ #ifndef RESOURCE_H #define RESOURCE_H -#include <QObject> -#include <QPointer> -#include "resource-library.h" -#include "resource-types.h" +#include <QString> -using namespace ResourceTypes; - -/** - * This class represents a (set of) requested resource(s). It is created through - * the ResourceFactory which knows how to connect it to the underying ResourceLibrary. - * This class is responsible for managing the requested resource and is the way - * for the application to communicate with the Resource Policy. - * - * If a program wants to use different sets of resources, e.g. a media player - * wanting to play either music or video (audio + video resources), it would then - * create multiple resource objects from the ResourceFactory. - * - * Here is an example of how a media player might use this class. First off you - * are going to need a \ref ResourceFactory which you use to create a \ref Resource. - * The Resource has a number of signals, most are not strictly mandatory to connect - * to, but in order to have it work correctly you should at least connect to the - * connectedToServer() and stateChanged() signals, see this example. - * \code - * resourceFactory = new ResourceFactory(this); - * QVERIFY(resourceFactory != NULL); - * Resource *resource = resourceFactory->createResource(MediaClass, AudioResource|VideoResource); - * if(resource == NULL) doError(); - * QObject::connect(resource, SIGNAL(connectedToServer()), - * &myResourceHandler, SLOT(connectedToServer())); - * QObject::connect(resource, SIGNAL(stateChanged(enum ResourceState)), - * &myResourceHandler, SLOT(stateChanged(enum ResourceState))); - * resource->connectToServer(); - * \endcode - * Then the resource handler would have methods like these to handle the signals: - * \code - * void MediaPlayerResourceHandler::connectedToServer() - * { - * //We are now connected to the Policy Server, we can start requesting. - * this->reserve(); - * } - * - * void MediaPlayerResourceHandler::stateChanged(enum ResourceState) - * { - * if(resourceState == ResourceTypes::NotOwnedState) { - * this->mediaStream->stopMedia(); - * } - * else if(resourceState == ResourceTypes::NotOwnedState) { - * //We now own the audio and video devices, so we can - * // freely start playing. - * this->mediaStream->startMedia(); - * } - * } - * \endcode - */ -class Resource: public QObject +class Resource { - Q_OBJECT - Q_DISABLE_COPY( Resource ); - friend class ResourceFactory; - friend class TestResource; -private: - //! \internal The Class that the application belongs to. - enum ResourceClass resourceClass; - //! \internal A bit mask of ResourceTypes::ResourceType types which determine the - //! the resources which we are interested in. - quint16 resourceType; - //! \internal The ResourceLibrary low-level protocol library wrapper. - ResourceLibrary *resourceLibrary; - //! The current state of the resource, whether we have exclusive access or not. - enum ResourceState resourceState; - - //! \internal Private Constructor to prevent accidental mis-use - Resource(enum ResourceClass requestedClass, quint16 requestedResources, QObject *parent=0); - //! \internal Initializes the resource library and the low-level protocol library. - bool initialize(ResourceLibrary *library); public: - virtual ~Resource(); - - //! Returns true if we have exclusive access to our resources (as a whole) - bool isReserved() const; - //! Method used to query whether we have a given resource type in our set. - bool hasResource(enum ResourceType resourceType) const; - - //! The application class that was registered to this resource. - enum ResourceClass applicationClass() const; - //! Returns the \ref ResourceTypes::ResourceType bit mask of resources. - quint16 resources() const; - - //! Connects and registeres with the Resource Policy Server. The connectedToServer() - //! signal is emited when the connection is successful. - bool connectToServer(); - - //! Disconnects from the Resource Policy server. - void disconnectFromServer(); - - //! \internal Stores the new state and emits a stateChanged() signal. - void handleStateChange(enum ResourceState newState); - //! \internal Emits the reservable() signal. - void emitReservable(); - - //! Reserve exclusive access to our resources. - //! \return true when the message was successfully sent to the server. - virtual bool reserve(); - //! Ask server to relinquish exclusive access to our resources. - //! \return true when the message was successfully sent to the server. - virtual bool release(); - //! Query the server of our current state. The stateChanged() signal will - //! be emited when the server responds. - virtual bool requestState(); - - //! Set the global Mute state. - virtual bool setMute(); - //! unset the global mute state. - virtual bool unsetMute(); - //! Query the server for the global mute state. The reply will come - //! via the muteState() signal. - virtual bool requestMute(); - - //! Set the global privacy override, i.e. pipe all audio to the public - //! audio device (IHF speakers). - virtual bool setPrivacyOverride(); - //! Unset the global privacy override, i.e. pipe audio to the private - //! headset or eaprpiece if policy dictates so. - virtual bool unsetPrivacyOverride(); - //! Query the server for the global provacy override state. The reply - //! comes via the privacyOverride() signal. - virtual bool requestPrivacyOverride(); - - //! Set the global bluetooth override, i.e. Do not route audio to the - //! bluetooth device even if it is connected. - virtual bool setBluetoothOverride(); - //! Unset the global bluetooth override, i.e. audio is routed to a - //! connected bluetooth headset if policy dictates so. - virtual bool unsetBluetoothOverride(); - //! Query the server for the global bluetooth override flag. The reply - //! comes via the bluetoothOverrideState() signal. - virtual bool requestBluetoothOverride(); - - //! Let the Policy Server know the process ID (PID) of the process which - //! will do the actual media playing, in case it differs from the PID - //! of the owner of this object. - virtual bool setPid(quint32 pid); - //! Tell policy the stream name of the media stream that this program uses, - //! for the same reasons as above. - virtual bool setStreamName(const QString & name); - -signals: - //! Emited when we are connected to the server. - void connectedToServer(); - //! Emited when the server notifies us that another process has precedence - //! over us. When receiving this signal with the ResourceTypes::NotOwnedState - //! the program should immediately stop using the resources it had reserved - //! (if it is in use). When receiving this signal with the - //! ResourceTypes::OwnedState, it means that we have received access to the - //! resources we asked for and are free to start to use them. - void stateChanged(enum ResourceState newState); - //! This is a hint by the Policy Server that our resources are free to use. - //! It means we can expect a reserve() to result in a - //! stateChanged(ResourceTypes::OwnedState) signal. - void reservable(); - //! This signal shows the global mute state. - void muteStateChanged(bool oldMuteState, bool newMuteState); - //! This signal shows the global privacy override state. - void privacyOverrideChanged(bool PrivacyState, bool newPrivacyState); - //! This signal shows the global bluetooth override state. - void bluetoothOverrideChanged(bool oldBluetoothState, bool newBluetoothState); + Resource(const Resource &other); + Resource(); + Resource &operator=(const Resource &other); + + ~Resource(); + + QString type() const; + void setType(const QString type); + bool isOptional() const; + void setOptional(bool resourceIsOptional=true); + bool isShared() const; + void setShared(bool shared=true); + quint32 id() const; + void setId(quint32 newId); + + bool operator==(const Resource &other); +private: + QString name; + bool optional; + bool shared; + quint32 identifier; }; #endif - diff --git a/libresourceqt.pro b/libresourceqt.pro index 543a171..125282d 100644 --- a/libresourceqt.pro +++ b/libresourceqt.pro @@ -6,23 +6,18 @@ DEPENDPATH += include src INCLUDEPATH += src include # Input -PUBLIC_HEADERS = include/resource.h \ - include/resource-factory.h \ - include/resource-types.h -HEADERS += $$PUBLIC_HEADERS \ - src/libplayback-wrapper.h \ - src/resource-library.h +PUBLIC_HEADERS = include/resource.h -SOURCES += src/libplayback-wrapper.cpp \ - src/resource.cpp \ - src/resource-factory.cpp +HEADERS += $$PUBLIC_HEADERS + +SOURCES += src/resource.cpp OBJECTS_DIR = build MOC_DIR = build CONFIG += qt link_pkgconfig dll QT = core dbus -PKGCONFIG += dbus-1 libplayback-1 +PKGCONFIG += dbus-1 # Install directives headers.files = $$PUBLIC_HEADERS diff --git a/src/resource.cpp b/src/resource.cpp index bc03847..3f529b6 100644 --- a/src/resource.cpp +++ b/src/resource.cpp @@ -1,140 +1,80 @@ #include "resource.h" -Resource::Resource(enum ResourceClass requestedClass, quint16 requestedResources, - QObject *parent) - : QObject(parent), resourceClass(requestedClass), - resourceType(requestedResources), resourceState(UnknownState) +Resource::Resource() : name(QString()), optional(false), shared(false) { + identifier = (quint32)this; } Resource::~Resource() { } -bool Resource::initialize(ResourceLibrary *library) +Resource::Resource(const Resource &other) + : name(other.name), optional(other.optional), + shared(other.shared), identifier(other.identifier) { - qDebug("Resource::initialize"); - resourceLibrary = library; - return resourceLibrary->initialize(); } -bool Resource::connectToServer() +Resource & Resource::operator=(const Resource &other) { - if(resourceLibrary == NULL) - return false; - return resourceLibrary->connectToServer(); -} - -quint16 Resource::resources() const -{ - return resourceType; -} + name = other.name; + optional = other.optional; + shared = other.shared; + identifier = other.identifier; -enum ResourceClass Resource::applicationClass() const -{ - return resourceClass; + return *this; } -bool Resource::hasResource(enum ResourceType resourceType) const +QString Resource::type() const { - if((this->resourceType & resourceType) == resourceType) - return true; - else - return false; -} - -bool Resource::isReserved() const -{ - if(resourceState == OwnedState) - return true; - else - return false; + return name; } -void Resource::handleStateChange(enum ResourceState newState) -{ - if(resourceState == UnknownState) { - resourceState = newState; - emit connectedToServer(); - } - else { - resourceState = newState; - emit stateChanged(resourceState); - } -} - -void Resource::emitReservable() -{ - emit reservable(); -} - -bool Resource::reserve() -{ - return false; -} - -bool Resource::release() +void Resource::setType(const QString type) { - return false; + name = type; } -bool Resource::requestState() +bool Resource::isOptional() const { - return false; + return optional; } -bool Resource::setMute() +void Resource::setOptional(bool resourceIsOptional) { - return false; + optional = resourceIsOptional; } -bool Resource::unsetMute() +bool Resource::isShared() const { - return false; + return shared; } -bool Resource::requestMute() +void Resource::setShared(bool shared) { - return false; + this->shared = shared; } -bool Resource::setPrivacyOverride() +quint32 Resource::id() const { - return false; + return identifier; } -bool Resource::unsetPrivacyOverride() +void Resource::setId(quint32 newId) { - return false; + identifier = newId; } -bool Resource::requestPrivacyOverride() +bool Resource::operator==(const Resource &other) { - return false; -} - -bool Resource::setBluetoothOverride() -{ - return false; -} - -bool Resource::unsetBluetoothOverride() -{ - return false; -} - -bool Resource::requestBluetoothOverride() -{ - return false; -} - -bool Resource::setPid(quint32 pid) -{ - return false; -} - -bool Resource::setStreamName(const QString & name) -{ - return false; + if(name != other.name) { + return false; + } + if((identifier != other.identifier) or + (shared != other.shared) or + (optional != other.optional)) + { + return false; + } + return true; } - diff --git a/tests/Makefile b/tests/Makefile index 8c9ab42..20ae9ef 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,5 +1,6 @@ QMAKE = qmake MAKEFILES = $(patsubst %.pro,%.make,$(wildcard *.pro)) +MAKEOVERRIDES = %.make: %.pro $(QMAKE) -o $@ $< diff --git a/tests/test-resource.cpp b/tests/test-resource.cpp index 5064df9..bc72957 100644 --- a/tests/test-resource.cpp +++ b/tests/test-resource.cpp @@ -1,9 +1,6 @@ #include "test-resource.h" -using namespace ResourceTypes; - TestResource::TestResource() - : isReservable(false), isReserved(false) { } @@ -13,147 +10,129 @@ TestResource::~TestResource() void TestResource::init() { - resource = new Resource(MediaClass, AudioResource|VideoResource, this); - resourceLibrary = new MockResourceLibrary(resource); - QVERIFY(resource != NULL); - QVERIFY(resource->applicationClass() == MediaClass); - QVERIFY(resource->resources() == (AudioResource|VideoResource)); - QVERIFY(resource->isReserved() == false); - QVERIFY(resource->hasResource(AudioResource)); - QVERIFY(resource->hasResource(VideoResource)); - isReservable =false; - isReserved = false; + resource = new Resource(); } -void TestResource::testApplicationClass() +void TestResource::cleanup() { - enum ResourceClass expected, actual; - - expected = MediaClass; - actual = resource->applicationClass(); - - QVERIFY(actual == expected); + delete resource; } -void TestResource::testResources() +void TestResource::testType_data() { - quint16 expected, actual; - - expected = AudioResource|VideoResource; - actual = resource->resources(); - - QVERIFY(actual == expected); + QTest::addColumn<QString>("type"); + QTest::addColumn<QString>("expected"); + + QTest::newRow("AudioPlayback") << "AudioPlayback" << "AudioPlayback"; + QTest::newRow("VideoPlayback") << "VideoPlayback" << "VideoPlayback"; + QTest::newRow("AudioRecording") << "AudioRecording" << "AudioRecording"; + QTest::newRow("VideoRecording") << "VideoRecording" << "VideoRecording"; + QTest::newRow("Invalid") << "Invalid" << "Invalid"; + QTest::newRow("Empty") << "" << ""; + QTest::newRow("Null") << QString() << QString(); } - -void TestResource::testInitializeSucceeds() +void TestResource::testType() { - bool initializeSucceeded = resource->initialize(resourceLibrary); + QFETCH(QString, type); + QFETCH(QString, expected); + resource->setType(type); + QString result = resource->type(); - QVERIFY(resource->resourceLibrary == resourceLibrary); - QVERIFY(initializeSucceeded); + QCOMPARE(result, expected); } -void TestResource::testInitializeFails() +void TestResource::testOptional_data() { - MockResourceLibrary *mockResourceLibrary = - static_cast<MockResourceLibrary *>(resourceLibrary); - mockResourceLibrary->makeInitializeFail(); - - bool initializeSucceeded = resource->initialize(resourceLibrary); - - QEXPECT_FAIL("", "Expecting resourceLibrary->initialize() to fail", Continue); - QVERIFY(initializeSucceeded); + QTest::addColumn<bool>("optional"); + QTest::addColumn<bool>("expected"); + + QTest::newRow("Resource is optional") << true << true; + QTest::newRow("Resource is not optional") << false << false; } -void TestResource::testConnectToServerSucceeds() +void TestResource::testOptional() { - bool initializeSucceeded = resource->initialize(resourceLibrary); + QFETCH(bool, optional); + QFETCH(bool, expected); - QVERIFY(resource->resourceLibrary == resourceLibrary); - QVERIFY(initializeSucceeded); + resource->setOptional(optional); + bool result = resource->isOptional(); - bool connectToServerSucceeded = resource->connectToServer(); - QVERIFY(connectToServerSucceeded); + QCOMPARE(result, expected); } -void TestResource::testConnectToServerFails() +void TestResource::testShared_data() { - MockResourceLibrary *mockResourceLibrary = - static_cast<MockResourceLibrary *>(resourceLibrary); - - bool initializeSucceeded = resource->initialize(resourceLibrary); - - QVERIFY(resource->resourceLibrary == resourceLibrary); - QVERIFY(initializeSucceeded); - - mockResourceLibrary->makeServerConnectFail(); - bool connectToServerSucceeded = resource->connectToServer(); - - QEXPECT_FAIL("", "Expecting resourceLibrary->connectToServer() to fail", Continue); - QVERIFY(connectToServerSucceeded); + QTest::addColumn<bool>("optional"); + QTest::addColumn<bool>("expected"); + + QTest::newRow("Resource is shared") << true << true; + QTest::newRow("Resource is not hhared") << false << false; } -// testStateChanged - -void TestResource::testReservable() +void TestResource::testShared() { - resource->initialize(resourceLibrary); - resource->connectToServer(); + QFETCH(bool, optional); + QFETCH(bool, expected); - QObject::connect(resource, SIGNAL(reservable()), this, SLOT(handleReservable())); + resource->setShared(optional); + bool result = resource->isShared(); - resource->emitReservable(); - - QVERIFY(isReservable); + QCOMPARE(result, expected); } -void TestResource::handleReservable() +void TestResource::testIdentifier_data() { - isReservable = true; + QTest::addColumn<quint32>("identifier"); + QTest::addColumn<quint32>("expected"); + + QTest::newRow("Set identifier") << (quint32)this << (quint32)this; + QTest::newRow("Set to 0") << 0U << 0U; } -void TestResource::testReserve() +void TestResource::testIdentifier() { - resource->initialize(resourceLibrary); - QObject::connect(resource, SIGNAL(stateChanged(enum ResourceState)), - this, SLOT(handleStateChanged(enum ResourceState))); - resource->connectToServer(); + QFETCH(quint32, identifier); + QFETCH(quint32, expected); - bool reserveSucceeds = resource->reserve(); + resource->setId(identifier); + quint32 result = resource->id(); - QVERIFY(resource->isReserved()); - QVERIFY(reserveSucceeds); - QVERIFY(isReserved); + QCOMPARE(result, expected); } -void TestResource::testReserveExpectFail() +void TestResource::testCopy() { - MockResourceLibrary *mockResourceLibrary = - static_cast<MockResourceLibrary *>(resourceLibrary); + Resource copy; + resource->setOptional(); - resource->initialize(resourceLibrary); - QObject::connect(resource, SIGNAL(stateChanged(enum ResourceState)), - this, SLOT(handleStateChanged(enum ResourceState))); - resource->connectToServer(); + copy = *resource; - mockResourceLibrary->makeReserveFail(); - bool reserveSucceeds = resource->reserve(); + QCOMPARE(copy.id(), resource->id()); + QCOMPARE(copy.isOptional(), resource->isOptional()); + QCOMPARE(copy.isShared(), resource->isShared()); + QCOMPARE(copy.type(), resource->type()); +} - QVERIFY(reserveSucceeds); - QEXPECT_FAIL("", "Expecting resourceLibrary->reserve() to fail", Continue); - QVERIFY(isReserved); +void TestResource::testCopyConstructor() +{ + resource->setOptional(); + Resource copy(*resource); + + QCOMPARE(copy.id(), resource->id()); + QCOMPARE(copy.isOptional(), resource->isOptional()); + QCOMPARE(copy.isShared(), resource->isShared()); + QCOMPARE(copy.type(), resource->type()); } -void TestResource::handleStateChanged(enum ResourceState newState) +void TestResource::testEqualsOperator() { - switch(newState) { - case OwnedState: - isReserved = true; - break; - default: - isReserved = false; - break; - } + Resource copy; + resource->setOptional(); + + copy = *resource; + + QVERIFY(copy == *resource); } QTEST_MAIN(TestResource) diff --git a/tests/test-resource.h b/tests/test-resource.h index a762a8a..e55e2af 100644 --- a/tests/test-resource.h +++ b/tests/test-resource.h @@ -3,41 +3,39 @@ #include <QtTest/QTest> #include "resource.h" -#include "mock-resource-library.h" class TestResource: public QObject { Q_OBJECT private: - ResourceLibrary *resourceLibrary; Resource *resource; - bool isReservable; - bool isReserved; public: TestResource(); ~TestResource(); -public slots: - void handleReservable(); - void handleStateChanged(enum ResourceState newState); private slots: + void init(); + void cleanup(); - void testInitializeSucceeds(); - void testInitializeFails(); + void testType_data(); + void testType(); + + void testOptional_data(); + void testOptional(); - void testConnectToServerSucceeds(); - void testConnectToServerFails(); + void testShared_data(); + void testShared(); - void testApplicationClass(); - void testResources(); + void testIdentifier_data(); + void testIdentifier(); - void testReservable(); + void testCopy(); + void testCopyConstructor(); - void testReserve(); - void testReserveExpectFail(); + void testEqualsOperator(); }; #endif diff --git a/tests/test-resource.pro b/tests/test-resource.pro index 3271864..bb4da4c 100644 --- a/tests/test-resource.pro +++ b/tests/test-resource.pro @@ -6,8 +6,8 @@ DEPENDPATH += $${BASE}/include $${BASE}/src . INCLUDEPATH += $${BASE}/src $${BASE}/include # Input -HEADERS += $${BASE}/include/resource.h test-resource.h mock-resource-library.h -SOURCES += $${BASE}/src/resource.cpp test-resource.cpp mock-resource-library.cpp +HEADERS += $${BASE}/include/resource.h test-resource.h +SOURCES += $${BASE}/src/resource.cpp test-resource.cpp OBJECTS_DIR = build MOC_DIR = build |