1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4#############################################################################
5##
6## Copyright (C) 2016 The Qt Company Ltd.
7## Contact: https://www.qt.io/licensing/
8##
9## This file is part of the test suite of Qt for Python.
10##
11## $QT_BEGIN_LICENSE:GPL-EXCEPT$
12## Commercial License Usage
13## Licensees holding valid commercial Qt licenses may use this file in
14## accordance with the commercial license agreement provided with the
15## Software or, alternatively, in accordance with the terms contained in
16## a written agreement between you and The Qt Company. For licensing terms
17## and conditions see https://www.qt.io/terms-conditions. For further
18## information use the contact form at https://www.qt.io/contact-us.
19##
20## GNU General Public License Usage
21## Alternatively, this file may be used under the terms of the GNU
22## General Public License version 3 as published by the Free Software
23## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
24## included in the packaging of this file. Please review the following
25## information to ensure the GNU General Public License requirements will
26## be met: https://www.gnu.org/licenses/gpl-3.0.html.
27##
28## $QT_END_LICENSE$
29##
30#############################################################################
31
32'''Ownership tests for cases of invalidation of Python wrapper after use.'''
33
34import os
35import sys
36import unittest
37
38sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
39from shiboken_paths import init_paths
40init_paths()
41
42from sample import ObjectType, ObjectTypeDerived, Event
43
44
45class ExtObjectType(ObjectType):
46    def __init__(self):
47        ObjectType.__init__(self)
48        self.type_of_last_event = None
49        self.last_event = None
50    def event(self, event):
51        self.last_event = event
52        self.type_of_last_event = event.eventType()
53        return True
54
55class MyObjectType (ObjectType):
56    def __init__(self):
57        super(MyObjectType, self).__init__()
58        self.fail = False
59
60    def event(self, ev):
61        self.callInvalidateEvent(ev)
62        try:
63            ev.eventType()
64        except:
65            self.fail = True
66            raise
67        return True
68
69    def invalidateEvent(self, ev):
70        pass
71
72class ExtObjectTypeDerived(ObjectTypeDerived):
73    def __init__(self):
74        ObjectTypeDerived.__init__(self)
75        self.type_of_last_event = None
76        self.last_event = None
77    def event(self, event):
78        self.last_event = event
79        self.type_of_last_event = event.eventType()
80        return True
81
82class OwnershipInvalidateAfterUseTest(unittest.TestCase):
83    '''Ownership tests for cases of invalidation of Python wrapper after use.'''
84
85    def testInvalidateAfterUse(self):
86        '''In ObjectType.event(Event*) the wrapper object created for Event must me marked as invalid after the method is called.'''
87        eot = ExtObjectType()
88        eot.causeEvent(Event.SOME_EVENT)
89        self.assertEqual(eot.type_of_last_event, Event.SOME_EVENT)
90        self.assertRaises(RuntimeError, eot.last_event.eventType)
91
92    def testObjectInvalidatedAfterUseAsParameter(self):
93        '''Tries to use wrapper invalidated after use as a parameter to another method.'''
94        eot = ExtObjectType()
95        ot = ObjectType()
96        eot.causeEvent(Event.ANY_EVENT)
97        self.assertEqual(eot.type_of_last_event, Event.ANY_EVENT)
98        self.assertRaises(RuntimeError, ot.event, eot.last_event)
99
100    def testit(self):
101        obj = MyObjectType()
102        obj.causeEvent(Event.BASIC_EVENT)
103        self.assertFalse(obj.fail)
104
105    def testInvalidateAfterUseInDerived(self):
106        '''Invalidate was failing in a derived C++ class that also inherited
107        other base classes'''
108        eot = ExtObjectTypeDerived()
109        eot.causeEvent(Event.SOME_EVENT)
110        self.assertEqual(eot.type_of_last_event, Event.SOME_EVENT)
111        self.assertRaises(RuntimeError, eot.last_event.eventType)
112
113if __name__ == '__main__':
114    unittest.main()
115
116