tdf#150457 Port UITest to CppunitTest

The UITest for tdf#150457 is now ported to CppunitTest. The new test can
be run by:

 make CPPUNIT_TEST_NAME=testTdf150457 -sr CppunitTest_sw_uiwriter3

Without the fix d05c176cc022f1b771f7c064f6ce74e9f8c27a1b in place,
running the test leads to a segfault.

Change-Id: I1d633cadf9f5579cdf3e1e59641108eadc7b7073
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151330
Tested-by: Jenkins
Reviewed-by: Xisco Fauli <[email protected]>
Reviewed-by: Hossein <[email protected]>
diff --git a/sw/qa/extras/uiwriter/uiwriter3.cxx b/sw/qa/extras/uiwriter/uiwriter3.cxx
index 00665b3..c7bc792 100644
--- a/sw/qa/extras/uiwriter/uiwriter3.cxx
+++ b/sw/qa/extras/uiwriter/uiwriter3.cxx
@@ -2294,6 +2294,62 @@ CPPUNIT_TEST_FIXTURE(SwUiWriterTest3, testTdf130680)
    CPPUNIT_ASSERT_EQUAL(sal_Int32(23), xIndexAccess->getCount());
}

CPPUNIT_TEST_FIXTURE(SwUiWriterTest3, testTdf150457)
{
    createSwDoc();
    SwXTextDocument* pTextDoc = dynamic_cast<SwXTextDocument*>(mxComponent.get());

    pTextDoc->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 'a', 0);
    Scheduler::ProcessEventsToIdle();

    dispatchCommand(mxComponent, ".uno:InsertFootnote", {});
    pTextDoc->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 'a', 0);
    pTextDoc->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 'b', 0);
    pTextDoc->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 'c', 0);
    Scheduler::ProcessEventsToIdle();

    auto xFootnotes = pTextDoc->getFootnotes();
    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xFootnotes->getCount());
    auto xParagraph = uno::Reference<text::XTextRange>(xFootnotes->getByIndex(0), uno::UNO_QUERY);
    CPPUNIT_ASSERT_EQUAL(OUString("abc"), xParagraph->getString());

    pTextDoc->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 0, KEY_PAGEUP);
    pTextDoc->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 0, KEY_RETURN);
    pTextDoc->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 'd', 0);
    Scheduler::ProcessEventsToIdle();

    dispatchCommand(mxComponent, ".uno:InsertFootnote", {});
    pTextDoc->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 'd', 0);
    pTextDoc->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 'e', 0);
    pTextDoc->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 'f', 0);
    Scheduler::ProcessEventsToIdle();

    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), xFootnotes->getCount());
    xParagraph = uno::Reference<text::XTextRange>(xFootnotes->getByIndex(1), uno::UNO_QUERY);
    CPPUNIT_ASSERT_EQUAL(OUString("def"), xParagraph->getString());

    // This key sequence deletes a footnote and its number (without the fix applied)
    pTextDoc->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 0, KEY_UP);
    pTextDoc->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 0, KEY_HOME);
    pTextDoc->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 0, KEY_SHIFT | KEY_DOWN);
    pTextDoc->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 0, KEY_DELETE);

    // Page up moves the cursor from the footnote area to the main text, then
    // doing select all and pressing delete removes all the text and footenote references,
    // thus removing all the footnotes
    pTextDoc->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 0, KEY_PAGEUP);
    pTextDoc->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 0, awt::Key::SELECT_ALL);
    pTextDoc->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 0, KEY_DELETE);

    // Without having fix in place, segfault happens after running next line
    Scheduler::ProcessEventsToIdle();

    // Without the fix, the above action should have already created a crash,
    // but here we also check to make sure that the number of footnotes are
    // exactly zero, as expected
    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), xFootnotes->getCount());
}

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/qa/uitest/writer_tests2/deleteFootnotes.py b/sw/qa/uitest/writer_tests2/deleteFootnotes.py
deleted file mode 100644
index 0c66440..0000000
--- a/sw/qa/uitest/writer_tests2/deleteFootnotes.py
+++ /dev/null
@@ -1,44 +0,0 @@
# -*- 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/.
#

from uitest.framework import UITestCase
from uitest.uihelper.common import type_text
from libreoffice.uno.propertyvalue import mkPropertyValues

class tdf150457(UITestCase):

   def test_delete_footnotes(self):
        with self.ui_test.create_doc_in_start_center("writer") as document:
            xWriterDoc = self.xUITest.getTopFocusWindow()
            xWriterEdit = xWriterDoc.getChild("writer_edit")

            type_text(xWriterEdit, "a")
            self.xUITest.executeCommand(".uno:InsertFootnote")
            type_text(xWriterEdit, "abc")
            self.assertEqual(document.Footnotes[0].String, "abc")
            self.assertEqual(document.Footnotes.getCount(), 1)

            xWriterEdit.executeAction("TYPE", mkPropertyValues({"KEYCODE": "PAGEUP"}))
            xWriterEdit.executeAction("TYPE", mkPropertyValues({"KEYCODE": "RETURN"}))
            type_text(xWriterEdit, "d")

            self.xUITest.executeCommand(".uno:InsertFootnote")
            type_text(xWriterEdit, "def")
            self.assertEqual(document.Footnotes[1].String, "def")
            self.assertEqual(document.Footnotes.getCount(), 2)

            xWriterEdit.executeAction("TYPE", mkPropertyValues({"KEYCODE": "UP"}))
            xWriterEdit.executeAction("TYPE", mkPropertyValues({"KEYCODE": "HOME"}))
            xWriterEdit.executeAction("TYPE", mkPropertyValues({"KEYCODE": "SHIFT+DOWN"}))
            xWriterEdit.executeAction("TYPE", mkPropertyValues({"KEYCODE": "DELETE"}))
            xWriterEdit.executeAction("TYPE", mkPropertyValues({"KEYCODE": "PAGEUP"}))
            xWriterEdit.executeAction("TYPE", mkPropertyValues({"KEYCODE": "CTRL+A"}))
            xWriterEdit.executeAction("TYPE", mkPropertyValues({"KEYCODE": "DELETE"}))

# vim: set shiftwidth=4 softtabstop=4 expandtab: