Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sd / qa / unit / tiledrendering / CallbackRecorder.hxx
blobad64f5ab3924d0a1d7d03b82f7f203f70727ab6f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #pragma once
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>
22 using namespace css;
24 namespace
26 std::vector<OUString> lcl_convertSeparated(std::u16string_view rString, sal_Unicode nSeparator)
28 std::vector<OUString> aRet;
30 sal_Int32 nIndex = 0;
33 OUString aToken(o3tl::trim(o3tl::getToken(rString, 0, nSeparator, nIndex)));
34 if (!aToken.isEmpty())
35 aRet.push_back(aToken);
36 } while (nIndex >= 0);
38 return aRet;
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
54 CallbackRecorder()
55 : m_bFound(true)
56 , m_nPart(0)
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;
66 bool m_bFound;
67 sal_Int32 m_nPart;
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)
84 switch (nType)
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);
92 break;
93 case LOK_CALLBACK_TEXT_SELECTION:
95 OUString aPayload = OUString::createFromAscii(pPayload);
96 m_aSelection.clear();
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;
105 else
106 ++m_nSelectionAfterSearchResult;
108 break;
109 case LOK_CALLBACK_SEARCH_NOT_FOUND:
111 m_bFound = false;
113 break;
114 case LOK_CALLBACK_DOCUMENT_SIZE_CHANGED:
116 m_aDocumentSizeCondition.set();
118 break;
119 case LOK_CALLBACK_SET_PART:
121 OUString aPayload = OUString::createFromAscii(pPayload);
122 m_nPart = aPayload.toInt32();
124 break;
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()));
142 break;
146 void registerCallbacksFor(SfxViewShell& rViewShell)
148 rViewShell.setLibreOfficeKitViewCallback(&m_callbackWrapper);
152 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */