DOCX import: fix lost page break when footer ends with a table

Regression from commit 7d3778e0ef9f54f3c8988f1b84d58e7002d6c625
(bnc#816593 DOCX import: ignore page breaks in tables, 2013-09-02), the
page break was ignored because the preceding footer ended with a table
(no empty paragraph at the end of the footer stream).

Fix the problem by saving/loading the table state around header/footers,
that way the page break is not ignored.

Adjust testTdf102466 to test the page number from Word.

Change-Id: Ia4c22452ee2c37f7f941dfd922db04c851644d0c
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/86435
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Tested-by: Jenkins
diff --git a/sw/qa/extras/ooxmlexport/ooxmlexport4.cxx b/sw/qa/extras/ooxmlexport/ooxmlexport4.cxx
index 6c0cfcf..abadb97 100644
--- a/sw/qa/extras/ooxmlexport/ooxmlexport4.cxx
+++ b/sw/qa/extras/ooxmlexport/ooxmlexport4.cxx
@@ -997,8 +997,8 @@ DECLARE_OOXMLEXPORT_TEST(testTdf102466, "tdf102466.docx")
        CPPUNIT_ASSERT_MESSAGE("The table is clipped in a fly frame.", nFlyPrtHeight >= nTableHeight);
    }

    // check how much pages we have
    CPPUNIT_ASSERT_EQUAL(10, getPages());
    // check how much pages we have: it should match the Word layout result
    CPPUNIT_ASSERT_EQUAL(11, getPages());

    // check content of the first page
    {
diff --git a/writerfilter/CppunitTest_writerfilter_dmapper.mk b/writerfilter/CppunitTest_writerfilter_dmapper.mk
index 9805e28..41b92e9 100644
--- a/writerfilter/CppunitTest_writerfilter_dmapper.mk
+++ b/writerfilter/CppunitTest_writerfilter_dmapper.mk
@@ -18,6 +18,7 @@ $(eval $(call gb_CppunitTest_use_externals,writerfilter_dmapper,\
$(eval $(call gb_CppunitTest_add_exception_objects,writerfilter_dmapper, \
    writerfilter/qa/cppunittests/dmapper/CellColorHandler \
    writerfilter/qa/cppunittests/dmapper/DomainMapperTableHandler \
    writerfilter/qa/cppunittests/dmapper/DomainMapper_Impl \
))

$(eval $(call gb_CppunitTest_use_libraries,writerfilter_dmapper, \
diff --git a/writerfilter/qa/cppunittests/dmapper/DomainMapper_Impl.cxx b/writerfilter/qa/cppunittests/dmapper/DomainMapper_Impl.cxx
new file mode 100644
index 0000000..00c83c9
--- /dev/null
+++ b/writerfilter/qa/cppunittests/dmapper/DomainMapper_Impl.cxx
@@ -0,0 +1,85 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-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/.
 */

#include <test/bootstrapfixture.hxx>
#include <unotest/macros_test.hxx>

#include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
#include <com/sun/star/frame/Desktop.hpp>
#include <com/sun/star/text/XTextDocument.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/drawing/FillStyle.hpp>
#include <com/sun/star/style/BreakType.hpp>

#include <comphelper/processfactory.hxx>

using namespace ::com::sun::star;

namespace
{
/// Tests for writerfilter/source/dmapper/DomainMapper_Impl.cxx.
class Test : public test::BootstrapFixture, public unotest::MacrosTest
{
private:
    uno::Reference<uno::XComponentContext> mxComponentContext;
    uno::Reference<lang::XComponent> mxComponent;

public:
    void setUp() override;
    void tearDown() override;
    uno::Reference<lang::XComponent>& getComponent() { return mxComponent; }
};

void Test::setUp()
{
    test::BootstrapFixture::setUp();

    mxComponentContext.set(comphelper::getComponentContext(getMultiServiceFactory()));
    mxDesktop.set(frame::Desktop::create(mxComponentContext));
}

void Test::tearDown()
{
    if (mxComponent.is())
        mxComponent->dispose();

    test::BootstrapFixture::tearDown();
}

char const DATA_DIRECTORY[] = "/writerfilter/qa/cppunittests/dmapper/data/";

CPPUNIT_TEST_FIXTURE(Test, testPageBreakFooterTable)
{
    // Load a document which refers to a footer which ends with a table, and there is a page break
    // in the body text right after the footer reference.
    OUString aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + "page-break-footer-table.docx";
    getComponent() = loadFromDesktop(aURL);

    // Check the last paragraph.
    uno::Reference<text::XTextDocument> xTextDocument(getComponent(), uno::UNO_QUERY);
    uno::Reference<container::XEnumerationAccess> xParaEnumAccess(xTextDocument->getText(),
                                                                  uno::UNO_QUERY);
    uno::Reference<container::XEnumeration> xParaEnum = xParaEnumAccess->createEnumeration();
    uno::Reference<beans::XPropertySet> xPara;
    while (xParaEnum->hasMoreElements())
    {
        xPara.set(xParaEnum->nextElement(), uno::UNO_QUERY);
    }
    style::BreakType eType = style::BreakType_NONE;
    xPara->getPropertyValue("BreakType") >>= eType;

    // Without the accompanying fix in place, this test would have failed with:
    // - Expected: 4
    // - Actual  : 0
    // i.e. there was no page break before the last paragraph.
    CPPUNIT_ASSERT_EQUAL(style::BreakType_PAGE_BEFORE, eType);
}
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/writerfilter/qa/cppunittests/dmapper/data/page-break-footer-table.docx b/writerfilter/qa/cppunittests/dmapper/data/page-break-footer-table.docx
new file mode 100644
index 0000000..376a1fb
--- /dev/null
+++ b/writerfilter/qa/cppunittests/dmapper/data/page-break-footer-table.docx
Binary files differ
diff --git a/writerfilter/source/dmapper/DomainMapper_Impl.cxx b/writerfilter/source/dmapper/DomainMapper_Impl.cxx
index 7842acb..7c4cc46 100644
--- a/writerfilter/source/dmapper/DomainMapper_Impl.cxx
+++ b/writerfilter/source/dmapper/DomainMapper_Impl.cxx
@@ -2171,8 +2171,9 @@ void DomainMapper_Impl::appendGlossaryEntry()

void DomainMapper_Impl::PushPageHeaderFooter(bool bHeader, SectionPropertyMap::PageType eType)
{
    m_aHeaderFooterStack.push(HeaderFooterContext(m_bTextInserted));
    m_aHeaderFooterStack.push(HeaderFooterContext(m_bTextInserted, m_nTableDepth));
    m_bTextInserted = false;
    m_nTableDepth = 0;

    const PropertyIds ePropIsOn = bHeader? PROP_HEADER_IS_ON: PROP_FOOTER_IS_ON;
    const PropertyIds ePropShared = bHeader? PROP_HEADER_IS_SHARED: PROP_FOOTER_IS_SHARED;
@@ -2261,6 +2262,7 @@ void DomainMapper_Impl::PopPageHeaderFooter()
    if (!m_aHeaderFooterStack.empty())
    {
        m_bTextInserted = m_aHeaderFooterStack.top().getTextInserted();
        m_nTableDepth = m_aHeaderFooterStack.top().getTableDepth();
        m_aHeaderFooterStack.pop();
    }
}
@@ -3566,8 +3568,9 @@ void DomainMapper_Impl::SetFieldLocked()
        m_aFieldStack.back()->SetFieldLocked();
}

HeaderFooterContext::HeaderFooterContext(bool bTextInserted)
HeaderFooterContext::HeaderFooterContext(bool bTextInserted, sal_Int32 nTableDepth)
    : m_bTextInserted(bTextInserted)
    , m_nTableDepth(nTableDepth)
{
}

@@ -3576,6 +3579,8 @@ bool HeaderFooterContext::getTextInserted() const
    return m_bTextInserted;
}

sal_Int32 HeaderFooterContext::getTableDepth() const { return m_nTableDepth; }

FieldContext::FieldContext(uno::Reference< text::XTextRange > const& xStart)
    : m_bFieldCommandCompleted(false)
    , m_xStartRange( xStart )
diff --git a/writerfilter/source/dmapper/DomainMapper_Impl.hxx b/writerfilter/source/dmapper/DomainMapper_Impl.hxx
index b050e95..2691bbf 100644
--- a/writerfilter/source/dmapper/DomainMapper_Impl.hxx
+++ b/writerfilter/source/dmapper/DomainMapper_Impl.hxx
@@ -140,9 +140,12 @@ enum class SkipFootnoteSeparator
class HeaderFooterContext
{
    bool const m_bTextInserted;
    sal_Int32 m_nTableDepth;

public:
    explicit HeaderFooterContext(bool bTextInserted);
    explicit HeaderFooterContext(bool bTextInserted, sal_Int32 nTableDepth);
    bool getTextInserted() const;
    sal_Int32 getTableDepth() const;
};

/// Information about a paragraph to be finished after a field end.