summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarcelo Lira <marcelo.lira@openbossa.org>2010-12-16 14:08:29 -0300
committerMarcelo Lira <marcelo.lira@openbossa.org>2010-12-16 14:38:04 -0300
commit2d146fad966ae24940c65812cde4c28b645ecf6b (patch)
tree08267561ce65de16fc422d59f2dceaadfd4309f9
parentf728e59aa228d5b796b5641bea683333b10919e4 (diff)
downloadshiboken-2d146fad966ae24940c65812cde4c28b645ecf6b.tar.gz
shiboken-2d146fad966ae24940c65812cde4c28b645ecf6b.tar.xz
shiboken-2d146fad966ae24940c65812cde4c28b645ecf6b.zip
Added tests to check the release of ownership of objects returned from Python.
The ObjectModel test class was introduced to check if the transference of ownership of objects returned from Python to C++ through a virtual method is working properly. Also updated the other test that uses the ObjectView class. Reviewed by Lauro Moura <lauro.neto@openbossa.org> Reviewed by Luciano Wolf <luciano.wolf@openbossa.org>
-rw-r--r--tests/libsample/CMakeLists.txt1
-rw-r--r--tests/libsample/objectmodel.cpp36
-rw-r--r--tests/libsample/objectmodel.h46
-rw-r--r--tests/libsample/objectview.cpp8
-rw-r--r--tests/libsample/objectview.h11
-rw-r--r--tests/samplebinding/CMakeLists.txt1
-rw-r--r--tests/samplebinding/global.h1
-rw-r--r--tests/samplebinding/keep_reference_test.py10
-rw-r--r--tests/samplebinding/modelview_test.py73
-rw-r--r--tests/samplebinding/typesystem_sample.xml9
10 files changed, 185 insertions, 11 deletions
diff --git a/tests/libsample/CMakeLists.txt b/tests/libsample/CMakeLists.txt
index 3452c576..b6cd1b92 100644
--- a/tests/libsample/CMakeLists.txt
+++ b/tests/libsample/CMakeLists.txt
@@ -16,6 +16,7 @@ modifications.cpp
mapuser.cpp
modified_constructor.cpp
multiple_derived.cpp
+objectmodel.cpp
objecttype.cpp
objecttypelayout.cpp
objectview.cpp
diff --git a/tests/libsample/objectmodel.cpp b/tests/libsample/objectmodel.cpp
new file mode 100644
index 00000000..93c25645
--- /dev/null
+++ b/tests/libsample/objectmodel.cpp
@@ -0,0 +1,36 @@
+/*
+ * This file is part of the Shiboken Python Binding Generator project.
+ *
+ * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
+ *
+ * Contact: PySide team <contact@pyside.org>
+ *
+ * This library 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 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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 library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "objectmodel.h"
+
+void
+ObjectModel::setData(ObjectType* data)
+{
+ m_data = data;
+}
+
+ObjectType*
+ObjectModel::data() const
+{
+ return m_data;
+}
+
diff --git a/tests/libsample/objectmodel.h b/tests/libsample/objectmodel.h
new file mode 100644
index 00000000..b226b1f9
--- /dev/null
+++ b/tests/libsample/objectmodel.h
@@ -0,0 +1,46 @@
+/*
+ * This file is part of the Shiboken Python Binding Generator project.
+ *
+ * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
+ *
+ * Contact: PySide team <contact@pyside.org>
+ *
+ * This library 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 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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 library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef OBJECTMODEL_H
+#define OBJECTMODEL_H
+
+#include "objecttype.h"
+#include "libsamplemacros.h"
+
+class LIBSAMPLE_API ObjectModel : public ObjectType
+{
+public:
+ ObjectModel(ObjectType* parent = 0)
+ : ObjectType(parent), m_data(0)
+ {}
+
+ void setData(ObjectType* data);
+ virtual ObjectType* data() const;
+
+private:
+ // The model holds only one piece of data.
+ // (This is just a test after all.)
+ ObjectType* m_data;
+};
+
+#endif // OBJECTMODEL_H
+
diff --git a/tests/libsample/objectview.cpp b/tests/libsample/objectview.cpp
index 835e7183..a5dbc0f1 100644
--- a/tests/libsample/objectview.cpp
+++ b/tests/libsample/objectview.cpp
@@ -21,7 +21,7 @@
*/
#include "objectview.h"
-#include "objecttype.h"
+#include "objectmodel.h"
#include "str.h"
Str
@@ -40,3 +40,9 @@ ObjectView::modifyModelData(Str& data)
}
+ObjectType*
+ObjectView::getRawModelData()
+{
+ return m_model->data();
+}
+
diff --git a/tests/libsample/objectview.h b/tests/libsample/objectview.h
index fd3d640b..3402b6d8 100644
--- a/tests/libsample/objectview.h
+++ b/tests/libsample/objectview.h
@@ -27,22 +27,25 @@
#include "libsamplemacros.h"
class Str;
+class ObjectModel;
class LIBSAMPLE_API ObjectView : public ObjectType
{
public:
- ObjectView(ObjectType* model = 0, ObjectType* parent = 0)
+ ObjectView(ObjectModel* model = 0, ObjectType* parent = 0)
: ObjectType(parent), m_model(model)
{}
- inline void setModel(ObjectType* model) { m_model = model; }
- inline ObjectType* model() const { return m_model; }
+ inline void setModel(ObjectModel* model) { m_model = model; }
+ inline ObjectModel* model() const { return m_model; }
Str displayModelData();
void modifyModelData(Str& data);
+ ObjectType* getRawModelData();
+
private:
- ObjectType* m_model;
+ ObjectModel* m_model;
};
#endif // OBJECTVIEW_H
diff --git a/tests/samplebinding/CMakeLists.txt b/tests/samplebinding/CMakeLists.txt
index d3e27591..052e246a 100644
--- a/tests/samplebinding/CMakeLists.txt
+++ b/tests/samplebinding/CMakeLists.txt
@@ -42,6 +42,7 @@ ${CMAKE_CURRENT_BINARY_DIR}/sample/modifications_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/modifiedconstructor_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/noimplicitconversion_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/nondefaultctor_wrapper.cpp
+${CMAKE_CURRENT_BINARY_DIR}/sample/objectmodel_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/objecttype_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/objecttypelayout_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/objectview_wrapper.cpp
diff --git a/tests/samplebinding/global.h b/tests/samplebinding/global.h
index d027b0f5..90cae083 100644
--- a/tests/samplebinding/global.h
+++ b/tests/samplebinding/global.h
@@ -20,6 +20,7 @@
#include "multiple_derived.h"
#include "noimplicitconversion.h"
#include "nondefaultctor.h"
+#include "objectmodel.h"
#include "objecttype.h"
#include "objecttypelayout.h"
#include "objecttypereference.h"
diff --git a/tests/samplebinding/keep_reference_test.py b/tests/samplebinding/keep_reference_test.py
index 13730553..fd99a23c 100644
--- a/tests/samplebinding/keep_reference_test.py
+++ b/tests/samplebinding/keep_reference_test.py
@@ -29,14 +29,14 @@
import unittest
from sys import getrefcount
-from sample import ObjectType, ObjectView
+from sample import ObjectModel, ObjectView
class TestKeepReference(unittest.TestCase):
'''Test case for objects that keep references to other object without owning them (e.g. model/view relationships).'''
def testReferenceCounting(self):
'''Tests reference count of model-like object referred by view-like objects.'''
- model1 = ObjectType()
+ model1 = ObjectModel()
refcount1 = getrefcount(model1)
view1 = ObjectView()
view1.setModel(model1)
@@ -46,13 +46,13 @@ class TestKeepReference(unittest.TestCase):
view2.setModel(model1)
self.assertEqual(getrefcount(view2.model()), refcount1 + 2)
- model2 = ObjectType()
+ model2 = ObjectModel()
view2.setModel(model2)
self.assertEqual(getrefcount(view1.model()), refcount1 + 1)
def testReferenceCountingWhenDeletingReferrer(self):
'''Tests reference count of model-like object referred by deceased view-like object.'''
- model = ObjectType()
+ model = ObjectModel()
refcount1 = getrefcount(model)
view = ObjectView()
view.setModel(model)
@@ -64,7 +64,7 @@ class TestKeepReference(unittest.TestCase):
def testReferreedObjectSurvivalAfterContextEnd(self):
'''Model-like object assigned to a view-like object must survive after get out of context.'''
def createModelAndSetToView(view):
- model = ObjectType()
+ model = ObjectModel()
model.setObjectName('created model')
view.setModel(model)
view = ObjectView()
diff --git a/tests/samplebinding/modelview_test.py b/tests/samplebinding/modelview_test.py
new file mode 100644
index 00000000..9f86166d
--- /dev/null
+++ b/tests/samplebinding/modelview_test.py
@@ -0,0 +1,73 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# This file is part of the Shiboken Python Bindings Generator project.
+#
+# Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
+#
+# Contact: PySide team <contact@pyside.org>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public License
+# version 2.1 as published by the Free Software Foundation. Please
+# review the following information to ensure the GNU Lesser General
+# Public License version 2.1 requirements will be met:
+# http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+# #
+# This program 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, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+# 02110-1301 USA
+
+'''Test case for objects that keep references to other object without owning them (e.g. model/view relationships).'''
+
+import unittest
+from sample import ObjectModel, ObjectType, ObjectView
+
+
+object_name = 'test object'
+
+class MyObject(ObjectType):
+ pass
+
+class ListModelKeepsReference(ObjectModel):
+ def __init__(self, parent=None):
+ ObjectModel.__init__(self, parent)
+ self.obj = MyObject()
+ self.obj.setObjectName(object_name)
+
+ def data(self):
+ return self.obj
+
+class ListModelDoesntKeepsReference(ObjectModel):
+ def data(self):
+ obj = MyObject()
+ obj.setObjectName(object_name)
+ return obj
+
+
+class ModelViewTest(unittest.TestCase):
+
+ def testListModelDoesntKeepsReference(self):
+ model = ListModelDoesntKeepsReference()
+ view = ObjectView(model)
+ obj = view.getRawModelData()
+ self.assertEqual(type(obj), ObjectType)
+ self.assertEqual(obj.objectName(), object_name)
+
+ def testListModelKeepsReference(self):
+ model = ListModelKeepsReference()
+ view = ObjectView(model)
+ obj = view.getRawModelData()
+ self.assertEqual(type(obj), MyObject)
+ self.assertEqual(obj.objectName(), object_name)
+
+
+if __name__ == '__main__':
+ unittest.main()
+
diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml
index ba58c5f2..ae6f61b9 100644
--- a/tests/samplebinding/typesystem_sample.xml
+++ b/tests/samplebinding/typesystem_sample.xml
@@ -236,13 +236,20 @@
</object-type>
<object-type name="ObjectView">
- <modify-function signature="setModel(ObjectType*)">
+ <modify-function signature="setModel(ObjectModel*)">
<modify-argument index="1">
<reference-count action="add"/>
</modify-argument>
</modify-function>
</object-type>
+ <object-type name="ObjectModel">
+ <modify-function signature="data() const">
+ <modify-argument index="return">
+ <define-ownership class="native" owner="c++"/>
+ </modify-argument>
+ </modify-function>
+ </object-type>
<value-type name="Event">
<enum-type name="EventType"/>