1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
12 #include <cppunit/TestAssert.h>
14 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
15 #include <boost/property_tree/json_parser.hpp>
16 #include <comphelper/string.hxx>
17 #include <osl/conditn.hxx>
18 #include <sfx2/viewsh.hxx>
19 #include <test/lokcallback.hxx>
20 #include <o3tl/string_view.hxx>
26 std::vector
<OUString
> lcl_convertSeparated(std::u16string_view rString
, sal_Unicode nSeparator
)
28 std::vector
<OUString
> aRet
;
33 OUString
aToken(o3tl::trim(o3tl::getToken(rString
, 0, nSeparator
, nIndex
)));
34 if (!aToken
.isEmpty())
35 aRet
.push_back(aToken
);
36 } while (nIndex
>= 0);
41 void lcl_convertRectangle(std::u16string_view rString
, tools::Rectangle
& rRectangle
)
43 uno::Sequence
<OUString
> aSeq
= comphelper::string::convertCommaSeparated(rString
);
44 CPPUNIT_ASSERT(aSeq
.getLength() == 4 || aSeq
.getLength() == 5);
45 rRectangle
.SetLeft(aSeq
[0].toInt32());
46 rRectangle
.SetTop(aSeq
[1].toInt32());
47 rRectangle
.setWidth(aSeq
[2].toInt32());
48 rRectangle
.setHeight(aSeq
[3].toInt32());
52 struct CallbackRecorder
57 , m_nSelectionBeforeSearchResult(0)
58 , m_nSelectionAfterSearchResult(0)
59 , m_nSearchResultCount(0)
60 , m_callbackWrapper(&callback
, this)
64 tools::Rectangle m_aInvalidation
;
65 std::vector
<::tools::Rectangle
> m_aSelection
;
68 std::vector
<OString
> m_aSearchResultSelection
;
69 std::vector
<int> m_aSearchResultPart
;
70 int m_nSelectionBeforeSearchResult
;
71 int m_nSelectionAfterSearchResult
;
72 int m_nSearchResultCount
;
73 /// For document size changed callback.
74 osl::Condition m_aDocumentSizeCondition
;
75 TestLokCallbackWrapper m_callbackWrapper
;
77 static void callback(int nType
, const char* pPayload
, void* pData
)
79 static_cast<CallbackRecorder
*>(pData
)->processCallback(nType
, pPayload
);
82 void processCallback(int nType
, const char* pPayload
)
86 case LOK_CALLBACK_INVALIDATE_TILES
:
88 OUString aPayload
= OUString::createFromAscii(pPayload
);
89 if (aPayload
!= "EMPTY" && m_aInvalidation
.IsEmpty())
90 lcl_convertRectangle(aPayload
, m_aInvalidation
);
93 case LOK_CALLBACK_TEXT_SELECTION
:
95 OUString aPayload
= OUString::createFromAscii(pPayload
);
97 for (const OUString
& rString
: lcl_convertSeparated(aPayload
, u
';'))
99 ::tools::Rectangle aRectangle
;
100 lcl_convertRectangle(rString
, aRectangle
);
101 m_aSelection
.push_back(aRectangle
);
103 if (m_aSearchResultSelection
.empty())
104 ++m_nSelectionBeforeSearchResult
;
106 ++m_nSelectionAfterSearchResult
;
109 case LOK_CALLBACK_SEARCH_NOT_FOUND
:
114 case LOK_CALLBACK_DOCUMENT_SIZE_CHANGED
:
116 m_aDocumentSizeCondition
.set();
119 case LOK_CALLBACK_SET_PART
:
121 OUString aPayload
= OUString::createFromAscii(pPayload
);
122 m_nPart
= aPayload
.toInt32();
125 case LOK_CALLBACK_SEARCH_RESULT_SELECTION
:
127 m_nSearchResultCount
++;
128 m_aSearchResultSelection
.clear();
129 m_aSearchResultPart
.clear();
130 boost::property_tree::ptree aTree
;
131 std::stringstream
aStream(pPayload
);
132 boost::property_tree::read_json(aStream
, aTree
);
133 for (const boost::property_tree::ptree::value_type
& rValue
:
134 aTree
.get_child("searchResultSelection"))
136 m_aSearchResultSelection
.emplace_back(
137 rValue
.second
.get
<std::string
>("rectangles").c_str());
138 m_aSearchResultPart
.push_back(
139 std::atoi(rValue
.second
.get
<std::string
>("part").c_str()));
146 void registerCallbacksFor(SfxViewShell
& rViewShell
)
148 rViewShell
.setLibreOfficeKitViewCallback(&m_callbackWrapper
);
152 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */