Add small unit test for closure.

This commit is contained in:
John Maguire 2012-03-19 18:38:38 +01:00
parent 9a17356389
commit 1a4bfd3ebe
4 changed files with 74 additions and 0 deletions

View File

@ -63,6 +63,7 @@ set(TESTUTILS-SOURCES
set(TESTUTILS-MOC-HEADERS
mock_networkaccessmanager.h
test_utils.h
testobjectdecorators.h
)
@ -138,6 +139,7 @@ add_test_file(song_test.cpp false)
add_test_file(translations_test.cpp false)
add_test_file(utilities_test.cpp false)
#add_test_file(xspfparser_test.cpp false)
add_test_file(closure_test.cpp false)
#if(LINUX AND HAVE_DBUS)
# add_test_file(mpris1_test.cpp true)

40
tests/closure_test.cpp Normal file
View File

@ -0,0 +1,40 @@
#include "gtest/gtest.h"
#include <QCoreApplication>
#include <QSignalSpy>
#include "core/closure.h"
#include "test_utils.h"
class ClosureTest : public ::testing::Test {
};
TEST_F(ClosureTest, ClosureInvokesReceiver) {
TestQObject sender;
TestQObject receiver;
Closure* closure = NewClosure(
&sender, SIGNAL(Emitted()),
&receiver, SLOT(Invoke()));
EXPECT_EQ(0, receiver.invoked());
sender.Emit();
EXPECT_EQ(1, receiver.invoked());
}
TEST_F(ClosureTest, ClosureDeletesSelf) {
TestQObject sender;
TestQObject receiver;
Closure* closure = NewClosure(
&sender, SIGNAL(Emitted()),
&receiver, SLOT(Invoke()));
QSignalSpy spy(closure, SIGNAL(destroyed()));
EXPECT_EQ(0, receiver.invoked());
sender.Emit();
EXPECT_EQ(1, receiver.invoked());
EXPECT_EQ(0, spy.count());
QEventLoop loop;
QObject::connect(closure, SIGNAL(destroyed()), &loop, SLOT(quit()));
loop.exec();
EXPECT_EQ(1, spy.count());
}

View File

@ -65,3 +65,16 @@ TemporaryResource::TemporaryResource(const QString& filename) {
reset();
}
TestQObject::TestQObject(QObject* parent)
: QObject(parent),
invoked_(0) {
}
void TestQObject::Emit() {
emit Emitted();
}
void TestQObject::Invoke() {
++invoked_;
}

View File

@ -62,4 +62,23 @@ public:
TemporaryResource(const QString& filename);
};
class TestQObject : public QObject {
Q_OBJECT
public:
TestQObject(QObject* parent = 0);
void Emit();
int invoked() const { return invoked_; }
signals:
void Emitted();
public slots:
void Invoke();
private:
int invoked_;
};
#endif // TEST_UTILS_H