Files
libreoffice/sd/qa/unit/tiledrendering/LOKitSearchTest.cxx

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

409 lines
15 KiB
C++
Raw Normal View History

/* -*- 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 "../sdmodeltestbase.hxx"
#include "CallbackRecorder.hxx"
#include <test/bootstrapfixture.hxx>
#include <test/helper/transferable.hxx>
#include <test/xmltesttools.hxx>
#include <LibreOfficeKit/LibreOfficeKitEnums.h>
#include <comphelper/dispatchcommand.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/propertysequence.hxx>
#include <comphelper/string.hxx>
#include <comphelper/lok.hxx>
#include <sfx2/dispatch.hxx>
#include <sfx2/viewfrm.hxx>
#include <svl/srchitem.hxx>
#include <svl/stritem.hxx>
#include <vcl/scheduler.hxx>
#include <ViewShellBase.hxx>
#include <ViewShell.hxx>
#include <unomodel.hxx>
#include <sdpage.hxx>
#include <svx/svdograf.hxx>
#include <com/sun/star/frame/Desktop.hpp>
using namespace css;
class LOKitSearchTest : public SdModelTestBase, public XmlTestTools
{
private:
static constexpr char DATA_DIRECTORY[] = "/sd/qa/unit/tiledrendering/data/";
public:
LOKitSearchTest() = default;
virtual void setUp() override;
virtual void tearDown() override;
void testSearch();
void testSearchAll();
void testSearchAllSelections();
void testSearchAllNotifications();
void testSearchAllFollowedBySearch();
void testDontSearchInMasterPages();
void testSearchInPDFNonExisting();
void testSearchInPDF();
void testSearchInMixedObject();
CPPUNIT_TEST_SUITE(LOKitSearchTest);
CPPUNIT_TEST(testSearch);
CPPUNIT_TEST(testSearchAll);
CPPUNIT_TEST(testSearchAllSelections);
CPPUNIT_TEST(testSearchAllNotifications);
CPPUNIT_TEST(testSearchAllFollowedBySearch);
CPPUNIT_TEST(testDontSearchInMasterPages);
CPPUNIT_TEST(testSearchInPDFNonExisting);
CPPUNIT_TEST(testSearchInPDF);
CPPUNIT_TEST(testSearchInMixedObject);
CPPUNIT_TEST_SUITE_END();
private:
SdXImpressDocument* createDoc(const char* pName,
const uno::Sequence<beans::PropertyValue>& rArguments
= uno::Sequence<beans::PropertyValue>());
uno::Reference<lang::XComponent> mxComponent;
std::unique_ptr<CallbackRecorder> mpCallbackRecorder;
};
void LOKitSearchTest::setUp()
{
test::BootstrapFixture::setUp();
// prevent showing warning message box
setenv("OOX_NO_SMARTART_WARNING", "1", 1);
comphelper::LibreOfficeKit::setActive(true);
mxDesktop.set(
css::frame::Desktop::create(comphelper::getComponentContext(getMultiServiceFactory())));
mpCallbackRecorder = std::make_unique<CallbackRecorder>();
}
void LOKitSearchTest::tearDown()
{
if (mxComponent.is())
mxComponent->dispose();
comphelper::LibreOfficeKit::setActive(false);
test::BootstrapFixture::tearDown();
}
SdXImpressDocument*
LOKitSearchTest::createDoc(const char* pName, const uno::Sequence<beans::PropertyValue>& rArguments)
{
if (mxComponent.is())
mxComponent->dispose();
mxComponent = loadFromDesktop(m_directories.getURLFromSrc(DATA_DIRECTORY)
+ OUString::createFromAscii(pName));
CPPUNIT_ASSERT(mxComponent.is());
SdXImpressDocument* pImpressDocument = dynamic_cast<SdXImpressDocument*>(mxComponent.get());
CPPUNIT_ASSERT(pImpressDocument);
pImpressDocument->initializeForTiledRendering(rArguments);
return pImpressDocument;
}
namespace
{
void lcl_search(const OUString& rKey, bool bFindAll = false)
{
Scheduler::ProcessEventsToIdle();
SvxSearchCmd eSearch = bFindAll ? SvxSearchCmd::FIND_ALL : SvxSearchCmd::FIND;
uno::Sequence<beans::PropertyValue> aPropertyValues(comphelper::InitPropertySequence({
{ "SearchItem.SearchString", uno::makeAny(rKey) },
{ "SearchItem.Backward", uno::makeAny(false) },
{ "SearchItem.Command", uno::makeAny(sal_uInt16(eSearch)) },
}));
comphelper::dispatchCommand(".uno:ExecuteSearch", aPropertyValues);
Scheduler::ProcessEventsToIdle();
}
} // end anonymous namespace
void LOKitSearchTest::testSearch()
{
SdXImpressDocument* pXImpressDocument = createDoc("dummy.odp");
sd::ViewShell* pViewShell = pXImpressDocument->GetDocShell()->GetViewShell();
mpCallbackRecorder->registerCallbacksFor(pViewShell->GetViewShellBase());
uno::Reference<container::XIndexAccess> xDrawPage(
pXImpressDocument->getDrawPages()->getByIndex(0), uno::UNO_QUERY);
uno::Reference<text::XTextRange> xShape(xDrawPage->getByIndex(0), uno::UNO_QUERY);
xShape->setString("Aaa bbb.");
lcl_search("bbb");
SdrView* pView = pViewShell->GetView();
EditView& rEditView = pView->GetTextEditOutlinerView()->GetEditView();
// Did we indeed manage to select the second word?
CPPUNIT_ASSERT_EQUAL(OUString("bbb"), rEditView.GetSelected());
// Did the selection callback fire?
CPPUNIT_ASSERT_EQUAL(static_cast<std::size_t>(1), mpCallbackRecorder->m_aSelection.size());
// Search for something on the second slide, and make sure that the set-part callback fired.
lcl_search("bbb");
CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), mpCallbackRecorder->m_nPart);
CPPUNIT_ASSERT_EQUAL(true, mpCallbackRecorder->m_bFound);
// This was 0; should be 1 match for "find".
CPPUNIT_ASSERT_EQUAL(static_cast<std::size_t>(1),
mpCallbackRecorder->m_aSearchResultSelection.size());
// Result is on the second slide.
CPPUNIT_ASSERT_EQUAL(1, mpCallbackRecorder->m_aSearchResultPart[0]);
// This should trigger the not-found callback.
lcl_search("ccc");
CPPUNIT_ASSERT_EQUAL(false, mpCallbackRecorder->m_bFound);
}
void LOKitSearchTest::testSearchAll()
{
SdXImpressDocument* pXImpressDocument = createDoc("search-all.odp");
sd::ViewShell* pViewShell = pXImpressDocument->GetDocShell()->GetViewShell();
mpCallbackRecorder->registerCallbacksFor(pViewShell->GetViewShellBase());
lcl_search("match", /*bFindAll=*/true);
// This was empty: find-all did not highlight the first match.
CPPUNIT_ASSERT_EQUAL(OString("match"),
apitest::helper::transferable::getTextSelection(
pXImpressDocument->getSelection(), "text/plain;charset=utf-8"));
// We're on the first slide, search for something on the second slide and make sure we get a SET_PART.
mpCallbackRecorder->m_nPart = 0;
lcl_search("second", /*bFindAll=*/true);
// This was 0: no SET_PART was emitted.
CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), mpCallbackRecorder->m_nPart);
}
void LOKitSearchTest::testSearchAllSelections()
{
SdXImpressDocument* pXImpressDocument = createDoc("search-all.odp");
sd::ViewShell* pViewShell = pXImpressDocument->GetDocShell()->GetViewShell();
mpCallbackRecorder->registerCallbacksFor(pViewShell->GetViewShellBase());
lcl_search("third", /*bFindAll=*/true);
// Make sure this is found on the 3rd slide.
CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), mpCallbackRecorder->m_nPart);
// This was 1: only the first match was highlighted.
CPPUNIT_ASSERT_EQUAL(static_cast<std::size_t>(2), mpCallbackRecorder->m_aSelection.size());
}
void LOKitSearchTest::testSearchAllNotifications()
{
SdXImpressDocument* pXImpressDocument = createDoc("search-all.odp");
sd::ViewShell* pViewShell = pXImpressDocument->GetDocShell()->GetViewShell();
mpCallbackRecorder->registerCallbacksFor(pViewShell->GetViewShellBase());
lcl_search("third", /*bFindAll=*/true);
// Make sure that we get no notifications about selection changes during search.
CPPUNIT_ASSERT_EQUAL(0, mpCallbackRecorder->m_nSelectionBeforeSearchResult);
// But we do get the selection of the first hit.
CPPUNIT_ASSERT(mpCallbackRecorder->m_nSelectionAfterSearchResult > 0);
}
void LOKitSearchTest::testSearchAllFollowedBySearch()
{
SdXImpressDocument* pXImpressDocument = createDoc("search-all.odp");
sd::ViewShell* pViewShell = pXImpressDocument->GetDocShell()->GetViewShell();
mpCallbackRecorder->registerCallbacksFor(pViewShell->GetViewShellBase());
lcl_search("third", /*bFindAll=*/true);
lcl_search("match" /*,bFindAll=false*/);
// This used to give wrong result: 'search' after 'search all' still
// returned 'third'
CPPUNIT_ASSERT_EQUAL(OString("match"),
apitest::helper::transferable::getTextSelection(
pXImpressDocument->getSelection(), "text/plain;charset=utf-8"));
}
void LOKitSearchTest::testDontSearchInMasterPages()
{
SdXImpressDocument* pXImpressDocument = createDoc("dummy.odp");
sd::ViewShell* pViewShell = pXImpressDocument->GetDocShell()->GetViewShell();
mpCallbackRecorder->registerCallbacksFor(pViewShell->GetViewShellBase());
// This should trigger the not-found callback ("date" is present only on
// the master page)
lcl_search("date");
CPPUNIT_ASSERT_EQUAL(false, mpCallbackRecorder->m_bFound);
}
void LOKitSearchTest::testSearchInPDFNonExisting()
{
SdXImpressDocument* pXImpressDocument = createDoc("PDFSearch.pdf");
sd::ViewShell* pViewShell = pXImpressDocument->GetDocShell()->GetViewShell();
CPPUNIT_ASSERT(pViewShell);
mpCallbackRecorder->registerCallbacksFor(pViewShell->GetViewShellBase());
SdPage* pPage = pViewShell->GetActualPage();
CPPUNIT_ASSERT(pPage);
SdrObject* pObject = pPage->GetObj(0);
CPPUNIT_ASSERT(pObject);
SdrGrafObj* pGraphicObject = dynamic_cast<SdrGrafObj*>(pObject);
CPPUNIT_ASSERT(pGraphicObject);
Graphic aGraphic = pGraphicObject->GetGraphic();
auto const& pVectorGraphicData = aGraphic.getVectorGraphicData();
CPPUNIT_ASSERT(pVectorGraphicData);
CPPUNIT_ASSERT_EQUAL(VectorGraphicDataType::Pdf,
pVectorGraphicData->getVectorGraphicDataType());
lcl_search("NonExisting");
CPPUNIT_ASSERT_EQUAL(false, mpCallbackRecorder->m_bFound);
}
void LOKitSearchTest::testSearchInPDF()
{
SdXImpressDocument* pXImpressDocument = createDoc("PDFSearch.pdf");
sd::ViewShell* pViewShell = pXImpressDocument->GetDocShell()->GetViewShell();
CPPUNIT_ASSERT(pViewShell);
mpCallbackRecorder->registerCallbacksFor(pViewShell->GetViewShellBase());
SdPage* pPage = pViewShell->GetActualPage();
CPPUNIT_ASSERT(pPage);
SdrObject* pObject = pPage->GetObj(0);
CPPUNIT_ASSERT(pObject);
SdrGrafObj* pGraphicObject = dynamic_cast<SdrGrafObj*>(pObject);
CPPUNIT_ASSERT(pGraphicObject);
Graphic aGraphic = pGraphicObject->GetGraphic();
auto const& pVectorGraphicData = aGraphic.getVectorGraphicData();
CPPUNIT_ASSERT(pVectorGraphicData);
CPPUNIT_ASSERT_EQUAL(VectorGraphicDataType::Pdf,
pVectorGraphicData->getVectorGraphicDataType());
// Search
lcl_search("ABC");
CPPUNIT_ASSERT_EQUAL(true, mpCallbackRecorder->m_bFound);
CPPUNIT_ASSERT_EQUAL(1, mpCallbackRecorder->m_nSearchResultCount);
CPPUNIT_ASSERT_EQUAL(size_t(1), mpCallbackRecorder->m_aSearchResultSelection.size());
CPPUNIT_ASSERT_EQUAL(OString("3763, 1331, 1432, 483"),
mpCallbackRecorder->m_aSearchResultSelection[0]);
CPPUNIT_ASSERT_EQUAL(tools::Rectangle(Point(3763, 1331), Size(1433, 484)),
mpCallbackRecorder->m_aSelection[0]);
// Search again - same result
lcl_search("ABC");
CPPUNIT_ASSERT_EQUAL(true, mpCallbackRecorder->m_bFound);
CPPUNIT_ASSERT_EQUAL(2, mpCallbackRecorder->m_nSearchResultCount);
CPPUNIT_ASSERT_EQUAL(size_t(1), mpCallbackRecorder->m_aSearchResultSelection.size());
CPPUNIT_ASSERT_EQUAL(OString("3763, 1331, 1432, 483"),
mpCallbackRecorder->m_aSearchResultSelection[0]);
CPPUNIT_ASSERT_EQUAL(tools::Rectangle(Point(3763, 1331), Size(1433, 484)),
mpCallbackRecorder->m_aSelection[0]);
}
void LOKitSearchTest::testSearchInMixedObject()
{
SdXImpressDocument* pXImpressDocument = createDoc("MixedTest1.odg");
sd::ViewShell* pViewShell = pXImpressDocument->GetDocShell()->GetViewShell();
CPPUNIT_ASSERT(pViewShell);
SdDrawDocument* pDocument = pXImpressDocument->GetDocShell()->GetDoc();
CPPUNIT_ASSERT(pDocument);
mpCallbackRecorder->registerCallbacksFor(pViewShell->GetViewShellBase());
// Check we have one page
CPPUNIT_ASSERT_EQUAL(sal_uInt16(1), pDocument->GetSdPageCount(PageKind::Standard));
SdPage* pPage = pViewShell->GetActualPage();
CPPUNIT_ASSERT(pPage);
// Check page hase 2 objects only
CPPUNIT_ASSERT_EQUAL(size_t(2), pPage->GetObjCount());
// Check Object 1
{
SdrObject* pObject = pPage->GetObj(0);
CPPUNIT_ASSERT(pObject);
CPPUNIT_ASSERT_EQUAL(sal_uInt16(OBJ_TEXT), pObject->GetObjIdentifier());
}
// Check Object 2
{
SdrObject* pObject = pPage->GetObj(1);
CPPUNIT_ASSERT(pObject);
CPPUNIT_ASSERT_EQUAL(sal_uInt16(OBJ_GRAF), pObject->GetObjIdentifier());
SdrGrafObj* pGraphicObject = dynamic_cast<SdrGrafObj*>(pObject);
CPPUNIT_ASSERT(pGraphicObject);
Graphic aGraphic = pGraphicObject->GetGraphic();
auto const& pVectorGraphicData = aGraphic.getVectorGraphicData();
CPPUNIT_ASSERT(pVectorGraphicData);
CPPUNIT_ASSERT_EQUAL(VectorGraphicDataType::Pdf,
pVectorGraphicData->getVectorGraphicDataType());
}
// Let's try to search now
lcl_search("ABC");
CPPUNIT_ASSERT_EQUAL(true, mpCallbackRecorder->m_bFound);
CPPUNIT_ASSERT_EQUAL(1, mpCallbackRecorder->m_nSearchResultCount);
CPPUNIT_ASSERT_EQUAL(size_t(1), mpCallbackRecorder->m_aSearchResultSelection.size());
CPPUNIT_ASSERT_EQUAL(size_t(1), mpCallbackRecorder->m_aSearchResultPart.size());
CPPUNIT_ASSERT_EQUAL(OString("3546, 3174, 738, 402"),
mpCallbackRecorder->m_aSearchResultSelection[0]);
// Search next
lcl_search("ABC");
CPPUNIT_ASSERT_EQUAL(true, mpCallbackRecorder->m_bFound);
CPPUNIT_ASSERT_EQUAL(2, mpCallbackRecorder->m_nSearchResultCount);
CPPUNIT_ASSERT_EQUAL(size_t(1), mpCallbackRecorder->m_aSearchResultSelection.size());
CPPUNIT_ASSERT_EQUAL(size_t(1), mpCallbackRecorder->m_aSearchResultPart.size());
CPPUNIT_ASSERT_EQUAL(OString("8412, 6385, 519, 174"),
mpCallbackRecorder->m_aSearchResultSelection[0]);
// Search next again - we should get the first object again
lcl_search("ABC");
CPPUNIT_ASSERT_EQUAL(true, mpCallbackRecorder->m_bFound);
CPPUNIT_ASSERT_EQUAL(3, mpCallbackRecorder->m_nSearchResultCount);
CPPUNIT_ASSERT_EQUAL(size_t(1), mpCallbackRecorder->m_aSearchResultSelection.size());
CPPUNIT_ASSERT_EQUAL(size_t(1), mpCallbackRecorder->m_aSearchResultPart.size());
CPPUNIT_ASSERT_EQUAL(OString("3546, 3174, 738, 402"),
mpCallbackRecorder->m_aSearchResultSelection[0]);
}
CPPUNIT_TEST_SUITE_REGISTRATION(LOKitSearchTest);
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */