Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sw / qa / python / check_table.py
blob35da08fe8195786ea777f375bf495f8e7276c5ac
1 #! /usr/bin/env python
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/.
10 import math
11 import unittest
12 from org.libreoffice.unotest import UnoInProcess
13 from com.sun.star.beans import PropertyValue
14 from com.sun.star.uno import RuntimeException
15 from com.sun.star.table import BorderLine
16 from com.sun.star.table import BorderLine2
17 from com.sun.star.table.BorderLineStyle import (DOUBLE, SOLID, EMBOSSED,
18 THICKTHIN_LARGEGAP, DASHED, DOTTED)
19 from com.sun.star.util import XNumberFormats
20 from com.sun.star.lang import Locale
23 class CheckTable(unittest.TestCase):
25 def _fill_table(self, xTable):
27 for x in range(3):
28 for y in range(3):
29 xTable[y,x].String = 'Cell %d %d' % (x, y)
31 def _check_table(self, xTable):
33 for x in range(3):
34 for y in range(3):
35 self.assertEqual('Cell %d %d' % (x, y), xTable[y,x].String)
37 @classmethod
38 def setUpClass(cls):
39 cls._uno = UnoInProcess()
40 cls._uno.setUp()
41 cls.OOLineHairline = 2
43 @classmethod
44 def tearDownClass(cls):
45 cls._uno.tearDown()
47 def __test_borderAsserts(self, xBorderLine, line_valid):
48 self.assertTrue(line_valid)
49 self.assertEqual(0, xBorderLine.InnerLineWidth)
50 self.assertEqual(self.OOLineHairline, xBorderLine.OuterLineWidth)
51 self.assertEqual(0, xBorderLine.LineDistance)
52 self.assertEqual(0, xBorderLine.Color)
54 def __test_borderAssertsWithLineStyle(self, xBorderLine, line_valid):
55 self.__test_borderAsserts(xBorderLine, line_valid)
56 self.assertEqual(self.OOLineHairline, xBorderLine.LineWidth)
57 self.assertEqual(SOLID, xBorderLine.LineStyle)
59 def __test_borderDistance(self, border):
60 self.assertTrue(border.IsDistanceValid)
61 self.assertEqual(97, border.Distance)
63 def test_tableborder(self):
64 xDoc = CheckTable._uno.openEmptyWriterDoc()
65 # insert table
66 xTable = xDoc.createInstance("com.sun.star.text.TextTable")
67 xTable.initialize(3, 3)
68 xText = xDoc.getText()
69 xCursor = xText.createTextCursor()
70 xText.insertTextContent(xCursor, xTable, False)
72 border_distances = xTable.TableBorderDistances
74 self.assertEqual(97, border_distances.TopDistance)
75 self.assertEqual(97, border_distances.BottomDistance)
76 self.assertEqual(97, border_distances.LeftDistance)
77 self.assertEqual(97, border_distances.RightDistance)
79 self.assertEqual(True, border_distances.IsTopDistanceValid)
80 self.assertEqual(True, border_distances.IsBottomDistanceValid)
81 self.assertEqual(True, border_distances.IsLeftDistanceValid)
82 self.assertEqual(True, border_distances.IsRightDistanceValid)
84 border = xTable.getPropertyValue("TableBorder")
86 self.__test_borderAsserts(border.TopLine, border.IsTopLineValid)
87 self.__test_borderAsserts(border.BottomLine, border.IsBottomLineValid)
88 self.__test_borderAsserts(border.LeftLine, border.IsLeftLineValid)
89 self.__test_borderAsserts(border.RightLine, border.IsRightLineValid)
90 self.__test_borderAsserts(border.HorizontalLine, border.IsHorizontalLineValid)
91 self.__test_borderAsserts(border.VerticalLine, border.IsVerticalLineValid)
93 self.__test_borderDistance(border)
95 # set border
96 border.TopLine = BorderLine(0, 11, 19, 19)
97 border.BottomLine = BorderLine(0xFF, 00, 11, 00)
98 border.HorizontalLine = BorderLine(0xFF00, 00, 90, 00)
99 xTable.setPropertyValue("TableBorder", border)
100 # read set border
101 border = xTable.getPropertyValue("TableBorder")
103 self.assertTrue(border.IsTopLineValid)
104 self.assertEqual(11, border.TopLine.InnerLineWidth)
105 self.assertEqual(19, border.TopLine.OuterLineWidth)
106 self.assertEqual(19, border.TopLine.LineDistance)
107 self.assertEqual(0, border.TopLine.Color)
109 self.assertTrue(border.IsBottomLineValid)
110 self.assertEqual(0, border.BottomLine.InnerLineWidth)
111 self.assertEqual(11, border.BottomLine.OuterLineWidth)
112 self.assertEqual(0, border.BottomLine.LineDistance)
113 self.assertEqual(0xFF, border.BottomLine.Color)
115 self.__test_borderAsserts(border.LeftLine, border.IsLeftLineValid)
117 self.__test_borderAsserts(border.RightLine, border.IsRightLineValid)
119 self.assertTrue(border.IsHorizontalLineValid)
120 self.assertEqual(0, border.HorizontalLine.InnerLineWidth)
121 self.assertEqual(90, border.HorizontalLine.OuterLineWidth)
122 self.assertEqual(0, border.HorizontalLine.LineDistance)
123 self.assertEqual(0xFF00, border.HorizontalLine.Color)
125 self.__test_borderAsserts(border.VerticalLine, border.IsVerticalLineValid)
127 self.__test_borderDistance(border)
129 border2 = xTable.getPropertyValue("TableBorder2")
130 self.assertTrue(border2.IsTopLineValid)
131 self.assertEqual(11, border2.TopLine.InnerLineWidth)
132 self.assertEqual(19, border2.TopLine.OuterLineWidth)
133 self.assertEqual(19, border2.TopLine.LineDistance)
134 self.assertEqual(0, border2.TopLine.Color)
135 self.assertEqual(DOUBLE, border2.TopLine.LineStyle)
136 self.assertEqual(49, border2.TopLine.LineWidth)
138 self.assertTrue(border2.IsBottomLineValid)
139 self.assertEqual(0, border2.BottomLine.InnerLineWidth)
140 self.assertEqual(11, border2.BottomLine.OuterLineWidth)
141 self.assertEqual(0, border2.BottomLine.LineDistance)
142 self.assertEqual(0xFF, border2.BottomLine.Color)
143 self.assertEqual(SOLID, border2.BottomLine.LineStyle)
144 self.assertEqual(11, border2.BottomLine.LineWidth)
146 self.__test_borderAssertsWithLineStyle(border2.LeftLine, border2.IsLeftLineValid)
148 self.__test_borderAssertsWithLineStyle(border2.RightLine, border2.IsRightLineValid)
150 self.assertTrue(border2.IsHorizontalLineValid)
151 self.assertEqual(0, border2.HorizontalLine.InnerLineWidth)
152 self.assertEqual(90, border2.HorizontalLine.OuterLineWidth)
153 self.assertEqual(0, border2.HorizontalLine.LineDistance)
154 self.assertEqual(0xFF00, border2.HorizontalLine.Color)
155 self.assertEqual(SOLID, border2.HorizontalLine.LineStyle)
156 self.assertEqual(90, border2.HorizontalLine.LineWidth)
158 self.__test_borderAssertsWithLineStyle(border2.VerticalLine, border2.IsVerticalLineValid)
160 self.__test_borderDistance(border2)
161 # set border2
162 border2.RightLine = BorderLine2(0, 0, 0, 0, THICKTHIN_LARGEGAP, 120)
163 border2.LeftLine = BorderLine2(0, 0, 0, 0, EMBOSSED, 90)
164 border2.VerticalLine = BorderLine2(0xFF, 0, 90, 0, DOTTED, 0)
165 border2.HorizontalLine = BorderLine2(0xFF00, 0, 0, 0, DASHED, 11)
166 xTable.setPropertyValue("TableBorder2", border2)
167 # read set border2
168 border2 = xTable.getPropertyValue("TableBorder2")
170 self.assertTrue(border2.IsTopLineValid)
171 self.assertEqual(11, border2.TopLine.InnerLineWidth)
172 self.assertEqual(19, border2.TopLine.OuterLineWidth)
173 self.assertEqual(19, border2.TopLine.LineDistance)
174 self.assertEqual(0, border2.TopLine.Color)
175 self.assertEqual(DOUBLE, border2.TopLine.LineStyle)
176 self.assertEqual(49, border2.TopLine.LineWidth)
178 self.assertTrue(border2.IsBottomLineValid)
179 self.assertEqual(0, border2.BottomLine.InnerLineWidth)
180 self.assertEqual(11, border2.BottomLine.OuterLineWidth)
181 self.assertEqual(0, border2.BottomLine.LineDistance)
182 self.assertEqual(0xFF, border2.BottomLine.Color)
183 self.assertEqual(SOLID, border2.BottomLine.LineStyle)
184 self.assertEqual(11, border2.BottomLine.LineWidth)
186 self.assertTrue(border2.IsLeftLineValid)
187 self.assertEqual(23, border2.LeftLine.InnerLineWidth)
188 self.assertEqual(23, border2.LeftLine.OuterLineWidth)
189 self.assertEqual(46, border2.LeftLine.LineDistance)
190 self.assertEqual(0, border2.LeftLine.Color)
191 self.assertEqual(EMBOSSED, border2.LeftLine.LineStyle)
192 self.assertEqual(90, border2.LeftLine.LineWidth)
194 self.assertTrue(border2.IsRightLineValid)
195 self.assertEqual(53, border2.RightLine.InnerLineWidth)
196 self.assertEqual(26, border2.RightLine.OuterLineWidth)
197 self.assertEqual(41, border2.RightLine.LineDistance)
198 self.assertEqual(0, border2.RightLine.Color)
199 self.assertEqual(THICKTHIN_LARGEGAP, border2.RightLine.LineStyle)
200 self.assertEqual(120, border2.RightLine.LineWidth)
202 self.assertTrue(border2.IsHorizontalLineValid)
203 self.assertEqual(0, border2.HorizontalLine.InnerLineWidth)
204 self.assertEqual(11, border2.HorizontalLine.OuterLineWidth)
205 self.assertEqual(0, border2.HorizontalLine.LineDistance)
206 self.assertEqual(0xFF00, border2.HorizontalLine.Color)
207 self.assertEqual(DASHED, border2.HorizontalLine.LineStyle)
208 self.assertEqual(11, border2.HorizontalLine.LineWidth)
210 self.assertTrue(border2.IsVerticalLineValid)
211 self.assertEqual(0, border2.VerticalLine.InnerLineWidth)
212 self.assertEqual(90, border2.VerticalLine.OuterLineWidth)
213 self.assertEqual(0, border2.VerticalLine.LineDistance)
214 self.assertEqual(0xFF, border2.VerticalLine.Color)
215 self.assertEqual(DOTTED, border2.VerticalLine.LineStyle)
216 self.assertEqual(90, border2.VerticalLine.LineWidth)
218 self.__test_borderDistance(border2)
220 # close document
221 xDoc.dispose()
223 def test_fdo58242(self):
224 xDoc = CheckTable._uno.openEmptyWriterDoc()
225 # insert table
226 xTable = xDoc.createInstance("com.sun.star.text.TextTable")
227 xTable.initialize(3, 3)
228 xText = xDoc.getText()
229 xCursor = xText.createTextCursor()
230 xText.insertTextContent(xCursor, xTable, False)
231 # get anchor
232 xAnchor = xTable.getAnchor()
234 # check all properties on the anchor - shouldn't crash despite
235 # pointing to a non-SwTextNode
236 xPropsInfo = xAnchor.getPropertySetInfo()
237 for i in xPropsInfo.getProperties():
238 try:
239 xAnchor.getPropertyValue(i.Name)
240 except RuntimeException:
241 pass
242 # close document
243 xDoc.dispose()
245 def test_descriptions(self):
246 xDoc = CheckTable._uno.openEmptyWriterDoc()
247 # insert table
248 xTable = xDoc.createInstance("com.sun.star.text.TextTable")
249 xTable.initialize(3, 3)
250 xCursor = xDoc.Text.createTextCursor()
251 xDoc.Text.insertTextContent(xCursor, xTable, False)
252 self.assertEqual(3, xTable.Rows.Count)
253 self.assertEqual(3, xTable.Columns.Count)
254 xTable.TableName = "foo"
255 self.assertEqual("foo", xTable.TableName)
256 xTable.TableTemplateName = "bar"
257 self.assertEqual("bar", xTable.TableTemplateName)
258 # fill table
259 self._fill_table(xTable)
260 self._check_table(xTable)
261 # check without labels first
262 xTable.ChartColumnAsLabel = False
263 xTable.ChartRowAsLabel = False
264 self.assertEqual(0, len(xTable.RowDescriptions))
265 self.assertEqual(0, len(xTable.ColumnDescriptions))
266 self.RowDescriptions = ('foo', 'bar', 'baz') # no labels, thus noop
267 self.ColumnDescriptions = ('foo', 'bar', 'baz') # no labels, thus noop
268 self._check_table(xTable)
269 # now check with labels
270 xTable.ChartColumnAsLabel = True
271 xTable.ChartRowAsLabel = True
272 self.assertEqual(2, len(xTable.RowDescriptions))
273 self.assertEqual('Cell 0 1', xTable.RowDescriptions[0])
274 self.assertEqual('Cell 0 2', xTable.RowDescriptions[1])
275 self.assertEqual(2, len(xTable.ColumnDescriptions))
276 self.assertEqual('Cell 1 0', xTable.ColumnDescriptions[0])
277 self.assertEqual('Cell 2 0', xTable.ColumnDescriptions[1])
278 with self.assertRaises(Exception):
279 xTable.RowDescriptions = ('foo',) # too short
280 with self.assertRaises(Exception):
281 xTable.ColumnDescriptions = ('foo',) # too short
282 self._check_table(xTable)
283 xTable.RowDescriptions = ('fooRow', 'bazRow')
284 xTable.ColumnDescriptions = ('fooColumn', 'bazColumn')
285 self.assertEqual('fooRow', xTable[1,0].String)
286 self.assertEqual('bazRow', xTable[2,0].String)
287 self.assertEqual('fooColumn', xTable[0,1].String)
288 self.assertEqual('bazColumn', xTable[0,2].String)
289 xTable[1,0].String = 'Cell 0 1' # reset changes values ...
290 xTable[2,0].String = 'Cell 0 2'
291 xTable[0,1].String = 'Cell 1 0'
292 xTable[0,2].String = 'Cell 2 0'
293 self._check_table(xTable) # ... to ensure the rest was untouched
294 # check disconnected table excepts, but doesn't crash
295 xTable2 = xDoc.createInstance("com.sun.star.text.TextTable")
296 xTable2.initialize(3, 3)
297 with self.assertRaises(Exception):
298 foo = xTable2.RowDescriptions
299 with self.assertRaises(Exception):
300 foo = xTable2.ColumnDescriptions
301 xDoc.dispose()
303 def test_getset_data(self):
304 xDoc = CheckTable._uno.openEmptyWriterDoc()
305 # insert table
306 xTable = xDoc.createInstance("com.sun.star.text.TextTable")
307 xTable.initialize(4, 3)
308 xCursor = xDoc.Text.createTextCursor()
309 xDoc.Text.insertTextContent(xCursor, xTable, False)
310 xTable.ChartColumnAsLabel = False
311 xTable.ChartRowAsLabel = False
312 # roundtrip
313 xTable.Data = ((1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12))
314 self.assertEqual(xTable.Data, ((1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12)))
315 # missing row
316 with self.assertRaises(Exception):
317 xTable.Data = ((1, 2, 3), (4, 5, 6), (7, 8, 9))
318 # missing column
319 with self.assertRaises(Exception):
320 xTable.Data = ((1, 2), (4, 5), (7, 8), (10, 11))
321 # with labels
322 xTable.ChartColumnAsLabel = True
323 xTable.ChartRowAsLabel = True
324 self.assertEqual(xTable.Data, ((5, 6), (8, 9), (11, 12)))
325 xTable.Data = ((55, 66), (88, 99), (1111, 1212))
326 xTable.ChartColumnAsLabel = True
327 xTable.ChartRowAsLabel = False
328 self.assertEqual(xTable.Data, ((2, 3), (55, 66), (88, 99), (1111, 1212)))
329 xTable.ChartColumnAsLabel = False
330 xTable.ChartRowAsLabel = True
331 self.assertEqual(xTable.Data, ((4, 55, 66), (7, 88, 99), (10, 1111, 1212)))
332 xTable.ChartColumnAsLabel = False
333 xTable.ChartRowAsLabel = False
334 self.assertEqual(xTable.Data, ((1, 2, 3), (4, 55, 66), (7, 88, 99), (10, 1111, 1212)))
335 xDoc.dispose()
337 def test_remove_colrow(self):
338 xDoc = CheckTable._uno.openEmptyWriterDoc()
339 xTable = xDoc.createInstance("com.sun.star.text.TextTable")
340 xTable.initialize(4, 3)
341 xCursor = xDoc.Text.createTextCursor()
342 xDoc.Text.insertTextContent(xCursor, xTable, False)
343 xTable.ChartColumnAsLabel = False
344 xTable.ChartRowAsLabel = False
345 xTable.Data = ((1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12))
346 xRows = xTable.Rows
347 self.assertEqual(xRows.ImplementationName, 'SwXTableRows')
348 self.assertTrue(xRows.supportsService('com.sun.star.text.TableRows'))
349 self.assertFalse(xRows.supportsService('foo'))
350 self.assertIn('com.sun.star.text.TableRows', xRows.SupportedServiceNames)
351 self.assertNotIn('foo', xRows.SupportedServiceNames)
352 xRows.removeByIndex(1, 2)
353 self.assertEqual(xTable.Data, ((1, 2, 3), (10, 11, 12)))
354 xCols = xTable.Columns
355 self.assertEqual(xCols.ImplementationName, 'SwXTableColumns')
356 self.assertTrue(xCols.supportsService('com.sun.star.text.TableColumns'))
357 self.assertFalse(xCols.supportsService('foo'))
358 self.assertIn('com.sun.star.text.TableColumns', xCols.SupportedServiceNames)
359 self.assertNotIn('foo', xCols.SupportedServiceNames)
360 xCols.removeByIndex(1, 1)
361 self.assertEqual(xTable.Data, ((1, 3), (10, 12)))
362 xDoc.dispose()
364 def test_insert_colrow(self):
365 xDoc = CheckTable._uno.openEmptyWriterDoc()
366 xTable = xDoc.createInstance("com.sun.star.text.TextTable")
367 xTable.initialize(4, 3)
368 xCursor = xDoc.Text.createTextCursor()
369 xDoc.Text.insertTextContent(xCursor, xTable, False)
370 xTable.ChartColumnAsLabel = False
371 xTable.ChartRowAsLabel = False
372 xTable.Data = ((1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12))
373 xRows = xTable.Rows
374 xRows.insertByIndex(1, 2)
375 nan = float('nan')
376 self.assertEqual(xTable.Data[0], (1, 2, 3))
377 self.assertEqual(xTable.Data[3], (4, 5, 6))
378 self.assertEqual(xTable.Data[4], (7, 8, 9))
379 self.assertEqual(xTable.Data[5], (10, 11, 12))
380 for x in range(3):
381 self.assertTrue(math.isnan(xTable.Data[1][x]))
382 self.assertTrue(math.isnan(xTable.Data[2][x]))
383 xCols = xTable.Columns
384 xCols.insertByIndex(1, 1)
385 self.assertEqual(xTable.Data[0][0], 1)
386 self.assertTrue(math.isnan(xTable.Data[0][1]))
387 self.assertEqual(xTable.Data[0][2], 2)
388 self.assertEqual(xTable.Data[0][3], 3)
389 self.assertEqual(xTable.Data[3][0], 4)
390 self.assertTrue(math.isnan(xTable.Data[3][1]))
391 self.assertEqual(xTable.Data[3][2], 5)
392 self.assertEqual(xTable.Data[3][3], 6)
393 self.assertEqual(xTable.Data[4][0], 7)
394 self.assertTrue(math.isnan(xTable.Data[4][1]))
395 self.assertEqual(xTable.Data[4][2], 8)
396 self.assertEqual(xTable.Data[4][3], 9)
397 self.assertEqual(xTable.Data[5][0], 10)
398 self.assertTrue(math.isnan(xTable.Data[5][1]))
399 self.assertEqual(xTable.Data[5][2], 11)
400 self.assertEqual(xTable.Data[5][3], 12)
401 for x in range(4):
402 self.assertTrue(math.isnan(xTable.Data[1][x]))
403 self.assertTrue(math.isnan(xTable.Data[2][x]))
404 xDoc.dispose()
406 def test_chartdataprovider(self):
407 xDoc = CheckTable._uno.openEmptyWriterDoc()
408 xTable = xDoc.createInstance("com.sun.star.text.TextTable")
409 xTable.initialize(4, 3)
410 xCursor = xDoc.Text.createTextCursor()
411 xDoc.Text.insertTextContent(xCursor, xTable, False)
412 xTable.ChartColumnAsLabel = False
413 xTable.ChartRowAsLabel = False
414 xTable.Data = ((1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12))
415 self.assertTrue(xTable.Name == 'Table1')
416 self.assertIn('com.sun.star.text.GenericTextDocument', xDoc.SupportedServiceNames)
417 xChartdataprovider = xDoc.createInstance('com.sun.star.chart2.data.DataProvider')
418 self.assertEqual(xChartdataprovider.ImplementationName, 'SwChartDataProvider')
419 self.assertTrue(xChartdataprovider.supportsService('com.sun.star.chart2.data.DataProvider'))
420 self.assertFalse(xChartdataprovider.supportsService('foo'))
421 self.assertIn('com.sun.star.chart2.data.DataProvider', xChartdataprovider.SupportedServiceNames)
422 pv = PropertyValue()
423 pv.Name = 'CellRangeRepresentation'
424 pv.Value = 'Table1.A1:C2'
425 xDataSource = xChartdataprovider.createDataSource((pv,))
426 self.assertEqual(len(xDataSource.DataSequences), 3)
427 expected_values = ((1, 4), (2, 5), (3, 6))
428 expected_cellrange = ('A1:A2', 'B1:B2', 'C1:C2')
430 for col in range(3):
431 xSeq = xDataSource.DataSequences[col].Values
432 self.assertEqual(xSeq.ImplementationName, 'SwChartDataSequence')
433 self.assertTrue(xSeq.supportsService('com.sun.star.chart2.data.DataSequence'))
434 self.assertFalse(xSeq.supportsService('foo'))
435 self.assertIn('com.sun.star.chart2.data.DataSequence', xSeq.SupportedServiceNames)
436 self.assertEqual(xSeq.SourceRangeRepresentation, 'Table1.%s' % expected_cellrange[col])
437 self.assertEqual(xSeq.Data, expected_values[col])
438 self.assertEqual(xSeq.NumericalData, expected_values[col])
439 self.assertEqual(
440 [int(txtval) for txtval in xSeq.TextualData],
441 [val for val in expected_values[col]])
443 xSeq.Role = "One xSeq to rule them all"
444 self.assertEqual("One xSeq to rule them all", xSeq.Role)
446 xSeqClone = xSeq.createClone()
447 self.assertEqual(xSeq.Role, xSeqClone.Role)
449 xDoc.dispose()
451 def test_tdf32082(self):
452 xDoc = CheckTable._uno.openEmptyWriterDoc()
453 xDocFrame = xDoc.CurrentController.Frame
454 xContext = CheckTable._uno.getContext()
455 xServiceManager = xContext.ServiceManager
456 xDispatcher = xServiceManager.createInstanceWithContext(
457 'com.sun.star.frame.DispatchHelper', xContext)
458 xTable = xDoc.createInstance("com.sun.star.text.TextTable")
459 xTable.initialize(1, 1)
460 xCursor = xDoc.Text.createTextCursor()
461 xDoc.Text.insertTextContent(xCursor, xTable, False)
462 # Setup numberformat for the cell
463 xNumberFormats = xDoc.NumberFormats
464 xLocale = Locale('en', 'US', '')
465 format_string = '#,##0.00 [$€-407];[RED]-#,##0.00 [$€-407]'
466 key = xNumberFormats.queryKey(format_string, xLocale, True)
467 if key == -1:
468 key = xNumberFormats.addNew(format_string, xLocale)
469 # Apply the format on the first cell
470 xTable[0,0].NumberFormat = key
471 xDispatcher.executeDispatch(xDocFrame, '.uno:GoToStartOfDoc', '', 0, ())
472 xDispatcher.executeDispatch(xDocFrame, '.uno:InsertText', '', 0,
473 (PropertyValue('Text', 0, '3', 0),))
474 xDispatcher.executeDispatch(xDocFrame, '.uno:JumpToNextCell', '', 0, ())
475 # Check that the formatting we set up is not destroyed
476 self.assertEqual(xTable[0,0].getString(), '3.00 €')
477 self.assertEqual(xTable[0,0].getValue(), 3)
478 # Verify that it works with number recognition turned on as well
479 xDispatcher.executeDispatch(xDocFrame, '.uno:TableNumberRecognition', '', 0,
480 (PropertyValue('TableNumberRecognition', 0, True, 0),))
481 xDispatcher.executeDispatch(xDocFrame, '.uno:InsertText', '', 0,
482 (PropertyValue('Text', 0, '4', 0),))
483 xDispatcher.executeDispatch(xDocFrame, '.uno:JumpToNextCell', '', 0, ())
484 self.assertEqual(xTable[1,0].getString(), '4.00 €')
485 self.assertEqual(xTable[1,0].getValue(), 4)
486 xDoc.dispose()
488 def test_numberRecognition(self):
489 xDoc = CheckTable._uno.openEmptyWriterDoc()
490 xDocFrame = xDoc.CurrentController.Frame
491 xContext = CheckTable._uno.getContext()
492 xServiceManager = xContext.ServiceManager
493 xDispatcher = xServiceManager.createInstanceWithContext(
494 'com.sun.star.frame.DispatchHelper', xContext)
495 xTable = xDoc.createInstance('com.sun.star.text.TextTable')
496 xTable.initialize(2, 1)
497 xCursor = xDoc.Text.createTextCursor()
498 xDoc.Text.insertTextContent(xCursor, xTable, False)
499 xDispatcher.executeDispatch(xDocFrame, '.uno:GoToStartOfDoc', '', 0, ())
500 xDispatcher.executeDispatch(xDocFrame, '.uno:InsertText', '', 0,
501 (PropertyValue('Text', 0, '2015-10-30', 0),))
502 xDispatcher.executeDispatch(xDocFrame, '.uno:JumpToNextCell', '', 0, ())
503 # Without number recognition 2015-10-30 should not be interpreted as a date
504 self.assertEqual(xTable[0,0].getString(), '2015-10-30')
505 self.assertEqual(xTable[0,0].getValue(), 0)
506 # Activate number recognition
507 xDispatcher.executeDispatch(xDocFrame, '.uno:TableNumberRecognition', '', 0,
508 (PropertyValue('TableNumberRecognition', 0, True, 0),))
509 xDispatcher.executeDispatch(xDocFrame, '.uno:InsertText', '', 0,
510 (PropertyValue('Text', 0, '2015-10-30', 0),))
511 xDispatcher.executeDispatch(xDocFrame, '.uno:JumpToNextCell', '', 0, ())
512 # With number recognition it should now be a date, confirm by checking
513 # the string and value of the cell.
514 self.assertEqual(xTable[1,0].getString(), '2015-10-30')
515 self.assertEqual(xTable[1,0].getValue(), 42307.0)
516 xDoc.dispose()
518 def test_tableTemplate(self):
519 xDoc = CheckTable._uno.openEmptyWriterDoc()
520 xTable = xDoc.createInstance("com.sun.star.text.TextTable")
521 xTable.initialize(1, 1)
522 xTable.setPropertyValue("TableTemplateName", "Default")
523 self.assertEqual(xTable.getPropertyValue("TableTemplateName"), "Default")
524 xTable.setPropertyValue("TableTemplateName", "other_style")
525 self.assertEqual(xTable.getPropertyValue("TableTemplateName"), "other_style")
526 xTable.setPropertyValue("TableTemplateName", "")
527 self.assertEqual(xTable.getPropertyValue("TableTemplateName"), "")
528 xDoc.dispose()
531 def test_unoNames(self):
532 xDoc = CheckTable._uno.openEmptyWriterDoc()
533 xTable = xDoc.createInstance("com.sun.star.text.TextTable")
534 xTable.initialize(3, 3)
535 xText = xDoc.getText()
536 xCursor = xText.createTextCursor()
537 xText.insertTextContent(xCursor, xTable, False)
539 self.assertEqual("SwXTextTable", xTable.ImplementationName)
540 self.assertEqual(("com.sun.star.document.LinkTarget",
541 "com.sun.star.text.TextTable",
542 "com.sun.star.text.TextContent",
543 "com.sun.star.text.TextSortable"), xTable.SupportedServiceNames)
544 self.assertEqual(b'', xTable.ImplementationId.value)
546 xCell = xTable[1,1]
547 self.assertEqual("SwXCell", xCell.ImplementationName)
548 self.assertEqual(("com.sun.star.text.CellProperties",), xCell.SupportedServiceNames)
549 self.assertEqual(b'', xCell.ImplementationId.value)
551 xRow = xTable.Rows[0]
552 self.assertEqual("SwXTextTableRow", xRow.ImplementationName)
553 self.assertEqual(("com.sun.star.text.TextTableRow",), xRow.SupportedServiceNames)
554 self.assertEqual(b'', xRow.ImplementationId.value)
556 xTableCursor = xTable.createCursorByCellName("A1")
557 self.assertEqual("SwXTextTableCursor", xTableCursor.ImplementationName)
558 self.assertEqual(("com.sun.star.text.TextTableCursor",), xTableCursor.SupportedServiceNames)
559 self.assertEqual(b'', xTableCursor.ImplementationId.value)
561 xDoc.dispose()
563 def test_xmlRangeConversions(self):
564 xDoc = CheckTable._uno.openEmptyWriterDoc()
565 xTable = xDoc.createInstance("com.sun.star.text.TextTable")
566 xTable.initialize(4, 3)
567 xCursor = xDoc.Text.createTextCursor()
568 xDoc.Text.insertTextContent(xCursor, xTable, False)
569 xTable.ChartColumnAsLabel = False
570 xTable.ChartRowAsLabel = False
571 xTable.Data = ((1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12))
573 xChartDataProvider = xDoc.createInstance('com.sun.star.chart2.data.DataProvider')
575 self.assertEqual('', xChartDataProvider.convertRangeToXML(''))
576 self.assertEqual('', xChartDataProvider.convertRangeFromXML(''))
577 self.assertEqual('.A1;.A1', xChartDataProvider.convertRangeFromXML('<some xml>'))
579 xml = xChartDataProvider.convertRangeToXML('Table1.A1:C3')
581 self.assertEqual("Table1.$A$1:.$C$3", xml)
583 xCellRangeString = xChartDataProvider.convertRangeFromXML("Table1.$A$1:.$C$3")
584 self.assertEqual("Table1.A1:C3", xCellRangeString)
586 def test_splitRangeHorizontal(self):
587 xDoc = CheckTable._uno.openEmptyWriterDoc()
588 xTable = xDoc.createInstance("com.sun.star.text.TextTable")
589 xTable.initialize(2, 2)
590 xText = xDoc.getText()
591 xCursor = xText.createTextCursor()
592 xText.insertTextContent(xCursor, xTable, False)
593 xTable.Data = ((1, 2), (3, 4))
594 xCursor = xTable.createCursorByCellName("A1")
595 xCursor.splitRange(2, True)
596 self.assertEqual(len(xTable.Data), 4)
597 self.assertEqual(xTable.Data[0], (float(1), float(2)))
598 self.assertEqual(xTable.Data[3], (float(3), float(4)))
599 self.assertTrue(math.isnan(xTable.Data[1][0]))
600 self.assertTrue(math.isnan(xTable.Data[1][1]))
601 self.assertTrue(math.isnan(xTable.Data[2][0]))
602 self.assertTrue(math.isnan(xTable.Data[2][1]))
604 def test_mergeRangeHorizontal(self):
605 xDoc = CheckTable._uno.openEmptyWriterDoc()
606 xTable = xDoc.createInstance("com.sun.star.text.TextTable")
607 xTable.initialize(3, 3)
608 xText = xDoc.getText()
609 xCursor = xText.createTextCursor()
610 xText.insertTextContent(xCursor, xTable, False)
611 xTable.Data = ((1, 2, 3), (4, 5, 6), (7, 8, 9))
612 xCursor = xTable.createCursorByCellName("A1")
613 xCursor.goDown(1, True)
614 xCursor.mergeRange()
615 self.assertEqual(len(xTable.Data), 3)
616 self.assertEqual(xTable.Data[0], (float(1), float(2), float(3)))
617 self.assertTrue(math.isnan(xTable.Data[1][0]))
618 self.assertEqual(xTable.Data[1][1], float(5))
619 self.assertEqual(xTable.Data[1][2], float(6))
620 self.assertEqual(xTable.Data[2], (float(7), float(8), float(9)))
622 if __name__ == '__main__':
623 unittest.main()
625 # vim: set shiftwidth=4 softtabstop=4 expandtab: