summaryrefslogtreecommitdiff
path: root/sw/qa/extras/indexing/SearchResultLocatorTest.cxx
blob: 99ee267e4e028d00b168210961cb91841185665e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* -*- 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 <memory>
#include <string_view>
#include <swmodeltestbase.hxx>
#include <docsh.hxx>
#include <unotxdoc.hxx>

#include <SearchResultLocator.hxx>

namespace
{
constexpr OUStringLiteral DATA_DIRECTORY = u"sw/qa/extras/indexing/data/";
}

class SearchResultLocatorTest : public SwModelTestBase
{
private:
    SwDoc* createDoc(const char* pName = nullptr);

public:
    void testSearchResultLocator();
    void testSearchResultLocatorForSdrObjects();

    CPPUNIT_TEST_SUITE(SearchResultLocatorTest);
    CPPUNIT_TEST(testSearchResultLocator);
    CPPUNIT_TEST(testSearchResultLocatorForSdrObjects);
    CPPUNIT_TEST_SUITE_END();
};

SwDoc* SearchResultLocatorTest::createDoc(const char* pName)
{
    if (!pName)
        loadURL("private:factory/swriter", nullptr);
    else
        load(DATA_DIRECTORY, pName);

    SwXTextDocument* pTextDoc = dynamic_cast<SwXTextDocument*>(mxComponent.get());
    CPPUNIT_ASSERT(pTextDoc);
    return pTextDoc->GetDocShell()->GetDoc();
}

void SearchResultLocatorTest::testSearchResultLocator()
{
    if (!IsDefaultDPI())
        return;

    SwDoc* pDoc = createDoc("IndexingExport_VariousParagraphs.odt");
    CPPUNIT_ASSERT(pDoc);

    sw::search::SearchResultLocator aLocator(pDoc);
    std::vector<sw::search::SearchIndexData> aDataVector;
    aDataVector.emplace_back(sw::search::NodeType::WriterNode, 14);

    sw::search::LocationResult aResult = aLocator.find(aDataVector);
    CPPUNIT_ASSERT_EQUAL(size_t(1), aResult.maRectangles.size());

    // skip asserting exact values for macOS and Windows because of
    // inconsistent results
#if !defined(_WIN32) && !defined(MACOSX)
    auto aRectangle = aResult.maRectangles[0];
    CPPUNIT_ASSERT_DOUBLES_EQUAL(1418.0, aRectangle.getMinX(), 1e-4);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(4444.0, aRectangle.getMinY(), 1e-4);

    CPPUNIT_ASSERT_DOUBLES_EQUAL(9638.0, aRectangle.getWidth(), 1e-4);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(276.0, aRectangle.getHeight(), 1e-4);
#endif
}

void SearchResultLocatorTest::testSearchResultLocatorForSdrObjects()
{
    if (!IsDefaultDPI())
        return;

    SwDoc* pDoc = createDoc("IndexingExport_Shapes.odt");
    CPPUNIT_ASSERT(pDoc);

    sw::search::SearchResultLocator aLocator(pDoc);
    std::vector<sw::search::SearchIndexData> aDataVector;
    aDataVector.emplace_back(sw::search::NodeType::SdrObject, 1, u"Circle");

    sw::search::LocationResult aResult = aLocator.find(aDataVector);
    CPPUNIT_ASSERT_EQUAL(size_t(1), aResult.maRectangles.size());

    // skip asserting exact values for macOS and Windows because of
    // inconsistent results
#if !defined(_WIN32) && !defined(MACOSX)
    auto aRectangle = aResult.maRectangles[0];
    CPPUNIT_ASSERT_DOUBLES_EQUAL(1478.0, aRectangle.getMinX(), 1e-4);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(3223.0, aRectangle.getMinY(), 1e-4);

    CPPUNIT_ASSERT_DOUBLES_EQUAL(2059.0, aRectangle.getWidth(), 1e-4);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(2059.0, aRectangle.getHeight(), 1e-4);
#endif
}

CPPUNIT_TEST_SUITE_REGISTRATION(SearchResultLocatorTest);

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */