calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / linguistic / qa / restprotocol.cxx
blob61c04185c757f27ee94037e7ab47b451b6066579
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 #include <sal/config.h>
12 #include <algorithm>
13 #include <cassert>
14 #include <cstring>
16 #include <sal/log.hxx>
17 #include <rtl/ustrbuf.hxx>
18 #include <rtl/strbuf.hxx>
19 #include <osl/socket.hxx>
20 #include <osl/thread.hxx>
21 #include <svtools/languagetoolcfg.hxx>
22 #include <unotest/bootstrapfixturebase.hxx>
24 #include <com/sun/star/beans/PropertyValue.hpp>
25 #include <com/sun/star/connection/XAcceptor.hpp>
26 #include <com/sun/star/connection/XConnector.hpp>
27 #include <com/sun/star/linguistic2/XProofreader.hpp>
28 #include <com/sun/star/linguistic2/ProofreadingResult.hpp>
30 using namespace ::com::sun::star::uno;
32 namespace
34 class MockServerThread : public ::osl::Thread
36 public:
37 MockServerThread()
38 : m_aSocketAddr("localhost", 2022)
42 virtual void SAL_CALL run()
44 if (m_aAcceptorSocket.acceptConnection(m_aStreamSocket) != osl_Socket_Ok)
46 return;
49 sal_Int32 nReadBytes;
50 Sequence<sal_Int8> aBuffer(512);
51 sal_Int32 nTcpNoDelay = sal_Int32(true);
52 m_aStreamSocket.setOption(osl_Socket_OptionTcpNoDelay, &nTcpNoDelay, sizeof(nTcpNoDelay),
53 osl_Socket_LevelTcp);
55 nReadBytes = m_aStreamSocket.recv(aBuffer.getArray(), aBuffer.getLength());
56 if (nReadBytes)
58 std::string aText(reinterpret_cast<const char*>(aBuffer.getConstArray()), nReadBytes);
60 if (aText.find("POST /api/check") == std::string::npos)
62 NotFound();
64 else if (aText.find("Content-Type: application/json") == std::string::npos)
66 NotFound();
68 else
70 ResponseOK();
75 void ResponseOK()
77 OString aResponse(
78 "HTTP/1.1 200 OK\r\n"
79 "Server: MockServer\r\n"
80 "Cache-Control: no-cache\r\n"
81 "Content-Type: application/json\r\n"
82 "\r\n"
83 "{\"check-positions\":[{\"offset\":15,\"length\":6,\"errorcode\":4711,\"type\":"
84 "\"orth\","
85 "\"severity\":1,\"proposals\":[\"Entwurf\",\"Entw\u00fcrfe\"]},"
86 "{\"offset\":22,\"length\":3,\"errorcode\":8221,\"type\":\"orth\",\"severity\":1}]}");
88 m_aStreamSocket.write(aResponse.getStr(), aResponse.getLength());
89 m_aStreamSocket.close();
92 void NotFound()
94 OString aResponse("HTTP/1.1 404 Not Found\r\n"
95 "Connection: Closed\r\n"
96 "\r\n");
98 m_aStreamSocket.write(aResponse.getStr(), aResponse.getLength());
99 m_aStreamSocket.close();
102 void stop()
104 m_aAcceptorSocket.close();
105 join();
108 void init()
110 m_aAcceptorSocket.setOption(osl_Socket_OptionReuseAddr, 1);
111 CPPUNIT_ASSERT(m_aAcceptorSocket.bind(m_aSocketAddr));
112 CPPUNIT_ASSERT(m_aAcceptorSocket.listen());
115 private:
116 ::osl::SocketAddr m_aSocketAddr;
117 ::osl::AcceptorSocket m_aAcceptorSocket;
118 ::osl::StreamSocket m_aStreamSocket;
122 MockServerThread aMockServer;
124 class TestRestProtocol : public test::BootstrapFixtureBase
126 public:
127 virtual void setUp() override;
128 virtual void tearDown() override;
130 private:
131 CPPUNIT_TEST_SUITE(TestRestProtocol);
132 CPPUNIT_TEST(testProofreading);
133 CPPUNIT_TEST_SUITE_END();
135 void testProofreading();
138 void TestRestProtocol::testProofreading()
140 css::lang::Locale aLocale("en", "US", "");
141 Sequence<::com::sun::star::beans::PropertyValue> aProperties;
142 SvxLanguageToolOptions& rLanguageOpts = SvxLanguageToolOptions::Get();
143 rLanguageOpts.setBaseURL("http://127.0.0.1:2022/api");
144 rLanguageOpts.setUsername("hcastro");
145 rLanguageOpts.setApiKey("hcvhcvhcv");
146 rLanguageOpts.setEnabled(true);
147 rLanguageOpts.setSSLVerification(false);
148 rLanguageOpts.setRestProtocol("duden");
149 CPPUNIT_ASSERT_EQUAL(OUString("duden"), rLanguageOpts.getRestProtocol());
151 Reference<::com::sun::star::linguistic2::XProofreader> xProofreader(
152 m_xSFactory->createInstance("com.sun.star.linguistic2.Proofreader"), UNO_QUERY);
153 CPPUNIT_ASSERT(xProofreader.is());
155 com::sun::star::linguistic2::ProofreadingResult aResult
156 = xProofreader->doProofreading("id", "ths is a tst", aLocale, 0, 0, aProperties);
158 CPPUNIT_ASSERT_EQUAL(2, aResult.aErrors.getLength());
161 void TestRestProtocol::setUp()
163 test::BootstrapFixtureBase::setUp();
165 aMockServer.init();
166 aMockServer.create();
167 osl::Thread::wait(std::chrono::seconds(1));
170 void TestRestProtocol::tearDown()
172 aMockServer.stop();
174 test::BootstrapFixtureBase::tearDown();
177 CPPUNIT_TEST_SUITE_REGISTRATION(TestRestProtocol);
179 CPPUNIT_PLUGIN_IMPLEMENT();
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */