/***********************************************************************
* fcallback-test.cpp - FCallback unit tests *
* *
* This file is part of the FINAL CUT widget toolkit *
* *
* Copyright 2020 Markus Gans *
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of *
* the License, or (at your option) any later version. *
* *
* FINAL CUT 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 Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* . *
***********************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
//----------------------------------------------------------------------
// class Widget
//----------------------------------------------------------------------
class Widget
{
public:
template
void addCallback (const finalcut::FString& cb_signal, Args&&... args)
{
cb.addCallback (cb_signal, std::forward(args)...);
}
template
void delCallback (Args&&... args)
{
cb.delCallback (std::forward(args)...);
}
void emitCallback (const finalcut::FString& emit_signal)
{
cb.emitCallback (emit_signal);
}
private:
finalcut::FCallback cb{};
};
//----------------------------------------------------------------------
// class cb_class
//----------------------------------------------------------------------
class cb_class final : public finalcut::FWidget
{
public:
cb_class (int i, finalcut::FWidget* parent)
: finalcut::FWidget{parent}
, data{i}
{ }
void cb_method_ptr (int* value)
{
(*value)--;
}
void cb_method_ptr_const (int* value) const
{
*value -= 4;
}
void cb_method_ref (int& value)
{
value -= data;
}
void cb_method_ref_const (int& value) const
{
value -= (2 * data);
}
private:
int data;
};
//----------------------------------------------------------------------
// functions
//----------------------------------------------------------------------
void cb_function_ptr (int* value)
{
(*value)++;
}
//----------------------------------------------------------------------
void cb_function_ref (int& value)
{
value += 2;
}
//----------------------------------------------------------------------
// class FCallbackTest
//----------------------------------------------------------------------
class FCallbackTest : public CPPUNIT_NS::TestFixture
{
public:
FCallbackTest()
{ }
protected:
void classNameTest();
void memberFunctionPointerCallbackTest();
void instanceWithFunctionObjectCallbackTest();
void functionObjectCallbackTest();
void functionObjectReferenceCallbackTest();
void functionReferenceCallbackTest();
void functionPointerCallbackTest();
void ownWidgetTest();
private:
// Adds code needed to register the test suite
CPPUNIT_TEST_SUITE (FCallbackTest);
// Add a methods to the test suite
CPPUNIT_TEST (classNameTest);
CPPUNIT_TEST (memberFunctionPointerCallbackTest);
CPPUNIT_TEST (instanceWithFunctionObjectCallbackTest);
CPPUNIT_TEST (functionObjectCallbackTest);
CPPUNIT_TEST (functionObjectReferenceCallbackTest);
CPPUNIT_TEST (functionReferenceCallbackTest);
CPPUNIT_TEST (functionPointerCallbackTest);
CPPUNIT_TEST (ownWidgetTest);
// End of test suite definition
CPPUNIT_TEST_SUITE_END();
// Data member
static finalcut::FWidget root_widget;
};
// static class attributes
finalcut::FWidget FCallbackTest::root_widget{nullptr};
//----------------------------------------------------------------------
void FCallbackTest::classNameTest()
{
const finalcut::FCallback cb;
const finalcut::FString& classname = cb.getClassName();
CPPUNIT_ASSERT ( classname == "FCallback" );
}
//----------------------------------------------------------------------
void FCallbackTest::memberFunctionPointerCallbackTest()
{
finalcut::FCallback cb{};
cb_class c{5, &root_widget};
int i{75};
using Object = decltype(&c);
using MemberFunctionPointer = decltype(&cb_class::cb_method_ptr);
CPPUNIT_ASSERT ( 0 == std::is_member_function_pointer