Branch libreoffice-5-0-4
[LibreOffice.git] / sw / qa / python / check_table.py
blobc802425f2d31414c87a948d2a2045d21b81207d2
1 import math
2 import unittest
3 from org.libreoffice.unotest import UnoInProcess
4 from com.sun.star.beans import PropertyValue
5 from com.sun.star.uno import RuntimeException
6 from com.sun.star.table import BorderLine
7 from com.sun.star.table import BorderLine2
8 from com.sun.star.table.BorderLineStyle import (DOUBLE, SOLID, EMBOSSED,\
9 THICKTHIN_LARGEGAP, DASHED, DOTTED)
10 from com.sun.star.util import XNumberFormats
11 from com.sun.star.lang import Locale
13 class CheckTable(unittest.TestCase):
14 _uno = None
15 def _fill_table(self, xTable):
16 for x in range(3):
17 for y in range(3):
18 xTable.getCellByPosition(x, y).String = 'Cell %d %d' % (x, y)
19 def _check_table(self, xTable):
20 for x in range(3):
21 for y in range(3):
22 self.assertEqual('Cell %d %d' % (x, y), xTable.getCellByPosition(x, y).String)
23 @classmethod
24 def setUpClass(cls):
25 cls._uno = UnoInProcess()
26 cls._uno.setUp()
27 @classmethod
28 def tearDownClass(cls):
29 cls._uno.tearDown()
31 def test_tableborder(self):
32 xDoc = CheckTable._uno.openEmptyWriterDoc()
33 # insert table
34 xTable = xDoc.createInstance("com.sun.star.text.TextTable")
35 xTable.initialize(3, 3)
36 xText = xDoc.getText()
37 xCursor = xText.createTextCursor()
38 xText.insertTextContent(xCursor, xTable, False)
40 border = xTable.getPropertyValue("TableBorder")
42 self.assertTrue(border.IsTopLineValid)
43 self.assertEqual(0, border.TopLine.InnerLineWidth)
44 self.assertEqual(2, border.TopLine.OuterLineWidth)
45 self.assertEqual(0, border.TopLine.LineDistance)
46 self.assertEqual(0, border.TopLine.Color)
48 self.assertTrue(border.IsBottomLineValid)
49 self.assertEqual(0, border.BottomLine.InnerLineWidth)
50 self.assertEqual(2, border.BottomLine.OuterLineWidth)
51 self.assertEqual(0, border.BottomLine.LineDistance)
52 self.assertEqual(0, border.BottomLine.Color)
54 self.assertTrue(border.IsLeftLineValid)
55 self.assertEqual(0, border.LeftLine.InnerLineWidth)
56 self.assertEqual(2, border.LeftLine.OuterLineWidth)
57 self.assertEqual(0, border.LeftLine.LineDistance)
58 self.assertEqual(0, border.LeftLine.Color)
60 self.assertTrue(border.IsRightLineValid)
61 self.assertEqual(0, border.RightLine.InnerLineWidth)
62 self.assertEqual(2, border.RightLine.OuterLineWidth)
63 self.assertEqual(0, border.RightLine.LineDistance)
64 self.assertEqual(0, border.RightLine.Color)
66 self.assertTrue(border.IsHorizontalLineValid)
67 self.assertEqual(0, border.HorizontalLine.InnerLineWidth)
68 self.assertEqual(2, border.HorizontalLine.OuterLineWidth)
69 self.assertEqual(0, border.HorizontalLine.LineDistance)
70 self.assertEqual(0, border.HorizontalLine.Color)
72 self.assertTrue(border.IsVerticalLineValid)
73 self.assertEqual(0, border.VerticalLine.InnerLineWidth)
74 self.assertEqual(2, border.VerticalLine.OuterLineWidth)
75 self.assertEqual(0, border.VerticalLine.LineDistance)
76 self.assertEqual(0, border.VerticalLine.Color)
78 self.assertTrue(border.IsDistanceValid)
79 self.assertEqual(97, border.Distance)
80 # set border
81 border.TopLine = BorderLine(0, 11, 19, 19)
82 border.BottomLine = BorderLine(0xFF, 00, 11, 00)
83 border.HorizontalLine = BorderLine(0xFF00, 00, 90, 00)
84 xTable.setPropertyValue("TableBorder", border)
85 # read set border
86 border = xTable.getPropertyValue("TableBorder")
88 self.assertTrue(border.IsTopLineValid)
89 self.assertEqual(11, border.TopLine.InnerLineWidth)
90 self.assertEqual(19, border.TopLine.OuterLineWidth)
91 self.assertEqual(19, border.TopLine.LineDistance)
92 self.assertEqual(0, border.TopLine.Color)
94 self.assertTrue(border.IsBottomLineValid)
95 self.assertEqual(0, border.BottomLine.InnerLineWidth)
96 self.assertEqual(11, border.BottomLine.OuterLineWidth)
97 self.assertEqual(0, border.BottomLine.LineDistance)
98 self.assertEqual(0xFF, border.BottomLine.Color)
100 self.assertTrue(border.IsLeftLineValid)
101 self.assertEqual(0, border.LeftLine.InnerLineWidth)
102 self.assertEqual(2, border.LeftLine.OuterLineWidth)
103 self.assertEqual(0, border.LeftLine.LineDistance)
104 self.assertEqual(0, border.LeftLine.Color)
106 self.assertTrue(border.IsRightLineValid)
107 self.assertEqual(0, border.RightLine.InnerLineWidth)
108 self.assertEqual(2, border.RightLine.OuterLineWidth)
109 self.assertEqual(0, border.RightLine.LineDistance)
110 self.assertEqual(0, border.RightLine.Color)
112 self.assertTrue(border.IsHorizontalLineValid)
113 self.assertEqual(0, border.HorizontalLine.InnerLineWidth)
114 self.assertEqual(90, border.HorizontalLine.OuterLineWidth)
115 self.assertEqual(0, border.HorizontalLine.LineDistance)
116 self.assertEqual(0xFF00, border.HorizontalLine.Color)
118 self.assertTrue(border.IsVerticalLineValid)
119 self.assertEqual(0, border.VerticalLine.InnerLineWidth)
120 self.assertEqual(2, border.VerticalLine.OuterLineWidth)
121 self.assertEqual(0, border.VerticalLine.LineDistance)
122 self.assertEqual(0, border.VerticalLine.Color)
124 self.assertTrue(border.IsDistanceValid)
125 self.assertEqual(97, border.Distance)
127 border2 = xTable.getPropertyValue("TableBorder2")
128 self.assertTrue(border2.IsTopLineValid)
129 self.assertEqual(11, border2.TopLine.InnerLineWidth)
130 self.assertEqual(19, border2.TopLine.OuterLineWidth)
131 self.assertEqual(19, border2.TopLine.LineDistance)
132 self.assertEqual(0, border2.TopLine.Color)
133 self.assertEqual(DOUBLE, border2.TopLine.LineStyle)
134 self.assertEqual(49, border2.TopLine.LineWidth)
136 self.assertTrue(border2.IsBottomLineValid)
137 self.assertEqual(0, border2.BottomLine.InnerLineWidth)
138 self.assertEqual(11, border2.BottomLine.OuterLineWidth)
139 self.assertEqual(0, border2.BottomLine.LineDistance)
140 self.assertEqual(0xFF, border2.BottomLine.Color)
141 self.assertEqual(SOLID, border2.BottomLine.LineStyle)
142 self.assertEqual(11, border2.BottomLine.LineWidth)
144 self.assertTrue(border2.IsLeftLineValid)
145 self.assertEqual(0, border2.LeftLine.InnerLineWidth)
146 self.assertEqual(2, border2.LeftLine.OuterLineWidth)
147 self.assertEqual(0, border2.LeftLine.LineDistance)
148 self.assertEqual(0, border2.LeftLine.Color)
149 self.assertEqual(SOLID, border2.LeftLine.LineStyle)
150 self.assertEqual(2, border2.LeftLine.LineWidth)
152 self.assertTrue(border2.IsRightLineValid)
153 self.assertEqual(0, border2.RightLine.InnerLineWidth)
154 self.assertEqual(2, border2.RightLine.OuterLineWidth)
155 self.assertEqual(0, border2.RightLine.LineDistance)
156 self.assertEqual(0, border2.RightLine.Color)
157 self.assertEqual(SOLID, border2.RightLine.LineStyle)
158 self.assertEqual(2, border2.RightLine.LineWidth)
160 self.assertTrue(border2.IsHorizontalLineValid)
161 self.assertEqual(0, border2.HorizontalLine.InnerLineWidth)
162 self.assertEqual(90, border2.HorizontalLine.OuterLineWidth)
163 self.assertEqual(0, border2.HorizontalLine.LineDistance)
164 self.assertEqual(0xFF00, border2.HorizontalLine.Color)
165 self.assertEqual(SOLID, border2.HorizontalLine.LineStyle)
166 self.assertEqual(90, border2.HorizontalLine.LineWidth)
168 self.assertTrue(border2.IsVerticalLineValid)
169 self.assertEqual(0, border2.VerticalLine.InnerLineWidth)
170 self.assertEqual(2, border2.VerticalLine.OuterLineWidth)
171 self.assertEqual(0, border2.VerticalLine.LineDistance)
172 self.assertEqual(0, border2.VerticalLine.Color)
173 self.assertEqual(SOLID, border2.VerticalLine.LineStyle)
174 self.assertEqual(2, border2.VerticalLine.LineWidth)
176 self.assertTrue(border2.IsDistanceValid)
177 self.assertEqual(97, border2.Distance)
178 # set border2
179 border2.RightLine = BorderLine2(0, 0, 0, 0, THICKTHIN_LARGEGAP, 120)
180 border2.LeftLine = BorderLine2(0, 0, 0, 0, EMBOSSED, 90)
181 border2.VerticalLine = BorderLine2(0xFF, 0, 90, 0, DOTTED, 0)
182 border2.HorizontalLine = BorderLine2(0xFF00, 0, 0, 0, DASHED, 11)
183 xTable.setPropertyValue("TableBorder2", border2)
184 # read set border2
185 border2 = xTable.getPropertyValue("TableBorder2")
187 self.assertTrue(border2.IsTopLineValid)
188 self.assertEqual(11, border2.TopLine.InnerLineWidth)
189 self.assertEqual(19, border2.TopLine.OuterLineWidth)
190 self.assertEqual(19, border2.TopLine.LineDistance)
191 self.assertEqual(0, border2.TopLine.Color)
192 self.assertEqual(DOUBLE, border2.TopLine.LineStyle)
193 self.assertEqual(49, border2.TopLine.LineWidth)
195 self.assertTrue(border2.IsBottomLineValid)
196 self.assertEqual(0, border2.BottomLine.InnerLineWidth)
197 self.assertEqual(11, border2.BottomLine.OuterLineWidth)
198 self.assertEqual(0, border2.BottomLine.LineDistance)
199 self.assertEqual(0xFF, border2.BottomLine.Color)
200 self.assertEqual(SOLID, border2.BottomLine.LineStyle)
201 self.assertEqual(11, border2.BottomLine.LineWidth)
203 self.assertTrue(border2.IsLeftLineValid)
204 self.assertEqual(23, border2.LeftLine.InnerLineWidth)
205 self.assertEqual(23, border2.LeftLine.OuterLineWidth)
206 self.assertEqual(46, border2.LeftLine.LineDistance)
207 self.assertEqual(0, border2.LeftLine.Color)
208 self.assertEqual(EMBOSSED, border2.LeftLine.LineStyle)
209 self.assertEqual(90, border2.LeftLine.LineWidth)
211 self.assertTrue(border2.IsRightLineValid)
212 self.assertEqual(53, border2.RightLine.InnerLineWidth)
213 self.assertEqual(26, border2.RightLine.OuterLineWidth)
214 self.assertEqual(41, border2.RightLine.LineDistance)
215 self.assertEqual(0, border2.RightLine.Color)
216 self.assertEqual(THICKTHIN_LARGEGAP, border2.RightLine.LineStyle)
217 self.assertEqual(120, border2.RightLine.LineWidth)
219 self.assertTrue(border2.IsHorizontalLineValid)
220 self.assertEqual(0, border2.HorizontalLine.InnerLineWidth)
221 self.assertEqual(11, border2.HorizontalLine.OuterLineWidth)
222 self.assertEqual(0, border2.HorizontalLine.LineDistance)
223 self.assertEqual(0xFF00, border2.HorizontalLine.Color)
224 self.assertEqual(DASHED, border2.HorizontalLine.LineStyle)
225 self.assertEqual(11, border2.HorizontalLine.LineWidth)
227 self.assertTrue(border2.IsVerticalLineValid)
228 self.assertEqual(0, border2.VerticalLine.InnerLineWidth)
229 self.assertEqual(90, border2.VerticalLine.OuterLineWidth)
230 self.assertEqual(0, border2.VerticalLine.LineDistance)
231 self.assertEqual(0xFF, border2.VerticalLine.Color)
232 self.assertEqual(DOTTED, border2.VerticalLine.LineStyle)
233 self.assertEqual(90, border2.VerticalLine.LineWidth)
235 self.assertTrue(border2.IsDistanceValid)
236 self.assertEqual(97, border2.Distance)
238 # close document
239 xDoc.dispose()
241 def test_fdo58242(self):
242 xDoc = CheckTable._uno.openEmptyWriterDoc()
243 # insert table
244 xTable = xDoc.createInstance("com.sun.star.text.TextTable")
245 xTable.initialize(3, 3)
246 xText = xDoc.getText()
247 xCursor = xText.createTextCursor()
248 xText.insertTextContent(xCursor, xTable, False)
249 # get anchor
250 xAnchor = xTable.getAnchor()
252 # check all properties on the anchor - shouldn't crash despite
253 # pointing to a non-SwTextNode
254 xPropsInfo = xAnchor.getPropertySetInfo()
255 for i in xPropsInfo.getProperties():
256 try:
257 xAnchor.getPropertyValue(i.Name)
258 except RuntimeException:
259 pass
260 # close document
261 xDoc.dispose()
263 def test_descriptions(self):
264 xDoc = CheckTable._uno.openEmptyWriterDoc()
265 # insert table
266 xTable = xDoc.createInstance("com.sun.star.text.TextTable")
267 xTable.initialize(3, 3)
268 xCursor = xDoc.Text.createTextCursor()
269 xDoc.Text.insertTextContent(xCursor, xTable, False)
270 self.assertEqual(3, xTable.Rows.Count)
271 self.assertEqual(3, xTable.Columns.Count)
272 # fill table
273 self._fill_table(xTable)
274 self._check_table(xTable)
275 # check without labels first
276 xTable.ChartColumnAsLabel = False
277 xTable.ChartRowAsLabel = False
278 self.assertEqual(0, len(xTable.RowDescriptions))
279 self.assertEqual(0, len(xTable.ColumnDescriptions))
280 self.RowDescriptions = ('foo', 'bar', 'baz') # no labels, thus noop
281 self.ColumnDescriptions = ('foo', 'bar', 'baz') # no labels, thus noop
282 self._check_table(xTable)
283 # now check with labels
284 xTable.ChartColumnAsLabel = True
285 xTable.ChartRowAsLabel = True
286 self.assertEqual(2, len(xTable.RowDescriptions))
287 self.assertEqual('Cell 0 1', xTable.RowDescriptions[0])
288 self.assertEqual('Cell 0 2', xTable.RowDescriptions[1])
289 self.assertEqual(2, len(xTable.ColumnDescriptions))
290 self.assertEqual('Cell 1 0', xTable.ColumnDescriptions[0])
291 self.assertEqual('Cell 2 0', xTable.ColumnDescriptions[1])
292 with self.assertRaises(Exception):
293 xTable.RowDescriptions = ('foo',) # too short
294 with self.assertRaises(Exception):
295 xTable.ColumnDescriptions = ('foo',) # too short
296 self._check_table(xTable)
297 xTable.RowDescriptions = ('fooRow', 'bazRow')
298 xTable.ColumnDescriptions = ('fooColumn', 'bazColumn')
299 self.assertEqual('fooRow', xTable.getCellByPosition(0,1).String)
300 self.assertEqual('bazRow', xTable.getCellByPosition(0,2).String)
301 self.assertEqual('fooColumn', xTable.getCellByPosition(1,0).String)
302 self.assertEqual('bazColumn', xTable.getCellByPosition(2,0).String)
303 xTable.getCellByPosition(0,1).String = 'Cell 0 1' # reset changes values ...
304 xTable.getCellByPosition(0,2).String = 'Cell 0 2'
305 xTable.getCellByPosition(1,0).String = 'Cell 1 0'
306 xTable.getCellByPosition(2,0).String = 'Cell 2 0'
307 self._check_table(xTable) # ... to ensure the rest was untouched
308 # check disconnected table excepts, but doesnt crash
309 xTable2 = xDoc.createInstance("com.sun.star.text.TextTable")
310 xTable2.initialize(3, 3)
311 with self.assertRaises(Exception):
312 foo = xTable2.RowDescriptions
313 with self.assertRaises(Exception):
314 foo = xTable2.ColumnDescriptions
315 xDoc.dispose()
317 def test_getset_data(self):
318 xDoc = CheckTable._uno.openEmptyWriterDoc()
319 # insert table
320 xTable = xDoc.createInstance("com.sun.star.text.TextTable")
321 xTable.initialize(4, 3)
322 xCursor = xDoc.Text.createTextCursor()
323 xDoc.Text.insertTextContent(xCursor, xTable, False)
324 xTable.ChartColumnAsLabel = False
325 xTable.ChartRowAsLabel = False
326 # roundtrip
327 xTable.Data = ((1,2,3), (4,5,6), (7,8,9), (10,11,12))
328 self.assertEqual( xTable.Data, ((1,2,3), (4,5,6), (7,8,9), (10,11,12)))
329 # missing row
330 with self.assertRaises(Exception):
331 xTable.Data = ((1,2,3), (4,5,6), (7,8,9))
332 # missing column
333 with self.assertRaises(Exception):
334 xTable.Data = ((1,2), (4,5), (7,8), (10,11))
335 # with labels
336 xTable.ChartColumnAsLabel = True
337 xTable.ChartRowAsLabel = True
338 self.assertEqual( xTable.Data, ((5,6), (8,9), (11,12)))
339 xTable.Data = ((55,66), (88,99), (1111,1212))
340 xTable.ChartColumnAsLabel = True
341 xTable.ChartRowAsLabel = False
342 self.assertEqual( xTable.Data, ((2,3), (55,66), (88,99), (1111,1212)))
343 xTable.ChartColumnAsLabel = False
344 xTable.ChartRowAsLabel = True
345 self.assertEqual( xTable.Data, ((4,55,66), (7,88,99), (10,1111,1212)))
346 xTable.ChartColumnAsLabel = False
347 xTable.ChartRowAsLabel = False
348 self.assertEqual( xTable.Data, ((1,2,3), (4,55,66), (7,88,99), (10,1111,1212)))
349 xDoc.dispose()
351 def test_tdf32082(self):
352 xDoc = CheckTable._uno.openEmptyWriterDoc()
353 xDocFrame = xDoc.CurrentController.Frame
354 xContext = CheckTable._uno.getContext()
355 xServiceManager = xContext.ServiceManager
356 xDispatcher = xServiceManager.createInstanceWithContext(
357 'com.sun.star.frame.DispatchHelper', xContext)
358 xTable = xDoc.createInstance("com.sun.star.text.TextTable")
359 xTable.initialize(1,1)
360 xCursor = xDoc.Text.createTextCursor()
361 xDoc.Text.insertTextContent(xCursor, xTable, False)
362 # Setup numberformat for the cell
363 xNumberFormats = xDoc.NumberFormats
364 xLocale = Locale('en', 'US', '')
365 formatString = '#,##0.00 [$€-407];[RED]-#,##0.00 [$€-407]'
366 key = xNumberFormats.queryKey(formatString, xLocale, True)
367 if key == -1:
368 key = xNumberFormats.addNew(formatString, xLocale)
369 # Apply the format on the first cell
370 xTable.getCellByPosition(0,0).NumberFormat = key
371 xDispatcher.executeDispatch(xDocFrame, '.uno:GoToStartOfDoc', '', 0, ())
372 xDispatcher.executeDispatch(xDocFrame, '.uno:InsertText', '', 0,
373 (PropertyValue('Text', 0, '3', 0),))
374 xDispatcher.executeDispatch(xDocFrame, '.uno:JumpToNextCell', '', 0, ())
375 # Check that the formatting we set up is not destroyed
376 self.assertEquals(xTable.getCellByPosition(0,0).getString(), '3.00 €')
377 self.assertEquals(xTable.getCellByPosition(0,0).getValue(), 3)
378 # Verify that it works with number recognition turned on as well
379 xDispatcher.executeDispatch(xDocFrame, '.uno:TableNumberRecognition', '', 0,
380 (PropertyValue('TableNumberRecognition', 0, True, 0),))
381 xDispatcher.executeDispatch(xDocFrame, '.uno:InsertText', '', 0,
382 (PropertyValue('Text', 0, '4', 0),))
383 xDispatcher.executeDispatch(xDocFrame, '.uno:JumpToNextCell', '', 0, ())
384 self.assertEquals(xTable.getCellByPosition(0,1).getString(), '4.00 €')
385 self.assertEquals(xTable.getCellByPosition(0,1).getValue(), 4)
386 xDoc.dispose()
388 def test_numberRecognition(self):
389 xDoc = CheckTable._uno.openEmptyWriterDoc()
390 xDocFrame = xDoc.CurrentController.Frame
391 xContext = CheckTable._uno.getContext()
392 xServiceManager = xContext.ServiceManager
393 xDispatcher = xServiceManager.createInstanceWithContext(
394 'com.sun.star.frame.DispatchHelper', xContext)
395 xTable = xDoc.createInstance('com.sun.star.text.TextTable')
396 xTable.initialize(2,1)
397 xCursor = xDoc.Text.createTextCursor()
398 xDoc.Text.insertTextContent(xCursor, xTable, False)
399 xDispatcher.executeDispatch(xDocFrame, '.uno:GoToStartOfDoc', '', 0, ())
400 xDispatcher.executeDispatch(xDocFrame, '.uno:InsertText', '', 0,
401 (PropertyValue('Text', 0, '15-10-30', 0),))
402 xDispatcher.executeDispatch(xDocFrame, '.uno:JumpToNextCell', '', 0, ())
403 # Without number recognition 15-10-30 should not be interperated as a date
404 self.assertEquals(xTable.getCellByPosition(0,0).getString(), '15-10-30')
405 self.assertEquals(xTable.getCellByPosition(0,0).getValue(), 0)
406 # Activate number recognition
407 xDispatcher.executeDispatch(xDocFrame, '.uno:TableNumberRecognition', '', 0,
408 (PropertyValue('TableNumberRecognition', 0, True, 0),))
409 xDispatcher.executeDispatch(xDocFrame, '.uno:InsertText', '', 0,
410 (PropertyValue('Text', 0, '15-10-30', 0),))
411 xDispatcher.executeDispatch(xDocFrame, '.uno:JumpToNextCell', '', 0, ())
412 # With number recognition it should now be a date, confirm by checking
413 # the string and value of the cell.
414 self.assertEquals(xTable.getCellByPosition(0,1).getString(), '2015-10-30')
415 self.assertEquals(xTable.getCellByPosition(0,1).getValue(), 42307.0)
416 xDoc.dispose()
417 if __name__ == '__main__':
418 unittest.main()
420 # /* vim:set shiftwidth=4 softtabstop=4 expandtab: */