2012-03-19 18:38:38 +01:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2014-02-06 18:24:46 +01:00
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
|
2012-03-19 18:38:38 +01:00
|
|
|
#include <QCoreApplication>
|
2012-03-19 19:36:52 +01:00
|
|
|
#include <QPointer>
|
|
|
|
#include <QSharedPointer>
|
2012-03-19 18:38:38 +01:00
|
|
|
#include <QSignalSpy>
|
|
|
|
|
2012-11-26 15:36:05 +01:00
|
|
|
#include "config.h"
|
2012-03-19 18:38:38 +01:00
|
|
|
#include "core/closure.h"
|
|
|
|
#include "test_utils.h"
|
|
|
|
|
2012-04-18 14:37:40 +02:00
|
|
|
TEST(ClosureTest, ClosureInvokesReceiver) {
|
2012-03-19 18:38:38 +01:00
|
|
|
TestQObject sender;
|
|
|
|
TestQObject receiver;
|
2012-11-22 15:15:07 +01:00
|
|
|
_detail::ClosureBase* closure = NewClosure(
|
2012-03-19 18:38:38 +01:00
|
|
|
&sender, SIGNAL(Emitted()),
|
|
|
|
&receiver, SLOT(Invoke()));
|
|
|
|
EXPECT_EQ(0, receiver.invoked());
|
|
|
|
sender.Emit();
|
|
|
|
EXPECT_EQ(1, receiver.invoked());
|
|
|
|
}
|
|
|
|
|
2012-04-18 14:37:40 +02:00
|
|
|
TEST(ClosureTest, ClosureDeletesSelf) {
|
2012-03-19 18:38:38 +01:00
|
|
|
TestQObject sender;
|
|
|
|
TestQObject receiver;
|
2012-11-22 15:15:07 +01:00
|
|
|
_detail::ClosureBase* closure = NewClosure(
|
2012-03-19 18:38:38 +01:00
|
|
|
&sender, SIGNAL(Emitted()),
|
|
|
|
&receiver, SLOT(Invoke()));
|
2012-11-22 15:15:07 +01:00
|
|
|
_detail::ObjectHelper* helper = closure->helper();
|
|
|
|
QSignalSpy spy(helper, SIGNAL(destroyed()));
|
2012-03-19 18:38:38 +01:00
|
|
|
EXPECT_EQ(0, receiver.invoked());
|
|
|
|
sender.Emit();
|
|
|
|
EXPECT_EQ(1, receiver.invoked());
|
|
|
|
|
|
|
|
EXPECT_EQ(0, spy.count());
|
|
|
|
QEventLoop loop;
|
2012-11-22 15:15:07 +01:00
|
|
|
QObject::connect(helper, SIGNAL(destroyed()), &loop, SLOT(quit()));
|
2012-03-19 18:38:38 +01:00
|
|
|
loop.exec();
|
|
|
|
EXPECT_EQ(1, spy.count());
|
|
|
|
}
|
2012-03-19 19:36:52 +01:00
|
|
|
|
2012-04-18 14:37:40 +02:00
|
|
|
TEST(ClosureTest, ClosureDoesNotCrashWithSharedPointerSender) {
|
2012-03-19 19:36:52 +01:00
|
|
|
TestQObject receiver;
|
|
|
|
TestQObject* sender;
|
2014-02-06 18:24:46 +01:00
|
|
|
std::unique_ptr<QSignalSpy> spy;
|
2012-11-22 15:15:07 +01:00
|
|
|
QPointer<_detail::ObjectHelper> closure;
|
2012-03-19 19:36:52 +01:00
|
|
|
{
|
|
|
|
QSharedPointer<TestQObject> sender_shared(new TestQObject);
|
|
|
|
sender = sender_shared.data();
|
2012-11-22 15:15:07 +01:00
|
|
|
closure = QPointer<_detail::ObjectHelper>(NewClosure(
|
2012-03-19 19:36:52 +01:00
|
|
|
sender_shared, SIGNAL(Emitted()),
|
2012-11-22 15:15:07 +01:00
|
|
|
&receiver, SLOT(Invoke()))->helper());
|
2012-03-19 19:36:52 +01:00
|
|
|
spy.reset(new QSignalSpy(sender, SIGNAL(destroyed())));
|
|
|
|
}
|
2012-11-22 15:15:07 +01:00
|
|
|
ASSERT_EQ(0, receiver.invoked());
|
2012-03-19 19:36:52 +01:00
|
|
|
sender->Emit();
|
2012-11-22 15:15:07 +01:00
|
|
|
ASSERT_EQ(1, receiver.invoked());
|
2012-03-19 19:36:52 +01:00
|
|
|
|
2012-11-22 15:15:07 +01:00
|
|
|
ASSERT_EQ(0, spy->count());
|
2012-03-19 19:36:52 +01:00
|
|
|
QEventLoop loop;
|
|
|
|
QObject::connect(sender, SIGNAL(destroyed()), &loop, SLOT(quit()));
|
|
|
|
loop.exec();
|
2012-11-22 15:15:07 +01:00
|
|
|
ASSERT_EQ(1, spy->count());
|
2012-03-19 19:36:52 +01:00
|
|
|
EXPECT_TRUE(closure.isNull());
|
|
|
|
}
|
2012-11-26 09:41:26 +01:00
|
|
|
|
2012-12-13 16:13:38 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void Foo(bool* called, int question, int* answer) {
|
|
|
|
*called = true;
|
|
|
|
*answer = question;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(ClosureTest, ClosureWorksWithFunctionPointers) {
|
|
|
|
TestQObject sender;
|
|
|
|
bool called = false;
|
|
|
|
int question = 42;
|
|
|
|
int answer = 0;
|
2012-12-13 16:27:00 +01:00
|
|
|
NewClosure(
|
2012-12-13 16:13:38 +01:00
|
|
|
&sender, SIGNAL(Emitted()),
|
|
|
|
&Foo, &called, question, &answer);
|
|
|
|
EXPECT_FALSE(called);
|
|
|
|
sender.Emit();
|
|
|
|
EXPECT_TRUE(called);
|
|
|
|
EXPECT_EQ(question, answer);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ClosureTest, ClosureWorksWithStandardFunctions) {
|
|
|
|
TestQObject sender;
|
|
|
|
bool called = false;
|
|
|
|
int question = 42;
|
|
|
|
int answer = 0;
|
2014-02-06 18:24:46 +01:00
|
|
|
std::function<void(bool*,int,int*)> callback(&Foo);
|
2012-12-13 16:27:00 +01:00
|
|
|
NewClosure(
|
2012-12-13 16:13:38 +01:00
|
|
|
&sender, SIGNAL(Emitted()),
|
|
|
|
callback, &called, question, &answer);
|
|
|
|
EXPECT_FALSE(called);
|
|
|
|
sender.Emit();
|
|
|
|
EXPECT_TRUE(called);
|
|
|
|
EXPECT_EQ(question, answer);
|
|
|
|
}
|
|
|
|
|
2012-12-13 16:27:00 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class Bar {
|
|
|
|
public:
|
|
|
|
explicit Bar(int a) : foo_(a) {}
|
2012-12-13 16:55:23 +01:00
|
|
|
bool Foo(int* answer) {
|
2012-12-13 16:27:00 +01:00
|
|
|
*answer = foo_;
|
2012-12-13 16:55:23 +01:00
|
|
|
return true;
|
2012-12-13 16:27:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int foo_;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ClosureTest, ClosureWorksWithMemberFunctionPointers) {
|
|
|
|
TestQObject sender;
|
|
|
|
Bar receiver(42);
|
|
|
|
int q = 1;
|
|
|
|
NewClosure(
|
|
|
|
&sender, SIGNAL(Emitted()),
|
|
|
|
&receiver, &Bar::Foo, &q);
|
|
|
|
EXPECT_EQ(1, q);
|
|
|
|
sender.Emit();
|
|
|
|
EXPECT_EQ(42, q);
|
|
|
|
}
|
|
|
|
|
2012-11-26 09:41:26 +01:00
|
|
|
TEST(ClosureTest, ClosureCallsLambda) {
|
|
|
|
TestQObject sender;
|
|
|
|
bool called = false;
|
2012-12-13 16:27:00 +01:00
|
|
|
NewClosure(
|
2012-11-26 09:41:26 +01:00
|
|
|
&sender, SIGNAL(Emitted()),
|
|
|
|
[&called] () { called = true; });
|
|
|
|
EXPECT_FALSE(called);
|
|
|
|
sender.Emit();
|
|
|
|
EXPECT_TRUE(called);
|
|
|
|
}
|