diff options
author | Marcelo Lira <marcelo.lira@openbossa.org> | 2010-02-05 11:42:19 -0300 |
---|---|---|
committer | Marcelo Lira <marcelo.lira@openbossa.org> | 2010-02-10 08:42:23 -0300 |
commit | 18e0f058e4d293ba9c75f73529e946c46eb89873 (patch) | |
tree | 827c764e20fbd7c16625fb15ea8c4260f174388d /tests | |
parent | 54b41b60e7c1b5bf2597cf1cc3ca4da428f2437e (diff) |
Adds tests for reference counting in model/view style relationships.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/libsample/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/libsample/objectview.cpp | 54 | ||||
-rw-r--r-- | tests/libsample/objectview.h | 61 | ||||
-rw-r--r-- | tests/samplebinding/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/samplebinding/global.h | 1 | ||||
-rwxr-xr-x | tests/samplebinding/keep_reference_test.py | 76 | ||||
-rw-r--r-- | tests/samplebinding/typesystem_sample.xml | 2 |
7 files changed, 196 insertions, 0 deletions
diff --git a/tests/libsample/CMakeLists.txt b/tests/libsample/CMakeLists.txt index 852bdf2f3..c1386bf17 100644 --- a/tests/libsample/CMakeLists.txt +++ b/tests/libsample/CMakeLists.txt @@ -17,6 +17,7 @@ mapuser.cpp multiple_derived.cpp objecttype.cpp objecttypelayout.cpp +objectview.cpp overload.cpp overloadsort.cpp pairuser.cpp diff --git a/tests/libsample/objectview.cpp b/tests/libsample/objectview.cpp new file mode 100644 index 000000000..03da872be --- /dev/null +++ b/tests/libsample/objectview.cpp @@ -0,0 +1,54 @@ +/* + * 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 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. + * + * As a special exception to the GNU Lesser General Public License + * version 2.1, the object code form of a "work that uses the Library" + * may incorporate material from a header file that is part of the + * Library. You may distribute such object code under terms of your + * choice, provided that the incorporated material (i) does not exceed + * more than 5% of the total size of the Library; and (ii) is limited to + * numerical parameters, data structure layouts, accessors, macros, + * inline functions and templates. + * + * 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 + */ + +#include "objectview.h" +#include "objecttype.h" +#include "str.h" + +Str +ObjectView::displayModelData() +{ + if (!m_model) + return Str("(NULL)"); + return Str("Name: %VAR").arg(m_model->objectName()); +} + +void +ObjectView::modifyModelData(Str& data) +{ + if (m_model) + m_model->setObjectName(data); +} + + diff --git a/tests/libsample/objectview.h b/tests/libsample/objectview.h new file mode 100644 index 000000000..ae65d98d4 --- /dev/null +++ b/tests/libsample/objectview.h @@ -0,0 +1,61 @@ +/* + * 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 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. + * + * As a special exception to the GNU Lesser General Public License + * version 2.1, the object code form of a "work that uses the Library" + * may incorporate material from a header file that is part of the + * Library. You may distribute such object code under terms of your + * choice, provided that the incorporated material (i) does not exceed + * more than 5% of the total size of the Library; and (ii) is limited to + * numerical parameters, data structure layouts, accessors, macros, + * inline functions and templates. + * + * 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 + */ + +#ifndef OBJECTVIEW_H +#define OBJECTVIEW_H + +#include "objecttype.h" +#include "libsamplemacros.h" + +class Str; + +class LIBSAMPLE_API ObjectView : public ObjectType +{ +public: + ObjectView(ObjectType* model = 0, ObjectType* parent = 0) + : ObjectType(parent), m_model(model) + {} + + void setModel(ObjectType* model) { m_model = model; } + ObjectType* model() const { return m_model; } + + Str displayModelData(); + void modifyModelData(Str& data); + +private: + ObjectType* m_model; +}; + +#endif // OBJECTVIEW_H + diff --git a/tests/samplebinding/CMakeLists.txt b/tests/samplebinding/CMakeLists.txt index de24c02fe..179761e0d 100644 --- a/tests/samplebinding/CMakeLists.txt +++ b/tests/samplebinding/CMakeLists.txt @@ -37,6 +37,7 @@ ${CMAKE_CURRENT_BINARY_DIR}/sample/modifications_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/nondefaultctor_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 ${CMAKE_CURRENT_BINARY_DIR}/sample/oddbooluser_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/overload_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/pairuser_wrapper.cpp diff --git a/tests/samplebinding/global.h b/tests/samplebinding/global.h index b11098631..b442623e2 100644 --- a/tests/samplebinding/global.h +++ b/tests/samplebinding/global.h @@ -16,6 +16,7 @@ #include "nondefaultctor.h" #include "objecttype.h" #include "objecttypelayout.h" +#include "objectview.h" #include "oddbool.h" #include "overload.h" #include "pairuser.h" diff --git a/tests/samplebinding/keep_reference_test.py b/tests/samplebinding/keep_reference_test.py new file mode 100755 index 000000000..13730553e --- /dev/null +++ b/tests/samplebinding/keep_reference_test.py @@ -0,0 +1,76 @@ +#!/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 sys import getrefcount + +from sample import ObjectType, 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() + refcount1 = getrefcount(model1) + view1 = ObjectView() + view1.setModel(model1) + self.assertEqual(getrefcount(view1.model()), refcount1 + 1) + + view2 = ObjectView() + view2.setModel(model1) + self.assertEqual(getrefcount(view2.model()), refcount1 + 2) + + model2 = ObjectType() + 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() + refcount1 = getrefcount(model) + view = ObjectView() + view.setModel(model) + self.assertEqual(getrefcount(view.model()), refcount1 + 1) + + del view + self.assertEqual(getrefcount(model), refcount1) + + 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.setObjectName('created model') + view.setModel(model) + view = ObjectView() + createModelAndSetToView(view) + model = view.model() + +if __name__ == '__main__': + unittest.main() + diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml index a722276ea..9d06a2803 100644 --- a/tests/samplebinding/typesystem_sample.xml +++ b/tests/samplebinding/typesystem_sample.xml @@ -138,6 +138,8 @@ </modify-function> </object-type> + <object-type name="ObjectView"/> + <value-type name="Event"/> <value-type name="BlackBox"> |