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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
#! /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 NoSuchElementException
from com.sun.star.lang import IllegalArgumentException
from com.sun.star.uno import RuntimeException
class XAutoTextContainer(unittest.TestCase):
# 0 indicates the path of the Office Basis layer
# 1 indicates the path of the user directory
GROUP_POSTFIX = '*1'
@classmethod
def setUpClass(self):
self._uno = UnoInProcess()
self._uno.setUp()
self._uno.openEmptyWriterDoc()
@classmethod
def tearDownClass(self):
self._uno.tearDown()
def test_insertNewByName(self):
# initialization
xAutoTextContainer = self.createAutoTextContainer()
if xAutoTextContainer is None:
return
# perform unit test
# group name must contain a-z, A-z, 0-9, '_', ' ' only
xNames = ['Name', 'TEST', 'Name2', '_With_underscore_', 'with space', '123456']
for xName in xNames:
xAutoTextContainer.insertNewByName(xName+self.GROUP_POSTFIX)
xAutoTextContainer.removeByName(xName+self.GROUP_POSTFIX)
def test_insertNewByName_Spaces(self):
# initialization
xAutoTextContainer = self.createAutoTextContainer()
if xAutoTextContainer is None:
return
# perform unit test
# add
xName = ' spaces '
xAutoTextContainer.insertNewByName(xName+self.GROUP_POSTFIX)
# try to remove
with self.assertRaises(NoSuchElementException):
xAutoTextContainer.removeByName(xName+self.GROUP_POSTFIX)
# remove trimmed
xAutoTextContainer.removeByName('spaces'+self.GROUP_POSTFIX)
def test_insertNewByName_Several(self):
# initialization
xAutoTextContainer = self.createAutoTextContainer()
if xAutoTextContainer is None:
return
# perform unit test
xAutoTextGroup1 = xAutoTextContainer.insertNewByName(
"atc_name1"+self.GROUP_POSTFIX)
xAutoTextGroup2 = xAutoTextContainer.insertNewByName(
"atc_name2"+self.GROUP_POSTFIX)
xAutoTextGroup3 = xAutoTextContainer.insertNewByName(
"atc_name3"+self.GROUP_POSTFIX)
self.assertEqual("atc_name1"+self.GROUP_POSTFIX, xAutoTextGroup1.getName())
self.assertEqual("atc_name2"+self.GROUP_POSTFIX, xAutoTextGroup2.getName())
self.assertEqual("atc_name3"+self.GROUP_POSTFIX, xAutoTextGroup3.getName())
xAutoTextContainer.removeByName("atc_name1"+self.GROUP_POSTFIX)
xAutoTextContainer.removeByName("atc_name2"+self.GROUP_POSTFIX)
xAutoTextContainer.removeByName("atc_name3"+self.GROUP_POSTFIX)
def test_insertNewByName_DifferentCase(self):
# initialization
xAutoTextContainer = self.createAutoTextContainer()
if xAutoTextContainer is None:
return
# perform unit test
xAutoTextGroup1 = xAutoTextContainer.insertNewByName("myname"+self.GROUP_POSTFIX)
xAutoTextGroup2 = xAutoTextContainer.insertNewByName("MYNAME"+self.GROUP_POSTFIX)
xAutoTextGroup3 = xAutoTextContainer.insertNewByName("MyName"+self.GROUP_POSTFIX)
self.assertEqual("myname"+self.GROUP_POSTFIX, xAutoTextGroup1.getName())
# Note: different platforms could support different cases
# in container names
validName2 = False
validName2 |= (xAutoTextGroup2.getName() == "MYNAME"+self.GROUP_POSTFIX)
validName2 |= (xAutoTextGroup2.getName()[:5] == "group")
validName3 = False
validName3 |= (xAutoTextGroup3.getName() == "MyName"+self.GROUP_POSTFIX)
validName3 |= (xAutoTextGroup3.getName()[:5] == "group")
self.assertTrue(validName2)
self.assertTrue(validName3)
xAutoTextContainer.removeByName("myname"+self.GROUP_POSTFIX)
xName = xAutoTextGroup2.getName()
xName = xName[:xName.find('*')]
xAutoTextContainer.removeByName(xName)
xName = xAutoTextGroup3.getName()
xName = xName[:xName.find('*')]
xAutoTextContainer.removeByName(xName)
def test_insertNewByName_Failed(self):
# initialization
xAutoTextContainer = self.createAutoTextContainer()
if xAutoTextContainer is None:
return
# perform unit test
# group name must contain a-z, A-z, 0-9, '_', ' ' only
xNames = ['', 'Name!!!', 'Red & White', 'Name.With.Dot', 'Name-2', 'A1:B1']
for xName in xNames:
with self.assertRaises(IllegalArgumentException):
xAutoTextContainer.insertNewByName(xName)
def test_removeByName_Unknown(self):
# initialization
xAutoTextContainer = self.createAutoTextContainer()
if xAutoTextContainer is None:
return
# perform unit test
with self.assertRaises(NoSuchElementException):
xAutoTextContainer.removeByName("Some Unknown Name")
def test_removeByName_DifferentCases(self):
# initialization
xAutoTextContainer = self.createAutoTextContainer()
if xAutoTextContainer is None:
return
# perform unit test
xAutoTextContainer.insertNewByName('GroupName'+self.GROUP_POSTFIX)
with self.assertRaises(NoSuchElementException):
xAutoTextContainer.removeByName('groupname'+self.GROUP_POSTFIX)
with self.assertRaises(NoSuchElementException):
xAutoTextContainer.removeByName('GROUPNAME'+self.GROUP_POSTFIX)
xAutoTextContainer.removeByName('GroupName'+self.GROUP_POSTFIX)
def createAutoTextContainer(self):
xServiceManager = self._uno.xContext.ServiceManager
self.assertIsNotNone(xServiceManager)
xAutoTextContainer = xServiceManager.createInstance(
"com.sun.star.text.AutoTextContainer")
self.assertIsNotNone(xAutoTextContainer)
# Note that in some systems the user may lack of
# write access to the Office Basis directory
xAutoTextGroup = xAutoTextContainer.insertNewByName(
"_PermCheck1"+self.GROUP_POSTFIX)
try:
titles = xAutoTextGroup.getTitles()
except RuntimeException:
return None
xAutoTextContainer.removeByName("_PermCheck1"+self.GROUP_POSTFIX)
# ok, we have permissions
return xAutoTextContainer
if __name__ == '__main__':
unittest.main()
# vim: set shiftwidth=4 softtabstop=4 expandtab:
|