summaryrefslogtreecommitdiffstats
path: root/sw/qa/python/check_xautotextgroup.py
blob: 504f12dcd4225257425982e3c30d13b88503e089 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#! /usr/bin/env python
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
import unittest
from org.libreoffice.unotest import UnoInProcess
from com.sun.star.container import ElementExistException
from com.sun.star.container import NoSuchElementException


class XAutoTextGroup(unittest.TestCase):
    # 0 indicates the path of the Office Basis layer
    # 1 indicates the path of the user directory
    GROUP_NAME = 'atg_name1*1'

    @classmethod
    def setUpClass(cls):
        cls._uno = UnoInProcess()
        cls._uno.setUp()
        cls._uno.openEmptyWriterDoc()

    def setUp(self):
        self.xAutoTextContainer = self.createAutoTextContainer()
        self.xAutoTextGroup = self.insertNewGroup(self.xAutoTextContainer)

        self.xText = self._uno.getDoc().getText()
        self.xCursor = self.xText.createTextCursor()
        self.xRange = self.xCursor.getStart()

    def tearDown(self):
        self.xAutoTextContainer.removeByName(self.GROUP_NAME)

    @classmethod
    def tearDownClass(cls):
        cls._uno.tearDown()

    def test_XAutoTextGroup(self):
        xName = 'Name'
        xTitle = 'Title'

        titlesBefore = self.xAutoTextGroup.getTitles()

        self.xAutoTextGroup.insertNewByName(xName, xTitle, self.xRange)
        self.assertNotEqual(titlesBefore, self.xAutoTextGroup.getTitles())

        self.xAutoTextGroup.removeByName(xName)
        self.assertEqual(titlesBefore, self.xAutoTextGroup.getTitles())

    def test_XAutoTextGroup_NoTitle(self):
        xName = 'Name'

        titlesBefore = self.xAutoTextGroup.getTitles()

        self.xAutoTextGroup.insertNewByName(xName, xName, self.xRange)
        self.assertNotEqual(titlesBefore, self.xAutoTextGroup.getTitles())

        self.xAutoTextGroup.removeByName(xName)
        self.assertEqual(titlesBefore, self.xAutoTextGroup.getTitles())

    def test_insertNewByName_Twice(self):
        xName = 'Name'
        xTitle = 'Title'

        self.xAutoTextGroup.insertNewByName(xName, xTitle, self.xRange)

        with self.assertRaises(ElementExistException):
            self.xAutoTextGroup.insertNewByName(xName, xTitle, self.xRange)

        self.xAutoTextGroup.removeByName(xName)

    def test_renameByName(self):
        xName = 'Name'
        xTitle = 'Title'

        xNewName = 'New Name'
        xNewTitle = 'New Title'

        self.xAutoTextGroup.insertNewByName(xName, xTitle, self.xRange)
        self.xAutoTextGroup.renameByName(xName, xNewName, xNewTitle)

        titlesBefore = self.xAutoTextGroup.getTitles()
        with self.assertRaises(NoSuchElementException):
            self.xAutoTextGroup.removeByName(xName)
        titlesAfter = self.xAutoTextGroup.getTitles()
        self.assertEqual(titlesBefore, titlesAfter)

        self.xAutoTextGroup.removeByName(xNewName)
        titlesAfter2 = self.xAutoTextGroup.getTitles()
        self.assertNotEqual(titlesBefore, titlesAfter2)

    def test_renameByName_Failed(self):
        xName = 'Name'
        xTitle = 'Title'

        xNewName = 'New Name'
        xNewTitle = 'New Title'

        self.xAutoTextGroup.insertNewByName(xName, xTitle, self.xRange)
        self.xAutoTextGroup.insertNewByName(xNewName, xNewTitle, self.xRange)

        with self.assertRaises(ElementExistException):
            self.xAutoTextGroup.renameByName(xName, xNewName, xNewTitle)

        self.xAutoTextGroup.removeByName(xName)
        self.xAutoTextGroup.removeByName(xNewName)

    def test_removeByName_Twice(self):
        xName = 'Name'
        xTitle = 'Title'

        self.xAutoTextGroup.insertNewByName(xName, xTitle, self.xRange)
        self.xAutoTextGroup.removeByName(xName)

        # 2nd attempt should fail
        with self.assertRaises(NoSuchElementException):
            self.xAutoTextGroup.removeByName(xName)

    def test_removeByName_Empty(self):
        with self.assertRaises(NoSuchElementException):
            self.xAutoTextGroup.removeByName('')

    def test_removeByName_NoSuchElement(self):
        with self.assertRaises(NoSuchElementException):
            self.xAutoTextGroup.removeByName('ForSureNotExistName1')

    def createAutoTextContainer(self):
        xServiceManager = self._uno.xContext.ServiceManager
        self.assertIsNotNone(xServiceManager)
        xAutoTextContainer = xServiceManager.createInstance("com.sun.star.text.AutoTextContainer")
        self.assertIsNotNone(xAutoTextContainer)

        return xAutoTextContainer

    def insertNewGroup(self, xAutoTextContainer):
        self.assertIsNotNone(xAutoTextContainer)
        xAutoTextGroup = xAutoTextContainer.insertNewByName(self.GROUP_NAME)
        self.assertIsNotNone(xAutoTextGroup)
        return xAutoTextGroup


if __name__ == '__main__':
    unittest.main()

# vim: set shiftwidth=4 softtabstop=4 expandtab: