Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sw / qa / python / check_bookmarks.py
blob73fd9bb6b98e2d23910c4443d53c9d575c31f0b6
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 # This file incorporates work covered by the following license notice:
12 # Licensed to the Apache Software Foundation (ASF) under one or more
13 # contributor license agreements. See the NOTICE file distributed
14 # with this work for additional information regarding copyright
15 # ownership. The ASF licenses this file to you under the Apache
16 # License, Version 2.0 (the "License"); you may not use this file
17 # except in compliance with the License. You may obtain a copy of
18 # the License at http://www.apache.org/licenses/LICENSE-2.0.
20 import unittest
21 import random
22 import os.path
23 from hashlib import sha1
24 from tempfile import TemporaryDirectory
25 from org.libreoffice.unotest import UnoInProcess, mkPropertyValues, systemPathToFileUrl
26 from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
29 class CheckBookmarks(unittest.TestCase):
30 expectedHashes = {
31 'nSetupHash': 0x8f88ee1a13a55d6024f58f470723b5174dfa21bb,
32 'nInsertRandomHash': 0x5f27e87e16d2cb3ff0bcb24237aa30da3b84cf24,
33 'nDeleteRandomHash': 0x1bdaa7773cbfc73a4dc0bb3e0f801b98f648e8e7,
34 'nLinebreakHash': 0x3f30a35f195efcfe0373a3e439de05087a2ad37c,
35 'nOdfReloadHash': 0x3f30a35f195efcfe0373a3e439de05087a2ad37c,
36 'nMsWordReloadHash': 0x3f30a35f195efcfe0373a3e439de05087a2ad37c,
39 @classmethod
40 def setUpClass(cls):
41 cls._uno = UnoInProcess()
42 cls._uno.setUp()
43 cls._xDoc = cls._uno.openEmptyWriterDoc()
44 smgr = cls._uno.xContext.ServiceManager
45 cls._desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", cls._uno.xContext)
47 @classmethod
48 def tearDownClass(cls):
49 cls._uno.tearDown()
51 def test_bookmarks(self):
52 self.xDoc = self.__class__._xDoc
53 self.xText = self.xDoc.getText()
54 ## setting and testing bookmarks
55 self.setupBookmarks()
56 self.assertEqual(self.expectedHashes['nSetupHash'],
57 self.getBookmarksHash(self.xDoc))
58 ## modifying bookmarks and testing again
59 self.insertRandomParts(200177)
60 self.assertEqual(self.expectedHashes['nInsertRandomHash'],
61 self.getBookmarksHash(self.xDoc))
62 ## modifying bookmarks and testing again
63 self.deleteRandomParts(4711)
64 self.assertEqual(self.expectedHashes['nDeleteRandomHash'],
65 self.getBookmarksHash(self.xDoc))
66 ## adding line breaks and testing again
67 self.insertLinebreaks(7)
68 self.assertEqual(self.expectedHashes['nLinebreakHash'],
69 self.getBookmarksHash(self.xDoc))
70 ## reloading document and testing again
71 with TemporaryDirectory() as tempdir:
72 xOdfReloadedDoc = self.reloadFrom(tempdir, "writer8", "odt")
73 self.assertEqual(self.expectedHashes['nOdfReloadHash'],
74 self.getBookmarksHash(xOdfReloadedDoc))
75 xOdfReloadedDoc.close(True)
76 ## reloading document as MS Word 97 doc and testing again
77 ## MsWord Hash is unstable over different systems
78 # xMsWordReloadedDoc = self.reloadFrom(tempdir, "MS Word 97", "doc")
79 # self.assertEqual(self.expectedHashes['nMsWordReloadHash'],
80 # self.getBookmarksHash(xMsWordReloadedDoc))
81 # xMsWordReloadedDoc.close(True)
82 print('tests ok')
84 def setupBookmarks(self):
85 xCursor = self.xText.createTextCursor()
86 for nPara in range(10):
87 for nBookmark in range(100):
88 s = "P{}word{}".format(nPara, nBookmark)
89 xCursor.gotoEnd(False)
90 xCursor.setString(s)
91 xBookmark = self.xDoc.createInstance("com.sun.star.text.Bookmark")
92 xBookmark.setName(s)
93 self.xText.insertTextContent(xCursor, xBookmark, True)
94 xCursor.End.setString(" ")
95 self.xText.insertControlCharacter(xCursor.End, PARAGRAPH_BREAK, False)
97 def getBookmarksHash(self, doc):
98 hash = sha1()
99 xBookmarks = doc.getBookmarks()
100 for xBookmark in xBookmarks:
101 s = '{}:{};'.format(xBookmark.Name,
102 xBookmark.getAnchor().getString().replace("\r\n", "\n"))
103 hash.update(str.encode(s))
104 return int(hash.hexdigest(), 16)
106 def insertRandomParts(self, seed):
107 random.seed(seed)
108 xCursor = self.xText.createTextCursor()
109 for i in range(600):
110 xCursor.goRight(random.randrange(100), False)
111 xCursor.setString(str(random.getrandbits(64)))
113 def deleteRandomParts(self, seed):
114 random.seed(seed)
115 xCursor = self.xText.createTextCursor()
116 for i in range(600):
117 xCursor.goRight(random.randrange(100), False)
118 xCursor.goRight(random.randrange(20), True)
119 xCursor.setString("")
121 def insertLinebreaks(self, seed):
122 random.seed(seed)
123 xCursor = self.xText.createTextCursor()
124 for i in range(30):
125 xCursor.goRight(random.randrange(300), False)
126 self.xText.insertControlCharacter(xCursor, PARAGRAPH_BREAK, False)
128 def reloadFrom(self, tempdir, sFilter, sExtension):
129 sFileUrl = os.path.join(tempdir, "Bookmarktest.{}".format(sExtension))
130 sFileUrl = systemPathToFileUrl(sFileUrl)
131 store_props = mkPropertyValues(Override=True, FilterName=sFilter)
132 self.xDoc.storeToURL(sFileUrl, store_props)
133 desktop = self.__class__._desktop
134 load_props = mkPropertyValues(Hidden=True, ReadOnly=False)
135 return desktop.loadComponentFromURL(sFileUrl, "_default", 0, load_props)
138 if __name__ == '__main__':
139 unittest.main()