1#!/usr/bin/python
2
3#############################################################################
4##
5## Copyright (C) 2016 The Qt Company Ltd.
6## Contact: https://www.qt.io/licensing/
7##
8## This file is part of the test suite of Qt for Python.
9##
10## $QT_BEGIN_LICENSE:GPL-EXCEPT$
11## Commercial License Usage
12## Licensees holding valid commercial Qt licenses may use this file in
13## accordance with the commercial license agreement provided with the
14## Software or, alternatively, in accordance with the terms contained in
15## a written agreement between you and The Qt Company. For licensing terms
16## and conditions see https://www.qt.io/terms-conditions. For further
17## information use the contact form at https://www.qt.io/contact-us.
18##
19## GNU General Public License Usage
20## Alternatively, this file may be used under the terms of the GNU
21## General Public License version 3 as published by the Free Software
22## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
23## included in the packaging of this file. Please review the following
24## information to ensure the GNU General Public License requirements will
25## be met: https://www.gnu.org/licenses/gpl-3.0.html.
26##
27## $QT_END_LICENSE$
28##
29#############################################################################
30
31import os
32import sys
33import unittest
34
35sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
36from init_paths import init_test_paths
37init_test_paths(False)
38
39from PySide2.QtCore import QByteArray
40from PySide2.QtXml import QDomDocument, QDomElement
41import py3kcompat as py3k
42
43class QDomDocumentTest(unittest.TestCase):
44
45    def setUp(self):
46        self.dom = QDomDocument()
47
48        self.goodXmlData = QByteArray(py3k.b('''
49        <typesystem package="PySide2.QtXml">
50            <value-type name="QDomDocument"/>
51            <value-type name="QDomElement"/>
52        </typesystem>
53        '''))
54
55        self.badXmlData = QByteArray(py3k.b('''
56        <typesystem package="PySide2.QtXml">
57            <value-type name="QDomDocument">
58        </typesystem>
59        '''))
60
61    def tearDown(self):
62        del self.dom
63        del self.goodXmlData
64        del self.badXmlData
65
66    def testQDomDocumentSetContentWithBadXmlData(self):
67        '''Sets invalid xml as the QDomDocument contents.'''
68        ok, errorStr, errorLine, errorColumn = self.dom.setContent(self.badXmlData, True)
69        self.assertFalse(ok)
70        self.assertEqual(errorStr, 'tag mismatch')
71        self.assertEqual(errorLine, 4)
72        self.assertEqual(errorColumn, 21)
73
74    def testQDomDocumentSetContentWithGoodXmlData(self):
75        '''Sets valid xml as the QDomDocument contents.'''
76        ok, errorStr, errorLine, errorColumn = self.dom.setContent(self.goodXmlData, True)
77        self.assertTrue(ok)
78        self.assertEqual(errorStr, '')
79        self.assertEqual(errorLine, 0)
80        self.assertEqual(errorColumn, 0)
81
82    def testQDomDocumentData(self):
83        '''Checks the QDomDocument elements for the valid xml contents.'''
84
85        def checkAttribute(element, attribute, value):
86            self.assertTrue(isinstance(root, QDomElement))
87            self.assertFalse(element.isNull())
88            self.assertTrue(element.hasAttribute(attribute))
89            self.assertEqual(element.attribute(attribute), value)
90
91        ok, errorStr, errorLine, errorColumn = self.dom.setContent(self.goodXmlData, True)
92        root = self.dom.documentElement()
93        self.assertEqual(root.tagName(), 'typesystem')
94        checkAttribute(root, 'package', 'PySide2.QtXml')
95
96        child = root.firstChildElement('value-type')
97        checkAttribute(child, 'name', 'QDomDocument')
98
99        child = child.nextSiblingElement('value-type')
100        checkAttribute(child, 'name', 'QDomElement')
101
102if __name__ == '__main__':
103    unittest.main()
104
105