tdf#45904 Move XRefreshable Java tests to C++

Move XRefreshable Java tests to C++ for ScDDELinkObj.

Change-Id: I121d9fe6a66c549e402b0cf6fd54fea1201e443f
Reviewed-on: https://gerrit.libreoffice.org/64511
Tested-by: Jenkins
Reviewed-by: Jens Carl <j.carl43@gmx.de>
diff --git a/framework/qa/complex/api_internal/api.lst b/framework/qa/complex/api_internal/api.lst
index a4b45df..4f1cc7c 100644
--- a/framework/qa/complex/api_internal/api.lst
+++ b/framework/qa/complex/api_internal/api.lst
@@ -84,7 +84,6 @@
job90=sc.ScDataPilotFieldObj
job91=sc.ScDataPilotFieldsObj
job92=sc.ScDataPilotTablesObj
job93=sc.ScDDELinkObj
job94=sc.ScDDELinksObj
job95=sc.ScDrawPagesObj
job96=sc.ScFilterDescriptorBase
diff --git a/include/test/util/xrefreshable.hxx b/include/test/util/xrefreshable.hxx
new file mode 100644
index 0000000..2ffab99
--- /dev/null
+++ b/include/test/util/xrefreshable.hxx
@@ -0,0 +1,38 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * 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/.
 */

#ifndef INCLUDED_TEST_UTIL_XREFRESHABLE_HXX
#define INCLUDED_TEST_UTIL_XREFRESHABLE_HXX

#include <com/sun/star/uno/XInterface.hpp>

#include <com/sun/star/uno/Reference.hxx>

#include <test/testdllapi.hxx>

namespace apitest
{
class OOO_DLLPUBLIC_TEST XRefreshable
{
public:
    virtual css::uno::Reference<css::uno::XInterface> init() = 0;

    void testAddRefreshListener();
    void testRefresh();
    void testRemoveRefreshListener();

protected:
    ~XRefreshable() {}
};

} // namespace apitest

#endif // INCLUDED_TEST_UTIL_XREFRESHABLE_HXX

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/qadevOOo/Jar_OOoRunner.mk b/qadevOOo/Jar_OOoRunner.mk
index 9de56bb..80b62f0 100644
--- a/qadevOOo/Jar_OOoRunner.mk
+++ b/qadevOOo/Jar_OOoRunner.mk
@@ -978,7 +978,6 @@
    qadevOOo/tests/java/mod/_sc/ScDataPilotItemObj \
    qadevOOo/tests/java/mod/_sc/ScDataPilotItemsObj \
    qadevOOo/tests/java/mod/_sc/ScDataPilotTablesObj \
    qadevOOo/tests/java/mod/_sc/ScDDELinkObj \
    qadevOOo/tests/java/mod/_sc/ScDDELinksObj \
    qadevOOo/tests/java/mod/_sc/ScDocumentConfiguration \
    qadevOOo/tests/java/mod/_sc/ScDrawPageObj \
diff --git a/qadevOOo/objdsc/sc/com.sun.star.comp.office.ScDDELinkObj.csv b/qadevOOo/objdsc/sc/com.sun.star.comp.office.ScDDELinkObj.csv
deleted file mode 100644
index 651b7c6..0000000
--- a/qadevOOo/objdsc/sc/com.sun.star.comp.office.ScDDELinkObj.csv
+++ /dev/null
@@ -1,3 +0,0 @@
"ScDDELinkObj";"com::sun::star::util::XRefreshable";"refresh()"
"ScDDELinkObj";"com::sun::star::util::XRefreshable";"addRefreshListener()"
"ScDDELinkObj";"com::sun::star::util::XRefreshable";"removeRefreshListener()"
diff --git a/qadevOOo/tests/java/mod/_sc/ScDDELinkObj.java b/qadevOOo/tests/java/mod/_sc/ScDDELinkObj.java
deleted file mode 100644
index 04e9bee..0000000
--- a/qadevOOo/tests/java/mod/_sc/ScDDELinkObj.java
+++ /dev/null
@@ -1,181 +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 mod._sc;

import java.io.PrintWriter;

import lib.TestCase;
import lib.TestEnvironment;
import lib.TestParameters;
import util.SOfficeFactory;

import com.sun.star.beans.XPropertySet;
import com.sun.star.container.XIndexAccess;
import com.sun.star.container.XNameAccess;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.sheet.XSpreadsheet;
import com.sun.star.sheet.XSpreadsheetDocument;
import com.sun.star.sheet.XSpreadsheets;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.Type;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XInterface;

/**
* Test for object which is represented by service
* <code>com.sun.star.sheet.DDELink</code>. <p>
* Object implements the following interfaces :
* <ul>
*  <li> <code>com::sun::star::container::XNamed</code></li>
*  <li> <code>com::sun::star::util::XRefreshable</code></li>
*  <li> <code>com::sun::star::sheet::XDDELink</code></li>
* </ul>
* The following files used by this test :
* <ul>
*  <li><b> ScDDELinksObj.ods </b> : the predefined testdocument </li>
* </ul> <p>
* @see com.sun.star.sheet.DDELink
* @see com.sun.star.container.XNamed
* @see com.sun.star.util.XRefreshable
* @see com.sun.star.sheet.XDDELink
* @see ifc.container._XNamed
* @see ifc.util._XRefreshable
* @see ifc.sheet._XDDELink
*/
public class ScDDELinkObj extends TestCase {
    private XSpreadsheetDocument xSheetDoc = null;
    static XComponent oDoc = null;

    /**
    * Creates Spreadsheet document.
    */
    @Override
    protected void initialize( TestParameters tParam, PrintWriter log ) throws Exception {
        SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF() );
        log.println( "creating a Spreadsheet document" );
        xSheetDoc = SOF.createCalcDoc(null);
    }

    /**
    * Disposes Spreadsheet document and testdocument if it was loaded already.
    */
    @Override
    protected void cleanup( TestParameters tParam, PrintWriter log ) {
        log.println( "    disposing xSheetDoc " );
        XComponent oComp = UnoRuntime.queryInterface(XComponent.class, xSheetDoc) ;
        util.DesktopTools.closeDoc(oComp);
        if (oDoc != null) {
            util.DesktopTools.closeDoc(oDoc);
        }
    }

    /**
    * Creating a TestEnvironment for the interfaces to be tested.
    * Disposes the testdocument if it was loaded already.
    * Creates an instance of the <code>com.sun.star.frame.Desktop</code>
    * and loads the predefined testdocument. Retrieves a collection of
    * spreadsheets from a document and takes one of them. Sets specific formula
    * to some cells in the spreadsheet(the formula specify DDE links to the
    * testdocument). Retrieves the collection of DDE links in the document and
    * retrieves first DDE link from the collection.
    * The retrieved DDE link is the instance of the service
    * <code>com.sun.star.sheet.DDELink</code>.
    * Object relations created :
    * <ul>
    *  <li> <code>'setName'</code> for
    *      {@link ifc.container._XNamed}(specify that the method
    *      <code>setName</code> must not be tested)</li>
    *  <li> <code>'APPLICATION'</code> for
    *      {@link ifc.sheet._XDDELink}(the name of the current application)</li>
    *  <li> <code>'ITEM'</code> for
    *      {@link ifc.sheet._XDDELink}(the DDE item that was set in the formula)</li>
    *  <li> <code>'TOPIC'</code> for
    *      {@link ifc.sheet._XDDELink}(the full testdocument name)</li>
    * </ul>
    * @see com.sun.star.frame.Desktop
    * @see com.sun.star.sheet.DDELink
    */
    @Override
    protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) throws Exception {

        XInterface oObj = null;

        if (oDoc != null) {
            util.DesktopTools.closeDoc(oDoc);
        }

        // creation of testobject here
        // first we write what we are intend to do to log file
        log.println( "Creating a test environment" );

        // create testobject here

        XMultiServiceFactory oMSF = Param.getMSF();

        // load the predefined testdocument
        String testdoc = util.utils.getFullTestURL("ScDDELinksObj.ods");
        oDoc = SOfficeFactory.getFactory(oMSF).loadDocument(testdoc);

        log.println("getting sheets");
        XSpreadsheets xSpreadsheets = xSheetDoc.getSheets();

        log.println("getting a sheet");
        XSpreadsheet oSheet = null;
        XIndexAccess oIndexAccess = UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
        oSheet = (XSpreadsheet) AnyConverter.toObject(
                new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));

        log.println("filling some cells");
        String sAppl = "soffice";
        String sItem = "Sheet1.A1";
        testdoc = util.utils.getFullTestDocName("ScDDELinksObj.ods");
        oSheet.getCellByPosition(5, 5).setFormula(
            "=DDE(\""+ sAppl +"\";\""+testdoc+"\";\""+ sItem +"\"");
        oSheet.getCellByPosition(1, 4).setFormula(
            "=DDE(\""+ sAppl +"\";\""+testdoc+"\";\""+ sItem +"\"");

        log.println("Getting test object ") ;

        // Getting named ranges.
        XPropertySet docProps = UnoRuntime.queryInterface(XPropertySet.class, xSheetDoc);
        XNameAccess links = (XNameAccess) AnyConverter.toObject(
            new Type(XNameAccess.class), UnoRuntime.queryInterface(
                XNameAccess.class, docProps.getPropertyValue("DDELinks")));

        String[] linkNames = links.getElementNames();

        oObj = (XInterface) AnyConverter.toObject(
            new Type(XInterface.class),links.getByName(linkNames[0]));
        log.println("Creating object - " +
                                        ((oObj == null) ? "FAILED" : "OK"));

        TestEnvironment tEnv = new TestEnvironment( oObj );

        // Other parameters required for interface tests
        tEnv.addObjRelation("APPLICATION", sAppl);
        tEnv.addObjRelation("ITEM", sItem);
        tEnv.addObjRelation("TOPIC", testdoc);
        tEnv.addObjRelation("setName", Boolean.TRUE);

        return tEnv;
    }

}


diff --git a/sc/qa/extras/scddelinkobj.cxx b/sc/qa/extras/scddelinkobj.cxx
index 2b2263a..204c5b6 100644
--- a/sc/qa/extras/scddelinkobj.cxx
+++ b/sc/qa/extras/scddelinkobj.cxx
@@ -10,6 +10,7 @@
#include <test/calc_unoapi_test.hxx>
#include <test/container/xnamed.hxx>
#include <test/sheet/xddelink.hxx>
#include <test/util/xrefreshable.hxx>

#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/container/XIndexAccess.hpp>
@@ -30,7 +31,10 @@

namespace sc_apitest
{
class ScDDELinkObj : public CalcUnoApiTest, public apitest::XDDELink, public apitest::XNamed
class ScDDELinkObj : public CalcUnoApiTest,
                     public apitest::XDDELink,
                     public apitest::XNamed,
                     public apitest::XRefreshable
{
public:
    ScDDELinkObj();
@@ -50,6 +54,11 @@
    CPPUNIT_TEST(testGetName);
    CPPUNIT_TEST(testSetNameThrowsException);

    // XRefreshable
    CPPUNIT_TEST(testAddRefreshListener);
    CPPUNIT_TEST(testRefresh);
    CPPUNIT_TEST(testRemoveRefreshListener);

    CPPUNIT_TEST_SUITE_END();

private:
diff --git a/sc/qa/unoapi/sc_3.sce b/sc/qa/unoapi/sc_3.sce
index 1848392..953875a 100644
--- a/sc/qa/unoapi/sc_3.sce
+++ b/sc/qa/unoapi/sc_3.sce
@@ -21,5 +21,4 @@
-o sc.ScCellTextCursor
-o sc.ScChartObj
-o sc.ScChartsObj
-o sc.ScDDELinkObj
-o sc.ScDDELinksObj
diff --git a/test/Library_subsequenttest.mk b/test/Library_subsequenttest.mk
index b6ca7bf..78c2e7f 100644
--- a/test/Library_subsequenttest.mk
+++ b/test/Library_subsequenttest.mk
@@ -159,6 +159,7 @@
	test/source/text/xtextfield \
	test/source/util/xindent \
	test/source/util/xmergeable \
	test/source/util/xrefreshable \
	test/source/util/xreplaceable \
	test/source/util/xsearchable \
))
diff --git a/test/source/util/xrefreshable.cxx b/test/source/util/xrefreshable.cxx
new file mode 100644
index 0000000..174115e
--- /dev/null
+++ b/test/source/util/xrefreshable.cxx
@@ -0,0 +1,81 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * 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/util/xrefreshable.hxx>

#include <com/sun/star/lang/EventObject.hpp>
#include <com/sun/star/util/XRefreshListener.hpp>
#include <com/sun/star/util/XRefreshable.hpp>

#include <com/sun/star/uno/Reference.hxx>

#include <cppuhelper/implbase.hxx>
#include <rtl/ref.hxx>

#include <cppunit/extensions/HelperMacros.h>

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

namespace apitest
{
class MockedRefreshListener : public ::cppu::WeakImplHelper<util::XRefreshListener>
{
public:
    MockedRefreshListener()
        : m_bListenerCalled(false)
    {
    }

    bool m_bListenerCalled;
    virtual void SAL_CALL refreshed(const lang::EventObject& /* xEvent */) override
    {
        m_bListenerCalled = true;
    }
    virtual void SAL_CALL disposing(const lang::EventObject& /* xEventObj */) override {}
};

void XRefreshable::testAddRefreshListener()
{
    uno::Reference<util::XRefreshable> xRefreshable(init(), uno::UNO_QUERY_THROW);

    rtl::Reference<MockedRefreshListener> xListener = new MockedRefreshListener();
    xRefreshable->addRefreshListener(uno::Reference<util::XRefreshListener>(xListener.get()));
}

void XRefreshable::testRefresh()
{
    uno::Reference<util::XRefreshable> xRefreshable(init(), uno::UNO_QUERY_THROW);

    rtl::Reference<MockedRefreshListener> xListener = new MockedRefreshListener();
    xRefreshable->addRefreshListener(uno::Reference<util::XRefreshListener>(xListener.get()));

    xRefreshable->refresh();

    CPPUNIT_ASSERT(xListener->m_bListenerCalled);
}

void XRefreshable::testRemoveRefreshListener()
{
    uno::Reference<util::XRefreshable> xRefreshable(init(), uno::UNO_QUERY_THROW);

    rtl::Reference<MockedRefreshListener> xListener = new MockedRefreshListener();
    xRefreshable->addRefreshListener(uno::Reference<util::XRefreshListener>(xListener.get()));

    xRefreshable->refresh();
    CPPUNIT_ASSERT(xListener->m_bListenerCalled);

    xListener->m_bListenerCalled = false;
    xRefreshable->removeRefreshListener(uno::Reference<util::XRefreshListener>(xListener.get()));
    xRefreshable->refresh();
    CPPUNIT_ASSERT(!xListener->m_bListenerCalled);
}

} // namespace apitest
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */