finalcut/test/fobject-test.cpp

591 lines
17 KiB
C++
Raw Normal View History

2018-03-14 00:53:28 +01:00
/***********************************************************************
2018-03-28 00:03:57 +02:00
* fobject-test.cpp - FPoint unit tests *
2018-03-14 00:53:28 +01:00
* *
* This file is part of the Final Cut widget toolkit *
* *
* Copyright 2018-2019 Markus Gans *
2018-03-14 00:53:28 +01:00
* *
* The 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. *
* *
* The 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 *
* <http://www.gnu.org/licenses/>. *
***********************************************************************/
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/TestFixture.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>
#include <final/final.h>
namespace test
{
2018-03-14 23:56:44 +01:00
//----------------------------------------------------------------------
// class FObject_protected
//----------------------------------------------------------------------
class FObject_protected : public finalcut::FObject
2018-03-14 23:56:44 +01:00
{
public:
2018-09-27 03:53:51 +02:00
FObject_protected()
: count(0)
{ }
bool event (finalcut::FEvent* ev)
2018-03-14 23:56:44 +01:00
{
return finalcut::FObject::event(ev);
2018-03-14 23:56:44 +01:00
}
2018-03-16 01:05:45 +01:00
TimerList* getTimerList() const
2018-03-16 01:05:45 +01:00
{
return finalcut::FObject::getTimerList();
2018-03-16 01:05:45 +01:00
}
2018-09-27 03:53:51 +02:00
uInt processEvent()
{
return processTimerEvent();
}
void setWidgetProperty (bool property)
{
finalcut::FObject::setWidgetProperty (property);
}
bool isWidget()
{
return finalcut::FObject::isWidget();
}
2018-09-27 03:53:51 +02:00
virtual void performTimerAction (const FObject*, const finalcut::FEvent*)
{
std::cout << ".";
fflush(stdout);
count++;
}
// Data member
2018-09-27 03:53:51 +02:00
uInt count;
2018-03-14 23:56:44 +01:00
};
//----------------------------------------------------------------------
class FObject_timer : public finalcut::FObject
{
public:
FObject_timer()
{ }
int getValue() const
{
return value;
}
protected:
virtual void onTimer (finalcut::FTimerEvent* ev)
{
if ( ev->getTimerId() == 1 )
value++;
}
private:
// Data member
int value{0};
};
//----------------------------------------------------------------------
class FObject_userEvent : public finalcut::FObject
{
public:
FObject_userEvent()
{ }
int getValue() const
{
return value;
}
protected:
virtual void onUserEvent (finalcut::FUserEvent* ev)
{
if ( ev->getUserId() == 42 )
value = *(static_cast<int*>(ev->getData()));
}
private:
// Data member
int value{0};
};
} // namespace test
2018-03-14 23:56:44 +01:00
2018-03-14 00:53:28 +01:00
//----------------------------------------------------------------------
// class FObjectTest
//----------------------------------------------------------------------
class FObjectTest : public CPPUNIT_NS::TestFixture
{
public:
FObjectTest()
{ }
protected:
void classNameTest();
2018-03-18 15:03:07 +01:00
void noArgumentTest();
2018-03-14 23:56:44 +01:00
void childObjectTest();
void widgetObjectTest();
2018-03-14 23:56:44 +01:00
void removeParentTest();
void addTest();
void delTest();
2018-03-16 01:05:45 +01:00
void iteratorTest();
void timeTest();
void timerTest();
2018-09-27 03:53:51 +02:00
void performTimerActionTest();
void userEventTest();
2018-03-14 00:53:28 +01:00
private:
// Adds code needed to register the test suite
CPPUNIT_TEST_SUITE (FObjectTest);
// Add a methods to the test suite
CPPUNIT_TEST (classNameTest);
2018-03-18 15:03:07 +01:00
CPPUNIT_TEST (noArgumentTest);
2018-03-14 23:56:44 +01:00
CPPUNIT_TEST (childObjectTest);
CPPUNIT_TEST (widgetObjectTest);
2018-03-14 23:56:44 +01:00
CPPUNIT_TEST (removeParentTest);
CPPUNIT_TEST (addTest);
CPPUNIT_TEST (delTest);
2018-03-16 01:05:45 +01:00
CPPUNIT_TEST (iteratorTest);
CPPUNIT_TEST (timeTest);
CPPUNIT_TEST (timerTest);
2018-09-27 03:53:51 +02:00
CPPUNIT_TEST (performTimerActionTest);
CPPUNIT_TEST (userEventTest);
2018-03-14 00:53:28 +01:00
// End of test suite definition
CPPUNIT_TEST_SUITE_END();
};
//----------------------------------------------------------------------
void FObjectTest::classNameTest()
{
finalcut::FObject o;
2019-10-05 23:20:07 +02:00
const finalcut::FString& classname = o.getClassName();
CPPUNIT_ASSERT ( classname == "FObject" );
2018-03-14 00:53:28 +01:00
}
//----------------------------------------------------------------------
2018-03-18 15:03:07 +01:00
void FObjectTest::noArgumentTest()
2018-03-14 00:53:28 +01:00
{
finalcut::FObject o1;
2018-10-22 01:26:24 +02:00
finalcut::FObject o2;
2018-03-14 00:53:28 +01:00
CPPUNIT_ASSERT ( ! o1.hasParent() );
CPPUNIT_ASSERT ( o1.getParent() == 0 );
CPPUNIT_ASSERT ( ! o1.hasChildren() );
CPPUNIT_ASSERT ( o1.getChild(0) == 0 );
CPPUNIT_ASSERT ( o1.getChild(1) == 0 );
CPPUNIT_ASSERT ( o1.numOfChildren() == 0 );
auto& children_list = o1.getChildren();
2018-03-14 00:53:28 +01:00
CPPUNIT_ASSERT ( children_list.begin() == o1.begin() );
CPPUNIT_ASSERT ( children_list.begin() == o1.end() );
CPPUNIT_ASSERT ( children_list.end() == o1.begin() );
CPPUNIT_ASSERT ( children_list.end() == o1.end() );
2018-10-22 01:26:24 +02:00
CPPUNIT_ASSERT ( ! o1.isChild(&o2) );
CPPUNIT_ASSERT ( ! o1.isDirectChild(&o2) );
2018-03-14 00:53:28 +01:00
CPPUNIT_ASSERT ( ! o1.isWidget() );
CPPUNIT_ASSERT ( o1.isInstanceOf("FObject") );
CPPUNIT_ASSERT ( ! o1.isTimerInUpdating() );
test::FObject_protected t;
auto ev = new finalcut::FEvent(finalcut::fc::None_Event);
2018-03-14 23:56:44 +01:00
CPPUNIT_ASSERT ( ! t.event(ev) );
2018-03-14 00:53:28 +01:00
delete ev;
2018-03-16 01:05:45 +01:00
ev = new finalcut::FEvent(finalcut::fc::Timer_Event);
2018-03-16 01:05:45 +01:00
CPPUNIT_ASSERT ( t.event(ev) );
delete ev;
CPPUNIT_ASSERT ( ! finalcut::fc::emptyFString::get().isNull() );
CPPUNIT_ASSERT ( finalcut::fc::emptyFString::get().isEmpty() );
2018-03-14 00:53:28 +01:00
}
//----------------------------------------------------------------------
2018-03-14 23:56:44 +01:00
void FObjectTest::childObjectTest()
2019-10-05 23:20:07 +02:00
{
// obj -> c1 -> c5 -> c6
// -> c2
// -> c3
// -> c4
finalcut::FObject obj;
auto c1 = new finalcut::FObject(&obj);
auto c2 = new finalcut::FObject(&obj);
auto c3 = new finalcut::FObject(&obj);
auto c4 = new finalcut::FObject(&obj);
auto c5 = new finalcut::FObject(c1);
auto c6 = new finalcut::FObject(c5);
auto c7 = new finalcut::FObject();
2018-03-14 23:56:44 +01:00
CPPUNIT_ASSERT ( obj.hasChildren() );
CPPUNIT_ASSERT ( obj.getChild(0) == 0 );
CPPUNIT_ASSERT ( obj.getChild(1) != 0 );
CPPUNIT_ASSERT ( obj.numOfChildren() == 4 );
CPPUNIT_ASSERT ( obj.isChild(c1) );
CPPUNIT_ASSERT ( obj.isChild(c2) );
CPPUNIT_ASSERT ( obj.isChild(c3) );
CPPUNIT_ASSERT ( obj.isChild(c4) );
CPPUNIT_ASSERT ( obj.isChild(c5) );
CPPUNIT_ASSERT ( obj.isChild(c6) );
2018-03-14 00:53:28 +01:00
2018-03-14 23:56:44 +01:00
CPPUNIT_ASSERT ( obj.isDirectChild(c1) );
CPPUNIT_ASSERT ( obj.isDirectChild(c2) );
CPPUNIT_ASSERT ( obj.isDirectChild(c3) );
CPPUNIT_ASSERT ( obj.isDirectChild(c4) );
CPPUNIT_ASSERT ( ! obj.isDirectChild(c5) );
CPPUNIT_ASSERT ( c1->isDirectChild(c5) );
CPPUNIT_ASSERT ( ! obj.isDirectChild(c6) );
CPPUNIT_ASSERT ( ! c1->isDirectChild(c6) );
CPPUNIT_ASSERT ( c5->isDirectChild(c6) );
CPPUNIT_ASSERT ( c1->hasParent() );
CPPUNIT_ASSERT ( c1->getParent() == &obj );
CPPUNIT_ASSERT ( c1->hasChildren() );
CPPUNIT_ASSERT ( ! c2->hasChildren() );
CPPUNIT_ASSERT ( c1->getChild(0) == 0 );
CPPUNIT_ASSERT ( c1->getChild(1) != 0 );
CPPUNIT_ASSERT ( c2->getChild(1) == 0 );
CPPUNIT_ASSERT ( c1->numOfChildren() == 1 );
CPPUNIT_ASSERT ( c2->numOfChildren() == 0 );
const auto& children_list2 = c1->getChildren();
2018-03-14 23:56:44 +01:00
CPPUNIT_ASSERT ( children_list2.begin() == c1->begin() );
CPPUNIT_ASSERT ( children_list2.begin() != c1->end() );
CPPUNIT_ASSERT ( children_list2.end() != c1->begin() );
CPPUNIT_ASSERT ( children_list2.end() == c1->end() );
2018-10-22 01:26:24 +02:00
CPPUNIT_ASSERT ( ! c1->isDirectChild(c7) );
2018-03-14 23:56:44 +01:00
CPPUNIT_ASSERT ( ! c1->isWidget() );
CPPUNIT_ASSERT ( c1->isInstanceOf("FObject") );
CPPUNIT_ASSERT ( ! c1->isTimerInUpdating() );
2018-03-14 00:53:28 +01:00
}
//----------------------------------------------------------------------
void FObjectTest::widgetObjectTest()
{
test::FObject_protected o;
CPPUNIT_ASSERT ( ! o.isWidget() );
o.setWidgetProperty (true);
CPPUNIT_ASSERT ( o.isWidget() );
o.setWidgetProperty (false);
CPPUNIT_ASSERT ( ! o.isWidget() );
}
2018-03-14 00:53:28 +01:00
//----------------------------------------------------------------------
2018-03-14 23:56:44 +01:00
void FObjectTest::removeParentTest()
2019-10-05 23:20:07 +02:00
{
// obj -> child
auto obj = new finalcut::FObject();
auto child = new finalcut::FObject(obj);
2018-03-14 23:56:44 +01:00
CPPUNIT_ASSERT ( obj->hasChildren() );
CPPUNIT_ASSERT ( obj->numOfChildren() == 1 );
CPPUNIT_ASSERT ( obj->isChild(child) );
CPPUNIT_ASSERT ( child->hasParent() );
CPPUNIT_ASSERT ( child->getParent() == obj );
child->removeParent();
CPPUNIT_ASSERT ( ! obj->hasChildren() );
CPPUNIT_ASSERT ( obj->numOfChildren() == 0 );
CPPUNIT_ASSERT ( ! obj->isChild(child) );
CPPUNIT_ASSERT ( ! child->hasParent() );
CPPUNIT_ASSERT ( child->getParent() != obj );
delete child;
delete obj; // also deletes the child object
}
//----------------------------------------------------------------------
void FObjectTest::addTest()
2019-10-05 23:20:07 +02:00
{
// obj -> child
auto obj = new finalcut::FObject();
auto child = new finalcut::FObject();
2018-03-14 23:56:44 +01:00
CPPUNIT_ASSERT ( ! obj->hasChildren() );
CPPUNIT_ASSERT ( obj->numOfChildren() == 0 );
CPPUNIT_ASSERT ( ! obj->isChild(child) );
CPPUNIT_ASSERT ( ! child->hasParent() );
CPPUNIT_ASSERT ( child->getParent() != obj );
obj->addChild(child);
CPPUNIT_ASSERT ( obj->hasChildren() );
CPPUNIT_ASSERT ( obj->numOfChildren() == 1 );
CPPUNIT_ASSERT ( obj->isChild(child) );
CPPUNIT_ASSERT ( child->hasParent() );
CPPUNIT_ASSERT ( child->getParent() == obj );
delete obj; // also deletes the child object
}
//----------------------------------------------------------------------
void FObjectTest::delTest()
2019-10-05 23:20:07 +02:00
{
// obj -> child
auto obj = new finalcut::FObject();
auto child = new finalcut::FObject(obj);
2018-03-14 23:56:44 +01:00
CPPUNIT_ASSERT ( obj->hasChildren() );
CPPUNIT_ASSERT ( obj->numOfChildren() == 1 );
CPPUNIT_ASSERT ( obj->isChild(child) );
CPPUNIT_ASSERT ( child->hasParent() );
CPPUNIT_ASSERT ( child->getParent() == obj );
obj->delChild(child);
CPPUNIT_ASSERT ( ! obj->hasChildren() );
CPPUNIT_ASSERT ( obj->numOfChildren() == 0 );
CPPUNIT_ASSERT ( ! obj->isChild(child) );
CPPUNIT_ASSERT ( ! child->hasParent() );
CPPUNIT_ASSERT ( child->getParent() != obj );
2018-03-14 00:53:28 +01:00
2018-03-14 23:56:44 +01:00
delete child;
delete obj;
2018-03-14 00:53:28 +01:00
}
2018-03-16 01:05:45 +01:00
//----------------------------------------------------------------------
void FObjectTest::iteratorTest()
2019-10-05 23:20:07 +02:00
{
// obj -> child1
// -> child2
// -> child3
auto obj = new finalcut::FObject();
auto child1 = new finalcut::FObject(obj);
auto child2 = new finalcut::FObject(obj);
auto child3 = new finalcut::FObject(obj);
2018-03-16 01:05:45 +01:00
CPPUNIT_ASSERT ( child1->getParent() == obj );
CPPUNIT_ASSERT ( child2->getParent() == obj );
CPPUNIT_ASSERT ( child3->getParent() == obj );
finalcut::FObject::const_iterator c_iter, c_last;
2018-03-16 01:05:45 +01:00
c_iter = obj->begin();
c_last = obj->end();
int i = 0;
while ( c_iter != c_last )
{
i++;
++c_iter;
}
CPPUNIT_ASSERT ( obj->numOfChildren() == i );
CPPUNIT_ASSERT ( i == 3 );
finalcut::FObject::iterator iter, last;
2018-03-16 01:05:45 +01:00
iter = obj->begin();
last = obj->end();
i = 0;
while ( iter != last )
{
i++;
++iter;
}
CPPUNIT_ASSERT ( obj->numOfChildren() == i );
CPPUNIT_ASSERT ( i == 3 );
delete obj;
}
//----------------------------------------------------------------------
void FObjectTest::timeTest()
{
struct timeval time1;
2018-12-28 22:57:43 +01:00
uInt64 timeout = 750000; // 750 ms
finalcut::FObject::getCurrentTime(&time1);
CPPUNIT_ASSERT ( ! finalcut::FObject::isTimeout (&time1, timeout) );
2018-03-16 01:05:45 +01:00
sleep(1);
CPPUNIT_ASSERT ( finalcut::FObject::isTimeout (&time1, timeout) );
time1.tv_sec = 300;
time1.tv_usec = 2000000; // > 1000000 µs to test diff underflow
CPPUNIT_ASSERT ( finalcut::FObject::isTimeout (&time1, timeout) );
2018-03-16 01:05:45 +01:00
}
2018-03-14 00:53:28 +01:00
2018-03-16 01:05:45 +01:00
//----------------------------------------------------------------------
void FObjectTest::timerTest()
{
using finalcut::operator +;
using finalcut::operator -;
using finalcut::operator +=;
using finalcut::operator <;
test::FObject_protected t1;
test::FObject_protected t2;
2018-03-16 01:05:45 +01:00
int id1, id2;
CPPUNIT_ASSERT ( t1.getTimerList()->empty() );
id1 = t1.addTimer(300);
CPPUNIT_ASSERT ( ! t1.getTimerList()->empty() );
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 1 );
id2 = t1.addTimer(900);
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 2 );
2018-03-17 14:16:12 +01:00
CPPUNIT_ASSERT ( &t1 != &t2 );
CPPUNIT_ASSERT ( id1 != id2 );
2018-03-16 01:05:45 +01:00
t1.delTimer (id1);
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 1 );
t1.delTimer (id2);
CPPUNIT_ASSERT ( t1.getTimerList()->empty() );
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 0 );
2018-03-17 14:16:12 +01:00
id1 = t1.addTimer(45);
id2 = t1.addTimer(95);
t1.delTimer (id2);
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 1 );
t1.delTimer (id1);
CPPUNIT_ASSERT ( t1.getTimerList()->empty() );
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 0 );
CPPUNIT_ASSERT ( ! t1.delTimer (id1) ); // id double delete
CPPUNIT_ASSERT ( ! t1.delAllTimer() );
2018-03-16 01:05:45 +01:00
t1.addTimer(250);
t1.addTimer(500);
t2.addTimer(750);
t2.addTimer(1000);
CPPUNIT_ASSERT_EQUAL ( t1.getTimerList(), t2.getTimerList() );
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 4 );
CPPUNIT_ASSERT ( t2.getTimerList()->size() == 4 );
t1.delOwnTimer();
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 2 );
CPPUNIT_ASSERT ( t2.getTimerList()->size() == 2 );
t1.addTimer(250);
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 3 );
CPPUNIT_ASSERT ( t2.getTimerList()->size() == 3 );
t2.delAllTimer();
CPPUNIT_ASSERT ( t1.getTimerList()->empty() );
CPPUNIT_ASSERT ( t2.getTimerList()->empty() );
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 0 );
CPPUNIT_ASSERT ( t2.getTimerList()->size() == 0 );
2018-03-17 14:16:12 +01:00
timeval tv1 = { 1321006271, 0 };
timeval tv2 = { 27166271, 0 };
timeval tv_sum = tv1 + tv2;
CPPUNIT_ASSERT ( tv_sum.tv_sec == 1348172542 );
CPPUNIT_ASSERT ( tv_sum.tv_usec == 0 );
timeval tv_difference = tv1 - tv2;
CPPUNIT_ASSERT ( tv_difference.tv_sec == 1293840000 );
CPPUNIT_ASSERT ( tv_difference.tv_usec == 0 );
tv_sum += tv2;
CPPUNIT_ASSERT ( tv_sum.tv_sec == 1375338813 );
CPPUNIT_ASSERT ( tv_sum.tv_usec == 0 );
CPPUNIT_ASSERT ( tv2 < tv1 );
CPPUNIT_ASSERT ( ! (tv1 < tv2) );
CPPUNIT_ASSERT ( tv1 < tv_sum );
CPPUNIT_ASSERT ( ! (tv_sum < tv1) );
CPPUNIT_ASSERT ( tv2 < tv_sum );
CPPUNIT_ASSERT ( ! (tv_sum < tv2) );
CPPUNIT_ASSERT ( tv_difference < tv_sum );
CPPUNIT_ASSERT ( ! (tv_sum < tv_difference) );
tv1.tv_usec = tv2.tv_usec = 600000;
tv_sum = tv1 + tv2;
CPPUNIT_ASSERT ( tv_sum.tv_sec == 1348172543 );
CPPUNIT_ASSERT ( tv_sum.tv_usec == 200000 );
tv1.tv_usec = 654321;
tv2.tv_usec = 123456;
tv_difference = tv1 - tv2;
CPPUNIT_ASSERT ( tv_difference.tv_sec == 1293840000 );
CPPUNIT_ASSERT ( tv_difference.tv_usec == 530865 );
tv2.tv_usec = 999888;
tv_sum += tv2;
CPPUNIT_ASSERT ( tv_sum.tv_sec == 1375338815 );
CPPUNIT_ASSERT ( tv_sum.tv_usec == 199888 );
CPPUNIT_ASSERT ( tv2 < tv1 );
CPPUNIT_ASSERT ( ! (tv1 < tv2) );
CPPUNIT_ASSERT ( tv_difference < tv_sum );
CPPUNIT_ASSERT ( ! (tv_sum < tv_difference) );
CPPUNIT_ASSERT ( ! t1.delTimer(0) );
CPPUNIT_ASSERT ( ! t1.delTimer(-1) );
2018-03-16 01:05:45 +01:00
}
2018-03-14 00:53:28 +01:00
2018-09-27 03:53:51 +02:00
//----------------------------------------------------------------------
void FObjectTest::performTimerActionTest()
{
test::FObject_protected t1;
2018-09-27 03:53:51 +02:00
uInt num_events = 0;
uInt loop = 0;
t1.addTimer(100);
2018-09-27 03:53:51 +02:00
while ( loop < 10 )
{
num_events += t1.processEvent();
// Wait 100 ms
const struct timespec ms[]{{0, 100000000L}};
nanosleep (ms, NULL);
2018-09-27 03:53:51 +02:00
loop++;
}
CPPUNIT_ASSERT ( loop == 10 );
CPPUNIT_ASSERT ( num_events == 9 );
CPPUNIT_ASSERT ( t1.count == 9 );
test::FObject_timer t2;
CPPUNIT_ASSERT ( t2.getValue() == 0 );
finalcut::FTimerEvent timer_ev (finalcut::fc::Timer_Event, 1);
for (int x = 0; x < 10; x++)
finalcut::FApplication::sendEvent (&t2, &timer_ev);
CPPUNIT_ASSERT ( t2.getValue() == 10 );
}
//----------------------------------------------------------------------
void FObjectTest::userEventTest()
{
test::FObject_userEvent user;
CPPUNIT_ASSERT ( user.getValue() == 0 );
int n = 9;
finalcut::FUserEvent user_ev (finalcut::fc::User_Event, 42);
user_ev.setData( (void*)(&n) );
finalcut::FApplication::sendEvent (&user, &user_ev);
CPPUNIT_ASSERT ( user.getValue() == 9 );
2018-09-27 03:53:51 +02:00
}
2018-03-14 00:53:28 +01:00
// Put the test suite in the registry
CPPUNIT_TEST_SUITE_REGISTRATION (FObjectTest);
// The general unit test main part
#include <main-test.inc>