move password handling from SwModelTestBase to UnoApiTest

so other places inheriting from UnoApiTest can also import/export
protected documents

Change-Id: I0e2716204dbb171c9e17e3939b266977e1b96dda
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/142592
Tested-by: Jenkins
Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org>
diff --git a/dbaccess/CppunitTest_dbaccess_firebird_test.mk b/dbaccess/CppunitTest_dbaccess_firebird_test.mk
index 9b7af21..a8d4704 100644
--- a/dbaccess/CppunitTest_dbaccess_firebird_test.mk
+++ b/dbaccess/CppunitTest_dbaccess_firebird_test.mk
@@ -18,6 +18,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,dbaccess_firebird_test, \
$(eval $(call gb_CppunitTest_use_libraries,dbaccess_firebird_test, \
    comphelper \
    cppu \
    cppuhelper \
    dbaxml \
    firebird_sdbc \
    sal \
diff --git a/embeddedobj/CppunitTest_embeddedobj_general.mk b/embeddedobj/CppunitTest_embeddedobj_general.mk
index eb794f3..6c4014e 100644
--- a/embeddedobj/CppunitTest_embeddedobj_general.mk
+++ b/embeddedobj/CppunitTest_embeddedobj_general.mk
@@ -22,6 +22,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,embeddedobj_general, \
$(eval $(call gb_CppunitTest_use_libraries,embeddedobj_general, \
    comphelper \
    cppu \
    cppuhelper \
    embobj \
    sal \
    subsequenttest \
diff --git a/include/test/unoapi_test.hxx b/include/test/unoapi_test.hxx
index 307d0f1..0277131 100644
--- a/include/test/unoapi_test.hxx
+++ b/include/test/unoapi_test.hxx
@@ -14,7 +14,9 @@

#include <string_view>

#include <rtl/ref.hxx>
#include <test/bootstrapfixture.hxx>
#include <test/testinteractionhandler.hxx>
#include <unotest/macros_test.hxx>
#include <com/sun/star/lang/XComponent.hpp>
#include <osl/file.hxx>
@@ -28,24 +30,32 @@ class OOO_DLLPUBLIC_TEST UnoApiTest : public test::BootstrapFixture, public unot
public:
    UnoApiTest(OUString path);

    OUString createFileURL(std::u16string_view aFileBase);
    OUString loadFromURL(std::u16string_view aFileBase);

    virtual void setUp() override;
    virtual void tearDown() override;

    OUString createFileURL(std::u16string_view aFileBase);
    void load(const OUString& rURL, const char* pPassword = nullptr);
    OUString loadFromURL(std::u16string_view aFileBase, const char* pPassword = nullptr);

    css::uno::Any executeMacro(const OUString& rScriptURL,
                               const css::uno::Sequence<css::uno::Any>& rParams = {});

    void save(const OUString& rFilter, const char* pPassword = nullptr);
    void saveAndClose(const OUString& rFilter);
    void saveAndReload(const OUString& rFilter);
    void saveAndReload(const OUString& rFilter, const char* pPassword = nullptr);

    std::unique_ptr<vcl::pdf::PDFiumDocument> parsePDFExport(const OString& rPassword = OString());

    void skipValidation() { mbSkipValidation = true; }
    void setFilterOptions(const OUString& rFilterOptions) { maFilterOptions = rFilterOptions; }

    void setImportFilterOptions(const OUString& rFilterOptions)
    {
        maImportFilterOptions = rFilterOptions;
    }

    void setImportFilterName(const OUString& rFilterName) { maImportFilterName = rFilterName; }

protected:
    // reference to document component that we are testing
    css::uno::Reference<css::lang::XComponent> mxComponent;
@@ -54,10 +64,19 @@ protected:

    SvMemoryStream maMemory; // Underlying memory for parsed PDF files.

    rtl::Reference<TestInteractionHandler> xInteractionHandler;

private:
    void
    setTestInteractionHandler(const char* pPassword,
                              std::vector<com::sun::star::beans::PropertyValue>& rFilterOptions);

    bool mbSkipValidation;
    OUString m_aBaseString;
    OUString maFilterOptions;

    OUString maImportFilterOptions;
    OUString maImportFilterName;
};

#endif // INCLUDED_TEST_UNOAPI_TEST_HXX
diff --git a/oox/CppunitTest_oox_drawingml.mk b/oox/CppunitTest_oox_drawingml.mk
index 1e47063..0ad75df 100644
--- a/oox/CppunitTest_oox_drawingml.mk
+++ b/oox/CppunitTest_oox_drawingml.mk
@@ -22,6 +22,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,oox_drawingml, \
$(eval $(call gb_CppunitTest_use_libraries,oox_drawingml, \
    comphelper \
    cppu \
    cppuhelper \
    oox \
    sal \
    subsequenttest \
diff --git a/oox/CppunitTest_oox_export.mk b/oox/CppunitTest_oox_export.mk
index 68d4566..b3e832f 100644
--- a/oox/CppunitTest_oox_export.mk
+++ b/oox/CppunitTest_oox_export.mk
@@ -23,6 +23,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,oox_export, \
$(eval $(call gb_CppunitTest_use_libraries,oox_export, \
    comphelper \
    cppu \
    cppuhelper \
    oox \
    sal \
    subsequenttest \
diff --git a/oox/CppunitTest_oox_mathml.mk b/oox/CppunitTest_oox_mathml.mk
index 133e66c..58d04b1 100644
--- a/oox/CppunitTest_oox_mathml.mk
+++ b/oox/CppunitTest_oox_mathml.mk
@@ -22,6 +22,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,oox_mathml, \
$(eval $(call gb_CppunitTest_use_libraries,oox_mathml, \
    comphelper \
    cppu \
    cppuhelper \
    oox \
    sal \
    subsequenttest \
diff --git a/oox/CppunitTest_oox_shape.mk b/oox/CppunitTest_oox_shape.mk
index 65bdacc..2d36387 100644
--- a/oox/CppunitTest_oox_shape.mk
+++ b/oox/CppunitTest_oox_shape.mk
@@ -22,6 +22,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,oox_shape, \
$(eval $(call gb_CppunitTest_use_libraries,oox_shape, \
    comphelper \
    cppu \
    cppuhelper \
    oox \
    sal \
    subsequenttest \
diff --git a/oox/CppunitTest_oox_vml.mk b/oox/CppunitTest_oox_vml.mk
index 28ccb2a..902f4ac 100644
--- a/oox/CppunitTest_oox_vml.mk
+++ b/oox/CppunitTest_oox_vml.mk
@@ -22,6 +22,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,oox_vml, \
$(eval $(call gb_CppunitTest_use_libraries,oox_vml, \
    comphelper \
    cppu \
    cppuhelper \
    oox \
    sal \
    subsequenttest \
diff --git a/sc/CppunitTest_sc_functionlistobj.mk b/sc/CppunitTest_sc_functionlistobj.mk
index a190b1c..ff4719d 100644
--- a/sc/CppunitTest_sc_functionlistobj.mk
+++ b/sc/CppunitTest_sc_functionlistobj.mk
@@ -21,6 +21,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,sc_functionlistobj, \

$(eval $(call gb_CppunitTest_use_libraries,sc_functionlistobj, \
	cppu \
	cppuhelper \
	sal \
	subsequenttest \
	test \
diff --git a/sc/CppunitTest_sc_pdf_export.mk b/sc/CppunitTest_sc_pdf_export.mk
index 6051b17..ddd856b 100644
--- a/sc/CppunitTest_sc_pdf_export.mk
+++ b/sc/CppunitTest_sc_pdf_export.mk
@@ -28,6 +28,7 @@ endif
$(eval $(call gb_CppunitTest_use_libraries,sc_pdf_export, \
    comphelper \
    cppu \
    cppuhelper \
    editeng \
    sal \
    sax \
diff --git a/sc/common_unoapi_tests.mk b/sc/common_unoapi_tests.mk
index 2f04459..025639a4 100644
--- a/sc/common_unoapi_tests.mk
+++ b/sc/common_unoapi_tests.mk
@@ -25,6 +25,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,sc_$(1), \
$(eval $(call gb_CppunitTest_use_libraries,sc_$(1), \
    comphelper \
    cppu \
    cppuhelper \
    sal \
    salhelper \
    sc \
diff --git a/sd/CppunitTest_sd_filter_eppt.mk b/sd/CppunitTest_sd_filter_eppt.mk
index 7e304e4..2a570b5 100644
--- a/sd/CppunitTest_sd_filter_eppt.mk
+++ b/sd/CppunitTest_sd_filter_eppt.mk
@@ -23,6 +23,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,sd_filter_eppt, \
$(eval $(call gb_CppunitTest_use_libraries,sd_filter_eppt, \
    comphelper \
    cppu \
    cppuhelper \
    sd \
    sal \
    subsequenttest \
diff --git a/sfx2/CppunitTest_sfx2_doc.mk b/sfx2/CppunitTest_sfx2_doc.mk
index 81ddc76..f1f404d 100644
--- a/sfx2/CppunitTest_sfx2_doc.mk
+++ b/sfx2/CppunitTest_sfx2_doc.mk
@@ -22,6 +22,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,sfx2_doc, \
$(eval $(call gb_CppunitTest_use_libraries,sfx2_doc, \
    comphelper \
    cppu \
    cppuhelper \
    sal \
    subsequenttest \
    test \
diff --git a/sfx2/CppunitTest_sfx2_view.mk b/sfx2/CppunitTest_sfx2_view.mk
index f6f247e..70ca709 100644
--- a/sfx2/CppunitTest_sfx2_view.mk
+++ b/sfx2/CppunitTest_sfx2_view.mk
@@ -22,6 +22,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,sfx2_view, \
$(eval $(call gb_CppunitTest_use_libraries,sfx2_view, \
    comphelper \
    cppu \
    cppuhelper \
    sal \
    subsequenttest \
    test \
diff --git a/svx/CppunitTest_svx_styles.mk b/svx/CppunitTest_svx_styles.mk
index b239846..60f7e13 100644
--- a/svx/CppunitTest_svx_styles.mk
+++ b/svx/CppunitTest_svx_styles.mk
@@ -22,6 +22,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,svx_styles, \
$(eval $(call gb_CppunitTest_use_libraries,svx_styles, \
    comphelper \
    cppu \
    cppuhelper \
    svx \
    sal \
    subsequenttest \
diff --git a/sw/qa/inc/swmodeltestbase.hxx b/sw/qa/inc/swmodeltestbase.hxx
index 7bbd5c0..dbc78e8 100644
--- a/sw/qa/inc/swmodeltestbase.hxx
+++ b/sw/qa/inc/swmodeltestbase.hxx
@@ -24,7 +24,6 @@

#include "swqahelperdllapi.h"
#include <test/unoapixml_test.hxx>
#include <test/testinteractionhandler.hxx>
#include <unotools/tempfile.hxx>

#include <doc.hxx>
@@ -94,13 +93,9 @@ class PDFiumDocument;
class SWQAHELPER_DLLPUBLIC SwModelTestBase : public UnoApiXmlTest
{
private:
    OUString maImportFilterOptions;
    OUString maImportFilterName;
    bool mbExported; ///< Does maTempFile already contain something useful?

protected:
    rtl::Reference<TestInteractionHandler> xInteractionHandler;

    xmlBufferPtr mpXmlBuffer;
    const char* mpFilter;

@@ -112,16 +107,6 @@ protected:
    void paste(std::u16string_view aFilename, css::uno::Reference<css::text::XTextRange> const& xTextRange);

public:
    void setImportFilterOptions(const OUString &rFilterOptions)
    {
        maImportFilterOptions = rFilterOptions;
    }

    void setImportFilterName(const OUString &rFilterName)
    {
        maImportFilterName = rFilterName;
    }

    SwModelTestBase(const OUString& pTestDocumentPath = OUString(), const char* pFilter = "");

protected:
@@ -361,8 +346,6 @@ protected:
    bool isExported(){ return mbExported; }

private:
    void setTestInteractionHandler(const char* pPassword, std::vector<beans::PropertyValue>& rFilterOptions);

    void loadURL(OUString const& rURL, const char* pName, const char* pPassword);

    void load(const char* pName, const char* pPassword = nullptr)
diff --git a/sw/qa/unit/swmodeltestbase.cxx b/sw/qa/unit/swmodeltestbase.cxx
index 7a9e51f..07ba9e2 100644
--- a/sw/qa/unit/swmodeltestbase.cxx
+++ b/sw/qa/unit/swmodeltestbase.cxx
@@ -20,7 +20,6 @@

#include <comphelper/processfactory.hxx>
#include <comphelper/propertyvalue.hxx>
#include <comphelper/sequence.hxx>
#include <officecfg/Office/Common.hxx>
#include <rtl/ustrbuf.hxx>
#include <sfx2/app.hxx>
@@ -448,48 +447,10 @@ uno::Reference<drawing::XShape> SwModelTestBase::getTextFrameByName(const OUStri
    return xShape;
}

void SwModelTestBase::setTestInteractionHandler(const char* pPassword,
                                                std::vector<beans::PropertyValue>& rFilterOptions)
{
    OUString sPassword = OUString::createFromAscii(pPassword);
    rFilterOptions.emplace_back();
    xInteractionHandler
        = rtl::Reference<TestInteractionHandler>(new TestInteractionHandler(sPassword));
    uno::Reference<task::XInteractionHandler2> const xInteraction(xInteractionHandler);
    rFilterOptions[0].Name = "InteractionHandler";
    rFilterOptions[0].Value <<= xInteraction;
}

void SwModelTestBase::header() {}

void SwModelTestBase::loadURL(OUString const& rURL, const char* pName, const char* pPassword)
{
    if (mxComponent.is())
        mxComponent->dispose();

    std::vector<beans::PropertyValue> aFilterOptions;

    if (pPassword)
    {
        setTestInteractionHandler(pPassword, aFilterOptions);
    }

    if (!maImportFilterOptions.isEmpty())
    {
        beans::PropertyValue aValue;
        aValue.Name = "FilterOptions";
        aValue.Value <<= maImportFilterOptions;
        aFilterOptions.push_back(aValue);
    }

    if (!maImportFilterName.isEmpty())
    {
        beans::PropertyValue aValue;
        aValue.Name = "FilterName";
        aValue.Value <<= maImportFilterName;
        aFilterOptions.push_back(aValue);
    }

    // Output name at load time, so in the case of a hang, the name of the hanging input file is visible.
    if (!isExported())
    {
@@ -498,14 +459,7 @@ void SwModelTestBase::loadURL(OUString const& rURL, const char* pName, const cha
        mnStartTime = osl_getGlobalTimer();
    }

    mxComponent
        = loadFromDesktop(rURL, OUString(), comphelper::containerToSequence(aFilterOptions));

    if (pPassword)
    {
        CPPUNIT_ASSERT_MESSAGE("Password set but not requested",
                               xInteractionHandler->wasPasswordRequested());
    }
    UnoApiXmlTest::load(rURL, pPassword);

    discardDumpedLayout();
    if (pName && mustCalcLayoutOf(pName))
diff --git a/test/source/unoapi_test.cxx b/test/source/unoapi_test.cxx
index bc93952..29d4020 100644
--- a/test/source/unoapi_test.cxx
+++ b/test/source/unoapi_test.cxx
@@ -14,6 +14,7 @@
#include <com/sun/star/frame/XStorable.hpp>
#include <comphelper/processfactory.hxx>
#include <comphelper/propertyvalue.hxx>
#include <comphelper/sequence.hxx>

#include <sfx2/app.hxx>
#include <sfx2/objsh.hxx>
@@ -56,7 +57,19 @@ OUString UnoApiTest::createFileURL(std::u16string_view aFileBase)
    return m_directories.getSrcRootURL() + m_aBaseString + "/" + aFileBase;
}

OUString UnoApiTest::loadFromURL(std::u16string_view aFileBase)
void UnoApiTest::setTestInteractionHandler(const char* pPassword,
                                           std::vector<beans::PropertyValue>& rFilterOptions)
{
    OUString sPassword = OUString::createFromAscii(pPassword);
    rFilterOptions.emplace_back();
    xInteractionHandler
        = rtl::Reference<TestInteractionHandler>(new TestInteractionHandler(sPassword));
    css::uno::Reference<task::XInteractionHandler2> const xInteraction(xInteractionHandler);
    rFilterOptions[0].Name = "InteractionHandler";
    rFilterOptions[0].Value <<= xInteraction;
}

void UnoApiTest::load(OUString const& rURL, const char* pPassword)
{
    if (mxComponent.is())
    {
@@ -64,8 +77,43 @@ OUString UnoApiTest::loadFromURL(std::u16string_view aFileBase)
        mxComponent.clear();
    }

    std::vector<beans::PropertyValue> aFilterOptions;

    if (pPassword)
    {
        setTestInteractionHandler(pPassword, aFilterOptions);
    }

    if (!maImportFilterOptions.isEmpty())
    {
        beans::PropertyValue aValue;
        aValue.Name = "FilterOptions";
        aValue.Value <<= maImportFilterOptions;
        aFilterOptions.push_back(aValue);
    }

    if (!maImportFilterName.isEmpty())
    {
        beans::PropertyValue aValue;
        aValue.Name = "FilterName";
        aValue.Value <<= maImportFilterName;
        aFilterOptions.push_back(aValue);
    }

    mxComponent
        = loadFromDesktop(rURL, OUString(), comphelper::containerToSequence(aFilterOptions));

    if (pPassword)
    {
        CPPUNIT_ASSERT_MESSAGE("Password set but not requested",
                               xInteractionHandler->wasPasswordRequested());
    }
}

OUString UnoApiTest::loadFromURL(std::u16string_view aFileBase, const char* pPassword)
{
    OUString aFileName = createFileURL(aFileBase);
    mxComponent = loadFromDesktop(aFileName);
    load(aFileName, pPassword);
    return aFileName;
}

@@ -139,17 +187,17 @@ void UnoApiTest::save(const OUString& rFilter, const char* pPassword)

void UnoApiTest::saveAndClose(const OUString& rFilter)
{
    save(rFilter);
    save(rFilter, nullptr);

    mxComponent->dispose();
    mxComponent.clear();
}

void UnoApiTest::saveAndReload(const OUString& rFilter)
void UnoApiTest::saveAndReload(const OUString& rFilter, const char* pPassword)
{
    saveAndClose(rFilter);
    save(rFilter, pPassword);

    mxComponent = loadFromDesktop(maTempFile.GetURL());
    load(maTempFile.GetURL(), pPassword);
}

std::unique_ptr<vcl::pdf::PDFiumDocument> UnoApiTest::parsePDFExport(const OString& rPassword)
diff --git a/writerfilter/CppunitTest_writerfilter_dmapper.mk b/writerfilter/CppunitTest_writerfilter_dmapper.mk
index c54f310..6dcfa52 100644
--- a/writerfilter/CppunitTest_writerfilter_dmapper.mk
+++ b/writerfilter/CppunitTest_writerfilter_dmapper.mk
@@ -30,6 +30,7 @@ $(eval $(call gb_CppunitTest_use_libraries,writerfilter_dmapper, \
    basegfx \
    comphelper \
    cppu \
    cppuhelper \
    oox \
    sal \
    subsequenttest \
diff --git a/writerfilter/CppunitTest_writerfilter_rtftok.mk b/writerfilter/CppunitTest_writerfilter_rtftok.mk
index e7e58d8..990aa65 100644
--- a/writerfilter/CppunitTest_writerfilter_rtftok.mk
+++ b/writerfilter/CppunitTest_writerfilter_rtftok.mk
@@ -28,6 +28,7 @@ $(eval $(call gb_CppunitTest_use_libraries,writerfilter_rtftok, \
    basegfx \
    comphelper \
    cppu \
    cppuhelper \
    oox \
    sal \
    subsequenttest \
diff --git a/xmloff/CppunitTest_xmloff_draw.mk b/xmloff/CppunitTest_xmloff_draw.mk
index 77f931e..8e842e6 100644
--- a/xmloff/CppunitTest_xmloff_draw.mk
+++ b/xmloff/CppunitTest_xmloff_draw.mk
@@ -23,6 +23,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,xmloff_draw, \
$(eval $(call gb_CppunitTest_use_libraries,xmloff_draw, \
    comphelper \
    cppu \
    cppuhelper \
    embobj \
    sal \
    subsequenttest \
diff --git a/xmloff/CppunitTest_xmloff_style.mk b/xmloff/CppunitTest_xmloff_style.mk
index 9b58ee3..22d37df 100644
--- a/xmloff/CppunitTest_xmloff_style.mk
+++ b/xmloff/CppunitTest_xmloff_style.mk
@@ -23,6 +23,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,xmloff_style, \
$(eval $(call gb_CppunitTest_use_libraries,xmloff_style, \
    comphelper \
    cppu \
    cppuhelper \
    subsequenttest \
    embobj \
    sal \
diff --git a/xmloff/CppunitTest_xmloff_text.mk b/xmloff/CppunitTest_xmloff_text.mk
index 6734e85..a611aa4 100644
--- a/xmloff/CppunitTest_xmloff_text.mk
+++ b/xmloff/CppunitTest_xmloff_text.mk
@@ -23,6 +23,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,xmloff_text, \
$(eval $(call gb_CppunitTest_use_libraries,xmloff_text, \
    comphelper \
    cppu \
    cppuhelper \
    embobj \
    sal \
    subsequenttest \