2 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
4 # This file is part of the LibreOffice project.
6 # This Source Code Form is subject to the terms of the Mozilla Public
7 # License, v. 2.0. If a copy of the MPL was not distributed with this
8 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 from org
.libreoffice
.unotest
import UnoInProcess
12 from com
.sun
.star
.container
import ElementExistException
13 from com
.sun
.star
.container
import NoSuchElementException
16 class XAutoTextGroup(unittest
.TestCase
):
17 # 0 indicates the path of the Office Basis layer
18 # 1 indicates the path of the user directory
19 GROUP_NAME
= 'atg_name1*1'
23 cls
._uno
= UnoInProcess()
25 cls
._uno
.openEmptyWriterDoc()
28 self
.xAutoTextContainer
= self
.createAutoTextContainer()
29 self
.xAutoTextGroup
= self
.insertNewGroup(self
.xAutoTextContainer
)
31 self
.xText
= self
._uno
.getDoc().getText()
32 self
.xCursor
= self
.xText
.createTextCursor()
33 self
.xRange
= self
.xCursor
.getStart()
36 self
.xAutoTextContainer
.removeByName(self
.GROUP_NAME
)
39 def tearDownClass(cls
):
42 def test_XAutoTextGroup(self
):
46 titlesBefore
= self
.xAutoTextGroup
.getTitles()
48 self
.xAutoTextGroup
.insertNewByName(xName
, xTitle
, self
.xRange
)
49 self
.assertNotEqual(titlesBefore
, self
.xAutoTextGroup
.getTitles())
51 self
.xAutoTextGroup
.removeByName(xName
)
52 self
.assertEqual(titlesBefore
, self
.xAutoTextGroup
.getTitles())
54 def test_XAutoTextGroup_NoTitle(self
):
57 titlesBefore
= self
.xAutoTextGroup
.getTitles()
59 self
.xAutoTextGroup
.insertNewByName(xName
, xName
, self
.xRange
)
60 self
.assertNotEqual(titlesBefore
, self
.xAutoTextGroup
.getTitles())
62 self
.xAutoTextGroup
.removeByName(xName
)
63 self
.assertEqual(titlesBefore
, self
.xAutoTextGroup
.getTitles())
65 def test_insertNewByName_Twice(self
):
69 self
.xAutoTextGroup
.insertNewByName(xName
, xTitle
, self
.xRange
)
71 with self
.assertRaises(ElementExistException
):
72 self
.xAutoTextGroup
.insertNewByName(xName
, xTitle
, self
.xRange
)
74 self
.xAutoTextGroup
.removeByName(xName
)
76 def test_renameByName(self
):
81 xNewTitle
= 'New Title'
83 self
.xAutoTextGroup
.insertNewByName(xName
, xTitle
, self
.xRange
)
84 self
.xAutoTextGroup
.renameByName(xName
, xNewName
, xNewTitle
)
86 titlesBefore
= self
.xAutoTextGroup
.getTitles()
87 with self
.assertRaises(NoSuchElementException
):
88 self
.xAutoTextGroup
.removeByName(xName
)
89 titlesAfter
= self
.xAutoTextGroup
.getTitles()
90 self
.assertEqual(titlesBefore
, titlesAfter
)
92 self
.xAutoTextGroup
.removeByName(xNewName
)
93 titlesAfter2
= self
.xAutoTextGroup
.getTitles()
94 self
.assertNotEqual(titlesBefore
, titlesAfter2
)
96 def test_renameByName_Failed(self
):
100 xNewName
= 'New Name'
101 xNewTitle
= 'New Title'
103 self
.xAutoTextGroup
.insertNewByName(xName
, xTitle
, self
.xRange
)
104 self
.xAutoTextGroup
.insertNewByName(xNewName
, xNewTitle
, self
.xRange
)
106 with self
.assertRaises(ElementExistException
):
107 self
.xAutoTextGroup
.renameByName(xName
, xNewName
, xNewTitle
)
109 self
.xAutoTextGroup
.removeByName(xName
)
110 self
.xAutoTextGroup
.removeByName(xNewName
)
112 def test_removeByName_Twice(self
):
116 self
.xAutoTextGroup
.insertNewByName(xName
, xTitle
, self
.xRange
)
117 self
.xAutoTextGroup
.removeByName(xName
)
119 # 2nd attempt should fail
120 with self
.assertRaises(NoSuchElementException
):
121 self
.xAutoTextGroup
.removeByName(xName
)
123 def test_removeByName_Empty(self
):
124 with self
.assertRaises(NoSuchElementException
):
125 self
.xAutoTextGroup
.removeByName('')
127 def test_removeByName_NoSuchElement(self
):
128 with self
.assertRaises(NoSuchElementException
):
129 self
.xAutoTextGroup
.removeByName('ForSureNotExistName1')
131 def createAutoTextContainer(self
):
132 xServiceManager
= self
._uno
.xContext
.ServiceManager
133 self
.assertIsNotNone(xServiceManager
)
134 xAutoTextContainer
= xServiceManager
.createInstance("com.sun.star.text.AutoTextContainer")
135 self
.assertIsNotNone(xAutoTextContainer
)
137 return xAutoTextContainer
139 def insertNewGroup(self
, xAutoTextContainer
):
140 self
.assertIsNotNone(xAutoTextContainer
)
141 xAutoTextGroup
= xAutoTextContainer
.insertNewByName(self
.GROUP_NAME
)
142 self
.assertIsNotNone(xAutoTextGroup
)
143 return xAutoTextGroup
146 if __name__
== '__main__':
149 # vim: set shiftwidth=4 softtabstop=4 expandtab: