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'''Test cases for virtual methods in multiple inheritance scenarios'''
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 VirtualMethods, ObjectType, Event
43
44
45class ImplementsNone(ObjectType, VirtualMethods):
46    '''Implements no virtual methods'''
47
48    def __init__(self):
49        ObjectType.__init__(self)
50        VirtualMethods.__init__(self)
51
52
53class ImplementsBoth(ObjectType, VirtualMethods):
54    '''Implements ObjectType.event and VirtualMethods.sum1'''
55
56    def __init__(self):
57        ObjectType.__init__(self)
58        VirtualMethods.__init__(self)
59        self.event_processed = False
60
61    def event(self, event):
62        self.event_processed = True
63        return True
64
65    def sum1(self, arg0, arg1, arg2):
66        return (arg0 + arg1 + arg2) * 2
67
68
69class CppVirtualTest(unittest.TestCase):
70    '''Virtual method defined in c++ called from C++'''
71
72    def testCpp(self):
73        '''C++ calling C++ virtual method in multiple inheritance scenario'''
74        obj = ImplementsNone()
75        self.assertTrue(ObjectType.processEvent([obj], Event(Event.BASIC_EVENT)))
76        self.assertRaises(AttributeError, getattr, obj, 'event_processed')
77
78        self.assertEqual(obj.callSum0(1, 2, 3), 6)
79
80
81class PyVirtualTest(unittest.TestCase):
82    '''Virtual method reimplemented in python called from C++'''
83
84    def testEvent(self):
85        '''C++ calling Python reimplementation of virtual in multiple inheritance'''
86        obj = ImplementsBoth()
87        self.assertTrue(ObjectType.processEvent([obj], Event(Event.BASIC_EVENT)))
88        self.assertTrue(obj.event_processed)
89
90        self.assertEqual(obj.callSum1(1, 2, 3), 12)
91
92
93if __name__ == '__main__':
94    unittest.main()
95