mirror of
https://github.com/clementine-player/Clementine
synced 2024-12-17 20:09:50 +01:00
Add small unit test for closure.
This commit is contained in:
parent
9a17356389
commit
1a4bfd3ebe
@ -63,6 +63,7 @@ set(TESTUTILS-SOURCES
|
|||||||
|
|
||||||
set(TESTUTILS-MOC-HEADERS
|
set(TESTUTILS-MOC-HEADERS
|
||||||
mock_networkaccessmanager.h
|
mock_networkaccessmanager.h
|
||||||
|
test_utils.h
|
||||||
testobjectdecorators.h
|
testobjectdecorators.h
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -138,6 +139,7 @@ add_test_file(song_test.cpp false)
|
|||||||
add_test_file(translations_test.cpp false)
|
add_test_file(translations_test.cpp false)
|
||||||
add_test_file(utilities_test.cpp false)
|
add_test_file(utilities_test.cpp false)
|
||||||
#add_test_file(xspfparser_test.cpp false)
|
#add_test_file(xspfparser_test.cpp false)
|
||||||
|
add_test_file(closure_test.cpp false)
|
||||||
|
|
||||||
#if(LINUX AND HAVE_DBUS)
|
#if(LINUX AND HAVE_DBUS)
|
||||||
# add_test_file(mpris1_test.cpp true)
|
# add_test_file(mpris1_test.cpp true)
|
||||||
|
40
tests/closure_test.cpp
Normal file
40
tests/closure_test.cpp
Normal 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());
|
||||||
|
}
|
@ -65,3 +65,16 @@ TemporaryResource::TemporaryResource(const QString& filename) {
|
|||||||
|
|
||||||
reset();
|
reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TestQObject::TestQObject(QObject* parent)
|
||||||
|
: QObject(parent),
|
||||||
|
invoked_(0) {
|
||||||
|
}
|
||||||
|
|
||||||
|
void TestQObject::Emit() {
|
||||||
|
emit Emitted();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TestQObject::Invoke() {
|
||||||
|
++invoked_;
|
||||||
|
}
|
||||||
|
@ -62,4 +62,23 @@ public:
|
|||||||
TemporaryResource(const QString& filename);
|
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
|
#endif // TEST_UTILS_H
|
||||||
|
Loading…
Reference in New Issue
Block a user