summaryrefslogtreecommitdiffstats
path: root/tests/samplebinding
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 /tests/samplebinding
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>
Diffstat (limited to 'tests/samplebinding')
-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
5 files changed, 88 insertions, 6 deletions
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"/>