sw/JunitTest_sw_complex.mk                       |    2 
 sw/PythonTest_sw_python.mk                       |    1 
 sw/qa/complex/writer/CheckBookmarks.java         |  243 -----------------------
 sw/qa/python/check_bookmarks.py                  |  139 +++++++++++++
 unotest/source/python/org/libreoffice/unotest.py |   12 +
 5 files changed, 152 insertions(+), 245 deletions(-)

New commits:
commit 52523f39aff9730a308eaca07a3c487829e36e8d
Author: jmzambon <jeanmarczam...@gmail.com>
Date:   Fri May 26 17:52:14 2017 +0200

    tdf#97362: CheckBookmarks unittest migrated to python (check_bookmarks.py)
    
    Change-Id: Ie5fa41d4346d921ed905795b76ea3c3e90b3d4a2
    Reviewed-on: https://gerrit.libreoffice.org/38080
    Tested-by: Jenkins <c...@libreoffice.org>
    Reviewed-by: Michael Meeks <michael.me...@collabora.com>

diff --git a/sw/JunitTest_sw_complex.mk b/sw/JunitTest_sw_complex.mk
index 9e11a75a8b7d..67eb2711350e 100644
--- a/sw/JunitTest_sw_complex.mk
+++ b/sw/JunitTest_sw_complex.mk
@@ -27,7 +27,6 @@ $(eval $(call gb_JunitTest_set_defs,sw_complex,\
 $(eval $(call gb_JunitTest_add_sourcefiles,sw_complex,\
     sw/qa/complex/accessibility/AccessibleRelationSet \
     sw/qa/complex/indeterminateState/CheckIndeterminateState \
-    sw/qa/complex/writer/CheckBookmarks \
     sw/qa/complex/writer/TestDocument \
     sw/qa/complex/writer/TextPortionEnumerationTest \
 ))
@@ -42,7 +41,6 @@ $(eval $(call gb_JunitTest_use_jars,sw_complex,\
 
 $(eval $(call gb_JunitTest_add_classes,sw_complex,\
     complex.accessibility.AccessibleRelationSet \
-    complex.writer.CheckBookmarks \
     complex.writer.TextPortionEnumerationTest \
 ))
 
diff --git a/sw/PythonTest_sw_python.mk b/sw/PythonTest_sw_python.mk
index 774f6f8aba42..6676cff2da74 100644
--- a/sw/PythonTest_sw_python.mk
+++ b/sw/PythonTest_sw_python.mk
@@ -14,6 +14,7 @@ $(eval $(call gb_PythonTest_set_defs,sw_python,\
 ))
 
 $(eval $(call gb_PythonTest_add_modules,sw_python,$(SRCDIR)/sw/qa/python,\
+       check_bookmarks \
        check_change_color \
        check_index \
        check_flies \
diff --git a/sw/qa/complex/writer/CheckBookmarks.java 
b/sw/qa/complex/writer/CheckBookmarks.java
deleted file mode 100644
index bec743056a95..000000000000
--- a/sw/qa/complex/writer/CheckBookmarks.java
+++ /dev/null
@@ -1,243 +0,0 @@
-/*
- * 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/.
- *
- * This file incorporates work covered by the following license notice:
- *
- *   Licensed to the Apache Software Foundation (ASF) under one or more
- *   contributor license agreements. See the NOTICE file distributed
- *   with this work for additional information regarding copyright
- *   ownership. The ASF licenses this file to you under the Apache
- *   License, Version 2.0 (the "License"); you may not use this file
- *   except in compliance with the License. You may obtain a copy of
- *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
- */
-
-package complex.writer;
-
-import com.sun.star.beans.PropertyValue;
-import com.sun.star.container.XNamed;
-import com.sun.star.container.XNameAccess;
-import com.sun.star.frame.XStorable;
-import com.sun.star.lang.XMultiServiceFactory;
-import com.sun.star.text.XBookmarksSupplier;
-import com.sun.star.text.XSimpleText;
-import com.sun.star.text.XText;
-import com.sun.star.text.XTextContent;
-import com.sun.star.text.XTextCursor;
-import com.sun.star.text.XTextDocument;
-import com.sun.star.text.XTextRange;
-import com.sun.star.uno.UnoRuntime;
-import java.math.BigInteger;
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.openoffice.test.OfficeConnection;
-import static org.junit.Assert.*;
-
-class BookmarkHashes {
-    public BigInteger m_nSetupHash;
-    public BigInteger m_nInsertRandomHash;
-    public BigInteger m_nDeleteRandomHash;
-    public BigInteger m_nLinebreakHash;
-    public BigInteger m_nOdfReloadHash;
-    private BigInteger m_nMsWordReloadHash;
-
-    public void assertExpectation(BookmarkHashes aExpectation) {
-        assertEquals(aExpectation.m_nSetupHash, m_nSetupHash);
-        assertEquals(aExpectation.m_nInsertRandomHash, m_nInsertRandomHash);
-        assertEquals(aExpectation.m_nDeleteRandomHash, m_nDeleteRandomHash);
-        assertEquals(aExpectation.m_nLinebreakHash, m_nLinebreakHash);
-        assertEquals(aExpectation.m_nOdfReloadHash, m_nOdfReloadHash);
-        assertEquals(aExpectation.m_nMsWordReloadHash, m_nMsWordReloadHash);
-    }
-
-    public static java.math.BigInteger getBookmarksHash(XTextDocument xDoc)
-        throws com.sun.star.uno.Exception, 
java.security.NoSuchAlgorithmException
-    {
-        StringBuffer buffer = new StringBuffer("");
-        XBookmarksSupplier xBookmarksSupplier = UnoRuntime.queryInterface(
-            XBookmarksSupplier.class,
-            xDoc);
-        XNameAccess xBookmarks = xBookmarksSupplier.getBookmarks();
-        for(String sBookmarkname : xBookmarks.getElementNames()) {
-            Object xBookmark = xBookmarks.getByName(sBookmarkname);
-            XTextContent xBookmarkAsContent = UnoRuntime.queryInterface(
-                XTextContent.class,
-                xBookmark);
-            buffer.append(sBookmarkname);
-            buffer.append(':');
-            
buffer.append(xBookmarkAsContent.getAnchor().getString().replaceAll("\r\n", 
"\n"));
-            buffer.append(';');
-        }
-        java.security.MessageDigest sha1 = 
java.security.MessageDigest.getInstance("SHA-1");
-        sha1.reset();
-        sha1.update(buffer.toString().getBytes());
-        return new java.math.BigInteger(sha1.digest());
-    }
-}
-
-public class CheckBookmarks {
-    private XMultiServiceFactory m_xMsf = null;
-    private XTextDocument m_xDoc = null;
-    private XTextDocument m_xOdfReloadedDoc = null;
-    private final BookmarkHashes actualHashes = new BookmarkHashes();
-
-    private BookmarkHashes get20111110Expectations() {
-        BookmarkHashes result = new BookmarkHashes();
-        result.m_nSetupHash = new 
BigInteger("-4b0706744e8452fe1ae9d5e1c28cf70fb6194795",16);
-        result.m_nInsertRandomHash = new 
BigInteger("25aa0fad3f4881832dcdfe658ec2efa8a1a02bc5",16);
-        result.m_nDeleteRandomHash = new 
BigInteger("-3ec87e810b46d734677c351ad893bbbf9ea10f55",16);
-        result.m_nLinebreakHash = new 
BigInteger("3ae08c284ea0d6e738cb43c0a8105e718a633550",16);
-        result.m_nOdfReloadHash = new 
BigInteger("3ae08c284ea0d6e738cb43c0a8105e718a633550",16);
-        // MsWord Hash is unstable over different systems
-       // result.m_nMsWordReloadHash = new 
BigInteger("3ae08c284ea0d6e738cb43c0a8105e718a633550",16);
-        return result;
-    }
-
-    @Test public void checkBookmarks()
-    {
-        actualHashes.assertExpectation(get20111110Expectations());
-    }
-
-    @Before public void setUpDocuments() throws Exception {
-        m_xMsf = UnoRuntime.queryInterface(
-            XMultiServiceFactory.class,
-            connection.getComponentContext().getServiceManager());
-        m_xDoc = util.WriterTools.createTextDoc(m_xMsf);
-        setupBookmarks();
-        actualHashes.m_nSetupHash = BookmarkHashes.getBookmarksHash(m_xDoc);
-        insertRandomParts(200177);
-        actualHashes.m_nInsertRandomHash = 
BookmarkHashes.getBookmarksHash(m_xDoc);
-        deleteRandomParts(4711);
-        actualHashes.m_nDeleteRandomHash = 
BookmarkHashes.getBookmarksHash(m_xDoc);
-        insertLinebreaks(7);
-        actualHashes.m_nLinebreakHash = 
BookmarkHashes.getBookmarksHash(m_xDoc);
-        m_xOdfReloadedDoc = reloadFrom("writer8", "odf");
-        actualHashes.m_nOdfReloadHash = 
BookmarkHashes.getBookmarksHash(m_xOdfReloadedDoc);
-        //m_xMsWordReloadedDoc = reloadFrom("MS Word 97", "doc");
-        //actualHashes.m_nMsWordReloadHash = 
BookmarkHashes.getBookmarksHash(m_xMsWordReloadedDoc);
-    }
-
-    @After public void tearDownDocuments() {
-        util.DesktopTools.closeDoc(m_xDoc);
-        util.DesktopTools.closeDoc(m_xOdfReloadedDoc);
-        //util.DesktopTools.closeDoc(m_xMsWordReloadedDoc);
-    }
-
-    @BeforeClass public static void setUpConnection() throws Exception {
-        connection.setUp();
-    }
-
-    @AfterClass public static void tearDownConnection()
-        throws InterruptedException, com.sun.star.uno.Exception
-    {
-        connection.tearDown();
-    }
-
-    private static final OfficeConnection connection = new OfficeConnection();
-
-    private void setupBookmarks()
-        throws com.sun.star.uno.Exception
-    {
-        XText xText = m_xDoc.getText();
-        UnoRuntime.queryInterface(XSimpleText.class, xText);
-        for(int nPara=0; nPara<10; ++nPara) {
-            for(int nBookmark=0; nBookmark<100; ++nBookmark){
-                insertBookmark(
-                    xText.createTextCursor(),
-                    "P" + nPara + "word" + nBookmark,
-                    "P" + nPara + "word" + nBookmark);
-                XTextCursor xWordCursor = xText.createTextCursor();
-                xWordCursor.setString(" ");
-            }
-            XTextCursor xParaCursor = xText.createTextCursor();
-            XTextRange xParaCursorAsRange = UnoRuntime.queryInterface(
-                XTextRange.class,
-                xParaCursor);
-            xText.insertControlCharacter(xParaCursorAsRange, 
com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
-        }
-    }
-
-    private void insertRandomParts(long seed)
-    {
-        java.util.Random rnd = new java.util.Random(seed);
-        XTextCursor xCursor = m_xDoc.getText().createTextCursor();
-        for(int i=0; i<600; i++) {
-            xCursor.goRight((short)rnd.nextInt(100), false);
-            xCursor.setString(Long.toString(rnd.nextLong()));
-        }
-    }
-
-    private void deleteRandomParts(long seed)
-    {
-        java.util.Random rnd = new java.util.Random(seed);
-        XTextCursor xCursor = m_xDoc.getText().createTextCursor();
-        for(int i=0; i<600; i++) {
-            xCursor.goRight((short)rnd.nextInt(100), false);
-            xCursor.goRight((short)rnd.nextInt(20), true);
-            xCursor.setString("");
-        }
-    }
-
-    private void insertLinebreaks(long seed)
-    {
-        XText xText = m_xDoc.getText();
-        java.util.Random rnd = new java.util.Random(seed);
-        XTextCursor xCursor = m_xDoc.getText().createTextCursor();
-        for(int i=0; i<30; i++) {
-            xCursor.goRight((short)rnd.nextInt(300), false);
-            XTextRange xCursorAsRange = UnoRuntime.queryInterface(
-                XTextRange.class,
-                xCursor);
-            xText.insertControlCharacter(xCursorAsRange, 
com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
-        }
-    }
-
-    private void insertBookmark(XTextCursor crsr, String name, String content)
-        throws com.sun.star.uno.Exception
-    {
-        XMultiServiceFactory xDocFactory = UnoRuntime.queryInterface(
-            XMultiServiceFactory.class,
-            m_xDoc);
-
-        Object xBookmark = 
xDocFactory.createInstance("com.sun.star.text.Bookmark");
-        XTextContent xBookmarkAsTextContent = UnoRuntime.queryInterface(
-            XTextContent.class,
-            xBookmark);
-        crsr.setString(content);
-        XNamed xBookmarkAsNamed = UnoRuntime.queryInterface(
-            XNamed.class,
-            xBookmark);
-        xBookmarkAsNamed.setName(name);
-        m_xDoc.getText().insertTextContent(crsr, xBookmarkAsTextContent, true);
-    }
-
-    private XTextDocument reloadFrom(String sFilter, String sExtension)
-        throws com.sun.star.io.IOException, com.sun.star.uno.Exception
-    {
-        String sFileUrl = util.utils.getOfficeTemp(m_xMsf) + "/Bookmarktest." 
+ sExtension;
-        try {
-            PropertyValue[] aStoreProperties = new PropertyValue[2];
-            aStoreProperties[0] = new PropertyValue();
-            aStoreProperties[1] = new PropertyValue();
-            aStoreProperties[0].Name = "Override";
-            aStoreProperties[0].Value = true;
-            aStoreProperties[1].Name = "FilterName";
-            aStoreProperties[1].Value = sFilter;
-            XStorable xStorable = UnoRuntime.queryInterface(
-                XStorable.class,
-                m_xDoc);
-            xStorable.storeToURL(sFileUrl, aStoreProperties);
-            return util.WriterTools.loadTextDoc(m_xMsf, sFileUrl);
-        } finally {
-            if(util.utils.fileExists(m_xMsf, sFileUrl))
-                util.utils.deleteFile(m_xMsf, sFileUrl);
-        }
-    }
-}
diff --git a/sw/qa/python/check_bookmarks.py b/sw/qa/python/check_bookmarks.py
new file mode 100644
index 000000000000..73fd9bb6b98e
--- /dev/null
+++ b/sw/qa/python/check_bookmarks.py
@@ -0,0 +1,139 @@
+#! /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/.
+
+# This file incorporates work covered by the following license notice:
+
+#   Licensed to the Apache Software Foundation (ASF) under one or more
+#   contributor license agreements. See the NOTICE file distributed
+#   with this work for additional information regarding copyright
+#   ownership. The ASF licenses this file to you under the Apache
+#   License, Version 2.0 (the "License"); you may not use this file
+#   except in compliance with the License. You may obtain a copy of
+#   the License at http://www.apache.org/licenses/LICENSE-2.0.
+
+import unittest
+import random
+import os.path
+from hashlib import sha1
+from tempfile import TemporaryDirectory
+from org.libreoffice.unotest import UnoInProcess, mkPropertyValues, 
systemPathToFileUrl
+from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
+
+
+class CheckBookmarks(unittest.TestCase):
+    expectedHashes = {
+        'nSetupHash': 0x8f88ee1a13a55d6024f58f470723b5174dfa21bb,
+        'nInsertRandomHash': 0x5f27e87e16d2cb3ff0bcb24237aa30da3b84cf24,
+        'nDeleteRandomHash': 0x1bdaa7773cbfc73a4dc0bb3e0f801b98f648e8e7,
+        'nLinebreakHash': 0x3f30a35f195efcfe0373a3e439de05087a2ad37c,
+        'nOdfReloadHash': 0x3f30a35f195efcfe0373a3e439de05087a2ad37c,
+        'nMsWordReloadHash': 0x3f30a35f195efcfe0373a3e439de05087a2ad37c,
+        }
+
+    @classmethod
+    def setUpClass(cls):
+        cls._uno = UnoInProcess()
+        cls._uno.setUp()
+        cls._xDoc = cls._uno.openEmptyWriterDoc()
+        smgr = cls._uno.xContext.ServiceManager
+        cls._desktop = 
smgr.createInstanceWithContext("com.sun.star.frame.Desktop", cls._uno.xContext)
+
+    @classmethod
+    def tearDownClass(cls):
+        cls._uno.tearDown()
+
+    def test_bookmarks(self):
+        self.xDoc = self.__class__._xDoc
+        self.xText = self.xDoc.getText()
+        ## setting and testing bookmarks
+        self.setupBookmarks()
+        self.assertEqual(self.expectedHashes['nSetupHash'],
+                         self.getBookmarksHash(self.xDoc))
+        ## modifying bookmarks and testing again
+        self.insertRandomParts(200177)
+        self.assertEqual(self.expectedHashes['nInsertRandomHash'],
+                         self.getBookmarksHash(self.xDoc))
+        ## modifying bookmarks and testing again
+        self.deleteRandomParts(4711)
+        self.assertEqual(self.expectedHashes['nDeleteRandomHash'],
+                         self.getBookmarksHash(self.xDoc))
+        ## adding line breaks and testing again
+        self.insertLinebreaks(7)
+        self.assertEqual(self.expectedHashes['nLinebreakHash'],
+                         self.getBookmarksHash(self.xDoc))
+        ## reloading document and testing again
+        with TemporaryDirectory() as tempdir:
+            xOdfReloadedDoc = self.reloadFrom(tempdir, "writer8", "odt")
+            self.assertEqual(self.expectedHashes['nOdfReloadHash'],
+                             self.getBookmarksHash(xOdfReloadedDoc))
+            xOdfReloadedDoc.close(True)
+        ## reloading document as MS Word 97 doc and testing again
+            ## MsWord Hash is unstable over different systems
+            # xMsWordReloadedDoc = self.reloadFrom(tempdir, "MS Word 97", 
"doc")
+            # self.assertEqual(self.expectedHashes['nMsWordReloadHash'],
+            #                  self.getBookmarksHash(xMsWordReloadedDoc))
+            # xMsWordReloadedDoc.close(True)
+        print('tests ok')
+
+    def setupBookmarks(self):
+        xCursor = self.xText.createTextCursor()
+        for nPara in range(10):
+            for nBookmark in range(100):
+                s = "P{}word{}".format(nPara, nBookmark)
+                xCursor.gotoEnd(False)
+                xCursor.setString(s)
+                xBookmark = 
self.xDoc.createInstance("com.sun.star.text.Bookmark")
+                xBookmark.setName(s)
+                self.xText.insertTextContent(xCursor, xBookmark, True)
+                xCursor.End.setString(" ")
+            self.xText.insertControlCharacter(xCursor.End, PARAGRAPH_BREAK, 
False)
+
+    def getBookmarksHash(self, doc):
+        hash = sha1()
+        xBookmarks = doc.getBookmarks()
+        for xBookmark in xBookmarks:
+            s = '{}:{};'.format(xBookmark.Name,
+                        xBookmark.getAnchor().getString().replace("\r\n", 
"\n"))
+            hash.update(str.encode(s))
+        return int(hash.hexdigest(), 16)
+
+    def insertRandomParts(self, seed):
+        random.seed(seed)
+        xCursor = self.xText.createTextCursor()
+        for i in range(600):
+            xCursor.goRight(random.randrange(100), False)
+            xCursor.setString(str(random.getrandbits(64)))
+
+    def deleteRandomParts(self, seed):
+        random.seed(seed)
+        xCursor = self.xText.createTextCursor()
+        for i in range(600):
+            xCursor.goRight(random.randrange(100), False)
+            xCursor.goRight(random.randrange(20), True)
+            xCursor.setString("")
+
+    def insertLinebreaks(self, seed):
+        random.seed(seed)
+        xCursor = self.xText.createTextCursor()
+        for i in range(30):
+            xCursor.goRight(random.randrange(300), False)
+            self.xText.insertControlCharacter(xCursor, PARAGRAPH_BREAK, False)
+
+    def reloadFrom(self, tempdir, sFilter, sExtension):
+        sFileUrl = os.path.join(tempdir, "Bookmarktest.{}".format(sExtension))
+        sFileUrl = systemPathToFileUrl(sFileUrl)
+        store_props = mkPropertyValues(Override=True, FilterName=sFilter)
+        self.xDoc.storeToURL(sFileUrl, store_props)
+        desktop = self.__class__._desktop
+        load_props = mkPropertyValues(Hidden=True, ReadOnly=False)
+        return desktop.loadComponentFromURL(sFileUrl, "_default", 0, 
load_props)
+
+
+if __name__ == '__main__':
+    unittest.main()
diff --git a/unotest/source/python/org/libreoffice/unotest.py 
b/unotest/source/python/org/libreoffice/unotest.py
index 13b00845bb36..0bb5212c9d4a 100644
--- a/unotest/source/python/org/libreoffice/unotest.py
+++ b/unotest/source/python/org/libreoffice/unotest.py
@@ -43,6 +43,18 @@ except ImportError:
 def mkPropertyValue(name, value):
     return uno.createUnoStruct("com.sun.star.beans.PropertyValue", name, 0, 
value, 0)
 
+def mkPropertyValues(**kwargs):
+    '''mkPropertyValues(Name=Value, Name=Value,...) -> (PropertyValue, 
PropertyValue,...)
+    ex. : mkPropertyValues(Hidden=True, ReadOnly=False)'''
+    from com.sun.star.beans import PropertyValue
+    return tuple(PropertyValue(k,0,kwargs[k],0) for k in kwargs)
+
+def fileUrlToSystemPath(url):
+    return pyuno.fileUrlToSystemPath(url)
+
+def systemPathToFileUrl(systemPath):
+    return pyuno.systemPathToFileUrl(systemPath)
+
 ### UNO utilities ###
 
 class OfficeConnection(object):
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to