aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMarcelo Lira <marcelo.lira@openbossa.org>2009-12-01 11:36:45 -0300
committerMarcelo Lira <marcelo.lira@openbossa.org>2009-12-01 15:25:55 -0300
commit6ea32275b157bcf3a97725172a06122b4ff98d0a (patch)
tree66f8117318e275b211c067f0e908e8c12ec7097e /tests
parent64cda5a441fd912469c4ad2c7afcc39f0da6819c (diff)
Updated ObjectType and ObjectTypeLayout to resemble the Qt's QLayout class.
Also added more test cases that use ObjectTypeLayout.
Diffstat (limited to 'tests')
-rw-r--r--tests/libsample/objecttype.cpp50
-rw-r--r--tests/libsample/objecttype.h12
-rw-r--r--tests/libsample/objecttypelayout.cpp29
-rw-r--r--tests/libsample/objecttypelayout.h8
-rwxr-xr-xtests/samplebinding/objecttypelayout_test.py130
-rw-r--r--tests/samplebinding/typesystem_sample.xml2
6 files changed, 217 insertions, 14 deletions
diff --git a/tests/libsample/objecttype.cpp b/tests/libsample/objecttype.cpp
index d691c6960..98d6a8685 100644
--- a/tests/libsample/objecttype.cpp
+++ b/tests/libsample/objecttype.cpp
@@ -35,10 +35,11 @@
#include "objecttype.h"
#include "objecttypelayout.h"
#include <algorithm>
+#include <iostream>
using namespace std;
-ObjectType::ObjectType(ObjectType* parent) : m_parent(0)
+ObjectType::ObjectType(ObjectType* parent) : m_parent(0), m_layout(0)
{
setParent(parent);
}
@@ -161,12 +162,47 @@ ObjectType::event(Event* event)
return true;
}
-void ObjectType::setObjectLayout(ObjectTypeLayout* layout)
+void ObjectType::setLayout(ObjectTypeLayout* l)
{
- layout->setParent(this);
- std::list<ObjectType*> objects = layout->objects();
- std::list<ObjectType*>::const_iterator it = objects.begin();
- for (; it != objects.end(); ++it)
- (*it)->setParent(this);
+ if (!l) {
+ cerr << "[WARNING] ObjectType::setLayout: Cannot set layout to 0." << endl;
+ return;
+ }
+
+ if (layout()) {
+ if (layout() != l) {
+ cerr << "[WARNING] ObjectType::setLayout: Attempting to set ObjectTypeLayout '" << l->objectName().cstring();
+ cerr << "' on ObjectType '" << objectName().cstring() << "', which already has a layout." << endl;
+ }
+ return;
+ }
+
+ ObjectType* oldParent = l->parent();
+ if (oldParent && oldParent != this) {
+ if (oldParent->isLayoutType()) {
+ cerr << "[WARNING] ObjectType::setLayout: Attempting to set ObjectTypeLayout '" << l->objectName().cstring();
+ cerr << "' on ObjectType '" << objectName().cstring() << "', when the ObjectTypeLayout already has a parent layout." << endl;
+ return;
+ } else {
+ // Steal the layout from an ObjectType parent.
+ oldParent->takeLayout();
+ }
+ }
+
+ m_layout = l;
+ if (oldParent != this) {
+ l->setParent(this);
+ l->reparentChildren(this);
+ }
+}
+
+ObjectTypeLayout* ObjectType::takeLayout()
+{
+ ObjectTypeLayout* l = layout();
+ if (!l)
+ return 0;
+ m_layout = 0;
+ l->setParent(0);
+ return l;
}
diff --git a/tests/libsample/objecttype.h b/tests/libsample/objecttype.h
index bd58b7b64..d7a7ae1c6 100644
--- a/tests/libsample/objecttype.h
+++ b/tests/libsample/objecttype.h
@@ -85,17 +85,27 @@ public:
// Returns true if the event is processed.
virtual bool event(Event* event);
+
// This nonsense method emulate QWidget.setLayout method
// All layout objects will became children of this object.
- void setObjectLayout(ObjectTypeLayout* layout);
+ void setLayout(ObjectTypeLayout* layout);
+ ObjectTypeLayout* layout() const { return m_layout; }
+
+ // This method should be reimplemented by ObjectTypeLayout.
+ virtual bool isLayoutType() { return false; }
private:
ObjectType(const ObjectType&);
ObjectType& operator=(const ObjectType&);
+ ObjectTypeLayout* takeLayout();
+
Str m_objectName;
ObjectType* m_parent;
ObjectTypeList m_children;
+
+ ObjectTypeLayout* m_layout;
};
+
#endif // OBJECTTYPE_H
diff --git a/tests/libsample/objecttypelayout.cpp b/tests/libsample/objecttypelayout.cpp
index 06c4555b4..92180418f 100644
--- a/tests/libsample/objecttypelayout.cpp
+++ b/tests/libsample/objecttypelayout.cpp
@@ -33,9 +33,26 @@
*/
#include "objecttypelayout.h"
+#include <iostream>
+
+using namespace std;
void ObjectTypeLayout::addObject(ObjectType* obj)
{
+ if (obj->isLayoutType()) {
+ ObjectTypeLayout* l = reinterpret_cast<ObjectTypeLayout*>(obj);
+ if (l->parent()) {
+ cerr << "[WARNING] ObjectTypeLayout::addObject: layout '" << l->objectName().cstring();
+ cerr << "' already has a parent." << endl;
+ return;
+ }
+
+ l->setParent(this);
+
+ if (parent() && !parent()->isLayoutType())
+ l->reparentChildren(parent());
+ }
+
m_objects.push_back(obj);
}
@@ -43,3 +60,15 @@ std::list< ObjectType* > ObjectTypeLayout::objects() const
{
return m_objects;
}
+
+void ObjectTypeLayout::reparentChildren(ObjectType* parent)
+{
+ std::list<ObjectType*>::const_iterator it = m_objects.begin();
+ for (; it != m_objects.end(); ++it) {
+ if ((*it)->isLayoutType())
+ reinterpret_cast<ObjectTypeLayout*>(*it)->reparentChildren(parent);
+ else
+ (*it)->setParent(parent);
+ }
+}
+
diff --git a/tests/libsample/objecttypelayout.h b/tests/libsample/objecttypelayout.h
index 7d25e09be..ba39fd330 100644
--- a/tests/libsample/objecttypelayout.h
+++ b/tests/libsample/objecttypelayout.h
@@ -46,8 +46,14 @@ class LIBSAMPLE_API ObjectTypeLayout : public ObjectType
public:
void addObject(ObjectType* obj);
std::list<ObjectType*> objects() const;
+
+ virtual bool isLayoutType() { return true; }
private:
std::list<ObjectType*> m_objects;
+
+ void reparentChildren(ObjectType* parent);
+ friend LIBSAMPLE_API void ObjectType::setLayout(ObjectTypeLayout* l);
};
-#endif
+#endif // OBJECTTYPELAYOUT_H
+
diff --git a/tests/samplebinding/objecttypelayout_test.py b/tests/samplebinding/objecttypelayout_test.py
index 74f7f5c64..a21b03d00 100755
--- a/tests/samplebinding/objecttypelayout_test.py
+++ b/tests/samplebinding/objecttypelayout_test.py
@@ -24,7 +24,7 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA
-'''Tests ObjectTypeLayout class'''
+'''Tests cases for ObjectTypeLayout class.'''
import sys
import unittest
@@ -33,11 +33,42 @@ from sample import *
class ObjectTypeLayoutTest(unittest.TestCase):
- '''Test cases ObjectTypeLayout class'''
+ '''Test cases for ObjectTypeLayout class.'''
- def testObjectTypeLayout(self):
+ def testSetNullLayout(self):
+ '''ObjectType.setLayout(0).'''
+ o2 = ObjectType()
+ o2.setLayout(None)
+
+ def testSetNullLayoutToObjectTypeCreatedInCpp(self):
+ '''ObjectType.setLayout(0) to object created in C++.'''
+ o1 = ObjectType.create()
+ o1.setLayout(None)
+ def testObjectTypeLayout(self):
'''ObjectType.setLayout.'''
+ p1 = ObjectType()
+ c1 = ObjectType()
+ c2 = ObjectType()
+ c3 = ObjectType()
+ layout = ObjectTypeLayout()
+ layout.addObject(c1)
+ layout.addObject(c2)
+ layout.addObject(c3)
+ self.assertEqual(c1.parent(), None)
+ self.assertEqual(c2.parent(), None)
+ self.assertEqual(c3.parent(), None)
+
+ p1.setLayout(layout)
+ del p1 # This must kill c1, c2 and c3
+
+ self.assertRaises(RuntimeError, c1.objectName)
+ self.assertRaises(RuntimeError, c2.objectName)
+ self.assertRaises(RuntimeError, c3.objectName)
+ self.assertRaises(RuntimeError, layout.objectName)
+
+ def testObjectTypeLayoutWithObjectsCreatedInCpp(self):
+ '''ObjectType.setLayout with objects created in C++.'''
p1 = ObjectType.create()
c1 = ObjectType.create()
c2 = ObjectType.create()
@@ -50,13 +81,104 @@ class ObjectTypeLayoutTest(unittest.TestCase):
self.assertEqual(c2.parent(), None)
self.assertEqual(c3.parent(), None)
- p1.setObjectLayout(layout)
+ p1.setLayout(layout)
del p1 # This must kill c1, c2 and c3
+
self.assertRaises(RuntimeError, c1.objectName)
self.assertRaises(RuntimeError, c2.objectName)
self.assertRaises(RuntimeError, c3.objectName)
self.assertRaises(RuntimeError, layout.objectName)
+ def testObjectTypeLayoutTransference(self):
+ '''Transfer a layout from one ObjectType to another, so that all the items in the layout get reparented.'''
+ p1 = ObjectType()
+ p2 = ObjectType()
+ c1 = ObjectType()
+ c2 = ObjectType()
+
+ layout = ObjectTypeLayout()
+ layout.addObject(c1)
+ layout.addObject(c2)
+
+ p1.setLayout(layout)
+
+ self.assertEqual(len(p2.children()), 0)
+ self.assertEqual(c1.parent(), p1)
+ self.assertEqual(c2.parent(), p1)
+ self.assertEqual(set(p1.children()), set([c1, c2, layout]))
+
+ p2.setLayout(layout)
+
+ self.assertEqual(len(p1.children()), 0)
+ self.assertEqual(c1.parent(), p2)
+ self.assertEqual(c2.parent(), p2)
+ self.assertEqual(set(p2.children()), set([c1, c2, layout]))
+
+ def testObjectTypeLayoutInsideAnotherLayout(self):
+ '''Adds one ObjectTypeLayout to another and sets the parent to an ObjectType.'''
+ p1 = ObjectType()
+
+ l1 = ObjectTypeLayout()
+ c1 = ObjectType()
+ l1.addObject(c1)
+ c2 = ObjectType()
+ l1.addObject(c2)
+
+ l2 = ObjectTypeLayout()
+ c3 = ObjectType()
+ l2.addObject(c3)
+ c4 = ObjectType()
+ l2.addObject(c4)
+
+ l1.addObject(l2)
+
+ p1.setLayout(l1)
+
+ self.assertEqual(c1.parent(), p1)
+ self.assertEqual(c2.parent(), p1)
+ self.assertEqual(c3.parent(), p1)
+ self.assertEqual(c4.parent(), p1)
+ self.assertEqual(l1.parent(), p1)
+ self.assertEqual(l2.parent(), l1)
+
+ def testTransferNestedLayoutsBetweenObjects(self):
+ '''Adds one ObjectTypeLayout to another, sets the parent to an ObjectType and then transfer it to another object.'''
+ p1 = ObjectType()
+ p2 = ObjectType()
+
+ l1 = ObjectTypeLayout()
+ c1 = ObjectType()
+ l1.addObject(c1)
+ c2 = ObjectType()
+ l1.addObject(c2)
+
+ l2 = ObjectTypeLayout()
+ c3 = ObjectType()
+ l2.addObject(c3)
+ c4 = ObjectType()
+ l2.addObject(c4)
+
+ l1.addObject(l2)
+
+ p1.setLayout(l1)
+
+ self.assertEqual(c1.parent(), p1)
+ self.assertEqual(c2.parent(), p1)
+ self.assertEqual(c3.parent(), p1)
+ self.assertEqual(c4.parent(), p1)
+ self.assertEqual(l1.parent(), p1)
+ self.assertEqual(l2.parent(), l1)
+
+ p2.setLayout(l1)
+ del p1
+
+ self.assertEqual(c1.parent(), p2)
+ self.assertEqual(c2.parent(), p2)
+ self.assertEqual(c3.parent(), p2)
+ self.assertEqual(c4.parent(), p2)
+ self.assertEqual(l1.parent(), p2)
+ self.assertEqual(l2.parent(), l1)
+
if __name__ == '__main__':
unittest.main()
diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml
index 315bc03c7..e8e4670b6 100644
--- a/tests/samplebinding/typesystem_sample.xml
+++ b/tests/samplebinding/typesystem_sample.xml
@@ -79,7 +79,7 @@
<parent index="this" action="add"/>
</modify-argument>
</modify-function>
- <modify-function signature="setObjectLayout(ObjectTypeLayout*)">
+ <modify-function signature="setLayout(ObjectTypeLayout*)">
<inject-code class="target" position="end">
const ObjectTypeList&amp; objChildren = %CPPSELF.children();
ObjectTypeList::const_iterator it = objChildren.begin();